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