A.3 Adding Hyperlink Types
Org has many built-in hyperlink types (see Hyperlinks), and an interface for adding new link types. The following example shows the process of adding Org links to Unix man pages, which look like this
[[man:printf][The printf manual]]
The following βol-man.el
β file implements it
;;; ol-man.el - Support for links to man pages in Org mode
(require 'ol)
(org-link-set-parameters "man"
:follow #'org-man-open
:export #'org-man-export
:store #'org-man-store-link)
(defcustom org-man-command 'man
"The Emacs command to be used to display a man page."
:group 'org-link
:type '(choice (const man) (const woman)))
(defun org-man-open (path _)
"Visit the manpage on PATH.
PATH should be a topic that can be thrown at the man command."
(funcall org-man-command path))
(defun org-man-store-link ()
"Store a link to a man page."
(when (memq major-mode '(Man-mode woman-mode))
;; This is a man page, we do make this link.
(let* ((page (org-man-get-page-name))
(link (concat "man:" page))
(description (format "Man page for %s" page)))
(org-link-store-props
:type "man"
:link link
:description description))))
(defun org-man-get-page-name ()
"Extract the page name from the buffer name."
;; This works for both `Man-mode' and `woman-mode'.
(if (string-match " \\(\\S-+\\)\\*" (buffer-name))
(match-string 1 (buffer-name))
(error "Cannot create link to this man page")))
(defun org-man-export (link description format _)
"Export a man page link from Org files."
(let ((path (format "http://man.he.net/?topic=%s§ion=all" link))
(desc (or description link)))
(pcase format
(`html (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
(`latex (format "\\href{%s}{%s}" path desc))
(`texinfo (format "@uref{%s,%s}" path desc))
(`ascii (format "%s (%s)" desc path))
(t path))))
(provide ol-man)
;;; ol-man.el ends here
To activate links to man pages in Org, enter this in the Emacs init file:
(require 'ol-man)
A review of βol-man.el
β:
First, β
(require 'ol)
β ensures that βol.el
β is loaded.Then
org-link-set-parameters
defines a new link type with βman
β prefix and associates functions for following, exporting and storing such links. See the variableorg-link-parameters
for a complete list of possible associations.The rest of the file implements necessary variables and functions.
For example,
org-man-store-link
is responsible for storing a link whenorg-store-link
(see Handling Links) is called from a buffer displaying a man page. It first checks if the major mode is appropriate. If check fails, the function returnsnil
, which means it isnβt responsible for creating a link to the current buffer. Otherwise the function makes a link string by combining the βman:
β prefix with the man topic. It also provides a default description. The functionorg-insert-link
can insert it back into an Org buffer later on.