Hi guys,

I was playing with org-capture today, and it strike me that it could be used as
a simple and lightweight alternative to create GUIs for user options, somewhat
resembling the use of well-known hydra/transient or built-in help macro or easy
menu.

I would like to propose a small 'feature/change' to org-capture templates, to
inlude a new target: 'exec'. It should be followed by a function to be
executed.

I believe that this is a simple change that does not intrude on anything else in
org-capture, you can see the attached patch. The goal is to just circumwent the
createion of capture buffer, bookmark and other processing done by org-capture
etc. However there might be things I am not aware of, so if someone have more
insight, it is welcomed to hear.

There is a small illustration in the attached patch as well.

The error handling could probably be much better, so I would like to hear
opinion and suggestion how can I improve it.

best regards
/arthur

>From b8a910235c688d9ea1cb717a186699da489987af Mon Sep 17 00:00:00 2001
From: Arthur Miller <arthur.mil...@live.com>
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

Reply via email to