unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Tim Morley \(remove vegetable for email address\)" <tim@teamlog.turnip.com>
Subject: Re: Emacs regexp/incrementer question
Date: Fri, 17 Jan 2003 10:01:59 +0100	[thread overview]
Message-ID: <b08go3$hbp$1@s1.read.news.oleane.net> (raw)
In-Reply-To: 87smvtjnlb.fsf@fbigm.here

[-- 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
>
>

  reply	other threads:[~2003-01-17  9:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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) [this message]
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

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='b08go3$hbp$1@s1.read.news.oleane.net' \
    --to=tim@teamlog.turnip.com \
    /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.
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).