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