Emacs. 实时拼写检查两种语言。

5

Windows 7, Emacs 25.1

我需要实时拼写检查我的自定义文本(例如强调不正确的单词)。但我用两种语言编写文本:英语和俄语。我希望能够轻松地在两种语言之间切换拼写检查。

什么是最好的emacs包来实现这个功能?谢谢。


请参考这个问题 - legoscia
2个回答

6
你想要这个:guess_language.el
(use-package guess-language         ; Automatically detect language for Flyspell
  :ensure t
  :defer t
  :init (add-hook 'text-mode-hook #'guess-language-mode)
  :config
  (setq guess-language-langcodes '((en . ("en_GB" "English"))
                                   (it . ("it_IT" "Italian")))
        guess-language-languages '(en it)
        guess-language-min-paragraph-length 45)
  :diminish guess-language-mode)

或者,如果您只想循环浏览它们:

(defvar mu-languages-ring nil "Languages ring for Ispell")

(let ((languages '("en_GB" "it_IT")))
  (validate-setq mu-languages-ring (make-ring (length languages)))
  (dolist (elem languages) (ring-insert mu-languages-ring elem)))

(defun mu-cycle-ispell-languages ()
  (interactive)
  (let ((language (ring-ref mu-languages-ring -1)))
    (ring-insert mu-languages-ring language)
    (ispell-change-dictionary language)))

这些应该与 FlySpell 一起使用


“guess-language”配置示例非常有用,谢谢 :-) 对于Fedora的“aspell-it”软件包用户(以及可能是其他用户),请注意:我一开始遇到了错误,为了使其正常工作,我必须在“guess-language-langcodes”中使用“"it"”而不是“"it_IT"”。 - Arch Stanton

2

我曾经遇到过类似的问题,但是我找到了一个解决方案,可以同时管理两种或更多语言,而不使用guess_language包。这个解决方案基于Hunspell拼写检查器。

系统: Windows 7 SP1,GNU Emacs 26.1

首先,在安装Hunspell之后进行Ispell/Hunspell配置。将下面的代码插入.emacs文件中,该文件通常位于C:/Users/Account目录下。

;; START BLOCK1 <-- Ispell/Hunspell setting
;;                          e.g., "C:/Hunspell/bin/hunspell.exe"
(setq-default ispell-program-name "hunspell.exe FULL PATH")
(setq-default ispell-extra-args  '("--sug-mode=ultra"))
;; Set "DICTDIR" variable, e.g., "C:/Hunspell/share/hunspell"
(setenv "DICTDIR" "hunspell DICTIONARY PATH")
;; Uncomment next line to set English or another dictionary
;; (setq ispell-dictionary "en_US")

;; Automatically enable flyspell-mode in text-mode
(setq text-mode-hook '(lambda() (flyspell-mode t) ))

(require 'ispell)
;; END BLOCK1 <-- Ispell/Hunspell setting

这段代码基于另一个讨论中的拼写配置部分,详见M Parashar - Load Theme。第一个代码块适用于默认字典,可以通过取消注释ispell-dictionary变量行来更改。这段代码对我完美地工作。
第二个代码块使我们能够使用多个字典,它基于AM Lafon - Multi Spell Checking。对我而言,这个代码块只能在第一个代码块后面使用。
;;START BLOCK2<-- Multiple dictionaries. Only works with BLOCK1
(with-eval-after-load "ispell"
  ;; Configure `LANG`, otherwise ispell.el cannot find a 'default
  ;; dictionary' even though multiple dictionaries will be configured
  ;; in next line.
  (setenv "LANG" "es_ES")
  ;; English/spanish configuration.
  (setq ispell-dictionary "en_US,es_ES")
  ;; ispell-set-spellchecker-params has to be called
  ;; before ispell-hunspell-add-multi-dic will work
  (ispell-set-spellchecker-params)
  (ispell-hunspell-add-multi-dic "en_US,es_ES")
  ;; For saving words to the personal dictionary, don't infer it from
  ;; the locale, otherwise it would save to ~/.hunspell_de_DE.
  (setq ispell-personal-dictionary "~/.hunspell_personal"))
;; The personal dictionary file has to exist, otherwise hunspell will
;; silently not use it.
(unless (file-exists-p ispell-personal-dictionary)
  (write-region "" nil ispell-personal-dictionary nil 0))
;;END BLOCK2<-- Multiple dictionaries. Only works with BLOCK1

这两个代码块可以同时启用英语/西班牙语的拼写检查。可以通过将"en_US,es_ES"字符串扩展为适当的词典名称来将更多语言添加到检查系统中。我使用了位于Share/Hunspell目录中的词典文件。没有快捷键可以在语言之间切换,也没有在文件开始时分配每个文件的词典。
# -\*- ispell-dictionary: "castellano8" -\*-. 

当在拼写错误的单词上单击中间鼠标按钮时,会显示上下文菜单,其中一个选项是“保存单词”条目。这些单词将使用变量ispell-personal-dictionary保存在名为.hunspell_personal的新文件中,该文件位于常规路径上。

包含两个代码块可以得到预期的结果。仅包含第二个代码块会引发错误“wrong-type-argument stringp nil”。


你缺少了 (setq ispell-program-name "hunspell") 的调用,Emacs 默认使用 aspell。还需要 ispell-change-dictionary 的调用。但是无论如何都要加个赞。我发布了一个片段,展示了 Emacs 28.0.50 所需的确切代码。 - Hi-Angel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接