emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Kaushal Modi <kaushal.modi@gmail.com>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: emacs-orgmode@gnu.org
Subject: Re: Add an optional HOLD argument to "n" Org macro
Date: Thu, 15 Jun 2017 18:07:19 +0000	[thread overview]
Message-ID: <CAFyQvY3FaNXNmYpp92TPy1ZkBwKNfPPjM1+JMSmvxRC1jQcYzQ@mail.gmail.com> (raw)
In-Reply-To: <871sqli69r.fsf@nicolasgoaziou.fr>


[-- Attachment #1.1: Type: text/plain, Size: 659 bytes --]

On Thu, Jun 15, 2017 at 12:07 PM Nicolas Goaziou <mail@nicolasgoaziou.fr>
wrote:

> We do not need this dependency. In particular, there is already
> `org-trim'.
>

OK, switch from string-trim to org-trim. Thanks.


> It is confusing to provide two ways to achieve the same action. I'd
> rather have "-" only.
>

OK.


> Could you split this into smaller tests, each one testing one feature?
>

Done. I ended up writing many more tests in the process. While I was doing
that, I realized that only the first arg NAME was ws (whitespace) sensitive
while the second arg wasn't. Now ws trimming is done on both args.

Revised patch is attached.
-- 

Kaushal Modi

[-- Attachment #1.2: Type: text/html, Size: 1359 bytes --]

[-- Attachment #2: 0001-Add-hold-action-to-the-n-macro-and-ws-trim-all-n-mac.patch --]
[-- Type: application/octet-stream, Size: 9420 bytes --]

From 0f96645aaf72d68fd7096e4135ab06e554296f46 Mon Sep 17 00:00:00 2001
From: Kaushal Modi <kaushal.modi@gmail.com>
Date: Thu, 15 Jun 2017 14:00:07 -0400
Subject: [PATCH] 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.
---
 doc/org.texi                   | 11 +++---
 lisp/org-macro.el              | 38 +++++++++++-------
 testing/lisp/test-org-macro.el | 87 +++++++++++++++++++++++++++++++++++++++---
 3 files changed, 113 insertions(+), 23 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index 5ba7d06bbc..110510a230 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 338c98811d..d18bf60bc7 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,31 @@ 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 ((action-trimmed (when (org-string-nw-p action)
+                          (org-trim action)))
+        (name-trimmed (when (org-string-nw-p name)
+                        (org-trim name))))
+    (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 7356e98f52..664bc88dfb 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
@@ -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))))))
 
-- 
2.13.0


  reply	other threads:[~2017-06-15 18:07 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <2ee94a64a94b46259b0da6e7d34675c9@HE1PR01MB1898.eurprd01.prod.exchangelabs.com>
2017-05-08 14:00 ` [RFC] The "c" Org macro Eric S Fraga
2017-05-08 15:32   ` Dushyant Juneja
     [not found]   ` <a4c6d561b12a4cc8ad4fe8c017fa2121@HE1PR01MB1898.eurprd01.prod.exchangelabs.com>
2017-05-08 15:59     ` Eric S Fraga
2017-05-08 16:52       ` Nicolas Goaziou
2017-05-09  7:35         ` Carsten Dominik
2017-05-09 10:35           ` Nicolas Goaziou
2017-05-09 11:25         ` Rasmus
2017-05-09 16:10           ` Nicolas Goaziou
     [not found]       ` <2069df8c23bc43f3b04b6e203b96be9d@HE1PR01MB1898.eurprd01.prod.exchangelabs.com>
2017-05-11  8:45         ` Eric S Fraga
2017-05-21 13:37           ` Nicolas Goaziou
2017-05-22  3:24             ` Kaushal Modi
2017-05-22  5:58               ` Nicolas Goaziou
2017-05-22 10:46                 ` Kaushal Modi
2017-05-22 11:47                   ` Nicolas Goaziou
2017-05-22 13:00                     ` Kaushal Modi
2017-05-22 13:10                       ` Kaushal Modi
2017-05-22 13:13                       ` Nicolas Goaziou
2017-05-22 13:39                         ` Kaushal Modi
2017-05-25 10:42             ` Nicolas Goaziou
2017-05-25 18:31               ` Kaushal Modi
2017-06-14 17:52                 ` Kaushal Modi
2017-06-14 18:33                   ` Add an optional HOLD argument to "n" Org macro (Was: [RFC] The "c" Org macro) Kaushal Modi
2017-06-14 19:47                     ` Add an optional HOLD argument to "n" Org macro Nicolas Goaziou
2017-06-15 13:10                       ` Kaushal Modi
2017-06-15 15:25                         ` Kaushal Modi
2017-06-15 16:07                           ` Nicolas Goaziou
2017-06-15 18:07                             ` Kaushal Modi [this message]
2017-06-17 14:34                               ` Kaushal Modi
2017-06-17 23:24                               ` Nicolas Goaziou
2017-06-18  4:03                                 ` Kaushal Modi
2017-06-18  7:16                                   ` Nicolas Goaziou
2017-06-18  7:45                                     ` Kaushal Modi
2017-06-14 19:44                   ` [RFC] The "c" " Nicolas Goaziou
     [not found]           ` <a8f5841641834b4cb51af85a3df785da@HE1PR01MB1898.eurprd01.prod.exchangelabs.com>
2017-05-22  8:34             ` Eric S Fraga
2017-05-08 16:30   ` Robert Horn

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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  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=CAFyQvY3FaNXNmYpp92TPy1ZkBwKNfPPjM1+JMSmvxRC1jQcYzQ@mail.gmail.com \
    --to=kaushal.modi@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* 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

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

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