completion/ivy: minor refactor

This commit is contained in:
Henrik Lissner 2019-10-25 20:24:36 -04:00
parent 5f00db871e
commit cf21cdc68c
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
4 changed files with 70 additions and 70 deletions

View file

@ -488,7 +488,6 @@ If ALL-FILES-P, search compressed and hidden files as well."
(interactive) (interactive)
(counsel-compile (projectile-project-root))) (counsel-compile (projectile-project-root)))
;;;###autoload ;;;###autoload
(defun +ivy/jump-list () (defun +ivy/jump-list ()
"Go to an entry in evil's (or better-jumper's) jumplist." "Go to an entry in evil's (or better-jumper's) jumplist."
@ -529,3 +528,11 @@ If ALL-FILES-P, search compressed and hidden files as well."
(goto-char (marker-position mark))))) (goto-char (marker-position mark)))))
:caller '+ivy/jump-list) :caller '+ivy/jump-list)
(mapc #'kill-buffer buffers)))) (mapc #'kill-buffer buffers))))
;;;###autoload
(defun +ivy/git-grep-other-window-action ()
"Open the current counsel-{ag,rg,git-grep} candidate in other-window."
(interactive)
(ivy-set-action #'+ivy-git-grep-other-window-action)
(setq ivy-exit 'done)
(exit-minibuffer))

View file

@ -34,14 +34,17 @@ If you want to already use git-grep or grep, set this to nil.")
"A plist mapping ivy/counsel commands to commands that generate an editable "A plist mapping ivy/counsel commands to commands that generate an editable
results buffer.") results buffer.")
(defmacro +ivy-do-action! (action) (defvar +ivy-standard-search-fn
"Returns an interactive lambda that sets the current ivy action and (if (featurep! +prescient)
immediately runs it on the current candidate (ending the ivy session)." #'+ivy-prescient-non-fuzzy
`(lambda () #'ivy--regex-plus)
(interactive) "Function to use for non-fuzzy search commands.")
(ivy-set-action ,action)
(setq ivy-exit 'done) (defvar +ivy-alternative-search-fn
(exit-minibuffer))) (cond ((featurep! +prescient) #'ivy-prescient-re-builder)
((featurep! +fuzzy) #'ivy--regex-fuzzy)
(#'ivy--regex-ignore-order))
"Function to use for fuzzy search commands.")
;; ;;
@ -51,13 +54,20 @@ immediately runs it on the current candidate (ending the ivy session)."
:after-call pre-command-hook :after-call pre-command-hook
:init :init
(setq ivy-re-builders-alist (setq ivy-re-builders-alist
'((counsel-ag . ivy--regex-plus) `(,@(cl-loop for cmd in '(counsel-ag
(counsel-rg . ivy--regex-plus) counsel-rg
(counsel-grep . ivy--regex-plus) counsel-grep
(swiper . ivy--regex-plus) swiper
(swiper-isearch . ivy--regex-plus) swiper-isearch)
collect (cons cmd +ivy-standard-search-fn))
;; Ignore order for non-fuzzy searches by default ;; Ignore order for non-fuzzy searches by default
(t . ivy--regex-ignore-order))) (t . ,+ivy-alternative-search-fn)))
(define-key!
[remap switch-to-buffer] #'+ivy/switch-buffer
[remap switch-to-buffer-other-window] #'+ivy/switch-buffer-other-window
[remap persp-switch-to-buffer] #'+ivy/switch-workspace-buffer
[remap evil-show-jumps] #'+ivy/jump-list)
:config :config
(setq ivy-height 15 (setq ivy-height 15
ivy-wrap t ivy-wrap t
@ -74,26 +84,26 @@ immediately runs it on the current candidate (ending the ivy session)."
;; enable ability to select prompt (alternative to `ivy-immediate-done') ;; enable ability to select prompt (alternative to `ivy-immediate-done')
ivy-use-selectable-prompt t) ivy-use-selectable-prompt t)
;; Highlight each ivy candidate including the following newline, so that it
;; extends to the right edge of the window
(setf (alist-get 't ivy-format-functions-alist) (setf (alist-get 't ivy-format-functions-alist)
#'ivy-format-function-line) #'ivy-format-function-line)
;; Integrate `ivy' with `better-jumper'; ensure a jump point is registered ;; Integrate `ivy' with `better-jumper'; ensure a jump point is registered
;; before jumping to new locations with ivy ;; before jumping to new locations with ivy
(defvar +ivy--origin nil) (setf (alist-get 't ivy-hooks-alist)
(defun +ivy--record-position-maybe-fn () (lambda ()
(with-ivy-window (with-ivy-window
(setq +ivy--origin (point-marker)))) (setq +ivy--origin (point-marker)))))
(setq ivy-hooks-alist '((t . +ivy--record-position-maybe-fn)))
(add-hook! 'minibuffer-exit-hook (add-hook! 'minibuffer-exit-hook
(defun +ivy--set-jump-point-maybe-h () (defun +ivy--set-jump-point-maybe-h ()
(with-demoted-errors "Ivy error: %s" (and (markerp (bound-and-true-p +ivy--origin))
(when (and (markerp +ivy--origin) (not (equal (ignore-errors (with-ivy-window (point-marker)))
(not (equal (with-ivy-window (point-marker)) +ivy--origin))
+ivy--origin)))
(with-current-buffer (marker-buffer +ivy--origin) (with-current-buffer (marker-buffer +ivy--origin)
(better-jumper-set-jump +ivy--origin))) (better-jumper-set-jump +ivy--origin)))
(setq +ivy--origin nil)))) (setq +ivy--origin nil)))
(after! yasnippet (after! yasnippet
(add-hook 'yas-prompt-functions #'+ivy-yas-prompt)) (add-hook 'yas-prompt-functions #'+ivy-yas-prompt))
@ -105,12 +115,6 @@ evil-ex-specific constructs, so we disable it solely in evil-ex."
(let ((completion-in-region-function #'completion--in-region)) (let ((completion-in-region-function #'completion--in-region))
(apply orig-fn args))) (apply orig-fn args)))
(define-key! ivy-mode-map
[remap switch-to-buffer] #'+ivy/switch-buffer
[remap switch-to-buffer-other-window] #'+ivy/switch-buffer-other-window
[remap persp-switch-to-buffer] #'+ivy/switch-workspace-buffer
[remap evil-show-jumps] #'+ivy/jump-list)
(define-key ivy-minibuffer-map (kbd "C-c C-e") #'+ivy/woccur) (define-key ivy-minibuffer-map (kbd "C-c C-e") #'+ivy/woccur)
(ivy-mode +1) (ivy-mode +1)
@ -343,10 +347,7 @@ evil-ex-specific constructs, so we disable it solely in evil-ex."
:when (featurep! +fuzzy) :when (featurep! +fuzzy)
:unless (featurep! +prescient) :unless (featurep! +prescient)
:defer t ; is loaded by ivy :defer t ; is loaded by ivy
:init :init (setq ivy-flx-limit 10000))
(setf (alist-get 't ivy-re-builders-alist) #'ivy--regex-fuzzy)
(setq ivy-initial-inputs-alist nil
ivy-flx-limit 10000))
(use-package! ivy-prescient (use-package! ivy-prescient
@ -358,15 +359,7 @@ evil-ex-specific constructs, so we disable it solely in evil-ex."
'(literal regexp initialism fuzzy) '(literal regexp initialism fuzzy)
'(literal regexp initialism)) '(literal regexp initialism))
ivy-prescient-enable-filtering nil ; we do this ourselves ivy-prescient-enable-filtering nil ; we do this ourselves
ivy-prescient-retain-classic-highlighting t ivy-prescient-retain-classic-highlighting t)
ivy-initial-inputs-alist nil
ivy-re-builders-alist
'((counsel-ag . +ivy-prescient-non-fuzzy)
(counsel-rg . +ivy-prescient-non-fuzzy)
(counsel-grep . +ivy-prescient-non-fuzzy)
(swiper . +ivy-prescient-non-fuzzy)
(swiper-isearch . +ivy-prescient-non-fuzzy)
(t . ivy-prescient-re-builder)))
:config :config
(defun +ivy-prescient-non-fuzzy (str) (defun +ivy-prescient-non-fuzzy (str)

View file

@ -342,7 +342,7 @@
(:after counsel (:after counsel
:map counsel-ag-map :map counsel-ag-map
"C-SPC" #'ivy-call-and-recenter ; preview "C-SPC" #'ivy-call-and-recenter ; preview
"M-RET" (+ivy-do-action! #'+ivy-git-grep-other-window-action)) "M-RET" #'+ivy/git-grep-other-window-action)
"C-M-y" #'counsel-yank-pop) "C-M-y" #'counsel-yank-pop)
;;; neotree ;;; neotree

View file

@ -248,7 +248,7 @@
:map counsel-ag-map :map counsel-ag-map
"C-SPC" #'ivy-call-and-recenter ; preview "C-SPC" #'ivy-call-and-recenter ; preview
"C-l" #'ivy-done "C-l" #'ivy-done
[C-return] (+ivy-do-action! #'+ivy-git-grep-other-window-action))) [C-return] #'+ivy/git-grep-other-window-action))
(:when (featurep! :completion helm) (:when (featurep! :completion helm)
(:after helm (:after helm