summaryrefslogtreecommitdiff
path: root/lisp/man-completion/man-completion.el
blob: 31c5bf706ee91b3f4e9bbee96030b4de958ef70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(defun man-completion-is-compression-suffix (suffix)
  (string-match-p "\\.bz2$\\|gzip$\\|zst$" suffix))

(defun man-completion-get-man-paths ()
  (let* ((manpath (getenv "MANPATH"))
         (manpaths (split-string manpath ":" t))
         (manpaths (mapcar (lambda (path)
                             (file-expand-wildcards (file-name-concat path "man*")))
                           manpaths))
         (manpaths (flatten-list manpaths))
         (manpaths (seq-filter 'file-exists-p manpaths)))
    manpaths))

(defun man-completion-find-pages ()
  (let* ((files (mapcar (lambda (p)
                          (directory-files-recursively p ".+" nil))
                        (man-completion-get-man-paths)))
         (files (flatten-list files))
         (files (mapcar 'file-name-base files))
         (files (mapcar (lambda (f)
                          (if (man-completion-is-compression-suffix f)
                              (file-name-sans-extension f)
                            f))
                        files)))
    files))

(defun man-completion-locate-page (page)
  (with-temp-buffer
    (let ((exit-code (call-process "man" nil (current-buffer) nil "--where" page)))
      (when exit-code
        (let* ((output (buffer-string))
               (trimmed (string-trim-right output "\n")))
          trimmed)))))

(defun man-completion (orig &rest args)
  (interactive)
  (let* ((arg (if args
                  (nth 0 args)
                (completing-read "Select page: "
                                 (man-completion-find-pages)
                                 nil
                                 t)))
         (page (man-completion-locate-page arg)))
    (funcall orig page)))

(advice-add 'man :around 'man-completion)

(provide 'man-completion)