mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 76bbd07de7add0f9d13c6914f158d19630fe2f62
commit | author | age
5cb5f7 1 ;;; helm-man.el --- Man and woman UI -*- lexical-binding: t -*-
C 2
3 ;; Copyright (C) 2012 ~ 2018 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Code:
19
20 (require 'cl-lib)
21 (require 'helm)
22 (require 'helm-help)
23
24 (defvar woman-topic-all-completions)
25 (defvar woman-manpath)
26 (defvar woman-path)
27 (defvar woman-expanded-directory-path)
28 (declare-function woman-file-name "woman.el" (topic &optional re-cache))
29 (declare-function woman-file-name-all-completions "woman.el" (topic))
30 (declare-function Man-getpage-in-background "man.el" (topic))
31 (declare-function woman-expand-directory-path "woman.el" (path-dirs path-regexps))
32 (declare-function woman-topic-all-completions "woman.el" (path))
33 (declare-function helm-generic-sort-fn "helm-utils.el" (S1 S2))
34
35 (defgroup helm-man nil
36   "Man and Woman applications for helm."
37   :group 'helm)
38
39 (defcustom helm-man-or-woman-function 'Man-getpage-in-background
40   "Default command to display a man page."
41   :group 'helm-man
42   :type '(radio :tag "Preferred command to display a man page"
43           (const :tag "Man" Man-getpage-in-background)
44           (const :tag "Woman" woman)))
45
46 (defcustom helm-man-format-switches (cl-case system-type
47                                       ((darwin macos) "%s")
48                                       (t "-l %s"))
49   "Arguments to pass to the `manual-entry' function.
50 Arguments are passed to `manual-entry' with `format.'"
51   :group 'helm-man
52   :type 'string)
53
54 ;; Internal
55 (defvar helm-man--pages nil
56   "All man pages on system.
57 Will be calculated the first time you invoke helm with this
58 source.")
59
60 (defun helm-man-default-action (candidate)
61   "Default action for jumping to a woman or man page from helm."
62   (let ((wfiles (mapcar #'car (woman-file-name-all-completions candidate))))
63     (condition-case nil
64         (let ((file (if (cdr wfiles)
65                         (helm-comp-read "ManFile: " wfiles :must-match t)
66                       (car wfiles))))
67           (if (eq helm-man-or-woman-function 'Man-getpage-in-background)
68               (manual-entry (format helm-man-format-switches file))
69             (condition-case nil
70                 (woman-find-file file)
71               ;; If woman is unable to format correctly
72               ;; try Man instead.
73               (error (kill-buffer)
74                      (manual-entry (format helm-man-format-switches file))))))
75       ;; If even Man failed with file as argument, try again with Man
76       ;; but using Topic candidate instead of the file calculated by
77       ;; woman.
78       (error (kill-buffer)
79              (Man-getpage-in-background candidate)))))
80
81 (defun helm-man--init ()
82   (require 'woman)
83   (require 'helm-utils)
84   (unless helm-man--pages
85     (setq woman-expanded-directory-path
86           (woman-expand-directory-path woman-manpath woman-path))
87     (setq woman-topic-all-completions
88           (woman-topic-all-completions woman-expanded-directory-path))
89     (setq helm-man--pages (mapcar 'car woman-topic-all-completions)))
90   (helm-init-candidates-in-buffer 'global helm-man--pages))
91
92 (defvar helm-source-man-pages
93   (helm-build-in-buffer-source "Manual Pages"
94     :init #'helm-man--init
95     :persistent-action #'ignore
96     :filtered-candidate-transformer
97      (lambda (candidates _source)
98        (sort candidates #'helm-generic-sort-fn))
99     :action  '(("Display Man page" . helm-man-default-action))
100     :group 'helm-man))
101
102 ;;;###autoload
103 (defun helm-man-woman (arg)
104   "Preconfigured `helm' for Man and Woman pages.
105 With a prefix arg reinitialize the cache."
106   (interactive "P")
107   (when arg (setq helm-man--pages nil))
108     (helm :sources 'helm-source-man-pages
109           :buffer "*helm man woman*"))
110
111 (provide 'helm-man)
112
113 ;; Local Variables:
114 ;; byte-compile-warnings: (not obsolete)
115 ;; coding: utf-8
116 ;; indent-tabs-mode: nil
117 ;; End:
118
119 ;;; helm-man.el ends here