unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* copy-region-to-variable
@ 2004-02-04 13:06 hans nieuwenhuizen
  2004-02-04 13:14 ` copy-region-to-variable Joakim Hove
  2004-02-04 19:47 ` copy-region-to-variable Gareth Rees
  0 siblings, 2 replies; 4+ messages in thread
From: hans nieuwenhuizen @ 2004-02-04 13:06 UTC (permalink / raw)


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

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?



--
----------------------------------------------------------------------------
Prof. Ir. j.k. nieuwenhuizen                 email: hans@nieuwenhuizen-jk.nl
dom Paul Bellotweg 8
5624 KZ Eindhoven - NL

Tel [{++31|0}40]2442226
----------------------------------------------------------------------------
-

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: copy-region-to-variable
  2004-02-04 13:06 copy-region-to-variable hans nieuwenhuizen
@ 2004-02-04 13:14 ` Joakim Hove
  2004-02-04 13:50   ` copy-region-to-variable Johan Bockgård
  2004-02-04 19:47 ` copy-region-to-variable Gareth Rees
  1 sibling, 1 reply; 4+ messages in thread
From: Joakim Hove @ 2004-02-04 13:14 UTC (permalink / raw)



"hans nieuwenhuizen" <j.k.nieuwenhuizen@tue.nl> writes:

> None of the almost 2000 built-in functions of emacs 21.2 seems to copy part
> of the buffer ( region ) into a variable.
>
> You need that e.g. if you have to enlarge the double in the region by a
> factor of
> say 3.456 .

If you just mean the text content of the buffer you can use

(buffer-substring p1 p2)

or

(buffer-substring-no-properties p1 p2) 

which return the buffer-content between p1 and p2.

HTH Joakim

Untested:
---------

(defun enlarge-double (p1 p2)
  (interactive "r")
  (let  ((new-number    (* 3.456 (string-to-number  (buffer-substring-no-properties p1 p2)))))
     (kill-region p1 p2)
     (goto-char p1)
     (insert (format "%s" new-number))))   
         

-- 
  /--------------------------------------------------------------------\
 / Joakim Hove  / hove@bccs.no  /  (55 5) 84076       |                 \
 | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
 | CMU                                                | 5231 Paradis    |
 \ Thormøhlensgt.55, 5020 Bergen.                     | 55 91 28 18     /
  \--------------------------------------------------------------------/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: copy-region-to-variable
  2004-02-04 13:14 ` copy-region-to-variable Joakim Hove
@ 2004-02-04 13:50   ` Johan Bockgård
  0 siblings, 0 replies; 4+ messages in thread
From: Johan Bockgård @ 2004-02-04 13:50 UTC (permalink / raw)


Joakim Hove <hove@bccs.no> writes:

>      (kill-region p1 p2)

(delete-region p1 p2) ; thou shalt not (mess with the) kill (ring)

Also consider:

     - Function: delete-and-extract-region start end
    
         This function deletes the text between positions START and
         END in the current buffer, and returns a string containing
         the text just deleted.
    
         If point was inside the deleted region, its value afterward
         is START. Otherwise, point relocates with the surrounding
         text, as markers do.
    
-- 
Johan Bockgård

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: copy-region-to-variable
  2004-02-04 13:06 copy-region-to-variable hans nieuwenhuizen
  2004-02-04 13:14 ` copy-region-to-variable Joakim Hove
@ 2004-02-04 19:47 ` Gareth Rees
  1 sibling, 0 replies; 4+ messages in thread
From: Gareth Rees @ 2004-02-04 19:47 UTC (permalink / raw)


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)
      (replace-match
        (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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-02-04 19:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-02-04 13:06 copy-region-to-variable hans nieuwenhuizen
2004-02-04 13:14 ` copy-region-to-variable Joakim Hove
2004-02-04 13:50   ` copy-region-to-variable Johan Bockgård
2004-02-04 19:47 ` copy-region-to-variable Gareth Rees

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).