diff --git a/lisp/man-wrapper/man-wrapper.el b/lisp/man-wrapper/man-wrapper.el new file mode 100644 index 0000000..56544c3 --- /dev/null +++ b/lisp/man-wrapper/man-wrapper.el @@ -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) diff --git a/load-local-lisp.el b/load-local-lisp.el index 0e1fba1..28ed25d 100644 --- a/load-local-lisp.el +++ b/load-local-lisp.el @@ -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) +