jcsalomon / smarttabs

Emacs smart tabs - indent with tabs, align with spaces!
http://www.emacswiki.org/emacs/SmartTabs
114 stars 20 forks source link

Set tab-width to c-basic-offset? #41

Open fice-t opened 8 years ago

fice-t commented 8 years ago

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.

mavit commented 4 years ago

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)