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