gpt4 book ai didi

emacs - 使用 sexp 进行组织模式捕获

转载 作者:行者123 更新时间:2023-12-04 01:33:03 26 4
gpt4 key购买 nike

我正在尝试创建一个捕获模板,该模板将 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 是错误的方法。克苏鲁 已经困扰着我,这不会让它变得更糟。

最佳答案

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

解决方案是从 sexp 中访问 kill ring:

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

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

关于emacs - 使用 sexp 进行组织模式捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6681407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com