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