From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Seweryn Kokot Newsgroups: gmane.emacs.help Subject: Re: How modify numbers in a region by a multiplier? Date: Thu, 1 Jul 2010 20:36:05 +0000 (UTC) Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1278016627 5161 80.91.229.12 (1 Jul 2010 20:37:07 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 1 Jul 2010 20:37:07 +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 Jul 01 22:37: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 1OUQVN-00055H-0S for geh-help-gnu-emacs@m.gmane.org; Thu, 01 Jul 2010 22:37:05 +0200 Original-Received: from localhost ([127.0.0.1]:60620 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OUQVM-0000nQ-7V for geh-help-gnu-emacs@m.gmane.org; Thu, 01 Jul 2010 16:37:04 -0400 Original-Received: from [140.186.70.92] (port=32893 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OUQUg-0000lL-K8 for help-gnu-emacs@gnu.org; Thu, 01 Jul 2010 16:36:27 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OUQUb-0001Ku-LA for help-gnu-emacs@gnu.org; Thu, 01 Jul 2010 16:36:22 -0400 Original-Received: from lo.gmane.org ([80.91.229.12]:36122) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OUQUb-0001Ki-9w for help-gnu-emacs@gnu.org; Thu, 01 Jul 2010 16:36:17 -0400 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1OUQUZ-0004hr-2t for help-gnu-emacs@gnu.org; Thu, 01 Jul 2010 22:36:15 +0200 Original-Received: from 94.162.35.238 ([94.162.35.238]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 01 Jul 2010 22:36:15 +0200 Original-Received: from sewkokot by 94.162.35.238 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 01 Jul 2010 22:36:15 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 45 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 94.162.35.238 (Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.1.10) Gecko/20100504 Firefox/3.5.10) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) 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:74037 Archived-At: 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)))) I tried this in a buffer with just "1 4 5 5 4 5 4" and specifying the multiplier of 0.001 gives Debugger entered--Lisp error: (error "Invalid search bound (wrong side of point)") re-search-forward("\\([0-9.]+\\)" 14 t) ... or modifies only a part of the buffer if the content of the buffer is "1 4 5 5 4 5 4 " (just adding one space). Then using my function gives 0.00 0.00 0.01 5 4 5 4 P.S. The function now is as follows: (defun my-multiply-numbers-in-region-or-buffer (multiplier) (interactive "nSpecify a multiplier (default is 0.001): ") ;; if nil - RET multiplier is 0.001 (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))) (unless multiplier (setq multiplier 0.001)) (goto-char beg) (while (re-search-forward "\\([0-9.]+\\)" end t) (replace-match (format "%.2f" (* (string-to-number (match-string 1)) multiplier)))) (message "Numbers in %s modified by multiplier %s." object multiplier)))