1;;; ol-man.el --- Links to man pages -*- lexical-binding: t; -*-
2;;
3;; Copyright (C) 2020-2021 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
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
23;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24;;
25;;; Commentary:
26
27(require 'ol)
28
29(org-link-set-parameters "man"
30			 :follow #'org-man-open
31			 :export #'org-man-export
32			 :store #'org-man-store-link)
33
34(defcustom org-man-command 'man
35  "The Emacs command to be used to display a man page."
36  :group 'org-link
37  :type '(choice (const man) (const woman)))
38
39(defun org-man-open (path _)
40  "Visit the manpage on PATH.
41PATH should be a topic that can be thrown at the man command.
42If PATH contains extra ::STRING which will use `occur' to search
43matched strings in man buffer."
44  (string-match "\\(.*?\\)\\(?:::\\(.*\\)\\)?$" path)
45  (let* ((command (match-string 1 path))
46	 (search (match-string 2 path)))
47    (funcall org-man-command command)
48    (when search
49      (with-current-buffer (concat "*Man " command "*")
50	(goto-char (point-min))
51	(search-forward search)))))
52
53(defun org-man-store-link ()
54  "Store a link to a README file."
55  (when (memq major-mode '(Man-mode woman-mode))
56    ;; This is a man page, we do make this link
57    (let* ((page (org-man-get-page-name))
58           (link (concat "man:" page))
59           (description (format "Manpage for %s" page)))
60      (org-link-store-props
61       :type "man"
62       :link link
63       :description description))))
64
65(defun org-man-get-page-name ()
66  "Extract the page name from the buffer name."
67  ;; This works for both `Man-mode' and `woman-mode'.
68  (if (string-match " \\(\\S-+\\)\\*" (buffer-name))
69      (match-string 1 (buffer-name))
70    (error "Cannot create link to this man page")))
71
72(defun org-man-export (link description format)
73  "Export a man page link from Org files."
74  (let ((path (format "http://man.he.net/?topic=%s&section=all" link))
75	(desc (or description link)))
76    (cond
77     ((eq format 'html) (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
78     ((eq format 'latex) (format "\\href{%s}{%s}" path desc))
79     ((eq format 'texinfo) (format "@uref{%s,%s}" path desc))
80     ((eq format 'ascii) (format "%s (%s)" desc path))
81     ((eq format 'md) (format "[%s](%s)" desc path))
82     (t path))))
83
84(provide 'ol-man)
85
86;;; ol-man.el ends here
87