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