(read-language-name): Doc fix.
[bpt/emacs.git] / lisp / textmodes / ispell.el
CommitLineData
be010748
RS
1;;; ispell.el --- spell checking using Ispell
2
1907b8e6 3;; Copyright (C) 1994, 1995, 1997 Free Software Foundation, Inc.
be010748 4
b578f267
EN
5;; Authors : Ken Stevens <k.stevens@ieee.org>
6;; Last Modified On: Tue Jun 13 12:05:28 EDT 1995
7;; Update Revision : 2.37
8;; Syntax : emacs-lisp
9;; Status : Release with 3.1.12+ ispell.
10;; Version : International Ispell Version 3.1 by Geoff Kuenning.
11;; Bug Reports : ispell-el-bugs@itcorp.com
be010748 12
b578f267
EN
13;; This file is part of GNU Emacs.
14
15;; GNU Emacs is free software; you can redistribute it and/or modify
16;; it under the terms of the GNU General Public License as published by
17;; the Free Software Foundation; either version 2, or (at your option)
18;; any later version.
19
20;; GNU Emacs is distributed in the hope that it will be useful,
21;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23;; GNU General Public License for more details.
24
25;; You should have received a copy of the GNU General Public License
26;; along with GNU Emacs; see the file COPYING. If not, write to the
27;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28;; Boston, MA 02111-1307, USA.
29
30;; Note: version numbers and time stamp are not updated
31;; when this file is edited for release with GNU Emacs.
be010748 32
007852e0 33;;; Commentary:
007852e0 34
b578f267
EN
35;; INSTRUCTIONS
36;;
37;; This code contains a section of user-settable variables that you should
38;; inspect prior to installation. Look past the end of the history list.
39;; Set them up for your locale and the preferences of the majority of the
40;; users. Otherwise the users may need to set a number of variables
41;; themselves.
42;; You particularly may want to change the default dictionary for your
43;; country and language.
44;;
45;;
46;; To fully install this, add this file to your Emacs Lisp directory and
47;; compile it with M-X byte-compile-file. Then add the following to the
48;; appropriate init file:
49;;
50;; (autoload 'ispell-word "ispell"
51;; "Check the spelling of word in buffer." t)
52;; (global-set-key "\e$" 'ispell-word)
53;; (autoload 'ispell-region "ispell"
54;; "Check the spelling of region." t)
55;; (autoload 'ispell-buffer "ispell"
56;; "Check the spelling of buffer." t)
57;; (autoload 'ispell-complete-word "ispell"
58;; "Look up current word in dictionary and try to complete it." t)
59;; (autoload 'ispell-change-dictionary "ispell"
60;; "Change ispell dictionary." t)
61;; (autoload 'ispell-message "ispell"
62;; "Check spelling of mail message or news post.")
63;;
64;; Depending on the mail system you use, you may want to include these:
65;;
66;; (add-hook 'news-inews-hook 'ispell-message)
67;; (add-hook 'mail-send-hook 'ispell-message)
68;; (add-hook 'mh-before-send-letter-hook 'ispell-message)
69;;
70;;
71;; Ispell has a TeX parser and a nroff parser (the default).
72;; The parsing is controlled by the variable ispell-parser. Currently
73;; it is just a "toggle" between TeX and nroff, but if more parsers are
74;; added it will be updated. See the variable description for more info.
75;;
76;;
77;; TABLE OF CONTENTS
78;;
79;; ispell-word
80;; ispell-region
81;; ispell-buffer
82;; ispell-message
83;; ispell-continue
84;; ispell-complete-word
85;; ispell-complete-word-interior-frag
86;; ispell-change-dictionary
87;; ispell-kill-ispell
88;; ispell-pdict-save
89;;
90;;
91;; Commands in ispell-region:
92;; Character replacement: Replace word with choice. May query-replace.
93;; ' ': Accept word this time.
94;; 'i': Accept word and insert into private dictionary.
95;; 'a': Accept word for this session.
96;; 'A': Accept word and place in buffer-local dictionary.
97;; 'r': Replace word with typed-in value. Rechecked.
98;; 'R': Replace word with typed-in value. Query-replaced in buffer. Rechecked.
99;; '?': Show these commands
100;; 'x': Exit spelling buffer. Move cursor to original point.
101;; 'X': Exit spelling buffer. Leave cursor at the current point.
102;; 'q': Quit spelling session (Kills ispell process).
103;; 'l': Look up typed-in replacement in alternate dictionary. Wildcards okay.
104;; 'u': Like 'i', but the word is lower-cased first.
105;; 'm': Like 'i', but allows one to include dictionary completion info.
106;; 'C-l': redraws screen
107;; 'C-r': recursive edit
108;; 'C-z': suspend emacs or iconify frame
109;;
110;; Buffer-Local features:
111;; There are a number of buffer-local features that can be used to customize
112;; ispell for the current buffer. This includes language dictionaries,
113;; personal dictionaries, parsing, and local word spellings. Each of these
114;; local customizations are done either through local variables, or by
115;; including the keyword and argument(s) at the end of the buffer (usually
116;; prefixed by the comment characters). See the end of this file for
117;; examples. The local keywords and variables are:
118;;
119;; ispell-dictionary-keyword language-dictionary
120;; uses local variable ispell-local-dictionary
121;; ispell-pdict-keyword personal-dictionary
122;; uses local variable ispell-local-pdict
123;; ispell-parsing-keyword mode-arg extended-char-arg
124;; ispell-words-keyword any number of local word spellings
125;;
126;;
127;; BUGS:
128;; Highlighting in version 19 still doesn't work on tty's.
129;; On some versions of emacs, growing the minibuffer fails.
130;;
131;; HISTORY
132;;
6ec7837b
RS
133;; Revision 2.38 1996/5/30 ethanb@phys.washington.edu
134;; Update ispell-message for gnus 5 (news-inews-hook => message-send-hook;
135;; different header for quoted message).
136;;
b578f267
EN
137;; Revision 2.37 1995/6/13 12:05:28 stevens
138;; Removed autoload from ispell-dictionary-alist. *choices* mode-line shows
139;; misspelled word. Block skip for pgp & forwarded messages added.
140;; RMS: the autoload changes had problems and I removed them.
141;;
142;; Revision 2.36 1995/2/6 17:39:38 stevens
143;; Properly adjust screen with different ispell-choices-win-default-height
144;; settings. Skips SGML entity references.
145;;
146;; Revision 2.35 1995/1/13 14:16:46 stevens
147;; Skips SGML tags, ispell-change-dictionary fix for add-hook, assure personal
148;; dictionary is saved when called from the menu
149;;
150;; Revision 2.34 1994/12/08 13:17:41 stevens
151;; Interaction corrected to function with all 3.1 ispell versions.
152;;
153;; Revision 2.33 1994/11/24 02:31:20 stevens
154;; Repaired bug introduced in 2.32 that corrupts buffers when correcting.
155;; Improved buffer scrolling. Nondestructive buffer selections allowed.
156;;
157;; Revision 2.32 1994/10/31 21:10:08 geoff
158;; Many revisions accepted from RMS/FSF. I think (though I don't know) that
159;; this represents an 'official' version.
160;;
161;; Revision 2.31 1994/5/31 10:18:17 stevens
162;; Repaired comments. buffer-local commands executed in `ispell-word' now.
163;; German dictionary described for extended character mode. Dict messages.
164;;
165;; Revision 2.30 1994/5/20 22:18:36 stevens
166;; Continue ispell from ispell-word, C-z functionality fixed.
167;;
168;; Revision 2.29 1994/5/12 09:44:33 stevens
169;; Restored ispell-use-ptys-p, ispell-message aborts sends with interrupt.
170;; defined fn ispell
171;;
172;; Revision 2.28 1994/4/28 16:24:40 stevens
173;; Window checking when ispell-message put on gnus-inews-article-hook jwz.
174;; prefixed ispell- to highlight functions and horiz-scroll fn.
175;; Try and respect case of word in ispell-complete-word.
176;; Ignore non-char events. Ispell-use-ptys-p commented out. Lucid menu.
177;; Better interrupt handling. ispell-message improvements from Ethan.
178;;
179;; Revision 2.27
180;; version 18 explicit C-g handling disabled as it didn't work. Added
181;; ispell-extra-args for ispell customization (jwz)
182;;
183;; Revision 2.26 1994/2/15 16:11:14 stevens
184;; name changes for copyright assignment. Added word-frags in complete-word.
185;; Horizontal scroll (John Conover). Query-replace matches words now. bugs.
186;;
187;; Revision 2.25
188;; minor mods, upgraded ispell-message
189;;
190;; Revision 2.24
191;; query-replace more robust, messages, defaults, ispell-change-dict.
192;;
193;; Revision 2.23 1993/11/22 23:47:03 stevens
194;; ispell-message, Fixed highlighting, added menu-bar, fixed ispell-help, ...
195;;
196;; Revision 2.22
197;; Added 'u' command. Fixed default in ispell-local-dictionary.
198;; fixed affix rules display. Tib skipping more robust. Contributions by
199;; Per Abraham (parser selection), Denis Howe, and Eberhard Mattes.
200;;
201;; Revision 2.21 1993/06/30 14:09:04 stevens
202;; minor bugs. (nroff word skipping fixed)
203;;
204;; Revision 2.20 1993/06/30 14:09:04 stevens
205;;
206;; Debugging and contributions by: Boris Aronov, Rik Faith, Chris Moore,
207;; Kevin Rodgers, Malcolm Davis.
208;; Particular thanks to Michael Lipp, Jamie Zawinski, Phil Queinnec
209;; and John Heidemann for suggestions and code.
210;; Major update including many tweaks.
211;; Many changes were integrations of suggestions.
212;; lookup-words rehacked to use call-process (Jamie).
213;; ispell-complete-word rehacked to be compatible with the rest of the
214;; system for word searching and to include multiple wildcards,
7a8f27db 215;; and its own dictionary.
b578f267
EN
216;; query-replace capability added. New options 'X', 'R', and 'A'.
217;; buffer-local modes for dictionary, word-spelling, and formatter-parsing.
218;; Many random bugs, like commented comments being skipped, fix to
219;; keep-choices-win, fix for math mode, added pipe mode choice,
220;; fixed 'q' command, ispell-word checks previous word and leave cursor
221;; in same location. Fixed tib code which could drop spelling regions.
222;; Cleaned up setq calls for efficiency. Gave more context on window overlays.
223;; Assure context on ispell-command-loop. Window lossage in look cmd fixed.
224;; Due to pervasive opinion, common-lisp package syntax removed. Display
225;; problem when not highlighting.
226;;
227;; Revision 2.19 1992/01/10 10:54:08 geoff
228;; Make another attempt at fixing the "Bogus, dude" problem. This one is
229;; less elegant, but has the advantage of working.
230;;
231;; Revision 2.18 1992/01/07 10:04:52 geoff
232;; Fix the "Bogus, Dude" problem in ispell-word.
233;;
234;; Revision 2.17 1991/09/12 00:01:42 geoff
235;; Add some changes to make ispell-complete-word work better, though
236;; still not perfectly.
237;;
238;; Revision 2.16 91/09/04 18:00:52 geoff
239;; More updates from Sebastian, to make the multiple-dictionary support
240;; more flexible.
241;;
242;; Revision 2.15 91/09/04 17:30:02 geoff
243;; Sebastian Kremer's tib support
244;;
245;; Revision 2.14 91/09/04 16:19:37 geoff
246;; Don't do set-window-start if the move-to-window-line moved us
247;; downward, rather than upward. This prevents getting the buffer all
248;; confused. Also, don't use the "not-modified" function to clear the
249;; modification flag; instead use set-buffer-modified-p. This prevents
250;; extra messages from flashing.
251;;
252;; Revision 2.13 91/09/04 14:35:41 geoff
253;; Fix a spelling error in a comment. Add code to handshake with the
254;; ispell process before sending anything to it.
255;;
256;; Revision 2.12 91/09/03 20:14:21 geoff
257;; Add Sebastian Kremer's multiple-language support.
258;;
259;;
260;; Walt Buehring
261;; Texas Instruments - Computer Science Center
262;; ARPA: Buehring%TI-CSL@CSNet-Relay
263;; UUCP: {smu, texsun, im4u, rice} ! ti-csl ! buehring
264;;
265;; ispell-region and associated routines added by
266;; Perry Smith
267;; pedz@bobkat
268;; Tue Jan 13 20:18:02 CST 1987
269;;
270;; extensively modified by Mark Davies and Andrew Vignaux
271;; {mark,andrew}@vuwcomp
272;; Sun May 10 11:45:04 NZST 1987
273;;
274;; Ken Stevens ARPA: k.stevens@ieee.org
275;; Tue Jan 3 16:59:07 PST 1989
276;; This file has overgone a major overhaul to be compatible with ispell
277;; version 2.1. Most of the functions have been totally rewritten, and
278;; many user-accessible variables have been added. The syntax table has
279;; been removed since it didn't work properly anyway, and a filter is
280;; used rather than a buffer. Regular expressions are used based on
281;; ispell's internal definition of characters (see ispell(4)).
282;; Some new updates:
283;; - Updated to version 3.0 to include terse processing.
284;; - Added a variable for the look command.
285;; - Fixed a bug in ispell-word when cursor is far away from the word
286;; that is to be checked.
287;; - Ispell places the incorrect word or guess in the minibuffer now.
288;; - fixed a bug with 'l' option when multiple windows are on the screen.
289;; - lookup-words just didn't work with the process filter. Fixed.
290;; - Rewrote the process filter to make it cleaner and more robust
291;; in the event of a continued line not being completed.
292;; - Made ispell-init-process more robust in handling errors.
293;; - Fixed bug in continuation location after a region has been modified by
294;; correcting a misspelling.
295;; Mon 17 Sept 1990
296;;
297;; Sebastian Kremer <sk@thp.uni-koeln.de>
298;; Wed Aug 7 14:02:17 MET DST 1991
299;; - Ported ispell-complete-word from Ispell 2 to Ispell 3.
300;; - Added ispell-kill-ispell command.
301;; - Added ispell-dictionary and ispell-dictionary-alist variables to
302;; support other than default language. See their docstrings and
303;; command ispell-change-dictionary.
304;; - (ispelled it :-)
305;; - Added ispell-skip-tib variable to support the tib bibliography
306;; program.
007852e0
RS
307
308
b578f267
EN
309;; **********************************************************************
310;; The following variables should be set according to personal preference
311;; and location of binaries:
312;; **********************************************************************
007852e0 313
b578f267 314;; ******* THIS FILE IS WRITTEN FOR ISPELL VERSION 3.1
007852e0 315
007852e0
RS
316;;; Code:
317
f2b7eb26
RS
318(defgroup ispell nil
319 "Spell checking using ispell"
320 :group 'processes)
007852e0 321
f2b7eb26
RS
322
323(defcustom ispell-highlight-p t
324 "*Highlight spelling errors when non-nil."
325 :type 'boolean
326 :group 'ispell)
327
328(defcustom ispell-highlight-face 'highlight
a9210a53
RS
329 "*The face used for Ispell highlighting. For Emacses with overlays.
330Possible values are `highlight', `modeline', `secondary-selection',
331`region', and `underline'.
007852e0
RS
332This variable can be set by the user to whatever face they desire.
333It's most convenient if the cursor color and highlight color are
f2b7eb26
RS
334slightly different."
335 :type 'face
336 :group 'ispell)
007852e0 337
f2b7eb26
RS
338(defcustom ispell-check-comments t
339 "*If nil, don't check spelling of comments."
340 :type 'boolean
341 :group 'ispell)
007852e0 342
f2b7eb26 343(defcustom ispell-query-replace-choices nil
4b03da72 344 "*Corrections made throughout region when non-nil.
f2b7eb26
RS
345Uses `query-replace' (\\[query-replace]) for corrections."
346 :type 'boolean
347 :group 'ispell)
007852e0 348
f2b7eb26 349(defcustom ispell-skip-tib nil
a9210a53 350 "*Does not spell check `tib' bibliography references when non-nil.
4b03da72 351Skips any text between strings matching regular expressions
a9210a53 352`ispell-tib-ref-beginning' and `ispell-tib-ref-end'.
007852e0
RS
353
354TeX users beware: Any field starting with [. will skip until a .] -- even
a9210a53 355your whole buffer -- unless you set `ispell-skip-tib' to nil. That includes
f2b7eb26
RS
356a [.5mm] type of number...."
357 :type 'boolean
358 :group 'ispell)
007852e0
RS
359
360(defvar ispell-tib-ref-beginning "[[<]\\."
361 "Regexp matching the beginning of a Tib reference.")
362
363(defvar ispell-tib-ref-end "\\.[]>]"
364 "Regexp matching the end of a Tib reference.")
365
f2b7eb26 366(defcustom ispell-keep-choices-win t
a9210a53 367 "*When not nil, the `*Choices*' window remains for spelling session.
f2b7eb26
RS
368This minimizes redisplay thrashing."
369 :type 'boolean
370 :group 'ispell)
007852e0 371
f2b7eb26 372(defcustom ispell-choices-win-default-height 2
a9210a53 373 "*The default size of the `*Choices*' window, including status line.
f2b7eb26
RS
374Must be greater than 1."
375 :type 'integer
376 :group 'ispell)
007852e0 377
f2b7eb26
RS
378(defcustom ispell-program-name "ispell"
379 "Program invoked by \\[ispell-word] and \\[ispell-region] commands."
380 :type 'string
381 :group 'ispell)
007852e0 382
f2b7eb26 383(defcustom ispell-alternate-dictionary
007852e0 384 (cond ((file-exists-p "/usr/dict/web2") "/usr/dict/web2")
e29702dd 385 ((file-exists-p "/usr/share/dict/web2") "/usr/share/dict/web2")
007852e0
RS
386 ((file-exists-p "/usr/dict/words") "/usr/dict/words")
387 ((file-exists-p "/usr/lib/dict/words") "/usr/lib/dict/words")
e29702dd 388 ((file-exists-p "/usr/share/dict/words") "/usr/share/dict/words")
007852e0
RS
389 ((file-exists-p "/sys/dict") "/sys/dict")
390 (t "/usr/dict/words"))
f2b7eb26
RS
391 "*Alternate dictionary for spelling help."
392 :type 'file
393 :group 'ispell)
007852e0 394
f2b7eb26
RS
395(defcustom ispell-complete-word-dict ispell-alternate-dictionary
396 "*Dictionary used for word completion."
397 :type 'file
398 :group 'ispell)
007852e0 399
e8649704 400(defvar ispell-grep-command "egrep"
007852e0
RS
401 "Name of the grep command for search processes.")
402
403(defvar ispell-grep-options "-i"
a9210a53 404 "String of options to use when running the program in `ispell-grep-command'.
007852e0
RS
405Should probably be \"-i\" or \"-e\".
406Some machines (like the NeXT) don't support \"-i\"")
407
e8649704 408(defvar ispell-look-command "look"
007852e0 409 "Name of the look command for search processes.
a9210a53 410This must be an absolute file name.")
007852e0 411
f2b7eb26 412(defcustom ispell-look-p (file-exists-p ispell-look-command)
e29702dd 413 "*Non-nil means use `look' rather than `grep'.
f2b7eb26
RS
414Default is based on whether `look' seems to be available."
415 :type 'boolean
416 :group 'ispell)
007852e0 417
f2b7eb26
RS
418(defcustom ispell-have-new-look nil
419 "*Non-nil means use the `-r' option (regexp) when running `look'."
420 :type 'boolean
421 :group 'ispell)
007852e0
RS
422
423(defvar ispell-look-options (if ispell-have-new-look "-dfr" "-df")
a9210a53 424 "String of command options for `ispell-look-command'.")
007852e0
RS
425
426(defvar ispell-use-ptys-p nil
a9210a53
RS
427 "When non-nil, Emacs uses ptys to communicate with Ispell.
428When nil, Emacs uses pipes.")
007852e0 429
f2b7eb26 430(defcustom ispell-following-word nil
a9210a53 431 "*Non-nil means `ispell-word' checks the word around or after point.
f2b7eb26
RS
432Otherwise `ispell-word' checks the preceding word."
433 :type 'boolean
434 :group 'ispell)
007852e0 435
f2b7eb26 436(defcustom ispell-help-in-bufferp nil
a9210a53 437 "*Non-nil means display interactive keymap help in a buffer.
f2b7eb26
RS
438Otherwise use the minibuffer."
439 :type 'boolean
440 :group 'ispell)
007852e0 441
f2b7eb26
RS
442(defcustom ispell-quietly nil
443 "*Non-nil means suppress messages in `ispell-word'."
444 :type 'boolean
445 :group 'ispell)
007852e0 446
f2b7eb26 447(defcustom ispell-format-word (function upcase)
4b03da72 448 "*Formatting function for displaying word being spell checked.
f2b7eb26
RS
449The function must take one string argument and return a string."
450 :type 'function
451 :group 'ispell)
007852e0 452
d38e5dbe 453;;;###autoload
f2b7eb26 454(defcustom ispell-personal-dictionary nil
e29702dd
KH
455 "*File name of your personal spelling dictionary, or nil.
456If nil, the default personal dictionary, \"~/.ispell_DICTNAME\" is used,
f2b7eb26
RS
457where DICTNAME is the name of your default dictionary."
458 :type 'file
459 :group 'ispell)
007852e0 460
f2b7eb26
RS
461(defcustom ispell-silently-savep nil
462 "*When non-nil, save the personal dictionary without confirmation."
463 :type 'boolean
464 :group 'ispell)
007852e0
RS
465
466;;; This variable contains the current dictionary being used if the ispell
467;;; process is running. Otherwise it contains the global default.
468(defvar ispell-dictionary nil
469 "If non-nil, a dictionary to use instead of the default one.
a9210a53
RS
470This is passed to the ispell process using the `-d' switch and is
471used as key in `ispell-dictionary-alist' (which see).
007852e0 472
a9210a53 473You should set this variable before your first use of Emacs spell-checking
e29702dd
KH
474commands in the Emacs session, or else use the \\[ispell-change-dictionary]
475command to change it. Otherwise, this variable only takes effect in a newly
a9210a53 476started Ispell process.")
007852e0 477
f2b7eb26 478(defcustom ispell-extra-args nil
a9210a53 479 "*If non-nil, a list of extra switches to pass to the Ispell program.
4b03da72
RS
480For example, '(\"-W\" \"3\") to cause it to accept all 1-3 character
481words as correct. See also `ispell-dictionary-alist', which may be used
f2b7eb26
RS
482for language-specific arguments."
483 :type '(repeat string)
484 :group 'ispell)
4b03da72 485
d38e5dbe
RS
486;;; The preparation of the menu bar menu must be autoloaded
487;;; because otherwise this file gets autoloaded every time Emacs starts
488;;; so that it can set up the menus and determine keyboard equivalents.
8ce8687e 489
d38e5dbe
RS
490;;;###autoload
491(defvar ispell-dictionary-alist-1 ; sk 9-Aug-1991 18:28
007852e0 492 '((nil ; default (english.aff)
99b11016 493 "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil)
7c5a1541 494 ("english" ; make English explicitly selectable
99b11016 495 "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil)
7c5a1541 496 ("british" ; British version
e29702dd 497 "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B" "-d" "british") nil)
7c5a1541
RS
498 ("american" ; American version
499 "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B" "-d" "american") nil)
007852e0 500 ("deutsch" ; deutsch.aff
e29702dd 501 "[a-zA-Z\"]" "[^a-zA-Z\"]" "[']" t ("-C") "~tex")
007852e0
RS
502 ("deutsch8"
503 "[a-zA-Z\304\326\334\344\366\337\374]"
504 "[^a-zA-Z\304\326\334\344\366\337\374]"
ef128a91 505 "[']" t ("-C" "-d" "deutsch") "~latin1" iso-latin-1)
e29702dd
KH
506 ("nederlands" ; nederlands.aff
507 "[A-Za-z\300-\305\307\310-\317\322-\326\331-\334\340-\345\347\350-\357\361\362-\366\371-\374]"
508 "[^A-Za-z\300-\305\307\310-\317\322-\326\331-\334\340-\345\347\350-\357\361\362-\366\371-\374]"
ef128a91 509 "[']" t ("-C") nil iso-latin-1)
007852e0
RS
510 ("nederlands8" ; dutch8.aff
511 "[A-Za-z\300-\305\307\310-\317\322-\326\331-\334\340-\345\347\350-\357\361\362-\366\371-\374]"
512 "[^A-Za-z\300-\305\307\310-\317\322-\326\331-\334\340-\345\347\350-\357\361\362-\366\371-\374]"
ef128a91 513 "[']" t ("-C") nil iso-latin-1)))
d38e5dbe
RS
514
515;;;###autoload
516(defvar ispell-dictionary-alist-2
517 '(("svenska" ;7 bit swedish mode
007852e0 518 "[A-Za-z}{|\\133\\135\\\\]" "[^A-Za-z}{|\\133\\135\\\\]"
e29702dd 519 "[']" nil ("-C") nil)
007852e0
RS
520 ("svenska8" ;8 bit swedish mode
521 "[A-Za-z\345\344\366\305\304\366]" "[^A-Za-z\345\344\366\305\304\366]"
ef128a91
KH
522 "[']" nil ("-C" "-d" "svenska") "~list" ; Add `"-T" "list"' instead?
523 iso-latin-1)
e29702dd
KH
524 ("francais7"
525 "[A-Za-z]" "[^A-Za-z]" "[`'^---]" t nil nil)
526 ("francais" ; francais.aff
527 "[A-Za-z\300\302\306\307\310\311\312\313\316\317\324\331\333\334\340\342\347\350\351\352\353\356\357\364\371\373\374]"
528 "[^A-Za-z\300\302\306\307\310\311\312\313\316\317\324\331\333\334\340\342\347\350\351\352\353\356\357\364\371\373\374]"
ef128a91 529 "[---']" t nil "~list" iso-latin-1)
e29702dd
KH
530 ("francais-tex" ; francais.aff
531 "[A-Za-z\300\302\306\307\310\311\312\313\316\317\324\331\333\334\340\342\347\350\351\352\353\356\357\364\371\373\374\\]"
532 "[^A-Za-z\300\302\306\307\310\311\312\313\316\317\324\331\333\334\340\342\347\350\351\352\353\356\357\364\371\373\374\\]"
ef128a91 533 "[---'^`\"]" t nil "~tex" iso-latin-1)
4b03da72 534 ("dansk" ; dansk.aff
007852e0 535 "[A-Z\306\330\305a-z\346\370\345]" "[^A-Z\306\330\305a-z\346\370\345]"
ef128a91 536 "[']" nil ("-C") nil iso-latin-1)
d38e5dbe
RS
537 ))
538
12e53d2e 539
d38e5dbe
RS
540;;; ispell-dictionary-alist is set up from two subvariables above
541;;; to avoid having very long lines in loaddefs.el.
542;;;###autoload
543(defvar ispell-dictionary-alist
544 (append ispell-dictionary-alist-1 ispell-dictionary-alist-2)
007852e0
RS
545 "An alist of dictionaries and their associated parameters.
546
547Each element of this list is also a list:
548
4b03da72 549\(DICTIONARY-NAME CASECHARS NOT-CASECHARS OTHERCHARS MANY-OTHERCHARS-P
007852e0
RS
550 ISPELL-ARGS EXTENDED-CHARACTER-MODE\)
551
a9210a53 552DICTIONARY-NAME is a possible value of variable `ispell-dictionary', nil
007852e0
RS
553means the default dictionary.
554
555CASECHARS is a regular expression of valid characters that comprise a
556word.
557
558NOT-CASECHARS is the opposite regexp of CASECHARS.
559
fbbd6e46
RS
560OTHERCHARS are characters in the NOT-CASECHARS set but which can be used to
561construct words in some special way. If OTHERCHARS characters follow and
562precede characters from CASECHARS, they are parsed as part of a word,
563otherwise they become word-breaks. As an example in English, assume the
564set ['] (as a regular expression) for OTHERCHARS. Then \"they're\" and
565\"Steven's\" are parsed as single words including the \"'\" character, but
566\"Stevens'\" does not include the quote character as part of the word.
567If you want OTHERCHARS to be empty, use nil.
007852e0
RS
568Hint: regexp syntax requires the hyphen to be declared first here.
569
fbbd6e46
RS
570MANY-OTHERCHARS-P is non-nil when multiple OTHERCHARS are allowed in a word.
571Otherwise only a single OTHERCHARS character is allowed to be part of any
572single word.
007852e0
RS
573
574ISPELL-ARGS is a list of additional arguments passed to the ispell
575subprocess.
576
577EXTENDED-CHARACTER-MODE should be used when dictionaries are used which
e29702dd 578have been configured in an Ispell affix file. (For example, umlauts
007852e0 579can be encoded as \\\"a, a\\\", \"a, ...) Defaults are ~tex and ~nroff
a9210a53
RS
580in English. This has the same effect as the command-line `-T' option.
581The buffer Major Mode controls Ispell's parsing in tex or nroff mode,
007852e0
RS
582but the dictionary can control the extended character mode.
583Both defaults can be overruled in a buffer-local fashion. See
a9210a53 584`ispell-parsing-keyword' for details on this.
007852e0
RS
585
586Note that the CASECHARS and OTHERCHARS slots of the alist should
587contain the same character set as casechars and otherchars in the
588language.aff file \(e.g., english.aff\).")
589
d38e5dbe 590;;;###autoload
e29702dd 591(defvar ispell-menu-map nil "Key map for ispell menu")
007852e0 592
d38e5dbe 593;;;###autoload
e29702dd
KH
594(defvar ispell-menu-lucid nil "Spelling menu for Lucid Emacs.")
595
596;;; Break out lucid menu and split into several calls to avoid having
597;;; long lines in loaddefs.el. Detect need off following constant.
12e53d2e 598
d38e5dbe 599;;;###autoload
e29702dd
KH
600(defconst ispell-menu-map-needed ; make sure this is not Lucid Emacs
601 (and (not ispell-menu-map)
d38e5dbe
RS
602;;; This is commented out because it fails in Emacs.
603;;; due to the fact that menu-bar is loaded much later than loaddefs.
604;;; ;; make sure this isn't Lucid Emacs
605;;; (featurep 'menu-bar)
8ce8687e 606 (not (string-match "Lucid" emacs-version))))
e29702dd 607
d38e5dbe
RS
608;;; Set up dictionary
609;;;###autoload
12e53d2e
RS
610(if ispell-menu-map-needed
611 (let ((dicts (reverse (cons (cons "default" nil) ispell-dictionary-alist)))
612 name)
12e53d2e 613 (setq ispell-menu-map (make-sparse-keymap "Spell"))
e29702dd 614 ;; add the dictionaries to the bottom of the list.
12e53d2e
RS
615 (while dicts
616 (setq name (car (car dicts))
617 dicts (cdr dicts))
618 (if (stringp name)
619 (define-key ispell-menu-map (vector (intern name))
620 (cons (concat "Select " (capitalize name))
621 (list 'lambda () '(interactive)
622 (list 'ispell-change-dictionary name))))))))
623
e29702dd 624;;; define commands in menu in opposite order you want them to appear.
d38e5dbe 625;;;###autoload
12e53d2e
RS
626(if ispell-menu-map-needed
627 (progn
12e53d2e
RS
628 (define-key ispell-menu-map [ispell-change-dictionary]
629 '("Change Dictionary" . ispell-change-dictionary))
630 (define-key ispell-menu-map [ispell-kill-ispell]
631 '("Kill Process" . ispell-kill-ispell))
632 (define-key ispell-menu-map [ispell-pdict-save]
1335d078 633 '("Save Dictionary" . (lambda () (interactive) (ispell-pdict-save t t))))
12e53d2e
RS
634 (define-key ispell-menu-map [ispell-complete-word]
635 '("Complete Word" . ispell-complete-word))
636 (define-key ispell-menu-map [ispell-complete-word-interior-frag]
637 '("Complete Word Frag" . ispell-complete-word-interior-frag))))
638
d38e5dbe 639;;;###autoload
12e53d2e
RS
640(if ispell-menu-map-needed
641 (progn
642 (define-key ispell-menu-map [ispell-continue]
643 '("Continue Check" . ispell-continue))
12e53d2e
RS
644 (define-key ispell-menu-map [ispell-word]
645 '("Check Word" . ispell-word))
81b328e5
KH
646 (define-key ispell-menu-map [ispell-region]
647 '("Check Region" . ispell-region))
12e53d2e 648 (define-key ispell-menu-map [ispell-buffer]
60d48874
RS
649 '("Check Buffer" . ispell-buffer))))
650
d38e5dbe 651;;;###autoload
60d48874
RS
652(if ispell-menu-map-needed
653 (progn
12e53d2e
RS
654 (define-key ispell-menu-map [ispell-message]
655 '("Check Message" . ispell-message))
656 (define-key ispell-menu-map [ispell-help]
e29702dd 657 ;; use (x-popup-menu last-nonmenu-event(list "" ispell-help-list)) ?
12e53d2e 658 '("Help" . (lambda () (interactive) (describe-function 'ispell-help))))
e29702dd
KH
659 (put 'ispell-region 'menu-enable 'mark-active)
660 (fset 'ispell-menu-map (symbol-value 'ispell-menu-map))))
661
662;;; Xemacs version 19
663(if (and (string-lessp "19" emacs-version)
664 (string-match "Lucid" emacs-version))
665 (let ((dicts (cons (cons "default" nil) ispell-dictionary-alist))
666 (current-menubar (or current-menubar default-menubar))
667 (menu
668 '(["Help" (describe-function 'ispell-help) t]
669 ;;["Help" (popup-menu ispell-help-list) t]
670 ["Check Message" ispell-message t]
671 ["Check Buffer" ispell-buffer t]
672 ["Check Word" ispell-word t]
673 ["Check Region" ispell-region (or (not zmacs-regions) (mark))]
674 ["Continue Check" ispell-continue t]
675 ["Complete Word Frag"ispell-complete-word-interior-frag t]
676 ["Complete Word" ispell-complete-word t]
677 ["Kill Process" ispell-kill-ispell t]
678 "-"
1335d078 679 ["Save Dictionary" (ispell-pdict-save t t) t]
e29702dd
KH
680 ["Change Dictionary" ispell-change-dictionary t]))
681 name)
682 (while dicts
683 (setq name (car (car dicts))
684 dicts (cdr dicts))
685 (if (stringp name)
686 (setq menu (append menu
687 (list
688 (vector (concat "Select " (capitalize name))
689 (list 'ispell-change-dictionary name)
690 t))))))
691 (setq ispell-menu-lucid menu)
692 (if current-menubar
693 (progn
694 (delete-menu-item '("Edit" "Spell")) ; in case already defined
695 (add-menu '("Edit") "Spell" ispell-menu-lucid)))))
007852e0
RS
696
697
698;;; **********************************************************************
699;;; The following are used by ispell, and should not be changed.
700;;; **********************************************************************
701
702
e29702dd 703;;; The version must be 3.1 or greater for this version of ispell.el
8ce8687e 704;;; There is an incompatibility between version 3.1.12 and lower versions.
e29702dd 705(defconst ispell-required-version '("3.1." 12)
007852e0 706 "Ispell versions with which this version of ispell.el is known to work.")
e29702dd
KH
707(defvar ispell-offset 1
708 "Offset that maps protocol differences between ispell 3.1 versions.")
007852e0 709
ef128a91
KH
710(defun ispell-decode-string (str)
711 (let (coding-system)
712 (if (and enable-multibyte-characters
713 (setq coding-system (ispell-get-coding-system)))
714 (decode-coding-string str coding-system)
715 str)))
716
007852e0 717(defun ispell-get-casechars ()
ef128a91
KH
718 (ispell-decode-string
719 (nth 1 (assoc ispell-dictionary ispell-dictionary-alist))))
007852e0 720(defun ispell-get-not-casechars ()
ef128a91
KH
721 (ispell-decode-string
722 (nth 2 (assoc ispell-dictionary ispell-dictionary-alist))))
007852e0 723(defun ispell-get-otherchars ()
ef128a91
KH
724 (ispell-decode-string
725 (nth 3 (assoc ispell-dictionary ispell-dictionary-alist))))
007852e0
RS
726(defun ispell-get-many-otherchars-p ()
727 (nth 4 (assoc ispell-dictionary ispell-dictionary-alist)))
728(defun ispell-get-ispell-args ()
729 (nth 5 (assoc ispell-dictionary ispell-dictionary-alist)))
730(defun ispell-get-extended-character-mode ()
731 (nth 6 (assoc ispell-dictionary ispell-dictionary-alist)))
ef128a91
KH
732(defun ispell-get-coding-system ()
733 (nth 7 (assoc ispell-dictionary ispell-dictionary-alist)))
007852e0
RS
734
735(defvar ispell-process nil
a9210a53 736 "The process object for Ispell.")
007852e0
RS
737
738(defvar ispell-pdict-modified-p nil
a9210a53 739 "Non-nil means personal dictionary has modifications to be saved.")
007852e0
RS
740
741;;; If you want to save the dictionary when quitting, must do so explicitly.
e29702dd
KH
742;;; When non-nil, the spell session is terminated.
743;;; When numeric, contains cursor location in buffer, and cursor remains there.
a9210a53 744(defvar ispell-quit nil)
007852e0
RS
745
746(defvar ispell-filter nil
a9210a53 747 "Output filter from piped calls to Ispell.")
007852e0
RS
748
749(defvar ispell-filter-continue nil
a9210a53 750 "Control variable for Ispell filter function.")
007852e0
RS
751
752(defvar ispell-process-directory nil
a9210a53 753 "The directory where `ispell-process' was started.")
007852e0
RS
754
755(defvar ispell-query-replace-marker (make-marker)
a9210a53 756 "Marker for `query-replace' processing.")
007852e0
RS
757
758(defvar ispell-checking-message nil
759 "Non-nil when we're checking a mail message")
760
761(defconst ispell-choices-buffer "*Choices*")
762
a9210a53 763(defvar ispell-overlay nil "Overlay variable for Ispell highlighting.")
007852e0
RS
764
765;;; *** Buffer Local Definitions ***
766
767;;; This is the local dictionary to use. When nil the default dictionary will
768;;; be used. Do not redefine default value or it will override the global!
769(defvar ispell-local-dictionary nil
a9210a53 770 "If non-nil, a dictionary to use for Ispell commands in this buffer.
e29702dd 771The value must be a string dictionary name in `ispell-dictionary-alist'.
a9210a53
RS
772This variable becomes buffer-local when set in any fashion.
773
774Setting ispell-local-dictionary to a value has the same effect as
775calling \\[ispell-change-dictionary] with that value. This variable
776is automatically set when defined in the file with either
777`ispell-dictionary-keyword' or the Local Variable syntax.")
007852e0
RS
778
779(make-variable-buffer-local 'ispell-local-dictionary)
780
781;; Use default directory, unless locally set.
782(set-default 'ispell-local-dictionary nil)
783
784(defconst ispell-words-keyword "LocalWords: "
785 "The keyword for local oddly-spelled words to accept.
786The keyword will be followed by any number of local word spellings.
787There can be multiple of these keywords in the file.")
788
789(defconst ispell-dictionary-keyword "Local IspellDict: "
790 "The keyword for local dictionary definitions.
791There should be only one dictionary keyword definition per file, and it
a9210a53 792should be followed by a correct dictionary name in `ispell-dictionary-alist'.")
007852e0
RS
793
794(defconst ispell-parsing-keyword "Local IspellParsing: "
a9210a53 795 "The keyword for overriding default Ispell parsing.
4b03da72
RS
796Determined by the buffer's major mode and extended-character mode as well as
797the default dictionary.
007852e0
RS
798
799The above keyword string should be followed by `latex-mode' or
800`nroff-mode' to put the current buffer into the desired parsing mode.
801
802Extended character mode can be changed for this buffer by placing
803a `~' followed by an extended-character mode -- such as `~.tex'.")
804
1335d078
RS
805(defvar ispell-skip-sgml nil
806 "Skips spell checking of SGML tags and entity references when non-nil.
8ce8687e 807This variable is set when major-mode is sgml-mode or html-mode.")
1335d078 808
007852e0 809(defvar ispell-local-pdict ispell-personal-dictionary
4b03da72 810 "A buffer local variable containing the current personal dictionary.
a9210a53 811If non-nil, the value must be a string, which is a file name.
4b03da72 812
a9210a53
RS
813If you specify a personal dictionary for the current buffer which is
814different from the current personal dictionary, the effect is similar
815to calling \\[ispell-change-dictionary]. This variable is automatically
816set when defined in the file with either `ispell-pdict-keyword' or the
817local variable syntax.")
007852e0
RS
818
819(make-variable-buffer-local 'ispell-local-pdict)
820
821(defconst ispell-pdict-keyword "Local IspellPersDict: "
822 "The keyword for defining buffer local dictionaries.")
823
824(defvar ispell-buffer-local-name nil
825 "Contains the buffer name if local word definitions were used.
826Ispell is then restarted because the local words could conflict.")
827
828(defvar ispell-parser 'use-mode-name
829 "*Indicates whether ispell should parse the current buffer as TeX Code.
a9210a53 830Special value `use-mode-name' tries to guess using the name of major-mode.
007852e0
RS
831Default parser is 'nroff.
832Currently the only other valid parser is 'tex.
833
834You can set this variable in hooks in your init file -- eg:
835
e29702dd 836(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))")
007852e0
RS
837
838(defvar ispell-region-end (make-marker)
839 "Marker that allows spelling continuations.")
840
0349bab6
RS
841(defvar ispell-check-only nil
842 "If non-nil, `ispell-word' does not try to correct the word.")
007852e0 843
e29702dd 844
007852e0
RS
845;;; **********************************************************************
846;;; **********************************************************************
847
848
1335d078
RS
849(and (string-lessp "19" emacs-version)
850 (not (boundp 'epoch::version))
851 (defalias 'ispell 'ispell-buffer))
4b03da72 852
8ce8687e
RS
853;;;###autoload
854(define-key global-map "\M-$" 'ispell-word)
a870f917 855
007852e0 856;;;###autoload
9e7ad89a 857(defun ispell-word (&optional following quietly continue)
007852e0 858 "Check spelling of word under or before the cursor.
a9210a53 859If the word is not found in dictionary, display possible corrections
e29702dd 860in a window allowing you to choose one.
a9210a53 861
4b03da72 862With a prefix argument (or if CONTINUE is non-nil),
9e7ad89a 863resume interrupted spell-checking of a buffer or region.
a9210a53 864
4b03da72 865If optional argument FOLLOWING is non-nil or if `ispell-following-word'
007852e0 866is non-nil when called interactively, then the following word
3ee3ac77 867\(rather than preceding\) is checked when the cursor is not over a word.
4b03da72 868When the optional argument QUIETLY is non-nil or `ispell-quietly' is non-nil
007852e0
RS
869when called interactively, non-corrective messages are suppressed.
870
4b03da72 871Word syntax described by `ispell-dictionary-alist' (which see).
007852e0
RS
872
873This will check or reload the dictionary. Use \\[ispell-change-dictionary]
3ee3ac77 874or \\[ispell-region] to update the Ispell process."
9e7ad89a
RS
875 (interactive (list nil nil current-prefix-arg))
876 (if continue
877 (ispell-continue)
878 (if (interactive-p)
879 (setq following ispell-following-word
880 quietly ispell-quietly))
e29702dd 881 (ispell-accept-buffer-local-defs) ; use the correct dictionary
9e7ad89a 882 (let ((cursor-location (point)) ; retain cursor location
9e7ad89a
RS
883 (word (ispell-get-word following))
884 start end poss replace)
885 ;; destructure return word info list.
886 (setq start (car (cdr word))
887 end (car (cdr (cdr word)))
888 word (car word))
889
890 ;; now check spelling of word.
891 (or quietly
892 (message "Checking spelling of %s..."
893 (funcall ispell-format-word word)))
9e7ad89a
RS
894 (process-send-string ispell-process "%\n") ;put in verbose mode
895 (process-send-string ispell-process (concat "^" word "\n"))
896 ;; wait until ispell has processed word
897 (while (progn
898 (accept-process-output ispell-process)
899 (not (string= "" (car ispell-filter)))))
900 ;;(process-send-string ispell-process "!\n") ;back to terse mode.
901 (setq ispell-filter (cdr ispell-filter))
902 (if (listp ispell-filter)
903 (setq poss (ispell-parse-output (car ispell-filter))))
904 (cond ((eq poss t)
905 (or quietly
3ee3ac77 906 (message "%s is correct" (funcall ispell-format-word word))))
9e7ad89a
RS
907 ((stringp poss)
908 (or quietly
909 (message "%s is correct because of root %s"
910 (funcall ispell-format-word word)
911 (funcall ispell-format-word poss))))
912 ((null poss) (message "Error in ispell process"))
0349bab6
RS
913 (ispell-check-only ; called from ispell minor mode.
914 (beep))
9e7ad89a 915 (t ; prompt for correct word.
29aec366
RS
916 (save-window-excursion
917 (setq replace (ispell-command-loop
918 (car (cdr (cdr poss)))
919 (car (cdr (cdr (cdr poss))))
e29702dd 920 (car poss) start end)))
9e7ad89a
RS
921 (cond ((equal 0 replace)
922 (ispell-add-per-file-word-list (car poss)))
923 (replace
9e7ad89a
RS
924 (setq word (if (atom replace) replace (car replace))
925 cursor-location (+ (- (length word) (- end start))
926 cursor-location))
e29702dd
KH
927 (if (not (equal word (car poss)))
928 (progn
929 (delete-region start end)
930 (insert word)))
9e7ad89a
RS
931 (if (not (atom replace)) ; recheck spelling of replacement
932 (progn
933 (goto-char cursor-location)
934 (ispell-word following quietly)))))
935 (if (get-buffer ispell-choices-buffer)
936 (kill-buffer ispell-choices-buffer))))
937 (goto-char cursor-location) ; return to original location
938 (ispell-pdict-save ispell-silently-savep)
939 (if ispell-quit (setq ispell-quit nil)))))
007852e0
RS
940
941
942(defun ispell-get-word (following &optional extra-otherchars)
943 "Return the word for spell-checking according to ispell syntax.
3ee3ac77 944If optional argument FOLLOWING is non-nil or if `ispell-following-word'
007852e0 945is non-nil when called interactively, then the following word
e29702dd 946\(rather than preceding\) is checked when the cursor is not over a word.
4b03da72 947Optional second argument contains otherchars that can be included in word
007852e0
RS
948many times.
949
4b03da72 950Word syntax described by `ispell-dictionary-alist' (which see)."
007852e0
RS
951 (let* ((ispell-casechars (ispell-get-casechars))
952 (ispell-not-casechars (ispell-get-not-casechars))
953 (ispell-otherchars (ispell-get-otherchars))
954 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
955 (word-regexp (concat ispell-casechars
956 "+\\("
957 ispell-otherchars
958 "?"
959 (if extra-otherchars
960 (concat extra-otherchars "?"))
961 ispell-casechars
962 "+\\)"
963 (if (or ispell-many-otherchars-p
964 extra-otherchars)
965 "*" "?")))
966 did-it-once
967 start end word)
968 ;; find the word
969 (if (not (looking-at ispell-casechars))
970 (if following
971 (re-search-forward ispell-casechars (point-max) t)
972 (re-search-backward ispell-casechars (point-min) t)))
973 ;; move to front of word
974 (re-search-backward ispell-not-casechars (point-min) 'start)
975 (while (and (or (looking-at ispell-otherchars)
976 (and extra-otherchars (looking-at extra-otherchars)))
977 (not (bobp))
978 (or (not did-it-once)
979 ispell-many-otherchars-p))
980 (if (and extra-otherchars (looking-at extra-otherchars))
981 (progn
982 (backward-char 1)
983 (if (looking-at ispell-casechars)
984 (re-search-backward ispell-not-casechars (point-min) 'move)))
985 (setq did-it-once t)
986 (backward-char 1)
987 (if (looking-at ispell-casechars)
988 (re-search-backward ispell-not-casechars (point-min) 'move)
989 (backward-char -1))))
990 ;; Now mark the word and save to string.
991 (or (re-search-forward word-regexp (point-max) t)
992 (error "No word found to check!"))
993 (setq start (match-beginning 0)
994 end (point)
995 word (buffer-substring start end))
996 (list word start end)))
997
998
999;;; Global ispell-pdict-modified-p is set by ispell-command-loop and
1000;;; tracks changes in the dictionary. The global may either be
1001;;; a value or a list, whose value is the state of whether the
1002;;; dictionary needs to be saved.
1003
1004(defun ispell-pdict-save (&optional no-query force-save)
1005 "Check to see if the personal dictionary has been modified.
3ee3ac77 1006If so, ask if it needs to be saved."
007852e0
RS
1007 (interactive (list ispell-silently-savep t))
1008 (if (and ispell-pdict-modified-p (listp ispell-pdict-modified-p))
1009 (setq ispell-pdict-modified-p (car ispell-pdict-modified-p)))
1010 (if (or ispell-pdict-modified-p force-save)
1011 (if (or no-query (y-or-n-p "Personal dictionary modified. Save? "))
1335d078
RS
1012 (progn
1013 (process-send-string ispell-process "#\n")
1014 (message "Personal dictionary saved."))))
007852e0
RS
1015 ;; unassert variable, even if not saved to avoid questioning.
1016 (setq ispell-pdict-modified-p nil))
1017
1018
29aec366 1019(defun ispell-command-loop (miss guess word start end)
007852e0
RS
1020 "Display possible corrections from list MISS.
1021GUESS lists possibly valid affix construction of WORD.
1022Returns nil to keep word.
4b03da72
RS
1023Returns 0 to insert locally into buffer-local dictionary.
1024Returns string for new chosen word.
1025Returns list for new replacement word (will be rechecked).
29aec366 1026Highlights the word, which is assumed to run from START to END.
3ee3ac77
RS
1027Global `ispell-pdict-modified-p' becomes a list where the only value
1028indicates whether the dictionary has been modified when option `a' or `i' is
007852e0 1029used."
e29702dd
KH
1030 (let ((textbuf (current-buffer))
1031 (count ?0)
1032 (line 2)
1033 (max-lines (- (window-height) 4)) ; assure 4 context lines.
1034 (choices miss)
1035 (window-min-height (min window-min-height
1036 ispell-choices-win-default-height))
1037 (command-characters '( ? ?i ?a ?A ?r ?R ?? ?x ?X ?q ?l ?u ?m ))
1038 (skipped 0)
1039 char num result textwin highlighted)
1040
1041 ;; setup the *Choices* buffer with valid data.
1042 (save-excursion
1043 (set-buffer (get-buffer-create ispell-choices-buffer))
8ce8687e 1044 (setq mode-line-format (concat "-- %b -- word: " word))
e29702dd
KH
1045 (erase-buffer)
1046 (if guess
1047 (progn
1048 (insert "Affix rules generate and capitalize "
1049 "this word as shown below:\n\t")
1050 (while guess
1051 (if (> (+ 4 (current-column) (length (car guess)))
1052 (window-width))
1053 (progn
1054 (insert "\n\t")
1055 (setq line (1+ line))))
1056 (insert (car guess) " ")
1057 (setq guess (cdr guess)))
1058 (insert "\nUse option `i' if this is a correct composition"
1059 " from the derivative root.\n")
1060 (setq line (+ line (if choices 3 2)))))
1061 (while (and choices
1062 (< (if (> (+ 7 (current-column) (length (car choices))
1063 (if (> count ?~) 3 0))
1064 (window-width))
1065 (progn
1066 (insert "\n")
1067 (setq line (1+ line)))
1068 line)
1069 max-lines))
1070 ;; not so good if there are over 20 or 30 options, but then, if
1071 ;; there are that many you don't want to scan them all anyway...
1072 (while (memq count command-characters) ; skip command characters.
1073 (setq count (1+ count)
1074 skipped (1+ skipped)))
1075 (insert "(" count ") " (car choices) " ")
1076 (setq choices (cdr choices)
1077 count (1+ count)))
1078 (setq count (- count ?0 skipped)))
1079
1080 ;; Assure word is visible
1081 (if (not (pos-visible-in-window-p end))
1082 (sit-for 0))
1083 ;; Display choices for misspelled word.
1084 (let ((choices-window (get-buffer-window ispell-choices-buffer)))
1085 (if choices-window
1335d078
RS
1086 (if (= line (window-height choices-window))
1087 (select-window choices-window)
1088 ;; *Choices* window changed size. Adjust the choices window
1089 ;; without scrolling the spelled window when possible
1090 (let ((window-line (- line (window-height choices-window)))
1091 (visible (progn (forward-line -1) (point))))
1092 (if (< line ispell-choices-win-default-height)
1093 (setq window-line (+ window-line
1094 (- ispell-choices-win-default-height
1095 line))))
1096 (move-to-window-line 0)
1097 (forward-line window-line)
1098 (set-window-start (selected-window)
1099 (if (> (point) visible) visible (point)))
1100 (goto-char end)
1101 (select-window (previous-window)) ; *Choices* window
1102 (enlarge-window window-line)))
e29702dd
KH
1103 ;; Overlay *Choices* window when it isn't showing
1104 (ispell-overlay-window (max line ispell-choices-win-default-height)))
1105 (switch-to-buffer ispell-choices-buffer)
1106 (goto-char (point-min)))
1107
1108 (select-window (setq textwin (next-window)))
1109
1110 ;; highlight word, protecting current buffer status
29aec366 1111 (unwind-protect
e29702dd 1112 (progn
29aec366 1113 (if ispell-highlight-p
e29702dd
KH
1114 (ispell-highlight-spelling-error start end t))
1115 ;; Loop until a valid choice is made.
29aec366
RS
1116 (while
1117 (eq
1118 t
1119 (setq
1120 result
1121 (progn
1122 (undo-boundary)
ab9d539c
KH
1123 (let (message-log-max)
1124 (message (concat "C-h or ? for more options; SPC to leave "
1125 "unchanged, Character to replace word")))
29aec366
RS
1126 (let ((inhibit-quit t))
1127 (setq char (if (fboundp 'read-char-exclusive)
1128 (read-char-exclusive)
1129 (read-char))
1130 skipped 0)
1131 (if (or quit-flag (= char ?\C-g)) ; C-g is like typing X
1132 (setq char ?X
1133 quit-flag nil)))
1134 ;; Adjust num to array offset skipping command characters.
1135 (let ((com-chars command-characters))
1136 (while com-chars
1137 (if (and (> (car com-chars) ?0) (< (car com-chars) char))
1138 (setq skipped (1+ skipped)))
1139 (setq com-chars (cdr com-chars)))
1140 (setq num (- char ?0 skipped)))
1141
1142 (cond
e29702dd 1143 ((= char ? ) nil) ; accept word this time only
29aec366
RS
1144 ((= char ?i) ; accept and insert word into pers dict
1145 (process-send-string ispell-process (concat "*" word "\n"))
1146 (setq ispell-pdict-modified-p '(t)) ; dictionary modified!
1147 nil)
1148 ((or (= char ?a) (= char ?A)) ; accept word without insert
1149 (process-send-string ispell-process (concat "@" word "\n"))
1150 (if (null ispell-pdict-modified-p)
1151 (setq ispell-pdict-modified-p
1152 (list ispell-pdict-modified-p)))
1153 (if (= char ?A) 0)) ; return 0 for ispell-add buffer-local
1154 ((or (= char ?r) (= char ?R)) ; type in replacement
1155 (if (or (= char ?R) ispell-query-replace-choices)
1156 (list (read-string "Query-replacement for: " word) t)
1157 (cons (read-string "Replacement for: " word) nil)))
1158 ((or (= char ??) (= char help-char) (= char ?\C-h))
1159 (ispell-help)
1160 t)
1161 ;; Quit and move point back.
1162 ((= char ?x)
1163 (ispell-pdict-save ispell-silently-savep)
1164 (message "Exited spell-checking")
1165 (setq ispell-quit t)
1166 nil)
1167 ;; Quit and preserve point.
1168 ((= char ?X)
1169 (ispell-pdict-save ispell-silently-savep)
10ba4783 1170 (message "%s"
29aec366
RS
1171 (substitute-command-keys
1172 (concat "Spell-checking suspended;"
1173 " use C-u \\[ispell-word] to resume")))
1174 (setq ispell-quit (max (point-min)
1175 (- (point) (length word))))
1176 nil)
1177 ((= char ?q)
1178 (if (y-or-n-p "Really kill Ispell process? ")
1179 (progn
1180 (ispell-kill-ispell t) ; terminate process.
1181 (setq ispell-quit (or (not ispell-checking-message)
1182 (point))
1183 ispell-pdict-modified-p nil))
e29702dd 1184 t)) ; continue if they don't quit.
29aec366
RS
1185 ((= char ?l)
1186 (let ((new-word (read-string
1187 "Lookup string (`*' is wildcard): "
1188 word))
1189 (new-line 2))
1190 (if new-word
007852e0 1191 (progn
29aec366
RS
1192 (save-excursion
1193 (set-buffer (get-buffer-create
1194 ispell-choices-buffer))
1195 (erase-buffer)
1196 (setq count ?0
1197 skipped 0
8ce8687e
RS
1198 mode-line-format (concat
1199 "-- %b -- word: "
1200 new-word)
29aec366
RS
1201 miss (lookup-words new-word)
1202 choices miss)
1203 (while (and choices ; adjust choices window.
1204 (< (if (> (+ 7 (current-column)
1205 (length (car choices))
1206 (if (> count ?~) 3 0))
1207 (window-width))
1208 (progn
1209 (insert "\n")
1210 (setq new-line
1211 (1+ new-line)))
1212 new-line)
1213 max-lines))
1214 (while (memq count command-characters)
1215 (setq count (1+ count)
1216 skipped (1+ skipped)))
1217 (insert "(" count ") " (car choices) " ")
1218 (setq choices (cdr choices)
1219 count (1+ count)))
1220 (setq count (- count ?0 skipped)))
1221 (select-window (previous-window))
1335d078
RS
1222 (if (and (/= new-line line)
1223 (> (max line new-line)
1224 ispell-choices-win-default-height))
1225 (let* ((minh ispell-choices-win-default-height)
1226 (gr-bl (if (< line minh) ; blanks
1227 (- minh line)
1228 0))
1229 (shr-bl (if (< new-line minh) ; blanks
1230 (- minh new-line)
1231 0)))
29aec366 1232 (if (> new-line line)
1335d078
RS
1233 (enlarge-window (- new-line line gr-bl))
1234 (shrink-window (- line new-line shr-bl)))
29aec366
RS
1235 (setq line new-line)))
1236 (select-window (next-window)))))
1237 t) ; reselect from new choices
1238 ((= char ?u)
1239 (process-send-string ispell-process
1240 (concat "*" (downcase word) "\n"))
1241 (setq ispell-pdict-modified-p '(t)) ; dictionary modified!
1242 nil)
1243 ((= char ?m) ; type in what to insert
1244 (process-send-string
1245 ispell-process (concat "*" (read-string "Insert: " word)
1246 "\n"))
1247 (setq ispell-pdict-modified-p '(t))
1248 (cons word nil))
1249 ((and (>= num 0) (< num count))
1250 (if ispell-query-replace-choices ; Query replace flag
1251 (list (nth num miss) 'query-replace)
1252 (nth num miss)))
1253 ((= char ?\C-l)
1254 (redraw-display) t)
1255 ((= char ?\C-r)
1256 (save-window-excursion (recursive-edit)) t)
1257 ((= char ?\C-z)
1258 (funcall (key-binding "\C-z"))
1259 t)
1260 (t (ding) t))))))
1261 result)
e29702dd
KH
1262 ;; protected
1263 (if ispell-highlight-p ; unhighlight
1264 (save-window-excursion
1265 (select-window textwin)
1266 (ispell-highlight-spelling-error start end))))))
007852e0
RS
1267
1268
829b9e73 1269;;;###autoload
007852e0 1270(defun ispell-help ()
3ee3ac77 1271 "Display a list of the options available when a misspelling is encountered.
007852e0
RS
1272
1273Selections are:
1274
1275DIGIT: Replace the word with a digit offered in the *Choices* buffer.
a9210a53
RS
1276SPC: Accept word this time.
1277`i': Accept word and insert into private dictionary.
1278`a': Accept word for this session.
1279`A': Accept word and place in `buffer-local dictionary'.
1280`r': Replace word with typed-in value. Rechecked.
1281`R': Replace word with typed-in value. Query-replaced in buffer. Rechecked.
1282`?': Show these commands.
1283`x': Exit spelling buffer. Move cursor to original point.
1284`X': Exit spelling buffer. Leaves cursor at the current point, and permits
007852e0 1285 the aborted check to be completed later.
a9210a53
RS
1286`q': Quit spelling session (Kills ispell process).
1287`l': Look up typed-in replacement in alternate dictionary. Wildcards okay.
1288`u': Like `i', but the word is lower-cased first.
1289`m': Like `i', but allows one to include dictionary completion information.
1290`C-l': redraws screen
1291`C-r': recursive edit
1292`C-z': suspend emacs or iconify frame"
007852e0 1293
4b03da72
RS
1294 (let ((help-1 (concat "[r/R]eplace word; [a/A]ccept for this session; "
1295 "[i]nsert into private dictionary"))
1296 (help-2 (concat "[l]ook a word up in alternate dictionary; "
1297 "e[x/X]it; [q]uit session"))
1298 (help-3 (concat "[u]ncapitalized insert into dictionary. "
1299 "Type 'C-h d ispell-help' for more help")))
007852e0
RS
1300 (save-window-excursion
1301 (if ispell-help-in-bufferp
1302 (progn
1303 (ispell-overlay-window 4)
1304 (switch-to-buffer (get-buffer-create "*Ispell Help*"))
1305 (insert (concat help-1 "\n" help-2 "\n" help-3))
1306 (sit-for 5)
1307 (kill-buffer "*Ispell Help*"))
1308 (select-window (minibuffer-window))
4b03da72 1309 ;;(enlarge-window 2)
007852e0
RS
1310 (erase-buffer)
1311 (cond ((string-match "Lucid" emacs-version)
1312 (message help-3)
1313 (enlarge-window 1)
1314 (message help-2)
1315 (enlarge-window 1)
4b03da72
RS
1316 (message help-1)
1317 (goto-char (point-min)))
007852e0 1318 (t
4b03da72 1319 (if (string-lessp "19" emacs-version)
007852e0
RS
1320 (message nil))
1321 (enlarge-window 2)
f27bd275
RS
1322 ;; Make sure we display the minibuffer
1323 ;; in this window, not some other.
1324 (set-minibuffer-window (selected-window))
007852e0
RS
1325 (insert (concat help-1 "\n" help-2 "\n" help-3))))
1326 (sit-for 5)
1327 (erase-buffer)))))
1328
1329
1330(defun lookup-words (word &optional lookup-dict)
1331 "Look up word in word-list dictionary.
4b03da72
RS
1332A `*' serves as a wild card. If no wild cards, `look' is used if it exists.
1333Otherwise the variable `ispell-grep-command' contains the command used to
1334search for the words (usually egrep).
1335
3ee3ac77 1336Optional second argument contains the dictionary to use; the default is
4b03da72 1337`ispell-alternate-dictionary'."
007852e0
RS
1338 ;; We don't use the filter for this function, rather the result is written
1339 ;; into a buffer. Hence there is no need to save the filter values.
1340 (if (null lookup-dict)
1341 (setq lookup-dict ispell-alternate-dictionary))
1342
1343 (let* ((process-connection-type ispell-use-ptys-p)
1344 (wild-p (string-match "\\*" word))
1345 (look-p (and ispell-look-p ; Only use look for an exact match.
1346 (or ispell-have-new-look (not wild-p))))
1347 (ispell-grep-buffer (get-buffer-create "*Ispell-Temp*")) ; result buf
1348 (prog (if look-p ispell-look-command ispell-grep-command))
1349 (args (if look-p ispell-look-options ispell-grep-options))
1350 status results loc)
1351 (unwind-protect
1352 (save-window-excursion
1353 (message "Starting \"%s\" process..." (file-name-nondirectory prog))
1354 (set-buffer ispell-grep-buffer)
1355 (if look-p
1356 nil
1357 ;; convert * to .*
1358 (insert "^" word "$")
1359 (while (search-backward "*" nil t) (insert "."))
1360 (setq word (buffer-string))
1361 (erase-buffer))
1362 (setq status (call-process prog nil t nil args word lookup-dict))
1363 ;; grep returns status 1 and no output when word not found, which
1364 ;; is a perfectly normal thing.
1365 (if (stringp status)
1366 (setq results (cons (format "error: %s exited with signal %s"
1367 (file-name-nondirectory prog) status)
1368 results))
1369 ;; else collect words into `results' in FIFO order
1370 (goto-char (point-max))
1371 ;; assure we've ended with \n
1372 (or (bobp) (= (preceding-char) ?\n) (insert ?\n))
1373 (while (not (bobp))
1374 (setq loc (point))
1375 (forward-line -1)
1376 (setq results (cons (buffer-substring (point) (1- loc))
1377 results)))))
1378 ;; protected
1379 (kill-buffer ispell-grep-buffer)
1380 (if (and results (string-match ".+: " (car results)))
1381 (error "%s error: %s" ispell-grep-command (car results))))
1382 results))
1383
1384
1385;;; "ispell-filter" is a list of output lines from the generating function.
1386;;; Each full line (ending with \n) is a separate item on the list.
1387;;; "output" can contain multiple lines, part of a line, or both.
1388;;; "start" and "end" are used to keep bounds on lines when "output" contains
1389;;; multiple lines.
1390;;; "ispell-filter-continue" is true when we have received only part of a
1391;;; line as output from a generating function ("output" did not end with \n)
a7acbbe4 1392;;; THIS FUNCTION WILL FAIL IF THE PROCESS OUTPUT DOESN'T END WITH \n!
007852e0
RS
1393;;; This is the case when a process dies or fails. The default behavior
1394;;; in this case treats the next input received as fresh input.
1395
1396(defun ispell-filter (process output)
1397 "Output filter function for ispell, grep, and look."
1398 (let ((start 0)
1399 (continue t)
1400 end)
1401 (while continue
1402 (setq end (string-match "\n" output start)) ; get text up to the newline.
1403 ;; If we get out of sync and ispell-filter-continue is asserted when we
1404 ;; are not continuing, treat the next item as a separate list. When
1405 ;; ispell-filter-continue is asserted, ispell-filter *should* always be a
1406 ;; list!
1407
1408 ;; Continue with same line (item)?
1409 (if (and ispell-filter-continue ispell-filter (listp ispell-filter))
1410 ;; Yes. Add it to the prev item
1411 (setcar ispell-filter
1412 (concat (car ispell-filter) (substring output start end)))
1413 ;; No. This is a new line and item.
1414 (setq ispell-filter
1415 (cons (substring output start end) ispell-filter)))
1416 (if (null end)
1417 ;; We've completed reading the output, but didn't finish the line.
1418 (setq ispell-filter-continue t continue nil)
1419 ;; skip over newline, this line complete.
1420 (setq ispell-filter-continue nil end (1+ end))
1421 (if (= end (length output)) ; No more lines in output
1422 (setq continue nil) ; so we can exit the filter.
1423 (setq start end)))))) ; else move start to next line of input
1424
1425
1426;;; This function destroys the mark location if it is in the word being
1427;;; highlighted.
4b03da72 1428(defun ispell-highlight-spelling-error-generic (start end &optional highlight)
3ee3ac77
RS
1429 "Highlight the word from START to END with a kludge using `inverse-video'.
1430When the optional third arg HIGHLIGHT is set, the word is highlighted;
1431otherwise it is displayed normally."
007852e0
RS
1432 (let ((modified (buffer-modified-p)) ; don't allow this fn to modify buffer
1433 (buffer-read-only nil) ; Allow highlighting read-only buffers.
1434 (text (buffer-substring start end)) ; Save highlight region
1435 (inhibit-quit t) ; inhibit interrupt processing here.
29aec366 1436 (buffer-undo-list t)) ; don't clutter the undo list.
007852e0 1437 (delete-region start end)
a7acbbe4 1438 (insert-char ? (- end start)) ; minimize amount of redisplay
007852e0
RS
1439 (sit-for 0) ; update display
1440 (if highlight (setq inverse-video (not inverse-video))) ; toggle video
1441 (delete-region start end) ; delete whitespace
1442 (insert text) ; insert text in inverse video.
1443 (sit-for 0) ; update display showing inverse video.
1444 (if highlight (setq inverse-video (not inverse-video))) ; toggle video
1445 (set-buffer-modified-p modified))) ; don't modify if flag not set.
1446
1447
4b03da72 1448(defun ispell-highlight-spelling-error-lucid (start end &optional highlight)
3ee3ac77 1449 "Highlight the word from START to END using `isearch-highlight'.
e29702dd 1450When the optional third arg HIGHLIGHT is set, the word is highlighted,
007852e0
RS
1451otherwise it is displayed normally."
1452 (if highlight
1453 (isearch-highlight start end)
1454 (isearch-dehighlight t))
1455 ;;(sit-for 0)
1456 )
1457
1458
4b03da72 1459(defun ispell-highlight-spelling-error-overlay (start end &optional highlight)
3ee3ac77
RS
1460 "Highlight the word from START to END using overlays.
1461When the optional third arg HIGHLIGHT is set, the word is highlighted
1462otherwise it is displayed normally.
4b03da72 1463
a9210a53 1464The variable `ispell-highlight-face' selects the face to use for highlighting."
007852e0
RS
1465 (if highlight
1466 (progn
1467 (setq ispell-overlay (make-overlay start end))
1468 (overlay-put ispell-overlay 'face ispell-highlight-face))
1469 (delete-overlay ispell-overlay)))
1470
1471
e29702dd 1472(defun ispell-highlight-spelling-error (start end &optional highlight)
29aec366
RS
1473 (cond
1474 ((string-match "Lucid" emacs-version)
1475 (ispell-highlight-spelling-error-lucid start end highlight))
e29702dd
KH
1476 ((and (string-lessp "19" emacs-version)
1477 (featurep 'faces) window-system)
29aec366
RS
1478 (ispell-highlight-spelling-error-overlay start end highlight))
1479 (t (ispell-highlight-spelling-error-generic start end highlight))))
007852e0 1480
e29702dd 1481
007852e0 1482(defun ispell-overlay-window (height)
3ee3ac77
RS
1483 "Create a window covering the top HEIGHT lines of the current window.
1484Ensure that the line above point is still visible but otherwise avoid
045dbcbc 1485scrolling the current window. Leave the new window selected."
007852e0
RS
1486 (save-excursion
1487 (let ((oldot (save-excursion (forward-line -1) (point)))
1488 (top (save-excursion (move-to-window-line height) (point))))
1489 ;; If line above old point (line starting at olddot) would be
1490 ;; hidden by new window, scroll it to just below new win
1491 ;; otherwise set top line of other win so it doesn't scroll.
1492 (if (< oldot top) (setq top oldot))
4b03da72
RS
1493 ;; NB: Lemacs 19.9 bug: If a window of size N (N includes the mode
1494 ;; line) is demanded, the last line is not visible.
1495 ;; At least this happens on AIX 3.2, lemacs w/ Motif, font 9x15.
1496 ;; So we increment the height for this case.
1497 (if (string-match "19\.9.*Lucid" (emacs-version))
1498 (setq height (1+ height)))
14fc129a 1499 (split-window nil height)
007852e0
RS
1500 (set-window-start (next-window) top))))
1501
1502
1503;;; Should we add a compound word match return value?
1504(defun ispell-parse-output (output)
a9210a53
RS
1505 "Parse the OUTPUT string from Ispell and return:
15061: t for an exact match.
007852e0
RS
15072: A string containing the root word for a match via suffix removal.
15083: A list of possible correct spellings of the format:
3ee3ac77
RS
1509 '(\"ORIGINAL-WORD\" OFFSET MISS-LIST GUESS-LIST)
1510 ORIGINAL-WORD is a string of the possibly misspelled word.
1511 OFFSET is an integer giving the line offset of the word.
1512 MISS-LIST and GUESS-LIST are possibly null lists of guesses and misses."
007852e0
RS
1513 (cond
1514 ((string= output "") t) ; for startup with pipes...
1515 ((string= output "*") t) ; exact match
1516 ((string= output "-") t) ; compound word match
1517 ((string= (substring output 0 1) "+") ; found cuz of root word
1518 (substring output 2)) ; return root word
1519 (t ; need to process &, ?, and #'s
1520 (let ((type (substring output 0 1)) ; &, ?, or #
1521 (original-word (substring output 2 (string-match " " output 2)))
1522 (cur-count 0) ; contains number of misses + guesses
1523 count miss-list guess-list offset)
1524 (setq output (substring output (match-end 0))) ; skip over misspelling
1525 (if (string= type "#")
1526 (setq count 0) ; no misses for type #
1527 (setq count (string-to-int output) ; get number of misses.
1528 output (substring output (1+ (string-match " " output 1)))))
1529 (setq offset (string-to-int output))
1530 (if (string= type "#") ; No miss or guess list.
1531 (setq output nil)
1532 (setq output (substring output (1+ (string-match " " output 1)))))
1533 (while output
1534 (let ((end (string-match ", \\|\\($\\)" output))) ; end of miss/guess.
1535 (setq cur-count (1+ cur-count))
1536 (if (> cur-count count)
1537 (setq guess-list (cons (substring output 0 end) guess-list))
1538 (setq miss-list (cons (substring output 0 end) miss-list)))
1539 (if (match-end 1) ; True only when at end of line.
1540 (setq output nil) ; no more misses or guesses
1541 (setq output (substring output (+ end 2))))))
1542 (list original-word offset miss-list guess-list)))))
1543
1544
1545(defun check-ispell-version ()
1546 ;; This is a little wasteful as we actually launch ispell twice: once
1547 ;; to make sure it's the right version, and once for real. But people
1548 ;; get confused by version mismatches *all* the time (and I've got the
1549 ;; email to prove it) so I think this is worthwhile. And the -v[ersion]
1550 ;; option is the only way I can think of to do this that works with
1551 ;; all versions, since versions earlier than 3.0.09 didn't identify
1552 ;; themselves on startup.
007852e0 1553 (save-excursion
1335d078
RS
1554 (let ((case-fold-search t)
1555 ;; avoid bugs when syntax of `.' changes in various default modes
1556 (default-major-mode 'fundamental-mode)
1557 status)
1558 (set-buffer (get-buffer-create " *ispell-tmp*"))
1559 (erase-buffer)
1560 (setq status (call-process ispell-program-name nil t nil "-v"))
007852e0 1561 (goto-char (point-min))
e29702dd
KH
1562 (if (not (memq status '(0 nil)))
1563 (error "%s exited with %s %s" ispell-program-name
1564 (if (stringp status) "signal" "code") status))
1565 (if (not (re-search-forward
1566 (concat "\\b\\("
1567 (regexp-quote (car ispell-required-version))
1568 "\\)\\([0-9]*\\)\\b")
1569 nil t))
1570 (error
1571 "%s version %s* is required: try renaming ispell4.el to ispell.el"
1572 ispell-program-name (car ispell-required-version))
1573 ;; check that it is the correct version.
1574 (if (< (car (read-from-string (buffer-substring
1575 (match-beginning 2) (match-end 2))))
1576 (car (cdr ispell-required-version)))
1577 (setq ispell-offset 0)))
007852e0
RS
1578 (kill-buffer (current-buffer)))))
1579
1580
1581(defun ispell-init-process ()
3ee3ac77 1582 "Check status of Ispell process and start if necessary."
007852e0
RS
1583 (if (and ispell-process
1584 (eq (process-status ispell-process) 'run)
1585 ;; If we're using a personal dictionary, assure
1586 ;; we're in the same default directory!
1587 (or (not ispell-personal-dictionary)
1588 (equal ispell-process-directory default-directory)))
1589 (setq ispell-filter nil ispell-filter-continue nil)
1590 ;; may need to restart to select new personal dictionary.
1591 (ispell-kill-ispell t)
3ee3ac77 1592 (message "Starting new Ispell process...")
007852e0
RS
1593 (sit-for 0)
1594 (check-ispell-version)
1595 (setq ispell-process
1596 (let ((process-connection-type ispell-use-ptys-p))
1597 (apply 'start-process
1598 "ispell" nil ispell-program-name
1599 "-a" ; accept single input lines
1600 "-m" ; make root/affix combos not in dict
1601 (let (args)
1602 ;; Local dictionary becomes the global dictionary in use.
1603 (if ispell-local-dictionary
1604 (setq ispell-dictionary ispell-local-dictionary))
1605 (setq args (ispell-get-ispell-args))
1606 (if ispell-dictionary ; use specified dictionary
1607 (setq args
1608 (append (list "-d" ispell-dictionary) args)))
1609 (if ispell-personal-dictionary ; use specified pers dict
1610 (setq args
1611 (append args
1612 (list "-p"
1613 (expand-file-name
1614 ispell-personal-dictionary)))))
4b03da72 1615 (setq args (append args ispell-extra-args))
007852e0
RS
1616 args)))
1617 ispell-filter nil
1618 ispell-filter-continue nil
1619 ispell-process-directory default-directory)
1620 (set-process-filter ispell-process 'ispell-filter)
ef128a91
KH
1621 (if (and enable-multibyte-characters
1622 ispell-dictionary)
1623 (set-process-coding-system ispell-process (ispell-get-coding-system)))
007852e0
RS
1624 (accept-process-output ispell-process) ; Get version ID line
1625 (cond ((null ispell-filter)
a8928dd2 1626 (error "%s did not output version line" ispell-program-name))
01ea0e3b
RS
1627 ((and
1628 (stringp (car ispell-filter))
1629 (if (string-match "warning: " (car ispell-filter))
1630 (progn
1631 (accept-process-output ispell-process 5) ; 1st was warn msg.
1632 (stringp (car ispell-filter)))
1633 (null (cdr ispell-filter)))
1634 (string-match "^@(#) " (car ispell-filter)))
007852e0
RS
1635 ;; got the version line as expected (we already know it's the right
1636 ;; version, so don't bother checking again.)
1637 nil)
1638 (t
1639 ;; Otherwise, it must be an error message. Show the user.
1640 ;; But first wait to see if some more output is going to arrive.
1641 ;; Otherwise we get cool errors like "Can't open ".
1642 (sleep-for 1)
1643 (accept-process-output)
1644 (error "%s" (mapconcat 'identity ispell-filter "\n"))))
1645 (setq ispell-filter nil) ; Discard version ID line
1646 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1647 (if extended-char-mode
1648 (process-send-string ispell-process
1649 (concat extended-char-mode "\n"))))
1650 (process-kill-without-query ispell-process)))
1651
829b9e73 1652;;;###autoload
007852e0 1653(defun ispell-kill-ispell (&optional no-error)
a9210a53
RS
1654 "Kill current Ispell process (so that you may start a fresh one).
1655With NO-ERROR, just return non-nil if there was no Ispell running."
007852e0
RS
1656 (interactive)
1657 (if (not (and ispell-process
1658 (eq (process-status ispell-process) 'run)))
1659 (or no-error
1660 (error "There is no ispell process running!"))
1661 (kill-process ispell-process)
1662 (setq ispell-process nil)
3ee3ac77 1663 (message "Ispell process killed")
007852e0
RS
1664 nil))
1665
1666
8ce8687e 1667;;; ispell-change-dictionary is set in some people's hooks. Maybe this should
1335d078
RS
1668;;; call ispell-init-process rather than wait for a spell checking command?
1669
007852e0
RS
1670;;;###autoload
1671(defun ispell-change-dictionary (dict &optional arg)
a9210a53 1672 "Change `ispell-dictionary' (q.v.) and kill old Ispell process.
007852e0
RS
1673A new one will be started as soon as necessary.
1674
1675By just answering RET you can find out what the current dictionary is.
1676
1677With prefix argument, set the default directory."
1678 (interactive
1679 (list (completing-read
1680 "Use new dictionary (RET for current, SPC to complete): "
1681 (cons (cons "default" nil) ispell-dictionary-alist) nil t)
1682 current-prefix-arg))
1683 (if (equal dict "default") (setq dict nil))
1684 ;; This relies on completing-read's bug of returning "" for no match
1685 (cond ((equal dict "")
1686 (message "Using %s dictionary"
1687 (or ispell-local-dictionary ispell-dictionary "default")))
1688 ((and (equal dict ispell-dictionary)
1335d078
RS
1689 (or (null ispell-local-dictionary)
1690 (equal dict ispell-local-dictionary)))
007852e0 1691 ;; Specified dictionary is the default already. No-op
1335d078
RS
1692 (and (interactive-p)
1693 (message "No change, using %s dictionary" (or dict "default"))))
007852e0
RS
1694 (t ; reset dictionary!
1695 (if (assoc dict ispell-dictionary-alist)
1696 (progn
1697 (if (or arg (null dict)) ; set default dictionary
1698 (setq ispell-dictionary dict))
1699 (if (null arg) ; set local dictionary
1700 (setq ispell-local-dictionary dict)))
1701 (error "Illegal dictionary: %s" dict))
1702 (ispell-kill-ispell t)
3ee3ac77 1703 (message "(Next %sIspell command will use %s dictionary)"
007852e0
RS
1704 (cond ((equal ispell-local-dictionary ispell-dictionary)
1705 "")
1706 (arg "global ")
1707 (t "local "))
1708 (or (if (or (equal ispell-local-dictionary ispell-dictionary)
1709 (null arg))
1710 ispell-local-dictionary
1711 ispell-dictionary)
1712 "default")))))
1713
1714
1715;;; Spelling of comments are checked when ispell-check-comments is non-nil.
1716
1717;;;###autoload
1718(defun ispell-region (reg-start reg-end)
1719 "Interactively check a region for spelling errors."
1720 (interactive "r") ; Don't flag errors on read-only bufs.
1721 (ispell-accept-buffer-local-defs) ; set up dictionary, local words, etc.
1722 (unwind-protect
4b03da72 1723 (save-excursion
e29702dd 1724 (message "Spell checking %s using %s dictionary..."
4b03da72 1725 (if (and (= reg-start (point-min)) (= reg-end (point-max)))
e29702dd
KH
1726 (buffer-name) "region")
1727 (or ispell-dictionary "default"))
1728 ;; Returns cursor to original location.
4b03da72 1729 (save-window-excursion
4b03da72 1730 (goto-char reg-start)
1335d078
RS
1731 (let ((transient-mark-mode nil)
1732 ref-type)
4b03da72
RS
1733 (while (and (not ispell-quit) (< (point) reg-end))
1734 (let ((start (point))
1735 (offset-change 0)
1736 (end (save-excursion (end-of-line) (min (point) reg-end)))
1737 (ispell-casechars (ispell-get-casechars))
1738 string)
1739 (cond ; LOOK AT THIS LINE AND SKIP OR PROCESS
1740 ((eolp) ; END OF LINE, just go to next line.
1741 (forward-char 1))
e29702dd 1742 ((and (null ispell-check-comments) ; SKIPPING COMMENTS
4b03da72
RS
1743 comment-start ; skip comments that start on the line.
1744 (search-forward comment-start end t)) ; or found here.
1745 (if (= (- (point) start) (length comment-start))
1746 ;; comment starts the line. Skip entire line or region
1747 (if (string= "" comment-end) ; skip to next line
1748 (beginning-of-line 2) ; or jump to comment end.
1749 (search-forward comment-end reg-end 'limit))
1750 ;; Comment later in line. Check spelling before comment.
1751 (let ((limit (- (point) (length comment-start))))
1752 (goto-char (1- limit))
1753 (if (looking-at "\\\\") ; "quoted" comment, don't skip
1754 ;; quoted comment. Skip over comment-start
1755 (if (= start (1- limit))
1756 (setq limit (+ limit (length comment-start)))
1757 (setq limit (1- limit))))
1758 (goto-char start)
1759 ;; Only check when "casechars" or math before comment
1760 (if (or (re-search-forward ispell-casechars limit t)
1761 (re-search-forward "[][()$]" limit t))
1762 (setq string
1763 (concat "^" (buffer-substring start limit)
e29702dd
KH
1764 "\n")
1765 offset-change (- offset-change ispell-offset)))
4b03da72 1766 (goto-char limit))))
1335d078
RS
1767 ((looking-at "[---#@*+!%~^]") ; SKIP SPECIAL ISPELL CHARACTERS
1768 (forward-char 1))
1769 ((or (and ispell-skip-tib ; SKIP TIB REFERENCES OR SGML MARKUP
1770 (re-search-forward ispell-tib-ref-beginning end t)
1771 (setq ref-type 'tib))
1772 (and ispell-skip-sgml
651939b5 1773 (re-search-forward "[<&]" end t)
1335d078
RS
1774 (setq ref-type 'sgml)))
1775 (if (or (and (eq 'tib ref-type) ; tib tag is 2 chars.
1776 (= (- (point) 2) start))
1777 (and (eq 'sgml ref-type) ; sgml skips 1 char.
1778 (= (- (point) 1) start)))
1779 ;; Skip to end of reference, not necessarily on this line
1780 ;; Return an error if tib/sgml reference not found
1781 (if (or
1782 (and
1783 (eq 'tib ref-type)
1784 (not
1785 (re-search-forward ispell-tib-ref-end reg-end t)))
1786 (and (eq 'sgml ref-type)
651939b5 1787 (not (re-search-forward "[>;]" reg-end t))))
4b03da72
RS
1788 (progn
1789 (ispell-pdict-save ispell-silently-savep)
1790 (ding)
1791 (message
1792 (concat
1335d078
RS
1793 "Open tib or SGML command. Fix buffer or set "
1794 (if (eq 'tib ref-type)
1795 "ispell-skip-tib"
1796 "ispell-skip-sgml")
1797 " to nil"))
4b03da72
RS
1798 ;; keep cursor at error location
1799 (setq ispell-quit (- (point) 2))))
1335d078
RS
1800 ;; Check spelling between reference and start of the line.
1801 (let ((limit (- (point) (if (eq 'tib ref-type) 2 1))))
4b03da72
RS
1802 (goto-char start)
1803 (if (or (re-search-forward ispell-casechars limit t)
1804 (re-search-forward "[][()$]" limit t))
1805 (setq string
1806 (concat "^" (buffer-substring start limit)
e29702dd
KH
1807 "\n")
1808 offset-change (- offset-change ispell-offset)))
4b03da72 1809 (goto-char limit))))
4b03da72
RS
1810 ((or (re-search-forward ispell-casechars end t) ; TEXT EXISTS
1811 (re-search-forward "[][()$]" end t)) ; or MATH COMMANDS
e29702dd
KH
1812 (setq string (concat "^" (buffer-substring start end) "\n")
1813 offset-change (- offset-change ispell-offset))
4b03da72
RS
1814 (goto-char end))
1815 (t (beginning-of-line 2))) ; EMPTY LINE, skip it.
1816
1817 (setq end (point)) ; "end" tracks end of region to check.
1818
1819 (if string ; there is something to spell!
1820 (let (poss)
1821 ;; send string to spell process and get input.
1822 (process-send-string ispell-process string)
1823 (while (progn
1824 (accept-process-output ispell-process)
1825 ;; Last item of output contains a blank line.
1826 (not (string= "" (car ispell-filter)))))
1827 ;; parse all inputs from the stream one word at a time.
1828 ;; Place in FIFO order and remove the blank item.
1829 (setq ispell-filter (nreverse (cdr ispell-filter)))
1830 (while (and (not ispell-quit) ispell-filter)
1831 (setq poss (ispell-parse-output (car ispell-filter)))
1832 (if (listp poss) ; spelling error occurred.
ef128a91
KH
1833 (let* ((word-start
1834 (if (and enable-multibyte-characters
1835 (ispell-get-coding-system))
1836 ;; OFFSET returned by ispell
1837 ;; counts non-ASCII chars as
1838 ;; one, so just adding OFFSET
1839 ;; to START will cause an
1840 ;; error.
1841 (save-excursion
1842 (goto-char (+ start offset-change))
1843 (forward-char (car (cdr poss)))
1844 (point))
1845 (+ start offset-change
1846 (car (cdr poss)))))
4b03da72
RS
1847 (word-end (+ word-start
1848 (length (car poss))))
1849 replace)
1850 (goto-char word-start)
1851 ;; Adjust the horizontal scroll & point
1852 (ispell-horiz-scroll)
1853 (goto-char word-end)
1854 (ispell-horiz-scroll)
1855 (goto-char word-start)
1856 (ispell-horiz-scroll)
1857 (if (/= word-end
1858 (progn
1859 (search-forward (car poss) word-end t)
1860 (point)))
1861 ;; This occurs due to filter pipe problems
1862 (error
1863 (concat "Ispell misalignment: word "
1864 "`%s' point %d; please retry")
1865 (car poss) word-start))
a594e0e1
RS
1866 (if (not (pos-visible-in-window-p))
1867 (sit-for 0))
29aec366
RS
1868 (if ispell-keep-choices-win
1869 (setq replace
1870 (ispell-command-loop
1871 (car (cdr (cdr poss)))
1872 (car (cdr (cdr (cdr poss))))
e29702dd 1873 (car poss) word-start word-end))
29aec366
RS
1874 (save-window-excursion
1875 (setq replace
1876 (ispell-command-loop
1877 (car (cdr (cdr poss)))
1878 (car (cdr (cdr (cdr poss))))
e29702dd 1879 (car poss) word-start word-end))))
4b03da72
RS
1880 (cond
1881 ((and replace (listp replace))
1882 ;; REPLACEMENT WORD entered. Recheck line
1883 ;; starting with the replacement word.
1884 (setq ispell-filter nil
1885 string (buffer-substring word-start
1886 word-end))
1887 (let ((change (- (length (car replace))
1888 (length (car poss)))))
1889 ;; adjust regions
1890 (setq reg-end (+ reg-end change)
1891 offset-change (+ offset-change
1892 change)))
e29702dd
KH
1893 (if (not (equal (car replace) (car poss)))
1894 (progn
1895 (delete-region word-start word-end)
1896 (insert (car replace))))
4b03da72
RS
1897 ;; I only need to recheck typed-in replacements
1898 (if (not (eq 'query-replace
1899 (car (cdr replace))))
1900 (backward-char (length (car replace))))
1901 (setq end (point)) ; reposition for recheck
1902 ;; when second arg exists, query-replace, saving regions
1903 (if (car (cdr replace))
1904 (unwind-protect
1905 (save-window-excursion
1906 (set-marker
1907 ispell-query-replace-marker reg-end)
1908 ;; Assume case-replace &
1909 ;; case-fold-search correct?
1910 (query-replace string (car replace)
1911 t))
1912 (setq reg-end
1913 (marker-position
1914 ispell-query-replace-marker))
1915 (set-marker ispell-query-replace-marker
1916 nil))))
1917 ((or (null replace)
1918 (equal 0 replace)) ; ACCEPT/INSERT
1919 (if (equal 0 replace) ; BUFFER-LOCAL DICT ADD
1920 (setq reg-end
1921 (ispell-add-per-file-word-list
1922 (car poss) reg-end)))
1923 ;; This avoids pointing out the word that was
1924 ;; just accepted (via 'i' or 'a') if it follows
1925 ;; on the same line.
1926 ;; Redo check following the accepted word.
1927 (if (and ispell-pdict-modified-p
1928 (listp ispell-pdict-modified-p))
1929 ;; Word accepted. Recheck line.
1930 (setq ispell-pdict-modified-p ; update flag
1931 (car ispell-pdict-modified-p)
1932 ispell-filter nil ; discontinue check
1933 end word-start))) ; reposition loc.
1934 (replace ; STRING REPLACEMENT for this word.
1935 (delete-region word-start word-end)
1936 (insert replace)
1937 (let ((change (- (length replace)
1938 (length (car poss)))))
1939 (setq reg-end (+ reg-end change)
1940 offset-change (+ offset-change change)
1941 end (+ end change)))))
1942 (if (not ispell-quit)
ab9d539c
KH
1943 (let (message-log-max)
1944 (message "Continuing spelling check using %s dictionary..."
1945 (or ispell-dictionary "default"))))
4b03da72
RS
1946 (sit-for 0)))
1947 ;; finished with line!
1948 (setq ispell-filter (cdr ispell-filter)))))
1949 (goto-char end)))))
1950 (not ispell-quit))
1951 ;; protected
1952 (if (get-buffer ispell-choices-buffer)
1953 (kill-buffer ispell-choices-buffer))
1954 (if ispell-quit
1955 (progn
1956 ;; preserve or clear the region for ispell-continue.
1957 (if (not (numberp ispell-quit))
1958 (set-marker ispell-region-end nil)
1959 ;; Enable ispell-continue.
1960 (set-marker ispell-region-end reg-end)
1961 (goto-char ispell-quit))
1962 ;; Check for aborting
1963 (if (and ispell-checking-message (numberp ispell-quit))
1964 (progn
1965 (setq ispell-quit nil)
1966 (error "Message send aborted.")))
1967 (setq ispell-quit nil))
1968 (set-marker ispell-region-end nil)
1969 ;; Only save if successful exit.
1970 (ispell-pdict-save ispell-silently-savep)
1971 (message "Spell-checking done"))))
007852e0
RS
1972
1973
1974
1975;;;###autoload
4b03da72 1976(defun ispell-buffer ()
007852e0
RS
1977 "Check the current buffer for spelling errors interactively."
1978 (interactive)
1979 (ispell-region (point-min) (point-max)))
1980
4b03da72 1981
829b9e73 1982;;;###autoload
007852e0
RS
1983(defun ispell-continue ()
1984 (interactive)
1985 "Continue a spelling session after making some changes."
1986 (if (not (marker-position ispell-region-end))
1987 (message "No session to continue. Use 'X' command when checking!")
1988 (if (not (equal (marker-buffer ispell-region-end) (current-buffer)))
1989 (message "Must continue ispell from buffer %s"
1990 (buffer-name (marker-buffer ispell-region-end)))
1991 (ispell-region (point) (marker-position ispell-region-end)))))
1992
1993
1994;;; Horizontal scrolling
4b03da72
RS
1995(defun ispell-horiz-scroll ()
1996 "Places point within the horizontal visibility of its window area."
007852e0
RS
1997 (if truncate-lines ; display truncating lines?
1998 ;; See if display needs to be scrolled.
1999 (let ((column (- (current-column) (max (window-hscroll) 1))))
2000 (if (and (< column 0) (> (window-hscroll) 0))
2001 (scroll-right (max (- column) 10))
2002 (if (>= column (- (window-width) 2))
2003 (scroll-left (max (- column (window-width) -3) 10)))))))
2004
2005
2006;;; Interactive word completion.
2007;;; Forces "previous-word" processing. Do we want to make this selectable?
2008
2009;;;###autoload
2010(defun ispell-complete-word (&optional interior-frag)
2011 "Look up word before or under point in dictionary (see lookup-words command)
2012and try to complete it. If optional INTERIOR-FRAG is non-nil then the word
2013may be a character sequence inside of a word.
2014
2015Standard ispell choices are then available."
2016 (interactive "P")
2017 (let ((cursor-location (point))
4b03da72 2018 case-fold-search
007852e0
RS
2019 (word (ispell-get-word nil "\\*")) ; force "previous-word" processing.
2020 start end possibilities replacement)
2021 (setq start (car (cdr word))
2022 end (car (cdr (cdr word)))
2023 word (car word)
2024 possibilities
2025 (or (string= word "") ; Will give you every word
2026 (lookup-words (concat (if interior-frag "*") word "*")
2027 ispell-complete-word-dict)))
2028 (cond ((eq possibilities t)
2029 (message "No word to complete"))
2030 ((null possibilities)
2031 (message "No match for \"%s\"" word))
2032 (t ; There is a modification...
4b03da72
RS
2033 (cond ; Try and respect case of word.
2034 ((string-match "^[^A-Z]+$" word)
2035 (setq possibilities (mapcar 'downcase possibilities)))
2036 ((string-match "^[^a-z]+$" word)
2037 (setq possibilities (mapcar 'upcase possibilities)))
2038 ((string-match "^[A-Z]" word)
2039 (setq possibilities (mapcar 'capitalize possibilities))))
29aec366
RS
2040 (save-window-excursion
2041 (setq replacement
2042 (ispell-command-loop possibilities nil word start end)))
007852e0
RS
2043 (cond
2044 ((equal 0 replacement) ; BUFFER-LOCAL ADDITION
2045 (ispell-add-per-file-word-list word))
2046 (replacement ; REPLACEMENT WORD
2047 (delete-region start end)
2048 (setq word (if (atom replacement) replacement (car replacement))
2049 cursor-location (+ (- (length word) (- end start))
2050 cursor-location))
2051 (insert word)
2052 (if (not (atom replacement)) ; recheck spelling of replacement.
2053 (progn
2054 (goto-char cursor-location)
2055 (ispell-word nil t)))))
2056 (if (get-buffer ispell-choices-buffer)
2057 (kill-buffer ispell-choices-buffer))))
2058 (ispell-pdict-save ispell-silently-savep)
2059 (goto-char cursor-location)))
2060
2061
2062;;;###autoload
2063(defun ispell-complete-word-interior-frag ()
4b03da72 2064 "Completes word matching character sequence inside a word."
007852e0
RS
2065 (interactive)
2066 (ispell-complete-word t))
2067
e29702dd 2068
0349bab6
RS
2069;;; **********************************************************************
2070;;; Ispell Minor Mode
2071;;; **********************************************************************
2072
2073(defvar ispell-minor-mode nil
2074 "Non-nil if Ispell minor mode is enabled.")
2075;; Variable indicating that ispell minor mode is active.
2076(make-variable-buffer-local 'ispell-minor-mode)
2077
2078(or (assq 'ispell-minor-mode minor-mode-alist)
2079 (setq minor-mode-alist
2080 (cons '(ispell-minor-mode " Spell") minor-mode-alist)))
2081
2082(defvar ispell-minor-keymap
2083 (let ((map (make-sparse-keymap)))
2084 (define-key map " " 'ispell-minor-check)
2085 (define-key map "\r" 'ispell-minor-check)
2086 map)
2087 "Keymap used for Ispell minor mode.")
2088
2089(or (not (boundp 'minor-mode-map-alist))
2090 (assoc 'ispell-minor-mode minor-mode-map-alist)
2091 (setq minor-mode-map-alist
2092 (cons (cons 'ispell-minor-mode ispell-minor-keymap)
2093 minor-mode-map-alist)))
2094
2095;;;###autoload
2096(defun ispell-minor-mode (&optional arg)
2097 "Toggle Ispell minor mode.
2098With prefix arg, turn Ispell minor mode on iff arg is positive.
2099
2100In Ispell minor mode, pressing SPC or RET
2101warns you if the previous word is incorrectly spelled."
2102 (interactive "P")
2103 (setq ispell-minor-mode
2104 (not (or (and (null arg) ispell-minor-mode)
2105 (<= (prefix-numeric-value arg) 0))))
2bcf8edb 2106 (force-mode-line-update))
0349bab6
RS
2107
2108(defun ispell-minor-check ()
2109 ;; Check previous word then continue with the normal binding of this key.
2110 (interactive "*")
2111 (let ((ispell-minor-mode nil)
2112 (ispell-check-only t))
2113 (save-restriction
2114 (narrow-to-region (point-min) (point))
2115 (ispell-word nil t))
2116 (call-interactively (key-binding (this-command-keys)))))
007852e0 2117
e29702dd 2118
007852e0
RS
2119;;; **********************************************************************
2120;;; Ispell Message
2121;;; **********************************************************************
7492978d
RS
2122;;; Original from D. Quinlan, E. Bradford, A. Albert, and M. Ernst
2123
2124
2125(defvar ispell-message-text-end
2126 (mapconcat (function identity)
2127 '(
2128 ;; Matches postscript files.
8ce8687e 2129 "^%!PS-Adobe-[123].0"
7492978d
RS
2130 ;; Matches uuencoded text
2131 "^begin [0-9][0-9][0-9] .*\nM.*\nM.*\nM"
2132 ;; Matches shell files (esp. auto-decoding)
e29702dd 2133 "^#! /bin/[ck]?sh"
8581f180
RS
2134 ;; Matches context difference listing
2135 "\\(diff -c .*\\)?\n\\*\\*\\* .*\n--- .*\n\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*"
f1f9f8d5
RS
2136 ;; Matches reporter.el bug report
2137 "^current state:\n==============\n"
7492978d 2138 ;; Matches "----------------- cut here"
441c2ae4
RS
2139 ;; and "------- Start of forwarded message",
2140 ;; or either one with "- " in front.
2141 "^\\(- \\)?[-=_]+\\s ?\\(cut here\\|\\(Start of \\)?forwarded message\\)")
7492978d
RS
2142 "\\|")
2143 "*End of text which will be checked in ispell-message.
e29702dd 2144If it is a string, limit at first occurrence of that regular expression.
7492978d 2145Otherwise, it must be a function which is called to get the limit.")
007852e0 2146
4b03da72 2147
8ce8687e
RS
2148(defvar ispell-message-start-skip
2149 (mapconcat (function identity)
2150 '(
2151 ;; Matches forwarded messages
2152 "^---* Forwarded Message"
2153 ;; Matches PGP Public Key block
2154 "^---*BEGIN PGP [A-Z ]*--*"
2155 )
2156 "\\|")
2157 "Spelling is skipped inside these start/end groups by ispell-message.
2158Assumed that blocks are not mutually inclusive.")
2159
2160
2161(defvar ispell-message-end-skip
2162 (mapconcat (function identity)
2163 '(
2164 ;; Matches forwarded messages
2165 "^--- End of Forwarded Message"
2166 ;; Matches PGP Public Key block
2167 "^---*END PGP [A-Z ]*--*"
2168 )
2169 "\\|")
2170 "Spelling is skipped inside these start/end groups by ispell-message.
2171Assumed that blocks are not mutually inclusive.")
2172
2173
007852e0
RS
2174;;;###autoload
2175(defun ispell-message ()
2176 "Check the spelling of a mail message or news post.
2177Don't check spelling of message headers except the Subject field.
2178Don't check included messages.
2179
6ec7837b
RS
2180To abort spell checking of a message region and send the message anyway,
2181use the `x' command. (Any subsequent regions will be checked.)
a9210a53 2182The `X' command aborts the message send so that you can edit the buffer.
007852e0
RS
2183
2184To spell-check whenever a message is sent, include the appropriate lines
2185in your .emacs file:
6ec7837b 2186 (add-hook 'message-send-hook 'ispell-message)
007852e0
RS
2187 (add-hook 'mail-send-hook 'ispell-message)
2188 (add-hook 'mh-before-send-letter-hook 'ispell-message)
2189
e29702dd 2190You can bind this to the key C-c i in GNUS or mail by adding to
a9210a53 2191`news-reply-mode-hook' or `mail-mode-hook' the following lambda expression:
007852e0
RS
2192 (function (lambda () (local-set-key \"\\C-ci\" 'ispell-message)))"
2193 (interactive)
7492978d 2194 (save-excursion
4b03da72
RS
2195 (goto-char (point-min))
2196 (let* ((internal-messagep (save-excursion
2197 (re-search-forward
2198 (concat "^"
2199 (regexp-quote mail-header-separator)
2200 "$")
2201 nil t)))
ae9fdfe5 2202 (limit (copy-marker
4b03da72
RS
2203 (cond
2204 ((not ispell-message-text-end) (point-max))
2205 ((char-or-string-p ispell-message-text-end)
2206 (if (re-search-forward ispell-message-text-end nil t)
2207 (match-beginning 0)
2208 (point-max)))
2209 (t (min (point-max) (funcall ispell-message-text-end))))))
7492978d
RS
2210 (cite-regexp ;Prefix of inserted text
2211 (cond
2212 ((featurep 'supercite) ; sc 3.0
2213 (concat "\\(" (sc-cite-regexp) "\\)" "\\|"
2214 (ispell-non-empty-string sc-reference-tag-string)))
2215 ((featurep 'sc) ; sc 2.3
2216 (concat "\\(" sc-cite-regexp "\\)" "\\|"
2217 (ispell-non-empty-string sc-reference-tag-string)))
6ec7837b 2218 ((equal major-mode 'news-reply-mode) ;GNUS 4 & below
7492978d
RS
2219 (concat "In article <" "\\|"
2220 (if mail-yank-prefix
2221 (ispell-non-empty-string mail-yank-prefix)
2222 "^ \\|^\t")))
6ec7837b
RS
2223 ((equal major-mode 'message-mode) ;GNUS 5
2224 (concat ".*@.* writes:$" "\\|"
2225 (if mail-yank-prefix
2226 (ispell-non-empty-string mail-yank-prefix)
2227 "^ \\|^\t")))
7492978d
RS
2228 ((equal major-mode 'mh-letter-mode) ; mh mail message
2229 (ispell-non-empty-string mh-ins-buf-prefix))
4b03da72 2230 ((not internal-messagep) ; Assume n sent us this message.
7492978d
RS
2231 (concat "In [a-zA-Z.]+ you write:" "\\|"
2232 "In <[^,;&+=]+> [^,;&+=]+ writes:" "\\|"
2233 " *> *"))
2234 ((boundp 'vm-included-text-prefix) ; VM mail message
2235 (concat "[^,;&+=]+ writes:" "\\|"
2236 (ispell-non-empty-string vm-included-text-prefix)))
2237 (mail-yank-prefix ; vanilla mail message.
2238 (ispell-non-empty-string mail-yank-prefix))
2239 (t "^ \\|^\t")))
4b03da72
RS
2240 (cite-regexp-start (concat "^[ \t]*$\\|" cite-regexp))
2241 (cite-regexp-end (concat "^\\(" cite-regexp "\\)"))
2242 (old-case-fold-search case-fold-search)
2243 (case-fold-search t)
2244 (ispell-checking-message t))
2245 (goto-char (point-min))
007852e0
RS
2246 ;; Skip header fields except Subject: without Re:'s
2247 ;;(search-forward mail-header-separator nil t)
2248 (while (if internal-messagep
2249 (< (point) internal-messagep)
4b03da72 2250 (and (looking-at "[a-zA-Z---]+:\\|\t\\| ")
007852e0 2251 (not (eobp))))
4b03da72 2252 (if (looking-at "Subject: *") ; Spell check new subject fields
7492978d
RS
2253 (progn
2254 (goto-char (match-end 0))
2255 (if (and (not (looking-at ".*Re\\>"))
2256 (not (looking-at "\\[")))
2257 (let ((case-fold-search old-case-fold-search))
2258 (ispell-region (point)
2259 (progn
2260 (end-of-line)
2261 (while (looking-at "\n[ \t]")
2262 (end-of-line 2))
2263 (point)))))))
007852e0
RS
2264 (forward-line 1))
2265 (setq case-fold-search nil)
2266 ;; Skip mail header, particularly for non-english languages.
e7a52e41 2267 (if (looking-at (concat (regexp-quote mail-header-separator) "$"))
007852e0 2268 (forward-line 1))
7492978d 2269 (while (< (point) limit)
007852e0
RS
2270 ;; Skip across text cited from other messages.
2271 (while (and (looking-at cite-regexp-start)
e29702dd
KH
2272 (< (point) limit)
2273 (zerop (forward-line 1))))
1335d078 2274
7492978d 2275 (if (< (point) limit)
1335d078
RS
2276 (let* ((start (point))
2277 ;; Check the next batch of lines that *aren't* cited.
2278 (end-c (and (re-search-forward cite-regexp-end limit 'end)
2279 (match-beginning 0)))
8ce8687e 2280 ;; Skip a block of included text.
1335d078 2281 (end-fwd (and (goto-char start)
8ce8687e
RS
2282 (re-search-forward ispell-message-start-skip
2283 limit 'end)
2284 (progn (beginning-of-line)
2285 (point))))
1335d078
RS
2286 (end (or (and end-c end-fwd (min end-c end-fwd))
2287 end-c end-fwd
a7acbbe4 2288 ;; default to limit of text.
1335d078
RS
2289 (marker-position limit))))
2290 (goto-char start)
2291 (ispell-region start end)
2292 (if (and end-fwd (= end end-fwd))
2293 (progn
2294 (goto-char end)
8ce8687e 2295 (re-search-forward ispell-message-end-skip limit 'end))
1335d078 2296 (goto-char end)))))
4b03da72
RS
2297 (set-marker limit nil))))
2298
007852e0
RS
2299
2300(defun ispell-non-empty-string (string)
2301 (if (or (not string) (string-equal string ""))
2302 "\\'\\`" ; An unmatchable string if string is null.
2303 (regexp-quote string)))
2304
2305
2306;;; **********************************************************************
2307;;; Buffer Local Functions
2308;;; **********************************************************************
2309
2310
2311(defun ispell-accept-buffer-local-defs ()
4b03da72 2312 "Load all buffer-local information, restarting ispell when necessary."
007852e0
RS
2313 (ispell-buffer-local-dict) ; May kill ispell-process.
2314 (ispell-buffer-local-words) ; Will initialize ispell-process.
2315 (ispell-buffer-local-parsing))
2316
2317
007852e0 2318(defun ispell-buffer-local-parsing ()
a9210a53 2319 "Place Ispell into parsing mode for this buffer.
4b03da72
RS
2320Overrides the default parsing mode.
2321Includes latex/nroff modes and extended character mode."
007852e0
RS
2322 ;; (ispell-init-process) must already be called.
2323 (process-send-string ispell-process "!\n") ; Put process in terse mode.
2324 ;; We assume all major modes with "tex-mode" in them should use latex parsing
2325 (if (or (and (eq ispell-parser 'use-mode-name)
2326 (string-match "[Tt][Ee][Xx]-mode" (symbol-name major-mode)))
2327 (eq ispell-parser 'tex))
2328 (process-send-string ispell-process "+\n") ; set ispell mode to tex
2329 (process-send-string ispell-process "-\n")) ; set mode to normal (nroff)
8ce8687e
RS
2330 ;; Hard-wire test for SGML & HTML mode.
2331 (setq ispell-skip-sgml (memq major-mode '(sgml-mode html-mode)))
007852e0
RS
2332 ;; Set default extended character mode for given buffer, if any.
2333 (let ((extended-char-mode (ispell-get-extended-character-mode)))
2334 (if extended-char-mode
2335 (process-send-string ispell-process (concat extended-char-mode "\n"))))
e29702dd 2336 ;; Set buffer-local parsing mode and extended character mode, if specified.
007852e0
RS
2337 (save-excursion
2338 (goto-char (point-min))
2339 ;; Uses last valid definition
2340 (while (search-forward ispell-parsing-keyword nil t)
2341 (let ((end (save-excursion (end-of-line) (point)))
2342 (case-fold-search t)
2343 string)
2344 (while (re-search-forward " *\\([^ \"]+\\)" end t)
2345 ;; space separated definitions.
2346 (setq string (buffer-substring (match-beginning 1) (match-end 1)))
2347 (cond ((string-match "latex-mode" string)
e29702dd 2348 (process-send-string ispell-process "+\n~tex\n"))
007852e0 2349 ((string-match "nroff-mode" string)
e29702dd 2350 (process-send-string ispell-process "-\n~nroff"))
007852e0
RS
2351 ((string-match "~" string) ; Set extended character mode.
2352 (process-send-string ispell-process (concat string "\n")))
2353 (t (message "Illegal Ispell Parsing argument!")
2354 (sit-for 2))))))))
2355
2356
2357;;; Can kill the current ispell process
2358
2359(defun ispell-buffer-local-dict ()
4b03da72 2360 "Initializes local dictionary.
007852e0 2361When a dictionary is defined in the buffer (see variable
a9210a53 2362`ispell-dictionary-keyword'), it will override the local setting
007852e0
RS
2363from \\[ispell-change-dictionary].
2364Both should not be used to define a buffer-local dictionary."
2365 (save-excursion
2366 (goto-char (point-min))
2367 (let (end)
2368 ;; Override the local variable definition.
2369 ;; Uses last valid definition.
2370 (while (search-forward ispell-dictionary-keyword nil t)
2371 (setq end (save-excursion (end-of-line) (point)))
2372 (if (re-search-forward " *\\([^ \"]+\\)" end t)
2373 (setq ispell-local-dictionary
2374 (buffer-substring (match-beginning 1) (match-end 1)))))
2375 (goto-char (point-min))
2376 (while (search-forward ispell-pdict-keyword nil t)
2377 (setq end (save-excursion (end-of-line) (point)))
2378 (if (re-search-forward " *\\([^ \"]+\\)" end t)
2379 (setq ispell-local-pdict
2380 (buffer-substring (match-beginning 1) (match-end 1)))))))
2381 ;; Reload if new personal dictionary defined.
2382 (if (and ispell-local-pdict
2383 (not (equal ispell-local-pdict ispell-personal-dictionary)))
2384 (progn
2385 (ispell-kill-ispell t)
2386 (setq ispell-personal-dictionary ispell-local-pdict)))
2387 ;; Reload if new dictionary defined.
2388 (if (and ispell-local-dictionary
2389 (not (equal ispell-local-dictionary ispell-dictionary)))
2390 (ispell-change-dictionary ispell-local-dictionary)))
2391
2392
2393(defun ispell-buffer-local-words ()
a9210a53 2394 "Loads the buffer-local dictionary in the current buffer."
007852e0
RS
2395 (if (and ispell-buffer-local-name
2396 (not (equal ispell-buffer-local-name (buffer-name))))
2397 (progn
2398 (ispell-kill-ispell t)
2399 (setq ispell-buffer-local-name nil)))
2400 (ispell-init-process)
2401 (save-excursion
2402 (goto-char (point-min))
2403 (while (search-forward ispell-words-keyword nil t)
2404 (or ispell-buffer-local-name
2405 (setq ispell-buffer-local-name (buffer-name)))
2406 (let ((end (save-excursion (end-of-line) (point)))
2407 string)
e29702dd
KH
2408 ;; buffer-local words separated by a space, and can contain
2409 ;; any character other than a space.
7195bb71 2410 (while (re-search-forward " *\\([^ ]+\\)" end t)
007852e0 2411 (setq string (buffer-substring (match-beginning 1) (match-end 1)))
4b03da72 2412 (process-send-string ispell-process (concat "@" string "\n")))))))
007852e0
RS
2413
2414
2415;;; returns optionally adjusted region-end-point.
2416
2417(defun ispell-add-per-file-word-list (word &optional reg-end)
2418 "Adds new word to the per-file word list."
2419 (or ispell-buffer-local-name
2420 (setq ispell-buffer-local-name (buffer-name)))
2421 (if (null reg-end)
2422 (setq reg-end 0))
2423 (save-excursion
2424 (goto-char (point-min))
4b03da72 2425 (let (case-fold-search line-okay search done string)
007852e0
RS
2426 (while (not done)
2427 (setq search (search-forward ispell-words-keyword nil 'move)
2428 line-okay (< (+ (length word) 1 ; 1 for space after word..
2429 (progn (end-of-line) (current-column)))
2430 80))
2431 (if (or (and search line-okay)
2432 (null search))
2433 (progn
2434 (setq done t)
2435 (if (null search)
2436 (progn
2437 (open-line 1)
2438 (setq string (concat comment-start " "
2439 ispell-words-keyword))
2440 ;; in case the keyword is in the middle of the file....
2441 (if (> reg-end (point))
2442 (setq reg-end (+ reg-end (length string))))
2443 (insert string)
2444 (if (and comment-end (not (equal "" comment-end)))
2445 (save-excursion
2446 (open-line 1)
2447 (forward-line 1)
2448 (insert comment-end)))))
2449 (if (> reg-end (point))
2450 (setq reg-end (+ 1 reg-end (length word))))
2451 (insert (concat " " word)))))))
2452 reg-end)
2453
2454
8ce8687e 2455(defconst ispell-version "2.37 -- Tue Jun 13 12:05:28 EDT 1995")
007852e0
RS
2456
2457(provide 'ispell)
2458
2459\f
2460;;; LOCAL VARIABLES AND BUFFER-LOCAL VALUE EXAMPLES.
2461
2462;;; Local Variable options:
2463;;; mode: name(-mode)
2464;;; eval: expression
2465;;; local-variable: value
2466
2467;;; The following sets the buffer local dictionary to english!
2468
2469;;; Local Variables:
2470;;; mode: emacs-lisp
2471;;; comment-column: 40
7c5a1541 2472;;; ispell-local-dictionary: "american"
007852e0
RS
2473;;; End:
2474
2475
2476;;; MORE EXAMPLES OF ISPELL BUFFER-LOCAL VALUES
2477
2478;;; The following places this file in nroff parsing and extended char modes.
2479;;; Local IspellParsing: nroff-mode ~nroff
2480;;; Change IspellDict to IspellDict: to enable the following line.
2481;;; Local IspellDict english
2482;;; Change IspellPersDict to IspellPersDict: to enable the following line.
2483;;; Local IspellPersDict ~/.ispell_lisp
2484;;; The following were automatically generated by ispell using the 'A' command:
2485; LocalWords: ispell ispell-highlight-p ispell-check-comments query-replace
2486; LocalWords: ispell-query-replace-choices ispell-skip-tib non-nil tib
2487; LocalWords: regexps ispell-tib-ref-beginning ispell-tib-ref-end
2488
2489;; ispell.el ends here