简体   繁体   English

emacs:手动切换模式时绕过模式挂钩

[英]emacs: mode hook is bypassed when mode is switched by hand

In my .emacs there is the following mode hooks:在我的.emacs有以下模式挂钩:

(defun my-html-mode-hook ()
  (interactive)
  (setq tab-width 4
        ;; this will make sure TABs are used instead of spaces
        indent-tabs-mode t)
  )

(defun my-javascript-mode-hook ()
  (interactive)
  (setq tab-width 4
        ;; this will make sure TABs are used instead of spaces
        indent-tabs-mode t)
  )


(add-hook 'javascript-mode-hook 'my-javascript-mode-hook)
(add-hook 'html-mode-hook 'my-html-mode-hook)

Now when I am opening a .html , it works as expected, same for opening .js .现在,当我打开.html ,它按预期工作,打开.js

However, when opening .html and then switching to JavaScript by Mx javascript-mode by hand, my-javascript-mode-hook is not run (at least tab width switches to 8).但是,当打开.html然后手动通过Mx javascript-mode切换到 JavaScript 时,不会运行my-javascript-mode-hook (至少标签宽度切换到 8)。 How to make that hook run when I switch the mode manually?当我手动切换模式时如何使该钩子运行?

There is no javascript-mode-hook , I'm afraid.恐怕没有javascript-mode-hook Certainly not by default.当然不是默认的。

javascript-mode is an alias for js-mode , meaning that only js-mode-hook is defined. javascript-modejs-mode别名,意味着只定义了js-mode-hook However, variable aliases are also a thing -- I suggest you Mx report-emacs-bug and ask whether javascript-mode-hook might be defined as a variable alias for js-mode-hook .但是,变量别名也是一回事——我建议你Mx report-emacs-bug并询问javascript-mode-hook是否可以定义为js-mode-hook的变量别名。 So long as that defvaralias was autoloaded (along with the mode's alias, and hence before you've used it), I believe it would work as desired.只要该defvaralias是自动加载的(以及模式的别名,因此在您使用它之前),我相信它会按预期工作。

As such, and as I expected, I am unable to reproduce/confirm your assertion that javascript-mode-hook "works as expected" in the case of opening a .js file.因此,正如我所料,在打开.js文件的情况下,我无法重现/确认您的断言,即javascript-mode-hook “按预期工作”。 Your code does not behave that way in either Emacs 26.3 or 27.0.90.您的代码在 Emacs 26.3 或 27.0.90 中都没有这种行为。

There appears to be a dreaded ambiguity in naming, namely both javascript-mode-hook and js-mode-hook are recognized and run on startup when a JavaScript file is opened.命名上似乎存在一个可怕的歧义,即javascript-mode-hookjs-mode-hook在 JavaScript 文件打开时都会被识别并在启动时运行。

However, when switching to JavaScript by hand, only js-mode-hook is run.但是,当手动切换到 JavaScript 时,只会运行js-mode-hook Both js-mode and javascript-mode mode trigger JavaScript mode (at least they are displayed as (JavaScript) in the status line), so someone got tired of typing the "avacript" letters and kicked them out of "javascript" but forgot to clean up some mess... js-modejavascript-mode模式都会触发JavaScript模式(至少它们在状态行中显示为(JavaScript) ),所以有人厌倦了输入“avacript”字母并将它们踢出“javascript”但忘记了清理一些乱七八糟的...

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

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