emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* %(SEXP) with %c in org-capture templates
@ 2011-07-12 23:46 Philipp Möller
  2011-07-15 22:38 ` Philipp Möller
  0 siblings, 1 reply; 5+ messages in thread
From: Philipp Möller @ 2011-07-12 23:46 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1561 bytes --]

Hello all,
I want to define a capture template which pre-processes the head of the kill
ring with a sexp that takes a string as an argument:

(setq org-capture-templates
      (quote
       (("l" "Link" entry (file+headline "" "Links")
         "* \"%c\" %(get-page-title \"%c\")"))))

;; throws Bad url
(get-page-title "foo-bar")
;; works as expected
(get-page-title "http://orgmode.org/manual/Template-expansion.html")

(defun get-page-title (url)
  "Get title of web page, whose url can be found in the current line"
  ;; Get title of web page, with the help of functions in url.el
  (with-current-buffer (url-retrieve-synchronously url)
    ;; find title by grep the html code
    (goto-char 0)
    (re-search-forward "<title>\\([^<]*\\)</title>" nil t 1)
    (setq web_title_str (match-string 1))
    ;; find charset by grep the html code
    (goto-char 0)

    ;; find the charset, assume utf-8 otherwise
    (if (re-search-forward "charset=\\([-0-9a-zA-Z]*\\)" nil t 1)
        (setq coding_charset (downcase (match-string 1)))
      (setq coding_charset "utf-8")
    ;; decode the string of title.
    (setq web_title_str (decode-coding-string web_title_str (intern

coding_charset)))
    )
  (concat "[[" url "][" web_title_str "]]")
  ))

Please just ignore that I'm trying to parse XML with a regexp here.

get-page-title works when called from code but always returns bad url when
called from the capture template.
Is the problem the way I escape the string or is this just not the way the
sexp in capture are supposed to be used?

Regards,
Philipp

[-- Attachment #2: Type: text/html, Size: 2135 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: %(SEXP) with %c in org-capture templates
  2011-07-12 23:46 %(SEXP) with %c in org-capture templates Philipp Möller
@ 2011-07-15 22:38 ` Philipp Möller
  2011-07-24 16:48   ` Bastien
  0 siblings, 1 reply; 5+ messages in thread
From: Philipp Möller @ 2011-07-15 22:38 UTC (permalink / raw)
  To: emacs-orgmode

On 13 July 2011 01:46, Philipp Möller <bootsarehax@googlemail.com> wrote:
> Hello all,
> I want to define a capture template which pre-processes the head of the kill
> ring with a sexp that takes a string as an argument:
>
> (setq org-capture-templates
>       (quote
>        (("l" "Link" entry (file+headline "" "Links")
>          "* \"%c\" %(get-page-title \"%c\")"))))
>
> ;; throws Bad url
> (get-page-title "foo-bar")
> ;; works as expected
> (get-page-title "http://orgmode.org/manual/Template-expansion.html")
>
> (defun get-page-title (url)
>   "Get title of web page, whose url can be found in the current line"
>   ;; Get title of web page, with the help of functions in url.el
>   (with-current-buffer (url-retrieve-synchronously url)
>     ;; find title by grep the html code
>     (goto-char 0)
>     (re-search-forward "<title>\\([^<]*\\)</title>" nil t 1)
>     (setq web_title_str (match-string 1))
>     ;; find charset by grep the html code
>     (goto-char 0)
>
>     ;; find the charset, assume utf-8 otherwise
>     (if (re-search-forward "charset=\\([-0-9a-zA-Z]*\\)" nil t 1)
>         (setq coding_charset (downcase (match-string 1)))
>       (setq coding_charset "utf-8")
>     ;; decode the string of title.
>     (setq web_title_str (decode-coding-string web_title_str (intern
>
> coding_charset)))
>     )
>   (concat "[[" url "][" web_title_str "]]")
>   ))
>
> Please just ignore that I'm trying to parse XML with a regexp here.
>
> get-page-title works when called from code but always returns bad url when
> called from the capture template.
> Is the problem the way I escape the string or is this just not the way the
> sexp in capture are supposed to be used?

Hi,
nevermind. I figured out that the expansion order of %s and s
expressions is the problem and also the reason for the with the
correct URL.
The solution is to use %(get-page-title (current-kill 0)).

Can this be added to the documentation or is it supposed to be obvious
from the order of available template expansions?

Regards,
Philipp


> Regards,
> Philipp
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: %(SEXP) with %c in org-capture templates
  2011-07-15 22:38 ` Philipp Möller
@ 2011-07-24 16:48   ` Bastien
  2011-07-25 13:49     ` Philipp Möller
  0 siblings, 1 reply; 5+ messages in thread
From: Bastien @ 2011-07-24 16:48 UTC (permalink / raw)
  To: Philipp Möller; +Cc: emacs-orgmode

Hi Philipp,

Philipp Möller <bootsarehax@googlemail.com> writes:

> nevermind. I figured out that the expansion order of %s and s
> expressions is the problem and also the reason for the with the
> correct URL.
> The solution is to use %(get-page-title (current-kill 0)).

Thanks for letting us know!

> Can this be added to the documentation or is it supposed to be obvious
> from the order of available template expansions?

IMHO this should go in the documentation -- could you provide a patch
for this?

Thanks,

-- 
 Bastien

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: %(SEXP) with %c in org-capture templates
  2011-07-24 16:48   ` Bastien
@ 2011-07-25 13:49     ` Philipp Möller
  2011-07-25 21:09       ` Bastien
  0 siblings, 1 reply; 5+ messages in thread
From: Philipp Möller @ 2011-07-25 13:49 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 756 bytes --]

On 24 July 2011 18:48, Bastien <bzg@altern.org> wrote:
> Hi Philipp,
>
> Philipp Möller <bootsarehax@googlemail.com> writes:
>
>> nevermind. I figured out that the expansion order of %s and s
>> expressions is the problem and also the reason for the with the
>> correct URL.
>> The solution is to use %(get-page-title (current-kill 0)).
>
> Thanks for letting us know!
>
>> Can this be added to the documentation or is it supposed to be obvious
>> from the order of available template expansions?
>
> IMHO this should go in the documentation -- could you provide a patch
> for this?

A patch for the doc string and the info manual is attached. Let me
know if anything is wrong with it.

Philipp

> Thanks,
>
> --
>  Bastien
>

[-- Attachment #2: org.texi.diff --]
[-- Type: text/plain, Size: 3697 bytes --]

diff --git a/doc/org.texi b/doc/org.texi
index 87f73a8..8c6b137 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -6526,39 +6526,39 @@ buffer again after capture is completed.
 
 In the template itself, special @kbd{%}-escapes@footnote{If you need one of
 these sequences literally, escape the @kbd{%} with a backslash.}  allow
-dynamic insertion of content:
+dynamic insertion of content. The templates are expanded in the order given here:
 
 @smallexample
-%^@{@var{prompt}@}  @r{prompt the user for a string and replace this sequence with it.}
-            @r{You may specify a default value and a completion table with}
-            @r{%^@{prompt|default|completion2|completion3...@}.}
-            @r{The arrow keys access a prompt-specific history.}
+%[@var{file}]     @r{insert the contents of the file given by @var{file}.}
+%(@var{sexp})     @r{evaluate Elisp @var{sexp} and replace with the result.}
+%<...>      @r{the result of format-time-string on the ... format specification.}
+%t          @r{timestamp, date only.}
+%T          @r{timestamp with date and time.}
+%u, %U      @r{like the above, but inactive timestamps.}
 %a          @r{annotation, normally the link created with @code{org-store-link}.}
-%A          @r{like @code{%a}, but prompt for the description part.}
 %i          @r{initial content, the region when capture is called while the}
             @r{region is active.}
             @r{The entire text will be indented like @code{%i} itself.}
-%t          @r{timestamp, date only.}
-%T          @r{timestamp with date and time.}
-%u, %U      @r{like the above, but inactive timestamps.}
-%^t         @r{like @code{%t}, but prompt for date.  Similarly @code{%^T}, @code{%^u}, @code{%^U}.}
-            @r{You may define a prompt like @code{%^@{Birthday@}t}.}
-%<...>      @r{the result of format-time-string on the ... format specification.}
-%n          @r{user name (taken from @code{user-full-name}).}
+%A          @r{like @code{%a}, but prompt for the description part.}
 %c          @r{Current kill ring head.}
 %x          @r{Content of the X clipboard.}
-%^C         @r{Interactive selection of which kill or clip to use.}
-%^L         @r{Like @code{%^C}, but insert as link.}
 %k          @r{title of the currently clocked task.}
 %K          @r{link to the currently clocked task.}
+%n          @r{user name (taken from @code{user-full-name}).}
 %f          @r{file visited by current buffer when org-capture was called.}
 %F          @r{full path of the file or directory visited by current buffer.}
+%:keyword   @r{specific information for certain link types, see below.}
 %^g         @r{prompt for tags, with completion on tags in target file.}
 %^G         @r{prompt for tags, with completion all tags in all agenda files.}
+%^t         @r{like @code{%t}, but prompt for date.  Similarly @code{%^T}, @code{%^u}, @code{%^U}.}
+            @r{You may define a prompt like @code{%^@{Birthday@}t}.}
+%^C         @r{Interactive selection of which kill or clip to use.}
+%^L         @r{Like @code{%^C}, but insert as link.}
 %^@{@var{prop}@}p   @r{Prompt the user for a value for property @var{prop}.}
-%:keyword   @r{specific information for certain link types, see below.}
-%[@var{file}]     @r{insert the contents of the file given by @var{file}.}
-%(@var{sexp})     @r{evaluate Elisp @var{sexp} and replace with the result.}
+%^@{@var{prompt}@}  @r{prompt the user for a string and replace this sequence with it.}
+            @r{You may specify a default value and a completion table with}
+            @r{%^@{prompt|default|completion2|completion3...@}.}
+            @r{The arrow keys access a prompt-specific history.}
 @end smallexample
 
 @noindent

[-- Attachment #3: org-capture.el.diff --]
[-- Type: text/plain, Size: 3668 bytes --]

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index d80bddc..d18dd96 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -206,39 +206,40 @@ properties are:
                      capture was invoked, kill the buffer again after capture
                      is finalized.
 
-The template defines the text to be inserted.  Often this is an org-mode
-entry (so the first line should start with a star) that will be filed as a
-child of the target headline.  It can also be freely formatted text.
-Furthermore, the following %-escapes will be replaced with content:
+The template defines the text to be inserted.  Often this is an
+org-mode entry (so the first line should start with a star) that
+will be filed as a child of the target headline.  It can also be
+freely formatted text.  Furthermore, the following %-escapes will
+be replaced with content and expanded in this order:
 
-  %^{prompt}  prompt the user for a string and replace this sequence with it.
-              A default value and a completion table ca be specified like this:
-              %^{prompt|default|completion2|completion3|...}.
+  %[pathname] insert the contents of the file given by `pathname'.
+  %(sexp)     evaluate elisp `(sexp)' and replace with the result.
+  %<...>      the result of format-time-string on the ... format specification.
   %t          time stamp, date only.
   %T          time stamp with date and time.
   %u, %U      like the above, but inactive time stamps.
-  %^t         like %t, but prompt for date.  Similarly %^T, %^u, %^U.
-              You may define a prompt like %^{Please specify birthday.
-  %<...>      the result of format-time-string on the ... format specification.
-  %n          user name (taken from `user-full-name').
   %a          annotation, normally the link created with `org-store-link'.
   %i          initial content, copied from the active region.  If %i is
               indented, the entire inserted text will be indented as well.
+  %A          like %a, but prompt for the description part.
   %c          current kill ring head.
   %x          content of the X clipboard.
-  %^C         interactive selection of which kill or clip to use.
-  %^L         like %^C, but insert as link.
   %k          title of currently clocked task.
   %K          link to currently clocked task.
+  %n          user name (taken from `user-full-name').
   %f          file visited by current buffer when org-capture was called.
   %F          full path of the file or directory visited by current buffer.
+  %:keyword   specific information for certain link types, see below.
   %^g         prompt for tags, with completion on tags in target file.
   %^G         prompt for tags, with completion on all tags in all agenda files.
+  %^t         like %t, but prompt for date.  Similarly %^T, %^u, %^U.
+              You may define a prompt like %^{Please specify birthday.
+  %^C         interactive selection of which kill or clip to use.
+  %^L         like %^C, but insert as link.
   %^{prop}p   prompt the user for a value for property `prop'.
-  %:keyword   specific information for certain link types, see below.
-  %[pathname] insert the contents of the file given by `pathname'.
-  %(sexp)     evaluate elisp `(sexp)' and replace with the result.
-
+  %^{prompt}  prompt the user for a string and replace this sequence with it.
+              A default value and a completion table ca be specified like this:
+              %^{prompt|default|completion2|completion3|...}.
   %?          After completing the template, position cursor here.
 
 Apart from these general escapes, you can access information specific to the

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: %(SEXP) with %c in org-capture templates
  2011-07-25 13:49     ` Philipp Möller
@ 2011-07-25 21:09       ` Bastien
  0 siblings, 0 replies; 5+ messages in thread
From: Bastien @ 2011-07-25 21:09 UTC (permalink / raw)
  To: Philipp Möller; +Cc: emacs-orgmode

Philipp Möller <bootsarehax@googlemail.com> writes:

> A patch for the doc string and the info manual is attached. Let me
> know if anything is wrong with it.

Applied, thanks a lot!

-- 
 Bastien

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-07-25 21:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-12 23:46 %(SEXP) with %c in org-capture templates Philipp Möller
2011-07-15 22:38 ` Philipp Möller
2011-07-24 16:48   ` Bastien
2011-07-25 13:49     ` Philipp Möller
2011-07-25 21:09       ` Bastien

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).