Remove incorrect uses of "modeline".
[bpt/emacs.git] / lisp / textmodes / ispell.el
CommitLineData
e8af40ee 1;;; ispell.el --- interface to International Ispell Versions 3.1 and 3.2
be010748 2
acaf905b 3;; Copyright (C) 1994-1995, 1997-2012 Free Software Foundation, Inc.
be010748 4
e8af40ee
PJ
5;; Author: Ken Stevens <k.stevens@ieee.org>
6;; Maintainer: Ken Stevens <k.stevens@ieee.org>
4d170362
SM
7;; Stevens Mod Date: Mon Jan 7 12:32:44 PST 2003
8;; Stevens Revision: 3.6
9050446c 9;; Status : Release with 3.1.12+ and 3.2.0+ ispell.
f5136913
RS
10;; Bug Reports : ispell-el-bugs@itcorp.com
11;; Web Site : http://kdstevens.com/~stevens/ispell-page.html
32395297 12;; Keywords: unix wp
be010748 13
b578f267
EN
14;; This file is part of GNU Emacs.
15
1fecc8fe 16;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 17;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
18;; the Free Software Foundation, either version 3 of the License, or
19;; (at your option) any later version.
b578f267
EN
20
21;; GNU Emacs is distributed in the hope that it will be useful,
22;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24;; GNU General Public License for more details.
25
26;; You should have received a copy of the GNU General Public License
1fecc8fe 27;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b578f267
EN
28
29;; Note: version numbers and time stamp are not updated
f753f75d 30;; when this file is edited for release with GNU Emacs.
be010748 31
007852e0 32;;; Commentary:
007852e0 33
b578f267 34;; INSTRUCTIONS
f5136913 35
6fdf40f3
KH
36;; This code contains a section of user-settable variables that you
37;; should inspect prior to installation. Look past the end of the history
38;; list. Set them up for your locale and the preferences of the majority
39;; of the users. Otherwise the users may need to set a number of variables
b578f267 40;; themselves.
6fdf40f3 41;; You particularly may want to change the default dictionary for your
b578f267 42;; country and language.
6fdf40f3
KH
43;; Most dictionary changes should be made in this file so all users can
44;; enjoy them. Local or modified dictionaries are supported in your .emacs
a6e7a34b
RS
45;; file. Use the variable `ispell-local-dictionary-alist' to specify
46;; your own dictionaries.
f5136913 47
b578f267 48;; Depending on the mail system you use, you may want to include these:
b578f267
EN
49;; (add-hook 'news-inews-hook 'ispell-message)
50;; (add-hook 'mail-send-hook 'ispell-message)
51;; (add-hook 'mh-before-send-letter-hook 'ispell-message)
f5136913 52
6fdf40f3 53;; Ispell has a TeX parser and a nroff parser (the default).
b578f267
EN
54;; The parsing is controlled by the variable ispell-parser. Currently
55;; it is just a "toggle" between TeX and nroff, but if more parsers are
56;; added it will be updated. See the variable description for more info.
f5136913
RS
57
58
b578f267 59;; TABLE OF CONTENTS
f5136913 60
b578f267
EN
61;; ispell-word
62;; ispell-region
63;; ispell-buffer
64;; ispell-message
bc7d6816 65;; ispell-comments-and-strings
b578f267
EN
66;; ispell-continue
67;; ispell-complete-word
68;; ispell-complete-word-interior-frag
69;; ispell-change-dictionary
70;; ispell-kill-ispell
71;; ispell-pdict-save
f5136913
RS
72;; ispell-skip-region-alist
73
b578f267
EN
74;; Commands in ispell-region:
75;; Character replacement: Replace word with choice. May query-replace.
f5136913
RS
76;; ` ': Accept word this time.
77;; `i': Accept word and insert into private dictionary.
78;; `a': Accept word for this session.
79;; `A': Accept word and place in buffer-local dictionary.
80;; `r': Replace word with typed-in value. Rechecked.
81;; `R': Replace word with typed-in value. Query-replaced in buffer. Rechecked.
82;; `?': Show these commands
83;; `x': Exit spelling buffer. Move cursor to original point.
84;; `X': Exit spelling buffer. Leaves cursor at the current point, and permits
85;; the check to be completed later.
86;; `q': Quit spelling session (Kills ispell process).
87;; `l': Look up typed-in replacement in alternate dictionary. Wildcards okay.
88;; `u': Like `i', but the word is lower-cased first.
89;; `m': Place entered value in personal dictionary, then recheck current word.
90;; `C-l': redraws screen
91;; `C-r': recursive edit
f753f75d 92;; `C-z': suspend Emacs or iconify frame
f5136913 93
b578f267
EN
94;; Buffer-Local features:
95;; There are a number of buffer-local features that can be used to customize
96;; ispell for the current buffer. This includes language dictionaries,
97;; personal dictionaries, parsing, and local word spellings. Each of these
98;; local customizations are done either through local variables, or by
99;; including the keyword and argument(s) at the end of the buffer (usually
100;; prefixed by the comment characters). See the end of this file for
101;; examples. The local keywords and variables are:
f5136913 102
b578f267
EN
103;; ispell-dictionary-keyword language-dictionary
104;; uses local variable ispell-local-dictionary
105;; ispell-pdict-keyword personal-dictionary
106;; uses local variable ispell-local-pdict
107;; ispell-parsing-keyword mode-arg extended-char-arg
108;; ispell-words-keyword any number of local word spellings
f5136913
RS
109
110;; Region skipping:
111;; Place new regular expression definitions of regions you prefer not to
112;; spell check in `ispell-skip-region-alist'. Mode-dependent features can
113;; be added to latex by modifying `ispell-tex-skip-alists'.
114;; `ispell-message' contains some custom skipping code for e-mail messages.
115
b578f267 116;; BUGS:
9050446c
GM
117;; Need a way to select between different character mappings without separate
118;; dictionary entries.
119;; Multi-byte characters if not defined by current dictionary may result in the
f753f75d
SM
120;; evil "misalignment error" in some versions of MULE Emacs.
121;; On some versions of Emacs, growing the minibuffer fails.
6fdf40f3 122;; see `ispell-help-in-bufferp'.
9050446c
GM
123;; Recursive edits (?C-r or ?R) inside a keyboard text replacement check (?r)
124;; can cause misalignment errors.
6fdf40f3
KH
125
126;; HISTORY
127
128;; Modifications made in latest versions:
129
4d170362
SM
130;; Revision 3.6 2003/01/07 12:32:44 kss
131;; Removed extra -d LIB in dictionary defs. (Pavel Janik)
132;; Filtered process calls with duplicate dictionary entries.
133;; Fixed bug where message-text-end is inside a mime skipped region.
134;; Minor fixes to get ispell menus right in XEmacs
135;; Fixed skip regexp so it doesn't match stuff like `/.\w'.
136;; Detecting dictionary change not working. Fixed. kss
137;; function `ispell-change-dictionary' now only completes valid dicts.
138
139;; Revision 3.5 2001/7/11 18:43:57 kss
140;; Added fix for aspell to work in XEmacs (ispell-check-version).
141;; Added Portuguese dictionary definition.
142;; New feature: MIME mail message support, Fcc support.
143;; Bug fix: retain comment syntax on lines with region skipping. (TeX $ bug...)
144;; Improved allocation for graphic mode lines. (Miles Bader)
145;; Support -v flag for old versions of aspell. (Eli Zaretskii)
146;; Clear minibuffer on ^G from ispell-help (Tak Ota)
147
fea6948a
GM
148;; Revision 3.4 2000/8/4 09:41:50 kss
149;; Support new color display functions.
150;; Fixed misalignment offset bug when replacing a string after a shift made.
151;; Set to standard Author/Maintainer heading,
152;; ensure localwords lists are separated from the text by newline. (Dave Love)
153;; Added dictionary definition for Italian (William Deakin)
154;; HTML region skipping greatly improved. (Chuck D. Phillips)
155;; improved menus. Fixed regexp matching http/email addresses.
f753f75d 156;; one arg always for XEmacs sleep-for (gunnar Evermann)
8eba343c
EZ
157;; support for synchronous processes (Eli Zaretskii)
158
9050446c
GM
159;; Revision 3.3 1999/11/29 11:38:34 kss
160;; Only word replacements entered in from the keyboard are rechecked.
161;; This fixes a bug in tex parsing and misalignment.
162;; Exceptions exist for recursive edit and query-replace, with tex error
163;; condition tested. Recursive editing improved.
164;; XEmacs repair for when `enable-multibyte-characters' defined - Didier Verna.
165;; ispell-help fixed for XEmacs. Choices minibuffer now displayed in XEmacs.
166;; Only list valid dictionaries in Spell menu. Russian dictionary doesn't allow
167;; run-together words, and uses koi8-r font. Don't skip text in html <TT>
168;; fonts.
169
6fdf40f3
KH
170;; Revision 3.2 1999/5/7 14:25:14 kss
171;; Accept ispell versions 3.X.Y where X>=1
172;; fine tuned latex region skipping. Fixed bug in ispell-word that did not
173;; point in right place on words < 2 chars. Simplified ispell-minor-mode.
174;; Fixed bug in TeX parsing when math commands are in the comments.
175;; Removed calls to `when' macro.
176
177;; Revision 3.1 1998/12/1 13:21:52 kss
178;; Improved and fixed customize support.
179;; Improved and fixed comments in variables and messages.
180;; A coding system is now required for all languages.
181;; casechars improved for castellano, castellano8, and norsk dictionaries.
182;; Dictionary norsk7-tex removed. Dictionary polish added.
183;; Dictionaries redefined at load-time to support dictionary changes.
184;; Menu redefined at load time to support dictionary changes.
185;; ispell-check-version added as an alias for `check-ispell-version'.
186;; Spelling suggestions returned in order generated by ispell.
187;; Small bug fixed in matching ispell error messages.
188;; Robustness added to ensure `case-fold-search' doesn't get redefined.
189;; Fixed bug that didn't respect case of word in `ispell-complete-word'.
190;; Multibyte character coding support added for process interactions.
191;; Ensure ispell process has terminated before starting new process.
192;; This can otherwise confuse process filters and hang ispell.
9050446c 193;; Improved skipping support for SGML.
6fdf40f3
KH
194;; Fixed bug using ^M rather than \r in `ispell-minor-check'.
195;; Improved message reference matching in `ispell-message'.
196;; Fixed bug in returning to nroff mode from tex mode.
007852e0 197
e26a7bc0 198;;; Compatibility code for XEmacs and (not too) older emacsen:
0aef89e7 199
e26a7bc0 200(eval-and-compile ;; Protect against declare-function undefined in XEmacs
034dc2a3
AM
201 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
202
049cb685 203(declare-function ispell-check-minver "ispell" (v1 v2))
531b1264
GM
204(declare-function ispell-looking-back "ispell"
205 (regexp &optional limit &rest ignored))
049cb685 206
0aef89e7
AM
207(if (fboundp 'version<=)
208 (defalias 'ispell-check-minver 'version<=)
209 (defun ispell-check-minver (minver version)
210 "Check if string VERSION is at least string MINVER.
2188975f
JB
211Both must be in [0-9]+.[0-9]+... format. This is a fallback
212compatibility function in case `version<=' is not available."
0aef89e7
AM
213 (let ((pending t)
214 (return t)
215 start-ver start-mver)
216 ;; Loop until an absolute greater or smaller condition is reached
217 ;; or until no elements are left in any of version and minver. In
218 ;; this case version is exactly the minimal, so return OK.
219 (while pending
220 (let (ver mver)
221 (if (string-match "[0-9]+" version start-ver)
222 (setq start-ver (match-end 0)
5abf1556 223 ver (string-to-number (match-string 0 version))))
0aef89e7
AM
224 (if (string-match "[0-9]+" minver start-mver)
225 (setq start-mver (match-end 0)
5abf1556 226 mver (string-to-number (match-string 0 minver))))
0aef89e7
AM
227
228 (if (or ver mver)
229 (progn
230 (or ver (setq ver 0))
231 (or mver (setq mver 0))
232 ;; If none of below conditions match, this element is the
233 ;; same. Go checking next element.
234 (if (> ver mver)
235 (setq pending nil)
236 (if (< ver mver)
237 (setq pending nil
238 return nil))))
239 (setq pending nil))))
240 return)))
b9431ae0 241
e26a7bc0
AM
242;; XEmacs does not have looking-back
243(if (fboundp 'looking-back)
244 (defalias 'ispell-looking-back 'looking-back)
245 (defun ispell-looking-back (regexp &optional limit &rest ignored)
246 "Return non-nil if text before point matches regular expression REGEXP.
247Like `looking-at' except matches before point, and is slower.
248LIMIT if non-nil speeds up the search by specifying a minimum
249starting position, to avoid checking matches that would start
250before LIMIT.
251
252This is a stripped down compatibility function for use when
253full featured `looking-back' function is missing."
254 (save-excursion
255 (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t))))
256
6e8a1786
AM
257;;; XEmacs21 does not have `with-no-warnings'. Taken from org mode.
258(defmacro ispell-with-no-warnings (&rest body)
259 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
85c8c5b6 260
007852e0
RS
261;;; Code:
262
66793230
JB
263(defvar mail-yank-prefix)
264
87a609d3 265(defgroup ispell nil
24b52f08 266 "User variables for Emacs ispell interface."
87a609d3 267 :group 'applications)
f2b7eb26 268
8eba343c
EZ
269(if (not (fboundp 'buffer-substring-no-properties))
270 (defun buffer-substring-no-properties (start end)
271 (buffer-substring start end)))
f5136913 272
4d170362 273(defalias 'check-ispell-version 'ispell-check-version)
8eba343c 274
f5136913
RS
275;;; **********************************************************************
276;;; The following variables should be set according to personal preference
277;;; and location of binaries:
278;;; **********************************************************************
279
280
6fdf40f3 281;;; ******* THIS FILE IS WRITTEN FOR ISPELL VERSION 3.1+
f5136913
RS
282
283(defcustom ispell-highlight-p 'block
fb7ada5f 284 "Highlight spelling errors when non-nil.
f5136913 285When set to `block', assumes a block cursor with TTY displays."
b9431ae0 286 :type '(choice (const block) (const :tag "off" nil) (const :tag "on" t))
f2b7eb26
RS
287 :group 'ispell)
288
1332f1a0 289(defcustom ispell-lazy-highlight (boundp 'lazy-highlight-cleanup)
fb7ada5f 290 "Controls the lazy-highlighting of spelling errors.
1332f1a0
JL
291When non-nil, all text in the buffer matching the current spelling
292error is highlighted lazily using isearch lazy highlighting (see
293`lazy-highlight-initial-delay' and `lazy-highlight-interval')."
294 :type 'boolean
295 :group 'lazy-highlight
296 :group 'ispell
297 :version "22.1")
298
299(defcustom ispell-highlight-face (if ispell-lazy-highlight 'isearch 'highlight)
37269466 300 "Face used for Ispell highlighting.
007852e0
RS
301This variable can be set by the user to whatever face they desire.
302It's most convenient if the cursor color and highlight color are
f2b7eb26
RS
303slightly different."
304 :type 'face
305 :group 'ispell)
007852e0 306
f2b7eb26 307(defcustom ispell-check-comments t
fb7ada5f 308 "Spelling of comments checked when non-nil.
f5136913
RS
309When set to `exclusive', ONLY comments are checked. (For code comments).
310Warning! Not checking comments, when a comment start is embedded in strings,
311may produce undesired results."
b9431ae0 312 :type '(choice (const exclusive) (const :tag "off" nil) (const :tag "on" t))
f2b7eb26 313 :group 'ispell)
5abf1556
SM
314;;;###autoload
315(put 'ispell-check-comments 'safe-local-variable
316 (lambda (a) (memq a '(nil t exclusive))))
007852e0 317
f2b7eb26 318(defcustom ispell-query-replace-choices nil
fb7ada5f 319 "Corrections made throughout region when non-nil.
f2b7eb26
RS
320Uses `query-replace' (\\[query-replace]) for corrections."
321 :type 'boolean
322 :group 'ispell)
007852e0 323
f2b7eb26 324(defcustom ispell-skip-tib nil
fb7ada5f 325 "Does not spell check `tib' bibliography references when non-nil.
4b03da72 326Skips any text between strings matching regular expressions
a9210a53 327`ispell-tib-ref-beginning' and `ispell-tib-ref-end'.
007852e0 328
d9a8eb66
EZ
329TeX users beware: Any text between [. and .] will be skipped -- even if
330that's your whole buffer -- unless you set `ispell-skip-tib' to nil.
331That includes the [.5mm] type of number..."
f2b7eb26
RS
332 :type 'boolean
333 :group 'ispell)
007852e0
RS
334
335(defvar ispell-tib-ref-beginning "[[<]\\."
336 "Regexp matching the beginning of a Tib reference.")
337
338(defvar ispell-tib-ref-end "\\.[]>]"
339 "Regexp matching the end of a Tib reference.")
340
f2b7eb26 341(defcustom ispell-keep-choices-win t
fb7ada5f 342 "If non-nil, keep the `*Choices*' window for the entire spelling session.
f2b7eb26
RS
343This minimizes redisplay thrashing."
344 :type 'boolean
345 :group 'ispell)
007852e0 346
e34850d1 347(defcustom ispell-choices-win-default-height 2
fb7ada5f 348 "The default size of the `*Choices*' window, including the mode line.
e34850d1 349Must be greater than 1."
f2b7eb26
RS
350 :type 'integer
351 :group 'ispell)
007852e0 352
55b958f0 353(defcustom ispell-program-name
60c4db3a
SM
354 (or (executable-find "aspell")
355 (executable-find "ispell")
356 (executable-find "hunspell")
55b958f0 357 "ispell")
f2b7eb26
RS
358 "Program invoked by \\[ispell-word] and \\[ispell-region] commands."
359 :type 'string
360 :group 'ispell)
007852e0 361
f2b7eb26 362(defcustom ispell-alternate-dictionary
fd5539c6
AM
363 (cond ((file-readable-p "/usr/dict/web2") "/usr/dict/web2")
364 ((file-readable-p "/usr/share/dict/web2") "/usr/share/dict/web2")
365 ((file-readable-p "/usr/dict/words") "/usr/dict/words")
366 ((file-readable-p "/usr/lib/dict/words") "/usr/lib/dict/words")
367 ((file-readable-p "/usr/share/dict/words") "/usr/share/dict/words")
368 ((file-readable-p "/usr/share/lib/dict/words")
2b2eabed 369 "/usr/share/lib/dict/words")
fd5539c6 370 ((file-readable-p "/sys/dict") "/sys/dict"))
fb7ada5f 371 "Alternate plain word-list dictionary for spelling help."
71c62b6b 372 :type '(choice file (const :tag "None" nil))
f2b7eb26 373 :group 'ispell)
007852e0 374
fd5539c6 375(defcustom ispell-complete-word-dict nil
fb7ada5f 376 "Plain word-list dictionary used for word completion if
fd5539c6 377different from `ispell-alternate-dictionary'."
71c62b6b 378 :type '(choice file (const :tag "None" nil))
f2b7eb26 379 :group 'ispell)
007852e0 380
f5136913 381(defcustom ispell-message-dictionary-alist nil
fb7ada5f 382 "List used by `ispell-message' to select a new dictionary.
f5136913
RS
383It consists of pairs (REGEXP . DICTIONARY). If REGEXP is found
384in the message headers, `ispell-local-dictionary' will be set to
385DICTIONARY if `ispell-local-dictionary' is not buffer-local.
386E.g. you may use the following value:
387 '((\"^Newsgroups:[ \\t]*de\\\\.\" . \"deutsch8\")
388 (\"^To:[^\\n,]+\\\\.de[ \\t\\n,>]\" . \"deutsch8\"))"
389 :type '(repeat (cons regexp string))
390 :group 'ispell)
007852e0 391
f5136913 392
4d170362 393(defcustom ispell-message-fcc-skip 50000
fb7ada5f 394 "Query before saving Fcc message copy if attachment larger than this value.
4d170362
SM
395Always stores Fcc copy of message when nil."
396 :type '(choice integer (const :tag "off" nil))
397 :group 'ispell)
398
399
6a314a29
EZ
400(defcustom ispell-grep-command
401 ;; MS-Windows/MS-DOS have `egrep' as a Unix shell script, so they
402 ;; cannot invoke it. Use "grep -E" instead (see ispell-grep-options
403 ;; below).
404 (if (memq system-type '(windows-nt ms-dos)) "grep" "egrep")
f5136913
RS
405 "Name of the grep command for search processes."
406 :type 'string
407 :group 'ispell)
408
6a314a29
EZ
409(defcustom ispell-grep-options
410 (if (memq system-type '(windows-nt ms-dos)) "-Ei" "-i")
a9210a53 411 "String of options to use when running the program in `ispell-grep-command'.
007852e0 412Should probably be \"-i\" or \"-e\".
d1f18ec0 413Some machines (like the NeXT) don't support \"-i\"."
f5136913
RS
414 :type 'string
415 :group 'ispell)
007852e0 416
f5136913
RS
417(defcustom ispell-look-command
418 (cond ((file-exists-p "/bin/look") "/bin/look")
419 ((file-exists-p "/usr/local/bin/look") "/usr/local/bin/look")
420 ((file-exists-p "/usr/bin/look") "/usr/bin/look")
421 (t "look"))
007852e0 422 "Name of the look command for search processes.
f5136913
RS
423This must be an absolute file name."
424 :type 'file
425 :group 'ispell)
007852e0 426
f2b7eb26 427(defcustom ispell-look-p (file-exists-p ispell-look-command)
fb7ada5f 428 "Non-nil means use `look' rather than `grep'.
f2b7eb26
RS
429Default is based on whether `look' seems to be available."
430 :type 'boolean
431 :group 'ispell)
007852e0 432
f2b7eb26 433(defcustom ispell-have-new-look nil
fb7ada5f 434 "Non-nil means use the `-r' option (regexp) when running `look'."
f2b7eb26
RS
435 :type 'boolean
436 :group 'ispell)
007852e0 437
f5136913
RS
438(defcustom ispell-look-options (if ispell-have-new-look "-dfr" "-df")
439 "String of command options for `ispell-look-command'."
440 :type 'string
441 :group 'ispell)
007852e0 442
f5136913 443(defcustom ispell-use-ptys-p nil
a9210a53 444 "When non-nil, Emacs uses ptys to communicate with Ispell.
f5136913
RS
445When nil, Emacs uses pipes."
446 :type 'boolean
447 :group 'ispell)
007852e0 448
f2b7eb26 449(defcustom ispell-following-word nil
fb7ada5f 450 "Non-nil means `ispell-word' checks the word around or after point.
f2b7eb26
RS
451Otherwise `ispell-word' checks the preceding word."
452 :type 'boolean
453 :group 'ispell)
007852e0 454
f2b7eb26 455(defcustom ispell-help-in-bufferp nil
fb7ada5f 456 "Non-nil means display interactive keymap help in a buffer.
b9431ae0 457The following values are supported:
f5136913
RS
458 nil Expand the minibuffer and display a short help message
459 there for a couple of seconds.
460 t Pop up a new buffer and display a short help message there
461 for a couple of seconds.
fea6948a 462 electric Pop up a new buffer and display a long help message there.
f5136913 463 User can browse and then exit the help mode."
b9431ae0 464 :type '(choice (const electric) (const :tag "off" nil) (const :tag "on" t))
f2b7eb26 465 :group 'ispell)
007852e0 466
f2b7eb26 467(defcustom ispell-quietly nil
fb7ada5f 468 "Non-nil means suppress messages in `ispell-word'."
f2b7eb26
RS
469 :type 'boolean
470 :group 'ispell)
007852e0 471
41d207e1 472(defcustom ispell-format-word-function (function upcase)
fb7ada5f 473 "Formatting function for displaying word being spell checked.
f2b7eb26
RS
474The function must take one string argument and return a string."
475 :type 'function
476 :group 'ispell)
41d207e1 477(defvaralias 'ispell-format-word 'ispell-format-word-function)
007852e0 478
f5136913
RS
479(defcustom ispell-use-framepop-p nil
480 "When non-nil ispell uses framepop to display choices in a dedicated frame.
481You can set this variable to dynamically use framepop if you are in a
482window system by evaluating the following on startup to set this variable:
483 (and window-system (condition-case () (require 'framepop) (error nil)))"
484 :type 'boolean
485 :group 'ispell)
486
d38e5dbe 487;;;###autoload
f2b7eb26 488(defcustom ispell-personal-dictionary nil
fb7ada5f 489 "File name of your personal spelling dictionary, or nil.
1525771e
AM
490If nil, the default personal dictionary, (\"~/.ispell_DICTNAME\" for ispell or
491\"~/.aspell.LANG.pws\" for aspell) is used, where DICTNAME is the name of your
492default dictionary and LANG the two letter language code."
6c80b133
RS
493 :type '(choice file
494 (const :tag "default" nil))
f2b7eb26 495 :group 'ispell)
007852e0 496
f2b7eb26 497(defcustom ispell-silently-savep nil
fb7ada5f 498 "When non-nil, save personal dictionary without asking for confirmation."
f2b7eb26
RS
499 :type 'boolean
500 :group 'ispell)
007852e0 501
a6e7a34b
RS
502(defvar ispell-local-dictionary-overridden nil
503 "Non-nil means the user has explicitly set this buffer's Ispell dictionary.")
a8097641 504(make-variable-buffer-local 'ispell-local-dictionary-overridden)
a6e7a34b 505
f5136913 506(defcustom ispell-local-dictionary nil
a6e7a34b
RS
507 "If non-nil, the dictionary to be used for Ispell commands in this buffer.
508The value must be a string dictionary name,
509or nil, which means use the global setting in `ispell-dictionary'.
510Dictionary names are defined in `ispell-local-dictionary-alist'
34995333 511and `ispell-dictionary-alist'.
f5136913 512
b9431ae0 513Setting `ispell-local-dictionary' to a value has the same effect as
f5136913
RS
514calling \\[ispell-change-dictionary] with that value. This variable
515is automatically set when defined in the file with either
a6e7a34b 516`ispell-dictionary-keyword' or the Local Variable syntax."
f5136913
RS
517 :type '(choice string
518 (const :tag "default" nil))
519 :group 'ispell)
5abf1556
SM
520;;;###autoload
521(put 'ispell-local-dictionary 'safe-local-variable 'string-or-null-p)
f5136913
RS
522
523(make-variable-buffer-local 'ispell-local-dictionary)
524
820c1f50
AM
525(defcustom ispell-dictionary nil
526 "Default dictionary to use if `ispell-local-dictionary' is nil."
527 :type '(choice string
528 (const :tag "default" nil))
529 :group 'ispell)
530
f2b7eb26 531(defcustom ispell-extra-args nil
fb7ada5f 532 "If non-nil, a list of extra switches to pass to the Ispell program.
f5136913 533For example, (\"-W\" \"3\") to cause it to accept all 1-3 character
4b03da72 534words as correct. See also `ispell-dictionary-alist', which may be used
f2b7eb26
RS
535for language-specific arguments."
536 :type '(repeat string)
537 :group 'ispell)
4b03da72 538
6fdf40f3 539
8eba343c 540
fea6948a 541(defcustom ispell-skip-html 'use-mode-name
fb7ada5f 542 "Indicates whether ispell should skip spell checking of SGML markup.
8eba343c
EZ
543If t, always skip SGML markup; if nil, never skip; if non-t and non-nil,
544guess whether SGML markup should be skipped according to the name of the
545buffer's major mode."
546 :type '(choice (const :tag "always" t) (const :tag "never" nil)
547 (const :tag "use-mode-name" use-mode-name))
548 :group 'ispell)
549
4d170362
SM
550(make-variable-buffer-local 'ispell-skip-html)
551
8eba343c 552
6fdf40f3 553(defcustom ispell-local-dictionary-alist nil
fb7ada5f 554 "List of local or customized dictionary definitions.
a6e7a34b 555These can override the values in `ispell-dictionary-alist'.
4d170362 556
a6e7a34b
RS
557To make permanent changes to your dictionary definitions, you
558will need to make your changes in this variable, save, and then
24b52f08 559re-start Emacs."
6fdf40f3
KH
560 :type '(repeat (list (choice :tag "Dictionary"
561 (string :tag "Dictionary name")
562 (const :tag "default" nil))
563 (regexp :tag "Case characters")
564 (regexp :tag "Non case characters")
565 (regexp :tag "Other characters")
566 (boolean :tag "Many other characters")
567 (repeat :tag "Ispell command line args"
568 (string :tag "Arg"))
569 (choice :tag "Extended character mode"
570 (const "~tex") (const "~plaintex")
571 (const "~nroff") (const "~list")
fea6948a 572 (const "~latin1") (const "~latin3")
6fdf40f3 573 (const :tag "default" nil))
72e4724b 574 (coding-system :tag "Coding System")))
6fdf40f3
KH
575 :group 'ispell)
576
577
f753f75d 578(defvar ispell-dictionary-base-alist
3851314b 579 '((nil ; default
a1e34e71
SM
580 ;; The default dictionary. It may be English.aff, or any other
581 ;; dictionary depending on locale and such things. We should probably
582 ;; ask ispell what dictionary it's using, but until we do that, let's
3851314b
AM
583 ;; just use a minimal regexp. [:alpha:] will later be set if possible.
584 "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil iso-8859-1)
f753f75d
SM
585 ("american" ; Yankee English
586 "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil iso-8859-1)
587 ("brasileiro" ; Brazilian mode
588 "[A-Z\301\311\315\323\332\300\310\314\322\331\303\325\307\334\302\312\324a-z\341\351\355\363\372\340\350\354\362\371\343\365\347\374\342\352\364]"
589 "[^A-Z\301\311\315\323\332\300\310\314\322\331\303\325\307\334\302\312\324a-z\341\351\355\363\372\340\350\354\362\371\343\365\347\374\342\352\364]"
590 "[']" nil nil nil iso-8859-1)
591 ("british" ; British version
592 "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil iso-8859-1)
593 ("castellano" ; Spanish mode
594 "[A-Z\301\311\315\321\323\332\334a-z\341\351\355\361\363\372\374]"
595 "[^A-Z\301\311\315\321\323\332\334a-z\341\351\355\361\363\372\374]"
596 "[-]" nil ("-B") "~tex" iso-8859-1)
597 ("castellano8" ; 8 bit Spanish mode
598 "[A-Z\301\311\315\321\323\332\334a-z\341\351\355\361\363\372\374]"
599 "[^A-Z\301\311\315\321\323\332\334a-z\341\351\355\361\363\372\374]"
600 "[-]" nil ("-B" "-d" "castellano") "~latin1" iso-8859-1)
601 ("czech"
602 "[A-Za-z\301\311\314\315\323\332\331\335\256\251\310\330\317\253\322\341\351\354\355\363\372\371\375\276\271\350\370\357\273\362]"
603 "[^A-Za-z\301\311\314\315\323\332\331\335\256\251\310\330\317\253\322\341\351\354\355\363\372\371\375\276\271\350\370\357\273\362]"
604 "" nil ("-B") nil iso-8859-2)
605 ("dansk" ; Dansk.aff
606 "[A-Z\306\330\305a-z\346\370\345]" "[^A-Z\306\330\305a-z\346\370\345]"
607 "[']" nil ("-C") nil iso-8859-1)
608 ("deutsch" ; Deutsch.aff
609 "[a-zA-Z\"]" "[^a-zA-Z\"]" "[']" t ("-C") "~tex" iso-8859-1)
610 ("deutsch8"
611 "[a-zA-Z\304\326\334\344\366\337\374]"
612 "[^a-zA-Z\304\326\334\344\366\337\374]"
613 "[']" t ("-C" "-d" "deutsch") "~latin1" iso-8859-1)
614 ("english" ; make English explicitly selectable
615 "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil iso-8859-1)
616 ("esperanto"
617 "[A-Za-z\246\254\266\274\306\330\335\336\346\370\375\376]"
618 "[^A-Za-z\246\254\266\274\306\330\335\336\346\370\375\376]"
619 "[-']" t ("-C") "~latin3" iso-8859-3)
620 ("esperanto-tex"
621 "[A-Za-z^\\]" "[^A-Za-z^\\]"
622 "[-'`\"]" t ("-C" "-d" "esperanto") "~tex" iso-8859-3)
aa311d6e
CY
623 ("finnish"
624 "[A-Za-z\345\344\366\305\304\326]"
625 "[^A-Za-z\345\344\366\305\304\326]"
626 "[:]" nil ("-C") "~list" iso-8859-1)
f753f75d
SM
627 ("francais7"
628 "[A-Za-z]" "[^A-Za-z]" "[`'^-]" t nil nil iso-8859-1)
629 ("francais" ; Francais.aff
630 "[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]"
631 "[^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]"
632 "[-'.@]" t nil "~list" iso-8859-1)
633 ("francais-tex" ; Francais.aff
634 "[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\\]"
635 "[^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\\]"
636 "[-'^`\".@]" t nil "~tex" iso-8859-1)
637 ("german" ; german.aff
638 "[a-zA-Z\"]" "[^a-zA-Z\"]" "[']" t ("-C") "~tex" iso-8859-1)
639 ("german8" ; german.aff
640 "[a-zA-Z\304\326\334\344\366\337\374]"
641 "[^a-zA-Z\304\326\334\344\366\337\374]"
642 "[']" t ("-C" "-d" "german") "~latin1" iso-8859-1)
643 ("italiano" ; Italian.aff
644 "[A-Z\300\301\310\311\314\315\322\323\331\332a-z\340\341\350\351\354\355\363\371\372]"
645 "[^A-Z\300\301\310\311\314\315\322\323\331\332a-z\340\341\350\351\354\355\363\371\372]"
646 "[-.]" nil ("-B" "-d" "italian") "~tex" iso-8859-1)
647 ("nederlands" ; Nederlands.aff
648 "[A-Za-z\300\301\302\303\304\305\307\310\311\312\313\314\315\316\317\322\323\324\325\326\331\332\333\334\340\341\342\343\344\345\347\350\351\352\353\354\355\356\357\361\362\363\364\365\366\371\372\373\374]"
649 "[^A-Za-z\300\301\302\303\304\305\307\310\311\312\313\314\315\316\317\322\323\324\325\326\331\332\333\334\340\341\342\343\344\345\347\350\351\352\353\354\355\356\357\361\362\363\364\365\366\371\372\373\374]"
650 "[']" t ("-C") nil iso-8859-1)
651 ("nederlands8" ; Dutch8.aff
652 "[A-Za-z\300\301\302\303\304\305\307\310\311\312\313\314\315\316\317\322\323\324\325\326\331\332\333\334\340\341\342\343\344\345\347\350\351\352\353\354\355\356\357\361\362\363\364\365\366\371\372\373\374]"
653 "[^A-Za-z\300\301\302\303\304\305\307\310\311\312\313\314\315\316\317\322\323\324\325\326\331\332\333\334\340\341\342\343\344\345\347\350\351\352\353\354\355\356\357\361\362\363\364\365\366\371\372\373\374]"
654 "[']" t ("-C") nil iso-8859-1)
655 ("norsk" ; 8 bit Norwegian mode
656 "[A-Za-z\305\306\307\310\311\322\324\330\345\346\347\350\351\362\364\370]"
657 "[^A-Za-z\305\306\307\310\311\322\324\330\345\346\347\350\351\362\364\370]"
658 "[\"]" nil nil "~list" iso-8859-1)
659 ("norsk7-tex" ; 7 bit Norwegian TeX mode
660 "[A-Za-z{}\\'^`]" "[^A-Za-z{}\\'^`]"
661 "[\"]" nil ("-d" "norsk") "~plaintex" iso-8859-1)
662 ("polish" ; Polish mode
663 "[A-Za-z\241\243\246\254\257\261\263\266\274\277\306\312\321\323\346\352\361\363]"
664 "[^A-Za-z\241\243\246\254\257\261\263\266\274\277\306\312\321\323\346\352\361\363]"
665 "[.]" nil nil nil iso-8859-2)
666 ("portugues" ; Portuguese mode
30d7ac37
AM
667 "[a-zA-Z\301\302\307\311\323\340\341\342\351\352\355\363\343\347\372]"
668 "[^a-zA-Z\301\302\307\311\323\340\341\342\351\352\355\363\343\347\372]"
f753f75d
SM
669 "[']" t ("-C") "~latin1" iso-8859-1)
670 ("russian" ; Russian.aff (KOI8-R charset)
671 "[\341\342\367\347\344\345\263\366\372\351\352\353\354\355\356\357\360\362\363\364\365\346\350\343\376\373\375\370\371\377\374\340\361\301\302\327\307\304\305\243\326\332\311\312\313\314\315\316\317\320\322\323\324\325\306\310\303\336\333\335\330\331\337\334\300\321]"
672 "[^\341\342\367\347\344\345\263\366\372\351\352\353\354\355\356\357\360\362\363\364\365\346\350\343\376\373\375\370\371\377\374\340\361\301\302\327\307\304\305\243\326\332\311\312\313\314\315\316\317\320\322\323\324\325\306\310\303\336\333\335\330\331\337\334\300\321]"
673 "" nil nil nil koi8-r)
674 ("russianw" ; russianw.aff (CP1251 charset)
675 "[\300\301\302\303\304\305\250\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\334\333\332\335\336\337\340\341\342\343\344\345\270\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\374\373\372\375\376\377]"
676 "[^\300\301\302\303\304\305\250\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\334\333\332\335\336\337\340\341\342\343\344\345\270\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\374\373\372\375\376\377]"
677 "" nil nil nil windows-1251)
678 ("slovak" ; Slovakian
679 "[A-Za-z\301\304\311\315\323\332\324\300\305\245\335\256\251\310\317\253\322\341\344\351\355\363\372\364\340\345\265\375\276\271\350\357\273\362]"
680 "[^A-Za-z\301\304\311\315\323\332\324\300\305\245\335\256\251\310\317\253\322\341\344\351\355\363\372\364\340\345\265\375\276\271\350\357\273\362]"
681 "" nil ("-B") nil iso-8859-2)
682 ("slovenian" ; Slovenian
683 "[A-Za-z\301\304\311\315\323\332\324\300\305\245\335\256\251\310\317\253\322\341\344\351\355\363\372\364\340\345\265\375\276\271\350\357\273\362]"
684 "[^A-Za-z\301\304\311\315\323\332\324\300\305\245\335\256\251\310\317\253\322\341\344\351\355\363\372\364\340\345\265\375\276\271\350\357\273\362]"
685 "" nil ("-B" "-d" "slovenian") nil iso-8859-2)
686 ("svenska" ; Swedish mode
687 "[A-Za-z\345\344\366\351\340\374\350\346\370\347\305\304\326\311\300\334\310\306\330\307]"
688 "[^A-Za-z\345\344\366\351\340\374\350\346\370\347\305\304\326\311\300\334\310\306\330\307]"
e2627d21
EZ
689 "[']" nil ("-C") "~list" iso-8859-1)
690 ("hebrew" "[\340\341\342\343\344\345\346\347\350\351\353\352\354\356\355\360\357\361\362\364\363\367\366\365\370\371\372]" "[^\340\341\342\343\344\345\346\347\350\351\353\352\354\356\355\360\357\361\362\364\363\367\366\365\370\371\372]" "" nil ("-B") nil cp1255))
f753f75d
SM
691 "Base value for `ispell-dictionary-alist'.")
692
693(defvar ispell-dictionary-alist nil
007852e0
RS
694 "An alist of dictionaries and their associated parameters.
695
696Each element of this list is also a list:
697
4b03da72 698\(DICTIONARY-NAME CASECHARS NOT-CASECHARS OTHERCHARS MANY-OTHERCHARS-P
f5136913 699 ISPELL-ARGS EXTENDED-CHARACTER-MODE CHARACTER-SET\)
007852e0 700
f5136913
RS
701DICTIONARY-NAME is a possible string value of variable `ispell-dictionary',
702nil means the default dictionary.
007852e0 703
b11a1b2e 704CASECHARS is a regular expression of valid characters that comprise a word.
007852e0
RS
705
706NOT-CASECHARS is the opposite regexp of CASECHARS.
707
f5136913
RS
708OTHERCHARS is a regexp of characters in the NOT-CASECHARS set but which can be
709used to construct words in some special way. If OTHERCHARS characters follow
710and precede characters from CASECHARS, they are parsed as part of a word,
fbbd6e46 711otherwise they become word-breaks. As an example in English, assume the
f5136913 712regular expression \"[']\" for OTHERCHARS. Then \"they're\" and
fbbd6e46
RS
713\"Steven's\" are parsed as single words including the \"'\" character, but
714\"Stevens'\" does not include the quote character as part of the word.
f5136913 715If you want OTHERCHARS to be empty, use the empty string.
007852e0
RS
716Hint: regexp syntax requires the hyphen to be declared first here.
717
ce6fb7e6 718CASECHARS, NOT-CASECHARS, and OTHERCHARS must be unibyte strings
bb1954bd 719containing bytes of CHARACTER-SET. In addition, if they contain
d9a8eb66 720non-ASCII bytes, the regular expression must be a single
bb1954bd
KH
721`character set' construct that doesn't specify a character range
722for non-ASCII bytes.
723
fbbd6e46
RS
724MANY-OTHERCHARS-P is non-nil when multiple OTHERCHARS are allowed in a word.
725Otherwise only a single OTHERCHARS character is allowed to be part of any
726single word.
007852e0
RS
727
728ISPELL-ARGS is a list of additional arguments passed to the ispell
729subprocess.
730
731EXTENDED-CHARACTER-MODE should be used when dictionaries are used which
e29702dd 732have been configured in an Ispell affix file. (For example, umlauts
007852e0 733can be encoded as \\\"a, a\\\", \"a, ...) Defaults are ~tex and ~nroff
a9210a53
RS
734in English. This has the same effect as the command-line `-T' option.
735The buffer Major Mode controls Ispell's parsing in tex or nroff mode,
007852e0 736but the dictionary can control the extended character mode.
d1f18ec0 737Both defaults can be overruled in a buffer-local fashion. See
a9210a53 738`ispell-parsing-keyword' for details on this.
007852e0 739
d9a8eb66
EZ
740CHARACTER-SET used to encode text sent to the ispell subprocess
741when the language uses non-ASCII characters.
f5136913 742
d9a8eb66
EZ
743Note that with \"ispell\" as the speller, the CASECHARS and
744OTHERCHARS slots of the alist should contain the same character
745set as casechars and otherchars in the LANGUAGE.aff file \(e.g.,
425df10c 746english.aff\). aspell and hunspell don't have this limitation.")
b9431ae0 747
5abf1556
SM
748(defvar ispell-really-aspell nil) ; Non-nil if we can use aspell extensions.
749(defvar ispell-really-hunspell nil) ; Non-nil if we can use hunspell extensions.
57bf8fd4 750(defvar ispell-encoding8-command nil
d9a8eb66
EZ
751 "Command line option prefix to select encoding if supported, nil otherwise.
752If setting the encoding is supported by spellchecker and is selectable from
753the command line, this variable will contain \"--encoding=\" for aspell
754and \"-i \" for hunspell, so the appropriate mime charset can be selected.
755That will be set in `ispell-check-version' for hunspell >= 1.1.6 and
756aspell >= 0.60.
757
758For aspell, non-nil also means to try to automatically find its dictionaries.
759
760Earlier aspell versions do not consistently support charset encoding. Handling
4ca8965c 761this would require some extra guessing in `ispell-aspell-find-dictionary'.")
8eba343c 762
57bf8fd4 763(defvar ispell-aspell-supports-utf8 nil
d9a8eb66 764 "Non-nil if aspell has consistent command line UTF-8 support. Obsolete.
57bf8fd4 765ispell.el and flyspell.el will use for this purpose the more generic
d1f18ec0 766variable `ispell-encoding8-command' for both aspell and hunspell. Is left
57bf8fd4
AM
767here just for backwards compatibility.")
768
769(make-obsolete-variable 'ispell-aspell-supports-utf8
770 'ispell-encoding8-command "23.1")
8eba343c 771
177eca34
AM
772(defvar ispell-emacs-alpha-regexp
773 (if (string-match "^[[:alpha:]]+$" "abcde")
774 "[[:alpha:]]"
775 nil)
776 "[[:alpha:]] if Emacs supports [:alpha:] regexp, nil
777otherwise (current XEmacs does not support it).")
8eba343c
EZ
778
779;;; **********************************************************************
780;;; The following are used by ispell, and should not be changed.
781;;; **********************************************************************
782
007852e0 783
9050446c 784
94eb3112
SM
785;; The version must be 3.1 or greater for this version of ispell.el
786;; There is an incompatibility between version 3.1.12 and lower versions.
9050446c
GM
787(defconst ispell-required-version '(3 1 12)
788 "Ispell versions with which this version of ispell.el is known to work.")
789(defvar ispell-offset -1
790 "Offset that maps protocol differences between ispell 3.1 versions.")
791
4d170362 792(defconst ispell-version "ispell.el 3.6 - 7-Jan-2003")
9050446c
GM
793
794
4d170362 795(defun ispell-check-version (&optional interactivep)
d9a8eb66 796 "Ensure that `ispell-program-name' is valid and has the correct version.
9050446c 797Returns version number if called interactively.
a60f1fcd 798Otherwise returns the library directory name, if that is defined."
9050446c
GM
799 ;; This is a little wasteful as we actually launch ispell twice: once
800 ;; to make sure it's the right version, and once for real. But people
801 ;; get confused by version mismatches *all* the time (and I've got the
802 ;; email to prove it) so I think this is worthwhile. And the -v[ersion]
803 ;; option is the only way I can think of to do this that works with
804 ;; all versions, since versions earlier than 3.0.09 didn't identify
805 ;; themselves on startup.
806 (interactive "p")
72f16325 807 (let ((default-directory (or (and (boundp 'temporary-file-directory)
4d170362
SM
808 temporary-file-directory)
809 default-directory))
0aef89e7 810 result status ispell-program-version)
0764ed37
AM
811
812 (with-temp-buffer
bc4c6633 813 (setq status (ispell-call-process
6ef3fdfb
EZ
814 ispell-program-name nil t nil
815 ;; aspell doesn't accept the -vv switch.
816 (let ((case-fold-search
817 (memq system-type '(ms-dos windows-nt)))
818 (speller
819 (file-name-nondirectory ispell-program-name)))
820 ;; Assume anything that isn't `aspell' is Ispell.
821 (if (string-match "\\`aspell" speller) "-v" "-vv"))))
9050446c
GM
822 (goto-char (point-min))
823 (if interactivep
4d170362 824 ;; report version information of ispell and ispell.el
9050446c
GM
825 (progn
826 (end-of-line)
827 (setq result (concat (buffer-substring-no-properties (point-min)
828 (point))
829 ", "
830 ispell-version))
5673af85 831 (message "%s" result))
a60f1fcd 832 ;; return library directory.
27ce741e 833 (if (re-search-forward "LIBDIR = \\\"\\([^ \t\n]*\\)\\\"" nil t)
72f16325 834 (setq result (match-string 1))))
9050446c
GM
835 (goto-char (point-min))
836 (if (not (memq status '(0 nil)))
837 (error "%s exited with %s %s" ispell-program-name
838 (if (stringp status) "signal" "code") status))
0aef89e7
AM
839
840 ;; Get relevant version strings. Only xx.yy.... format works well
841 (let (case-fold-search)
842 (setq ispell-program-version
843 (and (search-forward-regexp "\\([0-9]+\\.[0-9\\.]+\\)" nil t)
844 (match-string 1)))
845
846 ;; Make sure these variables are (re-)initialized to the default value
847 (setq ispell-really-aspell nil
848 ispell-aspell-supports-utf8 nil
57bf8fd4
AM
849 ispell-really-hunspell nil
850 ispell-encoding8-command nil)
0aef89e7
AM
851
852 (goto-char (point-min))
853 (or (setq ispell-really-aspell
854 (and (search-forward-regexp
d1dca201 855 "(but really Aspell \\([0-9]+\\.[0-9\\.-]+\\)?)" nil t)
0aef89e7
AM
856 (match-string 1)))
857 (setq ispell-really-hunspell
858 (and (search-forward-regexp
72f16325
SM
859 "(but really Hunspell \\([0-9]+\\.[0-9\\.-]+\\)?)"
860 nil t)
0aef89e7
AM
861 (match-string 1)))))
862
863 (let ((aspell-minver "0.50")
864 (aspell8-minver "0.60")
865 (ispell0-minver "3.1.0")
866 (ispell-minver "3.1.12")
867 (hunspell8-minver "1.1.6"))
868
869 (if (ispell-check-minver ispell0-minver ispell-program-version)
870 (or (ispell-check-minver ispell-minver ispell-program-version)
871 (setq ispell-offset 0))
872 (error "%s release %s or greater is required"
873 ispell-program-name
874 ispell-minver))
875
876 (cond
877 (ispell-really-aspell
878 (if (ispell-check-minver aspell-minver ispell-really-aspell)
879 (if (ispell-check-minver aspell8-minver ispell-really-aspell)
57bf8fd4
AM
880 (progn
881 (setq ispell-aspell-supports-utf8 t)
882 (setq ispell-encoding8-command "--encoding=")))
0aef89e7
AM
883 (setq ispell-really-aspell nil)))
884 (ispell-really-hunspell
57bf8fd4 885 (if (ispell-check-minver hunspell8-minver ispell-really-hunspell)
b39bb7e1 886 (setq ispell-encoding8-command "-i")
57bf8fd4 887 (setq ispell-really-hunspell nil))))))
9050446c
GM
888 result))
889
bc4c6633
RS
890(defun ispell-call-process (&rest args)
891 "Like `call-process' but defend against bad `default-directory'."
892 (let ((default-directory default-directory))
893 (unless (and (file-directory-p default-directory)
894 (file-readable-p default-directory))
895 (setq default-directory (expand-file-name "~/")))
896 (apply 'call-process args)))
897
898(defun ispell-call-process-region (&rest args)
899 "Like `call-process-region' but defend against bad `default-directory'."
900 (let ((default-directory default-directory))
901 (unless (and (file-directory-p default-directory)
902 (file-readable-p default-directory))
903 (setq default-directory (expand-file-name "~/")))
904 (apply 'call-process-region args)))
905
9050446c 906
8eba343c 907
72f16325
SM
908;; The preparation of the menu bar menu must be autoloaded
909;; because otherwise this file gets autoloaded every time Emacs starts
910;; so that it can set up the menus and determine keyboard equivalents.
8eba343c 911
d38e5dbe 912;;;###autoload
b9431ae0 913(defvar ispell-menu-map nil "Key map for ispell menu.")
60c4db3a 914;; Redo menu when loading ispell to get dictionary modifications
cbef3295 915(setq ispell-menu-map nil)
007852e0 916
d38e5dbe 917;;;###autoload
f5136913
RS
918(defvar ispell-menu-xemacs nil
919 "Spelling menu for XEmacs.
920If nil when package is loaded, a standard menu will be set,
921and added as a submenu of the \"Edit\" menu.")
e29702dd 922
f753f75d
SM
923;; Break out XEmacs menu and split into several calls to avoid having
924;; long lines in loaddefs.el. Detect need off following constant.
12e53d2e 925
f5136913 926;;; Set up dictionary
d38e5dbe 927;;;###autoload
9050446c 928(defvar ispell-menu-map-needed
f5136913 929 ;; only needed when not version 18 and not XEmacs.
e29702dd 930 (and (not ispell-menu-map)
877d353a 931 (not (featurep 'xemacs))
9050446c
GM
932 'reload))
933
a60f1fcd 934(defvar ispell-library-directory (condition-case ()
4d170362 935 (ispell-check-version)
a60f1fcd
RS
936 (error nil))
937 "Directory where ispell dictionaries reside.")
9050446c 938
fea6948a
GM
939(defvar ispell-process nil
940 "The process object for Ispell.")
941
2a997209 942(defvar ispell-async-processp (and (fboundp 'delete-process)
fea6948a
GM
943 (fboundp 'process-send-string)
944 (fboundp 'accept-process-output)
945 ;;(fboundp 'start-process)
946 ;;(fboundp 'set-process-filter)
947 ;;(fboundp 'process-kill-without-query)
948 )
949 "Non-nil means that the OS supports asynchronous processes.")
e29702dd 950
e7cdb3e0
RS
951;; Make ispell.el work better with aspell.
952
caea54f8
SM
953(defvar ispell-aspell-dictionary-alist nil
954 "An alist of parsed aspell dicts and associated parameters.
955Internal use.")
999f7d65 956
e7cdb3e0
RS
957(defun ispell-find-aspell-dictionaries ()
958 "Find Aspell's dictionaries, and record in `ispell-dictionary-alist'."
034dc2a3 959 (unless (and ispell-really-aspell ispell-encoding8-command)
5a0c3f56 960 (error "This function only works with aspell >= 0.60"))
a8097641
RS
961 (let* ((dictionaries
962 (split-string
963 (with-temp-buffer
bc4c6633 964 (ispell-call-process ispell-program-name nil t nil "dicts")
a8097641
RS
965 (buffer-string))))
966 ;; Search for the named dictionaries.
967 (found
24b52f08 968 (delq nil
a8097641 969 (mapcar #'ispell-aspell-find-dictionary dictionaries))))
918555b8
EZ
970 ;; Ensure aspell's alias dictionary will override standard
971 ;; definitions.
972 (setq found (ispell-aspell-add-aliases found))
8cab9efc 973 ;; Merge into FOUND any elements from the standard ispell-dictionary-base-alist
24b52f08 974 ;; which have no element in FOUND at all.
8cab9efc 975 (dolist (dict ispell-dictionary-base-alist)
a8097641
RS
976 (unless (assoc (car dict) found)
977 (setq found (nconc found (list dict)))))
caea54f8 978 (setq ispell-aspell-dictionary-alist found)
e7cdb3e0 979 ;; Add a default entry
5abf1556
SM
980 (let ((default-dict
981 '(nil "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-B") nil utf-8)))
caea54f8 982 (push default-dict ispell-aspell-dictionary-alist))))
e7cdb3e0
RS
983
984(defvar ispell-aspell-data-dir nil
985 "Data directory of Aspell.")
986
987(defvar ispell-aspell-dict-dir nil
988 "Dictionary directory of Aspell.")
989
990(defun ispell-get-aspell-config-value (key)
991 "Return value of Aspell configuration option KEY.
992Assumes that value contains no whitespace."
993 (with-temp-buffer
bc4c6633 994 (ispell-call-process ispell-program-name nil t nil "config" key)
e7cdb3e0
RS
995 (car (split-string (buffer-string)))))
996
997(defun ispell-aspell-find-dictionary (dict-name)
df987d70 998 "For aspell dictionary DICT-NAME, return a list of parameters if an
d9a8eb66
EZ
999associated data file is found or nil otherwise. List format is that
1000of `ispell-dictionary-base-alist' elements."
df987d70
AM
1001 ;; Make sure `ispell-aspell-data-dir' is defined
1002 (or ispell-aspell-data-dir
1003 (setq ispell-aspell-data-dir
1004 (ispell-get-aspell-config-value "data-dir")))
1005 ;; Try finding associated datafile
1006 (let* ((datafile1
1007 (concat ispell-aspell-data-dir "/"
1008 ;; Strip out variant, country code, etc.
1009 (and (string-match "^[[:alpha:]]+" dict-name)
1010 (match-string 0 dict-name)) ".dat"))
1011 (datafile2
1012 (concat ispell-aspell-data-dir "/"
1013 ;; Strip out anything but xx_YY.
1014 (and (string-match "^[[:alpha:]_]+" dict-name)
1015 (match-string 0 dict-name)) ".dat"))
e7cdb3e0 1016 (data-file
df987d70
AM
1017 (if (file-readable-p datafile1)
1018 datafile1
1019 (if (file-readable-p datafile2)
1020 datafile2)))
e7cdb3e0 1021 otherchars)
df987d70
AM
1022
1023 (if data-file
a8097641
RS
1024 (with-temp-buffer
1025 (insert-file-contents data-file)
1026 ;; There is zero or one line with special characters declarations.
1027 (when (search-forward-regexp "^special" nil t)
1028 (let ((specials (split-string
1029 (buffer-substring (point)
1030 (progn (end-of-line) (point))))))
1031 ;; The line looks like: special ' -** - -** . -** : -*-
1032 ;; -** means that this character
1033 ;; - doesn't appear at word start
1034 ;; * may appear in the middle of a word
1035 ;; * may appear at word end
1036 ;; `otherchars' is about the middle case.
1037 (while specials
1038 (when (eq (aref (cadr specials) 1) ?*)
1039 (push (car specials) otherchars))
1040 (setq specials (cddr specials)))))
1041 (list dict-name
1042 "[[:alpha:]]"
1043 "[^[:alpha:]]"
1044 (regexp-opt otherchars)
1045 t ; We can't tell, so set this to t
acf7984c 1046 (list "-d" dict-name)
a8097641
RS
1047 nil ; aspell doesn't support this
1048 ;; Here we specify the encoding to use while communicating with
1049 ;; aspell. This doesn't apply to command line arguments, so
1050 ;; just don't pass words to spellcheck as arguments...
df987d70 1051 'utf-8)))))
e7cdb3e0 1052
918555b8
EZ
1053(defun ispell-aspell-add-aliases (alist)
1054 "Find aspell's dictionary aliases and add them to dictionary ALIST.
1055Return the new dictionary alist."
5abf1556
SM
1056 (let ((aliases
1057 (file-expand-wildcards
e7cdb3e0
RS
1058 (concat (or ispell-aspell-dict-dir
1059 (setq ispell-aspell-dict-dir
1060 (ispell-get-aspell-config-value "dict-dir")))
1061 "/*.alias"))))
1062 (dolist (alias-file aliases)
1063 (with-temp-buffer
1064 (insert-file-contents alias-file)
1065 ;; Look for a line "add FOO.multi", extract FOO
1066 (when (search-forward-regexp "^add \\([^.]+\\)\\.multi" nil t)
66793230 1067 (let* ((aliasname (file-name-sans-extension
e7cdb3e0 1068 (file-name-nondirectory alias-file)))
918555b8 1069 (already-exists-p (assoc aliasname alist))
e7cdb3e0 1070 (realname (match-string 1))
918555b8 1071 (realdict (assoc realname alist)))
e7cdb3e0 1072 (when (and realdict (not already-exists-p))
918555b8
EZ
1073 (push (cons aliasname (cdr realdict)) alist))))))
1074 alist))
e7cdb3e0 1075
caea54f8
SM
1076;; Set params according to the selected spellchecker
1077
1078(defvar ispell-last-program-name nil
d1f18ec0 1079 "Last value of `ispell-program-name'. Internal use.")
caea54f8
SM
1080
1081(defvar ispell-initialize-spellchecker-hook nil
f753f75d
SM
1082 "Normal hook run on spellchecker initialization.
1083This hook is run when a spellchecker is used for the first
1084time, before `ispell-dictionary-alist' is set. It is intended for
1085sysadmins to override entries in `ispell-dictionary-base-alist'
1086by putting those overrides in `ispell-base-dicts-override-alist', which is
1087a dynamically scoped var with same format as `ispell-dictionary-alist'.
1088This alist will not override the auto-detected values (e.g. if a recent
1089aspell is used along with Emacs).")
caea54f8
SM
1090
1091(defun ispell-set-spellchecker-params ()
1092 "Initialize some spellchecker parameters when changed or first used."
1093 (unless (eq ispell-last-program-name ispell-program-name)
1094 (setq ispell-last-program-name ispell-program-name)
1095 (ispell-kill-ispell t)
1096 (if (and (condition-case ()
1097 (progn
1098 (setq ispell-library-directory (ispell-check-version))
1099 t)
1100 (error nil))
1101 ispell-really-aspell
57bf8fd4 1102 ispell-encoding8-command
177eca34 1103 ispell-emacs-alpha-regexp)
caea54f8
SM
1104 (unless ispell-aspell-dictionary-alist
1105 (ispell-find-aspell-dictionaries)))
1106
1107 ;; Substitute ispell-dictionary-alist with the list of dictionaries
1108 ;; corresponding to the given spellchecker. If a recent aspell, use
1109 ;; the list of really installed dictionaries and add to it elements
1110 ;; of the original list that are not present there. Allow distro info.
f753f75d 1111 (let ((found-dicts-alist
caea54f8 1112 (if (and ispell-really-aspell
57bf8fd4 1113 ispell-encoding8-command)
caea54f8
SM
1114 ispell-aspell-dictionary-alist
1115 nil))
1116 ispell-base-dicts-override-alist ; Override only base-dicts-alist
1117 all-dicts-alist)
1118
1119 (run-hooks 'ispell-initialize-spellchecker-hook)
1120
1121 ;; Add dicts to ``ispell-dictionary-alist'' unless already present.
1122 (dolist (dict (append found-dicts-alist
1123 ispell-base-dicts-override-alist
f753f75d 1124 ispell-dictionary-base-alist))
caea54f8
SM
1125 (unless (assoc (car dict) all-dicts-alist)
1126 (add-to-list 'all-dicts-alist dict)))
177eca34
AM
1127 (setq ispell-dictionary-alist all-dicts-alist))
1128
1129 ;; If Emacs flavor supports [:alpha:] use it for global dicts. If
1130 ;; spellchecker also supports UTF-8 via command-line option use it
1131 ;; in communication. This does not affect definitions in ~/.emacs.
1132 (if ispell-emacs-alpha-regexp
1133 (let (tmp-dicts-alist)
1134 (dolist (adict ispell-dictionary-alist)
1135 (add-to-list 'tmp-dicts-alist
1136 (list
1137 (nth 0 adict) ; dict name
1138 "[[:alpha:]]" ; casechars
1139 "[^[:alpha:]]" ; not-casechars
1140 (nth 3 adict) ; otherchars
1141 (nth 4 adict) ; many-otherchars-p
1142 (nth 5 adict) ; ispell-args
1143 (nth 6 adict) ; extended-character-mode
1144 (if ispell-encoding8-command
1145 'utf-8
1146 (nth 7 adict)))))
1147 (setq ispell-dictionary-alist tmp-dicts-alist)))))
caea54f8 1148
4d170362 1149(defun ispell-valid-dictionary-list ()
94eb3112 1150 "Return a list of valid dictionaries.
d9a8eb66 1151The variable `ispell-library-directory' defines their location."
caea54f8 1152 ;; Initialize variables and dictionaries alists for desired spellchecker.
f753f75d 1153 ;; Make sure ispell.el is loaded to avoid some autoload loops in XEmacs
caea54f8
SM
1154 ;; (and may be others)
1155 (if (featurep 'ispell)
1156 (ispell-set-spellchecker-params))
1157
a6e7a34b 1158 (let ((dicts (append ispell-local-dictionary-alist ispell-dictionary-alist))
4d170362 1159 (dict-list (cons "default" nil))
73b4d51a 1160 name dict-bname)
4d170362
SM
1161 (dolist (dict dicts)
1162 (setq name (car dict)
73b4d51a
AM
1163 dict-bname (or (car (cdr (member "-d" (nth 5 dict))))
1164 name))
4d170362
SM
1165 ;; Include if the dictionary is in the library, or dir not defined.
1166 (if (and
1167 name
e7cdb3e0
RS
1168 ;; For Aspell, we already know which dictionaries exist.
1169 (or ispell-really-aspell
73b4d51a
AM
1170 ;; Include all dictionaries if lib directory not known.
1171 ;; Same for Hunspell, where ispell-library-directory is nil.
e7cdb3e0 1172 (not ispell-library-directory)
4d170362 1173 (file-exists-p (concat ispell-library-directory
73b4d51a
AM
1174 "/" dict-bname ".hash"))
1175 (file-exists-p (concat ispell-library-directory
1176 "/" dict-bname ".has"))))
1177 (push name dict-list)))
4d170362
SM
1178 dict-list))
1179
60c4db3a 1180;; Define commands in menu in opposite order you want them to appear.
d38e5dbe 1181;;;###autoload
a60f1fcd 1182(if ispell-menu-map-needed
12e53d2e 1183 (progn
e7cdb3e0 1184 (setq ispell-menu-map (make-sparse-keymap "Spell"))
12e53d2e 1185 (define-key ispell-menu-map [ispell-change-dictionary]
8f43cbf3
DN
1186 `(menu-item ,(purecopy "Change Dictionary...") ispell-change-dictionary
1187 :help ,(purecopy "Supply explicit dictionary file name")))
12e53d2e 1188 (define-key ispell-menu-map [ispell-kill-ispell]
da00640a
AM
1189 `(menu-item ,(purecopy "Kill Process")
1190 (lambda () (interactive) (ispell-kill-ispell nil 'clear))
cdd0f857 1191 :enable (and (boundp 'ispell-process) ispell-process
8eba343c 1192 (eq (ispell-process-status) 'run))
8f43cbf3 1193 :help ,(purecopy "Terminate Ispell subprocess")))
12e53d2e 1194 (define-key ispell-menu-map [ispell-pdict-save]
8f43cbf3 1195 `(menu-item ,(purecopy "Save Dictionary")
fea6948a 1196 (lambda () (interactive) (ispell-pdict-save t t))
8f43cbf3 1197 :help ,(purecopy "Save personal dictionary")))
a9c6d330 1198 (define-key ispell-menu-map [ispell-customize]
8f43cbf3 1199 `(menu-item ,(purecopy "Customize...")
a9c6d330 1200 (lambda () (interactive) (customize-group 'ispell))
8f43cbf3 1201 :help ,(purecopy "Customize spell checking options")))
8eba343c
EZ
1202 (define-key ispell-menu-map [ispell-help]
1203 ;; use (x-popup-menu last-nonmenu-event(list "" ispell-help-list)) ?
8f43cbf3 1204 `(menu-item ,(purecopy "Help")
fea6948a 1205 (lambda () (interactive) (describe-function 'ispell-help))
8f43cbf3 1206 :help ,(purecopy "Show standard Ispell keybindings and commands")))
a9c6d330 1207 (define-key ispell-menu-map [flyspell-mode]
8f43cbf3 1208 `(menu-item ,(purecopy "Automatic spell checking (Flyspell)")
a9c6d330 1209 flyspell-mode
8f43cbf3 1210 :help ,(purecopy "Check spelling while you edit the text")
420adcc6 1211 :button (:toggle . (bound-and-true-p flyspell-mode))))
12e53d2e 1212 (define-key ispell-menu-map [ispell-complete-word]
8f43cbf3
DN
1213 `(menu-item ,(purecopy "Complete Word") ispell-complete-word
1214 :help ,(purecopy "Complete word at cursor using dictionary")))
12e53d2e 1215 (define-key ispell-menu-map [ispell-complete-word-interior-frag]
5abf1556
SM
1216 `(menu-item ,(purecopy "Complete Word Fragment")
1217 ispell-complete-word-interior-frag
8f43cbf3 1218 :help ,(purecopy "Complete word fragment at cursor")))))
12e53d2e 1219
d38e5dbe 1220;;;###autoload
a60f1fcd 1221(if ispell-menu-map-needed
12e53d2e
RS
1222 (progn
1223 (define-key ispell-menu-map [ispell-continue]
8f43cbf3 1224 `(menu-item ,(purecopy "Continue Spell-Checking") ispell-continue
8eba343c
EZ
1225 :enable (and (boundp 'ispell-region-end)
1226 (marker-position ispell-region-end)
1227 (equal (marker-buffer ispell-region-end)
fea6948a 1228 (current-buffer)))
8f43cbf3 1229 :help ,(purecopy "Continue spell checking last region")))
12e53d2e 1230 (define-key ispell-menu-map [ispell-word]
8f43cbf3
DN
1231 `(menu-item ,(purecopy "Spell-Check Word") ispell-word
1232 :help ,(purecopy "Spell-check word at cursor")))
f5136913 1233 (define-key ispell-menu-map [ispell-comments-and-strings]
5abf1556
SM
1234 `(menu-item ,(purecopy "Spell-Check Comments")
1235 ispell-comments-and-strings
8f43cbf3 1236 :help ,(purecopy "Spell-check only comments and strings")))))
60d48874 1237
d38e5dbe 1238;;;###autoload
a60f1fcd 1239(if ispell-menu-map-needed
60d48874 1240 (progn
8eba343c 1241 (define-key ispell-menu-map [ispell-region]
8f43cbf3 1242 `(menu-item ,(purecopy "Spell-Check Region") ispell-region
8eba343c 1243 :enable mark-active
8f43cbf3 1244 :help ,(purecopy "Spell-check text in marked region")))
12e53d2e 1245 (define-key ispell-menu-map [ispell-message]
8f43cbf3 1246 `(menu-item ,(purecopy "Spell-Check Message") ispell-message
17849b4a 1247 :visible (eq major-mode 'mail-mode)
8f43cbf3 1248 :help ,(purecopy "Skip headers and included message text")))
8eba343c 1249 (define-key ispell-menu-map [ispell-buffer]
8f43cbf3
DN
1250 `(menu-item ,(purecopy "Spell-Check Buffer") ispell-buffer
1251 :help ,(purecopy "Check spelling of selected buffer")))
fea6948a 1252 ;;(put 'ispell-region 'menu-enable 'mark-active)
e29702dd
KH
1253 (fset 'ispell-menu-map (symbol-value 'ispell-menu-map))))
1254
9050446c 1255;;; XEmacs versions 19 & 20
877d353a 1256(if (and (featurep 'xemacs)
f5136913 1257 (featurep 'menubar)
4d170362 1258 ;;(null ispell-menu-xemacs)
f5136913 1259 (not (and (boundp 'infodock-version) infodock-version)))
4d170362
SM
1260 (let ((dicts (if (fboundp 'ispell-valid-dictionary-list)
1261 (reverse (ispell-valid-dictionary-list))))
e29702dd
KH
1262 (current-menubar (or current-menubar default-menubar))
1263 (menu
1264 '(["Help" (describe-function 'ispell-help) t]
1265 ;;["Help" (popup-menu ispell-help-list) t]
1266 ["Check Message" ispell-message t]
1267 ["Check Buffer" ispell-buffer t]
f5136913 1268 ["Check Comments" ispell-comments-and-strings t]
e29702dd
KH
1269 ["Check Word" ispell-word t]
1270 ["Check Region" ispell-region (or (not zmacs-regions) (mark))]
1271 ["Continue Check" ispell-continue t]
1272 ["Complete Word Frag"ispell-complete-word-interior-frag t]
1273 ["Complete Word" ispell-complete-word t]
da00640a 1274 ["Kill Process" (ispell-kill-ispell nil 'clear) t]
4d170362
SM
1275 ["Customize..." (customize-group 'ispell) t]
1276 ;; flyspell-mode may not be bound...
1277 ;;["flyspell" flyspell-mode
1278 ;; :style toggle :selected flyspell-mode ]
e29702dd 1279 "-"
9050446c 1280 ["Save Personal Dict"(ispell-pdict-save t t) t]
4d170362
SM
1281 ["Change Dictionary" ispell-change-dictionary t])))
1282 (if (null dicts)
1283 (setq dicts (cons "default" nil)))
1284 (dolist (name dicts)
1285 (setq menu (append menu
1286 (list
1287 (vector
1288 (concat "Select " (capitalize name))
1289 (list 'ispell-change-dictionary name)
1290 t)))))
f5136913 1291 (setq ispell-menu-xemacs menu)
e29702dd
KH
1292 (if current-menubar
1293 (progn
4d170362
SM
1294 (if (car (find-menu-item current-menubar '("Cmds")))
1295 (progn
1296 ;; XEmacs 21.2
1297 (delete-menu-item '("Cmds" "Spell-Check"))
1298 (add-menu '("Cmds") "Spell-Check" ispell-menu-xemacs))
1299 ;; previous
1300 (delete-menu-item '("Edit" "Spell")) ; in case already defined
1301 (add-menu '("Edit") "Spell" ispell-menu-xemacs))))))
f5136913 1302
ec6918a8
DN
1303(defalias 'ispell-int-char
1304 ;; Allow incrementing characters as integers in XEmacs 20
1305 (if (and (featurep 'xemacs)
1306 (fboundp 'int-char))
1307 'int-char
1308 ;; Emacs and XEmacs 19 or earlier
1309 'identity))
007852e0
RS
1310
1311
1312;;; **********************************************************************
007852e0 1313
a6e7a34b 1314(defvar ispell-current-dictionary nil
f5136913 1315 "The name of the current dictionary, or nil for the default.
f5136913 1316This is passed to the ispell process using the `-d' switch and is
a6e7a34b
RS
1317used as key in `ispell-local-dictionary-alist' and `ispell-dictionary-alist'.")
1318
09b2eb5e
RS
1319(defvar ispell-current-personal-dictionary nil
1320 "The name of the current personal dictionary, or nil for the default.
1321This is passed to the ispell process using the `-p' switch.")
1322
ef128a91 1323(defun ispell-decode-string (str)
9050446c
GM
1324 "Decodes multibyte character strings.
1325Protects against bogus binding of `enable-multibyte-characters' in XEmacs."
f753f75d
SM
1326 ;; FIXME: enable-multibyte-characters is read-only, so bogus bindings are
1327 ;; really nasty (they signal an error in Emacs): Who does that? --Stef
877d353a 1328 (if (and (or (featurep 'xemacs)
9050446c
GM
1329 (and (boundp 'enable-multibyte-characters)
1330 enable-multibyte-characters))
f5136913 1331 (fboundp 'decode-coding-string)
f5136913
RS
1332 (ispell-get-coding-system))
1333 (decode-coding-string str (ispell-get-coding-system))
1334 str))
ef128a91 1335
ce77af79 1336;; Return a string decoded from Nth element of the current dictionary.
8253213c 1337(defun ispell-get-decoded-string (n)
94eb3112 1338 "Get the decoded string in slot N of the descriptor of the current dict."
a6e7a34b
RS
1339 (let* ((slot (or
1340 (assoc ispell-current-dictionary ispell-local-dictionary-alist)
a567af58 1341 (assoc ispell-current-dictionary ispell-dictionary-alist)
9ac7a13f
EZ
1342 (error "No data for dictionary \"%s\", neither in `ispell-local-dictionary-alist' nor in `ispell-dictionary-alist'"
1343 ispell-current-dictionary)))
8253213c
KH
1344 (str (nth n slot)))
1345 (when (and (> (length str) 0)
1346 (not (multibyte-string-p str)))
1347 (setq str (ispell-decode-string str))
ce77af79 1348 (or (multibyte-string-p str)
c31fd3c0 1349 (setq str (string-to-multibyte str))))
8253213c
KH
1350 str))
1351
007852e0 1352(defun ispell-get-casechars ()
8253213c 1353 (ispell-get-decoded-string 1))
007852e0 1354(defun ispell-get-not-casechars ()
8253213c 1355 (ispell-get-decoded-string 2))
007852e0 1356(defun ispell-get-otherchars ()
8253213c 1357 (ispell-get-decoded-string 3))
007852e0 1358(defun ispell-get-many-otherchars-p ()
a6e7a34b
RS
1359 (nth 4 (or (assoc ispell-current-dictionary ispell-local-dictionary-alist)
1360 (assoc ispell-current-dictionary ispell-dictionary-alist))))
007852e0 1361(defun ispell-get-ispell-args ()
a6e7a34b
RS
1362 (nth 5 (or (assoc ispell-current-dictionary ispell-local-dictionary-alist)
1363 (assoc ispell-current-dictionary ispell-dictionary-alist))))
007852e0 1364(defun ispell-get-extended-character-mode ()
935d1290
AM
1365 (if ispell-really-hunspell ;; hunspell treats ~word as ordinary words
1366 nil ;; in pipe mode. Disable extended-char-mode
1367 (nth 6 (or (assoc ispell-current-dictionary ispell-local-dictionary-alist)
1368 (assoc ispell-current-dictionary ispell-dictionary-alist)))))
ef128a91 1369(defun ispell-get-coding-system ()
a6e7a34b
RS
1370 (nth 7 (or (assoc ispell-current-dictionary ispell-local-dictionary-alist)
1371 (assoc ispell-current-dictionary ispell-dictionary-alist))))
007852e0 1372
8eba343c 1373
007852e0 1374(defvar ispell-pdict-modified-p nil
a9210a53 1375 "Non-nil means personal dictionary has modifications to be saved.")
007852e0 1376
60c4db3a
SM
1377;; If you want to save the dictionary when quitting, must do so explicitly.
1378;; When non-nil, the spell session is terminated.
1379;; When numeric, contains cursor location in buffer, and cursor remains there.
a9210a53 1380(defvar ispell-quit nil)
007852e0 1381
8eba343c
EZ
1382(defvar ispell-process-directory nil
1383 "The directory where `ispell-process' was started.")
1384
007852e0 1385(defvar ispell-filter nil
a9210a53 1386 "Output filter from piped calls to Ispell.")
007852e0
RS
1387
1388(defvar ispell-filter-continue nil
a9210a53 1389 "Control variable for Ispell filter function.")
007852e0 1390
8eba343c
EZ
1391(defvar ispell-output-buffer nil
1392 "Buffer used for reading output of a synchronous Ispell subprocess.")
1393
1394(defvar ispell-session-buffer nil
1395 "Buffer used for passing input to a synchronous Ispell subprocess.")
1396
1397(defvar ispell-cmd-args nil
1398 "Command-line arguments to pass to a synchronous Ispell subprocess.")
007852e0
RS
1399
1400(defvar ispell-query-replace-marker (make-marker)
a9210a53 1401 "Marker for `query-replace' processing.")
007852e0 1402
f5136913
RS
1403(defvar ispell-recursive-edit-marker (make-marker)
1404 "Marker for return point from recursive edit.")
1405
007852e0 1406(defvar ispell-checking-message nil
4d170362
SM
1407 "Non-nil when we're checking a mail message.
1408Set to the MIME boundary locations when checking messages.")
007852e0
RS
1409
1410(defconst ispell-choices-buffer "*Choices*")
1411
a9210a53 1412(defvar ispell-overlay nil "Overlay variable for Ispell highlighting.")
007852e0
RS
1413
1414;;; *** Buffer Local Definitions ***
1415
db95369b 1416(defconst ispell-words-keyword "LocalWords: "
007852e0
RS
1417 "The keyword for local oddly-spelled words to accept.
1418The keyword will be followed by any number of local word spellings.
d9a8eb66 1419There can be multiple instances of this keyword in the file.")
007852e0
RS
1420
1421(defconst ispell-dictionary-keyword "Local IspellDict: "
f5136913 1422 "The keyword for a local dictionary to use.
a6e7a34b
RS
1423The keyword must be followed by a valid dictionary name, defined in
1424`ispell-local-dictionary-alist' or `ispell-dictionary-alist'.
1425When multiple occurrences exist, the last keyword
f5136913
RS
1426definition is used.")
1427
1428(defconst ispell-pdict-keyword "Local IspellPersDict: "
1429 "The keyword for defining buffer local dictionaries.
1430Keyword must be followed by the filename of a personal dictionary.
1431The last occurring definition in the buffer will be used.")
007852e0
RS
1432
1433(defconst ispell-parsing-keyword "Local IspellParsing: "
a9210a53 1434 "The keyword for overriding default Ispell parsing.
007852e0
RS
1435The above keyword string should be followed by `latex-mode' or
1436`nroff-mode' to put the current buffer into the desired parsing mode.
1437
1438Extended character mode can be changed for this buffer by placing
f5136913
RS
1439a `~' followed by an extended-character mode -- such as `~.tex'.
1440The last occurring definition in the buffer will be used.")
1441
1442;;;###autoload
1443(defvar ispell-skip-region-alist
ca0a881a 1444 `((ispell-words-keyword forward-line)
f5136913
RS
1445 (ispell-dictionary-keyword forward-line)
1446 (ispell-pdict-keyword forward-line)
1447 (ispell-parsing-keyword forward-line)
5abf1556
SM
1448 (,(purecopy "^---*BEGIN PGP [A-Z ]*--*")
1449 . ,(purecopy "^---*END PGP [A-Z ]*--*"))
4d170362 1450 ;; assume multiline uuencoded file? "\nM.*$"?
ca0a881a
DN
1451 (,(purecopy "^begin [0-9][0-9][0-9] [^ \t]+$") . ,(purecopy "\nend\n"))
1452 (,(purecopy "^%!PS-Adobe-[123].0") . ,(purecopy "\n%%EOF\n"))
1453 (,(purecopy "^---* \\(Start of \\)?[Ff]orwarded [Mm]essage")
1454 . ,(purecopy "^---* End of [Ff]orwarded [Mm]essage"))
b857cb5c
EZ
1455 ;; Matches e-mail addresses, file names, http addresses, etc. The
1456 ;; `-+' `_+' patterns are necessary for performance reasons when
1457 ;; `-' or `_' part of word syntax.
6d341a2a 1458 (,(purecopy "\\(--+\\|_+\\|\\(/\\w\\|\\(\\(\\w\\|[-_]\\)+[.:@]\\)\\)\\(\\w\\|[-_]\\)*\\([.:/@]+\\(\\w\\|[-_~=?&]\\)+\\)+\\)"))
4d170362
SM
1459 ;; above checks /.\w sequences
1460 ;;("\\(--+\\|\\(/\\|\\(\\(\\w\\|[-_]\\)+[.:@]\\)\\)\\(\\w\\|[-_]\\)*\\([.:/@]+\\(\\w\\|[-_~=?&]\\)+\\)+\\)")
f5136913 1461 ;; This is a pretty complex regexp. It can be simplified to the following:
fea6948a 1462 ;; "\\(\\w\\|[-_]\\)*\\([.:/@]+\\(\\w\\|[-_]\\|~\\)+\\)+"
6fdf40f3 1463 ;; but some valid text will be skipped, e.g. "his/her". This could be
f5136913
RS
1464 ;; fixed up (at the expense of a moderately more complex regexp)
1465 ;; by not allowing "/" to be the character which triggers the
1466 ;; identification of the computer name, e.g.:
fea6948a 1467 ;; "\\(\\w\\|[-_]\\)+[.:@]\\(\\w\\|[-_]\\)*\\([.:/@]+\\(\\w\\|[-_]\\|~\\)+\\)+"
f5136913 1468 )
94487c4e 1469 "Alist expressing beginning and end of regions not to spell check.
f5136913
RS
1470The alist key must be a regular expression.
1471Valid forms include:
1472 (KEY) - just skip the key.
94487c4e 1473 (KEY . REGEXP) - skip to the end of REGEXP. REGEXP may be string or symbol.
f5136913 1474 (KEY REGEXP) - skip to end of REGEXP. REGEXP must be a string.
94487c4e 1475 (KEY FUNCTION ARGS) - FUNCTION called with ARGS returns end of region.")
09b2eb5e 1476(put 'ispell-skip-region-alist 'risky-local-variable t)
007852e0 1477
f5136913
RS
1478
1479;;;###autoload
1480(defvar ispell-tex-skip-alists
1e8780b1 1481 (purecopy
6fdf40f3 1482 '((;;("%\\[" . "%\\]") ; AMStex block comment...
f5136913
RS
1483 ;; All the standard LaTeX keywords from L. Lamport's guide:
1484 ;; \cite, \hspace, \hspace*, \hyphenation, \include, \includeonly, \input,
1485 ;; \label, \nocite, \rule (in ispell - rest included here)
1486 ("\\\\addcontentsline" ispell-tex-arg-end 2)
1487 ("\\\\add\\(tocontents\\|vspace\\)" ispell-tex-arg-end)
1488 ("\\\\\\([aA]lph\\|arabic\\)" ispell-tex-arg-end)
6fdf40f3 1489 ;;("\\\\author" ispell-tex-arg-end)
f5136913
RS
1490 ("\\\\bibliographystyle" ispell-tex-arg-end)
1491 ("\\\\makebox" ispell-tex-arg-end 0)
fea6948a 1492 ("\\\\e?psfig" ispell-tex-arg-end)
f5136913
RS
1493 ("\\\\document\\(class\\|style\\)" .
1494 "\\\\begin[ \t\n]*{[ \t\n]*document[ \t\n]*}"))
1495 (;; delimited with \begin. In ispell: displaymath, eqnarray, eqnarray*,
1496 ;; equation, minipage, picture, tabular, tabular* (ispell)
fea6948a
GM
1497 ("\\(figure\\|table\\)\\*?" ispell-tex-arg-end 0)
1498 ("list" ispell-tex-arg-end 2)
f5136913 1499 ("program" . "\\\\end[ \t\n]*{[ \t\n]*program[ \t\n]*}")
1e8780b1 1500 ("verbatim\\*?" . "\\\\end[ \t\n]*{[ \t\n]*verbatim\\*?[ \t\n]*}"))))
fb7ada5f 1501 "Lists of regions to be skipped in TeX mode.
f5136913
RS
1502First list is used raw.
1503Second list has key placed inside \\begin{}.
1504
1505Delete or add any regions you want to be automatically selected
1506for skipping in latex mode.")
09b2eb5e 1507(put 'ispell-tex-skip-alist 'risky-local-variable t)
f5136913
RS
1508
1509
4d170362 1510;;;###autoload
1e8780b1 1511(defconst ispell-html-skip-alists
4d170362
SM
1512 '(("<[cC][oO][dD][eE]\\>[^>]*>" "</[cC][oO][dD][eE]*>")
1513 ("<[sS][cC][rR][iI][pP][tT]\\>[^>]*>" "</[sS][cC][rR][iI][pP][tT]>")
1514 ("<[aA][pP][pP][lL][eE][tT]\\>[^>]*>" "</[aA][pP][pP][lL][eE][tT]>")
1515 ("<[vV][eE][rR][bB]\\>[^>]*>" "<[vV][eE][rR][bB]\\>[^>]*>")
1516 ;;("<[tT][tT]\\>[^>]*>" "<[tT][tT]\\>[^>]*>")
1517 ("<[tT][tT]/" "/")
1518 ("<[^ \t\n>]" ">")
1519 ("&[^ \t\n;]" "[; \t\n]"))
fb7ada5f 1520 "Lists of start and end keys to skip in HTML buffers.
2188975f 1521Same format as `ispell-skip-region-alist'.
4d170362 1522Note - substrings of other matches must come last
74a18969 1523 (e.g. \"<[tT][tT]/\" and \"<[^ \\t\\n>]\").")
09b2eb5e 1524(put 'ispell-html-skip-alists 'risky-local-variable t)
4d170362 1525
007852e0 1526(defvar ispell-local-pdict ispell-personal-dictionary
4b03da72 1527 "A buffer local variable containing the current personal dictionary.
a9210a53 1528If non-nil, the value must be a string, which is a file name.
4b03da72 1529
a9210a53
RS
1530If you specify a personal dictionary for the current buffer which is
1531different from the current personal dictionary, the effect is similar
1532to calling \\[ispell-change-dictionary]. This variable is automatically
1533set when defined in the file with either `ispell-pdict-keyword' or the
1534local variable syntax.")
007852e0
RS
1535
1536(make-variable-buffer-local 'ispell-local-pdict)
c38fe6ca 1537;;;###autoload(put 'ispell-local-pdict 'safe-local-variable 'stringp)
007852e0 1538
007852e0
RS
1539(defvar ispell-buffer-local-name nil
1540 "Contains the buffer name if local word definitions were used.
1541Ispell is then restarted because the local words could conflict.")
1542
da00640a
AM
1543(defvar ispell-buffer-session-localwords nil
1544 "List of words accepted for session in this buffer.")
1545
1546(make-variable-buffer-local 'ispell-buffer-session-localwords)
1547
007852e0 1548(defvar ispell-parser 'use-mode-name
fb7ada5f 1549 "Indicates whether ispell should parse the current buffer as TeX Code.
b9431ae0 1550Special value `use-mode-name' tries to guess using the name of `major-mode'.
f5136913
RS
1551Default parser is `nroff'.
1552Currently the only other valid parser is `tex'.
007852e0
RS
1553
1554You can set this variable in hooks in your init file -- eg:
1555
877d353a 1556\(add-hook 'tex-mode-hook (lambda () (setq ispell-parser 'tex)))")
007852e0
RS
1557
1558(defvar ispell-region-end (make-marker)
1559 "Marker that allows spelling continuations.")
1560
0349bab6
RS
1561(defvar ispell-check-only nil
1562 "If non-nil, `ispell-word' does not try to correct the word.")
007852e0 1563
e29702dd 1564
007852e0
RS
1565;;; **********************************************************************
1566;;; **********************************************************************
1567
1568
f5136913 1569
a1ff29b9 1570;;;###autoload (define-key esc-map "$" 'ispell-word)
b9431ae0 1571
4b03da72 1572
8eba343c
EZ
1573(defun ispell-accept-output (&optional timeout-secs timeout-msecs)
1574 "Wait for output from ispell process, or TIMEOUT-SECS and TIMEOUT-MSECS.
1575If asynchronous subprocesses are not supported, call `ispell-filter' and
1576pass it the output of the last ispell invocation."
1577 (if ispell-async-processp
1578 (accept-process-output ispell-process timeout-secs timeout-msecs)
1579 (if (null ispell-process)
1580 (error "No Ispell process to read output from!")
1581 (let ((buf ispell-output-buffer)
1582 ispell-output)
1583 (if (not (bufferp buf))
1584 (setq ispell-filter nil)
0c3cc4dd 1585 (with-current-buffer buf
8eba343c
EZ
1586 (setq ispell-output (buffer-substring-no-properties
1587 (point-min) (point-max))))
1588 (ispell-filter t ispell-output)
0c3cc4dd 1589 (with-current-buffer buf
8eba343c
EZ
1590 (erase-buffer)))))))
1591
8f0d9e76
EZ
1592(defun ispell-send-replacement (misspelled replacement)
1593 "Notify aspell that MISSPELLED should be spelled REPLACEMENT.
2188975f 1594This allows it to improve the suggestion list based on actual misspellings."
8f0d9e76
EZ
1595 (and ispell-really-aspell
1596 (ispell-send-string (concat "$$ra " misspelled "," replacement "\n"))))
1597
8eba343c
EZ
1598
1599(defun ispell-send-string (string)
1600 "Send the string STRING to the Ispell process."
1601 (if ispell-async-processp
1602 (process-send-string ispell-process string)
1603 ;; Asynchronous subprocesses aren't supported on this losing system.
1604 ;; We keep all the directives passed to Ispell during the entire
1605 ;; session in a buffer, and pass them anew each time we invoke
1606 ;; Ispell to process another chunk of text. (Yes, I know this is a
1607 ;; terrible kludge, and it's a bit slow, but it does get the work done.)
1608 (let ((cmd (aref string 0))
1609 ;; The following commands are not passed to Ispell until
3849454a 1610 ;; we have a *real* reason to invoke it.
8eba343c 1611 (cmds-to-defer '(?* ?@ ?~ ?+ ?- ?! ?%))
8eba343c
EZ
1612 (session-buf ispell-session-buffer)
1613 (output-buf ispell-output-buffer)
1614 (ispell-args ispell-cmd-args)
1615 (defdir ispell-process-directory)
1616 prev-pos)
72f16325 1617 (with-current-buffer session-buf
8eba343c
EZ
1618 (setq prev-pos (point))
1619 (setq default-directory defdir)
1620 (insert string)
1621 (if (not (memq cmd cmds-to-defer))
1622 (let (coding-system-for-read coding-system-for-write status)
4d170362
SM
1623 (if (and (boundp 'enable-multibyte-characters)
1624 enable-multibyte-characters)
8eba343c
EZ
1625 (setq coding-system-for-read (ispell-get-coding-system)
1626 coding-system-for-write (ispell-get-coding-system)))
1627 (set-buffer output-buf)
1628 (erase-buffer)
1629 (set-buffer session-buf)
1630 (setq status
bc4c6633
RS
1631 (apply 'ispell-call-process-region
1632 (point-min) (point-max)
8eba343c
EZ
1633 ispell-program-name nil
1634 output-buf nil
c478e4c5
AM
1635 "-a"
1636 ;; hunspell -m option means something different
1637 (if ispell-really-hunspell "" "-m")
1638 ispell-args))
8eba343c
EZ
1639 (set-buffer output-buf)
1640 (goto-char (point-min))
1641 (save-match-data
1642 (if (not (looking-at "@(#) "))
1643 (error "Ispell error: %s"
1644 (buffer-substring-no-properties
1645 (point) (progn (end-of-line) (point)))))
1646 ;; If STRING is "^Z\n", we just started Ispell and need
1647 ;; to retain its version ID line in the output buffer.
1648 ;; Otherwise, remove the ID line, as it will confuse
1649 ;; `ispell-filter'.
1650 (or (string= string "\032\n")
1651 (progn
1652 (forward-line)
1653 (delete-region (point-min) (point))))
1654 ;; If STRING begins with ^ or any normal character, we need
1655 ;; to remove the last line from the session buffer, since it
1656 ;; was just spell-checked, and we don't want to check it again.
1657 ;; The same goes for the # command, since Ispell already saved
1658 ;; the personal dictionary.
1659 (set-buffer session-buf)
1660 (delete-region prev-pos (point))
1661 ;; Ispell run synchronously saves the personal dictionary
1662 ;; after each successful command. So we can remove any
1663 ;; lines in the session buffer that insert words into the
1664 ;; dictionary.
1665 (if (memq status '(0 nil))
1666 (let ((more-lines t))
1667 (goto-char (point-min))
1668 (while more-lines
1669 (if (looking-at "^\\*")
1670 (let ((start (point)))
1671 (forward-line)
1672 (delete-region start (point)))
1673 (setq more-lines (= 0 (forward-line))))))))))))))
1674
f441b81f 1675
007852e0 1676;;;###autoload
69f5930a 1677(defun ispell-word (&optional following quietly continue region)
007852e0 1678 "Check spelling of word under or before the cursor.
a9210a53 1679If the word is not found in dictionary, display possible corrections
e29702dd 1680in a window allowing you to choose one.
a9210a53 1681
4b03da72 1682If optional argument FOLLOWING is non-nil or if `ispell-following-word'
007852e0 1683is non-nil when called interactively, then the following word
3ee3ac77 1684\(rather than preceding\) is checked when the cursor is not over a word.
4b03da72 1685When the optional argument QUIETLY is non-nil or `ispell-quietly' is non-nil
007852e0
RS
1686when called interactively, non-corrective messages are suppressed.
1687
b9431ae0
RS
1688With a prefix argument (or if CONTINUE is non-nil),
1689resume interrupted spell-checking of a buffer or region.
1690
69f5930a
JL
1691Interactively, in Transient Mark mode when the mark is active, call
1692`ispell-region' to check the active region for spelling errors.
1693
a6e7a34b
RS
1694Word syntax is controlled by the definition of the chosen dictionary,
1695which is in `ispell-local-dictionary-alist' or `ispell-dictionary-alist'.
007852e0
RS
1696
1697This will check or reload the dictionary. Use \\[ispell-change-dictionary]
9050446c
GM
1698or \\[ispell-region] to update the Ispell process.
1699
b11a1b2e 1700Return values:
4d170362 1701nil word is correct or spelling is accepted.
9050446c
GM
17020 word is inserted into buffer-local definitions.
1703\"word\" word corrected from word list.
1704\(\"word\" arg\) word is hand entered.
1705quit spell session exited."
69f5930a 1706 (interactive (list ispell-following-word ispell-quietly current-prefix-arg t))
75662d96 1707 (cond
85c8c5b6
AM
1708 ((and region
1709 (if (featurep 'emacs)
1710 (use-region-p)
1711 (and (boundp 'transient-mark-mode) transient-mark-mode
1712 (boundp 'mark-active) mark-active
1713 (not (eq (region-beginning) (region-end))))))
75662d96
JL
1714 (ispell-region (region-beginning) (region-end)))
1715 (continue (ispell-continue))
1716 (t
caea54f8 1717 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
e29702dd 1718 (ispell-accept-buffer-local-defs) ; use the correct dictionary
9e7ad89a 1719 (let ((cursor-location (point)) ; retain cursor location
9e7ad89a 1720 (word (ispell-get-word following))
f5136913
RS
1721 start end poss new-word replace)
1722 ;; De-structure return word info list.
1723 (setq start (car (cdr word))
1724 end (car (cdr (cdr word)))
1725 word (car word))
1726
f0a1f8bd
RS
1727 ;; At this point it used to ignore 2-letter words.
1728 ;; But that is silly; if the user asks for it, we should do it. - rms.
1729 (or quietly
1730 (message "Checking spelling of %s..."
41d207e1 1731 (funcall ispell-format-word-function word)))
f0a1f8bd
RS
1732 (ispell-send-string "%\n") ; put in verbose mode
1733 (ispell-send-string (concat "^" word "\n"))
1734 ;; wait until ispell has processed word
1735 (while (progn
1736 (ispell-accept-output)
1737 (not (string= "" (car ispell-filter)))))
1738 ;;(ispell-send-string "!\n") ;back to terse mode.
1739 (setq ispell-filter (cdr ispell-filter)) ; remove extra \n
1740 (if (and ispell-filter (listp ispell-filter))
1741 (if (> (length ispell-filter) 1)
1742 (error "Ispell and its process have different character maps")
1743 (setq poss (ispell-parse-output (car ispell-filter)))))
1744 (cond ((eq poss t)
1745 (or quietly
1746 (message "%s is correct"
41d207e1 1747 (funcall ispell-format-word-function word)))
a445370f 1748 (and (featurep 'xemacs)
f0a1f8bd 1749 (extent-at start)
e7cdb3e0
RS
1750 (and (fboundp 'delete-extent)
1751 (delete-extent (extent-at start)))))
f0a1f8bd
RS
1752 ((stringp poss)
1753 (or quietly
1754 (message "%s is correct because of root %s"
41d207e1
RW
1755 (funcall ispell-format-word-function word)
1756 (funcall ispell-format-word-function poss)))
a445370f 1757 (and (featurep 'xemacs)
f0a1f8bd 1758 (extent-at start)
e7cdb3e0
RS
1759 (and (fboundp 'delete-extent)
1760 (delete-extent (extent-at start)))))
744ba0e3
JL
1761 ((null poss)
1762 (message "Error checking word %s using %s with %s dictionary"
1763 (funcall ispell-format-word-function word)
1764 (file-name-nondirectory ispell-program-name)
1765 (or ispell-current-dictionary "default")))
f0a1f8bd
RS
1766 (ispell-check-only ; called from ispell minor mode.
1767 (if (fboundp 'make-extent)
e7cdb3e0
RS
1768 (if (fboundp 'set-extent-property)
1769 (let ((ext (make-extent start end)))
1770 (set-extent-property ext 'face ispell-highlight-face)
1771 (set-extent-property ext 'priority 2000)))
f0a1f8bd 1772 (beep)
41d207e1
RW
1773 (message "%s is incorrect"
1774 (funcall ispell-format-word-function word))))
f0a1f8bd
RS
1775 (t ; prompt for correct word.
1776 (save-window-excursion
1777 (setq replace (ispell-command-loop
1778 (car (cdr (cdr poss)))
1779 (car (cdr (cdr (cdr poss))))
1780 (car poss) start end)))
1781 (cond ((equal 0 replace)
1782 (ispell-add-per-file-word-list (car poss)))
1783 (replace
1784 (setq new-word (if (atom replace) replace (car replace))
1785 cursor-location (+ (- (length word) (- end start))
1786 cursor-location))
1787 (if (not (equal new-word (car poss)))
1788 (progn
76a47494
RS
1789 (goto-char start)
1790 ;; Insert first and then delete,
1791 ;; to avoid collapsing markers before and after
1792 ;; into a single place.
f621ccf5 1793 (insert new-word)
64953c0a 1794 (delete-region (point) end)
76a47494
RS
1795 ;; It is meaningless to preserve the cursor position
1796 ;; inside a word that has changed.
1797 (setq cursor-location (point))
f0a1f8bd
RS
1798 (setq end (point))))
1799 (if (not (atom replace)) ;recheck spelling of replacement
1800 (progn
1801 (if (car (cdr replace)) ; query replace requested
1802 (save-window-excursion
1803 (query-replace word new-word t)))
1804 (goto-char start)
1805 ;; single word could be split into multiple words
1806 (setq ispell-quit (not (ispell-region start end)))
1807 ))))
1808 ;; keep if rechecking word and we keep choices win.
1809 (if (get-buffer ispell-choices-buffer)
1810 (kill-buffer ispell-choices-buffer))))
1811 (ispell-pdict-save ispell-silently-savep)
1812 ;; NB: Cancels ispell-quit incorrectly if called from ispell-region
edfa1c2e 1813 (if ispell-quit (setq ispell-quit nil replace 'quit))
9050446c 1814 (goto-char cursor-location) ; return to original location
75662d96 1815 replace))))
007852e0
RS
1816
1817
1818(defun ispell-get-word (following &optional extra-otherchars)
1819 "Return the word for spell-checking according to ispell syntax.
3ee3ac77 1820If optional argument FOLLOWING is non-nil or if `ispell-following-word'
007852e0 1821is non-nil when called interactively, then the following word
e29702dd 1822\(rather than preceding\) is checked when the cursor is not over a word.
4b03da72 1823Optional second argument contains otherchars that can be included in word
d9a8eb66
EZ
1824many times (see the doc string of `ispell-dictionary-alist' for details
1825about otherchars).
007852e0 1826
a6e7a34b
RS
1827Word syntax is controlled by the definition of the chosen dictionary,
1828which is in `ispell-local-dictionary-alist' or `ispell-dictionary-alist'."
0579b5b1 1829 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
007852e0
RS
1830 (let* ((ispell-casechars (ispell-get-casechars))
1831 (ispell-not-casechars (ispell-get-not-casechars))
1832 (ispell-otherchars (ispell-get-otherchars))
1833 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1834 (word-regexp (concat ispell-casechars
1835 "+\\("
f5136913
RS
1836 (if (not (string= "" ispell-otherchars))
1837 (concat ispell-otherchars "?"))
007852e0
RS
1838 (if extra-otherchars
1839 (concat extra-otherchars "?"))
1840 ispell-casechars
1841 "+\\)"
1842 (if (or ispell-many-otherchars-p
1843 extra-otherchars)
1844 "*" "?")))
f5136913 1845 did-it-once prevpt
007852e0
RS
1846 start end word)
1847 ;; find the word
1848 (if (not (looking-at ispell-casechars))
1849 (if following
1850 (re-search-forward ispell-casechars (point-max) t)
1851 (re-search-backward ispell-casechars (point-min) t)))
1852 ;; move to front of word
1853 (re-search-backward ispell-not-casechars (point-min) 'start)
f5136913
RS
1854 (while (and (or (and (not (string= "" ispell-otherchars))
1855 (looking-at ispell-otherchars))
007852e0
RS
1856 (and extra-otherchars (looking-at extra-otherchars)))
1857 (not (bobp))
1858 (or (not did-it-once)
f5136913
RS
1859 ispell-many-otherchars-p)
1860 (not (eq prevpt (point))))
007852e0
RS
1861 (if (and extra-otherchars (looking-at extra-otherchars))
1862 (progn
1863 (backward-char 1)
1864 (if (looking-at ispell-casechars)
1865 (re-search-backward ispell-not-casechars (point-min) 'move)))
f5136913
RS
1866 (setq did-it-once t
1867 prevpt (point))
007852e0
RS
1868 (backward-char 1)
1869 (if (looking-at ispell-casechars)
1870 (re-search-backward ispell-not-casechars (point-min) 'move)
1871 (backward-char -1))))
1872 ;; Now mark the word and save to string.
909f0455
RS
1873 (if (not (re-search-forward word-regexp (point-max) t))
1874 (if ispell-check-only
f5136913 1875 ;; return dummy word when just flagging misspellings
909f0455
RS
1876 (list "" (point) (point))
1877 (error "No word found to check!"))
64953c0a
RS
1878 (setq start (copy-marker (match-beginning 0))
1879 end (point-marker)
909f0455
RS
1880 word (buffer-substring-no-properties start end))
1881 (list word start end))))
007852e0
RS
1882
1883
60c4db3a
SM
1884;; Global ispell-pdict-modified-p is set by ispell-command-loop and
1885;; tracks changes in the dictionary. The global may either be
1886;; a value or a list, whose value is the state of whether the
1887;; dictionary needs to be saved.
007852e0 1888
23b809c2 1889;;;###autoload
007852e0
RS
1890(defun ispell-pdict-save (&optional no-query force-save)
1891 "Check to see if the personal dictionary has been modified.
3ee3ac77 1892If so, ask if it needs to be saved."
007852e0
RS
1893 (interactive (list ispell-silently-savep t))
1894 (if (and ispell-pdict-modified-p (listp ispell-pdict-modified-p))
1895 (setq ispell-pdict-modified-p (car ispell-pdict-modified-p)))
1896 (if (or ispell-pdict-modified-p force-save)
1897 (if (or no-query (y-or-n-p "Personal dictionary modified. Save? "))
1335d078 1898 (progn
8eba343c 1899 (ispell-send-string "#\n") ; save dictionary
1335d078 1900 (message "Personal dictionary saved."))))
007852e0
RS
1901 ;; unassert variable, even if not saved to avoid questioning.
1902 (setq ispell-pdict-modified-p nil))
1903
1904
29aec366 1905(defun ispell-command-loop (miss guess word start end)
007852e0
RS
1906 "Display possible corrections from list MISS.
1907GUESS lists possibly valid affix construction of WORD.
1908Returns nil to keep word.
4b03da72
RS
1909Returns 0 to insert locally into buffer-local dictionary.
1910Returns string for new chosen word.
1911Returns list for new replacement word (will be rechecked).
f5136913
RS
1912 Query-replace when list length is 2.
1913 Automatic query-replace when second element is `query-replace'.
29aec366 1914Highlights the word, which is assumed to run from START to END.
3ee3ac77 1915Global `ispell-pdict-modified-p' becomes a list where the only value
2188975f
JB
1916indicates whether the dictionary has been modified when option `a'
1917or `i' is used.
f5136913 1918Global `ispell-quit' set to start location to continue spell session."
9050446c 1919 (let ((count ?0)
24b0afdc 1920 (line ispell-choices-win-default-height)
47a9f362
MB
1921 ;; ensure 4 context lines.
1922 (max-lines (- (ispell-adjusted-window-height) 4))
e29702dd
KH
1923 (choices miss)
1924 (window-min-height (min window-min-height
24b0afdc 1925 ispell-choices-win-default-height))
e29702dd 1926 (command-characters '( ? ?i ?a ?A ?r ?R ?? ?x ?X ?q ?l ?u ?m ))
8896f22d 1927 (dedicated (window-dedicated-p (selected-window)))
f5136913 1928 (skipped 0)
9050446c 1929 char num result textwin dedicated-win)
e29702dd
KH
1930
1931 ;; setup the *Choices* buffer with valid data.
0c3cc4dd 1932 (with-current-buffer (get-buffer-create ispell-choices-buffer)
4a65d2b3 1933 (setq mode-line-format
5abf1556
SM
1934 (concat
1935 "-- %b -- word: " word
1936 " -- dict: " (or ispell-current-dictionary "default")
1937 " -- prog: " (file-name-nondirectory ispell-program-name)))
4d170362 1938 ;; XEmacs: no need for horizontal scrollbar in choices window
85c8c5b6 1939 (ispell-with-no-warnings
6063ae55
RS
1940 (and (fboundp 'set-specifier)
1941 (boundp 'horizontal-scrollbar-visible-p)
1942 (set-specifier horizontal-scrollbar-visible-p nil
1943 (cons (current-buffer) nil))))
e29702dd
KH
1944 (erase-buffer)
1945 (if guess
1946 (progn
1947 (insert "Affix rules generate and capitalize "
1948 "this word as shown below:\n\t")
1949 (while guess
1950 (if (> (+ 4 (current-column) (length (car guess)))
1951 (window-width))
1952 (progn
1953 (insert "\n\t")
1954 (setq line (1+ line))))
1955 (insert (car guess) " ")
1956 (setq guess (cdr guess)))
f65d1611 1957 (insert "\nUse option `i' to accept this spelling and put it in your private dictionary.\n")
e29702dd
KH
1958 (setq line (+ line (if choices 3 2)))))
1959 (while (and choices
1960 (< (if (> (+ 7 (current-column) (length (car choices))
1961 (if (> count ?~) 3 0))
1962 (window-width))
1963 (progn
1964 (insert "\n")
1965 (setq line (1+ line)))
1966 line)
1967 max-lines))
1968 ;; not so good if there are over 20 or 30 options, but then, if
1969 ;; there are that many you don't want to scan them all anyway...
1970 (while (memq count command-characters) ; skip command characters.
f5136913 1971 (setq count (ispell-int-char (1+ count))
e29702dd
KH
1972 skipped (1+ skipped)))
1973 (insert "(" count ") " (car choices) " ")
1974 (setq choices (cdr choices)
f5136913
RS
1975 count (ispell-int-char (1+ count))))
1976 (setq count (ispell-int-char (- count ?0 skipped))))
e29702dd 1977
8eba343c 1978 ;; ensure word is visible
e29702dd
KH
1979 (if (not (pos-visible-in-window-p end))
1980 (sit-for 0))
fea6948a 1981
f5136913
RS
1982 ;; allow temporary split of dedicated windows...
1983 (if dedicated
1984 (progn
1985 (setq dedicated-win (selected-window))
1986 (set-window-dedicated-p dedicated-win nil)))
1987
e29702dd 1988 ;; Display choices for misspelled word.
f5136913 1989 (ispell-show-choices line end)
e29702dd
KH
1990 (select-window (setq textwin (next-window)))
1991
1992 ;; highlight word, protecting current buffer status
29aec366 1993 (unwind-protect
e29702dd 1994 (progn
f5136913
RS
1995 (and ispell-highlight-p
1996 (ispell-highlight-spelling-error start end t))
e29702dd 1997 ;; Loop until a valid choice is made.
29aec366
RS
1998 (while
1999 (eq
2000 t
2001 (setq
2002 result
2003 (progn
2004 (undo-boundary)
ab9d539c
KH
2005 (let (message-log-max)
2006 (message (concat "C-h or ? for more options; SPC to leave "
2007 "unchanged, Character to replace word")))
09144fdd
CY
2008 (let ((inhibit-quit t)
2009 (input-valid t))
2010 (setq char nil skipped 0)
2011 ;; If the user types C-g, or generates some other
2012 ;; non-character event (such as a frame switch
2013 ;; event), stop ispell. As a special exception,
d1f18ec0 2014 ;; ignore mouse events occurring in the same frame.
09144fdd 2015 (while (and input-valid (not (characterp char)))
7d0105e5 2016 (setq char (read-key))
09144fdd
CY
2017 (setq input-valid
2018 (or (characterp char)
2019 (and (mouse-event-p char)
2020 (eq (selected-frame)
2021 (window-frame
2022 (posn-window (event-start char))))))))
2023 (when (or quit-flag (not input-valid) (= char ?\C-g))
2024 (setq char ?X quit-flag nil)))
29aec366
RS
2025 ;; Adjust num to array offset skipping command characters.
2026 (let ((com-chars command-characters))
2027 (while com-chars
2028 (if (and (> (car com-chars) ?0) (< (car com-chars) char))
2029 (setq skipped (1+ skipped)))
2030 (setq com-chars (cdr com-chars)))
2031 (setq num (- char ?0 skipped)))
2032
2033 (cond
e29702dd 2034 ((= char ? ) nil) ; accept word this time only
29aec366 2035 ((= char ?i) ; accept and insert word into pers dict
8eba343c 2036 (ispell-send-string (concat "*" word "\n"))
29aec366
RS
2037 (setq ispell-pdict-modified-p '(t)) ; dictionary modified!
2038 nil)
2039 ((or (= char ?a) (= char ?A)) ; accept word without insert
8eba343c 2040 (ispell-send-string (concat "@" word "\n"))
da00640a
AM
2041 (add-to-list 'ispell-buffer-session-localwords word)
2042 (or ispell-buffer-local-name ; session localwords might conflict
2043 (setq ispell-buffer-local-name (buffer-name)))
29aec366
RS
2044 (if (null ispell-pdict-modified-p)
2045 (setq ispell-pdict-modified-p
2046 (list ispell-pdict-modified-p)))
2047 (if (= char ?A) 0)) ; return 0 for ispell-add buffer-local
2048 ((or (= char ?r) (= char ?R)) ; type in replacement
f5136913
RS
2049 (and (eq 'block ispell-highlight-p) ; refresh tty's
2050 (ispell-highlight-spelling-error start end nil t))
2051 (let ((result
2052 (if (or (= char ?R) ispell-query-replace-choices)
9050446c
GM
2053 (list (read-string
2054 (format "Query-replacement for %s: "word)
2055 word)
2056 t)
f5136913
RS
2057 (cons (read-string "Replacement for: " word)
2058 nil))))
2059 (and (eq 'block ispell-highlight-p)
2060 (ispell-highlight-spelling-error start end nil
2061 'block))
2062 result))
29aec366 2063 ((or (= char ??) (= char help-char) (= char ?\C-h))
f5136913
RS
2064 (and (eq 'block ispell-highlight-p)
2065 (ispell-highlight-spelling-error start end nil t))
29aec366 2066 (ispell-help)
f5136913
RS
2067 (and (eq 'block ispell-highlight-p)
2068 (ispell-highlight-spelling-error start end nil
2069 'block))
29aec366
RS
2070 t)
2071 ;; Quit and move point back.
2072 ((= char ?x)
2073 (ispell-pdict-save ispell-silently-savep)
2074 (message "Exited spell-checking")
2075 (setq ispell-quit t)
2076 nil)
2077 ;; Quit and preserve point.
2078 ((= char ?X)
2079 (ispell-pdict-save ispell-silently-savep)
10ba4783 2080 (message "%s"
29aec366
RS
2081 (substitute-command-keys
2082 (concat "Spell-checking suspended;"
2083 " use C-u \\[ispell-word] to resume")))
f5136913 2084 (setq ispell-quit start)
29aec366
RS
2085 nil)
2086 ((= char ?q)
2087 (if (y-or-n-p "Really kill Ispell process? ")
2088 (progn
2089 (ispell-kill-ispell t) ; terminate process.
2090 (setq ispell-quit (or (not ispell-checking-message)
2091 (point))
2092 ispell-pdict-modified-p nil))
e29702dd 2093 t)) ; continue if they don't quit.
29aec366 2094 ((= char ?l)
f5136913
RS
2095 (and (eq 'block ispell-highlight-p) ; refresh tty displays
2096 (ispell-highlight-spelling-error start end nil t))
29aec366
RS
2097 (let ((new-word (read-string
2098 "Lookup string (`*' is wildcard): "
9050446c 2099 word)))
29aec366 2100 (if new-word
007852e0 2101 (progn
0c3cc4dd
SM
2102 (with-current-buffer (get-buffer-create
2103 ispell-choices-buffer)
29aec366
RS
2104 (erase-buffer)
2105 (setq count ?0
2106 skipped 0
fd5539c6 2107 mode-line-format ;; setup the *Choices* buffer with valid data.
4a65d2b3 2108 (concat "-- %b -- word: " new-word
fd5539c6
AM
2109 " -- word-list: "
2110 (or ispell-complete-word-dict
2111 ispell-alternate-dictionary))
29aec366 2112 miss (lookup-words new-word)
9050446c 2113 choices miss
24b0afdc 2114 line ispell-choices-win-default-height)
29aec366
RS
2115 (while (and choices ; adjust choices window.
2116 (< (if (> (+ 7 (current-column)
2117 (length (car choices))
2118 (if (> count ?~) 3 0))
2119 (window-width))
2120 (progn
2121 (insert "\n")
9050446c
GM
2122 (setq line (1+ line)))
2123 line)
29aec366
RS
2124 max-lines))
2125 (while (memq count command-characters)
f5136913 2126 (setq count (ispell-int-char (1+ count))
29aec366
RS
2127 skipped (1+ skipped)))
2128 (insert "(" count ") " (car choices) " ")
2129 (setq choices (cdr choices)
f5136913
RS
2130 count (ispell-int-char (1+ count))))
2131 (setq count (ispell-int-char
2132 (- count ?0 skipped))))
9050446c 2133 (ispell-show-choices line end)
29aec366 2134 (select-window (next-window)))))
f5136913
RS
2135 (and (eq 'block ispell-highlight-p)
2136 (ispell-highlight-spelling-error start end nil
2137 'block))
29aec366 2138 t) ; reselect from new choices
6fdf40f3 2139 ((= char ?u) ; insert lowercase into dictionary
8eba343c 2140 (ispell-send-string (concat "*" (downcase word) "\n"))
29aec366
RS
2141 (setq ispell-pdict-modified-p '(t)) ; dictionary modified!
2142 nil)
2143 ((= char ?m) ; type in what to insert
8eba343c
EZ
2144 (ispell-send-string
2145 (concat "*" (read-string "Insert: " word) "\n"))
29aec366
RS
2146 (setq ispell-pdict-modified-p '(t))
2147 (cons word nil))
2148 ((and (>= num 0) (< num count))
2149 (if ispell-query-replace-choices ; Query replace flag
2150 (list (nth num miss) 'query-replace)
2151 (nth num miss)))
2152 ((= char ?\C-l)
2153 (redraw-display) t)
2154 ((= char ?\C-r)
9050446c 2155 ;; This may have alignment errors if current line is edited
f5136913
RS
2156 (if (marker-position ispell-recursive-edit-marker)
2157 (progn
2158 (message "Only one recursive edit session supported")
9050446c
GM
2159 (beep)
2160 (sit-for 2))
f5136913
RS
2161 (set-marker ispell-recursive-edit-marker start)
2162 ;;(set-marker ispell-region-end reg-end)
2163 (and ispell-highlight-p ; unhighlight
2164 (ispell-highlight-spelling-error start end))
2165 (unwind-protect
2166 (progn
9050446c
GM
2167 (message
2168 "%s"
2169 (substitute-command-keys
2170 (concat "Exit recursive edit with"
2171 " \\[exit-recursive-edit]")))
f5136913 2172 (save-window-excursion (save-excursion
9050446c
GM
2173 (recursive-edit))))
2174 ;; protected
2175 (goto-char ispell-recursive-edit-marker)
2176 (if (not (equal (marker-buffer
2177 ispell-recursive-edit-marker)
2178 (current-buffer)))
2179 (progn
2180 (set-marker ispell-recursive-edit-marker nil)
2181 (error
2182 "Cannot continue ispell from this buffer.")))
f5136913 2183 (set-marker ispell-recursive-edit-marker nil)))
9050446c 2184 (list word nil)) ; recheck starting at this word.
29aec366
RS
2185 ((= char ?\C-z)
2186 (funcall (key-binding "\C-z"))
2187 t)
2188 (t (ding) t))))))
2189 result)
e29702dd 2190 ;; protected
f5136913
RS
2191 (and ispell-highlight-p ; unhighlight
2192 (save-window-excursion
2193 (select-window textwin)
2194 (ispell-highlight-spelling-error start end)))
2195 (if dedicated
2196 (set-window-dedicated-p dedicated-win t)))))
2197
2198
2199
2200(defun ispell-show-choices (line end)
2188975f 2201 "Show the choices in another buffer or frame."
9050446c 2202 (if (and ispell-use-framepop-p (fboundp 'framepop-display-buffer))
f5136913
RS
2203 (progn
2204 (framepop-display-buffer (get-buffer ispell-choices-buffer))
94eb3112 2205 ;; (get-buffer-window ispell-choices-buffer t)
f5136913
RS
2206 (select-window (previous-window))) ; *Choices* window
2207 ;; standard selection by splitting a small buffer out of this window.
2208 (let ((choices-window (get-buffer-window ispell-choices-buffer)))
2209 (if choices-window
47a9f362 2210 (if (= line (ispell-adjusted-window-height choices-window))
f5136913
RS
2211 (select-window choices-window)
2212 ;; *Choices* window changed size. Adjust the choices window
2213 ;; without scrolling the spelled window when possible
47a9f362
MB
2214 (let ((window-line
2215 (- line (ispell-adjusted-window-height choices-window)))
f5136913 2216 (visible (progn (vertical-motion -1) (point))))
24b0afdc 2217 (if (< line ispell-choices-win-default-height)
f5136913 2218 (setq window-line (+ window-line
24b0afdc 2219 (- ispell-choices-win-default-height
f5136913
RS
2220 line))))
2221 (move-to-window-line 0)
2222 (vertical-motion window-line)
2223 (set-window-start (selected-window)
2224 (if (> (point) visible) visible (point)))
2225 (goto-char end)
4d170362 2226 (select-window choices-window)
f5136913
RS
2227 (enlarge-window window-line)))
2228 ;; Overlay *Choices* window when it isn't showing
24b0afdc 2229 (ispell-overlay-window (max line ispell-choices-win-default-height)))
f5136913
RS
2230 (switch-to-buffer ispell-choices-buffer)
2231 (goto-char (point-min)))))
007852e0
RS
2232
2233
829b9e73 2234;;;###autoload
007852e0 2235(defun ispell-help ()
3ee3ac77 2236 "Display a list of the options available when a misspelling is encountered.
007852e0
RS
2237
2238Selections are:
2239
2240DIGIT: Replace the word with a digit offered in the *Choices* buffer.
a9210a53
RS
2241SPC: Accept word this time.
2242`i': Accept word and insert into private dictionary.
2243`a': Accept word for this session.
2244`A': Accept word and place in `buffer-local dictionary'.
2245`r': Replace word with typed-in value. Rechecked.
d1f18ec0 2246`R': Replace word with typed-in value. Query-replaced in buffer. Rechecked.
a9210a53
RS
2247`?': Show these commands.
2248`x': Exit spelling buffer. Move cursor to original point.
2249`X': Exit spelling buffer. Leaves cursor at the current point, and permits
007852e0 2250 the aborted check to be completed later.
a9210a53
RS
2251`q': Quit spelling session (Kills ispell process).
2252`l': Look up typed-in replacement in alternate dictionary. Wildcards okay.
2253`u': Like `i', but the word is lower-cased first.
f5136913 2254`m': Place typed-in value in personal dictionary, then recheck current word.
b11a1b2e
JB
2255`C-l': Redraw screen.
2256`C-r': Recursive edit.
2257`C-z': Suspend Emacs or iconify frame."
007852e0 2258
f5136913
RS
2259 (if (equal ispell-help-in-bufferp 'electric)
2260 (progn
2261 (require 'ehelp)
fea6948a 2262 (with-electric-help
f5136913
RS
2263 (function (lambda ()
2264 ;;This shouldn't be necessary: with-electric-help needs
2265 ;; an optional argument telling it about the smallest
2266 ;; acceptable window-height of the help buffer.
4d170362
SM
2267 ;;(if (< (window-height) 15)
2268 ;; (enlarge-window
2269 ;; (- 15 (ispell-adjusted-window-height))))
f5136913
RS
2270 (princ "Selections are:
2271
2272DIGIT: Replace the word with a digit offered in the *Choices* buffer.
2273SPC: Accept word this time.
2274`i': Accept word and insert into private dictionary.
2275`a': Accept word for this session.
2276`A': Accept word and place in `buffer-local dictionary'.
2277`r': Replace word with typed-in value. Rechecked.
d1f18ec0 2278`R': Replace word with typed-in value. Query-replaced in buffer. Rechecked.
f5136913
RS
2279`?': Show these commands.
2280`x': Exit spelling buffer. Move cursor to original point.
2281`X': Exit spelling buffer. Leaves cursor at the current point, and permits
2282 the aborted check to be completed later.
2283`q': Quit spelling session (Kills ispell process).
2284`l': Look up typed-in replacement in alternate dictionary. Wildcards okay.
2285`u': Like `i', but the word is lower-cased first.
2286`m': Place typed-in value in personal dictionary, then recheck current word.
b11a1b2e
JB
2287`C-l': Redraw screen.
2288`C-r': Recursive edit.
2289`C-z': Suspend Emacs or iconify frame.")
2290 nil))))
f5136913
RS
2291
2292
2293 (let ((help-1 (concat "[r/R]eplace word; [a/A]ccept for this session; "
2294 "[i]nsert into private dictionary"))
2295 (help-2 (concat "[l]ook a word up in alternate dictionary; "
2296 "e[x/X]it; [q]uit session"))
2297 (help-3 (concat "[u]ncapitalized insert into dict. "
b3955f8d 2298 "Type 'x C-h f ispell-help' for more help")))
f5136913
RS
2299 (save-window-excursion
2300 (if ispell-help-in-bufferp
2301 (progn
24b0afdc 2302 (ispell-overlay-window 4)
f5136913
RS
2303 (switch-to-buffer (get-buffer-create "*Ispell Help*"))
2304 (insert (concat help-1 "\n" help-2 "\n" help-3))
2305 (sit-for 5)
2306 (kill-buffer "*Ispell Help*"))
41b495af 2307 (unwind-protect
56351784 2308 (let ((resize-mini-windows 'grow-only))
41b495af
RS
2309 (select-window (minibuffer-window))
2310 (erase-buffer)
2311 (message nil)
2312 ;;(set-minibuffer-window (selected-window))
2313 (enlarge-window 2)
2314 (insert (concat help-1 "\n" help-2 "\n" help-3))
2315 (sit-for 5))
2316 (erase-buffer)))))))
007852e0
RS
2317
2318
2319(defun lookup-words (word &optional lookup-dict)
b9431ae0 2320 "Look up WORD in optional word-list dictionary LOOKUP-DICT.
4b03da72
RS
2321A `*' serves as a wild card. If no wild cards, `look' is used if it exists.
2322Otherwise the variable `ispell-grep-command' contains the command used to
2323search for the words (usually egrep).
2324
3ee3ac77 2325Optional second argument contains the dictionary to use; the default is
0b381c7e 2326`ispell-alternate-dictionary', overridden by `ispell-complete-word-dict'
fd5539c6 2327if defined."
007852e0
RS
2328 ;; We don't use the filter for this function, rather the result is written
2329 ;; into a buffer. Hence there is no need to save the filter values.
2330 (if (null lookup-dict)
fd5539c6
AM
2331 (setq lookup-dict (or ispell-complete-word-dict
2332 ispell-alternate-dictionary)))
2333
2334 (if lookup-dict
2335 (unless (file-readable-p lookup-dict)
2336 (error "lookup-words error: Unreadable or missing plain word-list %s."
2337 lookup-dict))
5abf1556
SM
2338 (error (concat "lookup-words error: No plain word-list found at system"
2339 "default locations. "
2340 "Customize `ispell-alternate-dictionary' to set yours.")))
007852e0
RS
2341
2342 (let* ((process-connection-type ispell-use-ptys-p)
2343 (wild-p (string-match "\\*" word))
2344 (look-p (and ispell-look-p ; Only use look for an exact match.
2345 (or ispell-have-new-look (not wild-p))))
007852e0
RS
2346 (prog (if look-p ispell-look-command ispell-grep-command))
2347 (args (if look-p ispell-look-options ispell-grep-options))
2348 status results loc)
52d3c2d0
SM
2349 (with-temp-buffer
2350 (message "Starting \"%s\" process..." (file-name-nondirectory prog))
2351 (if look-p
2352 nil
2353 ;; Convert * to .*
2354 (insert "^" word "$")
2355 (while (search-backward "*" nil t) (insert "."))
2356 (setq word (buffer-string))
2357 (erase-buffer))
2358 (setq status (apply 'ispell-call-process prog nil t nil
2359 (nconc (if (and args (> (length args) 0))
2360 (list args)
2361 (if look-p nil
2362 (list "-e")))
2363 (list word)
2364 (if lookup-dict (list lookup-dict)))))
2365 ;; `grep' returns status 1 and no output when word not found, which
2366 ;; is a perfectly normal thing.
2367 (if (stringp status)
2368 (error "error: %s exited with signal %s"
2369 (file-name-nondirectory prog) status)
2370 ;; Else collect words into `results' in FIFO order.
2371 (goto-char (point-max))
2372 ;; Assure we've ended with \n.
2373 (or (bobp) (= (preceding-char) ?\n) (insert ?\n))
2374 (while (not (bobp))
2375 (setq loc (point))
2376 (forward-line -1)
2377 (push (buffer-substring-no-properties (point)
2378 (1- loc))
2379 results))))
2380 (if (and results (string-match ".+: " (car results)))
2381 (error "%s error: %s" ispell-grep-command (car results)))
007852e0
RS
2382 results))
2383
2384
5abf1556
SM
2385;; "ispell-filter" is a list of output lines from the generating function.
2386;; Each full line (ending with \n) is a separate item on the list.
2387;; "output" can contain multiple lines, part of a line, or both.
2388;; "start" and "end" are used to keep bounds on lines when "output" contains
2389;; multiple lines.
2390;; "ispell-filter-continue" is true when we have received only part of a
2391;; line as output from a generating function ("output" did not end with \n)
2392;; THIS FUNCTION WILL FAIL IF THE PROCESS OUTPUT DOESN'T END WITH \n!
2393;; This is the case when a process dies or fails. The default behavior
2394;; in this case treats the next input received as fresh input.
007852e0
RS
2395
2396(defun ispell-filter (process output)
2397 "Output filter function for ispell, grep, and look."
2398 (let ((start 0)
2399 (continue t)
2400 end)
2401 (while continue
2402 (setq end (string-match "\n" output start)) ; get text up to the newline.
2403 ;; If we get out of sync and ispell-filter-continue is asserted when we
2404 ;; are not continuing, treat the next item as a separate list. When
2405 ;; ispell-filter-continue is asserted, ispell-filter *should* always be a
2406 ;; list!
2407
2408 ;; Continue with same line (item)?
2409 (if (and ispell-filter-continue ispell-filter (listp ispell-filter))
2410 ;; Yes. Add it to the prev item
2411 (setcar ispell-filter
2412 (concat (car ispell-filter) (substring output start end)))
2413 ;; No. This is a new line and item.
2414 (setq ispell-filter
2415 (cons (substring output start end) ispell-filter)))
2416 (if (null end)
2417 ;; We've completed reading the output, but didn't finish the line.
2418 (setq ispell-filter-continue t continue nil)
2419 ;; skip over newline, this line complete.
2420 (setq ispell-filter-continue nil end (1+ end))
2421 (if (= end (length output)) ; No more lines in output
2422 (setq continue nil) ; so we can exit the filter.
2423 (setq start end)))))) ; else move start to next line of input
2424
2425
60c4db3a
SM
2426;; This function destroys the mark location if it is in the word being
2427;; highlighted.
f5136913
RS
2428(defun ispell-highlight-spelling-error-generic (start end &optional highlight
2429 refresh)
3ee3ac77
RS
2430 "Highlight the word from START to END with a kludge using `inverse-video'.
2431When the optional third arg HIGHLIGHT is set, the word is highlighted;
f5136913 2432otherwise it is displayed normally.
5755be32 2433Uses block cursor to highlight one character.
f5136913
RS
2434Optional REFRESH will unhighlighted then highlight, using block cursor
2435 highlighting when REFRESH is equal to `block'."
2436 (and (eq 'block ispell-highlight-p)
2437 (or (eq 'block refresh)
2438 (setq start (1+ start)))) ; On block non-refresh, inc start.
007852e0
RS
2439 (let ((modified (buffer-modified-p)) ; don't allow this fn to modify buffer
2440 (buffer-read-only nil) ; Allow highlighting read-only buffers.
e4769531
PE
2441 (text (buffer-substring-no-properties start end))
2442 ; Save highlight region.
007852e0 2443 (inhibit-quit t) ; inhibit interrupt processing here.
29aec366 2444 (buffer-undo-list t)) ; don't clutter the undo list.
f5136913 2445 (goto-char end)
007852e0 2446 (delete-region start end)
a7acbbe4 2447 (insert-char ? (- end start)) ; minimize amount of redisplay
007852e0
RS
2448 (sit-for 0) ; update display
2449 (if highlight (setq inverse-video (not inverse-video))) ; toggle video
2450 (delete-region start end) ; delete whitespace
2451 (insert text) ; insert text in inverse video.
2452 (sit-for 0) ; update display showing inverse video.
f5136913
RS
2453 (if (not highlight)
2454 (goto-char end)
2455 (setq inverse-video (not inverse-video)) ; toggle video
2456 (and (eq 'block ispell-highlight-p)
2457 (goto-char (1- start)))) ; use block cursor to "highlight" char
2458 (set-buffer-modified-p modified) ; don't modify if flag not set.
2459 (and refresh ; re-highlight
2460 (ispell-highlight-spelling-error-generic
2461 (if (eq 'block refresh) start (- start 2)) end t))))
2462
2463
2464(defun ispell-highlight-spelling-error-xemacs (start end &optional highlight)
3ee3ac77 2465 "Highlight the word from START to END using `isearch-highlight'.
e29702dd 2466When the optional third arg HIGHLIGHT is set, the word is highlighted,
007852e0
RS
2467otherwise it is displayed normally."
2468 (if highlight
2469 (isearch-highlight start end)
1332f1a0 2470 (isearch-dehighlight))
007852e0
RS
2471 ;;(sit-for 0)
2472 )
2473
2474
4b03da72 2475(defun ispell-highlight-spelling-error-overlay (start end &optional highlight)
3ee3ac77
RS
2476 "Highlight the word from START to END using overlays.
2477When the optional third arg HIGHLIGHT is set, the word is highlighted
2478otherwise it is displayed normally.
4b03da72 2479
a9210a53 2480The variable `ispell-highlight-face' selects the face to use for highlighting."
007852e0 2481 (if highlight
9bb3de4b
JL
2482 (if ispell-overlay
2483 (move-overlay ispell-overlay start end (current-buffer))
007852e0 2484 (setq ispell-overlay (make-overlay start end))
9bb3de4b 2485 (overlay-put ispell-overlay 'priority 1001) ;higher than lazy overlays
007852e0 2486 (overlay-put ispell-overlay 'face ispell-highlight-face))
9bb3de4b
JL
2487 (if ispell-overlay
2488 (delete-overlay ispell-overlay)))
1332f1a0
JL
2489 (if (and ispell-lazy-highlight (boundp 'lazy-highlight-cleanup))
2490 (if highlight
2491 (let ((isearch-string
2492 (concat
2493 "\\b"
2494 (regexp-quote (buffer-substring-no-properties start end))
2495 "\\b"))
2496 (isearch-regexp t)
2497 (isearch-case-fold-search nil))
2498 (isearch-lazy-highlight-new-loop
2499 (if (boundp 'reg-start) reg-start)
2500 (if (boundp 'reg-end) reg-end)))
2501 (lazy-highlight-cleanup lazy-highlight-cleanup)
2502 (setq isearch-lazy-highlight-last-string nil))))
007852e0
RS
2503
2504
f5136913 2505(defun ispell-highlight-spelling-error (start end &optional highlight refresh)
29aec366 2506 (cond
877d353a 2507 ((featurep 'xemacs)
f5136913 2508 (ispell-highlight-spelling-error-xemacs start end highlight))
bb41818d 2509 ((and (featurep 'faces)
3b5fab87
EZ
2510 (or (and (fboundp 'display-color-p) (display-color-p))
2511 window-system))
29aec366 2512 (ispell-highlight-spelling-error-overlay start end highlight))
f5136913 2513 (t (ispell-highlight-spelling-error-generic start end highlight refresh))))
007852e0 2514
47a9f362
MB
2515(defun ispell-adjusted-window-height (&optional window)
2516 "Like `window-height', adjusted to correct for the effect of tall mode-lines.
2517The value returned is actually the nominal number of text-lines in the
2518window plus 1. On a terminal, this is the same value returned by
2519`window-height', but if the window has a mode-line is taller than a normal
2520text line, the returned value may be smaller than that from
2521`window-height'."
2522 (cond ((fboundp 'window-text-height)
2523 (1+ (window-text-height window)))
4d170362
SM
2524 ((or (and (fboundp 'display-graphic-p) (display-graphic-p))
2525 (and (featurep 'xemacs) window-system))
47a9f362
MB
2526 (1- (window-height window)))
2527 (t
2528 (window-height window))))
e29702dd 2529
007852e0 2530(defun ispell-overlay-window (height)
3ee3ac77
RS
2531 "Create a window covering the top HEIGHT lines of the current window.
2532Ensure that the line above point is still visible but otherwise avoid
045dbcbc 2533scrolling the current window. Leave the new window selected."
007852e0 2534 (save-excursion
24b0afdc 2535 (let ((oldot (save-excursion (vertical-motion -1) (point)))
007852e0 2536 (top (save-excursion (move-to-window-line height) (point))))
6fdf40f3 2537 ;; If line above old point (line starting at oldot) would be
007852e0
RS
2538 ;; hidden by new window, scroll it to just below new win
2539 ;; otherwise set top line of other win so it doesn't scroll.
2540 (if (< oldot top) (setq top oldot))
c80e3b4a 2541 ;; if frame is unsplittable, temporarily disable that...
f5136913
RS
2542 (if (cdr (assq 'unsplittable (frame-parameters (selected-frame))))
2543 (let ((frame (selected-frame)))
2544 (modify-frame-parameters frame '((unsplittable . nil)))
2545 (split-window nil height)
2546 (modify-frame-parameters frame '((unsplittable . t))))
2547 (split-window nil height))
47a9f362 2548 (let ((deficit (- height (ispell-adjusted-window-height))))
24b0afdc 2549 (when (> deficit 0)
47a9f362
MB
2550 ;; Number of lines the window is still too short. We ensure that
2551 ;; there are at least (1- HEIGHT) lines visible in the window.
24b0afdc
MB
2552 (enlarge-window deficit)
2553 (goto-char top)
2554 (vertical-motion deficit)
2555 (setq top (min (point) oldot))))
007852e0
RS
2556 (set-window-start (next-window) top))))
2557
2558
60c4db3a 2559;; Should we add a compound word match return value?
9050446c 2560(defun ispell-parse-output (output &optional accept-list shift)
f5136913 2561 "Parse the OUTPUT string from Ispell process and return:
a9210a53 25621: t for an exact match.
f5136913 25632: A string containing the root word matched via suffix removal.
007852e0 25643: A list of possible correct spellings of the format:
f5136913 2565 (\"ORIGINAL-WORD\" OFFSET MISS-LIST GUESS-LIST)
3ee3ac77
RS
2566 ORIGINAL-WORD is a string of the possibly misspelled word.
2567 OFFSET is an integer giving the line offset of the word.
f5136913 2568 MISS-LIST and GUESS-LIST are possibly null lists of guesses and misses.
f0529b5b 25694: nil when an error has occurred.
9050446c 2570
72c04ce2 2571Optional second arg ACCEPT-LIST is list of words already accepted.
9050446c 2572Optional third arg SHIFT is an offset to apply based on previous corrections."
007852e0
RS
2573 (cond
2574 ((string= output "") t) ; for startup with pipes...
2575 ((string= output "*") t) ; exact match
f5136913 2576 ((string= output "-") t) ; compound word match
877d353a 2577 ((eq (aref output 0) ?+) ; found because of root word
007852e0 2578 (substring output 2)) ; return root word
b9431ae0 2579 ((equal 0 (string-match "[\ra-zA-Z]" output))
f5136913 2580 (ding) ; error message from ispell!
5673af85 2581 (message "Ispell error: %s" output)
f5136913
RS
2582 (sit-for 5)
2583 nil)
007852e0 2584 (t ; need to process &, ?, and #'s
877d353a 2585 (let ((type (aref output 0)) ; &, ?, or #
007852e0
RS
2586 (original-word (substring output 2 (string-match " " output 2)))
2587 (cur-count 0) ; contains number of misses + guesses
2588 count miss-list guess-list offset)
2589 (setq output (substring output (match-end 0))) ; skip over misspelling
877d353a 2590 (if (eq type ?#)
007852e0 2591 (setq count 0) ; no misses for type #
027a4b6b 2592 (setq count (string-to-number output) ; get number of misses.
007852e0 2593 output (substring output (1+ (string-match " " output 1)))))
027a4b6b 2594 (setq offset (string-to-number output))
94eb3112
SM
2595 (setq output (if (eq type ?#) ; No miss or guess list.
2596 nil
2597 (substring output (1+ (string-match " " output 1)))))
007852e0
RS
2598 (while output
2599 (let ((end (string-match ", \\|\\($\\)" output))) ; end of miss/guess.
2600 (setq cur-count (1+ cur-count))
2601 (if (> cur-count count)
94eb3112
SM
2602 (push (substring output 0 end) guess-list)
2603 (push (substring output 0 end) miss-list))
2604 (setq output (if (match-end 1) ; True only when at end of line.
2605 nil ; No more misses or guesses.
2606 (substring output (+ end 2))))))
9050446c
GM
2607 ;; return results. Accept word if it was already accepted.
2608 ;; adjust offset.
2609 (if (member original-word accept-list)
2610 t
2611 (list original-word
2612 (if (numberp shift) (+ shift offset) offset)
2613 (nreverse miss-list) (nreverse guess-list)))))))
007852e0
RS
2614
2615
8eba343c
EZ
2616(defun ispell-process-status ()
2617 "Return the status of the Ispell process.
2618When asynchronous processes are not supported, `run' is always returned."
2619 (if ispell-async-processp
2620 (process-status ispell-process)
2621 (and ispell-process 'run)))
2622
2623
2624(defun ispell-start-process ()
60c4db3a
SM
2625 "Start the Ispell process, with support for no asynchronous processes.
2626Keeps argument list for future Ispell invocations for no async support."
5abf1556
SM
2627 ;; Local dictionary becomes the global dictionary in use.
2628 (setq ispell-current-dictionary
2629 (or ispell-local-dictionary ispell-dictionary))
2630 (setq ispell-current-personal-dictionary
2631 (or ispell-local-pdict ispell-personal-dictionary))
2632 (let* ((default-directory
2633 (if (and (file-directory-p default-directory)
2634 (file-readable-p default-directory))
2635 default-directory
2636 ;; Defend against bad `default-directory'.
2637 (expand-file-name "~/")))
308e764f 2638 (orig-args (ispell-get-ispell-args))
5abf1556
SM
2639 (args
2640 (append
308e764f
AM
2641 (if (and ispell-current-dictionary ; Not for default dict (nil)
2642 (not (member "-d" orig-args))) ; Only define if not overridden.
5abf1556 2643 (list "-d" ispell-current-dictionary))
308e764f 2644 orig-args
5abf1556
SM
2645 (if ispell-current-personal-dictionary ; Use specified pers dict.
2646 (list "-p"
2647 (expand-file-name ispell-current-personal-dictionary)))
2648 ;; If we are using recent aspell or hunspell, make sure we use the
2649 ;; right encoding for communication. ispell or older aspell/hunspell
2650 ;; does not support this.
2651 (if ispell-encoding8-command
b39bb7e1
EZ
2652 (if ispell-really-hunspell
2653 (list ispell-encoding8-command
2654 (upcase (symbol-name (ispell-get-coding-system))))
2655 (list
2656 (concat ispell-encoding8-command
2657 (symbol-name (ispell-get-coding-system))))))
5abf1556 2658 ispell-extra-args)))
8eba343c 2659
09b2eb5e
RS
2660 ;; Initially we don't know any buffer's local words.
2661 (setq ispell-buffer-local-name nil)
2662
8eba343c
EZ
2663 (if ispell-async-processp
2664 (let ((process-connection-type ispell-use-ptys-p))
2665 (apply 'start-process
2666 "ispell" nil ispell-program-name
5abf1556
SM
2667 "-a" ; Accept single input lines.
2668 ;; Make root/affix combos not in dict.
2669 ;; hunspell -m option means different.
2670 (if ispell-really-hunspell "" "-m")
2671 args))
8eba343c
EZ
2672 (setq ispell-cmd-args args
2673 ispell-output-buffer (generate-new-buffer " *ispell-output*")
2674 ispell-session-buffer (generate-new-buffer " *ispell-session*"))
2675 (ispell-send-string "\032\n") ; so Ispell prints version and exits
2676 t)))
2677
2678
007852e0 2679(defun ispell-init-process ()
3ee3ac77 2680 "Check status of Ispell process and start if necessary."
567efba9
AM
2681 (let* (;; Basename of dictionary used by the spell-checker
2682 (dict-bname (or (car (cdr (member "-d" (ispell-get-ispell-args))))
2683 ispell-current-dictionary))
2684 ;; Use "~/" as default-directory unless using Ispell with per-dir
2685 ;; personal dictionaries and not in a minibuffer under XEmacs
2686 (default-directory
2687 (if (or ispell-really-aspell
2688 ispell-really-hunspell
2689 ;; Protect against bad default-directory
2690 (not (and (file-directory-p default-directory)
2691 (file-readable-p default-directory)))
2692 ;; Ispell and per-dir personal dicts available
2693 (not (or (file-readable-p (concat default-directory
2694 ".ispell_words"))
2695 (file-readable-p (concat default-directory
2696 ".ispell_"
2697 (or dict-bname
2698 "default")))))
2699 ;; Ispell, in a minibuffer, and XEmacs
2700 (and (window-minibuffer-p)
2701 (not (fboundp 'minibuffer-selected-window))))
2702 (expand-file-name "~/")
2703 (expand-file-name default-directory))))
2704 ;; Check if process needs restart
2705 (if (and ispell-process
2706 (eq (ispell-process-status) 'run)
5abf1556
SM
2707 ;; Unless we are using an explicit personal dictionary, ensure
2708 ;; we're in the same default directory! Restart check for
2709 ;; personal dictionary is done in
2710 ;; `ispell-internal-change-dictionary', called from
2711 ;; `ispell-buffer-local-dict'
567efba9
AM
2712 (or (or ispell-local-pdict ispell-personal-dictionary)
2713 (equal ispell-process-directory default-directory)))
2714 (setq ispell-filter nil ispell-filter-continue nil)
2715 ;; may need to restart to select new personal dictionary.
2716 (ispell-kill-ispell t)
8cab9efc
AM
2717 (message "Starting new Ispell process [%s::%s] ..."
2718 ispell-program-name
567efba9
AM
2719 (or ispell-local-dictionary ispell-dictionary "default"))
2720 (sit-for 0)
2721 (setq ispell-library-directory (ispell-check-version)
2722 ispell-process (ispell-start-process)
2723 ispell-filter nil
2724 ispell-filter-continue nil
2725 ispell-process-directory default-directory)
5abf1556 2726
5abf1556 2727 (unless (equal ispell-process-directory (expand-file-name "~/"))
36f7d366
AM
2728 ;; At this point, `ispell-process-directory' will be "~/" unless using
2729 ;; Ispell with directory-specific dicts and not in XEmacs minibuffer.
2730 ;; If not, kill ispell process when killing buffer. It may be in a
2731 ;; removable device that would otherwise become un-mountable.
2732 (with-current-buffer
2733 (if (and (window-minibuffer-p) ;; In minibuffer
2734 (fboundp 'minibuffer-selected-window)) ;; Not XEmacs.
2735 ;; In this case kill ispell only when parent buffer is killed
2736 ;; to avoid over and over ispell kill.
567efba9 2737 (window-buffer (minibuffer-selected-window))
36f7d366
AM
2738 (current-buffer))
2739 ;; 'local does not automatically make hook buffer-local in XEmacs.
2740 (if (featurep 'xemacs)
2741 (make-local-hook 'kill-buffer-hook))
2742 (add-hook 'kill-buffer-hook
2743 (lambda () (ispell-kill-ispell t)) nil 'local)))
567efba9
AM
2744
2745 (if ispell-async-processp
2746 (set-process-filter ispell-process 'ispell-filter))
36f7d366 2747 ;; Protect against XEmacs bogus binding of `enable-multibyte-characters'.
567efba9
AM
2748 (if (and (or (featurep 'xemacs)
2749 (and (boundp 'enable-multibyte-characters)
2750 enable-multibyte-characters))
2751 (fboundp 'set-process-coding-system))
2752 (set-process-coding-system ispell-process (ispell-get-coding-system)
2753 (ispell-get-coding-system)))
2754 ;; Get version ID line
2755 (ispell-accept-output 3)
2756 ;; get more output if filter empty?
2757 (if (null ispell-filter) (ispell-accept-output 3))
2758 (cond ((null ispell-filter)
2759 (error "%s did not output version line" ispell-program-name))
2760 ((and
2761 (stringp (car ispell-filter))
2762 (if (string-match "warning: " (car ispell-filter))
2763 (progn
2764 (ispell-accept-output 3) ; was warn msg.
2765 (stringp (car ispell-filter)))
2766 (null (cdr ispell-filter)))
2767 (string-match "^@(#) " (car ispell-filter)))
2768 ;; got the version line as expected (we already know it's the right
2769 ;; version, so don't bother checking again.)
2770 nil)
2771 (t
2772 ;; Otherwise, it must be an error message. Show the user.
2773 ;; But first wait to see if some more output is going to arrive.
2774 ;; Otherwise we get cool errors like "Can't open ".
2775 (sleep-for 1)
2776 (ispell-accept-output 3)
2777 (error "%s" (mapconcat 'identity ispell-filter "\n"))))
2778 (setq ispell-filter nil) ; Discard version ID line
2779 (let ((extended-char-mode (ispell-get-extended-character-mode)))
2780 (if extended-char-mode ; ~ extended character mode
2781 (ispell-send-string (concat extended-char-mode "\n"))))
2782 (if ispell-async-processp
e337e22c 2783 (if (featurep 'emacs)
36f7d366 2784 (set-process-query-on-exit-flag ispell-process nil)
e337e22c
GM
2785 (if (fboundp 'set-process-query-on-exit-flag)
2786 (set-process-query-on-exit-flag ispell-process nil)
2787 (process-kill-without-query ispell-process)))))))
007852e0 2788
829b9e73 2789;;;###autoload
da00640a 2790(defun ispell-kill-ispell (&optional no-error clear)
a9210a53 2791 "Kill current Ispell process (so that you may start a fresh one).
da00640a
AM
2792With NO-ERROR, just return non-nil if there was no Ispell running.
2793With CLEAR, buffer session localwords are cleaned."
007852e0 2794 (interactive)
2834b9b3
SM
2795 ;; This hook is typically used by flyspell to flush some variables used
2796 ;; to optimize the common cases.
2797 (run-hooks 'ispell-kill-ispell-hook)
6e8a1786
AM
2798 (if (or clear
2799 (if (featurep 'xemacs)
2800 (interactive-p)
2801 (called-interactively-p 'interactive)))
da00640a 2802 (setq ispell-buffer-session-localwords nil))
007852e0 2803 (if (not (and ispell-process
8eba343c 2804 (eq (ispell-process-status) 'run)))
007852e0 2805 (or no-error
60c4db3a 2806 (error "There is no Ispell process running!"))
8eba343c 2807 (if ispell-async-processp
2a997209 2808 (delete-process ispell-process)
60c4db3a
SM
2809 ;; Synchronous processes.
2810 (ispell-send-string "\n") ; Make sure side effects occurred.
8eba343c
EZ
2811 (kill-buffer ispell-output-buffer)
2812 (kill-buffer ispell-session-buffer)
2813 (setq ispell-output-buffer nil
2814 ispell-session-buffer nil))
007852e0 2815 (setq ispell-process nil)
3ee3ac77 2816 (message "Ispell process killed")
007852e0
RS
2817 nil))
2818
60c4db3a
SM
2819;; ispell-change-dictionary is set in some people's hooks. Maybe this should
2820;; call ispell-init-process rather than wait for a spell checking command?
1335d078 2821
007852e0
RS
2822;;;###autoload
2823(defun ispell-change-dictionary (dict &optional arg)
a6e7a34b 2824 "Change to dictionary DICT for Ispell.
8e07e84a
RS
2825With a prefix arg, set it \"globally\", for all buffers.
2826Without a prefix arg, set it \"locally\", just for this buffer.
007852e0 2827
8e07e84a 2828By just answering RET you can find out what the current dictionary is."
007852e0
RS
2829 (interactive
2830 (list (completing-read
2831 "Use new dictionary (RET for current, SPC to complete): "
4d170362 2832 (and (fboundp 'ispell-valid-dictionary-list)
a6e7a34b 2833 (mapcar 'list (ispell-valid-dictionary-list)))
4d170362 2834 nil t)
007852e0 2835 current-prefix-arg))
f6b1b0a8 2836 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
87a6b92c 2837 (unless arg (ispell-buffer-local-dict 'no-reload))
007852e0
RS
2838 (if (equal dict "default") (setq dict nil))
2839 ;; This relies on completing-read's bug of returning "" for no match
2840 (cond ((equal dict "")
87a6b92c 2841 (ispell-internal-change-dictionary)
007852e0 2842 (message "Using %s dictionary"
e42a01cb
CY
2843 (or (and (not arg) ispell-local-dictionary)
2844 ispell-dictionary "default")))
2845 ((equal dict (or (and (not arg) ispell-local-dictionary)
a6e7a34b 2846 ispell-dictionary "default"))
87a6b92c
EZ
2847 ;; Specified dictionary is the default already. Could reload
2848 ;; the dictionaries if needed.
2849 (ispell-internal-change-dictionary)
6e8a1786
AM
2850 (and (if (featurep 'xemacs)
2851 (interactive-p)
2852 (called-interactively-p 'interactive))
a6e7a34b 2853 (message "No change, using %s dictionary" dict)))
007852e0 2854 (t ; reset dictionary!
a6e7a34b
RS
2855 (if (or (assoc dict ispell-local-dictionary-alist)
2856 (assoc dict ispell-dictionary-alist))
2857 (if arg
2858 ;; set default dictionary
2859 (setq ispell-dictionary dict)
2860 ;; set local dictionary
2861 (setq ispell-local-dictionary dict)
2862 (setq ispell-local-dictionary-overridden t))
f5136913 2863 (error "Undefined dictionary: %s" dict))
fc8a237d 2864 (ispell-internal-change-dictionary)
da00640a 2865 (setq ispell-buffer-session-localwords nil)
a6e7a34b
RS
2866 (message "%s Ispell dictionary set to %s"
2867 (if arg "Global" "Local")
2868 dict))))
2869
2870(defun ispell-internal-change-dictionary ()
87a6b92c 2871 "Update the dictionary and the personal dictionary used by Ispell.
d9a8eb66
EZ
2872This may kill the Ispell process; if so, a new one will be started
2873when needed."
87a6b92c
EZ
2874 (let ((dict (or ispell-local-dictionary ispell-dictionary))
2875 (pdict (or ispell-local-pdict ispell-personal-dictionary)))
2876 (unless (and (equal ispell-current-dictionary dict)
2877 (equal ispell-current-personal-dictionary pdict))
09b2eb5e 2878 (ispell-kill-ispell t)
87a6b92c
EZ
2879 (setq ispell-current-dictionary dict
2880 ispell-current-personal-dictionary pdict))))
007852e0 2881
a9cb95dd
SM
2882;; Avoid error messages when compiling for these dynamic variables.
2883(defvar ispell-start)
2884(defvar ispell-end)
2885
2886;; Spelling of comments are checked when ispell-check-comments is non-nil.
007852e0
RS
2887
2888;;;###autoload
9050446c 2889(defun ispell-region (reg-start reg-end &optional recheckp shift)
5755be32 2890 "Interactively check a region for spelling errors.
d9a8eb66
EZ
2891Return nil if spell session was terminated, otherwise returns shift offset
2892amount for last line processed."
007852e0 2893 (interactive "r") ; Don't flag errors on read-only bufs.
caea54f8 2894 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
9050446c
GM
2895 (if (not recheckp)
2896 (ispell-accept-buffer-local-defs)) ; set up dictionary, local words, etc.
4d170362
SM
2897 (let ((skip-region-start (make-marker))
2898 (rstart (make-marker)))
007852e0 2899 (unwind-protect
4b03da72 2900 (save-excursion
75662d96 2901 (message "Spell-checking %s using %s with %s dictionary..."
4b03da72 2902 (if (and (= reg-start (point-min)) (= reg-end (point-max)))
e29702dd 2903 (buffer-name) "region")
4a65d2b3 2904 (file-name-nondirectory ispell-program-name)
a6e7a34b 2905 (or ispell-current-dictionary "default"))
e29702dd 2906 ;; Returns cursor to original location.
4b03da72 2907 (save-window-excursion
4b03da72 2908 (goto-char reg-start)
9050446c 2909 (let ((transient-mark-mode)
f5136913 2910 (case-fold-search case-fold-search)
4d170362
SM
2911 (query-fcc t)
2912 in-comment key)
f5136913
RS
2913 (let (message-log-max)
2914 (message "searching for regions to skip"))
4d170362 2915 (if (re-search-forward (ispell-begin-skip-region-regexp) reg-end t)
f5136913 2916 (progn
94eb3112 2917 (setq key (match-string-no-properties 0))
f5136913
RS
2918 (set-marker skip-region-start (- (point) (length key)))
2919 (goto-char reg-start)))
2920 (let (message-log-max)
5abf1556
SM
2921 (message
2922 "Continuing spelling check using %s with %s dictionary..."
2923 (file-name-nondirectory ispell-program-name)
2924 (or ispell-current-dictionary "default")))
4d170362 2925 (set-marker rstart reg-start)
f5136913
RS
2926 (set-marker ispell-region-end reg-end)
2927 (while (and (not ispell-quit)
2928 (< (point) ispell-region-end))
2929 ;; spell-check region with skipping
2930 (if (and (marker-position skip-region-start)
2931 (<= skip-region-start (point)))
2932 (progn
4d170362
SM
2933 ;; If region inside line comment, must keep comment start.
2934 (setq in-comment (point)
2935 in-comment
2936 (and comment-start
2937 (or (null comment-end) (string= "" comment-end))
2938 (save-excursion
2939 (beginning-of-line)
2940 (re-search-forward comment-start in-comment t))
2941 comment-start))
2942 ;; Can change skip-regexps (in ispell-message)
2943 (ispell-skip-region key) ; moves pt past region.
2944 (set-marker rstart (point))
2945 ;; check for saving large attachments...
2946 (setq query-fcc (and query-fcc
2947 (ispell-ignore-fcc skip-region-start
2948 rstart)))
2949 (if (and (< rstart ispell-region-end)
2950 (re-search-forward
2951 (ispell-begin-skip-region-regexp)
2952 ispell-region-end t))
f5136913 2953 (progn
81771793 2954 (setq key (match-string-no-properties 0))
f5136913
RS
2955 (set-marker skip-region-start
2956 (- (point) (length key)))
4d170362 2957 (goto-char rstart))
f5136913 2958 (set-marker skip-region-start nil))))
4d170362
SM
2959 (setq reg-end (max (point)
2960 (if (marker-position skip-region-start)
2961 (min skip-region-start ispell-region-end)
2962 (marker-position ispell-region-end))))
a9cb95dd 2963 (let* ((ispell-start (point))
9b026d9f 2964 (ispell-end (min (point-at-eol) reg-end))
a9cb95dd
SM
2965 (string (ispell-get-line
2966 ispell-start ispell-end in-comment)))
4d170362 2967 (if in-comment ; account for comment chars added
a9cb95dd 2968 (setq ispell-start (- ispell-start (length in-comment))
4d170362 2969 in-comment nil))
a9cb95dd 2970 (setq ispell-end (point)) ; "end" tracks region retrieved.
f5136913 2971 (if string ; there is something to spell check!
9050446c
GM
2972 ;; (special start end)
2973 (setq shift (ispell-process-line string
2974 (and recheckp shift))))
a9cb95dd 2975 (goto-char ispell-end)))))
9050446c
GM
2976 (if ispell-quit
2977 nil
2978 (or shift 0)))
4b03da72 2979 ;; protected
9050446c
GM
2980 (if (and (not (and recheckp ispell-keep-choices-win))
2981 (get-buffer ispell-choices-buffer))
4b03da72 2982 (kill-buffer ispell-choices-buffer))
4d170362
SM
2983 (set-marker skip-region-start nil)
2984 (set-marker rstart nil)
4b03da72
RS
2985 (if ispell-quit
2986 (progn
2987 ;; preserve or clear the region for ispell-continue.
2988 (if (not (numberp ispell-quit))
2989 (set-marker ispell-region-end nil)
f5136913 2990 ;; Ispell-continue enabled - ispell-region-end is set.
4b03da72
RS
2991 (goto-char ispell-quit))
2992 ;; Check for aborting
2993 (if (and ispell-checking-message (numberp ispell-quit))
2994 (progn
2995 (setq ispell-quit nil)
b9431ae0 2996 (error "Message send aborted")))
9050446c
GM
2997 (if (not recheckp) (setq ispell-quit nil)))
2998 (if (not recheckp) (set-marker ispell-region-end nil))
4b03da72
RS
2999 ;; Only save if successful exit.
3000 (ispell-pdict-save ispell-silently-savep)
221d9807 3001 (message "Spell-checking %s using %s with %s dictionary...done"
75662d96
JL
3002 (if (and (= reg-start (point-min)) (= reg-end (point-max)))
3003 (buffer-name) "region")
4a65d2b3
JL
3004 (file-name-nondirectory ispell-program-name)
3005 (or ispell-current-dictionary "default"))))))
007852e0
RS
3006
3007
f5136913 3008(defun ispell-begin-skip-region-regexp ()
2188975f 3009 "Return a regexp of the search keys for region skipping.
4d170362 3010Includes `ispell-skip-region-alist' plus tex, tib, html, and comment keys.
d9a8eb66 3011Must be called after `ispell-buffer-local-parsing' due to dependence on mode."
94eb3112
SM
3012 (mapconcat
3013 'identity
3014 (delq nil
3015 (list
3016 ;; messages
3017 (if (and ispell-checking-message
3018 (not (eq t ispell-checking-message)))
3019 (mapconcat #'car ispell-checking-message "\\|"))
3020 ;; tex
3021 (if (eq ispell-parser 'tex)
3022 (ispell-begin-tex-skip-regexp))
3023 ;; html stuff
3024 (if ispell-skip-html
3025 (ispell-begin-skip-region ispell-html-skip-alists))
3026 ;; tib
3027 (if ispell-skip-tib ispell-tib-ref-beginning)
3028 ;; Comments
3029 (if (and (eq 'exclusive ispell-check-comments) comment-start)
3030 ;; search from end of current comment to start of next comment.
3031 (if (string= "" comment-end) "^" (regexp-quote comment-end)))
3032 (if (and (null ispell-check-comments) comment-start)
3033 (regexp-quote comment-start))
3034 (ispell-begin-skip-region ispell-skip-region-alist)))
3035 "\\|"))
f5136913
RS
3036
3037
4d170362
SM
3038(defun ispell-begin-skip-region (skip-alist)
3039 "Regular expression for start of regions to skip generated from SKIP-ALIST.
3040Each selection should be a key of SKIP-ALIST;
3041otherwise, the current line is skipped."
3042 (mapconcat (lambda (lst) (if (stringp (car lst)) (car lst) (eval (car lst))))
3043 skip-alist
3044 "\\|"))
3045
3046
f5136913 3047(defun ispell-begin-tex-skip-regexp ()
b9431ae0
RS
3048 "Regular expression of tex commands to skip.
3049Generated from `ispell-tex-skip-alists'."
f5136913 3050 (concat
4d170362 3051 ;; raw tex keys
f5136913
RS
3052 (mapconcat (function (lambda (lst) (car lst)))
3053 (car ispell-tex-skip-alists)
3054 "\\|")
3055 "\\|"
4d170362 3056 ;; keys wrapped in begin{}
f5136913
RS
3057 (mapconcat (function (lambda (lst)
3058 (concat "\\\\begin[ \t\n]*{[ \t\n]*"
3059 (car lst)
3060 "[ \t\n]*}")))
3061 (car (cdr ispell-tex-skip-alists))
3062 "\\|")))
3063
3064
4d170362 3065(defun ispell-skip-region-list ()
2188975f 3066 "Return a list describing key and body regions to skip for this buffer.
4d170362
SM
3067Includes regions defined by `ispell-skip-region-alist', tex mode,
3068`ispell-html-skip-alists', and `ispell-checking-message'.
3069Manual checking must include comments and tib references.
3070The list is of the form described by variable `ispell-skip-region-alist'.
d9a8eb66 3071Must be called after `ispell-buffer-local-parsing' due to dependence on mode."
4d170362
SM
3072 (let ((skip-alist ispell-skip-region-alist))
3073 ;; only additional explicit region definition is tex.
3074 (if (eq ispell-parser 'tex)
3075 (setq case-fold-search nil
3076 skip-alist (append (car ispell-tex-skip-alists)
3077 (car (cdr ispell-tex-skip-alists))
3078 skip-alist)))
3079 (if ispell-skip-html
3080 (setq skip-alist (append ispell-html-skip-alists skip-alist)))
3081 (if (and ispell-checking-message
3082 (not (eq t ispell-checking-message)))
3083 (setq skip-alist (append ispell-checking-message skip-alist)))
3084 skip-alist))
f5136913
RS
3085
3086
3087(defun ispell-tex-arg-end (&optional arg)
4d170362 3088 "Skip across ARG number of braces."
f5136913
RS
3089 (condition-case nil
3090 (progn
3091 (while (looking-at "[ \t\n]*\\[") (forward-sexp))
3092 (forward-sexp (or arg 1)))
3093 (error
744ba0e3 3094 (message "Error skipping s-expressions at point %d." (point))
f5136913
RS
3095 (beep)
3096 (sit-for 2))))
3097
3098
4d170362 3099(defun ispell-ignore-fcc (start end)
2188975f 3100 "Delete the Fcc: message header when large attachments are included.
d9a8eb66 3101Return value `nil' if file with large attachments is saved.
4d170362
SM
3102This can be used to avoid multiple questions for multiple large attachments.
3103Returns point to starting location afterwards."
3104 (let ((result t))
3105 (if (and ispell-checking-message ispell-message-fcc-skip)
3106 (if (< ispell-message-fcc-skip (- end start))
3107 (let (case-fold-search head-end)
3108 (goto-char (point-min))
3109 (setq head-end
3110 (or (re-search-forward
3111 (concat "^" (regexp-quote mail-header-separator) "$")
3112 nil t)
3113 (re-search-forward "^$" nil t)
3114 (point-min)))
3115 (goto-char (point-min))
3116 (if (re-search-forward "^Fcc:" head-end t)
3117 (if (y-or-n-p
3118 "Save copy of this message with large attachments? ")
3119 (setq result nil)
3120 (beginning-of-line)
3121 (kill-line 1)))
3122 (goto-char end))))
3123 result))
3124
3125
3126(defun ispell-skip-region (key)
2188975f 3127 "Skip across KEY and then to end of region.
4d170362
SM
3128Key lookup determines region to skip.
3129Point is placed at end of skipped region."
f5136913
RS
3130 ;; move over key to begin checking.
3131 (forward-char (length key))
3132 (let ((start (point))
4d170362
SM
3133 ;; Regenerate each call... This function can change region definition.
3134 (alist (ispell-skip-region-list))
f5136913
RS
3135 alist-key null-skip)
3136 (cond
3137 ;; what about quoted comment, or comment inside strings?
3138 ((and (null ispell-check-comments) comment-start
3139 (string= key comment-start))
3140 (if (string= "" comment-end)
3141 (forward-line)
3142 (search-forward comment-end ispell-region-end t)))
3143 ((and (eq 'exclusive ispell-check-comments) comment-start
3144 (string= key comment-end))
7a914d3e 3145 (search-forward comment-start ispell-region-end :end))
f5136913
RS
3146 ((and ispell-skip-tib (string-match ispell-tib-ref-beginning key))
3147 (re-search-forward ispell-tib-ref-end ispell-region-end t))
f5136913
RS
3148 ;; markings from alist
3149 (t
3150 (while alist
3151 (setq alist-key (eval (car (car alist))))
3152 (if (string-match alist-key key)
3153 (progn
3154 (setq alist (cdr (car alist)))
3155 (cond
3156 ((null alist) (setq null-skip t)) ; done! Just skip key.
3157 ((not (consp alist))
3158 ;; Search past end of spell region to find this region end.
3159 (re-search-forward (eval alist) (point-max) t))
4d170362
SM
3160 ((and (= 1 (length alist))
3161 (stringp (car alist)))
3162 (re-search-forward (car alist) (point-max) t))
3163 (t
3164 (setq null-skip t) ; error handling in functions!
3165 (if (consp (cdr alist))
3166 (apply (car alist) (cdr alist))
3167 (funcall (car alist)))))
f5136913
RS
3168 (setq alist nil))
3169 (setq alist (cdr alist))))))
3170 (if (and (= start (point)) (null null-skip))
3171 (progn
3172 (message "Matching region end for `%s' point %d not found"
3173 key (point))
3174 (beep)
3175 (sit-for 2)))))
3176
3177
4d170362 3178(defun ispell-get-line (start end in-comment)
5abf1556
SM
3179 "Grab the next line of data.
3180Returns a string with the line data."
f5136913
RS
3181 (let ((ispell-casechars (ispell-get-casechars))
3182 string)
3183 (cond ; LOOK AT THIS LINE AND SKIP OR PROCESS
3184 ((eolp) ; END OF LINE, just go to next line.
3185 (forward-line))
fea6948a 3186 ;;((looking-at "[-#@*+!%~^]") ; SKIP SPECIAL ISPELL CHARACTERS
6fdf40f3 3187 ;; (forward-char 1)) ; not needed as quoted below.
f5136913
RS
3188 ((or (re-search-forward ispell-casechars end t) ; TEXT EXISTS
3189 (re-search-forward "[][()${}]" end t)) ; or MATH COMMANDS
4d170362
SM
3190 (setq string (concat "^" in-comment
3191 (buffer-substring-no-properties start end)
f5136913
RS
3192 "\n"))
3193 (goto-char end))
3194 (t (goto-char end))) ; EMPTY LINE, skip it.
3195 string))
3196
3197
664ca704
KH
3198(defun ispell-looking-at (string)
3199 (let ((coding (ispell-get-coding-system))
3200 (len (length string)))
3201 (and (<= (+ (point) len) (point-max))
3202 (equal (encode-coding-string string coding)
3203 (encode-coding-string (buffer-substring-no-properties
3204 (point) (+ (point) len))
3205 coding)))))
3206
9050446c 3207(defun ispell-process-line (string shift)
d9a8eb66 3208 "Send STRING, a line of text, to ispell and process the result.
9050446c 3209This will modify the buffer for spelling errors.
a9cb95dd
SM
3210Requires variables ISPELL-START and ISPELL-END to be defined in its
3211dynamic scope.
b11a1b2e 3212Returns the sum SHIFT due to changes in word replacements."
a9cb95dd 3213 ;;(declare special ispell-start ispell-end)
9050446c
GM
3214 (let (poss accept-list)
3215 (if (not (numberp shift))
3216 (setq shift 0))
f5136913 3217 ;; send string to spell process and get input.
8eba343c 3218 (ispell-send-string string)
f5136913 3219 (while (progn
8eba343c 3220 (ispell-accept-output)
f5136913
RS
3221 ;; Last item of output contains a blank line.
3222 (not (string= "" (car ispell-filter)))))
3223 ;; parse all inputs from the stream one word at a time.
3224 ;; Place in FIFO order and remove the blank item.
3225 (setq ispell-filter (nreverse (cdr ispell-filter)))
3226 (while (and (not ispell-quit) ispell-filter)
9050446c
GM
3227 ;; get next word, accounting for accepted words and start shifts
3228 (setq poss (ispell-parse-output (car ispell-filter)
3229 accept-list shift))
f5136913
RS
3230 (if (and poss (listp poss)) ; spelling error occurred.
3231 ;; Whenever we have misspellings, we can change
3232 ;; the buffer. Keep boundaries as markers.
3233 ;; Markers can move with highlighting! This destroys
3234 ;; end of region markers line-end and ispell-region-end
3235 (let ((word-start
a9cb95dd 3236 (copy-marker (+ ispell-start ispell-offset (car (cdr poss)))))
f5136913 3237 (word-len (length (car poss)))
a9cb95dd
SM
3238 (line-end (copy-marker ispell-end))
3239 (line-start (copy-marker ispell-start))
f5136913
RS
3240 recheck-region replace)
3241 (goto-char word-start)
3242 ;; Adjust the horizontal scroll & point
3243 (ispell-horiz-scroll)
3244 (goto-char (+ word-len word-start))
3245 (ispell-horiz-scroll)
3246 (goto-char word-start)
3247 (ispell-horiz-scroll)
9050446c
GM
3248
3249 ;; Alignment cannot be tracked and this error will occur when
3250 ;; `query-replace' makes multiple corrections on the starting line.
664ca704 3251 (or (ispell-looking-at (car poss))
f5136913
RS
3252 ;; This occurs due to filter pipe problems
3253 (error (concat "Ispell misalignment: word "
3254 "`%s' point %d; probably incompatible versions")
3255 (car poss) (marker-position word-start)))
f5136913
RS
3256 ;; ispell-cmd-loop can go recursive & change buffer
3257 (if ispell-keep-choices-win
3258 (setq replace (ispell-command-loop
3259 (car (cdr (cdr poss)))
3260 (car (cdr (cdr (cdr poss))))
3261 (car poss) (marker-position word-start)
3262 (+ word-len (marker-position word-start))))
3263 (save-window-excursion
3264 (setq replace (ispell-command-loop
3265 (car (cdr (cdr poss)))
3266 (car (cdr (cdr (cdr poss))))
3267 (car poss) (marker-position word-start)
3268 (+ word-len (marker-position word-start))))))
3269
f5136913 3270 (goto-char word-start)
9050446c
GM
3271 ;; Recheck when query replace edit changes misspelled word.
3272 ;; Error in tex mode when a potential math mode change exists.
3273 (if (and replace (listp replace) (= 2 (length replace)))
3274 (if (and (eq ispell-parser 'tex)
3275 (string-match "[\\\\][]()[]\\|\\\\begin\\|\\$"
3276 (regexp-quote string)))
3277 (error
3278 "Don't start query replace on a line with math characters"
3279 )
f5136913
RS
3280 (set-marker line-end (point))
3281 (setq ispell-filter nil
3282 recheck-region t)))
8eba343c 3283
9050446c 3284 ;; insert correction if needed
f5136913 3285 (cond
f5136913
RS
3286 ((or (null replace)
3287 (equal 0 replace)) ; ACCEPT/INSERT
3288 (if (equal 0 replace) ; BUFFER-LOCAL DICT ADD
3289 (ispell-add-per-file-word-list (car poss)))
9050446c
GM
3290 ;; do not recheck accepted word on this line
3291 (setq accept-list (cons (car poss) accept-list)))
3292 (t ; replacement word selected or entered
f5136913 3293 (delete-region (point) (+ word-len (point)))
9050446c
GM
3294 (if (not (listp replace))
3295 (progn
f621ccf5 3296 (insert replace) ; insert dictionary word
8f0d9e76 3297 (ispell-send-replacement (car poss) replace)
9050446c
GM
3298 (setq accept-list (cons replace accept-list)))
3299 (let ((replace-word (car replace)))
3300 ;; Recheck hand entered replacement word
3301 (insert replace-word)
8f0d9e76 3302 (ispell-send-replacement (car poss) replace-word)
9050446c
GM
3303 (if (car (cdr replace))
3304 (save-window-excursion
3305 (delete-other-windows) ; to correctly show help.
3306 ;; Assume case-replace &
3307 ;; case-fold-search correct?
3308 (query-replace (car poss) (car replace) t)))
3309 (goto-char word-start)
3310 ;; do not recheck if already accepted
3311 (if (member replace-word accept-list)
3312 (setq accept-list (cons replace-word accept-list)
3313 replace replace-word)
3314 (let ((region-end (copy-marker ispell-region-end)))
3315 (setq recheck-region ispell-filter
3316 ispell-filter nil ; save filter
fea6948a 3317 shift 0 ; already accounted
9050446c 3318 shift (ispell-region
fea6948a
GM
3319 word-start
3320 (+ word-start (length replace-word))
3321 t shift))
9050446c
GM
3322 (if (null shift) ; quitting check.
3323 (setq shift 0))
3324 (set-marker ispell-region-end region-end)
3325 (set-marker region-end nil)
3326 (setq ispell-filter recheck-region
3327 recheck-region nil
3328 replace replace-word)))))
3329
3330 (setq shift (+ shift (- (length replace) word-len)))
3331
3332 ;; Move line-start across word...
3333 ;; new shift function does this now...
3334 ;;(set-marker line-start (+ line-start
fa72a6a1
SM
3335 ;; (- (length replace)
3336 ;; (length (car poss)))))
9050446c 3337 ))
f5136913 3338 (if (not ispell-quit)
5abf1556 3339 ;; FIXME: remove redundancy with identical code above.
f5136913 3340 (let (message-log-max)
5abf1556
SM
3341 (message
3342 "Continuing spelling check using %s with %s dictionary..."
3343 (file-name-nondirectory ispell-program-name)
3344 (or ispell-current-dictionary "default"))))
f5136913 3345 (sit-for 0)
a9cb95dd
SM
3346 (setq ispell-start (marker-position line-start)
3347 ispell-end (marker-position line-end))
f5136913 3348 ;; Adjust markers when end of region lost from highlighting.
a9cb95dd
SM
3349 (if (and (not recheck-region)
3350 (< ispell-end (+ word-start word-len)))
3351 (setq ispell-end (+ word-start word-len)))
f5136913
RS
3352 (if (= word-start ispell-region-end)
3353 (set-marker ispell-region-end (+ word-start word-len)))
3354 ;; going out of scope - unneeded
3355 (set-marker line-start nil)
3356 (set-marker word-start nil)
3357 (set-marker line-end nil)))
3358 ;; finished with misspelling!
9050446c
GM
3359 (setq ispell-filter (cdr ispell-filter)))
3360 shift))
f5136913
RS
3361
3362
bc7d6816
GM
3363;;;###autoload
3364(defun ispell-comments-and-strings ()
3365 "Check comments and strings in the current buffer for spelling errors."
3366 (interactive)
3367 (goto-char (point-min))
3368 (let (state done)
3369 (while (not done)
3370 (setq done t)
3371 (setq state (parse-partial-sexp (point) (point-max)
3372 nil nil state 'syntax-table))
6fdf40f3
KH
3373 (if (or (nth 3 state) (nth 4 state))
3374 (let ((start (point)))
3375 (setq state (parse-partial-sexp start (point-max)
3376 nil nil state 'syntax-table))
3377 (if (or (nth 3 state) (nth 4 state))
3378 (error "Unterminated string or comment"))
3379 (save-excursion
3380 (setq done (not (ispell-region start (point))))))))))
bc7d6816 3381
007852e0
RS
3382
3383;;;###autoload
4b03da72 3384(defun ispell-buffer ()
007852e0
RS
3385 "Check the current buffer for spelling errors interactively."
3386 (interactive)
3387 (ispell-region (point-min) (point-max)))
3388
4b03da72 3389
829b9e73 3390;;;###autoload
007852e0 3391(defun ispell-continue ()
f5136913 3392 "Continue a halted spelling session beginning with the current word."
b9431ae0 3393 (interactive)
007852e0
RS
3394 (if (not (marker-position ispell-region-end))
3395 (message "No session to continue. Use 'X' command when checking!")
3396 (if (not (equal (marker-buffer ispell-region-end) (current-buffer)))
3397 (message "Must continue ispell from buffer %s"
3398 (buffer-name (marker-buffer ispell-region-end)))
f5136913
RS
3399 (ispell-region
3400 ;; find beginning of current word:
3401 (car (cdr (ispell-get-word t)))
3402 (marker-position ispell-region-end)))))
007852e0
RS
3403
3404
3405;;; Horizontal scrolling
4b03da72 3406(defun ispell-horiz-scroll ()
2188975f 3407 "Place point within the horizontal visibility of its window area."
007852e0
RS
3408 (if truncate-lines ; display truncating lines?
3409 ;; See if display needs to be scrolled.
3410 (let ((column (- (current-column) (max (window-hscroll) 1))))
3411 (if (and (< column 0) (> (window-hscroll) 0))
3412 (scroll-right (max (- column) 10))
3413 (if (>= column (- (window-width) 2))
3414 (scroll-left (max (- column (window-width) -3) 10)))))))
3415
3416
3417;;; Interactive word completion.
5abf1556 3418;; Forces "previous-word" processing. Do we want to make this selectable?
007852e0
RS
3419
3420;;;###autoload
3421(defun ispell-complete-word (&optional interior-frag)
05f362a8 3422 "Try to complete the word before or under point (see `lookup-words').
b9431ae0
RS
3423If optional INTERIOR-FRAG is non-nil then the word may be a character
3424sequence inside of a word.
007852e0
RS
3425
3426Standard ispell choices are then available."
60c4db3a 3427 ;; FIXME: completion-at-point-function.
007852e0
RS
3428 (interactive "P")
3429 (let ((cursor-location (point))
b9431ae0 3430 (case-fold-search-val case-fold-search)
007852e0
RS
3431 (word (ispell-get-word nil "\\*")) ; force "previous-word" processing.
3432 start end possibilities replacement)
3433 (setq start (car (cdr word))
3434 end (car (cdr (cdr word)))
3435 word (car word)
3436 possibilities
3437 (or (string= word "") ; Will give you every word
f5136913
RS
3438 (lookup-words (concat (and interior-frag "*") word
3439 (if (or interior-frag (null ispell-look-p))
3440 "*"))
fd5539c6
AM
3441 (or ispell-complete-word-dict
3442 ispell-alternate-dictionary))))
007852e0
RS
3443 (cond ((eq possibilities t)
3444 (message "No word to complete"))
3445 ((null possibilities)
3446 (message "No match for \"%s\"" word))
3447 (t ; There is a modification...
b9431ae0
RS
3448 (setq case-fold-search nil) ; Try and respect case of word.
3449 (cond
3450 ((string-equal (upcase word) word)
4b03da72 3451 (setq possibilities (mapcar 'upcase possibilities)))
877d353a 3452 ((eq (upcase (aref word 0)) (aref word 0))
b9431ae0
RS
3453 (setq possibilities (mapcar (function
3454 (lambda (pos)
877d353a 3455 (if (eq (aref word 0) (aref pos 0))
b9431ae0
RS
3456 pos
3457 (capitalize pos))))
3458 possibilities))))
3459 (setq case-fold-search case-fold-search-val)
29aec366
RS
3460 (save-window-excursion
3461 (setq replacement
3462 (ispell-command-loop possibilities nil word start end)))
007852e0
RS
3463 (cond
3464 ((equal 0 replacement) ; BUFFER-LOCAL ADDITION
3465 (ispell-add-per-file-word-list word))
3466 (replacement ; REPLACEMENT WORD
3467 (delete-region start end)
3468 (setq word (if (atom replacement) replacement (car replacement))
3469 cursor-location (+ (- (length word) (- end start))
3470 cursor-location))
f621ccf5 3471 (insert word)
007852e0
RS
3472 (if (not (atom replacement)) ; recheck spelling of replacement.
3473 (progn
3474 (goto-char cursor-location)
3475 (ispell-word nil t)))))
3476 (if (get-buffer ispell-choices-buffer)
3477 (kill-buffer ispell-choices-buffer))))
3478 (ispell-pdict-save ispell-silently-savep)
3479 (goto-char cursor-location)))
3480
3481
3482;;;###autoload
3483(defun ispell-complete-word-interior-frag ()
4b03da72 3484 "Completes word matching character sequence inside a word."
007852e0
RS
3485 (interactive)
3486 (ispell-complete-word t))
3487
e29702dd 3488
754bd273
GM
3489;;;###autoload
3490(defun ispell ()
3491 "Interactively check a region or buffer for spelling errors.
fea6948a 3492If `transient-mark-mode' is on, and a region is active, spell-check
3dfd2532
GM
3493that region. Otherwise spell-check the buffer.
3494
3495Ispell dictionaries are not distributed with Emacs. If you are
3496looking for a dictionary, please see the distribution of the GNU ispell
3497program, or do an Internet search; there are various dictionaries
3498available on the net."
754bd273 3499 (interactive)
4d170362
SM
3500 (if (and (boundp 'transient-mark-mode) transient-mark-mode
3501 (boundp 'mark-active) mark-active)
754bd273
GM
3502 (ispell-region (region-beginning) (region-end))
3503 (ispell-buffer)))
3504
3505
0349bab6
RS
3506;;; **********************************************************************
3507;;; Ispell Minor Mode
3508;;; **********************************************************************
3509
0349bab6
RS
3510(defvar ispell-minor-keymap
3511 (let ((map (make-sparse-keymap)))
3512 (define-key map " " 'ispell-minor-check)
3513 (define-key map "\r" 'ispell-minor-check)
3514 map)
3515 "Keymap used for Ispell minor mode.")
3516
0349bab6 3517;;;###autoload
94eb3112 3518(define-minor-mode ispell-minor-mode
ac6c8639
CY
3519 "Toggle last-word spell checking (Ispell minor mode).
3520With a prefix argument ARG, enable Ispell minor mode if ARG is
3521positive, and disable it otherwise. If called from Lisp, enable
3522the mode if ARG is omitted or nil.
fea6948a 3523
d9a8eb66 3524Ispell minor mode is a buffer-local minor mode. When enabled,
ac6c8639
CY
3525typing SPC or RET warns you if the previous word is incorrectly
3526spelled.
f5136913 3527
ac6c8639
CY
3528All the buffer-local variables and dictionaries are ignored. To
3529read them into the running ispell process, type \\[ispell-word]
3530SPC.
3531
3532For spell-checking \"on the fly\", not just after typing SPC or
3533RET, use `flyspell-mode'."
94eb3112 3534 nil " Spell" ispell-minor-keymap)
fea6948a 3535
0349bab6 3536(defun ispell-minor-check ()
d9a8eb66 3537 "Check previous word, then continue with the normal binding of this key.
f5136913
RS
3538Don't check previous word when character before point is a space or newline.
3539Don't read buffer-local settings or word lists."
0349bab6
RS
3540 (interactive "*")
3541 (let ((ispell-minor-mode nil)
f5136913
RS
3542 (ispell-check-only t)
3543 (last-char (char-after (1- (point)))))
6fdf40f3
KH
3544 (command-execute (key-binding (this-command-keys)))
3545 (if (not (or (eq last-char ?\ ) (eq last-char ?\n)
fea6948a
GM
3546 (and ispell-skip-html (eq last-char ?>))
3547 (and ispell-skip-html (eq last-char ?\;))))
6fdf40f3 3548 (ispell-word nil t))))
007852e0 3549
e29702dd 3550
007852e0
RS
3551;;; **********************************************************************
3552;;; Ispell Message
3553;;; **********************************************************************
7492978d
RS
3554
3555(defvar ispell-message-text-end
3556 (mapconcat (function identity)
3557 '(
f5136913
RS
3558 ;; Don't spell check signatures
3559 "^-- $"
7877f373 3560 ;; Matches PostScript files.
4d170362 3561 ;;"^%!PS-Adobe-[123].0"
7492978d 3562 ;; Matches uuencoded text
4d170362 3563 ;;"^begin [0-9][0-9][0-9] .*\nM.*\nM.*\nM"
6fdf40f3 3564 ;; Matches shell files (especially auto-decoding)
e29702dd 3565 "^#! /bin/[ck]?sh"
8581f180 3566 ;; Matches context difference listing
f5136913
RS
3567 "\\(\\(^cd .*\n\\)?diff -c .*\\)?\n\\*\\*\\* .*\n--- .*\n\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*"
3568 ;; Matches unidiff difference listing
02ba1267 3569 "\\(diff -u .*\\)?\n--- .*\n\\+\\+\\+ .*\n@@ [-+][0-9]+,[0-9]+ [-+][0-9]+,[0-9]+ @@"
f5136913
RS
3570 ;; Matches reporter.el bug report
3571 "^current state:\n==============\n"
3572 ;; Matches commonly used "cut" boundaries
3573 "^\\(- \\)?[-=_]+\\s ?\\(cut here\\|Environment Follows\\)")
7492978d 3574 "\\|")
fb7ada5f 3575 "Text beyond which `ispell-message' will not spell-check.
d9a8eb66 3576If it is a string, limit is the first occurrence of that regular expression.
7492978d 3577Otherwise, it must be a function which is called to get the limit.")
41d207e1 3578(put 'ispell-message-text-end 'risky-local-variable t)
007852e0 3579
4b03da72 3580
4d170362
SM
3581(defun ispell-mime-multipartp (&optional limit)
3582 "Return multipart message start boundary or nil if none."
3583 ;; caller must ensure `case-fold-search' is set to `t'
3584 (and
3585 (re-search-forward
3586 "Content-Type: *multipart/\\([^ \t\n]*;[ \t]*[\n]?[ \t]*\\)+boundary="
3587 limit t)
3588 (let (boundary)
3589 (if (looking-at "\"")
3590 (let (start)
3591 (forward-char)
3592 (setq start (point))
3593 (while (not (looking-at "\""))
3594 (forward-char 1))
3595 (setq boundary (buffer-substring-no-properties start (point))))
3596 (let ((start (point)))
3597 (while (looking-at "[-0-9a-zA-Z'()+_,./:=?]")
3598 (forward-char))
3599 (setq boundary (buffer-substring-no-properties start (point)))))
3600 (if (< (length boundary) 1)
3601 (setq boundary nil)
3602 (concat "--" boundary)))))
3603
3604
3605(defun ispell-mime-skip-part (boundary)
2188975f 3606 "Move point across header, or entire MIME part if message is encoded.
4d170362
SM
3607All specified types except `7bit' `8bit' and `quoted-printable' are considered
3608encoded and therefore skipped. See rfc 1521, 2183, ...
3609If no boundary is given, then entire message is skipped.
3610
3611This starts one line ABOVE the MIME content messages, on the boundary marker,
3612for operation with the generic region-skipping code.
3613This places new MIME boundaries into variable `ispell-checking-message'."
3614 (forward-line) ; skip over boundary to headers
3615 (let ((save-case-fold-search case-fold-search)
3616 (continuep t)
3617 textp)
3618 (setq case-fold-search t
3619 ispell-skip-html nil)
3620 (while continuep
3621 (setq continuep nil)
3622 (if (looking-at "Content-Type: *text/")
3623 (progn
3624 (goto-char (match-end 0))
3625 (if (looking-at "html")
3626 (setq ispell-skip-html t))
3627 (setq textp t
3628 continuep t)
3629 (re-search-forward "\\(.*;[ \t]*[\n]\\)*.*$" nil t)
3630 (forward-line)))
3631 (if (looking-at "Content-Transfer-Encoding: *\\([^ \t\n]*\\)")
3632 (let ((match (buffer-substring (match-beginning 1) (match-end 1))))
3633 (setq textp (member (upcase match)
3634 ;; only spell check the following encodings:
3635 '("7BIT" "8BIT" "QUOTED-PRINTABLE" "BINARY"))
3636 continuep t)
3637 (goto-char (match-end 0))
3638 (re-search-forward "\\(.*;[ \t]*[\n]\\)*.*$" nil t)
3639 (forward-line)))
3640 ;; hierarchical boundary definition
3641 (if (looking-at "Content-Type: *multipart/")
3642 (let ((new-boundary (ispell-mime-multipartp)))
3643 (if (string-match new-boundary boundary)
3644 (setq continuep t)
3645 ;; first pass redefine skip function to include new boundary
3646 ;;(re-search-backward boundary nil t)
3647 (forward-line)
3648 (setq ispell-checking-message
3649 (cons
3650 (list new-boundary 'ispell-mime-skip-part new-boundary)
3651 (if (eq t ispell-checking-message) nil
3652 ispell-checking-message))
3653 textp t
3654 continuep t)))
3655 ;; Skip all MIME headers that don't affect spelling
3656 (if (looking-at "Content-[^ \t]*: *\\(.*;[ \t]*[\n]\\)*.*$")
3657 (progn
3658 (setq continuep t)
3659 (goto-char (match-end 0))
3660 (forward-line)))))
3661
3662 (setq case-fold-search save-case-fold-search)
3663 (if textp
3664 (point)
3665 ;; encoded message. Skip to boundary, or entire message.
3666 (if (not boundary)
3667 (goto-char (point-max))
3668 (re-search-forward boundary nil t)
3669 (beginning-of-line)
3670 (point)))))
3671
8ce8687e 3672
007852e0
RS
3673;;;###autoload
3674(defun ispell-message ()
3675 "Check the spelling of a mail message or news post.
3676Don't check spelling of message headers except the Subject field.
3677Don't check included messages.
3678
6ec7837b
RS
3679To abort spell checking of a message region and send the message anyway,
3680use the `x' command. (Any subsequent regions will be checked.)
d9a8eb66 3681The `X' command aborts sending the message so that you can edit the buffer.
007852e0
RS
3682
3683To spell-check whenever a message is sent, include the appropriate lines
3684in your .emacs file:
f5136913
RS
3685 (add-hook 'message-send-hook 'ispell-message) ;; GNUS 5
3686 (add-hook 'news-inews-hook 'ispell-message) ;; GNUS 4
007852e0
RS
3687 (add-hook 'mail-send-hook 'ispell-message)
3688 (add-hook 'mh-before-send-letter-hook 'ispell-message)
3689
e29702dd 3690You can bind this to the key C-c i in GNUS or mail by adding to
a9210a53 3691`news-reply-mode-hook' or `mail-mode-hook' the following lambda expression:
007852e0
RS
3692 (function (lambda () (local-set-key \"\\C-ci\" 'ispell-message)))"
3693 (interactive)
7492978d 3694 (save-excursion
4b03da72 3695 (goto-char (point-min))
4d170362
SM
3696 (let* (boundary mimep
3697 (ispell-skip-region-alist-save ispell-skip-region-alist)
f753f75d 3698 ;; Nil when message came from outside (eg calling Emacs as editor)
f5136913
RS
3699 ;; Non-nil marker of end of headers.
3700 (internal-messagep
3701 (re-search-forward
3702 (concat "^" (regexp-quote mail-header-separator) "$") nil t))
3703 (end-of-headers ; Start of body.
3704 (copy-marker
3705 (or internal-messagep
3706 (re-search-forward "^$" nil t)
3707 (point-min))))
3708 (limit (copy-marker ; End of region we will spell check.
4b03da72
RS
3709 (cond
3710 ((not ispell-message-text-end) (point-max))
3711 ((char-or-string-p ispell-message-text-end)
3712 (if (re-search-forward ispell-message-text-end nil t)
3713 (match-beginning 0)
3714 (point-max)))
3715 (t (min (point-max) (funcall ispell-message-text-end))))))
f5136913
RS
3716 (default-prefix ; Vanilla cite prefix (just used for cite-regexp)
3717 (if (and (boundp 'mail-yank-prefix) mail-yank-prefix)
3718 (ispell-non-empty-string mail-yank-prefix)
3719 " \\|\t"))
3720 (cite-regexp ;Prefix of quoted text
7492978d 3721 (cond
4d170362 3722 ((functionp 'sc-cite-regexp) ; sc 3.0
85c8c5b6 3723 (ispell-with-no-warnings
6e8a1786
AM
3724 (concat "\\(" (sc-cite-regexp) "\\)" "\\|"
3725 (ispell-non-empty-string sc-reference-tag-string))))
4d170362 3726 ((boundp 'sc-cite-regexp) ; sc 2.3
7492978d 3727 (concat "\\(" sc-cite-regexp "\\)" "\\|"
85c8c5b6 3728 (ispell-with-no-warnings
6063ae55 3729 (ispell-non-empty-string sc-reference-tag-string))))
f5136913
RS
3730 ((or (equal major-mode 'news-reply-mode) ;GNUS 4 & below
3731 (equal major-mode 'message-mode)) ;GNUS 5
7492978d 3732 (concat "In article <" "\\|"
b9431ae0 3733 "[^,;&+=\n]+ <[^,;&+=]+> writes:" "\\|"
85c8c5b6 3734 (ispell-with-no-warnings message-cite-prefix-regexp)
6063ae55 3735 "\\|"
f5136913 3736 default-prefix))
7492978d 3737 ((equal major-mode 'mh-letter-mode) ; mh mail message
b9431ae0 3738 (concat "[^,;&+=\n]+ writes:" "\\|"
85c8c5b6 3739 (ispell-with-no-warnings
6063ae55 3740 (ispell-non-empty-string mh-ins-buf-prefix))))
f5136913 3741 ((not internal-messagep) ; Assume nn sent us this message.
7492978d
RS
3742 (concat "In [a-zA-Z.]+ you write:" "\\|"
3743 "In <[^,;&+=]+> [^,;&+=]+ writes:" "\\|"
3744 " *> *"))
3745 ((boundp 'vm-included-text-prefix) ; VM mail message
b9431ae0 3746 (concat "[^,;&+=\n]+ writes:" "\\|"
7492978d 3747 (ispell-non-empty-string vm-included-text-prefix)))
f5136913
RS
3748 (t default-prefix)))
3749 (ispell-skip-region-alist
3750 (cons (list (concat "^\\(" cite-regexp "\\)")
3751 (function forward-line))
3752 ispell-skip-region-alist))
4b03da72 3753 (old-case-fold-search case-fold-search)
f5136913 3754 (dictionary-alist ispell-message-dictionary-alist)
4b03da72 3755 (ispell-checking-message t))
f5136913
RS
3756
3757 ;; Select dictionary for message
3758 (or (local-variable-p 'ispell-local-dictionary (current-buffer))
3759 (while dictionary-alist
3760 (goto-char (point-min))
3761 (if (re-search-forward (car (car dictionary-alist))
3762 end-of-headers t)
3763 (setq ispell-local-dictionary (cdr (car dictionary-alist))
3764 dictionary-alist nil)
3765 (setq dictionary-alist (cdr dictionary-alist)))))
3766
3767 (unwind-protect
3768 (progn
3769 ;; Spell check any original Subject:
3770 (goto-char (point-min))
4d170362
SM
3771 (setq case-fold-search t
3772 mimep (re-search-forward "MIME-Version:" end-of-headers t))
3773 (goto-char (point-min))
f5136913
RS
3774 (if (re-search-forward "^Subject: *" end-of-headers t)
3775 (progn
3776 (goto-char (match-end 0))
3777 (if (and (not (looking-at ".*Re\\>"))
3778 (not (looking-at "\\[")))
b9431ae0
RS
3779 (progn
3780 (setq case-fold-search old-case-fold-search)
f5136913
RS
3781 (ispell-region (point)
3782 (progn ;Tab-initiated continuation lns.
3783 (end-of-line)
3784 (while (looking-at "\n[ \t]")
3785 (end-of-line 2))
3786 (point)))))))
4d170362
SM
3787 (if mimep
3788 (progn
3789 (goto-char (point-min))
3790 (setq boundary (ispell-mime-multipartp end-of-headers))))
3791 ;; Adjust message limit to MIME message if necessary.
3792 (and boundary
3793 (re-search-forward (concat boundary "--") nil t)
3794 (re-search-backward boundary nil t)
3795 (< (point) (marker-position limit))
3796 (set-marker limit (point)))
3797 (goto-char (point-min))
3798 ;; Select type or skip checking if this is a non-multipart message
3799 ;; Point moved to end of buffer if region is encoded.
94eb3112 3800 (when (and mimep (not boundary))
4d170362
SM
3801 (goto-char (point-min))
3802 (re-search-forward "Content-[^ \t]*:" end-of-headers t)
3803 (forward-line -1) ; following fn starts one line above
3804 (ispell-mime-skip-part nil)
3805 ;; if message-text-end region, limit may be less than point.
3806 (if (> (point) limit)
94eb3112 3807 (set-marker limit (point))))
4d170362 3808 (goto-char (max end-of-headers (point)))
f5136913 3809 (forward-line 1)
4d170362
SM
3810 (setq case-fold-search old-case-fold-search)
3811 ;; Define MIME regions to skip.
3812 (if boundary
3813 (setq ispell-checking-message
3814 (list (list boundary 'ispell-mime-skip-part boundary))))
f5136913
RS
3815 (ispell-region (point) limit))
3816 (set-marker end-of-headers nil)
4d170362
SM
3817 (set-marker limit nil)
3818 (setq ispell-skip-region-alist ispell-skip-region-alist-save
3819 ispell-skip-html nil
3820 case-fold-search old-case-fold-search)))))
4b03da72 3821
007852e0
RS
3822
3823(defun ispell-non-empty-string (string)
3824 (if (or (not string) (string-equal string ""))
3825 "\\'\\`" ; An unmatchable string if string is null.
3826 (regexp-quote string)))
3827
3828
3829;;; **********************************************************************
3830;;; Buffer Local Functions
3831;;; **********************************************************************
3832
3833
3834(defun ispell-accept-buffer-local-defs ()
f5136913 3835 "Load all buffer-local information, restarting Ispell when necessary."
007852e0
RS
3836 (ispell-buffer-local-dict) ; May kill ispell-process.
3837 (ispell-buffer-local-words) ; Will initialize ispell-process.
3838 (ispell-buffer-local-parsing))
3839
3840
007852e0 3841(defun ispell-buffer-local-parsing ()
a9210a53 3842 "Place Ispell into parsing mode for this buffer.
4b03da72 3843Overrides the default parsing mode.
d9a8eb66 3844Includes LaTeX/Nroff modes and extended character mode."
007852e0 3845 ;; (ispell-init-process) must already be called.
8eba343c 3846 (ispell-send-string "!\n") ; Put process in terse mode.
007852e0 3847 ;; We assume all major modes with "tex-mode" in them should use latex parsing
6fdf40f3
KH
3848 ;; When exclusively checking comments, set to raw text mode (nroff).
3849 (if (and (not (eq 'exclusive ispell-check-comments))
3850 (or (and (eq ispell-parser 'use-mode-name)
3851 (string-match "[Tt][Ee][Xx]-mode"
3852 (symbol-name major-mode)))
3853 (eq ispell-parser 'tex)))
f5136913 3854 (progn
8eba343c 3855 (ispell-send-string "+\n") ; set ispell mode to tex
f5136913
RS
3856 (if (not (eq ispell-parser 'tex))
3857 (set (make-local-variable 'ispell-parser) 'tex)))
8eba343c 3858 (ispell-send-string "-\n")) ; set mode to normal (nroff)
f5136913 3859 ;; If needed, test for SGML & HTML modes and set a buffer local nil/t value.
fea6948a 3860 (if (and ispell-skip-html (not (eq ispell-skip-html t)))
4d170362
SM
3861 (setq ispell-skip-html
3862 (not (null (string-match "sgml\\|html\\|xml"
3863 (downcase (symbol-name major-mode)))))))
007852e0
RS
3864 ;; Set default extended character mode for given buffer, if any.
3865 (let ((extended-char-mode (ispell-get-extended-character-mode)))
3866 (if extended-char-mode
8eba343c 3867 (ispell-send-string (concat extended-char-mode "\n"))))
e29702dd 3868 ;; Set buffer-local parsing mode and extended character mode, if specified.
007852e0 3869 (save-excursion
f5136913
RS
3870 (goto-char (point-max))
3871 ;; Uses last occurrence of ispell-parsing-keyword
3872 (if (search-backward ispell-parsing-keyword nil t)
e180ab9f 3873 (let ((end (point-at-eol))
f5136913
RS
3874 string)
3875 (search-forward ispell-parsing-keyword)
3876 (while (re-search-forward " *\\([^ \"]+\\)" end t)
3877 ;; space separated definitions.
81771793 3878 (setq string (downcase (match-string-no-properties 1)))
6fdf40f3
KH
3879 (cond ((and (string-match "latex-mode" string)
3880 (not (eq 'exclusive ispell-check-comments)))
8eba343c 3881 (ispell-send-string "+\n~tex\n"))
f5136913 3882 ((string-match "nroff-mode" string)
8eba343c 3883 (ispell-send-string "-\n~nroff\n"))
f5136913 3884 ((string-match "~" string) ; Set extended character mode.
8eba343c 3885 (ispell-send-string (concat string "\n")))
f5136913
RS
3886 (t (message "Invalid Ispell Parsing argument!")
3887 (sit-for 2))))))))
007852e0
RS
3888
3889
94eb3112 3890;; Can kill the current ispell process
007852e0 3891
87a6b92c 3892(defun ispell-buffer-local-dict (&optional no-reload)
f5136913 3893 "Initializes local dictionary and local personal dictionary.
d9a8eb66 3894If optional NO-RELOAD is non-nil, do not reload any dictionary.
007852e0 3895When a dictionary is defined in the buffer (see variable
a9210a53 3896`ispell-dictionary-keyword'), it will override the local setting
007852e0
RS
3897from \\[ispell-change-dictionary].
3898Both should not be used to define a buffer-local dictionary."
3899 (save-excursion
3900 (goto-char (point-min))
3901 (let (end)
3902 ;; Override the local variable definition.
f5136913
RS
3903 ;; Uses last occurrence of ispell-dictionary-keyword.
3904 (goto-char (point-max))
a6e7a34b
RS
3905 (unless ispell-local-dictionary-overridden
3906 (if (search-backward ispell-dictionary-keyword nil t)
3907 (progn
3908 (search-forward ispell-dictionary-keyword)
e180ab9f 3909 (setq end (point-at-eol))
a6e7a34b
RS
3910 (if (re-search-forward " *\\([^ \"]+\\)" end t)
3911 (setq ispell-local-dictionary
81771793 3912 (match-string-no-properties 1))))))
f5136913
RS
3913 (goto-char (point-max))
3914 (if (search-backward ispell-pdict-keyword nil t)
3915 (progn
3916 (search-forward ispell-pdict-keyword)
e180ab9f 3917 (setq end (point-at-eol))
f5136913
RS
3918 (if (re-search-forward " *\\([^ \"]+\\)" end t)
3919 (setq ispell-local-pdict
81771793 3920 (match-string-no-properties 1)))))))
87a6b92c
EZ
3921 (unless no-reload
3922 ;; Reload if new dictionary (maybe the personal one) defined.
3923 (ispell-internal-change-dictionary)))
007852e0
RS
3924
3925
3926(defun ispell-buffer-local-words ()
2188975f 3927 "Load the buffer-local dictionary in the current buffer."
09b2eb5e
RS
3928 ;; If there's an existing ispell process that's wrong for this use,
3929 ;; kill it.
007852e0
RS
3930 (if (and ispell-buffer-local-name
3931 (not (equal ispell-buffer-local-name (buffer-name))))
09b2eb5e
RS
3932 (ispell-kill-ispell t))
3933 ;; Actually start a new ispell process, because we need
3934 ;; to send commands now to specify the local words to it.
007852e0 3935 (ispell-init-process)
da00640a
AM
3936 (dolist (session-localword ispell-buffer-session-localwords)
3937 (ispell-send-string (concat "@" session-localword "\n")))
3938 (or ispell-buffer-local-name
3939 (if ispell-buffer-session-localwords
3940 (setq ispell-buffer-local-name (buffer-name))))
007852e0
RS
3941 (save-excursion
3942 (goto-char (point-min))
3943 (while (search-forward ispell-words-keyword nil t)
3944 (or ispell-buffer-local-name
3945 (setq ispell-buffer-local-name (buffer-name)))
e180ab9f 3946 (let ((end (point-at-eol))
f5136913 3947 (ispell-casechars (ispell-get-casechars))
007852e0 3948 string)
e29702dd 3949 ;; buffer-local words separated by a space, and can contain
f5136913 3950 ;; any character other than a space. Not rigorous enough.
7195bb71 3951 (while (re-search-forward " *\\([^ ]+\\)" end t)
81771793 3952 (setq string (match-string-no-properties 1))
64ef5f39 3953 ;; This can fail when string contains a word with invalid chars.
f753f75d 3954 ;; Error handling needs to be added between ispell and Emacs.
f5136913
RS
3955 (if (and (< 1 (length string))
3956 (equal 0 (string-match ispell-casechars string)))
8eba343c 3957 (ispell-send-string (concat "@" string "\n"))))))))
007852e0
RS
3958
3959
60c4db3a 3960;; Returns optionally adjusted region-end-point.
007852e0 3961
806c491a
GM
3962;; If comment-padright is defined, newcomment must be loaded.
3963(declare-function comment-add "newcomment" (arg))
3964
f5136913 3965(defun ispell-add-per-file-word-list (word)
b9431ae0 3966 "Add WORD to the per-file word list."
007852e0
RS
3967 (or ispell-buffer-local-name
3968 (setq ispell-buffer-local-name (buffer-name)))
007852e0
RS
3969 (save-excursion
3970 (goto-char (point-min))
94eb3112 3971 (let (line-okay search done found)
007852e0 3972 (while (not done)
94eb3112
SM
3973 (let ((case-fold-search nil))
3974 (setq search (search-forward ispell-words-keyword nil 'move)
fea6948a 3975 found (or found search)
007852e0
RS
3976 line-okay (< (+ (length word) 1 ; 1 for space after word..
3977 (progn (end-of-line) (current-column)))
94eb3112 3978 fill-column)))
007852e0
RS
3979 (if (or (and search line-okay)
3980 (null search))
3981 (progn
3982 (setq done t)
3983 (if (null search)
3984 (progn
3985 (open-line 1)
fea6948a 3986 (unless found (newline))
bf242939 3987 (insert (if comment-start
5192af46 3988 (concat
bf242939
AM
3989 (if (fboundp 'comment-padright)
3990 ;; Try and use the proper comment marker,
3991 ;; e.g. ";;" rather than ";".
3992 (comment-padright comment-start
3993 (comment-add nil))
3994 comment-start)
3995 " ")
3996 "")
3997 ispell-words-keyword)
3998 (if (and comment-end (> (length comment-end) 0))
007852e0 3999 (save-excursion
fea6948a 4000 (newline)
007852e0 4001 (insert comment-end)))))
f5136913 4002 (insert (concat " " word))))))))
007852e0 4003
f1ed9461 4004(add-to-list 'debug-ignored-errors "^No word found to check!$")
007852e0 4005
007852e0
RS
4006(provide 'ispell)
4007
4008\f
4009;;; LOCAL VARIABLES AND BUFFER-LOCAL VALUE EXAMPLES.
4010
f753f75d
SM
4011;; Local Variable options:
4012;; mode: name(-mode)
4013;; eval: expression
4014;; local-variable: value
007852e0 4015
f753f75d
SM
4016;; The following sets the buffer local dictionary to `american' English
4017;; and spell checks only comments.
007852e0 4018
f753f75d
SM
4019;; Local Variables:
4020;; mode: emacs-lisp
4021;; comment-column: 40
4022;; ispell-check-comments: exclusive
4023;; ispell-local-dictionary: "american"
4024;; End:
007852e0
RS
4025
4026
4027;;; MORE EXAMPLES OF ISPELL BUFFER-LOCAL VALUES
4028
f753f75d
SM
4029;; The following places this file in nroff parsing and extended char modes.
4030;; Local IspellParsing: nroff-mode ~nroff
4031;; Change IspellPersDict to IspellPersDict: to enable the following line.
4032;; Local IspellPersDict ~/.ispell_lisp
4033;; The following were automatically generated by ispell using the 'A' command:
6fdf40f3
KH
4034; LocalWords: settable alist inews mh frag pdict Wildcards iconify arg tex kss
4035; LocalWords: alists minibuffer bufferp autoload loaddefs aff Dansk KOI SPC op
4036; LocalWords: Francais Nederlands charset autoloaded popup nonmenu regexp num
4037; LocalWords: AMStex hspace includeonly nocite epsfig displaymath eqnarray reg
37269466 4038; LocalWords: minipage pers dict unhighlight buf grep sync prev inc
e4769531 4039; LocalWords: fn oldot NB AIX msg init read's bufs pt cmd Quinlan eg
c80e3b4a 4040; LocalWords: uuencoded unidiff sc nn VM SGML eval IspellPersDict
9050446c 4041; LocalWords: lns XEmacs HTML casechars Multibyte
f5136913
RS
4042
4043;;; ispell.el ends here