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