Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement consult-help command which allows to search all documentation strings #67

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ You may want to add a function which pulses the current line, e.g.,
(defvar-local consult-imenu-history nil
"Buffer-local history for the command `consult-imenu'.")

(defvar consult-help-history nil
"History for the command `consult-help'.")

(defvar consult-buffer-history nil
"History for the command `consult-buffer'.")

Expand Down Expand Up @@ -786,6 +789,80 @@ This command obeys narrowing. Optionally INITIAL input can be provided."
:category 'file
:history 'file-name-history))

(defun consult--help-candidates ()
"Return list of help candidates."
(message "Loading help...")
(let ((candidates))
(mapatoms
(lambda (sym)
(let ((str (symbol-name sym)))
(when (facep sym)
(push (consult--narrow-candidate ?a str (marginalia-annotate-face str)) candidates))
(when (and (fboundp 'cl-find-class) (cl-find-class sym))
(push (consult--narrow-candidate ?t str (marginalia-annotate-symbol str)) candidates))
(when (fboundp sym)
(cond
((and (commandp sym) (where-is-internal sym nil t))
(push (consult--narrow-candidate ?b str (marginalia-annotate-symbol str)) candidates))
((commandp sym)
(push (consult--narrow-candidate ?c str (marginalia-annotate-symbol str)) candidates))
((macrop sym)
(push (consult--narrow-candidate ?m str (marginalia-annotate-symbol str)) candidates)))
(push (consult--narrow-candidate ?f str (marginalia-annotate-symbol str)) candidates))
(when (boundp sym)
(when (custom-variable-p sym)
(push (consult--narrow-candidate ?u str (marginalia-annotate-symbol str)) candidates))
(push (consult--narrow-candidate ?v str (marginalia-annotate-symbol str)) candidates)))))
;; TODO better sorting, such that bindings or commands appear first?
(sort candidates #'string<)))

(defvar consult--help-cache nil)

;;;###autoload
(defun consult-help (&optional initial)
"Open help.
Optionally INITIAL input can be provided."
(interactive)
(minibuffer-with-setup-hook
(lambda ()
;; TODO move this to consult-selectrum
(setq-local selectrum-refine-candidates-function #'selectrum-default-candidate-refine-function))
(when-let* ((selected (consult--read "Help: "
(or consult--help-cache
(setq consult--help-cache (consult--with-increased-gc (consult--help-candidates))))
:category 'consult-help
:require-match t
:initial initial
:sort nil
:narrow
'((?f . "Function")
(?c . "Command")
(?b . "Binding")
(?m . "Macro")
(?u . "Custom Variable")
(?v . "Variable")
(?a . "Face")
(?t . "CL Type"))
:history 'consult-help-history))
(name (intern-soft (replace-regexp-in-string "^[^ ]+ \\| .*" "" selected))))
(pcase (elt selected 0)
;; lookup describe function via keybinding in order to support helpful
(?f (funcall (key-binding "\C-hf") name))
(?c (funcall (key-binding "\C-hf") name))
(?b (funcall (key-binding "\C-hf") name))
(?m (funcall (key-binding "\C-hf") name))
(?u (funcall (key-binding "\C-hv") name))
(?v (funcall (key-binding "\C-hv") name))
(?v (funcall (key-binding "\C-hv") name))
(?a (describe-face name))
(?t (funcall (key-binding "\C-ho") name))))))

;;;###autoload
(defun consult-help-symbol-at-point ()
"Open help for a symbol at point."
(interactive)
(consult-help (thing-at-point 'symbol)))

;;;###autoload
(defun consult-recent-file ()
"Find recent using `completing-read'."
Expand Down