completion/ivy: minor refactor

This commit is contained in:
Henrik Lissner 2019-10-18 17:42:37 -04:00
parent 875225cdd9
commit ace4c4c782
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -48,7 +48,6 @@ immediately runs it on the current candidate (ending the ivy session)."
;;; Packages ;;; Packages
(use-package! ivy (use-package! ivy
:defer 1
:after-call pre-command-hook :after-call pre-command-hook
:init :init
(setq ivy-re-builders-alist (setq ivy-re-builders-alist
@ -60,18 +59,10 @@ immediately runs it on the current candidate (ending the ivy session)."
;; Ignore order for non-fuzzy searches by default ;; Ignore order for non-fuzzy searches by default
(t . ivy--regex-ignore-order))) (t . ivy--regex-ignore-order)))
:config :config
;; Counsel has a history of modifying ivy's variables in a way that is very
;; difficult to predict. This makes changing its defaults (without changing
;; any possible user customizations of those defaults) very difficult. It's
;; easiest to load counsel immediately.
(require 'counsel nil t)
(setq ivy-height 15 (setq ivy-height 15
ivy-wrap t ivy-wrap t
ivy-fixed-height-minibuffer t ivy-fixed-height-minibuffer t
projectile-completion-system 'ivy projectile-completion-system 'ivy
;; Don't use ^ as initial input
ivy-initial-inputs-alist nil
;; disable magic slash on non-match ;; disable magic slash on non-match
ivy-magic-slash-non-match-action nil ivy-magic-slash-non-match-action nil
;; don't show recent files in switch-buffer ;; don't show recent files in switch-buffer
@ -86,32 +77,8 @@ immediately runs it on the current candidate (ending the ivy session)."
(setf (alist-get 't ivy-format-functions-alist) (setf (alist-get 't ivy-format-functions-alist)
#'ivy-format-function-line) #'ivy-format-function-line)
;; REVIEW Move this somewhere else and perhaps generalize this so both ;; Integrate `ivy' with `better-jumper'; ensure a jump point is registered
;; ivy/helm users can enjoy it. ;; before jumping to new locations with ivy
(defadvice! +ivy--counsel-file-jump-use-fd-rg-a (args)
"Change `counsel-file-jump' to use fd or ripgrep, if they are available."
:override #'counsel--find-return-list
(cl-destructuring-bind (find-program . args)
(cond ((executable-find "fd")
(cons "fd" (list "-t" "f" "-E" ".git")))
((executable-find "rg")
(cons "rg" (list "--files" "--hidden" "--no-messages")))
((cons find-program args)))
(unless (listp args)
(user-error "`counsel-file-jump-args' is a list now, please customize accordingly."))
(counsel--call
(cons find-program args)
(lambda ()
(goto-char (point-min))
(let ((offset (if (member find-program '("fd" "rg")) 0 2))
files)
(while (< (point) (point-max))
(push (buffer-substring
(+ offset (line-beginning-position)) (line-end-position)) files)
(forward-line 1))
(nreverse files))))))
;; Ensure a jump point is registered before jumping to new locations with ivy
(defvar +ivy--origin nil) (defvar +ivy--origin nil)
(defun +ivy--record-position-maybe-fn () (defun +ivy--record-position-maybe-fn ()
(with-ivy-window (with-ivy-window
@ -131,7 +98,7 @@ immediately runs it on the current candidate (ending the ivy session)."
(after! yasnippet (after! yasnippet
(add-hook 'yas-prompt-functions #'+ivy-yas-prompt)) (add-hook 'yas-prompt-functions #'+ivy-yas-prompt))
(defadvice! +ivy--inhibit-in-evil-ex-a (orig-fn &rest args) (defadvice! +ivy--inhibit-completion-in-region-a (orig-fn &rest args)
"`ivy-completion-in-region' struggles with completing certain "`ivy-completion-in-region' struggles with completing certain
evil-ex-specific constructs, so we disable it solely in evil-ex." evil-ex-specific constructs, so we disable it solely in evil-ex."
:around #'evil-ex :around #'evil-ex
@ -167,13 +134,12 @@ evil-ex-specific constructs, so we disable it solely in evil-ex."
'ivy-switch-buffer)))) 'ivy-switch-buffer))))
;; Include variable value in `counsel-describe-variable' ;; Include variable value in `counsel-describe-variable'
(setq ivy-rich-display-transformers-list (plist-put! ivy-rich-display-transformers-list
(plist-put ivy-rich-display-transformers-list 'counsel-describe-variable
'counsel-describe-variable '(:columns
'(:columns ((counsel-describe-variable-transformer (:width 40)) ; the original transformer
((counsel-describe-variable-transformer (:width 40)) ; the original transformer (+ivy-rich-describe-variable-transformer (:width 50))
(+ivy-rich-describe-variable-transformer (:width 50)) (ivy-rich-counsel-variable-docstring (:face font-lock-doc-face)))))
(ivy-rich-counsel-variable-docstring (:face font-lock-doc-face))))))
;; Remove built-in coloring of buffer list; we do our own ;; Remove built-in coloring of buffer list; we do our own
(setq ivy-switch-buffer-faces-alist nil) (setq ivy-switch-buffer-faces-alist nil)
@ -187,12 +153,10 @@ evil-ex-specific constructs, so we disable it solely in evil-ex."
(setcar switch-buffer-alist '+ivy-rich-buffer-name))) (setcar switch-buffer-alist '+ivy-rich-buffer-name)))
;; Apply switch buffer transformers to `counsel-projectile-switch-to-buffer' as well ;; Apply switch buffer transformers to `counsel-projectile-switch-to-buffer' as well
(setq ivy-rich-display-transformers-list (plist-put! ivy-rich-display-transformers-list
(plist-put ivy-rich-display-transformers-list 'counsel-projectile-switch-to-buffer
'counsel-projectile-switch-to-buffer (plist-get ivy-rich-display-transformers-list 'ivy-switch-buffer))
(plist-get ivy-rich-display-transformers-list 'ivy-switch-buffer)))
;; Reload ivy which so changes to `ivy-rich-display-transformers-list' work
(ivy-rich-mode +1)) (ivy-rich-mode +1))
@ -215,44 +179,76 @@ evil-ex-specific constructs, so we disable it solely in evil-ex."
(use-package! counsel (use-package! counsel
:commands counsel-describe-face :commands counsel-describe-face
:init :init
(map! [remap apropos] #'counsel-apropos (define-key!
[remap bookmark-jump] #'counsel-bookmark [remap apropos] #'counsel-apropos
[remap describe-face] #'counsel-faces [remap bookmark-jump] #'counsel-bookmark
[remap describe-function] #'counsel-describe-function [remap describe-face] #'counsel-faces
[remap describe-variable] #'counsel-describe-variable [remap describe-function] #'counsel-describe-function
[remap describe-bindings] #'counsel-descbinds [remap describe-variable] #'counsel-describe-variable
[remap set-variable] #'counsel-set-variable [remap describe-bindings] #'counsel-descbinds
[remap execute-extended-command] #'counsel-M-x [remap set-variable] #'counsel-set-variable
[remap find-file] #'counsel-find-file [remap execute-extended-command] #'counsel-M-x
[remap find-library] #'counsel-find-library [remap find-file] #'counsel-find-file
[remap info-lookup-symbol] #'counsel-info-lookup-symbol [remap find-library] #'counsel-find-library
[remap imenu] #'counsel-imenu [remap info-lookup-symbol] #'counsel-info-lookup-symbol
[remap recentf-open-files] #'counsel-recentf [remap imenu] #'counsel-imenu
[remap org-capture] #'counsel-org-capture [remap recentf-open-files] #'counsel-recentf
[remap swiper] #'counsel-grep-or-swiper [remap org-capture] #'counsel-org-capture
[remap evil-ex-registers] #'counsel-evil-registers [remap swiper] #'counsel-grep-or-swiper
[remap yank-pop] #'counsel-yank-pop [remap evil-ex-registers] #'counsel-evil-registers
[remap locate] #'counsel-locate [remap yank-pop] #'counsel-yank-pop
[remap compile] #'+ivy/compile [remap locate] #'counsel-locate
[remap projectile-compile-project] #'+ivy/project-compile) [remap compile] #'+ivy/compile
[remap projectile-compile-project] #'+ivy/project-compile)
:config :config
(set-popup-rule! "^\\*ivy-occur" :size 0.35 :ttl 0 :quit nil) (set-popup-rule! "^\\*ivy-occur" :size 0.35 :ttl 0 :quit nil)
;; Don't use ^ as initial input. Set this here because `counsel' defines more
;; of its own, on top of the defaults.
(setq ivy-initial-inputs-alist nil)
;; REVIEW Move this somewhere else and perhaps generalize this so both
;; ivy/helm users can enjoy it.
(defadvice! +ivy--counsel-file-jump-use-fd-rg-a (args)
"Change `counsel-file-jump' to use fd or ripgrep, if they are available."
:override #'counsel--find-return-list
(cl-destructuring-bind (find-program . args)
(cond ((executable-find "fd")
(cons "fd" (list "-t" "f" "-E" ".git")))
((executable-find "rg")
(cons "rg" (list "--files" "--hidden" "--no-messages")))
((cons find-program args)))
(unless (listp args)
(user-error "`counsel-file-jump-args' is a list now, please customize accordingly."))
(counsel--call
(cons find-program args)
(lambda ()
(goto-char (point-min))
(let ((offset (if (member find-program '("fd" "rg")) 0 2))
files)
(while (< (point) (point-max))
(push (buffer-substring
(+ offset (line-beginning-position)) (line-end-position)) files)
(forward-line 1))
(nreverse files))))))
;; Integrate with `helpful'
(setq counsel-describe-function-function #'helpful-callable
counsel-describe-variable-function #'helpful-variable)
;; Make `counsel-compile' projectile-aware (if you prefer it over ;; Make `counsel-compile' projectile-aware (if you prefer it over
;; `+ivy/compile' and `+ivy/project-compile') ;; `+ivy/compile' and `+ivy/project-compile')
(add-to-list 'counsel-compile-root-functions #'projectile-project-root) (add-to-list 'counsel-compile-root-functions #'projectile-project-root)
(after! savehist (after! savehist
;; Persist `counsel-compile' history ;; Persist `counsel-compile' history
(add-to-list 'savehist-additional-variables 'counsel-compile-history)) (add-to-list 'savehist-additional-variables 'counsel-compile-history))
;; Use spotlight on mac for `counsel-locate' by default
(when IS-MAC (when IS-MAC
(setq counsel-locate-cmd #'counsel-locate-cmd-mdfind)) (setq counsel-locate-cmd #'counsel-locate-cmd-mdfind))
(setq counsel-find-file-ignore-regexp "\\(?:^[#.]\\)\\|\\(?:[#~]$\\)\\|\\(?:^Icon?\\)"
counsel-describe-function-function #'helpful-callable
counsel-describe-variable-function #'helpful-variable)
(add-to-list 'swiper-font-lock-exclude #'+doom-dashboard-mode nil #'eq) ;; Don't mess with font-locking on the dashboard; it causes breakages
(add-to-list 'swiper-font-lock-exclude #'+doom-dashboard-mode)
;; Record in jumplist when opening files via counsel-{ag,rg,pt,git-grep} ;; Record in jumplist when opening files via counsel-{ag,rg,pt,git-grep}
(add-hook 'counsel-grep-post-action-hook #'better-jumper-set-jump) (add-hook 'counsel-grep-post-action-hook #'better-jumper-set-jump)
@ -270,6 +266,8 @@ evil-ex-specific constructs, so we disable it solely in evil-ex."
(funcall cmd source target 1)))) (funcall cmd source target 1))))
;; Configure `counsel-find-file' ;; Configure `counsel-find-file'
(setq counsel-find-file-at-point t
counsel-find-file-ignore-regexp "\\(?:^[#.]\\)\\|\\(?:[#~]$\\)\\|\\(?:^Icon?\\)")
(ivy-add-actions (ivy-add-actions
'counsel-find-file 'counsel-find-file
`(("b" counsel-find-file-cd-bookmark-action "cd bookmark") `(("b" counsel-find-file-cd-bookmark-action "cd bookmark")
@ -296,13 +294,21 @@ evil-ex-specific constructs, so we disable it solely in evil-ex."
(use-package! counsel-projectile (use-package! counsel-projectile
:defer t :defer t
:init :init
(map! [remap projectile-find-file] #'+ivy/projectile-find-file (define-key!
[remap projectile-find-dir] #'counsel-projectile-find-dir [remap projectile-find-file] #'+ivy/projectile-find-file
[remap projectile-switch-to-buffer] #'counsel-projectile-switch-to-buffer [remap projectile-find-dir] #'counsel-projectile-find-dir
[remap projectile-grep] #'counsel-projectile-grep [remap projectile-switch-to-buffer] #'counsel-projectile-switch-to-buffer
[remap projectile-ag] #'counsel-projectile-ag [remap projectile-grep] #'counsel-projectile-grep
[remap projectile-switch-project] #'counsel-projectile-switch-project) [remap projectile-ag] #'counsel-projectile-ag
[remap projectile-switch-project] #'counsel-projectile-switch-project)
:config :config
;; A more sensible `counsel-projectile-find-file' that reverts to
;; `counsel-find-file' if invoked from $HOME, `counsel-file-jump' if invoked
;; from a non-project, `projectile-find-file' if in a big project (more than
;; `ivy-sort-max-size' files), or `counsel-projectile-find-file' otherwise.
(setf (alist-get 'projectile-find-file counsel-projectile-key-bindings)
#'+ivy/projectile-find-file)
;; no highlighting visited files; slows down the filtering ;; no highlighting visited files; slows down the filtering
(ivy-set-display-transformer #'counsel-projectile-find-file nil)) (ivy-set-display-transformer #'counsel-projectile-find-file nil))
@ -333,8 +339,8 @@ evil-ex-specific constructs, so we disable it solely in evil-ex."
(use-package! flx (use-package! flx
:when (and (featurep! +fuzzy) :when (featurep! +fuzzy)
(not (featurep! +prescient))) :unless (featurep! +prescient)
:defer t ; is loaded by ivy :defer t ; is loaded by ivy
:init :init
(setf (alist-get 't ivy-re-builders-alist) #'ivy--regex-fuzzy) (setf (alist-get 't ivy-re-builders-alist) #'ivy--regex-fuzzy)