From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kaushal Modi Subject: Add an optional HOLD argument to "n" Org macro (Was: [RFC] The "c" Org macro) Date: Wed, 14 Jun 2017 18:33:50 +0000 Message-ID: References: <2ee94a64a94b46259b0da6e7d34675c9@HE1PR01MB1898.eurprd01.prod.exchangelabs.com> <87y3u7o3dj.fsf@t3610> <87pofjtk4b.fsf@t3610> <2069df8c23bc43f3b04b6e203b96be9d@HE1PR01MB1898.eurprd01.prod.exchangelabs.com> <87r2zvpyst.fsf@delle7240> <8760guib5i.fsf@nicolasgoaziou.fr> <87fuftb4lg.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a114b9742d3fc990551efcb12" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:37143) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLD7F-0000N0-Mn for emacs-orgmode@gnu.org; Wed, 14 Jun 2017 14:34:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dLD7E-0002Bo-7I for emacs-orgmode@gnu.org; Wed, 14 Jun 2017 14:34:05 -0400 Received: from mail-lf0-x22a.google.com ([2a00:1450:4010:c07::22a]:35632) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dLD7D-0002BI-Ri for emacs-orgmode@gnu.org; Wed, 14 Jun 2017 14:34:04 -0400 Received: by mail-lf0-x22a.google.com with SMTP id p189so7082440lfe.2 for ; Wed, 14 Jun 2017 11:34:03 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Nicolas Goaziou , emacs-orgmode@gnu.org --001a114b9742d3fc990551efcb12 Content-Type: text/plain; charset="UTF-8" I was writing something along the lines of this when I needed to specify the n macro to hold its previous value and not increment at all. ===== * Week {{{n(wk)}}}: Python Basics # 1 ** {{{n}}}. Introduction to Python # 1 ** {{{n}}}. Core Elements of Programs # 2 *** {{{n(,,hold)}}}.{{{n(topic,reset)}}}: Bindings # 2.1 *** {{{n(,,hold)}}}.{{{n(topic)}}}: Strings # 2.2 *** {{{n(,,hold)}}}.{{{n(topic)}}}: Input/Output # 2.3 * Week {{{n(wk)}}}: Simple Programs # 2 ** {{{n}}}. Simple Algorithms # 3 ** {{{n}}}. Functions # 4 * Week {{{n(wk)}}}: Structured Types # 3 ** {{{n}}}. Tuples and Lists # 5 ** {{{n}}}. Dictionaries # 6 ===== Below is an informal diff to show how the hold feature was implemented. diff --git a/lisp/org-macro.el b/lisp/org-macro.el index 338c98811d..cc74cebd0a 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,reset,hold}}}. ;; Upon exporting, "ox.el" will also provide {{{author}}}, {{{date}}}, ;; {{{email}}} and {{{title}}} macros. @@ -163,7 +163,7 @@ function installs the following ones: \"property\", ;; Initialize and install "n" macro. (org-macro--counter-initialize) (funcall update-templates - (cons "n" "(eval (org-macro--counter-increment \"$1\" \"$2\"))")) + (cons "n" "(eval (org-macro--counter-increment \"$1\" \"$2\" \"$3\"))")) (setq org-macro-templates templates))) (defun org-macro-expand (macro templates) @@ -327,14 +327,20 @@ 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 reset hold) "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 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. + +When non-nil, optional argument HOLD is a string. If it is a +non-empty string, keep the NAME counter at its previous value." (puthash name - (cond ((not (org-string-nw-p reset)) + (cond ((org-string-nw-p hold) + (gethash name org-macro--counter-table 0)) + ((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)) I haven't yet created a proper formatted patch, because I don't know if this is the best implementation. Would it be better to rename the RESET option to something more generic, like ARG? That way, - If ARG is a "-" string, the counter value will be held and not incremented. - If ARG is a number represented as a string, it set the NAME counter to that value (like now). - Else if ARG is a non-empty string, it resets the NAME counter to 1 (like now). -- Kaushal Modi --001a114b9742d3fc990551efcb12 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I was writing something along the lines of this when I nee= ded to specify the n macro to hold its previous value and not increment at = all.

=3D=3D=3D=3D=3D
* Week {{{n(wk)}}}: = Python Basics
# 1
** {{{n}}}. Introduction to Python
#=C2=A01
** {{{n}}}. Core Elements of Programs
#=C2=A02
*** {{{n(,,= hold)}}}.{{{n(topic,reset)}}}: Bindings
#=C2=A02.1
*** {{{n(,,hold)}}}.{{= {n(topic)}}}: Strings
#=C2=A02.2
*** {{{n(,,hold)}}}.{{{n(topic)}}}: Inpu= t/Output
#=C2= =A02.3
* Week {{{n(wk)}}}: Simple Programs
#= =C2=A02
= ** {{{n}}}. Simple Algorithms
#=C2=A03
** {{{n}}}. Functions<= /div>
#= =C2=A04
* Week {{{n(wk)}}}: Structured Types
#=C2=A03<= /div>
** {{{n}}}. Tuples and Lists
#=C2=A05
** {{{n}}}. D= ictionaries
#=C2=A06
=3D=3D=3D=3D=3D

Below is an informal diff to show how the hold feature was implement= ed.

diff --git a/lisp/org-macro.el b/lisp/org-macr= o.el
index 338c98811d..cc74cebd0a 100644
--- a= /lisp/org-macro.el
+++ b/lisp/org-macro.el
@@ -40,7 +40= ,7 @@
=C2=A0;; =C2=A0 {{{property(node-property)}}},
= =C2=A0;; =C2=A0 {{{input-file}}},
=C2=A0;; =C2=A0 {{{modification= -time(format-string)}}},
-;; =C2=A0 {{{n(counter,reset}}}.
<= div>+;; =C2=A0 {{{n(counter,reset,hold}}}.
=C2=A0
=C2= =A0;; Upon exporting, "ox.el" will also provide {{{author}}}, {{{= date}}},
=C2=A0;; {{{email}}} and {{{title}}} macros.
@= @ -163,7 +163,7 @@ function installs the following ones: \"property\&q= uot;,
=C2=A0 =C2=A0 =C2=A0;; Initialize and install "n"= macro.
=C2=A0 =C2=A0 =C2=A0(org-macro--counter-initialize)
=
=C2=A0 =C2=A0 =C2=A0(funcall update-templates
- =C2=A0 =C2=A0 (cons = "n" "(eval (org-macro--counter-increment \"$1\" \&= quot;$2\"))"))
+ =C2=A0 =C2=A0 (cons "n" "(eval (o= rg-macro--counter-increment \"$1\" \"$2\" \"$3\&qu= ot;))"))
=C2=A0 =C2=A0 =C2=A0(setq org-macro-templates templ= ates)))
=C2=A0
=C2=A0(defun org-macro-expand (macro tem= plates)
@@ -327,14 +327,20 @@ Return a list of arguments, as stri= ngs.=C2=A0 This is the opposite of
=C2=A0 =C2=A0"Initialize = `org-macro--counter-table'."
=C2=A0 =C2=A0(setq org-macr= o--counter-table (make-hash-table :test #'equal)))
=C2=A0
-(defun org-macro--counter-increment (name &optional reset)
+(defun org-macro--counter-increment (name &optional reset hold)<= /div>
=C2=A0 =C2=A0"Increment counter NAME.
-NAME is a s= tring identifying the counter.=C2=A0 When non-nil, optional
-argu= ment RESET is a string.=C2=A0 If it represents an integer, set the
-counter to this number.=C2=A0 Any other non-empty string resets the
-counter to 1."
+NAME is a string identifying the coun= ter.
+
+When non-nil, optional argument RESET is a stri= ng.=C2=A0 If it represents
+an integer, set the counter to this n= umber.=C2=A0 Any other non-empty
+string resets the counter to 1.=
+
+When non-nil, optional argument HOLD is a string.= =C2=A0 If it is a
+non-empty string, keep the NAME counter at its= previous value."
=C2=A0 =C2=A0(puthash name
- =C2=A0 (cond = ((not (org-string-nw-p reset))
+ =C2=A0 (cond ((org-string-nw-p hold)
=
+ = =C2=A0(gethash name org-macro--counter-table 0))
+ ((not (org-string-nw-p = reset))
=C2=A0 =C2=A0(1+ (gethash name org-macro--counter-table 0)))
=
=C2=A0 ((string-match-p "\\`[ \t]*[0-9]+[ \t]*\\'" reset)
=C2=A0 = =C2=A0(string-to-number reset))


<= div>
I haven't yet created a proper formatted patch, because I don&= #39;t know if this is the best implementation. Would it be better to rename= the RESET option to something more generic, like ARG? That way,
=
- If ARG is a "-" string, the counter value will b= e held and not incremented.=C2=A0
- If ARG is a number represente= d as a string, it set the NAME counter to that value (like now).
= - Else if ARG is a non-empty string, it resets the NAME counter to 1 (like = now).

=
--

Kaushal Modi

--001a114b9742d3fc990551efcb12--