如何在 emacs 中为所有文件和所有主要模式启用 Flyspell 模式?

发布于 2024-11-26 16:52:49 字数 96 浏览 1 评论 0原文

如何在 Emacs 启动时自动对每个文件和每个主要模式使用 Flyspell-mode?

另外,是否有一个 XML 字典不会将 XML 标签标记为拼写错误的单词?

How do you enable flyspell-mode to be automatically used for every file and every major mode as soon as Emacs is started?

Also, is there an XML dictionary that does not mark XML tags as misspelled words?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只等公子 2024-12-03 16:52:49

这个问题的答案对我有用:

如何启用自动拼写默认情况下检查吗?

此外,与其他先前的答案相比,它似乎更笼统。将以下行添加到 .emacsinit.el 中。

(add-hook 'text-mode-hook 'flyspell-mode)
(add-hook 'prog-mode-hook 'flyspell-prog-mode)

The answer from this question worked for me:

How to enable automatic spell check by default?

Furthermore, it appears to be more general compared to the other prior answers. Add the following lines to your .emacs or init.el.

(add-hook 'text-mode-hook 'flyspell-mode)
(add-hook 'prog-mode-hook 'flyspell-prog-mode)
半衬遮猫 2024-12-03 16:52:49

有可能,您并不真正希望为所有模式启用 flyspell-mode,而是希望为主要处理文本的模式(文本模式、 message-mode 等),以及用于编程模式(C/C++、Java、Ruby、Lisp 等)的 flyspell-prog-mode。两种模式之间的区别在于,第一种模式检查所有单词,而 Flyspell-prog-mode 只检查注释和字符串中的单词(从而避免检查代码,而代码通常不是单词)。

不管怎样,没有一个地方可以在所有文件/缓冲区中启用 Flyspell,因为它已被写入始终位于缓冲区本地。一个近似值是,

(defun turn-on-flyspell () (flyspell-mode 1))
(add-hook 'find-file-hooks 'turn-on-flyspell)

它不包括没有关联文件的缓冲区,我不建议使用它,因为它不区分编程模式和非编程模式 - 我认为这是有用的。

因为无法知道某些模式是否是编程模式,所以您需要手动为您关心的所有编程模式添加自定义,例如:

(mapcar (lambda (mode-hook) (add-hook mode-hook 'flyspell-prog-mode))
        '(c-mode-common-hook tcl-mode-hook emacs-lisp-mode-hook 
          ruby-mode-hook java-mode-hook))

注意:这两块代码可能无法很好地协同工作。

而且,关于 XML,flyspell 已经对 sgml-modehtml-modenxml-mode 进行了自定义,以不对标签进行拼写检查(从 Emacs 23.2 开始)。如果您使用的是旧版本的 Emacs(回到 21.1),您应该能够将其添加到 .emacs 中以获得对 nxml-mode 的支持:

(put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)

Chances are, you don't really want flyspell-mode enabled for all modes, but instead want flyspell-mode enabled for modes that deal primarily with text (text-mode, message-mode, etc.), and flyspell-prog-mode for the programming modes (C/C++, Java, Ruby, Lisp, etc.). The difference between the two modes is that the first checks all words, whereas the flyspell-prog-mode only checks words in comments and strings (thereby avoiding checking the code, which generally isn't words).

Either way, there is no single place to enable flyspell in all files/buffers because it has been written to always be buffer local. A close approximation would be

(defun turn-on-flyspell () (flyspell-mode 1))
(add-hook 'find-file-hooks 'turn-on-flyspell)

That doesn't cover buffers which don't have associated files, and I don't advise using it because it doesn't distinguish between the programming modes and non-programming modes - which I think is useful.

Because there is no way to know whether certain modes are programming modes or not, you need to manually add customizations for all the programming modes you care about, with something like:

(mapcar (lambda (mode-hook) (add-hook mode-hook 'flyspell-prog-mode))
        '(c-mode-common-hook tcl-mode-hook emacs-lisp-mode-hook 
          ruby-mode-hook java-mode-hook))

Note: the two chunks of code probably don't play well together.

And, regarding the XML, flyspell already has customizations for sgml-mode, html-mode, and nxml-mode to not spell check the tags (as of Emacs 23.2). If you're using an older version of Emacs (back to 21.1), you should be able to add this to your .emacs to get the support for nxml-mode:

(put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
明明#如月 2024-12-03 16:52:49

您可以将以下内容添加到 Emacs 初始化文件中:

(flyspell-all-modes)

函数描述如下:

在所有主要模式中使用 Flyspell。适用于现有缓冲区和
您随后创建的缓冲区。关闭“flyspell-text-modes”
如果打开。

编辑:显然,上述功能仅包含在 Emacs 24 中的 Flyspell 版本中。如果您无法使用该版本,则应该使用 Trey 建议的解决方案来“半全局”启用 Flyspell。要使用 NXML 禁用 XML 标记检查,您可以将以下行添加到 Emacs 初始化文件中:

(put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)

注意:此行已包含在 Emacs 24 中包含的 Flyspell.el 中。

You can add the following to your Emacs init file:

(flyspell-all-modes)

The function description states:

Use Flyspell in all major modes. Applies both to existing buffers and
buffers that you subsequently create. Turns off `flyspell-text-modes'
if on.

EDIT: Apparently the above function is only included in the version of flyspell that is in Emacs 24. If you can't use that version, you should instead use the solution suggested by Trey to "semi-globally" enable flyspell. To disable XML tag checking with NXML, you can add the following line to your Emacs init file:

(put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)

Note: This line is already in the flyspell.el included in Emacs 24.

蓝礼 2024-12-03 16:52:49

我无法说出具体时间,但现在 Flyspell-mode 可以很好地了解它所处的模式并做出相应的反应。这是我的使用包实现,带有公司完成的接口。

 (use-package flyspell :demand t
   :config
   (use-package 
     flyspell-correct-helm) 
   (defun flyspellCompletion() 
     (flyspell-mode 1) 
     (set (make-local-variable 'company-backends) 
          (copy-tree company-backends)) 
     (add-to-list 'company-backends 'company-ispell)) 
   (defun flyspell-most-modes() 
     (add-hook 'text-mode-hook 'flyspellCompletion) 
     (add-hook 'prog-mode-hook 'flyspellCompletion)
     (dolist (hook '(change-log-mode-hook log-edit-mode-hook)) 
       (add-hook hook (lambda () 
                        (flyspell-mode -1))))) 
   (flyspell-most-modes) 
   :bind (:map flyspell-mode-map
               ("C-." . flyspell-correct-wrapper)))

I couldn't say when, but flyspell-mode now does a pretty good job of knowing what mode it is in and reacting accordingly. Here is my use-package implementation with an interface to company-completion.

 (use-package flyspell :demand t
   :config
   (use-package 
     flyspell-correct-helm) 
   (defun flyspellCompletion() 
     (flyspell-mode 1) 
     (set (make-local-variable 'company-backends) 
          (copy-tree company-backends)) 
     (add-to-list 'company-backends 'company-ispell)) 
   (defun flyspell-most-modes() 
     (add-hook 'text-mode-hook 'flyspellCompletion) 
     (add-hook 'prog-mode-hook 'flyspellCompletion)
     (dolist (hook '(change-log-mode-hook log-edit-mode-hook)) 
       (add-hook hook (lambda () 
                        (flyspell-mode -1))))) 
   (flyspell-most-modes) 
   :bind (:map flyspell-mode-map
               ("C-." . flyspell-correct-wrapper)))
如梦初醒的夏天 2024-12-03 16:52:49

我将于 2021 年 10 月在 Debian 11.1 上使用 Emacs 27.1。我编写了一个函数,根据缓冲区主模式的类型执行正确的操作,并将其挂接到 find-file-hook,该函数针对每个访问的文件运行。我将此作为其他答案的补充,而不是替代。理由:

  • 我发现flyspell-mode(无论是通过按键绑定调用,还是作为模式挂钩),没有自动识别模式类型/buffer/file,与@RichieHH 的回答相反。
  • 我发现 flyspell-all-modes 不存在,正如 @zev 所暗示的那样。
  • 我发现添加到 text-mode-hook 的建议和
    prog-mode-hook,根据@b4hand,已尽其所能,但离开了
    Flyspell 关闭了我打开的许多随机文件
    基本模式。

一个潜在的缺点是,这不会针对不与任何文件关联的缓冲区运行。显然没有好的方法来挂钩“创建任何缓冲区”。然而,人们应该能够添加多个钩子而不会产生任何不良影响——如果多次调用,该函数应该发现 Flyspell 已经启用,并且不执行任何操作。因此,如果您愿意,也可以向 text-mode-hookprog-mode-hook 添加挂钩。

(add-hook 'find-file-hook 'flyspell-on-for-buffer-type)

(defun flyspell-on-for-buffer-type ()
  (interactive)
  ;; if flyspell mode is not already on, turn it on
  (if (not (symbol-value flyspell-mode))
      (if (derived-mode-p 'prog-mode)
      (progn
        (message "Flyspell on (code)")
        (flyspell-prog-mode))
    (progn
      (message "Flyspell on (text)")
      (flyspell-mode 1)))))

I am using Emacs 27.1 on Debian 11.1 in 2021 October. I wrote a function that does the right thing, based on the the type of the buffer's major mode, and hooked that to find-file-hook, which runs for every file visited. I offer this as an supplement to other answers, not a replacement. Rationale:

  • I found flyspell-mode (whether invoked by key binding, or as a mode hook), did not automatically recognize the type of mode/buffer/file, contrary to answer by @RichieHH.
  • I found flyspell-all-modes does not exist, as implied by @zev.
  • I found the suggestion of adding to text-mode-hook and
    prog-mode-hook, per @b4hand, worked as far as it went, but left
    Flyspell off for the many random files I have that open in
    Fundamental mode.

One potential drawback is, this will not run for buffers not associated with any file. There is apparently no good way to hook "creation of any buffer". However, one should be able to add to multiple hooks with no ill effects -- if called multiple times, the function should find Flyspell already enabled, and do nothing. So add hooks to text-mode-hook and prog-mode-hook as well, if you like.

(add-hook 'find-file-hook 'flyspell-on-for-buffer-type)

(defun flyspell-on-for-buffer-type ()
  (interactive)
  ;; if flyspell mode is not already on, turn it on
  (if (not (symbol-value flyspell-mode))
      (if (derived-mode-p 'prog-mode)
      (progn
        (message "Flyspell on (code)")
        (flyspell-prog-mode))
    (progn
      (message "Flyspell on (text)")
      (flyspell-mode 1)))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文