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