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