help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Replacing Text in Compilation buffer


From: Drew Adams
Subject: RE: Replacing Text in Compilation buffer
Date: Mon, 3 Nov 2008 11:43:54 -0800

> In Ada mode there is command the "ada-find-references" that lists all
> references of a word in a Compilation buffer.   Some sample output
> looks like this.
> -*- mode: compilation; default-directory: "~/prog/ada/project3/src/" - *-
> Compilation started at Mon Nov  3 11:54:06
> gnat find -rf print_ip:project3.adb -P"/home/joe/prog/ada/project3/
> project3.gpr"
> /home/joe/prog/ada/project3/src/project3.adb:50:17: (spec) Print_IP
> /home/joe/prog/ada/project3/src/project3.adb:50:17: (body) Print_IP
> /home/joe/prog/ada/project3/src/project3.adb:55:11:        Print_IP
> Compilation finished at Mon Nov  3 11:54:06
> 
> How would I go about replacing Print_IP with something else?


I assume you mean replacing it in the source files. ;-)

1. You can do this if you use Icicles (and Emacs 22 or later). In *grep* and
compilation buffers, C-c ` lets you visit grep/compilation hits that match
patterns that you can change on the fly. While doing that, you can replace all
or part of any hits you choose.

This is like query-replace without being queried for each occurrence.
Replacement is thus demand-driven: you hit a key to replace occurrences you
pick.

Example: 

`C-`' in compilation (or *grep*) buffer shows you all compilation lines as
completion candidates in *Completions*.

. You type `foo'. *Completions* shows only the lines that match `foo'.

. You use `next' a few times to cycle to a line you want to visit or change.

. You hit `C-RET' to visit that line in the source code.

. You hit `C-S-RET' to replace `foo' in that line. You enter the replacement
text.

`C-next' is like `next' + `C-RET': it visits each search (compilation) hit as
you cycle to it in *Completions*. Repeating `C-S-RET' replaces subsequent hits
the same way, including matches within the same line. `C-,' at any time toggles
between replacing the whole hit line and replacing only the part that matches
your current input (e.g. `foo'). `M-,' at any time prompts you for a new
replacement string. You can use a regexp to filter the hits and you can use a
Lisp sexp to control replacement a la `query-replace-regexp'.

You can use `C-|' to replace all hits that match your current input pattern. You
can change the input pattern on the fly, getting a different set of hits, and
then continue visiting or replacing text.

You can visit hits in any order. You can cycle among hits forward or backward.
Instead of cycling with `C-next' to a hit you are interested in, you can use
`C-mouse-2' in *Completions* to visit it directly. And you can use `C-S-mouse-2'
to replace text at that occurrence directly. 

*
http://www.emacswiki.org/emacs/Icicles_-_Other_Search_Commands#IciclesCompileOrG
rep
* http://www.emacswiki.org/emacs/Icicles_-_Search-And-Replace

You can also do the same kind of search-and-replace without bothering to use
`grep' or `compile' - you can search-and-replace in any number of files or
buffers at once.

* http://www.emacswiki.org/emacs/Icicles_-_Search_Commands%2c_Overview


2. Dunno if it ever got added to Emacs (I don't see anything about it in the
Emacs 23 manual), but there was also a vanilla Emacs patch (by Kim Storm) to let
you do text replacements based on `grep' hits. I imagine it worked also for
compilation buffer output. See the (brief) emacs-devel@gnu.org thread "Doing
search and replace via *grep* buffer", 2007-10-11:
http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00549.html.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]