>From 35e5c3d0b098dc65f321cc32626172e984cf2c2f Mon Sep 17 00:00:00 2001 From: Bastien Guerry
Date: Wed, 8 Aug 2012 01:01:29 +0200 Subject: [PATCH] org-feed.el: Add the capture feature "%(sexp)". * org-feed.el (org-feed-format-entry): Support %(sexp). (org-feed-default-template): Update docstring to document the support of %(sexp). * org-capture.el (org-capture-template-embedded-elisp-re): New constant. (org-capture-eval-and-replace-embedded-elisp): New function. (org-capture-fill-template): Use the new function. --- lisp/org-capture.el | 25 ++++++++++++++--------- lisp/org-feed.el | 56 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/lisp/org-capture.el b/lisp/org-capture.el index 05e3a0c..352e655 100644 --- a/lisp/org-capture.el +++ b/lisp/org-capture.el @@ -74,6 +74,9 @@ ;; to indicate that the link properties have already been stored (defvar org-capture-link-is-already-stored nil) +(defconst org-capture-template-embedded-elisp-re + "%\\((\\(.\\|\n\\)*)\\)") + (defgroup org-capture nil "Options concerning capturing new entries." :tag "Org Capture" @@ -1277,6 +1280,18 @@ Lisp programs can force the template by setting KEYS to a string." '(("C" "Customize org-capture-templates") ("q" "Abort")))))) +(defun org-capture-eval-and-replace-embedded-elisp () + "Evaluate embedded elisp %(sexp) and replace with the result." + (goto-char (point-min)) + (while (re-search-forward org-capture-template-embedded-elisp-re nil t) + (unless (org-capture-escaped-%) + (goto-char (match-beginning 0)) + (let ((template-start (point))) + (forward-char 1) + (let ((result (org-eval (read (current-buffer))))) + (delete-region template-start (point)) + (insert result)))))) + (defun org-capture-fill-template (&optional template initial annotation) "Fill a template and return the filled template as a string. The template may still contain \"%?\" for cursor positioning." @@ -1371,15 +1386,7 @@ The template may still contain \"%?\" for cursor positioning." (error (insert (format "%%![Couldn't insert %s: %s]" filename error))))))) ;; %() embedded elisp - (goto-char (point-min)) - (while (re-search-forward "%\\((.+)\\)" nil t) - (unless (org-capture-escaped-%) - (goto-char (match-beginning 0)) - (let ((template-start (point))) - (forward-char 1) - (let ((result (org-eval (read (current-buffer))))) - (delete-region template-start (point)) - (insert result))))) + (org-capture-eval-and-replace-embedded-elisp) ;; The current time (goto-char (point-min)) diff --git a/lisp/org-feed.el b/lisp/org-feed.el index 6901ffa..912dd6e 100644 --- a/lisp/org-feed.el +++ b/lisp/org-feed.el @@ -225,12 +225,15 @@ Any fields from the feed item can be interpolated into the template with %name, for example %title, %description, %pubDate etc. In addition, the following special escapes are valid as well: -%h the title, or the first line of the description -%t the date as a stamp, either from