Skip to content

Instantly share code, notes, and snippets.

@salewski
Last active November 16, 2019 18:43
Show Gist options
  • Save salewski/f27aa39e63d7917b7c368316d5d2e84c to your computer and use it in GitHub Desktop.
Save salewski/f27aa39e63d7917b7c368316d5d2e84c to your computer and use it in GitHub Desktop.
emacs org-mode: insert "CREATED" date and timestamp when creating new TODOs
;; I've customzied `org-log-note-headings' to add a 'created' heading. This
;; allows us to have a 'CREATED' timestamp automatically inserted into the
;; LOGBOOK when creating a new TODO item.
;;
;; This technique learned from:
;;
;; http://lists.gnu.org/archive/html/emacs-orgmode/2009-11/msg00914.html
;;
;; for TODOs created in the buffer
;;
;; ;; AL.NOTE (2019-03-13): I revisted the below (currently using the debian
;; ;; package for org-mode 9.0.3) and found that it still does not
;; ;; work. The problem is that `org-add-log-setup' invokes
;; ;; `org-add-log-note', and that latter function has a fixed hard-coded
;; ;; list of values that are able to appear in `org-log-note-purpose';
;; ;; it does not like the 'created' purpose we were formerly able to jam
;; ;; in there. Rather than continue with this approach, I'm instead
;; ;; switching to just advising `org-insert-todo-heading' with some
;; ;; straight-forward editing (see below).
;; (defun ads/org-insert-heading-hook-todo-insert-creation-date ()
;; "Insert CREATED timestamp into LOGBOOK"
;; (org-add-log-setup 'created nil nil 'findpos 'time))
;;
;; (add-hook 'org-insert-heading-hook 'ads/org-insert-heading-hook-todo-insert-creation-date)
;; (remove-hook 'org-insert-heading-hook 'ads/org-insert-heading-hook-todo-insert-creation-date) ;; FIXME: my customized function no longer works in org-mode 9.0.9
;; ;; FIXME: Related to the above, see also:
;; ;; * `org-treat-insert-todo-heading-as-state-change'
;; ;; * https://stackoverflow.com/questions/12262220/add-created-date-property-to-todos-in-org-mode
;; ;; * https://emacs.stackexchange.com/questions/21291/add-created-timestamp-to-logbook
;; ;; * https://gist.github.com/mrvdb/4037694
;; ;; * https://lists.gnu.org/archive/html/emacs-orgmode/2010-12/msg00441.html
;; ;;
;; ;; for remember
;; ;; (setq org-remember-templates (quote (("todo" ?t "* TODO %?\n :LOGBOOK:\n -
;; ;; CREATED:%U\n :END:\n" nil bottom nil)
;;
;;
;; AL.NOTE (2019-03-13): The following started as an adaptation of something I
;; found in the StackOverflow thread:
;;
;; https://stackoverflow.com/questions/12262220/add-created-date-property-to-todos-in-org-mode
;;
;; the following answer from 'mmorin', in particular:
;;
;; https://stackoverflow.com/a/52815573
;;
;; My solution below, however, is entirely my own.
;;
(require 'cl-lib) ;; for `cl-flet' (note: pulls in `org-macs')
;;
(defun ads/org-insert-created-date( &rest ignore )
"Insert an org-mode \":LOGBOOK:\" drawer with a \"CREATED:\" date and timestamp.
The \"CREATED\" field value is an inactive long-form
date/timestamp value with the current date and time. The user
will not be prompted to adjust these.
Intended to be installed as \"after\" advice around
`org-insert-todo-heading'.
Example:
** TODO
:LOGBOOK:
- CREATED: [2019-03-13 Wed 21:14]
:END:
Note that point is one space beyond \"TODO\" both when M-S-RET is
pressed and when the function returns.
The implementation is naive in that it makes assumtions about
where it is in the buffer; a new set of lines will be inserted
below the current line unconditionally to hold the new drawer. No
effort is made to verify that the context in which the function
is invoked makes sense; no effort is made to detect an existing
drawer or any other content around point. The function assumes
that org-mode has just inserted a new TODO item and that point is
at the end of the headline.
Upon completion, point is restored to its location at the time
this function was invoked. If the above assumptions are correct,
this behavior should leave point at the end of the headline."
(cl-flet ((__ads/org-indent () (indent-to-column (1+ (org-outline-level)))))
(save-excursion
(insert "\n")
(__ads/org-indent)
(insert ":LOGBOOK:\n")
(__ads/org-indent)
(insert (format "- CREATED: %s\n"
(format-time-string (org-time-stamp-format 'long 'inactive)
(org-current-effective-time))))
(__ads/org-indent)
(insert ":END:\n"))))
;;
;; The default org-mode binding of M-S-RET will invoke
;; `org-insert-todo-heading-respect-content', which then invokes
;; `org-insert-todo-heading', hence our adding to that latter function.
;;
;; Note that org-mode (at least as of 9.0.3, as distributed with the Debian
;; org-mode-9.0.3-2 package) does not have a hook that would allow us to
;; achieve this same effect (e.g., there is no `org-insert-todo-heading-hook').
;; If such a thing were to be added in the future, we would want to replace
;; our use of function advice with use of a hook function, instead.
;;
(advice-add 'org-insert-todo-heading :after #'ads/org-insert-created-date)
;(advice-remove 'org-insert-todo-heading #'ads/org-insert-created-date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment