If you're an Emacs user and find yourself juggling various tasks and projects, the following code snippet can be a game-changer. This Emacs code enhances your task management in Org Mode by automatically adding "DONE" entries into your org-dailies notes, providing a comprehensive log of completed tasks. Let's dive into the code and understand how it can supercharge your productivity.
I did find a few guides related to this on the web but none of them worked for org-roam2. After a bit of work I was able to get working for me in a reliable way.
The Full Code
(setq org-log-done 'note)
(setq org-log-done-with-time 'note)
(defun my/org-log-done-to-dailies ()
(when (and (equal org-state "DONE")
(not (string-empty-p (org-get-heading t t))))
(let* ((today (format-time-string "<%Y-%m-%d %a %H:%M>"))
(month-file (expand-file-name (format-time-string "%Y-%m.org") org-roam-dailies-directory))
(heading (org-get-heading t t))
(original-file (or (buffer-file-name) (dired-get-file-for-visit)))
(link (format "[[file:%s][%s]]" original-file heading))
(body-text (org-get-entry)))
(if (file-exists-p month-file)
(with-current-buffer (find-file-noselect month-file)
(goto-char (point-max))
(insert (format "\n* %s: %s :task:done:\n (Link: %s)\n%s" today heading link body-text))
(save-buffer))
(with-temp-buffer
(insert (format "#+TITLE: %s\n\n* %s: %s :task:done:\n (Link: %s)\n%s" (format-time-string "%Y-%m") today heading link body-text))
(write-file month-file)
(save-buffer))))))
(add-hook 'org-after-todo-state-change-hook #'my/org-log-done-to-dailies)
Understanding the Magic
Logging Configuration: The first two lines set up Org Mode to log "DONE" entries as notes with timestamps.
(setq org-log-done 'note) (setq org-log-done-with-time 'note)
Automatic Logging: The my/org-log-done-to-dailies function gets triggered after the TODO state changes to "DONE." It checks if the heading is not empty and then logs the task completion in your daily notes.
(add-hook 'org-after-todo-state-change-hook #'my/org-log-done-to-dailies)
Details of Logging: The function gathers information like the current date, the daily notes file, the task heading, a link to the completed task, and any additional notes. It then appends this information to your daily notes.
(let* ((today (format-time-string "<%Y-%m-%d %a %H:%M>")) (month-file (expand-file-name (format-time-string "%Y-%m.org") org-roam-dailies-directory)) (heading (org-get-heading t t)) (original-file (or (buffer-file-name) (dired-get-file-for-visit))) (link (format "[[file:%s][%s]]" original-file heading)) (body-text (org-get-entry))) ;; ...
How It Elevates Your Workflow
- Effortless Logging: With this setup, marking a task as "DONE" automatically logs it in your daily notes. No manual effort required.
- Rich Context: The logged entry includes a timestamp, a link to the completed task, and any additional notes, providing a rich context for your accomplishments.
- Organized Daily Notes: Daily notes are organized by month, keeping your task history neatly segmented.
Putting it to use
To implement this, you can directly copy and paste it into your Emacs configuration file. Ensure that you have the necessary dependencies and customize any file paths to match your setup.
With this code in place, managing your tasks becomes a breeze. Emacs Org Mode, combined with the automation provided by this code snippet, empowers you to focus on your work while keeping a detailed and organized log of your accomplishments. Happy tasking!