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