2021-08-28 13:41:35 +00:00
|
|
|
;;; init-minibuffer.el --- Minibuffer Completion Configuration File -*- lexical-binding: t -*-
|
|
|
|
;;; Commentary:
|
|
|
|
;; Config for completion etc in the minibuffer (vertico, embark, consult, etc)
|
|
|
|
;; Most of it is taken from the READMEs and wikis of those packages.
|
|
|
|
;; Relies on orderless config in init-completion.el
|
|
|
|
;;; Code:
|
|
|
|
|
2022-10-14 11:29:07 +00:00
|
|
|
(use-package vertico
|
|
|
|
:straight (vertico :files (:defaults "extensions/*")
|
2022-10-14 15:37:37 +00:00
|
|
|
:includes (vertico-directory vertico-repeat vertico-indexed vertico-quick))
|
2022-10-14 11:29:07 +00:00
|
|
|
:hook (emacs-startup . vertico-mode)
|
|
|
|
:custom (vertico-cycle t)
|
|
|
|
:config
|
2021-08-28 13:41:35 +00:00
|
|
|
;; Do not allow the cursor in the minibuffer prompt
|
|
|
|
(setq minibuffer-prompt-properties
|
|
|
|
'(read-only t cursor-intangible t face minibuffer-prompt))
|
|
|
|
(add-hook 'minibuffer-setup-hook #'cursor-intangible-mode)
|
|
|
|
(setq enable-recursive-minibuffers t)
|
2022-06-10 12:32:37 +00:00
|
|
|
(minibuffer-depth-indicate-mode t)
|
|
|
|
|
|
|
|
;; Add prompt indicator to `completing-read-multiple'.
|
|
|
|
;; We display [CRM<separator>], e.g., [CRM,] if the separator is a comma.
|
|
|
|
(defun crm-indicator (args)
|
|
|
|
(cons (format "[CRM%s] %s"
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"\\`\\[.*?]\\*\\|\\[.*?]\\*\\'" ""
|
|
|
|
crm-separator)
|
|
|
|
(car args))
|
|
|
|
(cdr args)))
|
|
|
|
(advice-add #'completing-read-multiple :filter-args #'crm-indicator)
|
|
|
|
|
|
|
|
;; Emacs 28: Hide commands in M-x which do not work in the current mode.
|
|
|
|
;; Vertico commands are hidden in normal buffers.
|
|
|
|
(setq read-extended-command-predicate
|
2022-10-14 11:29:07 +00:00
|
|
|
#'command-completion-default-include-p)
|
2021-08-28 13:41:35 +00:00
|
|
|
|
|
|
|
(advice-add #'vertico--format-candidate :around
|
2021-09-10 18:55:18 +00:00
|
|
|
(lambda (orig cand prefix suffix index start)
|
|
|
|
(setq cand (funcall orig cand prefix suffix index start))
|
2021-08-28 13:41:35 +00:00
|
|
|
(concat
|
|
|
|
(if (= vertico--index index)
|
|
|
|
(propertize "» " 'face 'vertico-current)
|
|
|
|
" ")
|
|
|
|
cand)))
|
2022-10-17 15:05:08 +00:00
|
|
|
|
2021-09-08 05:51:17 +00:00
|
|
|
(defun define-vertico-key (key &rest defs)
|
|
|
|
"Define KEY conditionally in the vertico keymap.
|
|
|
|
DEFS is a plist associating completion categories to commands."
|
|
|
|
(let ((default-command (lookup-key vertico-map (kbd key))))
|
|
|
|
(define-key vertico-map (kbd key)
|
|
|
|
(list 'menu-item nil defs :filter
|
|
|
|
(lambda (d)
|
|
|
|
(or (plist-get d (completion-metadata-get
|
|
|
|
(completion-metadata (minibuffer-contents)
|
|
|
|
minibuffer-completion-table
|
|
|
|
minibuffer-completion-predicate)
|
|
|
|
'category))
|
|
|
|
default-command))))))
|
|
|
|
|
2021-08-28 13:41:35 +00:00
|
|
|
(defun down-from-outside ()
|
|
|
|
"Move to next candidate in minibuffer, even when minibuffer isn't selected."
|
|
|
|
(interactive)
|
|
|
|
(with-selected-window (active-minibuffer-window)
|
|
|
|
(execute-kbd-macro [down])))
|
|
|
|
|
|
|
|
(defun up-from-outside ()
|
|
|
|
"Move to previous candidate in minibuffer, even when minibuffer isn't selected."
|
|
|
|
(interactive)
|
|
|
|
(with-selected-window (active-minibuffer-window)
|
|
|
|
(execute-kbd-macro [up])))
|
|
|
|
|
|
|
|
(defun preview-from-outside ()
|
|
|
|
"Preview the selected candidate, even when minibuffer isn't selected."
|
|
|
|
(interactive)
|
|
|
|
(with-selected-window (active-minibuffer-window)
|
|
|
|
(execute-kbd-macro (kbd "M-."))))
|
|
|
|
|
|
|
|
(defun to-and-fro-minibuffer ()
|
|
|
|
"Go back and forth between minibuffer and other window."
|
|
|
|
(interactive)
|
|
|
|
(if (window-minibuffer-p (selected-window))
|
|
|
|
(select-window (minibuffer-selected-window))
|
|
|
|
(select-window (active-minibuffer-window))))
|
|
|
|
|
2022-05-31 08:30:34 +00:00
|
|
|
(defun minibuffer-really-quit ()
|
|
|
|
"Quit minibuffer session, even if it is not the selected window."
|
|
|
|
(interactive)
|
|
|
|
(with-selected-window (active-minibuffer-window)
|
|
|
|
(minibuffer-keyboard-quit)))
|
|
|
|
|
2021-08-28 13:41:35 +00:00
|
|
|
:bind (("C-M-<" . up-from-outside)
|
|
|
|
("C-M->" . down-from-outside)
|
|
|
|
("C-M-+" . preview-from-outside)
|
|
|
|
("M-X" . to-and-fro-minibuffer)
|
2022-06-10 12:33:12 +00:00
|
|
|
("C-M-S-g" . minibuffer-really-quit)
|
|
|
|
(:map vertico-map ("M-RET" . minibuffer-force-complete-and-exit))))
|
2021-08-28 13:41:35 +00:00
|
|
|
|
2022-10-10 15:28:16 +00:00
|
|
|
(use-feature vertico-directory
|
2021-09-08 05:51:17 +00:00
|
|
|
:after vertico
|
2022-10-14 11:29:07 +00:00
|
|
|
:config
|
2021-08-28 13:41:35 +00:00
|
|
|
(defvar switching-project nil)
|
2021-09-08 05:51:17 +00:00
|
|
|
(defun vertico-directory-enter-or-select-project ()
|
2022-05-17 14:51:57 +00:00
|
|
|
"vertico-directory-enter wrapper that plays nicely with selecting new projects."
|
2021-08-28 13:41:35 +00:00
|
|
|
(interactive)
|
2021-09-08 05:51:17 +00:00
|
|
|
;; When selecting a project, use this to return, instead of entering the directory
|
2021-08-28 13:41:35 +00:00
|
|
|
(if switching-project
|
|
|
|
(vertico-exit)
|
|
|
|
(vertico-directory-enter)))
|
2021-10-12 05:38:27 +00:00
|
|
|
(defun vertico-directory-slash ()
|
|
|
|
(interactive)
|
|
|
|
(if (and (>= vertico--index 0)
|
|
|
|
(string-suffix-p "/" (vertico--candidate))
|
2022-07-19 15:04:29 +00:00
|
|
|
(eq 'file (vertico--metadata-get 'category)))
|
2021-10-12 05:38:27 +00:00
|
|
|
(vertico-insert)
|
|
|
|
(insert "/")))
|
|
|
|
(defun vertico-directory-home ()
|
|
|
|
(interactive)
|
|
|
|
(if (and (string-suffix-p "/" (vertico--candidate))
|
2022-07-19 15:04:29 +00:00
|
|
|
(eq 'file (vertico--metadata-get 'category)))
|
2021-10-12 05:38:27 +00:00
|
|
|
(insert "~/")
|
|
|
|
(insert "~")))
|
2021-08-28 13:41:35 +00:00
|
|
|
(defun read-project (orig &rest args)
|
|
|
|
(let ((switching-project t))
|
|
|
|
(apply orig args)))
|
2021-09-01 15:28:44 +00:00
|
|
|
(advice-add 'project-prompt-project-dir :around
|
2021-08-28 13:41:35 +00:00
|
|
|
'read-project)
|
2021-09-08 05:51:17 +00:00
|
|
|
(define-vertico-key "/"
|
2021-09-09 15:45:49 +00:00
|
|
|
'file #'vertico-directory-slash
|
|
|
|
'project-file #'vertico-directory-slash)
|
2021-09-08 05:51:17 +00:00
|
|
|
(define-vertico-key "RET"
|
|
|
|
'file #'vertico-directory-enter-or-select-project
|
|
|
|
'project-file #'vertico-directory-enter)
|
|
|
|
(define-vertico-key "~"
|
|
|
|
'file #'vertico-directory-home)
|
|
|
|
(define-vertico-key "DEL"
|
|
|
|
'file #'vertico-directory-delete-char
|
|
|
|
'project-file #'vertico-directory-delete-char)
|
|
|
|
(define-vertico-key "M-DEL"
|
|
|
|
'file #'vertico-directory-delete-word
|
|
|
|
'project-file #'vertico-directory-delete-word)
|
2021-10-12 05:38:27 +00:00
|
|
|
:commands (vertico-directory-enter vertico-directory-delete-word vertico-directory-delete-char)
|
2021-08-28 13:41:35 +00:00
|
|
|
;; Tidy shadowed file names
|
|
|
|
:hook (rfn-eshadow-update-overlay . vertico-directory-tidy))
|
|
|
|
|
2022-10-10 15:28:16 +00:00
|
|
|
(use-feature vertico-repeat
|
2022-10-17 15:10:46 +00:00
|
|
|
:bind
|
|
|
|
("C-\\" . vertico-repeat)
|
|
|
|
("C-|" . vertico-repeat-select)
|
2022-10-14 15:37:37 +00:00
|
|
|
:hook (minibuffer-setup . vertico-repeat-save)
|
|
|
|
:config
|
|
|
|
(add-to-list 'savehist-additional-variables 'vertico-repeat-history))
|
|
|
|
|
2022-10-17 15:05:08 +00:00
|
|
|
(use-extension vertico vertico-indexed
|
2022-11-02 21:07:56 +00:00
|
|
|
:config
|
2022-11-03 16:44:41 +00:00
|
|
|
(defmacro define-choose (n)
|
2022-11-02 21:07:56 +00:00
|
|
|
`(defun ,(intern (format "vertico-indexed-choose-%s" n)) ()
|
2022-11-03 16:44:41 +00:00
|
|
|
,(format "Exit minibuffer with candidate %s." n)
|
2022-11-02 21:07:56 +00:00
|
|
|
(interactive)
|
|
|
|
(let ((current-prefix-arg ,n))
|
|
|
|
(funcall-interactively 'vertico-exit))))
|
2022-11-03 16:44:41 +00:00
|
|
|
(defmacro define-insert (n)
|
2022-11-02 21:18:35 +00:00
|
|
|
`(defun ,(intern (format "vertico-indexed-insert-%s" n)) ()
|
2022-11-03 16:44:41 +00:00
|
|
|
,(format "Insert candidate %s in minibuffer." n)
|
2022-11-02 21:18:35 +00:00
|
|
|
(interactive)
|
|
|
|
(let ((current-prefix-arg ,n))
|
|
|
|
(funcall-interactively 'vertico-insert))))
|
2022-11-03 16:44:41 +00:00
|
|
|
(dotimes (n 10)
|
|
|
|
(eval `(define-choose ,n))
|
|
|
|
(eval `(define-insert ,n))
|
|
|
|
(define-key vertico-map (kbd (format "C-%s" n)) (intern (format "vertico-indexed-choose-%s" n)))
|
|
|
|
(define-key vertico-map (kbd (format "M-%s" n)) (intern (format "vertico-indexed-insert-%s" n))))
|
2022-11-02 21:07:56 +00:00
|
|
|
(vertico-indexed-mode 1))
|
2022-10-14 15:37:37 +00:00
|
|
|
|
2022-10-17 15:05:08 +00:00
|
|
|
(use-extension vertico vertico-quick
|
2022-10-14 15:37:37 +00:00
|
|
|
:bind (:map vertico-map
|
2022-10-22 14:58:00 +00:00
|
|
|
("M-;" . vertico-quick-insert)
|
|
|
|
("M-'" . vertico-quick-exit)))
|
2021-08-28 13:41:35 +00:00
|
|
|
|
|
|
|
(use-package consult
|
|
|
|
:bind (;; C-c bindings (mode-specific-map)
|
|
|
|
("C-c h" . consult-history)
|
|
|
|
("C-c m" . consult-mode-command)
|
|
|
|
("C-c b" . consult-bookmark)
|
|
|
|
("C-c k" . consult-kmacro)
|
|
|
|
;; C-x bindings (ctl-x-map)
|
2022-10-17 15:22:01 +00:00
|
|
|
("C-x M-:" . consult-complex-command) ;; orig. repeat-complex-command
|
|
|
|
("C-x b" . consult-buffer) ;; orig. switch-to-buffer
|
|
|
|
("C-x B" . consult-buffer-no-preview) ;; orig. switch-to-buffer
|
2021-08-28 13:41:35 +00:00
|
|
|
("C-x 4 b" . consult-buffer-other-window) ;; orig. switch-to-buffer-other-window
|
2022-10-17 15:22:01 +00:00
|
|
|
("C-x 5 b" . consult-buffer-other-frame) ;; orig. switch-to-buffer-other-frame
|
2021-08-28 13:41:35 +00:00
|
|
|
;; Custom M-# bindings for fast register access
|
|
|
|
("M-#" . consult-register-load)
|
2022-10-17 15:22:01 +00:00
|
|
|
("M-'" . consult-register-store) ;; orig. abbrev-prefix-mark (unrelated)
|
2021-08-28 13:41:35 +00:00
|
|
|
("C-M-#" . consult-register)
|
|
|
|
;; Other custom bindings
|
|
|
|
("C-S-s" . consult-line)
|
|
|
|
("M-*" . consult-line-symbol-at-point)
|
|
|
|
("C-c f" . consult-recent-file)
|
|
|
|
("C-c r" . consult-ripgrep)
|
2022-05-17 14:54:08 +00:00
|
|
|
;; TODO find an alternative to C-c c?
|
2022-10-21 19:16:57 +00:00
|
|
|
("C-c c r" . consult-ripgrep-no-preview)
|
2022-05-17 14:54:08 +00:00
|
|
|
("C-c c s" . consult-ripgrep-case-sensitive)
|
|
|
|
("C-c c z" . consult-z-ripgrep)
|
2022-04-28 05:59:12 +00:00
|
|
|
("C-c C-*" . consult-ripgrep-symbol-at-point)
|
2022-09-16 11:36:33 +00:00
|
|
|
("C-c C-^" . consult-ripgrep-parent)
|
2022-10-17 15:22:01 +00:00
|
|
|
("M-y" . consult-yank-pop) ;; orig. yank-pop
|
|
|
|
("<help> a" . consult-apropos) ;; orig. apropos-command
|
2021-08-28 13:41:35 +00:00
|
|
|
;; M-g bindings (goto-map)
|
|
|
|
("M-g e" . consult-compile-error)
|
|
|
|
("M-g f" . consult-flycheck)
|
2022-10-17 15:22:01 +00:00
|
|
|
("M-g g" . consult-goto-line) ;; orig. goto-line
|
|
|
|
("M-g M-g" . consult-goto-line) ;; orig. goto-line
|
|
|
|
("M-g o" . consult-outline) ;; Alternative: consult-org-heading
|
2021-08-28 13:41:35 +00:00
|
|
|
("M-g m" . consult-mark)
|
|
|
|
("M-g k" . consult-global-mark)
|
|
|
|
("M-g i" . consult-imenu)
|
2021-09-14 19:09:14 +00:00
|
|
|
("M-g I" . consult-imenu-multi)
|
2022-10-24 15:36:29 +00:00
|
|
|
(:map isearch-mode-map
|
|
|
|
("M-e" . consult-isearch-history) ;; orig. isearch-edit-string
|
|
|
|
("M-s e" . consult-isearch-history) ;; orig. isearch-edit-string
|
|
|
|
("M-s l" . consult-line)) ;; needed by consult-line to detect isearch
|
|
|
|
(:map search-map
|
|
|
|
("f" . consult-fd)
|
|
|
|
("F" . consult-find)
|
|
|
|
("M-f" . consult-locate)
|
|
|
|
("g" . consult-grep)
|
|
|
|
("G" . consult-git-grep)
|
|
|
|
("r" . consult-ripgrep)
|
|
|
|
("R" . consult-ripgrep) ;; can't use r in isearch-mode, so add R too
|
|
|
|
("M-r" . consult-ripgrep-unrestricted)
|
|
|
|
("*" . consult-ripgrep-symbol-at-point)
|
|
|
|
("z" . consult-z-ripgrep)
|
|
|
|
("^" . consult-ripgrep-parent)
|
|
|
|
("l" . consult-line)
|
|
|
|
("L" . consult-line-multi)
|
|
|
|
("m" . consult-multi-occur)
|
|
|
|
("k" . consult-keep-lines)
|
|
|
|
("u" . consult-focus-lines)
|
|
|
|
("e" . consult-isearch))
|
2021-09-07 18:23:56 +00:00
|
|
|
(:map vertico-map
|
|
|
|
;; These are used for previewing with some consult commands (see consult-customize call below)
|
|
|
|
("C-S-p" . vertico-previous)
|
|
|
|
("C-S-n" . vertico-next)
|
|
|
|
;; Toggle preview on/off without changing preview-key
|
|
|
|
("M-P" . consult-toggle-preview)))
|
2021-08-28 13:41:35 +00:00
|
|
|
|
2022-10-14 11:29:07 +00:00
|
|
|
:config
|
2021-08-28 13:41:35 +00:00
|
|
|
|
|
|
|
;; Optionally configure the register formatting. This improves the register
|
|
|
|
;; preview for `consult-register', `consult-register-load',
|
|
|
|
;; `consult-register-store' and the Emacs built-ins.
|
|
|
|
(setq register-preview-delay 0
|
|
|
|
register-preview-function #'consult-register-format)
|
|
|
|
|
|
|
|
;; Optionally tweak the register preview window.
|
|
|
|
;; This adds thin lines, sorting and hides the mode line of the window.
|
|
|
|
(advice-add #'register-preview :override #'consult-register-window)
|
|
|
|
|
|
|
|
;; Use Consult to select xref locations with preview
|
|
|
|
(setq xref-show-xrefs-function #'consult-xref
|
|
|
|
xref-show-definitions-function #'consult-xref)
|
|
|
|
|
2022-10-17 15:19:32 +00:00
|
|
|
(add-to-list 'consult-mode-histories '(cider-repl-mode cider-repl-input-history))
|
|
|
|
|
2021-08-28 13:41:35 +00:00
|
|
|
(defun consult-ripgrep-symbol-at-point (&optional dir initial)
|
|
|
|
(interactive
|
|
|
|
(list prefix-arg (when-let ((s (symbol-at-point)))
|
|
|
|
(symbol-name s))))
|
|
|
|
(consult-ripgrep dir initial))
|
2022-10-21 19:16:57 +00:00
|
|
|
(defun consult-ripgrep-no-preview (&optional dir initial)
|
2021-08-28 13:41:35 +00:00
|
|
|
(interactive "P")
|
|
|
|
(consult-ripgrep dir initial))
|
|
|
|
(defun consult-ripgrep-unrestricted (&optional dir initial)
|
|
|
|
(interactive "P")
|
|
|
|
(let ((consult-ripgrep-args (replace-regexp-in-string "\\." "-uu ." consult-ripgrep-args)))
|
|
|
|
(consult-ripgrep dir initial)))
|
|
|
|
(defun consult-z-ripgrep (&optional dir initial)
|
|
|
|
(interactive "P")
|
|
|
|
(let ((consult-ripgrep-args (replace-regexp-in-string "\\." "-z ." consult-ripgrep-args)))
|
|
|
|
(consult-ripgrep dir initial)))
|
2022-05-17 14:54:08 +00:00
|
|
|
(defun consult-ripgrep-case-sensitive (&optional dir initial)
|
|
|
|
(interactive "P")
|
|
|
|
(let ((consult-ripgrep-args (replace-regexp-in-string "\\." "-s ." consult-ripgrep-args)))
|
|
|
|
(consult-ripgrep dir initial)))
|
2021-08-28 13:41:35 +00:00
|
|
|
(defun consult-buffer-no-preview ()
|
|
|
|
(interactive)
|
|
|
|
(consult-buffer))
|
|
|
|
(defun consult-line-symbol-at-point ()
|
|
|
|
(interactive)
|
|
|
|
(consult-line (thing-at-point 'symbol)))
|
2022-10-05 15:41:12 +00:00
|
|
|
(defun consult-ripgrep-parent (&optional initial)
|
2022-09-16 11:36:33 +00:00
|
|
|
(interactive "P")
|
|
|
|
(consult-ripgrep (file-name-directory (directory-file-name (persp-current-project-root))) initial))
|
2021-09-01 09:28:23 +00:00
|
|
|
(defvar consult--fd-command nil)
|
|
|
|
(defun consult--fd-builder (input)
|
|
|
|
(unless consult--fd-command
|
|
|
|
(setq consult--fd-command
|
|
|
|
(if (eq 0 (call-process-shell-command "fdfind"))
|
|
|
|
"fdfind"
|
|
|
|
"fd")))
|
|
|
|
(pcase-let* ((`(,arg . ,opts) (consult--command-split input))
|
|
|
|
(`(,re . ,hl) (funcall consult--regexp-compiler
|
2022-05-17 14:54:46 +00:00
|
|
|
arg 'extended t)))
|
2021-09-01 09:28:23 +00:00
|
|
|
(when re
|
|
|
|
(list :command (append
|
|
|
|
(list consult--fd-command
|
|
|
|
"--color=never" "--full-path"
|
|
|
|
(consult--join-regexps re 'extended))
|
|
|
|
opts)
|
|
|
|
:highlight hl))))
|
|
|
|
|
|
|
|
(defun consult-fd (&optional dir initial)
|
|
|
|
(interactive "P")
|
|
|
|
(let* ((prompt-dir (consult--directory-prompt "Fd" dir))
|
|
|
|
(default-directory (cdr prompt-dir)))
|
|
|
|
(find-file (consult--find (car prompt-dir) #'consult--fd-builder initial))))
|
2021-08-28 13:41:35 +00:00
|
|
|
|
|
|
|
(consult-customize
|
|
|
|
consult-theme
|
|
|
|
:preview-key '(:debounce 0.2 any)
|
2022-10-21 15:39:56 +00:00
|
|
|
;; For these commands we can use C-S/C-P to scroll and preview, or M-. to preview
|
2022-10-21 19:16:57 +00:00
|
|
|
consult-git-grep consult-grep
|
|
|
|
consult-ripgrep-parent consult-ripgrep-no-preview
|
|
|
|
consult-ripgrep-unrestricted consult-z-ripgrep
|
2021-08-28 13:41:35 +00:00
|
|
|
consult-bookmark consult-recent-file consult-xref consult-buffer-no-preview
|
2022-02-28 11:39:13 +00:00
|
|
|
consult--source-recent-file consult--source-project-recent-file consult--source-bookmark
|
2021-09-07 18:23:56 +00:00
|
|
|
:preview-key (list (kbd "M-.") (kbd "C-S-n") (kbd "C-S-p")))
|
2021-08-28 13:41:35 +00:00
|
|
|
|
2021-09-09 15:49:51 +00:00
|
|
|
(defvar-local consult-toggle-preview-orig nil)
|
2021-09-07 18:23:56 +00:00
|
|
|
(defun consult-toggle-preview ()
|
|
|
|
"Command to enable/disable preview."
|
|
|
|
(interactive)
|
|
|
|
(if consult-toggle-preview-orig
|
|
|
|
(setq consult--preview-function consult-toggle-preview-orig
|
|
|
|
consult-toggle-preview-orig nil)
|
|
|
|
(setq consult-toggle-preview-orig consult--preview-function
|
|
|
|
consult--preview-function #'ignore)))
|
2021-08-28 13:41:35 +00:00
|
|
|
|
2021-09-07 18:23:56 +00:00
|
|
|
(setq consult-narrow-key "<")
|
2021-08-28 13:41:35 +00:00
|
|
|
|
2022-02-28 11:40:02 +00:00
|
|
|
(setq consult-project-function (lambda (_) (persp-current-project-root)))
|
2021-09-01 15:28:44 +00:00
|
|
|
|
2021-09-11 05:41:47 +00:00
|
|
|
;; Switches perspective if we select a buffer from another perspective, but note that previewing
|
|
|
|
;; a buffer adds it to the current perspective, so preview should be disabled before removing
|
|
|
|
;; perspective narrowing
|
|
|
|
(defun consult--persp-buffer-action (orig &rest args)
|
|
|
|
(when (not (cdr args)) ;; (cdr args) is norecord, which should distinguish preview/non-preview
|
|
|
|
(let ((buffer (window-normalize-buffer-to-switch-to (car args))))
|
|
|
|
(unless (persp-is-current-buffer buffer)
|
|
|
|
(let ((other-persp (persp-buffer-in-other-p buffer)))
|
|
|
|
(when (eq (car-safe other-persp) (selected-frame))
|
|
|
|
(persp-switch (cdr other-persp)))))))
|
|
|
|
(apply orig args))
|
|
|
|
(advice-add 'consult--buffer-action :around 'consult--persp-buffer-action)
|
2021-08-28 13:41:35 +00:00
|
|
|
|
|
|
|
(defvar consult-initial-narrow-config
|
2021-09-01 15:28:44 +00:00
|
|
|
'((consult-buffer . ?x)
|
2021-09-21 14:20:11 +00:00
|
|
|
(consult-buffer-no-preview . ?x)
|
2022-07-27 13:08:14 +00:00
|
|
|
(consult-buffer-other-window . ?x)
|
|
|
|
(consult-project-extra-find . ?f)))
|
2021-08-28 13:41:35 +00:00
|
|
|
;; Add initial narrowing hook
|
|
|
|
(defun consult-initial-narrow ()
|
|
|
|
(when-let (key (alist-get this-command consult-initial-narrow-config))
|
|
|
|
(setq unread-command-events (append unread-command-events (list key 32)))))
|
|
|
|
(add-hook 'minibuffer-setup-hook #'consult-initial-narrow)
|
|
|
|
|
2021-09-01 15:28:44 +00:00
|
|
|
(defvar consult--source-perspective-buffer
|
|
|
|
`(:name "Perspective Buffer"
|
|
|
|
:narrow (?x . "Perspective")
|
|
|
|
:hidden t
|
|
|
|
:category buffer
|
|
|
|
:face consult-buffer
|
|
|
|
:history buffer-name-history
|
|
|
|
:state ,#'consult--buffer-state
|
|
|
|
:enabled ,(lambda () persp-mode)
|
|
|
|
:items
|
|
|
|
,(lambda ()
|
|
|
|
(consult--buffer-query :sort 'visibility
|
|
|
|
:predicate #'persp-is-current-buffer
|
|
|
|
:as #'buffer-name)))
|
|
|
|
"Perspective buffer candidate source for `consult-buffer'.")
|
|
|
|
(add-to-list 'consult-buffer-sources 'consult--source-perspective-buffer t)
|
|
|
|
|
|
|
|
;; Copy of consult--source-project-file to use with perspective narrowing (identical except for narrowing key)
|
2021-09-03 06:35:31 +00:00
|
|
|
;; Put before consult--source-project-file so we get recentf behaviour here
|
2021-09-01 15:28:44 +00:00
|
|
|
(defvar consult--source-perspective-files
|
2021-12-22 13:00:48 +00:00
|
|
|
(plist-put (plist-put (copy-sequence consult--source-project-recent-file)
|
2021-09-07 18:24:48 +00:00
|
|
|
:name "Project File")
|
|
|
|
:narrow '(?x . "Perspective")))
|
2021-09-01 15:28:44 +00:00
|
|
|
(add-to-list 'consult-buffer-sources 'consult--source-perspective-files t)
|
|
|
|
|
|
|
|
;; Versions of consult--source-project-buffer and consult--source-project-file for use by consult-project-buffer
|
2021-09-17 15:37:43 +00:00
|
|
|
;; They allow narrowing with b, f and a (instead of p)
|
|
|
|
;; f is the recentf version provided by consult
|
|
|
|
;; a is an "all files" version based on fd (respecting .gitignore, hidden by default)
|
2021-09-01 15:28:44 +00:00
|
|
|
(defvar consult--project-source-project-buffer
|
|
|
|
(plist-put (plist-put (copy-sequence consult--source-project-buffer)
|
|
|
|
:hidden nil)
|
2021-09-07 18:24:48 +00:00
|
|
|
:narrow '(?b . "Buffer")))
|
2021-09-17 15:37:43 +00:00
|
|
|
(defvar consult--project-source-project-file-recentf
|
2021-12-22 13:00:48 +00:00
|
|
|
(plist-put (plist-put (copy-sequence consult--source-project-recent-file)
|
2021-09-17 15:37:43 +00:00
|
|
|
:hidden nil)
|
|
|
|
:narrow '(?f . "File (Recentf)")))
|
|
|
|
(defvar consult--project-source-project-file-all
|
2021-12-22 13:00:48 +00:00
|
|
|
(plist-put (plist-put (copy-sequence consult--source-project-recent-file)
|
2021-09-17 15:37:43 +00:00
|
|
|
:narrow '(?a . "File (All)"))
|
2021-09-06 12:41:50 +00:00
|
|
|
:items '(lambda ()
|
2021-09-17 15:37:43 +00:00
|
|
|
(when (eq 0 (call-process-shell-command "fd"))
|
|
|
|
(when-let (root (consult--project-root))
|
|
|
|
(let ((len (length root))
|
|
|
|
(inv-root (propertize root 'invisible t)))
|
|
|
|
(mapcar (lambda (x)
|
|
|
|
(concat inv-root (substring x len)))
|
|
|
|
(split-string
|
|
|
|
(shell-command-to-string
|
|
|
|
(format "fd --color never -t f -0 . %s" root))
|
|
|
|
"\0" t))))))))
|
2021-09-01 15:28:44 +00:00
|
|
|
|
|
|
|
(defun consult-project-buffer ()
|
|
|
|
(interactive)
|
|
|
|
(let ((consult-buffer-sources '(consult--project-source-project-buffer
|
2021-09-17 15:37:43 +00:00
|
|
|
consult--project-source-project-file-recentf
|
|
|
|
consult--project-source-project-file-all)))
|
2022-05-31 13:34:04 +00:00
|
|
|
(consult-buffer))))
|
2021-08-28 13:41:35 +00:00
|
|
|
|
|
|
|
(use-package consult-flycheck)
|
|
|
|
|
|
|
|
(use-package consult-lsp
|
|
|
|
:bind (:map lsp-mode-map
|
|
|
|
([remap xref-find-apropos] . consult-lsp-symbols)))
|
|
|
|
|
|
|
|
(use-package consult-dir
|
|
|
|
:bind (("C-x C-d" . consult-dir)
|
2021-09-01 15:17:23 +00:00
|
|
|
:map vertico-map
|
2021-08-28 13:41:35 +00:00
|
|
|
("C-x C-d" . consult-dir)
|
|
|
|
("C-x C-j" . consult-dir-jump-file)))
|
|
|
|
|
2022-05-17 14:55:35 +00:00
|
|
|
(use-package consult-ls-git)
|
2022-05-17 14:59:07 +00:00
|
|
|
(use-package consult-project-extra)
|
2022-05-17 14:55:35 +00:00
|
|
|
|
2021-08-28 13:41:35 +00:00
|
|
|
(use-package marginalia
|
2022-10-14 11:29:07 +00:00
|
|
|
:hook (emacs-startup . marginalia-mode)
|
|
|
|
:config
|
2022-07-27 13:08:52 +00:00
|
|
|
;; crux-recentf-find-file
|
|
|
|
(add-to-list 'marginalia-prompt-categories '("Choose recent file" . file)))
|
2021-08-28 13:41:35 +00:00
|
|
|
|
|
|
|
(use-package embark
|
|
|
|
:bind
|
2022-10-17 15:11:12 +00:00
|
|
|
(("C-." . embark-act)
|
2021-09-04 15:08:03 +00:00
|
|
|
("M-." . embark-dwim)
|
2021-08-28 13:41:35 +00:00
|
|
|
("C-c C-o" . embark-export)
|
|
|
|
("C-h b" . embark-bindings)
|
2021-09-01 09:34:46 +00:00
|
|
|
("C-h B" . describe-bindings)
|
|
|
|
(:map minibuffer-local-map
|
2022-11-02 21:14:56 +00:00
|
|
|
("M-." . embark-preview)
|
|
|
|
("C-," . embark-become))
|
2022-10-05 19:06:50 +00:00
|
|
|
(:map embark-become-file+buffer-map
|
|
|
|
("e" . consult-project-extra-find)
|
|
|
|
("E" . project-switch-consult-project-extra-find)))
|
2022-10-14 11:29:07 +00:00
|
|
|
:custom
|
|
|
|
(prefix-help-command 'embark-prefix-help-command)
|
2021-08-28 13:41:35 +00:00
|
|
|
:config
|
2022-11-02 16:40:40 +00:00
|
|
|
(defun embark-preview ()
|
2022-11-01 16:28:51 +00:00
|
|
|
"Previews candidate in vertico buffer, unless it's a consult command"
|
2021-09-01 09:34:46 +00:00
|
|
|
(interactive)
|
2022-11-01 16:28:51 +00:00
|
|
|
(unless (bound-and-true-p consult--preview-function)
|
2021-09-01 09:34:46 +00:00
|
|
|
(save-selected-window
|
2022-11-01 16:28:51 +00:00
|
|
|
(let ((embark-quit-after-action nil))
|
2021-09-06 20:00:01 +00:00
|
|
|
(embark-dwim)))))
|
2021-09-01 09:34:46 +00:00
|
|
|
|
2021-08-28 13:41:35 +00:00
|
|
|
;; Hide the mode line of the Embark live/completions buffers
|
|
|
|
(add-to-list 'display-buffer-alist
|
|
|
|
'("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
|
|
|
|
nil
|
|
|
|
(window-parameters (mode-line-format . none)))))
|
|
|
|
|
|
|
|
(use-package embark-consult
|
|
|
|
:after (embark consult)
|
2022-10-14 11:29:07 +00:00
|
|
|
;; demand, combined with after means that this will load after embark and consult
|
|
|
|
;; See https://github.com/oantolin/embark/commit/47daded610b245caf01a97d74c940aff91fe14e2#r46010972
|
|
|
|
:demand t
|
2022-10-05 19:06:50 +00:00
|
|
|
:bind
|
|
|
|
(:map embark-consult-async-search-map
|
|
|
|
("^" . consult-ripgrep-parent)
|
|
|
|
("R" . consult-ripgrep-unrestricted))
|
2021-08-28 13:41:35 +00:00
|
|
|
:hook
|
|
|
|
(embark-collect-mode . consult-preview-at-point-mode))
|
|
|
|
|
|
|
|
(provide 'init-minibuffer)
|
|
|
|
;;; init-minibuffer.el ends here
|