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