From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas Politz Newsgroups: gmane.emacs.help Subject: Re: auto-indenting C++ files upon saving Date: Fri, 19 Feb 2010 19:11:15 +0100 Message-ID: <87tytdp03g.fsf@fh-trier.de> References: <4B7EC239.5070703@cybersprocket.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1266603141 24064 80.91.229.12 (19 Feb 2010 18:12:21 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 19 Feb 2010 18:12:21 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 19 19:12:18 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1NiXKj-0000Sb-L3 for geh-help-gnu-emacs@m.gmane.org; Fri, 19 Feb 2010 19:12:10 +0100 Original-Received: from localhost ([127.0.0.1]:53809 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NiXKi-0000Ub-OM for geh-help-gnu-emacs@m.gmane.org; Fri, 19 Feb 2010 13:12:08 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NiXKK-0000UW-Sb for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 13:11:44 -0500 Original-Received: from [140.186.70.92] (port=40787 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NiXKJ-0000UO-Sy for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 13:11:44 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NiXKI-000658-Oz for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 13:11:43 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:33315) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NiXKI-00064w-FF for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 13:11:42 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1NiXKF-0008RY-Jj for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 19:11:39 +0100 Original-Received: from dslb-088-068-219-018.pools.arcor-ip.net ([88.68.219.18]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 19 Feb 2010 19:11:39 +0100 Original-Received: from politza by dslb-088-068-219-018.pools.arcor-ip.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 19 Feb 2010 19:11:39 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 53 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: dslb-088-068-219-018.pools.arcor-ip.net User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:s6A0VDuf3zuHmXiEuKMBH+pQDsI= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:71963 Archived-At: Eric James Michael Ritz 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