From 0feb33b83c2432b4535702fa6926a1d6cd59919d Mon Sep 17 00:00:00 2001 From: John Turner Date: Fri, 21 Oct 2022 22:57:24 -0400 Subject: renamed man-wrapper to man-completion --- lisp/man-completion/man-completion.el | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lisp/man-completion/man-completion.el (limited to 'lisp/man-completion/man-completion.el') diff --git a/lisp/man-completion/man-completion.el b/lisp/man-completion/man-completion.el new file mode 100644 index 0000000..31c5bf7 --- /dev/null +++ b/lisp/man-completion/man-completion.el @@ -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) -- cgit v1.2.3