6

Emacs帮你进行英文写作

 3 years ago
source link: https://www.lujun9972.win/blog/2018/06/03/emacs%E5%B8%AE%E4%BD%A0%E8%BF%9B%E8%A1%8C%E8%8B%B1%E6%96%87%E5%86%99%E4%BD%9C/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ispell

ispell是Unix下的一个拼写(不仅仅是英文)检查工具,同时它也是Emacs用来调用ispell类工具进行拼写检查的插件名称。

之所以说是ispell类工具是因为Emacs的ispell插件不仅仅支持调用ispell来进行拼写检查,也支持调用aspell/hunspell来进行拼写检查。

而且事实上,aspell已经逐渐取代ispell成为主流。

安装aspell

既然Emacs是调用外部工具来做的拼写检查,那么第一步当然是要安装 aspell

sudo pacman -S aspell --noconfirm
resolving dependencies...
looking for conflicting packages...

Packages (1) aspell-0.60.6.1-5

Total Download Size:   0.57 MiB
Total Installed Size:  3.00 MiB

:: Proceed with installation? [Y/n] 
:: Retrieving packages...
 aspell-0.60.6.1-5-x...     0.0   B  0.00B/s 00:00 [----------------------]   0%
 aspell-0.60.6.1-5-x...   361.4 KiB   531K/s 00:00 [#############---------]  61%
 aspell-0.60.6.1-5-x...   587.9 KiB  1760K/s 00:00 [######################] 100%
(0/1) checking keys in keyring                     [----------------------]   0%
(1/1) checking keys in keyring                     [######################] 100%
(0/1) checking package integrity                   [----------------------]   0%
(1/1) checking package integrity                   [######################] 100%
(0/1) loading package files                        [----------------------]   0%
(1/1) loading package files                        [######################] 100%
(0/1) checking for file conflicts                  [----------------------]   0%
(1/1) checking for file conflicts                  [######################] 100%
(0/1) checking available disk space                [----------------------]   0%
(1/1) checking available disk space                [######################] 100%
:: Processing package changes...
(1/1) installing aspell                            [----------------------]   0%
(1/1) installing aspell                            [######################] 100%
Optional dependencies for aspell
    perl: to import old dictionaries [installed]
:: Running post-transaction hooks...
(1/2) Arming ConditionNeedsUpdate...
(2/2) Updating the info directory file...

不过光安装 aspell 并没什么鸟用, aspell 是通过字典来检查拼写的,因此你还需要安装对应的字典.

pacman -Ss  aspell
extra/aspell 0.60.6.1-5 [已安装]
    A spell checker designed to eventually replace Ispell
extra/aspell-de 20161207-1
    German dictionary for aspell
extra/aspell-en 2017.08.24-1
    English dictionary for aspell
extra/aspell-es 1.11-6
    Spanish dictionary for aspell
extra/aspell-fr 0.50.3-7
    French dictionary for aspell
extra/aspell-nl 0.50.2-4
    Dutch dictionary for aspell
community/aspell-ca 2.3.0-2
    Catalan dictionary for aspell
community/aspell-cs 20040614-8
    Czech dictionary for aspell
community/aspell-el 0.08-2
    Greek dictionary for aspell
community/aspell-hu 0.99.4.2-5
    Hungarian spellcheck dictionary for aspell
community/aspell-it 2.2_20050523-6
    Italian dictionary for aspell
community/aspell-pl 20171220-1
    Polish dictionary for aspell
community/aspell-pt 20161001-1
    Portuguese and Brazilian Portuguese dictionary for aspell
community/aspell-ru 0.99f7-7
    Russian dictionary for aspell
community/aspell-sv 0.51-1
    Swedish dictionary for aspell
community/aspell-uk 1.8.0-1
    Ukrainian dictionary for aspell

你可以看到aspell有很多个语言的字典,也就是支持多种语言的拼写检查,不过可惜的是不支持中文拼写检查(中文分词是个大问题)。

这里我们来安装英文的字典吧

sudo pacman -S aspell-en --noconfirm
resolving dependencies...
looking for conflicting packages...

Packages (1) aspell-en-2017.08.24-1

Total Download Size:   1.06 MiB
Total Installed Size:  4.06 MiB

:: Proceed with installation? [Y/n] 
:: Retrieving packages...
 aspell-en-2017.08.2...     0.0   B  0.00B/s 00:00 [----------------------]   0%
 aspell-en-2017.08.2...   133.6 KiB   216K/s 00:04 [##--------------------]  12%
 aspell-en-2017.08.2...   551.5 KiB   827K/s 00:00 [###########-----------]  50%
 aspell-en-2017.08.2...  1087.1 KiB  1874K/s 00:01 [######################] 100%
(0/1) checking keys in keyring                     [----------------------]   0%
(1/1) checking keys in keyring                     [######################] 100%
(0/1) checking package integrity                   [----------------------]   0%
(1/1) checking package integrity                   [######################] 100%
(0/1) loading package files                        [----------------------]   0%
(1/1) loading package files                        [######################] 100%
(0/1) checking for file conflicts                  [----------------------]   0%
(1/1) checking for file conflicts                  [######################] 100%
(0/1) checking available disk space                [----------------------]   0%
(1/1) checking available disk space                [######################] 100%
:: Processing package changes...
(1/1) installing aspell-en                         [----------------------]   0%
(1/1) installing aspell-en                         [######################] 100%
:: Running post-transaction hooks...
(1/1) Arming ConditionNeedsUpdate...

现在你可以试着直接在shell中运行 aspell -c /tmp/t.txt 来进行拼写检查,然而你很有可能会得到这样一个错误提示

[lujun9972@T430S ~]$ aspell -c /tmp/t.txt 
错误:No word lists can be found for the language "zh_CN".

这是因为aspell会默认会根据 LANG 变量的值来查找对应的字典,结果当然是找不到中文的字典咯。

你需要通过 -l en 来指定语言代码为英文。

aspell -c /tmp/t.txt -l en

ispell-01.png

配置ispell插件

Emacs的ispell插件会自动以此查找aspell,ispell和hunspell,并以第一个找到的程序为检查程序。 因此大多数情况下,你无需特意手工设置拼写程序的名称(ispell-program-name)。

不过默认情况下,Emacs的ispell插件也会是根据 LANG 变量的值来决定使用哪个字典来进行拼写检查的,因此很大可能,你有必要设置一下ispell插件用来检查的字典。

ispell插件提供了两个变量来设置用来做·拼写检查的字典:

ispell-dictionary用于设置全局的默认字典 ispell-local-dictionary用来设置当前buffer局部使用的字典

(setq ispell-dictionary "en")

aspell程序本身还支持很多的选项参数,若要传递这些参数给aspell进程,则可以通过定义 ispell-extra-args 来实现。 比如下面配置让aspell不检查小于等于3个字符的单词

(setq ispell-extra-args '("\W" "3"))

使用Ispell进行拼写检查

ispell

使用ispell进行拼写检查最简单的办法就是直接运行 M-x ispell, 在选中区域的情况下它会调用 ispell-region 来对该区域内的内容进行检查,否则会调用 M-x ispell-buffer 来对整个buffer中的内容进行拼写检查。

若拼写检查无错误,则 ispell 直接在echo area显示一条信息:

Spell-checking region using aspell with en dictionary...done

若发现了不认识的单词,则 ispell 会将光标定位到该单词,并高亮该单词,然后在上方显示修改建议 ispell-02.png

此时你可以做如下动作:

按下修改建议前的字母使用对应的修改建议替换该单词 C-h / ?显示进一步的帮助 空格忽略这个单词 a / A全文忽略该单词 i将该单词认为是正确的单词并加入个人词典中 u将单词的小写形式加入个人词典中 r / R手工输入新单词来替换该单词,r与R不同之处在于r只会对该单词做一次替换,而R可以对buffer中所有该单词出现的地方进行替换。 x / X退出这次单词检查,但ispell进程不被杀死 q退出单词检查,并且杀死ispell进程

如果文档中有多处相同的拼写错误,可以使用“R”进入替换模式,然后输入新单词,当查找到第二个匹配的单词时,按下“!”就会自动将后面所有的单词替换成正确的单词了。

ispell支持递归编辑,当你在进行拼写检查的过程中,可能会发现其他某个地方需要做个修改,但是又不想中断拼写检查的这个过程。

这个时候就可以按下 C=r 进入递归编辑状态。

在递归编辑状态下,你可以像往常一样使用Emacs来修改文档,当完成递归编辑后再使用 C-M-c 退出递归编辑并继续拼写检查。

ispell-word

如果你在写作时,脑袋卡壳了,对某个单词不确实是不是拼写正确的,那么可以直接执行 M-x ispell-word, 它仅仅会对光标所在的单词进行检查。

ispell-minior-mode

ispell-minior-mode 能够实时的对你输入的内容进行拼写检查。

当你输入一个单词后(按下空格或回车),ispell会自动检查该单词,如果找不到该单词,则会给你以提示 ispell-03.png

ispell-complete-word

当你不记得一个单词怎么拼写的时候,还可以使用 ispell 来帮你进行单词补全。

运行 M-x ispell-complete-word 就能看到一个可选列表,列出了各种可供选择的单词。

不过要使用该功能,你还必须设置 ispell-alternate-dictionary 的值。将其指向一个 包含各种单词的存文本格式文件

ispell-04.png

ispell-kill-ispell

启用ispell后,它会一直在后台运行。如果觉得不爽它消耗了系统资源,可以运行 M-x ispell-kill-ispell 把它干掉。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK