]> code.delx.au - gnu-emacs/blob - lisp/textmodes/flyspell.el
Add 2010 to copyright years.
[gnu-emacs] / lisp / textmodes / flyspell.el
1 ;;; flyspell.el --- on-the-fly spell checker
2
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
7 ;; Maintainer: FSF
8 ;; Keywords: convenience
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26 ;;
27 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
28 ;; checking.
29 ;;
30 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
31 ;; This applies only to the current buffer.
32 ;;
33 ;; To enable Flyspell in text representing computer programs, type
34 ;; M-x flyspell-prog-mode.
35 ;; In that mode only text inside comments is checked.
36 ;;
37 ;; Note: consider setting the variable ispell-parser to `tex' to
38 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
39 ;;
40 ;; Some user variables control the behavior of flyspell. They are
41 ;; those defined under the `User variables' comment.
42
43 ;;; Code:
44
45 (require 'ispell)
46
47 ;;*---------------------------------------------------------------------*/
48 ;;* Group ... */
49 ;;*---------------------------------------------------------------------*/
50 (defgroup flyspell nil
51 "Spell checking on the fly."
52 :tag "FlySpell"
53 :prefix "flyspell-"
54 :group 'ispell
55 :group 'processes)
56
57 ;;*---------------------------------------------------------------------*/
58 ;;* User configuration ... */
59 ;;*---------------------------------------------------------------------*/
60 (defcustom flyspell-highlight-flag t
61 "How Flyspell should indicate misspelled words.
62 Non-nil means use highlight, nil means use minibuffer messages."
63 :group 'flyspell
64 :type 'boolean)
65
66 (defcustom flyspell-mark-duplications-flag t
67 "Non-nil means Flyspell reports a repeated word as an error.
68 See `flyspell-mark-duplications-exceptions' to add exceptions to this rule.
69 Detection of repeated words is not implemented in
70 \"large\" regions; see `flyspell-large-region'."
71 :group 'flyspell
72 :type 'boolean)
73
74 (defcustom flyspell-mark-duplications-exceptions
75 '(("francais" . ("nous" "vous")))
76 "A list of exceptions for duplicated words.
77 It should be a list of (LANGUAGE . EXCEPTION-LIST). LANGUAGE is matched
78 against the current dictionary and EXCEPTION-LIST is a list of strings.
79 The duplicated word is downcased before it is compared with the exceptions."
80 :group 'flyspell
81 :type '(alist :key-type string :value-type (repeat string)))
82
83 (defcustom flyspell-sort-corrections nil
84 "Non-nil means, sort the corrections alphabetically before popping them."
85 :group 'flyspell
86 :version "21.1"
87 :type 'boolean)
88
89 (defcustom flyspell-duplicate-distance -1
90 "The maximum distance for finding duplicates of unrecognized words.
91 This applies to the feature that when a word is not found in the dictionary,
92 if the same spelling occurs elsewhere in the buffer,
93 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
94 This variable specifies how far to search to find such a duplicate.
95 -1 means no limit (search the whole buffer).
96 0 means do not search for duplicate unrecognized spellings."
97 :group 'flyspell
98 :version "21.1"
99 :type '(choice (const :tag "no limit" -1)
100 number))
101
102 (defcustom flyspell-delay 3
103 "The number of seconds to wait before checking, after a \"delayed\" command."
104 :group 'flyspell
105 :type 'number)
106
107 (defcustom flyspell-persistent-highlight t
108 "Non-nil means misspelled words remain highlighted until corrected.
109 If this variable is nil, only the most recently detected misspelled word
110 is highlighted."
111 :group 'flyspell
112 :type 'boolean)
113
114 (defcustom flyspell-highlight-properties t
115 "Non-nil means highlight incorrect words even if a property exists for this word."
116 :group 'flyspell
117 :type 'boolean)
118
119 (defcustom flyspell-default-delayed-commands
120 '(self-insert-command
121 delete-backward-char
122 backward-or-forward-delete-char
123 delete-char
124 scrollbar-vertical-drag
125 backward-delete-char-untabify)
126 "The standard list of delayed commands for Flyspell.
127 See `flyspell-delayed-commands'."
128 :group 'flyspell
129 :version "21.1"
130 :type '(repeat (symbol)))
131
132 (defcustom flyspell-delayed-commands nil
133 "List of commands that are \"delayed\" for Flyspell mode.
134 After these commands, Flyspell checking is delayed for a short time,
135 whose length is specified by `flyspell-delay'."
136 :group 'flyspell
137 :type '(repeat (symbol)))
138
139 (defcustom flyspell-default-deplacement-commands
140 '(next-line
141 previous-line
142 scroll-up
143 scroll-down)
144 "The standard list of deplacement commands for Flyspell.
145 See `flyspell-deplacement-commands'."
146 :group 'flyspell
147 :version "21.1"
148 :type '(repeat (symbol)))
149
150 (defcustom flyspell-deplacement-commands nil
151 "List of commands that are \"deplacement\" for Flyspell mode.
152 After these commands, Flyspell checking is performed only if the previous
153 command was not the very same command."
154 :group 'flyspell
155 :version "21.1"
156 :type '(repeat (symbol)))
157
158 (defcustom flyspell-issue-welcome-flag t
159 "Non-nil means that Flyspell should display a welcome message when started."
160 :group 'flyspell
161 :type 'boolean)
162
163 (defcustom flyspell-issue-message-flag t
164 "Non-nil means that Flyspell emits messages when checking words."
165 :group 'flyspell
166 :type 'boolean)
167
168 (defcustom flyspell-incorrect-hook nil
169 "List of functions to be called when incorrect words are encountered.
170 Each function is given three arguments. The first two
171 arguments are the beginning and the end of the incorrect region.
172 The third is either the symbol `doublon' or the list
173 of possible corrections as returned by `ispell-parse-output'.
174
175 If any of the functions return non-nil, the word is not highlighted as
176 incorrect."
177 :group 'flyspell
178 :version "21.1"
179 :type 'hook)
180
181 (defcustom flyspell-default-dictionary nil
182 "A string that is the name of the default dictionary.
183 This is passed to the `ispell-change-dictionary' when flyspell is started.
184 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
185 when flyspell is started, the value of that variable is used instead
186 of `flyspell-default-dictionary' to select the default dictionary.
187 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
188 Ispell's ultimate default dictionary."
189 :group 'flyspell
190 :version "21.1"
191 :type '(choice string (const :tag "Default" nil)))
192
193 (defcustom flyspell-tex-command-regexp
194 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
195 "A string that is the regular expression that matches TeX commands."
196 :group 'flyspell
197 :version "21.1"
198 :type 'string)
199
200 (defcustom flyspell-check-tex-math-command nil
201 "Non-nil means check even inside TeX math environment.
202 TeX math environments are discovered by the TEXMATHP that implemented
203 inside the texmathp.el Emacs package. That package may be found at:
204 http://strw.leidenuniv.nl/~dominik/Tools"
205 :group 'flyspell
206 :type 'boolean)
207
208 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
209 '("francais" "deutsch8" "norsk")
210 "List of dictionary names that consider `-' as word delimiter."
211 :group 'flyspell
212 :version "21.1"
213 :type '(repeat (string)))
214
215 (defcustom flyspell-abbrev-p
216 nil
217 "If non-nil, add correction to abbreviation table."
218 :group 'flyspell
219 :version "21.1"
220 :type 'boolean)
221
222 (defcustom flyspell-use-global-abbrev-table-p
223 nil
224 "If non-nil, prefer global abbrev table to local abbrev table."
225 :group 'flyspell
226 :version "21.1"
227 :type 'boolean)
228
229 (defcustom flyspell-mode-line-string " Fly"
230 "String displayed on the modeline when flyspell is active.
231 Set this to nil if you don't want a modeline indicator."
232 :group 'flyspell
233 :type '(choice string (const :tag "None" nil)))
234
235 (defcustom flyspell-large-region 1000
236 "The threshold that determines if a region is small.
237 If the region is smaller than this number of characters,
238 `flyspell-region' checks the words sequentially using regular
239 flyspell methods. Else, if the region is large, a new Ispell process is
240 spawned for speed.
241
242 Doubled words are not detected in a large region, because Ispell
243 does not check for them.
244
245 If this variable is nil, all regions are treated as small."
246 :group 'flyspell
247 :version "21.1"
248 :type '(choice number (const :tag "All small" nil)))
249
250 (defcustom flyspell-insert-function (function insert)
251 "Function for inserting word by flyspell upon correction."
252 :group 'flyspell
253 :type 'function)
254
255 (defcustom flyspell-before-incorrect-word-string nil
256 "String used to indicate an incorrect word starting."
257 :group 'flyspell
258 :type '(choice string (const nil)))
259
260 (defcustom flyspell-after-incorrect-word-string nil
261 "String used to indicate an incorrect word ending."
262 :group 'flyspell
263 :type '(choice string (const nil)))
264
265 (defvar flyspell-mode-map)
266
267 (defcustom flyspell-use-meta-tab t
268 "Non-nil means that flyspell uses M-TAB to correct word."
269 :group 'flyspell
270 :type 'boolean
271 :initialize 'custom-initialize-default
272 :set (lambda (sym val)
273 (define-key flyspell-mode-map "\M-\t"
274 (if (set sym val)
275 'flyspell-auto-correct-word))))
276
277 (defcustom flyspell-auto-correct-binding
278 [(control ?\;)]
279 "The key binding for flyspell auto correction."
280 :group 'flyspell)
281
282 ;;*---------------------------------------------------------------------*/
283 ;;* Mode specific options */
284 ;;* ------------------------------------------------------------- */
285 ;;* Mode specific options enable users to disable flyspell on */
286 ;;* certain word depending of the emacs mode. For instance, when */
287 ;;* using flyspell with mail-mode add the following expression */
288 ;;* in your .emacs file: */
289 ;;* (add-hook 'mail-mode */
290 ;;* '(lambda () (setq flyspell-generic-check-word-predicate */
291 ;;* 'mail-mode-flyspell-verify))) */
292 ;;*---------------------------------------------------------------------*/
293 (defvar flyspell-generic-check-word-predicate nil
294 "Function providing per-mode customization over which words are flyspelled.
295 Returns t to continue checking, nil otherwise.
296 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
297 property of the major mode name.")
298 (make-variable-buffer-local 'flyspell-generic-check-word-predicate)
299 (defvaralias 'flyspell-generic-check-word-p
300 'flyspell-generic-check-word-predicate)
301
302 ;;*--- mail mode -------------------------------------------------------*/
303 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
304 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
305 (defvar message-signature-separator)
306 (defun mail-mode-flyspell-verify ()
307 "Function used for `flyspell-generic-check-word-predicate' in Mail mode."
308 (let ((header-end (save-excursion
309 (goto-char (point-min))
310 (re-search-forward
311 (concat "^"
312 (regexp-quote mail-header-separator)
313 "$")
314 nil t)
315 (point)))
316 (signature-begin
317 (if (not (boundp 'message-signature-separator))
318 (point-max)
319 (save-excursion
320 (goto-char (point-max))
321 (re-search-backward message-signature-separator nil t)
322 (point)))))
323 (cond ((< (point) header-end)
324 (and (save-excursion (beginning-of-line)
325 (looking-at "^Subject:"))
326 (> (point) (match-end 0))))
327 ((> (point) signature-begin)
328 nil)
329 (t
330 (save-excursion
331 (beginning-of-line)
332 (not (looking-at "[>}|]\\|To:")))))))
333
334 ;;*--- texinfo mode ----------------------------------------------------*/
335 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
336 (defun texinfo-mode-flyspell-verify ()
337 "Function used for `flyspell-generic-check-word-predicate' in Texinfo mode."
338 (save-excursion
339 (forward-word -1)
340 (not (looking-at "@"))))
341
342 ;;*--- tex mode --------------------------------------------------------*/
343 (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
344 (defun tex-mode-flyspell-verify ()
345 "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
346 (and
347 (not (save-excursion
348 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t)))
349 (not (save-excursion
350 (let ((this (point)))
351 (beginning-of-line)
352 (and (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}"
353 (line-end-position) t)
354 (>= this (match-beginning 0))
355 (<= this (match-end 0))))))))
356
357 ;;*--- sgml mode -------------------------------------------------------*/
358 (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
359 (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
360 (put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
361
362 (declare-function sgml-lexical-context "sgml-mode" &optional limit)
363
364 (defun sgml-mode-flyspell-verify ()
365 "Function used for `flyspell-generic-check-word-predicate' in SGML mode.
366 Tag and attribute names are not spell checked, everything else is.
367
368 String values of attributes are checked because they can be text
369 like <img alt=\"Some thing.\">."
370
371 (not (memq (car (sgml-lexical-context))
372 '(tag pi))))
373
374 ;;*---------------------------------------------------------------------*/
375 ;;* Programming mode */
376 ;;*---------------------------------------------------------------------*/
377 (defvar flyspell-prog-text-faces
378 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
379 "Faces corresponding to text in programming-mode buffers.")
380
381 (defun flyspell-generic-progmode-verify ()
382 "Used for `flyspell-generic-check-word-predicate' in programming modes."
383 (let ((f (get-text-property (point) 'face)))
384 (memq f flyspell-prog-text-faces)))
385
386 ;;;###autoload
387 (defun flyspell-prog-mode ()
388 "Turn on `flyspell-mode' for comments and strings."
389 (interactive)
390 (setq flyspell-generic-check-word-predicate
391 'flyspell-generic-progmode-verify)
392 (flyspell-mode 1)
393 (run-hooks 'flyspell-prog-mode-hook))
394
395 ;;*---------------------------------------------------------------------*/
396 ;;* Overlay compatibility */
397 ;;*---------------------------------------------------------------------*/
398 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
399 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
400 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
401 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
402 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
403 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
404 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
405 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
406
407 ;;*---------------------------------------------------------------------*/
408 ;;* The minor mode declaration. */
409 ;;*---------------------------------------------------------------------*/
410 (defvar flyspell-mouse-map
411 (let ((map (make-sparse-keymap)))
412 (if (featurep 'xemacs)
413 (define-key map [button2] #'flyspell-correct-word)
414 (define-key map [down-mouse-2] #'flyspell-correct-word)
415 (define-key map [mouse-2] 'undefined))
416 map)
417 "Keymap for Flyspell to put on erroneous words.")
418
419 (defvar flyspell-mode-map
420 (let ((map (make-sparse-keymap)))
421 (if flyspell-use-meta-tab
422 (define-key map "\M-\t" 'flyspell-auto-correct-word))
423 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
424 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
425 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
426 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
427 map)
428 "Minor mode keymap for Flyspell mode--for the whole buffer.")
429
430 ;; dash character machinery
431 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
432 "*Non-nil means that the `-' char is considered as a word delimiter.")
433 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
434 (defvar flyspell-dash-dictionary nil)
435 (make-variable-buffer-local 'flyspell-dash-dictionary)
436 (defvar flyspell-dash-local-dictionary nil)
437 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
438
439 ;;*---------------------------------------------------------------------*/
440 ;;* Highlighting */
441 ;;*---------------------------------------------------------------------*/
442 (defface flyspell-incorrect
443 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
444 (t (:bold t)))
445 "Face used for marking a misspelled word in Flyspell."
446 :group 'flyspell)
447 (define-obsolete-face-alias 'flyspell-incorrect-face 'flyspell-incorrect "22.1")
448
449 (defface flyspell-duplicate
450 '((((class color)) (:foreground "Gold3" :bold t :underline t))
451 (t (:bold t)))
452 "Face used for marking a misspelled word that appears twice in the buffer.
453 See also `flyspell-duplicate-distance'."
454 :group 'flyspell)
455 (define-obsolete-face-alias 'flyspell-duplicate-face 'flyspell-duplicate "22.1")
456
457 (defvar flyspell-overlay nil)
458
459 ;;*---------------------------------------------------------------------*/
460 ;;* flyspell-mode ... */
461 ;;*---------------------------------------------------------------------*/
462 ;;;###autoload(defvar flyspell-mode nil)
463 ;;;###autoload
464 (define-minor-mode flyspell-mode
465 "Minor mode performing on-the-fly spelling checking.
466 This spawns a single Ispell process and checks each word.
467 The default flyspell behavior is to highlight incorrect words.
468 With no argument, this command toggles Flyspell mode.
469 With a prefix argument ARG, turn Flyspell minor mode on if ARG is positive,
470 otherwise turn it off.
471
472 Bindings:
473 \\[ispell-word]: correct words (using Ispell).
474 \\[flyspell-auto-correct-word]: automatically correct word.
475 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
476 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
477
478 Hooks:
479 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
480
481 Remark:
482 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
483 valid. For instance, a different dictionary can be used by
484 invoking `ispell-change-dictionary'.
485
486 Consider using the `ispell-parser' to check your text. For instance
487 consider adding:
488 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
489 in your .emacs file.
490
491 \\[flyspell-region] checks all words inside a region.
492 \\[flyspell-buffer] checks the whole buffer."
493 :lighter flyspell-mode-line-string
494 :keymap flyspell-mode-map
495 :group 'flyspell
496 (if flyspell-mode
497 (condition-case ()
498 (flyspell-mode-on)
499 (error (message "Enabling Flyspell mode gave an error")
500 (flyspell-mode -1)))
501 (flyspell-mode-off)))
502
503 ;;;###autoload
504 (defun turn-on-flyspell ()
505 "Unconditionally turn on Flyspell mode."
506 (flyspell-mode 1))
507
508 ;;;###autoload
509 (defun turn-off-flyspell ()
510 "Unconditionally turn off Flyspell mode."
511 (flyspell-mode -1))
512
513 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
514
515 ;;*---------------------------------------------------------------------*/
516 ;;* flyspell-buffers ... */
517 ;;* ------------------------------------------------------------- */
518 ;;* For remembering buffers running flyspell */
519 ;;*---------------------------------------------------------------------*/
520 (defvar flyspell-buffers nil)
521
522 ;;*---------------------------------------------------------------------*/
523 ;;* flyspell-minibuffer-p ... */
524 ;;*---------------------------------------------------------------------*/
525 (defun flyspell-minibuffer-p (buffer)
526 "Is BUFFER a minibuffer?"
527 (let ((ws (get-buffer-window-list buffer t)))
528 (and (consp ws) (window-minibuffer-p (car ws)))))
529
530 ;;*---------------------------------------------------------------------*/
531 ;;* flyspell-accept-buffer-local-defs ... */
532 ;;*---------------------------------------------------------------------*/
533 (defvar flyspell-last-buffer nil
534 "The buffer in which the last flyspell operation took place.")
535
536 (defun flyspell-accept-buffer-local-defs (&optional force)
537 ;; When flyspell-word is used inside a loop (e.g. when processing
538 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
539 ;; up dwarfing everything else, so only do it when the buffer has changed.
540 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
541 (setq flyspell-last-buffer (current-buffer))
542 ;; Strange problem: If buffer in current window has font-lock turned on,
543 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
544 ;; call will reset the buffer to the buffer in the current window.
545 ;; However, it only happens at startup (fix by Albert L. Ting).
546 (save-current-buffer
547 (ispell-accept-buffer-local-defs))
548 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
549 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
550 ;; The dictionary has changed
551 (setq flyspell-dash-dictionary ispell-dictionary)
552 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
553 (setq flyspell-consider-dash-as-word-delimiter-flag
554 (member (or ispell-local-dictionary ispell-dictionary)
555 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
556
557 (defun flyspell-hack-local-variables-hook ()
558 ;; When local variables are loaded, see if the dictionary context
559 ;; has changed.
560 (flyspell-accept-buffer-local-defs 'force))
561
562 (defun flyspell-kill-ispell-hook ()
563 (setq flyspell-last-buffer nil)
564 (dolist (buf (buffer-list))
565 (with-current-buffer buf
566 (kill-local-variable 'flyspell-word-cache-word))))
567
568 ;; Make sure we flush our caches when needed. Do it here rather than in
569 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
570 ;; on flyspell-mode.
571 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
572
573 ;;*---------------------------------------------------------------------*/
574 ;;* flyspell-mode-on ... */
575 ;;*---------------------------------------------------------------------*/
576 (defun flyspell-mode-on ()
577 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
578 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
579 (setq ispell-highlight-face 'flyspell-incorrect)
580 ;; local dictionaries setup
581 (or ispell-local-dictionary ispell-dictionary
582 (if flyspell-default-dictionary
583 (ispell-change-dictionary flyspell-default-dictionary)))
584 ;; we have to force ispell to accept the local definition or
585 ;; otherwise it could be too late, the local dictionary may
586 ;; be forgotten!
587 ;; Pass the `force' argument for the case where flyspell was active already
588 ;; but the buffer's local-defs have been edited.
589 (flyspell-accept-buffer-local-defs 'force)
590 ;; we put the `flyspell-delayed' property on some commands
591 (flyspell-delay-commands)
592 ;; we put the `flyspell-deplacement' property on some commands
593 (flyspell-deplacement-commands)
594 ;; we bound flyspell action to post-command hook
595 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
596 ;; we bound flyspell action to pre-command hook
597 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
598 ;; we bound flyspell action to after-change hook
599 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
600 ;; we bound flyspell action to hack-local-variables-hook
601 (add-hook 'hack-local-variables-hook
602 (function flyspell-hack-local-variables-hook) t t)
603 ;; set flyspell-generic-check-word-predicate based on the major mode
604 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
605 (if mode-predicate
606 (setq flyspell-generic-check-word-predicate mode-predicate)))
607 ;; the welcome message
608 (if (and flyspell-issue-message-flag
609 flyspell-issue-welcome-flag
610 (called-interactively-p 'interactive))
611 (let ((binding (where-is-internal 'flyspell-auto-correct-word
612 nil 'non-ascii)))
613 (message "%s"
614 (if binding
615 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
616 (key-description binding))
617 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
618
619 ;;*---------------------------------------------------------------------*/
620 ;;* flyspell-delay-commands ... */
621 ;;*---------------------------------------------------------------------*/
622 (defun flyspell-delay-commands ()
623 "Install the standard set of Flyspell delayed commands."
624 (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
625 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
626
627 ;;*---------------------------------------------------------------------*/
628 ;;* flyspell-delay-command ... */
629 ;;*---------------------------------------------------------------------*/
630 (defun flyspell-delay-command (command)
631 "Set COMMAND to be delayed, for Flyspell.
632 When flyspell `post-command-hook' is invoked because a delayed command
633 as been used the current word is not immediately checked.
634 It will be checked only after `flyspell-delay' seconds."
635 (interactive "SDelay Flyspell after Command: ")
636 (put command 'flyspell-delayed t))
637
638 ;;*---------------------------------------------------------------------*/
639 ;;* flyspell-deplacement-commands ... */
640 ;;*---------------------------------------------------------------------*/
641 (defun flyspell-deplacement-commands ()
642 "Install the standard set of Flyspell deplacement commands."
643 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
644 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
645
646 ;;*---------------------------------------------------------------------*/
647 ;;* flyspell-deplacement-command ... */
648 ;;*---------------------------------------------------------------------*/
649 (defun flyspell-deplacement-command (command)
650 "Set COMMAND that implement cursor movements, for Flyspell.
651 When flyspell `post-command-hook' is invoked because of a deplacement command
652 as been used the current word is checked only if the previous command was
653 not the very same deplacement command."
654 (interactive "SDeplacement Flyspell after Command: ")
655 (put command 'flyspell-deplacement t))
656
657 ;;*---------------------------------------------------------------------*/
658 ;;* flyspell-word-cache ... */
659 ;;*---------------------------------------------------------------------*/
660 (defvar flyspell-word-cache-start nil)
661 (defvar flyspell-word-cache-end nil)
662 (defvar flyspell-word-cache-word nil)
663 (defvar flyspell-word-cache-result '_)
664 (make-variable-buffer-local 'flyspell-word-cache-start)
665 (make-variable-buffer-local 'flyspell-word-cache-end)
666 (make-variable-buffer-local 'flyspell-word-cache-word)
667 (make-variable-buffer-local 'flyspell-word-cache-result)
668
669 ;;*---------------------------------------------------------------------*/
670 ;;* The flyspell pre-hook, store the current position. In the */
671 ;;* post command hook, we will check, if the word at this position */
672 ;;* has to be spell checked. */
673 ;;*---------------------------------------------------------------------*/
674 (defvar flyspell-pre-buffer nil)
675 (defvar flyspell-pre-point nil)
676 (defvar flyspell-pre-column nil)
677 (defvar flyspell-pre-pre-buffer nil)
678 (defvar flyspell-pre-pre-point nil)
679 (make-variable-buffer-local 'flyspell-pre-point)
680
681 ;;*---------------------------------------------------------------------*/
682 ;;* flyspell-previous-command ... */
683 ;;*---------------------------------------------------------------------*/
684 (defvar flyspell-previous-command nil
685 "The last interactive command checked by Flyspell.")
686
687 ;;*---------------------------------------------------------------------*/
688 ;;* flyspell-pre-command-hook ... */
689 ;;*---------------------------------------------------------------------*/
690 (defun flyspell-pre-command-hook ()
691 "Save the current buffer and point for Flyspell's post-command hook."
692 (interactive)
693 (setq flyspell-pre-buffer (current-buffer))
694 (setq flyspell-pre-point (point))
695 (setq flyspell-pre-column (current-column)))
696
697 ;;*---------------------------------------------------------------------*/
698 ;;* flyspell-mode-off ... */
699 ;;*---------------------------------------------------------------------*/
700 ;;;###autoload
701 (defun flyspell-mode-off ()
702 "Turn Flyspell mode off."
703 ;; we remove the hooks
704 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
705 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
706 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
707 (remove-hook 'hack-local-variables-hook
708 (function flyspell-hack-local-variables-hook) t)
709 ;; we remove all the flyspell hilightings
710 (flyspell-delete-all-overlays)
711 ;; we have to erase pre cache variables
712 (setq flyspell-pre-buffer nil)
713 (setq flyspell-pre-point nil)
714 ;; we mark the mode as killed
715 (setq flyspell-mode nil))
716
717 ;;*---------------------------------------------------------------------*/
718 ;;* flyspell-check-pre-word-p ... */
719 ;;*---------------------------------------------------------------------*/
720 (defun flyspell-check-pre-word-p ()
721 "Return non-nil if we should check the word before point.
722 More precisely, it applies to the word that was before point
723 before the current command."
724 (cond
725 ((or (not (numberp flyspell-pre-point))
726 (not (bufferp flyspell-pre-buffer))
727 (not (buffer-live-p flyspell-pre-buffer)))
728 nil)
729 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
730 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
731 nil)
732 ((or (and (= flyspell-pre-point (- (point) 1))
733 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
734 (= flyspell-pre-point (point))
735 (= flyspell-pre-point (+ (point) 1)))
736 nil)
737 ((and (symbolp this-command)
738 (not executing-kbd-macro)
739 (or (get this-command 'flyspell-delayed)
740 (and (get this-command 'flyspell-deplacement)
741 (eq flyspell-previous-command this-command)))
742 (or (= (current-column) 0)
743 (= (current-column) flyspell-pre-column)
744 ;; If other post-command-hooks change the buffer,
745 ;; flyspell-pre-point can lie past eob (bug#468).
746 (null (char-after flyspell-pre-point))
747 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
748 nil)
749 ((not (eq (current-buffer) flyspell-pre-buffer))
750 t)
751 ((not (and (numberp flyspell-word-cache-start)
752 (numberp flyspell-word-cache-end)))
753 t)
754 (t
755 (or (< flyspell-pre-point flyspell-word-cache-start)
756 (> flyspell-pre-point flyspell-word-cache-end)))))
757
758 ;;*---------------------------------------------------------------------*/
759 ;;* The flyspell after-change-hook, store the change position. In */
760 ;;* the post command hook, we will check, if the word at this */
761 ;;* position has to be spell checked. */
762 ;;*---------------------------------------------------------------------*/
763 (defvar flyspell-changes nil)
764 (make-variable-buffer-local 'flyspell-changes)
765
766 ;;*---------------------------------------------------------------------*/
767 ;;* flyspell-after-change-function ... */
768 ;;*---------------------------------------------------------------------*/
769 (defun flyspell-after-change-function (start stop len)
770 "Save the current buffer and point for Flyspell's post-command hook."
771 (push (cons start stop) flyspell-changes))
772
773 ;;*---------------------------------------------------------------------*/
774 ;;* flyspell-check-changed-word-p ... */
775 ;;*---------------------------------------------------------------------*/
776 (defun flyspell-check-changed-word-p (start stop)
777 "Return t when the changed word has to be checked.
778 The answer depends of several criteria.
779 Mostly we check word delimiters."
780 (cond
781 ((and (memq (char-after start) '(?\n ? )) (> stop start))
782 t)
783 ((not (numberp flyspell-pre-point))
784 t)
785 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
786 nil)
787 ((let ((pos (point)))
788 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
789 nil)
790 (t
791 t)))
792
793 ;;*---------------------------------------------------------------------*/
794 ;;* flyspell-check-word-p ... */
795 ;;*---------------------------------------------------------------------*/
796 (defun flyspell-check-word-p ()
797 "Return t when the word at `point' has to be checked.
798 The answer depends of several criteria.
799 Mostly we check word delimiters."
800 (cond
801 ((<= (- (point-max) 1) (point-min))
802 ;; the buffer is not filled enough
803 nil)
804 ((and (and (> (current-column) 0)
805 (not (eq (current-column) flyspell-pre-column)))
806 (save-excursion
807 (backward-char 1)
808 (and (looking-at (flyspell-get-not-casechars))
809 (or flyspell-consider-dash-as-word-delimiter-flag
810 (not (looking-at "-"))))))
811 ;; yes because we have reached or typed a word delimiter.
812 t)
813 ((symbolp this-command)
814 (cond
815 ((get this-command 'flyspell-deplacement)
816 (not (eq flyspell-previous-command this-command)))
817 ((get this-command 'flyspell-delayed)
818 ;; the current command is not delayed, that
819 ;; is that we must check the word now
820 (and (not unread-command-events)
821 (sit-for flyspell-delay)))
822 (t t)))
823 (t t)))
824
825 ;;*---------------------------------------------------------------------*/
826 ;;* flyspell-debug-signal-no-check ... */
827 ;;*---------------------------------------------------------------------*/
828 (defun flyspell-debug-signal-no-check (msg obj)
829 (setq debug-on-error t)
830 (with-current-buffer (get-buffer-create "*flyspell-debug*")
831 (erase-buffer)
832 (insert "NO-CHECK:\n")
833 (insert (format " %S : %S\n" msg obj))))
834
835 ;;*---------------------------------------------------------------------*/
836 ;;* flyspell-debug-signal-pre-word-checked ... */
837 ;;*---------------------------------------------------------------------*/
838 (defun flyspell-debug-signal-pre-word-checked ()
839 (setq debug-on-error t)
840 (with-current-buffer (get-buffer-create "*flyspell-debug*")
841 (insert "PRE-WORD:\n")
842 (insert (format " pre-point : %S\n" flyspell-pre-point))
843 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
844 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
845 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
846 (goto-char (point-max))))
847
848 ;;*---------------------------------------------------------------------*/
849 ;;* flyspell-debug-signal-word-checked ... */
850 ;;*---------------------------------------------------------------------*/
851 (defun flyspell-debug-signal-word-checked ()
852 (setq debug-on-error t)
853 (let ((oldbuf (current-buffer))
854 (point (point)))
855 (with-current-buffer (get-buffer-create "*flyspell-debug*")
856 (insert "WORD:\n")
857 (insert (format " this-cmd : %S\n" this-command))
858 (insert (format " delayed : %S\n" (and (symbolp this-command)
859 (get this-command 'flyspell-delayed))))
860 (insert (format " point : %S\n" point))
861 (insert (format " prev-char : [%c] %S\n"
862 (with-current-buffer oldbuf
863 (let ((c (if (> (point) (point-min))
864 (save-excursion
865 (backward-char 1)
866 (char-after (point)))
867 ? )))
868 c))
869 (with-current-buffer oldbuf
870 (let ((c (if (> (point) (point-min))
871 (save-excursion
872 (backward-char 1)
873 (and (and (looking-at (flyspell-get-not-casechars)) 1)
874 (and (or flyspell-consider-dash-as-word-delimiter-flag
875 (not (looking-at "\\-"))) 2))))))
876 c))))
877 (insert (format " because : %S\n"
878 (cond
879 ((not (and (symbolp this-command)
880 (get this-command 'flyspell-delayed)))
881 ;; the current command is not delayed, that
882 ;; is that we must check the word now
883 'not-delayed)
884 ((with-current-buffer oldbuf
885 (let ((c (if (> (point) (point-min))
886 (save-excursion
887 (backward-char 1)
888 (and (looking-at (flyspell-get-not-casechars))
889 (or flyspell-consider-dash-as-word-delimiter-flag
890 (not (looking-at "\\-"))))))))
891 c))
892 ;; yes because we have reached or typed a word delimiter.
893 'separator)
894 ((not (integerp flyspell-delay))
895 ;; yes because the user had set up a no-delay configuration.
896 'no-delay)
897 (t
898 'sit-for))))
899 (goto-char (point-max)))))
900
901 ;;*---------------------------------------------------------------------*/
902 ;;* flyspell-debug-signal-changed-checked ... */
903 ;;*---------------------------------------------------------------------*/
904 (defun flyspell-debug-signal-changed-checked ()
905 (setq debug-on-error t)
906 (let ((point (point)))
907 (with-current-buffer (get-buffer-create "*flyspell-debug*")
908 (insert "CHANGED WORD:\n")
909 (insert (format " point : %S\n" point))
910 (goto-char (point-max)))))
911
912 ;;*---------------------------------------------------------------------*/
913 ;;* flyspell-post-command-hook ... */
914 ;;* ------------------------------------------------------------- */
915 ;;* It is possible that we check several words: */
916 ;;* 1- the current word is checked if the predicate */
917 ;;* FLYSPELL-CHECK-WORD-P is true */
918 ;;* 2- the word that used to be the current word before the */
919 ;;* THIS-COMMAND is checked if: */
920 ;;* a- the previous word is different from the current word */
921 ;;* b- the previous word as not just been checked by the */
922 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
923 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
924 ;;* previous word nor the current word */
925 ;;*---------------------------------------------------------------------*/
926 (defun flyspell-post-command-hook ()
927 "The `post-command-hook' used by flyspell to check a word in-the-fly."
928 (interactive)
929 (when flyspell-mode
930 (let ((command this-command)
931 ;; Prevent anything we do from affecting the mark.
932 deactivate-mark)
933 (if (flyspell-check-pre-word-p)
934 (with-current-buffer flyspell-pre-buffer
935 '(flyspell-debug-signal-pre-word-checked)
936 (save-excursion
937 (goto-char flyspell-pre-point)
938 (flyspell-word))))
939 (if (flyspell-check-word-p)
940 (progn
941 '(flyspell-debug-signal-word-checked)
942 (flyspell-word)
943 ;; we remember which word we have just checked.
944 ;; this will be used next time we will check a word
945 ;; to compare the next current word with the word
946 ;; that as been registered in the pre-command-hook
947 ;; that is these variables are used within the predicate
948 ;; FLYSPELL-CHECK-PRE-WORD-P
949 (setq flyspell-pre-pre-buffer (current-buffer))
950 (setq flyspell-pre-pre-point (point)))
951 (progn
952 (setq flyspell-pre-pre-buffer nil)
953 (setq flyspell-pre-pre-point nil)
954 ;; when a word is not checked because of a delayed command
955 ;; we do not disable the ispell cache.
956 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
957 (progn
958 (setq flyspell-word-cache-end -1)
959 (setq flyspell-word-cache-result '_)))))
960 (while (and (not (input-pending-p)) (consp flyspell-changes))
961 (let ((start (car (car flyspell-changes)))
962 (stop (cdr (car flyspell-changes))))
963 (if (flyspell-check-changed-word-p start stop)
964 (save-excursion
965 '(flyspell-debug-signal-changed-checked)
966 (goto-char start)
967 (flyspell-word)))
968 (setq flyspell-changes (cdr flyspell-changes))))
969 (setq flyspell-previous-command command))))
970
971 ;;*---------------------------------------------------------------------*/
972 ;;* flyspell-notify-misspell ... */
973 ;;*---------------------------------------------------------------------*/
974 (defun flyspell-notify-misspell (word poss)
975 (let ((replacements (if (stringp poss)
976 poss
977 (if flyspell-sort-corrections
978 (sort (car (cdr (cdr poss))) 'string<)
979 (car (cdr (cdr poss)))))))
980 (if flyspell-issue-message-flag
981 (message "misspelling `%s' %S" word replacements))))
982
983 ;;*---------------------------------------------------------------------*/
984 ;;* flyspell-word-search-backward ... */
985 ;;*---------------------------------------------------------------------*/
986 (defun flyspell-word-search-backward (word bound)
987 (save-excursion
988 (let ((r '())
989 (inhibit-point-motion-hooks t)
990 p)
991 (while (and (not r) (setq p (search-backward word bound t)))
992 (let ((lw (flyspell-get-word)))
993 (if (and (consp lw) (string-equal (car lw) word))
994 (setq r p)
995 (goto-char p))))
996 r)))
997
998 ;;*---------------------------------------------------------------------*/
999 ;;* flyspell-word-search-forward ... */
1000 ;;*---------------------------------------------------------------------*/
1001 (defun flyspell-word-search-forward (word bound)
1002 (save-excursion
1003 (let ((r '())
1004 (inhibit-point-motion-hooks t)
1005 p)
1006 (while (and (not r) (setq p (search-forward word bound t)))
1007 (let ((lw (flyspell-get-word)))
1008 (if (and (consp lw) (string-equal (car lw) word))
1009 (setq r p)
1010 (goto-char (1+ p)))))
1011 r)))
1012
1013 ;;*---------------------------------------------------------------------*/
1014 ;;* flyspell-word ... */
1015 ;;*---------------------------------------------------------------------*/
1016 (defun flyspell-word (&optional following)
1017 "Spell check a word.
1018 If the optional argument FOLLOWING, or, when called interactively
1019 `ispell-following-word', is non-nil, checks the following (rather
1020 than preceding) word when the cursor is not over a word."
1021 (interactive (list ispell-following-word))
1022 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1023 (save-excursion
1024 ;; use the correct dictionary
1025 (flyspell-accept-buffer-local-defs)
1026 (let* ((cursor-location (point))
1027 (flyspell-word (flyspell-get-word following))
1028 start end poss word ispell-filter)
1029 (if (or (eq flyspell-word nil)
1030 (and (fboundp flyspell-generic-check-word-predicate)
1031 (not (funcall flyspell-generic-check-word-predicate))))
1032 t
1033 (progn
1034 ;; destructure return flyspell-word info list.
1035 (setq start (car (cdr flyspell-word))
1036 end (car (cdr (cdr flyspell-word)))
1037 word (car flyspell-word))
1038 ;; before checking in the directory, we check for doublons.
1039 (cond
1040 ((and (or (not (eq ispell-parser 'tex))
1041 (and (> start (point-min))
1042 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1043 flyspell-mark-duplications-flag
1044 (not (catch 'exception
1045 (dolist (except flyspell-mark-duplications-exceptions)
1046 (and (string= (or ispell-local-dictionary
1047 ispell-dictionary)
1048 (car except))
1049 (member (downcase word) (cdr except))
1050 (throw 'exception t)))))
1051 (save-excursion
1052 (goto-char start)
1053 (let* ((bound
1054 (- start
1055 (- end start)
1056 (- (skip-chars-backward " \t\n\f"))))
1057 (p (when (>= bound (point-min))
1058 (flyspell-word-search-backward word bound))))
1059 (and p (/= p start)))))
1060 ;; yes, this is a doublon
1061 (flyspell-highlight-incorrect-region start end 'doublon)
1062 nil)
1063 ((and (eq flyspell-word-cache-start start)
1064 (eq flyspell-word-cache-end end)
1065 (string-equal flyspell-word-cache-word word))
1066 ;; this word had been already checked, we skip
1067 flyspell-word-cache-result)
1068 ((and (eq ispell-parser 'tex)
1069 (flyspell-tex-command-p flyspell-word))
1070 ;; this is a correct word (because a tex command)
1071 (flyspell-unhighlight-at start)
1072 (if (> end start)
1073 (flyspell-unhighlight-at (- end 1)))
1074 t)
1075 (t
1076 ;; we setup the cache
1077 (setq flyspell-word-cache-start start)
1078 (setq flyspell-word-cache-end end)
1079 (setq flyspell-word-cache-word word)
1080 ;; now check spelling of word.
1081 (ispell-send-string "%\n")
1082 ;; put in verbose mode
1083 (ispell-send-string (concat "^" word "\n"))
1084 ;; we mark the ispell process so it can be killed
1085 ;; when emacs is exited without query
1086 (set-process-query-on-exit-flag ispell-process nil)
1087 ;; Wait until ispell has processed word. Since this code is often
1088 ;; executed from post-command-hook but the ispell process may not
1089 ;; be responsive, it's important to make sure we re-enable C-g.
1090 (with-local-quit
1091 (while (progn
1092 (accept-process-output ispell-process)
1093 (not (string= "" (car ispell-filter))))))
1094 ;; (ispell-send-string "!\n")
1095 ;; back to terse mode.
1096 ;; Remove leading empty element
1097 (setq ispell-filter (cdr ispell-filter))
1098 ;; ispell process should return something after word is sent.
1099 ;; Tag word as valid (i.e., skip) otherwise
1100 (or ispell-filter
1101 (setq ispell-filter '(*)))
1102 (if (consp ispell-filter)
1103 (setq poss (ispell-parse-output (car ispell-filter))))
1104 (let ((res (cond ((eq poss t)
1105 ;; correct
1106 (setq flyspell-word-cache-result t)
1107 (flyspell-unhighlight-at start)
1108 (if (> end start)
1109 (flyspell-unhighlight-at (- end 1)))
1110 t)
1111 ((and (stringp poss) flyspell-highlight-flag)
1112 ;; correct
1113 (setq flyspell-word-cache-result t)
1114 (flyspell-unhighlight-at start)
1115 (if (> end start)
1116 (flyspell-unhighlight-at (- end 1)))
1117 t)
1118 ((null poss)
1119 (setq flyspell-word-cache-result t)
1120 (flyspell-unhighlight-at start)
1121 (if (> end start)
1122 (flyspell-unhighlight-at (- end 1)))
1123 t)
1124 ((or (and (< flyspell-duplicate-distance 0)
1125 (or (save-excursion
1126 (goto-char start)
1127 (flyspell-word-search-backward
1128 word
1129 (point-min)))
1130 (save-excursion
1131 (goto-char end)
1132 (flyspell-word-search-forward
1133 word
1134 (point-max)))))
1135 (and (> flyspell-duplicate-distance 0)
1136 (or (save-excursion
1137 (goto-char start)
1138 (flyspell-word-search-backward
1139 word
1140 (- start
1141 flyspell-duplicate-distance)))
1142 (save-excursion
1143 (goto-char end)
1144 (flyspell-word-search-forward
1145 word
1146 (+ end
1147 flyspell-duplicate-distance))))))
1148 ;; This is a misspelled word which occurs
1149 ;; twice within flyspell-duplicate-distance.
1150 (setq flyspell-word-cache-result nil)
1151 (if flyspell-highlight-flag
1152 (flyspell-highlight-duplicate-region
1153 start end poss)
1154 (message "duplicate `%s'" word))
1155 nil)
1156 (t
1157 (setq flyspell-word-cache-result nil)
1158 ;; incorrect highlight the location
1159 (if flyspell-highlight-flag
1160 (flyspell-highlight-incorrect-region
1161 start end poss)
1162 (flyspell-notify-misspell word poss))
1163 nil))))
1164 ;; return to original location
1165 (goto-char cursor-location)
1166 (if ispell-quit (setq ispell-quit nil))
1167 res))))))))
1168
1169 ;;*---------------------------------------------------------------------*/
1170 ;;* flyspell-math-tex-command-p ... */
1171 ;;* ------------------------------------------------------------- */
1172 ;;* This function uses the texmathp package to check if point */
1173 ;;* is within a TeX math environment. `texmathp' can yield errors */
1174 ;;* if the document is currently not valid TeX syntax. */
1175 ;;*---------------------------------------------------------------------*/
1176 (defun flyspell-math-tex-command-p ()
1177 (when (fboundp 'texmathp)
1178 (if flyspell-check-tex-math-command
1179 nil
1180 (condition-case nil
1181 (texmathp)
1182 (error nil)))))
1183
1184 ;;*---------------------------------------------------------------------*/
1185 ;;* flyspell-tex-command-p ... */
1186 ;;*---------------------------------------------------------------------*/
1187 (defun flyspell-tex-command-p (word)
1188 "Return t if WORD is a TeX command."
1189 (or (save-excursion
1190 (let ((b (car (cdr word))))
1191 (and (re-search-backward "\\\\" (- (point) 100) t)
1192 (or (= (match-end 0) b)
1193 (and (goto-char (match-end 0))
1194 (looking-at flyspell-tex-command-regexp)
1195 (>= (match-end 0) b))))))
1196 (flyspell-math-tex-command-p)))
1197
1198 ;;*---------------------------------------------------------------------*/
1199 ;;* flyspell-casechars-cache ... */
1200 ;;*---------------------------------------------------------------------*/
1201 (defvar flyspell-casechars-cache nil)
1202 (defvar flyspell-ispell-casechars-cache nil)
1203 (make-variable-buffer-local 'flyspell-casechars-cache)
1204 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1205
1206 ;;*---------------------------------------------------------------------*/
1207 ;;* flyspell-get-casechars ... */
1208 ;;*---------------------------------------------------------------------*/
1209 (defun flyspell-get-casechars ()
1210 "This function builds a string that is the regexp of word chars.
1211 In order to avoid one useless string construction,
1212 this function changes the last char of the `ispell-casechars' string."
1213 (let ((ispell-casechars (ispell-get-casechars)))
1214 (cond
1215 ((eq ispell-parser 'tex)
1216 (setq flyspell-ispell-casechars-cache ispell-casechars)
1217 (setq flyspell-casechars-cache
1218 (concat (substring ispell-casechars
1219 0
1220 (- (length ispell-casechars) 1))
1221 "]"))
1222 flyspell-casechars-cache)
1223 (t
1224 (setq flyspell-ispell-casechars-cache ispell-casechars)
1225 (setq flyspell-casechars-cache ispell-casechars)
1226 flyspell-casechars-cache))))
1227
1228 ;;*---------------------------------------------------------------------*/
1229 ;;* flyspell-get-not-casechars-cache ... */
1230 ;;*---------------------------------------------------------------------*/
1231 (defvar flyspell-not-casechars-cache nil)
1232 (defvar flyspell-ispell-not-casechars-cache nil)
1233 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1234 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1235
1236 ;;*---------------------------------------------------------------------*/
1237 ;;* flyspell-get-not-casechars ... */
1238 ;;*---------------------------------------------------------------------*/
1239 (defun flyspell-get-not-casechars ()
1240 "This function builds a string that is the regexp of non-word chars."
1241 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1242 (cond
1243 ((eq ispell-parser 'tex)
1244 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1245 (setq flyspell-not-casechars-cache
1246 (concat (substring ispell-not-casechars
1247 0
1248 (- (length ispell-not-casechars) 1))
1249 "]"))
1250 flyspell-not-casechars-cache)
1251 (t
1252 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1253 (setq flyspell-not-casechars-cache ispell-not-casechars)
1254 flyspell-not-casechars-cache))))
1255
1256 ;;*---------------------------------------------------------------------*/
1257 ;;* flyspell-get-word ... */
1258 ;;*---------------------------------------------------------------------*/
1259 (defun flyspell-get-word (&optional following extra-otherchars)
1260 "Return the word for spell-checking according to Ispell syntax.
1261 Optional argument FOLLOWING non-nil means to get the following
1262 \(rather than preceding) word when the cursor is not over a word.
1263 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1264 that may be included as part of a word (see `ispell-dictionary-alist')."
1265 (let* ((flyspell-casechars (flyspell-get-casechars))
1266 (flyspell-not-casechars (flyspell-get-not-casechars))
1267 (ispell-otherchars (ispell-get-otherchars))
1268 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1269 (word-regexp (concat flyspell-casechars
1270 "+\\("
1271 (if (not (string= "" ispell-otherchars))
1272 (concat ispell-otherchars "?"))
1273 (if extra-otherchars
1274 (concat extra-otherchars "?"))
1275 flyspell-casechars
1276 "+\\)"
1277 (if (or ispell-many-otherchars-p
1278 extra-otherchars)
1279 "*" "?")))
1280 did-it-once prevpt
1281 start end word)
1282 ;; find the word
1283 (if (not (looking-at flyspell-casechars))
1284 (if following
1285 (re-search-forward flyspell-casechars nil t)
1286 (re-search-backward flyspell-casechars nil t)))
1287 ;; move to front of word
1288 (re-search-backward flyspell-not-casechars nil 'start)
1289 (while (and (or (and (not (string= "" ispell-otherchars))
1290 (looking-at ispell-otherchars))
1291 (and extra-otherchars (looking-at extra-otherchars)))
1292 (not (bobp))
1293 (or (not did-it-once)
1294 ispell-many-otherchars-p)
1295 (not (eq prevpt (point))))
1296 (if (and extra-otherchars (looking-at extra-otherchars))
1297 (progn
1298 (backward-char 1)
1299 (if (looking-at flyspell-casechars)
1300 (re-search-backward flyspell-not-casechars nil 'move)))
1301 (setq did-it-once t
1302 prevpt (point))
1303 (backward-char 1)
1304 (if (looking-at flyspell-casechars)
1305 (re-search-backward flyspell-not-casechars nil 'move)
1306 (backward-char -1))))
1307 ;; Now mark the word and save to string.
1308 (if (not (re-search-forward word-regexp nil t))
1309 nil
1310 (progn
1311 (setq start (match-beginning 0)
1312 end (point)
1313 word (buffer-substring-no-properties start end))
1314 (list word start end)))))
1315
1316 ;;*---------------------------------------------------------------------*/
1317 ;;* flyspell-small-region ... */
1318 ;;*---------------------------------------------------------------------*/
1319 (defun flyspell-small-region (beg end)
1320 "Flyspell text between BEG and END."
1321 (save-excursion
1322 (if (> beg end)
1323 (let ((old beg))
1324 (setq beg end)
1325 (setq end old)))
1326 (goto-char beg)
1327 (let ((count 0))
1328 (while (< (point) end)
1329 (if (and flyspell-issue-message-flag (= count 100))
1330 (progn
1331 (message "Spell Checking...%d%%"
1332 (* 100 (/ (float (- (point) beg)) (- end beg))))
1333 (setq count 0))
1334 (setq count (+ 1 count)))
1335 (flyspell-word)
1336 (sit-for 0)
1337 (let ((cur (point)))
1338 (forward-word 1)
1339 (if (and (< (point) end) (> (point) (+ cur 1)))
1340 (backward-char 1)))))
1341 (backward-char 1)
1342 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1343 (flyspell-word)))
1344
1345 ;;*---------------------------------------------------------------------*/
1346 ;;* flyspell-external-ispell-process ... */
1347 ;;*---------------------------------------------------------------------*/
1348 (defvar flyspell-external-ispell-process '()
1349 "The external Flyspell Ispell process.")
1350
1351 ;;*---------------------------------------------------------------------*/
1352 ;;* flyspell-external-ispell-buffer ... */
1353 ;;*---------------------------------------------------------------------*/
1354 (defvar flyspell-external-ispell-buffer '())
1355 (defvar flyspell-large-region-buffer '())
1356 (defvar flyspell-large-region-beg (point-min))
1357 (defvar flyspell-large-region-end (point-max))
1358
1359 ;;*---------------------------------------------------------------------*/
1360 ;;* flyspell-external-point-words ... */
1361 ;;*---------------------------------------------------------------------*/
1362 (defun flyspell-external-point-words ()
1363 "Mark words from a buffer listing incorrect words in order of appearance.
1364 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1365 \(We finish by killing that buffer and setting the variable to nil.)
1366 The buffer to mark them in is `flyspell-large-region-buffer'."
1367 (let (words-not-found
1368 (ispell-otherchars (ispell-get-otherchars))
1369 (buffer-scan-pos flyspell-large-region-beg)
1370 case-fold-search)
1371 (with-current-buffer flyspell-external-ispell-buffer
1372 (goto-char (point-min))
1373 ;; Loop over incorrect words, in the order they were reported,
1374 ;; which is also the order they appear in the buffer being checked.
1375 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1376 ;; Bind WORD to the next one.
1377 (let ((word (match-string 1)) (wordpos (point)))
1378 ;; Here there used to be code to see if WORD is the same
1379 ;; as the previous iteration, and count the number of consecutive
1380 ;; identical words, and the loop below would search for that many.
1381 ;; That code seemed to be incorrect, and on principle, should
1382 ;; be unnecessary too. -- rms.
1383 (if flyspell-issue-message-flag
1384 (message "Spell Checking...%d%% [%s]"
1385 (* 100 (/ (float (point)) (point-max)))
1386 word))
1387 (with-current-buffer flyspell-large-region-buffer
1388 (goto-char buffer-scan-pos)
1389 (let ((keep t))
1390 ;; Iterate on string search until string is found as word,
1391 ;; not as substring
1392 (while keep
1393 (if (search-forward word
1394 flyspell-large-region-end t)
1395 (let* ((found-list
1396 (save-excursion
1397 ;; Move back into the match
1398 ;; so flyspell-get-word will find it.
1399 (forward-char -1)
1400 (flyspell-get-word)))
1401 (found (car found-list))
1402 (found-length (length found))
1403 (misspell-length (length word)))
1404 (when (or
1405 ;; Size matches, we really found it.
1406 (= found-length misspell-length)
1407 ;; Matches as part of a boundary-char separated word
1408 (member word
1409 (split-string found ispell-otherchars))
1410 ;; Misspelling has higher length than
1411 ;; what flyspell considers the
1412 ;; word. Caused by boundary-chars
1413 ;; mismatch. Validating seems safe.
1414 (< found-length misspell-length)
1415 ;; ispell treats beginning of some TeX
1416 ;; commands as nroff control sequences
1417 ;; and strips them in the list of
1418 ;; misspelled words thus giving a
1419 ;; non-existent word. Skip if ispell
1420 ;; is used, string is a TeX command
1421 ;; (char before beginning of word is
1422 ;; backslash) and none of the previous
1423 ;; contitions match
1424 (and (not ispell-really-aspell)
1425 (save-excursion
1426 (goto-char (- (nth 1 found-list) 1))
1427 (if (looking-at "[\\]" )
1428 t
1429 nil))))
1430 (setq keep nil)
1431 (flyspell-word)
1432 ;; Search for next misspelled word will begin from
1433 ;; end of last validated match.
1434 (setq buffer-scan-pos (point))))
1435 ;; Record if misspelling is not found and try new one
1436 (add-to-list 'words-not-found
1437 (concat " -> " word " - "
1438 (int-to-string wordpos)))
1439 (setq keep nil)))))))
1440 ;; we are done
1441 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1442 ;; Warn about not found misspellings
1443 (dolist (word words-not-found)
1444 (message "%s: word not found" word))
1445 ;; Kill and forget the buffer with the list of incorrect words.
1446 (kill-buffer flyspell-external-ispell-buffer)
1447 (setq flyspell-external-ispell-buffer nil)))
1448
1449 ;;*---------------------------------------------------------------------*/
1450 ;;* flyspell-process-localwords ... */
1451 ;;* ------------------------------------------------------------- */
1452 ;;* This function is used to prevent marking of words explicitly */
1453 ;;* declared correct. */
1454 ;;*---------------------------------------------------------------------*/
1455 (defun flyspell-process-localwords (misspellings-buffer)
1456 (let (localwords case-fold-search
1457 (ispell-casechars (ispell-get-casechars)))
1458 ;; Get localwords from the original buffer
1459 (save-excursion
1460 (goto-char (point-min))
1461 ;; Localwords parsing copied from ispell.el.
1462 (while (search-forward ispell-words-keyword nil t)
1463 (let ((end (save-excursion (end-of-line) (point)))
1464 string)
1465 ;; buffer-local words separated by a space, and can contain
1466 ;; any character other than a space. Not rigorous enough.
1467 (while (re-search-forward " *\\([^ ]+\\)" end t)
1468 (setq string (buffer-substring-no-properties (match-beginning 1)
1469 (match-end 1)))
1470 ;; This can fail when string contains a word with invalid chars.
1471 ;; Error handling needs to be added between Ispell and Emacs.
1472 (if (and (< 1 (length string))
1473 (equal 0 (string-match ispell-casechars string)))
1474 (push string localwords))))))
1475 ;; Remove localwords matches from misspellings-buffer.
1476 ;; The usual mechanism of communicating the local words to ispell
1477 ;; does not affect the special ispell process used by
1478 ;; flyspell-large-region.
1479 (with-current-buffer misspellings-buffer
1480 (save-excursion
1481 (dolist (word localwords)
1482 (goto-char (point-min))
1483 (let ((regexp (concat "^" word "\n")))
1484 (while (re-search-forward regexp nil t)
1485 (delete-region (match-beginning 0) (match-end 0)))))))))
1486
1487 ;;* ---------------------------------------------------------------
1488 ;;* flyspell-check-region-doublons
1489 ;;* ---------------------------------------------------------------
1490 (defun flyspell-check-region-doublons (beg end)
1491 "Check for adjacent duplicated words (doublons) in the given region."
1492 (save-excursion
1493 (goto-char beg)
1494 (flyspell-word) ; Make sure current word is checked
1495 (backward-word 1)
1496 (while (and (< (point) end)
1497 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1498 end 'move))
1499 (flyspell-word)
1500 (backward-word 1))
1501 (flyspell-word)))
1502
1503 ;;*---------------------------------------------------------------------*/
1504 ;;* flyspell-large-region ... */
1505 ;;*---------------------------------------------------------------------*/
1506 (defun flyspell-large-region (beg end)
1507 (let* ((curbuf (current-buffer))
1508 (buffer (get-buffer-create "*flyspell-region*")))
1509 (setq flyspell-external-ispell-buffer buffer)
1510 (setq flyspell-large-region-buffer curbuf)
1511 (setq flyspell-large-region-beg beg)
1512 (setq flyspell-large-region-end end)
1513 (flyspell-accept-buffer-local-defs)
1514 (set-buffer buffer)
1515 (erase-buffer)
1516 ;; this is done, we can start checking...
1517 (if flyspell-issue-message-flag (message "Checking region..."))
1518 (set-buffer curbuf)
1519 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1520 ;; Local dictionary becomes the global dictionary in use.
1521 (setq ispell-current-dictionary
1522 (or ispell-local-dictionary ispell-dictionary))
1523 (setq ispell-current-personal-dictionary
1524 (or ispell-local-pdict ispell-personal-dictionary))
1525 (let ((args (ispell-get-ispell-args))
1526 (encoding (ispell-get-coding-system))
1527 c)
1528 (if (and ispell-current-dictionary ; use specified dictionary
1529 (not (member "-d" args))) ; only define if not overridden
1530 (setq args
1531 (append (list "-d" ispell-current-dictionary) args)))
1532 (if ispell-current-personal-dictionary ; use specified pers dict
1533 (setq args
1534 (append args
1535 (list "-p"
1536 (expand-file-name
1537 ispell-current-personal-dictionary)))))
1538 (setq args (append args ispell-extra-args))
1539
1540 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1541 ;; for communication. ispell or older aspell/hunspell does not support this
1542 (if ispell-encoding8-command
1543 (setq args
1544 (append args
1545 (list
1546 (concat ispell-encoding8-command
1547 (symbol-name
1548 encoding))))))
1549
1550 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1551 (setq c (apply 'ispell-call-process-region beg
1552 end
1553 ispell-program-name
1554 nil
1555 buffer
1556 nil
1557 (if ispell-really-aspell "list" "-l")
1558 args)))
1559 (if (eq c 0)
1560 (progn
1561 (flyspell-process-localwords buffer)
1562 (with-current-buffer curbuf
1563 (flyspell-delete-region-overlays beg end)
1564 (flyspell-check-region-doublons beg end))
1565 (flyspell-external-point-words))
1566 (error "Can't check region")))))
1567
1568 ;;*---------------------------------------------------------------------*/
1569 ;;* flyspell-region ... */
1570 ;;* ------------------------------------------------------------- */
1571 ;;* Because `ispell -a' is too slow, it is not possible to use */
1572 ;;* it on large region. Then, when ispell is invoked on a large */
1573 ;;* text region, a new `ispell -l' process is spawned. The */
1574 ;;* pointed out words are then searched in the region a checked with */
1575 ;;* regular flyspell means. */
1576 ;;*---------------------------------------------------------------------*/
1577 ;;;###autoload
1578 (defun flyspell-region (beg end)
1579 "Flyspell text between BEG and END."
1580 (interactive "r")
1581 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1582 (if (= beg end)
1583 ()
1584 (save-excursion
1585 (if (> beg end)
1586 (let ((old beg))
1587 (setq beg end)
1588 (setq end old)))
1589 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1590 (flyspell-large-region beg end)
1591 (flyspell-small-region beg end)))))
1592
1593 ;;*---------------------------------------------------------------------*/
1594 ;;* flyspell-buffer ... */
1595 ;;*---------------------------------------------------------------------*/
1596 ;;;###autoload
1597 (defun flyspell-buffer ()
1598 "Flyspell whole buffer."
1599 (interactive)
1600 (flyspell-region (point-min) (point-max)))
1601
1602 ;;*---------------------------------------------------------------------*/
1603 ;;* old next error position ... */
1604 ;;*---------------------------------------------------------------------*/
1605 (defvar flyspell-old-buffer-error nil)
1606 (defvar flyspell-old-pos-error nil)
1607
1608 ;;*---------------------------------------------------------------------*/
1609 ;;* flyspell-goto-next-error ... */
1610 ;;*---------------------------------------------------------------------*/
1611 (defun flyspell-goto-next-error ()
1612 "Go to the next previously detected error.
1613 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1614 FLYSPELL-BUFFER."
1615 (interactive)
1616 (let ((pos (point))
1617 (max (point-max)))
1618 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1619 (eq pos flyspell-old-pos-error))
1620 (progn
1621 (if (= flyspell-old-pos-error max)
1622 ;; goto beginning of buffer
1623 (progn
1624 (message "Restarting from beginning of buffer")
1625 (goto-char (point-min)))
1626 (forward-word 1))
1627 (setq pos (point))))
1628 ;; seek the next error
1629 (while (and (< pos max)
1630 (let ((ovs (overlays-at pos))
1631 (r '()))
1632 (while (and (not r) (consp ovs))
1633 (if (flyspell-overlay-p (car ovs))
1634 (setq r t)
1635 (setq ovs (cdr ovs))))
1636 (not r)))
1637 (setq pos (1+ pos)))
1638 ;; save the current location for next invocation
1639 (setq flyspell-old-pos-error pos)
1640 (setq flyspell-old-buffer-error (current-buffer))
1641 (goto-char pos)
1642 (if (= pos max)
1643 (message "No more miss-spelled word!"))))
1644
1645 ;;*---------------------------------------------------------------------*/
1646 ;;* flyspell-overlay-p ... */
1647 ;;*---------------------------------------------------------------------*/
1648 (defun flyspell-overlay-p (o)
1649 "Return true if O is an overlay used by flyspell."
1650 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1651
1652 ;;*---------------------------------------------------------------------*/
1653 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1654 ;;* ------------------------------------------------------------- */
1655 ;;* Remove overlays introduced by flyspell. */
1656 ;;*---------------------------------------------------------------------*/
1657 (defun flyspell-delete-region-overlays (beg end)
1658 "Delete overlays used by flyspell in a given region."
1659 (remove-overlays beg end 'flyspell-overlay t))
1660
1661
1662 (defun flyspell-delete-all-overlays ()
1663 "Delete all the overlays used by flyspell."
1664 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1665
1666 ;;*---------------------------------------------------------------------*/
1667 ;;* flyspell-unhighlight-at ... */
1668 ;;*---------------------------------------------------------------------*/
1669 (defun flyspell-unhighlight-at (pos)
1670 "Remove the flyspell overlay that are located at POS."
1671 (if flyspell-persistent-highlight
1672 (let ((overlays (overlays-at pos)))
1673 (while (consp overlays)
1674 (if (flyspell-overlay-p (car overlays))
1675 (delete-overlay (car overlays)))
1676 (setq overlays (cdr overlays))))
1677 (if (flyspell-overlay-p flyspell-overlay)
1678 (delete-overlay flyspell-overlay))))
1679
1680 ;;*---------------------------------------------------------------------*/
1681 ;;* flyspell-properties-at-p ... */
1682 ;;* ------------------------------------------------------------- */
1683 ;;* Is there an highlight properties at position pos? */
1684 ;;*---------------------------------------------------------------------*/
1685 (defun flyspell-properties-at-p (pos)
1686 "Return t if there is a text property at POS, not counting `local-map'.
1687 If variable `flyspell-highlight-properties' is set to nil,
1688 text with properties are not checked. This function is used to discover
1689 if the character at POS has any other property."
1690 (let ((prop (text-properties-at pos))
1691 (keep t))
1692 (while (and keep (consp prop))
1693 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1694 (setq prop (cdr (cdr prop)))
1695 (setq keep nil)))
1696 (consp prop)))
1697
1698 ;;*---------------------------------------------------------------------*/
1699 ;;* make-flyspell-overlay ... */
1700 ;;*---------------------------------------------------------------------*/
1701 (defun make-flyspell-overlay (beg end face mouse-face)
1702 "Allocate an overlay to highlight an incorrect word.
1703 BEG and END specify the range in the buffer of that word.
1704 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1705 for the overlay."
1706 (let ((overlay (make-overlay beg end nil t nil)))
1707 (overlay-put overlay 'face face)
1708 (overlay-put overlay 'mouse-face mouse-face)
1709 (overlay-put overlay 'flyspell-overlay t)
1710 (overlay-put overlay 'evaporate t)
1711 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1712 (overlay-put overlay 'keymap flyspell-mouse-map)
1713 (when (eq face 'flyspell-incorrect)
1714 (and (stringp flyspell-before-incorrect-word-string)
1715 (overlay-put overlay 'before-string
1716 flyspell-before-incorrect-word-string))
1717 (and (stringp flyspell-after-incorrect-word-string)
1718 (overlay-put overlay 'after-string
1719 flyspell-after-incorrect-word-string)))
1720 overlay))
1721
1722 ;;*---------------------------------------------------------------------*/
1723 ;;* flyspell-highlight-incorrect-region ... */
1724 ;;*---------------------------------------------------------------------*/
1725 (defun flyspell-highlight-incorrect-region (beg end poss)
1726 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1727 POSS is usually a list of possible spelling/correction lists,
1728 as returned by `ispell-parse-output'.
1729 It can also be the symbol `doublon', in the case where the word
1730 is itself incorrect, but suspiciously repeated."
1731 (let ((inhibit-read-only t))
1732 (unless (run-hook-with-args-until-success
1733 'flyspell-incorrect-hook beg end poss)
1734 (if (or flyspell-highlight-properties
1735 (not (flyspell-properties-at-p beg)))
1736 (progn
1737 ;; we cleanup all the overlay that are in the region, not
1738 ;; beginning at the word start position
1739 (if (< (1+ beg) end)
1740 (let ((os (overlays-in (1+ beg) end)))
1741 (while (consp os)
1742 (if (flyspell-overlay-p (car os))
1743 (delete-overlay (car os)))
1744 (setq os (cdr os)))))
1745 ;; we cleanup current overlay at the same position
1746 (flyspell-unhighlight-at beg)
1747 ;; now we can use a new overlay
1748 (setq flyspell-overlay
1749 (make-flyspell-overlay
1750 beg end
1751 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1752 'highlight)))))))
1753
1754 ;;*---------------------------------------------------------------------*/
1755 ;;* flyspell-highlight-duplicate-region ... */
1756 ;;*---------------------------------------------------------------------*/
1757 (defun flyspell-highlight-duplicate-region (beg end poss)
1758 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1759 POSS is a list of possible spelling/correction lists,
1760 as returned by `ispell-parse-output'."
1761 (let ((inhibit-read-only t))
1762 (unless (run-hook-with-args-until-success
1763 'flyspell-incorrect-hook beg end poss)
1764 (if (or flyspell-highlight-properties
1765 (not (flyspell-properties-at-p beg)))
1766 (progn
1767 ;; we cleanup current overlay at the same position
1768 (flyspell-unhighlight-at beg)
1769 ;; now we can use a new overlay
1770 (setq flyspell-overlay
1771 (make-flyspell-overlay beg end
1772 'flyspell-duplicate
1773 'highlight)))))))
1774
1775 ;;*---------------------------------------------------------------------*/
1776 ;;* flyspell-auto-correct-cache ... */
1777 ;;*---------------------------------------------------------------------*/
1778 (defvar flyspell-auto-correct-pos nil)
1779 (defvar flyspell-auto-correct-region nil)
1780 (defvar flyspell-auto-correct-ring nil)
1781 (defvar flyspell-auto-correct-word nil)
1782 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1783 (make-variable-buffer-local 'flyspell-auto-correct-region)
1784 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1785 (make-variable-buffer-local 'flyspell-auto-correct-word)
1786
1787 ;;*---------------------------------------------------------------------*/
1788 ;;* flyspell-check-previous-highlighted-word ... */
1789 ;;*---------------------------------------------------------------------*/
1790 (defun flyspell-check-previous-highlighted-word (&optional arg)
1791 "Correct the closer misspelled word.
1792 This function scans a mis-spelled word before the cursor. If it finds one
1793 it proposes replacement for that word. With prefix arg, count that many
1794 misspelled words backwards."
1795 (interactive)
1796 (let ((pos1 (point))
1797 (pos (point))
1798 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1799 ov ovs)
1800 (if (catch 'exit
1801 (while (and (setq pos (previous-overlay-change pos))
1802 (not (= pos pos1)))
1803 (setq pos1 pos)
1804 (if (> pos (point-min))
1805 (progn
1806 (setq ovs (overlays-at (1- pos)))
1807 (while (consp ovs)
1808 (setq ov (car ovs))
1809 (setq ovs (cdr ovs))
1810 (if (and (flyspell-overlay-p ov)
1811 (= 0 (setq arg (1- arg))))
1812 (throw 'exit t)))))))
1813 (save-excursion
1814 (goto-char pos)
1815 (ispell-word))
1816 (error "No word to correct before point"))))
1817
1818 ;;*---------------------------------------------------------------------*/
1819 ;;* flyspell-display-next-corrections ... */
1820 ;;*---------------------------------------------------------------------*/
1821 (defun flyspell-display-next-corrections (corrections)
1822 (let ((string "Corrections:")
1823 (l corrections)
1824 (pos '()))
1825 (while (< (length string) 80)
1826 (if (equal (car l) flyspell-auto-correct-word)
1827 (setq pos (cons (+ 1 (length string)) pos)))
1828 (setq string (concat string " " (car l)))
1829 (setq l (cdr l)))
1830 (while (consp pos)
1831 (let ((num (car pos)))
1832 (put-text-property num
1833 (+ num (length flyspell-auto-correct-word))
1834 'face 'flyspell-incorrect
1835 string))
1836 (setq pos (cdr pos)))
1837 (if (fboundp 'display-message)
1838 (display-message 'no-log string)
1839 (message "%s" string))))
1840
1841 ;;*---------------------------------------------------------------------*/
1842 ;;* flyspell-abbrev-table ... */
1843 ;;*---------------------------------------------------------------------*/
1844 (defun flyspell-abbrev-table ()
1845 (if flyspell-use-global-abbrev-table-p
1846 global-abbrev-table
1847 (or local-abbrev-table global-abbrev-table)))
1848
1849 ;;*---------------------------------------------------------------------*/
1850 ;;* flyspell-define-abbrev ... */
1851 ;;*---------------------------------------------------------------------*/
1852 (defun flyspell-define-abbrev (name expansion)
1853 (let ((table (flyspell-abbrev-table)))
1854 (when table
1855 (define-abbrev table (downcase name) expansion))))
1856
1857 ;;*---------------------------------------------------------------------*/
1858 ;;* flyspell-auto-correct-word ... */
1859 ;;*---------------------------------------------------------------------*/
1860 (defun flyspell-auto-correct-word ()
1861 "Correct the current word.
1862 This command proposes various successive corrections for the current word."
1863 (interactive)
1864 (let ((pos (point))
1865 (old-max (point-max)))
1866 ;; use the correct dictionary
1867 (flyspell-accept-buffer-local-defs)
1868 (if (and (eq flyspell-auto-correct-pos pos)
1869 (consp flyspell-auto-correct-region))
1870 ;; we have already been using the function at the same location
1871 (let* ((start (car flyspell-auto-correct-region))
1872 (len (cdr flyspell-auto-correct-region)))
1873 (flyspell-unhighlight-at start)
1874 (delete-region start (+ start len))
1875 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1876 (let* ((word (car flyspell-auto-correct-ring))
1877 (len (length word)))
1878 (rplacd flyspell-auto-correct-region len)
1879 (goto-char start)
1880 (if flyspell-abbrev-p
1881 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1882 flyspell-auto-correct-word)
1883 (flyspell-change-abbrev (flyspell-abbrev-table)
1884 flyspell-auto-correct-word
1885 word)
1886 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1887 (funcall flyspell-insert-function word)
1888 (flyspell-word)
1889 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1890 (flyspell-ajust-cursor-point pos (point) old-max)
1891 (setq flyspell-auto-correct-pos (point)))
1892 ;; fetch the word to be checked
1893 (let ((word (flyspell-get-word)))
1894 (if (consp word)
1895 (let ((start (car (cdr word)))
1896 (end (car (cdr (cdr word))))
1897 (word (car word))
1898 poss ispell-filter)
1899 (setq flyspell-auto-correct-word word)
1900 ;; now check spelling of word.
1901 (ispell-send-string "%\n") ;put in verbose mode
1902 (ispell-send-string (concat "^" word "\n"))
1903 ;; wait until ispell has processed word.
1904 (while (progn
1905 (accept-process-output ispell-process)
1906 (not (string= "" (car ispell-filter)))))
1907 ;; Remove leading empty element
1908 (setq ispell-filter (cdr ispell-filter))
1909 ;; ispell process should return something after word is sent.
1910 ;; Tag word as valid (i.e., skip) otherwise
1911 (or ispell-filter
1912 (setq ispell-filter '(*)))
1913 (if (consp ispell-filter)
1914 (setq poss (ispell-parse-output (car ispell-filter))))
1915 (cond
1916 ((or (eq poss t) (stringp poss))
1917 ;; don't correct word
1918 t)
1919 ((null poss)
1920 ;; ispell error
1921 (error "Ispell: error in Ispell process"))
1922 (t
1923 ;; the word is incorrect, we have to propose a replacement
1924 (let ((replacements (if flyspell-sort-corrections
1925 (sort (car (cdr (cdr poss))) 'string<)
1926 (car (cdr (cdr poss))))))
1927 (setq flyspell-auto-correct-region nil)
1928 (if (consp replacements)
1929 (progn
1930 (let ((replace (car replacements)))
1931 (let ((new-word replace))
1932 (if (not (equal new-word (car poss)))
1933 (progn
1934 ;; the save the current replacements
1935 (setq flyspell-auto-correct-region
1936 (cons start (length new-word)))
1937 (let ((l replacements))
1938 (while (consp (cdr l))
1939 (setq l (cdr l)))
1940 (rplacd l (cons (car poss) replacements)))
1941 (setq flyspell-auto-correct-ring
1942 replacements)
1943 (flyspell-unhighlight-at start)
1944 (delete-region start end)
1945 (funcall flyspell-insert-function new-word)
1946 (if flyspell-abbrev-p
1947 (if (flyspell-already-abbrevp
1948 (flyspell-abbrev-table) word)
1949 (flyspell-change-abbrev
1950 (flyspell-abbrev-table)
1951 word
1952 new-word)
1953 (flyspell-define-abbrev word
1954 new-word)))
1955 (flyspell-word)
1956 (flyspell-display-next-corrections
1957 (cons new-word flyspell-auto-correct-ring))
1958 (flyspell-ajust-cursor-point pos
1959 (point)
1960 old-max))))))))))
1961 (setq flyspell-auto-correct-pos (point))
1962 (ispell-pdict-save t)))))))
1963
1964 ;;*---------------------------------------------------------------------*/
1965 ;;* flyspell-auto-correct-previous-pos ... */
1966 ;;*---------------------------------------------------------------------*/
1967 (defvar flyspell-auto-correct-previous-pos nil
1968 "Holds the start of the first incorrect word before point.")
1969
1970 ;;*---------------------------------------------------------------------*/
1971 ;;* flyspell-auto-correct-previous-hook ... */
1972 ;;*---------------------------------------------------------------------*/
1973 (defun flyspell-auto-correct-previous-hook ()
1974 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1975 Sets `flyspell-auto-correct-previous-pos' to nil"
1976 (interactive)
1977 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1978 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1979 (setq flyspell-auto-correct-previous-pos nil)))
1980
1981 ;;*---------------------------------------------------------------------*/
1982 ;;* flyspell-auto-correct-previous-word ... */
1983 ;;*---------------------------------------------------------------------*/
1984 (defun flyspell-auto-correct-previous-word (position)
1985 "Auto correct the first misspelled word that occurs before point.
1986 But don't look beyond what's visible on the screen."
1987 (interactive "d")
1988
1989 (let ((top (window-start))
1990 (bot (window-end)))
1991 (save-excursion
1992 (save-restriction
1993 (narrow-to-region top bot)
1994 (overlay-recenter (point))
1995
1996 (add-hook 'pre-command-hook
1997 (function flyspell-auto-correct-previous-hook) t t)
1998
1999 (unless flyspell-auto-correct-previous-pos
2000 ;; only reset if a new overlay exists
2001 (setq flyspell-auto-correct-previous-pos nil)
2002
2003 (let ((overlay-list (overlays-in (point-min) position))
2004 (new-overlay 'dummy-value))
2005
2006 ;; search for previous (new) flyspell overlay
2007 (while (and new-overlay
2008 (or (not (flyspell-overlay-p new-overlay))
2009 ;; check if its face has changed
2010 (not (eq (get-char-property
2011 (overlay-start new-overlay) 'face)
2012 'flyspell-incorrect))))
2013 (setq new-overlay (car-safe overlay-list))
2014 (setq overlay-list (cdr-safe overlay-list)))
2015
2016 ;; if nothing new exits new-overlay should be nil
2017 (if new-overlay ;; the length of the word may change so go to the start
2018 (setq flyspell-auto-correct-previous-pos
2019 (overlay-start new-overlay)))))
2020
2021 (when flyspell-auto-correct-previous-pos
2022 (save-excursion
2023 (goto-char flyspell-auto-correct-previous-pos)
2024 (let ((ispell-following-word t)) ;; point is at start
2025 (if (numberp flyspell-auto-correct-previous-pos)
2026 (goto-char flyspell-auto-correct-previous-pos))
2027 (flyspell-auto-correct-word))
2028 ;; the point may have moved so reset this
2029 (setq flyspell-auto-correct-previous-pos (point))))))))
2030
2031 ;;*---------------------------------------------------------------------*/
2032 ;;* flyspell-correct-word ... */
2033 ;;*---------------------------------------------------------------------*/
2034
2035 (defun flyspell-correct-word (event)
2036 "Pop up a menu of possible corrections for a misspelled word.
2037 The word checked is the word at the mouse position."
2038 (interactive "e")
2039 (let ((save (point)))
2040 (mouse-set-point event)
2041 (flyspell-correct-word-before-point event save)))
2042
2043 (defun flyspell-correct-word-before-point (&optional event opoint)
2044 "Pop up a menu of possible corrections for misspelled word before point.
2045 If EVENT is non-nil, it is the mouse event that invoked this operation;
2046 that controls where to put the menu.
2047 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2048 (interactive)
2049 (unless (mouse-position)
2050 (error "Pop-up menus do not work on this terminal"))
2051 ;; use the correct dictionary
2052 (flyspell-accept-buffer-local-defs)
2053 (or opoint (setq opoint (point)))
2054 (let ((cursor-location (point))
2055 (word (flyspell-get-word)))
2056 (if (consp word)
2057 (let ((start (car (cdr word)))
2058 (end (car (cdr (cdr word))))
2059 (word (car word))
2060 poss ispell-filter)
2061 ;; now check spelling of word.
2062 (ispell-send-string "%\n") ;put in verbose mode
2063 (ispell-send-string (concat "^" word "\n"))
2064 ;; wait until ispell has processed word
2065 (while (progn
2066 (accept-process-output ispell-process)
2067 (not (string= "" (car ispell-filter)))))
2068 ;; Remove leading empty element
2069 (setq ispell-filter (cdr ispell-filter))
2070 ;; ispell process should return something after word is sent.
2071 ;; Tag word as valid (i.e., skip) otherwise
2072 (or ispell-filter
2073 (setq ispell-filter '(*)))
2074 (if (consp ispell-filter)
2075 (setq poss (ispell-parse-output (car ispell-filter))))
2076 (cond
2077 ((or (eq poss t) (stringp poss))
2078 ;; don't correct word
2079 t)
2080 ((null poss)
2081 ;; ispell error
2082 (error "Ispell: error in Ispell process"))
2083 ((featurep 'xemacs)
2084 (flyspell-xemacs-popup
2085 poss word cursor-location start end opoint))
2086 (t
2087 ;; The word is incorrect, we have to propose a replacement.
2088 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2089 poss word cursor-location start end opoint)))
2090 (ispell-pdict-save t)))))
2091
2092 ;;*---------------------------------------------------------------------*/
2093 ;;* flyspell-do-correct ... */
2094 ;;*---------------------------------------------------------------------*/
2095 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2096 "The popup menu callback."
2097 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2098 ;; it instead right after calling the function.
2099 (cond ((eq replace 'ignore)
2100 (goto-char save)
2101 nil)
2102 ((eq replace 'save)
2103 (goto-char save)
2104 (ispell-send-string (concat "*" word "\n"))
2105 ;; This was added only to the XEmacs side in revision 1.18 of
2106 ;; flyspell. I assume its absence on the Emacs side was an
2107 ;; oversight. --Stef
2108 (ispell-send-string "#\n")
2109 (flyspell-unhighlight-at cursor-location)
2110 (setq ispell-pdict-modified-p '(t)))
2111 ((or (eq replace 'buffer) (eq replace 'session))
2112 (ispell-send-string (concat "@" word "\n"))
2113 (flyspell-unhighlight-at cursor-location)
2114 (if (null ispell-pdict-modified-p)
2115 (setq ispell-pdict-modified-p
2116 (list ispell-pdict-modified-p)))
2117 (goto-char save)
2118 (if (eq replace 'buffer)
2119 (ispell-add-per-file-word-list word)))
2120 (replace
2121 ;; This was added only to the Emacs side. I assume its absence on
2122 ;; the XEmacs side was an oversight. --Stef
2123 (flyspell-unhighlight-at cursor-location)
2124 (let ((old-max (point-max))
2125 (new-word (if (atom replace)
2126 replace
2127 (car replace)))
2128 (cursor-location (+ (- (length word) (- end start))
2129 cursor-location)))
2130 (unless (equal new-word (car poss))
2131 (delete-region start end)
2132 (goto-char start)
2133 (funcall flyspell-insert-function new-word)
2134 (if flyspell-abbrev-p
2135 (flyspell-define-abbrev word new-word)))
2136 ;; In the original Emacs code, this was only called in the body
2137 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2138 (flyspell-ajust-cursor-point save cursor-location old-max)))
2139 (t
2140 (goto-char save)
2141 nil)))
2142
2143 ;;*---------------------------------------------------------------------*/
2144 ;;* flyspell-ajust-cursor-point ... */
2145 ;;*---------------------------------------------------------------------*/
2146 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2147 (if (>= save cursor-location)
2148 (let ((new-pos (+ save (- (point-max) old-max))))
2149 (goto-char (cond
2150 ((< new-pos (point-min))
2151 (point-min))
2152 ((> new-pos (point-max))
2153 (point-max))
2154 (t new-pos))))
2155 (goto-char save)))
2156
2157 ;;*---------------------------------------------------------------------*/
2158 ;;* flyspell-emacs-popup ... */
2159 ;;*---------------------------------------------------------------------*/
2160 (defun flyspell-emacs-popup (event poss word)
2161 "The Emacs popup menu."
2162 (unless window-system
2163 (error "This command requires pop-up dialogs"))
2164 (if (not event)
2165 (let* ((mouse-pos (mouse-position))
2166 (mouse-pos (if (nth 1 mouse-pos)
2167 mouse-pos
2168 (set-mouse-position (car mouse-pos)
2169 (/ (frame-width) 2) 2)
2170 (mouse-position))))
2171 (setq event (list (list (car (cdr mouse-pos))
2172 (1+ (cdr (cdr mouse-pos))))
2173 (car mouse-pos)))))
2174 (let* ((corrects (if flyspell-sort-corrections
2175 (sort (car (cdr (cdr poss))) 'string<)
2176 (car (cdr (cdr poss)))))
2177 (cor-menu (if (consp corrects)
2178 (mapcar (lambda (correct)
2179 (list correct correct))
2180 corrects)
2181 '()))
2182 (affix (car (cdr (cdr (cdr poss)))))
2183 show-affix-info
2184 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2185 (list
2186 (list (concat "Save affix: " (car affix))
2187 'save)
2188 '("Accept (session)" session)
2189 '("Accept (buffer)" buffer))
2190 '(("Save word" save)
2191 ("Accept (session)" session)
2192 ("Accept (buffer)" buffer)))))
2193 (if (consp cor-menu)
2194 (append cor-menu (cons "" save))
2195 save)))
2196 (menu (cons "flyspell correction menu" base-menu)))
2197 (car (x-popup-menu event
2198 (list (format "%s [%s]" word (or ispell-local-dictionary
2199 ispell-dictionary))
2200 menu)))))
2201
2202 ;;*---------------------------------------------------------------------*/
2203 ;;* flyspell-xemacs-popup ... */
2204 ;;*---------------------------------------------------------------------*/
2205 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2206 "The XEmacs popup menu."
2207 (let* ((corrects (if flyspell-sort-corrections
2208 (sort (car (cdr (cdr poss))) 'string<)
2209 (car (cdr (cdr poss)))))
2210 (cor-menu (if (consp corrects)
2211 (mapcar (lambda (correct)
2212 (vector correct
2213 (list 'flyspell-do-correct
2214 correct
2215 (list 'quote poss)
2216 word
2217 cursor-location
2218 start
2219 end
2220 save)
2221 t))
2222 corrects)
2223 '()))
2224 (affix (car (cdr (cdr (cdr poss)))))
2225 show-affix-info
2226 (menu (let ((save (if (and (consp affix) show-affix-info)
2227 (vector
2228 (concat "Save affix: " (car affix))
2229 (list 'flyspell-do-correct
2230 ''save
2231 (list 'quote poss)
2232 word
2233 cursor-location
2234 start
2235 end
2236 save)
2237 t)
2238 (vector
2239 "Save word"
2240 (list 'flyspell-do-correct
2241 ''save
2242 (list 'quote poss)
2243 word
2244 cursor-location
2245 start
2246 end
2247 save)
2248 t)))
2249 (session (vector "Accept (session)"
2250 (list 'flyspell-do-correct
2251 ''session
2252 (list 'quote poss)
2253 word
2254 cursor-location
2255 start
2256 end
2257 save)
2258 t))
2259 (buffer (vector "Accept (buffer)"
2260 (list 'flyspell-do-correct
2261 ''buffer
2262 (list 'quote poss)
2263 word
2264 cursor-location
2265 start
2266 end
2267 save)
2268 t)))
2269 (if (consp cor-menu)
2270 (append cor-menu (list "-" save session buffer))
2271 (list save session buffer)))))
2272 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2273 ispell-dictionary))
2274 menu))))
2275
2276 ;;*---------------------------------------------------------------------*/
2277 ;;* Some example functions for real autocorrecting */
2278 ;;*---------------------------------------------------------------------*/
2279 (defun flyspell-maybe-correct-transposition (beg end poss)
2280 "Check replacements for transposed characters.
2281
2282 If the text between BEG and END is equal to a correction suggested by
2283 Ispell, after transposing two adjacent characters, correct the text,
2284 and return t.
2285
2286 The third arg POSS is either the symbol 'doublon' or a list of
2287 possible corrections as returned by `ispell-parse-output'.
2288
2289 This function is meant to be added to `flyspell-incorrect-hook'."
2290 (when (consp poss)
2291 (catch 'done
2292 (let ((str (buffer-substring beg end))
2293 (i 0) (len (- end beg)) tmp)
2294 (while (< (1+ i) len)
2295 (setq tmp (aref str i))
2296 (aset str i (aref str (1+ i)))
2297 (aset str (1+ i) tmp)
2298 (when (member str (nth 2 poss))
2299 (save-excursion
2300 (goto-char (+ beg i 1))
2301 (transpose-chars 1))
2302 (throw 'done t))
2303 (setq tmp (aref str i))
2304 (aset str i (aref str (1+ i)))
2305 (aset str (1+ i) tmp)
2306 (setq i (1+ i))))
2307 nil)))
2308
2309 (defun flyspell-maybe-correct-doubling (beg end poss)
2310 "Check replacements for doubled characters.
2311
2312 If the text between BEG and END is equal to a correction suggested by
2313 Ispell, after removing a pair of doubled characters, correct the text,
2314 and return t.
2315
2316 The third arg POSS is either the symbol 'doublon' or a list of
2317 possible corrections as returned by `ispell-parse-output'.
2318
2319 This function is meant to be added to `flyspell-incorrect-hook'."
2320 (when (consp poss)
2321 (catch 'done
2322 (let ((str (buffer-substring beg end))
2323 (i 0) (len (- end beg)))
2324 (while (< (1+ i) len)
2325 (when (and (= (aref str i) (aref str (1+ i)))
2326 (member (concat (substring str 0 (1+ i))
2327 (substring str (+ i 2)))
2328 (nth 2 poss)))
2329 (goto-char (+ beg i))
2330 (delete-char 1)
2331 (throw 'done t))
2332 (setq i (1+ i))))
2333 nil)))
2334
2335 ;;*---------------------------------------------------------------------*/
2336 ;;* flyspell-already-abbrevp ... */
2337 ;;*---------------------------------------------------------------------*/
2338 (defun flyspell-already-abbrevp (table word)
2339 (let ((sym (abbrev-symbol word table)))
2340 (and sym (symbolp sym))))
2341
2342 ;;*---------------------------------------------------------------------*/
2343 ;;* flyspell-change-abbrev ... */
2344 ;;*---------------------------------------------------------------------*/
2345 (defun flyspell-change-abbrev (table old new)
2346 (set (abbrev-symbol old table) new))
2347
2348 (provide 'flyspell)
2349
2350 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2351 ;;; flyspell.el ends here