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