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