简体   繁体   English

如何关闭特定主模式的电动缩进模式?

[英]How to turn off electric-indent-mode for specific Major mode?

I have few Major modes (like: Yaml and NXML) that I don't want electric-indent-mode (I want it for C like languages) but I'm unable to turn if off. 我有几个主要模式(比如:Yaml和NXML),我不想要电动缩进模式(我希望它用于C语言),但如果关闭我就无法转动。 To enable I have: 为了让我有:

(electric-indent-mode 1)

from documentation (for variable electric-indent-mode) 来自文档(用于可变电动缩进模式)

Non-nil if Electric-Indent mode is enabled. 如果启用电气缩进模式,则为非零。 See the command electric-indent-mode' for a description of this minor mode. Setting this variable directly does not take effect; either customize it (see the info node 有关此次要electric-indent-mode' for a description of this minor mode. Setting this variable directly does not take effect; either customize it (see the info node请参阅命令electric-indent-mode' for a description of this minor mode. Setting this variable directly does not take effect; either customize it (see the info node electric-indent-mode' for a description of this minor mode. Setting this variable directly does not take effect; either customize it (see the info node electric-indent-mode' for a description of this minor mode. Setting this variable directly does not take effect; either customize it (see the info node Easy Customization') or call the function `electric-indent-mode'. electric-indent-mode' for a description of this minor mode. Setting this variable directly does not take effect; either customize it (see the info node Easy Customization'),要么调用函数`electric-indent-mode'。

and for a function 并为一个功能

Toggle on-the-fly reindentation (Electric Indent mode). 切换动态重新注册(电动缩进模式)。 With a prefix argument ARG, enable Electric Indent mode if ARG is positive, and disable it otherwise. 使用前缀参数ARG,如果ARG为正,则启用Electric Indent模式,否则禁用它。 If called from Lisp, enable the mode if ARG is omitted or nil. 如果从Lisp调用,则在省略ARG或nil时启用该模式。

so I try to turn it off in a hook: 所以我试着把它关掉:

(add-hook 'yaml-mode-hook (lambda ()                        
                             (electric-indent-mode -1)))

(Actualy I use after-change-major-mode-hook and check (memql major-mode '(yaml-mode python-mode nxml-mode)) where I can add more modes to the list). (Actualy我使用after-change-major-mode-hook并检查(memql major-mode '(yaml-mode python-mode nxml-mode)) ,我可以在列表中添加更多模式)。

But it don't work, I've also try: 但它不起作用,我也尝试:

(set (make-local-variable 'electric-indent-mode) nil)

No joy. 没有快乐。 But it work when I eval (electric-indent-mode -1) from .emacs files. 但是当我从.emacs文件中评估(electric-indent-mode -1)时,它可以正常工作。

With a recent Emacs (probably Emacs snapshot only) you can use electric-indent-local-mode , eg: 使用最近的Emacs(可能只有Emacs快照),您可以使用electric-indent-local-mode ,例如:

(add-hook 'yaml-mode-hook (lambda () (electric-indent-local-mode -1)))

If your Emacs lacks this function, you can still sort of disable the mode via electric-indent-functions , eg 如果您的Emacs缺少此功能,您仍然可以通过electric-indent-functions禁用该模式,例如

(add-hook 'yaml-mode-hook
          (lambda ()
             (add-hook 'electric-indent-functions
                            (lambda () 'no-indent) nil 'local)))

And in either case, you may probably want to restore Cj , via 在任何一种情况下,您可能都希望通过恢复Cj

(add-hook 'yaml-mode-hook 
          (lambda () (local-set-key (kbd "C-j") #'newline-and-indent)))

electric-indent-mode will be enabled by default in 24.4. 默认情况下,24.4将启用electric-indent-mode To turn it off locally, you will be able to use electric-indent-local-mode as mentioned by lunaryorn. 要在本地关闭它,您将能够使用lunaryorn提到的electric-indent-local-mode But to turn it off locally in 24.3, you can do: 但要在24.3中将其关闭,您可以:

(add-hook 'foo-mode-hook
          (lambda () (set (make-local-variable 'electric-indent-mode) nil)))

You mentioned that the first form didn't work for you, but it should (ie if it doesn't, it's because of the some other problem). 你提到第一种形式不适合你,但它应该(即如果没有,那是因为其他一些问题)。

At least on emacs 24.3 you cannot disable electric indent mode locally, since it is a global-mode . 至少在emacs 24.3上你不能在本地禁用电子缩进模式,因为它是global-mode Anyways the issue with yaml-mode is that the electric-indent functionality is built into it ie it will be enabled even without electric-indent-mode . 无论如何, yaml-mode的问题在于内置了electric-indent功能,即使没有electric-indent-mode也可以启用它。 The package does not provide a way to turn this behaviour off, maybe you should file an issue on its github repo. 该软件包没有提供关闭此行为的方法,也许您应该在其github存储库上提交问题。

Try this to disable the electric-indent functionality in yaml-mode 尝试此操作以禁用yaml-mode的电动缩进功能

(define-key yaml-mode-map "|" nil)
(define-key yaml-mode-map ">" nil)
(define-key yaml-mode-map "-" nil)
(define-key yaml-mode-map "." nil)
(define-key yaml-mode-map [backspace] nil)

To restore the electric-indent behaviour afterwards, you can do 要在之后恢复电动缩进行为,您可以这样做

(define-key yaml-mode-map "|" 'yaml-electric-bar-and-angle)
(define-key yaml-mode-map ">" 'yaml-electric-bar-and-angle)
(define-key yaml-mode-map "-" 'yaml-electric-dash-and-dot)
(define-key yaml-mode-map "." 'yaml-electric-dash-and-dot)
(define-key yaml-mode-map [backspace] 'yaml-electric-backspace)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM