Emacs BSD/Allman Style with 4 Space Tabs? - emacs

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?

I don't use c-set-style, so maybe this advice doesn't fit your environment. I have in my init file:
(custom-set-variables
...
'(c-basic-offset 8)
...)
So, setting c-basic-offset to 4 after c-set-style might do the trick:
(add-hook 'c-mode-hook
(lambda ()
(c-set-style "linux")
(setq c-basic-offset 4)))
Look at C-h f c-set-style. Maybe, playing around with DONT-OVERRIDE might help also.

Related

How to revert indent-tabs-mode when editing makefiles in emacs?

I've got the following in my .emacs since for most source code I use spaces only:
(add-hook 'prog-mode-hook
(lambda ()
(setq indent-tabs-mode nil)))
This messes up my makefiles though since they require tabs. How do I work around this so that I get spaces by default for all source code, but makefiles retain tabs?
You can always add an add-hook for makefile-mode to change it back.
(add-hook 'makefile-mode-hook
(lambda ()
(setq indent-tabs-mode t)))

How to turn off ‘tab’ highlighting in go buffers?

I’ve just started writing Go programs in Emacs. How can I turn off tabs highlighting in go-mode buffers? I use «whitespace» for whitespace chars highlighting. Go btw is the only mode where I don’t want tabs highlighted since tabs are standard formatting in Go.
Sincerely, Pavel.
To be clear, you're doing something like
(require 'whitespace)
(global-whitespace-mode t)
right? You can disable whitespace-mode for go-mode with
(setq whitespace-global-modes '(not go-mode))
There is a related question on emacs stack exchange.
I found that this
(add-hook 'go-mode-hook
(lambda ()
(add-hook 'before-save-hook 'gofmt-before-save)
(setq whitespace-style '(face empty trailing lines-tail))
(setq tab-width 4)
(setq indent-tabs-mode 1)))
Worked a bit better for me. Leaves whitespace-mode on but doesn't highlight tabs. Also runs go fmt before save and sets tab width to 4. I'm using prelude.
Add this line
(whitespace-toggle-options '(tabs)))
To your go-mode hook e.g
(use-package go-mode
:preface
(defun go-mode-config ()
(whitespace-toggle-options '(tabs)))
:config
(add-hook 'go-mode-hook (lambda ()
(go-mode-config))))
Taken from prelue go config

emacs change indentation style specifically for c++

I would like to change the indentation style when I edit C++ files in my .emacs to 4-spaces instead of what seems to be the default current 2-spaces. I don't want the style to change for other languages, only for C++. How can I define this in my .emacs configuration?
Use this:
(add-hook 'c++-mode-hook
(lambda ()
(setq c-basic-offset 4)))

Configure C whitespace and Python whitspace independently in Emacs

I would like to configure my Emacs whitespace options independently for C and python files, because the indent highlighting (space before tabs, etc.) does not apply to the Python coding style. I currently set the whitespace variables globally, but would like a separate (minimal) configuration for Python. Here is the relevant part of my .emacs:
(require 'whitespace)
(setq whitespace-line-column 80)
(setq whitespace-style '(face lines-tail indentation trailing space-before-tab))
(add-hook 'c-mode-hook 'whitespace-mode)
This works for C. Preferably I would have '(face lines-tail trailing) for Python, but I don't know how to setq just for a specific mode. What is the correct way to do this? Thanks. Using Emacs 23 in Ubuntu.
I got it to work by putting the settings into the hook along with the evocation of whitespace-mode:
(add-hook 'python-mode-hook
(lambda ()
(progn
(setq whitespace-line-column 79)
(setq whitespace-style '(face lines-tail))
(whitespace-mode))))
Perhaps using File Local Variables could help you?

How do I set Emacs tab size to 4 chars wide for .py files?

I've tried using the following:
(setq-default tab-width 4)
(setq-default tab-stop-list (list 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60))
But the size of tabs when editing .py files is still 8 chars wide. In other files it has gone down to 4, so I assume the Python major mode is overriding this somehow. I see that I can set python-indent to 4, but this causes spaces to be inserted (which goes against our code style guide).
How do I make the tabs 4 chars in width?
Update:
I've also tried this, but it didn't do anything:
(add-hook 'python-mode-hook
(setq indent-tabs-mode t)
(setq tab-width 4)
)
(add-hook 'python-mode-hook
(lambda ()
(setq indent-tabs-mode t)
(setq tab-width 4)
(setq python-indent-offset 4)))
The correct form for the hook is this:
(add-hook 'python-mode-hook
(lambda ()
(setq indent-tabs-mode t)
(setq tab-width 4)))
You need to put the imperative statements inside a function (lambda).
I've had that same issue, but with C++ files. What finally did it for me was the following in my .emacs.
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(c-basic-offset 4 t)
)
Actually of course I set it from the C++ mode area of the Customize Emacs menu, but this was the result. I'd see if python mode has something similar. py-basic-offset or something? Surf around the mode settings for pyton.
(setq indent-tabs-mode t)
(setq python-indent-offset 4)