2011-07-13 38 views
8

我試圖創建一個捕獲模板,將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」。

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

回答

7

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

的解決方案是從SEXP內訪問剪切環:

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

EDIT現在此行爲是記錄在org-模式。

+0

爲什麼這不是一個被接受的答案? –

+0

@LeVieuxGildas接受我自己的答案總是有點痛苦。我更新並接受了它。 – pmr

+0

我閱讀[手冊頁](http://orgmode.org/manual/Template-expansion.html#Template-expansion)的方式表示它應該與此相反 - 「評估Elisp sexp並將結果替換。方便,%:關鍵字(見下文)表達式中的佔位符將在此之前展開。「但我同意你所描述的是似乎正在發生的事情。 – studgeek

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) 

然後使用一個基於瀏覽器的熱鍵綁定(在我的情況與歌劇,雖然設置爲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. 

(我打破了有機鏈接簡單地滾動保持在最低限度,這是不是在最初的兩行)

+0

不錯的解決方案。我不知道如何事先使用org協議,但這肯定比我的方法有更好的可用性。 – pmr

+0

我恢復了我說的。在碰到gconf以及所有其他不兼容的東西之後,我爬回到了我所有的emacs世界。這裏非常舒適,我不必忍受這個狗屎。 – pmr

+0

@pmr可能值得在郵件列表上詢問是否有人有辦法解決這個問題。否則最接近的我可以來看看你可能能夠修復它的地方是org.el的第8487-8498行,它指的是如何從w3和w3m中捕獲(應該能夠添加到org-store-link-道具包括標題) –

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並將結果替換。爲方便起見, %:關鍵字(見下文)表達式中的佔位符將在此之前展開爲 。

+0

就像我已經在回覆您的評論時所說的:確保您的組織版本與在線手冊中的一個相匹配。你的方法似乎很好地工作,雖然我不喜歡擺弄無證變量出於顯而易見的原因。 – pmr