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: Eric James Michael Ritz
Subject: Re: auto-indenting C++ files upon saving
Date: Fri, 19 Feb 2010 11:54:17 -0500
User-agent: Thunderbird 2.0.0.23 (X11/20090817)

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





reply via email to

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