emacs - 如何在组织模式下匹配/解析任务结束时的注释

标签 emacs elisp org-mode

我正在寻找一种与我所说的“注释”相匹配的方法,“注释”是组织任务的最后一行(或一组行),上面写着:“这些是任务的注释。


我已经编写了一个非常的正则表达式,可能可以完成这项工作,但我希望组织模式已经提供了更好的东西:

"^\\(\\*\\*\\)\\(?: +\\(Active\\|Next Action\\|Hold\\|Reference\\|Delegated\\|Postponed\\|Waiting\\|Someday\\|Planning\\|Canceled\\|None\\)\\)?\\(?: +\\(\\[#.\\]\\)\\)?\\(?: +\\(.*?\\)\\)??\\(?:[ ]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ ]*\\(?:\\(\n\\)\\)?\\(?: +\\(DEADLINE:\\|SCHEDULED:\\)\\)?\\(?: +\\(<\\)\\([^>]+\\)\\(>\\)\\)?\\(?: +\\(DEADLINE:\\|SCHEDULED:\\)\\)?\\(?: +\\(<\\)\\([^>]+\\)\\(>\\)\\)?\\(?:\\(\n\\)\\)?\\(?: +\\(CLOSED:.*\\)\\)?\\(?:\\(\n\\)\\)?\\(?: +\\(:PROPERTIES:\\)\\)?\\(?:\\(\n\\)\\)?\\(?: +\\(:ToodledoID:\\)\\)?\\(?: +\\([0-9]+\\)\\)?\\(?:\\(\n\\)\\)?\\(?: +\\(:ToodledoFolder:\\)\\)?\\(?: +\\(EVENTS\\|TASKS\\|UNDATED\\|DONE\\|CONTACT\\)\\)?\\(?:\\(\n\\)\\)\\(?: +\\(:Hash:\\)\\)?\\(?: +\\(.*+\\)\\)?\\(?:\\(\n\\)\\)?\\(?: +\\(:END:\\)\\)?\\(?:\\(\n\\)\\)?\\(.*\\(?:\n.*\\)*?\\)\\(?:^\\)"

** Delegated [#A] 0 @ Title of the task. :event:lawlist:
   DEADLINE: <2014-01-11 Sat 08:00>  SCHEDULED: <2014-01-11 Sat>
   :PROPERTIES:
   :ToodledoID: 353081871
   :ToodledoFolder: EVENTS
   :Hash: 20657e7586fcc67da708789d7bbc0fbd
   :END:
These are the notes of the task.

我发现的解析组织任务的最完整示例使用org-element-parse-buffer,但我仍然没有看到仅提取注释的关键:https://github.com/yyr/org-mode/blob/master/testing/lisp/test-org-element.el

如果可能的话,我想要类似于此想象代码(org-element-property :notes (org-element-at-point)),其中不需要缩小子树范围来提取注释。我有几百个任务,我正在寻找一种方法来快速梳理主组织文件并创建我自己的自定义议程缓冲区,以显示第一个和也许第二行(如果截止日期或已安排)和末尾的注释如果存在。我有一种感觉,如果我必须缩小到 200 个任务的子树,那么生成最终显示将会有很大的延迟。


任务的前两行已经这样处理:

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

(setq txt
  (save-excursion
    (org-back-to-heading t)
    (when (looking-at org-heading-regexp)
        (concat
          (match-string 1)
          " "
          (match-string 2)
          (if (and (looking-at lawlist-org-heading-regexp) (match-string 3))
            (match-string 3))
          "\n" )))) )

(message "%s"(org-element-map (org-element-parse-buffer) 'paragraph 'identity nil t)) 给我以下内容:

 (paragraph 
  (:begin 244 
      :end 276 
      :contents-begin 244 
      :contents-end 276 
      :post-blank 0 
      :post-affiliated 244 
      :parent (section 
           (:begin 58 
               :end 276 
               :contents-begin 58 
               :contents-end 276 
               :post-blank 0 
               :parent (headline 
                (:raw-value 0 
                        @ 
                        Title of the task. 
                        :begin 1 
                        :end 276 
                        :pre-blank 0 
                        :hiddenp nil 
                        :contents-begin 58 
                        :contents-end 276 
                        :level 2 
                        :priority 65 
                        :tags (event lawlist) 
                        :todo-keyword Delegated 
                        :todo-type todo 
                        :post-blank 0 
                        :footnote-section-p nil 
                        :archivedp nil 
                        :commentedp nil 
                        :quotedp nil 
                        :deadline (timestamp 
                               (:type active 
                                  :raw-value <2014-01-11 Sat 08:00> 
                                  :year-start 2014 
                                  :month-start 1 
                                  :day-start 11 
                                  :hour-start 8 
                                  :minute-start 0 
                                  :year-end 2014 
                                  :month-end 1 
                                  :day-end 11 
                                  :hour-end 8 
                                  :minute-end 0 
                                  :begin 71 
                                  :end 95 
                                  :post-blank 2)) 
                        :scheduled (timestamp 
                            (:type active 
                                   :raw-value <2014-01-11 Sat>
                                   :year-start 2014
                                   :month-start 1
                                   :day-start 11
                                   :hour-start nil
                                   :minute-start nil
                                   :year-end 2014
                                   :month-end 1
                                   :day-end 11
                                   :hour-end nil
                                   :minute-end nil
                                   :begin 106
                                   :end 122
                                   :post-blank 0))
                        :TOODLEDOID 353081871
                        :TOODLEDOFOLDER EVENTS
                        :HASH 20657e7586fcc67da708789d7bbc0fbd
                        :CATEGORY .scratch
                        :title (0 @ Title of the task.)
                        :parent (org-data nil #4)) #2)) 
           (planning (:closed nil
                  :deadline (timestamp 
                         (:type active
                            :raw-value <2014-01-11 Sat 08:00>
                            :year-start 2014
                            :month-start 1
                            :day-start 11
                            :hour-start 8
                            :minute-start 0
                            :year-end 2014
                            :month-end 1
                            :day-end 11
                            :hour-end 8
                            :minute-end 0
                            :begin 71
                            :end 95
                            :post-blank 2))
                  :scheduled (timestamp 
                          (:type active
                             :raw-value <2014-01-11 Sat>
                             :year-start 2014
                             :month-start 1
                             :day-start 11
                             :hour-start nil
                             :minute-start nil
                             :year-end 2014
                             :month-end 1
                             :day-end 11
                             :hour-end nil
                             :minute-end nil
                             :begin 106
                             :end 122
                             :post-blank 0))
                  :begin 58
                  :end 123
                  :post-blank 0
                  :parent #2)) 
           (property-drawer (:begin 123
                    :end 244
                    :hiddenp nil
                    :contents-begin 139
                    :contents-end 235
                    :post-blank 0
                    :post-affiliated 123
                    :parent #2) 
                (node-property (:key ToodledoID
                             :value 353081871
                             :begin 139
                             :end 165
                             :post-blank 0
                             :parent #3)) 
                (node-property (:key ToodledoFolder
                             :value EVENTS
                             :begin 165
                             :end 192
                             :post-blank 0
                             :parent #3)) 
                (node-property (:key Hash
                             :value 20657e7586fcc67da708789d7bbc0fbd
                             :begin 192
                             :end 235
                             :post-blank 0
                             :parent #3))) 
           #0))
  These are the notes of the task.)

最佳答案

编辑(2014 年 1 月 7 日):第一份工作草案。正则表达式的尾部将匹配注释,直到下一个空行 - 即,注释可能是多行,带有硬回车,并且它们之间没有空行。

编辑(2014 年 1 月 14 日):修订了 31 和 32,以便缓冲区末尾不需要最终的新行 - 即,任务末尾没有注释缓冲区现已被识别。将 20 修改为 24,以便 :ToodledoID: 抽屉和 :ToodledoFolder: 抽屉的位置可以互换。修改了示例 get-whopper 函数以排除 32。

编辑(2014 年 11 月 14 日):稍微更新了正则表达式,但功能保持不变。

编辑(2014 年 12 月 2 日):删除了自定义功能并替换为库存 org-back-to-heading

编辑(2014 年 12 月 6 日:对正则表达式进行重大修改。正则表达式现在支持大多数条目的可选项目,并且缓冲区末尾不需要最终新行。随着在新版本中,正则表达式可以在自定义 *Org Agenda* 缓冲区中使用,该缓冲区仅显示原始待办事项中的部分项目 - 例如,标题 + 截止日期/计划/关闭 + 注释;或标题 +仅注释。但是,:tag: 是强制 anchor — 即,正则表达式至少需要一 (1) 个标记才能正常工作。行结尾 — 即 \n -- 不再编号匹配条目。正则表达式现在支持捆绑在一起的待办事项条目,没有任何新行将它们分隔开。请参阅有关非捕获组的相关线程: https://stackoverflow.com/a/3513858/2112489 另请参阅有关多行正则表达式 emacs: http://www.emacswiki.org/emacs/MultilineRegexp

(defvar whopper-regexp (concat
  "^\\(\\*\\*\\)" ;; 1
  "\\(?: +\\(Active\\|Next Action\\|Hold\\|Reference\\|Delegated\\|Postponed\\|Waiting\\|Someday\\|Planning\\|Canceled\\|None\\)\\)?" ;; 2
  "\\(?: +\\(\\[#.\\]\\)\\)?" ;; 3
  "\\(?: +\\(.*?\\)\\)?" ;; 4
  "\\(?:[ ]+\\(:[[:alnum:]_@#%:]+:\\)\\)" ;; 5 ;; mandatory-anchor
  "\\(?:\n +\\(DEADLINE:\\|SCHEDULED:\\)\\)?" ;; 6
  "\\(?: +\\(<\\)\\([^>]+\\)\\(>\\)\\)?" ;; 7 | 8 | 9
  "\\(?: +\\(DEADLINE:\\|SCHEDULED:\\)\\)?" ;; 10
  "\\(?: +\\(<\\)\\([^>]+\\)\\(>\\)\\)?" ;; 11 | 12 | 13
  "\\(?:\n +\\(CLOSED:.*\\)\\)?" ;; 14
  "\\(?:\n +\\(:PROPERTIES:\\)\\)?" ;; 15
  "\\(?:\n +\\(:ToodledoID:\\|:ToodledoFolder:\\)\\)?" ;; 16
  "\\(?: +\\([0-9]+\\|EVENTS\\|TASKS\\|UNDATED\\|DONE\\|CONTACT\\)\\)?" ;; 17
  "\\(?:\n +\\(:ToodledoID:\\|:ToodledoFolder:\\)\\)?" ;; 18
  "\\(?: +\\([0-9]+\\|EVENTS\\|TASKS\\|UNDATED\\|DONE\\|CONTACT\\)\\)?" ;; 19
  "\\(?:\n +\\(:Hash:\\)\\)?" ;; 20
  "\\(?: +\\(.*\\)\\)?" ;; 21
  "\\(?:\n +\\(:END:\\)\\)?" ;; 22
  "\\(?:\n\\([a-zA-z0-9_@#%:-]+.*\\(?:\n[a-zA-z0-9_@#%:-].*[^\n]\\)*\\)\\)?" ;; 23
  ) "Custom regexp for tasks, events, undated, none, and contacts.")

(defun get-whopper ()
  (save-excursion
    (org-back-to-heading t)
    (when (looking-at whopper-regexp)
      (concat
        (when (not (null (match-string 1)))
          (match-string 1)) ;; stars
        (when (not (null (match-string 2)))
          (concat " " (match-string 2))) ;; todo-state
        (when (not (null (match-string 3)))
          (concat " " (match-string 3))) ;; priority
        (when (not (null (match-string 4)))
          (concat " " (match-string 4))) ;; title
        (when (not (null (match-string 5)))
          (concat " " (match-string 5))) ;; tag
        (cond
          ((and
              (not (null (match-string 6)))    ;; DEADLINE:
              (not (null (match-string 7)))    ;; <-deadline
              (not (null (match-string 8)))    ;; deadline-time-stamp
              (not (null (match-string 9)))    ;; >-deadline
              (not (null (match-string 10)))   ;; SCHEDULED:
              (not (null (match-string 11)))   ;; <-scheduled
              (not (null (match-string 12)))   ;; scheduled-time-stamp
              (not (null (match-string 13))) ) ;; >-scheduled
            (concat
              "\n   "
              (match-string 6)    ;; DEADLINE:
              " "
              (match-string 7)    ;; <-deadline
              (match-string 8)    ;; deadline-time-stamp
              (match-string 9)    ;; >-deadline
              "  "
              (match-string 10)    ;; SCHEDULED:
              " "
              (match-string 11)    ;; <-scheduled
              (match-string 12)    ;; scheduled-time-stamp
              (match-string 13) )) ;; >-scheduled
          ((and
              (not (null (match-string 6))) ;; DEADLINE:
              (not (null (match-string 7))) ;; <-deadline
              (not (null (match-string 8))) ;; deadline-time-stamp
              (not (null (match-string 9))) ;; >-deadline
              (null (match-string 10))      ;; SCHEDULED:
              (null (match-string 11))      ;; <-scheduled
              (null (match-string 12))      ;; scheduled-time-stamp
              (null (match-string 13)) )    ;; >-scheduled
            (concat
              "\n   "
              (match-string 6)    ;; DEADLINE:
              " "
              (match-string 7)    ;; <-deadline
              (match-string 8)    ;; deadline-time-stamp
              (match-string 9)) ) ;; >-deadline
          ((and
              (null (match-string 6))          ;; DEADLINE:
              (null (match-string 7))          ;; <-deadline
              (null (match-string 8))          ;; deadline-time-stamp
              (null (match-string 9))          ;; >-deadline
              (not (null (match-string 10)))   ;; SCHEDULED:
              (not (null (match-string 11)))   ;; <-scheduled
              (not (null (match-string 12)))   ;; scheduled-time-stamp
              (not (null (match-string 13))) ) ;; >-scheduled
            (concat
              "\n   "
              (match-string 10)     ;; SCHEDULED:
              " "
              (match-string 11)     ;; <-scheduled
              (match-string 12)     ;; scheduled-time-stamp
              (match-string 13)) )) ;; >-scheduled
        (when (not (null (match-string 14)))
          (concat "\n   " (match-string 14))) ;; CLOSED:
        (when (not (null (match-string 15)))
          (concat "\n   " (match-string 15))) ;; :PROPERTIES:
        (when (not (null (match-string 16)))
          (concat "\n   " (match-string 16)))      ;; :ToodledoID:
        (when (not (null (match-string 17)))
          (concat " " (match-string 17)))   ;; id-string
        (when (not (null (match-string 18)))
          (concat "\n   " (match-string 18)))      ;; :ToodledoFolder:
        (when (not (null (match-string 19)))
          (concat " " (match-string 19)))   ;; folder-name
        (when (not (null (match-string 20)))
          (concat "\n   " (match-string 20)))      ;; :HASH:
        (when (not (null (match-string 21)))
          (concat " " (match-string 21)))   ;; hash-string
        (when (not (null (match-string 22)))
          (concat "\n   " (match-string 22))) ;; :END:
        (when (not (null (match-string 23)))
          (concat "\n" (match-string 23))) )))) ;; notes

关于emacs - 如何在组织模式下匹配/解析任务结束时的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20942168/

相关文章:

functional-programming - 有条件地选择调用哪个函数

emacs - 如何在 emacs org-mode 的议程 View 中只显示一次 SCHEDULED 和 DEADLINE's todo

emacs - emacs 中 ansi-term 中的奇怪字符

Emacs 组织模式,小细节

Emacs 的 PHP 语法高亮器

emacs - 使用 ido 插入文件名

Windows 上的 Emacs elisp 扩展文件名行为

emacs - 组织模式忽略 HTML 导出中的分页 latex 命令

emacs - 在 Emacs 中选择不同的打印颜色主题

emacs - 如何在当前目录中打开终端?