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