org-man.el 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ;;; org-man.el - Support for links to manpages in Org-mode
  2. ;;
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 1.0
  7. ;;
  8. ;; This file is not yet part of GNU Emacs.
  9. ;;
  10. ;; This program is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 3, or (at your option)
  13. ;; any later version.
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. (require 'org)
  24. (org-add-link-type "man" 'org-man-open 'org-man-export)
  25. (add-hook 'org-store-link-functions 'org-man-store-link)
  26. (defcustom org-man-command 'man
  27. "The Emacs command to be used to display a man page."
  28. :group 'org-link
  29. :type '(choice (const man) (const woman)))
  30. (defun org-man-open (path)
  31. "Visit the manpage on PATH.
  32. PATH should be a topic that can be thrown at the man command."
  33. (funcall org-man-command path))
  34. (defun org-man-store-link ()
  35. "Store a link to a README file."
  36. (when (memq major-mode '(Man-mode woman-mode))
  37. ;; This is a man page, we do make this link
  38. (let* ((page (org-man-get-page-name))
  39. (link (concat "man:" page))
  40. (description (format "Manpage for %s" page)))
  41. (org-store-link-props
  42. :type "man"
  43. :link link
  44. :description description))))
  45. (defun org-man-get-page-name ()
  46. "Extract the page name from the buffer name."
  47. ;; This works for both `Man-mode' and `woman-mode'.
  48. (if (string-match " \\(\\S-+\\)\\*" (buffer-name))
  49. (match-string 1 (buffer-name))
  50. (error "Cannot create link to this man page")))
  51. (defun org-man-export (link description format)
  52. "Export a man page link from Org files."
  53. (let ((path (format "http://man.he.net/?topic=%s&section=all" link))
  54. (desc (or description link)))
  55. (cond
  56. ((eq format 'html) (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
  57. ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
  58. ((eq format 'texinfo) (format "@uref{%s,%s}" path desc))
  59. ((eq format 'ascii) (format "%s (%s)" desc path))
  60. (t path))))
  61. (provide 'org-man)
  62. ;;; org-man.el ends here