Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

auto-indent yanks

16 views
Skip to first unread message

Mark Mynsted

unread,
Jul 16, 2001, 5:55:19 PM7/16/01
to
How would I hook into yank so that text that is yanked is inserted and
indented according to the major mode? The first thing I need often do
after inserting text is to indent the region.

--
-MM
/"\
(No un-solicited email please.) \ / ASCII Ribbon Campaign
See following url, X Against HTML Mail
http://pages.prodigy.net/mmynsted/spamoff.htm / \


Eli Zaretskii

unread,
Jul 16, 2001, 11:23:21 PM7/16/01
to
Mark Mynsted wrote:
>
> How would I hook into yank so that text that is yanked is inserted and
> indented according to the major mode? The first thing I need often do
> after inserting text is to indent the region.

Does it help to type C-M-\, immediately after yanking the text?

Klaus Berndl

unread,
Jul 17, 2001, 7:44:05 AM7/17/01
to Mark Mynsted
On Mon, 16 Jul 2001, Mark Mynsted wrote:

> How would I hook into yank so that text that is yanked is inserted and
> indented according to the major mode? The first thing I need often do
> after inserting text is to indent the region.

The following works for me:

,----
| ;; automatically indenting yanked text if in programming-modes
| (defadvice yank (after indent-region activate)
| (if (member major-mode '(emacs-lisp-mode
| c-mode c++-mode
| tcl-mode sql-mode
| perl-mode cperl-mode
| java-mode jde-mode
| LaTeX-mode TeX-mode))
| (indent-region (region-beginning) (region-end) nil)))
`----

Klaus

--
Klaus Berndl mailto: klaus....@sdm.de
sd&m AG http://www.sdm.de
software design & management
Thomas-Dehler-Str. 27, 81737 München, Germany
Tel +49 89 63812-392, Fax -220

Mark Mynsted

unread,
Jul 17, 2001, 9:55:01 AM7/17/01
to
Eli Zaretskii <el...@is.elta.co.il> writes:

Well, no. That is what I meant by the first thing I do after insert
text is to indent the region. It just seems to me that the text
should be indented AS it is inserted into the buffer. While coding
there really is no occasion when I would want to insert text, via a
yank, and not have it be correctly indented. Why should an additional
key stroke be required?

Stein A. Strømme

unread,
Jul 17, 2001, 10:02:49 AM7/17/01
to
[Mark Mynsted]

| Eli Zaretskii <el...@is.elta.co.il> writes:
|
>> Mark Mynsted wrote:
>> >
>> > How would I hook into yank so that text that is yanked is inserted and
>> > indented according to the major mode? The first thing I need often do
>> > after inserting text is to indent the region.
>>
>> Does it help to type C-M-\, immediately after yanking the text?
|
| Well, no. That is what I meant by the first thing I do after insert
| text is to indent the region. It just seems to me that the text
| should be indented AS it is inserted into the buffer. While coding
| there really is no occasion when I would want to insert text, via a
| yank, and not have it be correctly indented. Why should an additional
| key stroke be required?

I'm not Eli, but perhaps his question is the first step in designing a
solution. It is not hard to make a function which first yanks and
then does the equivalent of C-M-\. If that is what you want.
--
Stein Arild Strømme <http://www.mi.uib.no/~stromme>

Mark Mynsted

unread,
Jul 17, 2001, 10:03:32 AM7/17/01
to
Klaus Berndl <Klaus....@sdm.de> writes:

> On Mon, 16 Jul 2001, Mark Mynsted wrote:
>
> > How would I hook into yank so that text that is yanked is inserted and
> > indented according to the major mode? The first thing I need often do
> > after inserting text is to indent the region.
>
> The following works for me:
>
> ,----
> | ;; automatically indenting yanked text if in programming-modes
> | (defadvice yank (after indent-region activate)
> | (if (member major-mode '(emacs-lisp-mode
> | c-mode c++-mode
> | tcl-mode sql-mode
> | perl-mode cperl-mode
> | java-mode jde-mode
> | LaTeX-mode TeX-mode))
> | (indent-region (region-beginning) (region-end) nil)))
> `----
>
> Klaus

That is perfect! I did not know about the advice mechanism at all.
This is an elegant solution, thank you.

Edric M Ellis

unread,
Jul 17, 2001, 11:06:29 AM7/17/01
to
>>>>> "KB" == Klaus Berndl <Klaus....@sdm.de> writes:

KB> The following works for me:

That's really cool! I had to modify it a little, because I use
transient-mark-mode:

(defadvice yank (after indent-region activate)
(if (member major-mode '(emacs-lisp-mode
c-mode c++-mode
tcl-mode sql-mode
perl-mode cperl-mode
java-mode jde-mode
LaTeX-mode TeX-mode))

(let ((transient-mark-mode nil))
(indent-region (region-beginning) (region-end) nil))))

This works - is it the right way to do it?

Edric.

--
Edric M Ellis
The MathWorks, Ltd.
Tel: +44 (0) 1223 423 200 Ext: 218
Fax: +44 (0) 1223 423 289

Kai Großjohann

unread,
Jul 17, 2001, 11:58:20 AM7/17/01
to
On 17 Jul 2001, Edric M. Ellis wrote:

> (let ((transient-mark-mode nil))
> (indent-region (region-beginning) (region-end) nil))))

As an alternative, you might be able to use (mark t) and (point).
Normally, (mark) barfs in transient mark mode if the region isn't
active, but (mark t) works even if the region isn't active.

kai
--
~/.signature: No such file or directory

Edric M Ellis

unread,
Jul 18, 2001, 2:52:12 AM7/18/01
to
>>>>> "KG" == Kai Großjohann <Kai.Gro...@CS.Uni-Dortmund.DE> writes:

KG> As an alternative, you might be able to use (mark t) and
KG> (point). Normally, (mark) barfs in transient mark mode if the
KG> region isn't active, but (mark t) works even if the region
KG> isn't active.

Aah, didn't know about that - thanks.

0 new messages