The Problem
My main org-mode files have a header structure like this:
* 2015
** March
*** Friday 6 March 2015
**** 10:00. Emacs coaching with Sacha :emacs:
<2015-03-06 10:00-11:00>
**** 23:37. Emacs progress note
[2015-03-06 23:37]
*** Saturday 7 March 2015
**** 12:55. Stratford King Lear in cinemas :wcss:
<2015-03-07 12:55-15:55>
The two things I do most often are start writing notes in the present, and set up scheduled events in the future. Scheduled events need active timestamps so that they show up in the agenda view:
Week-agenda (W10):
Monday 2 March 2015 W10
Tuesday 3 March 2015
Wednesday 4 March 2015
Thursday 5 March 2015
Friday 6 March 2015
10:00. Emacs Coaching with Sacha :emacs:
Saturday 7 March 2015
12:55. Stratford King Lear in cinemas :wcss:
Sunday 8 March 2015
[How to get the agenda view? Make sure the file you’re in is in org-agenda-files by pressing C-c [
, then run org-agenda either by M-x org-agenda
or the common (but not out-of-the-box) shortcut C-c a
, then press a
to get the agenda for the week.]
[Active timestamps are in < >
; inactive timestamps are in [ ]
. If we want to promote an inactive timestamp to be active and show up in the agenda view, we can run M-x org-toggle-timestamp-type
from the inactive timestamp.]
[Start time is duplicated between header and timestamp deliberately: sometimes I review the file in org-mode outline with only the headers. If not for that use-case it would make sense to leave the start time out of the header.]
What I’d like to be able to do is open the file, and then with as little ceremony or setup as possible, start typing.
Adding a New Note
When I’ve already got a date header for the current date, this is trivially simple: I can put my cursor under that date and call M-x my note
, having previously defined
(defun my/note ()
(interactive)
(insert "\n\n**** " (format-time-string "%H:%M") ". ")
(save-excursion
(insert "\n" (format-time-string "[%Y-%m-%d %H:%M]") "\n\n")))
… and it does pretty much what it says.
The values passed into format-time-string
(%Y
, %m
, %d
etc.) will be the values for the current date and time.
save-excursion
means “do what’s inside here and then return the point (cursor) to where it was before the block was called”, so it fills in the timestamp and then returns the cursor to the end of the header line, ready for the title to be filled in.
Since after that I’d need to move the cursor back down to below the timestamp, I can be even lazier and prompt for the title and the tags (if any) and fill them in and move the cursor down to where the text should start:
(defun my/note-2 (title tags)
(interactive (list
(read-from-minibuffer "Title? ")
(read-from-minibuffer "Tags? ")))
(insert "\n\n**** " (format-time-string "%H:%M") ". " title)
(unless (string= tags "")
(insert " :" tags ":")
)
(insert "\n" (format-time-string "[%Y-%m-%d %H:%M]") "\n\n"))
If we were just setting one argument, say title, this would be simpler:
(defun my/note-2-eg (title)
(interactive "MTitle? ")
But since we want two, we need to tell interactive
it’s getting a list
, and then the arguments are set in order.
(defun my/note-2 (title tags)
(interactive (list
(read-from-minibuffer "Title? ")
(read-from-minibuffer "Tags? ")))
Just as we can create an inactive timestamp by inserting a formatted date with [ ]
, we can add tags by inserting a string surrounded by : :
. We don’t want to do this if the tag argument was left blank, of course, so we surround it with an unless
:
(unless (string= tags "")
(insert " :" tags ":")
)
[Side-note: if you’re doing anything more complicated than this with strings, you probably want Magnar Sveen’s s.el (string manipulation library), which here would let you do (unless (s-blank? tags)…
instead.]
At the end of which, because we took out the save-excursion
, the cursor is below the timestamp and I’m ready to type.
All good. What if we don’t already have a date header for the current date? Can we auto-generate the date header?
Auto-generating Date Headers
Org-mode has a function called org-datetree-find-date-create
. If you pass it in a list of numbers (the month, the day, and the year), and if your header structure is
* 2015
** 2015-03 March
*** 2015-03-06 Friday
then if you called that function passing in (3 7 2015), it would automatically add:
*** 2015-03-07 Saturday
For that matter, if you called it passing in (4 1 2015), it would automatically add
** 2015-04 April
*** 2015-04-01 Wednesday
If you call it passing in a date which is already there, it moves the cursor to that date. So, we could change the format of the org file headers, update our new note function to call the function, and be done.
(defun my/note-3 (title tags)
(interactive (list
(read-from-minibuffer "Title? ")
(read-from-minibuffer "Tags? ")))
(org-datetree-find-date-create (org-date-to-gregorian
(format-time-string "%Y-%m-%d")))
(org-end-of-subtree)
(insert "\n\n**** " (format-time-string "%H:%M") ". " title)
(unless (string= tags "")
(insert " :" tags ":")
)
(insert "\n" (format-time-string "[%Y-%m-%d %H:%M]") "\n\n"))
In the new bit:
(org-datetree-find-date-create (org-date-to-gregorian
(format-time-string "%Y-%m-%d")))
(org-end-of-subtree)
format-time-string “%Y-%m-%d”
will return “2015-03-07”, and org-date-to-gregorian
will turn that into (3 7 2015), which is the format that org-datetree-find-date-create
expects. Determining this involved looking at the source for org-datetree-find-date-create
to see what arguments it expected (C-h f org-datetree-find-date-create
takes you to a help buffer that links to the source file, in this case org-datetree.el
; click on the link to go to the function definition) and a certain amount of trial and error. At one point, before org-date-to-gregorian
, I had the also working but rather less clear:
(org-datetree-find-date-create (mapcar ‘string-to-number
(split-string (format-time-string "%m %d %Y"))))
org-end-of-subtree
just takes the cursor to the bottom of the section for the date.
And that then works. What about adding new events in the future?
Adding a New Event
org-datetree-find-date-create
makes it easier to fill in missing month and date headers to create a new future event:
(defun my/event (date end-time)
(interactive (list
(org-read-date)
(read-from-minibuffer "end time (e.g. 22:00)? ")))
(org-datetree-find-date-create (org-date-to-gregorian date))
(goto-char (line-end-position))
(setq start-time (nth 1 (split-string date)))
(if (string= start-time nil)
(setq start-time ""))
(insert "\n\n**** " start-time ". ")
(save-excursion
(if (string= end-time "")
(setq timestamp-string date)
(setq timestamp-string (concat date "-" end-time)))
(insert "\n<" timestamp-string ">\n\n")))
There’s a problem I haven’t solved here, which is that org-read-date
brings up the date prompt and lets you select a date, including a time or time range. If you select a time, it will be included in the date. If you select a time range, say 19:30-22:30, it ignores the time and the date object returned uses the current time. That’s not what we want.
So when the date prompt comes up:
Date+time [2015-03-07]: _ => <2015-03-07 Sat>
I can put in a new date and start time, say “2015-04-01 11:00”:
Date+time [2015-03-07]: 2015-04-01 11:00_ => <2015-04-01 Wed 11:00-14:00>
and then press enter and that gives me the date and the start time in a single string. We extract the start time and put it into the header like so:
(setq start-time (nth 1 (split-string date)))
(if (string= start-time nil)
(setq start-time ""))
(insert "\n\n**** " start-time ". ")
Where split-string
splits the date “2015-04-01 11:00” into a two-element list (“2015-04-01” “11:00”) {n}, and nth 1
returns the second element (list elements, here as elsewhere, are numbered starting from 0), or “11:00” {n}.
If the date had been “2015-04-01” without a start time, line 1 would have set start-time to nil, which would have blown up as an argument to insert (with a “Wrong type argument: char-or-string-p, nil”), so we check for a nil and set it to an empty string instead.
For the agenda view we’ll need the end-time as well, so we grab that as a second argument, and reassemble the date, say “2015-04-01 11:00”, and end-time, say “14:00”, into the active timestamp:
(if (string= end-time "")
(setq timestamp-string date)
(setq timestamp-string (concat date "-" end-time)))
(insert "\n<" timestamp-string ">\n\n"))
though here again, if we had just pressed enter when prompted for end time we would end up with an empty string and an invalid active timestamp, like “
”, so check whether end-time has been set and build the timestamp string accordingly. And since this is a future event and we’re probably only filling in the title, we’ll use save-excursion
again to fill in the active timestamp and then go back and leave the cursor on the header to fill in the title.
The End
And we’re done. Or we would be, but I would really prefer the header to read “March” instead of “2015-03 March”, and “Saturday 7 March 2015” instead of “2015-03-07 Saturday”. We might need to come back to that.