From: Andreas Politz <politza@fh-trier.de>
To: help-gnu-emacs@gnu.org
Subject: Re: auto-indenting C++ files upon saving
Date: Fri, 19 Feb 2010 19:11:15 +0100 [thread overview]
Message-ID: <87tytdp03g.fsf@fh-trier.de> (raw)
In-Reply-To: 4B7EC239.5070703@cybersprocket.com
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
next prev parent reply other threads:[~2010-02-19 18:11 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87tytdp03g.fsf@fh-trier.de \
--to=politza@fh-trier.de \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.