[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Emacs regexp/incrementer question
From: |
Peter Boettcher |
Subject: |
Re: Emacs regexp/incrementer question |
Date: |
Thu, 16 Jan 2003 09:49:13 -0500 |
User-agent: |
Gnus/5.090008 (Oort Gnus v0.08) Emacs/21.3.50 (i686-pc-linux-gnu) |
"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