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