在org-mode中将CREATED date属性添加到TODO

时间:2012-09-04 11:05:32

标签: emacs elisp org-mode

我阅读了组织模式手册,但找不到向新创建的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创作的解决方案。

8 个答案:

答案 0 :(得分:14)

我使用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

答案 1 :(得分:6)

您不需要使用' defadvice'修改功能。在捕获时运行到期代码。 你应该使用hook:

(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))))

答案 2 :(得分:5)

更轻量级的解决方案是将!标志添加到TODO状态:

(setq org-todo-keywords '((sequence "TODO(!)" "DONE")))

然后:

* TODO get of your ass
  - State "TODO"    from    [2016-06-03 to. 10:35]

但它并不是很漂亮。

参考:http://orgmode.org/org.html#Tracking-TODO-state-changes

答案 3 :(得分:4)

Org提供了一个钩子ls -d1A .* | tail -n +3 ,你可以在这里使用:

  

org-after-todo-state-change-hook是'org.el'中定义的变量。

     

文档:

     

更改TODO项目状态后运行的挂钩。   新状态(带有TODO关键字或nil的字符串)可用于   Lisp变量'org-state'。

按如下方式使用:

org-after-todo-state-change-hook

org-expiry是org-contrib的一部分,即included in the org-plus-contrib package on the org ELPA

答案 4 :(得分:3)

如果使用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]]

答案 5 :(得分:3)

这是一个埋藏的宝藏:

(setq org-treat-insert-todo-heading-as-state-change t)

我找到了here,以回应有人说他们想要org-insert-todo-heading-hook

刚试了一下,并且真的要形成,当你org-insert-todo-heading时,它会被视为状态变化,所以ex:#+TODO: TODO(t!) | ...会添加一个日志。

答案 6 :(得分:1)

您可以在创建时使用零配置添加时间戳,但不会将其标记为CREATED。而不是手动键入TODO,使用C-c C-t。然后将其记录为“状态从”“更改为TODO”并加盖时间戳。

答案 7 :(得分:1)

这里是不需要外部包装的轻型解决方案。我从@MarcinAntczak的答案,@Clément的评论和this similar thread中得到了答案。它适用于org-captureM-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中定义的函数。