From 142625bad45ca07a185d95caa0d810225e389b95 Mon Sep 17 00:00:00 2001 From: Michael Brand
Date: Sat, 11 Aug 2012 17:26:57 +0200 Subject: [PATCH] Add the capture feature sexp to org feed * org-feed.el (org-feed-format-entry): Require `org-capture'. Expand Elisp %(...) templates. (org-feed-default-template): Update docstring. * org-capture.el (org-capture-expand-embedded-elisp): New function. (org-capture-fill-template): Use it. (org-capture-inside-embedded-elisp-p): New function to tell if we are within an Elisp %(...) template. --- lisp/org-capture.el | 40 ++++++++++++++++++++++++++++-------- lisp/org-feed.el | 56 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 68 insertions(+), 28 deletions(-) diff --git a/lisp/org-capture.el b/lisp/org-capture.el index 84e5ff6..4a11462 100644 --- a/lisp/org-capture.el +++ b/lisp/org-capture.el @@ -1388,15 +1388,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-expand-embedded-elisp) ;; The current time (goto-char (point-min)) @@ -1530,6 +1522,36 @@ The template may still contain \"%?\" for cursor positioning." t) nil)) +(defun org-capture-expand-embedded-elisp () + "Evaluate embedded elisp %(sexp) and replace with the result." + (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)))))) + +(defun org-capture-inside-embedded-elisp-p () + "Return non-nil if point is inside of embedded elisp %(sexp)." + (let ((buf (buffer-string)) (p (point)) beg end) + (with-temp-buffer ; to keep major mode and font-lock in original buffer + (emacs-lisp-mode) ; to deal with for example %(length ")") + (insert buf) + (goto-char p) + ;; `looking-at' and `search-backward' below do not match the "%(" if + ;; point is in its middle + (when (equal (char-before) ?%) + (backward-char)) + (save-match-data + (when (or (looking-at "%(") (search-backward "%(" nil t)) + (setq beg (point)) + (setq end (progn (forward-char) (forward-sexp) (1- (point))))))) + (when (and beg end) + (and (<= p end) (>= p beg))))) + ;;;###autoload (defun org-capture-import-remember-templates () "Set org-capture-templates to be similar to `org-remember-templates'." diff --git a/lisp/org-feed.el b/lisp/org-feed.el index 6901ffa..8b3414b 100644 --- a/lisp/org-feed.el +++ b/lisp/org-feed.el @@ -225,12 +225,14 @@ 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