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