Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

elisp - Emacs indent level global override

I want to set the indentation mode to tabs only, with a 4 character width for any mode. This seems like a trivial thing, but I have not had success. Every mode seems to have its own variables and options. I've tried doing this for Perl and R without success. Things that have not worked:

(setq-default tab-width 4)
(setq standard-indent 4)
(setq-default r-indent-level 4)
(setq perl-indent-level 4)

(setq c-basic-offset 4) works for c-mode but nothing else. Am I forgetting something? Did I set the wrong variables? Is there no such option?

I work with a variety of languages (R, Perl, sh, C/C++ etc.) on a daily basis. Since I like to use the same indentation across languages, is there such a global override variable that I can set so that the indentation level and style is consistent across all modes? If not, is there a way to set them for each mode on startup? If all else fails, there has to be an elisp script that does this.

Using Emacs 23


RESOLVED: I had to set the variables for each mode individually because there is no such global override. You can put the following statements in your ~/.emacs file to configure emacs on startup.

R mode comes from the ESS package. Reading through the documentation, I found this: (setq ess-indent-level 4)

In CPerl mode (setq cperl-indent-level 4)

Looks like you'll just have to search for the right variable in each mode.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Indentation in Emacs isn't really a "trivial thing". You can read all about it at the Emacs Wiki:
http://www.emacswiki.org/emacs/CategoryIndentation

Any major mode is free to implement indentation however it wishes and, as you've noticed, several of them introduce indentation-related variables; so no, there is no global indentation configuration which is guaranteed to affect every possible major mode (although in practice, certain variables are completely standard by convention).

If not, is there a way to set them for each mode on startup?

Of course. The easiest way is to configure values and defaults using the M-x customize RET interface, although only variables defined with defcustom appear there, so it isn't necessarily comprehensive (but it can still be very useful for browsing some of the available settings, even if you don't actually use it to set the values).

Setting values (or defaults in the case of automatically buffer-local variables) in your init file with setq and setq-default, as you've done, is also fine.

If you want more control, you can use mode hooks. Pretty much every mode runs the list of functions assigned to the (mode-name)-hook variable after initialising itself in a buffer, so any mode-specific customisations can be written in an elisp function and added to the appropriate hook list, in your init file.

e.g.:

(defun my-c-mode-config ()
  (whitespace-mode 1)
  (setq indent-tabs-mode t
        tab-width        4
        c-basic-offset   4))

(add-hook 'c-mode-hook 'my-c-mode-config)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...