From: Nicolas Goaziou <mail@nicolasgoaziou.fr>
To: Org Mode List <emacs-orgmode@gnu.org>
Subject: [RFC] The "c" Org macro
Date: Mon, 08 May 2017 13:26:00 +0200 [thread overview]
Message-ID: <87o9v3614n.fsf@nicolasgoaziou.fr> (raw)
[-- Attachment #1: Type: text/plain, Size: 821 bytes --]
Hello,
I would like to scratch a long-standing itch and introduce the "c"
macro, which is basically a way to handle multiple counters in an Org
document. So, the following document
* Part {{{c}}}
* Part {{{c}}}
* Part {{{c(other)}}}
* Part {{{c}}}
* Part {{{c(other)}}}
is expanded as
* Part 1
* Part 2
* Part 1
* Part 3
* Part 2
Initially, I wanted to name it "#", but macro names must start with
[a-zA-Z]. I also wanted something short, hence the "c". It could also be
"n". Feel free to suggest something better.
Anyway, I attach an implementation for the feature along with
a documentation update. If the feature is to be included in Org, I'll
also write tests and an ORG-NEWS entry.
Feedback welcome.
Regards,
--
Nicolas Goaziou 0x80A93738
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: c macro implementation --]
[-- Type: text/x-diff, Size: 4158 bytes --]
From 581a66bbf3c808e906d275c3411f640428552c39 Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Date: Mon, 8 May 2017 12:38:38 +0200
Subject: [PATCH] org-macro: Implement the "c" macro
* lisp/org-macro.el (org-macro--counter-table): New variable.
(org-macro--counter-initialize):
(org-macro--counter-increment): New functions.
(org-macro-initialize-templates): Use new functions.
* doc/org.texi (Macro replacement): Document new macro.
---
doc/org.texi | 7 +++++++
lisp/org-macro.el | 31 ++++++++++++++++++++++++++++---
2 files changed, 35 insertions(+), 3 deletions(-)
diff --git a/doc/org.texi b/doc/org.texi
index 312870f91..20a9e0948 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -10852,6 +10852,13 @@ This macro refers to the filename of the exported file, if any.
This macro returns the value of property @var{PROPERTY-NAME} in current
entry. If @var{SEARCH-OPTION} (@pxref{Search options}) refers to a remote
entry, it will be used instead.
+
+@item @{@{@{c@}@}@}
+@itemx @{@{@{c(@var{SEED})@}@}@}
+@cindex c, macro
+@cindex counter, macro
+This macro returns the number of occurrences of this macro expanded so far.
+Each @var{SEED} is associated to a dedicated 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 71e917b71..d11b0dbe5 100644
--- a/lisp/org-macro.el
+++ b/lisp/org-macro.el
@@ -36,8 +36,11 @@
;; Along with macros defined through #+MACRO: keyword, default
;; templates include the following hard-coded macros:
-;; {{{time(format-string)}}}, {{{property(node-property)}}},
-;; {{{input-file}}} and {{{modification-time(format-string)}}}.
+;; {{{time(format-string)}}},
+;; {{{property(node-property)}}},
+;; {{{input-file}}},
+;; {{{modification-time(format-string)}}},
+;; {{{c(seed}}}.
;; Upon exporting, "ox.el" will also provide {{{author}}}, {{{date}}},
;; {{{email}}} and {{{title}}} macros.
@@ -129,7 +132,7 @@ function installs the following ones: \"property\",
(let ((old-template (assoc (car cell) templates)))
(if old-template (setcdr old-template (cdr cell))
(push cell templates))))))
- ;; Install hard-coded macros.
+ ;; Install "property", "time" macros.
(mapc update-templates
(list (cons "property"
"(eval (save-excursion
@@ -143,6 +146,7 @@ function installs the following ones: \"property\",
l)))))
(org-entry-get nil \"$1\" 'selective)))")
(cons "time" "(eval (format-time-string \"$1\"))")))
+ ;; Install "input-file", "modification-time" macros.
(let ((visited-file (buffer-file-name (buffer-base-buffer))))
(when (and visited-file (file-exists-p visited-file))
(mapc update-templates
@@ -152,6 +156,10 @@ function installs the following ones: \"property\",
(prin1-to-string visited-file)
(prin1-to-string
(nth 5 (file-attributes visited-file)))))))))
+ ;; Initialize and install "c" macro.
+ (org-macro--counter-initialize)
+ (funcall update-templates
+ (cons "c" "(eval (org-macro--counter-increment \"$1\"))"))
(setq org-macro-templates templates)))
(defun org-macro-expand (macro templates)
@@ -280,6 +288,9 @@ Return a list of arguments, as strings. This is the opposite of
s nil t)
"\000"))
+\f
+;;; Helper functions and variables for internal macros
+
(defun org-macro--vc-modified-time (file)
(save-window-excursion
(when (vc-backend file)
@@ -304,6 +315,20 @@ Return a list of arguments, as strings. This is the opposite of
(kill-buffer buf))
date))))
+(defvar org-macro--counter-table nil
+ "Hash table containing counter value per seed.")
+
+(defun org-macro--counter-initialize ()
+ "Initialize `org-macro--counter-table'."
+ (setq org-macro--counter-table (make-hash-table :test #'equal)))
+
+(defun org-macro--counter-increment (seed)
+ "Increment counter with SEED."
+ (let ((value (gethash seed org-macro--counter-table)))
+ (puthash seed
+ (if (null value) 1 (1+ value))
+ org-macro--counter-table)))
+
(provide 'org-macro)
;;; org-macro.el ends here
--
2.12.2
next reply other threads:[~2017-05-08 11:26 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-08 11:26 Nicolas Goaziou [this message]
[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 19:44 ` 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=87o9v3614n.fsf@nicolasgoaziou.fr \
--to=mail@nicolasgoaziou.fr \
--cc=emacs-orgmode@gnu.org \
/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).