2013-09-23 Automatic insertion of habit templates (en)

I have reconciled myself to the fact that more and more of my life is being assimilated by Emacs’ Org-mode;). And a few days ago, I decided to give the habits module a try. However, I found inserting new habits a bit cumbersome: manually setting the STYLE and LOGGING properties, inserting the timestamp (with the repeater) is too much a repetitive task for an Emacs user. So, here’s a quick fix:

(defun org-insert-habit ()
  "Insert a new TODO subheading and set its properties so that it becomes a habit."
  (interactive)
  (beginning-of-line)
  (org-insert-todo-subheading nil)
  (org-schedule nil (format-time-string "%Y-%m-%d" (current-time)))
  (save-excursion
    (search-forward ">")
    (backward-char)
    (insert (concat
	     " .+"
	     (read-string "Minimum interval: ")
	     "/"
	     (read-string "Maximum interval: "))))
  (org-set-property "STYLE" "habit")
  (org-set-property "LOGGING" "TODO DONE(!)"))

Admittedly, it’s rather crude, but I thought that a quick-and-dirty hack like this, to be used maybe a dozen times at first and then once a few days or even weeks, need not have fancy things like history of repeater intervals etc. (Also, read-string in the middle of a function is indeed ugly – it really should be made into an argument with a suitable interactive clause. I did not have enough time/patience to look up and recall its syntax again, though…)

Have fun tracking your habits!

Edit: deleted the exclamation mark after TODO, which introduced a bug.

CategoryEnglish, CategoryBlog, CategoryEmacs