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."
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)))
375 ;; the name of the overlay property that defines the keymap
376 (defvar flyspell-overlay-keymap-property-name 'keymap)
378 ;; dash character machinery
379 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
380 "*Non-nil means that the `-' char is considered as a word delimiter.")
381 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
382 (defvar flyspell-dash-dictionary nil)
383 (make-variable-buffer-local 'flyspell-dash-dictionary)
384 (defvar flyspell-dash-local-dictionary nil)
385 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
387 ;*---------------------------------------------------------------------*/
389 ;*---------------------------------------------------------------------*/
390 (defface flyspell-incorrect-face
391 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
393 "Face used for marking a misspelled word in Flyspell."
396 (defface flyspell-duplicate-face
397 '((((class color)) (:foreground "Gold3" :bold t :underline t))
399 "Face used for marking a misspelled word that appears twice in the buffer.
400 See also `flyspell-duplicate-distance'."
403 (defvar flyspell-overlay nil)
405 ;*---------------------------------------------------------------------*/
406 ;* flyspell-mode ... */
407 ;*---------------------------------------------------------------------*/
409 (defun flyspell-mode (&optional arg)
410 "Minor mode performing on-the-fly spelling checking.
411 Ispell is automatically spawned on background for each entered words.
412 The default flyspell behavior is to highlight incorrect words.
413 With no argument, this command toggles Flyspell mode.
414 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
417 \\[ispell-word]: correct words (using Ispell).
418 \\[flyspell-auto-correct-word]: automatically correct word.
419 \\[flyspell-correct-word] (or mouse-2): popup correct words.
422 This runs `flyspell-mode-hook' after flyspell is entered.
425 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
426 valid. For instance, a personal dictionary can be used by
427 invoking `ispell-change-dictionary'.
429 Consider using the `ispell-parser' to check your text. For instance
431 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
434 \\[flyspell-region] checks all words inside a region.
435 \\[flyspell-buffer] checks the whole buffer."
437 (let ((old-flyspell-mode flyspell-mode))
438 ;; Mark the mode as on or off.
439 (setq flyspell-mode (not (or (and (null arg) flyspell-mode)
440 (<= (prefix-numeric-value arg) 0))))
442 (unless (eq flyspell-mode old-flyspell-mode)
446 ;; Force modeline redisplay.
447 (set-buffer-modified-p (buffer-modified-p)))))
449 ;*---------------------------------------------------------------------*/
451 ;*---------------------------------------------------------------------*/
453 (add-minor-mode 'flyspell-mode
454 'flyspell-mode-line-string
459 ;*---------------------------------------------------------------------*/
460 ;* flyspell-buffers ... */
461 ;* ------------------------------------------------------------- */
462 ;* For remembering buffers running flyspell */
463 ;*---------------------------------------------------------------------*/
464 (defvar flyspell-buffers nil)
466 ;*---------------------------------------------------------------------*/
467 ;* flyspell-minibuffer-p ... */
468 ;*---------------------------------------------------------------------*/
469 (defun flyspell-minibuffer-p (buffer)
470 "Is BUFFER a minibuffer?"
471 (let ((ws (get-buffer-window-list buffer t)))
472 (and (consp ws) (window-minibuffer-p (car ws)))))
474 ;*---------------------------------------------------------------------*/
475 ;* flyspell-accept-buffer-local-defs ... */
476 ;*---------------------------------------------------------------------*/
477 (defun flyspell-accept-buffer-local-defs ()
478 (ispell-accept-buffer-local-defs)
479 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
480 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
481 ;; the dictionary as changed
483 (setq flyspell-dash-dictionary ispell-dictionary)
484 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
485 (if (member (or ispell-local-dictionary ispell-dictionary)
486 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
487 (setq flyspell-consider-dash-as-word-delimiter-flag t)
488 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
490 ;*---------------------------------------------------------------------*/
491 ;* flyspell-mode-on ... */
492 ;*---------------------------------------------------------------------*/
493 (eval-when-compile (defvar flyspell-local-mouse-map))
495 (defun flyspell-mode-on ()
496 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
497 (setq ispell-highlight-face 'flyspell-incorrect-face)
498 ;; local dictionaries setup
499 (ispell-change-dictionary
500 (or ispell-local-dictionary ispell-dictionary flyspell-default-dictionary))
501 ;; we have to force ispell to accept the local definition or
502 ;; otherwise it could be too late, the local dictionary may
504 (flyspell-accept-buffer-local-defs)
505 ;; we put the `flyspel-delayed' property on some commands
506 (flyspell-delay-commands)
507 ;; we put the `flyspel-deplacement' property on some commands
508 (flyspell-deplacement-commands)
509 ;; we bound flyspell action to post-command hook
510 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
511 ;; we bound flyspell action to pre-command hook
512 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
513 ;; we bound flyspell action to after-change hook
514 (make-local-variable 'after-change-functions)
515 (setq after-change-functions
516 (cons 'flyspell-after-change-function after-change-functions))
517 ;; set flyspell-generic-check-word-p based on the major mode
518 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
520 (setq flyspell-generic-check-word-p mode-predicate)))
521 ;; the welcome message
522 (if (and flyspell-issue-welcome-flag (interactive-p))
523 (let ((binding (where-is-internal 'flyspell-auto-correct-word
527 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
528 (key-description binding))
529 "Welcome to flyspell. Use Mouse-2 to correct words."))))
531 ;; we end with the flyspell hooks
532 (run-hooks 'flyspell-mode-hook))
534 ;*---------------------------------------------------------------------*/
535 ;* flyspell-delay-commands ... */
536 ;*---------------------------------------------------------------------*/
537 (defun flyspell-delay-commands ()
538 "Install the standard set of Flyspell delayed commands."
539 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
540 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
542 ;*---------------------------------------------------------------------*/
543 ;* flyspell-delay-command ... */
544 ;*---------------------------------------------------------------------*/
545 (defun flyspell-delay-command (command)
546 "Set COMMAND to be delayed, for Flyspell.
547 When flyspell `post-command-hook' is invoked because a delayed command
548 as been used the current word is not immediately checked.
549 It will be checked only after `flyspell-delay' seconds."
550 (interactive "SDelay Flyspell after Command: ")
551 (put command 'flyspell-delayed t))
553 ;*---------------------------------------------------------------------*/
554 ;* flyspell-deplacement-commands ... */
555 ;*---------------------------------------------------------------------*/
556 (defun flyspell-deplacement-commands ()
557 "Install the standard set of Flyspell deplacement commands."
558 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
559 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
561 ;*---------------------------------------------------------------------*/
562 ;* flyspell-deplacement-command ... */
563 ;*---------------------------------------------------------------------*/
564 (defun flyspell-deplacement-command (command)
565 "Set COMMAND that implement cursor movements, for Flyspell.
566 When flyspell `post-command-hook' is invoked because of a deplacement command
567 as been used the current word is checked only if the previous command was
568 not the very same deplacement command."
569 (interactive "SDeplacement Flyspell after Command: ")
570 (put command 'flyspell-deplacement t))
572 ;*---------------------------------------------------------------------*/
573 ;* flyspell-word-cache ... */
574 ;*---------------------------------------------------------------------*/
575 (defvar flyspell-word-cache-start nil)
576 (defvar flyspell-word-cache-end nil)
577 (defvar flyspell-word-cache-word nil)
578 (make-variable-buffer-local 'flyspell-word-cache-start)
579 (make-variable-buffer-local 'flyspell-word-cache-end)
580 (make-variable-buffer-local 'flyspell-word-cache-word)
582 ;*---------------------------------------------------------------------*/
583 ;* The flyspell pre-hook, store the current position. In the */
584 ;* post command hook, we will check, if the word at this position */
585 ;* has to be spell checked. */
586 ;*---------------------------------------------------------------------*/
587 (defvar flyspell-pre-buffer nil)
588 (defvar flyspell-pre-point nil)
589 (defvar flyspell-pre-column nil)
590 (defvar flyspell-pre-pre-buffer nil)
591 (defvar flyspell-pre-pre-point nil)
593 ;*---------------------------------------------------------------------*/
594 ;* flyspell-previous-command ... */
595 ;*---------------------------------------------------------------------*/
596 (defvar flyspell-previous-command nil
597 "The last interactive command checked by Flyspell.")
599 ;*---------------------------------------------------------------------*/
600 ;* flyspell-pre-command-hook ... */
601 ;*---------------------------------------------------------------------*/
602 (defun flyspell-pre-command-hook ()
603 "Save the current buffer and point for Flyspell's post-command hook."
605 (setq flyspell-pre-buffer (current-buffer))
606 (setq flyspell-pre-point (point))
607 (setq flyspell-pre-column (current-column)))
609 ;*---------------------------------------------------------------------*/
610 ;* flyspell-mode-off ... */
611 ;*---------------------------------------------------------------------*/
613 (defun flyspell-mode-off ()
614 "Turn Flyspell mode off."
615 ;; we remove the hooks
616 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
617 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
618 (setq after-change-functions (delq 'flyspell-after-change-function
619 after-change-functions))
620 ;; we remove all the flyspell hilightings
621 (flyspell-delete-all-overlays)
622 ;; we have to erase pre cache variables
623 (setq flyspell-pre-buffer nil)
624 (setq flyspell-pre-point nil)
625 ;; we mark the mode as killed
626 (setq flyspell-mode nil))
628 ;*---------------------------------------------------------------------*/
629 ;* flyspell-check-pre-word-p ... */
630 ;*---------------------------------------------------------------------*/
631 (defun flyspell-check-pre-word-p ()
632 "Return non-nil if we should to check the word before point.
633 More precisely, it applies to the word that was before point
634 before the current command."
636 ((or (not (numberp flyspell-pre-point))
637 (not (bufferp flyspell-pre-buffer))
638 (not (buffer-live-p flyspell-pre-buffer)))
640 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
641 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
643 ((or (and (= flyspell-pre-point (- (point) 1))
644 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
645 (= flyspell-pre-point (point))
646 (= flyspell-pre-point (+ (point) 1)))
648 ((and (symbolp this-command)
649 (or (get this-command 'flyspell-delayed)
650 (and (get this-command 'flyspell-deplacement)
651 (eq flyspell-previous-command this-command)))
652 (or (= (current-column) 0)
653 (= (current-column) flyspell-pre-column)
654 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
656 ((not (eq (current-buffer) flyspell-pre-buffer))
658 ((not (and (numberp flyspell-word-cache-start)
659 (numberp flyspell-word-cache-end)))
662 (or (< flyspell-pre-point flyspell-word-cache-start)
663 (> flyspell-pre-point flyspell-word-cache-end)))))
665 ;*---------------------------------------------------------------------*/
666 ;* The flyspell after-change-hook, store the change position. In */
667 ;* the post command hook, we will check, if the word at this */
668 ;* position has to be spell checked. */
669 ;*---------------------------------------------------------------------*/
670 (defvar flyspell-changes nil)
672 ;*---------------------------------------------------------------------*/
673 ;* flyspell-after-change-function ... */
674 ;*---------------------------------------------------------------------*/
675 (defun flyspell-after-change-function (start stop len)
676 "Save the current buffer and point for Flyspell's post-command hook."
678 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
680 ;*---------------------------------------------------------------------*/
681 ;* flyspell-check-changed-word-p ... */
682 ;*---------------------------------------------------------------------*/
683 (defun flyspell-check-changed-word-p (start stop)
684 "Return t when the changed word has to be checked.
685 The answer depends of several criteria.
686 Mostly we check word delimiters."
688 ((and (eq (char-after start) ?\n) (> stop start))
690 ((not (numberp flyspell-pre-point))
692 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
694 ((let ((pos (point)))
695 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
700 ;*---------------------------------------------------------------------*/
701 ;* flyspell-check-word-p ... */
702 ;*---------------------------------------------------------------------*/
703 (defun flyspell-check-word-p ()
704 "Return t when the word at `point' has to be checked.
705 The answer depends of several criteria.
706 Mostly we check word delimiters."
708 ((<= (- (point-max) 1) (point-min))
709 ;; the buffer is not filled enough
711 ((and (and (> (current-column) 0)
712 (not (eq (current-column) flyspell-pre-column)))
715 (and (looking-at (flyspell-get-not-casechars))
716 (or flyspell-consider-dash-as-word-delimiter-flag
717 (not (looking-at "\\-"))))))
718 ;; yes because we have reached or typed a word delimiter.
720 ((symbolp this-command)
722 ((get this-command 'flyspell-deplacement)
723 (not (eq flyspell-previous-command this-command)))
724 ((get this-command 'flyspell-delayed)
725 ;; the current command is not delayed, that
726 ;; is that we must check the word now
727 (if (fboundp 'about-xemacs)
728 (sit-for flyspell-delay nil)
729 (sit-for flyspell-delay 0 nil)))
733 ;*---------------------------------------------------------------------*/
734 ;* flyspell-debug-signal-no-check ... */
735 ;*---------------------------------------------------------------------*/
736 (defun flyspell-debug-signal-no-check (msg obj)
737 (setq debug-on-error t)
739 (let ((buffer (get-buffer-create "*flyspell-debug*")))
742 (insert "NO-CHECK:\n")
743 (insert (format " %S : %S\n" msg obj)))))
745 ;*---------------------------------------------------------------------*/
746 ;* flyspell-debug-signal-pre-word-checked ... */
747 ;*---------------------------------------------------------------------*/
748 (defun flyspell-debug-signal-pre-word-checked ()
749 (setq debug-on-error t)
751 (let ((buffer (get-buffer-create "*flyspell-debug*")))
753 (insert "PRE-WORD:\n")
754 (insert (format " pre-point : %S\n" flyspell-pre-point))
755 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
756 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
757 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
758 (goto-char (point-max)))))
760 ;*---------------------------------------------------------------------*/
761 ;* flyspell-debug-signal-word-checked ... */
762 ;*---------------------------------------------------------------------*/
763 (defun flyspell-debug-signal-word-checked ()
764 (setq debug-on-error t)
766 (let ((oldbuf (current-buffer))
767 (buffer (get-buffer-create "*flyspell-debug*"))
771 (insert (format " this-cmd : %S\n" this-command))
772 (insert (format " delayed : %S\n" (and (symbolp this-command)
773 (get this-command 'flyspell-delayed))))
774 (insert (format " point : %S\n" point))
775 (insert (format " prev-char : [%c] %S\n"
778 (let ((c (if (> (point) (point-min))
781 (char-after (point)))
787 (let ((c (if (> (point) (point-min))
790 (and (and (looking-at (flyspell-get-not-casechars)) 1)
791 (and (or flyspell-consider-dash-as-word-delimiter-flag
792 (not (looking-at "\\-"))) 2))))))
795 (insert (format " because : %S\n"
797 ((not (and (symbolp this-command)
798 (get this-command 'flyspell-delayed)))
799 ;; the current command is not delayed, that
800 ;; is that we must check the word now
804 (let ((c (if (> (point) (point-min))
807 (and (looking-at (flyspell-get-not-casechars))
808 (or flyspell-consider-dash-as-word-delimiter-flag
809 (not (looking-at "\\-"))))))))
812 ;; yes because we have reached or typed a word delimiter.
814 ((not (integerp flyspell-delay))
815 ;; yes because the user had set up a no-delay configuration.
819 (goto-char (point-max)))))
821 ;*---------------------------------------------------------------------*/
822 ;* flyspell-debug-signal-changed-checked ... */
823 ;*---------------------------------------------------------------------*/
824 (defun flyspell-debug-signal-changed-checked ()
825 (setq debug-on-error t)
827 (let ((buffer (get-buffer-create "*flyspell-debug*"))
830 (insert "CHANGED WORD:\n")
831 (insert (format " point : %S\n" point))
832 (goto-char (point-max)))))
834 ;*---------------------------------------------------------------------*/
835 ;* flyspell-post-command-hook ... */
836 ;* ------------------------------------------------------------- */
837 ;* It is possible that we check several words: */
838 ;* 1- the current word is checked if the predicate */
839 ;* FLYSPELL-CHECK-WORD-P is true */
840 ;* 2- the word that used to be the current word before the */
841 ;* THIS-COMMAND is checked if: */
842 ;* a- the previous word is different from the current word */
843 ;* b- the previous word as not just been checked by the */
844 ;* previous FLYSPELL-POST-COMMAND-HOOK */
845 ;* 3- the words changed by the THIS-COMMAND that are neither the */
846 ;* previous word nor the current word */
847 ;*---------------------------------------------------------------------*/
848 (defun flyspell-post-command-hook ()
849 "The `post-command-hook' used by flyspell to check a word in-the-fly."
851 (let ((command this-command))
852 (if (flyspell-check-pre-word-p)
854 '(flyspell-debug-signal-pre-word-checked)
855 (set-buffer flyspell-pre-buffer)
857 (goto-char flyspell-pre-point)
859 (if (flyspell-check-word-p)
861 '(flyspell-debug-signal-word-checked)
863 ;; we remember which word we have just checked.
864 ;; this will be used next time we will check a word
865 ;; to compare the next current word with the word
866 ;; that as been registered in the pre-command-hook
867 ;; that is these variables are used within the predicate
868 ;; FLYSPELL-CHECK-PRE-WORD-P
869 (setq flyspell-pre-pre-buffer (current-buffer))
870 (setq flyspell-pre-pre-point (point)))
872 (setq flyspell-pre-pre-buffer nil)
873 (setq flyspell-pre-pre-point nil)
874 ;; when a word is not checked because of a delayed command
875 ;; we do not disable the ispell cache.
876 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
877 (setq flyspell-word-cache-end -1))))
878 (while (consp flyspell-changes)
879 (let ((start (car (car flyspell-changes)))
880 (stop (cdr (car flyspell-changes))))
881 (if (flyspell-check-changed-word-p start stop)
883 '(flyspell-debug-signal-changed-checked)
886 (setq flyspell-changes (cdr flyspell-changes))))
887 (setq flyspell-previous-command command)))
889 ;*---------------------------------------------------------------------*/
890 ;* flyspell-notify-misspell ... */
891 ;*---------------------------------------------------------------------*/
892 (defun flyspell-notify-misspell (start end word poss)
893 (let ((replacements (if (stringp poss)
895 (if flyspell-sort-corrections
896 (sort (car (cdr (cdr poss))) 'string<)
897 (car (cdr (cdr poss)))))))
898 (message (format "mispelling `%s' %S" word replacements))))
900 ;*---------------------------------------------------------------------*/
901 ;* flyspell-word ... */
902 ;*---------------------------------------------------------------------*/
903 (defun flyspell-word (&optional following)
904 "Spell check a word."
905 (interactive (list current-prefix-arg))
907 (setq following ispell-following-word))
909 ;; use the correct dictionary
910 (flyspell-accept-buffer-local-defs)
911 (let* ((cursor-location (point))
912 (flyspell-word (flyspell-get-word following))
914 (if (or (eq flyspell-word nil)
915 (and (fboundp flyspell-generic-check-word-p)
916 (not (funcall flyspell-generic-check-word-p))))
919 ;; destructure return flyspell-word info list.
920 (setq start (car (cdr flyspell-word))
921 end (car (cdr (cdr flyspell-word)))
922 word (car flyspell-word))
923 ;; before checking in the directory, we check for doublons.
925 ((and (or (not (eq ispell-parser 'tex))
926 (not (eq (char-after start) ?\\)))
927 flyspell-mark-duplications-flag
930 (word-search-backward word
934 ;; yes, this is a doublon
935 (flyspell-highlight-incorrect-region start end 'doublon))
936 ((and (eq flyspell-word-cache-start start)
937 (eq flyspell-word-cache-end end)
938 (string-equal flyspell-word-cache-word word))
939 ;; this word had been already checked, we skip
941 ((and (eq ispell-parser 'tex)
942 (flyspell-tex-command-p flyspell-word))
943 ;; this is a correct word (because a tex command)
944 (flyspell-unhighlight-at start)
946 (flyspell-unhighlight-at (- end 1)))
949 ;; we setup the cache
950 (setq flyspell-word-cache-start start)
951 (setq flyspell-word-cache-end end)
952 (setq flyspell-word-cache-word word)
953 ;; now check spelling of word.
954 (process-send-string ispell-process "%\n")
955 ;; put in verbose mode
956 (process-send-string ispell-process
957 (concat "^" word "\n"))
958 ;; we mark the ispell process so it can be killed
959 ;; when emacs is exited without query
960 (if (fboundp 'process-kill-without-query)
961 (process-kill-without-query ispell-process))
962 ;; wait until ispell has processed word
964 (accept-process-output ispell-process)
965 (not (string= "" (car ispell-filter)))))
966 ;; (process-send-string ispell-process "!\n")
967 ;; back to terse mode.
968 (setq ispell-filter (cdr ispell-filter))
969 (if (consp ispell-filter)
970 (setq poss (ispell-parse-output (car ispell-filter))))
973 (flyspell-unhighlight-at start)
975 (flyspell-unhighlight-at (- end 1)))
977 ((and (stringp poss) flyspell-highlight-flag)
979 (flyspell-unhighlight-at start)
981 (flyspell-unhighlight-at (- end 1)))
984 (flyspell-unhighlight-at start)
986 (flyspell-unhighlight-at (- end 1))))
987 ((or (and (< flyspell-duplicate-distance 0)
990 (word-search-backward word
995 (word-search-forward word
998 (and (> flyspell-duplicate-distance 0)
1001 (word-search-backward
1004 flyspell-duplicate-distance)
1008 (word-search-forward
1011 flyspell-duplicate-distance)
1013 (if flyspell-highlight-flag
1014 (flyspell-highlight-duplicate-region start end)
1015 (message (format "duplicate `%s'" word))))
1017 ;; incorrect highlight the location
1018 (if flyspell-highlight-flag
1019 (flyspell-highlight-incorrect-region start end poss)
1020 (flyspell-notify-misspell start end word poss))))
1021 ;; return to original location
1022 (goto-char cursor-location)
1023 (if ispell-quit (setq ispell-quit nil)))))))))
1025 ;*---------------------------------------------------------------------*/
1026 ;* flyspell-tex-math-initialized ... */
1027 ;*---------------------------------------------------------------------*/
1028 (defvar flyspell-tex-math-initialized nil)
1030 ;*---------------------------------------------------------------------*/
1031 ;* flyspell-math-tex-command-p ... */
1032 ;* ------------------------------------------------------------- */
1033 ;* This function uses the texmathp package to check if (point) */
1034 ;* is within a tex command. In order to avoid using */
1035 ;* condition-case each time we use the variable */
1036 ;* flyspell-tex-math-initialized to make a special case the first */
1037 ;* time that function is called. */
1038 ;*---------------------------------------------------------------------*/
1039 (defun flyspell-math-tex-command-p ()
1041 (flyspell-check-tex-math-command
1043 ((eq flyspell-tex-math-initialized t)
1045 ((eq flyspell-tex-math-initialized 'error)
1048 (setq flyspell-tex-math-initialized t)
1052 (setq flyspell-tex-math-initialized 'error)
1055 ;*---------------------------------------------------------------------*/
1056 ;* flyspell-tex-command-p ... */
1057 ;*---------------------------------------------------------------------*/
1058 (defun flyspell-tex-command-p (word)
1059 "Return t if WORD is a TeX command."
1061 (let ((b (car (cdr word))))
1062 (and (re-search-backward "\\\\" (- (point) 100) t)
1063 (or (= (match-end 0) b)
1064 (and (goto-char (match-end 0))
1065 (looking-at flyspell-tex-command-regexp)
1066 (>= (match-end 0) b))))))
1067 (flyspell-math-tex-command-p)))
1069 ;*---------------------------------------------------------------------*/
1070 ;* flyspell-casechars-cache ... */
1071 ;*---------------------------------------------------------------------*/
1072 (defvar flyspell-casechars-cache nil)
1073 (defvar flyspell-ispell-casechars-cache nil)
1074 (make-variable-buffer-local 'flyspell-casechars-cache)
1075 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1077 ;*---------------------------------------------------------------------*/
1078 ;* flyspell-get-casechars ... */
1079 ;*---------------------------------------------------------------------*/
1080 (defun flyspell-get-casechars ()
1081 "This function builds a string that is the regexp of word chars.
1082 In order to avoid one useless string construction,
1083 this function changes the last char of the `ispell-casechars' string."
1084 (let ((ispell-casechars (ispell-get-casechars)))
1086 ((eq ispell-parser 'tex)
1087 (setq flyspell-ispell-casechars-cache ispell-casechars)
1088 (setq flyspell-casechars-cache
1089 (concat (substring ispell-casechars
1091 (- (length ispell-casechars) 1))
1093 flyspell-casechars-cache)
1095 (setq flyspell-ispell-casechars-cache ispell-casechars)
1096 (setq flyspell-casechars-cache ispell-casechars)
1097 flyspell-casechars-cache))))
1099 ;*---------------------------------------------------------------------*/
1100 ;* flyspell-get-not-casechars-cache ... */
1101 ;*---------------------------------------------------------------------*/
1102 (defvar flyspell-not-casechars-cache nil)
1103 (defvar flyspell-ispell-not-casechars-cache nil)
1104 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1105 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1107 ;*---------------------------------------------------------------------*/
1108 ;* flyspell-get-not-casechars ... */
1109 ;*---------------------------------------------------------------------*/
1110 (defun flyspell-get-not-casechars ()
1111 "This function builds a string that is the regexp of non-word chars."
1112 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1114 ((eq ispell-parser 'tex)
1115 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1116 (setq flyspell-not-casechars-cache
1117 (concat (substring ispell-not-casechars
1119 (- (length ispell-not-casechars) 1))
1121 flyspell-not-casechars-cache)
1123 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1124 (setq flyspell-not-casechars-cache ispell-not-casechars)
1125 flyspell-not-casechars-cache))))
1127 ;*---------------------------------------------------------------------*/
1128 ;* flyspell-get-word ... */
1129 ;*---------------------------------------------------------------------*/
1130 (defun flyspell-get-word (following)
1131 "Return the word for spell-checking according to Ispell syntax.
1132 If argument FOLLOWING is non-nil or if `ispell-following-word'
1133 is non-nil when called interactively, then the following word
1134 \(rather than preceding\) is checked when the cursor is not over a word.
1135 Optional second argument contains other chars that can be included in word
1138 Word syntax described by `ispell-dictionary-alist' (which see)."
1139 (let* ((flyspell-casechars (flyspell-get-casechars))
1140 (flyspell-not-casechars (flyspell-get-not-casechars))
1141 (ispell-otherchars (ispell-get-otherchars))
1142 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1143 (word-regexp (if (string< "" ispell-otherchars)
1144 (concat flyspell-casechars
1150 (if ispell-many-otherchars-p
1152 (concat flyspell-casechars "+")))
1156 (if (not (looking-at flyspell-casechars))
1158 (re-search-forward flyspell-casechars (point-max) t)
1159 (re-search-backward flyspell-casechars (point-min) t)))
1160 ;; move to front of word
1161 (re-search-backward flyspell-not-casechars (point-min) 'start)
1163 (if (string< "" ispell-otherchars)
1164 (while (and (looking-at ispell-otherchars)
1166 (or (not did-it-once)
1167 ispell-many-otherchars-p)
1168 (not (eq pos (point))))
1170 (setq did-it-once t)
1172 (if (looking-at flyspell-casechars)
1173 (re-search-backward flyspell-not-casechars (point-min) 'move)
1174 (backward-char -1)))))
1175 ;; Now mark the word and save to string.
1176 (if (eq (re-search-forward word-regexp (point-max) t) nil)
1179 (setq start (match-beginning 0)
1181 word (buffer-substring-no-properties start end))
1182 (list word start end)))))
1184 ;*---------------------------------------------------------------------*/
1185 ;* flyspell-small-region ... */
1186 ;*---------------------------------------------------------------------*/
1187 (defun flyspell-small-region (beg end)
1188 "Flyspell text between BEG and END."
1196 (while (< (point) end)
1199 (message "Spell Checking...%d%%"
1200 (* 100 (/ (float (- (point) beg)) (- end beg))))
1202 (setq count (+ 1 count)))
1205 (let ((cur (point)))
1207 (if (and (< (point) end) (> (point) (+ cur 1)))
1208 (backward-char 1)))))
1210 (message "Spell Checking completed.")
1213 ;*---------------------------------------------------------------------*/
1214 ;* flyspell-external-ispell-process ... */
1215 ;*---------------------------------------------------------------------*/
1216 (defvar flyspell-external-ispell-process '()
1217 "The external Flyspell Ispell process.")
1219 ;*---------------------------------------------------------------------*/
1220 ;* flyspell-external-ispell-buffer ... */
1221 ;*---------------------------------------------------------------------*/
1222 (defvar flyspell-external-ispell-buffer '())
1223 (defvar flyspell-large-region-buffer '())
1224 (defvar flyspell-large-region-beg (point-min))
1225 (defvar flyspell-large-region-end (point-max))
1227 ;*---------------------------------------------------------------------*/
1228 ;* flyspell-external-point-words ... */
1229 ;*---------------------------------------------------------------------*/
1230 (defun flyspell-external-point-words ()
1231 (let ((buffer flyspell-external-ispell-buffer))
1233 (beginning-of-buffer)
1234 (let ((size (- flyspell-large-region-end flyspell-large-region-beg))
1235 (start flyspell-large-region-beg))
1236 ;; now we are done with ispell, we have to find the word in
1237 ;; the initial buffer
1238 (while (< (point) (- (point-max) 1))
1239 ;; we have to fetch the incorrect word
1240 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1241 (let ((word (match-string 1)))
1242 (goto-char (match-end 0))
1243 (set-buffer flyspell-large-region-buffer)
1244 (goto-char flyspell-large-region-beg)
1245 (message "Spell Checking...%d%% [%s]"
1246 (* 100 (/ (float (- (point) start)) size))
1248 (if (search-forward word flyspell-large-region-end t)
1250 (setq flyspell-large-region-beg (point))
1251 (goto-char (- (point) 1))
1253 (set-buffer buffer))
1254 (goto-char (point-max)))))
1256 (message "Spell Checking completed.")
1257 ;; ok, we are done with pointing out incorrect words, we just
1258 ;; have to kill the temporary buffer
1259 (kill-buffer flyspell-external-ispell-buffer)
1260 (setq flyspell-external-ispell-buffer nil)))
1262 ;*---------------------------------------------------------------------*/
1263 ;* flyspell-large-region ... */
1264 ;*---------------------------------------------------------------------*/
1265 (defun flyspell-large-region (beg end)
1266 (let* ((curbuf (current-buffer))
1267 (buffer (get-buffer-create "*flyspell-region*")))
1268 (setq flyspell-external-ispell-buffer buffer)
1269 (setq flyspell-large-region-buffer curbuf)
1270 (setq flyspell-large-region-beg beg)
1271 (setq flyspell-large-region-end end)
1274 ;; this is done, we can start checking...
1275 (message "Checking region...")
1277 (let ((c (apply 'call-process-region beg
1285 ;; Local dictionary becomes the global dictionary in use.
1286 (if ispell-local-dictionary
1287 (setq ispell-dictionary ispell-local-dictionary))
1288 (setq args (ispell-get-ispell-args))
1289 (if ispell-dictionary ; use specified dictionary
1291 (append (list "-d" ispell-dictionary) args)))
1292 (if ispell-personal-dictionary ; use specified pers dict
1297 ispell-personal-dictionary)))))
1298 (setq args (append args ispell-extra-args))
1301 (flyspell-external-point-words)
1302 (error "Can't check region...")))))
1304 ;*---------------------------------------------------------------------*/
1305 ;* flyspell-region ... */
1306 ;* ------------------------------------------------------------- */
1307 ;* Because `ispell -a' is too slow, it is not possible to use */
1308 ;* it on large region. Then, when ispell is invoked on a large */
1309 ;* text region, a new `ispell -l' process is spawned. The */
1310 ;* pointed out words are then searched in the region a checked with */
1311 ;* regular flyspell means. */
1312 ;*---------------------------------------------------------------------*/
1314 (defun flyspell-region (beg end)
1315 "Flyspell text between BEG and END."
1324 (if (> (- end beg) flyspell-large-region)
1325 (flyspell-large-region beg end)
1326 (flyspell-small-region beg end)))))
1328 ;*---------------------------------------------------------------------*/
1329 ;* flyspell-buffer ... */
1330 ;*---------------------------------------------------------------------*/
1332 (defun flyspell-buffer ()
1333 "Flyspell whole buffer."
1335 (flyspell-region (point-min) (point-max)))
1337 ;*---------------------------------------------------------------------*/
1338 ;* old next error position ... */
1339 ;*---------------------------------------------------------------------*/
1340 (defvar flyspell-old-buffer-error nil)
1341 (defvar flyspell-old-pos-error nil)
1343 ;*---------------------------------------------------------------------*/
1344 ;* flyspell-goto-next-error ... */
1345 ;*---------------------------------------------------------------------*/
1346 (defun flyspell-goto-next-error ()
1347 "Go to the next previously detected error.
1348 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1353 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1354 (eq pos flyspell-old-pos-error))
1356 (if (= flyspell-old-pos-error max)
1357 ;; goto beginning of buffer
1359 (message "Restarting from beginning of buffer")
1360 (goto-char (point-min)))
1362 (setq pos (point))))
1363 ;; seek the next error
1364 (while (and (< pos max)
1365 (let ((ovs (overlays-at pos))
1367 (while (and (not r) (consp ovs))
1368 (if (flyspell-overlay-p (car ovs))
1370 (setq ovs (cdr ovs))))
1372 (setq pos (1+ pos)))
1373 ;; save the current location for next invocation
1374 (setq flyspell-old-pos-error pos)
1375 (setq flyspell-old-buffer-error (current-buffer))
1378 (message "No more miss-spelled word!"))))
1380 ;*---------------------------------------------------------------------*/
1381 ;* flyspell-overlay-p ... */
1382 ;*---------------------------------------------------------------------*/
1383 (defun flyspell-overlay-p (o)
1384 "A predicate that return true iff O is an overlay used by flyspell."
1385 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1387 ;*---------------------------------------------------------------------*/
1388 ;* flyspell-delete-all-overlays ... */
1389 ;* ------------------------------------------------------------- */
1390 ;* Remove all the overlays introduced by flyspell. */
1391 ;*---------------------------------------------------------------------*/
1392 (defun flyspell-delete-all-overlays ()
1393 "Delete all the overlays used by flyspell."
1394 (let ((l (overlays-in (point-min) (point-max))))
1397 (if (flyspell-overlay-p (car l))
1398 (delete-overlay (car l)))
1399 (setq l (cdr l))))))
1401 ;*---------------------------------------------------------------------*/
1402 ;* flyspell-unhighlight-at ... */
1403 ;*---------------------------------------------------------------------*/
1404 (defun flyspell-unhighlight-at (pos)
1405 "Remove the flyspell overlay that are located at POS."
1406 (if flyspell-persistent-highlight
1407 (let ((overlays (overlays-at pos)))
1408 (while (consp overlays)
1409 (if (flyspell-overlay-p (car overlays))
1410 (delete-overlay (car overlays)))
1411 (setq overlays (cdr overlays))))
1412 (if (flyspell-overlay-p flyspell-overlay)
1413 (delete-overlay flyspell-overlay))))
1415 ;*---------------------------------------------------------------------*/
1416 ;* flyspell-properties-at-p ... */
1417 ;* ------------------------------------------------------------- */
1418 ;* Is there an highlight properties at position pos? */
1419 ;*---------------------------------------------------------------------*/
1420 (defun flyspell-properties-at-p (pos)
1421 "Return t if there is a text property at POS, not counting `local-map'.
1422 If variable `flyspell-highlight-properties' is set to nil,
1423 text with properties are not checked. This function is used to discover
1424 if the character at POS has any other property."
1425 (let ((prop (text-properties-at pos))
1427 (while (and keep (consp prop))
1428 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1429 (setq prop (cdr (cdr prop)))
1433 ;*---------------------------------------------------------------------*/
1434 ;* make-flyspell-overlay ... */
1435 ;*---------------------------------------------------------------------*/
1436 (defun make-flyspell-overlay (beg end face mouse-face)
1437 "Allocate an overlay to highlight an incorrect word.
1438 BEG and END specify the range in the buffer of that word.
1439 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1441 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1442 (overlay-put flyspell-overlay 'face face)
1443 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1444 (overlay-put flyspell-overlay 'flyspell-overlay t)
1445 (if flyspell-use-local-map
1446 (overlay-put flyspell-overlay
1447 flyspell-overlay-keymap-property-name
1448 flyspell-mouse-map))
1451 ;*---------------------------------------------------------------------*/
1452 ;* flyspell-highlight-incorrect-region ... */
1453 ;*---------------------------------------------------------------------*/
1454 (defun flyspell-highlight-incorrect-region (beg end poss)
1455 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1456 (unless (run-hook-with-args-until-success
1457 'flyspell-incorrect-hook beg end poss)
1458 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1460 ;; we cleanup current overlay at the same position
1461 (if (and (not flyspell-persistent-highlight)
1462 (overlayp flyspell-overlay))
1463 (delete-overlay flyspell-overlay)
1464 (let ((overlays (overlays-at beg)))
1465 (while (consp overlays)
1466 (if (flyspell-overlay-p (car overlays))
1467 (delete-overlay (car overlays)))
1468 (setq overlays (cdr overlays)))))
1469 ;; now we can use a new overlay
1470 (setq flyspell-overlay
1471 (make-flyspell-overlay beg end
1472 'flyspell-incorrect-face 'highlight))))))
1474 ;*---------------------------------------------------------------------*/
1475 ;* flyspell-highlight-duplicate-region ... */
1476 ;*---------------------------------------------------------------------*/
1477 (defun flyspell-highlight-duplicate-region (beg end)
1478 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
1479 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1481 ;; we cleanup current overlay at the same position
1482 (if (and (not flyspell-persistent-highlight)
1483 (overlayp flyspell-overlay))
1484 (delete-overlay flyspell-overlay)
1485 (let ((overlays (overlays-at beg)))
1486 (while (consp overlays)
1487 (if (flyspell-overlay-p (car overlays))
1488 (delete-overlay (car overlays)))
1489 (setq overlays (cdr overlays)))))
1490 ;; now we can use a new overlay
1491 (setq flyspell-overlay
1492 (make-flyspell-overlay beg end
1493 'flyspell-duplicate-face 'highlight)))))
1495 ;*---------------------------------------------------------------------*/
1496 ;* flyspell-auto-correct-cache ... */
1497 ;*---------------------------------------------------------------------*/
1498 (defvar flyspell-auto-correct-pos nil)
1499 (defvar flyspell-auto-correct-region nil)
1500 (defvar flyspell-auto-correct-ring nil)
1501 (defvar flyspell-auto-correct-word nil)
1502 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1503 (make-variable-buffer-local 'flyspell-auto-correct-region)
1504 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1505 (make-variable-buffer-local 'flyspell-auto-correct-word)
1507 ;*---------------------------------------------------------------------*/
1508 ;* flyspell-check-previous-highlighted-word ... */
1509 ;*---------------------------------------------------------------------*/
1510 (defun flyspell-check-previous-highlighted-word (&optional arg)
1511 "Correct the closer misspelled word.
1512 This function scans a mis-spelled word before the cursor. If it finds one
1513 it proposes replacement for that word. With prefix arg, count that many
1514 misspelled words backwards."
1516 (let ((pos1 (point))
1518 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1521 (while (and (setq pos (previous-overlay-change pos))
1524 (if (> pos (point-min))
1526 (setq ovs (overlays-at (1- pos)))
1529 (setq ovs (cdr ovs))
1530 (if (and (overlay-get ov 'flyspell-overlay)
1531 (= 0 (setq arg (1- arg))))
1532 (throw 'exit t)))))))
1536 (error "No word to correct before point"))))
1538 ;*---------------------------------------------------------------------*/
1539 ;* flyspell-display-next-corrections ... */
1540 ;*---------------------------------------------------------------------*/
1541 (defun flyspell-display-next-corrections (corrections)
1542 (let ((string "Corrections:")
1545 (while (< (length string) 80)
1546 (if (equal (car l) flyspell-auto-correct-word)
1547 (setq pos (cons (+ 1 (length string)) pos)))
1548 (setq string (concat string " " (car l)))
1551 (let ((num (car pos)))
1552 (put-text-property num
1553 (+ num (length flyspell-auto-correct-word))
1555 'flyspell-incorrect-face
1557 (setq pos (cdr pos)))
1558 (if (fboundp 'display-message)
1559 (display-message 'no-log string)
1562 ;*---------------------------------------------------------------------*/
1563 ;* flyspell-abbrev-table ... */
1564 ;*---------------------------------------------------------------------*/
1565 (defun flyspell-abbrev-table ()
1566 (if flyspell-use-global-abbrev-table-p
1568 local-abbrev-table))
1570 ;*---------------------------------------------------------------------*/
1571 ;* flyspell-auto-correct-word ... */
1572 ;*---------------------------------------------------------------------*/
1573 (defun flyspell-auto-correct-word ()
1574 "Correct the current word.
1575 This command proposes various successive corrections for the current word."
1578 (old-max (point-max)))
1579 ;; use the correct dictionary
1580 (flyspell-accept-buffer-local-defs)
1581 (if (and (eq flyspell-auto-correct-pos pos)
1582 (consp flyspell-auto-correct-region))
1583 ;; we have already been using the function at the same location
1584 (let* ((start (car flyspell-auto-correct-region))
1585 (len (cdr flyspell-auto-correct-region)))
1586 (delete-region start (+ start len))
1587 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1588 (let* ((word (car flyspell-auto-correct-ring))
1589 (len (length word)))
1590 (rplacd flyspell-auto-correct-region len)
1592 (if flyspell-abbrev-p
1593 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1594 flyspell-auto-correct-word)
1595 (flyspell-change-abbrev (flyspell-abbrev-table)
1596 flyspell-auto-correct-word
1598 (define-abbrev (flyspell-abbrev-table)
1599 flyspell-auto-correct-word word)))
1602 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1603 (flyspell-ajust-cursor-point pos (point) old-max)
1604 (setq flyspell-auto-correct-pos (point)))
1605 ;; fetch the word to be checked
1606 (let ((word (flyspell-get-word nil))
1608 ;; destructure return word info list.
1609 (setq start (car (cdr word))
1610 end (car (cdr (cdr word)))
1612 (setq flyspell-auto-correct-word word)
1613 ;; now check spelling of word.
1614 (process-send-string ispell-process "%\n") ;put in verbose mode
1615 (process-send-string ispell-process (concat "^" word "\n"))
1616 ;; wait until ispell has processed word
1618 (accept-process-output ispell-process)
1619 (not (string= "" (car ispell-filter)))))
1620 (setq ispell-filter (cdr ispell-filter))
1621 (if (consp ispell-filter)
1622 (setq poss (ispell-parse-output (car ispell-filter))))
1623 (cond ((or (eq poss t) (stringp poss))
1624 ;; don't correct word
1628 (error "Ispell: error in Ispell process"))
1630 ;; the word is incorrect, we have to propose a replacement
1631 (let ((replacements (if flyspell-sort-corrections
1632 (sort (car (cdr (cdr poss))) 'string<)
1633 (car (cdr (cdr poss))))))
1634 (setq flyspell-auto-correct-region nil)
1635 (if (consp replacements)
1637 (let ((replace (car replacements)))
1638 (let ((new-word replace))
1639 (if (not (equal new-word (car poss)))
1641 ;; the save the current replacements
1642 (setq flyspell-auto-correct-region
1643 (cons start (length new-word)))
1644 (let ((l replacements))
1645 (while (consp (cdr l))
1647 (rplacd l (cons (car poss) replacements)))
1648 (setq flyspell-auto-correct-ring
1650 (delete-region start end)
1652 (if flyspell-abbrev-p
1653 (if (flyspell-already-abbrevp
1654 (flyspell-abbrev-table) word)
1655 (flyspell-change-abbrev
1656 (flyspell-abbrev-table)
1659 (define-abbrev (flyspell-abbrev-table)
1662 (flyspell-display-next-corrections
1663 (cons new-word flyspell-auto-correct-ring))
1664 (flyspell-ajust-cursor-point pos
1667 (setq flyspell-auto-correct-pos (point))
1668 (ispell-pdict-save t)))))
1670 ;*---------------------------------------------------------------------*/
1671 ;* flyspell-correct-word ... */
1672 ;*---------------------------------------------------------------------*/
1673 (defun flyspell-correct-word (event)
1674 "Pop up a menu of possible corrections for a misspelled word.
1675 The word checked is the word at the mouse position."
1677 ;; use the correct dictionary
1678 (flyspell-accept-buffer-local-defs)
1679 ;; retain cursor location (I don't know why but save-excursion here fails).
1680 (let ((save (point)))
1681 (mouse-set-point event)
1682 (let ((cursor-location (point))
1683 (word (flyspell-get-word nil))
1684 start end poss replace)
1685 ;; destructure return word info list.
1686 (setq start (car (cdr word))
1687 end (car (cdr (cdr word)))
1689 ;; now check spelling of word.
1690 (process-send-string ispell-process "%\n") ;put in verbose mode
1691 (process-send-string ispell-process (concat "^" word "\n"))
1692 ;; wait until ispell has processed word
1694 (accept-process-output ispell-process)
1695 (not (string= "" (car ispell-filter)))))
1696 (setq ispell-filter (cdr ispell-filter))
1697 (if (consp ispell-filter)
1698 (setq poss (ispell-parse-output (car ispell-filter))))
1699 (cond ((or (eq poss t) (stringp poss))
1700 ;; don't correct word
1704 (error "Ispell: error in Ispell process"))
1705 ((string-match "GNU" (emacs-version))
1706 ;; the word is incorrect, we have to propose a replacement
1707 (setq replace (flyspell-emacs-popup event poss word))
1708 (cond ((eq replace 'ignore)
1713 (process-send-string ispell-process (concat "*" word "\n"))
1714 (flyspell-unhighlight-at cursor-location)
1715 (setq ispell-pdict-modified-p '(t)))
1716 ((or (eq replace 'buffer) (eq replace 'session))
1717 (process-send-string ispell-process (concat "@" word "\n"))
1718 (if (null ispell-pdict-modified-p)
1719 (setq ispell-pdict-modified-p
1720 (list ispell-pdict-modified-p)))
1721 (flyspell-unhighlight-at cursor-location)
1723 (if (eq replace 'buffer)
1724 (ispell-add-per-file-word-list word)))
1726 (let ((new-word (if (atom replace)
1729 (cursor-location (+ (- (length word) (- end start))
1731 (if (not (equal new-word (car poss)))
1732 (let ((old-max (point-max)))
1733 (delete-region start end)
1735 (if flyspell-abbrev-p
1736 (define-abbrev (flyspell-abbrev-table)
1739 (flyspell-ajust-cursor-point save
1745 ((eq flyspell-emacs 'xemacs)
1746 (flyspell-xemacs-popup
1747 event poss word cursor-location start end save)
1749 (ispell-pdict-save t))))
1751 ;*---------------------------------------------------------------------*/
1752 ;* flyspell-xemacs-correct ... */
1753 ;*---------------------------------------------------------------------*/
1754 (defun flyspell-xemacs-correct (replace poss word cursor-location start end save)
1755 "The xemacs popup menu callback."
1756 (cond ((eq replace 'ignore)
1759 (process-send-string ispell-process (concat "*" word "\n"))
1760 (process-send-string ispell-process "#\n")
1761 (flyspell-unhighlight-at cursor-location)
1762 (setq ispell-pdict-modified-p '(t)))
1763 ((or (eq replace 'buffer) (eq replace 'session))
1764 (process-send-string ispell-process (concat "@" word "\n"))
1765 (flyspell-unhighlight-at cursor-location)
1766 (if (null ispell-pdict-modified-p)
1767 (setq ispell-pdict-modified-p
1768 (list ispell-pdict-modified-p)))
1769 (if (eq replace 'buffer)
1770 (ispell-add-per-file-word-list word)))
1772 (let ((old-max (point-max))
1773 (new-word (if (atom replace)
1776 (cursor-location (+ (- (length word) (- end start))
1778 (if (not (equal new-word (car poss)))
1780 (delete-region start end)
1783 (if flyspell-abbrev-p
1784 (define-abbrev (flyspell-abbrev-table)
1787 (flyspell-ajust-cursor-point save cursor-location old-max)))))
1789 ;*---------------------------------------------------------------------*/
1790 ;* flyspell-ajust-cursor-point ... */
1791 ;*---------------------------------------------------------------------*/
1792 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
1793 (if (>= save cursor-location)
1794 (let ((new-pos (+ save (- (point-max) old-max))))
1796 ((< new-pos (point-min))
1798 ((> new-pos (point-max))
1803 ;*---------------------------------------------------------------------*/
1804 ;* flyspell-emacs-popup ... */
1805 ;*---------------------------------------------------------------------*/
1806 (defun flyspell-emacs-popup (event poss word)
1807 "The Emacs popup menu."
1809 (let* ((mouse-pos (mouse-position))
1810 (mouse-pos (if (nth 1 mouse-pos)
1812 (set-mouse-position (car mouse-pos)
1813 (/ (frame-width) 2) 2)
1816 (setq event (list (list (car (cdr mouse-pos))
1817 (1+ (cdr (cdr mouse-pos))))
1819 (let* ((corrects (if flyspell-sort-corrections
1820 (sort (car (cdr (cdr poss))) 'string<)
1821 (car (cdr (cdr poss)))))
1822 (cor-menu (if (consp corrects)
1823 (mapcar (lambda (correct)
1824 (list correct correct))
1827 (affix (car (cdr (cdr (cdr poss)))))
1828 (base-menu (let ((save (if (consp affix)
1830 (list (concat "Save affix: " (car affix))
1832 '("Accept (session)" accept)
1833 '("Accept (buffer)" buffer))
1834 '(("Save word" save)
1835 ("Accept (session)" session)
1836 ("Accept (buffer)" buffer)))))
1837 (if (consp cor-menu)
1838 (append cor-menu (cons "" save))
1840 (menu (cons "flyspell correction menu" base-menu)))
1841 (car (x-popup-menu event
1842 (list (format "%s [%s]" word (or ispell-local-dictionary
1846 ;*---------------------------------------------------------------------*/
1847 ;* flyspell-xemacs-popup ... */
1848 ;*---------------------------------------------------------------------*/
1849 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
1850 "The XEmacs popup menu."
1851 (let* ((corrects (if flyspell-sort-corrections
1852 (sort (car (cdr (cdr poss))) 'string<)
1853 (car (cdr (cdr poss)))))
1854 (cor-menu (if (consp corrects)
1855 (mapcar (lambda (correct)
1857 (list 'flyspell-xemacs-correct
1868 (affix (car (cdr (cdr (cdr poss)))))
1869 (menu (let ((save (if (consp affix)
1871 (concat "Save affix: " (car affix))
1872 (list 'flyspell-xemacs-correct
1883 (list 'flyspell-xemacs-correct
1892 (session (vector "Accept (session)"
1893 (list 'flyspell-xemacs-correct
1902 (buffer (vector "Accept (buffer)"
1903 (list 'flyspell-xemacs-correct
1912 (if (consp cor-menu)
1913 (append cor-menu (list "-" save session buffer))
1914 (list save session buffer)))))
1915 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
1919 ;*---------------------------------------------------------------------*/
1920 ;* Some example functions for real autocorrecting */
1921 ;*---------------------------------------------------------------------*/
1923 (defun flyspell-maybe-correct-transposition (beg end poss)
1924 "Check replacements for transposed characters.
1926 If the text between BEG and END is equal to a correction suggested by
1927 Ispell, after transposing two adjacent characters, correct the text,
1930 The third arg POSS is either the symbol 'doublon' or a list of
1931 possible corrections as returned by 'ispell-parse-output'.
1933 This function is meant to be added to 'flyspell-incorrect-hook'."
1935 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
1938 (copy-to-buffer temp-buffer beg end)
1939 (set-buffer temp-buffer)
1940 (goto-char (1+ (point-min)))
1941 (while (and (not (eobp)) (not found))
1943 (if (member (buffer-string) (nth 2 poss))
1944 (setq found (point))
1945 (transpose-chars -1)
1949 (goto-char (+ beg found -1))
1950 (transpose-chars -1)
1953 (defun flyspell-maybe-correct-doubling (beg end poss)
1954 "Check replacements for doubled characters.
1956 If the text between BEG and END is equal to a correction suggested by
1957 Ispell, after removing a pair of doubled characters, correct the text,
1960 The third arg POSS is either the symbol 'doublon' or a list of
1961 possible corrections as returned by 'ispell-parse-output'.
1963 This function is meant to be added to 'flyspell-incorrect-hook'."
1965 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
1968 (copy-to-buffer temp-buffer beg end)
1969 (set-buffer temp-buffer)
1970 (goto-char (1+ (point-min)))
1971 (while (and (not (eobp)) (not found))
1972 (when (char-equal (char-after) (char-before))
1974 (if (member (buffer-string) (nth 2 poss))
1975 (setq found (point))
1976 (insert-char (char-before) 1)))
1980 (goto-char (+ beg found -1))
1984 ;*---------------------------------------------------------------------*/
1985 ;* flyspell-already-abbrevp ... */
1986 ;*---------------------------------------------------------------------*/
1987 (defun flyspell-already-abbrevp (table word)
1988 (let ((sym (abbrev-symbol word table)))
1989 (and sym (symbolp sym))))
1991 ;*---------------------------------------------------------------------*/
1992 ;* flyspell-change-abbrev ... */
1993 ;*---------------------------------------------------------------------*/
1994 (defun flyspell-change-abbrev (table old new)
1995 (set (abbrev-symbol old table) new))
1999 ;;; flyspell.el ends here