From 980b713f28596c7f6486dc1ccfa82f76de7c963d 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 "n" 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. * testing/lisp/test-org-macro.el (test-org-macro/n): (test-org-macro/property): New tests. --- etc/ORG-NEWS | 3 ++ lisp/org-macro.el | 38 +++++++++++++-- testing/lisp/test-org-macro.el | 102 +++++++++++++++++++++++++++++++++++------ 3 files changed, 127 insertions(+), 16 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 3ca5b0553..b6110c412 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -197,6 +197,9 @@ contents, are now supported. This new function is meant to be used in back-ends supporting images as descriptions of links, a.k.a. image links. See its docstring for details. +**** New macro : ~{{{n}}}~ +This macro creates and increment multiple counters in a document. See +manual for details. **** Add global macros through ~org-export-global-macros~ With this variable, one can define macros available for all documents. **** New keyword ~#+EXPORT_FILE_NAME~ diff --git a/lisp/org-macro.el b/lisp/org-macro.el index 71e917b71..f5ddb92e4 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)}}}, +;; {{{n(counter,reset}}}. ;; 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 "n" macro. + (org-macro--counter-initialize) + (funcall update-templates + (cons "n" "(eval (org-macro--counter-increment \"$1\" \"$2\"))")) (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,27 @@ 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 name.") + +(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 (name &optional reset) + "Increment counter NAME. +NAME is a string identifying the counter. If optional argument +RESET is a non-empty string, reset the counter instead." + (if (org-string-nw-p reset) + (let ((new-value (if (string-match-p "\\`[ \t]*[0-9]+[ \t]*\\'" reset) + (string-to-number reset) + 1))) + (puthash name new-value org-macro--counter-table)) + (let ((value (gethash name org-macro--counter-table))) + (puthash name + (if (null value) 1 (1+ value)) + org-macro--counter-table)))) + (provide 'org-macro) ;;; org-macro.el ends here diff --git a/testing/lisp/test-org-macro.el b/testing/lisp/test-org-macro.el index 26c56745c..64b0a97cc 100644 --- a/testing/lisp/test-org-macro.el +++ b/testing/lisp/test-org-macro.el @@ -75,9 +75,22 @@ (org-macro-initialize-templates) (org-macro-replace-all org-macro-templates) (buffer-string)))) - ;; Test special "property" macro. With only one argument, retrieve - ;; property from current headline. Otherwise, the second argument - ;; is a search option to get the property from another headline. + ;; Macro expansion ignores narrowing. + (should + (string-match + "expansion" + (org-test-with-temp-text + "#+MACRO: macro expansion\n{{{macro}}}\n