8

I'm trying to get create a capture template that converts an URL to an org-mode link with the <title> as the link name.

My conversion function looks like this:

(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 "]]")
  ))

When called from normal emacs lisp code it returns the correct result. But when used in this org-capture-template it only returns bad url.

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

Is the order of expansion different? Do I need to escape the string differently? Magic? The first %c is only their to debug the string and indeed is getting printed as "url".

Please don't even bother pointing out that parsing XML with regexp is the wrong approach. Cthulhu is already haunting me and this isn't going to make it worse.

7

The problem is the order of expansion of template parameters. The simple % templates are expanded after the sexp has been evaluated. The original error message still contains a template and thus is expanded into the contents of the clipboard and thus the error message contains not the string that was originally passed to get-page-title.

The solution is to access the kill ring from within the sexp:

%(get-page-title (current-kill 0))

EDIT This behavior is now documented in org-mode.

3
  • @LeVieuxGildas Accepting my own answers is always a little painful. I updated and accepted it.
    – pmr
    Oct 17 '12 at 8:05
  • 2
    The way I read the manual page it says it should work the opposite - "Evaluate Elisp sexp and replace with the result. For convenience, %:keyword (see below) placeholders within the expression will be expanded prior to this." But I agree what you describe is what seems to happening.
    – studgeek
    Jan 13 '14 at 2:38
  • @studgeek This is highly version specific. The part about the convenience has been added after I fixed the doc to mention the ordering. Check your org-mode version and check the results.
    – pmr
    Jan 13 '14 at 10:59
6

Wouldn't the solution be to use org-protocol.el? http://orgmode.org/worg/org-contrib/org-protocol.html

I just tested it with the following template (adding a sub-heading for your desired title as headline).

Template:

("t"
"Testing Template"
entry
(file+headline "~/org/capture.org" "Testing")
"* %^{Title}\n** %:description\n\n  Source: %u, %c\n\n%i"
:empty-lines 1)

Then using a browser-based keybind (in my case with Opera, although examples for Firefox, Uzbl, Acrobat and Conkeror are provided as well) I was able to capture the following:

** Testing for StackExchange
*** org-protocol.el - Intercept calls from emacsclient to trigger custom actions

  Source: [2011-08-05 Fri], [[http://orgmode.org/worg/org-contrib/org-protocol.html]
  [org-protocol.el - Intercept calls from emacsclient to trigger custom actions]]

    org-protocol intercepts calls from emacsclient to trigger custom actions
    without external dependencies.

(I broke the org-link simply to keep the scrolling to a minimum, it was not on two lines originally)

5
  • Nice solution. I didn't know how to use org-protocol beforehand but this certainly has slightly better usability than my approach.
    – pmr
    Aug 5 '11 at 19:38
  • I revert what I said. After having to touch gconf and all those other not backwards-compatible things I crawled back into my everything emacs world. It's very cosy here and I don't have to put up with this shit.
    – pmr
    Aug 5 '11 at 21:47
  • @pmr It might perhaps be worth asking on the mailing list to see if anyone has any method to resolve this. Otherwise the closest I can come to seeing somewhere you might be able to fix it would be lines 8487-8498 of org.el which refer to how to capture from w3 and w3m (should be able to add to the org-store-link-props to include the title) Aug 6 '11 at 1:10
  • Well, everything works on the emacs side and manually calling emacsclient with org-protocol works. Convincing any of the weird gconf-tools to do anything they are told is another story. ;)
    – pmr
    Aug 6 '11 at 1:28
  • @pmr I hadn't tested it on Linux yet, I'd done the work on a XP machine and it worked fine. When I tried on my Arch box over the weekend I came up to the same issue you had, no gnome on the box so no gconf tools to make the fix. However Opera was still able to get it to work (can configure custom protocols in it, not just map to defaults). I'll ask on the mailing list where I should look to configure the protocol when gconf tools aren't available. Aug 9 '11 at 12:55
4

@aboabo shared an undocumented variable in https://stackoverflow.com/a/21080770/255961 that provides a more general solution to the question's topic of how to use sexp with keyword values in a template (beyond kill ring). The variable org-store-link-plist stores all the information being passed to the capture. So you can access its values directly from a function like this:

(defun url()
  (plist-get org-store-link-plist :url))
("w" "capture" entry (file "~/refile.org")
     "* [[%:link][%:description]] :NOTE:\n%(url)%U\n"
     :immediate-finish t) 

PS, According to the manual page (quote below) it sounds to me like your approach in teh question should work also. But I agree what you describe is what seems to be actually happening - it seems like a bug relative to the manual.

%(sexp) Evaluate Elisp sexp and replace with the result. For convenience, %:keyword (see below) placeholders within the expression will be expanded prior to this.

1
  • Like I already said in response to your comment: make sure your org-version matches the one of the online manual. Your approach seems to work nicely although I dislike fiddling with undocumented variables for obvious reasons.
    – pmr
    Jan 13 '14 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.