2021-10-07 13:36:55 -04:00
|
|
|
;;; ol-man.el --- Links to man pages -*- lexical-binding: t; -*-
|
2021-10-07 11:47:14 -04:00
|
|
|
;;
|
2023-01-01 05:31:12 -05:00
|
|
|
;; Copyright (C) 2020-2023 Free Software Foundation, Inc.
|
2021-10-07 11:47:14 -04:00
|
|
|
;; Author: Carsten Dominik <carsten.dominik@gmail.com>
|
|
|
|
;; Maintainer: Bastien Guerry <bzg@gnu.org>
|
|
|
|
;; Keywords: outlines, hypermedia, calendar, wp
|
2021-09-26 03:44:29 -04:00
|
|
|
;; URL: https://orgmode.org
|
2021-10-07 11:47:14 -04:00
|
|
|
;;
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;;
|
2021-12-20 16:27:54 -05:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2021-10-07 11:47:14 -04:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2021-12-20 16:27:54 -05:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2021-10-07 11:47:14 -04:00
|
|
|
|
2021-12-20 16:27:54 -05:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2021-10-07 11:47:14 -04:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;
|
|
|
|
;;; Commentary:
|
|
|
|
|
2023-10-25 06:46:00 -04:00
|
|
|
;; This file implements links to man pages from within Org mode.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2022-08-04 09:53:05 -04:00
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2021-10-07 11:47:14 -04:00
|
|
|
(require 'ol)
|
|
|
|
|
|
|
|
(org-link-set-parameters "man"
|
2023-10-09 07:47:04 -04:00
|
|
|
:complete #'org-man-complete
|
2021-10-07 11:47:14 -04:00
|
|
|
:follow #'org-man-open
|
|
|
|
:export #'org-man-export
|
|
|
|
:store #'org-man-store-link)
|
|
|
|
|
|
|
|
(defcustom org-man-command 'man
|
|
|
|
"The Emacs command to be used to display a man page."
|
|
|
|
:group 'org-link
|
2023-12-13 10:03:36 -05:00
|
|
|
:type '(choice (const man) (const :tag "WoMan (obsolete)" woman)))
|
2021-10-07 11:47:14 -04:00
|
|
|
|
|
|
|
(defun org-man-open (path _)
|
|
|
|
"Visit the manpage on PATH.
|
|
|
|
PATH should be a topic that can be thrown at the man command.
|
|
|
|
If PATH contains extra ::STRING which will use `occur' to search
|
|
|
|
matched strings in man buffer."
|
|
|
|
(string-match "\\(.*?\\)\\(?:::\\(.*\\)\\)?$" path)
|
|
|
|
(let* ((command (match-string 1 path))
|
2022-07-29 02:33:22 -04:00
|
|
|
(search (match-string 2 path))
|
|
|
|
(buffer (funcall org-man-command command)))
|
2021-10-07 11:47:14 -04:00
|
|
|
(when search
|
2022-07-29 02:33:22 -04:00
|
|
|
(with-current-buffer buffer
|
|
|
|
(goto-char (point-min))
|
|
|
|
(unless (search-forward search nil t)
|
|
|
|
(let ((process (get-buffer-process buffer)))
|
|
|
|
(while (process-live-p process)
|
|
|
|
(accept-process-output process)))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(search-forward search))
|
|
|
|
(forward-line -1)
|
|
|
|
(let ((point (point)))
|
|
|
|
(let ((window (get-buffer-window buffer)))
|
|
|
|
(set-window-point window point)
|
|
|
|
(set-window-start window point)))))))
|
2021-10-07 11:47:14 -04:00
|
|
|
|
|
|
|
(defun org-man-store-link ()
|
|
|
|
"Store a link to a README file."
|
|
|
|
(when (memq major-mode '(Man-mode woman-mode))
|
|
|
|
;; This is a man page, we do make this link
|
|
|
|
(let* ((page (org-man-get-page-name))
|
|
|
|
(link (concat "man:" page))
|
|
|
|
(description (format "Manpage for %s" page)))
|
|
|
|
(org-link-store-props
|
|
|
|
:type "man"
|
|
|
|
:link link
|
|
|
|
:description description))))
|
|
|
|
|
|
|
|
(defun org-man-get-page-name ()
|
|
|
|
"Extract the page name from the buffer name."
|
|
|
|
;; This works for both `Man-mode' and `woman-mode'.
|
|
|
|
(if (string-match " \\(\\S-+\\)\\*" (buffer-name))
|
|
|
|
(match-string 1 (buffer-name))
|
|
|
|
(error "Cannot create link to this man page")))
|
|
|
|
|
2023-10-17 07:00:01 -04:00
|
|
|
(defun org-man-export (link description backend)
|
|
|
|
"Export a man page LINK with DESCRIPTION.
|
|
|
|
BACKEND is the current export backend."
|
2021-10-07 11:47:14 -04:00
|
|
|
(let ((path (format "http://man.he.net/?topic=%s§ion=all" link))
|
|
|
|
(desc (or description link)))
|
|
|
|
(cond
|
2023-10-17 07:00:01 -04:00
|
|
|
((eq backend 'html) (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
|
|
|
|
((eq backend 'latex) (format "\\href{%s}{%s}" path desc))
|
|
|
|
((eq backend 'texinfo) (format "@uref{%s,%s}" path desc))
|
2023-10-24 10:45:36 -04:00
|
|
|
((eq backend 'ascii) (format "[%s] (<%s>)" desc path))
|
2023-10-17 07:00:01 -04:00
|
|
|
((eq backend 'md) (format "[%s](%s)" desc path))
|
2021-10-07 11:47:14 -04:00
|
|
|
(t path))))
|
|
|
|
|
2023-10-09 07:47:04 -04:00
|
|
|
(defun org-man-complete (&optional _arg)
|
|
|
|
"Complete man pages for `org-insert-link'."
|
|
|
|
(require 'man)
|
|
|
|
(concat
|
|
|
|
"man:"
|
|
|
|
(let ((completion-ignore-case t) ; See `man' comments.
|
|
|
|
(Man-completion-cache)) ; See `man' implementation.
|
|
|
|
(completing-read
|
|
|
|
"Manual entry: "
|
|
|
|
'Man-completion-table))))
|
|
|
|
|
2021-10-07 11:47:14 -04:00
|
|
|
(provide 'ol-man)
|
|
|
|
|
|
|
|
;;; ol-man.el ends here
|