From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas Politz Newsgroups: gmane.emacs.help Subject: Re: How modify numbers in a region by a multiplier? Date: Thu, 01 Jul 2010 23:03:12 +0200 Organization: FH-Trier Message-ID: <87aaqaewz3.fsf@fh-trier.de> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1291891516 1581 80.91.229.12 (9 Dec 2010 10:45:16 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 9 Dec 2010 10:45:16 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 09 11:45:06 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PQdzi-0006M3-8A for geh-help-gnu-emacs@m.gmane.org; Thu, 09 Dec 2010 11:45:02 +0100 Original-Received: from localhost ([127.0.0.1]:59438 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQRBL-0004c5-MV for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 16:04:11 -0500 Original-Path: usenet.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!kanaga.switch.ch!switch.ch!news.belwue.de!news.uni-kl.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 35 Original-NNTP-Posting-Host: 143-93-54-11.arno.fh-trier.de Original-X-Trace: news.uni-kl.de 1278018192 7716 143.93.54.11 (1 Jul 2010 21:03:12 GMT) Original-X-Complaints-To: usenet@news.uni-kl.de Original-NNTP-Posting-Date: Thu, 1 Jul 2010 21:03:12 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) Cancel-Lock: sha1:4y+JTp4kN4lIKC0sJRAU/gzkSA0= Cache-Post-Path: arno.fh-trier.de!unknown@dslb-084-059-210-115.pools.arcor-ip.net X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) Original-Xref: usenet.stanford.edu gnu.emacs.help:179431 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:76966 Archived-At: Seweryn Kokot writes: > Juanma Barranquero gmail.com> writes: > >> Qiang Guo is right, though, in pointing that you're using a fixed >> `end' but the buffer or region length is potentially changing with >> every replace. > > Is there a canonical solution to this problem, since it seems that it should > happen frequently when using the following structure: > (while (re-search-forward "\\([0-9.]+\\)" end t) > (replace-match (format "%.2f" (* (string-to-number (match-string 1)) > multiplier)))) > Yes, use a marker. (defun my-multiply-numbers-in-region-or-buffer (multiplier beg end) (interactive (cons (read-number "Specify a multiplier: " 0.001) (if (use-region-p) (list (region-beginning) (region-end)) (list (point-min) (point-max))))) (goto-char beg) (let ((end (copy-marker end))) (while (re-search-forward "\\([0-9.]+\\)" end t) (replace-match (format "%.2f" (* (string-to-number (match-string 1)) multiplier))))) (when (interactive-p) (message "Numbers in %s modified by multiplier %s." (if (use-region-p) "region" "buffer") multiplier))) -ap