ol-man.el 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ;;; ol-man.el --- Links to man pages -*- lexical-binding: t; -*-
  2. ;;
  3. ;; Copyright (C) 2020-2022 Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
  5. ;; Maintainer: Bastien Guerry <bzg@gnu.org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: https://orgmode.org
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. (require 'ol)
  25. (org-link-set-parameters "man"
  26. :follow #'org-man-open
  27. :export #'org-man-export
  28. :store #'org-man-store-link)
  29. (defcustom org-man-command 'man
  30. "The Emacs command to be used to display a man page."
  31. :group 'org-link
  32. :type '(choice (const man) (const woman)))
  33. (defun org-man-open (path _)
  34. "Visit the manpage on PATH.
  35. PATH should be a topic that can be thrown at the man command.
  36. If PATH contains extra ::STRING which will use `occur' to search
  37. matched strings in man buffer."
  38. (string-match "\\(.*?\\)\\(?:::\\(.*\\)\\)?$" path)
  39. (let* ((command (match-string 1 path))
  40. (search (match-string 2 path)))
  41. (funcall org-man-command command)
  42. (when search
  43. (with-current-buffer (concat "*Man " command "*")
  44. (goto-char (point-min))
  45. (search-forward search)))))
  46. (defun org-man-store-link ()
  47. "Store a link to a README file."
  48. (when (memq major-mode '(Man-mode woman-mode))
  49. ;; This is a man page, we do make this link
  50. (let* ((page (org-man-get-page-name))
  51. (link (concat "man:" page))
  52. (description (format "Manpage for %s" page)))
  53. (org-link-store-props
  54. :type "man"
  55. :link link
  56. :description description))))
  57. (defun org-man-get-page-name ()
  58. "Extract the page name from the buffer name."
  59. ;; This works for both `Man-mode' and `woman-mode'.
  60. (if (string-match " \\(\\S-+\\)\\*" (buffer-name))
  61. (match-string 1 (buffer-name))
  62. (error "Cannot create link to this man page")))
  63. (defun org-man-export (link description format)
  64. "Export a man page link from Org files."
  65. (let ((path (format "http://man.he.net/?topic=%s&section=all" link))
  66. (desc (or description link)))
  67. (cond
  68. ((eq format 'html) (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
  69. ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
  70. ((eq format 'texinfo) (format "@uref{%s,%s}" path desc))
  71. ((eq format 'ascii) (format "%s (%s)" desc path))
  72. ((eq format 'md) (format "[%s](%s)" desc path))
  73. (t path))))
  74. (provide 'ol-man)
  75. ;;; ol-man.el ends here