From c9275729659d403bd8f4a5921e1517c653eb96f6 Mon Sep 17 00:00:00 2001
From: Kaushal Modi <kaushal.modi@gmail.com>
Date: Wed, 14 Jun 2017 14:34:53 -0400
Subject: [PATCH] Add hold 'action' to the "n" macro

* lisp/org-macro.el (org-macro--counter-increment): Rename the
optional arg RESET to ACTION, as now that action can mean setting,
resetting or even holding the specified counter.  ACTION set to
"hold" or "-" will hold the previous value of the counter.

* doc/org.texi (Macro replacement): Document the new hold action.

* testing/lisp/test-org-macro.el (test-org-macro/n): Add new test for
the hold action.
---
 doc/org.texi                   | 12 +++++++-----
 lisp/org-macro.el              | 38 +++++++++++++++++++++++++-------------
 testing/lisp/test-org-macro.el | 18 +++++++++++++++++-
 3 files changed, 49 insertions(+), 19 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index 96aede2aa1..32cc1431b4 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -10867,15 +10867,17 @@ entry, that will be used instead.
 
 @item @{@{@{n@}@}@}
 @itemx @{@{@{n(@var{NAME})@}@}@}
-@itemx @{@{@{n(@var{NAME},@var{RESET})@}@}@}
+@itemx @{@{@{n(@var{NAME},@var{ACTION})@}@}@}
 @cindex n, macro
 @cindex counter, macro
 This macro implements custom counters by returning the number of times the
 macro has been expanded so far while exporting the buffer.  You can create
-more than one counter using different @var{NAME} values.  If @var{RESET} is
-non-empty, the specified counter is reset to the value specified if it is
-a number, or 1 otherwise.  You may leave @var{NAME} empty to reset the
-default counter.
+more than one counter using different @var{NAME} values.  If @var{ACTION} is
+@code{hold} or @code{-}, previous value of the counter is held, i.e. the
+specified counter is not incremented.  If the value is a number, the
+specified counter is set to that value.  If it is any other non-empty string,
+the specified counter is reset to 1.  You may leave @var{NAME} empty to reset
+the default counter.
 @end table
 
 The surrounding brackets can be made invisible by setting
diff --git a/lisp/org-macro.el b/lisp/org-macro.el
index 338c98811d..cdee7474d6 100644
--- a/lisp/org-macro.el
+++ b/lisp/org-macro.el
@@ -40,7 +40,7 @@
 ;;   {{{property(node-property)}}},
 ;;   {{{input-file}}},
 ;;   {{{modification-time(format-string)}}},
-;;   {{{n(counter,reset}}}.
+;;   {{{n(counter,action}}}.
 
 ;; Upon exporting, "ox.el" will also provide {{{author}}}, {{{date}}},
 ;; {{{email}}} and {{{title}}} macros.
@@ -63,6 +63,7 @@
 (declare-function vc-backend "vc-hooks" (f))
 (declare-function vc-call "vc-hooks" (fun file &rest args) t)
 (declare-function vc-exec-after "vc-dispatcher" (code))
+(declare-function string-trim "subr-x"  (string &optional trim-left trim-right))
 
 ;;; Variables
 
@@ -327,19 +328,30 @@ Return a list of arguments, as strings.  This is the opposite of
   "Initialize `org-macro--counter-table'."
   (setq org-macro--counter-table (make-hash-table :test #'equal)))
 
-(defun org-macro--counter-increment (name &optional reset)
+(defun org-macro--counter-increment (name &optional action)
   "Increment counter NAME.
-NAME is a string identifying the counter.  When non-nil, optional
-argument RESET is a string.  If it represents an integer, set the
-counter to this number.  Any other non-empty string resets the
-counter to 1."
-  (puthash name
-	   (cond ((not (org-string-nw-p reset))
-		  (1+ (gethash name org-macro--counter-table 0)))
-		 ((string-match-p "\\`[ \t]*[0-9]+[ \t]*\\'" reset)
-		  (string-to-number reset))
-		 (t 1))
-	   org-macro--counter-table))
+NAME is a string identifying the counter.
+
+When non-nil, optional argument ACTION is a string.
+
+If the string is \"hold\" or \"-\", keep the NAME counter at its
+current value, i.e. do not increment.
+
+If the string represents an integer, set the counter to this number.
+
+Any other non-empty string resets the counter to 1."
+  (let ((action-trimmed (when (org-string-nw-p action)
+			  (require 'subr-x)
+			  (string-trim action))))
+    (puthash name
+	     (cond ((not (org-string-nw-p action-trimmed))
+		    (1+ (gethash name org-macro--counter-table 0)))
+		   ((string-match-p "\\`\\(-\\|hold\\)\\'" action-trimmed)
+		    (gethash name org-macro--counter-table 1))
+		   ((string-match-p "\\`[0-9]+\\'" action-trimmed)
+		    (string-to-number action-trimmed))
+		   (t 1))
+	     org-macro--counter-table)))
 
 
 (provide 'org-macro)
diff --git a/testing/lisp/test-org-macro.el b/testing/lisp/test-org-macro.el
index 7356e98f52..84f092ed44 100644
--- a/testing/lisp/test-org-macro.el
+++ b/testing/lisp/test-org-macro.el
@@ -210,7 +210,23 @@
             (org-macro-initialize-templates)
             (org-macro-replace-all org-macro-templates)
             (buffer-substring-no-properties
-             (line-beginning-position) (line-end-position))))))
+             (line-beginning-position) (line-end-position)))))
+  ;; Second argument set to "-" or "hold" holds the counter value.
+  (should
+   (equal "1.1 2.2 8.3 8.1 8.2 8.3 9.3 9.3"
+          (org-test-with-temp-text
+	   (concat "{{{n(,-)}}}.{{{n(c)}}}" ;Hold before even starting the counter
+		   " {{{n}}}.{{{n(c)}}}"    ;Increment after hold
+		   " {{{n(,8)}}}.{{{n(c)}}}"
+		   " {{{n(,hold)}}}.{{{n(c,reset)}}}" ;Alternative hold arg
+		   " {{{n(, - )}}}.{{{n(c)}}}"	      ;With spaces
+		   " {{{n(, hold )}}}.{{{n(c)}}}"     ;With spaces
+		   " {{{n}}}.{{{n(c,hold)}}}" ;Hold on another counter
+		   " {{{n(,hold)}}}.{{{n(c,-)}}}") ;Hold on both counters
+           (org-macro-initialize-templates)
+           (org-macro-replace-all org-macro-templates)
+           (buffer-substring-no-properties
+            (line-beginning-position) (line-end-position))))))
 
 (ert-deftest test-org-macro/escape-arguments ()
   "Test `org-macro-escape-arguments' specifications."
-- 
2.13.0

