Add `+org-define-capture-template'

This commit is contained in:
Case Duckworth 2022-01-31 13:55:00 -06:00
parent f37cb20764
commit 5bb0040b8a
1 changed files with 60 additions and 1 deletions

View File

@ -98,8 +98,67 @@ properly process the variable."
(setf (+org-capture--get ek (symbol-value list))
(list (format "(Group %s)" ek)))))))
(defun +org-define-capture-template (keys title &rest args)
(defcustom +org-capture-default-type 'entry
"Default template for `org-capture-templates'."
:type '(choice (const :tag "Entry" entry)
(const :tag "Item" item)
(const :tag "Check Item" checkitem)
(const :tag "Table Line" table-line)
(const :tag "Plain Text" plain)))
(defcustom +org-capture-default-target ""
"Default target for `org-capture-templates'."
;; TODO: type
)
(defcustom +org-capture-default-template nil
"Default template for `org-capture-templates'."
;; TODO: type
)
(defun +org-define-capture-templates-group (keys description)
"Add a group title to `org-capture-templates'."
(setf (+org-capture--get keys org-capture-templates)
(list description)))
;; [[https://github.com/cadadr/configuration/blob/39813a771286e542af3aa333172858532c3bb257/emacs.d/gk/gk-org.el#L1573][from cadadr]]
(defun +org-define-capture-template (keys description &rest args)
"Define a capture template and necessary antecedents.
ARGS is a plist, which in addition to the additional options
`org-capture-templates' accepts, takes the following and places
them accordingly: :type, :target, and :template. Each of these
corresponds to the same field in `org-capture-templates's
docstring, which see. Likewise with KEYS and DESCRIPTION, which
are passed separately to the function.
This function will also create all the necessary intermediate
capture keys needed for `org-capture'; that is, if KEYS is
\"wcp\", entries for \"w\" and \"wc\" will both be ensured in
`org-capture-templates'."
(declare (indent 2))
;; Check for existence of parent groups
(when (> (length keys) 1)
(let ((expected (cl-loop for i from 1 to (1- (length keys))
collect (substring 0 i) into keys
finally return keys)))
(cl-loop
for ek in expected
if (not (+org-capture--get ek org-capture-templates))
do (+org-define-capture-templates-group ek (format "(Group %s)" ek)))))
(if (null args)
;; Add the title
(+org-define-capture-templates-group keys description)
;; Add the capture template.
(setf (+org-capture--get keys org-capture-templates)
(append (list (or (plist-get args :type)
+org-capture-default-type)
(or ( plist-get args :target)
+org-capture-default-target)
(or (plist-get args :template)
+org-capture-default-template))
(cl-loop for (key val) on args by #'cddr
unless (member key '(:type :target :template))
append (list key val))))))
(provide '+org-capture)
;;; +org-capture.el ends here