ol-man.el 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; ol-man.el - Links to man pages
  2. ;;
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: https://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 <https://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. (require 'ol)
  24. (org-link-set-parameters "man"
  25. :follow #'org-man-open
  26. :export #'org-man-export
  27. :store #'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. If PATH contains extra ::STRING which will use `occur' to search
  36. matched strings in man buffer."
  37. (string-match "\\(.*?\\)\\(?:::\\(.*\\)\\)?$" path)
  38. (let* ((command (match-string 1 path))
  39. (search (match-string 2 path)))
  40. (funcall org-man-command command)
  41. (when search
  42. (with-current-buffer (concat "*Man " command "*")
  43. (goto-char (point-min))
  44. (search-forward search)))))
  45. (defun org-man-store-link ()
  46. "Store a link to a README file."
  47. (when (memq major-mode '(Man-mode woman-mode))
  48. ;; This is a man page, we do make this link
  49. (let* ((page (org-man-get-page-name))
  50. (link (concat "man:" page))
  51. (description (format "Manpage for %s" page)))
  52. (org-link-store-props
  53. :type "man"
  54. :link link
  55. :description description))))
  56. (defun org-man-get-page-name ()
  57. "Extract the page name from the buffer name."
  58. ;; This works for both `Man-mode' and `woman-mode'.
  59. (if (string-match " \\(\\S-+\\)\\*" (buffer-name))
  60. (match-string 1 (buffer-name))
  61. (error "Cannot create link to this man page")))
  62. (defun org-man-export (link description format)
  63. "Export a man page link from Org files."
  64. (let ((path (format "http://man.he.net/?topic=%s&section=all" link))
  65. (desc (or description link)))
  66. (cond
  67. ((eq format 'html) (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
  68. ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
  69. ((eq format 'texinfo) (format "@uref{%s,%s}" path desc))
  70. ((eq format 'ascii) (format "%s (%s)" desc path))
  71. ((eq format 'md) (format "[%s](%s)" desc path))
  72. (t path))))
  73. (provide 'ol-man)
  74. ;;; ol-man.el ends here