From mboxrd@z Thu Jan 1 00:00:00 1970 From: Bastien Subject: Re: Add the capture feature "%(sexp)" to org-feed Date: Wed, 08 Aug 2012 01:05:10 +0200 Message-ID: <87y5lqe21l.fsf@gnu.org> References: <7822.1339898820@alphaville> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([208.118.235.92]:55414) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyspO-0006FM-B5 for emacs-orgmode@gnu.org; Tue, 07 Aug 2012 19:04:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SyspM-0007j8-P6 for emacs-orgmode@gnu.org; Tue, 07 Aug 2012 19:04:42 -0400 Received: from mail-wi0-f177.google.com ([209.85.212.177]:43441) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyspM-0007iv-EU for emacs-orgmode@gnu.org; Tue, 07 Aug 2012 19:04:40 -0400 Received: by wibhm11 with SMTP id hm11so103449wib.12 for ; Tue, 07 Aug 2012 16:04:39 -0700 (PDT) In-Reply-To: (Michael Brand's message of "Sun, 24 Jun 2012 19:51:32 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Michael Brand Cc: nicholas.dokos@hp.com, Org Mode --=-=-= Content-Type: text/plain Hi Michael, Michael Brand writes: > Thank you for the help. I have chosen the solution with require org-capture. > > The patch is now finished and attached: I've reworked some parts of your patch, specifically, I used `org-at-regexp-p' instead of `org-capture-inside-embedded-elisp-p'. Can you check this is working okay for you? Thanks! --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-org-feed.el-Add-the-capture-feature-sexp.patch >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 (if present), or - the current date. -%T date and time -%u,%U like %t,%T, but inactive time stamps -%a A link, from if that is a permalink, else from " +%h The title, or the first line of the description +%t The date as a stamp, either from (if present), or + The current date. +%T Date and time +%u,%U Like %t,%T, but inactive time stamps +%a A link, from if that is a permalink, else from +%(sexp) Evaluate the Elisp sexp `(sexp)' and replace with the result. + The above simple %-escapes can be used as arguments, + for example %(capitalize \\\"%h\\\")" :group 'org-feed :type '(string :tag "Template")) @@ -506,9 +509,10 @@ This will find DRAWER and extract the alist." ENTRY is a property list. This function adds a `:formatted-for-org' property and returns the full property list. If that property is already present, nothing changes." + (require 'org-capture) (if formatter (funcall formatter entry) - (let (dlines fmt tmp indent time name + (let (dlines time escape name tmp v-h v-t v-T v-u v-U v-a) (setq dlines (org-split-string (or (plist-get entry :description) "???") "\n") @@ -527,20 +531,34 @@ If that property is already present, nothing changes." "")) (with-temp-buffer (insert template) + + ;; Simple %-escapes before embedded elisp to support simple + ;; %-escapes as arguments for embedded elisp (goto-char (point-min)) (while (re-search-forward "%\\([a-zA-Z]+\\)" nil t) - (setq name (match-string 1)) - (cond - ((member name '("h" "t" "T" "u" "U" "a")) - (replace-match (symbol-value (intern (concat "v-" name))) t t)) - ((setq tmp (plist-get entry (intern (concat ":" name)))) - (save-excursion - (save-match-data - (beginning-of-line 1) - (when (looking-at (concat "^\\([ \t]*\\)%" name "[ \t]*$")) - (setq tmp (org-feed-make-indented-block - tmp (org-get-indentation)))))) - (replace-match tmp t t)))) + (unless (org-capture-escaped-%) + (setq name (match-string 1) + escape (org-at-regexp-p org-capture-template-embedded-elisp-re)) + (cond + ((member name '("h" "t" "T" "u" "U" "a")) + (setq tmp (symbol-value (intern (concat "v-" name))))) + ((setq tmp (plist-get entry (intern (concat ":" name)))) + (save-excursion + (save-match-data + (beginning-of-line 1) + (when (looking-at + (concat "^\\([ \t]*\\)%" name "[ \t]*$")) + (setq tmp (org-feed-make-indented-block + tmp (org-get-indentation)))))))) + (when tmp + ;; escape string delimiters `"' when inside %() embedded lisp + (when escape + (setq tmp (replace-regexp-in-string "\"" "\\\\\"" tmp))) + (replace-match tmp t t)))) + + ;; %() embedded elisp + (org-capture-eval-and-replace-embedded-elisp) + (decode-coding-string (buffer-string) (detect-coding-region (point-min) (point-max) t)))))) -- 1.7.10.2 --=-=-= Content-Type: text/plain -- Bastien --=-=-=--