From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: gareth.rees@pobox.com (Gareth Rees) Newsgroups: gmane.emacs.help Subject: Re: indentation Date: 23 Oct 2003 06:19:06 -0700 Organization: http://groups.google.com Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: deer.gmane.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: sea.gmane.org 1066915950 26442 80.91.224.253 (23 Oct 2003 13:32:30 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 23 Oct 2003 13:32:30 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Oct 23 15:32:28 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1ACfZj-0002Xz-00 for ; Thu, 23 Oct 2003 15:32:27 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1ACfZD-0003cH-PS for geh-help-gnu-emacs@m.gmane.org; Thu, 23 Oct 2003 09:31:55 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!cyclone.bc.net!news.maxwell.syr.edu!postnews1.google.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 78 Original-NNTP-Posting-Host: 193.112.141.206 Original-X-Trace: posting.google.com 1066915147 8950 127.0.0.1 (23 Oct 2003 13:19:07 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Thu, 23 Oct 2003 13:19:07 +0000 (UTC) Original-Xref: shelby.stanford.edu gnu.emacs.help:117552 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:13484 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:13484 Yen Tran wrote: > How do I leave indentation mode unchanged? For example, if current > *.c or *.cpp file is using 5 spaces as indentation, then use 5 spaces; > if it's using 4-space tab, then use tab. > > By default, everything I edit with Emacs adds tab to file. Emacs indentation behaviour in C mode is under the control of these variables: * 'indent-tabs-mode' Indentation can insert tabs if this is non-nil. * 'tab-width' Distance between tab stops (for display of tab characters), in columns. * 'c-basic-offset' Amount of basic offset used by + and - symbols in 'c-offsets-alist'. The best thing to do is to agree with all your colleagues to use spaces for indentation (hence indent-tabs-mode should be nil) and how many of them. Then you and they can set editor settings accordingly. The next best thing to do is to specify behaviour for different files by putting a local variables section into the file (see the Info node (emacs)File Variables). For example, a file using indentation of 5 spaces should have the following somewhere near the end: /* Local Variables: */ /* indent-tabs-mode: nil */ /* c-basic-offset: 5 */ /* End: */ while a file using tab characters for indent which you want to display with tab stops every 4 characters should have the following: /* Local Variables: */ /* indent-tabs-mode: t */ /* tab-width: 4 */ /* c-basic-offset: 4 */ /* End: */ If this is no good either, then you could write something that guesses the settings for the file, and put that in c-mode-hook. For example, you might do something like this: (defun guess-indent-settings () (save-excursion (goto-char (point-min)) ;; Don't guess if local variables already set. (when (and (not (search-forward "Local Variables:" nil t)) (re-search-forward "\\([\t ]*\\).*{\n\\1\\(\t\\| +\\)" nil t)) (make-local-variable 'c-basic-offset) (if (string= (match-string 2) "\t") (setq indent-tabs-mode t tab-width 4 ; See note below c-basic-offset 4) ; Ditto (setq indent-tabs-mode nil c-basic-offset (length (match-string 2))))))) (add-hook 'c-mode-hook 'guess-indent-settings) Note that when code uses tab characters for indenting, there's no way to automatically guess by reading the file how many columns are needed per tab stop. A good reason not to use that indentation style! As a last resort, I suppose you could look at the filename and apply some rules based on what you know about the different projects you work on, perhaps like this: (setq indent-tabs-mode nil c-basic-offset (let ((f (buffer-file-name))) (cond ((string-match "/projects/foo/" f) 5) ((string-match "/projects/bar/" f) 8) (t 4)))) -- Gareth Rees http://www.garethrees.org/