[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How modify numbers in a region by a multiplier?
From: |
Andreas Politz |
Subject: |
Re: How modify numbers in a region by a multiplier? |
Date: |
Wed, 08 Dec 2010 15:16:57 -0000 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) |
Seweryn Kokot <sewkokot@gmail.com> writes:
> Juanma Barranquero <lekktu <at> 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
- Re: How modify numbers in a region by a multiplier?, David Kastrup, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, Marc Mientki, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, Andreas Politz, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, Pascal J. Bourguignon, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, David Kastrup, 2010/12/08
- Re: How modify numbers in a region by a multiplier?,
Andreas Politz <=
- Re: How modify numbers in a region by a multiplier?, David Kastrup, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, David Kastrup, 2010/12/08