diff options
author | Kaushal Modi <kaushal.modi@gmail.com> | 2017-06-17 22:12:20 -0400 |
---|---|---|
committer | Kaushal Modi <kaushal.modi@gmail.com> | 2017-06-17 23:46:24 -0400 |
commit | d48cfdf68b659a1ffd6136565d27c4a9191427ef (patch) | |
tree | 200d4000d4d1e8a1484dd3cca420b79df0ed1e79 | |
parent | 27c2ae47fc106065bb7a444abc04d5a3c3d9723a (diff) | |
download | org-mode-d48cfdf68b659a1ffd6136565d27c4a9191427ef.tar.gz |
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.
-rw-r--r-- | doc/org.texi | 11 | ||||
-rw-r--r-- | lisp/org-macro.el | 37 | ||||
-rw-r--r-- | testing/lisp/test-org-macro.el | 87 |
3 files changed, 112 insertions, 23 deletions
diff --git a/doc/org.texi b/doc/org.texi index 5ba7d06..110510a 100644 --- a/doc/org.texi +++ b/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 diff --git a/lisp/org-macro.el b/lisp/org-macro.el index 338c988..67d71b0 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,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) diff --git a/testing/lisp/test-org-macro.el b/testing/lisp/test-org-macro.el index 7356e98..664bc88 100644 --- a/testing/lisp/test-org-macro.el +++ b/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 @@ -210,6 +244,49 @@ (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)))))) (ert-deftest test-org-macro/escape-arguments () |