Change the current time in Org-mode
11 Apr 2019Intro
I'm constantly amazed by other people's Org workflows. Now that the weekly tips are a thing, I see more and more cool Org configs, and I'm inspired to get more organized myself.
My own Org usage is simplistic in some areas, and quite advanced in others. While I wrote a lot of code to manipulate Org files ( worf, org-download, orca, org-fu, counsel), the amount of Org files and TODO items that I have isn't huge:
(counsel-git "org$ !log")
;; 174 items
(counsel-rg "\\* DONE|CANCELLED|TODO")
;; 8103 items
Still, that's enough to get out-of-date files: just today I dug up a file with 20 outstanding TODO items that should have been canceled last November!
How to close 20 TODOs using a timestamp in the past
When I cancel an item, pressing tc (mnemonic for TODO-Cancel), Org mode inserts a time
stamp with the current time. However, for this file, I wanted to use October 31st 2018 instead of
the current time. Org mode already has options like org-use-last-clock-out-time-as-effective-time
,
org-use-effective-time
, and org-extend-today-until
that manipulate the current time for
timestamps, but they didn't fit my use case.
So I've advised org-current-effective-time
:
(defvar-local worf--current-effective-time nil)
(defun worf--current-effective-time (orig-fn)
(or worf--current-effective-time
(funcall orig-fn)))
(advice-add 'org-current-effective-time
:around #'worf--current-effective-time)
(defun worf-change-time ()
"Set `current-time' in the current buffer for `org-todo'.
Use `keyboard-quit' to unset it."
(interactive)
(setq worf--current-effective-time
(condition-case nil
(org-read-date t 'totime)
(quit nil))))
A few things of note here:
worf--current-effective-time
is buffer-local, so that it modifies time only for the current buffer- I re-use the awesome
org-read-date
for a nice visual feedback when inputting the new time - Instead of having a separate function to undo the current-time override, I capture the
quit
signal that C-g sends.
Outro
The above code is already part of worf and is bound to cT. I even added it to the manual. I hope you find it useful. Happy organizing!