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