【问题标题】:Custom searches using timestamps in LOGBOOK in org-mode在 org 模式下使用 LOGBOOK 中的时间戳进行自定义搜索
【发布时间】:2011-11-07 16:26:59
【问题描述】:

我想创建一个自定义议程搜索,它将根据日志中的时间条目查找 TODO 项目。具体来说,我想根据标记进入等待状态的时间戳找到标记为 WAITING 的项目。这些条目如下所示:

:LOGBOOK:
- State "WAITING"     from "TODO"    [2011-11-02 Wed 15:10] \\
  Emailed so-and-so about such-and-such.
:END:

我可以使用日志中的信息执行此操作吗?我使用的是 7.5 版,但如有必要可以升级。

谢谢!

编辑:一个用例可能是查找等待状态超过一周的等待待办事项。 (这通常意味着我需要再次打扰某人。)

【问题讨论】:

  • 根据日志,您有什么特别的理由想要它吗?如果项目当前处于“等待”状态,您应该能够基于此进行自定义搜索。我怀疑可以从这里的归档方法中调整一些东西:doc.norang.ca/org-mode.html#sec-12-2。只需调整匹配参数以匹配“状态\”WAITING\“”。虽然我不确定如何仅根据这些时间戳而不是包含的其他时间戳对其进行排序。
  • 一个用例是查找一个多星期前进入等待状态的 WAITING 项目。由于我的 todo 设置已经在日志中记录了它进入 WAITING 状态的时间,这似乎是开始的地方。不过,我对其他方法持开放态度。
  • LOGBOOK 中您想要匹配的时间条目是否总是最新的?它要么必须是最新的日志条目,要么必须与该标题的所有日志条目匹配(不管此后的更新)。我怀疑您只希望它与最新的 LOGBOOK 条目匹配,否则它将与您的用例不匹配。

标签: search logging time org-mode


【解决方案1】:

以下应该可以满足您的需求。您只需调整自定义议程命令以适合您的用例。 (在测试和配置它时,我使用了我的 TODO 关键字)。此代码的一部分可能重复了内置 org 函数的工作,特别是因为它类似于 Scheduled 和 Deadline 方法/过期行为,但我看不到任何可重用的特定函数。

在自定义命令中使用的实际函数如下。

(defun zin/since-state (since todo-state &optional done all)
  "List Agenda items that are older than SINCE.

TODO-STATE is a regexp for matching to TODO states.  It is provided to
`zin/find-state' to match inactive timestamps.
SINCE is compared to the result of `zin/org-date-diff'.  If
`zin/org-date-diff' is greater than SINCE, the entry is shown in the
Agenda. 
Optional argument DONE allows for done and not-done headlines to be
evaluated.  If DONE is non-nil, match completed tasks.
Optional argument ALL is passed to `zin/find-state' to specify whether
to search for any possible match of STATE, or only in the most recent
log entry."
  (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
    ;; If DONE is non-nil, look for done keywords, if nil look for not-done
    (if (member (org-get-todo-state)
                (if done
                    org-done-keywords
                  org-not-done-keywords))
        (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
               (subtree-valid (save-excursion
                               (forward-line 1)
                               (if (and (< (point) subtree-end)
                                        ;; Find the timestamp to test
                                        (zin/find-state todo-state subtree-end all))
                                   (let ((startpoint (point)))
                                     (forward-word 3)
                                     ;; Convert timestamp into days difference from today
                                     (zin/org-date-diff startpoint (point)))))))
          (if (or (not subtree-valid)
                  (<= subtree-valid since))
              next-headline
            nil))
      (or next-headline (point-max)))))

以下函数通过重新搜索转发查找日志条目。

(defun zin/find-state (state &optional end all)
  "Used to search through the logbook of subtrees.

Tests to see if the first line of the logbook is a change of todo
status to status STATE
- Status \"STATE\" from ...
The search brings the point to the start of YYYY-MM-DD in inactive timestamps.

Optional argument END defines the point at which to stop searching.
Optional argument ALL when non-nil specifies to look for any occurence
of STATE in the subtree, not just in the most recent entry."
  (let ((drawer (if all "" ":.*:\\W")))
    (re-search-forward (concat drawer ".*State \\\"" state "\\\"\\W+from.*\\[") end t)))

最后一个函数确定今天与上面函数找到的时间戳之间的天数差。

(defun zin/org-date-diff (start end &optional compare)
  "Calculate difference between  selected timestamp to current date.

The difference between the dates is calculated in days.
START and END define the region within which the timestamp is found.
Optional argument COMPARE allows for comparison to a specific date rather than to current date."
  (let* ((start-date (if compare compare (calendar-current-date))))
    (- (calendar-absolute-from-gregorian start-date) (org-time-string-to-absolute (buffer-substring-no-properties start end)))
    ))

使用上述函数的两个示例自定义议程命令。第一个匹配您的用例,您只需将“PEND”更改为“WAITING”即可匹配正确的关键字。第二个查找超过 30 天前完成的 DONE 关键字(而不是像我在第一条评论中链接的示例中那样查找月份与本月/上月匹配的时间戳)。

(setq org-agenda-custom-commands
      (quote (("T" "Tasks that have been pending more than 7 days." tags "-REFILE/"
               ((org-agenda-overriding-header "Pending tasks")
                (org-agenda-skip-function '(zin/since-state 7 "PEND"))))
              ("A" "Tasks that were completed more than 30 days ago." tags "-REFILE/"
               ((org-agenda-overriding-header "Archivable tasks")
                (org-agenda-skip-function '(zin/since-state 30 "\\\(DONE\\\|CANC\\\)" t))))
              )))

【讨论】:

  • 谢谢!这看起来正是我需要的。遗憾的是,有些内置插件并没有更有用,因为这似乎是一个常见的用例。此外,还有一个额外的参数“all”传递给 zin/find-state。删除它使代码正常运行。
  • 糟糕,实际上是t 是从zin/since-statezin/find-state 的额外调用。它应该默认为 nil,并且需要从议程命令本身传递 t(如果 ALL 是 t,它将发现任何事件,而不仅仅是日志中最近的事件,这应该会产生误报)。我会在我的答案中修正这个值。
【解决方案2】:

除了Jonathan Leech-Pepin的回答,如果你想看(setq org-log-done 'time)配置添加的CLOSED:抽屉,你可以像这样改进zin/find-state函数:

(defun zin/find-state (state &optional end all)
  (let ((drawer (if all "" ":.*:\\W" "CLOSED:")))
    (or (re-search-forward (concat drawer ".*State \\\"" state "\\\"\\W+from.*\\[") end t)
        (re-search-forward (concat drawer ".*\\[") end t))))

PS:这只是对答案的改进,正确答案是乔纳森的答案。

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2018-07-28
    • 2017-05-19
    • 2017-11-05
    相关资源
    最近更新 更多