[Gllug] Writing C

Nix nix at esperi.org.uk
Fri Aug 20 22:06:45 UTC 2004


On Tue, 17 Aug 2004, Richard Jones said:
> On Tue, Aug 17, 2004 at 11:16:29AM +0100, Dan Kolb wrote:
>> Incidentally, if someone could say how to switch the indentation in C-mode from
>> two spaces to four spaces, it'd be much appreciated.
> 
> I think:
> 
> (custom-set-variables '(c-basic-offset 2))
> 
> in .emacs or .xemacs should do the trick.

You should never use custom-set-variables to set variables `by hand'.

Use setq; that's what it's there for:

(setq c-basic-offset 2)

but you can be much more elaborate, e.g. (sorry about the long lines)

(defun nix-setup-c-mode ()
 "Set up C, C++ and Java mode.

This is run in the C-mode-common-hook to set up indentation and other
parameters on creation of buffers managed by cc-mode.el for Nix's personal
coding style."
 (c-add-style "nix" '((c-basic-offset . 1)      ; I use single-indentation
                      (c-recognize-knr-p . nil)  ; K&R? Blugh. Not usin' *that*.
                      (c-comment-only-line-offset . (0 . 0)) ; Do nil for lone comments
                      (c-block-comment-prefix . "") ; We don't use *-prefixed comments
                      (c-indent-comments-syntactically-p . t) ; Even with no code before them
                      (c-cleanup-list . (space-before-funcall compact-empty-funcall)) ; Make function calls look nice
                      (c-electric-pound-behavior . 'alignleft) ; Snap #s to the first column
                      (defun-prompt-regexp . " ") ; Regexp to find the starting brace of a block
                      (c-offsets-alist . ((defun-open  . +)
                                          (defun-close . 0)
                                          (class-open  . +)
                                          (class-close . +)
                                          (block-open  . +)
                                          (block-close . 0)
                                          (case-label  . +)
                                          (statement-case-intro . +)
                                          (inline-open . 3)
                                          (inline-close . 0)
                                          (extern-lang-open . +)
                                          (extern-lang-close . +)
                                          (access-label . 0)
                                          (substatement-open . +)
                                          (statement-cont . c-lineup-math)
                                          (do-while-closure . 0)
                                          (else-clause . 0))))))


or even go more extreme and write your own indentation functions that
can conditionalize on, well, anything :)

-- 
`The copyright file is for everyone.  That we make it available in
 plain-text, uncompressed form rather than in spinning, throbbing
 OpenGL-rendered 3D text over a thumping dance music soundtrack is a
 feature, not a bug.' --- Branden Robinson

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list