org-capture Refile Functions & Helm Sources

18. February 2019

For a few years, predating my adoption of emacs, I’ve kept a Quotes file with phrases I want to remember or come back to. Originally formatted as Markdown, converting it to org was easy enough with macros (pandoc would’ve been a great alternative if the structure was more complex).

I organise that file like so:

1
2
3
4
* ${AuthorName}
  - [Timestamp of Creation**: An entry represents an unattributed quote, such as
  from a press conference
** Subitems are individual titles

A complete example looks like this:

1
2
3
4
* John Stuart Mill
** On Liberty
   - The initiation of all wise or noble things, comes and must come from
     individuals; generally at first from some one individual.

An issue I’d had in the past was having to grep around to find a particular author or title, which quickly got tricky when there were authors with many references to other works or notes that I’d added with the same. Fortunately, org-mode exposes the structure of the document’s headings and subheadings programatically, enabling me to create a helm popup with a filterable, interactive list of my existing entries.

I turned to org-capture templates. These allow you to add a new target to the org-capture window (default binding: C-c c).

1
2
3
4
5
6
7
8
9
10
11
12
13
(defun my/org-dir-file (name)
      "Prepend name with path to the org-directory root"
      (concat org-directory name))

(require 'helm-org)
(defun my/helm-in-org-buffer (filename)
    (interactive)
    (helm :sources
          (helm-source-org-headings-for-files (list filename))
        :candidate-number-limit 99999
        :truncate-lines helm-org-truncate-lines
        :preselect "Unfiled"
        :buffer "*helm org in buffers*"))
1
2
3
4
;; template
("q" "quote" item
 (function (lambda () (my/helm-in-org-buffer (my/org-dir-file "quotes.org"))))
 "   - %U: %? %(my/org-capture-clipboard-or-nil)\n")

Invoking the org-capture window with C-c c q will now bring me to a Helm window where I can filter down candidates or add it to the Unfiled section to organise in the future.