about summary refs log tree commit diff stats
path: root/lisp/+org-capture.el
blob: 2f7bf6aa230af96bf8f5a9cd6c05fb761dfe826e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
;;; +org-capture.el  -*- lexical-binding: t; -*-

;;; Code:

(require 'cl-lib)
;; Don't /require/ `org-capture', since that'll pull in all of `org' and that'll
;; take a minute.  Just let the compiler know that this variable exists.
(defvar org-capture-templates nil)

;; https://github.com/cadadr/configuration/blob/39813a771286e542af3aa333172858532c3bb257/emacs.d/gk/gk-org.el#L1573
(defun +org-capture-template-define (description &rest args)
  "Define a capture template.
Creates a list and adds it to `org-capture-templates', if it's
not already there.  ARGS is a plist, which in addition to the
additional options `org-capture-templates' accepts (which see),
takes the following and puts them in the right spot: `:keys',
`:description', `:type', `:target', and `:template'."
  (declare (indent 1))
  (let* ((keys (plist-get args :keys))
         (type (plist-get args :type))
         (target (plist-get args :target))
         (template (plist-get args :template))
         (template-value (append
                          (list description)
                          (when (or type target template)
                            (list (or type 'entry) target template))
                          (cl-loop for i from 0 below (length args) by 2
                                   unless (member (nth i args)
                                                  '(:keys :description :type
                                                          :target :template))
                                   append (list (nth i args)
                                                (plist-get args (nth i
                                                                     args)))))))
    ;; The only way I know how to do this properly (add a value to the end of
    ;; the list, if it exists; otherwise update it) is to do this weird if-setf
    ;; dance.
    (if (seq-find (lambda (el) (equal (car el) keys))
                  org-capture-templates)
        (setf (alist-get keys org-capture-templates nil nil #'equal)
              template-value)
      (setf org-capture-templates
            (append org-capture-templates
                    (list (cons keys template-value)))))
    ;; Regardless of what we do, return the new value of
    ;; `org-capture-templates'.
    org-capture-templates))

(provide '+org-capture)
;;; +org-capture.el