2021-07-09 20:16:11 +03:00
|
|
|
;;; completion/vertico/autoload/vertico.el -*- lexical-binding: t; -*-
|
2021-02-16 16:46:35 -06:00
|
|
|
|
2021-07-24 16:17:04 +03:00
|
|
|
;; To prevent "Unused lexical variable" warning from +vertico--company-capf--candidates-a
|
|
|
|
;;;###autoload
|
|
|
|
(defvar orderless-match-faces)
|
|
|
|
|
2021-07-31 10:50:00 +03:00
|
|
|
;; To prevent "Defining as dynamic an already lexical var" from +vertico/embark-preview
|
|
|
|
;;;###autoload
|
|
|
|
(defvar embark-quit-after-action)
|
|
|
|
|
2021-02-16 16:46:35 -06:00
|
|
|
;;;###autoload
|
2021-07-09 20:28:40 +03:00
|
|
|
(defadvice! +vertico--company-capf--candidates-a (fn &rest args)
|
2021-07-07 01:24:31 +03:00
|
|
|
"Highlight company matches correctly, and try default completion styles before
|
|
|
|
orderless."
|
2021-07-31 10:49:00 +03:00
|
|
|
:around #'company-capf--candidates
|
2021-07-07 01:24:31 +03:00
|
|
|
(let ((orderless-match-faces [completions-common-part])
|
2021-07-24 17:21:55 +03:00
|
|
|
(completion-styles +vertico-company-completion-styles))
|
2021-02-16 16:46:35 -06:00
|
|
|
(apply fn args)))
|
|
|
|
|
2021-07-31 10:47:00 +03:00
|
|
|
;;;###autoload
|
|
|
|
(defadvice! +vertico--consult-recent-file-a (&rest _args)
|
|
|
|
"`consult-recent-file' needs to have `recentf-mode' on to work correctly"
|
|
|
|
:before #'consult-recent-file
|
|
|
|
(recentf-mode +1))
|
|
|
|
|
2021-01-16 20:21:50 +08:00
|
|
|
;;;###autoload
|
2021-07-09 20:28:40 +03:00
|
|
|
(cl-defun +vertico-file-search (&key query in all-files (recursive t) prompt args)
|
2021-01-16 20:21:50 +08:00
|
|
|
"Conduct a file search using ripgrep.
|
2021-02-22 15:47:06 -06:00
|
|
|
|
2021-01-16 20:21:50 +08:00
|
|
|
:query STRING
|
|
|
|
Determines the initial input to search for.
|
|
|
|
:in PATH
|
2021-02-16 20:42:41 -06:00
|
|
|
Sets what directory to base the search out of. Defaults to the current project's root.
|
2021-01-16 20:21:50 +08:00
|
|
|
:recursive BOOL
|
|
|
|
Whether or not to search files recursively from the base directory."
|
|
|
|
(declare (indent defun))
|
|
|
|
(unless (executable-find "rg")
|
|
|
|
(user-error "Couldn't find ripgrep in your PATH"))
|
|
|
|
(require 'consult)
|
2021-02-16 20:42:41 -06:00
|
|
|
(setq deactivate-mark t)
|
2021-05-17 13:20:02 +03:00
|
|
|
(let* ((project-root (or (doom-project-root) default-directory))
|
2021-02-23 22:12:10 -06:00
|
|
|
(directory (or in project-root))
|
2021-08-10 17:48:00 +03:00
|
|
|
(consult-ripgrep-args
|
2021-08-17 22:06:22 +03:00
|
|
|
(concat "rg "
|
|
|
|
(if all-files "-uu ")
|
|
|
|
(unless recursive "--maxdepth 1 ")
|
|
|
|
"--line-buffered --color=never --max-columns=1000 "
|
|
|
|
"--path-separator / --smart-case --no-heading --line-number "
|
|
|
|
"--hidden -g !.git "
|
|
|
|
(mapconcat #'shell-quote-argument args " ")
|
|
|
|
"."))
|
2021-07-26 22:44:51 -04:00
|
|
|
(prompt (if (stringp prompt) (string-trim prompt) "Search"))
|
2021-02-23 22:12:10 -06:00
|
|
|
(query (or query
|
|
|
|
(when (doom-region-active-p)
|
2021-08-10 17:48:00 +03:00
|
|
|
(regexp-quote (doom-thing-at-point-or-region)))))
|
2021-07-26 22:44:51 -04:00
|
|
|
(consult-async-split-style consult-async-split-style)
|
|
|
|
(consult-async-split-styles-alist consult-async-split-styles-alist))
|
|
|
|
;; Change the split style if the initial query contains the separator.
|
|
|
|
(when query
|
|
|
|
(cl-destructuring-bind (&key type separator initial)
|
|
|
|
(consult--async-split-style)
|
|
|
|
(pcase type
|
|
|
|
(`separator
|
|
|
|
(replace-regexp-in-string (regexp-quote (char-to-string separator))
|
2021-07-29 09:42:42 +07:00
|
|
|
(concat "\\" (char-to-string separator))
|
2021-07-26 22:44:51 -04:00
|
|
|
query t t))
|
|
|
|
(`perl
|
|
|
|
(when (string-match-p initial query)
|
|
|
|
(setf (alist-get 'perlalt consult-async-split-styles-alist)
|
|
|
|
`(:initial ,(or (cl-loop for char in (list "%" "@" "!" "&" "/" ";")
|
|
|
|
unless (string-match-p char query)
|
|
|
|
return char)
|
|
|
|
"%")
|
|
|
|
:type perl)
|
|
|
|
consult-async-split-style 'perlalt))))))
|
2021-08-10 17:48:00 +03:00
|
|
|
(consult--grep prompt #'consult--ripgrep-builder directory query)))
|
2021-01-16 20:21:50 +08:00
|
|
|
|
|
|
|
;;;###autoload
|
2021-07-09 20:28:40 +03:00
|
|
|
(defun +vertico/project-search (&optional arg initial-query directory)
|
2021-02-16 20:42:41 -06:00
|
|
|
"Peforms a live project search from the project root using ripgrep.
|
2021-01-16 20:21:50 +08:00
|
|
|
If ARG (universal argument), include all files, even hidden or compressed ones,
|
|
|
|
in the search."
|
|
|
|
(interactive "P")
|
2021-07-09 20:28:40 +03:00
|
|
|
(+vertico-file-search :query initial-query :in directory :all-files arg))
|
2021-01-16 20:21:50 +08:00
|
|
|
|
|
|
|
;;;###autoload
|
2021-07-09 20:28:40 +03:00
|
|
|
(defun +vertico/project-search-from-cwd (&optional arg initial-query)
|
2021-02-16 20:42:41 -06:00
|
|
|
"Performs a live project search from the current directory.
|
2021-01-16 20:21:50 +08:00
|
|
|
If ARG (universal argument), include all files, even hidden or compressed ones."
|
|
|
|
(interactive "P")
|
2021-07-09 20:28:40 +03:00
|
|
|
(+vertico/project-search arg initial-query default-directory))
|
2021-02-19 20:48:34 -06:00
|
|
|
|
|
|
|
;;;###autoload
|
2021-07-09 20:28:40 +03:00
|
|
|
(defun +vertico/search-symbol-at-point ()
|
2021-02-19 20:48:34 -06:00
|
|
|
(interactive)
|
|
|
|
(consult-line (thing-at-point 'symbol)))
|
2021-05-07 21:27:14 +03:00
|
|
|
|
2021-07-28 11:51:19 -04:00
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico-embark-target-package-fn ()
|
|
|
|
"Targets Doom's package! statements and returns the package name"
|
|
|
|
(when (or (derived-mode-p 'emacs-lisp-mode) (derived-mode-p 'org-mode))
|
|
|
|
(save-excursion
|
2021-08-21 17:01:58 +03:00
|
|
|
(when (and (search-backward "(" nil t)
|
|
|
|
(looking-at "(\\s-*package!\\s-*\\(\\(\\sw\\|\\s_\\)+\\)\\s-*"))
|
2021-07-28 11:51:19 -04:00
|
|
|
(let ((pkg (match-string 1)))
|
|
|
|
(set-text-properties 0 (length pkg) nil pkg)
|
|
|
|
`(package . ,pkg))))))
|
2021-06-30 19:24:27 +03:00
|
|
|
|
2021-05-07 21:27:14 +03:00
|
|
|
;;;###autoload
|
2021-07-09 20:28:40 +03:00
|
|
|
(defun +vertico/embark-export-write ()
|
2021-07-09 20:16:11 +03:00
|
|
|
"Export the current vertico results to a writable buffer if possible.
|
2021-05-21 13:19:52 +03:00
|
|
|
|
|
|
|
Supports exporting consult-grep to wgrep, file to wdeired, and consult-location to occur-edit"
|
2021-05-07 21:27:14 +03:00
|
|
|
(interactive)
|
2021-07-25 19:05:52 +03:00
|
|
|
(require 'embark)
|
2021-05-07 21:27:14 +03:00
|
|
|
(require 'wgrep)
|
|
|
|
(pcase-let ((`(,type . ,candidates)
|
|
|
|
(run-hook-with-args-until-success 'embark-candidate-collectors)))
|
2021-05-21 13:19:52 +03:00
|
|
|
(pcase type
|
|
|
|
('consult-grep (let ((embark-after-export-hook #'wgrep-change-to-wgrep-mode))
|
|
|
|
(embark-export)))
|
|
|
|
('file (let ((embark-after-export-hook #'wdired-change-to-wdired-mode))
|
|
|
|
(embark-export)))
|
|
|
|
('consult-location (let ((embark-after-export-hook #'occur-edit-mode))
|
|
|
|
(embark-export)))
|
|
|
|
(x (user-error "embark category %S doesn't support writable export" x)))))
|
2021-05-22 01:21:10 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
2021-07-09 20:28:40 +03:00
|
|
|
(defun +vertico/embark-preview ()
|
2021-07-09 20:16:11 +03:00
|
|
|
"Previews candidate in vertico buffer, unless it's a consult command"
|
2021-05-22 01:21:10 +03:00
|
|
|
(interactive)
|
2021-05-22 23:22:17 +03:00
|
|
|
(unless (bound-and-true-p consult--preview-function)
|
|
|
|
(save-selected-window
|
|
|
|
(let ((embark-quit-after-action nil))
|
2021-07-31 10:50:00 +03:00
|
|
|
(embark-dwim)))))
|
2021-05-22 01:21:10 +03:00
|
|
|
|
2021-07-28 12:53:02 -04:00
|
|
|
(defvar +vertico/find-file-in--history nil)
|
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico/find-file-in (&optional dir initial)
|
|
|
|
"Jump to file under DIR (recursive).
|
|
|
|
If INITIAL is non-nil, use as initial input."
|
|
|
|
(interactive)
|
2021-07-31 10:48:00 +03:00
|
|
|
(require 'consult)
|
2021-07-28 12:53:02 -04:00
|
|
|
(let* ((default-directory (or dir default-directory))
|
|
|
|
(prompt-dir (consult--directory-prompt "Find" default-directory))
|
2021-08-10 17:48:00 +03:00
|
|
|
(cmd (split-string-and-unquote +vertico-consult-fd-args " ")))
|
2021-07-28 12:53:02 -04:00
|
|
|
(find-file
|
|
|
|
(consult--read
|
|
|
|
(split-string (cdr (apply #'doom-call-process cmd)) "\n" t)
|
|
|
|
:prompt default-directory
|
|
|
|
:sort nil
|
|
|
|
:initial (if initial (shell-quote-argument initial))
|
|
|
|
:add-history (thing-at-point 'filename)
|
2021-07-31 10:48:00 +03:00
|
|
|
:category 'file
|
2021-07-28 12:53:02 -04:00
|
|
|
:history '(:input +vertico/find-file-in--history)))))
|
2021-07-28 13:02:46 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico/jump-list (jump)
|
|
|
|
"Go to an entry in evil's (or better-jumper's) jumplist."
|
|
|
|
(interactive
|
|
|
|
(let (buffers)
|
2021-08-21 21:09:39 +03:00
|
|
|
(require 'consult)
|
2021-07-28 13:02:46 -04:00
|
|
|
(unwind-protect
|
|
|
|
(list
|
|
|
|
(consult--read
|
|
|
|
;; REVIEW Refactor me
|
|
|
|
(nreverse
|
|
|
|
(delete-dups
|
|
|
|
(delq
|
2021-08-21 03:29:13 -04:00
|
|
|
nil (mapcar
|
|
|
|
(lambda (mark)
|
|
|
|
(when mark
|
|
|
|
(cl-destructuring-bind (path pt _id) mark
|
|
|
|
(let* ((visiting (find-buffer-visiting path))
|
|
|
|
(buf (or visiting (find-file-noselect path t)))
|
|
|
|
(dir default-directory))
|
|
|
|
(unless visiting
|
|
|
|
(push buf buffers))
|
|
|
|
(with-current-buffer buf
|
|
|
|
(goto-char pt)
|
|
|
|
(font-lock-fontify-region
|
|
|
|
(line-beginning-position) (line-end-position))
|
|
|
|
(format "%s:%d: %s"
|
|
|
|
(car (cl-sort (list (abbreviate-file-name (buffer-file-name buf))
|
|
|
|
(file-relative-name (buffer-file-name buf) dir))
|
|
|
|
#'< :key #'length))
|
|
|
|
(line-number-at-pos)
|
|
|
|
(string-trim-right (or (thing-at-point 'line) ""))))))))
|
|
|
|
(cddr (better-jumper-jump-list-struct-ring
|
|
|
|
(better-jumper-get-jumps (better-jumper--get-current-context))))))))
|
2021-07-28 13:02:46 -04:00
|
|
|
:prompt "jumplist: "
|
|
|
|
:sort nil
|
|
|
|
:require-match t
|
|
|
|
:category 'jump-list))
|
|
|
|
(mapc #'kill-buffer buffers))))
|
2021-08-21 03:29:13 -04:00
|
|
|
(if (not (string-match "^\\([^:]+\\):\\([0-9]+\\): " jump))
|
|
|
|
(user-error "No match")
|
|
|
|
(let ((file (match-string-no-properties 1 jump))
|
|
|
|
(line (match-string-no-properties 2 jump)))
|
|
|
|
(find-file file)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(forward-line (string-to-number line)))))
|
2021-07-31 10:41:00 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
2021-08-10 17:52:00 +03:00
|
|
|
(defun +vertico-embark-which-key-indicator ()
|
2021-07-31 10:41:00 +03:00
|
|
|
"An embark indicator that displays keymaps using which-key.
|
|
|
|
The which-key help message will show the type and value of the
|
|
|
|
current target followed by an ellipsis if there are further
|
|
|
|
targets."
|
|
|
|
(lambda (&optional keymap targets prefix)
|
|
|
|
(if (null keymap)
|
|
|
|
(kill-buffer which-key--buffer)
|
|
|
|
(which-key--show-keymap
|
|
|
|
(if (eq (caar targets) 'embark-become)
|
|
|
|
"Become"
|
|
|
|
(format "Act on %s '%s'%s"
|
2021-09-17 17:24:22 +03:00
|
|
|
(plist-get (car targets) :type)
|
|
|
|
(embark--truncate-target (plist-get (car targets) :target))
|
2021-07-31 10:41:00 +03:00
|
|
|
(if (cdr targets) "…" "")))
|
|
|
|
(if prefix (lookup-key keymap prefix) keymap)
|
|
|
|
nil nil t))))
|
2021-07-31 10:43:00 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico/crm-select ()
|
|
|
|
"Enter candidate in `consult-completing-read-multiple'"
|
|
|
|
(interactive)
|
|
|
|
(let ((idx vertico--index))
|
|
|
|
(unless (get-text-property 0 'consult--crm-selected (nth vertico--index vertico--candidates))
|
|
|
|
(setq idx (1+ idx)))
|
2021-08-17 22:06:22 +03:00
|
|
|
(run-at-time 0 nil (cmd! (vertico--goto idx) (vertico--exhibit))))
|
2021-07-31 10:43:00 +03:00
|
|
|
(vertico-exit))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico/crm-exit ()
|
|
|
|
"Enter candidate in `consult-completing-read-multiple'"
|
|
|
|
(interactive)
|
|
|
|
(run-at-time 0 nil #'vertico-exit)
|
|
|
|
(vertico-exit))
|
2021-08-10 17:48:00 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico--consult--fd-builder (input)
|
|
|
|
(pcase-let* ((cmd (split-string-and-unquote +vertico-consult-fd-args))
|
|
|
|
(`(,arg . ,opts) (consult--command-split input))
|
|
|
|
(`(,re . ,hl) (funcall consult--regexp-compiler
|
|
|
|
arg 'extended)))
|
|
|
|
(when re
|
|
|
|
(list :command (append cmd
|
|
|
|
(list (consult--join-regexps re 'extended))
|
|
|
|
opts)
|
|
|
|
:highlight hl))))
|
|
|
|
|
|
|
|
(autoload #'consult--directory-prompt "consult")
|
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico/consult-fd (&optional dir initial)
|
|
|
|
(interactive "P")
|
|
|
|
(if doom-projectile-fd-binary
|
2021-08-17 22:06:22 +03:00
|
|
|
(let* ((prompt-dir (consult--directory-prompt "Fd" dir))
|
|
|
|
(default-directory (cdr prompt-dir)))
|
|
|
|
(find-file (consult--find (car prompt-dir) #'+vertico--consult--fd-builder initial)))
|
|
|
|
(consult-find dir initial)))
|
2021-08-10 17:50:00 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico-embark-vertico-indicator ()
|
|
|
|
"An embark indicator that colorizes the vertico candidate differently on act"
|
|
|
|
(let ((fr face-remapping-alist))
|
|
|
|
(lambda (&optional keymap _targets prefix)
|
|
|
|
(when (bound-and-true-p vertico--input)
|
|
|
|
(setq-local face-remapping-alist
|
|
|
|
(if keymap
|
|
|
|
(cons '(vertico-current . embark-target) fr)
|
|
|
|
fr))))))
|
2021-08-13 00:26:10 +03:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico-basic-remote-try-completion (string table pred point)
|
|
|
|
(and (vertico--remote-p string)
|
|
|
|
(completion-basic-try-completion string table pred point)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +vertico-basic-remote-all-completions (string table pred point)
|
|
|
|
(and (vertico--remote-p string)
|
|
|
|
(completion-basic-all-completions string table pred point)))
|