bash Emacs shell 脚本模式钩子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8892813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:20:01  来源:igfitidea点击:

Emacs shell script mode hook

bashshellemacs

提问by user673592

For some reason my shell script mode hooks do not get executed. Example in my .emacs:

出于某种原因,我的 shell 脚本模式钩子没有被执行。我的 .emacs 中的示例:

(add-hook 'shell-script-mode-hook (lambda () (rainbow-delimiters-mode 1)))

(add-hook 'shell-script-mode-hook (lambda () (rainbow-delimiters-mode 1)))

causes the variables to be set, but the mode is not loaded for the opened script files. What is the proper way to hook here?

导致设置变量,但不会为打开的脚本文件加载模式。在这里挂钩的正确方法是什么?

I use the default shell script mode (modeline says e.g. Shell-script[bash]). Do I have to hook for each shell type individually (sh, bash, zsh)? If yes can you please tell me how?

我使用默认的 shell 脚本模式(模式行说 eg Shell-script[bash])。我是否必须单独挂钩每种外壳类型(sh、bash、zsh)?如果是,你能告诉我怎么做吗?

Thank you very much!

非常感谢!

EDIT3:

编辑3:

It turned out to be due a conflict of textmate-mode with the skeleton-pair-insert in sh-mode (I tried to avoid the conflict by disabling textmate in sh-mode, which then left the sh-mood-hook aparatus in ruins. I've removed textmate-mode completely and use now the standard skeleton-pair approch globaly.

原来是由于 textmate-mode 与 sh-mode 中的 skeleton-pair-insert 发生冲突(我试图通过在 sh-mode 中禁用 textmate 来避免冲突,然后使 sh-mood-hook aparatus 成为废墟. 我已经完全删除了 textmate-mode 并且现在使用标准的 skeleton-pair approch globaly。

I'll accept phils answer - without him I'd probably not be able to debug this on my own.

我会接受 phils 的回答——没有他,我可能无法自己调试。

EDIT2:

编辑2:

Thanks to phils, I think his comment takes us closer to solution. It's not a problem with rainbow-delimiters though. I removed all sh-mode-hook except your hello message one and restart Emacs. When I open a .sh file I get this:

感谢 phils,我认为他的评论使我们更接近解决方案。不过,彩虹分隔符不是问题。我删除了除您的 hello 消息之外的所有 sh-mode-hook 并重新启动 Emacs。当我打开一个 .sh 文件时,我得到了这个:

Setting up indent for shell type bash setting up indent stuff Indentation variables are now local. Indentation setup for shell type bash File mode specification error: (void-function nil)

为 shell 类型 bash 设置缩进 设置缩进内容 缩进变量现在是本地的。shell 类型 bash 文件模式规范错误的缩进设置:(void-function nil)

Note no "hello" message. The value of sh-mode-hook is: (nil (lambda nil (message "hello")))

注意没有“你好”消息。sh-mode-hook 的值是: (nil (lambda nil (message "hello")))

I think the problem is this first nil value - though I don't see that it would be set anywhere.

我认为问题是这个第一个 nil 值 - 尽管我没有看到它会被设置在任何地方。

If I eval this:

如果我评估这个:

(setq sh-mode-hook t) (add-hook 'sh-mode-hook (lambda () (message "hello")))

(setq sh-mode-hook t) (add-hook 'sh-mode-hook (lambda () (message "hello")))

I see the hello message, though after restart (I've put those lines in .emacs) it is gone again (the nil is again on top of the hook).

我看到 hello 消息,但在重新启动后(我将这些行放在 .emacs 中)它又消失了(nil 再次位于钩子的顶部)。

Any idea what to do to have active hook at setup?

知道如何在设置时使用活动钩子吗?

EDIT1: I've tried also:

EDIT1:我也试过:

(add-hook 'sh-mode-hook (lambda () (rainbow-delimiters-mode 1)))

(add-hook 'sh-mode-hook (lambda () (rainbow-delimiters-mode 1)))

with same negative result - not sure if this is relevant though...

具有相同的负面结果 - 虽然不确定这是否相关...

回答by phils

shell-script-modeis an alias for sh-mode. I haven't checked, but I would suspect that only the hook variable for the 'real' function name is evaluated, so I think sh-mode-hookwould be the one to use.

shell-script-mode是 的别名sh-mode。我没有检查过,但我怀疑只评估了“真实”函数名称的钩子变量,所以我认为sh-mode-hook应该使用它。

Anyhow, there's nothing broken about your syntax, so there may be something amiss with the use of (rainbow-delimiters-mode 1). For instance, you should be able to observe that the following works correctly:

无论如何,您的语法没有任何问题,因此使用(rainbow-delimiters-mode 1). 例如,您应该能够观察到以下内容正常工作:

(add-hook 'sh-mode-hook (lambda () (message "hello")))

(add-hook 'sh-mode-hook (lambda () (message "hello")))

FWIW, for hooks I recommend not using anonymous functions at all, simply because it's mucheasier to update your hook function if it is named (removing the old lambda expression from the variable before adding an updated one is just annoying in my books).

FWIW,对于钩子,我建议根本不使用匿名函数,只是因为如果命名了钩子函数更容易更新(在添加更新的变量之前从变量中删除旧的 lambda 表达式在我的书中很烦人)。

回答by mico

Try to remove ':

尝试删除 ':

(add-hook 'shell-script-mode-hook (lambda () (rainbow-delimiters-mode 1)))