From: ················@gmail.com
Subject: replace-string hit or miss?
Date: 
Message-ID: <1139687997.073627.285830@g44g2000cwa.googlegroups.com>
Hi,
  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....
)

;;a chunk of the text file
(goal e1b1ctr
  (goal qleadin
	"Consider the following question:<P>")
  (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>")
  (goal dummyans "<P>Please enter your answer:"
	(("$anything else$")))
  (goal ialeadin
	"Here is how a physics expert answered the same question:<P>")
From: Harald Hanche-Olsen
Subject: Re: replace-string hit or miss?
Date: 
Message-ID: <pcoaccxy2d7.fsf@shuttle.math.ntnu.no>
This is really an emacs question and not a lisp question, so you
should have asked in gnu.emacs.help or a similar place.  That said,
your problem seems easy enough, so I'll risk attracting the ire of
others by answering it here.

+ ················@gmail.com:

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

The body of your function has the structure

  (goto-char (point-min))
  (replace-string A1 B1)
  (replace-string A2 B2)
  ...

However, replace-string starts at the current point and leaves point
at the final replacement it makes, so the next replace-string starts
where the previous one left off.  You're better adviced to use the
optional arguments to replace-string:

  (replace-string A1 B1 nil (point-min) (point-max))
  (replace-string A2 B2 nil (point-min) (point-max))
  ...

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell