[comp.emacs] Anyone have a replace-regexp-region ?

sm2@sequent.cc.hull.ac.uk (Simon Marshall) (06/17/91)

Peter A Fletcher (peterf@csis.dit.csiro.au) writes:

| Hello there!  I was wondering if anyone had an elisp function to replace
| regexps in only the currently selected region ?  It would be a really handy
| thing to have around.

	Just rap around the replace-regexp function!  Like so:

(defun replace-regexp-region (regexp to-string start finish &optional delimmed)
  "Replace things matching REGEXP with TO-STRING in region.
Preserve case in each match if case-replace and case-fold-search
are non-nil and REGEXP has no uppercase letters.
If called non interactively, region is specified by third and fourth args START
and FINISH.
Fifth arg DELIMITED (prefix arg if interactive) non-nil means replace
only matches surrounded by word boundaries.
In TO-STRING, \& means insert what matched REGEXP,
and \<n> means insert what matched <n>th \(...\) in REGEXP.

	Simon. "
  (interactive "sReplace regexp: \nsReplace regexp %s with: \nrP")
  (save-excursion
    (save-restriction
      (narrow-to-region start finish)
      (goto-char (point-min))
      (perform-replace regexp to-string nil t delimmed)
      (message "Done"))))

_______________________________________________________________________________
Simon Marshall, Dept. of Computer Science, University of Hull, Hull HU6 7RX, UK
	      "``La la la la la la la la la'' means I love you."
 Email: S.Marshall@Hull.ac.uk    Phone: +44 482 465951 (office)    Fax: 466666