Jump to most recent timestamp in an org document

| categories: orgmode | tags: | View Comments

Timestamps serve many purposes in an org document: deadlines, scheduled time, record of time, etc… Depending on the document, you may have organized a set of tasks logically, but you are completing them in some other order. Here, we examine how to jump to the most recent timestamp.

Here is an example of an inactive (will not cause an agenda entry) timestamp [2014-08-07 Thu] and an active timestamp <2014-08-13 Wed>. You can also have times in the timestamps, e.g. <2014-08-14 Thu 13:00> or <2014-08-14 Thu 14:00>. There may be a timestamp that is in the future [2014-08-15 Fri].

We may have some headlines with a due date.

1 Headline with duedate

2 Analyzing the timestamps in this file

We can get a sorted list of the time-stamps like this.

(sort (org-element-map (org-element-parse-buffer) 'timestamp
	(lambda (timestamp)
	  (org-element-property :raw-value timestamp))) 'org-time>)
[2014-08-15 Fri] <2014-08-14 Thu 14:00> <2014-08-14 Thu 13:00> <2014-08-13 Wed> [2014-08-07 Thu]

Interestingly, the deadline timestamp does not appear in this list! I am not sure why it doesn't show up.

Unfortunately, having those time stamps in this buffer will complicate further analysis, because org-mode will parse them too. What we need to do next is figure out a way to sort with the positions, so we can jump to it.

Side bar: How to sort a cons list based on the first element of each cons cell? Here are two examples.

(cl-sort '((3 . "a") (2 . "b") (1 . "c")) '> :key 'car)

((3 . a) (2 . b) (1 . c))

(sort '((3 . "a") (2 . "b") (1 . "c"))
      (lambda (a b)
	(> (car a) (car b))))

((3 . a) (2 . b) (1 . c))

So, we just build up a sorted cons-list of timestamps and their beginning characters, then pop the top entry off and jump to it. Here is the code. Running this jumps directly to the most recent (which could be in the future) timestamp.

(let ((timestamps (cl-sort
		   (org-element-map (org-element-parse-buffer) 'timestamp
		     (lambda (timestamp)
		       `(,(org-element-property :raw-value timestamp) . ,(org-element-property :begin timestamp))))
		   'org-time> :key 'car)))
  (goto-char
   (cdr
    (pop timestamps))))

There are some limitations here:

  1. Only free timestamps are shown, timestamps associated with deadlines and scheduling do not appear to be considered here. You would need to map over the headlines and check for deadlines to get these I think.
  2. The code block above finds the timestamp with the highest time value, which may be in the future. It is an interesting exercise to think about how to find the most recent timestamp that is not in the future.

Copyright (C) 2014 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.7c

blog comments powered by Disqus