1 ;;; flyspell.el --- On-the-fly spell checker
3 ;; Copyright (C) 1998, 2000 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 Meta-x flyspell-mode.
31 ;; This applies only to the current buffer.
33 ;; To enable Flyspell in text representing computer programs, type
34 ;; Meta-x flyspell-prog-mode.
35 ;; In that mode only text inside comments are 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 "Spellchecking 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."
74 (defcustom flyspell-duplicate-distance -1
75 "*The maximum distance for finding duplicates of unrecognized words.
76 This applies to the feature that when a word is not found in the dictionary,
77 if the same spelling occurs elsewhere in the buffer,
78 Flyspell uses a different face (`flyspell-duplicate-face') to highlight it.
79 This variable specifies how far to search to find such a duplicate.
80 -1 means no limit (search the whole buffer).
81 0 means do not search for duplicate unrecognized spellings."
85 (defcustom flyspell-delay 3
86 "*The number of seconds to wait before checking, after a \"delayed\" command."
90 (defcustom flyspell-persistent-highlight t
91 "*Non-nil means misspelled words remain highlighted until corrected.
92 If this variable is nil, only the most recently detected misspelled word
97 (defcustom flyspell-highlight-properties t
98 "*Non-nil means highlight incorrect words even if a property exists for this word."
102 (defcustom flyspell-default-delayed-commands
103 '(self-insert-command
105 backward-or-forward-delete-char
107 scrollbar-vertical-drag)
108 "The standard list of delayed commands for Flyspell.
109 See `flyspell-delayed-commands'."
111 :type '(repeat (symbol)))
113 (defcustom flyspell-delayed-commands nil
114 "List of commands that are \"delayed\" for Flyspell mode.
115 After these commands, Flyspell checking is delayed for a short time,
116 whose length is specified by `flyspell-delay'."
118 :type '(repeat (symbol)))
120 (defcustom flyspell-default-deplacement-commands
125 "The standard list of deplacement commands for Flyspell.
126 See `flyspell-deplacement-commands'."
128 :type '(repeat (symbol)))
130 (defcustom flyspell-deplacement-commands nil
131 "List of commands that are \"deplacement\" for Flyspell mode.
132 After these commands, Flyspell checking is performed only if the previous
133 command was not the very same command."
135 :type '(repeat (symbol)))
137 (defcustom flyspell-issue-welcome-flag t
138 "*Non-nil means that Flyspell should display a welcome message when started."
142 (defcustom flyspell-incorrect-hook nil
143 "*List of functions to be called when incorrect words are encountered.
144 Each function is given three arguments: the beginning and the end
145 of the incorrect region. The third is either the symbol 'doublon' or the list
146 of possible corrections returned as returned by 'ispell-parse-output'.
148 If any of the functions return non-Nil, the word is not highligted as
153 (defcustom flyspell-default-dictionary "american"
154 "A string that is the name of the default dictionary.
155 This is passed to the ispell-change-dictionary when flyspell is started.
156 If the variables ispell-local-dictionary or ispell-dictionary are non nil
157 when flyspell is started, the value of that variables is used instead
158 of flyspell-default-dictionary to select the default dictionary."
162 (defcustom flyspell-tex-command-regexp
163 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
164 "A string that is the regular expression that matches TeX commands."
168 (defcustom flyspell-check-tex-math-command nil
169 "*Non nils means check even inside TeX math environement. TeX math
170 environement are discovered byt eh TEXMATHP that is implemented inside
171 the eponyme emacs package. That package may be found at:
172 http://strw.leidenuniv.nl/~dominik/Tools"
176 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
177 '("francais" "deutsch8" "norsk")
178 "List of dictionary names that consider `-' as word delimiter."
180 :type '(repeat (string)))
182 (defcustom flyspell-abbrev-p
184 "*If true, add correction to abbreviation table."
188 (defcustom flyspell-use-global-abbrev-table-p
190 "*If true, prefer global abbrev table to local abbrev table."
195 (defcustom flyspell-mode-line-string " Fly"
196 "*String displayed on the modeline when flyspell is active.
197 Set this to nil if you don't want a modeline indicator."
201 (defcustom flyspell-large-region 1000
202 "*The threshold that determines if an region is small. The flyspell-region
203 is invoked, if the region is small, the word are checked one after the
204 other using regular flyspell check means. If the region is large, a new
205 ispell process is spawned to get speed."
209 ;*---------------------------------------------------------------------*/
210 ;* Mode specific options */
211 ;* ------------------------------------------------------------- */
212 ;* Mode specific options enable users to disable flyspell on */
213 ;* certain word depending of the emacs mode. For instance, when */
214 ;* using flyspell with mail-mode add the following expression */
215 ;* in your .emacs file: */
216 ;* (add-hook 'mail-mode */
217 ;* '(lambda () (setq flyspell-generic-check-word-p */
218 ;* 'mail-mode-flyspell-verify))) */
219 ;*---------------------------------------------------------------------*/
220 (defvar flyspell-generic-check-word-p nil
221 "Function providing per-mode customization over which words are flyspelled.
222 Returns t to continue checking, nil otherwise.
223 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
224 property of the major mode name.")
225 (make-variable-buffer-local 'flyspell-generic-check-word-p)
227 ;*--- mail mode -------------------------------------------------------*/
228 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
229 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
230 (defun mail-mode-flyspell-verify ()
231 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
232 (let ((in-headers (save-excursion
233 (re-search-forward mail-header-separator nil t)))
234 (in-signature (save-excursion
235 (re-search-backward message-signature-separator nil t))))
239 (looking-at "^Subject:")))
245 (not (looking-at "[>}|]\\To:")))))))
247 ;*--- texinfo mode ----------------------------------------------------*/
248 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
249 (defun texinfo-mode-flyspell-verify ()
250 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
253 (not (looking-at "@"))))
255 ;*--- tex mode --------------------------------------------------------*/
256 (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
257 (defun tex-mode-flyspell-verify ()
258 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
261 (re-search-backward "^[ \t]*%%%[ \t]+Local" (point-min) t)))
263 (let ((this (point-marker))
264 (e (progn (end-of-line) (point-marker))))
266 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t)
267 (and (>= this (match-beginning 0))
268 (<= this (match-end 0)) )))))))
270 ;*--- sgml mode -------------------------------------------------------*/
271 (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
272 (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
274 (defun sgml-mode-flyspell-verify ()
275 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
277 (let ((this (point-marker))
278 (s (progn (beginning-of-line) (point-marker)))
279 (e (progn (end-of-line) (point-marker))))
282 (and (re-search-forward "[^<]*>" e t)
283 (= (match-beginning 0) this)))
286 (and (re-search-backward "<[^>]*" s t)
287 (= (match-end 0) this)))
290 (and (re-search-forward "[^&]*;" e t)
291 (= (match-beginning 0) this)))
294 (and (re-search-backward "&[^;]*" s t)
295 (= (match-end 0) this)))))))))
297 ;*---------------------------------------------------------------------*/
298 ;* Programming mode */
299 ;*---------------------------------------------------------------------*/
300 (defvar flyspell-prog-text-faces
301 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
302 "Faces corresponding to text in programming-mode buffers.")
304 (defun flyspell-generic-progmode-verify ()
305 "Used for `flyspell-generic-check-word-p' in programming modes."
306 (let ((f (get-text-property (point) 'face)))
307 (memq f flyspell-prog-text-faces)))
310 (defun flyspell-prog-mode ()
311 "Turn on `flyspell-mode' for comments and strings."
313 (setq flyspell-generic-check-word-p 'flyspell-generic-progmode-verify)
316 ;*---------------------------------------------------------------------*/
317 ;* Overlay compatibility */
318 ;*---------------------------------------------------------------------*/
319 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
320 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
321 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
322 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
323 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
324 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
325 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
326 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
328 ;*---------------------------------------------------------------------*/
329 ;* Which emacs are we currently running */
330 ;*---------------------------------------------------------------------*/
331 (defvar flyspell-emacs
333 ((string-match "XEmacs" emacs-version)
337 "The type of Emacs we are currently running.")
339 (defvar flyspell-use-local-map
340 (or (eq flyspell-emacs 'xemacs)
341 (not (string< emacs-version "20"))))
343 ;*---------------------------------------------------------------------*/
344 ;* The minor mode declaration. */
345 ;*---------------------------------------------------------------------*/
346 (defvar flyspell-mode nil)
347 (make-variable-buffer-local 'flyspell-mode)
349 (defvar flyspell-mouse-map
350 (let ((map (make-sparse-keymap)))
352 ((eq flyspell-emacs 'xemacs)
353 (define-key map [(button2)]
354 #'flyspell-correct-word/mouse-keymap)
355 (define-key map "\M-\t" #'flyspell-auto-correct-word))
356 (flyspell-use-local-map
357 (define-key map [(mouse-2)] #'flyspell-correct-word/mouse-keymap)
358 (define-key map "\M-\t" #'flyspell-auto-correct-word)))
362 (defvar flyspell-mode-map (make-sparse-keymap))
364 ;; mouse, keyboard bindings and misc definition
365 (when (or (assoc 'flyspell-mode minor-mode-map-alist)
366 (setq minor-mode-map-alist
367 (cons (cons 'flyspell-mode flyspell-mode-map)
368 minor-mode-map-alist)))
369 (define-key flyspell-mode-map "\M-\t" 'flyspell-auto-correct-word)
370 (define-key flyspell-mode-map [(mouse-2)]
371 (function flyspell-correct-word/local-keymap)))
374 ;; the name of the overlay property that defines the keymap
375 (defvar flyspell-overlay-keymap-property-name
376 (if (string-match "19.*XEmacs" emacs-version)
380 ;; dash character machinery
381 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
382 "*Non-nil means that the `-' char is considered as a word delimiter.")
383 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
384 (defvar flyspell-dash-dictionary nil)
385 (make-variable-buffer-local 'flyspell-dash-dictionary)
386 (defvar flyspell-dash-local-dictionary nil)
387 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
389 ;*---------------------------------------------------------------------*/
391 ;*---------------------------------------------------------------------*/
392 (defface flyspell-incorrect-face
393 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
395 "Face used for marking a misspelled word in Flyspell."
398 (defface flyspell-duplicate-face
399 '((((class color)) (:foreground "Gold3" :bold t :underline t))
401 "Face used for marking a misspelled word that appears twice in the buffer.
402 See also `flyspell-duplicate-distance'."
405 (defvar flyspell-overlay nil)
407 ;*---------------------------------------------------------------------*/
408 ;* flyspell-mode ... */
409 ;*---------------------------------------------------------------------*/
411 (defun flyspell-mode (&optional arg)
412 "Minor mode performing on-the-fly spelling checking.
413 Ispell is automatically spawned on background for each entered words.
414 The default flyspell behavior is to highlight incorrect words.
415 With no argument, this command toggles Flyspell mode.
416 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
419 \\[ispell-word]: correct words (using Ispell).
420 \\[flyspell-auto-correct-word]: automatically correct word.
421 \\[flyspell-correct-word] (or mouse-2): popup correct words.
424 flyspell-mode-hook is run after flyspell is entered.
427 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
428 valid. For instance, a personal dictionary can be used by
429 invoking `ispell-change-dictionary'.
431 Consider using the `ispell-parser' to check your text. For instance
433 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
436 flyspell-region checks all words inside a region.
438 flyspell-buffer checks the whole buffer."
440 (let ((old-flyspell-mode flyspell-mode))
441 ;; Mark the mode as on or off.
442 (setq flyspell-mode (not (or (and (null arg) flyspell-mode)
443 (<= (prefix-numeric-value arg) 0))))
445 (unless (eq flyspell-mode old-flyspell-mode)
449 ;; Force modeline redisplay.
450 (set-buffer-modified-p (buffer-modified-p)))))
452 ;*---------------------------------------------------------------------*/
454 ;*---------------------------------------------------------------------*/
456 (if (fboundp 'add-minor-mode)
457 (add-minor-mode 'flyspell-mode
458 'flyspell-mode-line-string
462 (or (assoc 'flyspell-mode minor-mode-alist)
463 (setq minor-mode-alist
464 (cons '(flyspell-mode flyspell-mode-line-string)
467 (or (assoc 'flyspell-mode minor-mode-map-alist)
468 (setq minor-mode-map-alist
469 (cons (cons 'flyspell-mode flyspell-mode-map)
470 minor-mode-map-alist))))
473 ;*---------------------------------------------------------------------*/
474 ;* flyspell-buffers ... */
475 ;* ------------------------------------------------------------- */
476 ;* For remembering buffers running flyspell */
477 ;*---------------------------------------------------------------------*/
478 (defvar flyspell-buffers nil)
480 ;*---------------------------------------------------------------------*/
481 ;* flyspell-minibuffer-p ... */
482 ;*---------------------------------------------------------------------*/
483 (defun flyspell-minibuffer-p (buffer)
484 "Is BUFFER a minibuffer?"
485 (let ((ws (get-buffer-window-list buffer t)))
486 (and (consp ws) (window-minibuffer-p (car ws)))))
488 ;*---------------------------------------------------------------------*/
489 ;* flyspell-accept-buffer-local-defs ... */
490 ;*---------------------------------------------------------------------*/
491 (defun flyspell-accept-buffer-local-defs ()
492 (ispell-accept-buffer-local-defs)
493 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
494 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
495 ;; the dictionary as changed
497 (setq flyspell-dash-dictionary ispell-dictionary)
498 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
499 (if (member (or ispell-local-dictionary ispell-dictionary)
500 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
501 (setq flyspell-consider-dash-as-word-delimiter-flag t)
502 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
504 ;*---------------------------------------------------------------------*/
505 ;* flyspell-mode-on ... */
506 ;*---------------------------------------------------------------------*/
507 (eval-when-compile (defvar flyspell-local-mouse-map))
509 (defun flyspell-mode-on ()
510 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
511 (setq ispell-highlight-face 'flyspell-incorrect-face)
512 ;; local dictionaries setup
513 (ispell-change-dictionary
514 (or ispell-local-dictionary ispell-dictionary flyspell-default-dictionary))
515 ;; we have to force ispell to accept the local definition or
516 ;; otherwise it could be too late, the local dictionary may
518 (flyspell-accept-buffer-local-defs)
519 ;; we put the `flyspel-delayed' property on some commands
520 (flyspell-delay-commands)
521 ;; we put the `flyspel-deplacement' property on some commands
522 (flyspell-deplacement-commands)
523 ;; we bound flyspell action to post-command hook
524 (make-local-hook 'post-command-hook)
525 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
526 ;; we bound flyspell action to pre-command hook
527 (make-local-hook 'pre-command-hook)
528 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
529 ;; we bound flyspell action to after-change hook
530 (make-local-variable 'after-change-functions)
531 (setq after-change-functions
532 (cons 'flyspell-after-change-function after-change-functions))
533 ;; set flyspell-generic-check-word-p based on the major mode
534 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
536 (setq flyspell-generic-check-word-p mode-predicate)))
537 ;; work around the fact that the `local-map' text-property replaces the
538 ;; buffer's local map rather than shadowing it.
539 (set (make-local-variable 'flyspell-mouse-map)
540 (let ((map (copy-keymap flyspell-mouse-map)))
541 (set-keymap-parent map (current-local-map))
543 ;; the welcome message
544 (if (and flyspell-issue-welcome-flag (interactive-p))
545 (let ((binding (where-is-internal 'flyspell-auto-correct-word
549 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
550 (key-description binding))
551 "Welcome to flyspell. Use Mouse-2 to correct words."))))
553 ;; Use this so that we can still get major mode bindings at a
554 ;; misspelled word (unless they're overridden by
555 ;; `flyspell-mouse-map').
556 (set (make-local-variable 'flyspell-local-mouse-map)
557 (let ((map (copy-keymap flyspell-mouse-map)))
558 (if (eq flyspell-emacs 'xemacs)
559 (set-keymap-parents (list (current-local-map)))
560 (set-keymap-parent map (current-local-map)))
563 ;; we end with the flyspell hooks
564 (run-hooks 'flyspell-mode-hook))
566 ;*---------------------------------------------------------------------*/
567 ;* flyspell-delay-commands ... */
568 ;*---------------------------------------------------------------------*/
569 (defun flyspell-delay-commands ()
570 "Install the standard set of Flyspell delayed commands."
571 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
572 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
574 ;*---------------------------------------------------------------------*/
575 ;* flyspell-delay-command ... */
576 ;*---------------------------------------------------------------------*/
577 (defun flyspell-delay-command (command)
578 "Set COMMAND to be delayed, for Flyspell.
579 When flyspell `post-command-hook' is invoked because a delayed command
580 as been used the current word is not immediatly checked.
581 It will be checked only after `flyspell-delay' seconds."
582 (interactive "SDelay Flyspell after Command: ")
583 (put command 'flyspell-delayed t))
585 ;*---------------------------------------------------------------------*/
586 ;* flyspell-deplacement-commands ... */
587 ;*---------------------------------------------------------------------*/
588 (defun flyspell-deplacement-commands ()
589 "Install the standard set of Flyspell deplacement commands."
590 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
591 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
593 ;*---------------------------------------------------------------------*/
594 ;* flyspell-deplacement-command ... */
595 ;*---------------------------------------------------------------------*/
596 (defun flyspell-deplacement-command (command)
597 "Set COMMAND that implement cursor movements, for Flyspell.
598 When flyspell `post-command-hook' is invoked because of a deplacement command
599 as been used the current word is checked only if the previous command was
600 not the very same deplacement command."
601 (interactive "SDeplacement Flyspell after Command: ")
602 (put command 'flyspell-deplacement t))
604 ;*---------------------------------------------------------------------*/
605 ;* flyspell-word-cache ... */
606 ;*---------------------------------------------------------------------*/
607 (defvar flyspell-word-cache-start nil)
608 (defvar flyspell-word-cache-end nil)
609 (defvar flyspell-word-cache-word nil)
610 (make-variable-buffer-local 'flyspell-word-cache-start)
611 (make-variable-buffer-local 'flyspell-word-cache-end)
612 (make-variable-buffer-local 'flyspell-word-cache-word)
614 ;*---------------------------------------------------------------------*/
615 ;* The flyspell pre-hook, store the current position. In the */
616 ;* post command hook, we will check, if the word at this position */
617 ;* has to be spell checked. */
618 ;*---------------------------------------------------------------------*/
619 (defvar flyspell-pre-buffer nil)
620 (defvar flyspell-pre-point nil)
621 (defvar flyspell-pre-column nil)
622 (defvar flyspell-pre-pre-buffer nil)
623 (defvar flyspell-pre-pre-point nil)
625 ;*---------------------------------------------------------------------*/
626 ;* flyspell-previous-command ... */
627 ;*---------------------------------------------------------------------*/
628 (defvar flyspell-previous-command nil
629 "The last interactive command checked by Flyspell.")
631 ;*---------------------------------------------------------------------*/
632 ;* flyspell-pre-command-hook ... */
633 ;*---------------------------------------------------------------------*/
634 (defun flyspell-pre-command-hook ()
635 "Save the current buffer and point for Flyspell's post-command hook."
637 (setq flyspell-pre-buffer (current-buffer))
638 (setq flyspell-pre-point (point))
639 (setq flyspell-pre-column (current-column)))
641 ;*---------------------------------------------------------------------*/
642 ;* flyspell-mode-off ... */
643 ;*---------------------------------------------------------------------*/
645 (defun flyspell-mode-off ()
646 "Turn Flyspell mode off."
647 ;; we remove the hooks
648 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
649 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
650 (setq after-change-functions (delq 'flyspell-after-change-function
651 after-change-functions))
652 ;; we remove all the flyspell hilightings
653 (flyspell-delete-all-overlays)
654 ;; we have to erase pre cache variables
655 (setq flyspell-pre-buffer nil)
656 (setq flyspell-pre-point nil)
657 ;; we mark the mode as killed
658 (setq flyspell-mode nil))
660 ;*---------------------------------------------------------------------*/
661 ;* flyspell-check-pre-word-p ... */
662 ;*---------------------------------------------------------------------*/
663 (defun flyspell-check-pre-word-p ()
664 "Return non-nil if we should to check the word before point.
665 More precisely, it applies to the word that was before point
666 before the current command."
668 ((or (not (numberp flyspell-pre-point))
669 (not (bufferp flyspell-pre-buffer))
670 (not (buffer-live-p flyspell-pre-buffer)))
672 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
673 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
675 ((or (and (= flyspell-pre-point (- (point) 1))
676 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
677 (= flyspell-pre-point (point))
678 (= flyspell-pre-point (+ (point) 1)))
680 ((and (symbolp this-command)
681 (or (get this-command 'flyspell-delayed)
682 (and (get this-command 'flyspell-deplacement)
683 (eq flyspell-previous-command this-command)))
684 (or (= (current-column) 0)
685 (= (current-column) flyspell-pre-column)
686 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
688 ((not (eq (current-buffer) flyspell-pre-buffer))
690 ((not (and (numberp flyspell-word-cache-start)
691 (numberp flyspell-word-cache-end)))
694 (or (< flyspell-pre-point flyspell-word-cache-start)
695 (> flyspell-pre-point flyspell-word-cache-end)))))
697 ;*---------------------------------------------------------------------*/
698 ;* The flyspell after-change-hook, store the change position. In */
699 ;* the post command hook, we will check, if the word at this */
700 ;* position has to be spell checked. */
701 ;*---------------------------------------------------------------------*/
702 (defvar flyspell-changes nil)
704 ;*---------------------------------------------------------------------*/
705 ;* flyspell-after-change-function ... */
706 ;*---------------------------------------------------------------------*/
707 (defun flyspell-after-change-function (start stop len)
708 "Save the current buffer and point for Flyspell's post-command hook."
710 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
712 ;*---------------------------------------------------------------------*/
713 ;* flyspell-check-changed-word-p ... */
714 ;*---------------------------------------------------------------------*/
715 (defun flyspell-check-changed-word-p (start stop)
716 "Return t when the changed word has to be checked.
717 The answer depends of several criteria.
718 Mostly we check word delimiters."
720 ((and (eq (char-after start) ?\n) (> stop start))
722 ((not (numberp flyspell-pre-point))
724 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
726 ((let ((pos (point)))
727 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
732 ;*---------------------------------------------------------------------*/
733 ;* flyspell-check-word-p ... */
734 ;*---------------------------------------------------------------------*/
735 (defun flyspell-check-word-p ()
736 "Return t when the word at `point' has to be checked.
737 The answer depends of several criteria.
738 Mostly we check word delimiters."
740 ((<= (- (point-max) 1) (point-min))
741 ;; the buffer is not filled enough
743 ((and (and (> (current-column) 0)
744 (not (eq (current-column) flyspell-pre-column)))
747 (and (looking-at (flyspell-get-not-casechars))
748 (or flyspell-consider-dash-as-word-delimiter-flag
749 (not (looking-at "\\-"))))))
750 ;; yes because we have reached or typed a word delimiter.
752 ((symbolp this-command)
754 ((get this-command 'flyspell-deplacement)
755 (not (eq flyspell-previous-command this-command)))
756 ((get this-command 'flyspell-delayed)
757 ;; the current command is not delayed, that
758 ;; is that we must check the word now
759 (if (fboundp 'about-xemacs)
760 (sit-for flyspell-delay nil)
761 (sit-for flyspell-delay 0 nil)))
765 ;*---------------------------------------------------------------------*/
766 ;* flyspell-debug-signal-no-check ... */
767 ;*---------------------------------------------------------------------*/
768 (defun flyspell-debug-signal-no-check (msg obj)
769 (setq debug-on-error t)
771 (let ((buffer (get-buffer-create "*flyspell-debug*")))
774 (insert "NO-CHECK:\n")
775 (insert (format " %S : %S\n" msg obj)))))
777 ;*---------------------------------------------------------------------*/
778 ;* flyspell-debug-signal-pre-word-checked ... */
779 ;*---------------------------------------------------------------------*/
780 (defun flyspell-debug-signal-pre-word-checked ()
781 (setq debug-on-error t)
783 (let ((buffer (get-buffer-create "*flyspell-debug*")))
785 (insert "PRE-WORD:\n")
786 (insert (format " pre-point : %S\n" flyspell-pre-point))
787 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
788 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
789 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
790 (goto-char (point-max)))))
792 ;*---------------------------------------------------------------------*/
793 ;* flyspell-debug-signal-word-checked ... */
794 ;*---------------------------------------------------------------------*/
795 (defun flyspell-debug-signal-word-checked ()
796 (setq debug-on-error t)
798 (let ((oldbuf (current-buffer))
799 (buffer (get-buffer-create "*flyspell-debug*"))
803 (insert (format " this-cmd : %S\n" this-command))
804 (insert (format " delayed : %S\n" (and (symbolp this-command)
805 (get this-command 'flyspell-delayed))))
806 (insert (format " point : %S\n" point))
807 (insert (format " prev-char : [%c] %S\n"
810 (let ((c (if (> (point) (point-min))
813 (char-after (point)))
819 (let ((c (if (> (point) (point-min))
822 (and (and (looking-at (flyspell-get-not-casechars)) 1)
823 (and (or flyspell-consider-dash-as-word-delimiter-flag
824 (not (looking-at "\\-"))) 2))))))
827 (insert (format " because : %S\n"
829 ((not (and (symbolp this-command)
830 (get this-command 'flyspell-delayed)))
831 ;; the current command is not delayed, that
832 ;; is that we must check the word now
836 (let ((c (if (> (point) (point-min))
839 (and (looking-at (flyspell-get-not-casechars))
840 (or flyspell-consider-dash-as-word-delimiter-flag
841 (not (looking-at "\\-"))))))))
844 ;; yes because we have reached or typed a word delimiter.
846 ((not (integerp flyspell-delay))
847 ;; yes because the user had set up a no-delay configuration.
851 (goto-char (point-max)))))
853 ;*---------------------------------------------------------------------*/
854 ;* flyspell-debug-signal-changed-checked ... */
855 ;*---------------------------------------------------------------------*/
856 (defun flyspell-debug-signal-changed-checked ()
857 (setq debug-on-error t)
859 (let ((buffer (get-buffer-create "*flyspell-debug*"))
862 (insert "CHANGED WORD:\n")
863 (insert (format " point : %S\n" point))
864 (goto-char (point-max)))))
866 ;*---------------------------------------------------------------------*/
867 ;* flyspell-post-command-hook ... */
868 ;* ------------------------------------------------------------- */
869 ;* It is possible that we check several words: */
870 ;* 1- the current word is checked if the predicate */
871 ;* FLYSPELL-CHECK-WORD-P is true */
872 ;* 2- the word that used to be the current word before the */
873 ;* THIS-COMMAND is checked if: */
874 ;* a- the previous word is different from the current word */
875 ;* b- the previous word as not just been checked by the */
876 ;* previous FLYSPELL-POST-COMMAND-HOOK */
877 ;* 3- the words changed by the THIS-COMMAND that are neither the */
878 ;* previous word nor the current word */
879 ;*---------------------------------------------------------------------*/
880 (defun flyspell-post-command-hook ()
881 "The `post-command-hook' used by flyspell to check a word in-the-fly."
883 (let ((command this-command))
884 (if (flyspell-check-pre-word-p)
886 '(flyspell-debug-signal-pre-word-checked)
887 (set-buffer flyspell-pre-buffer)
889 (goto-char flyspell-pre-point)
891 (if (flyspell-check-word-p)
893 '(flyspell-debug-signal-word-checked)
895 ;; we remember which word we have just checked.
896 ;; this will be used next time we will check a word
897 ;; to compare the next current word with the word
898 ;; that as been registered in the pre-command-hook
899 ;; that is these variables are used within the predicate
900 ;; FLYSPELL-CHECK-PRE-WORD-P
901 (setq flyspell-pre-pre-buffer (current-buffer))
902 (setq flyspell-pre-pre-point (point)))
904 (setq flyspell-pre-pre-buffer nil)
905 (setq flyspell-pre-pre-point nil)
906 ;; when a word is not checked because of a delayed command
907 ;; we do not disable the ispell cache.
908 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
909 (setq flyspell-word-cache-end -1))))
910 (while (consp flyspell-changes)
911 (let ((start (car (car flyspell-changes)))
912 (stop (cdr (car flyspell-changes))))
913 (if (flyspell-check-changed-word-p start stop)
915 '(flyspell-debug-signal-changed-checked)
918 (setq flyspell-changes (cdr flyspell-changes))))
919 (setq flyspell-previous-command command)))
921 ;*---------------------------------------------------------------------*/
922 ;* flyspell-notify-misspell ... */
923 ;*---------------------------------------------------------------------*/
924 (defun flyspell-notify-misspell (start end word poss)
925 (let ((replacements (if (stringp poss)
927 (if flyspell-sort-corrections
928 (sort (car (cdr (cdr poss))) 'string<)
929 (car (cdr (cdr poss)))))))
930 (message (format "mispelling `%s' %S" word replacements))))
932 ;*---------------------------------------------------------------------*/
933 ;* flyspell-word ... */
934 ;*---------------------------------------------------------------------*/
935 (defun flyspell-word (&optional following)
936 "Spell check a word."
937 (interactive (list current-prefix-arg))
939 (setq following ispell-following-word))
941 ;; use the correct dictionary
942 (flyspell-accept-buffer-local-defs)
943 (let* ((cursor-location (point))
944 (flyspell-word (flyspell-get-word following))
946 (if (or (eq flyspell-word nil)
947 (and (fboundp flyspell-generic-check-word-p)
948 (not (funcall flyspell-generic-check-word-p))))
951 ;; destructure return flyspell-word info list.
952 (setq start (car (cdr flyspell-word))
953 end (car (cdr (cdr flyspell-word)))
954 word (car flyspell-word))
955 ;; before checking in the directory, we check for doublons.
957 ((and (or (not (eq ispell-parser 'tex))
958 (not (eq (char-after start) ?\\)))
959 flyspell-mark-duplications-flag
962 (word-search-backward word
966 ;; yes, this is a doublon
967 (flyspell-highlight-incorrect-region start end 'doublon))
968 ((and (eq flyspell-word-cache-start start)
969 (eq flyspell-word-cache-end end)
970 (string-equal flyspell-word-cache-word word))
971 ;; this word had been already checked, we skip
973 ((and (eq ispell-parser 'tex)
974 (flyspell-tex-command-p flyspell-word))
975 ;; this is a correct word (because a tex command)
976 (flyspell-unhighlight-at start)
978 (flyspell-unhighlight-at (- end 1)))
981 ;; we setup the cache
982 (setq flyspell-word-cache-start start)
983 (setq flyspell-word-cache-end end)
984 (setq flyspell-word-cache-word word)
985 ;; now check spelling of word.
986 (process-send-string ispell-process "%\n")
987 ;; put in verbose mode
988 (process-send-string ispell-process
989 (concat "^" word "\n"))
990 ;; we mark the ispell process so it can be killed
991 ;; when emacs is exited without query
992 (if (fboundp 'process-kill-without-query)
993 (process-kill-without-query ispell-process))
994 ;; wait until ispell has processed word
996 (accept-process-output ispell-process)
997 (not (string= "" (car ispell-filter)))))
998 ;; (process-send-string ispell-process "!\n")
999 ;; back to terse mode.
1000 (setq ispell-filter (cdr ispell-filter))
1001 (if (consp ispell-filter)
1002 (setq poss (ispell-parse-output (car ispell-filter))))
1005 (flyspell-unhighlight-at start)
1007 (flyspell-unhighlight-at (- end 1)))
1009 ((and (stringp poss) flyspell-highlight-flag)
1011 (flyspell-unhighlight-at start)
1013 (flyspell-unhighlight-at (- end 1)))
1016 (flyspell-unhighlight-at start)
1018 (flyspell-unhighlight-at (- end 1))))
1019 ((or (and (< flyspell-duplicate-distance 0)
1022 (word-search-backward word
1027 (word-search-forward word
1030 (and (> flyspell-duplicate-distance 0)
1033 (word-search-backward
1036 flyspell-duplicate-distance)
1040 (word-search-forward
1043 flyspell-duplicate-distance)
1045 (if flyspell-highlight-flag
1046 (flyspell-highlight-duplicate-region start end)
1047 (message (format "duplicate `%s'" word))))
1049 ;; incorrect highlight the location
1050 (if flyspell-highlight-flag
1051 (flyspell-highlight-incorrect-region start end poss)
1052 (flyspell-notify-misspell start end word poss))))
1053 ;; return to original location
1054 (goto-char cursor-location)
1055 (if ispell-quit (setq ispell-quit nil)))))))))
1057 ;*---------------------------------------------------------------------*/
1058 ;* flyspell-tex-math-initialized ... */
1059 ;*---------------------------------------------------------------------*/
1060 (defvar flyspell-tex-math-initialized nil)
1062 ;*---------------------------------------------------------------------*/
1063 ;* flyspell-math-tex-command-p ... */
1064 ;* ------------------------------------------------------------- */
1065 ;* This function uses the texmathp package to check if (point) */
1066 ;* is within a tex command. In order to avoid using */
1067 ;* condition-case each time we use the variable */
1068 ;* flyspell-tex-math-initialized to make a special case the first */
1069 ;* time that function is called. */
1070 ;*---------------------------------------------------------------------*/
1071 (defun flyspell-math-tex-command-p ()
1073 (flyspell-check-tex-math-command
1075 ((eq flyspell-tex-math-initialized t)
1077 ((eq flyspell-tex-math-initialized 'error)
1080 (setq flyspell-tex-math-initialized t)
1084 (setq flyspell-tex-math-initialized 'error)
1087 ;*---------------------------------------------------------------------*/
1088 ;* flyspell-tex-command-p ... */
1089 ;*---------------------------------------------------------------------*/
1090 (defun flyspell-tex-command-p (word)
1091 "Return t if WORD is a TeX command."
1093 (let ((b (car (cdr word))))
1094 (and (re-search-backward "\\\\" (- (point) 100) t)
1095 (or (= (match-end 0) b)
1096 (and (goto-char (match-end 0))
1097 (looking-at flyspell-tex-command-regexp)
1098 (>= (match-end 0) b))))))
1099 (flyspell-math-tex-command-p)))
1101 ;*---------------------------------------------------------------------*/
1102 ;* flyspell-casechars-cache ... */
1103 ;*---------------------------------------------------------------------*/
1104 (defvar flyspell-casechars-cache nil)
1105 (defvar flyspell-ispell-casechars-cache nil)
1106 (make-variable-buffer-local 'flyspell-casechars-cache)
1107 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1109 ;*---------------------------------------------------------------------*/
1110 ;* flyspell-get-casechars ... */
1111 ;*---------------------------------------------------------------------*/
1112 (defun flyspell-get-casechars ()
1113 "This function builds a string that is the regexp of word chars.
1114 In order to avoid one useless string construction,
1115 this function changes the last char of the `ispell-casechars' string."
1116 (let ((ispell-casechars (ispell-get-casechars)))
1118 ((eq ispell-parser 'tex)
1119 (setq flyspell-ispell-casechars-cache ispell-casechars)
1120 (setq flyspell-casechars-cache
1121 (concat (substring ispell-casechars
1123 (- (length ispell-casechars) 1))
1125 flyspell-casechars-cache)
1127 (setq flyspell-ispell-casechars-cache ispell-casechars)
1128 (setq flyspell-casechars-cache ispell-casechars)
1129 flyspell-casechars-cache))))
1131 ;*---------------------------------------------------------------------*/
1132 ;* flyspell-get-not-casechars-cache ... */
1133 ;*---------------------------------------------------------------------*/
1134 (defvar flyspell-not-casechars-cache nil)
1135 (defvar flyspell-ispell-not-casechars-cache nil)
1136 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1137 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1139 ;*---------------------------------------------------------------------*/
1140 ;* flyspell-get-not-casechars ... */
1141 ;*---------------------------------------------------------------------*/
1142 (defun flyspell-get-not-casechars ()
1143 "This function builds a string that is the regexp of non-word chars."
1144 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1146 ((eq ispell-parser 'tex)
1147 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1148 (setq flyspell-not-casechars-cache
1149 (concat (substring ispell-not-casechars
1151 (- (length ispell-not-casechars) 1))
1153 flyspell-not-casechars-cache)
1155 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1156 (setq flyspell-not-casechars-cache ispell-not-casechars)
1157 flyspell-not-casechars-cache))))
1159 ;*---------------------------------------------------------------------*/
1160 ;* flyspell-get-word ... */
1161 ;*---------------------------------------------------------------------*/
1162 (defun flyspell-get-word (following)
1163 "Return the word for spell-checking according to Ispell syntax.
1164 If argument FOLLOWING is non-nil or if `ispell-following-word'
1165 is non-nil when called interactively, then the following word
1166 \(rather than preceding\) is checked when the cursor is not over a word.
1167 Optional second argument contains otherchars that can be included in word
1170 Word syntax described by `ispell-dictionary-alist' (which see)."
1171 (let* ((flyspell-casechars (flyspell-get-casechars))
1172 (flyspell-not-casechars (flyspell-get-not-casechars))
1173 (ispell-otherchars (ispell-get-otherchars))
1174 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1175 (word-regexp (if (string< "" ispell-otherchars)
1176 (concat flyspell-casechars
1182 (if ispell-many-otherchars-p
1184 (concat flyspell-casechars "+")))
1188 (if (not (looking-at flyspell-casechars))
1190 (re-search-forward flyspell-casechars (point-max) t)
1191 (re-search-backward flyspell-casechars (point-min) t)))
1192 ;; move to front of word
1193 (re-search-backward flyspell-not-casechars (point-min) 'start)
1195 (if (string< "" ispell-otherchars)
1196 (while (and (looking-at ispell-otherchars)
1198 (or (not did-it-once)
1199 ispell-many-otherchars-p)
1200 (not (eq pos (point))))
1202 (setq did-it-once t)
1204 (if (looking-at flyspell-casechars)
1205 (re-search-backward flyspell-not-casechars (point-min) 'move)
1206 (backward-char -1)))))
1207 ;; Now mark the word and save to string.
1208 (if (eq (re-search-forward word-regexp (point-max) t) nil)
1211 (setq start (match-beginning 0)
1213 word (buffer-substring start end))
1214 (list word start end)))))
1216 ;*---------------------------------------------------------------------*/
1217 ;* flyspell-small-region ... */
1218 ;*---------------------------------------------------------------------*/
1219 (defun flyspell-small-region (beg end)
1220 "Flyspell text between BEG and END."
1228 (while (< (point) end)
1231 (message "Spell Checking...%d%%"
1232 (* 100 (/ (float (- (point) beg)) (- end beg))))
1234 (setq count (+ 1 count)))
1237 (let ((cur (point)))
1239 (if (and (< (point) end) (> (point) (+ cur 1)))
1240 (backward-char 1)))))
1242 (message "Spell Checking completed.")
1245 ;*---------------------------------------------------------------------*/
1246 ;* flyspell-external-ispell-process ... */
1247 ;*---------------------------------------------------------------------*/
1248 (defvar flyspell-external-ispell-process '()
1249 "The external Flyspell ispell process")
1251 ;*---------------------------------------------------------------------*/
1252 ;* flyspell-external-ispell-buffer ... */
1253 ;*---------------------------------------------------------------------*/
1254 (defvar flyspell-external-ispell-buffer '())
1255 (defvar flyspell-large-region-buffer '())
1256 (defvar flyspell-large-region-beg (point-min))
1257 (defvar flyspell-large-region-end (point-max))
1259 ;*---------------------------------------------------------------------*/
1260 ;* flyspell-external-point-words ... */
1261 ;*---------------------------------------------------------------------*/
1262 (defun flyspell-external-point-words ()
1263 (let ((buffer flyspell-external-ispell-buffer))
1265 (beginning-of-buffer)
1266 (let ((size (- flyspell-large-region-end flyspell-large-region-beg))
1267 (start flyspell-large-region-beg))
1268 ;; now we are done with ispell, we have to find the word in
1269 ;; the initial buffer
1270 (while (< (point) (- (point-max) 1))
1271 ;; we have to fetch the incorrect word
1272 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1273 (let ((word (match-string 1)))
1274 (goto-char (match-end 0))
1275 (set-buffer flyspell-large-region-buffer)
1276 (goto-char flyspell-large-region-beg)
1277 (message "Spell Checking...%d%% [%s]"
1278 (* 100 (/ (float (- (point) start)) size))
1280 (if (search-forward word flyspell-large-region-end t)
1282 (setq flyspell-large-region-beg (point))
1283 (goto-char (- (point) 1))
1285 (set-buffer buffer))
1286 (goto-char (point-max)))))
1288 (message "Spell Checking completed.")
1289 ;; ok, we are done with pointing out incorrect words, we just
1290 ;; have to kill the temporary buffer
1291 (kill-buffer flyspell-external-ispell-buffer)
1292 (setq flyspell-external-ispell-buffer nil)))
1294 ;*---------------------------------------------------------------------*/
1295 ;* flyspell-large-region ... */
1296 ;*---------------------------------------------------------------------*/
1297 (defun flyspell-large-region (beg end)
1298 (let* ((curbuf (current-buffer))
1299 (buffer (get-buffer-create "*flyspell-region*")))
1300 (setq flyspell-external-ispell-buffer buffer)
1301 (setq flyspell-large-region-buffer curbuf)
1302 (setq flyspell-large-region-beg beg)
1303 (setq flyspell-large-region-end end)
1306 ;; this is done, we can start ckecking...
1307 (message "Checking region...")
1309 (let ((c (apply 'call-process-region beg
1317 ;; Local dictionary becomes the global dictionary in use.
1318 (if ispell-local-dictionary
1319 (setq ispell-dictionary ispell-local-dictionary))
1320 (setq args (ispell-get-ispell-args))
1321 (if ispell-dictionary ; use specified dictionary
1323 (append (list "-d" ispell-dictionary) args)))
1324 (if ispell-personal-dictionary ; use specified pers dict
1329 ispell-personal-dictionary)))))
1330 (setq args (append args ispell-extra-args))
1333 (flyspell-external-point-words)
1334 (error "Can't check region...")))))
1336 ;*---------------------------------------------------------------------*/
1337 ;* flyspell-region ... */
1338 ;* ------------------------------------------------------------- */
1339 ;* Because `ispell -a' is too slow, it is not possible to use */
1340 ;* it on large region. Then, when ispell is invoked on a large */
1341 ;* text region, a new `ispell -l' process is spawned. The */
1342 ;* pointed out words are then searched in the region a checked with */
1343 ;* regular flyspell means. */
1344 ;*---------------------------------------------------------------------*/
1345 (defun flyspell-region (beg end)
1346 "Flyspell text between BEG and END."
1355 (if (> (- end beg) flyspell-large-region)
1356 (flyspell-large-region beg end)
1357 (flyspell-small-region beg end)))))
1359 ;*---------------------------------------------------------------------*/
1360 ;* flyspell-buffer ... */
1361 ;*---------------------------------------------------------------------*/
1362 (defun flyspell-buffer ()
1363 "Flyspell whole buffer."
1365 (flyspell-region (point-min) (point-max)))
1367 ;*---------------------------------------------------------------------*/
1368 ;* old next error position ... */
1369 ;*---------------------------------------------------------------------*/
1370 (defvar flyspell-old-buffer-error nil)
1371 (defvar flyspell-old-pos-error nil)
1373 ;*---------------------------------------------------------------------*/
1374 ;* flyspell-goto-next-error ... */
1375 ;*---------------------------------------------------------------------*/
1376 (defun flyspell-goto-next-error ()
1377 "Go to the next previously detected error.
1378 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1383 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1384 (eq pos flyspell-old-pos-error))
1386 (if (= flyspell-old-pos-error max)
1387 ;; goto beginning of buffer
1389 (message "Restarting from beginning of buffer")
1390 (goto-char (point-min)))
1392 (setq pos (point))))
1393 ;; seek the next error
1394 (while (and (< pos max)
1395 (let ((ovs (overlays-at pos))
1397 (while (and (not r) (consp ovs))
1398 (if (flyspell-overlay-p (car ovs))
1400 (setq ovs (cdr ovs))))
1402 (setq pos (1+ pos)))
1403 ;; save the current location for next invokation
1404 (setq flyspell-old-pos-error pos)
1405 (setq flyspell-old-buffer-error (current-buffer))
1408 (message "No more miss-spelled word!"))))
1410 ;*---------------------------------------------------------------------*/
1411 ;* flyspell-overlay-p ... */
1412 ;*---------------------------------------------------------------------*/
1413 (defun flyspell-overlay-p (o)
1414 "A predicate that return true iff O is an overlay used by flyspell."
1415 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1417 ;*---------------------------------------------------------------------*/
1418 ;* flyspell-delete-all-overlays ... */
1419 ;* ------------------------------------------------------------- */
1420 ;* Remove all the overlays introduced by flyspell. */
1421 ;*---------------------------------------------------------------------*/
1422 (defun flyspell-delete-all-overlays ()
1423 "Delete all the overlays used by flyspell."
1424 (let ((l (overlays-in (point-min) (point-max))))
1427 (if (flyspell-overlay-p (car l))
1428 (delete-overlay (car l)))
1429 (setq l (cdr l))))))
1431 ;*---------------------------------------------------------------------*/
1432 ;* flyspell-unhighlight-at ... */
1433 ;*---------------------------------------------------------------------*/
1434 (defun flyspell-unhighlight-at (pos)
1435 "Remove the flyspell overlay that are located at POS."
1436 (if flyspell-persistent-highlight
1437 (let ((overlays (overlays-at pos)))
1438 (while (consp overlays)
1439 (if (flyspell-overlay-p (car overlays))
1440 (delete-overlay (car overlays)))
1441 (setq overlays (cdr overlays))))
1442 (if (flyspell-overlay-p flyspell-overlay)
1443 (delete-overlay flyspell-overlay))))
1445 ;*---------------------------------------------------------------------*/
1446 ;* flyspell-properties-at-p ... */
1447 ;* ------------------------------------------------------------- */
1448 ;* Is there an highlight properties at position pos? */
1449 ;*---------------------------------------------------------------------*/
1450 (defun flyspell-properties-at-p (pos)
1451 "Return t if there is a text property at POS, not counting `local-map'.
1452 If variable `flyspell-highlight-properties' is set to nil,
1453 text with properties are not checked. This function is used to discover
1454 if the character at POS has any other property."
1455 (let ((prop (text-properties-at pos))
1457 (while (and keep (consp prop))
1458 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1459 (setq prop (cdr (cdr prop)))
1463 ;*---------------------------------------------------------------------*/
1464 ;* make-flyspell-overlay ... */
1465 ;*---------------------------------------------------------------------*/
1466 (defun make-flyspell-overlay (beg end face mouse-face)
1467 "Allocate an overlay to highlight an incorrect word.
1468 BEG and END specify the range in the buffer of that word.
1469 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1471 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1472 (overlay-put flyspell-overlay 'face face)
1473 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1474 (overlay-put flyspell-overlay 'flyspell-overlay t)
1475 (if flyspell-use-local-map
1476 (overlay-put flyspell-overlay
1477 flyspell-overlay-keymap-property-name
1478 flyspell-local-mouse-map))
1481 ;*---------------------------------------------------------------------*/
1482 ;* flyspell-highlight-incorrect-region ... */
1483 ;*---------------------------------------------------------------------*/
1484 (defun flyspell-highlight-incorrect-region (beg end poss)
1485 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1486 (unless (run-hook-with-args-until-success
1487 'flyspell-incorrect-hook beg end poss)
1488 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1490 ;; we cleanup current overlay at the same position
1491 (if (and (not flyspell-persistent-highlight)
1492 (overlayp flyspell-overlay))
1493 (delete-overlay flyspell-overlay)
1494 (let ((overlays (overlays-at beg)))
1495 (while (consp overlays)
1496 (if (flyspell-overlay-p (car overlays))
1497 (delete-overlay (car overlays)))
1498 (setq overlays (cdr overlays)))))
1499 ;; now we can use a new overlay
1500 (setq flyspell-overlay
1501 (make-flyspell-overlay beg end
1502 'flyspell-incorrect-face 'highlight))))))
1504 ;*---------------------------------------------------------------------*/
1505 ;* flyspell-highlight-duplicate-region ... */
1506 ;*---------------------------------------------------------------------*/
1507 (defun flyspell-highlight-duplicate-region (beg end)
1508 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
1509 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1511 ;; we cleanup current overlay at the same position
1512 (if (and (not flyspell-persistent-highlight)
1513 (overlayp flyspell-overlay))
1514 (delete-overlay flyspell-overlay)
1515 (let ((overlays (overlays-at beg)))
1516 (while (consp overlays)
1517 (if (flyspell-overlay-p (car overlays))
1518 (delete-overlay (car overlays)))
1519 (setq overlays (cdr overlays)))))
1520 ;; now we can use a new overlay
1521 (setq flyspell-overlay
1522 (make-flyspell-overlay beg end
1523 'flyspell-duplicate-face 'highlight)))))
1525 ;*---------------------------------------------------------------------*/
1526 ;* flyspell-auto-correct-cache ... */
1527 ;*---------------------------------------------------------------------*/
1528 (defvar flyspell-auto-correct-pos nil)
1529 (defvar flyspell-auto-correct-region nil)
1530 (defvar flyspell-auto-correct-ring nil)
1531 (defvar flyspell-auto-correct-word nil)
1532 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1533 (make-variable-buffer-local 'flyspell-auto-correct-region)
1534 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1535 (make-variable-buffer-local 'flyspell-auto-correct-word)
1537 ;*---------------------------------------------------------------------*/
1538 ;* flyspell-check-previous-highlighted-word ... */
1539 ;*---------------------------------------------------------------------*/
1540 (defun flyspell-check-previous-highlighted-word (&optional arg)
1541 "Correct the closer mispelled word.
1542 This function scans a mis-spelled word before the cursor. If it finds one
1543 it proposes replacement for that word. With prefix arg, count that many
1544 misspelled words backwards."
1546 (let ((pos1 (point))
1548 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1551 (while (and (setq pos (previous-overlay-change pos))
1554 (if (> pos (point-min))
1556 (setq ovs (overlays-at (1- pos)))
1559 (setq ovs (cdr ovs))
1560 (if (and (overlay-get ov 'flyspell-overlay)
1561 (= 0 (setq arg (1- arg))))
1562 (throw 'exit t)))))))
1566 (error "No word to correct before point."))))
1568 ;*---------------------------------------------------------------------*/
1569 ;* flyspell-display-next-corrections ... */
1570 ;*---------------------------------------------------------------------*/
1571 (defun flyspell-display-next-corrections (corrections)
1572 (let ((string "Corrections:")
1575 (while (< (length string) 80)
1576 (if (equal (car l) flyspell-auto-correct-word)
1577 (setq pos (cons (+ 1 (length string)) pos)))
1578 (setq string (concat string " " (car l)))
1581 (let ((num (car pos)))
1582 (put-text-property num
1583 (+ num (length flyspell-auto-correct-word))
1585 'flyspell-incorrect-face
1587 (setq pos (cdr pos)))
1588 (if (fboundp 'display-message)
1589 (display-message 'no-log string)
1592 ;*---------------------------------------------------------------------*/
1593 ;* flyspell-abbrev-table ... */
1594 ;*---------------------------------------------------------------------*/
1595 (defun flyspell-abbrev-table ()
1596 (if flyspell-use-global-abbrev-table-p
1598 local-abbrev-table))
1600 ;*---------------------------------------------------------------------*/
1601 ;* flyspell-auto-correct-word ... */
1602 ;*---------------------------------------------------------------------*/
1603 (defun flyspell-auto-correct-word ()
1604 "Correct the current word.
1605 This command proposes various successive corrections for the current word."
1608 (old-max (point-max)))
1609 ;; use the correct dictionary
1610 (flyspell-accept-buffer-local-defs)
1611 (if (and (eq flyspell-auto-correct-pos pos)
1612 (consp flyspell-auto-correct-region))
1613 ;; we have already been using the function at the same location
1614 (let* ((start (car flyspell-auto-correct-region))
1615 (len (cdr flyspell-auto-correct-region)))
1616 (delete-region start (+ start len))
1617 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1618 (let* ((word (car flyspell-auto-correct-ring))
1619 (len (length word)))
1620 (rplacd flyspell-auto-correct-region len)
1622 (if flyspell-abbrev-p
1623 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1624 flyspell-auto-correct-word)
1625 (flyspell-change-abbrev (flyspell-abbrev-table)
1626 flyspell-auto-correct-word
1628 (define-abbrev (flyspell-abbrev-table)
1629 flyspell-auto-correct-word word)))
1632 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1633 (flyspell-ajust-cursor-point pos (point) old-max)
1634 (setq flyspell-auto-correct-pos (point)))
1635 ;; fetch the word to be checked
1636 (let ((word (flyspell-get-word nil))
1638 ;; destructure return word info list.
1639 (setq start (car (cdr word))
1640 end (car (cdr (cdr word)))
1642 (setq flyspell-auto-correct-word word)
1643 ;; now check spelling of word.
1644 (process-send-string ispell-process "%\n") ;put in verbose mode
1645 (process-send-string ispell-process (concat "^" word "\n"))
1646 ;; wait until ispell has processed word
1648 (accept-process-output ispell-process)
1649 (not (string= "" (car ispell-filter)))))
1650 (setq ispell-filter (cdr ispell-filter))
1651 (if (consp ispell-filter)
1652 (setq poss (ispell-parse-output (car ispell-filter))))
1653 (cond ((or (eq poss t) (stringp poss))
1654 ;; don't correct word
1658 (error "Ispell: error in Ispell process"))
1660 ;; the word is incorrect, we have to propose a replacement
1661 (let ((replacements (if flyspell-sort-corrections
1662 (sort (car (cdr (cdr poss))) 'string<)
1663 (car (cdr (cdr poss))))))
1664 (setq flyspell-auto-correct-region nil)
1665 (if (consp replacements)
1667 (let ((replace (car replacements)))
1668 (let ((new-word replace))
1669 (if (not (equal new-word (car poss)))
1671 ;; the save the current replacements
1672 (setq flyspell-auto-correct-region
1673 (cons start (length new-word)))
1674 (let ((l replacements))
1675 (while (consp (cdr l))
1677 (rplacd l (cons (car poss) replacements)))
1678 (setq flyspell-auto-correct-ring
1680 (delete-region start end)
1682 (if flyspell-abbrev-p
1683 (if (flyspell-already-abbrevp
1684 (flyspell-abbrev-table) word)
1685 (flyspell-change-abbrev
1686 (flyspell-abbrev-table)
1689 (define-abbrev (flyspell-abbrev-table)
1692 (flyspell-display-next-corrections
1693 (cons new-word flyspell-auto-correct-ring))
1694 (flyspell-ajust-cursor-point pos
1697 (setq flyspell-auto-correct-pos (point))
1698 (ispell-pdict-save t)))))
1700 ;*---------------------------------------------------------------------*/
1701 ;* flyspell-correct-word ... */
1702 ;*---------------------------------------------------------------------*/
1703 (defun flyspell-correct-word (event)
1704 "Check spelling of word under or before the cursor.
1705 If the word is not found in dictionary, display possible corrections
1706 in a popup menu allowing you to choose one.
1708 Word syntax described by `ispell-dictionary-alist' (which see).
1710 This will check or reload the dictionary. Use \\[ispell-change-dictionary]
1711 or \\[ispell-region] to update the Ispell process."
1713 (if (eq flyspell-emacs 'xemacs)
1714 (flyspell-correct-word/mouse-keymap event)
1715 (flyspell-correct-word/local-keymap event)))
1717 ;*---------------------------------------------------------------------*/
1718 ;* flyspell-correct-word/local-keymap ... */
1719 ;*---------------------------------------------------------------------*/
1720 (defun flyspell-correct-word/local-keymap (event)
1721 "emacs 19.xx seems to be buggous. Overlay keymap does not seems
1722 to work correctly with local map. That is, if a key is not
1723 defined for the overlay keymap, the current local map, is not
1724 checked. The binding is resolved with the global map. The
1725 consequence is that we can not use overlay map with flyspell."
1727 (save-window-excursion
1728 (let ((save (point)))
1729 (mouse-set-point event)
1730 ;; we look for a flyspell overlay here
1731 (let ((overlays (overlays-at (point)))
1733 (while (consp overlays)
1734 (if (flyspell-overlay-p (car overlays))
1736 (setq overlay (car overlays))
1737 (setq overlays nil))
1738 (setq overlays (cdr overlays))))
1739 ;; we return to the correct location
1741 ;; we check to see if button2 has been used overlay a
1744 ;; yes, so we use the flyspell function
1745 (flyspell-correct-word/mouse-keymap event)
1746 ;; no so we have to use the non flyspell binding
1747 (let ((flyspell-mode nil))
1748 (if (key-binding (this-command-keys))
1749 (command-execute (key-binding (this-command-keys))))))))))
1751 ;*---------------------------------------------------------------------*/
1752 ;* flyspell-correct-word/mouse-keymap ... */
1753 ;*---------------------------------------------------------------------*/
1754 (defun flyspell-correct-word/mouse-keymap (event)
1755 "Pop up a menu of possible corrections for a misspelled word.
1756 The word checked is the word at the mouse position."
1758 ;; use the correct dictionary
1759 (flyspell-accept-buffer-local-defs)
1760 ;; retain cursor location (I don't know why but save-excursion here fails).
1761 (let ((save (point)))
1762 (mouse-set-point event)
1763 (let ((cursor-location (point))
1764 (word (flyspell-get-word nil))
1765 start end poss replace)
1766 ;; destructure return word info list.
1767 (setq start (car (cdr word))
1768 end (car (cdr (cdr word)))
1770 ;; now check spelling of word.
1771 (process-send-string ispell-process "%\n") ;put in verbose mode
1772 (process-send-string ispell-process (concat "^" word "\n"))
1773 ;; wait until ispell has processed word
1775 (accept-process-output ispell-process)
1776 (not (string= "" (car ispell-filter)))))
1777 (setq ispell-filter (cdr ispell-filter))
1778 (if (consp ispell-filter)
1779 (setq poss (ispell-parse-output (car ispell-filter))))
1780 (cond ((or (eq poss t) (stringp poss))
1781 ;; don't correct word
1785 (error "Ispell: error in Ispell process"))
1786 ((string-match "GNU" (emacs-version))
1787 ;; the word is incorrect, we have to propose a replacement
1788 (setq replace (flyspell-emacs-popup event poss word))
1789 (cond ((eq replace 'ignore)
1794 (process-send-string ispell-process (concat "*" word "\n"))
1795 (flyspell-unhighlight-at cursor-location)
1796 (setq ispell-pdict-modified-p '(t)))
1797 ((or (eq replace 'buffer) (eq replace 'session))
1798 (process-send-string ispell-process (concat "@" word "\n"))
1799 (if (null ispell-pdict-modified-p)
1800 (setq ispell-pdict-modified-p
1801 (list ispell-pdict-modified-p)))
1802 (flyspell-unhighlight-at cursor-location)
1804 (if (eq replace 'buffer)
1805 (ispell-add-per-file-word-list word)))
1807 (let ((new-word (if (atom replace)
1810 (cursor-location (+ (- (length word) (- end start))
1812 (if (not (equal new-word (car poss)))
1813 (let ((old-max (point-max)))
1814 (delete-region start end)
1816 (if flyspell-abbrev-p
1817 (define-abbrev (flyspell-abbrev-table)
1820 (flyspell-ajust-cursor-point save
1826 ((eq flyspell-emacs 'xemacs)
1827 (flyspell-xemacs-popup
1828 event poss word cursor-location start end save)
1830 (ispell-pdict-save t))))
1832 ;*---------------------------------------------------------------------*/
1833 ;* flyspell-xemacs-correct ... */
1834 ;*---------------------------------------------------------------------*/
1835 (defun flyspell-xemacs-correct (replace poss word cursor-location start end save)
1836 "The xemacs popup menu callback."
1837 (cond ((eq replace 'ignore)
1840 (process-send-string ispell-process (concat "*" word "\n"))
1841 (process-send-string ispell-process "#\n")
1842 (flyspell-unhighlight-at cursor-location)
1843 (setq ispell-pdict-modified-p '(t)))
1844 ((or (eq replace 'buffer) (eq replace 'session))
1845 (process-send-string ispell-process (concat "@" word "\n"))
1846 (flyspell-unhighlight-at cursor-location)
1847 (if (null ispell-pdict-modified-p)
1848 (setq ispell-pdict-modified-p
1849 (list ispell-pdict-modified-p)))
1850 (if (eq replace 'buffer)
1851 (ispell-add-per-file-word-list word)))
1853 (let ((old-max (point-max))
1854 (new-word (if (atom replace)
1857 (cursor-location (+ (- (length word) (- end start))
1859 (if (not (equal new-word (car poss)))
1861 (delete-region start end)
1864 (if flyspell-abbrev-p
1865 (define-abbrev (flyspell-abbrev-table)
1868 (flyspell-ajust-cursor-point save cursor-location old-max)))))
1870 ;*---------------------------------------------------------------------*/
1871 ;* flyspell-ajust-cursor-point ... */
1872 ;*---------------------------------------------------------------------*/
1873 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
1874 (if (>= save cursor-location)
1875 (let ((new-pos (+ save (- (point-max) old-max))))
1877 ((< new-pos (point-min))
1879 ((> new-pos (point-max))
1884 ;*---------------------------------------------------------------------*/
1885 ;* flyspell-emacs-popup ... */
1886 ;*---------------------------------------------------------------------*/
1887 (defun flyspell-emacs-popup (event poss word)
1888 "The Emacs popup menu."
1890 (let* ((mouse-pos (mouse-position))
1891 (mouse-pos (if (nth 1 mouse-pos)
1893 (set-mouse-position (car mouse-pos)
1894 (/ (frame-width) 2) 2)
1897 (setq event (list (list (car (cdr mouse-pos))
1898 (1+ (cdr (cdr mouse-pos))))
1900 (let* ((corrects (if flyspell-sort-corrections
1901 (sort (car (cdr (cdr poss))) 'string<)
1902 (car (cdr (cdr poss)))))
1903 (cor-menu (if (consp corrects)
1904 (mapcar (lambda (correct)
1905 (list correct correct))
1908 (affix (car (cdr (cdr (cdr poss)))))
1909 (base-menu (let ((save (if (consp affix)
1911 (list (concat "Save affix: " (car affix))
1913 '("Accept (session)" accept)
1914 '("Accept (buffer)" buffer))
1915 '(("Save word" save)
1916 ("Accept (session)" session)
1917 ("Accept (buffer)" buffer)))))
1918 (if (consp cor-menu)
1919 (append cor-menu (cons "" save))
1921 (menu (cons "flyspell correction menu" base-menu)))
1922 (car (x-popup-menu event
1923 (list (format "%s [%s]" word (or ispell-local-dictionary
1927 ;*---------------------------------------------------------------------*/
1928 ;* flyspell-xemacs-popup ... */
1929 ;*---------------------------------------------------------------------*/
1930 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
1931 "The xemacs popup menu."
1932 (let* ((corrects (if flyspell-sort-corrections
1933 (sort (car (cdr (cdr poss))) 'string<)
1934 (car (cdr (cdr poss)))))
1935 (cor-menu (if (consp corrects)
1936 (mapcar (lambda (correct)
1938 (list 'flyspell-xemacs-correct
1949 (affix (car (cdr (cdr (cdr poss)))))
1950 (menu (let ((save (if (consp affix)
1952 (concat "Save affix: " (car affix))
1953 (list 'flyspell-xemacs-correct
1964 (list 'flyspell-xemacs-correct
1973 (session (vector "Accept (session)"
1974 (list 'flyspell-xemacs-correct
1983 (buffer (vector "Accept (buffer)"
1984 (list 'flyspell-xemacs-correct
1993 (if (consp cor-menu)
1994 (append cor-menu (list "-" save session buffer))
1995 (list save session buffer)))))
1996 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2000 ;*---------------------------------------------------------------------*/
2001 ;* Some example functions for real autocrrecting xb */
2002 ;*---------------------------------------------------------------------*/
2003 (defun flyspell-maybe-correct-transposition (beg end poss)
2004 "Apply 'transpose-chars' to all points in the region BEG to END and
2005 return t if any those result in a possible replacement suggested by ispell
2006 in POSS. Otherwise the change is undone.
2008 This function is meant to be added to 'flyspell-incorrect-hook'."
2011 (let ((str (buffer-substring beg end))
2012 (i 0) (len (- end beg)) tmp)
2013 (while (< (1+ i) len)
2014 (setq tmp (aref str i))
2015 (aset str i (aref str (1+ i)))
2016 (aset str (1+ i) tmp)
2017 (when (member str (nth 2 poss))
2019 (goto-char (+ beg i 1))
2020 (transpose-chars 1))
2022 (setq tmp (aref str i))
2023 (aset str i (aref str (1+ i)))
2024 (aset str (1+ i) tmp)
2028 (defun flyspell-maybe-correct-doubling (beg end poss)
2029 "For each doubled charachter in the region BEG to END, remove one and
2030 return t if any those result in a possible replacement suggested by ispell
2031 in POSS. Otherwise the change is undone.
2033 This function is meant to be added to 'flyspell-incorrect-hook'."
2036 (let ((str (buffer-substring beg end))
2037 (i 0) (len (- end beg)))
2038 (while (< (1+ i) len)
2039 (when (and (= (aref str i) (aref str (1+ i)))
2040 (member (concat (substring str 0 (1+ i))
2041 (substring str (+ i 2)))
2043 (goto-char (+ beg i))
2049 ;*---------------------------------------------------------------------*/
2050 ;* flyspell-already-abbrevp ... */
2051 ;*---------------------------------------------------------------------*/
2052 (defun flyspell-already-abbrevp (table word)
2053 (let ((sym (abbrev-symbol word table)))
2054 (and sym (symbolp sym))))
2056 ;*---------------------------------------------------------------------*/
2057 ;* flyspell-change-abbrev ... */
2058 ;*---------------------------------------------------------------------*/
2059 (defun flyspell-change-abbrev (table old new)
2060 (set (abbrev-symbol old table) new))
2063 ;;; flyspell.el ends here