Fix bug #9221 with memory leak in bidi display.
[emacs.git] / lisp / textmodes / flyspell.el
blobe6837d0abdeec71282d414f1e97a3b75c02f75d3
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 &optional ignore-case)
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)
1004 (if ignore-case
1005 (string-equal (downcase (car lw)) (downcase word))
1006 (string-equal (car lw) word)))
1007 (setq r p)
1008 (goto-char p))))
1009 r)))
1011 ;;*---------------------------------------------------------------------*/
1012 ;;* flyspell-word-search-forward ... */
1013 ;;*---------------------------------------------------------------------*/
1014 (defun flyspell-word-search-forward (word bound)
1015 (save-excursion
1016 (let ((r '())
1017 (inhibit-point-motion-hooks t)
1019 (while (and (not r) (setq p (search-forward word bound t)))
1020 (let ((lw (flyspell-get-word)))
1021 (if (and (consp lw) (string-equal (car lw) word))
1022 (setq r p)
1023 (goto-char (1+ p)))))
1024 r)))
1026 ;;*---------------------------------------------------------------------*/
1027 ;;* flyspell-word ... */
1028 ;;*---------------------------------------------------------------------*/
1029 (defun flyspell-word (&optional following known-misspelling)
1030 "Spell check a word.
1031 If the optional argument FOLLOWING, or, when called interactively
1032 `ispell-following-word', is non-nil, checks the following (rather
1033 than preceding) word when the cursor is not over a word. If
1034 optional argument KNOWN-MISSPELLING is non nil considers word a
1035 misspelling and skips redundant spell-checking step."
1036 (interactive (list ispell-following-word))
1037 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1038 (save-excursion
1039 ;; use the correct dictionary
1040 (flyspell-accept-buffer-local-defs)
1041 (let* ((cursor-location (point))
1042 (flyspell-word (flyspell-get-word following))
1043 start end poss word ispell-filter)
1044 (if (or (eq flyspell-word nil)
1045 (and (fboundp flyspell-generic-check-word-predicate)
1046 (not (funcall flyspell-generic-check-word-predicate))))
1048 (progn
1049 ;; destructure return flyspell-word info list.
1050 (setq start (car (cdr flyspell-word))
1051 end (car (cdr (cdr flyspell-word)))
1052 word (car flyspell-word))
1053 ;; before checking in the directory, we check for doublons.
1054 (cond
1055 ((and (or (not (eq ispell-parser 'tex))
1056 (and (> start (point-min))
1057 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1058 flyspell-mark-duplications-flag
1059 (not (catch 'exception
1060 (let ((dict (or ispell-local-dictionary
1061 ispell-dictionary)))
1062 (dolist (except flyspell-mark-duplications-exceptions)
1063 (and (or (null (car except))
1064 (and (stringp dict)
1065 (string-match (car except) dict)))
1066 (member (downcase word) (cdr except))
1067 (throw 'exception t))))))
1068 (save-excursion
1069 (goto-char start)
1070 (let* ((bound
1071 (- start
1072 (- end start)
1073 (- (skip-chars-backward " \t\n\f"))))
1074 (p (when (>= bound (point-min))
1075 (flyspell-word-search-backward word bound t))))
1076 (and p (/= p start)))))
1077 ;; yes, this is a doublon
1078 (flyspell-highlight-incorrect-region start end 'doublon)
1079 nil)
1080 ((and (eq flyspell-word-cache-start start)
1081 (eq flyspell-word-cache-end end)
1082 (string-equal flyspell-word-cache-word word))
1083 ;; this word had been already checked, we skip
1084 flyspell-word-cache-result)
1085 ((and (eq ispell-parser 'tex)
1086 (flyspell-tex-command-p flyspell-word))
1087 ;; this is a correct word (because a tex command)
1088 (flyspell-unhighlight-at start)
1089 (if (> end start)
1090 (flyspell-unhighlight-at (- end 1)))
1093 ;; we setup the cache
1094 (setq flyspell-word-cache-start start)
1095 (setq flyspell-word-cache-end end)
1096 (setq flyspell-word-cache-word word)
1097 ;; now check spelling of word.
1098 (if (not known-misspelling)
1099 (progn
1100 (ispell-send-string "%\n")
1101 ;; put in verbose mode
1102 (ispell-send-string (concat "^" word "\n"))
1103 ;; we mark the ispell process so it can be killed
1104 ;; when emacs is exited without query
1105 (set-process-query-on-exit-flag ispell-process nil)
1106 ;; Wait until ispell has processed word. Since this
1107 ;; code is often executed from post-command-hook but
1108 ;; the ispell process may not be responsive, it's
1109 ;; important to make sure we re-enable C-g.
1110 (with-local-quit
1111 (while (progn
1112 (accept-process-output ispell-process)
1113 (not (string= "" (car ispell-filter))))))
1114 ;; (ispell-send-string "!\n")
1115 ;; back to terse mode.
1116 ;; Remove leading empty element
1117 (setq ispell-filter (cdr ispell-filter))
1118 ;; ispell process should return something after word is sent.
1119 ;; Tag word as valid (i.e., skip) otherwise
1120 (or ispell-filter
1121 (setq ispell-filter '(*)))
1122 (if (consp ispell-filter)
1123 (setq poss (ispell-parse-output (car ispell-filter)))))
1124 ;; Else, this was a known misspelling to begin with, and
1125 ;; we should forge an ispell return value.
1126 (setq poss (list word 0 '() '())))
1127 (let ((res (cond ((eq poss t)
1128 ;; correct
1129 (setq flyspell-word-cache-result t)
1130 (flyspell-unhighlight-at start)
1131 (if (> end start)
1132 (flyspell-unhighlight-at (- end 1)))
1134 ((and (stringp poss) flyspell-highlight-flag)
1135 ;; correct
1136 (setq flyspell-word-cache-result t)
1137 (flyspell-unhighlight-at start)
1138 (if (> end start)
1139 (flyspell-unhighlight-at (- end 1)))
1141 ((null poss)
1142 (setq flyspell-word-cache-result t)
1143 (flyspell-unhighlight-at start)
1144 (if (> end start)
1145 (flyspell-unhighlight-at (- end 1)))
1147 ((or (and (< flyspell-duplicate-distance 0)
1148 (or (save-excursion
1149 (goto-char start)
1150 (flyspell-word-search-backward
1151 word
1152 (point-min)))
1153 (save-excursion
1154 (goto-char end)
1155 (flyspell-word-search-forward
1156 word
1157 (point-max)))))
1158 (and (> flyspell-duplicate-distance 0)
1159 (or (save-excursion
1160 (goto-char start)
1161 (flyspell-word-search-backward
1162 word
1163 (- start
1164 flyspell-duplicate-distance)))
1165 (save-excursion
1166 (goto-char end)
1167 (flyspell-word-search-forward
1168 word
1169 (+ end
1170 flyspell-duplicate-distance))))))
1171 ;; This is a misspelled word which occurs
1172 ;; twice within flyspell-duplicate-distance.
1173 (setq flyspell-word-cache-result nil)
1174 (if flyspell-highlight-flag
1175 (flyspell-highlight-duplicate-region
1176 start end poss)
1177 (message "duplicate `%s'" word))
1178 nil)
1180 (setq flyspell-word-cache-result nil)
1181 ;; Highlight the location as incorrect,
1182 ;; including offset specified in POSS.
1183 (if flyspell-highlight-flag
1184 (flyspell-highlight-incorrect-region
1185 (if (and (consp poss)
1186 (integerp (nth 1 poss)))
1187 (+ start (nth 1 poss) -1)
1188 start)
1189 end poss)
1190 (flyspell-notify-misspell word poss))
1191 nil))))
1192 ;; return to original location
1193 (goto-char cursor-location)
1194 (if ispell-quit (setq ispell-quit nil))
1195 res))))))))
1197 ;;*---------------------------------------------------------------------*/
1198 ;;* flyspell-math-tex-command-p ... */
1199 ;;* ------------------------------------------------------------- */
1200 ;;* This function uses the texmathp package to check if point */
1201 ;;* is within a TeX math environment. `texmathp' can yield errors */
1202 ;;* if the document is currently not valid TeX syntax. */
1203 ;;*---------------------------------------------------------------------*/
1204 (defun flyspell-math-tex-command-p ()
1205 (when (fboundp 'texmathp)
1206 (if flyspell-check-tex-math-command
1208 (condition-case nil
1209 (texmathp)
1210 (error nil)))))
1212 ;;*---------------------------------------------------------------------*/
1213 ;;* flyspell-tex-command-p ... */
1214 ;;*---------------------------------------------------------------------*/
1215 (defun flyspell-tex-command-p (word)
1216 "Return t if WORD is a TeX command."
1217 (or (save-excursion
1218 (let ((b (car (cdr word))))
1219 (and (re-search-backward "\\\\" (- (point) 100) t)
1220 (or (= (match-end 0) b)
1221 (and (goto-char (match-end 0))
1222 (looking-at flyspell-tex-command-regexp)
1223 (>= (match-end 0) b))))))
1224 (flyspell-math-tex-command-p)))
1226 ;;*---------------------------------------------------------------------*/
1227 ;;* flyspell-casechars-cache ... */
1228 ;;*---------------------------------------------------------------------*/
1229 (defvar flyspell-casechars-cache nil)
1230 (defvar flyspell-ispell-casechars-cache nil)
1231 (make-variable-buffer-local 'flyspell-casechars-cache)
1232 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1234 ;;*---------------------------------------------------------------------*/
1235 ;;* flyspell-get-casechars ... */
1236 ;;*---------------------------------------------------------------------*/
1237 (defun flyspell-get-casechars ()
1238 "This function builds a string that is the regexp of word chars.
1239 In order to avoid one useless string construction,
1240 this function changes the last char of the `ispell-casechars' string."
1241 (let ((ispell-casechars (ispell-get-casechars)))
1242 (cond
1243 ((eq ispell-parser 'tex)
1244 (setq flyspell-ispell-casechars-cache ispell-casechars)
1245 (setq flyspell-casechars-cache
1246 (concat (substring ispell-casechars
1248 (- (length ispell-casechars) 1))
1249 "]"))
1250 flyspell-casechars-cache)
1252 (setq flyspell-ispell-casechars-cache ispell-casechars)
1253 (setq flyspell-casechars-cache ispell-casechars)
1254 flyspell-casechars-cache))))
1256 ;;*---------------------------------------------------------------------*/
1257 ;;* flyspell-get-not-casechars-cache ... */
1258 ;;*---------------------------------------------------------------------*/
1259 (defvar flyspell-not-casechars-cache nil)
1260 (defvar flyspell-ispell-not-casechars-cache nil)
1261 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1262 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1264 ;;*---------------------------------------------------------------------*/
1265 ;;* flyspell-get-not-casechars ... */
1266 ;;*---------------------------------------------------------------------*/
1267 (defun flyspell-get-not-casechars ()
1268 "This function builds a string that is the regexp of non-word chars."
1269 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1270 (cond
1271 ((eq ispell-parser 'tex)
1272 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1273 (setq flyspell-not-casechars-cache
1274 (concat (substring ispell-not-casechars
1276 (- (length ispell-not-casechars) 1))
1277 "]"))
1278 flyspell-not-casechars-cache)
1280 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1281 (setq flyspell-not-casechars-cache ispell-not-casechars)
1282 flyspell-not-casechars-cache))))
1284 ;;*---------------------------------------------------------------------*/
1285 ;;* flyspell-get-word ... */
1286 ;;*---------------------------------------------------------------------*/
1287 (defun flyspell-get-word (&optional following extra-otherchars)
1288 "Return the word for spell-checking according to Ispell syntax.
1289 Optional argument FOLLOWING non-nil means to get the following
1290 \(rather than preceding) word when the cursor is not over a word.
1291 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1292 that may be included as part of a word (see `ispell-dictionary-alist')."
1293 (let* ((flyspell-casechars (flyspell-get-casechars))
1294 (flyspell-not-casechars (flyspell-get-not-casechars))
1295 (ispell-otherchars (ispell-get-otherchars))
1296 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1297 (word-regexp (concat flyspell-casechars
1298 "+\\("
1299 (if (not (string= "" ispell-otherchars))
1300 (concat ispell-otherchars "?"))
1301 (if extra-otherchars
1302 (concat extra-otherchars "?"))
1303 flyspell-casechars
1304 "+\\)"
1305 (if (or ispell-many-otherchars-p
1306 extra-otherchars)
1307 "*" "?")))
1308 did-it-once prevpt
1309 start end word)
1310 ;; find the word
1311 (if (not (looking-at flyspell-casechars))
1312 (if following
1313 (re-search-forward flyspell-casechars nil t)
1314 (re-search-backward flyspell-casechars nil t)))
1315 ;; move to front of word
1316 (re-search-backward flyspell-not-casechars nil 'start)
1317 (while (and (or (and (not (string= "" ispell-otherchars))
1318 (looking-at ispell-otherchars))
1319 (and extra-otherchars (looking-at extra-otherchars)))
1320 (not (bobp))
1321 (or (not did-it-once)
1322 ispell-many-otherchars-p)
1323 (not (eq prevpt (point))))
1324 (if (and extra-otherchars (looking-at extra-otherchars))
1325 (progn
1326 (backward-char 1)
1327 (if (looking-at flyspell-casechars)
1328 (re-search-backward flyspell-not-casechars nil 'move)))
1329 (setq did-it-once t
1330 prevpt (point))
1331 (backward-char 1)
1332 (if (looking-at flyspell-casechars)
1333 (re-search-backward flyspell-not-casechars nil 'move)
1334 (backward-char -1))))
1335 ;; Now mark the word and save to string.
1336 (if (not (re-search-forward word-regexp nil t))
1338 (progn
1339 (setq start (match-beginning 0)
1340 end (point)
1341 word (buffer-substring-no-properties start end))
1342 (list word start end)))))
1344 ;;*---------------------------------------------------------------------*/
1345 ;;* flyspell-small-region ... */
1346 ;;*---------------------------------------------------------------------*/
1347 (defun flyspell-small-region (beg end)
1348 "Flyspell text between BEG and END."
1349 (save-excursion
1350 (if (> beg end)
1351 (let ((old beg))
1352 (setq beg end)
1353 (setq end old)))
1354 (goto-char beg)
1355 (let ((count 0))
1356 (while (< (point) end)
1357 (if (and flyspell-issue-message-flag (= count 100))
1358 (progn
1359 (message "Spell Checking...%d%%"
1360 (* 100 (/ (float (- (point) beg)) (- end beg))))
1361 (setq count 0))
1362 (setq count (+ 1 count)))
1363 (flyspell-word)
1364 (sit-for 0)
1365 (let ((cur (point)))
1366 (forward-word 1)
1367 (if (and (< (point) end) (> (point) (+ cur 1)))
1368 (backward-char 1)))))
1369 (backward-char 1)
1370 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1371 (flyspell-word)))
1373 ;;*---------------------------------------------------------------------*/
1374 ;;* flyspell-external-ispell-process ... */
1375 ;;*---------------------------------------------------------------------*/
1376 (defvar flyspell-external-ispell-process '()
1377 "The external Flyspell Ispell process.")
1379 ;;*---------------------------------------------------------------------*/
1380 ;;* flyspell-external-ispell-buffer ... */
1381 ;;*---------------------------------------------------------------------*/
1382 (defvar flyspell-external-ispell-buffer '())
1383 (defvar flyspell-large-region-buffer '())
1384 (defvar flyspell-large-region-beg (point-min))
1385 (defvar flyspell-large-region-end (point-max))
1387 ;;*---------------------------------------------------------------------*/
1388 ;;* flyspell-external-point-words ... */
1389 ;;*---------------------------------------------------------------------*/
1390 (defun flyspell-external-point-words ()
1391 "Mark words from a buffer listing incorrect words in order of appearance.
1392 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1393 \(We finish by killing that buffer and setting the variable to nil.)
1394 The buffer to mark them in is `flyspell-large-region-buffer'."
1395 (let (words-not-found
1396 (ispell-otherchars (ispell-get-otherchars))
1397 (buffer-scan-pos flyspell-large-region-beg)
1398 case-fold-search)
1399 (with-current-buffer flyspell-external-ispell-buffer
1400 (goto-char (point-min))
1401 ;; Loop over incorrect words, in the order they were reported,
1402 ;; which is also the order they appear in the buffer being checked.
1403 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1404 ;; Bind WORD to the next one.
1405 (let ((word (match-string 1)) (wordpos (point)))
1406 ;; Here there used to be code to see if WORD is the same
1407 ;; as the previous iteration, and count the number of consecutive
1408 ;; identical words, and the loop below would search for that many.
1409 ;; That code seemed to be incorrect, and on principle, should
1410 ;; be unnecessary too. -- rms.
1411 (if flyspell-issue-message-flag
1412 (message "Spell Checking...%d%% [%s]"
1413 (* 100 (/ (float (point)) (point-max)))
1414 word))
1415 (with-current-buffer flyspell-large-region-buffer
1416 (goto-char buffer-scan-pos)
1417 (let ((keep t))
1418 ;; Iterate on string search until string is found as word,
1419 ;; not as substring
1420 (while keep
1421 (if (search-forward word
1422 flyspell-large-region-end t)
1423 (let* ((found-list
1424 (save-excursion
1425 ;; Move back into the match
1426 ;; so flyspell-get-word will find it.
1427 (forward-char -1)
1428 (flyspell-get-word)))
1429 (found (car found-list))
1430 (found-length (length found))
1431 (misspell-length (length word)))
1432 (when (or
1433 ;; Size matches, we really found it.
1434 (= found-length misspell-length)
1435 ;; Matches as part of a boundary-char separated word
1436 (member word
1437 (split-string found ispell-otherchars))
1438 ;; Misspelling has higher length than
1439 ;; what flyspell considers the
1440 ;; word. Caused by boundary-chars
1441 ;; mismatch. Validating seems safe.
1442 (< found-length misspell-length)
1443 ;; ispell treats beginning of some TeX
1444 ;; commands as nroff control sequences
1445 ;; and strips them in the list of
1446 ;; misspelled words thus giving a
1447 ;; non-existent word. Skip if ispell
1448 ;; is used, string is a TeX command
1449 ;; (char before beginning of word is
1450 ;; backslash) and none of the previous
1451 ;; contitions match
1452 (and (not ispell-really-aspell)
1453 (save-excursion
1454 (goto-char (- (nth 1 found-list) 1))
1455 (if (looking-at "[\\]" )
1457 nil))))
1458 (setq keep nil)
1459 (flyspell-word nil t)
1460 ;; Search for next misspelled word will begin from
1461 ;; end of last validated match.
1462 (setq buffer-scan-pos (point))))
1463 ;; Record if misspelling is not found and try new one
1464 (add-to-list 'words-not-found
1465 (concat " -> " word " - "
1466 (int-to-string wordpos)))
1467 (setq keep nil)))))))
1468 ;; we are done
1469 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1470 ;; Warn about not found misspellings
1471 (dolist (word words-not-found)
1472 (message "%s: word not found" word))
1473 ;; Kill and forget the buffer with the list of incorrect words.
1474 (kill-buffer flyspell-external-ispell-buffer)
1475 (setq flyspell-external-ispell-buffer nil)))
1477 ;;*---------------------------------------------------------------------*/
1478 ;;* flyspell-process-localwords ... */
1479 ;;* ------------------------------------------------------------- */
1480 ;;* This function is used to prevent marking of words explicitly */
1481 ;;* declared correct. */
1482 ;;*---------------------------------------------------------------------*/
1483 (defun flyspell-process-localwords (misspellings-buffer)
1484 (let (localwords case-fold-search
1485 (ispell-casechars (ispell-get-casechars)))
1486 ;; Get localwords from the original buffer
1487 (save-excursion
1488 (goto-char (point-min))
1489 ;; Localwords parsing copied from ispell.el.
1490 (while (search-forward ispell-words-keyword nil t)
1491 (let ((end (point-at-eol))
1492 string)
1493 ;; buffer-local words separated by a space, and can contain
1494 ;; any character other than a space. Not rigorous enough.
1495 (while (re-search-forward " *\\([^ ]+\\)" end t)
1496 (setq string (buffer-substring-no-properties (match-beginning 1)
1497 (match-end 1)))
1498 ;; This can fail when string contains a word with invalid chars.
1499 ;; Error handling needs to be added between Ispell and Emacs.
1500 (if (and (< 1 (length string))
1501 (equal 0 (string-match ispell-casechars string)))
1502 (push string localwords))))))
1503 ;; Remove localwords matches from misspellings-buffer.
1504 ;; The usual mechanism of communicating the local words to ispell
1505 ;; does not affect the special ispell process used by
1506 ;; flyspell-large-region.
1507 (with-current-buffer misspellings-buffer
1508 (save-excursion
1509 (dolist (word localwords)
1510 (goto-char (point-min))
1511 (let ((regexp (concat "^" word "\n")))
1512 (while (re-search-forward regexp nil t)
1513 (delete-region (match-beginning 0) (match-end 0)))))))))
1515 ;;* ---------------------------------------------------------------
1516 ;;* flyspell-check-region-doublons
1517 ;;* ---------------------------------------------------------------
1518 (defun flyspell-check-region-doublons (beg end)
1519 "Check for adjacent duplicated words (doublons) in the given region."
1520 (save-excursion
1521 (goto-char beg)
1522 (flyspell-word) ; Make sure current word is checked
1523 (backward-word 1)
1524 (while (and (< (point) end)
1525 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1526 end 'move))
1527 (flyspell-word)
1528 (backward-word 1))
1529 (flyspell-word)))
1531 ;;*---------------------------------------------------------------------*/
1532 ;;* flyspell-large-region ... */
1533 ;;*---------------------------------------------------------------------*/
1534 (defun flyspell-large-region (beg end)
1535 (let* ((curbuf (current-buffer))
1536 (buffer (get-buffer-create "*flyspell-region*")))
1537 (setq flyspell-external-ispell-buffer buffer)
1538 (setq flyspell-large-region-buffer curbuf)
1539 (setq flyspell-large-region-beg beg)
1540 (setq flyspell-large-region-end end)
1541 (flyspell-accept-buffer-local-defs)
1542 (set-buffer buffer)
1543 (erase-buffer)
1544 ;; this is done, we can start checking...
1545 (if flyspell-issue-message-flag (message "Checking region..."))
1546 (set-buffer curbuf)
1547 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1548 ;; Local dictionary becomes the global dictionary in use.
1549 (setq ispell-current-dictionary
1550 (or ispell-local-dictionary ispell-dictionary))
1551 (setq ispell-current-personal-dictionary
1552 (or ispell-local-pdict ispell-personal-dictionary))
1553 (let ((args (ispell-get-ispell-args))
1554 (encoding (ispell-get-coding-system))
1556 (if (and ispell-current-dictionary ; use specified dictionary
1557 (not (member "-d" args))) ; only define if not overridden
1558 (setq args
1559 (append (list "-d" ispell-current-dictionary) args)))
1560 (if ispell-current-personal-dictionary ; use specified pers dict
1561 (setq args
1562 (append args
1563 (list "-p"
1564 (expand-file-name
1565 ispell-current-personal-dictionary)))))
1566 (setq args (append args ispell-extra-args))
1568 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1569 ;; for communication. ispell or older aspell/hunspell does not support this
1570 (if ispell-encoding8-command
1571 (setq args
1572 (append args
1573 (list
1574 (concat ispell-encoding8-command
1575 (symbol-name
1576 encoding))))))
1578 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1579 (setq c (apply 'ispell-call-process-region beg
1581 ispell-program-name
1583 buffer
1585 (if ispell-really-aspell "list" "-l")
1586 args)))
1587 (if (eq c 0)
1588 (progn
1589 (flyspell-process-localwords buffer)
1590 (with-current-buffer curbuf
1591 (flyspell-delete-region-overlays beg end)
1592 (flyspell-check-region-doublons beg end))
1593 (flyspell-external-point-words))
1594 (error "Can't check region")))))
1596 ;;*---------------------------------------------------------------------*/
1597 ;;* flyspell-region ... */
1598 ;;* ------------------------------------------------------------- */
1599 ;;* Because `ispell -a' is too slow, it is not possible to use */
1600 ;;* it on large region. Then, when ispell is invoked on a large */
1601 ;;* text region, a new `ispell -l' process is spawned. The */
1602 ;;* pointed out words are then searched in the region a checked with */
1603 ;;* regular flyspell means. */
1604 ;;*---------------------------------------------------------------------*/
1605 ;;;###autoload
1606 (defun flyspell-region (beg end)
1607 "Flyspell text between BEG and END."
1608 (interactive "r")
1609 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1610 (if (= beg end)
1612 (save-excursion
1613 (if (> beg end)
1614 (let ((old beg))
1615 (setq beg end)
1616 (setq end old)))
1617 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1618 (flyspell-large-region beg end)
1619 (flyspell-small-region beg end)))))
1621 ;;*---------------------------------------------------------------------*/
1622 ;;* flyspell-buffer ... */
1623 ;;*---------------------------------------------------------------------*/
1624 ;;;###autoload
1625 (defun flyspell-buffer ()
1626 "Flyspell whole buffer."
1627 (interactive)
1628 (flyspell-region (point-min) (point-max)))
1630 ;;*---------------------------------------------------------------------*/
1631 ;;* old next error position ... */
1632 ;;*---------------------------------------------------------------------*/
1633 (defvar flyspell-old-buffer-error nil)
1634 (defvar flyspell-old-pos-error nil)
1636 ;;*---------------------------------------------------------------------*/
1637 ;;* flyspell-goto-next-error ... */
1638 ;;*---------------------------------------------------------------------*/
1639 (defun flyspell-goto-next-error ()
1640 "Go to the next previously detected error.
1641 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1642 FLYSPELL-BUFFER."
1643 (interactive)
1644 (let ((pos (point))
1645 (max (point-max)))
1646 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1647 (eq pos flyspell-old-pos-error))
1648 (progn
1649 (if (= flyspell-old-pos-error max)
1650 ;; goto beginning of buffer
1651 (progn
1652 (message "Restarting from beginning of buffer")
1653 (goto-char (point-min)))
1654 (forward-word 1))
1655 (setq pos (point))))
1656 ;; seek the next error
1657 (while (and (< pos max)
1658 (let ((ovs (overlays-at pos))
1659 (r '()))
1660 (while (and (not r) (consp ovs))
1661 (if (flyspell-overlay-p (car ovs))
1662 (setq r t)
1663 (setq ovs (cdr ovs))))
1664 (not r)))
1665 (setq pos (1+ pos)))
1666 ;; save the current location for next invocation
1667 (setq flyspell-old-pos-error pos)
1668 (setq flyspell-old-buffer-error (current-buffer))
1669 (goto-char pos)
1670 (if (= pos max)
1671 (message "No more miss-spelled word!"))))
1673 ;;*---------------------------------------------------------------------*/
1674 ;;* flyspell-overlay-p ... */
1675 ;;*---------------------------------------------------------------------*/
1676 (defun flyspell-overlay-p (o)
1677 "Return true if O is an overlay used by flyspell."
1678 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1680 ;;*---------------------------------------------------------------------*/
1681 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1682 ;;* ------------------------------------------------------------- */
1683 ;;* Remove overlays introduced by flyspell. */
1684 ;;*---------------------------------------------------------------------*/
1685 (defun flyspell-delete-region-overlays (beg end)
1686 "Delete overlays used by flyspell in a given region."
1687 (remove-overlays beg end 'flyspell-overlay t))
1690 (defun flyspell-delete-all-overlays ()
1691 "Delete all the overlays used by flyspell."
1692 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1694 ;;*---------------------------------------------------------------------*/
1695 ;;* flyspell-unhighlight-at ... */
1696 ;;*---------------------------------------------------------------------*/
1697 (defun flyspell-unhighlight-at (pos)
1698 "Remove the flyspell overlay that are located at POS."
1699 (if flyspell-persistent-highlight
1700 (let ((overlays (overlays-at pos)))
1701 (while (consp overlays)
1702 (if (flyspell-overlay-p (car overlays))
1703 (delete-overlay (car overlays)))
1704 (setq overlays (cdr overlays))))
1705 (if (flyspell-overlay-p flyspell-overlay)
1706 (delete-overlay flyspell-overlay))))
1708 ;;*---------------------------------------------------------------------*/
1709 ;;* flyspell-properties-at-p ... */
1710 ;;* ------------------------------------------------------------- */
1711 ;;* Is there an highlight properties at position pos? */
1712 ;;*---------------------------------------------------------------------*/
1713 (defun flyspell-properties-at-p (pos)
1714 "Return t if there is a text property at POS, not counting `local-map'.
1715 If variable `flyspell-highlight-properties' is set to nil,
1716 text with properties are not checked. This function is used to discover
1717 if the character at POS has any other property."
1718 (let ((prop (text-properties-at pos))
1719 (keep t))
1720 (while (and keep (consp prop))
1721 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1722 (setq prop (cdr (cdr prop)))
1723 (setq keep nil)))
1724 (consp prop)))
1726 ;;*---------------------------------------------------------------------*/
1727 ;;* make-flyspell-overlay ... */
1728 ;;*---------------------------------------------------------------------*/
1729 (defun make-flyspell-overlay (beg end face mouse-face)
1730 "Allocate an overlay to highlight an incorrect word.
1731 BEG and END specify the range in the buffer of that word.
1732 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1733 for the overlay."
1734 (let ((overlay (make-overlay beg end nil t nil)))
1735 (overlay-put overlay 'face face)
1736 (overlay-put overlay 'mouse-face mouse-face)
1737 (overlay-put overlay 'flyspell-overlay t)
1738 (overlay-put overlay 'evaporate t)
1739 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1740 (overlay-put overlay 'keymap flyspell-mouse-map)
1741 (when (eq face 'flyspell-incorrect)
1742 (and (stringp flyspell-before-incorrect-word-string)
1743 (overlay-put overlay 'before-string
1744 flyspell-before-incorrect-word-string))
1745 (and (stringp flyspell-after-incorrect-word-string)
1746 (overlay-put overlay 'after-string
1747 flyspell-after-incorrect-word-string)))
1748 overlay))
1750 ;;*---------------------------------------------------------------------*/
1751 ;;* flyspell-highlight-incorrect-region ... */
1752 ;;*---------------------------------------------------------------------*/
1753 (defun flyspell-highlight-incorrect-region (beg end poss)
1754 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1755 POSS is usually a list of possible spelling/correction lists,
1756 as returned by `ispell-parse-output'.
1757 It can also be the symbol `doublon', in the case where the word
1758 is itself incorrect, but suspiciously repeated."
1759 (let ((inhibit-read-only t))
1760 (unless (run-hook-with-args-until-success
1761 'flyspell-incorrect-hook beg end poss)
1762 (if (or flyspell-highlight-properties
1763 (not (flyspell-properties-at-p beg)))
1764 (progn
1765 ;; we cleanup all the overlay that are in the region, not
1766 ;; beginning at the word start position
1767 (if (< (1+ beg) end)
1768 (let ((os (overlays-in (1+ beg) end)))
1769 (while (consp os)
1770 (if (flyspell-overlay-p (car os))
1771 (delete-overlay (car os)))
1772 (setq os (cdr os)))))
1773 ;; we cleanup current overlay at the same position
1774 (flyspell-unhighlight-at beg)
1775 ;; now we can use a new overlay
1776 (setq flyspell-overlay
1777 (make-flyspell-overlay
1778 beg end
1779 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1780 'highlight)))))))
1782 ;;*---------------------------------------------------------------------*/
1783 ;;* flyspell-highlight-duplicate-region ... */
1784 ;;*---------------------------------------------------------------------*/
1785 (defun flyspell-highlight-duplicate-region (beg end poss)
1786 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1787 POSS is a list of possible spelling/correction lists,
1788 as returned by `ispell-parse-output'."
1789 (let ((inhibit-read-only t))
1790 (unless (run-hook-with-args-until-success
1791 'flyspell-incorrect-hook beg end poss)
1792 (if (or flyspell-highlight-properties
1793 (not (flyspell-properties-at-p beg)))
1794 (progn
1795 ;; we cleanup current overlay at the same position
1796 (flyspell-unhighlight-at beg)
1797 ;; now we can use a new overlay
1798 (setq flyspell-overlay
1799 (make-flyspell-overlay beg end
1800 'flyspell-duplicate
1801 'highlight)))))))
1803 ;;*---------------------------------------------------------------------*/
1804 ;;* flyspell-auto-correct-cache ... */
1805 ;;*---------------------------------------------------------------------*/
1806 (defvar flyspell-auto-correct-pos nil)
1807 (defvar flyspell-auto-correct-region nil)
1808 (defvar flyspell-auto-correct-ring nil)
1809 (defvar flyspell-auto-correct-word nil)
1810 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1811 (make-variable-buffer-local 'flyspell-auto-correct-region)
1812 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1813 (make-variable-buffer-local 'flyspell-auto-correct-word)
1815 ;;*---------------------------------------------------------------------*/
1816 ;;* flyspell-check-previous-highlighted-word ... */
1817 ;;*---------------------------------------------------------------------*/
1818 (defun flyspell-check-previous-highlighted-word (&optional arg)
1819 "Correct the closer misspelled word.
1820 This function scans a mis-spelled word before the cursor. If it finds one
1821 it proposes replacement for that word. With prefix arg, count that many
1822 misspelled words backwards."
1823 (interactive)
1824 (let ((pos1 (point))
1825 (pos (point))
1826 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1827 ov ovs)
1828 (if (catch 'exit
1829 (while (and (setq pos (previous-overlay-change pos))
1830 (not (= pos pos1)))
1831 (setq pos1 pos)
1832 (if (> pos (point-min))
1833 (progn
1834 (setq ovs (overlays-at (1- pos)))
1835 (while (consp ovs)
1836 (setq ov (car ovs))
1837 (setq ovs (cdr ovs))
1838 (if (and (flyspell-overlay-p ov)
1839 (= 0 (setq arg (1- arg))))
1840 (throw 'exit t)))))))
1841 (save-excursion
1842 (goto-char pos)
1843 (ispell-word)
1844 (setq flyspell-word-cache-word nil) ;; Force flyspell-word re-check
1845 (flyspell-word))
1846 (error "No word to correct before point"))))
1848 ;;*---------------------------------------------------------------------*/
1849 ;;* flyspell-display-next-corrections ... */
1850 ;;*---------------------------------------------------------------------*/
1851 (defun flyspell-display-next-corrections (corrections)
1852 (let ((string "Corrections:")
1853 (l corrections)
1854 (pos '()))
1855 (while (< (length string) 80)
1856 (if (equal (car l) flyspell-auto-correct-word)
1857 (setq pos (cons (+ 1 (length string)) pos)))
1858 (setq string (concat string " " (car l)))
1859 (setq l (cdr l)))
1860 (while (consp pos)
1861 (let ((num (car pos)))
1862 (put-text-property num
1863 (+ num (length flyspell-auto-correct-word))
1864 'face 'flyspell-incorrect
1865 string))
1866 (setq pos (cdr pos)))
1867 (if (fboundp 'display-message)
1868 (display-message 'no-log string)
1869 (message "%s" string))))
1871 ;;*---------------------------------------------------------------------*/
1872 ;;* flyspell-abbrev-table ... */
1873 ;;*---------------------------------------------------------------------*/
1874 (defun flyspell-abbrev-table ()
1875 (if flyspell-use-global-abbrev-table-p
1876 global-abbrev-table
1877 (or local-abbrev-table global-abbrev-table)))
1879 ;;*---------------------------------------------------------------------*/
1880 ;;* flyspell-define-abbrev ... */
1881 ;;*---------------------------------------------------------------------*/
1882 (defun flyspell-define-abbrev (name expansion)
1883 (let ((table (flyspell-abbrev-table)))
1884 (when table
1885 (define-abbrev table (downcase name) expansion))))
1887 ;;*---------------------------------------------------------------------*/
1888 ;;* flyspell-auto-correct-word ... */
1889 ;;*---------------------------------------------------------------------*/
1890 (defun flyspell-auto-correct-word ()
1891 "Correct the current word.
1892 This command proposes various successive corrections for the current word."
1893 (interactive)
1894 (let ((pos (point))
1895 (old-max (point-max)))
1896 ;; use the correct dictionary
1897 (flyspell-accept-buffer-local-defs)
1898 (if (and (eq flyspell-auto-correct-pos pos)
1899 (consp flyspell-auto-correct-region))
1900 ;; we have already been using the function at the same location
1901 (let* ((start (car flyspell-auto-correct-region))
1902 (len (cdr flyspell-auto-correct-region)))
1903 (flyspell-unhighlight-at start)
1904 (delete-region start (+ start len))
1905 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1906 (let* ((word (car flyspell-auto-correct-ring))
1907 (len (length word)))
1908 (rplacd flyspell-auto-correct-region len)
1909 (goto-char start)
1910 (if flyspell-abbrev-p
1911 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1912 flyspell-auto-correct-word)
1913 (flyspell-change-abbrev (flyspell-abbrev-table)
1914 flyspell-auto-correct-word
1915 word)
1916 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1917 (funcall flyspell-insert-function word)
1918 (flyspell-word)
1919 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1920 (flyspell-ajust-cursor-point pos (point) old-max)
1921 (setq flyspell-auto-correct-pos (point)))
1922 ;; fetch the word to be checked
1923 (let ((word (flyspell-get-word)))
1924 (if (consp word)
1925 (let ((start (car (cdr word)))
1926 (end (car (cdr (cdr word))))
1927 (word (car word))
1928 poss ispell-filter)
1929 (setq flyspell-auto-correct-word word)
1930 ;; now check spelling of word.
1931 (ispell-send-string "%\n") ;put in verbose mode
1932 (ispell-send-string (concat "^" word "\n"))
1933 ;; wait until ispell has processed word.
1934 (while (progn
1935 (accept-process-output ispell-process)
1936 (not (string= "" (car ispell-filter)))))
1937 ;; Remove leading empty element
1938 (setq ispell-filter (cdr ispell-filter))
1939 ;; ispell process should return something after word is sent.
1940 ;; Tag word as valid (i.e., skip) otherwise
1941 (or ispell-filter
1942 (setq ispell-filter '(*)))
1943 (if (consp ispell-filter)
1944 (setq poss (ispell-parse-output (car ispell-filter))))
1945 (cond
1946 ((or (eq poss t) (stringp poss))
1947 ;; don't correct word
1949 ((null poss)
1950 ;; ispell error
1951 (error "Ispell: error in Ispell process"))
1953 ;; the word is incorrect, we have to propose a replacement
1954 (let ((replacements (if flyspell-sort-corrections
1955 (sort (car (cdr (cdr poss))) 'string<)
1956 (car (cdr (cdr poss))))))
1957 (setq flyspell-auto-correct-region nil)
1958 (if (consp replacements)
1959 (progn
1960 (let ((replace (car replacements)))
1961 (let ((new-word replace))
1962 (if (not (equal new-word (car poss)))
1963 (progn
1964 ;; the save the current replacements
1965 (setq flyspell-auto-correct-region
1966 (cons start (length new-word)))
1967 (let ((l replacements))
1968 (while (consp (cdr l))
1969 (setq l (cdr l)))
1970 (rplacd l (cons (car poss) replacements)))
1971 (setq flyspell-auto-correct-ring
1972 replacements)
1973 (flyspell-unhighlight-at start)
1974 (delete-region start end)
1975 (funcall flyspell-insert-function new-word)
1976 (if flyspell-abbrev-p
1977 (if (flyspell-already-abbrevp
1978 (flyspell-abbrev-table) word)
1979 (flyspell-change-abbrev
1980 (flyspell-abbrev-table)
1981 word
1982 new-word)
1983 (flyspell-define-abbrev word
1984 new-word)))
1985 (flyspell-word)
1986 (flyspell-display-next-corrections
1987 (cons new-word flyspell-auto-correct-ring))
1988 (flyspell-ajust-cursor-point pos
1989 (point)
1990 old-max))))))))))
1991 (setq flyspell-auto-correct-pos (point))
1992 (ispell-pdict-save t)))))))
1994 ;;*---------------------------------------------------------------------*/
1995 ;;* flyspell-auto-correct-previous-pos ... */
1996 ;;*---------------------------------------------------------------------*/
1997 (defvar flyspell-auto-correct-previous-pos nil
1998 "Holds the start of the first incorrect word before point.")
2000 ;;*---------------------------------------------------------------------*/
2001 ;;* flyspell-auto-correct-previous-hook ... */
2002 ;;*---------------------------------------------------------------------*/
2003 (defun flyspell-auto-correct-previous-hook ()
2004 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
2005 Sets `flyspell-auto-correct-previous-pos' to nil"
2006 (interactive)
2007 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
2008 (unless (eq this-command (function flyspell-auto-correct-previous-word))
2009 (setq flyspell-auto-correct-previous-pos nil)))
2011 ;;*---------------------------------------------------------------------*/
2012 ;;* flyspell-auto-correct-previous-word ... */
2013 ;;*---------------------------------------------------------------------*/
2014 (defun flyspell-auto-correct-previous-word (position)
2015 "Auto correct the first misspelled word that occurs before point.
2016 But don't look beyond what's visible on the screen."
2017 (interactive "d")
2019 (let ((top (window-start))
2020 (bot (window-end)))
2021 (save-excursion
2022 (save-restriction
2023 (narrow-to-region top bot)
2024 (overlay-recenter (point))
2026 (add-hook 'pre-command-hook
2027 (function flyspell-auto-correct-previous-hook) t t)
2029 (unless flyspell-auto-correct-previous-pos
2030 ;; only reset if a new overlay exists
2031 (setq flyspell-auto-correct-previous-pos nil)
2033 (let ((overlay-list (overlays-in (point-min) position))
2034 (new-overlay 'dummy-value))
2036 ;; search for previous (new) flyspell overlay
2037 (while (and new-overlay
2038 (or (not (flyspell-overlay-p new-overlay))
2039 ;; check if its face has changed
2040 (not (eq (get-char-property
2041 (overlay-start new-overlay) 'face)
2042 'flyspell-incorrect))))
2043 (setq new-overlay (car-safe overlay-list))
2044 (setq overlay-list (cdr-safe overlay-list)))
2046 ;; if nothing new exits new-overlay should be nil
2047 (if new-overlay ;; the length of the word may change so go to the start
2048 (setq flyspell-auto-correct-previous-pos
2049 (overlay-start new-overlay)))))
2051 (when flyspell-auto-correct-previous-pos
2052 (save-excursion
2053 (goto-char flyspell-auto-correct-previous-pos)
2054 (let ((ispell-following-word t)) ;; point is at start
2055 (if (numberp flyspell-auto-correct-previous-pos)
2056 (goto-char flyspell-auto-correct-previous-pos))
2057 (flyspell-auto-correct-word))
2058 ;; the point may have moved so reset this
2059 (setq flyspell-auto-correct-previous-pos (point))))))))
2061 ;;*---------------------------------------------------------------------*/
2062 ;;* flyspell-correct-word ... */
2063 ;;*---------------------------------------------------------------------*/
2065 (defun flyspell-correct-word (event)
2066 "Pop up a menu of possible corrections for a misspelled word.
2067 The word checked is the word at the mouse position."
2068 (interactive "e")
2069 (let ((save (point)))
2070 (mouse-set-point event)
2071 (flyspell-correct-word-before-point event save)))
2073 (defun flyspell-correct-word-before-point (&optional event opoint)
2074 "Pop up a menu of possible corrections for misspelled word before point.
2075 If EVENT is non-nil, it is the mouse event that invoked this operation;
2076 that controls where to put the menu.
2077 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2078 (interactive)
2079 (unless (mouse-position)
2080 (error "Pop-up menus do not work on this terminal"))
2081 ;; use the correct dictionary
2082 (flyspell-accept-buffer-local-defs)
2083 (or opoint (setq opoint (point)))
2084 (let ((cursor-location (point))
2085 (word (flyspell-get-word)))
2086 (if (consp word)
2087 (let ((start (car (cdr word)))
2088 (end (car (cdr (cdr word))))
2089 (word (car word))
2090 poss ispell-filter)
2091 ;; now check spelling of word.
2092 (ispell-send-string "%\n") ;put in verbose mode
2093 (ispell-send-string (concat "^" word "\n"))
2094 ;; wait until ispell has processed word
2095 (while (progn
2096 (accept-process-output ispell-process)
2097 (not (string= "" (car ispell-filter)))))
2098 ;; Remove leading empty element
2099 (setq ispell-filter (cdr ispell-filter))
2100 ;; ispell process should return something after word is sent.
2101 ;; Tag word as valid (i.e., skip) otherwise
2102 (or ispell-filter
2103 (setq ispell-filter '(*)))
2104 (if (consp ispell-filter)
2105 (setq poss (ispell-parse-output (car ispell-filter))))
2106 (cond
2107 ((or (eq poss t) (stringp poss))
2108 ;; don't correct word
2110 ((null poss)
2111 ;; ispell error
2112 (error "Ispell: error in Ispell process"))
2113 ((featurep 'xemacs)
2114 (flyspell-xemacs-popup
2115 poss word cursor-location start end opoint))
2117 ;; The word is incorrect, we have to propose a replacement.
2118 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2119 poss word cursor-location start end opoint)))
2120 (ispell-pdict-save t)))))
2122 ;;*---------------------------------------------------------------------*/
2123 ;;* flyspell-do-correct ... */
2124 ;;*---------------------------------------------------------------------*/
2125 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2126 "The popup menu callback."
2127 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2128 ;; it instead right after calling the function.
2129 (cond ((eq replace 'ignore)
2130 (goto-char save)
2131 nil)
2132 ((eq replace 'save)
2133 (goto-char save)
2134 (ispell-send-string (concat "*" word "\n"))
2135 ;; This was added only to the XEmacs side in revision 1.18 of
2136 ;; flyspell. I assume its absence on the Emacs side was an
2137 ;; oversight. --Stef
2138 (ispell-send-string "#\n")
2139 (flyspell-unhighlight-at cursor-location)
2140 (setq ispell-pdict-modified-p '(t)))
2141 ((or (eq replace 'buffer) (eq replace 'session))
2142 (ispell-send-string (concat "@" word "\n"))
2143 (flyspell-unhighlight-at cursor-location)
2144 (if (null ispell-pdict-modified-p)
2145 (setq ispell-pdict-modified-p
2146 (list ispell-pdict-modified-p)))
2147 (goto-char save)
2148 (if (eq replace 'buffer)
2149 (ispell-add-per-file-word-list word)))
2150 (replace
2151 ;; This was added only to the Emacs side. I assume its absence on
2152 ;; the XEmacs side was an oversight. --Stef
2153 (flyspell-unhighlight-at cursor-location)
2154 (let ((old-max (point-max))
2155 (new-word (if (atom replace)
2156 replace
2157 (car replace)))
2158 (cursor-location (+ (- (length word) (- end start))
2159 cursor-location)))
2160 (unless (equal new-word (car poss))
2161 (delete-region start end)
2162 (goto-char start)
2163 (funcall flyspell-insert-function new-word)
2164 (if flyspell-abbrev-p
2165 (flyspell-define-abbrev word new-word)))
2166 ;; In the original Emacs code, this was only called in the body
2167 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2168 (flyspell-ajust-cursor-point save cursor-location old-max)))
2170 (goto-char save)
2171 nil)))
2173 ;;*---------------------------------------------------------------------*/
2174 ;;* flyspell-ajust-cursor-point ... */
2175 ;;*---------------------------------------------------------------------*/
2176 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2177 (if (>= save cursor-location)
2178 (let ((new-pos (+ save (- (point-max) old-max))))
2179 (goto-char (cond
2180 ((< new-pos (point-min))
2181 (point-min))
2182 ((> new-pos (point-max))
2183 (point-max))
2184 (t new-pos))))
2185 (goto-char save)))
2187 ;;*---------------------------------------------------------------------*/
2188 ;;* flyspell-emacs-popup ... */
2189 ;;*---------------------------------------------------------------------*/
2190 (defun flyspell-emacs-popup (event poss word)
2191 "The Emacs popup menu."
2192 (unless window-system
2193 (error "This command requires pop-up dialogs"))
2194 (if (not event)
2195 (let* ((mouse-pos (mouse-position))
2196 (mouse-pos (if (nth 1 mouse-pos)
2197 mouse-pos
2198 (set-mouse-position (car mouse-pos)
2199 (/ (frame-width) 2) 2)
2200 (mouse-position))))
2201 (setq event (list (list (car (cdr mouse-pos))
2202 (1+ (cdr (cdr mouse-pos))))
2203 (car mouse-pos)))))
2204 (let* ((corrects (if flyspell-sort-corrections
2205 (sort (car (cdr (cdr poss))) 'string<)
2206 (car (cdr (cdr poss)))))
2207 (cor-menu (if (consp corrects)
2208 (mapcar (lambda (correct)
2209 (list correct correct))
2210 corrects)
2211 '()))
2212 (affix (car (cdr (cdr (cdr poss)))))
2213 show-affix-info
2214 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2215 (list
2216 (list (concat "Save affix: " (car affix))
2217 'save)
2218 '("Accept (session)" session)
2219 '("Accept (buffer)" buffer))
2220 '(("Save word" save)
2221 ("Accept (session)" session)
2222 ("Accept (buffer)" buffer)))))
2223 (if (consp cor-menu)
2224 (append cor-menu (cons "" save))
2225 save)))
2226 (menu (cons "flyspell correction menu" base-menu)))
2227 (car (x-popup-menu event
2228 (list (format "%s [%s]" word (or ispell-local-dictionary
2229 ispell-dictionary))
2230 menu)))))
2232 ;;*---------------------------------------------------------------------*/
2233 ;;* flyspell-xemacs-popup ... */
2234 ;;*---------------------------------------------------------------------*/
2235 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2236 "The XEmacs popup menu."
2237 (let* ((corrects (if flyspell-sort-corrections
2238 (sort (car (cdr (cdr poss))) 'string<)
2239 (car (cdr (cdr poss)))))
2240 (cor-menu (if (consp corrects)
2241 (mapcar (lambda (correct)
2242 (vector correct
2243 (list 'flyspell-do-correct
2244 correct
2245 (list 'quote poss)
2246 word
2247 cursor-location
2248 start
2250 save)
2252 corrects)
2253 '()))
2254 (affix (car (cdr (cdr (cdr poss)))))
2255 show-affix-info
2256 (menu (let ((save (if (and (consp affix) show-affix-info)
2257 (vector
2258 (concat "Save affix: " (car affix))
2259 (list 'flyspell-do-correct
2260 ''save
2261 (list 'quote poss)
2262 word
2263 cursor-location
2264 start
2266 save)
2268 (vector
2269 "Save word"
2270 (list 'flyspell-do-correct
2271 ''save
2272 (list 'quote poss)
2273 word
2274 cursor-location
2275 start
2277 save)
2278 t)))
2279 (session (vector "Accept (session)"
2280 (list 'flyspell-do-correct
2281 ''session
2282 (list 'quote poss)
2283 word
2284 cursor-location
2285 start
2287 save)
2289 (buffer (vector "Accept (buffer)"
2290 (list 'flyspell-do-correct
2291 ''buffer
2292 (list 'quote poss)
2293 word
2294 cursor-location
2295 start
2297 save)
2298 t)))
2299 (if (consp cor-menu)
2300 (append cor-menu (list "-" save session buffer))
2301 (list save session buffer)))))
2302 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2303 ispell-dictionary))
2304 menu))))
2306 ;;*---------------------------------------------------------------------*/
2307 ;;* Some example functions for real autocorrecting */
2308 ;;*---------------------------------------------------------------------*/
2309 (defun flyspell-maybe-correct-transposition (beg end poss)
2310 "Check replacements for transposed characters.
2312 If the text between BEG and END is equal to a correction suggested by
2313 Ispell, after transposing two adjacent characters, correct the text,
2314 and return t.
2316 The third arg POSS is either the symbol 'doublon' or a list of
2317 possible corrections as returned by `ispell-parse-output'.
2319 This function is meant to be added to `flyspell-incorrect-hook'."
2320 (when (consp poss)
2321 (catch 'done
2322 (let ((str (buffer-substring beg end))
2323 (i 0) (len (- end beg)) tmp)
2324 (while (< (1+ i) len)
2325 (setq tmp (aref str i))
2326 (aset str i (aref str (1+ i)))
2327 (aset str (1+ i) tmp)
2328 (when (member str (nth 2 poss))
2329 (save-excursion
2330 (goto-char (+ beg i 1))
2331 (transpose-chars 1))
2332 (throw 'done t))
2333 (setq tmp (aref str i))
2334 (aset str i (aref str (1+ i)))
2335 (aset str (1+ i) tmp)
2336 (setq i (1+ i))))
2337 nil)))
2339 (defun flyspell-maybe-correct-doubling (beg end poss)
2340 "Check replacements for doubled characters.
2342 If the text between BEG and END is equal to a correction suggested by
2343 Ispell, after removing a pair of doubled characters, correct the text,
2344 and return t.
2346 The third arg POSS is either the symbol 'doublon' or a list of
2347 possible corrections as returned by `ispell-parse-output'.
2349 This function is meant to be added to `flyspell-incorrect-hook'."
2350 (when (consp poss)
2351 (catch 'done
2352 (let ((str (buffer-substring beg end))
2353 (i 0) (len (- end beg)))
2354 (while (< (1+ i) len)
2355 (when (and (= (aref str i) (aref str (1+ i)))
2356 (member (concat (substring str 0 (1+ i))
2357 (substring str (+ i 2)))
2358 (nth 2 poss)))
2359 (goto-char (+ beg i))
2360 (delete-char 1)
2361 (throw 'done t))
2362 (setq i (1+ i))))
2363 nil)))
2365 ;;*---------------------------------------------------------------------*/
2366 ;;* flyspell-already-abbrevp ... */
2367 ;;*---------------------------------------------------------------------*/
2368 (defun flyspell-already-abbrevp (table word)
2369 (let ((sym (abbrev-symbol word table)))
2370 (and sym (symbolp sym))))
2372 ;;*---------------------------------------------------------------------*/
2373 ;;* flyspell-change-abbrev ... */
2374 ;;*---------------------------------------------------------------------*/
2375 (defun flyspell-change-abbrev (table old new)
2376 (set (abbrev-symbol old table) new))
2378 (provide 'flyspell)
2380 ;;; flyspell.el ends here