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