unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Emacs regexp/incrementer question
@ 2003-01-16 11:15 Tim Morley (remove vegetable for email address)
  2003-01-16 12:17 ` Friedrich Dominicus
  2003-01-16 14:49 ` Peter Boettcher
  0 siblings, 2 replies; 7+ messages in thread
From: Tim Morley (remove vegetable for email address) @ 2003-01-16 11:15 UTC (permalink / raw)


Hi all.

Does anybody know of a way to include an incrementing number in a regexp
search-and-replace in Emacs? What I want to do is search for a regexp, and
the first time I find it, replace it with "case 1", the second time replace
it with "case 2", etc.

Even if the solution is a several-stager, I'd be more than happy, because
I'm currently having to replace each occurrence with "case xxx" and then
change each "xxx" to a number by hand afterwards -- hardly ideal. I could
use awk or something to change the xxx's to incrementing numbers, but I'd
much prefer to do it all in Emacs if I can.

Thanks in advance for your help.


Tim Morley

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs regexp/incrementer question
  2003-01-16 11:15 Emacs regexp/incrementer question Tim Morley (remove vegetable for email address)
@ 2003-01-16 12:17 ` Friedrich Dominicus
  2003-01-17  9:01   ` Tim Morley (remove vegetable for email address)
  2003-01-16 14:49 ` Peter Boettcher
  1 sibling, 1 reply; 7+ messages in thread
From: Friedrich Dominicus @ 2003-01-16 12:17 UTC (permalink / raw)


"Tim Morley \(remove vegetable for email address\)" <tim@teamlog.turnip.com> writes:

> Hi all.
> 
> Does anybody know of a way to include an incrementing number in a regexp
> search-and-replace in Emacs? What I want to do is search for a regexp, and
> the first time I find it, replace it with "case 1", the second time replace
> it with "case 2", etc.
> 
> Even if the solution is a several-stager, I'd be more than happy, because
> I'm currently having to replace each occurrence with "case xxx" and then
> change each "xxx" to a number by hand afterwards -- hardly ideal. I could
> use awk or something to change the xxx's to incrementing numbers, but I'd
> much prefer to do it all in Emacs if I can.
> 
> Thanks in advance for your help.
Here's a starting point for doing that
(defun replace-with-changing-replacement (pattern replacement)
  (interactive "sPattern: \nsReplacement: ")
  (let ((count 1))
    (while (re-search-forward pattern nil t)
      (let ((repl (format "%s %d" replacement count)))
        (incf count)
        (replace-match repl)))))

Not really tested but just checked it works for this
foobar 
another foo
yet another foo
and last but not foo least foo
foo foo foo 
end foo
M-x replace-with-changing-replacement RET
Pattern: \<foo\> RET
Replacement: case RET

will change that too:
foobar 
another case 1
yet another case 2
and last but not case 3 least case 4
case 5 case 6 case 7 
end case 8

Which is what you want.

Regards
Friedrich

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs regexp/incrementer question
  2003-01-16 11:15 Emacs regexp/incrementer question Tim Morley (remove vegetable for email address)
  2003-01-16 12:17 ` Friedrich Dominicus
@ 2003-01-16 14:49 ` Peter Boettcher
  1 sibling, 0 replies; 7+ messages in thread
From: Peter Boettcher @ 2003-01-16 14:49 UTC (permalink / raw)


"Tim Morley \(remove vegetable for email address\)"
<tim@teamlog.turnip.com> writes:

> Hi all.
>
> Does anybody know of a way to include an incrementing number in a
> regexp search-and-replace in Emacs? What I want to do is search for
> a regexp, and the first time I find it, replace it with "case 1",
> the second time replace it with "case 2", etc.
>
> Even if the solution is a several-stager, I'd be more than happy,
> because I'm currently having to replace each occurrence with "case
> xxx" and then change each "xxx" to a number by hand afterwards --
> hardly ideal. I could use awk or something to change the xxx's to
> incrementing numbers, but I'd much prefer to do it all in Emacs if I
> can.
>
> Thanks in advance for your help.

I have a few counter functions bound to keys to use with keyboard
macros.


(defvar macro-counter 0)

(defun macro-counter-reset nil
  (interactive)
  (setq macro-counter 0))

(defun macro-counter-insert nil
  (interactive)
  (insert (number-to-string macro-counter)))

(defun macro-counter-increment-and-insert nil
  (interactive)
  (setq macro-counter (+ macro-counter 1))
  (insert (number-to-string macro-counter)))

(global-set-key "\C-c=" 'macro-counter-insert)
(global-set-key "\C-c-" 'macro-counter-reset)
(global-set-key "\C-c+" 'macro-counter-increment-and-insert)


So then whenever I need to generate some code with increasing
numbers, I can do hacks like:

C-x (        ; start recording macro
C-s foo RET  ; look for foo
M-d          ; delete foo
c a s e SPC  ; just type the constant part
C-c +        ; increment and insert counter
C-n C-a      ; next line, beginning of line
C-x )        ; end macro

Then

C-x e        ; run macro once to be sure it does what I want
C-u C-u C-x e  ; run it a bunch of times (16)


-- 
Peter Boettcher
MIT Lincoln Laboratory
boettcher@ll.mit.edu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs regexp/incrementer question
  2003-01-16 12:17 ` Friedrich Dominicus
@ 2003-01-17  9:01   ` Tim Morley (remove vegetable for email address)
  2003-01-17 12:59     ` Friedrich Dominicus
  0 siblings, 1 reply; 7+ messages in thread
From: Tim Morley (remove vegetable for email address) @ 2003-01-17  9:01 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2295 bytes --]

Hi Friedrich.

I'm no lisp hacker at all, so thanks very much for the copy-and-paste
solution. Unfortunately I'm having trouble persuading it to work on my
machine. (Win2K, GNU Emacs 20.3.1)

I even copyied-and-pasted the example from your post to be sure  :o),  did a
M-x replace-with-changing-replacement, and entered the pattern and the
replacement. On finding the first 'foo' though, the function stopped with
the error : "Symbol's function definition is void: incf". As I said, I don't
know lisp at all -- is it just a typo? If not, do you have a suggestion as
to how I might resolve this?

Thanks in advance.


Tim


"Friedrich Dominicus" <frido@q-software-solutions.com> a écrit dans le
message news: 87smvtjnlb.fsf@fbigm.here...
> "Tim Morley \(remove vegetable for email address\)"
<tim@teamlog.turnip.com> writes:
>
> > Hi all.
> >
> > Does anybody know of a way to include an incrementing number in a regexp
> > search-and-replace in Emacs? What I want to do is search for a regexp,
and
> > the first time I find it, replace it with "case 1", the second time
replace
> > it with "case 2", etc.
> >
> > Even if the solution is a several-stager, I'd be more than happy,
because
> > I'm currently having to replace each occurrence with "case xxx" and then
> > change each "xxx" to a number by hand afterwards -- hardly ideal. I
could
> > use awk or something to change the xxx's to incrementing numbers, but
I'd
> > much prefer to do it all in Emacs if I can.
> >
> > Thanks in advance for your help.
> Here's a starting point for doing that
> (defun replace-with-changing-replacement (pattern replacement)
>   (interactive "sPattern: \nsReplacement: ")
>   (let ((count 1))
>     (while (re-search-forward pattern nil t)
>       (let ((repl (format "%s %d" replacement count)))
>         (incf count)
>         (replace-match repl)))))
>
> Not really tested but just checked it works for this
> foobar
> another foo
> yet another foo
> and last but not foo least foo
> foo foo foo
> end foo
> M-x replace-with-changing-replacement RET
> Pattern: \<foo\> RET
> Replacement: case RET
>
> will change that too:
> foobar
> another case 1
> yet another case 2
> and last but not case 3 least case 4
> case 5 case 6 case 7
> end case 8
>
> Which is what you want.
>
> Regards
> Friedrich
>
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs regexp/incrementer question
  2003-01-17  9:01   ` Tim Morley (remove vegetable for email address)
@ 2003-01-17 12:59     ` Friedrich Dominicus
  2003-01-17 13:44       ` Christopher J. White
  2003-01-17 14:12       ` Tim Morley (remove vegetable for email address)
  0 siblings, 2 replies; 7+ messages in thread
From: Friedrich Dominicus @ 2003-01-17 12:59 UTC (permalink / raw)


"Tim Morley \(remove vegetable for email address\)" <tim@teamlog.turnip.com> writes:

> Hi Friedrich.
> 
> I'm no lisp hacker at all, so thanks very much for the copy-and-paste
> solution. Unfortunately I'm having trouble persuading it to work on my
> machine. (Win2K, GNU Emacs 20.3.1)
> 
> I even copyied-and-pasted the example from your post to be sure  :o),  did a
> M-x replace-with-changing-replacement, and entered the pattern and the
> replacement. On finding the first 'foo' though, the function stopped with
> the error : "Symbol's function definition is void: incf".
Just put a (require 'cl) before that function or even better put it in
the first place of your .emacs file. Or replace it with
(setq i (1+ i)) or (setq i (+ i 1)) or write a macro for it ;-)

> As I said, I don't
> know lisp at all -- is it just a typo? If not, do you have a suggestion as
> to how I might resolve this?
Done see above ;-)

Regards
Friedrich

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs regexp/incrementer question
  2003-01-17 12:59     ` Friedrich Dominicus
@ 2003-01-17 13:44       ` Christopher J. White
  2003-01-17 14:12       ` Tim Morley (remove vegetable for email address)
  1 sibling, 0 replies; 7+ messages in thread
From: Christopher J. White @ 2003-01-17 13:44 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 687 bytes --]

>>>>> "tim" == Tim Morley \(remove vegetable for email address\) <Tim> writes:

tim> Does anybody know of a way to include an incrementing number in a
tim> regexp search-and-replace in Emacs? What I want to do is search
tim> for a regexp, and the first time I find it, replace it with "case
tim> 1", the second time replace it with "case 2", etc.

Here's a function that I wrote a while ago...almost what you want.
When run interactively, gives something like:

  Replace [n] skip [1] case1 [2] case2 [3] case3?

Press [n] to skip to next match, [1-3] to replace with the
case1-3, [return] to quit.

Could be modified pretty easily to autoincrement
a value in the replace string.

...cj

[-- Attachment #2: query-replace-regexp-select --]
[-- Type: application/emacs-lisp, Size: 2556 bytes --]

[-- Attachment #3: Type: text/plain, Size: 242 bytes --]


-- 
------------------------------------------------------------------------------
 Christopher J. White                                    chris@grierwhite.com
------------------------------------------------------------------------------ 

[-- Attachment #4: Type: text/plain, Size: 151 bytes --]

_______________________________________________
Help-gnu-emacs mailing list
Help-gnu-emacs@gnu.org
http://mail.gnu.org/mailman/listinfo/help-gnu-emacs

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs regexp/incrementer question
  2003-01-17 12:59     ` Friedrich Dominicus
  2003-01-17 13:44       ` Christopher J. White
@ 2003-01-17 14:12       ` Tim Morley (remove vegetable for email address)
  1 sibling, 0 replies; 7+ messages in thread
From: Tim Morley (remove vegetable for email address) @ 2003-01-17 14:12 UTC (permalink / raw)


Friedrich,

> >
> > I'm no lisp hacker at all, so thanks very much for the copy-and-paste
> > solution.
>
> Just put a (require 'cl) before that function or even better put it in
> the first place of your .emacs file. Or replace it with
> (setq i (1+ i)) or (setq i (+ i 1)) or write a macro for it ;-)
>

Thanks aplenty -- works like a dream. I'm even picking up bits of lisp
working through examples like yours.  :o)

Thanks also for the other proposed solutions -- cj, Peter -- cheers.


Tim

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-01-17 14:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-01-16 11:15 Emacs regexp/incrementer question Tim Morley (remove vegetable for email address)
2003-01-16 12:17 ` Friedrich Dominicus
2003-01-17  9:01   ` Tim Morley (remove vegetable for email address)
2003-01-17 12:59     ` Friedrich Dominicus
2003-01-17 13:44       ` Christopher J. White
2003-01-17 14:12       ` Tim Morley (remove vegetable for email address)
2003-01-16 14:49 ` Peter Boettcher

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).