From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: How modify numbers in a region by a multiplier? Date: Thu, 01 Jul 2010 20:54:02 +0200 Organization: Informatimago Message-ID: <87lj9vxcc5.fsf@kuiper.lan.informatimago.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1291843386 6323 80.91.229.12 (8 Dec 2010 21:23:06 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 8 Dec 2010 21:23:06 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 08 22:23:02 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 1PQRTX-0005jU-Co for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 22:23:00 +0100 Original-Received: from localhost ([127.0.0.1]:36399 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQR6G-0002LU-Mt for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 15:58:56 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 69 Original-X-Trace: individual.net c3uz5MNok/D7S4mZVNyMeA/hnxu5bK2Xw379+Ehaxy+dGdD9J/ Cancel-Lock: sha1:NzYwMTgwOTc3MWE5MDA0N2M3ZGIwOGE0N2VhOWE0NjcxZjM5OWY1Nw== sha1:lsptLIKRTFzOirQE1Q7Fd3YK88o= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.101 (Gnus v5.10.10) Emacs/23.2 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:179422 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:76038 Archived-At: Qiang Guo writes: > At Thu, 1 Jul 2010 13:55:11 +0000 (UTC), > Seweryn Kokot wrote: >> >> Hi, >> >> I would like to modify numbers in a region/buffer by a given multiplier. >> >> Imagine that I have some numbers >> >> 33.444 3333 4433.4443 3344 .34234 >> >> and I want them multiplied for example by 0.1 >> >> to receive >> >> 3.3444 333.3 443.34443 334.4 .034234 >> >> I tried with this function I wrote, but it doesn't work properly. >> Any idea why? >> >> (defun my-multiply-numbers-in-region-or-buffer (multiplier) >> (interactive "nGive multiplier: ") >> (let (beg end object) >> (if (use-region-p) >> (progn >> (setq object "region") >> (setq beg (region-beginning)) >> (setq end (region-end))) >> (setq object "buffer") >> (setq beg (point-min)) >> (setq end (point-max))) >> (goto-char beg) >> (while (re-search-forward "\\([0-9]*\\.?[0-9]*\\)" end t) >> (replace-match (format "%.3f" (* (string-to-number (match-string 1)) >> multiplier)))) >> (message "Numbers in %s modified by multiplier %s." object >> multiplier))) >> >> > > > > Hi, > > The problem is the re-search-forward. It should be > dynamically changed according to the replace events, since > the (point) would exceed the variable end after replacement. Obviously NOT! The point is a marker, not an integer. (with-temp-buffer (insert "aaa-bbb-ccc") (goto-char 0) (when (re-search-forward "\\(b+\\)") (replace-match "xxxxxx") (insert "|")) (buffer-substring (point-min) (point-max))) --> "aaa-xxxxxx|-ccc" -- __Pascal Bourguignon__ http://www.informatimago.com/