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