编辑(2013 年 12 月 15 日):更新了基于变量org-heading-regexp
(在 中定义org.el
)的解决方案及其修改以包括(如果存在)包含截止日期的第二行 - 即lawlist-org-heading-regexp
. 修订版还包括一个漂亮的功能regexp-quote
,@Drew 刚刚在超级用户上教给我: 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
and isearch-dehighlight
,并注释掉了highlight-regexp
and unhighlight-regexp
。当使用更复杂的函数移动点时,highlight-regexp
不能可靠地突出显示整个字符串——这可能是因为屏幕没有刷新,也可能是其他因素导致的——例如,hl-line-mode 等。 ) -- 放置各种sit-for 0
不能解决问题highlight-regexp
--isearch-highlight
效果更好。
编辑(2014 年 1 月 6 日):另请参阅此相关线程以获取完整的正则表达式,以匹配从星号到注释结尾的整个待办事项的任何元素: 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")))))
编辑(2013 年 10 月 27 日):作为最终答案演变过程的历史部分暂时保留的先前解决方案。但是,它不再是首选方法。
(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)))))))