用sexp捕获组织模式

时间:2011-07-13 15:28:50

标签: emacs org-mode

我正在尝试创建一个捕获模板,将URL转换为组织模式链接,并将<title>作为链接名称。

我的转换功能如下所示:

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

从正常的emacs lisp代码调用时,它会返回正确的结果。但在此org-capture-template中使用时,它只会返回bad url

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

扩张顺序是否不同?我需要以不同的方式逃避字符串吗?魔法? 第一个%c仅用于调试字符串,实际上打印为“url”。

请不要指出用regexp解析XML是错误的方法。 Cthulhu 已经困扰着我,这不会让情况变得更糟。

3 个答案:

答案 0 :(得分:7)

问题是模板参数的扩展顺序。在评估sexp之后扩展简单的%模板。原始错误消息仍包含模板,因此会扩展为剪贴板的内容,因此错误消息不包含最初传递给get-page-title的字符串。

解决方法是从sexp中访问杀戮戒指:

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

编辑此行为现已记录在org-mode中。

答案 1 :(得分:6)

解决方案不是要使用org-protocol.el吗? http://orgmode.org/worg/org-contrib/org-protocol.html

我刚刚使用以下模板对其进行了测试(为标题添加了所需标题的子标题)。

模板:

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

然后使用基于浏览器的keybind(在我的情况下使用Opera,虽然也提供了Firefox,Uzbl,Acrobat和Conkeror的示例),我能够捕获以下内容:

** 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.

(我打破org-link只是为了让滚动保持最小,原来不是两行)

答案 2 :(得分:2)

@aboabo在https://stackoverflow.com/a/21080770/255961中共享了一个未记录的变量,它提供了一个更通用的解决方案,用于解决如何在模板中使用带有关键字值的sexp的问题(超出杀死环)。变量org-store-link-plist存储传递给捕获的所有信息。因此,您可以直接从以下函数访问其值:

(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,根据manual page(下面的引用),我觉得你的方法也应该有效。但我同意你所描述的似乎实际发生的事情 - 这似乎是与手册相关的错误。

  

%(sexp)评估Elisp sexp并替换为结果。为了方便,   %:关键字(见下文)表达式中的占位符将是   在此之前扩大。

相关问题