ol-man.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. ;; URL: 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 'org-macs)
  25. (org-assert-version)
  26. (require 'ol)
  27. (org-link-set-parameters "man"
  28. :follow #'org-man-open
  29. :export #'org-man-export
  30. :store #'org-man-store-link)
  31. (defcustom org-man-command 'man
  32. "The Emacs command to be used to display a man page."
  33. :group 'org-link
  34. :type '(choice (const man) (const woman)))
  35. (defun org-man-open (path _)
  36. "Visit the manpage on PATH.
  37. PATH should be a topic that can be thrown at the man command.
  38. If PATH contains extra ::STRING which will use `occur' to search
  39. matched strings in man buffer."
  40. (string-match "\\(.*?\\)\\(?:::\\(.*\\)\\)?$" path)
  41. (let* ((command (match-string 1 path))
  42. (search (match-string 2 path))
  43. (buffer (funcall org-man-command command)))
  44. (when search
  45. (with-current-buffer buffer
  46. (goto-char (point-min))
  47. (unless (search-forward search nil t)
  48. (let ((process (get-buffer-process buffer)))
  49. (while (process-live-p process)
  50. (accept-process-output process)))
  51. (goto-char (point-min))
  52. (search-forward search))
  53. (forward-line -1)
  54. (let ((point (point)))
  55. (let ((window (get-buffer-window buffer)))
  56. (set-window-point window point)
  57. (set-window-start window point)))))))
  58. (defun org-man-store-link ()
  59. "Store a link to a README file."
  60. (when (memq major-mode '(Man-mode woman-mode))
  61. ;; This is a man page, we do make this link
  62. (let* ((page (org-man-get-page-name))
  63. (link (concat "man:" page))
  64. (description (format "Manpage for %s" page)))
  65. (org-link-store-props
  66. :type "man"
  67. :link link
  68. :description description))))
  69. (defun org-man-get-page-name ()
  70. "Extract the page name from the buffer name."
  71. ;; This works for both `Man-mode' and `woman-mode'.
  72. (if (string-match " \\(\\S-+\\)\\*" (buffer-name))
  73. (match-string 1 (buffer-name))
  74. (error "Cannot create link to this man page")))
  75. (defun org-man-export (link description format)
  76. "Export a man page link from Org files."
  77. (let ((path (format "http://man.he.net/?topic=%s&section=all" link))
  78. (desc (or description link)))
  79. (cond
  80. ((eq format 'html) (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
  81. ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
  82. ((eq format 'texinfo) (format "@uref{%s,%s}" path desc))
  83. ((eq format 'ascii) (format "%s (%s)" desc path))
  84. ((eq format 'md) (format "[%s](%s)" desc path))
  85. (t path))))
  86. (provide 'ol-man)
  87. ;;; ol-man.el ends here