【发布时间】:2008-10-20 13:55:07
【问题描述】:
我想在 Emacs 中使用 ispell-buffer 命令。它默认使用英语。有没有一种简单的方法可以切换到另一个字典(例如,另一种语言)?
【问题讨论】:
-
我建议所有回答这个问题的人也告知如何添加例如法语到 ispell 字典集,如果它在安装过程中没有到达那里。我认为这里给出的所有答案都不完整。
我想在 Emacs 中使用 ispell-buffer 命令。它默认使用英语。有没有一种简单的方法可以切换到另一个字典(例如,另一种语言)?
【问题讨论】:
以下命令建议使用已安装字典的列表:
M-x ispell-change-dictionary
通常,M-x isp-c-d 也会扩展到上述内容。
【讨论】:
ispell-change-dictionary 完成后,您可以检查变量ispell-dictionary 允许使用哪些字符串。选择想要的并自定义ispell-dictionary(即M-x customize-optionispell-dictionary,然后在相应的字段中输入您想要的字典)。
您可以从文件 ispell.el 中为ispell 命令指定一些选项。这可以通过在文件末尾添加一个部分来实现,如下所示:
;; Local Variables:
;; ispell-check-comments: exclusive
;; ispell-local-dictionary: "american"
;; End:
注意双分号表示当前模式下 cmets 的开始。可能应该更改它以反映您的文件(编程语言)引入 cmets 的方式,例如用于 Java 的 //。
【讨论】:
您可以在 LaTeX 文件的末尾使用:
%%% Local Variables:
%%% ispell-local-dictionary: "british"
%%% End:
这会将字典设置为仅用于该文件。
【讨论】:
使用M-x ispell-change-dictionary 并点击TAB 来查看有哪些词典可供您使用。
然后在您的.emacs 中写入默认字典的设置,并添加一个钩子以针对您的特定模式自动启动 ispell(如果需要)。
例如,在AUCTeX中自动使用英式英语启动ispell(默认英语词典是美式英语)
(add-hook 'LaTeX-mode-hook 'flyspell-mode) ;start flyspell-mode
(setq ispell-dictionary "british") ;set the default dictionary
(add-hook 'LaTeX-mode-hook 'ispell) ;start ispell
【讨论】:
如果您想按目录更改语言,可以将其添加到.dir-locals.el 文件中:
(ispell-local-dictionary . "american")
如果您还没有.dir-locals.el 文件,它将如下所示:
((nil .
((ispell-local-dictionary . "american")))
)
请参阅emacs wiki page about directory variables 了解更多信息。
【讨论】:
为方便起见 (f7),我在 .emacs 中添加了以下内容:
(global-set-key [f7] 'spell-checker)
(require 'ispell)
(require 'flyspell)
(defun spell-checker ()
"spell checker (on/off) with selectable dictionary"
(interactive)
(if flyspell-mode
(flyspell-mode-off)
(progn
(flyspell-mode)
(ispell-change-dictionary
(completing-read
"Use new dictionary (RET for *default*): "
(and (fboundp 'ispell-valid-dictionary-list)
(mapcar 'list (ispell-valid-dictionary-list)))
nil t))
)))
顺便说一句:不要忘记安装所需的字典。例如。在 debian/ubuntu 上,用于德语和英语词典:
sudo apt install aspell-de aspell-en
【讨论】:
这里有一些代码可以重新映射 C-\ 键以在多种语言之间自动切换和以将输入法更改为相应的语言。 (来源于此帖:https://stackoverflow.com/a/45891514/17936582)
;; Toggle both distionary and input method with C-\
(let ((languages '("en" "it" "de")))
(setq ispell-languages-ring (make-ring (length languages)))
(dolist (elem languages) (ring-insert ispell-languages-ring elem)))
(defun ispell-cycle-languages ()
(interactive)
(let ((language (ring-ref ispell-languages-ring -1)))
(ring-insert ispell-languages-ring language)
(ispell-change-dictionary language)
(cond
((string-match "it" language) (activate-input-method "italian-postfix"))
((string-match "de" language) (activate-input-method "german-postfix"))
((string-match "en" language) (deactivate-input-method)))))
(define-key (current-global-map) [remap toggle-input-method] 'ispell-cycle-languages)
【讨论】: