如何搜索保存为变量的完整组织标题 [英] How to search for a complete org headline that was saved as a variable

查看:174
本文介绍了如何搜索保存为变量的完整组织标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数(org-heading-components)(org-element-property)为星号数也优先。我想将整个标题存储为变量,然后使用重新搜索转发(或类似的功能)返回到那个标题,但是我预见到当找不到整数时会发生的问题。我需要将整个标题存储为一个变量,因为我经常有要重复的标题的条目,但其他组件是不同的。



例如,以下todo: / p>

  **活动[#A]询问stackoverflow上的天才如何做到这一点。 :当用(org-heading-components)

code>看起来像这样:

 (2 2活动65询问stackoverflow上的天才如何做到:lawlist:)

所以,当将它作为一个变量存储, c $ c> re-search-forward 会出现问题,因为 2 2 ** 65 [#A] 不同。

 (defun lawlist()
(interactive)
(let *(
(点))
(完整标题(组织标题组件)))
* * *
(goto-char(point-min))
(重新搜索 - 完成标题nil t)))


解决方案

strong> EDIT (2013年12月15日):根据变量 org-heading-regexp (在 org.el中定义)更新解决方案)和其修改包括(如果存在)包含截止日期的第二行 - 即 lawlist-org-heading-regexp 。修订版还包括一个刚刚通过@Drew超级用户教给我的一个漂亮的函数 regexp-quote https://superuser.com/questions/688781/how-to-highlight- string-and-unhighlight-string-in-buffer-make-overlay?noredirect = 1#comment874515_688781 (buffer-substring-no-properties beg end)是用于将字符串设置为变量。



编辑(2013年12月17日):添加 isearch-highlight isearch-dehighlight ,并注释掉 highlight-regexp unhighlight-正则表达式。当使用更复杂的函数移动点时, highlight-regexp 不能可靠地突出显示整个字符串 - 这可能是因为屏幕没有刷新,或者也可能是由其他因素引起的 - 例如,hl-line-mode等) - 放置各种 sit-for 0 没有解决问题与 highlight-regexp - isearch-highlight 更好地工作。



编辑(2014年1月6日):另请参阅这个相关的线程,获取完整的正则表达式,以匹配从星号到笔记末尾的整个todo的任何元素:
(defvar lawlist-org-heading-regexp
^ \\(\\ * + \\)\\(? + \\(。*?\\)\\)?[\t] * \\(\\\
。* DEADLINE。* $ \\)
匹配标题,加上seco )

(defun example()
(interactive)
(switch-to-buffer(get-buffer-createfoo))
(组织模式)
(插入* Example\\\
\\\

(插入**活动[#A]这是一个活动的todo。 :b)b $ b(插入DEADLINE:< 2013-12-15 Sun 08:00> SCHEDULED:< 2013-12-15 Sun> \\\
\\\

(插入**下一个动作[#B]这是一个不活动的待办事项:法律清单:\\\

(插入DEADLINE:< 2013-12-16 Mon 08:00&SCH :$ {
(goto-char(point-min))
(sit-for 2)
(re-search-forward(regexp-quote ** Active [#A]))
(sit-for 2)
(let((init-pos(point)))
(org-back-to-heading t )
(let $(
lawlist-item-whole
lawlist-item-partial
(beg(point)))
(if(and
(查看org-heading-regexp)
(和(查找lawlist-org-heading-regexp)(match-string 3)))
(re-search-forward lawlist-org-标题-regexp nil t)
(re-search-forward org-heading-regexp nil t))
(let((end(point)))
(setq lawlist-item-whole (buffer-substring-no-properties乞求结束)
(setq lawlist-item-partial(buffer-substring-no-properties beg init-pos))
(re-search-backward(regexp-quote lawlist-item-whole)nil t)
;; (highlight-regexp(regexp-quote lawlist-item-whole))
(isearch-highlight beg end)
(sit-for 2)
;; (unhighlight-regexp(regexp-quote lawlist-item-whole))
(isearch-dehighlight)
(re-search-forward(regexp-quote lawlist-item-partial)nil t)
(sit-for 2)
(kill-bufferfoo)))))



< hr>

编辑(2013年10月27日):作为进化过程的历史部分暂时保留的最终解决方案。但是,它不再是首选方法。

 (defun lawlist-org-heading-components()
org-back-to-heading t)
(if(let(case-fold-search)(look-in org-complex-heading-regexp))
(concat
(cond
((等于(org-match-string-no-properties 1)**)
^ [*] [*])
((等于(org-match-string - 不属性1)*)
^ [*]))
(cond
((和(match-end 2))(aref(match-string 2)1 )
(concat(org-match-string-no-properties 2))))
(cond
((和(match-end 3))(aref(match-string 3)
(concat\\(org-match-string-no-properties 3))))
(cond
((和(match-end 4 )(aref(match-string 4)3))
(concat(org-match-string-no-properties 4))))
(cond
((和(match -end 5)(aref(match-string 5)4))
(concat(org-match-string-no-properties 5))))))))


The function (org-heading-components) and (org-element-property) produce integers for the number of stars and also for the priority. I'd like to store the entire headline as a variable and then use re-search-forward (or a similar function) to go back to that heading, but I foresee the problem that will occur when it cannot find an integer. I need to store the whole heading as a variable, because I often have todo entries with duplicate titles but the other components are diferent.

For example, the following todo:

** Active [#A] Ask the geniuses on stackoverflow how to do this.  :lawlist:

when evaluated with (org-heading-components) looks like this:

(2 2 "Active" 65 "Ask the geniuses on stackoverflow how to do this." ":lawlist:")

So, when storing that as a variable and later using re-search-forward there will be problems because 2 2 is not the same as **, and 65 is not the same as [#A].

(defun lawlist ()
(interactive)
  (let* (
        (beg (point))
        (complete-heading (org-heading-components) ))
  * * *
  (goto-char (point-min))
  (re-search-forward complete-heading nil t) ))

解决方案

EDIT (December 15, 2013):  Updated solution based upon the variable org-heading-regexp (defined within org.el) and a modification thereof to include (if it exists) a second line containing a deadline - i.e., lawlist-org-heading-regexp. The revision also includes a nifty function regexp-quote that was just taught to me by @Drew over on superuser: https://superuser.com/questions/688781/how-to-highlight-string-and-unhighlight-string-in-buffer-make-overlay?noredirect=1#comment874515_688781  (buffer-substring-no-properties beg end) is used to set the string as a variable.

EDIT (December 17, 2013):   Added isearch-highlight and isearch-dehighlight, and commented out highlight-regexp and unhighlight-regexp. When moving the point around with more complex functions, highlight-regexp does not reliably highlight the entire string -- this may be because the screen has not refreshed, or it may also be caused by other factors -- e.g., hl-line-mode, etc.) -- placing various sit-for 0 did not fix the issue with highlight-regexp -- isearch-highlight works better.

EDIT (January 6, 2014):  See also this related thread for a complete regexp to match any element of the entire todo from stars through to the end of the notes:  https://stackoverflow.com/a/20960301/2112489

(require 'org)

(defvar lawlist-org-heading-regexp
  "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*\\(\n.*DEADLINE.*$\\)"
    "Match headline, plus second line with a deadline.")

(defun example ()
(interactive)
  (switch-to-buffer (get-buffer-create "foo"))
  (org-mode)
  (insert "* Example\n\n")
  (insert "** Active [#A] This is an active todo. :lawlist:\n")
  (insert "   DEADLINE: <2013-12-15 Sun 08:00>  SCHEDULED: <2013-12-15 Sun>\n\n")
  (insert "** Next-Action [#B] This is an inactive todo. :lawlist:\n")
  (insert "   DEADLINE: <2013-12-16 Mon 08:00>  SCHEDULED: <2013-12-16 Mon>")
  (goto-char (point-min))
  (sit-for 2)
  (re-search-forward (regexp-quote "** Active [#A] "))
  (sit-for 2)
  (let ((init-pos (point)))
    (org-back-to-heading t)
    (let* (
          lawlist-item-whole
          lawlist-item-partial
          (beg (point)))
      (if (and
            (looking-at org-heading-regexp)
            (and (looking-at lawlist-org-heading-regexp) (match-string 3)))
        (re-search-forward lawlist-org-heading-regexp nil t)
        (re-search-forward org-heading-regexp nil t))
      (let ((end (point)))
        (setq lawlist-item-whole (buffer-substring-no-properties beg end))
        (setq lawlist-item-partial (buffer-substring-no-properties beg init-pos))
        (re-search-backward (regexp-quote lawlist-item-whole) nil t)
        ;; (highlight-regexp (regexp-quote lawlist-item-whole))
        (isearch-highlight beg end)
        (sit-for 2)
        ;; (unhighlight-regexp (regexp-quote lawlist-item-whole))
        (isearch-dehighlight)
        (re-search-forward (regexp-quote lawlist-item-partial) nil t) 
        (sit-for 2)
        (kill-buffer "foo")))))


EDIT (October 27, 2013):  Prior solution that is being preserved temporarily as a historical part of the evolution process towards a final answer. However, it is no longer a preferred method.

(defun lawlist-org-heading-components ()
  (org-back-to-heading t)
  (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
    (concat
      (cond
        ((equal (org-match-string-no-properties 1) "**")
          "^[*][*]")
        ((equal (org-match-string-no-properties 1) "*")
          "^[*]"))
      (cond
        ((and (match-end 2) (aref (match-string 2) 1))
          (concat " " (org-match-string-no-properties 2))))
      (cond
        ((and (match-end 3) (aref (match-string 3) 2))
          (concat " \\" (org-match-string-no-properties 3))))
      (cond
        ((and (match-end 4) (aref (match-string 4) 3))
          (concat " " (org-match-string-no-properties 4))))
      (cond
        ((and (match-end 5) (aref (match-string 5) 4))
          (concat " " (org-match-string-no-properties 5)))))))

这篇关于如何搜索保存为变量的完整组织标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆