当使用ispell时,我该如何在Emacs中更改语言?

56

我想在Emacs中使用ispell-buffer命令,默认使用英语。是否有简单的方法可以切换到另一个词典(例如,另一种语言)?


我建议所有回答这个问题的人也说明如何将例如法语添加到ispell字典集中,如果在安装过程中没有添加。我认为这里给出的所有答案都是不完整的。 - Albert van der Horst
7个回答

55
以下命令提供了可用的安装字典列表:
M-x ispell-change-dictionary

通常情况下,M-x isp-c-d 也会扩展为上述内容。

1
这将仅为当前会话设置字典。我如何将其设置为Emacs的默认值? - dknight
1
通过完成 ispell-change-dictionary,您可以检查哪些字符串适用于变量 ispell-dictionary。选择所需的字符串并自定义 ispell-dictionary(即 M-x customize-option ispell-dictionary,然后将所需的字典输入到相应的字段中)。 - Tobias

33

你可以从文件 ispell.el 中为 ispell 命令指定一些选项。方法是在文件末尾添加以下类似的段落:

;; Local Variables:
;; ispell-check-comments: exclusive
;; ispell-local-dictionary: "american"
;; End:

注意,双分号标记当前模式下的注释的开始。它可能需要更改以反映您的文件(编程语言)引入注释的方式,例如Java中的//


22

在LaTeX文件的结尾,您可以使用:

%%% Local Variables:
%%% ispell-local-dictionary: "british"
%%% End:

那将会将该字典设置为仅在此文件中使用。


14

使用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

4

如果你想在每个目录上基于语言进行更改,你可以将以下内容添加到.dir-locals.el文件中:

(ispell-local-dictionary . "american")

如果您还没有.dir-locals.el文件,它会像这样:

((nil .
   ((ispell-local-dictionary . "american")))
)

请查看emacs维基关于目录变量的页面获取更多信息。


3

为了方便起见,我在我的 .emacs 文件中添加了以下内容(f7):

(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

1

以下是将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)

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