Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / 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, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
7 ;; Maintainer: FSF
8 ;; Keywords: convenience
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26 ;;
27 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
28 ;; checking.
29 ;;
30 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
31 ;; This applies only to the current buffer.
32 ;;
33 ;; To enable Flyspell in text representing computer programs, type
34 ;; M-x flyspell-prog-mode.
35 ;; In that mode only text inside comments is checked.
36 ;;
37 ;; Note: consider setting the variable ispell-parser to `tex' to
38 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
39 ;;
40 ;; Some user variables control the behavior of flyspell. They are
41 ;; those defined under the `User variables' comment.
42
43 ;;; Code:
44
45 (require 'ispell)
46
47 ;;*---------------------------------------------------------------------*/
48 ;;* Group ... */
49 ;;*---------------------------------------------------------------------*/
50 (defgroup flyspell nil
51 "Spell checking on the fly."
52 :tag "FlySpell"
53 :prefix "flyspell-"
54 :group 'ispell
55 :group 'processes)
56
57 ;;*---------------------------------------------------------------------*/
58 ;;* User configuration ... */
59 ;;*---------------------------------------------------------------------*/
60 (defcustom flyspell-highlight-flag t
61 "How Flyspell should indicate misspelled words.
62 Non-nil means use highlight, nil means use minibuffer messages."
63 :group 'flyspell
64 :type 'boolean)
65
66 (defcustom flyspell-mark-duplications-flag t
67 "Non-nil means Flyspell reports a repeated word as an error.
68 See `flyspell-mark-duplications-exceptions' to add exceptions to this rule.
69 Detection of repeated words is not implemented in
70 \"large\" regions; see `flyspell-large-region'."
71 :group 'flyspell
72 :type 'boolean)
73
74 (defcustom flyspell-mark-duplications-exceptions
75 '(("francais" . ("nous" "vous")))
76 "A list of exceptions for duplicated words.
77 It should be a list of (LANGUAGE . EXCEPTION-LIST). LANGUAGE is matched
78 against the current dictionary and EXCEPTION-LIST is a list of strings.
79 The duplicated word is downcased before it is compared with the exceptions."
80 :group 'flyspell
81 :type '(alist :key-type string :value-type (repeat string)))
82
83 (defcustom flyspell-sort-corrections nil
84 "Non-nil means, sort the corrections alphabetically before popping them."
85 :group 'flyspell
86 :version "21.1"
87 :type 'boolean)
88
89 (defcustom flyspell-duplicate-distance -1
90 "The maximum distance for finding duplicates of unrecognized words.
91 This applies to the feature that when a word is not found in the dictionary,
92 if the same spelling occurs elsewhere in the buffer,
93 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
94 This variable specifies how far to search to find such a duplicate.
95 -1 means no limit (search the whole buffer).
96 0 means do not search for duplicate unrecognized spellings."
97 :group 'flyspell
98 :version "21.1"
99 :type '(choice (const :tag "no limit" -1)
100 number))
101
102 (defcustom flyspell-delay 3
103 "The number of seconds to wait before checking, after a \"delayed\" command."
104 :group 'flyspell
105 :type 'number)
106
107 (defcustom flyspell-persistent-highlight t
108 "Non-nil means misspelled words remain highlighted until corrected.
109 If this variable is nil, only the most recently detected misspelled word
110 is highlighted."
111 :group 'flyspell
112 :type 'boolean)
113
114 (defcustom flyspell-highlight-properties t
115 "Non-nil means highlight incorrect words even if a property exists for this word."
116 :group 'flyspell
117 :type 'boolean)
118
119 (defcustom flyspell-default-delayed-commands
120 '(self-insert-command
121 delete-backward-char
122 backward-or-forward-delete-char
123 delete-char
124 scrollbar-vertical-drag
125 backward-delete-char-untabify)
126 "The standard list of delayed commands for Flyspell.
127 See `flyspell-delayed-commands'."
128 :group 'flyspell
129 :version "21.1"
130 :type '(repeat (symbol)))
131
132 (defcustom flyspell-delayed-commands nil
133 "List of commands that are \"delayed\" for Flyspell mode.
134 After these commands, Flyspell checking is delayed for a short time,
135 whose length is specified by `flyspell-delay'."
136 :group 'flyspell
137 :type '(repeat (symbol)))
138
139 (defcustom flyspell-default-deplacement-commands
140 '(next-line
141 previous-line
142 scroll-up
143 scroll-down)
144 "The standard list of deplacement commands for Flyspell.
145 See `flyspell-deplacement-commands'."
146 :group 'flyspell
147 :version "21.1"
148 :type '(repeat (symbol)))
149
150 (defcustom flyspell-deplacement-commands nil
151 "List of commands that are \"deplacement\" for Flyspell mode.
152 After these commands, Flyspell checking is performed only if the previous
153 command was not the very same command."
154 :group 'flyspell
155 :version "21.1"
156 :type '(repeat (symbol)))
157
158 (defcustom flyspell-issue-welcome-flag t
159 "Non-nil means that Flyspell should display a welcome message when started."
160 :group 'flyspell
161 :type 'boolean)
162
163 (defcustom flyspell-issue-message-flag t
164 "Non-nil means that Flyspell emits messages when checking words."
165 :group 'flyspell
166 :type 'boolean)
167
168 (defcustom flyspell-incorrect-hook nil
169 "List of functions to be called when incorrect words are encountered.
170 Each function is given three arguments. The first two
171 arguments are the beginning and the end of the incorrect region.
172 The third is either the symbol `doublon' or the list
173 of possible corrections as returned by `ispell-parse-output'.
174
175 If any of the functions return non-nil, the word is not highlighted as
176 incorrect."
177 :group 'flyspell
178 :version "21.1"
179 :type 'hook)
180
181 (defcustom flyspell-default-dictionary nil
182 "A string that is the name of the default dictionary.
183 This is passed to the `ispell-change-dictionary' when flyspell is started.
184 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
185 when flyspell is started, the value of that variable is used instead
186 of `flyspell-default-dictionary' to select the default dictionary.
187 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
188 Ispell's ultimate default dictionary."
189 :group 'flyspell
190 :version "21.1"
191 :type '(choice string (const :tag "Default" nil)))
192
193 (defcustom flyspell-tex-command-regexp
194 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
195 "A string that is the regular expression that matches TeX commands."
196 :group 'flyspell
197 :version "21.1"
198 :type 'string)
199
200 (defcustom flyspell-check-tex-math-command nil
201 "Non-nil means check even inside TeX math environment.
202 TeX math environments are discovered by the TEXMATHP that implemented
203 inside the texmathp.el Emacs package. That package may be found at:
204 http://strw.leidenuniv.nl/~dominik/Tools"
205 :group 'flyspell
206 :type 'boolean)
207
208 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
209 '("francais" "deutsch8" "norsk")
210 "List of dictionary names that consider `-' as word delimiter."
211 :group 'flyspell
212 :version "21.1"
213 :type '(repeat (string)))
214
215 (defcustom flyspell-abbrev-p
216 nil
217 "If non-nil, add correction to abbreviation table."
218 :group 'flyspell
219 :version "21.1"
220 :type 'boolean)
221
222 (defcustom flyspell-use-global-abbrev-table-p
223 nil
224 "If non-nil, prefer global abbrev table to local abbrev table."
225 :group 'flyspell
226 :version "21.1"
227 :type 'boolean)
228
229 (defcustom flyspell-mode-line-string " Fly"
230 "String displayed on the modeline when flyspell is active.
231 Set this to nil if you don't want a modeline indicator."
232 :group 'flyspell
233 :type '(choice string (const :tag "None" nil)))
234
235 (defcustom flyspell-large-region 1000
236 "The threshold that determines if a region is small.
237 If the region is smaller than this number of characters,
238 `flyspell-region' checks the words sequentially using regular
239 flyspell methods. Else, if the region is large, a new Ispell process is
240 spawned for speed.
241
242 Doubled words are not detected in a large region, because Ispell
243 does not check for them.
244
245 If `flyspell-large-region' is nil, all regions are treated as small."
246 :group 'flyspell
247 :version "21.1"
248 :type '(choice number (const :tag "All small" nil)))
249
250 (defcustom flyspell-insert-function (function insert)
251 "Function for inserting word by flyspell upon correction."
252 :group 'flyspell
253 :type 'function)
254
255 (defcustom flyspell-before-incorrect-word-string nil
256 "String used to indicate an incorrect word starting."
257 :group 'flyspell
258 :type '(choice string (const nil)))
259
260 (defcustom flyspell-after-incorrect-word-string nil
261 "String used to indicate an incorrect word ending."
262 :group 'flyspell
263 :type '(choice string (const nil)))
264
265 (defcustom flyspell-use-meta-tab t
266 "Non-nil means that flyspell uses M-TAB to correct word."
267 :group 'flyspell
268 :type 'boolean)
269
270 (defcustom flyspell-auto-correct-binding
271 [(control ?\;)]
272 "The key binding for flyspell auto correction."
273 :group 'flyspell)
274
275 ;;*---------------------------------------------------------------------*/
276 ;;* Mode specific options */
277 ;;* ------------------------------------------------------------- */
278 ;;* Mode specific options enable users to disable flyspell on */
279 ;;* certain word depending of the emacs mode. For instance, when */
280 ;;* using flyspell with mail-mode add the following expression */
281 ;;* in your .emacs file: */
282 ;;* (add-hook 'mail-mode */
283 ;;* '(lambda () (setq flyspell-generic-check-word-predicate */
284 ;;* 'mail-mode-flyspell-verify))) */
285 ;;*---------------------------------------------------------------------*/
286 (defvar flyspell-generic-check-word-predicate nil
287 "Function providing per-mode customization over which words are flyspelled.
288 Returns t to continue checking, nil otherwise.
289 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
290 property of the major mode name.")
291 (make-variable-buffer-local 'flyspell-generic-check-word-predicate)
292 (defvaralias 'flyspell-generic-check-word-p
293 'flyspell-generic-check-word-predicate)
294
295 ;;*--- mail mode -------------------------------------------------------*/
296 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
297 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
298 (defvar message-signature-separator)
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 (put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
352
353 (defun sgml-mode-flyspell-verify ()
354 "Function used for `flyspell-generic-check-word-predicate' in SGML mode."
355 (not (save-excursion
356 (let ((this (point-marker))
357 (s (progn (beginning-of-line) (point-marker)))
358 (e (progn (end-of-line) (point-marker))))
359 (or (progn
360 (goto-char this)
361 (and (re-search-forward "[^<]*>" e t)
362 (= (match-beginning 0) this)))
363 (progn
364 (goto-char this)
365 (and (re-search-backward "<[^>]*" s t)
366 (= (match-end 0) this)))
367 (and (progn
368 (goto-char this)
369 (and (re-search-forward "[^&]*;" e t)
370 (= (match-beginning 0) this)))
371 (progn
372 (goto-char this)
373 (and (re-search-backward "&[^;]*" s t)
374 (= (match-end 0) this)))))))))
375
376 ;;*---------------------------------------------------------------------*/
377 ;;* Programming mode */
378 ;;*---------------------------------------------------------------------*/
379 (defvar flyspell-prog-text-faces
380 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
381 "Faces corresponding to text in programming-mode buffers.")
382
383 (defun flyspell-generic-progmode-verify ()
384 "Used for `flyspell-generic-check-word-predicate' in programming modes."
385 (let ((f (get-text-property (point) 'face)))
386 (memq f flyspell-prog-text-faces)))
387
388 ;;;###autoload
389 (defun flyspell-prog-mode ()
390 "Turn on `flyspell-mode' for comments and strings."
391 (interactive)
392 (setq flyspell-generic-check-word-predicate
393 'flyspell-generic-progmode-verify)
394 (flyspell-mode 1)
395 (run-hooks 'flyspell-prog-mode-hook))
396
397 ;;*---------------------------------------------------------------------*/
398 ;;* Overlay compatibility */
399 ;;*---------------------------------------------------------------------*/
400 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
401 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
402 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
403 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
404 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
405 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
406 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
407 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
408
409 ;;*---------------------------------------------------------------------*/
410 ;;* The minor mode declaration. */
411 ;;*---------------------------------------------------------------------*/
412 (defvar flyspell-mouse-map
413 (let ((map (make-sparse-keymap)))
414 (define-key map (if (featurep 'xemacs) [button2] [down-mouse-2])
415 #'flyspell-correct-word)
416 map)
417 "Keymap for Flyspell to put on erroneous words.")
418
419 (defvar flyspell-mode-map
420 (let ((map (make-sparse-keymap)))
421 (if flyspell-use-meta-tab
422 (define-key map "\M-\t" 'flyspell-auto-correct-word))
423 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
424 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
425 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
426 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
427 map)
428 "Minor mode keymap for Flyspell mode--for the whole buffer.")
429
430 ;; dash character machinery
431 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
432 "*Non-nil means that the `-' char is considered as a word delimiter.")
433 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
434 (defvar flyspell-dash-dictionary nil)
435 (make-variable-buffer-local 'flyspell-dash-dictionary)
436 (defvar flyspell-dash-local-dictionary nil)
437 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
438
439 ;;*---------------------------------------------------------------------*/
440 ;;* Highlighting */
441 ;;*---------------------------------------------------------------------*/
442 (defface flyspell-incorrect
443 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
444 (t (:bold t)))
445 "Face used for marking a misspelled word in Flyspell."
446 :group 'flyspell)
447 ;; backward-compatibility alias
448 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
449
450 (defface flyspell-duplicate
451 '((((class color)) (:foreground "Gold3" :bold t :underline t))
452 (t (:bold t)))
453 "Face used for marking a misspelled word that appears twice in the buffer.
454 See also `flyspell-duplicate-distance'."
455 :group 'flyspell)
456 ;; backward-compatibility alias
457 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
458
459 (defvar flyspell-overlay nil)
460
461 ;;*---------------------------------------------------------------------*/
462 ;;* flyspell-mode ... */
463 ;;*---------------------------------------------------------------------*/
464 ;;;###autoload(defvar flyspell-mode nil)
465 ;;;###autoload
466 (define-minor-mode flyspell-mode
467 "Minor mode performing on-the-fly spelling checking.
468 This spawns a single Ispell process and checks each word.
469 The default flyspell behavior is to highlight incorrect words.
470 With no argument, this command toggles Flyspell mode.
471 With a prefix argument ARG, turn Flyspell minor mode on if ARG is positive,
472 otherwise turn it off.
473
474 Bindings:
475 \\[ispell-word]: correct words (using Ispell).
476 \\[flyspell-auto-correct-word]: automatically correct word.
477 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
478 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
479
480 Hooks:
481 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
482
483 Remark:
484 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
485 valid. For instance, a personal dictionary can be used by
486 invoking `ispell-change-dictionary'.
487
488 Consider using the `ispell-parser' to check your text. For instance
489 consider adding:
490 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
491 in your .emacs file.
492
493 \\[flyspell-region] checks all words inside a region.
494 \\[flyspell-buffer] checks the whole buffer."
495 :lighter flyspell-mode-line-string
496 :keymap flyspell-mode-map
497 :group 'flyspell
498 (if flyspell-mode
499 (condition-case ()
500 (flyspell-mode-on)
501 (error (message "Enabling Flyspell mode gave an error")
502 (flyspell-mode -1)))
503 (flyspell-mode-off)))
504
505 ;;;###autoload
506 (defun turn-on-flyspell ()
507 "Unconditionally turn on Flyspell mode."
508 (flyspell-mode 1))
509
510 ;;;###autoload
511 (defun turn-off-flyspell ()
512 "Unconditionally turn off Flyspell mode."
513 (flyspell-mode -1))
514
515 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
516
517 ;;*---------------------------------------------------------------------*/
518 ;;* flyspell-buffers ... */
519 ;;* ------------------------------------------------------------- */
520 ;;* For remembering buffers running flyspell */
521 ;;*---------------------------------------------------------------------*/
522 (defvar flyspell-buffers nil)
523
524 ;;*---------------------------------------------------------------------*/
525 ;;* flyspell-minibuffer-p ... */
526 ;;*---------------------------------------------------------------------*/
527 (defun flyspell-minibuffer-p (buffer)
528 "Is BUFFER a minibuffer?"
529 (let ((ws (get-buffer-window-list buffer t)))
530 (and (consp ws) (window-minibuffer-p (car ws)))))
531
532 ;;*---------------------------------------------------------------------*/
533 ;;* flyspell-accept-buffer-local-defs ... */
534 ;;*---------------------------------------------------------------------*/
535 (defvar flyspell-last-buffer nil
536 "The buffer in which the last flyspell operation took place.")
537
538 (defun flyspell-accept-buffer-local-defs (&optional force)
539 ;; When flyspell-word is used inside a loop (e.g. when processing
540 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
541 ;; up dwarfing everything else, so only do it when the buffer has changed.
542 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
543 (setq flyspell-last-buffer (current-buffer))
544 ;; Strange problem: If buffer in current window has font-lock turned on,
545 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
546 ;; call will reset the buffer to the buffer in the current window.
547 ;; However, it only happens at startup (fix by Albert L. Ting).
548 (save-current-buffer
549 (ispell-accept-buffer-local-defs))
550 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
551 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
552 ;; The dictionary has changed
553 (setq flyspell-dash-dictionary ispell-dictionary)
554 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
555 (setq flyspell-consider-dash-as-word-delimiter-flag
556 (member (or ispell-local-dictionary ispell-dictionary)
557 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
558
559 (defun flyspell-hack-local-variables-hook ()
560 ;; When local variables are loaded, see if the dictionary context
561 ;; has changed.
562 (flyspell-accept-buffer-local-defs 'force))
563
564 (defun flyspell-kill-ispell-hook ()
565 (setq flyspell-last-buffer nil)
566 (dolist (buf (buffer-list))
567 (with-current-buffer buf
568 (kill-local-variable 'flyspell-word-cache-word))))
569
570 ;; Make sure we flush our caches when needed. Do it here rather than in
571 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
572 ;; on flyspell-mode.
573 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
574
575 ;;*---------------------------------------------------------------------*/
576 ;;* flyspell-mode-on ... */
577 ;;*---------------------------------------------------------------------*/
578 (defun flyspell-mode-on ()
579 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
580 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
581 (setq ispell-highlight-face 'flyspell-incorrect)
582 ;; local dictionaries setup
583 (or ispell-local-dictionary ispell-dictionary
584 (if flyspell-default-dictionary
585 (ispell-change-dictionary flyspell-default-dictionary)))
586 ;; we have to force ispell to accept the local definition or
587 ;; otherwise it could be too late, the local dictionary may
588 ;; be forgotten!
589 ;; Pass the `force' argument for the case where flyspell was active already
590 ;; but the buffer's local-defs have been edited.
591 (flyspell-accept-buffer-local-defs 'force)
592 ;; we put the `flyspell-delayed' property on some commands
593 (flyspell-delay-commands)
594 ;; we put the `flyspell-deplacement' property on some commands
595 (flyspell-deplacement-commands)
596 ;; we bound flyspell action to post-command hook
597 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
598 ;; we bound flyspell action to pre-command hook
599 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
600 ;; we bound flyspell action to after-change hook
601 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
602 ;; we bound flyspell action to hack-local-variables-hook
603 (add-hook 'hack-local-variables-hook
604 (function flyspell-hack-local-variables-hook) t t)
605 ;; set flyspell-generic-check-word-predicate based on the major mode
606 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
607 (if mode-predicate
608 (setq flyspell-generic-check-word-predicate mode-predicate)))
609 ;; the welcome message
610 (if (and flyspell-issue-message-flag
611 flyspell-issue-welcome-flag
612 (interactive-p))
613 (let ((binding (where-is-internal 'flyspell-auto-correct-word
614 nil 'non-ascii)))
615 (message "%s"
616 (if binding
617 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
618 (key-description binding))
619 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
620
621 ;;*---------------------------------------------------------------------*/
622 ;;* flyspell-delay-commands ... */
623 ;;*---------------------------------------------------------------------*/
624 (defun flyspell-delay-commands ()
625 "Install the standard set of Flyspell delayed commands."
626 (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
627 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
628
629 ;;*---------------------------------------------------------------------*/
630 ;;* flyspell-delay-command ... */
631 ;;*---------------------------------------------------------------------*/
632 (defun flyspell-delay-command (command)
633 "Set COMMAND to be delayed, for Flyspell.
634 When flyspell `post-command-hook' is invoked because a delayed command
635 as been used the current word is not immediately checked.
636 It will be checked only after `flyspell-delay' seconds."
637 (interactive "SDelay Flyspell after Command: ")
638 (put command 'flyspell-delayed t))
639
640 ;;*---------------------------------------------------------------------*/
641 ;;* flyspell-deplacement-commands ... */
642 ;;*---------------------------------------------------------------------*/
643 (defun flyspell-deplacement-commands ()
644 "Install the standard set of Flyspell deplacement commands."
645 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
646 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
647
648 ;;*---------------------------------------------------------------------*/
649 ;;* flyspell-deplacement-command ... */
650 ;;*---------------------------------------------------------------------*/
651 (defun flyspell-deplacement-command (command)
652 "Set COMMAND that implement cursor movements, for Flyspell.
653 When flyspell `post-command-hook' is invoked because of a deplacement command
654 as been used the current word is checked only if the previous command was
655 not the very same deplacement command."
656 (interactive "SDeplacement Flyspell after Command: ")
657 (put command 'flyspell-deplacement t))
658
659 ;;*---------------------------------------------------------------------*/
660 ;;* flyspell-word-cache ... */
661 ;;*---------------------------------------------------------------------*/
662 (defvar flyspell-word-cache-start nil)
663 (defvar flyspell-word-cache-end nil)
664 (defvar flyspell-word-cache-word nil)
665 (defvar flyspell-word-cache-result '_)
666 (make-variable-buffer-local 'flyspell-word-cache-start)
667 (make-variable-buffer-local 'flyspell-word-cache-end)
668 (make-variable-buffer-local 'flyspell-word-cache-word)
669 (make-variable-buffer-local 'flyspell-word-cache-result)
670
671 ;;*---------------------------------------------------------------------*/
672 ;;* The flyspell pre-hook, store the current position. In the */
673 ;;* post command hook, we will check, if the word at this position */
674 ;;* has to be spell checked. */
675 ;;*---------------------------------------------------------------------*/
676 (defvar flyspell-pre-buffer nil)
677 (defvar flyspell-pre-point nil)
678 (defvar flyspell-pre-column nil)
679 (defvar flyspell-pre-pre-buffer nil)
680 (defvar flyspell-pre-pre-point nil)
681
682 ;;*---------------------------------------------------------------------*/
683 ;;* flyspell-previous-command ... */
684 ;;*---------------------------------------------------------------------*/
685 (defvar flyspell-previous-command nil
686 "The last interactive command checked by Flyspell.")
687
688 ;;*---------------------------------------------------------------------*/
689 ;;* flyspell-pre-command-hook ... */
690 ;;*---------------------------------------------------------------------*/
691 (defun flyspell-pre-command-hook ()
692 "Save the current buffer and point for Flyspell's post-command hook."
693 (interactive)
694 (setq flyspell-pre-buffer (current-buffer))
695 (setq flyspell-pre-point (point))
696 (setq flyspell-pre-column (current-column)))
697
698 ;;*---------------------------------------------------------------------*/
699 ;;* flyspell-mode-off ... */
700 ;;*---------------------------------------------------------------------*/
701 ;;;###autoload
702 (defun flyspell-mode-off ()
703 "Turn Flyspell mode off."
704 ;; we remove the hooks
705 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
706 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
707 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
708 (remove-hook 'hack-local-variables-hook
709 (function flyspell-hack-local-variables-hook) t)
710 ;; we remove all the flyspell hilightings
711 (flyspell-delete-all-overlays)
712 ;; we have to erase pre cache variables
713 (setq flyspell-pre-buffer nil)
714 (setq flyspell-pre-point nil)
715 ;; we mark the mode as killed
716 (setq flyspell-mode nil))
717
718 ;;*---------------------------------------------------------------------*/
719 ;;* flyspell-check-pre-word-p ... */
720 ;;*---------------------------------------------------------------------*/
721 (defun flyspell-check-pre-word-p ()
722 "Return non-nil if we should check the word before point.
723 More precisely, it applies to the word that was before point
724 before the current command."
725 (cond
726 ((or (not (numberp flyspell-pre-point))
727 (not (bufferp flyspell-pre-buffer))
728 (not (buffer-live-p flyspell-pre-buffer)))
729 nil)
730 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
731 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
732 nil)
733 ((or (and (= flyspell-pre-point (- (point) 1))
734 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
735 (= flyspell-pre-point (point))
736 (= flyspell-pre-point (+ (point) 1)))
737 nil)
738 ((and (symbolp this-command)
739 (not executing-kbd-macro)
740 (or (get this-command 'flyspell-delayed)
741 (and (get this-command 'flyspell-deplacement)
742 (eq flyspell-previous-command this-command)))
743 (or (= (current-column) 0)
744 (= (current-column) flyspell-pre-column)
745 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
746 nil)
747 ((not (eq (current-buffer) flyspell-pre-buffer))
748 t)
749 ((not (and (numberp flyspell-word-cache-start)
750 (numberp flyspell-word-cache-end)))
751 t)
752 (t
753 (or (< flyspell-pre-point flyspell-word-cache-start)
754 (> flyspell-pre-point flyspell-word-cache-end)))))
755
756 ;;*---------------------------------------------------------------------*/
757 ;;* The flyspell after-change-hook, store the change position. In */
758 ;;* the post command hook, we will check, if the word at this */
759 ;;* position has to be spell checked. */
760 ;;*---------------------------------------------------------------------*/
761 (defvar flyspell-changes nil)
762 (make-variable-buffer-local 'flyspell-changes)
763
764 ;;*---------------------------------------------------------------------*/
765 ;;* flyspell-after-change-function ... */
766 ;;*---------------------------------------------------------------------*/
767 (defun flyspell-after-change-function (start stop len)
768 "Save the current buffer and point for Flyspell's post-command hook."
769 (push (cons start stop) flyspell-changes))
770
771 ;;*---------------------------------------------------------------------*/
772 ;;* flyspell-check-changed-word-p ... */
773 ;;*---------------------------------------------------------------------*/
774 (defun flyspell-check-changed-word-p (start stop)
775 "Return t when the changed word has to be checked.
776 The answer depends of several criteria.
777 Mostly we check word delimiters."
778 (cond
779 ((and (memq (char-after start) '(?\n ? )) (> stop start))
780 t)
781 ((not (numberp flyspell-pre-point))
782 t)
783 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
784 nil)
785 ((let ((pos (point)))
786 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
787 nil)
788 (t
789 t)))
790
791 ;;*---------------------------------------------------------------------*/
792 ;;* flyspell-check-word-p ... */
793 ;;*---------------------------------------------------------------------*/
794 (defun flyspell-check-word-p ()
795 "Return t when the word at `point' has to be checked.
796 The answer depends of several criteria.
797 Mostly we check word delimiters."
798 (cond
799 ((<= (- (point-max) 1) (point-min))
800 ;; the buffer is not filled enough
801 nil)
802 ((and (and (> (current-column) 0)
803 (not (eq (current-column) flyspell-pre-column)))
804 (save-excursion
805 (backward-char 1)
806 (and (looking-at (flyspell-get-not-casechars))
807 (or flyspell-consider-dash-as-word-delimiter-flag
808 (not (looking-at "-"))))))
809 ;; yes because we have reached or typed a word delimiter.
810 t)
811 ((symbolp this-command)
812 (cond
813 ((get this-command 'flyspell-deplacement)
814 (not (eq flyspell-previous-command this-command)))
815 ((get this-command 'flyspell-delayed)
816 ;; the current command is not delayed, that
817 ;; is that we must check the word now
818 (and (not unread-command-events)
819 (sit-for flyspell-delay)))
820 (t t)))
821 (t t)))
822
823 ;;*---------------------------------------------------------------------*/
824 ;;* flyspell-debug-signal-no-check ... */
825 ;;*---------------------------------------------------------------------*/
826 (defun flyspell-debug-signal-no-check (msg obj)
827 (setq debug-on-error t)
828 (with-current-buffer (get-buffer-create "*flyspell-debug*")
829 (erase-buffer)
830 (insert "NO-CHECK:\n")
831 (insert (format " %S : %S\n" msg obj))))
832
833 ;;*---------------------------------------------------------------------*/
834 ;;* flyspell-debug-signal-pre-word-checked ... */
835 ;;*---------------------------------------------------------------------*/
836 (defun flyspell-debug-signal-pre-word-checked ()
837 (setq debug-on-error t)
838 (with-current-buffer (get-buffer-create "*flyspell-debug*")
839 (insert "PRE-WORD:\n")
840 (insert (format " pre-point : %S\n" flyspell-pre-point))
841 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
842 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
843 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
844 (goto-char (point-max))))
845
846 ;;*---------------------------------------------------------------------*/
847 ;;* flyspell-debug-signal-word-checked ... */
848 ;;*---------------------------------------------------------------------*/
849 (defun flyspell-debug-signal-word-checked ()
850 (setq debug-on-error t)
851 (let ((oldbuf (current-buffer))
852 (point (point)))
853 (with-current-buffer (get-buffer-create "*flyspell-debug*")
854 (insert "WORD:\n")
855 (insert (format " this-cmd : %S\n" this-command))
856 (insert (format " delayed : %S\n" (and (symbolp this-command)
857 (get this-command 'flyspell-delayed))))
858 (insert (format " point : %S\n" point))
859 (insert (format " prev-char : [%c] %S\n"
860 (with-current-buffer oldbuf
861 (let ((c (if (> (point) (point-min))
862 (save-excursion
863 (backward-char 1)
864 (char-after (point)))
865 ? )))
866 c))
867 (with-current-buffer oldbuf
868 (let ((c (if (> (point) (point-min))
869 (save-excursion
870 (backward-char 1)
871 (and (and (looking-at (flyspell-get-not-casechars)) 1)
872 (and (or flyspell-consider-dash-as-word-delimiter-flag
873 (not (looking-at "\\-"))) 2))))))
874 c))))
875 (insert (format " because : %S\n"
876 (cond
877 ((not (and (symbolp this-command)
878 (get this-command 'flyspell-delayed)))
879 ;; the current command is not delayed, that
880 ;; is that we must check the word now
881 'not-delayed)
882 ((with-current-buffer oldbuf
883 (let ((c (if (> (point) (point-min))
884 (save-excursion
885 (backward-char 1)
886 (and (looking-at (flyspell-get-not-casechars))
887 (or flyspell-consider-dash-as-word-delimiter-flag
888 (not (looking-at "\\-"))))))))
889 c))
890 ;; yes because we have reached or typed a word delimiter.
891 'separator)
892 ((not (integerp flyspell-delay))
893 ;; yes because the user had set up a no-delay configuration.
894 'no-delay)
895 (t
896 'sit-for))))
897 (goto-char (point-max)))))
898
899 ;;*---------------------------------------------------------------------*/
900 ;;* flyspell-debug-signal-changed-checked ... */
901 ;;*---------------------------------------------------------------------*/
902 (defun flyspell-debug-signal-changed-checked ()
903 (setq debug-on-error t)
904 (let ((point (point)))
905 (with-current-buffer (get-buffer-create "*flyspell-debug*")
906 (insert "CHANGED WORD:\n")
907 (insert (format " point : %S\n" point))
908 (goto-char (point-max)))))
909
910 ;;*---------------------------------------------------------------------*/
911 ;;* flyspell-post-command-hook ... */
912 ;;* ------------------------------------------------------------- */
913 ;;* It is possible that we check several words: */
914 ;;* 1- the current word is checked if the predicate */
915 ;;* FLYSPELL-CHECK-WORD-P is true */
916 ;;* 2- the word that used to be the current word before the */
917 ;;* THIS-COMMAND is checked if: */
918 ;;* a- the previous word is different from the current word */
919 ;;* b- the previous word as not just been checked by the */
920 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
921 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
922 ;;* previous word nor the current word */
923 ;;*---------------------------------------------------------------------*/
924 (defun flyspell-post-command-hook ()
925 "The `post-command-hook' used by flyspell to check a word in-the-fly."
926 (interactive)
927 (let ((command this-command)
928 ;; Prevent anything we do from affecting the mark.
929 deactivate-mark)
930 (if (flyspell-check-pre-word-p)
931 (with-current-buffer flyspell-pre-buffer
932 '(flyspell-debug-signal-pre-word-checked)
933 (save-excursion
934 (goto-char flyspell-pre-point)
935 (flyspell-word))))
936 (if (flyspell-check-word-p)
937 (progn
938 '(flyspell-debug-signal-word-checked)
939 (flyspell-word)
940 ;; we remember which word we have just checked.
941 ;; this will be used next time we will check a word
942 ;; to compare the next current word with the word
943 ;; that as been registered in the pre-command-hook
944 ;; that is these variables are used within the predicate
945 ;; FLYSPELL-CHECK-PRE-WORD-P
946 (setq flyspell-pre-pre-buffer (current-buffer))
947 (setq flyspell-pre-pre-point (point)))
948 (progn
949 (setq flyspell-pre-pre-buffer nil)
950 (setq flyspell-pre-pre-point nil)
951 ;; when a word is not checked because of a delayed command
952 ;; we do not disable the ispell cache.
953 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
954 (progn
955 (setq flyspell-word-cache-end -1)
956 (setq flyspell-word-cache-result '_)))))
957 (while (and (not (input-pending-p)) (consp flyspell-changes))
958 (let ((start (car (car flyspell-changes)))
959 (stop (cdr (car flyspell-changes))))
960 (if (flyspell-check-changed-word-p start stop)
961 (save-excursion
962 '(flyspell-debug-signal-changed-checked)
963 (goto-char start)
964 (flyspell-word)))
965 (setq flyspell-changes (cdr flyspell-changes))))
966 (setq flyspell-previous-command command)))
967
968 ;;*---------------------------------------------------------------------*/
969 ;;* flyspell-notify-misspell ... */
970 ;;*---------------------------------------------------------------------*/
971 (defun flyspell-notify-misspell (word poss)
972 (let ((replacements (if (stringp poss)
973 poss
974 (if flyspell-sort-corrections
975 (sort (car (cdr (cdr poss))) 'string<)
976 (car (cdr (cdr poss)))))))
977 (if flyspell-issue-message-flag
978 (message "misspelling `%s' %S" word replacements))))
979
980 ;;*---------------------------------------------------------------------*/
981 ;;* flyspell-word-search-backward ... */
982 ;;*---------------------------------------------------------------------*/
983 (defun flyspell-word-search-backward (word bound)
984 (save-excursion
985 (let ((r '())
986 (inhibit-point-motion-hooks t)
987 p)
988 (while (and (not r) (setq p (search-backward word bound t)))
989 (let ((lw (flyspell-get-word '())))
990 (if (and (consp lw) (string-equal (car lw) word))
991 (setq r p)
992 (goto-char p))))
993 r)))
994
995 ;;*---------------------------------------------------------------------*/
996 ;;* flyspell-word-search-forward ... */
997 ;;*---------------------------------------------------------------------*/
998 (defun flyspell-word-search-forward (word bound)
999 (save-excursion
1000 (let ((r '())
1001 (inhibit-point-motion-hooks t)
1002 p)
1003 (while (and (not r) (setq p (search-forward word bound t)))
1004 (let ((lw (flyspell-get-word '())))
1005 (if (and (consp lw) (string-equal (car lw) word))
1006 (setq r p)
1007 (goto-char (1+ p)))))
1008 r)))
1009
1010 ;;*---------------------------------------------------------------------*/
1011 ;;* flyspell-word ... */
1012 ;;*---------------------------------------------------------------------*/
1013 (defun flyspell-word (&optional following)
1014 "Spell check a word."
1015 (interactive (list ispell-following-word))
1016 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
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-set-spellchecker-params) ; Initialize variables and dicts alists
1533 ;; Local dictionary becomes the global dictionary in use.
1534 (setq ispell-current-dictionary
1535 (or ispell-local-dictionary ispell-dictionary))
1536 (setq ispell-current-personal-dictionary
1537 (or ispell-local-pdict ispell-personal-dictionary))
1538 (let ((args (ispell-get-ispell-args))
1539 (encoding (ispell-get-coding-system))
1540 c)
1541 (if (and ispell-current-dictionary ; use specified dictionary
1542 (not (member "-d" args))) ; only define if not overridden
1543 (setq args
1544 (append (list "-d" ispell-current-dictionary) args)))
1545 (if ispell-current-personal-dictionary ; use specified pers dict
1546 (setq args
1547 (append args
1548 (list "-p"
1549 (expand-file-name
1550 ispell-current-personal-dictionary)))))
1551 (setq args (append args ispell-extra-args))
1552 (if (and ispell-really-aspell
1553 ispell-aspell-supports-utf8)
1554 (setq args
1555 (append args
1556 (list
1557 (concat "--encoding="
1558 (symbol-name
1559 encoding))))))
1560 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1561 (setq c (apply 'ispell-call-process-region beg
1562 end
1563 ispell-program-name
1564 nil
1565 buffer
1566 nil
1567 (if ispell-really-aspell "list" "-l")
1568 args)))
1569 (if (eq c 0)
1570 (progn
1571 (flyspell-process-localwords buffer)
1572 (with-current-buffer curbuf
1573 (flyspell-delete-region-overlays beg end)
1574 (flyspell-check-region-doublons beg end))
1575 (flyspell-external-point-words))
1576 (error "Can't check region...")))))
1577
1578 ;;*---------------------------------------------------------------------*/
1579 ;;* flyspell-region ... */
1580 ;;* ------------------------------------------------------------- */
1581 ;;* Because `ispell -a' is too slow, it is not possible to use */
1582 ;;* it on large region. Then, when ispell is invoked on a large */
1583 ;;* text region, a new `ispell -l' process is spawned. The */
1584 ;;* pointed out words are then searched in the region a checked with */
1585 ;;* regular flyspell means. */
1586 ;;*---------------------------------------------------------------------*/
1587 ;;;###autoload
1588 (defun flyspell-region (beg end)
1589 "Flyspell text between BEG and END."
1590 (interactive "r")
1591 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1592 (if (= beg end)
1593 ()
1594 (save-excursion
1595 (if (> beg end)
1596 (let ((old beg))
1597 (setq beg end)
1598 (setq end old)))
1599 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1600 (flyspell-large-region beg end)
1601 (flyspell-small-region beg end)))))
1602
1603 ;;*---------------------------------------------------------------------*/
1604 ;;* flyspell-buffer ... */
1605 ;;*---------------------------------------------------------------------*/
1606 ;;;###autoload
1607 (defun flyspell-buffer ()
1608 "Flyspell whole buffer."
1609 (interactive)
1610 (flyspell-region (point-min) (point-max)))
1611
1612 ;;*---------------------------------------------------------------------*/
1613 ;;* old next error position ... */
1614 ;;*---------------------------------------------------------------------*/
1615 (defvar flyspell-old-buffer-error nil)
1616 (defvar flyspell-old-pos-error nil)
1617
1618 ;;*---------------------------------------------------------------------*/
1619 ;;* flyspell-goto-next-error ... */
1620 ;;*---------------------------------------------------------------------*/
1621 (defun flyspell-goto-next-error ()
1622 "Go to the next previously detected error.
1623 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1624 FLYSPELL-BUFFER."
1625 (interactive)
1626 (let ((pos (point))
1627 (max (point-max)))
1628 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1629 (eq pos flyspell-old-pos-error))
1630 (progn
1631 (if (= flyspell-old-pos-error max)
1632 ;; goto beginning of buffer
1633 (progn
1634 (message "Restarting from beginning of buffer")
1635 (goto-char (point-min)))
1636 (forward-word 1))
1637 (setq pos (point))))
1638 ;; seek the next error
1639 (while (and (< pos max)
1640 (let ((ovs (overlays-at pos))
1641 (r '()))
1642 (while (and (not r) (consp ovs))
1643 (if (flyspell-overlay-p (car ovs))
1644 (setq r t)
1645 (setq ovs (cdr ovs))))
1646 (not r)))
1647 (setq pos (1+ pos)))
1648 ;; save the current location for next invocation
1649 (setq flyspell-old-pos-error pos)
1650 (setq flyspell-old-buffer-error (current-buffer))
1651 (goto-char pos)
1652 (if (= pos max)
1653 (message "No more miss-spelled word!"))))
1654
1655 ;;*---------------------------------------------------------------------*/
1656 ;;* flyspell-overlay-p ... */
1657 ;;*---------------------------------------------------------------------*/
1658 (defun flyspell-overlay-p (o)
1659 "Return true if O is an overlay used by flyspell."
1660 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1661
1662 ;;*---------------------------------------------------------------------*/
1663 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1664 ;;* ------------------------------------------------------------- */
1665 ;;* Remove overlays introduced by flyspell. */
1666 ;;*---------------------------------------------------------------------*/
1667 (defun flyspell-delete-region-overlays (beg end)
1668 "Delete overlays used by flyspell in a given region."
1669 (remove-overlays beg end 'flyspell-overlay t))
1670
1671
1672 (defun flyspell-delete-all-overlays ()
1673 "Delete all the overlays used by flyspell."
1674 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1675
1676 ;;*---------------------------------------------------------------------*/
1677 ;;* flyspell-unhighlight-at ... */
1678 ;;*---------------------------------------------------------------------*/
1679 (defun flyspell-unhighlight-at (pos)
1680 "Remove the flyspell overlay that are located at POS."
1681 (if flyspell-persistent-highlight
1682 (let ((overlays (overlays-at pos)))
1683 (while (consp overlays)
1684 (if (flyspell-overlay-p (car overlays))
1685 (delete-overlay (car overlays)))
1686 (setq overlays (cdr overlays))))
1687 (if (flyspell-overlay-p flyspell-overlay)
1688 (delete-overlay flyspell-overlay))))
1689
1690 ;;*---------------------------------------------------------------------*/
1691 ;;* flyspell-properties-at-p ... */
1692 ;;* ------------------------------------------------------------- */
1693 ;;* Is there an highlight properties at position pos? */
1694 ;;*---------------------------------------------------------------------*/
1695 (defun flyspell-properties-at-p (pos)
1696 "Return t if there is a text property at POS, not counting `local-map'.
1697 If variable `flyspell-highlight-properties' is set to nil,
1698 text with properties are not checked. This function is used to discover
1699 if the character at POS has any other property."
1700 (let ((prop (text-properties-at pos))
1701 (keep t))
1702 (while (and keep (consp prop))
1703 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1704 (setq prop (cdr (cdr prop)))
1705 (setq keep nil)))
1706 (consp prop)))
1707
1708 ;;*---------------------------------------------------------------------*/
1709 ;;* make-flyspell-overlay ... */
1710 ;;*---------------------------------------------------------------------*/
1711 (defun make-flyspell-overlay (beg end face mouse-face)
1712 "Allocate an overlay to highlight an incorrect word.
1713 BEG and END specify the range in the buffer of that word.
1714 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1715 for the overlay."
1716 (let ((overlay (make-overlay beg end nil t nil)))
1717 (overlay-put overlay 'face face)
1718 (overlay-put overlay 'mouse-face mouse-face)
1719 (overlay-put overlay 'flyspell-overlay t)
1720 (overlay-put overlay 'evaporate t)
1721 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1722 (overlay-put overlay 'keymap flyspell-mouse-map)
1723 (when (eq face 'flyspell-incorrect)
1724 (and (stringp flyspell-before-incorrect-word-string)
1725 (overlay-put overlay 'before-string
1726 flyspell-before-incorrect-word-string))
1727 (and (stringp flyspell-after-incorrect-word-string)
1728 (overlay-put overlay 'after-string
1729 flyspell-after-incorrect-word-string)))
1730 overlay))
1731
1732 ;;*---------------------------------------------------------------------*/
1733 ;;* flyspell-highlight-incorrect-region ... */
1734 ;;*---------------------------------------------------------------------*/
1735 (defun flyspell-highlight-incorrect-region (beg end poss)
1736 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1737 POSS is usually a list of possible spelling/correction lists,
1738 as returned by `ispell-parse-output'.
1739 It can also be the symbol `doublon', in the case where the word
1740 is itself incorrect, but suspiciously repeated."
1741 (let ((inhibit-read-only t))
1742 (unless (run-hook-with-args-until-success
1743 'flyspell-incorrect-hook beg end poss)
1744 (if (or flyspell-highlight-properties
1745 (not (flyspell-properties-at-p beg)))
1746 (progn
1747 ;; we cleanup all the overlay that are in the region, not
1748 ;; beginning at the word start position
1749 (if (< (1+ beg) end)
1750 (let ((os (overlays-in (1+ beg) end)))
1751 (while (consp os)
1752 (if (flyspell-overlay-p (car os))
1753 (delete-overlay (car os)))
1754 (setq os (cdr os)))))
1755 ;; we cleanup current overlay at the same position
1756 (flyspell-unhighlight-at beg)
1757 ;; now we can use a new overlay
1758 (setq flyspell-overlay
1759 (make-flyspell-overlay
1760 beg end
1761 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1762 'highlight)))))))
1763
1764 ;;*---------------------------------------------------------------------*/
1765 ;;* flyspell-highlight-duplicate-region ... */
1766 ;;*---------------------------------------------------------------------*/
1767 (defun flyspell-highlight-duplicate-region (beg end poss)
1768 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1769 POSS is a list of possible spelling/correction lists,
1770 as returned by `ispell-parse-output'."
1771 (let ((inhibit-read-only t))
1772 (unless (run-hook-with-args-until-success
1773 'flyspell-incorrect-hook beg end poss)
1774 (if (or flyspell-highlight-properties
1775 (not (flyspell-properties-at-p beg)))
1776 (progn
1777 ;; we cleanup current overlay at the same position
1778 (flyspell-unhighlight-at beg)
1779 ;; now we can use a new overlay
1780 (setq flyspell-overlay
1781 (make-flyspell-overlay beg end
1782 'flyspell-duplicate
1783 'highlight)))))))
1784
1785 ;;*---------------------------------------------------------------------*/
1786 ;;* flyspell-auto-correct-cache ... */
1787 ;;*---------------------------------------------------------------------*/
1788 (defvar flyspell-auto-correct-pos nil)
1789 (defvar flyspell-auto-correct-region nil)
1790 (defvar flyspell-auto-correct-ring nil)
1791 (defvar flyspell-auto-correct-word nil)
1792 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1793 (make-variable-buffer-local 'flyspell-auto-correct-region)
1794 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1795 (make-variable-buffer-local 'flyspell-auto-correct-word)
1796
1797 ;;*---------------------------------------------------------------------*/
1798 ;;* flyspell-check-previous-highlighted-word ... */
1799 ;;*---------------------------------------------------------------------*/
1800 (defun flyspell-check-previous-highlighted-word (&optional arg)
1801 "Correct the closer misspelled word.
1802 This function scans a mis-spelled word before the cursor. If it finds one
1803 it proposes replacement for that word. With prefix arg, count that many
1804 misspelled words backwards."
1805 (interactive)
1806 (let ((pos1 (point))
1807 (pos (point))
1808 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1809 ov ovs)
1810 (if (catch 'exit
1811 (while (and (setq pos (previous-overlay-change pos))
1812 (not (= pos pos1)))
1813 (setq pos1 pos)
1814 (if (> pos (point-min))
1815 (progn
1816 (setq ovs (overlays-at (1- pos)))
1817 (while (consp ovs)
1818 (setq ov (car ovs))
1819 (setq ovs (cdr ovs))
1820 (if (and (flyspell-overlay-p ov)
1821 (= 0 (setq arg (1- arg))))
1822 (throw 'exit t)))))))
1823 (save-excursion
1824 (goto-char pos)
1825 (ispell-word))
1826 (error "No word to correct before point"))))
1827
1828 ;;*---------------------------------------------------------------------*/
1829 ;;* flyspell-display-next-corrections ... */
1830 ;;*---------------------------------------------------------------------*/
1831 (defun flyspell-display-next-corrections (corrections)
1832 (let ((string "Corrections:")
1833 (l corrections)
1834 (pos '()))
1835 (while (< (length string) 80)
1836 (if (equal (car l) flyspell-auto-correct-word)
1837 (setq pos (cons (+ 1 (length string)) pos)))
1838 (setq string (concat string " " (car l)))
1839 (setq l (cdr l)))
1840 (while (consp pos)
1841 (let ((num (car pos)))
1842 (put-text-property num
1843 (+ num (length flyspell-auto-correct-word))
1844 'face 'flyspell-incorrect
1845 string))
1846 (setq pos (cdr pos)))
1847 (if (fboundp 'display-message)
1848 (display-message 'no-log string)
1849 (message "%s" string))))
1850
1851 ;;*---------------------------------------------------------------------*/
1852 ;;* flyspell-abbrev-table ... */
1853 ;;*---------------------------------------------------------------------*/
1854 (defun flyspell-abbrev-table ()
1855 (if flyspell-use-global-abbrev-table-p
1856 global-abbrev-table
1857 (or local-abbrev-table global-abbrev-table)))
1858
1859 ;;*---------------------------------------------------------------------*/
1860 ;;* flyspell-define-abbrev ... */
1861 ;;*---------------------------------------------------------------------*/
1862 (defun flyspell-define-abbrev (name expansion)
1863 (let ((table (flyspell-abbrev-table)))
1864 (when table
1865 (define-abbrev table (downcase name) expansion))))
1866
1867 ;;*---------------------------------------------------------------------*/
1868 ;;* flyspell-auto-correct-word ... */
1869 ;;*---------------------------------------------------------------------*/
1870 (defun flyspell-auto-correct-word ()
1871 "Correct the current word.
1872 This command proposes various successive corrections for the current word."
1873 (interactive)
1874 (let ((pos (point))
1875 (old-max (point-max)))
1876 ;; use the correct dictionary
1877 (flyspell-accept-buffer-local-defs)
1878 (if (and (eq flyspell-auto-correct-pos pos)
1879 (consp flyspell-auto-correct-region))
1880 ;; we have already been using the function at the same location
1881 (let* ((start (car flyspell-auto-correct-region))
1882 (len (cdr flyspell-auto-correct-region)))
1883 (flyspell-unhighlight-at start)
1884 (delete-region start (+ start len))
1885 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1886 (let* ((word (car flyspell-auto-correct-ring))
1887 (len (length word)))
1888 (rplacd flyspell-auto-correct-region len)
1889 (goto-char start)
1890 (if flyspell-abbrev-p
1891 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1892 flyspell-auto-correct-word)
1893 (flyspell-change-abbrev (flyspell-abbrev-table)
1894 flyspell-auto-correct-word
1895 word)
1896 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1897 (funcall flyspell-insert-function word)
1898 (flyspell-word)
1899 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1900 (flyspell-ajust-cursor-point pos (point) old-max)
1901 (setq flyspell-auto-correct-pos (point)))
1902 ;; fetch the word to be checked
1903 (let ((word (flyspell-get-word nil)))
1904 (if (consp word)
1905 (let ((start (car (cdr word)))
1906 (end (car (cdr (cdr word))))
1907 (word (car word))
1908 poss ispell-filter)
1909 (setq flyspell-auto-correct-word word)
1910 ;; now check spelling of word.
1911 (ispell-send-string "%\n") ;put in verbose mode
1912 (ispell-send-string (concat "^" word "\n"))
1913 ;; wait until ispell has processed word.
1914 (while (progn
1915 (accept-process-output ispell-process)
1916 (not (string= "" (car ispell-filter)))))
1917 ;; Remove leading empty element
1918 (setq ispell-filter (cdr ispell-filter))
1919 ;; ispell process should return something after word is sent.
1920 ;; Tag word as valid (i.e., skip) otherwise
1921 (or ispell-filter
1922 (setq ispell-filter '(*)))
1923 (if (consp ispell-filter)
1924 (setq poss (ispell-parse-output (car ispell-filter))))
1925 (cond
1926 ((or (eq poss t) (stringp poss))
1927 ;; don't correct word
1928 t)
1929 ((null poss)
1930 ;; ispell error
1931 (error "Ispell: error in Ispell process"))
1932 (t
1933 ;; the word is incorrect, we have to propose a replacement
1934 (let ((replacements (if flyspell-sort-corrections
1935 (sort (car (cdr (cdr poss))) 'string<)
1936 (car (cdr (cdr poss))))))
1937 (setq flyspell-auto-correct-region nil)
1938 (if (consp replacements)
1939 (progn
1940 (let ((replace (car replacements)))
1941 (let ((new-word replace))
1942 (if (not (equal new-word (car poss)))
1943 (progn
1944 ;; the save the current replacements
1945 (setq flyspell-auto-correct-region
1946 (cons start (length new-word)))
1947 (let ((l replacements))
1948 (while (consp (cdr l))
1949 (setq l (cdr l)))
1950 (rplacd l (cons (car poss) replacements)))
1951 (setq flyspell-auto-correct-ring
1952 replacements)
1953 (flyspell-unhighlight-at start)
1954 (delete-region start end)
1955 (funcall flyspell-insert-function new-word)
1956 (if flyspell-abbrev-p
1957 (if (flyspell-already-abbrevp
1958 (flyspell-abbrev-table) word)
1959 (flyspell-change-abbrev
1960 (flyspell-abbrev-table)
1961 word
1962 new-word)
1963 (flyspell-define-abbrev word
1964 new-word)))
1965 (flyspell-word)
1966 (flyspell-display-next-corrections
1967 (cons new-word flyspell-auto-correct-ring))
1968 (flyspell-ajust-cursor-point pos
1969 (point)
1970 old-max))))))))))
1971 (setq flyspell-auto-correct-pos (point))
1972 (ispell-pdict-save t)))))))
1973
1974 ;;*---------------------------------------------------------------------*/
1975 ;;* flyspell-auto-correct-previous-pos ... */
1976 ;;*---------------------------------------------------------------------*/
1977 (defvar flyspell-auto-correct-previous-pos nil
1978 "Holds the start of the first incorrect word before point.")
1979
1980 ;;*---------------------------------------------------------------------*/
1981 ;;* flyspell-auto-correct-previous-hook ... */
1982 ;;*---------------------------------------------------------------------*/
1983 (defun flyspell-auto-correct-previous-hook ()
1984 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1985 Sets `flyspell-auto-correct-previous-pos' to nil"
1986 (interactive)
1987 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1988 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1989 (setq flyspell-auto-correct-previous-pos nil)))
1990
1991 ;;*---------------------------------------------------------------------*/
1992 ;;* flyspell-auto-correct-previous-word ... */
1993 ;;*---------------------------------------------------------------------*/
1994 (defun flyspell-auto-correct-previous-word (position)
1995 "Auto correct the first mispelled word that occurs before point.
1996 But don't look beyond what's visible on the screen."
1997 (interactive "d")
1998
1999 (let ((top (window-start))
2000 (bot (window-end)))
2001 (save-excursion
2002 (save-restriction
2003 (narrow-to-region top bot)
2004 (overlay-recenter (point))
2005
2006 (add-hook 'pre-command-hook
2007 (function flyspell-auto-correct-previous-hook) t t)
2008
2009 (unless flyspell-auto-correct-previous-pos
2010 ;; only reset if a new overlay exists
2011 (setq flyspell-auto-correct-previous-pos nil)
2012
2013 (let ((overlay-list (overlays-in (point-min) position))
2014 (new-overlay 'dummy-value))
2015
2016 ;; search for previous (new) flyspell overlay
2017 (while (and new-overlay
2018 (or (not (flyspell-overlay-p new-overlay))
2019 ;; check if its face has changed
2020 (not (eq (get-char-property
2021 (overlay-start new-overlay) 'face)
2022 'flyspell-incorrect))))
2023 (setq new-overlay (car-safe overlay-list))
2024 (setq overlay-list (cdr-safe overlay-list)))
2025
2026 ;; if nothing new exits new-overlay should be nil
2027 (if new-overlay ;; the length of the word may change so go to the start
2028 (setq flyspell-auto-correct-previous-pos
2029 (overlay-start new-overlay)))))
2030
2031 (when flyspell-auto-correct-previous-pos
2032 (save-excursion
2033 (goto-char flyspell-auto-correct-previous-pos)
2034 (let ((ispell-following-word t)) ;; point is at start
2035 (if (numberp flyspell-auto-correct-previous-pos)
2036 (goto-char flyspell-auto-correct-previous-pos))
2037 (flyspell-auto-correct-word))
2038 ;; the point may have moved so reset this
2039 (setq flyspell-auto-correct-previous-pos (point))))))))
2040
2041 ;;*---------------------------------------------------------------------*/
2042 ;;* flyspell-correct-word ... */
2043 ;;*---------------------------------------------------------------------*/
2044
2045 (defun flyspell-correct-word (event)
2046 "Pop up a menu of possible corrections for a misspelled word.
2047 The word checked is the word at the mouse position."
2048 (interactive "e")
2049 (let ((save (point)))
2050 (mouse-set-point event)
2051 (flyspell-correct-word-before-point event save)))
2052
2053 (defun flyspell-correct-word-before-point (&optional event opoint)
2054 "Pop up a menu of possible corrections for misspelled word before point.
2055 If EVENT is non-nil, it is the mouse event that invoked this operation;
2056 that controls where to put the menu.
2057 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2058 (interactive)
2059 (unless (mouse-position)
2060 (error "Pop-up menus do not work on this terminal"))
2061 ;; use the correct dictionary
2062 (flyspell-accept-buffer-local-defs)
2063 (or opoint (setq opoint (point-marker)))
2064 (let ((cursor-location (point))
2065 (word (flyspell-get-word nil)))
2066 (if (consp word)
2067 (let ((start (car (cdr word)))
2068 (end (car (cdr (cdr word))))
2069 (word (car word))
2070 poss ispell-filter)
2071 ;; now check spelling of word.
2072 (ispell-send-string "%\n") ;put in verbose mode
2073 (ispell-send-string (concat "^" word "\n"))
2074 ;; wait until ispell has processed word
2075 (while (progn
2076 (accept-process-output ispell-process)
2077 (not (string= "" (car ispell-filter)))))
2078 ;; Remove leading empty element
2079 (setq ispell-filter (cdr ispell-filter))
2080 ;; ispell process should return something after word is sent.
2081 ;; Tag word as valid (i.e., skip) otherwise
2082 (or ispell-filter
2083 (setq ispell-filter '(*)))
2084 (if (consp ispell-filter)
2085 (setq poss (ispell-parse-output (car ispell-filter))))
2086 (cond
2087 ((or (eq poss t) (stringp poss))
2088 ;; don't correct word
2089 t)
2090 ((null poss)
2091 ;; ispell error
2092 (error "Ispell: error in Ispell process"))
2093 ((featurep 'xemacs)
2094 (flyspell-xemacs-popup
2095 poss word cursor-location start end opoint))
2096 (t
2097 ;; The word is incorrect, we have to propose a replacement.
2098 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2099 poss word cursor-location start end opoint)))
2100 (ispell-pdict-save t)))))
2101
2102 ;;*---------------------------------------------------------------------*/
2103 ;;* flyspell-do-correct ... */
2104 ;;*---------------------------------------------------------------------*/
2105 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2106 "The popup menu callback."
2107 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2108 ;; it instead right after calling the function.
2109 (cond ((eq replace 'ignore)
2110 (goto-char save)
2111 nil)
2112 ((eq replace 'save)
2113 (goto-char save)
2114 (ispell-send-string (concat "*" word "\n"))
2115 ;; This was added only to the XEmacs side in revision 1.18 of
2116 ;; flyspell. I assume its absence on the Emacs side was an
2117 ;; oversight. --Stef
2118 (ispell-send-string "#\n")
2119 (flyspell-unhighlight-at cursor-location)
2120 (setq ispell-pdict-modified-p '(t)))
2121 ((or (eq replace 'buffer) (eq replace 'session))
2122 (ispell-send-string (concat "@" word "\n"))
2123 (flyspell-unhighlight-at cursor-location)
2124 (if (null ispell-pdict-modified-p)
2125 (setq ispell-pdict-modified-p
2126 (list ispell-pdict-modified-p)))
2127 (goto-char save)
2128 (if (eq replace 'buffer)
2129 (ispell-add-per-file-word-list word)))
2130 (replace
2131 ;; This was added only to the Emacs side. I assume its absence on
2132 ;; the XEmacs side was an oversight. --Stef
2133 (flyspell-unhighlight-at cursor-location)
2134 (let ((old-max (point-max))
2135 (new-word (if (atom replace)
2136 replace
2137 (car replace)))
2138 (cursor-location (+ (- (length word) (- end start))
2139 cursor-location)))
2140 (unless (equal new-word (car poss))
2141 (delete-region start end)
2142 (goto-char start)
2143 (funcall flyspell-insert-function new-word)
2144 (if flyspell-abbrev-p
2145 (flyspell-define-abbrev word new-word)))
2146 ;; In the original Emacs code, this was only called in the body
2147 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2148 (flyspell-ajust-cursor-point save cursor-location old-max)))
2149 (t
2150 (goto-char save)
2151 nil)))
2152
2153 ;;*---------------------------------------------------------------------*/
2154 ;;* flyspell-ajust-cursor-point ... */
2155 ;;*---------------------------------------------------------------------*/
2156 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2157 (if (>= save cursor-location)
2158 (let ((new-pos (+ save (- (point-max) old-max))))
2159 (goto-char (cond
2160 ((< new-pos (point-min))
2161 (point-min))
2162 ((> new-pos (point-max))
2163 (point-max))
2164 (t new-pos))))
2165 (goto-char save)))
2166
2167 ;;*---------------------------------------------------------------------*/
2168 ;;* flyspell-emacs-popup ... */
2169 ;;*---------------------------------------------------------------------*/
2170 (defun flyspell-emacs-popup (event poss word)
2171 "The Emacs popup menu."
2172 (unless window-system
2173 (error "This command requires pop-up dialogs"))
2174 (if (not event)
2175 (let* ((mouse-pos (mouse-position))
2176 (mouse-pos (if (nth 1 mouse-pos)
2177 mouse-pos
2178 (set-mouse-position (car mouse-pos)
2179 (/ (frame-width) 2) 2)
2180 (mouse-position))))
2181 (setq event (list (list (car (cdr mouse-pos))
2182 (1+ (cdr (cdr mouse-pos))))
2183 (car mouse-pos)))))
2184 (let* ((corrects (if flyspell-sort-corrections
2185 (sort (car (cdr (cdr poss))) 'string<)
2186 (car (cdr (cdr poss)))))
2187 (cor-menu (if (consp corrects)
2188 (mapcar (lambda (correct)
2189 (list correct correct))
2190 corrects)
2191 '()))
2192 (affix (car (cdr (cdr (cdr poss)))))
2193 show-affix-info
2194 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2195 (list
2196 (list (concat "Save affix: " (car affix))
2197 'save)
2198 '("Accept (session)" session)
2199 '("Accept (buffer)" buffer))
2200 '(("Save word" save)
2201 ("Accept (session)" session)
2202 ("Accept (buffer)" buffer)))))
2203 (if (consp cor-menu)
2204 (append cor-menu (cons "" save))
2205 save)))
2206 (menu (cons "flyspell correction menu" base-menu)))
2207 (car (x-popup-menu event
2208 (list (format "%s [%s]" word (or ispell-local-dictionary
2209 ispell-dictionary))
2210 menu)))))
2211
2212 ;;*---------------------------------------------------------------------*/
2213 ;;* flyspell-xemacs-popup ... */
2214 ;;*---------------------------------------------------------------------*/
2215 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2216 "The XEmacs popup menu."
2217 (let* ((corrects (if flyspell-sort-corrections
2218 (sort (car (cdr (cdr poss))) 'string<)
2219 (car (cdr (cdr poss)))))
2220 (cor-menu (if (consp corrects)
2221 (mapcar (lambda (correct)
2222 (vector correct
2223 (list 'flyspell-do-correct
2224 correct
2225 (list 'quote poss)
2226 word
2227 cursor-location
2228 start
2229 end
2230 save)
2231 t))
2232 corrects)
2233 '()))
2234 (affix (car (cdr (cdr (cdr poss)))))
2235 show-affix-info
2236 (menu (let ((save (if (and (consp affix) show-affix-info)
2237 (vector
2238 (concat "Save affix: " (car affix))
2239 (list 'flyspell-do-correct
2240 ''save
2241 (list 'quote poss)
2242 word
2243 cursor-location
2244 start
2245 end
2246 save)
2247 t)
2248 (vector
2249 "Save word"
2250 (list 'flyspell-do-correct
2251 ''save
2252 (list 'quote poss)
2253 word
2254 cursor-location
2255 start
2256 end
2257 save)
2258 t)))
2259 (session (vector "Accept (session)"
2260 (list 'flyspell-do-correct
2261 ''session
2262 (list 'quote poss)
2263 word
2264 cursor-location
2265 start
2266 end
2267 save)
2268 t))
2269 (buffer (vector "Accept (buffer)"
2270 (list 'flyspell-do-correct
2271 ''buffer
2272 (list 'quote poss)
2273 word
2274 cursor-location
2275 start
2276 end
2277 save)
2278 t)))
2279 (if (consp cor-menu)
2280 (append cor-menu (list "-" save session buffer))
2281 (list save session buffer)))))
2282 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2283 ispell-dictionary))
2284 menu))))
2285
2286 ;;*---------------------------------------------------------------------*/
2287 ;;* Some example functions for real autocorrecting */
2288 ;;*---------------------------------------------------------------------*/
2289 (defun flyspell-maybe-correct-transposition (beg end poss)
2290 "Check replacements for transposed characters.
2291
2292 If the text between BEG and END is equal to a correction suggested by
2293 Ispell, after transposing two adjacent characters, correct the text,
2294 and return t.
2295
2296 The third arg POSS is either the symbol 'doublon' or a list of
2297 possible corrections as returned by `ispell-parse-output'.
2298
2299 This function is meant to be added to `flyspell-incorrect-hook'."
2300 (when (consp poss)
2301 (catch 'done
2302 (let ((str (buffer-substring beg end))
2303 (i 0) (len (- end beg)) tmp)
2304 (while (< (1+ i) len)
2305 (setq tmp (aref str i))
2306 (aset str i (aref str (1+ i)))
2307 (aset str (1+ i) tmp)
2308 (when (member str (nth 2 poss))
2309 (save-excursion
2310 (goto-char (+ beg i 1))
2311 (transpose-chars 1))
2312 (throw 'done t))
2313 (setq tmp (aref str i))
2314 (aset str i (aref str (1+ i)))
2315 (aset str (1+ i) tmp)
2316 (setq i (1+ i))))
2317 nil)))
2318
2319 (defun flyspell-maybe-correct-doubling (beg end poss)
2320 "Check replacements for doubled characters.
2321
2322 If the text between BEG and END is equal to a correction suggested by
2323 Ispell, after removing a pair of doubled characters, correct the text,
2324 and return t.
2325
2326 The third arg POSS is either the symbol 'doublon' or a list of
2327 possible corrections as returned by `ispell-parse-output'.
2328
2329 This function is meant to be added to `flyspell-incorrect-hook'."
2330 (when (consp poss)
2331 (catch 'done
2332 (let ((str (buffer-substring beg end))
2333 (i 0) (len (- end beg)))
2334 (while (< (1+ i) len)
2335 (when (and (= (aref str i) (aref str (1+ i)))
2336 (member (concat (substring str 0 (1+ i))
2337 (substring str (+ i 2)))
2338 (nth 2 poss)))
2339 (goto-char (+ beg i))
2340 (delete-char 1)
2341 (throw 'done t))
2342 (setq i (1+ i))))
2343 nil)))
2344
2345 ;;*---------------------------------------------------------------------*/
2346 ;;* flyspell-already-abbrevp ... */
2347 ;;*---------------------------------------------------------------------*/
2348 (defun flyspell-already-abbrevp (table word)
2349 (let ((sym (abbrev-symbol word table)))
2350 (and sym (symbolp sym))))
2351
2352 ;;*---------------------------------------------------------------------*/
2353 ;;* flyspell-change-abbrev ... */
2354 ;;*---------------------------------------------------------------------*/
2355 (defun flyspell-change-abbrev (table old new)
2356 (set (abbrev-symbol old table) new))
2357
2358 (provide 'flyspell)
2359
2360 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2361 ;;; flyspell.el ends here