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 (or (get this-command 'flyspell-delayed)
660 (and (get this-command 'flyspell-deplacement)
661 (eq flyspell-previous-command this-command)))
662 (or (= (current-column) 0)
663 (= (current-column) flyspell-pre-column)
664 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
666 ((not (eq (current-buffer) flyspell-pre-buffer))
668 ((not (and (numberp flyspell-word-cache-start)
669 (numberp flyspell-word-cache-end)))
672 (or (< flyspell-pre-point flyspell-word-cache-start)
673 (> flyspell-pre-point flyspell-word-cache-end)))))
675 ;*---------------------------------------------------------------------*/
676 ;* The flyspell after-change-hook, store the change position. In */
677 ;* the post command hook, we will check, if the word at this */
678 ;* position has to be spell checked. */
679 ;*---------------------------------------------------------------------*/
680 (defvar flyspell-changes nil)
682 ;*---------------------------------------------------------------------*/
683 ;* flyspell-after-change-function ... */
684 ;*---------------------------------------------------------------------*/
685 (defun flyspell-after-change-function (start stop len)
686 "Save the current buffer and point for Flyspell's post-command hook."
688 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
690 ;*---------------------------------------------------------------------*/
691 ;* flyspell-check-changed-word-p ... */
692 ;*---------------------------------------------------------------------*/
693 (defun flyspell-check-changed-word-p (start stop)
694 "Return t when the changed word has to be checked.
695 The answer depends of several criteria.
696 Mostly we check word delimiters."
698 ((and (eq (char-after start) ?\n) (> stop start))
700 ((not (numberp flyspell-pre-point))
702 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
704 ((let ((pos (point)))
705 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
710 ;*---------------------------------------------------------------------*/
711 ;* flyspell-check-word-p ... */
712 ;*---------------------------------------------------------------------*/
713 (defun flyspell-check-word-p ()
714 "Return t when the word at `point' has to be checked.
715 The answer depends of several criteria.
716 Mostly we check word delimiters."
718 ((<= (- (point-max) 1) (point-min))
719 ;; the buffer is not filled enough
721 ((and (and (> (current-column) 0)
722 (not (eq (current-column) flyspell-pre-column)))
725 (and (looking-at (flyspell-get-not-casechars))
726 (or flyspell-consider-dash-as-word-delimiter-flag
727 (not (looking-at "\\-"))))))
728 ;; yes because we have reached or typed a word delimiter.
730 ((symbolp this-command)
732 ((get this-command 'flyspell-deplacement)
733 (not (eq flyspell-previous-command this-command)))
734 ((get this-command 'flyspell-delayed)
735 ;; the current command is not delayed, that
736 ;; is that we must check the word now
737 (if (fboundp 'about-xemacs)
738 (sit-for flyspell-delay nil)
739 (sit-for flyspell-delay 0 nil)))
743 ;*---------------------------------------------------------------------*/
744 ;* flyspell-debug-signal-no-check ... */
745 ;*---------------------------------------------------------------------*/
746 (defun flyspell-debug-signal-no-check (msg obj)
747 (setq debug-on-error t)
749 (let ((buffer (get-buffer-create "*flyspell-debug*")))
752 (insert "NO-CHECK:\n")
753 (insert (format " %S : %S\n" msg obj)))))
755 ;*---------------------------------------------------------------------*/
756 ;* flyspell-debug-signal-pre-word-checked ... */
757 ;*---------------------------------------------------------------------*/
758 (defun flyspell-debug-signal-pre-word-checked ()
759 (setq debug-on-error t)
761 (let ((buffer (get-buffer-create "*flyspell-debug*")))
763 (insert "PRE-WORD:\n")
764 (insert (format " pre-point : %S\n" flyspell-pre-point))
765 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
766 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
767 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
768 (goto-char (point-max)))))
770 ;*---------------------------------------------------------------------*/
771 ;* flyspell-debug-signal-word-checked ... */
772 ;*---------------------------------------------------------------------*/
773 (defun flyspell-debug-signal-word-checked ()
774 (setq debug-on-error t)
776 (let ((oldbuf (current-buffer))
777 (buffer (get-buffer-create "*flyspell-debug*"))
781 (insert (format " this-cmd : %S\n" this-command))
782 (insert (format " delayed : %S\n" (and (symbolp this-command)
783 (get this-command 'flyspell-delayed))))
784 (insert (format " point : %S\n" point))
785 (insert (format " prev-char : [%c] %S\n"
788 (let ((c (if (> (point) (point-min))
791 (char-after (point)))
797 (let ((c (if (> (point) (point-min))
800 (and (and (looking-at (flyspell-get-not-casechars)) 1)
801 (and (or flyspell-consider-dash-as-word-delimiter-flag
802 (not (looking-at "\\-"))) 2))))))
805 (insert (format " because : %S\n"
807 ((not (and (symbolp this-command)
808 (get this-command 'flyspell-delayed)))
809 ;; the current command is not delayed, that
810 ;; is that we must check the word now
814 (let ((c (if (> (point) (point-min))
817 (and (looking-at (flyspell-get-not-casechars))
818 (or flyspell-consider-dash-as-word-delimiter-flag
819 (not (looking-at "\\-"))))))))
822 ;; yes because we have reached or typed a word delimiter.
824 ((not (integerp flyspell-delay))
825 ;; yes because the user had set up a no-delay configuration.
829 (goto-char (point-max)))))
831 ;*---------------------------------------------------------------------*/
832 ;* flyspell-debug-signal-changed-checked ... */
833 ;*---------------------------------------------------------------------*/
834 (defun flyspell-debug-signal-changed-checked ()
835 (setq debug-on-error t)
837 (let ((buffer (get-buffer-create "*flyspell-debug*"))
840 (insert "CHANGED WORD:\n")
841 (insert (format " point : %S\n" point))
842 (goto-char (point-max)))))
844 ;*---------------------------------------------------------------------*/
845 ;* flyspell-post-command-hook ... */
846 ;* ------------------------------------------------------------- */
847 ;* It is possible that we check several words: */
848 ;* 1- the current word is checked if the predicate */
849 ;* FLYSPELL-CHECK-WORD-P is true */
850 ;* 2- the word that used to be the current word before the */
851 ;* THIS-COMMAND is checked if: */
852 ;* a- the previous word is different from the current word */
853 ;* b- the previous word as not just been checked by the */
854 ;* previous FLYSPELL-POST-COMMAND-HOOK */
855 ;* 3- the words changed by the THIS-COMMAND that are neither the */
856 ;* previous word nor the current word */
857 ;*---------------------------------------------------------------------*/
858 (defun flyspell-post-command-hook ()
859 "The `post-command-hook' used by flyspell to check a word in-the-fly."
861 (let ((command this-command))
862 (if (flyspell-check-pre-word-p)
864 '(flyspell-debug-signal-pre-word-checked)
865 (set-buffer flyspell-pre-buffer)
867 (goto-char flyspell-pre-point)
869 (if (flyspell-check-word-p)
871 '(flyspell-debug-signal-word-checked)
873 ;; we remember which word we have just checked.
874 ;; this will be used next time we will check a word
875 ;; to compare the next current word with the word
876 ;; that as been registered in the pre-command-hook
877 ;; that is these variables are used within the predicate
878 ;; FLYSPELL-CHECK-PRE-WORD-P
879 (setq flyspell-pre-pre-buffer (current-buffer))
880 (setq flyspell-pre-pre-point (point)))
882 (setq flyspell-pre-pre-buffer nil)
883 (setq flyspell-pre-pre-point nil)
884 ;; when a word is not checked because of a delayed command
885 ;; we do not disable the ispell cache.
886 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
887 (setq flyspell-word-cache-end -1))))
888 (while (consp flyspell-changes)
889 (let ((start (car (car flyspell-changes)))
890 (stop (cdr (car flyspell-changes))))
891 (if (flyspell-check-changed-word-p start stop)
893 '(flyspell-debug-signal-changed-checked)
896 (setq flyspell-changes (cdr flyspell-changes))))
897 (setq flyspell-previous-command command)))
899 ;*---------------------------------------------------------------------*/
900 ;* flyspell-notify-misspell ... */
901 ;*---------------------------------------------------------------------*/
902 (defun flyspell-notify-misspell (start end word poss)
903 (let ((replacements (if (stringp poss)
905 (if flyspell-sort-corrections
906 (sort (car (cdr (cdr poss))) 'string<)
907 (car (cdr (cdr poss)))))))
908 (message (format "mispelling `%s' %S" word replacements))))
910 ;*---------------------------------------------------------------------*/
911 ;* flyspell-word ... */
912 ;*---------------------------------------------------------------------*/
913 (defun flyspell-word (&optional following)
914 "Spell check a word."
915 (interactive (list current-prefix-arg))
917 (setq following ispell-following-word))
919 ;; use the correct dictionary
920 (flyspell-accept-buffer-local-defs)
921 (let* ((cursor-location (point))
922 (flyspell-word (flyspell-get-word following))
924 (if (or (eq flyspell-word nil)
925 (and (fboundp flyspell-generic-check-word-p)
926 (not (funcall flyspell-generic-check-word-p))))
929 ;; destructure return flyspell-word info list.
930 (setq start (car (cdr flyspell-word))
931 end (car (cdr (cdr flyspell-word)))
932 word (car flyspell-word))
933 ;; before checking in the directory, we check for doublons.
935 ((and (or (not (eq ispell-parser 'tex))
936 (not (eq (char-after start) ?\\)))
937 flyspell-mark-duplications-flag
940 (word-search-backward word
944 ;; yes, this is a doublon
945 (flyspell-highlight-incorrect-region start end 'doublon))
946 ((and (eq flyspell-word-cache-start start)
947 (eq flyspell-word-cache-end end)
948 (string-equal flyspell-word-cache-word word))
949 ;; this word had been already checked, we skip
951 ((and (eq ispell-parser 'tex)
952 (flyspell-tex-command-p flyspell-word))
953 ;; this is a correct word (because a tex command)
954 (flyspell-unhighlight-at start)
956 (flyspell-unhighlight-at (- end 1)))
959 ;; we setup the cache
960 (setq flyspell-word-cache-start start)
961 (setq flyspell-word-cache-end end)
962 (setq flyspell-word-cache-word word)
963 ;; now check spelling of word.
964 (process-send-string ispell-process "%\n")
965 ;; put in verbose mode
966 (process-send-string ispell-process
967 (concat "^" word "\n"))
968 ;; we mark the ispell process so it can be killed
969 ;; when emacs is exited without query
970 (if (fboundp 'process-kill-without-query)
971 (process-kill-without-query ispell-process))
972 ;; wait until ispell has processed word
974 (accept-process-output ispell-process)
975 (not (string= "" (car ispell-filter)))))
976 ;; (process-send-string ispell-process "!\n")
977 ;; back to terse mode.
978 (setq ispell-filter (cdr ispell-filter))
979 (if (consp ispell-filter)
980 (setq poss (ispell-parse-output (car ispell-filter))))
983 (flyspell-unhighlight-at start)
985 (flyspell-unhighlight-at (- end 1)))
987 ((and (stringp poss) flyspell-highlight-flag)
989 (flyspell-unhighlight-at start)
991 (flyspell-unhighlight-at (- end 1)))
994 (flyspell-unhighlight-at start)
996 (flyspell-unhighlight-at (- end 1))))
997 ((or (and (< flyspell-duplicate-distance 0)
1000 (word-search-backward word
1005 (word-search-forward word
1008 (and (> flyspell-duplicate-distance 0)
1011 (word-search-backward
1014 flyspell-duplicate-distance)
1018 (word-search-forward
1021 flyspell-duplicate-distance)
1023 (if flyspell-highlight-flag
1024 (flyspell-highlight-duplicate-region start end)
1025 (message (format "duplicate `%s'" word))))
1027 ;; incorrect highlight the location
1028 (if flyspell-highlight-flag
1029 (flyspell-highlight-incorrect-region start end poss)
1030 (flyspell-notify-misspell start end word poss))))
1031 ;; return to original location
1032 (goto-char cursor-location)
1033 (if ispell-quit (setq ispell-quit nil)))))))))
1035 ;*---------------------------------------------------------------------*/
1036 ;* flyspell-tex-math-initialized ... */
1037 ;*---------------------------------------------------------------------*/
1038 (defvar flyspell-tex-math-initialized nil)
1040 ;*---------------------------------------------------------------------*/
1041 ;* flyspell-math-tex-command-p ... */
1042 ;* ------------------------------------------------------------- */
1043 ;* This function uses the texmathp package to check if (point) */
1044 ;* is within a tex command. In order to avoid using */
1045 ;* condition-case each time we use the variable */
1046 ;* flyspell-tex-math-initialized to make a special case the first */
1047 ;* time that function is called. */
1048 ;*---------------------------------------------------------------------*/
1049 (defun flyspell-math-tex-command-p ()
1051 (flyspell-check-tex-math-command
1053 ((eq flyspell-tex-math-initialized t)
1055 ((eq flyspell-tex-math-initialized 'error)
1058 (setq flyspell-tex-math-initialized t)
1062 (setq flyspell-tex-math-initialized 'error)
1065 ;*---------------------------------------------------------------------*/
1066 ;* flyspell-tex-command-p ... */
1067 ;*---------------------------------------------------------------------*/
1068 (defun flyspell-tex-command-p (word)
1069 "Return t if WORD is a TeX command."
1071 (let ((b (car (cdr word))))
1072 (and (re-search-backward "\\\\" (- (point) 100) t)
1073 (or (= (match-end 0) b)
1074 (and (goto-char (match-end 0))
1075 (looking-at flyspell-tex-command-regexp)
1076 (>= (match-end 0) b))))))
1077 (flyspell-math-tex-command-p)))
1079 ;*---------------------------------------------------------------------*/
1080 ;* flyspell-casechars-cache ... */
1081 ;*---------------------------------------------------------------------*/
1082 (defvar flyspell-casechars-cache nil)
1083 (defvar flyspell-ispell-casechars-cache nil)
1084 (make-variable-buffer-local 'flyspell-casechars-cache)
1085 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1087 ;*---------------------------------------------------------------------*/
1088 ;* flyspell-get-casechars ... */
1089 ;*---------------------------------------------------------------------*/
1090 (defun flyspell-get-casechars ()
1091 "This function builds a string that is the regexp of word chars.
1092 In order to avoid one useless string construction,
1093 this function changes the last char of the `ispell-casechars' string."
1094 (let ((ispell-casechars (ispell-get-casechars)))
1096 ((eq ispell-parser 'tex)
1097 (setq flyspell-ispell-casechars-cache ispell-casechars)
1098 (setq flyspell-casechars-cache
1099 (concat (substring ispell-casechars
1101 (- (length ispell-casechars) 1))
1103 flyspell-casechars-cache)
1105 (setq flyspell-ispell-casechars-cache ispell-casechars)
1106 (setq flyspell-casechars-cache ispell-casechars)
1107 flyspell-casechars-cache))))
1109 ;*---------------------------------------------------------------------*/
1110 ;* flyspell-get-not-casechars-cache ... */
1111 ;*---------------------------------------------------------------------*/
1112 (defvar flyspell-not-casechars-cache nil)
1113 (defvar flyspell-ispell-not-casechars-cache nil)
1114 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1115 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1117 ;*---------------------------------------------------------------------*/
1118 ;* flyspell-get-not-casechars ... */
1119 ;*---------------------------------------------------------------------*/
1120 (defun flyspell-get-not-casechars ()
1121 "This function builds a string that is the regexp of non-word chars."
1122 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1124 ((eq ispell-parser 'tex)
1125 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1126 (setq flyspell-not-casechars-cache
1127 (concat (substring ispell-not-casechars
1129 (- (length ispell-not-casechars) 1))
1131 flyspell-not-casechars-cache)
1133 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1134 (setq flyspell-not-casechars-cache ispell-not-casechars)
1135 flyspell-not-casechars-cache))))
1137 ;*---------------------------------------------------------------------*/
1138 ;* flyspell-get-word ... */
1139 ;*---------------------------------------------------------------------*/
1140 (defun flyspell-get-word (following)
1141 "Return the word for spell-checking according to Ispell syntax.
1142 If argument FOLLOWING is non-nil or if `ispell-following-word'
1143 is non-nil when called interactively, then the following word
1144 \(rather than preceding\) is checked when the cursor is not over a word.
1145 Optional second argument contains other chars that can be included in word
1148 Word syntax described by `ispell-dictionary-alist' (which see)."
1149 (let* ((flyspell-casechars (flyspell-get-casechars))
1150 (flyspell-not-casechars (flyspell-get-not-casechars))
1151 (ispell-otherchars (ispell-get-otherchars))
1152 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1153 (word-regexp (if (string< "" ispell-otherchars)
1154 (concat flyspell-casechars
1160 (if ispell-many-otherchars-p
1162 (concat flyspell-casechars "+")))
1166 (if (not (looking-at flyspell-casechars))
1168 (re-search-forward flyspell-casechars (point-max) t)
1169 (re-search-backward flyspell-casechars (point-min) t)))
1170 ;; move to front of word
1171 (re-search-backward flyspell-not-casechars (point-min) 'start)
1173 (if (string< "" ispell-otherchars)
1174 (while (and (looking-at ispell-otherchars)
1176 (or (not did-it-once)
1177 ispell-many-otherchars-p)
1178 (not (eq pos (point))))
1180 (setq did-it-once t)
1182 (if (looking-at flyspell-casechars)
1183 (re-search-backward flyspell-not-casechars (point-min) 'move)
1184 (backward-char -1)))))
1185 ;; Now mark the word and save to string.
1186 (if (eq (re-search-forward word-regexp (point-max) t) nil)
1189 (setq start (match-beginning 0)
1191 word (buffer-substring-no-properties start end))
1192 (list word start end)))))
1194 ;*---------------------------------------------------------------------*/
1195 ;* flyspell-small-region ... */
1196 ;*---------------------------------------------------------------------*/
1197 (defun flyspell-small-region (beg end)
1198 "Flyspell text between BEG and END."
1206 (while (< (point) end)
1209 (message "Spell Checking...%d%%"
1210 (* 100 (/ (float (- (point) beg)) (- end beg))))
1212 (setq count (+ 1 count)))
1215 (let ((cur (point)))
1217 (if (and (< (point) end) (> (point) (+ cur 1)))
1218 (backward-char 1)))))
1220 (message "Spell Checking completed.")
1223 ;*---------------------------------------------------------------------*/
1224 ;* flyspell-external-ispell-process ... */
1225 ;*---------------------------------------------------------------------*/
1226 (defvar flyspell-external-ispell-process '()
1227 "The external Flyspell Ispell process.")
1229 ;*---------------------------------------------------------------------*/
1230 ;* flyspell-external-ispell-buffer ... */
1231 ;*---------------------------------------------------------------------*/
1232 (defvar flyspell-external-ispell-buffer '())
1233 (defvar flyspell-large-region-buffer '())
1234 (defvar flyspell-large-region-beg (point-min))
1235 (defvar flyspell-large-region-end (point-max))
1237 ;*---------------------------------------------------------------------*/
1238 ;* flyspell-external-point-words ... */
1239 ;*---------------------------------------------------------------------*/
1240 (defun flyspell-external-point-words ()
1241 (let ((buffer flyspell-external-ispell-buffer))
1243 (beginning-of-buffer)
1244 (let ((size (- flyspell-large-region-end flyspell-large-region-beg))
1245 (start flyspell-large-region-beg))
1246 ;; now we are done with ispell, we have to find the word in
1247 ;; the initial buffer
1248 (while (< (point) (- (point-max) 1))
1249 ;; we have to fetch the incorrect word
1250 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1251 (let ((word (match-string 1)))
1252 (goto-char (match-end 0))
1253 (set-buffer flyspell-large-region-buffer)
1254 (goto-char flyspell-large-region-beg)
1255 (message "Spell Checking...%d%% [%s]"
1256 (* 100 (/ (float (- (point) start)) size))
1258 (if (search-forward word flyspell-large-region-end t)
1260 (setq flyspell-large-region-beg (point))
1261 (goto-char (- (point) 1))
1263 (set-buffer buffer))
1264 (goto-char (point-max)))))
1266 (message "Spell Checking completed.")
1267 ;; ok, we are done with pointing out incorrect words, we just
1268 ;; have to kill the temporary buffer
1269 (kill-buffer flyspell-external-ispell-buffer)
1270 (setq flyspell-external-ispell-buffer nil)))
1272 ;*---------------------------------------------------------------------*/
1273 ;* flyspell-large-region ... */
1274 ;*---------------------------------------------------------------------*/
1275 (defun flyspell-large-region (beg end)
1276 (let* ((curbuf (current-buffer))
1277 (buffer (get-buffer-create "*flyspell-region*")))
1278 (setq flyspell-external-ispell-buffer buffer)
1279 (setq flyspell-large-region-buffer curbuf)
1280 (setq flyspell-large-region-beg beg)
1281 (setq flyspell-large-region-end end)
1284 ;; this is done, we can start checking...
1285 (message "Checking region...")
1287 (let ((c (apply 'call-process-region beg
1295 ;; Local dictionary becomes the global dictionary in use.
1296 (if ispell-local-dictionary
1297 (setq ispell-dictionary ispell-local-dictionary))
1298 (setq args (ispell-get-ispell-args))
1299 (if ispell-dictionary ; use specified dictionary
1301 (append (list "-d" ispell-dictionary) args)))
1302 (if ispell-personal-dictionary ; use specified pers dict
1307 ispell-personal-dictionary)))))
1308 (setq args (append args ispell-extra-args))
1311 (flyspell-external-point-words)
1312 (error "Can't check region...")))))
1314 ;*---------------------------------------------------------------------*/
1315 ;* flyspell-region ... */
1316 ;* ------------------------------------------------------------- */
1317 ;* Because `ispell -a' is too slow, it is not possible to use */
1318 ;* it on large region. Then, when ispell is invoked on a large */
1319 ;* text region, a new `ispell -l' process is spawned. The */
1320 ;* pointed out words are then searched in the region a checked with */
1321 ;* regular flyspell means. */
1322 ;*---------------------------------------------------------------------*/
1324 (defun flyspell-region (beg end)
1325 "Flyspell text between BEG and END."
1334 (if (> (- end beg) flyspell-large-region)
1335 (flyspell-large-region beg end)
1336 (flyspell-small-region beg end)))))
1338 ;*---------------------------------------------------------------------*/
1339 ;* flyspell-buffer ... */
1340 ;*---------------------------------------------------------------------*/
1342 (defun flyspell-buffer ()
1343 "Flyspell whole buffer."
1345 (flyspell-region (point-min) (point-max)))
1347 ;*---------------------------------------------------------------------*/
1348 ;* old next error position ... */
1349 ;*---------------------------------------------------------------------*/
1350 (defvar flyspell-old-buffer-error nil)
1351 (defvar flyspell-old-pos-error nil)
1353 ;*---------------------------------------------------------------------*/
1354 ;* flyspell-goto-next-error ... */
1355 ;*---------------------------------------------------------------------*/
1356 (defun flyspell-goto-next-error ()
1357 "Go to the next previously detected error.
1358 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1363 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1364 (eq pos flyspell-old-pos-error))
1366 (if (= flyspell-old-pos-error max)
1367 ;; goto beginning of buffer
1369 (message "Restarting from beginning of buffer")
1370 (goto-char (point-min)))
1372 (setq pos (point))))
1373 ;; seek the next error
1374 (while (and (< pos max)
1375 (let ((ovs (overlays-at pos))
1377 (while (and (not r) (consp ovs))
1378 (if (flyspell-overlay-p (car ovs))
1380 (setq ovs (cdr ovs))))
1382 (setq pos (1+ pos)))
1383 ;; save the current location for next invocation
1384 (setq flyspell-old-pos-error pos)
1385 (setq flyspell-old-buffer-error (current-buffer))
1388 (message "No more miss-spelled word!"))))
1390 ;*---------------------------------------------------------------------*/
1391 ;* flyspell-overlay-p ... */
1392 ;*---------------------------------------------------------------------*/
1393 (defun flyspell-overlay-p (o)
1394 "A predicate that return true iff O is an overlay used by flyspell."
1395 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1397 ;*---------------------------------------------------------------------*/
1398 ;* flyspell-delete-all-overlays ... */
1399 ;* ------------------------------------------------------------- */
1400 ;* Remove all the overlays introduced by flyspell. */
1401 ;*---------------------------------------------------------------------*/
1402 (defun flyspell-delete-all-overlays ()
1403 "Delete all the overlays used by flyspell."
1404 (let ((l (overlays-in (point-min) (point-max))))
1407 (if (flyspell-overlay-p (car l))
1408 (delete-overlay (car l)))
1409 (setq l (cdr l))))))
1411 ;*---------------------------------------------------------------------*/
1412 ;* flyspell-unhighlight-at ... */
1413 ;*---------------------------------------------------------------------*/
1414 (defun flyspell-unhighlight-at (pos)
1415 "Remove the flyspell overlay that are located at POS."
1416 (if flyspell-persistent-highlight
1417 (let ((overlays (overlays-at pos)))
1418 (while (consp overlays)
1419 (if (flyspell-overlay-p (car overlays))
1420 (delete-overlay (car overlays)))
1421 (setq overlays (cdr overlays))))
1422 (if (flyspell-overlay-p flyspell-overlay)
1423 (delete-overlay flyspell-overlay))))
1425 ;*---------------------------------------------------------------------*/
1426 ;* flyspell-properties-at-p ... */
1427 ;* ------------------------------------------------------------- */
1428 ;* Is there an highlight properties at position pos? */
1429 ;*---------------------------------------------------------------------*/
1430 (defun flyspell-properties-at-p (pos)
1431 "Return t if there is a text property at POS, not counting `local-map'.
1432 If variable `flyspell-highlight-properties' is set to nil,
1433 text with properties are not checked. This function is used to discover
1434 if the character at POS has any other property."
1435 (let ((prop (text-properties-at pos))
1437 (while (and keep (consp prop))
1438 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1439 (setq prop (cdr (cdr prop)))
1443 ;*---------------------------------------------------------------------*/
1444 ;* make-flyspell-overlay ... */
1445 ;*---------------------------------------------------------------------*/
1446 (defun make-flyspell-overlay (beg end face mouse-face)
1447 "Allocate an overlay to highlight an incorrect word.
1448 BEG and END specify the range in the buffer of that word.
1449 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1451 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1452 (overlay-put flyspell-overlay 'face face)
1453 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1454 (overlay-put flyspell-overlay 'flyspell-overlay t)
1455 (if flyspell-use-local-map
1456 (overlay-put flyspell-overlay
1457 flyspell-overlay-keymap-property-name
1458 flyspell-mouse-map))
1461 ;*---------------------------------------------------------------------*/
1462 ;* flyspell-highlight-incorrect-region ... */
1463 ;*---------------------------------------------------------------------*/
1464 (defun flyspell-highlight-incorrect-region (beg end poss)
1465 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1466 (unless (run-hook-with-args-until-success
1467 'flyspell-incorrect-hook beg end poss)
1468 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1470 ;; we cleanup current overlay at the same position
1471 (if (and (not flyspell-persistent-highlight)
1472 (overlayp flyspell-overlay))
1473 (delete-overlay flyspell-overlay)
1474 (let ((overlays (overlays-at beg)))
1475 (while (consp overlays)
1476 (if (flyspell-overlay-p (car overlays))
1477 (delete-overlay (car overlays)))
1478 (setq overlays (cdr overlays)))))
1479 ;; now we can use a new overlay
1480 (setq flyspell-overlay
1481 (make-flyspell-overlay beg end
1482 'flyspell-incorrect-face 'highlight))))))
1484 ;*---------------------------------------------------------------------*/
1485 ;* flyspell-highlight-duplicate-region ... */
1486 ;*---------------------------------------------------------------------*/
1487 (defun flyspell-highlight-duplicate-region (beg end)
1488 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
1489 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1491 ;; we cleanup current overlay at the same position
1492 (if (and (not flyspell-persistent-highlight)
1493 (overlayp flyspell-overlay))
1494 (delete-overlay flyspell-overlay)
1495 (let ((overlays (overlays-at beg)))
1496 (while (consp overlays)
1497 (if (flyspell-overlay-p (car overlays))
1498 (delete-overlay (car overlays)))
1499 (setq overlays (cdr overlays)))))
1500 ;; now we can use a new overlay
1501 (setq flyspell-overlay
1502 (make-flyspell-overlay beg end
1503 'flyspell-duplicate-face 'highlight)))))
1505 ;*---------------------------------------------------------------------*/
1506 ;* flyspell-auto-correct-cache ... */
1507 ;*---------------------------------------------------------------------*/
1508 (defvar flyspell-auto-correct-pos nil)
1509 (defvar flyspell-auto-correct-region nil)
1510 (defvar flyspell-auto-correct-ring nil)
1511 (defvar flyspell-auto-correct-word nil)
1512 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1513 (make-variable-buffer-local 'flyspell-auto-correct-region)
1514 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1515 (make-variable-buffer-local 'flyspell-auto-correct-word)
1517 ;*---------------------------------------------------------------------*/
1518 ;* flyspell-check-previous-highlighted-word ... */
1519 ;*---------------------------------------------------------------------*/
1520 (defun flyspell-check-previous-highlighted-word (&optional arg)
1521 "Correct the closer misspelled word.
1522 This function scans a mis-spelled word before the cursor. If it finds one
1523 it proposes replacement for that word. With prefix arg, count that many
1524 misspelled words backwards."
1526 (let ((pos1 (point))
1528 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1531 (while (and (setq pos (previous-overlay-change pos))
1534 (if (> pos (point-min))
1536 (setq ovs (overlays-at (1- pos)))
1539 (setq ovs (cdr ovs))
1540 (if (and (overlay-get ov 'flyspell-overlay)
1541 (= 0 (setq arg (1- arg))))
1542 (throw 'exit t)))))))
1546 (error "No word to correct before point"))))
1548 ;*---------------------------------------------------------------------*/
1549 ;* flyspell-display-next-corrections ... */
1550 ;*---------------------------------------------------------------------*/
1551 (defun flyspell-display-next-corrections (corrections)
1552 (let ((string "Corrections:")
1555 (while (< (length string) 80)
1556 (if (equal (car l) flyspell-auto-correct-word)
1557 (setq pos (cons (+ 1 (length string)) pos)))
1558 (setq string (concat string " " (car l)))
1561 (let ((num (car pos)))
1562 (put-text-property num
1563 (+ num (length flyspell-auto-correct-word))
1565 'flyspell-incorrect-face
1567 (setq pos (cdr pos)))
1568 (if (fboundp 'display-message)
1569 (display-message 'no-log string)
1572 ;*---------------------------------------------------------------------*/
1573 ;* flyspell-abbrev-table ... */
1574 ;*---------------------------------------------------------------------*/
1575 (defun flyspell-abbrev-table ()
1576 (if flyspell-use-global-abbrev-table-p
1578 local-abbrev-table))
1580 ;*---------------------------------------------------------------------*/
1581 ;* flyspell-auto-correct-word ... */
1582 ;*---------------------------------------------------------------------*/
1583 (defun flyspell-auto-correct-word ()
1584 "Correct the current word.
1585 This command proposes various successive corrections for the current word."
1588 (old-max (point-max)))
1589 ;; use the correct dictionary
1590 (flyspell-accept-buffer-local-defs)
1591 (if (and (eq flyspell-auto-correct-pos pos)
1592 (consp flyspell-auto-correct-region))
1593 ;; we have already been using the function at the same location
1594 (let* ((start (car flyspell-auto-correct-region))
1595 (len (cdr flyspell-auto-correct-region)))
1596 (delete-region start (+ start len))
1597 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1598 (let* ((word (car flyspell-auto-correct-ring))
1599 (len (length word)))
1600 (rplacd flyspell-auto-correct-region len)
1602 (if flyspell-abbrev-p
1603 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1604 flyspell-auto-correct-word)
1605 (flyspell-change-abbrev (flyspell-abbrev-table)
1606 flyspell-auto-correct-word
1608 (define-abbrev (flyspell-abbrev-table)
1609 flyspell-auto-correct-word word)))
1612 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1613 (flyspell-ajust-cursor-point pos (point) old-max)
1614 (setq flyspell-auto-correct-pos (point)))
1615 ;; fetch the word to be checked
1616 (let ((word (flyspell-get-word nil))
1618 ;; destructure return word info list.
1619 (setq start (car (cdr word))
1620 end (car (cdr (cdr word)))
1622 (setq flyspell-auto-correct-word word)
1623 ;; now check spelling of word.
1624 (process-send-string ispell-process "%\n") ;put in verbose mode
1625 (process-send-string ispell-process (concat "^" word "\n"))
1626 ;; wait until ispell has processed word
1628 (accept-process-output ispell-process)
1629 (not (string= "" (car ispell-filter)))))
1630 (setq ispell-filter (cdr ispell-filter))
1631 (if (consp ispell-filter)
1632 (setq poss (ispell-parse-output (car ispell-filter))))
1633 (cond ((or (eq poss t) (stringp poss))
1634 ;; don't correct word
1638 (error "Ispell: error in Ispell process"))
1640 ;; the word is incorrect, we have to propose a replacement
1641 (let ((replacements (if flyspell-sort-corrections
1642 (sort (car (cdr (cdr poss))) 'string<)
1643 (car (cdr (cdr poss))))))
1644 (setq flyspell-auto-correct-region nil)
1645 (if (consp replacements)
1647 (let ((replace (car replacements)))
1648 (let ((new-word replace))
1649 (if (not (equal new-word (car poss)))
1651 ;; the save the current replacements
1652 (setq flyspell-auto-correct-region
1653 (cons start (length new-word)))
1654 (let ((l replacements))
1655 (while (consp (cdr l))
1657 (rplacd l (cons (car poss) replacements)))
1658 (setq flyspell-auto-correct-ring
1660 (delete-region start end)
1662 (if flyspell-abbrev-p
1663 (if (flyspell-already-abbrevp
1664 (flyspell-abbrev-table) word)
1665 (flyspell-change-abbrev
1666 (flyspell-abbrev-table)
1669 (define-abbrev (flyspell-abbrev-table)
1672 (flyspell-display-next-corrections
1673 (cons new-word flyspell-auto-correct-ring))
1674 (flyspell-ajust-cursor-point pos
1677 (setq flyspell-auto-correct-pos (point))
1678 (ispell-pdict-save t)))))
1680 ;*---------------------------------------------------------------------*/
1681 ;* flyspell-correct-word ... */
1682 ;*---------------------------------------------------------------------*/
1683 (defun flyspell-correct-word (event)
1684 "Pop up a menu of possible corrections for a misspelled word.
1685 The word checked is the word at the mouse position."
1687 ;; use the correct dictionary
1688 (flyspell-accept-buffer-local-defs)
1689 ;; retain cursor location (I don't know why but save-excursion here fails).
1690 (let ((save (point)))
1691 (mouse-set-point event)
1692 (let ((cursor-location (point))
1693 (word (flyspell-get-word nil))
1694 start end poss replace)
1695 ;; destructure return word info list.
1696 (setq start (car (cdr word))
1697 end (car (cdr (cdr word)))
1699 ;; now check spelling of word.
1700 (process-send-string ispell-process "%\n") ;put in verbose mode
1701 (process-send-string ispell-process (concat "^" word "\n"))
1702 ;; wait until ispell has processed word
1704 (accept-process-output ispell-process)
1705 (not (string= "" (car ispell-filter)))))
1706 (setq ispell-filter (cdr ispell-filter))
1707 (if (consp ispell-filter)
1708 (setq poss (ispell-parse-output (car ispell-filter))))
1709 (cond ((or (eq poss t) (stringp poss))
1710 ;; don't correct word
1714 (error "Ispell: error in Ispell process"))
1715 ((string-match "GNU" (emacs-version))
1716 ;; the word is incorrect, we have to propose a replacement
1717 (setq replace (flyspell-emacs-popup event poss word))
1718 (cond ((eq replace 'ignore)
1723 (process-send-string ispell-process (concat "*" word "\n"))
1724 (flyspell-unhighlight-at cursor-location)
1725 (setq ispell-pdict-modified-p '(t)))
1726 ((or (eq replace 'buffer) (eq replace 'session))
1727 (process-send-string ispell-process (concat "@" word "\n"))
1728 (if (null ispell-pdict-modified-p)
1729 (setq ispell-pdict-modified-p
1730 (list ispell-pdict-modified-p)))
1731 (flyspell-unhighlight-at cursor-location)
1733 (if (eq replace 'buffer)
1734 (ispell-add-per-file-word-list word)))
1736 (let ((new-word (if (atom replace)
1739 (cursor-location (+ (- (length word) (- end start))
1741 (if (not (equal new-word (car poss)))
1742 (let ((old-max (point-max)))
1743 (delete-region start end)
1745 (if flyspell-abbrev-p
1746 (define-abbrev (flyspell-abbrev-table)
1749 (flyspell-ajust-cursor-point save
1755 ((eq flyspell-emacs 'xemacs)
1756 (flyspell-xemacs-popup
1757 event poss word cursor-location start end save)
1759 (ispell-pdict-save t))))
1761 ;*---------------------------------------------------------------------*/
1762 ;* flyspell-xemacs-correct ... */
1763 ;*---------------------------------------------------------------------*/
1764 (defun flyspell-xemacs-correct (replace poss word cursor-location start end save)
1765 "The xemacs popup menu callback."
1766 (cond ((eq replace 'ignore)
1769 (process-send-string ispell-process (concat "*" word "\n"))
1770 (process-send-string ispell-process "#\n")
1771 (flyspell-unhighlight-at cursor-location)
1772 (setq ispell-pdict-modified-p '(t)))
1773 ((or (eq replace 'buffer) (eq replace 'session))
1774 (process-send-string ispell-process (concat "@" word "\n"))
1775 (flyspell-unhighlight-at cursor-location)
1776 (if (null ispell-pdict-modified-p)
1777 (setq ispell-pdict-modified-p
1778 (list ispell-pdict-modified-p)))
1779 (if (eq replace 'buffer)
1780 (ispell-add-per-file-word-list word)))
1782 (let ((old-max (point-max))
1783 (new-word (if (atom replace)
1786 (cursor-location (+ (- (length word) (- end start))
1788 (if (not (equal new-word (car poss)))
1790 (delete-region start end)
1793 (if flyspell-abbrev-p
1794 (define-abbrev (flyspell-abbrev-table)
1797 (flyspell-ajust-cursor-point save cursor-location old-max)))))
1799 ;*---------------------------------------------------------------------*/
1800 ;* flyspell-ajust-cursor-point ... */
1801 ;*---------------------------------------------------------------------*/
1802 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
1803 (if (>= save cursor-location)
1804 (let ((new-pos (+ save (- (point-max) old-max))))
1806 ((< new-pos (point-min))
1808 ((> new-pos (point-max))
1813 ;*---------------------------------------------------------------------*/
1814 ;* flyspell-emacs-popup ... */
1815 ;*---------------------------------------------------------------------*/
1816 (defun flyspell-emacs-popup (event poss word)
1817 "The Emacs popup menu."
1819 (let* ((mouse-pos (mouse-position))
1820 (mouse-pos (if (nth 1 mouse-pos)
1822 (set-mouse-position (car mouse-pos)
1823 (/ (frame-width) 2) 2)
1826 (setq event (list (list (car (cdr mouse-pos))
1827 (1+ (cdr (cdr mouse-pos))))
1829 (let* ((corrects (if flyspell-sort-corrections
1830 (sort (car (cdr (cdr poss))) 'string<)
1831 (car (cdr (cdr poss)))))
1832 (cor-menu (if (consp corrects)
1833 (mapcar (lambda (correct)
1834 (list correct correct))
1837 (affix (car (cdr (cdr (cdr poss)))))
1838 (base-menu (let ((save (if (consp affix)
1840 (list (concat "Save affix: " (car affix))
1842 '("Accept (session)" accept)
1843 '("Accept (buffer)" buffer))
1844 '(("Save word" save)
1845 ("Accept (session)" session)
1846 ("Accept (buffer)" buffer)))))
1847 (if (consp cor-menu)
1848 (append cor-menu (cons "" save))
1850 (menu (cons "flyspell correction menu" base-menu)))
1851 (car (x-popup-menu event
1852 (list (format "%s [%s]" word (or ispell-local-dictionary
1856 ;*---------------------------------------------------------------------*/
1857 ;* flyspell-xemacs-popup ... */
1858 ;*---------------------------------------------------------------------*/
1859 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
1860 "The XEmacs popup menu."
1861 (let* ((corrects (if flyspell-sort-corrections
1862 (sort (car (cdr (cdr poss))) 'string<)
1863 (car (cdr (cdr poss)))))
1864 (cor-menu (if (consp corrects)
1865 (mapcar (lambda (correct)
1867 (list 'flyspell-xemacs-correct
1878 (affix (car (cdr (cdr (cdr poss)))))
1879 (menu (let ((save (if (consp affix)
1881 (concat "Save affix: " (car affix))
1882 (list 'flyspell-xemacs-correct
1893 (list 'flyspell-xemacs-correct
1902 (session (vector "Accept (session)"
1903 (list 'flyspell-xemacs-correct
1912 (buffer (vector "Accept (buffer)"
1913 (list 'flyspell-xemacs-correct
1922 (if (consp cor-menu)
1923 (append cor-menu (list "-" save session buffer))
1924 (list save session buffer)))))
1925 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
1929 ;*---------------------------------------------------------------------*/
1930 ;* Some example functions for real autocorrecting */
1931 ;*---------------------------------------------------------------------*/
1933 (defun flyspell-maybe-correct-transposition (beg end poss)
1934 "Check replacements for transposed characters.
1936 If the text between BEG and END is equal to a correction suggested by
1937 Ispell, after transposing two adjacent characters, correct the text,
1940 The third arg POSS is either the symbol 'doublon' or a list of
1941 possible corrections as returned by 'ispell-parse-output'.
1943 This function is meant to be added to 'flyspell-incorrect-hook'."
1945 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
1948 (copy-to-buffer temp-buffer beg end)
1949 (set-buffer temp-buffer)
1950 (goto-char (1+ (point-min)))
1951 (while (and (not (eobp)) (not found))
1953 (if (member (buffer-string) (nth 2 poss))
1954 (setq found (point))
1955 (transpose-chars -1)
1959 (goto-char (+ beg found -1))
1960 (transpose-chars -1)
1963 (defun flyspell-maybe-correct-doubling (beg end poss)
1964 "Check replacements for doubled characters.
1966 If the text between BEG and END is equal to a correction suggested by
1967 Ispell, after removing a pair of doubled characters, correct the text,
1970 The third arg POSS is either the symbol 'doublon' or a list of
1971 possible corrections as returned by 'ispell-parse-output'.
1973 This function is meant to be added to 'flyspell-incorrect-hook'."
1975 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
1978 (copy-to-buffer temp-buffer beg end)
1979 (set-buffer temp-buffer)
1980 (goto-char (1+ (point-min)))
1981 (while (and (not (eobp)) (not found))
1982 (when (char-equal (char-after) (char-before))
1984 (if (member (buffer-string) (nth 2 poss))
1985 (setq found (point))
1986 (insert-char (char-before) 1)))
1990 (goto-char (+ beg found -1))
1994 ;*---------------------------------------------------------------------*/
1995 ;* flyspell-already-abbrevp ... */
1996 ;*---------------------------------------------------------------------*/
1997 (defun flyspell-already-abbrevp (table word)
1998 (let ((sym (abbrev-symbol word table)))
1999 (and sym (symbolp sym))))
2001 ;*---------------------------------------------------------------------*/
2002 ;* flyspell-change-abbrev ... */
2003 ;*---------------------------------------------------------------------*/
2004 (defun flyspell-change-abbrev (table old new)
2005 (set (abbrev-symbol old table) new))
2009 ;;; flyspell.el ends here