branch: master commit 70f60554c634c05e449f1f29051fd14329313812 Author: Oleh Krehel <ohwoeo...@gmail.com> Commit: Oleh Krehel <ohwoeo...@gmail.com>
counsel.el (counsel-git-grep-query-replace): Add and bind to "M-q" * counsel.el (counsel-git-grep-map): Bind "M-q" to `counsel-git-grep-query-replace'. (counsel-git-grep-query-replace): New command. Perform `query-replace' on all matches of git-grep in all buffers. --- counsel.el | 25 +++++++++++++++++++++++++ 1 files changed, 25 insertions(+), 0 deletions(-) diff --git a/counsel.el b/counsel.el index eab1a3c..3694aa6 100644 --- a/counsel.el +++ b/counsel.el @@ -375,8 +375,33 @@ (defvar counsel-git-grep-map (let ((map (make-sparse-keymap))) (define-key map (kbd "C-l") 'counsel-git-grep-recenter) + (define-key map (kbd "M-q") 'counsel-git-grep-query-replace) map)) +(defun counsel-git-grep-query-replace () + "Start `query-replace' with string to replace from last search string." + (interactive) + (if (null (window-minibuffer-p)) + (user-error + "Should only be called in the minibuffer through `counsel-git-grep-map'") + (let* ((enable-recursive-minibuffers t) + (from (ivy--regex ivy-text)) + (to (query-replace-read-to from "Query replace" t))) + (let (done-buffers) + (dolist (cand ivy--old-cands) + (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" cand) + (with-ivy-window + (let ((file-name (match-string-no-properties 1 cand))) + (setq file-name (expand-file-name file-name counsel--git-grep-dir)) + (unless (member file-name done-buffers) + (push file-name done-buffers) + (find-file file-name) + (goto-char (point-min))) + (perform-replace from to t t nil)))))) + (delete-minibuffer-contents) + (swiper--cleanup) + (exit-minibuffer)))) + (defun counsel-git-grep-recenter () (interactive) (with-ivy-window