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