From 581a66bbf3c808e906d275c3411f640428552c39 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou 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")) + +;;; 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