org-man.el 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. ;; GNU Emacs 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. ;; GNU Emacs 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; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. (require 'org)
  26. (org-add-link-type "man" 'org-man-open 'org-man-export)
  27. (add-hook 'org-store-link-functions 'org-man-store-link)
  28. (defcustom org-man-command 'man
  29. "The Emacs command to be used to display a man page."
  30. :group 'org-link
  31. :type '(choice (const man) (const woman)))
  32. (defun org-man-open (path)
  33. "Visit the manpage on PATH.
  34. PATH should be a topic that can be thrown at the man command."
  35. (funcall org-man-command path))
  36. (defun org-man-store-link ()
  37. "Store a link to a README file."
  38. (when (memq major-mode '(Man-mode woman-mode))
  39. ;; This is a man page, we do make this link
  40. (let* ((page (org-man-get-page-name))
  41. (link (concat "man:" page))
  42. (description (format "Manpage for %s" page)))
  43. (org-store-link-props
  44. :type "man"
  45. :link link
  46. :description description))))
  47. (defun org-man-get-page-name ()
  48. "Extract the page name from the buffer name."
  49. ;; This works for both `Man-mode' and `woman-mode'.
  50. (if (string-match " \\(\\S-+\\)\\*" (buffer-name))
  51. (match-string 1 (buffer-name))
  52. (error "Cannot create link to this man page")))
  53. (defun org-man-export (link description format)
  54. "Export a man page link from Org files."
  55. (let ((path (format "http://man.he.net/?topic=%s&section=all" link))
  56. (desc (or description link)))
  57. (cond
  58. ((eq format 'html) (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
  59. ((eq format 'latex) (format "\href{%s}{%s}" path desc))
  60. ((eq format 'ascii) (format "%s (%s)" desc path))
  61. (t path))))
  62. (provide 'org-man)
  63. ;;; org-man.el ends here