From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Peter Boettcher Newsgroups: gmane.emacs.help Subject: Re: Emacs regexp/incrementer question Date: Thu, 16 Jan 2003 09:49:13 -0500 Organization: MIT Lincoln Laboratory Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1042728732 23754 80.91.224.249 (16 Jan 2003 14:52:12 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Thu, 16 Jan 2003 14:52:12 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 18ZBNK-0006At-00 for ; Thu, 16 Jan 2003 15:52:10 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 18ZBME-0001mh-07 for gnu-help-gnu-emacs@m.gmane.org; Thu, 16 Jan 2003 09:51:02 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!bloom-beacon.mit.edu!llslave.llan.ll.mit.edu!53ab2750!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 64 User-Agent: Gnus/5.090008 (Oort Gnus v0.08) Emacs/21.3.50 (i686-pc-linux-gnu) Cancel-Lock: sha1:pcR6ZxllvuSvjRJd3kz6/BRX7xw= Original-NNTP-Posting-Host: 155.34.163.111 Original-X-Complaints-To: news@ll.mit.edu Original-X-Trace: llslave.llan.ll.mit.edu 1042728553 155.34.163.111 (Thu, 16 Jan 2003 09:49:13 EST) Original-NNTP-Posting-Date: Thu, 16 Jan 2003 09:49:13 EST Original-Xref: shelby.stanford.edu gnu.emacs.help:109027 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:5555 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:5555 "Tim Morley \(remove vegetable for email address\)" 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