emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Org-mode macros with variable argument number
@ 2020-10-12 18:46 Holger Pirk
  0 siblings, 0 replies; only message in thread
From: Holger Pirk @ 2020-10-12 18:46 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 785 bytes --]

Hi folks,

For a while, I have been bugged by the fact that org-mode macros need to hardcode the arguments. Among other things, this prevents, macros with a variable number of arguments (arguably most useful for “(eval”-style lisp macros). Today, I finally implemented this feature (see attached patch). It simply substitutes $arg with a list of the macro arguments.

It allows macros like this one: 

#+MACRO: no-op (eval (string-join $args ","))

Which simple outputs its arguments but does not require calls to the macro to escape commas.

{{{no-op(Hi there, this macro does nothing)}}} -> Hi there, this macro does nothing

Can you have a look to see if this can be included in the org-mode distribution?

    many thanks and best regards,

        holger



[-- Attachment #2: 0001-add-args-placeholder-to-macros-to-capture-all-argume.patch --]
[-- Type: application/octet-stream, Size: 1547 bytes --]

From 52692c5091f8958690d22371eb2dc7bdec4dbbb5 Mon Sep 17 00:00:00 2001
From: Holger <holger@pirk.name>
Date: Mon, 12 Oct 2020 15:56:14 +0100
Subject: [PATCH] add $args placeholder to macros to capture all arguments in
 list

---
 lisp/org-macro.el | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/lisp/org-macro.el b/lisp/org-macro.el
index 5ddfae4e1..fafae953d 100644
--- a/lisp/org-macro.el
+++ b/lisp/org-macro.el
@@ -167,15 +167,18 @@ default value.  Return nil if no template was found."
       (let* ((eval? (string-match-p "\\`(eval\\>" template))
 	     (value
 	      (replace-regexp-in-string
-	       "\\$[0-9]+"
-	       (lambda (m)
-		 (let ((arg (or (nth (1- (string-to-number (substring m 1)))
-				     (org-element-property :args macro))
-				;; No argument: remove place-holder.
-				"")))
-		   ;; `eval' implies arguments are strings.
-		   (if eval? (format "%S" arg) arg)))
-	       template nil 'literal)))
+               "\\$args"
+               (format "(quote %S)" (org-element-property :args macro))
+	       (replace-regexp-in-string
+		"\\$[0-9]+"
+		(lambda (m)
+		  (let ((arg (or (nth (1- (string-to-number (substring m 1)))
+				      (org-element-property :args macro))
+				 ;; No argument: remove place-holder.
+				 "")))
+		    ;; `eval' implies arguments are strings.
+		    (if eval? (format "%S" arg) arg)))
+		template nil 'literal))))
         (when eval?
           (setq value (eval (condition-case nil (read value)
 			      (error (debug))))))
-- 
2.28.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-10-12 19:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-12 18:46 Org-mode macros with variable argument number Holger Pirk

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).