]> jturnerusa.dev Git - emacs.d/commitdiff
renamed man-wrapper to man-completion
authorJohn Turner <jturner.usa@gmail.com>
Sat, 22 Oct 2022 02:57:24 +0000 (22:57 -0400)
committerJohn Turner <jturner.usa@gmail.com>
Sat, 22 Oct 2022 02:57:24 +0000 (22:57 -0400)
init.el
lisp/man-completion/man-completion.el [new file with mode: 0644]
lisp/man-wrapper/man-wrapper.el [deleted file]

diff --git a/init.el b/init.el
index e5907c2c14dc65167aa95c4760ec692547cdd93e..2bf6153c8c71911eaa781816c160ac8106046004 100644 (file)
--- a/init.el
+++ b/init.el
@@ -93,6 +93,6 @@
   (require 'init-native-comp))
 
 (let ((local-lisp-directory (file-name-concat user-emacs-directory "lisp")))
-  (add-to-list 'load-path (file-name-concat local-lisp-directory "man-wrapper")))
+  (add-to-list 'load-path (file-name-concat local-lisp-directory "man-completion")))
 
-(require 'man-wrapper)
+(require 'man-completion)
diff --git a/lisp/man-completion/man-completion.el b/lisp/man-completion/man-completion.el
new file mode 100644 (file)
index 0000000..31c5bf7
--- /dev/null
@@ -0,0 +1,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)
diff --git a/lisp/man-wrapper/man-wrapper.el b/lisp/man-wrapper/man-wrapper.el
deleted file mode 100644 (file)
index 56544c3..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-(defun man-wrapper-find-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 man-wrapper-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-wrapper (orig &rest args)
-  (interactive)
-  (let* ((arg (if args
-                  (nth 0 args)
-                (completing-read "Select page: " (man-wrapper-find-pages))))
-         (page (man-wrapper-locate-page arg)))
-    (funcall orig page)))
-
-(advice-add 'man :around 'man-wrapper)
-
-(provide 'man-wrapper)