emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Holger Pirk <holger@pirk.name>
To: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Org-mode macros with variable argument number
Date: Mon, 12 Oct 2020 18:46:41 +0000	[thread overview]
Message-ID: <B2235314-8068-42B1-8DFC-60E84964DDFC@pirk.name> (raw)

[-- 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


                 reply	other threads:[~2020-10-12 19:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=B2235314-8068-42B1-8DFC-60E84964DDFC@pirk.name \
    --to=holger@pirk.name \
    --cc=emacs-orgmode@gnu.org \
    --subject='Re: Org-mode macros with variable argument number' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this 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).