From 6e6462f6f0756e1b349ae450dce4783255287c77 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              | 26 +++++++++++++++++---------
 testing/lisp/test-org-macro.el | 17 ++++++++++++++++-
 3 files changed, 40 insertions(+), 15 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..1b3ee28f2d 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.
@@ -327,17 +327,25 @@ 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."
+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."
   (puthash name
-	   (cond ((not (org-string-nw-p reset))
+	   (cond ((not (org-string-nw-p action))
 		  (1+ (gethash name org-macro--counter-table 0)))
-		 ((string-match-p "\\`[ \t]*[0-9]+[ \t]*\\'" reset)
-		  (string-to-number reset))
+		 ((string-match-p "\\`\\(-\\|hold\\)\\'" action)
+		  (gethash name org-macro--counter-table 1))
+		 ((string-match-p "\\`[ \t]*[0-9]+[ \t]*\\'" action)
+		  (string-to-number action))
 		 (t 1))
 	   org-macro--counter-table))
 
diff --git a/testing/lisp/test-org-macro.el b/testing/lisp/test-org-macro.el
index 7356e98f52..eb96140899 100644
--- a/testing/lisp/test-org-macro.el
+++ b/testing/lisp/test-org-macro.el
@@ -210,7 +210,22 @@
             (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 9.2 9.2"
+          (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)}}}"
+		   " {{{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

