completion/ivy: refactor ag/rg/pt/git-grep advice functions

They are no isolated to their evil use cases.
This commit is contained in:
Henrik Lissner 2018-03-18 22:00:14 -04:00
parent ff2f30f9b4
commit 8d72257aa6
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 74 additions and 56 deletions

View file

@ -48,6 +48,10 @@
(default-directory directory))
(setq +ivy--file-last-search query)
(require 'counsel)
(cl-letf (((symbol-function 'counsel-ag-function)
(symbol-function '+ivy*counsel-ag-function))
((symbol-function 'counsel-git-grep-function)
(symbol-function '+ivy*counsel-git-grep-function)))
(pcase engine
('grep
(let ((args (if recursion-p " -r"))
@ -78,7 +82,7 @@
(unless recursion-p " --depth=1")))
(default-directory directory))
(counsel-pt query)))
(_ (error "No search engine specified")))))
(_ (error "No search engine specified"))))))
;;;###autoload (autoload '+ivy:pt "completion/ivy/autoload/evil" nil t)
(evil-define-operator +ivy:pt (beg end query &optional all-files-p directory)
@ -160,3 +164,41 @@ NOTE: ripgrep doesn't support multiline searches (yet)."
(interactive "<r><a><!>")
(let ((+ivy--file-search-recursion-p (not bang)))
(+ivy:rg beg end query t default-directory)))
;;
;; Advice
;;
;;;###autoload
(defun +ivy*counsel-ag-function (string)
"Advice to get rid of the character limit from `counsel-ag-function'.
NOTE This may need to be updated frequently, to meet changes upstream (in
counsel-rg)."
(if (< (length string) 1) ; <-- modified the character limit
(counsel-more-chars 1) ; <--
(let ((default-directory (ivy-state-directory ivy-last))
(regex (counsel-unquote-regex-parens
(setq ivy--old-re
(ivy--regex string)))))
(counsel--async-command (format counsel-ag-command
(shell-quote-argument regex)))
nil)))
;;;###autoload
(defun +ivy*counsel-git-grep-function (string)
"Advice to get rid of the character limit from `counsel-git-grep-function'.
NOTE This may need to be updated frequently, to meet changes upstream (in
counsel-git-grep)."
(if (and (> counsel--git-grep-count counsel--git-grep-count-threshold)
(< (length string) 1)) ; <-- modified the character limit
(counsel-more-chars 1) ; <--
(let* ((default-directory (ivy-state-directory ivy-last))
(cmd (format counsel-git-grep-cmd
(setq ivy--old-re (ivy--regex string t)))))
(if (<= counsel--git-grep-count counsel--git-grep-count-threshold)
(split-string (shell-command-to-string cmd) "\n" t)
(counsel--gg-candidates (ivy--regex string))
nil))))

View file

@ -155,23 +155,6 @@ search current file. See `+ivy-task-tags' to customize what this searches for."
:action #'+ivy--tasks-open-action
:caller '+ivy/tasks))
;;;###autoload
(defun +ivy*counsel-ag-function (string)
"Advice to get rid of the character limit from `counsel-ag-function' and use
`evil-set-jump' if evil is active.
NOTE This may need to be updated frequently, to meet changes upstream (in
counsel-rg)."
(if (< (length string) 1) ; <-- modified the character limit
(counsel-more-chars 1) ; <--
(let ((default-directory (ivy-state-directory ivy-last))
(regex (counsel-unquote-regex-parens
(setq ivy--old-re
(ivy--regex string)))))
(counsel--async-command (format counsel-ag-command
(shell-quote-argument regex)))
nil)))
;;;###autoload
(defun +ivy/wgrep-occur ()
"Invoke the search+replace wgrep buffer on the current ag/rg search results."

View file

@ -107,14 +107,7 @@ immediately runs it on the current candidate (ending the ivy session)."
(dolist (cmd '(counsel-ag counsel-rg counsel-pt))
(ivy-add-actions
cmd
'(("O" +ivy-git-grep-other-window-action "open in other window"))))
;; Removes character limit from `counsel-ag-function' and sets a jump point
;; for `evil'.
;;
;; This may need to be updated frequently, to meet changes upstream.
;; counsel-ag, counsel-rg and counsel-pt all use this function
(advice-add #'counsel-ag-function :override #'+ivy*counsel-ag-function))
'(("O" +ivy-git-grep-other-window-action "open in other window")))))
(def-package! counsel-projectile