all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Marc Mientki <mientki@nonet.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How modify numbers in a region by a multiplier?
Date: Thu, 01 Jul 2010 16:19:30 +0200	[thread overview]
Message-ID: <i0i85j$vqf$1@news.onet.pl> (raw)
In-Reply-To: <mailman.0.1277992530.1006.help-gnu-emacs@gnu.org>

Am 01.07.2010 15:55, schrieb Seweryn Kokot:
 > 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)))
 >
 >

I use for such purposes macro-math.el from Nikolaj Schumacher
(http://nschum.de/src/emacs/macro-math/) and bind so:

(global-set-key "\C-x~" 'macro-math-eval-and-round-region)
(global-set-key "\C-x=" 'macro-math-eval-region)

The next step is to write macro where you search begin of the numer and
set there mark at this position, then go to the end of the number (don't
worry that mark disapear - it is still there) and type mathematical
operation - in your case *0.1 - and call macro-math-eval-region with
C-x= which replace all marked text (original number and '*0.1') with the
results of the operation. When you don't wish to replace text call
macro-math-eval-region with C-u prefix. In this case the result will be
written to kill-ring and yiu can yank it on other place.

This is a very simple example of macro for your task:
(fset 'mult-by-0.1
    [?\C-  ?\C-s ?  ?\C-m left ?* ?0 ?. ?1 ?\C-x ?= ?\C-f])


HTH
regards
Marc





       reply	other threads:[~2010-07-01 14:19 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.0.1277992530.1006.help-gnu-emacs@gnu.org>
2010-07-01 14:19 ` Marc Mientki [this message]
2010-07-01 15:55 ` How modify numbers in a region by a multiplier? Andreas Politz
2010-07-01 16:45   ` Pascal J. Bourguignon
2010-07-01 13:55 Seweryn Kokot
2010-07-01 14:16 ` Juanma Barranquero
2010-07-01 14:31   ` Seweryn Kokot
2010-07-01 15:56     ` Juanma Barranquero
2010-07-01 20:10       ` Seweryn Kokot
2010-07-01 20:16         ` Juanma Barranquero
2010-07-01 20:36           ` Seweryn Kokot
2010-07-01 22:38             ` Juanma Barranquero
2010-07-02  6:41               ` Seweryn Kokot
2010-07-02  9:33                 ` Juanma Barranquero
2010-07-02  9:53                   ` Seweryn Kokot
2010-07-02 10:05                     ` Juanma Barranquero
2010-07-02 18:15                       ` Štěpán Němec
2010-07-02 20:30                         ` Juanma Barranquero
2010-07-05  9:49                           ` Seweryn Kokot
2010-07-05 11:46                             ` Juanma Barranquero
2010-07-05 13:40                               ` Seweryn Kokot
     [not found]                             ` <mailman.4.1278330410.9916.help-gnu-emacs@gnu.org>
2010-07-05 13:30                               ` David Kastrup
     [not found]           ` <mailman.2.1278016589.8594.help-gnu-emacs@gnu.org>
2010-07-01 20:42             ` David Kastrup
2010-07-01 21:03             ` Andreas Politz
2010-07-01 15:01   ` Dan Davison
2010-07-01 20:16     ` Seweryn Kokot
     [not found] ` <mailman.0.1277993821.20412.help-gnu-emacs@gnu.org>
2010-07-01 14:19   ` David Kastrup
2010-07-01 16:54 ` Qiang Guo
     [not found] ` <mailman.1.1278003297.20718.help-gnu-emacs@gnu.org>
2010-07-01 18:54   ` Pascal J. Bourguignon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='i0i85j$vqf$1@news.onet.pl' \
    --to=mientki@nonet.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.