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

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

Re: Doing numbered changes in a buffer


From: Brendan Halpin
Subject: Re: Doing numbered changes in a buffer
Date: Fri, 16 Dec 2005 16:54:00 +0000
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

"gamename" <namesagame-usenet@yahoo.com> writes:

> Is there any way to do a string replace in the buffer such that each
> instance of "puts" would be assigned a unique message number? For
> example:
>
> puts "001 - something went wrong"
> ...
> puts "002 - something went wrong"

Using these two functions below (the second of which I got off
Usenet many years ago, and have used regularly since), put point at
the start of where you want the changes to happen, and do:

M-x enumerate-bit-of-text RET something went wrong RET

You can fine-tune the result by adding e.g. "(forward-char 5)"
before the "insert" function, or inserting more than the "!1". If
you really want the leading zeros, use a format statement in the
"(insert (int-to-string start))" enumerate function.

Regards,

Brendan


(defun enumerate-bit-of-text (text)
  (interactive "sText to enumerate: ")
  (save-excursion 
    (while (re-search-forward text nil t)
      (goto-char (match-beginning 0))
      (insert "!1")
      (goto-char (match-end 0))))
  (enumerate "!1" 1))

;; A function I got from David Biesack, SAS Institute Inc., many
;; years ago:
(defun enumerate (&optional pattern start)
  "Replace PATTERN with sequential numbers beginning
with prefix arg START (default is 1). "
  (interactive "sRegular expression to enumerate [default \"!1\"]: \np")
  (if (string= "" pattern) (setq pattern "!1"))
  (save-excursion
    (while (re-search-forward pattern nil t)
      (delete-region (match-beginning 0) (match-end 0))
      (insert (int-to-string start))
      (setq start (1+ start)))))

-- 
Brendan Halpin,  Department of Sociology,  University of Limerick,  Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F2-025 x 3147
mailto:brendan.halpin@ul.ie  http://www.ul.ie/sociology/brendan.halpin.html


reply via email to

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