From b8a910235c688d9ea1cb717a186699da489987af Mon Sep 17 00:00:00 2001 From: Arthur Miller Date: Thu, 26 May 2022 17:15:37 +0200 Subject: [PATCH] Introduce 'executable' org-capture-templates * etc/ORG-NEWS: Documented new feature. * doc/misc/org.org: Documented new template target. * lisp/org/org-capture.el: 'org-capture' add handler for 'exec' target. --- doc/misc/org.org | 14 ++++++++++++++ etc/ORG-NEWS | 28 ++++++++++++++++++++++++++++ lisp/org/org-capture.el | 5 +++++ 3 files changed, 47 insertions(+) diff --git a/doc/misc/org.org b/doc/misc/org.org index 3dce83c936..2445c57942 100644 --- a/doc/misc/org.org +++ b/doc/misc/org.org @@ -7629,6 +7629,20 @@ Now lets look at the elements of a template definition. Each entry in the target entry or as a top-level entry. The target file should be an Org file. + - ~exec~ :: + + An executable function. Function will be executed and the result + returned immidiately. No further processing by org-capture will be + performed, no capture buffer will be created, no last capture + bookmark etc, will be created. The eventual other template + parameters are ignored. Use this when you need to execute a Lisp + function for the side effects. Useful if you would like to create + lightweight GUIs for user choices based on org-capture mechanism. + + Simple example: + + '("h" "Say hello" exec (lambda () (message "Hello, World!"))) + - ~item~ :: A plain list item, placed in the first plain list at the target diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 37a39131d9..53ac10ba45 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -108,6 +108,34 @@ If you prefer to keep the keybinding, you can add it back to #+end_src ** New features +*** 'Executable' org-capture-templates + +New target, "exec" is added to templates which provides an easy option +to run a function from within org-capture dialog. It is meant as a +lightweight option to create GUIs for user options based on +org-capture mechanism. + +Exec should be followed by a lisp function, a lambda or a function +object that will be executed and result returned without further +processing by org-capture. As a consequence other template parameters +are not used with this target. + +Illustration: + +#+begin_src emacs-lisp +(defvar my-templates + `(("h" "Hello World" exec + (lambda () + (message "Hello, World"))) + ("f" "Find file" exec ,(function find-file)))) + +(defun simple-menu () + (interactive) + (let ((org-capture-templates my-templates)) + (org-capture))) + +(define-key global-map (kbd "C-S-n") #'simple-menu) +#+end_src *** New citation engine diff --git a/lisp/org/org-capture.el b/lisp/org/org-capture.el index 2fd9a9c74d..1a9b59de2f 100644 --- a/lisp/org/org-capture.el +++ b/lisp/org/org-capture.el @@ -665,6 +665,11 @@ org-capture (remove-text-properties 0 (length annotation) '(read-only t) annotation)) (cond + ((equal (nth 2 entry) 'exec) + (let ((f (plist-get entry 'exec))) + (if (not f) (error "Missing function specification.") + (if (commandp f) (call-interactively f) + (if (functionp f) (funcall f) (error "Invalid function specification.")))))) ((equal entry "C") (customize-variable 'org-capture-templates)) ((equal entry "q") -- 2.36.1