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 "american"
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 variables `ispell-local-dictionary' or `ispell-dictionary' are non nil
163 when flyspell is started, the value of that variables is used instead
164 of `flyspell-default-dictionary' to select the default dictionary."
169 (defcustom flyspell-tex-command-regexp
170 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
171 "A string that is the regular expression that matches TeX commands."
176 (defcustom flyspell-check-tex-math-command nil
177 "*Non nil means check even inside TeX math environment.
178 TeX math environments are discovered by the TEXMATHP that implemented
179 inside the texmathp.el Emacs package. That package may be found at:
180 http://strw.leidenuniv.nl/~dominik/Tools"
184 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
185 '("francais" "deutsch8" "norsk")
186 "List of dictionary names that consider `-' as word delimiter."
189 :type '(repeat (string)))
191 (defcustom flyspell-abbrev-p
193 "*If true, add correction to abbreviation table."
198 (defcustom flyspell-use-global-abbrev-table-p
200 "*If true, prefer global abbrev table to local abbrev table."
206 (defcustom flyspell-mode-line-string " Fly"
207 "*String displayed on the modeline when flyspell is active.
208 Set this to nil if you don't want a modeline indicator."
212 (defcustom flyspell-large-region 1000
213 "*The threshold that determines if a region is small.
214 The `flyspell-region' function is invoked if the region is small, the
215 word are checked one after the other using regular flyspell check
216 means. If the region is large, a new Ispell process is spawned to get
222 ;*---------------------------------------------------------------------*/
223 ;* Mode specific options */
224 ;* ------------------------------------------------------------- */
225 ;* Mode specific options enable users to disable flyspell on */
226 ;* certain word depending of the emacs mode. For instance, when */
227 ;* using flyspell with mail-mode add the following expression */
228 ;* in your .emacs file: */
229 ;* (add-hook 'mail-mode */
230 ;* '(lambda () (setq flyspell-generic-check-word-p */
231 ;* 'mail-mode-flyspell-verify))) */
232 ;*---------------------------------------------------------------------*/
233 (defvar flyspell-generic-check-word-p nil
234 "Function providing per-mode customization over which words are flyspelled.
235 Returns t to continue checking, nil otherwise.
236 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
237 property of the major mode name.")
238 (make-variable-buffer-local 'flyspell-generic-check-word-p)
240 ;*--- mail mode -------------------------------------------------------*/
241 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
242 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
243 (defun mail-mode-flyspell-verify ()
244 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
245 (let ((in-headers (save-excursion
246 (re-search-forward mail-header-separator nil t)))
247 (in-signature (save-excursion
248 (re-search-backward message-signature-separator nil t))))
250 (and (save-excursion (beginning-of-line)
251 (looking-at "^Subject:"))
252 (> (point) (match-end 0))))
258 (not (looking-at "[>}|]\\To:")))))))
260 ;*--- texinfo mode ----------------------------------------------------*/
261 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
262 (defun texinfo-mode-flyspell-verify ()
263 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
266 (not (looking-at "@"))))
268 ;*--- tex mode --------------------------------------------------------*/
269 (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
270 (defun tex-mode-flyspell-verify ()
271 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
274 (re-search-backward "^[ \t]*%%%[ \t]+Local" (point-min) t)))
276 (let ((this (point-marker))
277 (e (progn (end-of-line) (point-marker))))
279 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t)
280 (and (>= this (match-beginning 0))
281 (<= this (match-end 0)) )))))))
283 ;*--- sgml mode -------------------------------------------------------*/
284 (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
285 (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
287 (defun sgml-mode-flyspell-verify ()
288 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
290 (let ((this (point-marker))
291 (s (progn (beginning-of-line) (point-marker)))
292 (e (progn (end-of-line) (point-marker))))
295 (and (re-search-forward "[^<]*>" e t)
296 (= (match-beginning 0) this)))
299 (and (re-search-backward "<[^>]*" s t)
300 (= (match-end 0) this)))
303 (and (re-search-forward "[^&]*;" e t)
304 (= (match-beginning 0) this)))
307 (and (re-search-backward "&[^;]*" s t)
308 (= (match-end 0) this)))))))))
310 ;*---------------------------------------------------------------------*/
311 ;* Programming mode */
312 ;*---------------------------------------------------------------------*/
313 (defvar flyspell-prog-text-faces
314 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
315 "Faces corresponding to text in programming-mode buffers.")
317 (defun flyspell-generic-progmode-verify ()
318 "Used for `flyspell-generic-check-word-p' in programming modes."
319 (let ((f (get-text-property (point) 'face)))
320 (memq f flyspell-prog-text-faces)))
323 (defun flyspell-prog-mode ()
324 "Turn on `flyspell-mode' for comments and strings."
326 (setq flyspell-generic-check-word-p 'flyspell-generic-progmode-verify)
329 ;*---------------------------------------------------------------------*/
330 ;* Overlay compatibility */
331 ;*---------------------------------------------------------------------*/
332 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
333 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
334 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
335 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
336 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
337 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
338 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
339 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
341 ;*---------------------------------------------------------------------*/
342 ;* Which emacs are we currently running */
343 ;*---------------------------------------------------------------------*/
344 (defvar flyspell-emacs
346 ((string-match "XEmacs" emacs-version)
350 "The type of Emacs we are currently running.")
352 (defvar flyspell-use-local-map
353 (or (eq flyspell-emacs 'xemacs)
354 (not (string< emacs-version "20"))))
356 ;*---------------------------------------------------------------------*/
357 ;* The minor mode declaration. */
358 ;*---------------------------------------------------------------------*/
359 (defvar flyspell-mode nil)
360 (make-variable-buffer-local 'flyspell-mode)
362 (defvar flyspell-mouse-map
363 (let ((map (make-sparse-keymap)))
365 ((eq flyspell-emacs 'xemacs)
366 (define-key map [(button2)] #'flyspell-correct-word)
367 (define-key map "\M-\t" #'flyspell-auto-correct-word))
368 (flyspell-use-local-map
369 (define-key map [(mouse-2)] #'flyspell-correct-word)
370 (define-key map "\M-\t" #'flyspell-auto-correct-word)))
373 ;; the name of the overlay property that defines the keymap
374 (defvar flyspell-overlay-keymap-property-name 'keymap)
376 ;; dash character machinery
377 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
378 "*Non-nil means that the `-' char is considered as a word delimiter.")
379 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
380 (defvar flyspell-dash-dictionary nil)
381 (make-variable-buffer-local 'flyspell-dash-dictionary)
382 (defvar flyspell-dash-local-dictionary nil)
383 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
385 ;*---------------------------------------------------------------------*/
387 ;*---------------------------------------------------------------------*/
388 (defface flyspell-incorrect-face
389 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
391 "Face used for marking a misspelled word in Flyspell."
394 (defface flyspell-duplicate-face
395 '((((class color)) (:foreground "Gold3" :bold t :underline t))
397 "Face used for marking a misspelled word that appears twice in the buffer.
398 See also `flyspell-duplicate-distance'."
401 (defvar flyspell-overlay nil)
403 ;*---------------------------------------------------------------------*/
404 ;* flyspell-mode ... */
405 ;*---------------------------------------------------------------------*/
407 (defun flyspell-mode (&optional arg)
408 "Minor mode performing on-the-fly spelling checking.
409 Ispell is automatically spawned on background for each entered words.
410 The default flyspell behavior is to highlight incorrect words.
411 With no argument, this command toggles Flyspell mode.
412 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
415 \\[ispell-word]: correct words (using Ispell).
416 \\[flyspell-auto-correct-word]: automatically correct word.
417 \\[flyspell-correct-word] (or mouse-2): popup correct words.
420 This runs `flyspell-mode-hook' after flyspell is entered.
423 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
424 valid. For instance, a personal dictionary can be used by
425 invoking `ispell-change-dictionary'.
427 Consider using the `ispell-parser' to check your text. For instance
429 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
432 \\[flyspell-region] checks all words inside a region.
433 \\[flyspell-buffer] checks the whole buffer."
435 (let ((old-flyspell-mode flyspell-mode))
436 ;; Mark the mode as on or off.
437 (setq flyspell-mode (not (or (and (null arg) flyspell-mode)
438 (<= (prefix-numeric-value arg) 0))))
440 (unless (eq flyspell-mode old-flyspell-mode)
444 ;; Force modeline redisplay.
445 (set-buffer-modified-p (buffer-modified-p)))))
447 ;*---------------------------------------------------------------------*/
449 ;*---------------------------------------------------------------------*/
451 (add-minor-mode 'flyspell-mode
452 'flyspell-mode-line-string
457 ;*---------------------------------------------------------------------*/
458 ;* flyspell-buffers ... */
459 ;* ------------------------------------------------------------- */
460 ;* For remembering buffers running flyspell */
461 ;*---------------------------------------------------------------------*/
462 (defvar flyspell-buffers nil)
464 ;*---------------------------------------------------------------------*/
465 ;* flyspell-minibuffer-p ... */
466 ;*---------------------------------------------------------------------*/
467 (defun flyspell-minibuffer-p (buffer)
468 "Is BUFFER a minibuffer?"
469 (let ((ws (get-buffer-window-list buffer t)))
470 (and (consp ws) (window-minibuffer-p (car ws)))))
472 ;*---------------------------------------------------------------------*/
473 ;* flyspell-accept-buffer-local-defs ... */
474 ;*---------------------------------------------------------------------*/
475 (defun flyspell-accept-buffer-local-defs ()
476 (ispell-accept-buffer-local-defs)
477 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
478 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
479 ;; the dictionary as changed
481 (setq flyspell-dash-dictionary ispell-dictionary)
482 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
483 (if (member (or ispell-local-dictionary ispell-dictionary)
484 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
485 (setq flyspell-consider-dash-as-word-delimiter-flag t)
486 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
488 ;*---------------------------------------------------------------------*/
489 ;* flyspell-mode-on ... */
490 ;*---------------------------------------------------------------------*/
491 (eval-when-compile (defvar flyspell-local-mouse-map))
493 (defun flyspell-mode-on ()
494 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
495 (setq ispell-highlight-face 'flyspell-incorrect-face)
496 ;; local dictionaries setup
497 (ispell-change-dictionary
498 (or ispell-local-dictionary ispell-dictionary flyspell-default-dictionary))
499 ;; we have to force ispell to accept the local definition or
500 ;; otherwise it could be too late, the local dictionary may
502 (flyspell-accept-buffer-local-defs)
503 ;; we put the `flyspel-delayed' property on some commands
504 (flyspell-delay-commands)
505 ;; we put the `flyspel-deplacement' property on some commands
506 (flyspell-deplacement-commands)
507 ;; we bound flyspell action to post-command hook
508 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
509 ;; we bound flyspell action to pre-command hook
510 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
511 ;; we bound flyspell action to after-change hook
512 (make-local-variable 'after-change-functions)
513 (setq after-change-functions
514 (cons 'flyspell-after-change-function after-change-functions))
515 ;; set flyspell-generic-check-word-p based on the major mode
516 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
518 (setq flyspell-generic-check-word-p mode-predicate)))
519 ;; the welcome message
520 (if (and flyspell-issue-welcome-flag (interactive-p))
521 (let ((binding (where-is-internal 'flyspell-auto-correct-word
525 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
526 (key-description binding))
527 "Welcome to flyspell. Use Mouse-2 to correct words."))))
529 ;; we end with the flyspell hooks
530 (run-hooks 'flyspell-mode-hook))
532 ;*---------------------------------------------------------------------*/
533 ;* flyspell-delay-commands ... */
534 ;*---------------------------------------------------------------------*/
535 (defun flyspell-delay-commands ()
536 "Install the standard set of Flyspell delayed commands."
537 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
538 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
540 ;*---------------------------------------------------------------------*/
541 ;* flyspell-delay-command ... */
542 ;*---------------------------------------------------------------------*/
543 (defun flyspell-delay-command (command)
544 "Set COMMAND to be delayed, for Flyspell.
545 When flyspell `post-command-hook' is invoked because a delayed command
546 as been used the current word is not immediately checked.
547 It will be checked only after `flyspell-delay' seconds."
548 (interactive "SDelay Flyspell after Command: ")
549 (put command 'flyspell-delayed t))
551 ;*---------------------------------------------------------------------*/
552 ;* flyspell-deplacement-commands ... */
553 ;*---------------------------------------------------------------------*/
554 (defun flyspell-deplacement-commands ()
555 "Install the standard set of Flyspell deplacement commands."
556 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
557 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
559 ;*---------------------------------------------------------------------*/
560 ;* flyspell-deplacement-command ... */
561 ;*---------------------------------------------------------------------*/
562 (defun flyspell-deplacement-command (command)
563 "Set COMMAND that implement cursor movements, for Flyspell.
564 When flyspell `post-command-hook' is invoked because of a deplacement command
565 as been used the current word is checked only if the previous command was
566 not the very same deplacement command."
567 (interactive "SDeplacement Flyspell after Command: ")
568 (put command 'flyspell-deplacement t))
570 ;*---------------------------------------------------------------------*/
571 ;* flyspell-word-cache ... */
572 ;*---------------------------------------------------------------------*/
573 (defvar flyspell-word-cache-start nil)
574 (defvar flyspell-word-cache-end nil)
575 (defvar flyspell-word-cache-word nil)
576 (make-variable-buffer-local 'flyspell-word-cache-start)
577 (make-variable-buffer-local 'flyspell-word-cache-end)
578 (make-variable-buffer-local 'flyspell-word-cache-word)
580 ;*---------------------------------------------------------------------*/
581 ;* The flyspell pre-hook, store the current position. In the */
582 ;* post command hook, we will check, if the word at this position */
583 ;* has to be spell checked. */
584 ;*---------------------------------------------------------------------*/
585 (defvar flyspell-pre-buffer nil)
586 (defvar flyspell-pre-point nil)
587 (defvar flyspell-pre-column nil)
588 (defvar flyspell-pre-pre-buffer nil)
589 (defvar flyspell-pre-pre-point nil)
591 ;*---------------------------------------------------------------------*/
592 ;* flyspell-previous-command ... */
593 ;*---------------------------------------------------------------------*/
594 (defvar flyspell-previous-command nil
595 "The last interactive command checked by Flyspell.")
597 ;*---------------------------------------------------------------------*/
598 ;* flyspell-pre-command-hook ... */
599 ;*---------------------------------------------------------------------*/
600 (defun flyspell-pre-command-hook ()
601 "Save the current buffer and point for Flyspell's post-command hook."
603 (setq flyspell-pre-buffer (current-buffer))
604 (setq flyspell-pre-point (point))
605 (setq flyspell-pre-column (current-column)))
607 ;*---------------------------------------------------------------------*/
608 ;* flyspell-mode-off ... */
609 ;*---------------------------------------------------------------------*/
611 (defun flyspell-mode-off ()
612 "Turn Flyspell mode off."
613 ;; we remove the hooks
614 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
615 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
616 (setq after-change-functions (delq 'flyspell-after-change-function
617 after-change-functions))
618 ;; we remove all the flyspell hilightings
619 (flyspell-delete-all-overlays)
620 ;; we have to erase pre cache variables
621 (setq flyspell-pre-buffer nil)
622 (setq flyspell-pre-point nil)
623 ;; we mark the mode as killed
624 (setq flyspell-mode nil))
626 ;*---------------------------------------------------------------------*/
627 ;* flyspell-check-pre-word-p ... */
628 ;*---------------------------------------------------------------------*/
629 (defun flyspell-check-pre-word-p ()
630 "Return non-nil if we should to check the word before point.
631 More precisely, it applies to the word that was before point
632 before the current command."
634 ((or (not (numberp flyspell-pre-point))
635 (not (bufferp flyspell-pre-buffer))
636 (not (buffer-live-p flyspell-pre-buffer)))
638 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
639 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
641 ((or (and (= flyspell-pre-point (- (point) 1))
642 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
643 (= flyspell-pre-point (point))
644 (= flyspell-pre-point (+ (point) 1)))
646 ((and (symbolp this-command)
647 (or (get this-command 'flyspell-delayed)
648 (and (get this-command 'flyspell-deplacement)
649 (eq flyspell-previous-command this-command)))
650 (or (= (current-column) 0)
651 (= (current-column) flyspell-pre-column)
652 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
654 ((not (eq (current-buffer) flyspell-pre-buffer))
656 ((not (and (numberp flyspell-word-cache-start)
657 (numberp flyspell-word-cache-end)))
660 (or (< flyspell-pre-point flyspell-word-cache-start)
661 (> flyspell-pre-point flyspell-word-cache-end)))))
663 ;*---------------------------------------------------------------------*/
664 ;* The flyspell after-change-hook, store the change position. In */
665 ;* the post command hook, we will check, if the word at this */
666 ;* position has to be spell checked. */
667 ;*---------------------------------------------------------------------*/
668 (defvar flyspell-changes nil)
670 ;*---------------------------------------------------------------------*/
671 ;* flyspell-after-change-function ... */
672 ;*---------------------------------------------------------------------*/
673 (defun flyspell-after-change-function (start stop len)
674 "Save the current buffer and point for Flyspell's post-command hook."
676 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
678 ;*---------------------------------------------------------------------*/
679 ;* flyspell-check-changed-word-p ... */
680 ;*---------------------------------------------------------------------*/
681 (defun flyspell-check-changed-word-p (start stop)
682 "Return t when the changed word has to be checked.
683 The answer depends of several criteria.
684 Mostly we check word delimiters."
686 ((and (eq (char-after start) ?\n) (> stop start))
688 ((not (numberp flyspell-pre-point))
690 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
692 ((let ((pos (point)))
693 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
698 ;*---------------------------------------------------------------------*/
699 ;* flyspell-check-word-p ... */
700 ;*---------------------------------------------------------------------*/
701 (defun flyspell-check-word-p ()
702 "Return t when the word at `point' has to be checked.
703 The answer depends of several criteria.
704 Mostly we check word delimiters."
706 ((<= (- (point-max) 1) (point-min))
707 ;; the buffer is not filled enough
709 ((and (and (> (current-column) 0)
710 (not (eq (current-column) flyspell-pre-column)))
713 (and (looking-at (flyspell-get-not-casechars))
714 (or flyspell-consider-dash-as-word-delimiter-flag
715 (not (looking-at "\\-"))))))
716 ;; yes because we have reached or typed a word delimiter.
718 ((symbolp this-command)
720 ((get this-command 'flyspell-deplacement)
721 (not (eq flyspell-previous-command this-command)))
722 ((get this-command 'flyspell-delayed)
723 ;; the current command is not delayed, that
724 ;; is that we must check the word now
725 (if (fboundp 'about-xemacs)
726 (sit-for flyspell-delay nil)
727 (sit-for flyspell-delay 0 nil)))
731 ;*---------------------------------------------------------------------*/
732 ;* flyspell-debug-signal-no-check ... */
733 ;*---------------------------------------------------------------------*/
734 (defun flyspell-debug-signal-no-check (msg obj)
735 (setq debug-on-error t)
737 (let ((buffer (get-buffer-create "*flyspell-debug*")))
740 (insert "NO-CHECK:\n")
741 (insert (format " %S : %S\n" msg obj)))))
743 ;*---------------------------------------------------------------------*/
744 ;* flyspell-debug-signal-pre-word-checked ... */
745 ;*---------------------------------------------------------------------*/
746 (defun flyspell-debug-signal-pre-word-checked ()
747 (setq debug-on-error t)
749 (let ((buffer (get-buffer-create "*flyspell-debug*")))
751 (insert "PRE-WORD:\n")
752 (insert (format " pre-point : %S\n" flyspell-pre-point))
753 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
754 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
755 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
756 (goto-char (point-max)))))
758 ;*---------------------------------------------------------------------*/
759 ;* flyspell-debug-signal-word-checked ... */
760 ;*---------------------------------------------------------------------*/
761 (defun flyspell-debug-signal-word-checked ()
762 (setq debug-on-error t)
764 (let ((oldbuf (current-buffer))
765 (buffer (get-buffer-create "*flyspell-debug*"))
769 (insert (format " this-cmd : %S\n" this-command))
770 (insert (format " delayed : %S\n" (and (symbolp this-command)
771 (get this-command 'flyspell-delayed))))
772 (insert (format " point : %S\n" point))
773 (insert (format " prev-char : [%c] %S\n"
776 (let ((c (if (> (point) (point-min))
779 (char-after (point)))
785 (let ((c (if (> (point) (point-min))
788 (and (and (looking-at (flyspell-get-not-casechars)) 1)
789 (and (or flyspell-consider-dash-as-word-delimiter-flag
790 (not (looking-at "\\-"))) 2))))))
793 (insert (format " because : %S\n"
795 ((not (and (symbolp this-command)
796 (get this-command 'flyspell-delayed)))
797 ;; the current command is not delayed, that
798 ;; is that we must check the word now
802 (let ((c (if (> (point) (point-min))
805 (and (looking-at (flyspell-get-not-casechars))
806 (or flyspell-consider-dash-as-word-delimiter-flag
807 (not (looking-at "\\-"))))))))
810 ;; yes because we have reached or typed a word delimiter.
812 ((not (integerp flyspell-delay))
813 ;; yes because the user had set up a no-delay configuration.
817 (goto-char (point-max)))))
819 ;*---------------------------------------------------------------------*/
820 ;* flyspell-debug-signal-changed-checked ... */
821 ;*---------------------------------------------------------------------*/
822 (defun flyspell-debug-signal-changed-checked ()
823 (setq debug-on-error t)
825 (let ((buffer (get-buffer-create "*flyspell-debug*"))
828 (insert "CHANGED WORD:\n")
829 (insert (format " point : %S\n" point))
830 (goto-char (point-max)))))
832 ;*---------------------------------------------------------------------*/
833 ;* flyspell-post-command-hook ... */
834 ;* ------------------------------------------------------------- */
835 ;* It is possible that we check several words: */
836 ;* 1- the current word is checked if the predicate */
837 ;* FLYSPELL-CHECK-WORD-P is true */
838 ;* 2- the word that used to be the current word before the */
839 ;* THIS-COMMAND is checked if: */
840 ;* a- the previous word is different from the current word */
841 ;* b- the previous word as not just been checked by the */
842 ;* previous FLYSPELL-POST-COMMAND-HOOK */
843 ;* 3- the words changed by the THIS-COMMAND that are neither the */
844 ;* previous word nor the current word */
845 ;*---------------------------------------------------------------------*/
846 (defun flyspell-post-command-hook ()
847 "The `post-command-hook' used by flyspell to check a word in-the-fly."
849 (let ((command this-command))
850 (if (flyspell-check-pre-word-p)
852 '(flyspell-debug-signal-pre-word-checked)
853 (set-buffer flyspell-pre-buffer)
855 (goto-char flyspell-pre-point)
857 (if (flyspell-check-word-p)
859 '(flyspell-debug-signal-word-checked)
861 ;; we remember which word we have just checked.
862 ;; this will be used next time we will check a word
863 ;; to compare the next current word with the word
864 ;; that as been registered in the pre-command-hook
865 ;; that is these variables are used within the predicate
866 ;; FLYSPELL-CHECK-PRE-WORD-P
867 (setq flyspell-pre-pre-buffer (current-buffer))
868 (setq flyspell-pre-pre-point (point)))
870 (setq flyspell-pre-pre-buffer nil)
871 (setq flyspell-pre-pre-point nil)
872 ;; when a word is not checked because of a delayed command
873 ;; we do not disable the ispell cache.
874 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
875 (setq flyspell-word-cache-end -1))))
876 (while (consp flyspell-changes)
877 (let ((start (car (car flyspell-changes)))
878 (stop (cdr (car flyspell-changes))))
879 (if (flyspell-check-changed-word-p start stop)
881 '(flyspell-debug-signal-changed-checked)
884 (setq flyspell-changes (cdr flyspell-changes))))
885 (setq flyspell-previous-command command)))
887 ;*---------------------------------------------------------------------*/
888 ;* flyspell-notify-misspell ... */
889 ;*---------------------------------------------------------------------*/
890 (defun flyspell-notify-misspell (start end word poss)
891 (let ((replacements (if (stringp poss)
893 (if flyspell-sort-corrections
894 (sort (car (cdr (cdr poss))) 'string<)
895 (car (cdr (cdr poss)))))))
896 (message (format "mispelling `%s' %S" word replacements))))
898 ;*---------------------------------------------------------------------*/
899 ;* flyspell-word ... */
900 ;*---------------------------------------------------------------------*/
901 (defun flyspell-word (&optional following)
902 "Spell check a word."
903 (interactive (list current-prefix-arg))
905 (setq following ispell-following-word))
907 ;; use the correct dictionary
908 (flyspell-accept-buffer-local-defs)
909 (let* ((cursor-location (point))
910 (flyspell-word (flyspell-get-word following))
912 (if (or (eq flyspell-word nil)
913 (and (fboundp flyspell-generic-check-word-p)
914 (not (funcall flyspell-generic-check-word-p))))
917 ;; destructure return flyspell-word info list.
918 (setq start (car (cdr flyspell-word))
919 end (car (cdr (cdr flyspell-word)))
920 word (car flyspell-word))
921 ;; before checking in the directory, we check for doublons.
923 ((and (or (not (eq ispell-parser 'tex))
924 (not (eq (char-after start) ?\\)))
925 flyspell-mark-duplications-flag
928 (word-search-backward word
932 ;; yes, this is a doublon
933 (flyspell-highlight-incorrect-region start end 'doublon))
934 ((and (eq flyspell-word-cache-start start)
935 (eq flyspell-word-cache-end end)
936 (string-equal flyspell-word-cache-word word))
937 ;; this word had been already checked, we skip
939 ((and (eq ispell-parser 'tex)
940 (flyspell-tex-command-p flyspell-word))
941 ;; this is a correct word (because a tex command)
942 (flyspell-unhighlight-at start)
944 (flyspell-unhighlight-at (- end 1)))
947 ;; we setup the cache
948 (setq flyspell-word-cache-start start)
949 (setq flyspell-word-cache-end end)
950 (setq flyspell-word-cache-word word)
951 ;; now check spelling of word.
952 (process-send-string ispell-process "%\n")
953 ;; put in verbose mode
954 (process-send-string ispell-process
955 (concat "^" word "\n"))
956 ;; we mark the ispell process so it can be killed
957 ;; when emacs is exited without query
958 (if (fboundp 'process-kill-without-query)
959 (process-kill-without-query ispell-process))
960 ;; wait until ispell has processed word
962 (accept-process-output ispell-process)
963 (not (string= "" (car ispell-filter)))))
964 ;; (process-send-string ispell-process "!\n")
965 ;; back to terse mode.
966 (setq ispell-filter (cdr ispell-filter))
967 (if (consp ispell-filter)
968 (setq poss (ispell-parse-output (car ispell-filter))))
971 (flyspell-unhighlight-at start)
973 (flyspell-unhighlight-at (- end 1)))
975 ((and (stringp poss) flyspell-highlight-flag)
977 (flyspell-unhighlight-at start)
979 (flyspell-unhighlight-at (- end 1)))
982 (flyspell-unhighlight-at start)
984 (flyspell-unhighlight-at (- end 1))))
985 ((or (and (< flyspell-duplicate-distance 0)
988 (word-search-backward word
993 (word-search-forward word
996 (and (> flyspell-duplicate-distance 0)
999 (word-search-backward
1002 flyspell-duplicate-distance)
1006 (word-search-forward
1009 flyspell-duplicate-distance)
1011 (if flyspell-highlight-flag
1012 (flyspell-highlight-duplicate-region start end)
1013 (message (format "duplicate `%s'" word))))
1015 ;; incorrect highlight the location
1016 (if flyspell-highlight-flag
1017 (flyspell-highlight-incorrect-region start end poss)
1018 (flyspell-notify-misspell start end word poss))))
1019 ;; return to original location
1020 (goto-char cursor-location)
1021 (if ispell-quit (setq ispell-quit nil)))))))))
1023 ;*---------------------------------------------------------------------*/
1024 ;* flyspell-tex-math-initialized ... */
1025 ;*---------------------------------------------------------------------*/
1026 (defvar flyspell-tex-math-initialized nil)
1028 ;*---------------------------------------------------------------------*/
1029 ;* flyspell-math-tex-command-p ... */
1030 ;* ------------------------------------------------------------- */
1031 ;* This function uses the texmathp package to check if (point) */
1032 ;* is within a tex command. In order to avoid using */
1033 ;* condition-case each time we use the variable */
1034 ;* flyspell-tex-math-initialized to make a special case the first */
1035 ;* time that function is called. */
1036 ;*---------------------------------------------------------------------*/
1037 (defun flyspell-math-tex-command-p ()
1039 (flyspell-check-tex-math-command
1041 ((eq flyspell-tex-math-initialized t)
1043 ((eq flyspell-tex-math-initialized 'error)
1046 (setq flyspell-tex-math-initialized t)
1050 (setq flyspell-tex-math-initialized 'error)
1053 ;*---------------------------------------------------------------------*/
1054 ;* flyspell-tex-command-p ... */
1055 ;*---------------------------------------------------------------------*/
1056 (defun flyspell-tex-command-p (word)
1057 "Return t if WORD is a TeX command."
1059 (let ((b (car (cdr word))))
1060 (and (re-search-backward "\\\\" (- (point) 100) t)
1061 (or (= (match-end 0) b)
1062 (and (goto-char (match-end 0))
1063 (looking-at flyspell-tex-command-regexp)
1064 (>= (match-end 0) b))))))
1065 (flyspell-math-tex-command-p)))
1067 ;*---------------------------------------------------------------------*/
1068 ;* flyspell-casechars-cache ... */
1069 ;*---------------------------------------------------------------------*/
1070 (defvar flyspell-casechars-cache nil)
1071 (defvar flyspell-ispell-casechars-cache nil)
1072 (make-variable-buffer-local 'flyspell-casechars-cache)
1073 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1075 ;*---------------------------------------------------------------------*/
1076 ;* flyspell-get-casechars ... */
1077 ;*---------------------------------------------------------------------*/
1078 (defun flyspell-get-casechars ()
1079 "This function builds a string that is the regexp of word chars.
1080 In order to avoid one useless string construction,
1081 this function changes the last char of the `ispell-casechars' string."
1082 (let ((ispell-casechars (ispell-get-casechars)))
1084 ((eq ispell-parser 'tex)
1085 (setq flyspell-ispell-casechars-cache ispell-casechars)
1086 (setq flyspell-casechars-cache
1087 (concat (substring ispell-casechars
1089 (- (length ispell-casechars) 1))
1091 flyspell-casechars-cache)
1093 (setq flyspell-ispell-casechars-cache ispell-casechars)
1094 (setq flyspell-casechars-cache ispell-casechars)
1095 flyspell-casechars-cache))))
1097 ;*---------------------------------------------------------------------*/
1098 ;* flyspell-get-not-casechars-cache ... */
1099 ;*---------------------------------------------------------------------*/
1100 (defvar flyspell-not-casechars-cache nil)
1101 (defvar flyspell-ispell-not-casechars-cache nil)
1102 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1103 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1105 ;*---------------------------------------------------------------------*/
1106 ;* flyspell-get-not-casechars ... */
1107 ;*---------------------------------------------------------------------*/
1108 (defun flyspell-get-not-casechars ()
1109 "This function builds a string that is the regexp of non-word chars."
1110 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1112 ((eq ispell-parser 'tex)
1113 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1114 (setq flyspell-not-casechars-cache
1115 (concat (substring ispell-not-casechars
1117 (- (length ispell-not-casechars) 1))
1119 flyspell-not-casechars-cache)
1121 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1122 (setq flyspell-not-casechars-cache ispell-not-casechars)
1123 flyspell-not-casechars-cache))))
1125 ;*---------------------------------------------------------------------*/
1126 ;* flyspell-get-word ... */
1127 ;*---------------------------------------------------------------------*/
1128 (defun flyspell-get-word (following)
1129 "Return the word for spell-checking according to Ispell syntax.
1130 If argument FOLLOWING is non-nil or if `ispell-following-word'
1131 is non-nil when called interactively, then the following word
1132 \(rather than preceding\) is checked when the cursor is not over a word.
1133 Optional second argument contains other chars that can be included in word
1136 Word syntax described by `ispell-dictionary-alist' (which see)."
1137 (let* ((flyspell-casechars (flyspell-get-casechars))
1138 (flyspell-not-casechars (flyspell-get-not-casechars))
1139 (ispell-otherchars (ispell-get-otherchars))
1140 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1141 (word-regexp (if (string< "" ispell-otherchars)
1142 (concat flyspell-casechars
1148 (if ispell-many-otherchars-p
1150 (concat flyspell-casechars "+")))
1154 (if (not (looking-at flyspell-casechars))
1156 (re-search-forward flyspell-casechars (point-max) t)
1157 (re-search-backward flyspell-casechars (point-min) t)))
1158 ;; move to front of word
1159 (re-search-backward flyspell-not-casechars (point-min) 'start)
1161 (if (string< "" ispell-otherchars)
1162 (while (and (looking-at ispell-otherchars)
1164 (or (not did-it-once)
1165 ispell-many-otherchars-p)
1166 (not (eq pos (point))))
1168 (setq did-it-once t)
1170 (if (looking-at flyspell-casechars)
1171 (re-search-backward flyspell-not-casechars (point-min) 'move)
1172 (backward-char -1)))))
1173 ;; Now mark the word and save to string.
1174 (if (eq (re-search-forward word-regexp (point-max) t) nil)
1177 (setq start (match-beginning 0)
1179 word (buffer-substring-no-properties start end))
1180 (list word start end)))))
1182 ;*---------------------------------------------------------------------*/
1183 ;* flyspell-small-region ... */
1184 ;*---------------------------------------------------------------------*/
1185 (defun flyspell-small-region (beg end)
1186 "Flyspell text between BEG and END."
1194 (while (< (point) end)
1197 (message "Spell Checking...%d%%"
1198 (* 100 (/ (float (- (point) beg)) (- end beg))))
1200 (setq count (+ 1 count)))
1203 (let ((cur (point)))
1205 (if (and (< (point) end) (> (point) (+ cur 1)))
1206 (backward-char 1)))))
1208 (message "Spell Checking completed.")
1211 ;*---------------------------------------------------------------------*/
1212 ;* flyspell-external-ispell-process ... */
1213 ;*---------------------------------------------------------------------*/
1214 (defvar flyspell-external-ispell-process '()
1215 "The external Flyspell Ispell process.")
1217 ;*---------------------------------------------------------------------*/
1218 ;* flyspell-external-ispell-buffer ... */
1219 ;*---------------------------------------------------------------------*/
1220 (defvar flyspell-external-ispell-buffer '())
1221 (defvar flyspell-large-region-buffer '())
1222 (defvar flyspell-large-region-beg (point-min))
1223 (defvar flyspell-large-region-end (point-max))
1225 ;*---------------------------------------------------------------------*/
1226 ;* flyspell-external-point-words ... */
1227 ;*---------------------------------------------------------------------*/
1228 (defun flyspell-external-point-words ()
1229 (let ((buffer flyspell-external-ispell-buffer))
1231 (beginning-of-buffer)
1232 (let ((size (- flyspell-large-region-end flyspell-large-region-beg))
1233 (start flyspell-large-region-beg))
1234 ;; now we are done with ispell, we have to find the word in
1235 ;; the initial buffer
1236 (while (< (point) (- (point-max) 1))
1237 ;; we have to fetch the incorrect word
1238 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1239 (let ((word (match-string 1)))
1240 (goto-char (match-end 0))
1241 (set-buffer flyspell-large-region-buffer)
1242 (goto-char flyspell-large-region-beg)
1243 (message "Spell Checking...%d%% [%s]"
1244 (* 100 (/ (float (- (point) start)) size))
1246 (if (search-forward word flyspell-large-region-end t)
1248 (setq flyspell-large-region-beg (point))
1249 (goto-char (- (point) 1))
1251 (set-buffer buffer))
1252 (goto-char (point-max)))))
1254 (message "Spell Checking completed.")
1255 ;; ok, we are done with pointing out incorrect words, we just
1256 ;; have to kill the temporary buffer
1257 (kill-buffer flyspell-external-ispell-buffer)
1258 (setq flyspell-external-ispell-buffer nil)))
1260 ;*---------------------------------------------------------------------*/
1261 ;* flyspell-large-region ... */
1262 ;*---------------------------------------------------------------------*/
1263 (defun flyspell-large-region (beg end)
1264 (let* ((curbuf (current-buffer))
1265 (buffer (get-buffer-create "*flyspell-region*")))
1266 (setq flyspell-external-ispell-buffer buffer)
1267 (setq flyspell-large-region-buffer curbuf)
1268 (setq flyspell-large-region-beg beg)
1269 (setq flyspell-large-region-end end)
1272 ;; this is done, we can start checking...
1273 (message "Checking region...")
1275 (let ((c (apply 'call-process-region beg
1283 ;; Local dictionary becomes the global dictionary in use.
1284 (if ispell-local-dictionary
1285 (setq ispell-dictionary ispell-local-dictionary))
1286 (setq args (ispell-get-ispell-args))
1287 (if ispell-dictionary ; use specified dictionary
1289 (append (list "-d" ispell-dictionary) args)))
1290 (if ispell-personal-dictionary ; use specified pers dict
1295 ispell-personal-dictionary)))))
1296 (setq args (append args ispell-extra-args))
1299 (flyspell-external-point-words)
1300 (error "Can't check region...")))))
1302 ;*---------------------------------------------------------------------*/
1303 ;* flyspell-region ... */
1304 ;* ------------------------------------------------------------- */
1305 ;* Because `ispell -a' is too slow, it is not possible to use */
1306 ;* it on large region. Then, when ispell is invoked on a large */
1307 ;* text region, a new `ispell -l' process is spawned. The */
1308 ;* pointed out words are then searched in the region a checked with */
1309 ;* regular flyspell means. */
1310 ;*---------------------------------------------------------------------*/
1312 (defun flyspell-region (beg end)
1313 "Flyspell text between BEG and END."
1322 (if (> (- end beg) flyspell-large-region)
1323 (flyspell-large-region beg end)
1324 (flyspell-small-region beg end)))))
1326 ;*---------------------------------------------------------------------*/
1327 ;* flyspell-buffer ... */
1328 ;*---------------------------------------------------------------------*/
1330 (defun flyspell-buffer ()
1331 "Flyspell whole buffer."
1333 (flyspell-region (point-min) (point-max)))
1335 ;*---------------------------------------------------------------------*/
1336 ;* old next error position ... */
1337 ;*---------------------------------------------------------------------*/
1338 (defvar flyspell-old-buffer-error nil)
1339 (defvar flyspell-old-pos-error nil)
1341 ;*---------------------------------------------------------------------*/
1342 ;* flyspell-goto-next-error ... */
1343 ;*---------------------------------------------------------------------*/
1344 (defun flyspell-goto-next-error ()
1345 "Go to the next previously detected error.
1346 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1351 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1352 (eq pos flyspell-old-pos-error))
1354 (if (= flyspell-old-pos-error max)
1355 ;; goto beginning of buffer
1357 (message "Restarting from beginning of buffer")
1358 (goto-char (point-min)))
1360 (setq pos (point))))
1361 ;; seek the next error
1362 (while (and (< pos max)
1363 (let ((ovs (overlays-at pos))
1365 (while (and (not r) (consp ovs))
1366 (if (flyspell-overlay-p (car ovs))
1368 (setq ovs (cdr ovs))))
1370 (setq pos (1+ pos)))
1371 ;; save the current location for next invocation
1372 (setq flyspell-old-pos-error pos)
1373 (setq flyspell-old-buffer-error (current-buffer))
1376 (message "No more miss-spelled word!"))))
1378 ;*---------------------------------------------------------------------*/
1379 ;* flyspell-overlay-p ... */
1380 ;*---------------------------------------------------------------------*/
1381 (defun flyspell-overlay-p (o)
1382 "A predicate that return true iff O is an overlay used by flyspell."
1383 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1385 ;*---------------------------------------------------------------------*/
1386 ;* flyspell-delete-all-overlays ... */
1387 ;* ------------------------------------------------------------- */
1388 ;* Remove all the overlays introduced by flyspell. */
1389 ;*---------------------------------------------------------------------*/
1390 (defun flyspell-delete-all-overlays ()
1391 "Delete all the overlays used by flyspell."
1392 (let ((l (overlays-in (point-min) (point-max))))
1395 (if (flyspell-overlay-p (car l))
1396 (delete-overlay (car l)))
1397 (setq l (cdr l))))))
1399 ;*---------------------------------------------------------------------*/
1400 ;* flyspell-unhighlight-at ... */
1401 ;*---------------------------------------------------------------------*/
1402 (defun flyspell-unhighlight-at (pos)
1403 "Remove the flyspell overlay that are located at POS."
1404 (if flyspell-persistent-highlight
1405 (let ((overlays (overlays-at pos)))
1406 (while (consp overlays)
1407 (if (flyspell-overlay-p (car overlays))
1408 (delete-overlay (car overlays)))
1409 (setq overlays (cdr overlays))))
1410 (if (flyspell-overlay-p flyspell-overlay)
1411 (delete-overlay flyspell-overlay))))
1413 ;*---------------------------------------------------------------------*/
1414 ;* flyspell-properties-at-p ... */
1415 ;* ------------------------------------------------------------- */
1416 ;* Is there an highlight properties at position pos? */
1417 ;*---------------------------------------------------------------------*/
1418 (defun flyspell-properties-at-p (pos)
1419 "Return t if there is a text property at POS, not counting `local-map'.
1420 If variable `flyspell-highlight-properties' is set to nil,
1421 text with properties are not checked. This function is used to discover
1422 if the character at POS has any other property."
1423 (let ((prop (text-properties-at pos))
1425 (while (and keep (consp prop))
1426 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1427 (setq prop (cdr (cdr prop)))
1431 ;*---------------------------------------------------------------------*/
1432 ;* make-flyspell-overlay ... */
1433 ;*---------------------------------------------------------------------*/
1434 (defun make-flyspell-overlay (beg end face mouse-face)
1435 "Allocate an overlay to highlight an incorrect word.
1436 BEG and END specify the range in the buffer of that word.
1437 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1439 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1440 (overlay-put flyspell-overlay 'face face)
1441 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1442 (overlay-put flyspell-overlay 'flyspell-overlay t)
1443 (if flyspell-use-local-map
1444 (overlay-put flyspell-overlay
1445 flyspell-overlay-keymap-property-name
1446 flyspell-mouse-map))
1449 ;*---------------------------------------------------------------------*/
1450 ;* flyspell-highlight-incorrect-region ... */
1451 ;*---------------------------------------------------------------------*/
1452 (defun flyspell-highlight-incorrect-region (beg end poss)
1453 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1454 (unless (run-hook-with-args-until-success
1455 'flyspell-incorrect-hook beg end poss)
1456 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1458 ;; we cleanup current overlay at the same position
1459 (if (and (not flyspell-persistent-highlight)
1460 (overlayp flyspell-overlay))
1461 (delete-overlay flyspell-overlay)
1462 (let ((overlays (overlays-at beg)))
1463 (while (consp overlays)
1464 (if (flyspell-overlay-p (car overlays))
1465 (delete-overlay (car overlays)))
1466 (setq overlays (cdr overlays)))))
1467 ;; now we can use a new overlay
1468 (setq flyspell-overlay
1469 (make-flyspell-overlay beg end
1470 'flyspell-incorrect-face 'highlight))))))
1472 ;*---------------------------------------------------------------------*/
1473 ;* flyspell-highlight-duplicate-region ... */
1474 ;*---------------------------------------------------------------------*/
1475 (defun flyspell-highlight-duplicate-region (beg end)
1476 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
1477 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1479 ;; we cleanup current overlay at the same position
1480 (if (and (not flyspell-persistent-highlight)
1481 (overlayp flyspell-overlay))
1482 (delete-overlay flyspell-overlay)
1483 (let ((overlays (overlays-at beg)))
1484 (while (consp overlays)
1485 (if (flyspell-overlay-p (car overlays))
1486 (delete-overlay (car overlays)))
1487 (setq overlays (cdr overlays)))))
1488 ;; now we can use a new overlay
1489 (setq flyspell-overlay
1490 (make-flyspell-overlay beg end
1491 'flyspell-duplicate-face 'highlight)))))
1493 ;*---------------------------------------------------------------------*/
1494 ;* flyspell-auto-correct-cache ... */
1495 ;*---------------------------------------------------------------------*/
1496 (defvar flyspell-auto-correct-pos nil)
1497 (defvar flyspell-auto-correct-region nil)
1498 (defvar flyspell-auto-correct-ring nil)
1499 (defvar flyspell-auto-correct-word nil)
1500 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1501 (make-variable-buffer-local 'flyspell-auto-correct-region)
1502 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1503 (make-variable-buffer-local 'flyspell-auto-correct-word)
1505 ;*---------------------------------------------------------------------*/
1506 ;* flyspell-check-previous-highlighted-word ... */
1507 ;*---------------------------------------------------------------------*/
1508 (defun flyspell-check-previous-highlighted-word (&optional arg)
1509 "Correct the closer misspelled word.
1510 This function scans a mis-spelled word before the cursor. If it finds one
1511 it proposes replacement for that word. With prefix arg, count that many
1512 misspelled words backwards."
1514 (let ((pos1 (point))
1516 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1519 (while (and (setq pos (previous-overlay-change pos))
1522 (if (> pos (point-min))
1524 (setq ovs (overlays-at (1- pos)))
1527 (setq ovs (cdr ovs))
1528 (if (and (overlay-get ov 'flyspell-overlay)
1529 (= 0 (setq arg (1- arg))))
1530 (throw 'exit t)))))))
1534 (error "No word to correct before point"))))
1536 ;*---------------------------------------------------------------------*/
1537 ;* flyspell-display-next-corrections ... */
1538 ;*---------------------------------------------------------------------*/
1539 (defun flyspell-display-next-corrections (corrections)
1540 (let ((string "Corrections:")
1543 (while (< (length string) 80)
1544 (if (equal (car l) flyspell-auto-correct-word)
1545 (setq pos (cons (+ 1 (length string)) pos)))
1546 (setq string (concat string " " (car l)))
1549 (let ((num (car pos)))
1550 (put-text-property num
1551 (+ num (length flyspell-auto-correct-word))
1553 'flyspell-incorrect-face
1555 (setq pos (cdr pos)))
1556 (if (fboundp 'display-message)
1557 (display-message 'no-log string)
1560 ;*---------------------------------------------------------------------*/
1561 ;* flyspell-abbrev-table ... */
1562 ;*---------------------------------------------------------------------*/
1563 (defun flyspell-abbrev-table ()
1564 (if flyspell-use-global-abbrev-table-p
1566 local-abbrev-table))
1568 ;*---------------------------------------------------------------------*/
1569 ;* flyspell-auto-correct-word ... */
1570 ;*---------------------------------------------------------------------*/
1571 (defun flyspell-auto-correct-word ()
1572 "Correct the current word.
1573 This command proposes various successive corrections for the current word."
1576 (old-max (point-max)))
1577 ;; use the correct dictionary
1578 (flyspell-accept-buffer-local-defs)
1579 (if (and (eq flyspell-auto-correct-pos pos)
1580 (consp flyspell-auto-correct-region))
1581 ;; we have already been using the function at the same location
1582 (let* ((start (car flyspell-auto-correct-region))
1583 (len (cdr flyspell-auto-correct-region)))
1584 (delete-region start (+ start len))
1585 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1586 (let* ((word (car flyspell-auto-correct-ring))
1587 (len (length word)))
1588 (rplacd flyspell-auto-correct-region len)
1590 (if flyspell-abbrev-p
1591 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1592 flyspell-auto-correct-word)
1593 (flyspell-change-abbrev (flyspell-abbrev-table)
1594 flyspell-auto-correct-word
1596 (define-abbrev (flyspell-abbrev-table)
1597 flyspell-auto-correct-word word)))
1600 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1601 (flyspell-ajust-cursor-point pos (point) old-max)
1602 (setq flyspell-auto-correct-pos (point)))
1603 ;; fetch the word to be checked
1604 (let ((word (flyspell-get-word nil))
1606 ;; destructure return word info list.
1607 (setq start (car (cdr word))
1608 end (car (cdr (cdr word)))
1610 (setq flyspell-auto-correct-word word)
1611 ;; now check spelling of word.
1612 (process-send-string ispell-process "%\n") ;put in verbose mode
1613 (process-send-string ispell-process (concat "^" word "\n"))
1614 ;; wait until ispell has processed word
1616 (accept-process-output ispell-process)
1617 (not (string= "" (car ispell-filter)))))
1618 (setq ispell-filter (cdr ispell-filter))
1619 (if (consp ispell-filter)
1620 (setq poss (ispell-parse-output (car ispell-filter))))
1621 (cond ((or (eq poss t) (stringp poss))
1622 ;; don't correct word
1626 (error "Ispell: error in Ispell process"))
1628 ;; the word is incorrect, we have to propose a replacement
1629 (let ((replacements (if flyspell-sort-corrections
1630 (sort (car (cdr (cdr poss))) 'string<)
1631 (car (cdr (cdr poss))))))
1632 (setq flyspell-auto-correct-region nil)
1633 (if (consp replacements)
1635 (let ((replace (car replacements)))
1636 (let ((new-word replace))
1637 (if (not (equal new-word (car poss)))
1639 ;; the save the current replacements
1640 (setq flyspell-auto-correct-region
1641 (cons start (length new-word)))
1642 (let ((l replacements))
1643 (while (consp (cdr l))
1645 (rplacd l (cons (car poss) replacements)))
1646 (setq flyspell-auto-correct-ring
1648 (delete-region start end)
1650 (if flyspell-abbrev-p
1651 (if (flyspell-already-abbrevp
1652 (flyspell-abbrev-table) word)
1653 (flyspell-change-abbrev
1654 (flyspell-abbrev-table)
1657 (define-abbrev (flyspell-abbrev-table)
1660 (flyspell-display-next-corrections
1661 (cons new-word flyspell-auto-correct-ring))
1662 (flyspell-ajust-cursor-point pos
1665 (setq flyspell-auto-correct-pos (point))
1666 (ispell-pdict-save t)))))
1668 ;*---------------------------------------------------------------------*/
1669 ;* flyspell-correct-word ... */
1670 ;*---------------------------------------------------------------------*/
1671 (defun flyspell-correct-word (event)
1672 "Pop up a menu of possible corrections for a misspelled word.
1673 The word checked is the word at the mouse position."
1675 ;; use the correct dictionary
1676 (flyspell-accept-buffer-local-defs)
1677 ;; retain cursor location (I don't know why but save-excursion here fails).
1678 (let ((save (point)))
1679 (mouse-set-point event)
1680 (let ((cursor-location (point))
1681 (word (flyspell-get-word nil))
1682 start end poss replace)
1683 ;; destructure return word info list.
1684 (setq start (car (cdr word))
1685 end (car (cdr (cdr word)))
1687 ;; now check spelling of word.
1688 (process-send-string ispell-process "%\n") ;put in verbose mode
1689 (process-send-string ispell-process (concat "^" word "\n"))
1690 ;; wait until ispell has processed word
1692 (accept-process-output ispell-process)
1693 (not (string= "" (car ispell-filter)))))
1694 (setq ispell-filter (cdr ispell-filter))
1695 (if (consp ispell-filter)
1696 (setq poss (ispell-parse-output (car ispell-filter))))
1697 (cond ((or (eq poss t) (stringp poss))
1698 ;; don't correct word
1702 (error "Ispell: error in Ispell process"))
1703 ((string-match "GNU" (emacs-version))
1704 ;; the word is incorrect, we have to propose a replacement
1705 (setq replace (flyspell-emacs-popup event poss word))
1706 (cond ((eq replace 'ignore)
1711 (process-send-string ispell-process (concat "*" word "\n"))
1712 (flyspell-unhighlight-at cursor-location)
1713 (setq ispell-pdict-modified-p '(t)))
1714 ((or (eq replace 'buffer) (eq replace 'session))
1715 (process-send-string ispell-process (concat "@" word "\n"))
1716 (if (null ispell-pdict-modified-p)
1717 (setq ispell-pdict-modified-p
1718 (list ispell-pdict-modified-p)))
1719 (flyspell-unhighlight-at cursor-location)
1721 (if (eq replace 'buffer)
1722 (ispell-add-per-file-word-list word)))
1724 (let ((new-word (if (atom replace)
1727 (cursor-location (+ (- (length word) (- end start))
1729 (if (not (equal new-word (car poss)))
1730 (let ((old-max (point-max)))
1731 (delete-region start end)
1733 (if flyspell-abbrev-p
1734 (define-abbrev (flyspell-abbrev-table)
1737 (flyspell-ajust-cursor-point save
1743 ((eq flyspell-emacs 'xemacs)
1744 (flyspell-xemacs-popup
1745 event poss word cursor-location start end save)
1747 (ispell-pdict-save t))))
1749 ;*---------------------------------------------------------------------*/
1750 ;* flyspell-xemacs-correct ... */
1751 ;*---------------------------------------------------------------------*/
1752 (defun flyspell-xemacs-correct (replace poss word cursor-location start end save)
1753 "The xemacs popup menu callback."
1754 (cond ((eq replace 'ignore)
1757 (process-send-string ispell-process (concat "*" word "\n"))
1758 (process-send-string ispell-process "#\n")
1759 (flyspell-unhighlight-at cursor-location)
1760 (setq ispell-pdict-modified-p '(t)))
1761 ((or (eq replace 'buffer) (eq replace 'session))
1762 (process-send-string ispell-process (concat "@" word "\n"))
1763 (flyspell-unhighlight-at cursor-location)
1764 (if (null ispell-pdict-modified-p)
1765 (setq ispell-pdict-modified-p
1766 (list ispell-pdict-modified-p)))
1767 (if (eq replace 'buffer)
1768 (ispell-add-per-file-word-list word)))
1770 (let ((old-max (point-max))
1771 (new-word (if (atom replace)
1774 (cursor-location (+ (- (length word) (- end start))
1776 (if (not (equal new-word (car poss)))
1778 (delete-region start end)
1781 (if flyspell-abbrev-p
1782 (define-abbrev (flyspell-abbrev-table)
1785 (flyspell-ajust-cursor-point save cursor-location old-max)))))
1787 ;*---------------------------------------------------------------------*/
1788 ;* flyspell-ajust-cursor-point ... */
1789 ;*---------------------------------------------------------------------*/
1790 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
1791 (if (>= save cursor-location)
1792 (let ((new-pos (+ save (- (point-max) old-max))))
1794 ((< new-pos (point-min))
1796 ((> new-pos (point-max))
1801 ;*---------------------------------------------------------------------*/
1802 ;* flyspell-emacs-popup ... */
1803 ;*---------------------------------------------------------------------*/
1804 (defun flyspell-emacs-popup (event poss word)
1805 "The Emacs popup menu."
1807 (let* ((mouse-pos (mouse-position))
1808 (mouse-pos (if (nth 1 mouse-pos)
1810 (set-mouse-position (car mouse-pos)
1811 (/ (frame-width) 2) 2)
1814 (setq event (list (list (car (cdr mouse-pos))
1815 (1+ (cdr (cdr mouse-pos))))
1817 (let* ((corrects (if flyspell-sort-corrections
1818 (sort (car (cdr (cdr poss))) 'string<)
1819 (car (cdr (cdr poss)))))
1820 (cor-menu (if (consp corrects)
1821 (mapcar (lambda (correct)
1822 (list correct correct))
1825 (affix (car (cdr (cdr (cdr poss)))))
1826 (base-menu (let ((save (if (consp affix)
1828 (list (concat "Save affix: " (car affix))
1830 '("Accept (session)" accept)
1831 '("Accept (buffer)" buffer))
1832 '(("Save word" save)
1833 ("Accept (session)" session)
1834 ("Accept (buffer)" buffer)))))
1835 (if (consp cor-menu)
1836 (append cor-menu (cons "" save))
1838 (menu (cons "flyspell correction menu" base-menu)))
1839 (car (x-popup-menu event
1840 (list (format "%s [%s]" word (or ispell-local-dictionary
1844 ;*---------------------------------------------------------------------*/
1845 ;* flyspell-xemacs-popup ... */
1846 ;*---------------------------------------------------------------------*/
1847 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
1848 "The XEmacs popup menu."
1849 (let* ((corrects (if flyspell-sort-corrections
1850 (sort (car (cdr (cdr poss))) 'string<)
1851 (car (cdr (cdr poss)))))
1852 (cor-menu (if (consp corrects)
1853 (mapcar (lambda (correct)
1855 (list 'flyspell-xemacs-correct
1866 (affix (car (cdr (cdr (cdr poss)))))
1867 (menu (let ((save (if (consp affix)
1869 (concat "Save affix: " (car affix))
1870 (list 'flyspell-xemacs-correct
1881 (list 'flyspell-xemacs-correct
1890 (session (vector "Accept (session)"
1891 (list 'flyspell-xemacs-correct
1900 (buffer (vector "Accept (buffer)"
1901 (list 'flyspell-xemacs-correct
1910 (if (consp cor-menu)
1911 (append cor-menu (list "-" save session buffer))
1912 (list save session buffer)))))
1913 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
1917 ;*---------------------------------------------------------------------*/
1918 ;* Some example functions for real autocorrecting */
1919 ;*---------------------------------------------------------------------*/
1921 (defun flyspell-maybe-correct-transposition (beg end poss)
1922 "Check replacements for transposed characters.
1924 If the text between BEG and END is equal to a correction suggested by
1925 Ispell, after transposing two adjacent characters, correct the text,
1928 The third arg POSS is either the symbol 'doublon' or a list of
1929 possible corrections as returned by 'ispell-parse-output'.
1931 This function is meant to be added to 'flyspell-incorrect-hook'."
1933 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
1936 (copy-to-buffer temp-buffer beg end)
1937 (set-buffer temp-buffer)
1938 (goto-char (1+ (point-min)))
1939 (while (and (not (eobp)) (not found))
1941 (if (member (buffer-string) (nth 2 poss))
1942 (setq found (point))
1943 (transpose-chars -1)
1947 (goto-char (+ beg found -1))
1948 (transpose-chars -1)
1951 (defun flyspell-maybe-correct-doubling (beg end poss)
1952 "Check replacements for doubled characters.
1954 If the text between BEG and END is equal to a correction suggested by
1955 Ispell, after removing a pair of doubled characters, correct the text,
1958 The third arg POSS is either the symbol 'doublon' or a list of
1959 possible corrections as returned by 'ispell-parse-output'.
1961 This function is meant to be added to 'flyspell-incorrect-hook'."
1963 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
1966 (copy-to-buffer temp-buffer beg end)
1967 (set-buffer temp-buffer)
1968 (goto-char (1+ (point-min)))
1969 (while (and (not (eobp)) (not found))
1970 (when (char-equal (char-after) (char-before))
1972 (if (member (buffer-string) (nth 2 poss))
1973 (setq found (point))
1974 (insert-char (char-before) 1)))
1978 (goto-char (+ beg found -1))
1982 ;*---------------------------------------------------------------------*/
1983 ;* flyspell-already-abbrevp ... */
1984 ;*---------------------------------------------------------------------*/
1985 (defun flyspell-already-abbrevp (table word)
1986 (let ((sym (abbrev-symbol word table)))
1987 (and sym (symbolp sym))))
1989 ;*---------------------------------------------------------------------*/
1990 ;* flyspell-change-abbrev ... */
1991 ;*---------------------------------------------------------------------*/
1992 (defun flyspell-change-abbrev (table old new)
1993 (set (abbrev-symbol old table) new))
1997 ;;; flyspell.el ends here