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