ispell.el: better `default-directory' and kill-ispell-on-exit handling
[bpt/emacs.git] / lisp / textmodes / ispell.el
1 ;;; ispell.el --- interface to International Ispell Versions 3.1 and 3.2
2
3 ;; Copyright (C) 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 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-readable-p "/usr/dict/web2") "/usr/dict/web2")
361 ((file-readable-p "/usr/share/dict/web2") "/usr/share/dict/web2")
362 ((file-readable-p "/usr/dict/words") "/usr/dict/words")
363 ((file-readable-p "/usr/lib/dict/words") "/usr/lib/dict/words")
364 ((file-readable-p "/usr/share/dict/words") "/usr/share/dict/words")
365 ((file-readable-p "/usr/share/lib/dict/words")
366 "/usr/share/lib/dict/words")
367 ((file-readable-p "/sys/dict") "/sys/dict"))
368 "*Alternate plain word-list dictionary for spelling help."
369 :type '(choice file (const :tag "None" nil))
370 :group 'ispell)
371
372 (defcustom ispell-complete-word-dict nil
373 "*Plain word-list dictionary used for word completion if
374 different from `ispell-alternate-dictionary'."
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 ;; setup the *Choices* buffer with valid data.
2053 (concat "-- %b -- word: " new-word
2054 " -- word-list: "
2055 (or ispell-complete-word-dict
2056 ispell-alternate-dictionary))
2057 miss (lookup-words new-word)
2058 choices miss
2059 line ispell-choices-win-default-height)
2060 (while (and choices ; adjust choices window.
2061 (< (if (> (+ 7 (current-column)
2062 (length (car choices))
2063 (if (> count ?~) 3 0))
2064 (window-width))
2065 (progn
2066 (insert "\n")
2067 (setq line (1+ line)))
2068 line)
2069 max-lines))
2070 (while (memq count command-characters)
2071 (setq count (ispell-int-char (1+ count))
2072 skipped (1+ skipped)))
2073 (insert "(" count ") " (car choices) " ")
2074 (setq choices (cdr choices)
2075 count (ispell-int-char (1+ count))))
2076 (setq count (ispell-int-char
2077 (- count ?0 skipped))))
2078 (ispell-show-choices line end)
2079 (select-window (next-window)))))
2080 (and (eq 'block ispell-highlight-p)
2081 (ispell-highlight-spelling-error start end nil
2082 'block))
2083 t) ; reselect from new choices
2084 ((= char ?u) ; insert lowercase into dictionary
2085 (ispell-send-string (concat "*" (downcase word) "\n"))
2086 (setq ispell-pdict-modified-p '(t)) ; dictionary modified!
2087 nil)
2088 ((= char ?m) ; type in what to insert
2089 (ispell-send-string
2090 (concat "*" (read-string "Insert: " word) "\n"))
2091 (setq ispell-pdict-modified-p '(t))
2092 (cons word nil))
2093 ((and (>= num 0) (< num count))
2094 (if ispell-query-replace-choices ; Query replace flag
2095 (list (nth num miss) 'query-replace)
2096 (nth num miss)))
2097 ((= char ?\C-l)
2098 (redraw-display) t)
2099 ((= char ?\C-r)
2100 ;; This may have alignment errors if current line is edited
2101 (if (marker-position ispell-recursive-edit-marker)
2102 (progn
2103 (message "Only one recursive edit session supported")
2104 (beep)
2105 (sit-for 2))
2106 (set-marker ispell-recursive-edit-marker start)
2107 ;;(set-marker ispell-region-end reg-end)
2108 (and ispell-highlight-p ; unhighlight
2109 (ispell-highlight-spelling-error start end))
2110 (unwind-protect
2111 (progn
2112 (message
2113 "%s"
2114 (substitute-command-keys
2115 (concat "Exit recursive edit with"
2116 " \\[exit-recursive-edit]")))
2117 (save-window-excursion (save-excursion
2118 (recursive-edit))))
2119 ;; protected
2120 (goto-char ispell-recursive-edit-marker)
2121 (if (not (equal (marker-buffer
2122 ispell-recursive-edit-marker)
2123 (current-buffer)))
2124 (progn
2125 (set-marker ispell-recursive-edit-marker nil)
2126 (error
2127 "Cannot continue ispell from this buffer.")))
2128 (set-marker ispell-recursive-edit-marker nil)))
2129 (list word nil)) ; recheck starting at this word.
2130 ((= char ?\C-z)
2131 (funcall (key-binding "\C-z"))
2132 t)
2133 (t (ding) t))))))
2134 result)
2135 ;; protected
2136 (and ispell-highlight-p ; unhighlight
2137 (save-window-excursion
2138 (select-window textwin)
2139 (ispell-highlight-spelling-error start end)))
2140 (if dedicated
2141 (set-window-dedicated-p dedicated-win t)))))
2142
2143
2144
2145 (defun ispell-show-choices (line end)
2146 "Show the choices in another buffer or frame."
2147 (if (and ispell-use-framepop-p (fboundp 'framepop-display-buffer))
2148 (progn
2149 (framepop-display-buffer (get-buffer ispell-choices-buffer))
2150 ;;; (get-buffer-window ispell-choices-buffer t)
2151 (select-window (previous-window))) ; *Choices* window
2152 ;; standard selection by splitting a small buffer out of this window.
2153 (let ((choices-window (get-buffer-window ispell-choices-buffer)))
2154 (if choices-window
2155 (if (= line (ispell-adjusted-window-height choices-window))
2156 (select-window choices-window)
2157 ;; *Choices* window changed size. Adjust the choices window
2158 ;; without scrolling the spelled window when possible
2159 (let ((window-line
2160 (- line (ispell-adjusted-window-height choices-window)))
2161 (visible (progn (vertical-motion -1) (point))))
2162 (if (< line ispell-choices-win-default-height)
2163 (setq window-line (+ window-line
2164 (- ispell-choices-win-default-height
2165 line))))
2166 (move-to-window-line 0)
2167 (vertical-motion window-line)
2168 (set-window-start (selected-window)
2169 (if (> (point) visible) visible (point)))
2170 (goto-char end)
2171 (select-window choices-window)
2172 (enlarge-window window-line)))
2173 ;; Overlay *Choices* window when it isn't showing
2174 (ispell-overlay-window (max line ispell-choices-win-default-height)))
2175 (switch-to-buffer ispell-choices-buffer)
2176 (goto-char (point-min)))))
2177
2178
2179 ;;;###autoload
2180 (defun ispell-help ()
2181 "Display a list of the options available when a misspelling is encountered.
2182
2183 Selections are:
2184
2185 DIGIT: Replace the word with a digit offered in the *Choices* buffer.
2186 SPC: Accept word this time.
2187 `i': Accept word and insert into private dictionary.
2188 `a': Accept word for this session.
2189 `A': Accept word and place in `buffer-local dictionary'.
2190 `r': Replace word with typed-in value. Rechecked.
2191 `R': Replace word with typed-in value. Query-replaced in buffer. Rechecked.
2192 `?': Show these commands.
2193 `x': Exit spelling buffer. Move cursor to original point.
2194 `X': Exit spelling buffer. Leaves cursor at the current point, and permits
2195 the aborted check to be completed later.
2196 `q': Quit spelling session (Kills ispell process).
2197 `l': Look up typed-in replacement in alternate dictionary. Wildcards okay.
2198 `u': Like `i', but the word is lower-cased first.
2199 `m': Place typed-in value in personal dictionary, then recheck current word.
2200 `C-l': Redraw screen.
2201 `C-r': Recursive edit.
2202 `C-z': Suspend Emacs or iconify frame."
2203
2204 (if (equal ispell-help-in-bufferp 'electric)
2205 (progn
2206 (require 'ehelp)
2207 (with-electric-help
2208 (function (lambda ()
2209 ;;This shouldn't be necessary: with-electric-help needs
2210 ;; an optional argument telling it about the smallest
2211 ;; acceptable window-height of the help buffer.
2212 ;;(if (< (window-height) 15)
2213 ;; (enlarge-window
2214 ;; (- 15 (ispell-adjusted-window-height))))
2215 (princ "Selections are:
2216
2217 DIGIT: Replace the word with a digit offered in the *Choices* buffer.
2218 SPC: Accept word this time.
2219 `i': Accept word and insert into private dictionary.
2220 `a': Accept word for this session.
2221 `A': Accept word and place in `buffer-local dictionary'.
2222 `r': Replace word with typed-in value. Rechecked.
2223 `R': Replace word with typed-in value. Query-replaced in buffer. Rechecked.
2224 `?': Show these commands.
2225 `x': Exit spelling buffer. Move cursor to original point.
2226 `X': Exit spelling buffer. Leaves cursor at the current point, and permits
2227 the aborted check to be completed later.
2228 `q': Quit spelling session (Kills ispell process).
2229 `l': Look up typed-in replacement in alternate dictionary. Wildcards okay.
2230 `u': Like `i', but the word is lower-cased first.
2231 `m': Place typed-in value in personal dictionary, then recheck current word.
2232 `C-l': Redraw screen.
2233 `C-r': Recursive edit.
2234 `C-z': Suspend Emacs or iconify frame.")
2235 nil))))
2236
2237
2238 (let ((help-1 (concat "[r/R]eplace word; [a/A]ccept for this session; "
2239 "[i]nsert into private dictionary"))
2240 (help-2 (concat "[l]ook a word up in alternate dictionary; "
2241 "e[x/X]it; [q]uit session"))
2242 (help-3 (concat "[u]ncapitalized insert into dict. "
2243 "Type 'x C-h f ispell-help' for more help")))
2244 (save-window-excursion
2245 (if ispell-help-in-bufferp
2246 (progn
2247 (ispell-overlay-window 4)
2248 (switch-to-buffer (get-buffer-create "*Ispell Help*"))
2249 (insert (concat help-1 "\n" help-2 "\n" help-3))
2250 (sit-for 5)
2251 (kill-buffer "*Ispell Help*"))
2252 (unwind-protect
2253 (let ((resize-mini-windows 'grow-only))
2254 (select-window (minibuffer-window))
2255 (erase-buffer)
2256 (message nil)
2257 ;;(set-minibuffer-window (selected-window))
2258 (enlarge-window 2)
2259 (insert (concat help-1 "\n" help-2 "\n" help-3))
2260 (sit-for 5))
2261 (erase-buffer)))))))
2262
2263
2264 (defun lookup-words (word &optional lookup-dict)
2265 "Look up WORD in optional word-list dictionary LOOKUP-DICT.
2266 A `*' serves as a wild card. If no wild cards, `look' is used if it exists.
2267 Otherwise the variable `ispell-grep-command' contains the command used to
2268 search for the words (usually egrep).
2269
2270 Optional second argument contains the dictionary to use; the default is
2271 `ispell-alternate-dictionary', overriden by `ispell-complete-word-dict'
2272 if defined."
2273 ;; We don't use the filter for this function, rather the result is written
2274 ;; into a buffer. Hence there is no need to save the filter values.
2275 (if (null lookup-dict)
2276 (setq lookup-dict (or ispell-complete-word-dict
2277 ispell-alternate-dictionary)))
2278
2279 (if lookup-dict
2280 (unless (file-readable-p lookup-dict)
2281 (error "lookup-words error: Unreadable or missing plain word-list %s."
2282 lookup-dict))
2283 (error (concat "lookup-words error: No plain word-list found at system default "
2284 "locations. Customize `ispell-alternate-dictionary' to set yours.")))
2285
2286 (let* ((process-connection-type ispell-use-ptys-p)
2287 (wild-p (string-match "\\*" word))
2288 (look-p (and ispell-look-p ; Only use look for an exact match.
2289 (or ispell-have-new-look (not wild-p))))
2290 (ispell-grep-buffer (get-buffer-create "*Ispell-Temp*")) ; result buf
2291 (prog (if look-p ispell-look-command ispell-grep-command))
2292 (args (if look-p ispell-look-options ispell-grep-options))
2293 status results loc)
2294 (unwind-protect
2295 (save-window-excursion
2296 (message "Starting \"%s\" process..." (file-name-nondirectory prog))
2297 (set-buffer ispell-grep-buffer)
2298 (if look-p
2299 nil
2300 ;; convert * to .*
2301 (insert "^" word "$")
2302 (while (search-backward "*" nil t) (insert "."))
2303 (setq word (buffer-string))
2304 (erase-buffer))
2305 (setq status (apply 'ispell-call-process prog nil t nil
2306 (nconc (if (and args (> (length args) 0))
2307 (list args)
2308 (if look-p nil
2309 (list "-e")))
2310 (list word)
2311 (if lookup-dict (list lookup-dict)))))
2312 ;; grep returns status 1 and no output when word not found, which
2313 ;; is a perfectly normal thing.
2314 (if (stringp status)
2315 (setq results (cons (format "error: %s exited with signal %s"
2316 (file-name-nondirectory prog) status)
2317 results))
2318 ;; else collect words into `results' in FIFO order
2319 (goto-char (point-max))
2320 ;; assure we've ended with \n
2321 (or (bobp) (= (preceding-char) ?\n) (insert ?\n))
2322 (while (not (bobp))
2323 (setq loc (point))
2324 (forward-line -1)
2325 (setq results (cons (buffer-substring-no-properties (point)
2326 (1- loc))
2327 results)))))
2328 ;; protected
2329 (kill-buffer ispell-grep-buffer)
2330 (if (and results (string-match ".+: " (car results)))
2331 (error "%s error: %s" ispell-grep-command (car results))))
2332 results))
2333
2334
2335 ;;; "ispell-filter" is a list of output lines from the generating function.
2336 ;;; Each full line (ending with \n) is a separate item on the list.
2337 ;;; "output" can contain multiple lines, part of a line, or both.
2338 ;;; "start" and "end" are used to keep bounds on lines when "output" contains
2339 ;;; multiple lines.
2340 ;;; "ispell-filter-continue" is true when we have received only part of a
2341 ;;; line as output from a generating function ("output" did not end with \n)
2342 ;;; THIS FUNCTION WILL FAIL IF THE PROCESS OUTPUT DOESN'T END WITH \n!
2343 ;;; This is the case when a process dies or fails. The default behavior
2344 ;;; in this case treats the next input received as fresh input.
2345
2346 (defun ispell-filter (process output)
2347 "Output filter function for ispell, grep, and look."
2348 (let ((start 0)
2349 (continue t)
2350 end)
2351 (while continue
2352 (setq end (string-match "\n" output start)) ; get text up to the newline.
2353 ;; If we get out of sync and ispell-filter-continue is asserted when we
2354 ;; are not continuing, treat the next item as a separate list. When
2355 ;; ispell-filter-continue is asserted, ispell-filter *should* always be a
2356 ;; list!
2357
2358 ;; Continue with same line (item)?
2359 (if (and ispell-filter-continue ispell-filter (listp ispell-filter))
2360 ;; Yes. Add it to the prev item
2361 (setcar ispell-filter
2362 (concat (car ispell-filter) (substring output start end)))
2363 ;; No. This is a new line and item.
2364 (setq ispell-filter
2365 (cons (substring output start end) ispell-filter)))
2366 (if (null end)
2367 ;; We've completed reading the output, but didn't finish the line.
2368 (setq ispell-filter-continue t continue nil)
2369 ;; skip over newline, this line complete.
2370 (setq ispell-filter-continue nil end (1+ end))
2371 (if (= end (length output)) ; No more lines in output
2372 (setq continue nil) ; so we can exit the filter.
2373 (setq start end)))))) ; else move start to next line of input
2374
2375
2376 ;;; This function destroys the mark location if it is in the word being
2377 ;;; highlighted.
2378 (defun ispell-highlight-spelling-error-generic (start end &optional highlight
2379 refresh)
2380 "Highlight the word from START to END with a kludge using `inverse-video'.
2381 When the optional third arg HIGHLIGHT is set, the word is highlighted;
2382 otherwise it is displayed normally.
2383 Uses block cursor to highlight one character.
2384 Optional REFRESH will unhighlighted then highlight, using block cursor
2385 highlighting when REFRESH is equal to `block'."
2386 (and (eq 'block ispell-highlight-p)
2387 (or (eq 'block refresh)
2388 (setq start (1+ start)))) ; On block non-refresh, inc start.
2389 (let ((modified (buffer-modified-p)) ; don't allow this fn to modify buffer
2390 (buffer-read-only nil) ; Allow highlighting read-only buffers.
2391 (text (buffer-substring-no-properties start end)) ; Save hilight region
2392 (inhibit-quit t) ; inhibit interrupt processing here.
2393 (buffer-undo-list t)) ; don't clutter the undo list.
2394 (goto-char end)
2395 (delete-region start end)
2396 (insert-char ? (- end start)) ; minimize amount of redisplay
2397 (sit-for 0) ; update display
2398 (if highlight (setq inverse-video (not inverse-video))) ; toggle video
2399 (delete-region start end) ; delete whitespace
2400 (insert text) ; insert text in inverse video.
2401 (sit-for 0) ; update display showing inverse video.
2402 (if (not highlight)
2403 (goto-char end)
2404 (setq inverse-video (not inverse-video)) ; toggle video
2405 (and (eq 'block ispell-highlight-p)
2406 (goto-char (1- start)))) ; use block cursor to "highlight" char
2407 (set-buffer-modified-p modified) ; don't modify if flag not set.
2408 (and refresh ; re-highlight
2409 (ispell-highlight-spelling-error-generic
2410 (if (eq 'block refresh) start (- start 2)) end t))))
2411
2412
2413 (defun ispell-highlight-spelling-error-xemacs (start end &optional highlight)
2414 "Highlight the word from START to END using `isearch-highlight'.
2415 When the optional third arg HIGHLIGHT is set, the word is highlighted,
2416 otherwise it is displayed normally."
2417 (if highlight
2418 (isearch-highlight start end)
2419 (isearch-dehighlight))
2420 ;;(sit-for 0)
2421 )
2422
2423
2424 (defun ispell-highlight-spelling-error-overlay (start end &optional highlight)
2425 "Highlight the word from START to END using overlays.
2426 When the optional third arg HIGHLIGHT is set, the word is highlighted
2427 otherwise it is displayed normally.
2428
2429 The variable `ispell-highlight-face' selects the face to use for highlighting."
2430 (if highlight
2431 (if ispell-overlay
2432 (move-overlay ispell-overlay start end (current-buffer))
2433 (setq ispell-overlay (make-overlay start end))
2434 (overlay-put ispell-overlay 'priority 1001) ;higher than lazy overlays
2435 (overlay-put ispell-overlay 'face ispell-highlight-face))
2436 (if ispell-overlay
2437 (delete-overlay ispell-overlay)))
2438 (if (and ispell-lazy-highlight (boundp 'lazy-highlight-cleanup))
2439 (if highlight
2440 (let ((isearch-string
2441 (concat
2442 "\\b"
2443 (regexp-quote (buffer-substring-no-properties start end))
2444 "\\b"))
2445 (isearch-regexp t)
2446 (isearch-case-fold-search nil))
2447 (isearch-lazy-highlight-new-loop
2448 (if (boundp 'reg-start) reg-start)
2449 (if (boundp 'reg-end) reg-end)))
2450 (lazy-highlight-cleanup lazy-highlight-cleanup)
2451 (setq isearch-lazy-highlight-last-string nil))))
2452
2453
2454 (defun ispell-highlight-spelling-error (start end &optional highlight refresh)
2455 (cond
2456 ((featurep 'xemacs)
2457 (ispell-highlight-spelling-error-xemacs start end highlight))
2458 ((and (featurep 'faces)
2459 (or (and (fboundp 'display-color-p) (display-color-p))
2460 window-system))
2461 (ispell-highlight-spelling-error-overlay start end highlight))
2462 (t (ispell-highlight-spelling-error-generic start end highlight refresh))))
2463
2464 (defun ispell-adjusted-window-height (&optional window)
2465 "Like `window-height', adjusted to correct for the effect of tall mode-lines.
2466 The value returned is actually the nominal number of text-lines in the
2467 window plus 1. On a terminal, this is the same value returned by
2468 `window-height', but if the window has a mode-line is taller than a normal
2469 text line, the returned value may be smaller than that from
2470 `window-height'."
2471 (cond ((fboundp 'window-text-height)
2472 (1+ (window-text-height window)))
2473 ((or (and (fboundp 'display-graphic-p) (display-graphic-p))
2474 (and (featurep 'xemacs) window-system))
2475 (1- (window-height window)))
2476 (t
2477 (window-height window))))
2478
2479 (defun ispell-overlay-window (height)
2480 "Create a window covering the top HEIGHT lines of the current window.
2481 Ensure that the line above point is still visible but otherwise avoid
2482 scrolling the current window. Leave the new window selected."
2483 (save-excursion
2484 (let ((oldot (save-excursion (vertical-motion -1) (point)))
2485 (top (save-excursion (move-to-window-line height) (point))))
2486 ;; If line above old point (line starting at oldot) would be
2487 ;; hidden by new window, scroll it to just below new win
2488 ;; otherwise set top line of other win so it doesn't scroll.
2489 (if (< oldot top) (setq top oldot))
2490 ;; if frame is unsplitable, temporarily disable that...
2491 (if (cdr (assq 'unsplittable (frame-parameters (selected-frame))))
2492 (let ((frame (selected-frame)))
2493 (modify-frame-parameters frame '((unsplittable . nil)))
2494 (split-window nil height)
2495 (modify-frame-parameters frame '((unsplittable . t))))
2496 (split-window nil height))
2497 (let ((deficit (- height (ispell-adjusted-window-height))))
2498 (when (> deficit 0)
2499 ;; Number of lines the window is still too short. We ensure that
2500 ;; there are at least (1- HEIGHT) lines visible in the window.
2501 (enlarge-window deficit)
2502 (goto-char top)
2503 (vertical-motion deficit)
2504 (setq top (min (point) oldot))))
2505 (set-window-start (next-window) top))))
2506
2507
2508 ;;; Should we add a compound word match return value?
2509 (defun ispell-parse-output (output &optional accept-list shift)
2510 "Parse the OUTPUT string from Ispell process and return:
2511 1: t for an exact match.
2512 2: A string containing the root word matched via suffix removal.
2513 3: A list of possible correct spellings of the format:
2514 (\"ORIGINAL-WORD\" OFFSET MISS-LIST GUESS-LIST)
2515 ORIGINAL-WORD is a string of the possibly misspelled word.
2516 OFFSET is an integer giving the line offset of the word.
2517 MISS-LIST and GUESS-LIST are possibly null lists of guesses and misses.
2518 4: nil when an error has occurred.
2519
2520 Optional second arg ACCEPT-LIST is list of words already accepted.
2521 Optional third arg SHIFT is an offset to apply based on previous corrections."
2522 (cond
2523 ((string= output "") t) ; for startup with pipes...
2524 ((string= output "*") t) ; exact match
2525 ((string= output "-") t) ; compound word match
2526 ((eq (aref output 0) ?+) ; found because of root word
2527 (substring output 2)) ; return root word
2528 ((equal 0 (string-match "[\ra-zA-Z]" output))
2529 (ding) ; error message from ispell!
2530 (message "Ispell error: %s" output)
2531 (sit-for 5)
2532 nil)
2533 (t ; need to process &, ?, and #'s
2534 (let ((type (aref output 0)) ; &, ?, or #
2535 (original-word (substring output 2 (string-match " " output 2)))
2536 (cur-count 0) ; contains number of misses + guesses
2537 count miss-list guess-list offset)
2538 (setq output (substring output (match-end 0))) ; skip over misspelling
2539 (if (eq type ?#)
2540 (setq count 0) ; no misses for type #
2541 (setq count (string-to-number output) ; get number of misses.
2542 output (substring output (1+ (string-match " " output 1)))))
2543 (setq offset (string-to-number output))
2544 (if (eq type ?#) ; No miss or guess list.
2545 (setq output nil)
2546 (setq output (substring output (1+ (string-match " " output 1)))))
2547 (while output
2548 (let ((end (string-match ", \\|\\($\\)" output))) ; end of miss/guess.
2549 (setq cur-count (1+ cur-count))
2550 (if (> cur-count count)
2551 (setq guess-list (cons (substring output 0 end) guess-list))
2552 (setq miss-list (cons (substring output 0 end) miss-list)))
2553 (if (match-end 1) ; True only when at end of line.
2554 (setq output nil) ; no more misses or guesses
2555 (setq output (substring output (+ end 2))))))
2556 ;; return results. Accept word if it was already accepted.
2557 ;; adjust offset.
2558 (if (member original-word accept-list)
2559 t
2560 (list original-word
2561 (if (numberp shift) (+ shift offset) offset)
2562 (nreverse miss-list) (nreverse guess-list)))))))
2563
2564
2565 (defun ispell-process-status ()
2566 "Return the status of the Ispell process.
2567 When asynchronous processes are not supported, `run' is always returned."
2568 (if ispell-async-processp
2569 (process-status ispell-process)
2570 (and ispell-process 'run)))
2571
2572
2573 (defun ispell-start-process ()
2574 "Start the ispell process, with support for no asynchronous processes.
2575 Keeps argument list for future ispell invocations for no async support."
2576 (let ((default-directory default-directory)
2577 args)
2578 (unless (and (file-directory-p default-directory)
2579 (file-readable-p default-directory))
2580 ;; Defend against bad `default-directory'.
2581 (setq default-directory (expand-file-name "~/")))
2582 ;; Local dictionary becomes the global dictionary in use.
2583 (setq ispell-current-dictionary
2584 (or ispell-local-dictionary ispell-dictionary))
2585 (setq ispell-current-personal-dictionary
2586 (or ispell-local-pdict ispell-personal-dictionary))
2587 (setq args (ispell-get-ispell-args))
2588 (if (and ispell-current-dictionary ; use specified dictionary
2589 (not (member "-d" args))) ; only define if not overridden
2590 (setq args
2591 (append (list "-d" ispell-current-dictionary) args)))
2592 (if ispell-current-personal-dictionary ; use specified pers dict
2593 (setq args
2594 (append args
2595 (list "-p"
2596 (expand-file-name ispell-current-personal-dictionary)))))
2597
2598 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
2599 ;; for communication. ispell or older aspell/hunspell does not support this
2600 (if ispell-encoding8-command
2601 (setq args
2602 (append args
2603 (list
2604 (concat ispell-encoding8-command
2605 (symbol-name (ispell-get-coding-system)))))))
2606 (setq args (append args ispell-extra-args))
2607
2608 ;; Initially we don't know any buffer's local words.
2609 (setq ispell-buffer-local-name nil)
2610
2611 (if ispell-async-processp
2612 (let ((process-connection-type ispell-use-ptys-p))
2613 (apply 'start-process
2614 "ispell" nil ispell-program-name
2615 "-a" ; accept single input lines
2616 (if ispell-really-hunspell "" "-m") ; make root/affix combos not in dict
2617 args)) ; hunspell -m option means different
2618 (setq ispell-cmd-args args
2619 ispell-output-buffer (generate-new-buffer " *ispell-output*")
2620 ispell-session-buffer (generate-new-buffer " *ispell-session*"))
2621 (ispell-send-string "\032\n") ; so Ispell prints version and exits
2622 t)))
2623
2624
2625 (defun ispell-init-process ()
2626 "Check status of Ispell process and start if necessary."
2627 (let* (;; Basename of dictionary used by the spell-checker
2628 (dict-bname (or (car (cdr (member "-d" (ispell-get-ispell-args))))
2629 ispell-current-dictionary))
2630 ;; Use "~/" as default-directory unless using Ispell with per-dir
2631 ;; personal dictionaries and not in a minibuffer under XEmacs
2632 (default-directory
2633 (if (or ispell-really-aspell
2634 ispell-really-hunspell
2635 ;; Protect against bad default-directory
2636 (not (and (file-directory-p default-directory)
2637 (file-readable-p default-directory)))
2638 ;; Ispell and per-dir personal dicts available
2639 (not (or (file-readable-p (concat default-directory
2640 ".ispell_words"))
2641 (file-readable-p (concat default-directory
2642 ".ispell_"
2643 (or dict-bname
2644 "default")))))
2645 ;; Ispell, in a minibuffer, and XEmacs
2646 (and (window-minibuffer-p)
2647 (not (fboundp 'minibuffer-selected-window))))
2648 (expand-file-name "~/")
2649 (expand-file-name default-directory))))
2650 ;; Check if process needs restart
2651 (if (and ispell-process
2652 (eq (ispell-process-status) 'run)
2653 ;; Unless we are using an explicit personal dictionary,
2654 ;; ensure we're in the same default directory!
2655 ;; Restart check for personal dictionary is done in
2656 ;; `ispell-internal-change-dictionary', called from `ispell-buffer-local-dict'
2657 (or (or ispell-local-pdict ispell-personal-dictionary)
2658 (equal ispell-process-directory default-directory)))
2659 (setq ispell-filter nil ispell-filter-continue nil)
2660 ;; may need to restart to select new personal dictionary.
2661 (ispell-kill-ispell t)
2662 (message "Starting new Ispell process [%s] ..."
2663 (or ispell-local-dictionary ispell-dictionary "default"))
2664 (sit-for 0)
2665 (setq ispell-library-directory (ispell-check-version)
2666 ispell-process (ispell-start-process)
2667 ispell-filter nil
2668 ispell-filter-continue nil
2669 ispell-process-directory default-directory)
2670 ;; When spellchecking minibuffer contents, assign ispell process to parent
2671 ;; buffer if known (not known for XEmacs). Use (buffer-name) otherwise.
2672 (setq ispell-process-buffer-name
2673 (if (and (window-minibuffer-p)
2674 (fboundp 'minibuffer-selected-window)) ;; Not XEmacs
2675 (window-buffer (minibuffer-selected-window))
2676 (buffer-name)))
2677
2678 (if ispell-async-processp
2679 (set-process-filter ispell-process 'ispell-filter))
2680 ;; protect against bogus binding of `enable-multibyte-characters' in XEmacs
2681 (if (and (or (featurep 'xemacs)
2682 (and (boundp 'enable-multibyte-characters)
2683 enable-multibyte-characters))
2684 (fboundp 'set-process-coding-system))
2685 (set-process-coding-system ispell-process (ispell-get-coding-system)
2686 (ispell-get-coding-system)))
2687 ;; Get version ID line
2688 (ispell-accept-output 3)
2689 ;; get more output if filter empty?
2690 (if (null ispell-filter) (ispell-accept-output 3))
2691 (cond ((null ispell-filter)
2692 (error "%s did not output version line" ispell-program-name))
2693 ((and
2694 (stringp (car ispell-filter))
2695 (if (string-match "warning: " (car ispell-filter))
2696 (progn
2697 (ispell-accept-output 3) ; was warn msg.
2698 (stringp (car ispell-filter)))
2699 (null (cdr ispell-filter)))
2700 (string-match "^@(#) " (car ispell-filter)))
2701 ;; got the version line as expected (we already know it's the right
2702 ;; version, so don't bother checking again.)
2703 nil)
2704 (t
2705 ;; Otherwise, it must be an error message. Show the user.
2706 ;; But first wait to see if some more output is going to arrive.
2707 ;; Otherwise we get cool errors like "Can't open ".
2708 (sleep-for 1)
2709 (ispell-accept-output 3)
2710 (error "%s" (mapconcat 'identity ispell-filter "\n"))))
2711 (setq ispell-filter nil) ; Discard version ID line
2712 (let ((extended-char-mode (ispell-get-extended-character-mode)))
2713 (if extended-char-mode ; ~ extended character mode
2714 (ispell-send-string (concat extended-char-mode "\n"))))
2715 (if ispell-async-processp
2716 (set-process-query-on-exit-flag ispell-process nil)))))
2717
2718 ;;;###autoload
2719 (defun ispell-kill-ispell (&optional no-error)
2720 "Kill current Ispell process (so that you may start a fresh one).
2721 With NO-ERROR, just return non-nil if there was no Ispell running."
2722 (interactive)
2723 ;; This hook is typically used by flyspell to flush some variables used
2724 ;; to optimize the common cases.
2725 (run-hooks 'ispell-kill-ispell-hook)
2726 (if (not (and ispell-process
2727 (eq (ispell-process-status) 'run)))
2728 (or no-error
2729 (error "There is no ispell process running!"))
2730 (if ispell-async-processp
2731 (delete-process ispell-process)
2732 ;; synchronous processes
2733 (ispell-send-string "\n") ; make sure side effects occurred.
2734 (kill-buffer ispell-output-buffer)
2735 (kill-buffer ispell-session-buffer)
2736 (setq ispell-output-buffer nil
2737 ispell-session-buffer nil))
2738 (setq ispell-process-buffer-name nil)
2739 (setq ispell-process nil)
2740 (message "Ispell process killed")
2741 nil))
2742
2743 ;; Kill ispell process when killing its associated buffer if using Ispell
2744 ;; per-directory personal dictionaries.
2745 (add-hook 'kill-buffer-hook
2746 '(lambda ()
2747 (if (and (not (equal ispell-process-directory (expand-file-name "~/")))
2748 (equal ispell-process-buffer-name (buffer-name)))
2749 (ispell-kill-ispell t))))
2750
2751 ;;; ispell-change-dictionary is set in some people's hooks. Maybe this should
2752 ;;; call ispell-init-process rather than wait for a spell checking command?
2753
2754 ;;;###autoload
2755 (defun ispell-change-dictionary (dict &optional arg)
2756 "Change to dictionary DICT for Ispell.
2757 With a prefix arg, set it \"globally\", for all buffers.
2758 Without a prefix arg, set it \"locally\", just for this buffer.
2759
2760 By just answering RET you can find out what the current dictionary is."
2761 (interactive
2762 (list (completing-read
2763 "Use new dictionary (RET for current, SPC to complete): "
2764 (and (fboundp 'ispell-valid-dictionary-list)
2765 (mapcar 'list (ispell-valid-dictionary-list)))
2766 nil t)
2767 current-prefix-arg))
2768 (ispell-set-spellchecker-params) ; Initilize variables and dicts alists
2769 (unless arg (ispell-buffer-local-dict 'no-reload))
2770 (if (equal dict "default") (setq dict nil))
2771 ;; This relies on completing-read's bug of returning "" for no match
2772 (cond ((equal dict "")
2773 (ispell-internal-change-dictionary)
2774 (message "Using %s dictionary"
2775 (or (and (not arg) ispell-local-dictionary)
2776 ispell-dictionary "default")))
2777 ((equal dict (or (and (not arg) ispell-local-dictionary)
2778 ispell-dictionary "default"))
2779 ;; Specified dictionary is the default already. Could reload
2780 ;; the dictionaries if needed.
2781 (ispell-internal-change-dictionary)
2782 (and (interactive-p)
2783 (message "No change, using %s dictionary" dict)))
2784 (t ; reset dictionary!
2785 (if (or (assoc dict ispell-local-dictionary-alist)
2786 (assoc dict ispell-dictionary-alist))
2787 (if arg
2788 ;; set default dictionary
2789 (setq ispell-dictionary dict)
2790 ;; set local dictionary
2791 (setq ispell-local-dictionary dict)
2792 (setq ispell-local-dictionary-overridden t))
2793 (error "Undefined dictionary: %s" dict))
2794 (ispell-internal-change-dictionary)
2795 (message "%s Ispell dictionary set to %s"
2796 (if arg "Global" "Local")
2797 dict))))
2798
2799 (defun ispell-internal-change-dictionary ()
2800 "Update the dictionary and the personal dictionary used by Ispell.
2801 This may kill the Ispell process; if so,
2802 a new one will be started when needed."
2803 (let ((dict (or ispell-local-dictionary ispell-dictionary))
2804 (pdict (or ispell-local-pdict ispell-personal-dictionary)))
2805 (unless (and (equal ispell-current-dictionary dict)
2806 (equal ispell-current-personal-dictionary pdict))
2807 (ispell-kill-ispell t)
2808 (setq ispell-current-dictionary dict
2809 ispell-current-personal-dictionary pdict))))
2810
2811 ;;; Spelling of comments are checked when ispell-check-comments is non-nil.
2812
2813 ;;;###autoload
2814 (defun ispell-region (reg-start reg-end &optional recheckp shift)
2815 "Interactively check a region for spelling errors.
2816 Return nil if spell session is quit,
2817 otherwise returns shift offset amount for last line processed."
2818 (interactive "r") ; Don't flag errors on read-only bufs.
2819 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
2820 (if (not recheckp)
2821 (ispell-accept-buffer-local-defs)) ; set up dictionary, local words, etc.
2822 (let ((skip-region-start (make-marker))
2823 (rstart (make-marker)))
2824 (unwind-protect
2825 (save-excursion
2826 (message "Spell-checking %s using %s with %s dictionary..."
2827 (if (and (= reg-start (point-min)) (= reg-end (point-max)))
2828 (buffer-name) "region")
2829 (file-name-nondirectory ispell-program-name)
2830 (or ispell-current-dictionary "default"))
2831 ;; Returns cursor to original location.
2832 (save-window-excursion
2833 (goto-char reg-start)
2834 (let ((transient-mark-mode)
2835 (case-fold-search case-fold-search)
2836 (query-fcc t)
2837 in-comment key)
2838 (let (message-log-max)
2839 (message "searching for regions to skip"))
2840 (if (re-search-forward (ispell-begin-skip-region-regexp) reg-end t)
2841 (progn
2842 (setq key (buffer-substring-no-properties
2843 (match-beginning 0) (match-end 0)))
2844 (set-marker skip-region-start (- (point) (length key)))
2845 (goto-char reg-start)))
2846 (let (message-log-max)
2847 (message "Continuing spelling check using %s with %s dictionary..."
2848 (file-name-nondirectory ispell-program-name)
2849 (or ispell-current-dictionary "default")))
2850 (set-marker rstart reg-start)
2851 (set-marker ispell-region-end reg-end)
2852 (while (and (not ispell-quit)
2853 (< (point) ispell-region-end))
2854 ;; spell-check region with skipping
2855 (if (and (marker-position skip-region-start)
2856 (<= skip-region-start (point)))
2857 (progn
2858 ;; If region inside line comment, must keep comment start.
2859 (setq in-comment (point)
2860 in-comment
2861 (and comment-start
2862 (or (null comment-end) (string= "" comment-end))
2863 (save-excursion
2864 (beginning-of-line)
2865 (re-search-forward comment-start in-comment t))
2866 comment-start))
2867 ;; Can change skip-regexps (in ispell-message)
2868 (ispell-skip-region key) ; moves pt past region.
2869 (set-marker rstart (point))
2870 ;; check for saving large attachments...
2871 (setq query-fcc (and query-fcc
2872 (ispell-ignore-fcc skip-region-start
2873 rstart)))
2874 (if (and (< rstart ispell-region-end)
2875 (re-search-forward
2876 (ispell-begin-skip-region-regexp)
2877 ispell-region-end t))
2878 (progn
2879 (setq key (match-string-no-properties 0))
2880 (set-marker skip-region-start
2881 (- (point) (length key)))
2882 (goto-char rstart))
2883 (set-marker skip-region-start nil))))
2884 (setq reg-end (max (point)
2885 (if (marker-position skip-region-start)
2886 (min skip-region-start ispell-region-end)
2887 (marker-position ispell-region-end))))
2888 (let* ((start (point))
2889 (end (save-excursion (end-of-line) (min (point) reg-end)))
2890 (string (ispell-get-line start end in-comment)))
2891 (if in-comment ; account for comment chars added
2892 (setq start (- start (length in-comment))
2893 in-comment nil))
2894 (setq end (point)) ; "end" tracks region retrieved.
2895 (if string ; there is something to spell check!
2896 ;; (special start end)
2897 (setq shift (ispell-process-line string
2898 (and recheckp shift))))
2899 (goto-char end)))))
2900 (if ispell-quit
2901 nil
2902 (or shift 0)))
2903 ;; protected
2904 (if (and (not (and recheckp ispell-keep-choices-win))
2905 (get-buffer ispell-choices-buffer))
2906 (kill-buffer ispell-choices-buffer))
2907 (set-marker skip-region-start nil)
2908 (set-marker rstart nil)
2909 (if ispell-quit
2910 (progn
2911 ;; preserve or clear the region for ispell-continue.
2912 (if (not (numberp ispell-quit))
2913 (set-marker ispell-region-end nil)
2914 ;; Ispell-continue enabled - ispell-region-end is set.
2915 (goto-char ispell-quit))
2916 ;; Check for aborting
2917 (if (and ispell-checking-message (numberp ispell-quit))
2918 (progn
2919 (setq ispell-quit nil)
2920 (error "Message send aborted")))
2921 (if (not recheckp) (setq ispell-quit nil)))
2922 (if (not recheckp) (set-marker ispell-region-end nil))
2923 ;; Only save if successful exit.
2924 (ispell-pdict-save ispell-silently-savep)
2925 (message "Spell-checking %s using %s with %s dictionary...done"
2926 (if (and (= reg-start (point-min)) (= reg-end (point-max)))
2927 (buffer-name) "region")
2928 (file-name-nondirectory ispell-program-name)
2929 (or ispell-current-dictionary "default"))))))
2930
2931
2932 (defun ispell-begin-skip-region-regexp ()
2933 "Return a regexp of the search keys for region skipping.
2934 Includes `ispell-skip-region-alist' plus tex, tib, html, and comment keys.
2935 Must call after `ispell-buffer-local-parsing' due to dependence on mode."
2936 ;; start with regions generic to all buffers
2937 (let ((skip-regexp (ispell-begin-skip-region ispell-skip-region-alist)))
2938 ;; Comments
2939 (if (and (null ispell-check-comments) comment-start)
2940 (setq skip-regexp (concat (regexp-quote comment-start) "\\|"
2941 skip-regexp)))
2942 (if (and (eq 'exclusive ispell-check-comments) comment-start)
2943 ;; search from end of current comment to start of next comment.
2944 (setq skip-regexp (concat (if (string= "" comment-end) "^"
2945 (regexp-quote comment-end))
2946 "\\|" skip-regexp)))
2947 ;; tib
2948 (if ispell-skip-tib
2949 (setq skip-regexp (concat ispell-tib-ref-beginning "\\|" skip-regexp)))
2950 ;; html stuff
2951 (if ispell-skip-html
2952 (setq skip-regexp (concat
2953 (ispell-begin-skip-region ispell-html-skip-alists)
2954 "\\|"
2955 skip-regexp)))
2956 ;; tex
2957 (if (eq ispell-parser 'tex)
2958 (setq skip-regexp (concat (ispell-begin-tex-skip-regexp) "\\|"
2959 skip-regexp)))
2960 ;; messages
2961 (if (and ispell-checking-message
2962 (not (eq t ispell-checking-message)))
2963 (setq skip-regexp (concat
2964 (mapconcat (lambda (lst) (car lst))
2965 ispell-checking-message
2966 "\\|")
2967 "\\|"
2968 skip-regexp)))
2969
2970 ;; return new regexp
2971 skip-regexp))
2972
2973
2974 (defun ispell-begin-skip-region (skip-alist)
2975 "Regular expression for start of regions to skip generated from SKIP-ALIST.
2976 Each selection should be a key of SKIP-ALIST;
2977 otherwise, the current line is skipped."
2978 (mapconcat (lambda (lst) (if (stringp (car lst)) (car lst) (eval (car lst))))
2979 skip-alist
2980 "\\|"))
2981
2982
2983 (defun ispell-begin-tex-skip-regexp ()
2984 "Regular expression of tex commands to skip.
2985 Generated from `ispell-tex-skip-alists'."
2986 (concat
2987 ;; raw tex keys
2988 (mapconcat (function (lambda (lst) (car lst)))
2989 (car ispell-tex-skip-alists)
2990 "\\|")
2991 "\\|"
2992 ;; keys wrapped in begin{}
2993 (mapconcat (function (lambda (lst)
2994 (concat "\\\\begin[ \t\n]*{[ \t\n]*"
2995 (car lst)
2996 "[ \t\n]*}")))
2997 (car (cdr ispell-tex-skip-alists))
2998 "\\|")))
2999
3000
3001 (defun ispell-skip-region-list ()
3002 "Return a list describing key and body regions to skip for this buffer.
3003 Includes regions defined by `ispell-skip-region-alist', tex mode,
3004 `ispell-html-skip-alists', and `ispell-checking-message'.
3005 Manual checking must include comments and tib references.
3006 The list is of the form described by variable `ispell-skip-region-alist'.
3007 Must call after `ispell-buffer-local-parsing' due to dependence on mode."
3008 (let ((skip-alist ispell-skip-region-alist))
3009 ;; only additional explicit region definition is tex.
3010 (if (eq ispell-parser 'tex)
3011 (setq case-fold-search nil
3012 skip-alist (append (car ispell-tex-skip-alists)
3013 (car (cdr ispell-tex-skip-alists))
3014 skip-alist)))
3015 (if ispell-skip-html
3016 (setq skip-alist (append ispell-html-skip-alists skip-alist)))
3017 (if (and ispell-checking-message
3018 (not (eq t ispell-checking-message)))
3019 (setq skip-alist (append ispell-checking-message skip-alist)))
3020 skip-alist))
3021
3022
3023 (defun ispell-tex-arg-end (&optional arg)
3024 "Skip across ARG number of braces."
3025 (condition-case nil
3026 (progn
3027 (while (looking-at "[ \t\n]*\\[") (forward-sexp))
3028 (forward-sexp (or arg 1)))
3029 (error
3030 (message "error skipping s-expressions at point %d." (point))
3031 (beep)
3032 (sit-for 2))))
3033
3034
3035 (defun ispell-ignore-fcc (start end)
3036 "Delete the Fcc: message header when large attachments are included.
3037 Return value `nil' if file with large attachments are saved.
3038 This can be used to avoid multiple questions for multiple large attachments.
3039 Returns point to starting location afterwards."
3040 (let ((result t))
3041 (if (and ispell-checking-message ispell-message-fcc-skip)
3042 (if (< ispell-message-fcc-skip (- end start))
3043 (let (case-fold-search head-end)
3044 (goto-char (point-min))
3045 (setq head-end
3046 (or (re-search-forward
3047 (concat "^" (regexp-quote mail-header-separator) "$")
3048 nil t)
3049 (re-search-forward "^$" nil t)
3050 (point-min)))
3051 (goto-char (point-min))
3052 (if (re-search-forward "^Fcc:" head-end t)
3053 (if (y-or-n-p
3054 "Save copy of this message with large attachments? ")
3055 (setq result nil)
3056 (beginning-of-line)
3057 (kill-line 1)))
3058 (goto-char end))))
3059 result))
3060
3061
3062 (defun ispell-skip-region (key)
3063 "Skip across KEY and then to end of region.
3064 Key lookup determines region to skip.
3065 Point is placed at end of skipped region."
3066 ;; move over key to begin checking.
3067 (forward-char (length key))
3068 (let ((start (point))
3069 ;; Regenerate each call... This function can change region definition.
3070 (alist (ispell-skip-region-list))
3071 alist-key null-skip)
3072 (cond
3073 ;; what about quoted comment, or comment inside strings?
3074 ((and (null ispell-check-comments) comment-start
3075 (string= key comment-start))
3076 (if (string= "" comment-end)
3077 (forward-line)
3078 (search-forward comment-end ispell-region-end t)))
3079 ((and (eq 'exclusive ispell-check-comments) comment-start
3080 (string= key comment-end))
3081 (search-forward comment-start ispell-region-end :end))
3082 ((and ispell-skip-tib (string-match ispell-tib-ref-beginning key))
3083 (re-search-forward ispell-tib-ref-end ispell-region-end t))
3084 ;; markings from alist
3085 (t
3086 (while alist
3087 (setq alist-key (eval (car (car alist))))
3088 (if (string-match alist-key key)
3089 (progn
3090 (setq alist (cdr (car alist)))
3091 (cond
3092 ((null alist) (setq null-skip t)) ; done! Just skip key.
3093 ((not (consp alist))
3094 ;; Search past end of spell region to find this region end.
3095 (re-search-forward (eval alist) (point-max) t))
3096 ((and (= 1 (length alist))
3097 (stringp (car alist)))
3098 (re-search-forward (car alist) (point-max) t))
3099 (t
3100 (setq null-skip t) ; error handling in functions!
3101 (if (consp (cdr alist))
3102 (apply (car alist) (cdr alist))
3103 (funcall (car alist)))))
3104 (setq alist nil))
3105 (setq alist (cdr alist))))))
3106 (if (and (= start (point)) (null null-skip))
3107 (progn
3108 (message "Matching region end for `%s' point %d not found"
3109 key (point))
3110 (beep)
3111 (sit-for 2)))))
3112
3113
3114 ;;; Grab the next line of data.
3115 ;;; Returns a string with the line data
3116 (defun ispell-get-line (start end in-comment)
3117 (let ((ispell-casechars (ispell-get-casechars))
3118 string)
3119 (cond ; LOOK AT THIS LINE AND SKIP OR PROCESS
3120 ((eolp) ; END OF LINE, just go to next line.
3121 (forward-line))
3122 ;;((looking-at "[-#@*+!%~^]") ; SKIP SPECIAL ISPELL CHARACTERS
3123 ;; (forward-char 1)) ; not needed as quoted below.
3124 ((or (re-search-forward ispell-casechars end t) ; TEXT EXISTS
3125 (re-search-forward "[][()${}]" end t)) ; or MATH COMMANDS
3126 (setq string (concat "^" in-comment
3127 (buffer-substring-no-properties start end)
3128 "\n"))
3129 (goto-char end))
3130 (t (goto-char end))) ; EMPTY LINE, skip it.
3131 string))
3132
3133
3134 (defun ispell-looking-at (string)
3135 (let ((coding (ispell-get-coding-system))
3136 (len (length string)))
3137 (and (<= (+ (point) len) (point-max))
3138 (equal (encode-coding-string string coding)
3139 (encode-coding-string (buffer-substring-no-properties
3140 (point) (+ (point) len))
3141 coding)))))
3142
3143 ;;; Avoid error messages when compiling for these dynamic variables.
3144 (defvar start)
3145 (defvar end)
3146
3147 (defun ispell-process-line (string shift)
3148 "Send STRING, a line of text, to ispell and processes the result.
3149 This will modify the buffer for spelling errors.
3150 Requires variables START and END to be defined in its lexical scope.
3151 Returns the sum SHIFT due to changes in word replacements."
3152 ;;(declare special start end)
3153 (let (poss accept-list)
3154 (if (not (numberp shift))
3155 (setq shift 0))
3156 ;; send string to spell process and get input.
3157 (ispell-send-string string)
3158 (while (progn
3159 (ispell-accept-output)
3160 ;; Last item of output contains a blank line.
3161 (not (string= "" (car ispell-filter)))))
3162 ;; parse all inputs from the stream one word at a time.
3163 ;; Place in FIFO order and remove the blank item.
3164 (setq ispell-filter (nreverse (cdr ispell-filter)))
3165 (while (and (not ispell-quit) ispell-filter)
3166 ;; get next word, accounting for accepted words and start shifts
3167 (setq poss (ispell-parse-output (car ispell-filter)
3168 accept-list shift))
3169 (if (and poss (listp poss)) ; spelling error occurred.
3170 ;; Whenever we have misspellings, we can change
3171 ;; the buffer. Keep boundaries as markers.
3172 ;; Markers can move with highlighting! This destroys
3173 ;; end of region markers line-end and ispell-region-end
3174 (let ((word-start
3175 (copy-marker (+ start ispell-offset (car (cdr poss)))))
3176 (word-len (length (car poss)))
3177 (line-end (copy-marker end))
3178 (line-start (copy-marker start))
3179 recheck-region replace)
3180 (goto-char word-start)
3181 ;; Adjust the horizontal scroll & point
3182 (ispell-horiz-scroll)
3183 (goto-char (+ word-len word-start))
3184 (ispell-horiz-scroll)
3185 (goto-char word-start)
3186 (ispell-horiz-scroll)
3187
3188 ;; Alignment cannot be tracked and this error will occur when
3189 ;; `query-replace' makes multiple corrections on the starting line.
3190 (or (ispell-looking-at (car poss))
3191 ;; This occurs due to filter pipe problems
3192 (error (concat "Ispell misalignment: word "
3193 "`%s' point %d; probably incompatible versions")
3194 (car poss) (marker-position word-start)))
3195 ;; ispell-cmd-loop can go recursive & change buffer
3196 (if ispell-keep-choices-win
3197 (setq replace (ispell-command-loop
3198 (car (cdr (cdr poss)))
3199 (car (cdr (cdr (cdr poss))))
3200 (car poss) (marker-position word-start)
3201 (+ word-len (marker-position word-start))))
3202 (save-window-excursion
3203 (setq replace (ispell-command-loop
3204 (car (cdr (cdr poss)))
3205 (car (cdr (cdr (cdr poss))))
3206 (car poss) (marker-position word-start)
3207 (+ word-len (marker-position word-start))))))
3208
3209 (goto-char word-start)
3210 ;; Recheck when query replace edit changes misspelled word.
3211 ;; Error in tex mode when a potential math mode change exists.
3212 (if (and replace (listp replace) (= 2 (length replace)))
3213 (if (and (eq ispell-parser 'tex)
3214 (string-match "[\\\\][]()[]\\|\\\\begin\\|\\$"
3215 (regexp-quote string)))
3216 (error
3217 "Don't start query replace on a line with math characters"
3218 )
3219 (set-marker line-end (point))
3220 (setq ispell-filter nil
3221 recheck-region t)))
3222
3223 ;; insert correction if needed
3224 (cond
3225 ((or (null replace)
3226 (equal 0 replace)) ; ACCEPT/INSERT
3227 (if (equal 0 replace) ; BUFFER-LOCAL DICT ADD
3228 (ispell-add-per-file-word-list (car poss)))
3229 ;; do not recheck accepted word on this line
3230 (setq accept-list (cons (car poss) accept-list)))
3231 (t ; replacement word selected or entered
3232 (delete-region (point) (+ word-len (point)))
3233 (if (not (listp replace))
3234 (progn
3235 (ispell-insert-word replace) ; insert dictionary word
3236 (ispell-send-replacement (car poss) replace)
3237 (setq accept-list (cons replace accept-list)))
3238 (let ((replace-word (car replace)))
3239 ;; Recheck hand entered replacement word
3240 (insert replace-word)
3241 (ispell-send-replacement (car poss) replace-word)
3242 (if (car (cdr replace))
3243 (save-window-excursion
3244 (delete-other-windows) ; to correctly show help.
3245 ;; Assume case-replace &
3246 ;; case-fold-search correct?
3247 (query-replace (car poss) (car replace) t)))
3248 (goto-char word-start)
3249 ;; do not recheck if already accepted
3250 (if (member replace-word accept-list)
3251 (setq accept-list (cons replace-word accept-list)
3252 replace replace-word)
3253 (let ((region-end (copy-marker ispell-region-end)))
3254 (setq recheck-region ispell-filter
3255 ispell-filter nil ; save filter
3256 shift 0 ; already accounted
3257 shift (ispell-region
3258 word-start
3259 (+ word-start (length replace-word))
3260 t shift))
3261 (if (null shift) ; quitting check.
3262 (setq shift 0))
3263 (set-marker ispell-region-end region-end)
3264 (set-marker region-end nil)
3265 (setq ispell-filter recheck-region
3266 recheck-region nil
3267 replace replace-word)))))
3268
3269 (setq shift (+ shift (- (length replace) word-len)))
3270
3271 ;; Move line-start across word...
3272 ;; new shift function does this now...
3273 ;;(set-marker line-start (+ line-start
3274 ;; (- (length replace)
3275 ;; (length (car poss)))))
3276 ))
3277 (if (not ispell-quit)
3278 (let (message-log-max)
3279 (message "Continuing spelling check using %s with %s dictionary..."
3280 (file-name-nondirectory ispell-program-name)
3281 (or ispell-current-dictionary "default"))))
3282 (sit-for 0)
3283 (setq start (marker-position line-start)
3284 end (marker-position line-end))
3285 ;; Adjust markers when end of region lost from highlighting.
3286 (if (and (not recheck-region) (< end (+ word-start word-len)))
3287 (setq end (+ word-start word-len)))
3288 (if (= word-start ispell-region-end)
3289 (set-marker ispell-region-end (+ word-start word-len)))
3290 ;; going out of scope - unneeded
3291 (set-marker line-start nil)
3292 (set-marker word-start nil)
3293 (set-marker line-end nil)))
3294 ;; finished with misspelling!
3295 (setq ispell-filter (cdr ispell-filter)))
3296 shift))
3297
3298
3299 ;;;###autoload
3300 (defun ispell-comments-and-strings ()
3301 "Check comments and strings in the current buffer for spelling errors."
3302 (interactive)
3303 (goto-char (point-min))
3304 (let (state done)
3305 (while (not done)
3306 (setq done t)
3307 (setq state (parse-partial-sexp (point) (point-max)
3308 nil nil state 'syntax-table))
3309 (if (or (nth 3 state) (nth 4 state))
3310 (let ((start (point)))
3311 (setq state (parse-partial-sexp start (point-max)
3312 nil nil state 'syntax-table))
3313 (if (or (nth 3 state) (nth 4 state))
3314 (error "Unterminated string or comment"))
3315 (save-excursion
3316 (setq done (not (ispell-region start (point))))))))))
3317
3318
3319 ;;;###autoload
3320 (defun ispell-buffer ()
3321 "Check the current buffer for spelling errors interactively."
3322 (interactive)
3323 (ispell-region (point-min) (point-max)))
3324
3325
3326 ;;;###autoload
3327 (defun ispell-continue ()
3328 "Continue a halted spelling session beginning with the current word."
3329 (interactive)
3330 (if (not (marker-position ispell-region-end))
3331 (message "No session to continue. Use 'X' command when checking!")
3332 (if (not (equal (marker-buffer ispell-region-end) (current-buffer)))
3333 (message "Must continue ispell from buffer %s"
3334 (buffer-name (marker-buffer ispell-region-end)))
3335 (ispell-region
3336 ;; find beginning of current word:
3337 (car (cdr (ispell-get-word t)))
3338 (marker-position ispell-region-end)))))
3339
3340
3341 ;;; Horizontal scrolling
3342 (defun ispell-horiz-scroll ()
3343 "Place point within the horizontal visibility of its window area."
3344 (if truncate-lines ; display truncating lines?
3345 ;; See if display needs to be scrolled.
3346 (let ((column (- (current-column) (max (window-hscroll) 1))))
3347 (if (and (< column 0) (> (window-hscroll) 0))
3348 (scroll-right (max (- column) 10))
3349 (if (>= column (- (window-width) 2))
3350 (scroll-left (max (- column (window-width) -3) 10)))))))
3351
3352
3353 ;;; Interactive word completion.
3354 ;;; Forces "previous-word" processing. Do we want to make this selectable?
3355
3356 ;;;###autoload
3357 (defun ispell-complete-word (&optional interior-frag)
3358 "Try to complete the word before or under point (see `lookup-words').
3359 If optional INTERIOR-FRAG is non-nil then the word may be a character
3360 sequence inside of a word.
3361
3362 Standard ispell choices are then available."
3363 (interactive "P")
3364 (let ((cursor-location (point))
3365 (case-fold-search-val case-fold-search)
3366 (word (ispell-get-word nil "\\*")) ; force "previous-word" processing.
3367 start end possibilities replacement)
3368 (setq start (car (cdr word))
3369 end (car (cdr (cdr word)))
3370 word (car word)
3371 possibilities
3372 (or (string= word "") ; Will give you every word
3373 (lookup-words (concat (and interior-frag "*") word
3374 (if (or interior-frag (null ispell-look-p))
3375 "*"))
3376 (or ispell-complete-word-dict
3377 ispell-alternate-dictionary))))
3378 (cond ((eq possibilities t)
3379 (message "No word to complete"))
3380 ((null possibilities)
3381 (message "No match for \"%s\"" word))
3382 (t ; There is a modification...
3383 (setq case-fold-search nil) ; Try and respect case of word.
3384 (cond
3385 ((string-equal (upcase word) word)
3386 (setq possibilities (mapcar 'upcase possibilities)))
3387 ((eq (upcase (aref word 0)) (aref word 0))
3388 (setq possibilities (mapcar (function
3389 (lambda (pos)
3390 (if (eq (aref word 0) (aref pos 0))
3391 pos
3392 (capitalize pos))))
3393 possibilities))))
3394 (setq case-fold-search case-fold-search-val)
3395 (save-window-excursion
3396 (setq replacement
3397 (ispell-command-loop possibilities nil word start end)))
3398 (cond
3399 ((equal 0 replacement) ; BUFFER-LOCAL ADDITION
3400 (ispell-add-per-file-word-list word))
3401 (replacement ; REPLACEMENT WORD
3402 (delete-region start end)
3403 (setq word (if (atom replacement) replacement (car replacement))
3404 cursor-location (+ (- (length word) (- end start))
3405 cursor-location))
3406 (ispell-insert-word word)
3407 (if (not (atom replacement)) ; recheck spelling of replacement.
3408 (progn
3409 (goto-char cursor-location)
3410 (ispell-word nil t)))))
3411 (if (get-buffer ispell-choices-buffer)
3412 (kill-buffer ispell-choices-buffer))))
3413 (ispell-pdict-save ispell-silently-savep)
3414 (goto-char cursor-location)))
3415
3416
3417 ;;;###autoload
3418 (defun ispell-complete-word-interior-frag ()
3419 "Completes word matching character sequence inside a word."
3420 (interactive)
3421 (ispell-complete-word t))
3422
3423
3424 ;;;###autoload
3425 (defun ispell ()
3426 "Interactively check a region or buffer for spelling errors.
3427 If `transient-mark-mode' is on, and a region is active, spell-check
3428 that region. Otherwise spell-check the buffer.
3429
3430 Ispell dictionaries are not distributed with Emacs. If you are
3431 looking for a dictionary, please see the distribution of the GNU ispell
3432 program, or do an Internet search; there are various dictionaries
3433 available on the net."
3434 (interactive)
3435 (if (and (boundp 'transient-mark-mode) transient-mark-mode
3436 (boundp 'mark-active) mark-active)
3437 (ispell-region (region-beginning) (region-end))
3438 (ispell-buffer)))
3439
3440
3441 ;;; **********************************************************************
3442 ;;; Ispell Minor Mode
3443 ;;; **********************************************************************
3444
3445 (defvar ispell-minor-mode nil
3446 "Non-nil if Ispell minor mode is enabled.")
3447 ;; Variable indicating that ispell minor mode is active.
3448 (make-variable-buffer-local 'ispell-minor-mode)
3449
3450 (or (assq 'ispell-minor-mode minor-mode-alist)
3451 (setq minor-mode-alist
3452 (cons '(ispell-minor-mode " Spell") minor-mode-alist)))
3453
3454 (defvar ispell-minor-keymap
3455 (let ((map (make-sparse-keymap)))
3456 (define-key map " " 'ispell-minor-check)
3457 (define-key map "\r" 'ispell-minor-check)
3458 map)
3459 "Keymap used for Ispell minor mode.")
3460
3461 (or (not (boundp 'minor-mode-map-alist))
3462 (assoc 'ispell-minor-mode minor-mode-map-alist)
3463 (setq minor-mode-map-alist
3464 (cons (cons 'ispell-minor-mode ispell-minor-keymap)
3465 minor-mode-map-alist)))
3466
3467 ;;;###autoload
3468 (defun ispell-minor-mode (&optional arg)
3469 "Toggle Ispell minor mode.
3470 With prefix argument ARG, turn Ispell minor mode on if ARG is positive,
3471 otherwise turn it off.
3472
3473 In Ispell minor mode, pressing SPC or RET
3474 warns you if the previous word is incorrectly spelled.
3475
3476 All the buffer-local variables and dictionaries are ignored -- to read
3477 them into the running ispell process, type \\[ispell-word] SPC."
3478 (interactive "P")
3479 (setq ispell-minor-mode
3480 (not (or (and (null arg) ispell-minor-mode)
3481 (<= (prefix-numeric-value arg) 0))))
3482 (force-mode-line-update))
3483
3484 (defun ispell-minor-check ()
3485 "Check previous word then continue with the normal binding of this key.
3486 Don't check previous word when character before point is a space or newline.
3487 Don't read buffer-local settings or word lists."
3488 (interactive "*")
3489 (let ((ispell-minor-mode nil)
3490 (ispell-check-only t)
3491 (last-char (char-after (1- (point)))))
3492 (command-execute (key-binding (this-command-keys)))
3493 (if (not (or (eq last-char ?\ ) (eq last-char ?\n)
3494 (and ispell-skip-html (eq last-char ?>))
3495 (and ispell-skip-html (eq last-char ?\;))))
3496 (ispell-word nil t))))
3497
3498
3499 ;;; **********************************************************************
3500 ;;; Ispell Message
3501 ;;; **********************************************************************
3502
3503 (defvar ispell-message-text-end
3504 (mapconcat (function identity)
3505 '(
3506 ;; Don't spell check signatures
3507 "^-- $"
3508 ;; Matches postscript files.
3509 ;;"^%!PS-Adobe-[123].0"
3510 ;; Matches uuencoded text
3511 ;;"^begin [0-9][0-9][0-9] .*\nM.*\nM.*\nM"
3512 ;; Matches shell files (especially auto-decoding)
3513 "^#! /bin/[ck]?sh"
3514 ;; Matches context difference listing
3515 "\\(\\(^cd .*\n\\)?diff -c .*\\)?\n\\*\\*\\* .*\n--- .*\n\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*"
3516 ;; Matches unidiff difference listing
3517 "\\(diff -u .*\\)?\n--- .*\n\\+\\+\\+ .*\n@@ [-+][0-9]+,[0-9]+ [-+][0-9]+,[0-9]+ @@"
3518 ;; Matches reporter.el bug report
3519 "^current state:\n==============\n"
3520 ;; Matches commonly used "cut" boundaries
3521 "^\\(- \\)?[-=_]+\\s ?\\(cut here\\|Environment Follows\\)")
3522 "\\|")
3523 "*End of text which will be checked in `ispell-message'.
3524 If it is a string, limit at first occurrence of that regular expression.
3525 Otherwise, it must be a function which is called to get the limit.")
3526 (put 'ispell-message-text-end 'risky-local-variable t)
3527
3528
3529 (defun ispell-mime-multipartp (&optional limit)
3530 "Return multipart message start boundary or nil if none."
3531 ;; caller must ensure `case-fold-search' is set to `t'
3532 (and
3533 (re-search-forward
3534 "Content-Type: *multipart/\\([^ \t\n]*;[ \t]*[\n]?[ \t]*\\)+boundary="
3535 limit t)
3536 (let (boundary)
3537 (if (looking-at "\"")
3538 (let (start)
3539 (forward-char)
3540 (setq start (point))
3541 (while (not (looking-at "\""))
3542 (forward-char 1))
3543 (setq boundary (buffer-substring-no-properties start (point))))
3544 (let ((start (point)))
3545 (while (looking-at "[-0-9a-zA-Z'()+_,./:=?]")
3546 (forward-char))
3547 (setq boundary (buffer-substring-no-properties start (point)))))
3548 (if (< (length boundary) 1)
3549 (setq boundary nil)
3550 (concat "--" boundary)))))
3551
3552
3553 (defun ispell-mime-skip-part (boundary)
3554 "Move point across header, or entire MIME part if message is encoded.
3555 All specified types except `7bit' `8bit' and `quoted-printable' are considered
3556 encoded and therefore skipped. See rfc 1521, 2183, ...
3557 If no boundary is given, then entire message is skipped.
3558
3559 This starts one line ABOVE the MIME content messages, on the boundary marker,
3560 for operation with the generic region-skipping code.
3561 This places new MIME boundaries into variable `ispell-checking-message'."
3562 (forward-line) ; skip over boundary to headers
3563 (let ((save-case-fold-search case-fold-search)
3564 (continuep t)
3565 textp)
3566 (setq case-fold-search t
3567 ispell-skip-html nil)
3568 (while continuep
3569 (setq continuep nil)
3570 (if (looking-at "Content-Type: *text/")
3571 (progn
3572 (goto-char (match-end 0))
3573 (if (looking-at "html")
3574 (setq ispell-skip-html t))
3575 (setq textp t
3576 continuep t)
3577 (re-search-forward "\\(.*;[ \t]*[\n]\\)*.*$" nil t)
3578 (forward-line)))
3579 (if (looking-at "Content-Transfer-Encoding: *\\([^ \t\n]*\\)")
3580 (let ((match (buffer-substring (match-beginning 1) (match-end 1))))
3581 (setq textp (member (upcase match)
3582 ;; only spell check the following encodings:
3583 '("7BIT" "8BIT" "QUOTED-PRINTABLE" "BINARY"))
3584 continuep t)
3585 (goto-char (match-end 0))
3586 (re-search-forward "\\(.*;[ \t]*[\n]\\)*.*$" nil t)
3587 (forward-line)))
3588 ;; hierarchical boundary definition
3589 (if (looking-at "Content-Type: *multipart/")
3590 (let ((new-boundary (ispell-mime-multipartp)))
3591 (if (string-match new-boundary boundary)
3592 (setq continuep t)
3593 ;; first pass redefine skip function to include new boundary
3594 ;;(re-search-backward boundary nil t)
3595 (forward-line)
3596 (setq ispell-checking-message
3597 (cons
3598 (list new-boundary 'ispell-mime-skip-part new-boundary)
3599 (if (eq t ispell-checking-message) nil
3600 ispell-checking-message))
3601 textp t
3602 continuep t)))
3603 ;; Skip all MIME headers that don't affect spelling
3604 (if (looking-at "Content-[^ \t]*: *\\(.*;[ \t]*[\n]\\)*.*$")
3605 (progn
3606 (setq continuep t)
3607 (goto-char (match-end 0))
3608 (forward-line)))))
3609
3610 (setq case-fold-search save-case-fold-search)
3611 (if textp
3612 (point)
3613 ;; encoded message. Skip to boundary, or entire message.
3614 (if (not boundary)
3615 (goto-char (point-max))
3616 (re-search-forward boundary nil t)
3617 (beginning-of-line)
3618 (point)))))
3619
3620
3621 ;;;###autoload
3622 (defun ispell-message ()
3623 "Check the spelling of a mail message or news post.
3624 Don't check spelling of message headers except the Subject field.
3625 Don't check included messages.
3626
3627 To abort spell checking of a message region and send the message anyway,
3628 use the `x' command. (Any subsequent regions will be checked.)
3629 The `X' command aborts the message send so that you can edit the buffer.
3630
3631 To spell-check whenever a message is sent, include the appropriate lines
3632 in your .emacs file:
3633 (add-hook 'message-send-hook 'ispell-message) ;; GNUS 5
3634 (add-hook 'news-inews-hook 'ispell-message) ;; GNUS 4
3635 (add-hook 'mail-send-hook 'ispell-message)
3636 (add-hook 'mh-before-send-letter-hook 'ispell-message)
3637
3638 You can bind this to the key C-c i in GNUS or mail by adding to
3639 `news-reply-mode-hook' or `mail-mode-hook' the following lambda expression:
3640 (function (lambda () (local-set-key \"\\C-ci\" 'ispell-message)))"
3641 (interactive)
3642 (save-excursion
3643 (goto-char (point-min))
3644 (let* (boundary mimep
3645 (ispell-skip-region-alist-save ispell-skip-region-alist)
3646 ;; Nil when message came from outside (eg calling Emacs as editor)
3647 ;; Non-nil marker of end of headers.
3648 (internal-messagep
3649 (re-search-forward
3650 (concat "^" (regexp-quote mail-header-separator) "$") nil t))
3651 (end-of-headers ; Start of body.
3652 (copy-marker
3653 (or internal-messagep
3654 (re-search-forward "^$" nil t)
3655 (point-min))))
3656 (limit (copy-marker ; End of region we will spell check.
3657 (cond
3658 ((not ispell-message-text-end) (point-max))
3659 ((char-or-string-p ispell-message-text-end)
3660 (if (re-search-forward ispell-message-text-end nil t)
3661 (match-beginning 0)
3662 (point-max)))
3663 (t (min (point-max) (funcall ispell-message-text-end))))))
3664 (default-prefix ; Vanilla cite prefix (just used for cite-regexp)
3665 (if (and (boundp 'mail-yank-prefix) mail-yank-prefix)
3666 (ispell-non-empty-string mail-yank-prefix)
3667 " \\|\t"))
3668 (cite-regexp ;Prefix of quoted text
3669 (cond
3670 ((functionp 'sc-cite-regexp) ; sc 3.0
3671 (with-no-warnings
3672 (concat "\\(" (sc-cite-regexp) "\\)" "\\|"
3673 (ispell-non-empty-string sc-reference-tag-string))))
3674 ((boundp 'sc-cite-regexp) ; sc 2.3
3675 (concat "\\(" sc-cite-regexp "\\)" "\\|"
3676 (with-no-warnings
3677 (ispell-non-empty-string sc-reference-tag-string))))
3678 ((or (equal major-mode 'news-reply-mode) ;GNUS 4 & below
3679 (equal major-mode 'message-mode)) ;GNUS 5
3680 (concat "In article <" "\\|"
3681 "[^,;&+=\n]+ <[^,;&+=]+> writes:" "\\|"
3682 (with-no-warnings message-cite-prefix-regexp)
3683 "\\|"
3684 default-prefix))
3685 ((equal major-mode 'mh-letter-mode) ; mh mail message
3686 (concat "[^,;&+=\n]+ writes:" "\\|"
3687 (with-no-warnings
3688 (ispell-non-empty-string mh-ins-buf-prefix))))
3689 ((not internal-messagep) ; Assume nn sent us this message.
3690 (concat "In [a-zA-Z.]+ you write:" "\\|"
3691 "In <[^,;&+=]+> [^,;&+=]+ writes:" "\\|"
3692 " *> *"))
3693 ((boundp 'vm-included-text-prefix) ; VM mail message
3694 (concat "[^,;&+=\n]+ writes:" "\\|"
3695 (ispell-non-empty-string vm-included-text-prefix)))
3696 (t default-prefix)))
3697 (ispell-skip-region-alist
3698 (cons (list (concat "^\\(" cite-regexp "\\)")
3699 (function forward-line))
3700 ispell-skip-region-alist))
3701 (old-case-fold-search case-fold-search)
3702 (dictionary-alist ispell-message-dictionary-alist)
3703 (ispell-checking-message t))
3704
3705 ;; Select dictionary for message
3706 (or (local-variable-p 'ispell-local-dictionary (current-buffer))
3707 (while dictionary-alist
3708 (goto-char (point-min))
3709 (if (re-search-forward (car (car dictionary-alist))
3710 end-of-headers t)
3711 (setq ispell-local-dictionary (cdr (car dictionary-alist))
3712 dictionary-alist nil)
3713 (setq dictionary-alist (cdr dictionary-alist)))))
3714
3715 (unwind-protect
3716 (progn
3717 ;; Spell check any original Subject:
3718 (goto-char (point-min))
3719 (setq case-fold-search t
3720 mimep (re-search-forward "MIME-Version:" end-of-headers t))
3721 (goto-char (point-min))
3722 (if (re-search-forward "^Subject: *" end-of-headers t)
3723 (progn
3724 (goto-char (match-end 0))
3725 (if (and (not (looking-at ".*Re\\>"))
3726 (not (looking-at "\\[")))
3727 (progn
3728 (setq case-fold-search old-case-fold-search)
3729 (ispell-region (point)
3730 (progn ;Tab-initiated continuation lns.
3731 (end-of-line)
3732 (while (looking-at "\n[ \t]")
3733 (end-of-line 2))
3734 (point)))))))
3735 (if mimep
3736 (progn
3737 (goto-char (point-min))
3738 (setq boundary (ispell-mime-multipartp end-of-headers))))
3739 ;; Adjust message limit to MIME message if necessary.
3740 (and boundary
3741 (re-search-forward (concat boundary "--") nil t)
3742 (re-search-backward boundary nil t)
3743 (< (point) (marker-position limit))
3744 (set-marker limit (point)))
3745 (goto-char (point-min))
3746 ;; Select type or skip checking if this is a non-multipart message
3747 ;; Point moved to end of buffer if region is encoded.
3748 (if (and mimep (not boundary))
3749 (let (skip-regexp) ; protect from `ispell-mime-skip-part'
3750 (goto-char (point-min))
3751 (re-search-forward "Content-[^ \t]*:" end-of-headers t)
3752 (forward-line -1) ; following fn starts one line above
3753 (ispell-mime-skip-part nil)
3754 ;; if message-text-end region, limit may be less than point.
3755 (if (> (point) limit)
3756 (set-marker limit (point)))))
3757 (goto-char (max end-of-headers (point)))
3758 (forward-line 1)
3759 (setq case-fold-search old-case-fold-search)
3760 ;; Define MIME regions to skip.
3761 (if boundary
3762 (setq ispell-checking-message
3763 (list (list boundary 'ispell-mime-skip-part boundary))))
3764 (ispell-region (point) limit))
3765 (set-marker end-of-headers nil)
3766 (set-marker limit nil)
3767 (setq ispell-skip-region-alist ispell-skip-region-alist-save
3768 ispell-skip-html nil
3769 case-fold-search old-case-fold-search)))))
3770
3771
3772 (defun ispell-non-empty-string (string)
3773 (if (or (not string) (string-equal string ""))
3774 "\\'\\`" ; An unmatchable string if string is null.
3775 (regexp-quote string)))
3776
3777
3778 ;;; **********************************************************************
3779 ;;; Buffer Local Functions
3780 ;;; **********************************************************************
3781
3782
3783 (defun ispell-accept-buffer-local-defs ()
3784 "Load all buffer-local information, restarting Ispell when necessary."
3785 (ispell-buffer-local-dict) ; May kill ispell-process.
3786 (ispell-buffer-local-words) ; Will initialize ispell-process.
3787 (ispell-buffer-local-parsing))
3788
3789
3790 (defun ispell-buffer-local-parsing ()
3791 "Place Ispell into parsing mode for this buffer.
3792 Overrides the default parsing mode.
3793 Includes Latex/Nroff modes and extended character mode."
3794 ;; (ispell-init-process) must already be called.
3795 (ispell-send-string "!\n") ; Put process in terse mode.
3796 ;; We assume all major modes with "tex-mode" in them should use latex parsing
3797 ;; When exclusively checking comments, set to raw text mode (nroff).
3798 (if (and (not (eq 'exclusive ispell-check-comments))
3799 (or (and (eq ispell-parser 'use-mode-name)
3800 (string-match "[Tt][Ee][Xx]-mode"
3801 (symbol-name major-mode)))
3802 (eq ispell-parser 'tex)))
3803 (progn
3804 (ispell-send-string "+\n") ; set ispell mode to tex
3805 (if (not (eq ispell-parser 'tex))
3806 (set (make-local-variable 'ispell-parser) 'tex)))
3807 (ispell-send-string "-\n")) ; set mode to normal (nroff)
3808 ;; If needed, test for SGML & HTML modes and set a buffer local nil/t value.
3809 (if (and ispell-skip-html (not (eq ispell-skip-html t)))
3810 (setq ispell-skip-html
3811 (not (null (string-match "sgml\\|html\\|xml"
3812 (downcase (symbol-name major-mode)))))))
3813 ;; Set default extended character mode for given buffer, if any.
3814 (let ((extended-char-mode (ispell-get-extended-character-mode)))
3815 (if extended-char-mode
3816 (ispell-send-string (concat extended-char-mode "\n"))))
3817 ;; Set buffer-local parsing mode and extended character mode, if specified.
3818 (save-excursion
3819 (goto-char (point-max))
3820 ;; Uses last occurrence of ispell-parsing-keyword
3821 (if (search-backward ispell-parsing-keyword nil t)
3822 (let ((end (save-excursion (end-of-line) (point)))
3823 string)
3824 (search-forward ispell-parsing-keyword)
3825 (while (re-search-forward " *\\([^ \"]+\\)" end t)
3826 ;; space separated definitions.
3827 (setq string (downcase (match-string-no-properties 1)))
3828 (cond ((and (string-match "latex-mode" string)
3829 (not (eq 'exclusive ispell-check-comments)))
3830 (ispell-send-string "+\n~tex\n"))
3831 ((string-match "nroff-mode" string)
3832 (ispell-send-string "-\n~nroff\n"))
3833 ((string-match "~" string) ; Set extended character mode.
3834 (ispell-send-string (concat string "\n")))
3835 (t (message "Invalid Ispell Parsing argument!")
3836 (sit-for 2))))))))
3837
3838
3839 ;;; Can kill the current ispell process
3840
3841 (defun ispell-buffer-local-dict (&optional no-reload)
3842 "Initializes local dictionary and local personal dictionary.
3843 If optional NO-RELOAD is non-nil, do not make any dictionary reloading.
3844 When a dictionary is defined in the buffer (see variable
3845 `ispell-dictionary-keyword'), it will override the local setting
3846 from \\[ispell-change-dictionary].
3847 Both should not be used to define a buffer-local dictionary."
3848 (save-excursion
3849 (goto-char (point-min))
3850 (let (end)
3851 ;; Override the local variable definition.
3852 ;; Uses last occurrence of ispell-dictionary-keyword.
3853 (goto-char (point-max))
3854 (unless ispell-local-dictionary-overridden
3855 (if (search-backward ispell-dictionary-keyword nil t)
3856 (progn
3857 (search-forward ispell-dictionary-keyword)
3858 (setq end (save-excursion (end-of-line) (point)))
3859 (if (re-search-forward " *\\([^ \"]+\\)" end t)
3860 (setq ispell-local-dictionary
3861 (match-string-no-properties 1))))))
3862 (goto-char (point-max))
3863 (if (search-backward ispell-pdict-keyword nil t)
3864 (progn
3865 (search-forward ispell-pdict-keyword)
3866 (setq end (save-excursion (end-of-line) (point)))
3867 (if (re-search-forward " *\\([^ \"]+\\)" end t)
3868 (setq ispell-local-pdict
3869 (match-string-no-properties 1)))))))
3870 (unless no-reload
3871 ;; Reload if new dictionary (maybe the personal one) defined.
3872 (ispell-internal-change-dictionary)))
3873
3874
3875 (defun ispell-buffer-local-words ()
3876 "Load the buffer-local dictionary in the current buffer."
3877 ;; If there's an existing ispell process that's wrong for this use,
3878 ;; kill it.
3879 (if (and ispell-buffer-local-name
3880 (not (equal ispell-buffer-local-name (buffer-name))))
3881 (ispell-kill-ispell t))
3882 ;; Actually start a new ispell process, because we need
3883 ;; to send commands now to specify the local words to it.
3884 (ispell-init-process)
3885 (save-excursion
3886 (goto-char (point-min))
3887 (while (search-forward ispell-words-keyword nil t)
3888 (or ispell-buffer-local-name
3889 (setq ispell-buffer-local-name (buffer-name)))
3890 (let ((end (save-excursion (end-of-line) (point)))
3891 (ispell-casechars (ispell-get-casechars))
3892 string)
3893 ;; buffer-local words separated by a space, and can contain
3894 ;; any character other than a space. Not rigorous enough.
3895 (while (re-search-forward " *\\([^ ]+\\)" end t)
3896 (setq string (match-string-no-properties 1))
3897 ;; This can fail when string contains a word with invalid chars.
3898 ;; Error handling needs to be added between ispell and Emacs.
3899 (if (and (< 1 (length string))
3900 (equal 0 (string-match ispell-casechars string)))
3901 (ispell-send-string (concat "@" string "\n"))))))))
3902
3903
3904 ;;; returns optionally adjusted region-end-point.
3905
3906 (defun ispell-add-per-file-word-list (word)
3907 "Add WORD to the per-file word list."
3908 (or ispell-buffer-local-name
3909 (setq ispell-buffer-local-name (buffer-name)))
3910 (save-excursion
3911 (goto-char (point-min))
3912 (let ((old-case-fold-search case-fold-search)
3913 line-okay search done found)
3914 (while (not done)
3915 (setq case-fold-search nil
3916 search (search-forward ispell-words-keyword nil 'move)
3917 found (or found search)
3918 line-okay (< (+ (length word) 1 ; 1 for space after word..
3919 (progn (end-of-line) (current-column)))
3920 80)
3921 case-fold-search old-case-fold-search)
3922 (if (or (and search line-okay)
3923 (null search))
3924 (progn
3925 (setq done t)
3926 (if (null search)
3927 (progn
3928 (open-line 1)
3929 (unless found (newline))
3930 (insert (concat comment-start " " ispell-words-keyword))
3931 (if (> (length comment-end) 0)
3932 (save-excursion
3933 (newline)
3934 (insert comment-end)))))
3935 (insert (concat " " word))))))))
3936
3937 (add-to-list 'debug-ignored-errors "^No word found to check!$")
3938
3939 (provide 'ispell)
3940
3941 \f
3942 ;;; LOCAL VARIABLES AND BUFFER-LOCAL VALUE EXAMPLES.
3943
3944 ;; Local Variable options:
3945 ;; mode: name(-mode)
3946 ;; eval: expression
3947 ;; local-variable: value
3948
3949 ;; The following sets the buffer local dictionary to `american' English
3950 ;; and spell checks only comments.
3951
3952 ;; Local Variables:
3953 ;; mode: emacs-lisp
3954 ;; comment-column: 40
3955 ;; ispell-check-comments: exclusive
3956 ;; ispell-local-dictionary: "american"
3957 ;; End:
3958
3959
3960 ;;; MORE EXAMPLES OF ISPELL BUFFER-LOCAL VALUES
3961
3962 ;; The following places this file in nroff parsing and extended char modes.
3963 ;; Local IspellParsing: nroff-mode ~nroff
3964 ;; Change IspellPersDict to IspellPersDict: to enable the following line.
3965 ;; Local IspellPersDict ~/.ispell_lisp
3966 ;; The following were automatically generated by ispell using the 'A' command:
3967 ; LocalWords: settable alist inews mh frag pdict Wildcards iconify arg tex kss
3968 ; LocalWords: alists minibuffer bufferp autoload loaddefs aff Dansk KOI SPC op
3969 ; LocalWords: Francais Nederlands charset autoloaded popup nonmenu regexp num
3970 ; LocalWords: AMStex hspace includeonly nocite epsfig displaymath eqnarray reg
3971 ; LocalWords: minipage modeline pers dict unhighlight buf grep sync prev inc
3972 ; LocalWords: fn hilight oldot NB AIX msg init read's bufs pt cmd Quinlan eg
3973 ; LocalWords: uuencoded unidiff sc nn VM SGML eval IspellPersDict unsplitable
3974 ; LocalWords: lns XEmacs HTML casechars Multibyte
3975
3976 ;; arch-tag: 4941b9f9-3b7c-4a76-a4ed-5fa8b6010ef5
3977 ;;; ispell.el ends here