我阅读了组织模式手册,但找不到向新创建的TODO添加CREATED字段的简单方法。与org-log-done
结合,然后可以计算关闭特定TODO所花费的时间。这在使用存档文件时特别有用。
例:
* TODO Do something
CREATED: [2012-09-02 Sun 23:02]
* DONE Do something else
CREATED: [2012-09-02 Sun 20:02]
CLOSED: [2012-09-02 Sun 22:02]
我希望每当保存文件时,CREATED字段都会被添加到新任务(没有该字段的任务)中。
有关如何实现这一目标的任何建议?使用像Git这样的东西不是我跟踪TODOS创作的解决方案。
我使用org-expiry来实现该功能,该功能位于org的contrib目录中。
我使用的基本配置是:
;; Allow automatically handing of created/expired meta data.
(require 'org-expiry)
;; Configure it a bit to my liking
(setq
org-expiry-created-property-name "CREATED" ; Name of property when an item is created
org-expiry-inactive-timestamps t ; Don't have everything in the agenda view
)
(defun mrb/insert-created-timestamp()
"Insert a CREATED property using org-expiry.el for TODO entries"
(org-expiry-insert-created)
(org-back-to-heading)
(org-end-of-line)
(insert " ")
)
;; Whenever a TODO entry is created, I want a timestamp
;; Advice org-insert-todo-heading to insert a created timestamp using org-expiry
(defadvice org-insert-todo-heading (after mrb/created-timestamp-advice activate)
"Insert a CREATED property using org-expiry.el for TODO entries"
(mrb/insert-created-timestamp)
)
;; Make it active
(ad-activate 'org-insert-todo-heading)
如果你使用捕获它不会自动工作,需要一点胶水。我在这里发布了完整的配置:https://gist.github.com/4037694
您不需要使用'defadvice'修改函数来在捕获时运行到期代码。你应该使用钩子:
(add-hook 'org-capture-before-finalize-hook
#'(lambda()
(save-excursion
(org-back-to-heading)
(org-expiry-insert-created))))
'org-insert-todo-heading'也是如此。有一个钩子:
(add-hook 'org-insert-heading-hook
#'(lambda()
(save-excursion
(org-back-to-heading)
(org-expiry-insert-created))))
一个更轻量级的解决方案是将!
标志添加到TODO
状态:
(setq org-todo-keywords '((sequence "TODO(!)" "DONE")))
然后:
* TODO get of your ass
- State "TODO" from [2016-06-03 to. 10:35]
虽然不是很漂亮。
Org提供了一个钩子org-after-todo-state-change-hook
,你可以在这里使用:
org-after-todo-state-change-hook是'org.el'中定义的变量。
文档:
在更改TODO项目状态后运行的挂钩。新状态(带有TODO关键字的字符串,或nil)在Lisp变量'org-state'中可用。
使用方法如下:
(require 'org-expiry)
(add-hook 'org-after-todo-state-change-hook
(lambda ()
(when (string= org-state "TODO")
(save-excursion
(org-back-to-heading)
(org-expiry-insert-created)))))
org-expiry是org-contrib的一部分,它是included in the org-plus-contrib package on the org ELPA。
如果您使用org-capture
创建所有TODO,则以下捕获模板可以实现:
(setq org-capture-templates
'(
("t" "TODO Task" entry (file+headline "~/inbox.org" "Tasks")
"* TODO %?\nCREATED: %u\nSRC: %a\n%i\n")
))
结果将如下所示:
* Tasks
** TODO Dummy task
CREATED: [2015-05-08 Fri]
SRC: [[file:~/path/to/file/where/you/created/the/task.org::*heading"][heading]]
这是一个埋藏的宝藏:
(setq org-treat-insert-todo-heading-as-state-change t)
我发现它here,回应有人说他们想要一个org-insert-todo-heading-hook
。
刚试了一下,真的形成,当你org-insert-todo-heading
,它算作一个状态变化,所以ex:#+TODO: TODO(t!) | ...
将添加一个日志。
您可以在创建时使用零配置添加时间戳,但不会将其标记为CREATED。而不是手动键入TODO,使用C-c C-t。然后将其记录为“状态从”“更改为TODO”并加盖时间戳。
这是一个轻量级的解决方案,不需要外部包。我从@MarcinAntczak的答案,@Clément和this similar thread的评论中得到了答案。它适用于org-capture
和M-S-RET
。把它放在你的Emacs初始化文件中(例如~/.emacs
):
(defun insert-created-date(&rest ignore)
(insert (format-time-string
(concat "\nCREATED: "
(cdr org-time-stamp-formats))
))
(org-back-to-heading) ; in org-capture, this folds the entry; when inserting a heading, this moves point back to the heading line
(move-end-of-line()) ; when inserting a heading, this moves point to the end of the line
)
; add to the org-capture hook
(add-hook 'org-capture-before-finalize-hook
#'insert-created-date
)
; hook it to adding headings with M-S-RET
; do not add this to org-insert-heading-hook, otherwise this also works in non-TODO items
; and Org-mode has no org-insert-todo-heading-hook
(advice-add 'org-insert-todo-heading :after #'insert-created-date)
我没有将此函数添加到状态更改(例如,从普通标题到TODO
),因为它需要在属性抽屉中,我更喜欢没有那些额外的行。如果您希望在属性中使用它,请使用参见this thread中定义的函数。