* A little lisp help
@ 2003-01-21 19:06 Peter Lee
2003-01-21 19:24 ` Henrik Enberg
` (2 more replies)
0 siblings, 3 replies; 13+ messages in thread
From: Peter Lee @ 2003-01-21 19:06 UTC (permalink / raw)
I'm learning lisp atm, and decided to write a macro to insert curly
braces around a region and re-indent.
So for this,
if (x)
y=0;
z=0;
w=0;
*
setting mark at begining of line y=0 and setting point at * would
produce the following.
if (x)
{
y=0;
z=0;
w=0;
}
This is what I have so far, but it doesn't work very well.
(defun curly-brace-region (mark point)
"Inserts curly braces around region and indents"
(interactive "r")
(message "point=%d, mark=%d" point mark)
(goto-char mark)
(insert-string "{")
(newline-and-indent)
(goto-char point)
(insert-string "}")
(newline-and-indent)
(fill-region mark point))
There's probably functions to make this trivial and I'm just missing
them due to lack of familiarity.
TIA.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-21 19:06 A little lisp help Peter Lee
@ 2003-01-21 19:24 ` Henrik Enberg
2003-01-21 20:13 ` Stefan Monnier <foo@acm.com>
2003-01-21 21:52 ` Peter Lee
2 siblings, 0 replies; 13+ messages in thread
From: Henrik Enberg @ 2003-01-21 19:24 UTC (permalink / raw)
Peter Lee <spam@nospam.org> writes:
> I'm learning lisp atm, and decided to write a macro to insert curly
> braces around a region and re-indent.
>
> So for this,
>
> if (x)
> y=0;
>
> z=0;
> w=0;
> *
>
> setting mark at begining of line y=0 and setting point at * would
> produce the following.
>
> if (x)
> {
> y=0;
>
> z=0;
> w=0;
> }
Something like this perhaps. The `narrow-to-region' function is very
powerful. It lets you work on a region as if it is the whole buffer.
(defun curly-brace-region (beg end)
"Inserts curly braces around region and indents."
(interactive "r")
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(insert "{\n")
(goto-char (point-max))
(insert "}\n")
(c-indent-region beg end)))
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-21 19:06 A little lisp help Peter Lee
2003-01-21 19:24 ` Henrik Enberg
@ 2003-01-21 20:13 ` Stefan Monnier <foo@acm.com>
2003-01-21 20:32 ` Peter Lee
2003-01-21 21:52 ` Peter Lee
2 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-01-21 20:13 UTC (permalink / raw)
>>>>> "Peter" == Peter Lee <spam@nospam.org> writes:
> I'm learning lisp atm, and decided to write a macro to insert curly
> braces around a region and re-indent.
[...]
> (fill-region mark point))
Shouldn't that be indent-region since you want to re-indent ?
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-21 20:13 ` Stefan Monnier <foo@acm.com>
@ 2003-01-21 20:32 ` Peter Lee
2003-01-21 20:41 ` Barry Margolin
0 siblings, 1 reply; 13+ messages in thread
From: Peter Lee @ 2003-01-21 20:32 UTC (permalink / raw)
"Stefan Monnier <foo@acm.com>"
<monnier+gnu.emacs.help/news/@flint.cs.yale.edu> writes:
> Shouldn't that be indent-region since you want to re-indent ?
Ahh yes. Thanks. My macro still seems pretty nasty. Thought there
would be a better way. It doesn't remember cursor pos or anything
like that atm.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-21 20:32 ` Peter Lee
@ 2003-01-21 20:41 ` Barry Margolin
0 siblings, 0 replies; 13+ messages in thread
From: Barry Margolin @ 2003-01-21 20:41 UTC (permalink / raw)
In article <uwukyb5vr.fsf@nospam.org>, Peter Lee <spam@nospam.org> wrote:
>"Stefan Monnier <foo@acm.com>"
><monnier+gnu.emacs.help/news/@flint.cs.yale.edu> writes:
>
>> Shouldn't that be indent-region since you want to re-indent ?
>
>Ahh yes. Thanks. My macro still seems pretty nasty. Thought there
>would be a better way. It doesn't remember cursor pos or anything
>like that atm.
Use save-excursion.
--
Barry Margolin, barmar@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-21 19:06 A little lisp help Peter Lee
2003-01-21 19:24 ` Henrik Enberg
2003-01-21 20:13 ` Stefan Monnier <foo@acm.com>
@ 2003-01-21 21:52 ` Peter Lee
2003-01-21 22:34 ` Peter Lee
2003-01-22 8:24 ` Kai Großjohann
2 siblings, 2 replies; 13+ messages in thread
From: Peter Lee @ 2003-01-21 21:52 UTC (permalink / raw)
Ok this is what I have so far.
(defun curly-brace-region (m p)
"Inserts curly braces around region and indents"
(interactive "r")
;; (message "mark=%d, point=%d" m p)
(kill-region m p)
(insert-string "{")
(newline-and-indent)
(insert-string "}")
(newline-and-indent)
(previous-line 2)
(newline-and-indent)
(yank 1)
(kill-line)
(next-line 1)
(indent-region (- m 1) (point) nil))
(global-set-key "\M-]" 'curly-brace-region)
It works great when I mark a region and type: M-x curly-brace-region.
However, it doesn't work the same way when I M-]. In this case the
first curly brace is never inserted and the formatting is way off.
Anyone know why they would behave differently ?
Thanks.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-21 21:52 ` Peter Lee
@ 2003-01-21 22:34 ` Peter Lee
2003-01-22 22:55 ` Christopher J. White
2003-01-22 8:24 ` Kai Großjohann
1 sibling, 1 reply; 13+ messages in thread
From: Peter Lee @ 2003-01-21 22:34 UTC (permalink / raw)
Peter Lee <spam@nospam.org> writes:
> It works great when I mark a region and type: M-x curly-brace-region.
> However, it doesn't work the same way when I M-]. In this case the
> first curly brace is never inserted and the formatting is way off.
> Anyone know why they would behave differently ?
I've solved my problem, the docs advocated using forward-line with
negative arg vs. previous-line from a lisp program. When I changed
that it started working correctly when bound to a key.
(defun curly-brace-region (m p)
"Inserts curly braces around region and indents"
(interactive "r")
(if mark-active
(let ((transient-mark-mode nil))
(kill-region m p)
(insert-string "{")
(newline-and-indent)
(insert-string "}")
(newline-and-indent)
(forward-line -1)
(yank 1)
(forward-line 1)
(indent-region (- m 1) (point) nil))))
Thanks for the help.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-21 22:34 ` Peter Lee
@ 2003-01-22 22:55 ` Christopher J. White
2003-01-22 23:02 ` Stefan Monnier <foo@acm.com>
2003-01-24 7:39 ` Jim Janney
0 siblings, 2 replies; 13+ messages in thread
From: Christopher J. White @ 2003-01-22 22:55 UTC (permalink / raw)
>>>>> "peter" == Peter Lee <spam@nospam.org> writes:
peter> (defun curly-brace-region (m p)
peter> "Inserts curly braces around region and indents"
peter> (interactive "r")
peter> (if mark-active
peter> (let ((transient-mark-mode nil))
peter> (kill-region m p)
peter> (insert-string "{")
peter> (newline-and-indent)
peter> (insert-string "}")
peter> (newline-and-indent)
peter> (forward-line -1)
peter> (yank 1)
peter> (forward-line 1)
peter> (indent-region (- m 1) (point) nil))))
peter> Thanks for the help.
You have to be careful when using region end-points
and inserting text. m and p above are numerical values
that reference offsets from the beginning of the file and
do not "move" when text is inserted before them.
Use markers if you really want to be able to move around
and insert text, but the best method was the previous
suggestion to narrow-to-region.
...cj
--
------------------------------------------------------------------------------
Christopher J. White chris@grierwhite.com
------------------------------------------------------------------------------
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-22 22:55 ` Christopher J. White
@ 2003-01-22 23:02 ` Stefan Monnier <foo@acm.com>
2003-01-24 7:39 ` Jim Janney
1 sibling, 0 replies; 13+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-01-22 23:02 UTC (permalink / raw)
> Use markers if you really want to be able to move around
> and insert text, but the best method was the previous
> suggestion to narrow-to-region.
Be careful with narrow-to-region, though, because it hides the
rest of the text which can prevent things from working correctly.
A good example would be indentation: don't call indent-region
from within narrowing because it might not do the right thing.
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-22 22:55 ` Christopher J. White
2003-01-22 23:02 ` Stefan Monnier <foo@acm.com>
@ 2003-01-24 7:39 ` Jim Janney
2003-01-27 14:48 ` Stefan Monnier <foo@acm.com>
1 sibling, 1 reply; 13+ messages in thread
From: Jim Janney @ 2003-01-24 7:39 UTC (permalink / raw)
Christopher J. White <chris@grierwhite.com> wrote:
> You have to be careful when using region end-points
> and inserting text. m and p above are numerical values
> that reference offsets from the beginning of the file and
> do not "move" when text is inserted before them.
>
> Use markers if you really want to be able to move around
> and insert text, but the best method was the previous
> suggestion to narrow-to-region.
Markers are the most general solution, but you can sometimes get by
without them by making changes in reverse buffer order. For example, in
this case
(goto-char p)
(insert "}\n")
(save-excursion (goto-char m) (insert "{\n"))
(indent-region m (point) nil))
Note that (interactive "r") always passes the arguments smallest first,
but for a function that might also be called from lisp code you'd want
to check that m really comes before p. I prefer to call the values
"start" and "end" since they may not be the actual point and mark.
--
Jim Janney
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-24 7:39 ` Jim Janney
@ 2003-01-27 14:48 ` Stefan Monnier <foo@acm.com>
0 siblings, 0 replies; 13+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-01-27 14:48 UTC (permalink / raw)
> Markers are the most general solution, but you can sometimes get by
> without them by making changes in reverse buffer order. For example, in
> this case
> (goto-char p)
> (insert "}\n")
> (save-excursion (goto-char m) (insert "{\n"))
> (indent-region m (point) nil))
Note that `save-excursion' uses a marker ;-)
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: A little lisp help
2003-01-21 21:52 ` Peter Lee
2003-01-21 22:34 ` Peter Lee
@ 2003-01-22 8:24 ` Kai Großjohann
1 sibling, 0 replies; 13+ messages in thread
From: Kai Großjohann @ 2003-01-22 8:24 UTC (permalink / raw)
Peter Lee <spam@nospam.org> writes:
> (kill-region m p)
> (insert-string "{")
> (newline-and-indent)
> (insert-string "}")
> (newline-and-indent)
> (previous-line 2)
> (newline-and-indent)
> (yank 1)
It doesn't make sense to kill and yank here.
--
Ambibibentists unite!
^ permalink raw reply [flat|nested] 13+ messages in thread
* RE: A little lisp help
@ 2003-01-24 16:51 Bingham, Jay
0 siblings, 0 replies; 13+ messages in thread
From: Bingham, Jay @ 2003-01-24 16:51 UTC (permalink / raw)
On Friday, January 24, 2003 1:39 AM Jim Janney wrote:
>Christopher J. White <chris@grierwhite.com> wrote:
>
>> You have to be careful when using region end-points
>> and inserting text. m and p above are numerical values
>> that reference offsets from the beginning of the file and
>> do not "move" when text is inserted before them.
>>
>> Use markers if you really want to be able to move around
>> and insert text, but the best method was the previous
>> suggestion to narrow-to-region.
>
>Markers are the most general solution, but you can sometimes get by
>without them by making changes in reverse buffer order. For example,
in
>this case
>
> (goto-char p)
> (insert "}\n")
> (save-excursion (goto-char m) (insert "{\n"))
> (indent-region m (point) nil))
>
>Note that (interactive "r") always passes the arguments smallest first,
>but for a function that might also be called from lisp code you'd want
>to check that m really comes before p. I prefer to call the values
>"start" and "end" since they may not be the actual point and mark.
Markers are indeed a general solution, however, there are some
circumstances where they do not work as advertized. In Emacs 20.4 I
found that the replace-match function does not update markers. (I have
not tried it in Emacs 21).
If the modifications will occur entirely before the location that needs
to be preserved, a simple method for doing this is to calculate the
distance of the location from the end of the buffer before doing the
modifications, then when the modifications are complete the new value of
the location can be calculated by subtracting offset from the new end of
the buffer. For example:
(setq offset-from-eob (- point-max point-I-want-to-remember))
... the modifications happen here ...
(setq point-I-want-to-remember (- point-max offset-from-eob))
-_
J_)
C_)ingham
. HP - NonStop Austin Software & Services - Software Quality
Assurance
. Austin, TX
. "Language is the apparel in which your thoughts parade in public.
. Never clothe them in vulgar and shoddy attire." -Dr. George W.
Crane-
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2003-01-27 14:48 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-01-21 19:06 A little lisp help Peter Lee
2003-01-21 19:24 ` Henrik Enberg
2003-01-21 20:13 ` Stefan Monnier <foo@acm.com>
2003-01-21 20:32 ` Peter Lee
2003-01-21 20:41 ` Barry Margolin
2003-01-21 21:52 ` Peter Lee
2003-01-21 22:34 ` Peter Lee
2003-01-22 22:55 ` Christopher J. White
2003-01-22 23:02 ` Stefan Monnier <foo@acm.com>
2003-01-24 7:39 ` Jim Janney
2003-01-27 14:48 ` Stefan Monnier <foo@acm.com>
2003-01-22 8:24 ` Kai Großjohann
-- strict thread matches above, loose matches on Subject: below --
2003-01-24 16:51 Bingham, Jay
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.