help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: auto-indenting C++ files upon saving


From: Richard Riley
Subject: Re: auto-indenting C++ files upon saving
Date: Fri, 19 Feb 2010 17:57:27 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

Art Werschulz <agw@dsm.fordham.edu> writes:

> Hi.
>
> I am trying to teach my students that they should auto-indent their C++
> files before saving, by issuing the commands
>   C-x h M-C-\
> i.e., 
>   (mark-whole-buffer)
>   (indent-region)
> However, they haven't trained their "muscle memory" to do this, i.e., to
> automatically type the sequence 
>   C-x M-C-\ C-x C-s
> How can this be automated, so that a file gets auto-indented whenever
> it's saved?

As a point of contention, I don't think you should do this when you
save. When you save you should be saving what you see. Students should
be indenting as they go.

Another reason is that people often save periodically anyway as they
edit - it would really annoy me if suddenly the program took it on
itself to re-indent the block I was working on.

As for putting it on auto-save this is even worse - it can auto-save
while you are typing and cause significant disruption.

>
> I thinking of something along the lines of
> (setq auto-save-hook
>      (lambda 
>      (mark-whole-region)
>      (indent-region)))
> but this didn't seem to work.

Is mark-whole-region a function?

>
> Actually, I'd only want this to work in some situations, e.g., a file
> whose name matches a certain pattern.  Said pattern would be stored in
> some variable.
>
> My emacs-lisp is very weak.  Suggestions?  Thanks!

At the minimum if you want auto-save-hook (I dont think its a good idea)
you should save your point etc (save-excursion) before doing the rest.

regards,

r.





reply via email to

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