From 4d5f2a3f3c206b1d4f4578b90e3dc64a5ec4a3fd Mon Sep 17 00:00:00 2001 From: Vedang Manerikar Date: Wed, 14 Oct 2020 07:47:44 +0530 Subject: [PATCH] Create capture templates for org-brain I plan to use these templates to implement functionality `org-brain-add-todo` in a future commit. The summary of changes in this commit are as follows: 1. Set up capture templates for brain. I'm using "b" as a template prefix key for capture templates related to org-brain. Since people may already be using this key in their own capture templates, this functionality is turned off by default (via `org-brain-visualize-use-capture-templates` var) and the user can select a different prefix key (via `org-brain-visualize-capture-prefix-key` var) Since I was setting up capture templates, I also moved the capture template described in the README to this code. Finally, these templates only work in the context of org-brain-visualize, to avoid clutter in other modes. 2. `org-brain-visualize--register-capture-templates` This is a helper function to ensure that we set up the capture templates only once. It's internal and invisible to the user. --- org-brain.el | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/org-brain.el b/org-brain.el index fcfc488..aa6449e 100644 --- a/org-brain.el +++ b/org-brain.el @@ -2702,6 +2702,53 @@ See `org-brain-add-resource'." (defalias 'org-brain-visualize-paste-resource #'org-brain-paste-resource) +;;;; Capture Templates + +(defvar org-brain-visualize-use-capture-templates nil + "Add useful capture templates to your org-brain setup. +This variable is nil by default, since we are modifying +`org-capture-templates' which can have undesirable effects. +Set this to t to activate org-brain related capture templates. +These templates only work in the `org-brain-visualize-mode' and +are keyed off `org-brain-visualize-capture-prefix-key'.") + +(defvar org-brain-visualize-capture-prefix-key "b" + "The default prefix key for org-brain capture templates. +Change this value if you already use 'b' for a different capture template.") + +(defvar org-brain-visualize--capture-templates-registered-p nil + "A helper var. +Track if we've added the necessary org-brain capture templates to +`org-capture'.") + +(defun org-brain-visualize--register-capture-templates () + "A helper function. +Set up the capture templates we need in `org-brain-visualize-mode'." + (with-eval-after-load 'org-capture + (progn + (when (and org-brain-visualize-use-capture-templates + (not org-brain-visualize--capture-templates-registered-p)) + (push `(,org-brain-visualize-capture-prefix-key + "Templates when working with org-brain") + org-capture-templates) + (push `(,(concat org-brain-visualize-capture-prefix-key "b") + "Brain Note" + plain + (function org-brain-goto-end) + "* %i%?" + :empty-lines 1) + org-capture-templates) + (push `(,(concat org-brain-visualize-capture-prefix-key "t") + "Brain Todo" + entry + (function org-brain-goto-current) + "* TODO %?\n%U\n%i" + :clock-keep t) + org-capture-templates) + (push '("b" ((in-mode . "org-brain-visualize-mode"))) + org-capture-templates-contexts) + (setq org-brain-visualize--capture-templates-registered-p t))))) + ;;;###autoload (defun org-brain-select-button () "Toggle selection of the entry linked to by the button at point." @@ -2794,7 +2841,8 @@ point before the buffer was reverted." special-mode "Org-brain Visualize" "Major mode for `org-brain-visualize'. \\{org-brain-visualize-mode-map}" - (setq-local revert-buffer-function #'org-brain-visualize-revert)) + (setq-local revert-buffer-function #'org-brain-visualize-revert) + (org-brain-visualize--register-capture-templates)) ;;;;; Keybindings