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>")
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