Different tab indent settings in different modes - emacs

I'm currently using whitespace-cleanup in my save hook. Using indent-tabs-mode, I'm able to save files without any tabs.
All is well, I don't want tabs in my files. But.
Makefiles do need tabs. That is my problem. How do I change my settings for makefile-mode?
I tried to setq either indent-tabs-mode (the doc says it becomes buffer-local) or whitespace-style, it does not work.

OK, my bad. Files were loaded before changing the mode.
The following code works fine, provided it is loaded in the .emacs before opening any file (I have my own project manager which re-opens last files).
(defun my-tabs-makefile-hook ()
(setq indent-tabs-mode t))
(add-hook 'makefile-mode-hook 'my-tabs-makefile-hook)

I've had the same problem, and it seems that this is a bug in Emacs (as of 24.2). Try this, using the following .emacs:
(setq-default indent-tabs-mode nil)
(add-hook 'after-save-hook 'whitespace-cleanup)
If you open a file, save it, and then open a Makefile, you'll have the problem you described. But if you open a Makefile first, save it, and then open another type of file, you'll have the opposite problem: 8 spaces will be replaced by tabs.
The problem is that indent-tabs-mode is buffer-local, but in whitespace.el it is set to a regular variable called whitespace-indent-tabs-mode. Hence, the first value that's seen is the one that counts.
Here's another workaround that solves some other problems too. Add this to your .emacs:
(defadvice whitespace-cleanup (around whitespace-cleanup-indent-tab
activate)
"Fix whitespace-cleanup indent-tabs-mode bug"
(let ((whitespace-indent-tabs-mode indent-tabs-mode)
(whitespace-tab-width tab-width))
ad-do-it))

The best solution I have found for whitespace is ethan-wspace. It cleans up any whitespace that you made dirty yourself but leaves other whitespace intact.
It works for tabs in makefiles and avoids the messy diff problem where there are lots of diff lines that are just whitespace changes

Related

How to disable Emacs Lisp auto-reformatting?

I absolutely despise that Emacs automatically reformats my init.el file. Every time I use the GUI to change a setting, when I look back at my init.el file, Emacs has warped all of the formatting I've done on the file (e.g. changing the indentation, moving things onto their own lines, etc.).
How do I disable this functionality?
Your choice is to either avoid the customize GUI altogether (I am not
even sure that is possible because, e.g.,
packages
use them), or put something like this in your .emacs:
(custom-set-variables
`(custom-file ,(expand-file-name "custom.el" user-emacs-directory)))
(when (file-readable-p custom-file)
(message "ignoring and deleting `%s'!" custom-file)
(delete-file custom-file))
This will make Emacs write all its custom forms to a file that you
never load and delete whenever you start Emacs.
This also means that you will have to manually move all your
customizations from custom-file to your .emacs yourself.

Customize emacs space indentation

I have to adjust to 4 space indentation for a project I'm collaborating on at work. I hate looking at it, being accustomed to a nice compact 2 space indentation. Is there any way to have emacs display 2 spaces as one? Or maybe tabify on file load, and untabify on save. I'm working in c++. I have searched through google, but can't seem to find an answer to this.
You can try using prettify-symbols-mode, which is built into Emacs >=24.5.
(defun jpk/contract-spaces ()
(add-to-list 'prettify-symbols-alist '(" " . ?\ ))
(prettify-symbols-mode 1))
(add-hook 'c-mode-common-hook 'jpk/c-contract-spaces)
This changes the display, not the content of the buffer or file. I'm not sure this is the best idea. It will affect (auto)indentation and you won't see what the saved file will look like.

Customizing emacs in .emacs file

I want emacs to start with specific settings by default. I found that I need to edit the .emacs file in my home directory and use LISP language. However I do get some errors. I need to have:
Windows split by vertical line (I work in C++ with headers and source files)
Column number mode
Cua-mode enabled (to work with normal copy, cut & paste shortcuts)
That's what I have in my .emacs file:
(column-number-mode)
(load "cua-mode")
(CUA-mode t)
(split-window-right)
I'ver tried coding two middle settings in one - (cua-mode). It didn't work out well.
The column-number-mode works, cua does not load and my window is split horizontally (top and bottom window). Where is my error? Thanks for feedback.
From the comments to the question:
if you're using Emacs 24.1 or later,
(column-number-mode)
(load "cua-mode")
(cua-mode t)
(split-window-right)
but if you're using an earlier version,
(column-number-mode)
(load "cua-mode")
(cua-mode t)
(split-window-horizontally)
By the way, the split-window-horizontally also works in later versions of Emacs (I'm using Emacs 25.2.1).

Tab indentation in CoffeeScript using Emacs

I hope this is within the reasonable scope of this site and not too trivial, this is my first post. I am new to Emacs and am trying to set up the environment so that when I start a new line in coffee-mode the automatic indentation is in the form of tabs. As I understood the documentation of coffee-mode all I need to do is set coffee-indent-tabs-mode to t. I've appended my init file with the code below:
(custom-set-variables
'(coffee-tab-width 2)
'(coffee-indent-tabs-mode t))
However when I start Emacs and open a .coffee file, though it gets the tab width right, when I press enter it indents with spaces. Quibbles about whether I need to indent with tabs aside, what am I doing wrong?
In the coffee-mode I find in GNU ELPA, there is no coffee-indent-tabs-mode. I recommend you simply do:
(add-hook 'coffee-mode-hook
(lambda ()
(set (make-local-variable 'tab-width) 2)
(set (make-local-variable 'indent-tabs-mode) t)))
This should work for pretty much any major mode.

How to set Emacs tabs to spaces in every new file?

I would like to have an .emacs setting so that tabs are always formed by consecutive spaces. Preferably in each possible mode. In other editors it never seemed a problem, but in .emacs I'm a bit stuck with the tabs I'm afraid.
add this in your .emacs:
(setq-default indent-tabs-mode nil)
or you can define a before-save-hook that eliminate hard tabs
Also of use, M-x untabify, which will convert all the tabs into spaces in the current region. You can use this to get rid of the existing tabs in files you've edited before you had the indent-tabs-mode set properly.
C-x h (M-x mark-whole-buffer)
M-x untabify