How to match / parse the notes at the end of a task in org-mode

前端 未结 1 1702
星月不相逢
星月不相逢 2021-01-22 13:10

I\'m looking for a way to match what I call the \"notes\", which is the last line (or set of lines) of an org task that says:   \"These are the notes of the task.\

1条回答
  •  [愿得一人]
    2021-01-22 14:00

    EDIT (January 7, 2014):  First working draft . The tail end of the regexp will match notes until the next blank line -- i.e., notes may be several lines with hard returns and no blank lines between them.

    EDIT (January 14, 2014): Revised 31 and 32 so that no final new line is needed at the end of the buffer -- i.e., a task with no notes at the end of the buffer is now recognized. Revised 20 to 24 so that the positions of the :ToodledoID: drawer and the :ToodledoFolder: drawer are interchangeable. Revised example get-whopper function to exclude 32.

    EDIT (November 14, 2014):  Updated regexp a bit, but the functionality remains the same.

    EDIT (December 2, 2014):  Removed custom function and replaced with stock org-back-to-heading.

    EDIT (December 6, 2014:  Major revamping of regex. The regex now supports optional items for the majority of the entries, and no final new line is required at the end of the buffer. With the new revision, the regexp can be used in a custom *Org Agenda* buffer that displays only partial items from the original todo -- e.g., heading + deadline / scheduled / closed + notes; or heading + notes only. The :tag:, however, is a mandatory anchor -- i.e., at least one (1) tag is needed for the regexp to work correctly. Line endings -- i.e., \n -- are no longer numbered matching entries. The regex now supports todo entries that are bunched together without any new line separating them. See related thread regarding non-capturing group: https://stackoverflow.com/a/3513858/2112489 See also the related thread regarding multi-line regexp 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
    

    0 讨论(0)
提交回复
热议问题