Next: , Previous: , Up: Hacking   [Contents][Index]


A.3 Adding hyperlink types

Org has many built-in hyperlink types (see Hyperlinks), and an interface for adding new link types. The example file, org-man.el, shows the process of adding Org links to Unix man pages, which look like this: ‘[[man:printf][The printf manpage]]’:

;;; org-man.el - Support for links to manpages in Org

(require 'org)

(org-add-link-type "man" 'org-man-open)
(add-hook 'org-store-link-functions '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 manpage."
  (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 "Manpage for %s" page)))
      (org-store-link-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")))

(provide 'org-man)

;;; org-man.el ends here

To activate links to man pages in Org, enter this in the init file:

(require 'org-man)

A review of org-man.el:

  1. First, (require 'org) ensures org.el is loaded.
  2. The org-add-link-type defines a new link type with ‘man’ prefix. The call contains the function to call that follows the link type.
  3. The next line adds a function to org-store-link-functions that records a useful link with the command C-c l in a buffer displaying a man page.

The rest of the file defines necessary variables and functions. First is the customization variable org-man-command. It has two options, man and woman. Next is a function whose argument is the link path, which for man pages is the topic of the man command. To follow the link, the function calls the org-man-command to display the man page.

C-c l constructs and stores the link.

C-c l calls the function org-man-store-link, which first checks if the major-mode is appropriate. If check fails, the function returns nil. Otherwise the function makes a link string by combining the ‘man:’ prefix with the man topic. The function then calls org-store-link-props with :type and :link properties. A :description property is an optional string that is displayed when the function inserts the link in the Org buffer.

C-c C-l inserts the stored link.

To define new link types, define a function that implements completion support with C-c C-l. This function should not accept any arguments but return the appropriate prefix and complete link string.


Next: , Previous: , Up: Hacking   [Contents][Index]