[ivy] counsel search commands use spacemacs--ivy-file-actions

spacemacs/counsel-search counsel-rg counsel-ag and counsel-find-file now share a
comon set of actions defined in `spacemacs--ivy-file-actions`
This commit is contained in:
Thanh Vuong 2021-09-20 23:20:51 -06:00 committed by Maxi Wolff
parent 497c767036
commit 03ab15ed36
No known key found for this signature in database
GPG Key ID: 2DD07025BFDBD89A
3 changed files with 60 additions and 11 deletions

View File

@ -13,6 +13,7 @@
- [[#key-bindings][Key bindings]]
- [[#transient-state][Transient state]]
- [[#colorsfaces][Colors/Faces]]
- [[#search-files-with-ivy][Search files with ivy]]
* Description
This layer enables Ivy for completion. It will replace the default completion by
@ -140,3 +141,37 @@ are found in [[https://oremacs.com/swiper/#minibuffer-key-bindings][the Hydra se
| ~SPC C f~ | =counsel-colors-faces= |
| ~SPC C w~ | =counsel-colors-web= |
| ~SPC h d F~ | =counsel-describe-face= |
** Search files with ivy
=ripgrep= is recommended and =Spacemacs= will pick it up as the default seach
app if found. To pass parameters to =ripgrep= use double dash then everything
after it is treated as parameters for search app.
For example:
#+begin_src
phrase I want to search -- -tlisp
#+end_src
will search only on lisp files.
Commands available while browsing the search result:
| Key binding | Description |
|------------------+----------------------------------|
| ~C-SPC~ or ~C-l~ | Preview result |
| ~C-x C-d~ | Change search folder |
| ~M-q~ | =counsel-git-grep-query-replace= |
| ~C-c C-o~ | =ivy-occur= |
| ~C-c C-e~ | Spacemacs's =counsel-edit= |
When you =M-o= on the result list of =counsel-find-file= and file search result
you have these following extra actions:
| Key binding | Description |
|-------------+---------------------------------|
| ~f~ | =find-file-other-frame= |
| ~j~ | =find-file-other-window= |
| ~v~ | =spacemacs/find-file-vsplit= |
| ~s~ | =spacemacs/find-file-split= |
| ~l~ | =find-file-literally= |
| ~d~ | =spacemacs/delete-file-confirm= |
| ~r~ | =spacemacs/rename-file= |

View File

@ -361,18 +361,27 @@ To prevent this error we just wrap `describe-mode' to defeat the
(call-interactively 'describe-mode))))
(defun spacemacs//counsel-with-git-grep (func x)
"This function should be kept in sync with `counsel-git-grep-action'.
We copy exactly that function and modify it a bit which allows us
to programatically add extra actions to counsel git-grep based
commands."
(when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
(with-ivy-window
(let ((file-name (match-string-no-properties 1 x))
(line-number (match-string-no-properties 2 x)))
(funcall func
(expand-file-name file-name (ivy-state-directory ivy-last)))
(goto-char (point-min))
(forward-line (1- (string-to-number line-number)))
(re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
(unless (eq ivy-exit 'done)
(swiper--cleanup)
(swiper--add-overlays (ivy--regex ivy-text)))))))
(let ((file-name (match-string-no-properties 1 x))
(line-number (match-string-no-properties 2 x)))
;; this line is the difference to `counsel-git-grep-action'
(funcall func
(expand-file-name file-name (ivy-state-directory ivy-last)))
(goto-char (point-min))
(forward-line (1- (string-to-number line-number)))
(when (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
(when swiper-goto-start-of-match
(goto-char (match-beginning 0))))
(swiper--ensure-visible)
(run-hooks 'counsel-grep-post-action-hook)
(unless (eq ivy-exit 'done)
(swiper--cleanup)
(swiper--add-overlays (ivy--regex ivy-text))))))
;;; org

View File

@ -156,6 +156,11 @@
'counsel-find-file
spacemacs--ivy-file-actions)
(dolist (action '(spacemacs/counsel-search counsel-rg counsel-ag))
(ivy-set-actions
action
spacemacs--ivy-grep-actions))
(when (or (eq 'vim dotspacemacs-editing-style)
(and (eq 'hybrid dotspacemacs-editing-style)
hybrid-style-enable-hjkl-bindings))