Configure C whitespace and Python whitspace independently in Emacs - 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?

Related

Emacs Elisp Overriding Default Value

I have several abbrev defined that I was accessible everywhere except in latex mode. I defined
(setq-default abbrev-mode t)
(add-hook 'latex-mode-hook (lambda () (abbrev-mode -1)))
But whenever I open a latex file it still has abbrev mode enabled. What's going on?
Never worked with latex before, but for me the following works fine:
(setq auto-mode-alist (cons '("\\.lat\\'" . latex-mode) auto-mode-alist))
(setq-default abbrev-mode t)
(add-hook 'latex-mode-hook (lambda () (abbrev-mode -1)))
M-x abbrev-mode
%Abbrev mode enabled in current buffer
Please make sure the emacs recognized your file as a latex file, the first line I wrote should do the trick.
The reason was that AUCTex uses LaTeX-mode-hook. Thanks to stefan in the comment for pointing that out

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

How do I disable electric indent on RET but still keep other electric characters (e.g., ‘{’)?

In Emacs 24.4, the default indentation behavior has been changed—new lines are now automatically indented. From the release notes:
*** `electric-indent-mode' is now enabled by default.
Typing RET reindents the current line and indents the new line.
`C-j' inserts a newline but does not indent. In some programming modes,
additional characters are electric (eg `{').
I prefer the old behavior, so I added
(electric-indent-mode 0)
to my .emacs file. However, this disables all electric characters, which is not what I intended.
Is there any way to disable the new behavior while still having characters like ‘{’ or ‘:’ trigger an indentation?
You want to remove ?\n from electric-indent-chars. You can do this globally with:
(setq electric-indent-chars (remq ?\n electric-indent-chars))
or only in a particular mode (e.g. C):
(add-hook 'c-mode-hook
(lambda ()
(setq-local electric-indent-chars (remq ?\n electric-indent-chars))))
By checking the documentation for c-electric-brace, I found that the behavior of electric characters is controlled by the buffer-local variable c-electric-flag. It worked after I added the following lines to my .emacs file:
(add-hook 'c-mode-hook
(lambda ()
(set 'c-electric-flag t)))
(add-hook 'c++-mode-hook
(lambda ()
(set 'c-electric-flag t)))

Emacs BSD/Allman Style with 4 Space Tabs?

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.

Emacs: globally enable whitespace-mode

I want globally enable whitespace-mode. I have tried this in my .emacs:
(require 'whitespace)
(setq-default whitespace-style '(face trailing lines empty indentation::space))
(setq-default whitespace-line-column 80)
(setq global-whitespace-mode 1)
(whitespace-mode 1)
but without success... I able to enable it via M+x whitespace-mode, but I want it to enable it globally... Any suggestions? I am using GNU Emacs 23.3.1 .
In general it's best to enable/disable modes by using the function call, not setting the variable (which is what you did for global-whitespace-mode).
Try:
(global-whitespace-mode 1)