[Top][All Lists]

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

Re: copy-region-to-variable

From: Gareth Rees
Subject: Re: copy-region-to-variable
Date: 4 Feb 2004 11:47:44 -0800

Hans Nieuwenhuizen wrote:
> None of the almost 2000 built-in functions of emacs 21.2 seems to copy
> part of the buffer (region) into a variable.

As others have said, 'buffer-substring' gives you a substring of a
buffer.  However, you don't need that to solve your problem.

> You need that e.g. if you have to enlarge the double in the region by
> a factor of say 3.456.  Can anybody tell me how to do that?

If you want to do this interactively, use 'query-replace-regexp-eval'.
For example, to multiply each number by 3.456, type:

    M-x query-replace-regexp-eval RET
    [0-9.]+ RET
    (* 3.456 (string-to-number \0)) RET

Non-interactively, you'd write Lisp like this:

    (while (re-search-forward "[0-9]+\\(\\.[0-9]+\\)?" nil t)
        (number-to-string (* 3.456 (string-to-number (match-string 0))))))

(Note that you need to be a bit more careful with the regexp for numbers
when you're not interactive, and you need 'number-to-string'.)

Gareth Rees

reply via email to

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