* Operate on region string
@ 2006-09-18 9:59 Hadron Quark
2006-09-18 10:37 ` Tassilo Horn
2006-09-18 11:13 ` Pascal Bourguignon
0 siblings, 2 replies; 13+ messages in thread
From: Hadron Quark @ 2006-09-18 9:59 UTC (permalink / raw)
Whats the "de-facto" recommended way to work on a region?
I guess the sequence is something like
(setq t (region-text))
(setq t (dowork-on-text((t))))
(replace-region(t))
What is the correct approach?
--
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 9:59 Operate on region string Hadron Quark
@ 2006-09-18 10:37 ` Tassilo Horn
2006-09-18 12:28 ` Hadron Quark
2006-09-18 11:13 ` Pascal Bourguignon
1 sibling, 1 reply; 13+ messages in thread
From: Tassilo Horn @ 2006-09-18 10:37 UTC (permalink / raw)
Hadron Quark <hadronquark@gmail.com> writes:
Hi Hadron,
> Whats the "de-facto" recommended way to work on a region?
>
> I guess the sequence is something like
>
> (setq t (region-text))
> (setq t (dowork-on-text((t))))
> (replace-region(t))
>
> What is the correct approach?
Here's a samplte function which operates on a region. It reverses the
lines of the region, boxquotes it and titles the boxquote (needs
boxquote.el):
+-------------------------------+
|The beginning and end positions|
|of the marked region. |
+-------------------------------+
X
/ \
/ \
(defun reverse-boxquote-region (BEG END)
(interactive "r") ;; <-- "r" means: "Operate on a region"
;; see C-h f interactive RET
(reverse-region BEG END)
(boxquote-region BEG END)
(boxquote-title "Reversed and boxquoted region"))
Bye,
Tassilo
--
"Mein Baby bekommt nur Trockenmilch, da brauche ich hinterher nur
abzustauben."
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 9:59 Operate on region string Hadron Quark
2006-09-18 10:37 ` Tassilo Horn
@ 2006-09-18 11:13 ` Pascal Bourguignon
2006-09-18 12:21 ` Hadron Quark
1 sibling, 1 reply; 13+ messages in thread
From: Pascal Bourguignon @ 2006-09-18 11:13 UTC (permalink / raw)
Hadron Quark <hadronquark@gmail.com> writes:
> Whats the "de-facto" recommended way to work on a region?
>
> I guess the sequence is something like
>
> (setq t (region-text))
> (setq t (dowork-on-text((t))))
> (replace-region(t))
>
> What is the correct approach?
1- DO NOT ASSIGN to CONSTANTS!
2- Know your constants: NIL, T, the keywords [starting with ':'],
and anything else declared with defconst.
3- Know your programming language and your library functions. There
is no function named t [so you cannot call it with (t)], and in
emacs lisp, which is a lisp-2, contrarily to scheme which is a
lisp-1, it's meaning less for the operator in a form to be anything
else than a symbol or a lambda expression [therefore, no ((f))].
4- There is no region-text function in emacs. You can use
buffer-substring or buffer-substring-no-properties.
5- There is no function replace-region in emacs. You can use
delete-region and insert.
But if you want the best performances, emacs is optimized to work in
the buffers, not on strings. So if you can do it, better to directly
modify the buffer:
(defun my-command (start end)
(interactive "r")
(do-work-on-region start end))
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Indentation! -- I will show you how to indent when I indent your skull!"
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 11:13 ` Pascal Bourguignon
@ 2006-09-18 12:21 ` Hadron Quark
2006-09-19 9:07 ` Pascal Bourguignon
0 siblings, 1 reply; 13+ messages in thread
From: Hadron Quark @ 2006-09-18 12:21 UTC (permalink / raw)
Pascal Bourguignon <pjb@informatimago.com> writes:
> Hadron Quark <hadronquark@gmail.com> writes:
>
>> Whats the "de-facto" recommended way to work on a region?
>>
>> I guess the sequence is something like
>>
>> (setq t (region-text))
>> (setq t (dowork-on-text((t))))
>> (replace-region(t))
>>
>> What is the correct approach?
>
> 1- DO NOT ASSIGN to CONSTANTS!
I should have but "text"...
>
> 2- Know your constants: NIL, T, the keywords [starting with ':'],
> and anything else declared with defconst.
it was just pseudo code : but I take your point.
>
> 3- Know your programming language and your library functions. There
> is no function named t [so you cannot call it with (t)], and in
typo : see above.
> emacs lisp, which is a lisp-2, contrarily to scheme which is a
> lisp-1, it's meaning less for the operator in a form to be anything
> else than a symbol or a lambda expression [therefore, no ((f))].
>
> 4- There is no region-text function in emacs. You can use
> buffer-substring or buffer-substring-no-properties.
It really wasnt code : see above ... :-;
>
> 5- There is no function replace-region in emacs. You can use
> delete-region and insert.
>
> But if you want the best performances, emacs is optimized to work in
> the buffers, not on strings. So if you can do it, better to directly
> modify the buffer:
>
> (defun my-command (start end)
> (interactive "r")
> (do-work-on-region start end))
this is what I wanted to know : what is the best way to "do-work-on-region"?
--
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 10:37 ` Tassilo Horn
@ 2006-09-18 12:28 ` Hadron Quark
2006-09-18 12:55 ` Tassilo Horn
2006-09-18 13:02 ` Mathias Dahl
0 siblings, 2 replies; 13+ messages in thread
From: Hadron Quark @ 2006-09-18 12:28 UTC (permalink / raw)
Tassilo Horn <heimdall@uni-koblenz.de> writes:
> Hadron Quark <hadronquark@gmail.com> writes:
>
> Hi Hadron,
>
>> Whats the "de-facto" recommended way to work on a region?
>>
>> I guess the sequence is something like
>>
>> (setq t (region-text))
>> (setq t (dowork-on-text((t))))
>> (replace-region(t))
>>
>> What is the correct approach?
>
> Here's a samplte function which operates on a region. It reverses the
> lines of the region, boxquotes it and titles the boxquote (needs
> boxquote.el):
>
> +-------------------------------+
> |The beginning and end positions|
> |of the marked region. |
> +-------------------------------+
> X
> / \
> / \
> (defun reverse-boxquote-region (BEG END)
> (interactive "r") ;; <-- "r" means: "Operate on a region"
> ;; see C-h f interactive RET
> (reverse-region BEG END)
> (boxquote-region BEG END)
> (boxquote-title "Reversed and boxquoted region"))
Thanks Tassilo - but this isnt showing me how to actually do anything
myself with the text string contained by the region if you get my
meaning.
I tried reading the code in boxquote, but there are too many function
calls not documented.
Maybe you could help?
I want something like this to work on a buffer region
(defun remove-vowels-region(te)
"Remove the vowels"
(interactive "sTe:")
(replace-regexp-in-string "[aeiouAEIOU]" "" te)
)
I hilite some text and then call remove-vowels-in-region.
A first step would be sufficient to get me on my way : I'm not a lisp
programmer but am learning a little.
>
> Bye,
> Tassilo
--
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 12:28 ` Hadron Quark
@ 2006-09-18 12:55 ` Tassilo Horn
2006-09-18 15:24 ` Hadron Quark
2006-09-18 13:02 ` Mathias Dahl
1 sibling, 1 reply; 13+ messages in thread
From: Tassilo Horn @ 2006-09-18 12:55 UTC (permalink / raw)
Hadron Quark <hadronquark@gmail.com> writes:
Hi Hadron,
> I want something like this to work on a buffer region
>
> (defun remove-vowels-region(te)
> "Remove the vowels"
> (interactive "sTe:")
> (replace-regexp-in-string "[aeiouAEIOU]" "" te)
> )
Try that:
(defun remove-vowels-region (start end)
(interactive "r")
(replace-regexp "[aeiouAEIOU]" "" nil start end))
Bye,
Tassilo
--
[Emacs] is written in Lisp, which is the only computer language that is
beautiful. -- Neal Stephenson, _In the Beginning was the Command Line_
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 12:28 ` Hadron Quark
2006-09-18 12:55 ` Tassilo Horn
@ 2006-09-18 13:02 ` Mathias Dahl
2006-09-18 13:10 ` Hadron Quark
1 sibling, 1 reply; 13+ messages in thread
From: Mathias Dahl @ 2006-09-18 13:02 UTC (permalink / raw)
Hadron Quark <hadronquark@gmail.com> writes:
> I want something like this to work on a buffer region
>
> (defun remove-vowels-region(te)
> "Remove the vowels"
> (interactive "sTe:")
> (replace-regexp-in-string "[aeiouAEIOU]" "" te)
> )
>
> I hilite some text and then call remove-vowels-in-region.
What about this:
(defun remove-vowels-in-region (beg end)
(interactive "r")
(save-excursion
(goto-char beg)
(while (search-forward-regexp "[aeiouAEIOU]" end nil)
(replace-match ""))))
/Mathias
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 13:02 ` Mathias Dahl
@ 2006-09-18 13:10 ` Hadron Quark
2006-09-19 22:20 ` John Sullivan
0 siblings, 1 reply; 13+ messages in thread
From: Hadron Quark @ 2006-09-18 13:10 UTC (permalink / raw)
Mathias Dahl <brakjoller@gmail.com> writes:
> Hdrn Qrk <hdrnqrk@gml.cm> wrts:
>
>> wnt smthng lk ths t wrk n bffr rgn
>>
>> (dfn rmv-vwls-rgn(t)
>> "Rmv th vwls"
>> (ntrctv "sT:")
>> (rplc-rgxp-n-strng "[]" "" t)
>> )
>>
>> hlt sm txt nd thn cll rmv-vwls-n-rgn.
>
> Wht bt ths:
>
> (dfn rmv-vwls-n-rgn (bg nd)
> (ntrctv "r")
> (sv-xcrsn
> (gt-chr bg)
> (whl (srch-frwrd-rgxp "[]" nd nl)
> (rplc-mtch ""))))
>
> /Mths
Wrks grt thnks : 'll stp thrgh nd lrn frm t - thnks lt.
:)
--
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 12:55 ` Tassilo Horn
@ 2006-09-18 15:24 ` Hadron Quark
2006-09-20 10:14 ` robert.thorpe
0 siblings, 1 reply; 13+ messages in thread
From: Hadron Quark @ 2006-09-18 15:24 UTC (permalink / raw)
Tassilo Horn <heimdall@uni-koblenz.de> writes:
> Hadron Quark <hadronquark@gmail.com> writes:
>
> Hi Hadron,
>
>> I want something like this to work on a buffer region
>>
>> (defun remove-vowels-region(te)
>> "Remove the vowels"
>> (interactive "sTe:")
>> (replace-regexp-in-string "[aeiouAEIOU]" "" te)
>> )
>
> Try that:
>
> (defun remove-vowels-region (start end)
> (interactive "r")
> (replace-regexp "[aeiouAEIOU]" "" nil start end))
>
> Bye,
> Tassilo
vn shrtr nd swtr!
(even shorter and sweeter)
thanks!
--
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 12:21 ` Hadron Quark
@ 2006-09-19 9:07 ` Pascal Bourguignon
2006-10-14 22:23 ` David Combs
0 siblings, 1 reply; 13+ messages in thread
From: Pascal Bourguignon @ 2006-09-19 9:07 UTC (permalink / raw)
Hadron Quark <hadronquark@gmail.com> writes:
>> But if you want the best performances, emacs is optimized to work in
>> the buffers, not on strings. So if you can do it, better to directly
>> modify the buffer:
>>
>> (defun my-command (start end)
>> (interactive "r")
>> (do-work-on-region start end))
>
> this is what I wanted to know : what is the best way to "do-work-on-region"?
You need to learn the emacs lisp functions, notably those who work on
the buffers and on the strings.
Most of the functions that work on the buffers are actually commands
that are also bound to some keys and that you use.
For example, if you want to remove every other character in the region,
you can do it manually with C-f C-d C-f C-d ...
You can know what functions are called when you type C-f or C-d:
C-h k C-f
C-h k C-d
You need to know also the current position of the cursor. It's given
by the function point.
Then you can write a loop:
(defun delete-every-other-character (start end)
(interactive "r")
(goto-char start)
(while (< (point) end)
(forward-char 1)
(delete-char 1)
(setf end (1- end))))
The best, to know all these functions is to read the emacs lisp manual.
Execute these forms:
(info "Emacs-lisp-intro")
(info "Elisp")
Type:
(info "Elisp") C-x C-e
in the buffer *scratch*
--
__Pascal Bourguignon__ http://www.informatimago.com/
READ THIS BEFORE OPENING PACKAGE: According to certain suggested
versions of the Grand Unified Theory, the primary particles
constituting this product may decay to nothingness within the next
four hundred million years.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 13:10 ` Hadron Quark
@ 2006-09-19 22:20 ` John Sullivan
0 siblings, 0 replies; 13+ messages in thread
From: John Sullivan @ 2006-09-19 22:20 UTC (permalink / raw)
Hadron Quark <hadronquark@gmail.com> writes:
> Wrks grt thnks : 'll stp thrgh nd lrn frm t - thnks lt.
Depending on how many or what kind of operations you want to do on a region,
you may find it helpful to narrow to the region in the buffer that you are
operating on.
See narrow-to-region.
--
-John Sullivan
-http://www.wjsullivan.net
-GPG Key: AE8600B6
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-18 15:24 ` Hadron Quark
@ 2006-09-20 10:14 ` robert.thorpe
0 siblings, 0 replies; 13+ messages in thread
From: robert.thorpe @ 2006-09-20 10:14 UTC (permalink / raw)
Hadron Quark wrote:
> Tassilo Horn <heimdall@uni-koblenz.de> writes:
>
> > Hadron Quark <hadronquark@gmail.com> writes:
> >
> > Hi Hadron,
> >
> >> I want something like this to work on a buffer region
> >>
> >> (defun remove-vowels-region(te)
> >> "Remove the vowels"
> >> (interactive "sTe:")
> >> (replace-regexp-in-string "[aeiouAEIOU]" "" te)
> >> )
> >
> > Try that:
> >
> > (defun remove-vowels-region (start end)
> > (interactive "r")
> > (replace-regexp "[aeiouAEIOU]" "" nil start end))
> >
> > Bye,
> > Tassilo
>
> vn shrtr nd swtr!
>
> (even shorter and sweeter)
As a sidenote, you look at the problem like this:
* Is there a function that works on a region I can use for my purpose.
If so it is simple like the above.
* If not, maybe there is a function that works on the whole region. In
this case I can use M-x widen and M-x narrow-to-region to perform an
action on a region.
* If that doesn't work (which is very unlikely) then convert the region
to a string and operate on the string.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Operate on region string
2006-09-19 9:07 ` Pascal Bourguignon
@ 2006-10-14 22:23 ` David Combs
0 siblings, 0 replies; 13+ messages in thread
From: David Combs @ 2006-10-14 22:23 UTC (permalink / raw)
In article <87lkogz0kj.fsf@thalassa.informatimago.com>,
Pascal Bourguignon <pjb@informatimago.com> wrote:
>Hadron Quark <hadronquark@gmail.com> writes:
>>> But if you want the best performances, emacs is optimized to work in
>>> the buffers, not on strings. So if you can do it, better to directly
>>> modify the buffer:
>>>
>>> (defun my-command (start end)
>>> (interactive "r")
>>> (do-work-on-region start end))
>>
>> this is what I wanted to know : what is the best way to "do-work-on-region"?
>
>You need to learn the emacs lisp functions, notably those who work on
>the buffers and on the strings.
>
>Most of the functions that work on the buffers are actually commands
>that are also bound to some keys and that you use.
>
>For example, if you want to remove every other character in the region,
>you can do it manually with C-f C-d C-f C-d ...
>
>You can know what functions are called when you type C-f or C-d:
>
> C-h k C-f
> C-h k C-d
>
Easy way to do that: define the seq as a macro,
then do "insert-macro" (or whatever it's called) --
that code shows the function-NAMES too.
David
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2006-10-14 22:23 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-09-18 9:59 Operate on region string Hadron Quark
2006-09-18 10:37 ` Tassilo Horn
2006-09-18 12:28 ` Hadron Quark
2006-09-18 12:55 ` Tassilo Horn
2006-09-18 15:24 ` Hadron Quark
2006-09-20 10:14 ` robert.thorpe
2006-09-18 13:02 ` Mathias Dahl
2006-09-18 13:10 ` Hadron Quark
2006-09-19 22:20 ` John Sullivan
2006-09-18 11:13 ` Pascal Bourguignon
2006-09-18 12:21 ` Hadron Quark
2006-09-19 9:07 ` Pascal Bourguignon
2006-10-14 22:23 ` David Combs
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.