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

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

Re: string-replace interactive function hit or miss?


From: Kevin Rodgers
Subject: Re: string-replace interactive function hit or miss?
Date: Mon, 13 Feb 2006 14:26:48 -0700
User-agent: Mozilla Thunderbird 0.9 (X11/20041105)

ChristineLWilson@gmail.com wrote:
>   I have a large text file that I need to replace multiple pieces of
> text with other chunks...so I open my file in emacs, make my function
> interactive, but it seems as though the replacements are happening hit
> or miss.  Any ideas why this isn't running properly and how I can make
> it complete/reliable?  Please see the function and text chunk below.
>
> Thanks,
> Christine
>
> ;;the interactive function
> (defun replacegoals ()
>   (interactive)
>   (goto-char (point-min))
>   (replace-string "(goal qleadin" "\"Consider the following
> question:<P>\", \"GNAME:qleadin\",")
>   (replace-string "(goal e1b1.q" "\"<B>Suppose that the suitcase has no
> wheels, and is instead subject to a frictional force as it slides
> across the floor.  If everything else were the same, how would the work
> done by the strap on the suitcase change?</B>\", \"GNAME:e1b1.q\",")
>   (replace-string "(goal dummyans" "\"<P>Please enter your answer:\",
> \"GNAME:dummyans\",")
>   (replace-string "(goal ialeadin" "\"Here is how a physics expert
> answered the same question:<P>\", \"GNAME:ialeadin\",")
> ;;;and so on....
> )

Don't use replace-string.  Do as its doc string suggests, making sure
you move point to the beginning of the buffer before each replacement:

| This function is usually the wrong thing to use in a Lisp program.
| What you probably want is a loop like this:
|   (while (search-forward FROM-STRING nil t)
|     (replace-match TO-STRING nil t))
| which will run faster and will not set the mark or print anything.
| (You may need a more complex loop if FROM-STRING can match the null string
| and TO-STRING is also null.)

--
Kevin Rodgers





reply via email to

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