簡體   English   中英

使用 sexp 進行組織模式捕獲

[英]org-mode capture with sexp

我正在嘗試創建一個捕獲模板,它將 URL 轉換為以<title>作為鏈接名稱的組織模式鏈接。

我的轉換 function 看起來像這樣:

(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”。

請不要費心指出用正則表達式解析 XML 是錯誤的方法。 克蘇魯已經困擾着我,這不會讓事情變得更糟。

問題是模板參數的擴展順序。 簡單的%模板在評估完 sexp 后展開。 原始錯誤消息仍然包含一個模板,因此被擴展為剪貼板的內容,因此錯誤消息不包含最初傳遞給get-page-title的字符串。

解決方案是從 sexp 中訪問 kill ring:

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

編輯此行為現在記錄在 org-mode 中。

解決方案不是使用 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)

然后使用基於瀏覽器的鍵綁定(在我使用 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 只是為了將滾動保持在最低限度,它最初不在兩行)

@aboabo 在https://stackoverflow.com/a/21080770/255961中共享了一個未記錄的變量,該變量為問題的主題提供了一個更通用的解決方案,即如何在模板中使用帶有關鍵字值的 sexp(除了殺戮環)。 變量org-store-link-plist存儲傳遞給捕獲的所有信息。 因此,您可以像這樣直接從 function 訪問其值:

(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,根據手冊頁(下面的引用),在我看來,您在問題中的方法也應該有效。 但我同意你所描述的似乎是實際發生的事情——這似乎是一個相對於手冊的錯誤。

%(sexp) 評估 Elisp 的 sexp 並替換為結果。 為方便起見,表達式中的 %:keyword(見下文)占位符將在此之前展開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM