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