smarttabs
smarttabs copied to clipboard
Set tab-width to c-basic-offset?
Would automatically setting tab-width
to the value of c-basic-offset
in C/C++ buffers be a good idea?
For example, if tab-width
is 8 and c-default-style
for C/C++ is a style that has c-basic-offset
of 4 (e.g. stroustrup), then I think smart-tabs-insinuate
should set tab-width
to 4 to match the offset.
I guess there are two camps of people: those coming from the tabs world perhaps want to set tab-width
to their preferred value and use that everywhere.
Myself, I do want the behaviour described above, so I've added the following to my .emacs
:
(defvar mavit:smart-tabs-languages
(list 'c 'c++ 'java 'javascript 'cperl 'ruby 'nxml)
"A list of languages for which smart-tabs-mode should be enabled.")
(defun mavit:smart-tabs-tab-width-from-mode ()
"Apply the mode's usual indentation when using smart-tabs-mode."
(when smart-tabs-mode
(dolist (language mavit:smart-tabs-languages)
(when (string-equal major-mode
(concat (symbol-name language) "-mode"))
(setq tab-width (symbol-value (nth 2 (nth 3 (nth 2 (nth 3 (assoc language smart-tabs-insinuate-alist)))))))
(return)))))
(add-hook 'hack-local-variables-hook 'mavit:smart-tabs-tab-width-from-mode)
(apply 'smart-tabs-insinuate mavit:smart-tabs-languages)