all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* auto-indenting C++ files upon saving
@ 2010-02-19 16:23 Art Werschulz
  2010-02-19 16:54 ` Eric James Michael Ritz
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Art Werschulz @ 2010-02-19 16:23 UTC (permalink / raw
  To: help-gnu-emacs

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?

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.

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!

-- 
Art Werschulz (8-{)}   "Metaphors be with you."  -- bumper sticker
GCS/M (GAT): d? -p+ c++ l++ u+ P++ e--- m* s n+ h f g+ w+ t+ r- 
Net: agw@dsm.fordham.edu http://www.dsm.fordham.edu/~agw
Phone:   Fordham U. (212) 636-6325, Columbia U. (646) 775-6035


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

* Re: auto-indenting C++ files upon saving
  2010-02-19 16:23 auto-indenting C++ files upon saving Art Werschulz
@ 2010-02-19 16:54 ` Eric James Michael Ritz
  2010-02-19 18:11   ` Andreas Politz
  2010-02-19 16:57 ` Richard Riley
       [not found] ` <mailman.1505.1266598832.14305.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 5+ messages in thread
From: Eric James Michael Ritz @ 2010-02-19 16:54 UTC (permalink / raw
  To: help-gnu-emacs; +Cc: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Art Werschulz wrote:
> [...]
>
> How can this be automated, so that a file gets auto-indented whenever
> it's saved?
>
> 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.
>
> 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!

You should be able to add a hook to ‘before-save-hook’ to be called on
save that will indent the file for you.  If you specifically want this
for C++ files then it may be easier to check the current major mode to
see if it is in c++-mode, compared to trying to match against file
names for C++ files.  Maybe something like this?

(add-hook 'before-save-hook
          (lambda ()
            (when (eq 'c++-mode (buffer-local-value 'major-mode (current-buffer)))
              (mark-whole-buffer)
              (indent-region))))

I’m not very confident about my Emacs Lisp either, so there may be a
better way to have save-related hooks for specific modes :)

- --
Eric James Michael Ritz
Cyber Sprocket Labs
(843) 225-3830
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJLfsI4AAoJEEHUZXw5hMWs5ewIAJx4zCzoCmt6Ue9l7alOScS3
3YX1GlI4sYPa/ZoC1iJF7Aj2Km6EfKmhfVZR4621KKm88+gcQOrbYNTU0SLywGcn
U2yYLiuiYk15aoarm/tewnceAJKzvhA1ZnwZBhRSeReZHR8TugqlqW0QAheZjZex
7kfHSE/oDWAIsRa4totMC/30uUW4Jse5FCCv46GSrb3++5HQzsqWX2+5TQm+A9R8
3QuoALWEZHz+HclcKyEZ+bWnk/W4CnQyKHrrWfwuLml1SrgzD8yygeY4SvWgG+tx
olMASxEqH6WwO1m8CaW8O9a9T4+ZQe6VsEKlB+njYoAgd6ZBbn6WosoO90RSPZ4=
=oidM
-----END PGP SIGNATURE-----





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

* Re: auto-indenting C++ files upon saving
  2010-02-19 16:23 auto-indenting C++ files upon saving Art Werschulz
  2010-02-19 16:54 ` Eric James Michael Ritz
@ 2010-02-19 16:57 ` Richard Riley
       [not found] ` <mailman.1505.1266598832.14305.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 5+ messages in thread
From: Richard Riley @ 2010-02-19 16:57 UTC (permalink / raw
  To: help-gnu-emacs

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.





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

* Re: auto-indenting C++ files upon saving
  2010-02-19 16:54 ` Eric James Michael Ritz
@ 2010-02-19 18:11   ` Andreas Politz
  0 siblings, 0 replies; 5+ messages in thread
From: Andreas Politz @ 2010-02-19 18:11 UTC (permalink / raw
  To: help-gnu-emacs

Eric James Michael Ritz <Eric@cybersprocket.com> writes:

> Art Werschulz wrote:
>> [...]
>>
>> How can this be automated, so that a file gets auto-indented whenever
>> it's saved?
>>
>> 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.
>>
>> 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!
>
> You should be able to add a hook to ‘before-save-hook’ to be called on
> save that will indent the file for you.  If you specifically want this
> for C++ files then it may be easier to check the current major mode to
> see if it is in c++-mode, compared to trying to match against file
> names for C++ files.  Maybe something like this?
>
> (add-hook 'before-save-hook
>           (lambda ()
>             (when (eq 'c++-mode (buffer-local-value 'major-mode
>             (current-buffer)))
>               (mark-whole-buffer) (indent-region))))
>
> I’m not very confident about my Emacs Lisp either, so there may be a
> better way to have save-related hooks for specific modes :)
>


I think it's better style to use a buffer-local hook.

(defun c++-indent-buffer-maybe ()
  (when (and (string-match
              "\\(foo\\|bar\\)\\.cpp\\'"
              (buffer-file-name))
             (y-or-n-p "Indent buffer before saving ?"))
    (indent-region (point-min)
                   (point-max))))

(defun my-c++-hook ()
  (add-hook 'before-save-hook 'c++-indent-buffer-maybe nil t))

Here the last argument `t' makes the hook local to the buffer.

(add-hook 'c++-mode-hook 'my-c++-hook)

-ap





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

* Re: auto-indenting C++ files upon saving
       [not found] ` <mailman.1505.1266598832.14305.help-gnu-emacs@gnu.org>
@ 2010-02-22  6:10   ` Torsten Mueller
  0 siblings, 0 replies; 5+ messages in thread
From: Torsten Mueller @ 2010-02-22  6:10 UTC (permalink / raw
  To: help-gnu-emacs

Richard Riley <rileyrgdev@gmail.com> schrieb:

> > 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.

I agree with this completely. For me it would be horror to get noticed
that any automatic process in the background modified my sources and I
cannot control it. If this really has to be done for any reason I
would prefer if this would happen in front of my eyes while I'm
editing. (Indeed I'm also not a fan of this, I want the text to be
what I type in, not what any dumb tool thinks it should be.)

T.M.


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

end of thread, other threads:[~2010-02-22  6:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-02-19 16:23 auto-indenting C++ files upon saving Art Werschulz
2010-02-19 16:54 ` Eric James Michael Ritz
2010-02-19 18:11   ` Andreas Politz
2010-02-19 16:57 ` Richard Riley
     [not found] ` <mailman.1505.1266598832.14305.help-gnu-emacs@gnu.org>
2010-02-22  6:10   ` Torsten Mueller

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.