Hi all,
I would like to propose (patch attached) the possibility of using an
alternate character for separate arguments in replacement macros,
following a suggestion from Nicolas Goaziou in this (closed) thread:
https://orgmode.org/list/[email protected]/
The idea would be to explicitly indicate the separator just before the
parentheses. The allowed characters are any character other than a
letter, a number, a space, a dash, a low line or a parenthesis.
A new property `:sep' is added to `org-element-macro-parser', whose
default value is a comma.
Example of use. Suppose we define this macro:
#+MACRO: foo (eval (format "%s and %s" $1 $2))
Under normal conditions, the expected separator will be the comma:
{{{foo(x,z\, y)}}}
=> x and z, y
But we can also do this:
{{{foo@(x@z, y \@)}}}
=> x and z, y @
I think sometimes it may be preferable to separate the arguments by an
alternative character. For example, let's imagine we define a macro
(named 'lg') for LaTeX export, which admits two arguments, exactly the
same args as the Babel (LaTeX) macro \foreignlanguage{lang}{short-text}:
{{{lg(lang,short-text)}}}.
It would be much more comfortable something like:
{{{lg|(latin|trado, tradidi, traditur)}}}
instead of having to escape commas in:
{{{lg(latin,trado\, tradidi\, traditur)}}}
Best regards,
Juan Manuel
>From 400d5779508fd7206a353bdb444c3cba382b8f01 Mon Sep 17 00:00:00 2001
From: juanmanuel <[email protected]>
Date: Fri, 30 Apr 2021 14:45:54 +0200
Subject: [PATCH] Alternative args separator for replacement macros
---
lisp/org-element.el | 9 +++++++--
lisp/org-macro.el | 9 +++++----
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/lisp/org-element.el b/lisp/org-element.el
index a675bf512..34a9b880a 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -3279,21 +3279,25 @@ CONTENTS is the contents of the object, or nil."
"Parse macro at point, if any.
When at a macro, return a list whose car is `macro' and cdr
-a plist with `:key', `:args', `:begin', `:end', `:value' and
+a plist with `:key', `:args', `:begin', `:end', `:sep', `:value' and
`:post-blank' as keywords. Otherwise, return nil.
Assume point is at the macro."
(save-excursion
- (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\((\\([^\000]*?\\))\\)?}}}")
+ (when (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\([^a-zA-Z\s()]*[^-a-zA-Z0-9_\s]*\\)\\((\\([^\000]*?\\))\\)?}}}")
(let ((begin (point))
(key (downcase (match-string-no-properties 1)))
(value (match-string-no-properties 0))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point))
+ (sep (if (not (equal (match-string-no-properties 2) ""))
+ (match-string-no-properties 2)
+ ","))
(args (pcase (match-string-no-properties 3)
(`nil nil)
(a (org-macro-extract-arguments
+ sep
(replace-regexp-in-string
"[ \t\r\n]+" " " (org-trim a)))))))
(list 'macro
@@ -3302,6 +3306,7 @@ Assume point is at the macro."
:args args
:begin begin
:end end
+ :sep sep
:post-blank post-blank))))))
(defun org-element-macro-interpreter (macro _)
diff --git a/lisp/org-macro.el b/lisp/org-macro.el
index 29c403658..e047cd78e 100644
--- a/lisp/org-macro.el
+++ b/lisp/org-macro.el
@@ -294,20 +294,21 @@ of `org-macro-extract-arguments'."
nil t)
s)))))
-(defun org-macro-extract-arguments (s)
+(defun org-macro-extract-arguments (sep s)
"Extract macro arguments from string S.
S is a string containing comma separated values properly escaped.
-Return a list of arguments, as strings. This is the opposite of
+SEP is the character used to separate arguments. Return a list
+of arguments, as strings. This is the opposite of
`org-macro-escape-arguments'."
;; Do not use `org-split-string' since empty strings are
;; meaningful here.
(split-string
(replace-regexp-in-string
- "\\(\\\\*\\),"
+ (format "\\(\\\\*\\)%s" sep)
(lambda (str)
(let ((len (length (match-string 1 str))))
(concat (make-string (/ len 2) ?\\)
- (if (zerop (mod len 2)) "\000" ","))))
+ (if (zerop (mod len 2)) "\000" (format "%s" sep)))))
s nil t)
"\000"))
--
2.26.0