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