help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How modify numbers in a region by a multiplier?


From: David Kastrup
Subject: Re: How modify numbers in a region by a multiplier?
Date: Wed, 08 Dec 2010 15:17:20 -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:
>
>> > I see nothing in the docstrings that would suggest that (unless you use
>> > `narrow-to-region', obviously -- which the original function discussed
>> > in this thread did *not* use, although it would have been the right
>> > thing to do IIRC).
>> 
>> Yes, of course you're right. I was talking about using narrow-to-region.
>
> During the weekend I rethought the function I wrote with your help, now I 
> have 
> two versions - one without narrow-to-region and markers and the other with 
> narrow-to-region functions and without markers. Can we say that the second 
> version is more elispy (more elegant and safer) than the first one? 

No.  It's half-baked.  There is no point in using &optional if giving
nil values is going to cause errors.  There is no point in using a Lisp
interactive form where you can use a normal interactive string.  There
is no point of using variables beg and end if you can just do

   (save-restriction
     (when (use-region-p)
        (narrow-to-region (region-beginning) (region-end)))
     ...

> ;; second version
> (defun my-multiply-numbers-in-region-or-buffer (&optional multiplier float-
> points)
>   (interactive
>    (list
>       (read-number "Specify a multiplier: " 0.001)
>       (read-number "Specify the number of significant figures: " 3)))
>   (let (beg end object end-mark)
>       (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)))
>       (save-restriction
>         (narrow-to-region beg end)
> ;       (setq end-mark (point-max-marker))
>         (goto-char (point-min))
> ;       (while (re-search-forward "\\([0-9.]+\\)" end-mark t)
>         (while (re-search-forward "\\([0-9.]+\\)" nil t)
>         (replace-match (format (concat "%." (number-to-string float-
> points) "f" )  (* (string-to-number (match-string 1)) multiplier))))
>       (message "Numbers in %s modified by multiplier %s." object 
> multiplier))))

Something like the following (I ran out of steam, it is incomplete)

(defun my-multiply-numbers-in-region-or-buffer (&optional multiplier 
float-points)
  (interactive "nn\nSpecify a multiplier: \nSpecify the number of significant 
figures: ")
  (unless multiplier (setq multiplier 0.001))
  (unless float-points (setq float-points 3))
  (let ((object "buffer))
    (save-restriction
      (when (use-region-p)
        (setq object "region")
        (narrow-to-region (region-beginning) (region-end)))
      (goto-char (point-min))
      (while (re-search-forward "\\([0-9.]+\\)" nil t)
      (replace-match (format (format "%%.%df" float-points) (* 
(string-to-number (match-string 1)) multiplier))))
        (message "Numbers in %s modified by multiplier %s." object 
multiplier))))


-- 
David Kastrup


reply via email to

[Prev in Thread] Current Thread [Next in Thread]