1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
5 ;; Author: Manuel Serrano <Manuel.Serrano@unice.fr>
6 ;; Keywords: convenience
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
30 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
31 ;; This applies only to the current buffer.
33 ;; To enable Flyspell in text representing computer programs, type
34 ;; M-x flyspell-prog-mode.
35 ;; In that mode only text inside comments is checked.
37 ;; Note: consider setting the variable ispell-parser to `tex' to
38 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
40 ;; Some user variables control the behavior of flyspell. They are
41 ;; those defined under the `User variables' comment.
46 ;*---------------------------------------------------------------------*/
48 ;*---------------------------------------------------------------------*/
49 (defgroup flyspell nil
50 "Spell checking on the fly."
55 ;*---------------------------------------------------------------------*/
56 ;* User configuration ... */
57 ;*---------------------------------------------------------------------*/
58 (defcustom flyspell-highlight-flag t
59 "*How Flyspell should indicate misspelled words.
60 Non-nil means use highlight, nil means use minibuffer messages."
64 (defcustom flyspell-mark-duplications-flag t
65 "*Non-nil means Flyspell reports a repeated word as an error."
69 (defcustom flyspell-sort-corrections nil
70 "*Non-nil means, sort the corrections alphabetically before popping them."
75 (defcustom flyspell-duplicate-distance -1
76 "*The maximum distance for finding duplicates of unrecognized words.
77 This applies to the feature that when a word is not found in the dictionary,
78 if the same spelling occurs elsewhere in the buffer,
79 Flyspell uses a different face (`flyspell-duplicate-face') to highlight it.
80 This variable specifies how far to search to find such a duplicate.
81 -1 means no limit (search the whole buffer).
82 0 means do not search for duplicate unrecognized spellings."
87 (defcustom flyspell-delay 3
88 "*The number of seconds to wait before checking, after a \"delayed\" command."
92 (defcustom flyspell-persistent-highlight t
93 "*Non-nil means misspelled words remain highlighted until corrected.
94 If this variable is nil, only the most recently detected misspelled word
99 (defcustom flyspell-highlight-properties t
100 "*Non-nil means highlight incorrect words even if a property exists for this word."
104 (defcustom flyspell-default-delayed-commands
105 '(self-insert-command
107 backward-or-forward-delete-char
109 scrollbar-vertical-drag)
110 "The standard list of delayed commands for Flyspell.
111 See `flyspell-delayed-commands'."
114 :type '(repeat (symbol)))
116 (defcustom flyspell-delayed-commands nil
117 "List of commands that are \"delayed\" for Flyspell mode.
118 After these commands, Flyspell checking is delayed for a short time,
119 whose length is specified by `flyspell-delay'."
121 :type '(repeat (symbol)))
123 (defcustom flyspell-default-deplacement-commands
128 "The standard list of deplacement commands for Flyspell.
129 See `flyspell-deplacement-commands'."
132 :type '(repeat (symbol)))
134 (defcustom flyspell-deplacement-commands nil
135 "List of commands that are \"deplacement\" for Flyspell mode.
136 After these commands, Flyspell checking is performed only if the previous
137 command was not the very same command."
140 :type '(repeat (symbol)))
142 (defcustom flyspell-issue-welcome-flag t
143 "*Non-nil means that Flyspell should display a welcome message when started."
147 (defcustom flyspell-incorrect-hook nil
148 "*List of functions to be called when incorrect words are encountered.
149 Each function is given three arguments: the beginning and the end
150 of the incorrect region. The third is either the symbol 'doublon' or the list
151 of possible corrections as returned by 'ispell-parse-output'.
153 If any of the functions return non-Nil, the word is not highlighted as
159 (defcustom flyspell-default-dictionary nil
160 "A string that is the name of the default dictionary.
161 This is passed to the `ispell-change-dictionary' when flyspell is started.
162 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
163 when flyspell is started, the value of that variable is used instead
164 of `flyspell-default-dictionary' to select the default dictionary.
165 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
166 Ispell's ultimate default dictionary."
169 :type '(choice string (const :tag "Default" nil)))
171 (defcustom flyspell-tex-command-regexp
172 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
173 "A string that is the regular expression that matches TeX commands."
178 (defcustom flyspell-check-tex-math-command nil
179 "*Non nil means check even inside TeX math environment.
180 TeX math environments are discovered by the TEXMATHP that implemented
181 inside the texmathp.el Emacs package. That package may be found at:
182 http://strw.leidenuniv.nl/~dominik/Tools"
186 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
187 '("francais" "deutsch8" "norsk")
188 "List of dictionary names that consider `-' as word delimiter."
191 :type '(repeat (string)))
193 (defcustom flyspell-abbrev-p
195 "*If true, add correction to abbreviation table."
200 (defcustom flyspell-use-global-abbrev-table-p
202 "*If true, prefer global abbrev table to local abbrev table."
208 (defcustom flyspell-mode-line-string " Fly"
209 "*String displayed on the modeline when flyspell is active.
210 Set this to nil if you don't want a modeline indicator."
212 :type '(choice string (const :tag "None" nil)))
214 (defcustom flyspell-large-region 1000
215 "*The threshold that determines if a region is small.
216 The `flyspell-region' function is invoked if the region is small, the
217 word are checked one after the other using regular flyspell check
218 means. If the region is large, a new Ispell process is spawned to get
224 ;*---------------------------------------------------------------------*/
225 ;* Mode specific options */
226 ;* ------------------------------------------------------------- */
227 ;* Mode specific options enable users to disable flyspell on */
228 ;* certain word depending of the emacs mode. For instance, when */
229 ;* using flyspell with mail-mode add the following expression */
230 ;* in your .emacs file: */
231 ;* (add-hook 'mail-mode */
232 ;* '(lambda () (setq flyspell-generic-check-word-p */
233 ;* 'mail-mode-flyspell-verify))) */
234 ;*---------------------------------------------------------------------*/
235 (defvar flyspell-generic-check-word-p nil
236 "Function providing per-mode customization over which words are flyspelled.
237 Returns t to continue checking, nil otherwise.
238 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
239 property of the major mode name.")
240 (make-variable-buffer-local 'flyspell-generic-check-word-p)
242 ;*--- mail mode -------------------------------------------------------*/
243 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
244 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
245 (defun mail-mode-flyspell-verify ()
246 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
247 (let ((in-headers (save-excursion
248 (re-search-forward mail-header-separator nil t)))
249 (in-signature (save-excursion
250 (re-search-backward message-signature-separator nil t))))
252 (and (save-excursion (beginning-of-line)
253 (looking-at "^Subject:"))
254 (> (point) (match-end 0))))
260 (not (looking-at "[>}|]\\To:")))))))
262 ;*--- texinfo mode ----------------------------------------------------*/
263 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
264 (defun texinfo-mode-flyspell-verify ()
265 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
268 (not (looking-at "@"))))
270 ;*--- tex mode --------------------------------------------------------*/
271 (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
272 (defun tex-mode-flyspell-verify ()
273 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
276 (re-search-backward "^[ \t]*%%%[ \t]+Local" (point-min) t)))
278 (let ((this (point-marker))
279 (e (progn (end-of-line) (point-marker))))
281 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t)
282 (and (>= this (match-beginning 0))
283 (<= this (match-end 0)) )))))))
285 ;*--- sgml mode -------------------------------------------------------*/
286 (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
287 (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
289 (defun sgml-mode-flyspell-verify ()
290 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
292 (let ((this (point-marker))
293 (s (progn (beginning-of-line) (point-marker)))
294 (e (progn (end-of-line) (point-marker))))
297 (and (re-search-forward "[^<]*>" e t)
298 (= (match-beginning 0) this)))
301 (and (re-search-backward "<[^>]*" s t)
302 (= (match-end 0) this)))
305 (and (re-search-forward "[^&]*;" e t)
306 (= (match-beginning 0) this)))
309 (and (re-search-backward "&[^;]*" s t)
310 (= (match-end 0) this)))))))))
312 ;*---------------------------------------------------------------------*/
313 ;* Programming mode */
314 ;*---------------------------------------------------------------------*/
315 (defvar flyspell-prog-text-faces
316 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
317 "Faces corresponding to text in programming-mode buffers.")
319 (defun flyspell-generic-progmode-verify ()
320 "Used for `flyspell-generic-check-word-p' in programming modes."
321 (let ((f (get-text-property (point) 'face)))
322 (memq f flyspell-prog-text-faces)))
325 (defun flyspell-prog-mode ()
326 "Turn on `flyspell-mode' for comments and strings."
328 (setq flyspell-generic-check-word-p 'flyspell-generic-progmode-verify)
331 ;*---------------------------------------------------------------------*/
332 ;* Overlay compatibility */
333 ;*---------------------------------------------------------------------*/
334 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
335 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
336 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
337 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
338 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
339 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
340 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
341 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
343 ;*---------------------------------------------------------------------*/
344 ;* Which emacs are we currently running */
345 ;*---------------------------------------------------------------------*/
346 (defvar flyspell-emacs
348 ((string-match "XEmacs" emacs-version)
352 "The type of Emacs we are currently running.")
354 (defvar flyspell-use-local-map
355 (or (eq flyspell-emacs 'xemacs)
356 (not (string< emacs-version "20"))))
358 ;*---------------------------------------------------------------------*/
359 ;* The minor mode declaration. */
360 ;*---------------------------------------------------------------------*/
361 (defvar flyspell-mode nil)
362 (make-variable-buffer-local 'flyspell-mode)
364 (defvar flyspell-mouse-map
365 (let ((map (make-sparse-keymap)))
367 ((eq flyspell-emacs 'xemacs)
368 (define-key map [(button2)] #'flyspell-correct-word)
369 (define-key map "\M-\t" #'flyspell-auto-correct-word))
370 (flyspell-use-local-map
371 (define-key map [(mouse-2)] #'flyspell-correct-word)
372 (define-key map "\M-\t" #'flyspell-auto-correct-word)))
376 (defvar flyspell-mode-map (make-sparse-keymap))
378 ;; mouse, keyboard bindings and misc definition
379 (when (or (assoc 'flyspell-mode minor-mode-map-alist)
380 (setq minor-mode-map-alist
381 (cons (cons 'flyspell-mode flyspell-mode-map)
382 minor-mode-map-alist)))
383 (define-key flyspell-mode-map "\M-\t" 'flyspell-auto-correct-word))
385 ;; the name of the overlay property that defines the keymap
386 (defvar flyspell-overlay-keymap-property-name 'keymap)
388 ;; dash character machinery
389 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
390 "*Non-nil means that the `-' char is considered as a word delimiter.")
391 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
392 (defvar flyspell-dash-dictionary nil)
393 (make-variable-buffer-local 'flyspell-dash-dictionary)
394 (defvar flyspell-dash-local-dictionary nil)
395 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
397 ;*---------------------------------------------------------------------*/
399 ;*---------------------------------------------------------------------*/
400 (defface flyspell-incorrect-face
401 '((((class color)) (:foreground "OrangeRed" :weight bold :underline t))
403 "Face used for marking a misspelled word in Flyspell."
406 (defface flyspell-duplicate-face
407 '((((class color)) (:foreground "Gold3" :weight bold :underline t))
409 "Face used for marking a misspelled word that appears twice in the buffer.
410 See also `flyspell-duplicate-distance'."
413 (defvar flyspell-overlay nil)
415 ;*---------------------------------------------------------------------*/
416 ;* flyspell-mode ... */
417 ;*---------------------------------------------------------------------*/
419 (defun flyspell-mode (&optional arg)
420 "Minor mode performing on-the-fly spelling checking.
421 Ispell is automatically spawned on background for each entered words.
422 The default flyspell behavior is to highlight incorrect words.
423 With no argument, this command toggles Flyspell mode.
424 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
427 \\[ispell-word]: correct words (using Ispell).
428 \\[flyspell-auto-correct-word]: automatically correct word.
429 \\[flyspell-correct-word] (or mouse-2): popup correct words.
432 This runs `flyspell-mode-hook' after flyspell is entered.
435 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
436 valid. For instance, a personal dictionary can be used by
437 invoking `ispell-change-dictionary'.
439 Consider using the `ispell-parser' to check your text. For instance
441 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
444 \\[flyspell-region] checks all words inside a region.
445 \\[flyspell-buffer] checks the whole buffer."
447 (let ((old-flyspell-mode flyspell-mode))
448 ;; Mark the mode as on or off.
449 (setq flyspell-mode (not (or (and (null arg) flyspell-mode)
450 (<= (prefix-numeric-value arg) 0))))
452 (unless (eq flyspell-mode old-flyspell-mode)
456 ;; Force modeline redisplay.
457 (set-buffer-modified-p (buffer-modified-p)))))
459 ;*---------------------------------------------------------------------*/
461 ;*---------------------------------------------------------------------*/
463 (add-minor-mode 'flyspell-mode
464 'flyspell-mode-line-string
469 ;*---------------------------------------------------------------------*/
470 ;* flyspell-buffers ... */
471 ;* ------------------------------------------------------------- */
472 ;* For remembering buffers running flyspell */
473 ;*---------------------------------------------------------------------*/
474 (defvar flyspell-buffers nil)
476 ;*---------------------------------------------------------------------*/
477 ;* flyspell-minibuffer-p ... */
478 ;*---------------------------------------------------------------------*/
479 (defun flyspell-minibuffer-p (buffer)
480 "Is BUFFER a minibuffer?"
481 (let ((ws (get-buffer-window-list buffer t)))
482 (and (consp ws) (window-minibuffer-p (car ws)))))
484 ;*---------------------------------------------------------------------*/
485 ;* flyspell-accept-buffer-local-defs ... */
486 ;*---------------------------------------------------------------------*/
487 (defun flyspell-accept-buffer-local-defs ()
488 (ispell-accept-buffer-local-defs)
489 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
490 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
491 ;; the dictionary as changed
493 (setq flyspell-dash-dictionary ispell-dictionary)
494 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
495 (if (member (or ispell-local-dictionary ispell-dictionary)
496 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
497 (setq flyspell-consider-dash-as-word-delimiter-flag t)
498 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
500 ;*---------------------------------------------------------------------*/
501 ;* flyspell-mode-on ... */
502 ;*---------------------------------------------------------------------*/
503 (eval-when-compile (defvar flyspell-local-mouse-map))
505 (defun flyspell-mode-on ()
506 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
507 (setq ispell-highlight-face 'flyspell-incorrect-face)
508 ;; local dictionaries setup
509 (ispell-change-dictionary
510 (or ispell-local-dictionary ispell-dictionary flyspell-default-dictionary))
511 ;; we have to force ispell to accept the local definition or
512 ;; otherwise it could be too late, the local dictionary may
514 (flyspell-accept-buffer-local-defs)
515 ;; we put the `flyspel-delayed' property on some commands
516 (flyspell-delay-commands)
517 ;; we put the `flyspel-deplacement' property on some commands
518 (flyspell-deplacement-commands)
519 ;; we bound flyspell action to post-command hook
520 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
521 ;; we bound flyspell action to pre-command hook
522 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
523 ;; we bound flyspell action to after-change hook
524 (make-local-variable 'after-change-functions)
525 (setq after-change-functions
526 (cons 'flyspell-after-change-function after-change-functions))
527 ;; set flyspell-generic-check-word-p based on the major mode
528 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
530 (setq flyspell-generic-check-word-p mode-predicate)))
531 ;; the welcome message
532 (if (and flyspell-issue-welcome-flag (interactive-p))
533 (let ((binding (where-is-internal 'flyspell-auto-correct-word
537 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
538 (key-description binding))
539 "Welcome to flyspell. Use Mouse-2 to correct words."))))
541 ;; we end with the flyspell hooks
542 (run-hooks 'flyspell-mode-hook))
544 ;*---------------------------------------------------------------------*/
545 ;* flyspell-delay-commands ... */
546 ;*---------------------------------------------------------------------*/
547 (defun flyspell-delay-commands ()
548 "Install the standard set of Flyspell delayed commands."
549 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
550 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
552 ;*---------------------------------------------------------------------*/
553 ;* flyspell-delay-command ... */
554 ;*---------------------------------------------------------------------*/
555 (defun flyspell-delay-command (command)
556 "Set COMMAND to be delayed, for Flyspell.
557 When flyspell `post-command-hook' is invoked because a delayed command
558 as been used the current word is not immediately checked.
559 It will be checked only after `flyspell-delay' seconds."
560 (interactive "SDelay Flyspell after Command: ")
561 (put command 'flyspell-delayed t))
563 ;*---------------------------------------------------------------------*/
564 ;* flyspell-deplacement-commands ... */
565 ;*---------------------------------------------------------------------*/
566 (defun flyspell-deplacement-commands ()
567 "Install the standard set of Flyspell deplacement commands."
568 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
569 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
571 ;*---------------------------------------------------------------------*/
572 ;* flyspell-deplacement-command ... */
573 ;*---------------------------------------------------------------------*/
574 (defun flyspell-deplacement-command (command)
575 "Set COMMAND that implement cursor movements, for Flyspell.
576 When flyspell `post-command-hook' is invoked because of a deplacement command
577 as been used the current word is checked only if the previous command was
578 not the very same deplacement command."
579 (interactive "SDeplacement Flyspell after Command: ")
580 (put command 'flyspell-deplacement t))
582 ;*---------------------------------------------------------------------*/
583 ;* flyspell-word-cache ... */
584 ;*---------------------------------------------------------------------*/
585 (defvar flyspell-word-cache-start nil)
586 (defvar flyspell-word-cache-end nil)
587 (defvar flyspell-word-cache-word nil)
588 (make-variable-buffer-local 'flyspell-word-cache-start)
589 (make-variable-buffer-local 'flyspell-word-cache-end)
590 (make-variable-buffer-local 'flyspell-word-cache-word)
592 ;*---------------------------------------------------------------------*/
593 ;* The flyspell pre-hook, store the current position. In the */
594 ;* post command hook, we will check, if the word at this position */
595 ;* has to be spell checked. */
596 ;*---------------------------------------------------------------------*/
597 (defvar flyspell-pre-buffer nil)
598 (defvar flyspell-pre-point nil)
599 (defvar flyspell-pre-column nil)
600 (defvar flyspell-pre-pre-buffer nil)
601 (defvar flyspell-pre-pre-point nil)
603 ;*---------------------------------------------------------------------*/
604 ;* flyspell-previous-command ... */
605 ;*---------------------------------------------------------------------*/
606 (defvar flyspell-previous-command nil
607 "The last interactive command checked by Flyspell.")
609 ;*---------------------------------------------------------------------*/
610 ;* flyspell-pre-command-hook ... */
611 ;*---------------------------------------------------------------------*/
612 (defun flyspell-pre-command-hook ()
613 "Save the current buffer and point for Flyspell's post-command hook."
615 (setq flyspell-pre-buffer (current-buffer))
616 (setq flyspell-pre-point (point))
617 (setq flyspell-pre-column (current-column)))
619 ;*---------------------------------------------------------------------*/
620 ;* flyspell-mode-off ... */
621 ;*---------------------------------------------------------------------*/
623 (defun flyspell-mode-off ()
624 "Turn Flyspell mode off."
625 ;; we remove the hooks
626 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
627 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
628 (setq after-change-functions (delq 'flyspell-after-change-function
629 after-change-functions))
630 ;; we remove all the flyspell hilightings
631 (flyspell-delete-all-overlays)
632 ;; we have to erase pre cache variables
633 (setq flyspell-pre-buffer nil)
634 (setq flyspell-pre-point nil)
635 ;; we mark the mode as killed
636 (setq flyspell-mode nil))
638 ;*---------------------------------------------------------------------*/
639 ;* flyspell-check-pre-word-p ... */
640 ;*---------------------------------------------------------------------*/
641 (defun flyspell-check-pre-word-p ()
642 "Return non-nil if we should to check the word before point.
643 More precisely, it applies to the word that was before point
644 before the current command."
646 ((or (not (numberp flyspell-pre-point))
647 (not (bufferp flyspell-pre-buffer))
648 (not (buffer-live-p flyspell-pre-buffer)))
650 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
651 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
653 ((or (and (= flyspell-pre-point (- (point) 1))
654 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
655 (= flyspell-pre-point (point))
656 (= flyspell-pre-point (+ (point) 1)))
658 ((and (symbolp this-command)
659 (not executing-kbd-macro)
660 (or (get this-command 'flyspell-delayed)
661 (and (get this-command 'flyspell-deplacement)
662 (eq flyspell-previous-command this-command)))
663 (or (= (current-column) 0)
664 (= (current-column) flyspell-pre-column)
665 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
667 ((not (eq (current-buffer) flyspell-pre-buffer))
669 ((not (and (numberp flyspell-word-cache-start)
670 (numberp flyspell-word-cache-end)))
673 (or (< flyspell-pre-point flyspell-word-cache-start)
674 (> flyspell-pre-point flyspell-word-cache-end)))))
676 ;*---------------------------------------------------------------------*/
677 ;* The flyspell after-change-hook, store the change position. In */
678 ;* the post command hook, we will check, if the word at this */
679 ;* position has to be spell checked. */
680 ;*---------------------------------------------------------------------*/
681 (defvar flyspell-changes nil)
683 ;*---------------------------------------------------------------------*/
684 ;* flyspell-after-change-function ... */
685 ;*---------------------------------------------------------------------*/
686 (defun flyspell-after-change-function (start stop len)
687 "Save the current buffer and point for Flyspell's post-command hook."
689 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
691 ;*---------------------------------------------------------------------*/
692 ;* flyspell-check-changed-word-p ... */
693 ;*---------------------------------------------------------------------*/
694 (defun flyspell-check-changed-word-p (start stop)
695 "Return t when the changed word has to be checked.
696 The answer depends of several criteria.
697 Mostly we check word delimiters."
699 ((and (eq (char-after start) ?\n) (> stop start))
701 ((not (numberp flyspell-pre-point))
703 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
705 ((let ((pos (point)))
706 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
711 ;*---------------------------------------------------------------------*/
712 ;* flyspell-check-word-p ... */
713 ;*---------------------------------------------------------------------*/
714 (defun flyspell-check-word-p ()
715 "Return t when the word at `point' has to be checked.
716 The answer depends of several criteria.
717 Mostly we check word delimiters."
719 ((<= (- (point-max) 1) (point-min))
720 ;; the buffer is not filled enough
722 ((and (and (> (current-column) 0)
723 (not (eq (current-column) flyspell-pre-column)))
726 (and (looking-at (flyspell-get-not-casechars))
727 (or flyspell-consider-dash-as-word-delimiter-flag
728 (not (looking-at "\\-"))))))
729 ;; yes because we have reached or typed a word delimiter.
731 ((symbolp this-command)
733 ((get this-command 'flyspell-deplacement)
734 (not (eq flyspell-previous-command this-command)))
735 ((get this-command 'flyspell-delayed)
736 ;; the current command is not delayed, that
737 ;; is that we must check the word now
738 (if (fboundp 'about-xemacs)
739 (sit-for flyspell-delay nil)
740 (sit-for flyspell-delay 0 nil)))
744 ;*---------------------------------------------------------------------*/
745 ;* flyspell-debug-signal-no-check ... */
746 ;*---------------------------------------------------------------------*/
747 (defun flyspell-debug-signal-no-check (msg obj)
748 (setq debug-on-error t)
750 (let ((buffer (get-buffer-create "*flyspell-debug*")))
753 (insert "NO-CHECK:\n")
754 (insert (format " %S : %S\n" msg obj)))))
756 ;*---------------------------------------------------------------------*/
757 ;* flyspell-debug-signal-pre-word-checked ... */
758 ;*---------------------------------------------------------------------*/
759 (defun flyspell-debug-signal-pre-word-checked ()
760 (setq debug-on-error t)
762 (let ((buffer (get-buffer-create "*flyspell-debug*")))
764 (insert "PRE-WORD:\n")
765 (insert (format " pre-point : %S\n" flyspell-pre-point))
766 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
767 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
768 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
769 (goto-char (point-max)))))
771 ;*---------------------------------------------------------------------*/
772 ;* flyspell-debug-signal-word-checked ... */
773 ;*---------------------------------------------------------------------*/
774 (defun flyspell-debug-signal-word-checked ()
775 (setq debug-on-error t)
777 (let ((oldbuf (current-buffer))
778 (buffer (get-buffer-create "*flyspell-debug*"))
782 (insert (format " this-cmd : %S\n" this-command))
783 (insert (format " delayed : %S\n" (and (symbolp this-command)
784 (get this-command 'flyspell-delayed))))
785 (insert (format " point : %S\n" point))
786 (insert (format " prev-char : [%c] %S\n"
789 (let ((c (if (> (point) (point-min))
792 (char-after (point)))
798 (let ((c (if (> (point) (point-min))
801 (and (and (looking-at (flyspell-get-not-casechars)) 1)
802 (and (or flyspell-consider-dash-as-word-delimiter-flag
803 (not (looking-at "\\-"))) 2))))))
806 (insert (format " because : %S\n"
808 ((not (and (symbolp this-command)
809 (get this-command 'flyspell-delayed)))
810 ;; the current command is not delayed, that
811 ;; is that we must check the word now
815 (let ((c (if (> (point) (point-min))
818 (and (looking-at (flyspell-get-not-casechars))
819 (or flyspell-consider-dash-as-word-delimiter-flag
820 (not (looking-at "\\-"))))))))
823 ;; yes because we have reached or typed a word delimiter.
825 ((not (integerp flyspell-delay))
826 ;; yes because the user had set up a no-delay configuration.
830 (goto-char (point-max)))))
832 ;*---------------------------------------------------------------------*/
833 ;* flyspell-debug-signal-changed-checked ... */
834 ;*---------------------------------------------------------------------*/
835 (defun flyspell-debug-signal-changed-checked ()
836 (setq debug-on-error t)
838 (let ((buffer (get-buffer-create "*flyspell-debug*"))
841 (insert "CHANGED WORD:\n")
842 (insert (format " point : %S\n" point))
843 (goto-char (point-max)))))
845 ;*---------------------------------------------------------------------*/
846 ;* flyspell-post-command-hook ... */
847 ;* ------------------------------------------------------------- */
848 ;* It is possible that we check several words: */
849 ;* 1- the current word is checked if the predicate */
850 ;* FLYSPELL-CHECK-WORD-P is true */
851 ;* 2- the word that used to be the current word before the */
852 ;* THIS-COMMAND is checked if: */
853 ;* a- the previous word is different from the current word */
854 ;* b- the previous word as not just been checked by the */
855 ;* previous FLYSPELL-POST-COMMAND-HOOK */
856 ;* 3- the words changed by the THIS-COMMAND that are neither the */
857 ;* previous word nor the current word */
858 ;*---------------------------------------------------------------------*/
859 (defun flyspell-post-command-hook ()
860 "The `post-command-hook' used by flyspell to check a word in-the-fly."
862 (let ((command this-command))
863 (if (flyspell-check-pre-word-p)
865 '(flyspell-debug-signal-pre-word-checked)
866 (set-buffer flyspell-pre-buffer)
868 (goto-char flyspell-pre-point)
870 (if (flyspell-check-word-p)
872 '(flyspell-debug-signal-word-checked)
874 ;; we remember which word we have just checked.
875 ;; this will be used next time we will check a word
876 ;; to compare the next current word with the word
877 ;; that as been registered in the pre-command-hook
878 ;; that is these variables are used within the predicate
879 ;; FLYSPELL-CHECK-PRE-WORD-P
880 (setq flyspell-pre-pre-buffer (current-buffer))
881 (setq flyspell-pre-pre-point (point)))
883 (setq flyspell-pre-pre-buffer nil)
884 (setq flyspell-pre-pre-point nil)
885 ;; when a word is not checked because of a delayed command
886 ;; we do not disable the ispell cache.
887 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
888 (setq flyspell-word-cache-end -1))))
889 (while (consp flyspell-changes)
890 (let ((start (car (car flyspell-changes)))
891 (stop (cdr (car flyspell-changes))))
892 (if (flyspell-check-changed-word-p start stop)
894 '(flyspell-debug-signal-changed-checked)
897 (setq flyspell-changes (cdr flyspell-changes))))
898 (setq flyspell-previous-command command)))
900 ;*---------------------------------------------------------------------*/
901 ;* flyspell-notify-misspell ... */
902 ;*---------------------------------------------------------------------*/
903 (defun flyspell-notify-misspell (start end word poss)
904 (let ((replacements (if (stringp poss)
906 (if flyspell-sort-corrections
907 (sort (car (cdr (cdr poss))) 'string<)
908 (car (cdr (cdr poss)))))))
909 (message (format "mispelling `%s' %S" word replacements))))
911 ;*---------------------------------------------------------------------*/
912 ;* flyspell-word ... */
913 ;*---------------------------------------------------------------------*/
914 (defun flyspell-word (&optional following)
915 "Spell check a word."
916 (interactive (list current-prefix-arg))
918 (setq following ispell-following-word))
920 ;; use the correct dictionary
921 (flyspell-accept-buffer-local-defs)
922 (let* ((cursor-location (point))
923 (flyspell-word (flyspell-get-word following))
925 (if (or (eq flyspell-word nil)
926 (and (fboundp flyspell-generic-check-word-p)
927 (not (funcall flyspell-generic-check-word-p))))
930 ;; destructure return flyspell-word info list.
931 (setq start (car (cdr flyspell-word))
932 end (car (cdr (cdr flyspell-word)))
933 word (car flyspell-word))
934 ;; before checking in the directory, we check for doublons.
936 ((and (or (not (eq ispell-parser 'tex))
937 (not (eq (char-after start) ?\\)))
938 flyspell-mark-duplications-flag
941 (word-search-backward word
945 ;; yes, this is a doublon
946 (flyspell-highlight-incorrect-region start end 'doublon))
947 ((and (eq flyspell-word-cache-start start)
948 (eq flyspell-word-cache-end end)
949 (string-equal flyspell-word-cache-word word))
950 ;; this word had been already checked, we skip
952 ((and (eq ispell-parser 'tex)
953 (flyspell-tex-command-p flyspell-word))
954 ;; this is a correct word (because a tex command)
955 (flyspell-unhighlight-at start)
957 (flyspell-unhighlight-at (- end 1)))
960 ;; we setup the cache
961 (setq flyspell-word-cache-start start)
962 (setq flyspell-word-cache-end end)
963 (setq flyspell-word-cache-word word)
964 ;; now check spelling of word.
965 (process-send-string ispell-process "%\n")
966 ;; put in verbose mode
967 (process-send-string ispell-process
968 (concat "^" word "\n"))
969 ;; we mark the ispell process so it can be killed
970 ;; when emacs is exited without query
971 (if (fboundp 'process-kill-without-query)
972 (process-kill-without-query ispell-process))
973 ;; wait until ispell has processed word
975 (accept-process-output ispell-process)
976 (not (string= "" (car ispell-filter)))))
977 ;; (process-send-string ispell-process "!\n")
978 ;; back to terse mode.
979 (setq ispell-filter (cdr ispell-filter))
980 (if (consp ispell-filter)
981 (setq poss (ispell-parse-output (car ispell-filter))))
984 (flyspell-unhighlight-at start)
986 (flyspell-unhighlight-at (- end 1)))
988 ((and (stringp poss) flyspell-highlight-flag)
990 (flyspell-unhighlight-at start)
992 (flyspell-unhighlight-at (- end 1)))
995 (flyspell-unhighlight-at start)
997 (flyspell-unhighlight-at (- end 1))))
998 ((or (and (< flyspell-duplicate-distance 0)
1001 (word-search-backward word
1006 (word-search-forward word
1009 (and (> flyspell-duplicate-distance 0)
1012 (word-search-backward
1015 flyspell-duplicate-distance)
1019 (word-search-forward
1022 flyspell-duplicate-distance)
1024 (if flyspell-highlight-flag
1025 (flyspell-highlight-duplicate-region start end)
1026 (message (format "duplicate `%s'" word))))
1028 ;; incorrect highlight the location
1029 (if flyspell-highlight-flag
1030 (flyspell-highlight-incorrect-region start end poss)
1031 (flyspell-notify-misspell start end word poss))))
1032 ;; return to original location
1033 (goto-char cursor-location)
1034 (if ispell-quit (setq ispell-quit nil)))))))))
1036 ;*---------------------------------------------------------------------*/
1037 ;* flyspell-tex-math-initialized ... */
1038 ;*---------------------------------------------------------------------*/
1039 (defvar flyspell-tex-math-initialized nil)
1041 ;*---------------------------------------------------------------------*/
1042 ;* flyspell-math-tex-command-p ... */
1043 ;* ------------------------------------------------------------- */
1044 ;* This function uses the texmathp package to check if (point) */
1045 ;* is within a tex command. In order to avoid using */
1046 ;* condition-case each time we use the variable */
1047 ;* flyspell-tex-math-initialized to make a special case the first */
1048 ;* time that function is called. */
1049 ;*---------------------------------------------------------------------*/
1050 (defun flyspell-math-tex-command-p ()
1052 (flyspell-check-tex-math-command
1054 ((eq flyspell-tex-math-initialized t)
1056 ((eq flyspell-tex-math-initialized 'error)
1059 (setq flyspell-tex-math-initialized t)
1063 (setq flyspell-tex-math-initialized 'error)
1066 ;*---------------------------------------------------------------------*/
1067 ;* flyspell-tex-command-p ... */
1068 ;*---------------------------------------------------------------------*/
1069 (defun flyspell-tex-command-p (word)
1070 "Return t if WORD is a TeX command."
1072 (let ((b (car (cdr word))))
1073 (and (re-search-backward "\\\\" (- (point) 100) t)
1074 (or (= (match-end 0) b)
1075 (and (goto-char (match-end 0))
1076 (looking-at flyspell-tex-command-regexp)
1077 (>= (match-end 0) b))))))
1078 (flyspell-math-tex-command-p)))
1080 ;*---------------------------------------------------------------------*/
1081 ;* flyspell-casechars-cache ... */
1082 ;*---------------------------------------------------------------------*/
1083 (defvar flyspell-casechars-cache nil)
1084 (defvar flyspell-ispell-casechars-cache nil)
1085 (make-variable-buffer-local 'flyspell-casechars-cache)
1086 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1088 ;*---------------------------------------------------------------------*/
1089 ;* flyspell-get-casechars ... */
1090 ;*---------------------------------------------------------------------*/
1091 (defun flyspell-get-casechars ()
1092 "This function builds a string that is the regexp of word chars.
1093 In order to avoid one useless string construction,
1094 this function changes the last char of the `ispell-casechars' string."
1095 (let ((ispell-casechars (ispell-get-casechars)))
1097 ((eq ispell-parser 'tex)
1098 (setq flyspell-ispell-casechars-cache ispell-casechars)
1099 (setq flyspell-casechars-cache
1100 (concat (substring ispell-casechars
1102 (- (length ispell-casechars) 1))
1104 flyspell-casechars-cache)
1106 (setq flyspell-ispell-casechars-cache ispell-casechars)
1107 (setq flyspell-casechars-cache ispell-casechars)
1108 flyspell-casechars-cache))))
1110 ;*---------------------------------------------------------------------*/
1111 ;* flyspell-get-not-casechars-cache ... */
1112 ;*---------------------------------------------------------------------*/
1113 (defvar flyspell-not-casechars-cache nil)
1114 (defvar flyspell-ispell-not-casechars-cache nil)
1115 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1116 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1118 ;*---------------------------------------------------------------------*/
1119 ;* flyspell-get-not-casechars ... */
1120 ;*---------------------------------------------------------------------*/
1121 (defun flyspell-get-not-casechars ()
1122 "This function builds a string that is the regexp of non-word chars."
1123 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1125 ((eq ispell-parser 'tex)
1126 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1127 (setq flyspell-not-casechars-cache
1128 (concat (substring ispell-not-casechars
1130 (- (length ispell-not-casechars) 1))
1132 flyspell-not-casechars-cache)
1134 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1135 (setq flyspell-not-casechars-cache ispell-not-casechars)
1136 flyspell-not-casechars-cache))))
1138 ;*---------------------------------------------------------------------*/
1139 ;* flyspell-get-word ... */
1140 ;*---------------------------------------------------------------------*/
1141 (defun flyspell-get-word (following)
1142 "Return the word for spell-checking according to Ispell syntax.
1143 If argument FOLLOWING is non-nil or if `ispell-following-word'
1144 is non-nil when called interactively, then the following word
1145 \(rather than preceding\) is checked when the cursor is not over a word.
1146 Optional second argument contains other chars that can be included in word
1149 Word syntax described by `ispell-dictionary-alist' (which see)."
1150 (let* ((flyspell-casechars (flyspell-get-casechars))
1151 (flyspell-not-casechars (flyspell-get-not-casechars))
1152 (ispell-otherchars (ispell-get-otherchars))
1153 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1154 (word-regexp (if (string< "" ispell-otherchars)
1155 (concat flyspell-casechars
1161 (if ispell-many-otherchars-p
1163 (concat flyspell-casechars "+")))
1167 (if (not (looking-at flyspell-casechars))
1169 (re-search-forward flyspell-casechars (point-max) t)
1170 (re-search-backward flyspell-casechars (point-min) t)))
1171 ;; move to front of word
1172 (re-search-backward flyspell-not-casechars (point-min) 'start)
1174 (if (string< "" ispell-otherchars)
1175 (while (and (looking-at ispell-otherchars)
1177 (or (not did-it-once)
1178 ispell-many-otherchars-p)
1179 (not (eq pos (point))))
1181 (setq did-it-once t)
1183 (if (looking-at flyspell-casechars)
1184 (re-search-backward flyspell-not-casechars (point-min) 'move)
1185 (backward-char -1)))))
1186 ;; Now mark the word and save to string.
1187 (if (eq (re-search-forward word-regexp (point-max) t) nil)
1190 (setq start (match-beginning 0)
1192 word (buffer-substring-no-properties start end))
1193 (list word start end)))))
1195 ;*---------------------------------------------------------------------*/
1196 ;* flyspell-small-region ... */
1197 ;*---------------------------------------------------------------------*/
1198 (defun flyspell-small-region (beg end)
1199 "Flyspell text between BEG and END."
1207 (while (< (point) end)
1210 (message "Spell Checking...%d%%"
1211 (* 100 (/ (float (- (point) beg)) (- end beg))))
1213 (setq count (+ 1 count)))
1216 (let ((cur (point)))
1218 (if (and (< (point) end) (> (point) (+ cur 1)))
1219 (backward-char 1)))))
1221 (message "Spell Checking completed.")
1224 ;*---------------------------------------------------------------------*/
1225 ;* flyspell-external-ispell-process ... */
1226 ;*---------------------------------------------------------------------*/
1227 (defvar flyspell-external-ispell-process '()
1228 "The external Flyspell Ispell process.")
1230 ;*---------------------------------------------------------------------*/
1231 ;* flyspell-external-ispell-buffer ... */
1232 ;*---------------------------------------------------------------------*/
1233 (defvar flyspell-external-ispell-buffer '())
1234 (defvar flyspell-large-region-buffer '())
1235 (defvar flyspell-large-region-beg (point-min))
1236 (defvar flyspell-large-region-end (point-max))
1238 ;*---------------------------------------------------------------------*/
1239 ;* flyspell-external-point-words ... */
1240 ;*---------------------------------------------------------------------*/
1241 (defun flyspell-external-point-words ()
1242 (let ((buffer flyspell-external-ispell-buffer))
1244 (beginning-of-buffer)
1245 (let ((size (- flyspell-large-region-end flyspell-large-region-beg))
1246 (start flyspell-large-region-beg))
1247 ;; now we are done with ispell, we have to find the word in
1248 ;; the initial buffer
1249 (while (< (point) (- (point-max) 1))
1250 ;; we have to fetch the incorrect word
1251 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1252 (let ((word (match-string 1)))
1253 (goto-char (match-end 0))
1254 (set-buffer flyspell-large-region-buffer)
1255 (goto-char flyspell-large-region-beg)
1256 (message "Spell Checking...%d%% [%s]"
1257 (* 100 (/ (float (- (point) start)) size))
1259 (if (search-forward word flyspell-large-region-end t)
1261 (setq flyspell-large-region-beg (point))
1262 (goto-char (- (point) 1))
1264 (set-buffer buffer))
1265 (goto-char (point-max)))))
1267 (message "Spell Checking completed.")
1268 ;; ok, we are done with pointing out incorrect words, we just
1269 ;; have to kill the temporary buffer
1270 (kill-buffer flyspell-external-ispell-buffer)
1271 (setq flyspell-external-ispell-buffer nil)))
1273 ;*---------------------------------------------------------------------*/
1274 ;* flyspell-large-region ... */
1275 ;*---------------------------------------------------------------------*/
1276 (defun flyspell-large-region (beg end)
1277 (let* ((curbuf (current-buffer))
1278 (buffer (get-buffer-create "*flyspell-region*")))
1279 (setq flyspell-external-ispell-buffer buffer)
1280 (setq flyspell-large-region-buffer curbuf)
1281 (setq flyspell-large-region-beg beg)
1282 (setq flyspell-large-region-end end)
1285 ;; this is done, we can start checking...
1286 (message "Checking region...")
1288 (let ((c (apply 'call-process-region beg
1296 ;; Local dictionary becomes the global dictionary in use.
1297 (if ispell-local-dictionary
1298 (setq ispell-dictionary ispell-local-dictionary))
1299 (setq args (ispell-get-ispell-args))
1300 (if ispell-dictionary ; use specified dictionary
1302 (append (list "-d" ispell-dictionary) args)))
1303 (if ispell-personal-dictionary ; use specified pers dict
1308 ispell-personal-dictionary)))))
1309 (setq args (append args ispell-extra-args))
1312 (flyspell-external-point-words)
1313 (error "Can't check region...")))))
1315 ;*---------------------------------------------------------------------*/
1316 ;* flyspell-region ... */
1317 ;* ------------------------------------------------------------- */
1318 ;* Because `ispell -a' is too slow, it is not possible to use */
1319 ;* it on large region. Then, when ispell is invoked on a large */
1320 ;* text region, a new `ispell -l' process is spawned. The */
1321 ;* pointed out words are then searched in the region a checked with */
1322 ;* regular flyspell means. */
1323 ;*---------------------------------------------------------------------*/
1325 (defun flyspell-region (beg end)
1326 "Flyspell text between BEG and END."
1335 (if (> (- end beg) flyspell-large-region)
1336 (flyspell-large-region beg end)
1337 (flyspell-small-region beg end)))))
1339 ;*---------------------------------------------------------------------*/
1340 ;* flyspell-buffer ... */
1341 ;*---------------------------------------------------------------------*/
1343 (defun flyspell-buffer ()
1344 "Flyspell whole buffer."
1346 (flyspell-region (point-min) (point-max)))
1348 ;*---------------------------------------------------------------------*/
1349 ;* old next error position ... */
1350 ;*---------------------------------------------------------------------*/
1351 (defvar flyspell-old-buffer-error nil)
1352 (defvar flyspell-old-pos-error nil)
1354 ;*---------------------------------------------------------------------*/
1355 ;* flyspell-goto-next-error ... */
1356 ;*---------------------------------------------------------------------*/
1357 (defun flyspell-goto-next-error ()
1358 "Go to the next previously detected error.
1359 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1364 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1365 (eq pos flyspell-old-pos-error))
1367 (if (= flyspell-old-pos-error max)
1368 ;; goto beginning of buffer
1370 (message "Restarting from beginning of buffer")
1371 (goto-char (point-min)))
1373 (setq pos (point))))
1374 ;; seek the next error
1375 (while (and (< pos max)
1376 (let ((ovs (overlays-at pos))
1378 (while (and (not r) (consp ovs))
1379 (if (flyspell-overlay-p (car ovs))
1381 (setq ovs (cdr ovs))))
1383 (setq pos (1+ pos)))
1384 ;; save the current location for next invocation
1385 (setq flyspell-old-pos-error pos)
1386 (setq flyspell-old-buffer-error (current-buffer))
1389 (message "No more miss-spelled word!"))))
1391 ;*---------------------------------------------------------------------*/
1392 ;* flyspell-overlay-p ... */
1393 ;*---------------------------------------------------------------------*/
1394 (defun flyspell-overlay-p (o)
1395 "A predicate that return true iff O is an overlay used by flyspell."
1396 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1398 ;*---------------------------------------------------------------------*/
1399 ;* flyspell-delete-all-overlays ... */
1400 ;* ------------------------------------------------------------- */
1401 ;* Remove all the overlays introduced by flyspell. */
1402 ;*---------------------------------------------------------------------*/
1403 (defun flyspell-delete-all-overlays ()
1404 "Delete all the overlays used by flyspell."
1405 (let ((l (overlays-in (point-min) (point-max))))
1408 (if (flyspell-overlay-p (car l))
1409 (delete-overlay (car l)))
1410 (setq l (cdr l))))))
1412 ;*---------------------------------------------------------------------*/
1413 ;* flyspell-unhighlight-at ... */
1414 ;*---------------------------------------------------------------------*/
1415 (defun flyspell-unhighlight-at (pos)
1416 "Remove the flyspell overlay that are located at POS."
1417 (if flyspell-persistent-highlight
1418 (let ((overlays (overlays-at pos)))
1419 (while (consp overlays)
1420 (if (flyspell-overlay-p (car overlays))
1421 (delete-overlay (car overlays)))
1422 (setq overlays (cdr overlays))))
1423 (if (flyspell-overlay-p flyspell-overlay)
1424 (delete-overlay flyspell-overlay))))
1426 ;*---------------------------------------------------------------------*/
1427 ;* flyspell-properties-at-p ... */
1428 ;* ------------------------------------------------------------- */
1429 ;* Is there an highlight properties at position pos? */
1430 ;*---------------------------------------------------------------------*/
1431 (defun flyspell-properties-at-p (pos)
1432 "Return t if there is a text property at POS, not counting `local-map'.
1433 If variable `flyspell-highlight-properties' is set to nil,
1434 text with properties are not checked. This function is used to discover
1435 if the character at POS has any other property."
1436 (let ((prop (text-properties-at pos))
1438 (while (and keep (consp prop))
1439 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1440 (setq prop (cdr (cdr prop)))
1444 ;*---------------------------------------------------------------------*/
1445 ;* make-flyspell-overlay ... */
1446 ;*---------------------------------------------------------------------*/
1447 (defun make-flyspell-overlay (beg end face mouse-face)
1448 "Allocate an overlay to highlight an incorrect word.
1449 BEG and END specify the range in the buffer of that word.
1450 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1452 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1453 (overlay-put flyspell-overlay 'face face)
1454 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1455 (overlay-put flyspell-overlay 'flyspell-overlay t)
1456 (if flyspell-use-local-map
1457 (overlay-put flyspell-overlay
1458 flyspell-overlay-keymap-property-name
1459 flyspell-mouse-map))
1462 ;*---------------------------------------------------------------------*/
1463 ;* flyspell-highlight-incorrect-region ... */
1464 ;*---------------------------------------------------------------------*/
1465 (defun flyspell-highlight-incorrect-region (beg end poss)
1466 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1467 (unless (run-hook-with-args-until-success
1468 'flyspell-incorrect-hook beg end poss)
1469 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1471 ;; we cleanup current overlay at the same position
1472 (if (and (not flyspell-persistent-highlight)
1473 (overlayp flyspell-overlay))
1474 (delete-overlay flyspell-overlay)
1475 (let ((overlays (overlays-at beg)))
1476 (while (consp overlays)
1477 (if (flyspell-overlay-p (car overlays))
1478 (delete-overlay (car overlays)))
1479 (setq overlays (cdr overlays)))))
1480 ;; now we can use a new overlay
1481 (setq flyspell-overlay
1482 (make-flyspell-overlay beg end
1483 'flyspell-incorrect-face 'highlight))))))
1485 ;*---------------------------------------------------------------------*/
1486 ;* flyspell-highlight-duplicate-region ... */
1487 ;*---------------------------------------------------------------------*/
1488 (defun flyspell-highlight-duplicate-region (beg end)
1489 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
1490 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1492 ;; we cleanup current overlay at the same position
1493 (if (and (not flyspell-persistent-highlight)
1494 (overlayp flyspell-overlay))
1495 (delete-overlay flyspell-overlay)
1496 (let ((overlays (overlays-at beg)))
1497 (while (consp overlays)
1498 (if (flyspell-overlay-p (car overlays))
1499 (delete-overlay (car overlays)))
1500 (setq overlays (cdr overlays)))))
1501 ;; now we can use a new overlay
1502 (setq flyspell-overlay
1503 (make-flyspell-overlay beg end
1504 'flyspell-duplicate-face 'highlight)))))
1506 ;*---------------------------------------------------------------------*/
1507 ;* flyspell-auto-correct-cache ... */
1508 ;*---------------------------------------------------------------------*/
1509 (defvar flyspell-auto-correct-pos nil)
1510 (defvar flyspell-auto-correct-region nil)
1511 (defvar flyspell-auto-correct-ring nil)
1512 (defvar flyspell-auto-correct-word nil)
1513 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1514 (make-variable-buffer-local 'flyspell-auto-correct-region)
1515 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1516 (make-variable-buffer-local 'flyspell-auto-correct-word)
1518 ;*---------------------------------------------------------------------*/
1519 ;* flyspell-check-previous-highlighted-word ... */
1520 ;*---------------------------------------------------------------------*/
1521 (defun flyspell-check-previous-highlighted-word (&optional arg)
1522 "Correct the closer misspelled word.
1523 This function scans a mis-spelled word before the cursor. If it finds one
1524 it proposes replacement for that word. With prefix arg, count that many
1525 misspelled words backwards."
1527 (let ((pos1 (point))
1529 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1532 (while (and (setq pos (previous-overlay-change pos))
1535 (if (> pos (point-min))
1537 (setq ovs (overlays-at (1- pos)))
1540 (setq ovs (cdr ovs))
1541 (if (and (overlay-get ov 'flyspell-overlay)
1542 (= 0 (setq arg (1- arg))))
1543 (throw 'exit t)))))))
1547 (error "No word to correct before point"))))
1549 ;*---------------------------------------------------------------------*/
1550 ;* flyspell-display-next-corrections ... */
1551 ;*---------------------------------------------------------------------*/
1552 (defun flyspell-display-next-corrections (corrections)
1553 (let ((string "Corrections:")
1556 (while (< (length string) 80)
1557 (if (equal (car l) flyspell-auto-correct-word)
1558 (setq pos (cons (+ 1 (length string)) pos)))
1559 (setq string (concat string " " (car l)))
1562 (let ((num (car pos)))
1563 (put-text-property num
1564 (+ num (length flyspell-auto-correct-word))
1566 'flyspell-incorrect-face
1568 (setq pos (cdr pos)))
1569 (if (fboundp 'display-message)
1570 (display-message 'no-log string)
1573 ;*---------------------------------------------------------------------*/
1574 ;* flyspell-abbrev-table ... */
1575 ;*---------------------------------------------------------------------*/
1576 (defun flyspell-abbrev-table ()
1577 (if flyspell-use-global-abbrev-table-p
1579 local-abbrev-table))
1581 ;*---------------------------------------------------------------------*/
1582 ;* flyspell-auto-correct-word ... */
1583 ;*---------------------------------------------------------------------*/
1584 (defun flyspell-auto-correct-word ()
1585 "Correct the current word.
1586 This command proposes various successive corrections for the current word."
1589 (old-max (point-max)))
1590 ;; use the correct dictionary
1591 (flyspell-accept-buffer-local-defs)
1592 (if (and (eq flyspell-auto-correct-pos pos)
1593 (consp flyspell-auto-correct-region))
1594 ;; we have already been using the function at the same location
1595 (let* ((start (car flyspell-auto-correct-region))
1596 (len (cdr flyspell-auto-correct-region)))
1597 (delete-region start (+ start len))
1598 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1599 (let* ((word (car flyspell-auto-correct-ring))
1600 (len (length word)))
1601 (rplacd flyspell-auto-correct-region len)
1603 (if flyspell-abbrev-p
1604 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1605 flyspell-auto-correct-word)
1606 (flyspell-change-abbrev (flyspell-abbrev-table)
1607 flyspell-auto-correct-word
1609 (define-abbrev (flyspell-abbrev-table)
1610 flyspell-auto-correct-word word)))
1613 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1614 (flyspell-ajust-cursor-point pos (point) old-max)
1615 (setq flyspell-auto-correct-pos (point)))
1616 ;; fetch the word to be checked
1617 (let ((word (flyspell-get-word nil))
1619 ;; destructure return word info list.
1620 (setq start (car (cdr word))
1621 end (car (cdr (cdr word)))
1623 (setq flyspell-auto-correct-word word)
1624 ;; now check spelling of word.
1625 (process-send-string ispell-process "%\n") ;put in verbose mode
1626 (process-send-string ispell-process (concat "^" word "\n"))
1627 ;; wait until ispell has processed word
1629 (accept-process-output ispell-process)
1630 (not (string= "" (car ispell-filter)))))
1631 (setq ispell-filter (cdr ispell-filter))
1632 (if (consp ispell-filter)
1633 (setq poss (ispell-parse-output (car ispell-filter))))
1634 (cond ((or (eq poss t) (stringp poss))
1635 ;; don't correct word
1639 (error "Ispell: error in Ispell process"))
1641 ;; the word is incorrect, we have to propose a replacement
1642 (let ((replacements (if flyspell-sort-corrections
1643 (sort (car (cdr (cdr poss))) 'string<)
1644 (car (cdr (cdr poss))))))
1645 (setq flyspell-auto-correct-region nil)
1646 (if (consp replacements)
1648 (let ((replace (car replacements)))
1649 (let ((new-word replace))
1650 (if (not (equal new-word (car poss)))
1652 ;; the save the current replacements
1653 (setq flyspell-auto-correct-region
1654 (cons start (length new-word)))
1655 (let ((l replacements))
1656 (while (consp (cdr l))
1658 (rplacd l (cons (car poss) replacements)))
1659 (setq flyspell-auto-correct-ring
1661 (delete-region start end)
1663 (if flyspell-abbrev-p
1664 (if (flyspell-already-abbrevp
1665 (flyspell-abbrev-table) word)
1666 (flyspell-change-abbrev
1667 (flyspell-abbrev-table)
1670 (define-abbrev (flyspell-abbrev-table)
1673 (flyspell-display-next-corrections
1674 (cons new-word flyspell-auto-correct-ring))
1675 (flyspell-ajust-cursor-point pos
1678 (setq flyspell-auto-correct-pos (point))
1679 (ispell-pdict-save t)))))
1681 ;*---------------------------------------------------------------------*/
1682 ;* flyspell-correct-word ... */
1683 ;*---------------------------------------------------------------------*/
1684 (defun flyspell-correct-word (event)
1685 "Pop up a menu of possible corrections for a misspelled word.
1686 The word checked is the word at the mouse position."
1688 ;; use the correct dictionary
1689 (flyspell-accept-buffer-local-defs)
1690 ;; retain cursor location (I don't know why but save-excursion here fails).
1691 (let ((save (point)))
1692 (mouse-set-point event)
1693 (let ((cursor-location (point))
1694 (word (flyspell-get-word nil))
1695 start end poss replace)
1696 ;; destructure return word info list.
1697 (setq start (car (cdr word))
1698 end (car (cdr (cdr word)))
1700 ;; now check spelling of word.
1701 (process-send-string ispell-process "%\n") ;put in verbose mode
1702 (process-send-string ispell-process (concat "^" word "\n"))
1703 ;; wait until ispell has processed word
1705 (accept-process-output ispell-process)
1706 (not (string= "" (car ispell-filter)))))
1707 (setq ispell-filter (cdr ispell-filter))
1708 (if (consp ispell-filter)
1709 (setq poss (ispell-parse-output (car ispell-filter))))
1710 (cond ((or (eq poss t) (stringp poss))
1711 ;; don't correct word
1715 (error "Ispell: error in Ispell process"))
1716 ((string-match "GNU" (emacs-version))
1717 ;; the word is incorrect, we have to propose a replacement
1718 (setq replace (flyspell-emacs-popup event poss word))
1719 (cond ((eq replace 'ignore)
1724 (process-send-string ispell-process (concat "*" word "\n"))
1725 (flyspell-unhighlight-at cursor-location)
1726 (setq ispell-pdict-modified-p '(t)))
1727 ((or (eq replace 'buffer) (eq replace 'session))
1728 (process-send-string ispell-process (concat "@" word "\n"))
1729 (if (null ispell-pdict-modified-p)
1730 (setq ispell-pdict-modified-p
1731 (list ispell-pdict-modified-p)))
1732 (flyspell-unhighlight-at cursor-location)
1734 (if (eq replace 'buffer)
1735 (ispell-add-per-file-word-list word)))
1737 (let ((new-word (if (atom replace)
1740 (cursor-location (+ (- (length word) (- end start))
1742 (if (not (equal new-word (car poss)))
1743 (let ((old-max (point-max)))
1744 (delete-region start end)
1746 (if flyspell-abbrev-p
1747 (define-abbrev (flyspell-abbrev-table)
1750 (flyspell-ajust-cursor-point save
1756 ((eq flyspell-emacs 'xemacs)
1757 (flyspell-xemacs-popup
1758 event poss word cursor-location start end save)
1760 (ispell-pdict-save t))))
1762 ;*---------------------------------------------------------------------*/
1763 ;* flyspell-xemacs-correct ... */
1764 ;*---------------------------------------------------------------------*/
1765 (defun flyspell-xemacs-correct (replace poss word cursor-location start end save)
1766 "The xemacs popup menu callback."
1767 (cond ((eq replace 'ignore)
1770 (process-send-string ispell-process (concat "*" word "\n"))
1771 (process-send-string ispell-process "#\n")
1772 (flyspell-unhighlight-at cursor-location)
1773 (setq ispell-pdict-modified-p '(t)))
1774 ((or (eq replace 'buffer) (eq replace 'session))
1775 (process-send-string ispell-process (concat "@" word "\n"))
1776 (flyspell-unhighlight-at cursor-location)
1777 (if (null ispell-pdict-modified-p)
1778 (setq ispell-pdict-modified-p
1779 (list ispell-pdict-modified-p)))
1780 (if (eq replace 'buffer)
1781 (ispell-add-per-file-word-list word)))
1783 (let ((old-max (point-max))
1784 (new-word (if (atom replace)
1787 (cursor-location (+ (- (length word) (- end start))
1789 (if (not (equal new-word (car poss)))
1791 (delete-region start end)
1794 (if flyspell-abbrev-p
1795 (define-abbrev (flyspell-abbrev-table)
1798 (flyspell-ajust-cursor-point save cursor-location old-max)))))
1800 ;*---------------------------------------------------------------------*/
1801 ;* flyspell-ajust-cursor-point ... */
1802 ;*---------------------------------------------------------------------*/
1803 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
1804 (if (>= save cursor-location)
1805 (let ((new-pos (+ save (- (point-max) old-max))))
1807 ((< new-pos (point-min))
1809 ((> new-pos (point-max))
1814 ;*---------------------------------------------------------------------*/
1815 ;* flyspell-emacs-popup ... */
1816 ;*---------------------------------------------------------------------*/
1817 (defun flyspell-emacs-popup (event poss word)
1818 "The Emacs popup menu."
1820 (let* ((mouse-pos (mouse-position))
1821 (mouse-pos (if (nth 1 mouse-pos)
1823 (set-mouse-position (car mouse-pos)
1824 (/ (frame-width) 2) 2)
1827 (setq event (list (list (car (cdr mouse-pos))
1828 (1+ (cdr (cdr mouse-pos))))
1830 (let* ((corrects (if flyspell-sort-corrections
1831 (sort (car (cdr (cdr poss))) 'string<)
1832 (car (cdr (cdr poss)))))
1833 (cor-menu (if (consp corrects)
1834 (mapcar (lambda (correct)
1835 (list correct correct))
1838 (affix (car (cdr (cdr (cdr poss)))))
1839 (base-menu (let ((save (if (consp affix)
1841 (list (concat "Save affix: " (car affix))
1843 '("Accept (session)" accept)
1844 '("Accept (buffer)" buffer))
1845 '(("Save word" save)
1846 ("Accept (session)" session)
1847 ("Accept (buffer)" buffer)))))
1848 (if (consp cor-menu)
1849 (append cor-menu (cons "" save))
1851 (menu (cons "flyspell correction menu" base-menu)))
1852 (car (x-popup-menu event
1853 (list (format "%s [%s]" word (or ispell-local-dictionary
1857 ;*---------------------------------------------------------------------*/
1858 ;* flyspell-xemacs-popup ... */
1859 ;*---------------------------------------------------------------------*/
1860 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
1861 "The XEmacs popup menu."
1862 (let* ((corrects (if flyspell-sort-corrections
1863 (sort (car (cdr (cdr poss))) 'string<)
1864 (car (cdr (cdr poss)))))
1865 (cor-menu (if (consp corrects)
1866 (mapcar (lambda (correct)
1868 (list 'flyspell-xemacs-correct
1879 (affix (car (cdr (cdr (cdr poss)))))
1880 (menu (let ((save (if (consp affix)
1882 (concat "Save affix: " (car affix))
1883 (list 'flyspell-xemacs-correct
1894 (list 'flyspell-xemacs-correct
1903 (session (vector "Accept (session)"
1904 (list 'flyspell-xemacs-correct
1913 (buffer (vector "Accept (buffer)"
1914 (list 'flyspell-xemacs-correct
1923 (if (consp cor-menu)
1924 (append cor-menu (list "-" save session buffer))
1925 (list save session buffer)))))
1926 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
1930 ;*---------------------------------------------------------------------*/
1931 ;* Some example functions for real autocorrecting */
1932 ;*---------------------------------------------------------------------*/
1934 (defun flyspell-maybe-correct-transposition (beg end poss)
1935 "Check replacements for transposed characters.
1937 If the text between BEG and END is equal to a correction suggested by
1938 Ispell, after transposing two adjacent characters, correct the text,
1941 The third arg POSS is either the symbol 'doublon' or a list of
1942 possible corrections as returned by 'ispell-parse-output'.
1944 This function is meant to be added to 'flyspell-incorrect-hook'."
1946 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
1949 (copy-to-buffer temp-buffer beg end)
1950 (set-buffer temp-buffer)
1951 (goto-char (1+ (point-min)))
1952 (while (and (not (eobp)) (not found))
1954 (if (member (buffer-string) (nth 2 poss))
1955 (setq found (point))
1956 (transpose-chars -1)
1960 (goto-char (+ beg found -1))
1961 (transpose-chars -1)
1964 (defun flyspell-maybe-correct-doubling (beg end poss)
1965 "Check replacements for doubled characters.
1967 If the text between BEG and END is equal to a correction suggested by
1968 Ispell, after removing a pair of doubled characters, correct the text,
1971 The third arg POSS is either the symbol 'doublon' or a list of
1972 possible corrections as returned by 'ispell-parse-output'.
1974 This function is meant to be added to 'flyspell-incorrect-hook'."
1976 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
1979 (copy-to-buffer temp-buffer beg end)
1980 (set-buffer temp-buffer)
1981 (goto-char (1+ (point-min)))
1982 (while (and (not (eobp)) (not found))
1983 (when (char-equal (char-after) (char-before))
1985 (if (member (buffer-string) (nth 2 poss))
1986 (setq found (point))
1987 (insert-char (char-before) 1)))
1991 (goto-char (+ beg found -1))
1995 ;*---------------------------------------------------------------------*/
1996 ;* flyspell-already-abbrevp ... */
1997 ;*---------------------------------------------------------------------*/
1998 (defun flyspell-already-abbrevp (table word)
1999 (let ((sym (abbrev-symbol word table)))
2000 (and sym (symbolp sym))))
2002 ;*---------------------------------------------------------------------*/
2003 ;* flyspell-change-abbrev ... */
2004 ;*---------------------------------------------------------------------*/
2005 (defun flyspell-change-abbrev (table old new)
2006 (set (abbrev-symbol old table) new))
2010 ;;; flyspell.el ends here