From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eric James Michael Ritz Newsgroups: gmane.emacs.help Subject: Re: auto-indenting C++ files upon saving Date: Fri, 19 Feb 2010 11:54:17 -0500 Message-ID: <4B7EC239.5070703@cybersprocket.com> References: 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 1266598968 9554 80.91.229.12 (19 Feb 2010 17:02:48 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 19 Feb 2010 17:02:48 +0000 (UTC) Cc: help-gnu-emacs@gnu.org 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 18:02:46 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 1NiWFY-0002Ba-6U for geh-help-gnu-emacs@m.gmane.org; Fri, 19 Feb 2010 18:02:44 +0100 Original-Received: from localhost ([127.0.0.1]:54936 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NiWFX-0003ba-Hc for geh-help-gnu-emacs@m.gmane.org; Fri, 19 Feb 2010 12:02:43 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NiWD7-00039E-TD for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 12:00:13 -0500 Original-Received: from [140.186.70.92] (port=51331 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NiWD3-00038U-30 for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 12:00:13 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NiWD1-000164-VW for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 12:00:08 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:35636) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NiWD1-00015p-L5 for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 12:00:07 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1NiWCz-0000No-LQ for help-gnu-emacs@gnu.org; Fri, 19 Feb 2010 18:00:05 +0100 Original-Received: from 70-89-149-94-charleston.hfc.comcastbusiness.net ([70.89.149.94]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 19 Feb 2010 18:00:05 +0100 Original-Received: from Eric by 70-89-149-94-charleston.hfc.comcastbusiness.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 19 Feb 2010 18:00:05 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 54 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 70-89-149-94-charleston.hfc.comcastbusiness.net User-Agent: Thunderbird 2.0.0.23 (X11/20090817) In-Reply-To: X-Enigmail-Version: 0.96.0 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:71961 Archived-At: -----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-----