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