ol-man.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. ;; This program 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, or (at your option)
  14. ;; any later version.
  15. ;; This program 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. (buffer (funcall org-man-command command)))
  42. (when search
  43. (with-current-buffer buffer
  44. (goto-char (point-min))
  45. (unless (search-forward search nil t)
  46. (let ((process (get-buffer-process buffer)))
  47. (while (process-live-p process)
  48. (accept-process-output process)))
  49. (goto-char (point-min))
  50. (search-forward search))
  51. (forward-line -1)
  52. (let ((point (point)))
  53. (let ((window (get-buffer-window buffer)))
  54. (set-window-point window point)
  55. (set-window-start window point)))))))
  56. (defun org-man-store-link ()
  57. "Store a link to a README file."
  58. (when (memq major-mode '(Man-mode woman-mode))
  59. ;; This is a man page, we do make this link
  60. (let* ((page (org-man-get-page-name))
  61. (link (concat "man:" page))
  62. (description (format "Manpage for %s" page)))
  63. (org-link-store-props
  64. :type "man"
  65. :link link
  66. :description description))))
  67. (defun org-man-get-page-name ()
  68. "Extract the page name from the buffer name."
  69. ;; This works for both `Man-mode' and `woman-mode'.
  70. (if (string-match " \\(\\S-+\\)\\*" (buffer-name))
  71. (match-string 1 (buffer-name))
  72. (error "Cannot create link to this man page")))
  73. (defun org-man-export (link description format)
  74. "Export a man page link from Org files."
  75. (let ((path (format "http://man.he.net/?topic=%s&section=all" link))
  76. (desc (or description link)))
  77. (cond
  78. ((eq format 'html) (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
  79. ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
  80. ((eq format 'texinfo) (format "@uref{%s,%s}" path desc))
  81. ((eq format 'ascii) (format "%s (%s)" desc path))
  82. ((eq format 'md) (format "[%s](%s)" desc path))
  83. (t path))))
  84. (provide 'ol-man)
  85. ;;; ol-man.el ends here