[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: setting emacs variables

From: Daniel Wetterbro
Subject: Re: setting emacs variables
Date: Wed, 17 Oct 2001 17:54:24 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.2) Gecko/20010726 Netscape6/6.1


The reason c-basic-offset isn't set to 4 is because Emacs resets all "c-related variables" when entering cc-mode. Therefor you have to set up your own c-style, which is a list of predefined values for all "c-related variables" and then make sure your c-style is automatically loaded while entering cc-mode.
I don't know under which platform you're running emacs, but if you have access to the info documentation under Linux (or another Unix) do as follows:
(If you're not familiar with info I suggest you type: info info <ret> and read that doc first, then do as described below)

1. info emacs <ret> (start info and go to the Emacs section)
2. m Programs <ret> (go to the "Programs" menu)
3. m Program Indent

Here you can find just what you need to customize the emacs indentation behaviour. (Note the "Custom C Indent" menu)
You might also find this information helpful (it also gives you a sample for setting up your own c-style):

1. info emacs <ret>
2. m Customization <ret>
3. m Variables <ret> (if you go to "Init File" here instead you'll learn the basics of writing an .emacs file)

Here's a part of my own .emacs file (mostly the same as the one in the info doc):

(setq my-c-style '((c-basic-offset . 4)
                   (c-cleanup-list . (scope-operator
                   (c-offsets-alist . ((arglist-close . c-lineup-arglist)
                                       (substatement-open . 0)))))
(add-hook 'c-mode-common-hook
          (function (lambda ()
                      (c-add-style "my-style" my-c-style t))))

The first big block is setting up a new c-style called "my-c-style" and the second is making
"my-c-style" being used when entering cc-mode.

Gerald A Winters wrote:

Thanks for the reply. Your code below showed me
how to customize the indentation along with
the help facility.

However, according to the emacs help this should work:

(setq c-basic-offset 4)

but it doesn't -- it has absolutely no effect that I can tell.
If you understand why the above statement won't work I'd
like to know. But regardless thanks for the help.


Gerald A Winters wrote:

> I am a C-programmer and am using v20.7. I would like to
> change the default indentation level from "2" to "4" when
> in C-mode. I have a couple manuals and both indicate to
> change the emacs varibale "c-indent-level". However, neither
> of these works in my .emacs file:
> (setq c-indent-level 4)
> (setq-default c-indent-level 4)
> I would be *very* grateful if someone can suggest a solution.
> thanks.
> --jerry
> _______________________________________________
> Help-gnu-emacs mailing list

Emacs uses the cc-mode to fontify C files, and to indent too. Indentation
is based on the 'tab-width' variable, which can be redifined for each
mode. The C structure's indentation is related to
c-basic-offset variable and an offset for the different part of the
structure. In order to have more
precise informations try M-x hell RET i RET C-s 'CC mode', section
Customizing indentation and try this in your .emacs file :

(setq tab-width 4)
;; this will make sure spaces are used instead of tabs
(setq indent-tabs-mode nil)
(setq c-tab-always-indent t)

;; Highlight matching parantheses
(show-paren-mode 1)

;; indentation
;; For more information : M-x info cc-mode
(setq c-comment-only-line-offset 0);; -1000 to have it at the
beginning of the line
(setq c-basic-offset 3);; general value for
indentation on wich every arg of the
;; c-se t-offset will be based on
;; + mean c-basic-offset*1
(c-set-offset 'arglist-close 0);; 0 mean c-basic-offset*0
(c-set-offset 'arglist-intro '+);; function parameters
(c-set-offset 'substatement-open 0);; indentation of braces for
if, for, while
(c-set-offset 'substatement '+);; indentation of a statement
(each line of a function)
(c-set-offset 'case-label '+);; indentation for the label
in a switch-case structure
(c-set-offset 'defun-block-intro '+);; indentation of variable
def for a function (no tab)

(c-set-offset 'statement 0);; indentation of the
function's body (the first line
;; starts at this point)

The wright way to do this is to mak a new hook for the c-mode. You can
see the documentation about hooks : M_x help RET i -RET and look for
hook description.
---------------- -------------------------------------------------
Olivier PIERRAT Tel. : +33 (0) 03 90 40 00 00
ALPLOG Port. : +33 (0) 06 76 41 16 00
Immeuble le Platon mailto :
67400 ILLKIRCH mailto :


Help-gnu-emacs mailing list

Help-gnu-emacs mailing list

reply via email to

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