summaryrefslogtreecommitdiff
path: root/config/man/config-man-helper-functions.el
blob: a58ba756cf4b8c8bec16db586cf0be991f7f0266 (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
(defun config-man-pages ()
  (let* ((manpath (getenv "MANPATH"))
         (directories (split-string manpath ":" t))
         (that-exist (seq-filter 'file-exists-p directories))
         (files (mapcar (lambda (d)
                          (directory-files-recursively d ".*" nil))
                        that-exist))
         (flattened (flatten-list files))
         (pages (mapcar 'file-name-nondirectory flattened))
         (without-second-ext (mapcar (lambda (p)
                                       (let ((extension (file-name-extension p)))
                                         (if (string-match-p "^[0-9+]$" extension)
                                             p
                                           (file-name-sans-extension p))))
                                     pages)))
    without-second-ext))

(defun config-man-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 config-man-advice (orig &rest args)
  (interactive)
  (let ((page (or args (list (completing-read "Select page: " (config-man-pages) nil t)))))
    (apply orig page)))

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