Next: Search Options in File Links, Previous: Using Links Outside Org, Up: Hyperlinks   [Contents][Index]


4.7 Link Abbreviations ¶

输入长 URL 十分繁琐,而文档中又常常需要大量相似链接,这时可以使用链接缩写。缩写链接格式如下:

[[linkword:tag][description]]

其中标签 tag 为可选部分。链接词 linkword 必须是一个单词,以字母开头,后跟字母、数字、=-= 和 ‘_’ 。缩写根据变量 org-link-abbrev-alist 中的映射关系解析,该变量将链接词与替换文本相关联。示例如下:

(setq org-link-abbrev-alist
      '(("bugzilla"        . "https://10.1.2.9/bugzilla/show_bug.cgi?id=")
        ("Nu Html Checker" . "https://validator.w3.org/nu/?doc=%h")
        ("duckduckgo"      . "https://duckduckgo.com/?q=%s")
        ("omap"            . "https://nominatim.openstreetmap.org/search?q=%s&polygon=1")
        ("ads"             . "https://ui.adsabs.harvard.edu/search/q=%20author%3A\"%s\"")))

如果替换文本包含 ‘%s=,则会被标签替换。使用 =%h’ 替代 ‘%s’ 会对标签进行 URL 百分号编码(如上例中需要对 URL 参数编码)。使用 ‘%(my-function)’ 可将标签传入自定义 Lisp 函数,并以返回字符串作为替换结果。

如果替换文本不包含任何占位符,则直接拼接在链接词后生成完整链接。

除了字符串,你也可以指定一个 Lisp 函数来生成链接,该函数以标签作为唯一参数。

使用以上配置后,你可以用 ‘[[bugzilla:129]]’ 链接到特定缺陷单,用 ‘[[duckduckgo:OrgMode]]’ 搜索 OrgMode,用 ‘[[omap:31 Milk Street, Boston]]’ 显示自由软件基金会所在地地图,用 ‘[[omap:Science Park 904, Amsterdam, The Netherlands]]’ 定位 Carsten 的办公室,还可以用 ‘[[ads:Dominik,C]]’ 查询 Org 作者除 Emacs 开发外的学术成果。

如果只需要在单个 Org 缓冲区中使用专用缩写,可以在文件内定义:

With the above setting, you could link to a specific bug with ‘[[bugzilla:129]]’, search the web for ‘OrgMode’ with ‘[[duckduckgo:OrgMode]]’, show the map location of the Free Software Foundation ‘[[omap:31 Milk Street, Boston]]’ or of Carsten’s office ‘[[omap:Science Park 904, Amsterdam, The Netherlands]]’ and find out what the Org author is doing besides Emacs hacking with ‘[[ads:Dominik,C]]’.

#+LINK: bugzilla  https://10.1.2.9/bugzilla/show_bug.cgi?id=
#+LINK: duckduckgo https://duckduckgo.com/?q=%s
#+LINK: "Nu Html Checker" https://validator.w3.org/nu/?doc=%h

包含空格的缩写必须用引号包裹。

缓冲区内部补全(见 Completion)可在输入 ‘[’ 后补全链接缩写。你还可以定义 Lisp 函数,为使用 C-c C-l 插入此类链接提供专用(如补全)支持。该函数不应接受任何参数,并返回带前缀的完整链接。设置链接补全函数的示例如下:

(org-link-set-parameter "type" :complete #'some-completion-function)

Next: Search Options in File Links, Previous: Using Links Outside Org, Up: Hyperlinks   [Contents][Index]