From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: chris@grierwhite.com (Christopher J. White) Newsgroups: gmane.emacs.help Subject: Re: Emacs regexp/incrementer question Date: Fri, 17 Jan 2003 08:44:48 -0500 Organization: Posted via Supernews, http://www.supernews.com Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: <87smvtjnlb.fsf@fbigm.here> <87lm1kue30.fsf@fbigm.here> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: main.gmane.org 1042811095 22409 80.91.224.249 (17 Jan 2003 13:44:55 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Fri, 17 Jan 2003 13:44:55 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 18ZWnl-0005pE-00 for ; Fri, 17 Jan 2003 14:44:53 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 18ZWof-0006cb-00 for gnu-help-gnu-emacs@m.gmane.org; Fri, 17 Jan 2003 08:45:49 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!cyclone.bc.net!snoopy.risq.qc.ca!chi1.webusenet.com!c03.atl99!news.webusenet.com!telocity-west!DIRECTV!sn-xit-03!sn-xit-01!sn-xit-06!sn-post-01!supernews.com!corp.supernews.com!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.090007 (Oort Gnus v0.07) Emacs/21.2 (powerpc-apple-darwin) Cancel-Lock: sha1:hgwXdpwopXzWNT7TlgEcDd9yYKU= Original-X-Complaints-To: abuse@supernews.com Original-Lines: 117 Original-Xref: shelby.stanford.edu gnu.emacs.help:109072 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:5600 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:5600 --=-=-= >>>>> "tim" == Tim Morley \(remove vegetable for email address\) writes: tim> Does anybody know of a way to include an incrementing number in a tim> regexp search-and-replace in Emacs? What I want to do is search tim> for a regexp, and the first time I find it, replace it with "case tim> 1", the second time replace it with "case 2", etc. Here's a function that I wrote a while ago...almost what you want. When run interactively, gives something like: Replace [n] skip [1] case1 [2] case2 [3] case3? Press [n] to skip to next match, [1-3] to replace with the case1-3, [return] to quit. Could be modified pretty easily to autoincrement a value in the replace string. ...cj --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=replace.el Content-Transfer-Encoding: 8bit Content-Description: query-replace-regexp-select (defun query-replace-regexp-select (from to-list) "(query-replace-regexp REGEXP TO) Replace some things after point matching REGEXP. If TO is a single string, replacement is TO. If TO is a list, user may specify which element of TO by specifying 1-n where n is the number of elements in the list. Run interactively, asks for REGEXP, then queries user for multiple TO replacement strings. Enter an empty replacement string to finish the list." (interactive (list (read-string "Query replace select regexp: ") (let ((count 1) str ) (setq to-list nil) (while (not (string= (setq str (read-string (format "With[%d]: " count))) "")) (setq to-list (append to-list (list str))) (setq count (1+ count)) ) to-list ))) (unwind-protect (let (key match msg count width to-alist (to-chars "123456789abcdefghijklmop") (quit nil)) (setq count 1) (setq width (/ (window-width) (length to-list))) (mapcar (lambda (x) (setq msg (concat msg (format "[%d] %s%s " count (substring x 0 (min width (length x))) (if (> (length x) width) "..." "") ))) (setq to-alist (append to-alist (list (list (string-to-char (substring to-chars (1- count) count)) x)))) (setq count (1+ count))) to-list) (set-mark (point)) (while (and (not quit) (re-search-forward from nil t)) (replace-highlight (match-beginning 0) (match-end 0)) (message "Replace [n] skip %s" msg) (setq key (read-char)) (cond ((setq match (assoc key to-alist)) (replace-match (cadr match))) ((eq ? key) (setq quit t)) ((eq ?, key) (setq key (read-char)) (if (setq match (assoc key to-alist)) (progn (replace-match (cadr match)) (setq key (read-char)) (if (eq ? key) (setq quit t))) ) ) ) ) ) (replace-dehighlight) ) ) --=-=-= -- ------------------------------------------------------------------------------ Christopher J. White chris@grierwhite.com ------------------------------------------------------------------------------ --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit _______________________________________________ Help-gnu-emacs mailing list Help-gnu-emacs@gnu.org http://mail.gnu.org/mailman/listinfo/help-gnu-emacs --=-=-=--