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