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