From fba18ca527956a5b8ea4255a67be2c6df9989d15 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sat, 23 May 2020 16:25:13 +0200 Subject: [PATCH] Add helper function to insert templates of periodic tasks A periodic task is a task tagged with :PERIODIC:, and whose first child is an item called "Template". Following the template are the instances of the periodic tasks, which constitute the actual things to do and which can be scheduled independently of each other. Whenever such an instance is due, the template of the periodic task is supposed to be copied to the instance as a first step. This copying can be done manually, but of course doing it automatically is easier. The new function added in this commit represents a first try to add such an automatism. --- init.el | 3 ++- site-lisp/db-utils.el | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/init.el b/init.el index a62ecbc..1e78e9d 100644 --- a/init.el +++ b/init.el @@ -575,7 +575,8 @@ db/dired-from-shell-command db/system-open db/switch-to-dark-theme - db/switch-to-light-theme)) + db/switch-to-light-theme + db/org-copy-template-for-periodic-task)) (use-package db-hydras :commands (hydra-toggle/body diff --git a/site-lisp/db-utils.el b/site-lisp/db-utils.el index dc31942..83ad9a8 100644 --- a/site-lisp/db-utils.el +++ b/site-lisp/db-utils.el @@ -406,6 +406,33 @@ Work task and home task are determined by the current values of org-home-task-id)) (org-clock-mark-default-task)))) +(defun db/org-copy-template-for-periodic-task () + "Copy template of the enclosing periodic task to item at point. +The template must be placed into an item titled 'Template', +called the template item. The template item must be the first +headline of the periodic task, i.e., of the parent of the current +item at point. The body of the template item, without any +drawers, will be copied to point." + (interactive) + (let ((template (save-mark-and-excursion + (let ((template-element (progn + (outline-up-heading 1 'invisible-ok) + (outline-next-heading) + (org-element-at-point)))) + (unless (string-equal (org-element-property :title template-element) + "Template") + (assert "Template must be first headline in periodic task.")) + ;; XXX: trying to get the contents of the current item, without any + ;; drawers, by going to the end of the template item and marking the + ;; element at point, which, incidentally, seems to be the content we are + ;; looking for; this feels hackish, there must be a better way to do it. + (goto-char (org-element-property :contents-end template-element)) + (org-mark-element) + (buffer-substring-no-properties (region-beginning) + (region-end)))))) + (insert template) + (org-update-statistics-cookies nil))) + ;;; Calendar