Discussion:
Emacs BSD/Allman Style with 4 Space Tabs?
(too old to reply)
h***@gmail.com
2012-11-01 02:15:25 UTC
Permalink
Using this emacs lisp setting one ends up with BSD/Allman style braces but introduces tabs with 8 spaces.

(add-hook 'c-mode-hook
(lambda ()
(c-set-style "linux")))

How do I keep the Allman style braces but keep the tab spaces at 4?

Thanks.
Giorgos Keramidas
2012-11-03 08:52:38 UTC
Permalink
Post by h***@gmail.com
Using this emacs lisp setting one ends up with BSD/Allman style braces
but introduces tabs with 8 spaces.
(add-hook 'c-mode-hook
(lambda ()
(c-set-style "linux")))
How do I keep the Allman style braces but keep the tab spaces at 4?
It's usually better to avoid mixing up the idea of 'indentation level'
and 'size of TAB indentation'. If you fall into the common trap of
changing the size of TAB to tweak the indentation level then you will
use literal ASCII TAB characters and really 'see' something like this:

int
main(void)
{
if (condition) {
some_code();
if (nested_condition) {
more_code();
if (deeply_nested_condition && some_other_condition &&
stuff_that_should_align_properly) {
inner_most_code();
}
}
}
}

but then people look at your code with cat/more/less or any other
program except for Emacs, they will see misaligned code that looks like
this:

int
main(void)
{
if (condition) {
some_code();
if (nested_condition) {
more_code();
if (deeply_nested_condition && some_other_condition &&
stuff_that_should_align_properly) {
inner_most_code();
}
}
}
}

Note how what you 'see' as 'stuff_that_should_align_properly' directly
under the opening parenthesis of the innermost if statement is now
completely out of place and how the _perceived_ look of the code in the
original Emacs buffer layout is something that only exists in Emacs land
and has no actual match with reality.

For this reason, you should keep TAB size at 8 columns, and experiment
with changing the _indentation_ level instead:

(defun my-c-mode-setup ()
(c-set-style "linux")

;; Basic indent is 4 columns.
(make-local-variable 'c-basic-offset)
(setq c-basic-offset 4))

(add-hook 'c-mode-hook 'my-c-mode-setup)

If you want to avoid literal TAB characters altogether, then you should
also set indent-tabs-mode to nil in `my-c-mode-setup' too, e.g.:

(defun my-c-mode-setup ()
(c-set-style "linux")

;; Basic indent is 4 columns.
(make-local-variable 'c-basic-offset)
(setq c-basic-offset 4)

;; Use only space characters for indentation, avoiding literal TABs
;; altogether.
(make-local-variable 'indent-tabs-mode)
(setq indent-tabs-mode nil))

(add-hook 'c-mode-hook 'my-c-mode-setup)

This should work much better than changing TAB size.

Loading...