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: Seweryn Kokot
Subject: Re: How modify numbers in a region by a multiplier?
Date: Mon, 5 Jul 2010 09:49:08 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

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? 

;; first 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)))
        (setq end-mark (copy-marker end))
        (goto-char beg)
        (while (re-search-forward "\\([0-9.]+\\)" end-mark 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)))

;; 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))))







reply via email to

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