From 03ab15ed36d265cb48c05d572638e21bc7508e26 Mon Sep 17 00:00:00 2001 From: Thanh Vuong Date: Mon, 20 Sep 2021 23:20:51 -0600 Subject: [PATCH] [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` --- layers/+completion/ivy/README.org | 35 ++++++++++++++++++++++++++++++ layers/+completion/ivy/funcs.el | 31 ++++++++++++++++---------- layers/+completion/ivy/packages.el | 5 +++++ 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/layers/+completion/ivy/README.org b/layers/+completion/ivy/README.org index 218dacd70..eedf18038 100644 --- a/layers/+completion/ivy/README.org +++ b/layers/+completion/ivy/README.org @@ -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= | diff --git a/layers/+completion/ivy/funcs.el b/layers/+completion/ivy/funcs.el index 5ff0da58d..6868a1492 100644 --- a/layers/+completion/ivy/funcs.el +++ b/layers/+completion/ivy/funcs.el @@ -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 diff --git a/layers/+completion/ivy/packages.el b/layers/+completion/ivy/packages.el index 75935f354..dee58830d 100644 --- a/layers/+completion/ivy/packages.el +++ b/layers/+completion/ivy/packages.el @@ -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))