Emacs 缩进级别全局覆盖 [英] Emacs indent level global override

查看:19
本文介绍了Emacs 缩进级别全局覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将缩进模式设置为仅制表符,任何模式的宽度均为 4 个字符.这似乎是一件微不足道的事情,但我没有成功.每种模式似乎都有自己的变量和选项.我试过为 Perl 和 R 做这件事,但没有成功.没有奏效的事情:

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) 仅适用于 c-mode.我是不是忘记了什么?我是否设置了错误的变量?没有这个选项吗?

(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?

我每天都使用多种语言(R、Perl、sh、C/C++ 等).由于我喜欢跨语言使用相同的缩进,是否有这样一个全局覆盖变量可以设置,以便所有模式下的缩进级别和样式保持一致?如果没有,有没有办法在启动时为每种模式设置它们?如果所有其他方法都失败了,则必须有一个 elisp 脚本来执行此操作.

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.

使用 Emacs 23

Using Emacs 23

已解决:我不得不为每个模式单独设置变量,因为没有这样的全局覆盖.您可以将以下语句放入 ~/.emacs 文件中以在启动时配置 emacs.

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 模式来自 ESS 包.通读文档,我发现:(setq ess-indent-level 4)

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

在 CPerl 模式下 (setq cperl-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.

推荐答案

Emacs 中的缩进并不是真正的微不足道的事情".您可以在 Emacs Wiki 上阅读所有相关内容:
http://www.emacswiki.org/emacs/CategoryIndentation

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?

当然.最简单的方法是使用 Mx customize RET 接口配置值和默认值,尽管只有使用 defcustom 定义的变量出现在那里,因此它不一定全面(但它对于浏览一些可用设置仍然非常有用,即使您实际上并未使用它来设置值).

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).

使用 setqsetq-default 在 init 文件中设置值(或在自动缓冲区局部变量的情况下为默认值),正如您所做的那样,是也不错.

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.

如果你想要更多的控制,你可以使用模式钩子.几乎每种模式都会在缓冲区中初始化自身后运行分配给 (mode-name)-hook 变量的函数列表,因此任何特定于模式的自定义都可以写入 elisp 函数并添加到适当的钩子列表,在你的 init 文件中.

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.

例如:

(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)

这篇关于Emacs 缩进级别全局覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆