]> jturnerusa.dev Git - emacs.d/commitdiff
restored man wrapper functions into the new lisp directory
authorJohn Turner <jturner.usa@gmail.com>
Mon, 4 Jul 2022 03:47:39 +0000 (23:47 -0400)
committerJohn Turner <jturner.usa@gmail.com>
Mon, 4 Jul 2022 03:47:39 +0000 (23:47 -0400)
lisp/man-wrapper/man-wrapper.el [new file with mode: 0644]
load-local-lisp.el

diff --git a/lisp/man-wrapper/man-wrapper.el b/lisp/man-wrapper/man-wrapper.el
new file mode 100644 (file)
index 0000000..56544c3
--- /dev/null
@@ -0,0 +1,36 @@
+(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)
index 0e1fba1b7a58a545bb5ef9a017ee7821ceb06990..28ed25d1688bf3f8395c1b135e8ec16e04322d2d 100644 (file)
@@ -1,4 +1,7 @@
 (let ((local-lisp-directory (file-name-concat user-emacs-directory "lisp")))
-  (add-to-list 'load-path (file-name-concat local-lisp-directory "backup-before-save")))
+  (add-to-list 'load-path (file-name-concat local-lisp-directory "backup-before-save"))
+  (add-to-list 'load-path (file-name-concat local-lisp-directory "man-wrapper")))
 
 (require 'backup-before-save)
+(require 'man-wrapper)
+