emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: orgmode <emacs-orgmode@gnu.org>
Subject: [PATCH] Possibility of using alternative separators in macros
Date: Fri, 30 Apr 2021 13:26:31 +0000	[thread overview]
Message-ID: <87wnsjx5rs.fsf@posteo.net> (raw)

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

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:

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}:

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 

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Alternative-args-separator-for-replacement-macros.patch --]
[-- Type: text/x-patch, Size: 2967 bytes --]

From 400d5779508fd7206a353bdb444c3cba382b8f01 Mon Sep 17 00:00:00 2001
From: juanmanuel <maciaschain@posteo.net>
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."
-    (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
 			 "[ \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)
-(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
   ;; Do not use `org-split-string' since empty strings are
   ;; meaningful here.
-    "\\(\\\\*\\),"
+    (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)

             reply	other threads:[~2021-04-30 13:29 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-30 13:26 Juan Manuel Macías [this message]
2021-05-01  8:30 ` [PATCH] Possibility of using alternative separators in macros Bastien
2021-05-01 10:04   ` Nicolas Goaziou
2021-05-01 10:17     ` Bastien
2021-05-01 10:18     ` Bastien
2021-05-01 21:50     ` Juan Manuel Macías
2021-05-02 21:08       ` Christian Moe
2021-05-12 11:49         ` Maxim Nikulin
2021-05-16 19:21           ` Christian Moe
2021-05-17 17:03             ` Maxim Nikulin
2021-05-17 18:51               ` Christian Moe
2021-05-02 12:13     ` Eric S Fraga
2021-05-11 11:01 ` Eric S Fraga
2021-05-11 16:12   ` Juan Manuel Macías
     [not found]     ` <87im3prvz8.fsf@ucl.ac.uk>
2021-05-11 18:25       ` Juan Manuel Macías
2021-05-15 13:29         ` Bastien
2021-05-15 20:14           ` Juan Manuel Macías
2021-05-15 20:25             ` Bastien
2021-05-15 21:05               ` Juan Manuel Macías
2021-05-16 12:17                 ` Bastien
2021-05-16 16:48                   ` Maxim Nikulin

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:

  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=87wnsjx5rs.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


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).