用于组织捕获扩展的自定义组织捕获模板



我正在使用出色的 Org Capture Extension Firefox 插件将我的 Web 链接直接捕获到我的 Emacs 文档中。

最小的组织捕获模板是:

(setq org-capture-templates `(
     ("L" "Protocol Link" entry (file+headline "~/web.org" "Links")
      "* [[%:link][%:description]]n")  
     ;; ... your other templates
))

我用它来为文章添加书签,奇怪的是,其中很多都在arxiv.org。问题是arxiv标题页包含[]字符,例如:

[1606.04838] 大规模机器学习的优化方法

这与模板中用于创建组织模式链接的[[%:link][%:description]]不能很好地混合。例如,捕获返回:

** [[https://arxiv.org/abs/1606.04838][[1606.04838] Optimization Methods for Large-Scale Machine Learning]]

并且由于"[1606.04838]"字符串中的括号,组织模式链接已断开

如何解决这个问题?

解决方法是将[%:description]描述的链接转换为不包含方括号 [] 的字符串。为此,我们可以定义一个函数,将 [, ] 字符转换为 (, ) 字符。

(defun transform-square-brackets-to-round-ones(string-to-transform)
  "Transforms [ into ( and ] into ), other chars left unchanged."
  (concat 
  (mapcar #'(lambda (c) (if (equal c ?[) ?( (if (equal c ?]) ?) c))) string-to-transform))
  )

然后我们可以将此功能用于 org-capture-template .%(sexp) 语法用于计算模板中的 lisp 代码:

%(sexp) 评估 Elisp sexp 并替换为结果。 为方便起见,%:关键字(见下文)占位符 在此之前,表达式将展开。 sexp 必须返回一个字符串。

修改后的org-capture-template为:

(setq org-capture-templates '(
    ("L" "Protocol Link" entry (file+headline "~/web.org" "Links")
     "* [[%:link][%(transform-square-brackets-to-round-ones "%:description")]]n")
    ;; ... your other templates
))

然后,当您单击Firefox Org-Capture按钮时,模板将正确扩展到

** (1606.04838) Optimization Methods for Large-Scale Machine Learning

具有格式良好的组织模式链接(请注意,[1606.04838] 已转换为 (1606.04838))

最新更新