Browse Source

Add hold 'action' to the "n" macro and ws-trim all "n" macro args

* 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
"-" will hold the previous value of the counter.  White-space is
now trimmed from the NAME arg too.

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

* testing/lisp/test-org-macro.el (test-org-macro/n): Add new tests for
the hold action.
Kaushal Modi 7 years ago
parent
commit
d48cfdf68b
3 changed files with 112 additions and 23 deletions
  1. 6 5
      doc/org.texi
  2. 24 13
      lisp/org-macro.el
  3. 82 5
      testing/lisp/test-org-macro.el

+ 6 - 5
doc/org.texi

@@ -10867,15 +10867,16 @@ 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{-}, 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

+ 24 - 13
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,19 +327,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 \"-\", 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 ((name-trimmed (org-trim name))
+        (action-trimmed (when (org-string-nw-p action)
+                          (org-trim action))))
+    (puthash name-trimmed
+             (cond ((not (org-string-nw-p action-trimmed))
+                    (1+ (gethash name-trimmed org-macro--counter-table 0)))
+                   ((string= "-" action-trimmed)
+                    (gethash name-trimmed 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)

+ 82 - 5
testing/lisp/test-org-macro.el

@@ -180,17 +180,51 @@
             (org-macro-replace-all org-macro-templates)
             (buffer-substring-no-properties
              (line-beginning-position) (line-end-position)))))
-  ;; Tolerate spaces in second argument.
+  ;; Check that reset happens when the second argument is neither "-"
+  ;; nor a number.
   (should
-   (equal "9 10"
-          (org-test-with-temp-text "{{{n(c, 9)}}} {{{n(c)}}}"
+   (equal "9 1 1 1"
+          (org-test-with-temp-text
+	      (concat "{{{n(c,9)}}} {{{n(c,reiniciar)}}}"
+		      " {{{n(c,réinitialiser)}}} {{{n(c,zurückstellen)}}}")
             (org-macro-initialize-templates)
             (org-macro-replace-all org-macro-templates)
             (buffer-substring-no-properties
              (line-beginning-position) (line-end-position)))))
+  ;; Tolerate spaces in first argument.
   (should
-   (equal "9 1"
-          (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c, reset)}}}"
+   (equal "1 2 3 4"
+          (org-test-with-temp-text "{{{n(c)}}} {{{n(c )}}} {{{n( c)}}} {{{n( c )}}}"
+            (org-macro-initialize-templates)
+            (org-macro-replace-all org-macro-templates)
+            (buffer-substring-no-properties
+             (line-beginning-position) (line-end-position)))))
+  ;; Tolerate spaces when second argument is an integer.
+  (should
+   (equal "2 3 5 7"
+          (org-test-with-temp-text
+	      (concat "{{{n(c,2)}}} {{{n(c, 3)}}}"
+		      " {{{n(c,5 )}}} {{{n(c, 7 )}}}")
+            (org-macro-initialize-templates)
+            (org-macro-replace-all org-macro-templates)
+            (buffer-substring-no-properties
+             (line-beginning-position) (line-end-position)))))
+  ;; Tolerate spaces when second argument is the hold argument.
+  (should
+   (equal "7 7 8 8 9 9"
+          (org-test-with-temp-text
+	      (concat "{{{n(,7)}}} {{{n(, -)}}}"
+		      " {{{n}}} {{{n(,- )}}} {{{n}}} {{{n(, - )}}}")
+            (org-macro-initialize-templates)
+            (org-macro-replace-all org-macro-templates)
+            (buffer-substring-no-properties
+             (line-beginning-position) (line-end-position)))))
+  ;; Tolerate spaces when second argument is used to reset the counter.
+  (should
+   (equal "9 1 1 1 1"
+          (org-test-with-temp-text
+	      (concat "{{{n(c,9)}}} {{{n(c,reset)}}} {{{n(c, reset)}}}"
+		      " {{{n(c,reset )}}} {{{n(c, reset )}}}")
             (org-macro-initialize-templates)
             (org-macro-replace-all org-macro-templates)
             (buffer-substring-no-properties
@@ -209,6 +243,49 @@
           (org-test-with-temp-text "{{{n(c,2)}}} {{{n(c,)}}}"
             (org-macro-initialize-templates)
             (org-macro-replace-all org-macro-templates)
+            (buffer-substring-no-properties
+             (line-beginning-position) (line-end-position)))))
+  ;; Hold value at reset value of 1 if the counter hasn't yet started.
+  (should
+   (equal "1"
+          (org-test-with-temp-text "{{{n(,-)}}}"
+            (org-macro-initialize-templates)
+            (org-macro-replace-all org-macro-templates)
+            (buffer-substring-no-properties
+             (line-beginning-position) (line-end-position)))))
+  ;; Increment counter following a hold.
+  (should
+   (equal "1 1 2"
+          (org-test-with-temp-text "{{{n}}} {{{n(,-)}}} {{{n}}}"
+            (org-macro-initialize-templates)
+            (org-macro-replace-all org-macro-templates)
+            (buffer-substring-no-properties
+             (line-beginning-position) (line-end-position)))))
+  ;; Hold counter value following a counter value set.
+  (should
+   (equal "1 10 10"
+          (org-test-with-temp-text "{{{n}}} {{{n(,10)}}} {{{n(,-)}}}"
+            (org-macro-initialize-templates)
+            (org-macro-replace-all org-macro-templates)
+            (buffer-substring-no-properties
+             (line-beginning-position) (line-end-position)))))
+  ;; Hold counter value in a multiple-counter situation.
+  (should
+   (equal "1.1 1.2 1.3"
+          (org-test-with-temp-text
+	      "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
+            (org-macro-initialize-templates)
+            (org-macro-replace-all org-macro-templates)
+            (buffer-substring-no-properties
+             (line-beginning-position) (line-end-position)))))
+  ;; Hold counter values on one or multiple counters at the same time.
+  (should
+   (equal "1.1 1.2 2.2 2.2"
+          (org-test-with-temp-text
+	      (concat "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
+		      " {{{n}}}.{{{n(c,-)}}} {{{n(,-)}}}.{{{n(c,-)}}}")
+            (org-macro-initialize-templates)
+            (org-macro-replace-all org-macro-templates)
             (buffer-substring-no-properties
              (line-beginning-position) (line-end-position))))))