Skip to content

Commit

Permalink
Add a TODO item under the current org-brain entry
Browse files Browse the repository at this point in the history
Add a key-binding to directly add a TODO entry to the current
entry in org-brain. This makes it possible to capture action items
about the current entry without leaving the org-brain-visualization
window.

I use org-capture to implement this functionality and have made
changes that I don't have too much experience with. I'd love feedback
/ different ways to do this.

Summary of changes:

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. Add `org-brain-visualize-add-todo` and `org-brain-add-todo`

Once the user configures the variables above, they can use the 'y'
key-binding in org-brain to create a TODO for the current brain entry.

3. `org-brain-visualize--register-capture-templates`

This is a helper function to ensure that we set up the capture
templates only once (after org-capture is loaded). I'm not sure what
the idiomatic way to do this is, feedback welcome.

This is the first cut of this functionality. If you think this is
okay, I'll make changes in the README file (and any other changes you
require). I am also not sure if I've created the vars and functions in
the correct place in the org-brain.el. I'll move them to the correct
places based on your feedback.
  • Loading branch information
vedang committed Apr 2, 2020
1 parent 7d36218 commit a67b523
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion org-brain.el
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,79 @@ If called interactively, select ENTRY with
(if (eq major-mode 'org-brain-visualize-mode)
(org-brain-add-note (org-brain-entry-at-pt))
(user-error "Not in org-brain-visualize")))

(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'."
(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)))))

(defun org-brain-add-todo (entry)
"Add a todo item to the ENTRY.
If called interactively, select ENTRY with
`org-brain-choose-entry', give a preference to
`org-brain-entry-at-pt', if any."
(interactive
(let ((def-choice (ignore-errors
(org-brain-entry-name (org-brain-entry-at-pt)))))
(list (org-brain-choose-entry "Add a TODO item to: "
'all nil nil def-choice))))
(when (not org-brain-visualize-use-capture-templates)
(user-error "The appropriate capture templates have not been set up. Check the README for instructions"))
(if (org-brain-filep entry)
;; Entry = File
(user-error "Only headline entries support adding a TODO item")
;; Entry = Headline
(org-capture nil (concat org-brain-visualize-capture-prefix-key "t"))))

(defun org-brain-visualize-add-todo ()
"Add a todo item to the currently active entry."
(interactive)
(if (eq major-mode 'org-brain-visualize-mode)
(org-brain-add-todo (org-brain-entry-at-pt))
(user-error "Not in org-brain-visualize")))

;;;; Buffer commands

;;;###autoload
Expand Down Expand Up @@ -2623,7 +2696,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

Expand All @@ -2635,6 +2709,7 @@ point before the buffer was reverted."
(define-key org-brain-visualize-mode-map "h" 'org-brain-add-child-headline)
(define-key org-brain-visualize-mode-map "n" 'org-brain-pin)
(define-key org-brain-visualize-mode-map "N" 'org-brain-visualize-add-note)
(define-key org-brain-visualize-mode-map "y" 'org-brain-visualize-add-todo)
(define-key org-brain-visualize-mode-map "t" 'org-brain-set-title)
(define-key org-brain-visualize-mode-map "j" 'forward-button)
(define-key org-brain-visualize-mode-map "k" 'backward-button)
Expand Down

0 comments on commit a67b523

Please sign in to comment.