unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: storm@cua.dk (Kim F. Storm)
To: emacs-devel@gnu.org
Subject: Doing search and replace via *grep* buffer
Date: Thu, 15 Feb 2007 13:11:20 +0100	[thread overview]
Message-ID: <m3hctnaadz.fsf@kfs-l.imdomain.dk> (raw)


Here is a small patch which provides a very handy search and replace
functionality via the normal grep interfaces (including lgrep/rgrep).

It requires that grep regexp highlighting works (I suppose most modern
grep programs do that).


Here is an example of its use, replacing _some_ occurrences of 
the identifier `oldtext' with `newtext':

First use rgrep to search for oldtext in all .c and .h files:
  M-x rgrep RET oldtext RET ch RET RET

Then in the *grep* buffer, move to the first instance which you want to
replace (use `n' or C-n).

Then hit / and type the replacement text:
  / newtext RET

Now, still in the *grep* buffer, move to the next instance you want
to modify, and just hit . to repeat the replacement, for example:
  n n . n . n . n n n .

(I suppose VI users will find this very familiar).


*** grep.el	14 Feb 2007 12:54:20 +0100	1.69
--- grep.el	15 Feb 2007 12:46:01 +0100	
***************
*** 185,192 ****
--- 185,195 ----
      (define-key map "\r" 'compile-goto-error)  ;; ?
      (define-key map "n" 'next-error-no-select)
      (define-key map "p" 'previous-error-no-select)
+     (define-key map "o" 'grep-goto-error-no-select)
      (define-key map "{" 'compilation-previous-file)
      (define-key map "}" 'compilation-next-file)
+     (define-key map "/" 'grep-replace-match)
+     (define-key map "." 'grep-repeat-replace-match)
      (define-key map "\t" 'compilation-next-error)
      (define-key map [backtab] 'compilation-previous-error)
  
***************
*** 790,795 ****
--- 793,831 ----
  	  (if (eq next-error-last-buffer (current-buffer))
  	      (setq default-directory dir)))))))
  
+ (defun grep-goto-error-no-select ()
+   "Display currently grep match in other window."
+   (interactive)
+   (save-selected-window
+     (compile-goto-error)))
+ 
+ (defvar grep-last-replace-string nil)
+ 
+ (defun grep-replace-match-internal ()
+   (when compilation-highlight-overlay
+     (let ((start (overlay-start compilation-highlight-overlay)))
+       (goto-char start)
+       (undo-boundary)
+       (delete-region start (overlay-end compilation-highlight-overlay))
+       (insert grep-last-replace-string)
+       (move-overlay compilation-highlight-overlay start (point)))))
+ 
+ (defun grep-replace-match (string)
+   "Replace current grep match with string STRING."
+   (interactive "sReplace with: ")
+   (save-selected-window
+     (setq grep-last-replace-string string)
+     (let ((next-error-highlight
+ 	   (if (numberp next-error-highlight) next-error-highlight 0.01))
+ 	  (next-error-hook '(grep-replace-match-internal)))
+       (compile-goto-error))))
+ 
+ (defun grep-repeat-replace-match ()
+   "Replace current grep match with last match replace string."
+   (interactive)
+   (if (not grep-last-replace-string)
+       (call-interactively 'grep-replace-match)
+     (grep-replace-match grep-last-replace-string)))
+ 
  
  (provide 'grep)
  

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

             reply	other threads:[~2007-02-15 12:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-15 12:11 Kim F. Storm [this message]
2007-02-15 12:35 ` Doing search and replace via *grep* buffer Lennart Borgman (gmail)
2007-02-15 13:46   ` Kim F. Storm
2007-02-15 14:16     ` Lennart Borgman (gmail)
2007-02-15 19:39       ` David Kastrup
2007-02-15 16:47     ` Stefan Monnier
2007-02-15 14:04 ` Chris Moore
2007-02-16  8:51   ` David Kastrup
2007-02-15 18:41 ` Andreas Roehler
2007-02-16  7:46 ` Richard Stallman
  -- strict thread matches above, loose matches on Subject: below --
2007-10-11 15:17 René Kyllingstad
2007-10-11 16:12 ` Drew Adams
2007-10-12  2:46 ` Richard Stallman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3hctnaadz.fsf@kfs-l.imdomain.dk \
    --to=storm@cua.dk \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).