1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
8 ;; Keywords: convenience
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
32 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
33 ;; This applies only to the current buffer.
35 ;; To enable Flyspell in text representing computer programs, type
36 ;; M-x flyspell-prog-mode.
37 ;; In that mode only text inside comments is checked.
39 ;; Note: consider setting the variable ispell-parser to `tex' to
40 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
42 ;; Some user variables control the behavior of flyspell. They are
43 ;; those defined under the `User variables' comment.
49 ;;*---------------------------------------------------------------------*/
51 ;;*---------------------------------------------------------------------*/
52 (defgroup flyspell nil
53 "Spell checking on the fly."
59 ;;*---------------------------------------------------------------------*/
60 ;;* User configuration ... */
61 ;;*---------------------------------------------------------------------*/
62 (defcustom flyspell-highlight-flag t
63 "How Flyspell should indicate misspelled words.
64 Non-nil means use highlight, nil means use minibuffer messages."
68 (defcustom flyspell-mark-duplications-flag t
69 "Non-nil means Flyspell reports a repeated word as an error.
70 Detection of repeated words is not implemented in
71 \"large\" regions; see `flyspell-large-region'."
75 (defcustom flyspell-sort-corrections nil
76 "Non-nil means, sort the corrections alphabetically before popping them."
81 (defcustom flyspell-duplicate-distance -
1
82 "The maximum distance for finding duplicates of unrecognized words.
83 This applies to the feature that when a word is not found in the dictionary,
84 if the same spelling occurs elsewhere in the buffer,
85 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
86 This variable specifies how far to search to find such a duplicate.
87 -1 means no limit (search the whole buffer).
88 0 means do not search for duplicate unrecognized spellings."
93 (defcustom flyspell-delay
3
94 "The number of seconds to wait before checking, after a \"delayed\" command."
98 (defcustom flyspell-persistent-highlight t
99 "Non-nil means misspelled words remain highlighted until corrected.
100 If this variable is nil, only the most recently detected misspelled word
105 (defcustom flyspell-highlight-properties t
106 "Non-nil means highlight incorrect words even if a property exists for this word."
110 (defcustom flyspell-default-delayed-commands
111 '(self-insert-command
113 backward-or-forward-delete-char
115 scrollbar-vertical-drag
116 backward-delete-char-untabify
)
117 "The standard list of delayed commands for Flyspell.
118 See `flyspell-delayed-commands'."
121 :type
'(repeat (symbol)))
123 (defcustom flyspell-delayed-commands nil
124 "List of commands that are \"delayed\" for Flyspell mode.
125 After these commands, Flyspell checking is delayed for a short time,
126 whose length is specified by `flyspell-delay'."
128 :type
'(repeat (symbol)))
130 (defcustom flyspell-default-deplacement-commands
135 "The standard list of deplacement commands for Flyspell.
136 See `flyspell-deplacement-commands'."
139 :type
'(repeat (symbol)))
141 (defcustom flyspell-deplacement-commands nil
142 "List of commands that are \"deplacement\" for Flyspell mode.
143 After these commands, Flyspell checking is performed only if the previous
144 command was not the very same command."
147 :type
'(repeat (symbol)))
149 (defcustom flyspell-issue-welcome-flag t
150 "Non-nil means that Flyspell should display a welcome message when started."
154 (defcustom flyspell-issue-message-flag t
155 "Non-nil means that Flyspell emits messages when checking words."
159 (defcustom flyspell-incorrect-hook nil
160 "List of functions to be called when incorrect words are encountered.
161 Each function is given three arguments. The first two
162 arguments are the beginning and the end of the incorrect region.
163 The third is either the symbol `doublon' or the list
164 of possible corrections as returned by `ispell-parse-output'.
166 If any of the functions return non-nil, the word is not highlighted as
172 (defcustom flyspell-default-dictionary nil
173 "A string that is the name of the default dictionary.
174 This is passed to the `ispell-change-dictionary' when flyspell is started.
175 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
176 when flyspell is started, the value of that variable is used instead
177 of `flyspell-default-dictionary' to select the default dictionary.
178 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
179 Ispell's ultimate default dictionary."
182 :type
'(choice string
(const :tag
"Default" nil
)))
184 (defcustom flyspell-tex-command-regexp
185 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
186 "A string that is the regular expression that matches TeX commands."
191 (defcustom flyspell-check-tex-math-command nil
192 "Non nil means check even inside TeX math environment.
193 TeX math environments are discovered by the TEXMATHP that implemented
194 inside the texmathp.el Emacs package. That package may be found at:
195 http://strw.leidenuniv.nl/~dominik/Tools"
199 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
200 '("francais" "deutsch8" "norsk")
201 "List of dictionary names that consider `-' as word delimiter."
204 :type
'(repeat (string)))
206 (defcustom flyspell-abbrev-p
208 "If non-nil, add correction to abbreviation table."
213 (defcustom flyspell-use-global-abbrev-table-p
215 "If non-nil, prefer global abbrev table to local abbrev table."
220 (defcustom flyspell-mode-line-string
" Fly"
221 "String displayed on the modeline when flyspell is active.
222 Set this to nil if you don't want a modeline indicator."
224 :type
'(choice string
(const :tag
"None" nil
)))
226 (defcustom flyspell-large-region
1000
227 "The threshold that determines if a region is small.
228 If the region is smaller than this number of characters,
229 `flyspell-region' checks the words sequentially using regular
230 flyspell methods. Else, if the region is large, a new Ispell process is
233 Doubled words are not detected in a large region, because Ispell
234 does not check for them.
236 If `flyspell-large-region' is nil, all regions are treated as small."
239 :type
'(choice number
(const :tag
"All small" nil
)))
241 (defcustom flyspell-insert-function
(function insert
)
242 "Function for inserting word by flyspell upon correction."
246 (defcustom flyspell-before-incorrect-word-string nil
247 "String used to indicate an incorrect word starting."
249 :type
'(choice string
(const nil
)))
251 (defcustom flyspell-after-incorrect-word-string nil
252 "String used to indicate an incorrect word ending."
254 :type
'(choice string
(const nil
)))
256 (defcustom flyspell-use-meta-tab t
257 "Non-nil means that flyspell uses M-TAB to correct word."
261 (defcustom flyspell-auto-correct-binding
263 "The key binding for flyspell auto correction."
266 ;;*---------------------------------------------------------------------*/
267 ;;* Mode specific options */
268 ;;* ------------------------------------------------------------- */
269 ;;* Mode specific options enable users to disable flyspell on */
270 ;;* certain word depending of the emacs mode. For instance, when */
271 ;;* using flyspell with mail-mode add the following expression */
272 ;;* in your .emacs file: */
273 ;;* (add-hook 'mail-mode */
274 ;;* '(lambda () (setq flyspell-generic-check-word-p */
275 ;;* 'mail-mode-flyspell-verify))) */
276 ;;*---------------------------------------------------------------------*/
277 (defvar flyspell-generic-check-word-p nil
278 "Function providing per-mode customization over which words are flyspelled.
279 Returns t to continue checking, nil otherwise.
280 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
281 property of the major mode name.")
282 (make-variable-buffer-local 'flyspell-generic-check-word-p
)
284 ;;*--- mail mode -------------------------------------------------------*/
285 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
286 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
287 (defun mail-mode-flyspell-verify ()
288 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
289 (let ((header-end (save-excursion
290 (goto-char (point-min))
293 (regexp-quote mail-header-separator
)
297 (signature-begin (save-excursion
298 (goto-char (point-max))
299 (re-search-backward message-signature-separator
302 (cond ((< (point) header-end
)
303 (and (save-excursion (beginning-of-line)
304 (looking-at "^Subject:"))
305 (> (point) (match-end 0))))
306 ((> (point) signature-begin
)
311 (not (looking-at "[>}|]\\|To:")))))))
313 ;;*--- texinfo mode ----------------------------------------------------*/
314 (put 'texinfo-mode
'flyspell-mode-predicate
'texinfo-mode-flyspell-verify
)
315 (defun texinfo-mode-flyspell-verify ()
316 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
319 (not (looking-at "@"))))
321 ;;*--- tex mode --------------------------------------------------------*/
322 (put 'tex-mode
'flyspell-mode-predicate
'tex-mode-flyspell-verify
)
323 (defun tex-mode-flyspell-verify ()
324 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
327 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t
)))
329 (let ((this (point-marker))
330 (e (progn (end-of-line) (point-marker))))
332 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t
)
333 (and (>= this
(match-beginning 0))
334 (<= this
(match-end 0)) )))))))
336 ;;*--- sgml mode -------------------------------------------------------*/
337 (put 'sgml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
338 (put 'html-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
340 (defun sgml-mode-flyspell-verify ()
341 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
343 (let ((this (point-marker))
344 (s (progn (beginning-of-line) (point-marker)))
345 (e (progn (end-of-line) (point-marker))))
348 (and (re-search-forward "[^<]*>" e t
)
349 (= (match-beginning 0) this
)))
352 (and (re-search-backward "<[^>]*" s t
)
353 (= (match-end 0) this
)))
356 (and (re-search-forward "[^&]*;" e t
)
357 (= (match-beginning 0) this
)))
360 (and (re-search-backward "&[^;]*" s t
)
361 (= (match-end 0) this
)))))))))
363 ;;*---------------------------------------------------------------------*/
364 ;;* Programming mode */
365 ;;*---------------------------------------------------------------------*/
366 (defvar flyspell-prog-text-faces
367 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
368 "Faces corresponding to text in programming-mode buffers.")
370 (defun flyspell-generic-progmode-verify ()
371 "Used for `flyspell-generic-check-word-p' in programming modes."
372 (let ((f (get-text-property (point) 'face
)))
373 (memq f flyspell-prog-text-faces
)))
376 (defun flyspell-prog-mode ()
377 "Turn on `flyspell-mode' for comments and strings."
379 (setq flyspell-generic-check-word-p
'flyspell-generic-progmode-verify
)
381 (run-hooks 'flyspell-prog-mode-hook
))
383 ;;*---------------------------------------------------------------------*/
384 ;;* Overlay compatibility */
385 ;;*---------------------------------------------------------------------*/
386 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
387 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
388 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
389 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
390 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
391 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
392 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
393 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
395 ;;*---------------------------------------------------------------------*/
396 ;;* The minor mode declaration. */
397 ;;*---------------------------------------------------------------------*/
398 (defvar flyspell-mouse-map
399 (let ((map (make-sparse-keymap)))
400 (define-key map
(if (featurep 'xemacs
) [button2] [down-mouse-2])
401 #'flyspell-correct-word)
403 "Keymap for Flyspell to put on erroneous words.")
405 (defvar flyspell-mode-map
406 (let ((map (make-sparse-keymap)))
407 (if flyspell-use-meta-tab
408 (define-key map "\M-\t" 'flyspell-auto-correct-word))
409 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
410 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
411 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
413 "Minor mode keymap for Flyspell mode--for the whole buffer.")
415 ;; dash character machinery
416 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
417 "*Non-nil means that the `-' char is considered as a word delimiter.")
418 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
419 (defvar flyspell-dash-dictionary nil)
420 (make-variable-buffer-local 'flyspell-dash-dictionary)
421 (defvar flyspell-dash-local-dictionary nil)
422 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
424 ;;*---------------------------------------------------------------------*/
426 ;;*---------------------------------------------------------------------*/
427 (defface flyspell-incorrect
428 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
430 "Face used for marking a misspelled word in Flyspell."
432 ;; backward-compatibility alias
433 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
435 (defface flyspell-duplicate
436 '((((class color)) (:foreground "Gold3" :bold t :underline t))
438 "Face used for marking a misspelled word that appears twice in the buffer.
439 See also `flyspell-duplicate-distance'."
441 ;; backward-compatibility alias
442 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
444 (defvar flyspell-overlay nil)
446 ;;*---------------------------------------------------------------------*/
447 ;;* flyspell-mode ... */
448 ;;*---------------------------------------------------------------------*/
449 ;;;###autoload(defvar flyspell-mode nil)
451 (define-minor-mode flyspell-mode
452 "Minor mode performing on-the-fly spelling checking.
453 This spawns a single Ispell process and checks each word.
454 The default flyspell behavior is to highlight incorrect words.
455 With no argument, this command toggles Flyspell mode.
456 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
459 \\[ispell-word]: correct words (using Ispell).
460 \\[flyspell-auto-correct-word]: automatically correct word.
461 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
462 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
465 This runs `flyspell-mode-hook' after flyspell is entered.
468 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
469 valid. For instance, a personal dictionary can be used by
470 invoking `ispell-change-dictionary'.
472 Consider using the `ispell-parser' to check your text. For instance
474 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
477 \\[flyspell-region] checks all words inside a region.
478 \\[flyspell-buffer] checks the whole buffer."
479 :lighter flyspell-mode-line-string
480 :keymap flyspell-mode-map
484 (flyspell-mode-off)))
486 ;;*---------------------------------------------------------------------*/
487 ;;* flyspell-buffers ... */
488 ;;* ------------------------------------------------------------- */
489 ;;* For remembering buffers running flyspell */
490 ;;*---------------------------------------------------------------------*/
491 (defvar flyspell-buffers nil)
493 ;;*---------------------------------------------------------------------*/
494 ;;* flyspell-minibuffer-p ... */
495 ;;*---------------------------------------------------------------------*/
496 (defun flyspell-minibuffer-p (buffer)
497 "Is BUFFER a minibuffer?"
498 (let ((ws (get-buffer-window-list buffer t)))
499 (and (consp ws) (window-minibuffer-p (car ws)))))
501 ;;*---------------------------------------------------------------------*/
502 ;;* flyspell-accept-buffer-local-defs ... */
503 ;;*---------------------------------------------------------------------*/
504 (defvar flyspell-last-buffer nil
505 "The buffer in which the last flyspell operation took place.")
507 (defun flyspell-accept-buffer-local-defs (&optional force)
508 ;; When flyspell-word is used inside a loop (e.g. when processing
509 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
510 ;; up dwarfing everything else, so only do it when the buffer has changed.
511 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
512 (setq flyspell-last-buffer (current-buffer))
513 ;; Strange problem: If buffer in current window has font-lock turned on,
514 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
515 ;; call will reset the buffer to the buffer in the current window.
516 ;; However, it only happens at startup (fix by Albert L. Ting).
518 (ispell-accept-buffer-local-defs))
519 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
520 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
521 ;; The dictionary has changed
522 (setq flyspell-dash-dictionary ispell-dictionary)
523 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
524 (setq flyspell-consider-dash-as-word-delimiter-flag
525 (member (or ispell-local-dictionary ispell-dictionary)
526 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
528 ;;*---------------------------------------------------------------------*/
529 ;;* flyspell-mode-on ... */
530 ;;*---------------------------------------------------------------------*/
531 (defun flyspell-mode-on ()
532 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
533 (ispell-maybe-find-aspell-dictionaries)
534 (setq ispell-highlight-face 'flyspell-incorrect)
535 ;; local dictionaries setup
536 (or ispell-local-dictionary ispell-dictionary
537 (if flyspell-default-dictionary
538 (ispell-change-dictionary flyspell-default-dictionary)))
539 ;; we have to force ispell to accept the local definition or
540 ;; otherwise it could be too late, the local dictionary may
542 ;; Pass the `force' argument for the case where flyspell was active already
543 ;; but the buffer's local-defs have been edited.
544 (flyspell-accept-buffer-local-defs 'force)
545 ;; we put the `flyspell-delayed' property on some commands
546 (flyspell-delay-commands)
547 ;; we put the `flyspell-deplacement' property on some commands
548 (flyspell-deplacement-commands)
549 ;; we bound flyspell action to post-command hook
550 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
551 ;; we bound flyspell action to pre-command hook
552 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
553 ;; we bound flyspell action to after-change hook
554 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
555 ;; set flyspell-generic-check-word-p based on the major mode
556 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
558 (setq flyspell-generic-check-word-p mode-predicate)))
559 ;; the welcome message
560 (if (and flyspell-issue-message-flag
561 flyspell-issue-welcome-flag
563 (let ((binding (where-is-internal 'flyspell-auto-correct-word
567 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
568 (key-description binding))
569 "Welcome to flyspell. Use Mouse-2 to correct words."))))
570 ;; we end with the flyspell hooks
571 (run-hooks 'flyspell-mode-hook))
573 ;;*---------------------------------------------------------------------*/
574 ;;* flyspell-delay-commands ... */
575 ;;*---------------------------------------------------------------------*/
576 (defun flyspell-delay-commands ()
577 "Install the standard set of Flyspell delayed commands."
578 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
579 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
581 ;;*---------------------------------------------------------------------*/
582 ;;* flyspell-delay-command ... */
583 ;;*---------------------------------------------------------------------*/
584 (defun flyspell-delay-command (command)
585 "Set COMMAND to be delayed, for Flyspell.
586 When flyspell `post-command-hook' is invoked because a delayed command
587 as been used the current word is not immediately checked.
588 It will be checked only after `flyspell-delay' seconds."
589 (interactive "SDelay Flyspell after Command: ")
590 (put command 'flyspell-delayed t))
592 ;;*---------------------------------------------------------------------*/
593 ;;* flyspell-deplacement-commands ... */
594 ;;*---------------------------------------------------------------------*/
595 (defun flyspell-deplacement-commands ()
596 "Install the standard set of Flyspell deplacement commands."
597 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
598 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
600 ;;*---------------------------------------------------------------------*/
601 ;;* flyspell-deplacement-command ... */
602 ;;*---------------------------------------------------------------------*/
603 (defun flyspell-deplacement-command (command)
604 "Set COMMAND that implement cursor movements, for Flyspell.
605 When flyspell `post-command-hook' is invoked because of a deplacement command
606 as been used the current word is checked only if the previous command was
607 not the very same deplacement command."
608 (interactive "SDeplacement Flyspell after Command: ")
609 (put command 'flyspell-deplacement t))
611 ;;*---------------------------------------------------------------------*/
612 ;;* flyspell-word-cache ... */
613 ;;*---------------------------------------------------------------------*/
614 (defvar flyspell-word-cache-start nil)
615 (defvar flyspell-word-cache-end nil)
616 (defvar flyspell-word-cache-word nil)
617 (defvar flyspell-word-cache-result '_)
618 (make-variable-buffer-local 'flyspell-word-cache-start)
619 (make-variable-buffer-local 'flyspell-word-cache-end)
620 (make-variable-buffer-local 'flyspell-word-cache-word)
621 (make-variable-buffer-local 'flyspell-word-cache-result)
623 ;;*---------------------------------------------------------------------*/
624 ;;* The flyspell pre-hook, store the current position. In the */
625 ;;* post command hook, we will check, if the word at this position */
626 ;;* has to be spell checked. */
627 ;;*---------------------------------------------------------------------*/
628 (defvar flyspell-pre-buffer nil)
629 (defvar flyspell-pre-point nil)
630 (defvar flyspell-pre-column nil)
631 (defvar flyspell-pre-pre-buffer nil)
632 (defvar flyspell-pre-pre-point nil)
634 ;;*---------------------------------------------------------------------*/
635 ;;* flyspell-previous-command ... */
636 ;;*---------------------------------------------------------------------*/
637 (defvar flyspell-previous-command nil
638 "The last interactive command checked by Flyspell.")
640 ;;*---------------------------------------------------------------------*/
641 ;;* flyspell-pre-command-hook ... */
642 ;;*---------------------------------------------------------------------*/
643 (defun flyspell-pre-command-hook ()
644 "Save the current buffer and point for Flyspell's post-command hook."
646 (setq flyspell-pre-buffer (current-buffer))
647 (setq flyspell-pre-point (point))
648 (setq flyspell-pre-column (current-column)))
650 ;;*---------------------------------------------------------------------*/
651 ;;* flyspell-mode-off ... */
652 ;;*---------------------------------------------------------------------*/
654 (defun flyspell-mode-off ()
655 "Turn Flyspell mode off."
656 ;; we remove the hooks
657 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
658 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
659 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
660 ;; we remove all the flyspell hilightings
661 (flyspell-delete-all-overlays)
662 ;; we have to erase pre cache variables
663 (setq flyspell-pre-buffer nil)
664 (setq flyspell-pre-point nil)
665 ;; we mark the mode as killed
666 (setq flyspell-mode nil))
668 ;;*---------------------------------------------------------------------*/
669 ;;* flyspell-check-pre-word-p ... */
670 ;;*---------------------------------------------------------------------*/
671 (defun flyspell-check-pre-word-p ()
672 "Return non-nil if we should check the word before point.
673 More precisely, it applies to the word that was before point
674 before the current command."
676 ((or (not (numberp flyspell-pre-point))
677 (not (bufferp flyspell-pre-buffer))
678 (not (buffer-live-p flyspell-pre-buffer)))
680 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
681 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
683 ((or (and (= flyspell-pre-point (- (point) 1))
684 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
685 (= flyspell-pre-point (point))
686 (= flyspell-pre-point (+ (point) 1)))
688 ((and (symbolp this-command)
689 (not executing-kbd-macro)
690 (or (get this-command 'flyspell-delayed)
691 (and (get this-command 'flyspell-deplacement)
692 (eq flyspell-previous-command this-command)))
693 (or (= (current-column) 0)
694 (= (current-column) flyspell-pre-column)
695 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
697 ((not (eq (current-buffer) flyspell-pre-buffer))
699 ((not (and (numberp flyspell-word-cache-start)
700 (numberp flyspell-word-cache-end)))
703 (or (< flyspell-pre-point flyspell-word-cache-start)
704 (> flyspell-pre-point flyspell-word-cache-end)))))
706 ;;*---------------------------------------------------------------------*/
707 ;;* The flyspell after-change-hook, store the change position. In */
708 ;;* the post command hook, we will check, if the word at this */
709 ;;* position has to be spell checked. */
710 ;;*---------------------------------------------------------------------*/
711 (defvar flyspell-changes nil)
712 (make-variable-buffer-local 'flyspell-changes)
714 ;;*---------------------------------------------------------------------*/
715 ;;* flyspell-after-change-function ... */
716 ;;*---------------------------------------------------------------------*/
717 (defun flyspell-after-change-function (start stop len)
718 "Save the current buffer and point for Flyspell's post-command hook."
719 (push (cons start stop) flyspell-changes))
721 ;;*---------------------------------------------------------------------*/
722 ;;* flyspell-check-changed-word-p ... */
723 ;;*---------------------------------------------------------------------*/
724 (defun flyspell-check-changed-word-p (start stop)
725 "Return t when the changed word has to be checked.
726 The answer depends of several criteria.
727 Mostly we check word delimiters."
729 ((and (memq (char-after start) '(?\n ? )) (> stop start))
731 ((not (numberp flyspell-pre-point))
733 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
735 ((let ((pos (point)))
736 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
741 ;;*---------------------------------------------------------------------*/
742 ;;* flyspell-check-word-p ... */
743 ;;*---------------------------------------------------------------------*/
744 (defun flyspell-check-word-p ()
745 "Return t when the word at `point' has to be checked.
746 The answer depends of several criteria.
747 Mostly we check word delimiters."
749 ((<= (- (point-max) 1) (point-min))
750 ;; the buffer is not filled enough
752 ((and (and (> (current-column) 0)
753 (not (eq (current-column) flyspell-pre-column)))
756 (and (looking-at (flyspell-get-not-casechars))
757 (or flyspell-consider-dash-as-word-delimiter-flag
758 (not (looking-at "-"))))))
759 ;; yes because we have reached or typed a word delimiter.
761 ((symbolp this-command)
763 ((get this-command 'flyspell-deplacement)
764 (not (eq flyspell-previous-command this-command)))
765 ((get this-command 'flyspell-delayed)
766 ;; the current command is not delayed, that
767 ;; is that we must check the word now
768 (and (not unread-command-events)
769 (sit-for flyspell-delay)))
773 ;;*---------------------------------------------------------------------*/
774 ;;* flyspell-debug-signal-no-check ... */
775 ;;*---------------------------------------------------------------------*/
776 (defun flyspell-debug-signal-no-check (msg obj)
777 (setq debug-on-error t)
778 (with-current-buffer (get-buffer-create "*flyspell-debug*")
780 (insert "NO-CHECK:\n")
781 (insert (format " %S : %S\n" msg obj))))
783 ;;*---------------------------------------------------------------------*/
784 ;;* flyspell-debug-signal-pre-word-checked ... */
785 ;;*---------------------------------------------------------------------*/
786 (defun flyspell-debug-signal-pre-word-checked ()
787 (setq debug-on-error t)
788 (with-current-buffer (get-buffer-create "*flyspell-debug*")
789 (insert "PRE-WORD:\n")
790 (insert (format " pre-point : %S\n" flyspell-pre-point))
791 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
792 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
793 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
794 (goto-char (point-max))))
796 ;;*---------------------------------------------------------------------*/
797 ;;* flyspell-debug-signal-word-checked ... */
798 ;;*---------------------------------------------------------------------*/
799 (defun flyspell-debug-signal-word-checked ()
800 (setq debug-on-error t)
801 (let ((oldbuf (current-buffer))
803 (with-current-buffer (get-buffer-create "*flyspell-debug*")
805 (insert (format " this-cmd : %S\n" this-command))
806 (insert (format " delayed : %S\n" (and (symbolp this-command)
807 (get this-command 'flyspell-delayed))))
808 (insert (format " point : %S\n" point))
809 (insert (format " prev-char : [%c] %S\n"
810 (with-current-buffer oldbuf
811 (let ((c (if (> (point) (point-min))
814 (char-after (point)))
817 (with-current-buffer oldbuf
818 (let ((c (if (> (point) (point-min))
821 (and (and (looking-at (flyspell-get-not-casechars)) 1)
822 (and (or flyspell-consider-dash-as-word-delimiter-flag
823 (not (looking-at "\\-"))) 2))))))
825 (insert (format " because : %S\n"
827 ((not (and (symbolp this-command)
828 (get this-command 'flyspell-delayed)))
829 ;; the current command is not delayed, that
830 ;; is that we must check the word now
832 ((with-current-buffer oldbuf
833 (let ((c (if (> (point) (point-min))
836 (and (looking-at (flyspell-get-not-casechars))
837 (or flyspell-consider-dash-as-word-delimiter-flag
838 (not (looking-at "\\-"))))))))
840 ;; yes because we have reached or typed a word delimiter.
842 ((not (integerp flyspell-delay))
843 ;; yes because the user had set up a no-delay configuration.
847 (goto-char (point-max)))))
849 ;;*---------------------------------------------------------------------*/
850 ;;* flyspell-debug-signal-changed-checked ... */
851 ;;*---------------------------------------------------------------------*/
852 (defun flyspell-debug-signal-changed-checked ()
853 (setq debug-on-error t)
854 (let ((point (point)))
855 (with-current-buffer (get-buffer-create "*flyspell-debug*")
856 (insert "CHANGED WORD:\n")
857 (insert (format " point : %S\n" point))
858 (goto-char (point-max)))))
860 ;;*---------------------------------------------------------------------*/
861 ;;* flyspell-post-command-hook ... */
862 ;;* ------------------------------------------------------------- */
863 ;;* It is possible that we check several words: */
864 ;;* 1- the current word is checked if the predicate */
865 ;;* FLYSPELL-CHECK-WORD-P is true */
866 ;;* 2- the word that used to be the current word before the */
867 ;;* THIS-COMMAND is checked if: */
868 ;;* a- the previous word is different from the current word */
869 ;;* b- the previous word as not just been checked by the */
870 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
871 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
872 ;;* previous word nor the current word */
873 ;;*---------------------------------------------------------------------*/
874 (defun flyspell-post-command-hook ()
875 "The `post-command-hook' used by flyspell to check a word in-the-fly."
877 (let ((command this-command)
878 ;; Prevent anything we do from affecting the mark.
880 (if (flyspell-check-pre-word-p)
881 (with-current-buffer flyspell-pre-buffer
882 '(flyspell-debug-signal-pre-word-checked)
884 (goto-char flyspell-pre-point)
886 (if (flyspell-check-word-p)
888 '(flyspell-debug-signal-word-checked)
890 ;; we remember which word we have just checked.
891 ;; this will be used next time we will check a word
892 ;; to compare the next current word with the word
893 ;; that as been registered in the pre-command-hook
894 ;; that is these variables are used within the predicate
895 ;; FLYSPELL-CHECK-PRE-WORD-P
896 (setq flyspell-pre-pre-buffer (current-buffer))
897 (setq flyspell-pre-pre-point (point)))
899 (setq flyspell-pre-pre-buffer nil)
900 (setq flyspell-pre-pre-point nil)
901 ;; when a word is not checked because of a delayed command
902 ;; we do not disable the ispell cache.
903 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
905 (setq flyspell-word-cache-end -1)
906 (setq flyspell-word-cache-result '_)))))
907 (while (and (not (input-pending-p)) (consp flyspell-changes))
908 (let ((start (car (car flyspell-changes)))
909 (stop (cdr (car flyspell-changes))))
910 (if (flyspell-check-changed-word-p start stop)
912 '(flyspell-debug-signal-changed-checked)
915 (setq flyspell-changes (cdr flyspell-changes))))
916 (setq flyspell-previous-command command)))
918 ;;*---------------------------------------------------------------------*/
919 ;;* flyspell-notify-misspell ... */
920 ;;*---------------------------------------------------------------------*/
921 (defun flyspell-notify-misspell (word poss)
922 (let ((replacements (if (stringp poss)
924 (if flyspell-sort-corrections
925 (sort (car (cdr (cdr poss))) 'string<)
926 (car (cdr (cdr poss)))))))
927 (if flyspell-issue-message-flag
928 (message "misspelling `%s' %S" word replacements))))
930 ;;*---------------------------------------------------------------------*/
931 ;;* flyspell-word-search-backward ... */
932 ;;*---------------------------------------------------------------------*/
933 (defun flyspell-word-search-backward (word bound)
937 (while (and (not r) (setq p (search-backward word bound t)))
938 (let ((lw (flyspell-get-word '())))
939 (if (and (consp lw) (string-equal (car lw) word))
944 ;;*---------------------------------------------------------------------*/
945 ;;* flyspell-word-search-forward ... */
946 ;;*---------------------------------------------------------------------*/
947 (defun flyspell-word-search-forward (word bound)
951 (while (and (not r) (setq p (search-forward word bound t)))
952 (let ((lw (flyspell-get-word '())))
953 (if (and (consp lw) (string-equal (car lw) word))
955 (goto-char (1+ p)))))
958 ;;*---------------------------------------------------------------------*/
959 ;;* flyspell-word ... */
960 ;;*---------------------------------------------------------------------*/
961 (defun flyspell-word (&optional following)
962 "Spell check a word."
963 (interactive (list ispell-following-word))
965 ;; use the correct dictionary
966 (flyspell-accept-buffer-local-defs)
967 (let* ((cursor-location (point))
968 (flyspell-word (flyspell-get-word following))
970 (if (or (eq flyspell-word nil)
971 (and (fboundp flyspell-generic-check-word-p)
972 (not (funcall flyspell-generic-check-word-p))))
975 ;; destructure return flyspell-word info list.
976 (setq start (car (cdr flyspell-word))
977 end (car (cdr (cdr flyspell-word)))
978 word (car flyspell-word))
979 ;; before checking in the directory, we check for doublons.
981 ((and (or (not (eq ispell-parser 'tex))
982 (and (> start (point-min))
983 (not (memq (char-after (1- start)) '(?\} ?\\)))))
984 flyspell-mark-duplications-flag
986 (goto-char (1- start))
987 (let ((p (flyspell-word-search-backward
989 (- start (1+ (- end start))))))
990 (and p (/= p (1- start))))))
991 ;; yes, this is a doublon
992 (flyspell-highlight-incorrect-region start end 'doublon)
994 ((and (eq flyspell-word-cache-start start)
995 (eq flyspell-word-cache-end end)
996 (string-equal flyspell-word-cache-word word))
997 ;; this word had been already checked, we skip
998 flyspell-word-cache-result)
999 ((and (eq ispell-parser 'tex)
1000 (flyspell-tex-command-p flyspell-word))
1001 ;; this is a correct word (because a tex command)
1002 (flyspell-unhighlight-at start)
1004 (flyspell-unhighlight-at (- end 1)))
1007 ;; we setup the cache
1008 (setq flyspell-word-cache-start start)
1009 (setq flyspell-word-cache-end end)
1010 (setq flyspell-word-cache-word word)
1011 ;; now check spelling of word.
1012 (ispell-send-string "%\n")
1013 ;; put in verbose mode
1014 (ispell-send-string (concat "^" word "\n"))
1015 ;; we mark the ispell process so it can be killed
1016 ;; when emacs is exited without query
1017 (set-process-query-on-exit-flag ispell-process nil)
1018 ;; Wait until ispell has processed word. Since this code is often
1019 ;; executed from post-command-hook but the ispell process may not
1020 ;; be responsive, it's important to make sure we re-enable C-g.
1023 (accept-process-output ispell-process)
1024 (not (string= "" (car ispell-filter))))))
1025 ;; (ispell-send-string "!\n")
1026 ;; back to terse mode.
1027 (setq ispell-filter (cdr ispell-filter))
1028 (if (consp ispell-filter)
1029 (setq poss (ispell-parse-output (car ispell-filter))))
1030 (let ((res (cond ((eq poss t)
1032 (setq flyspell-word-cache-result t)
1033 (flyspell-unhighlight-at start)
1035 (flyspell-unhighlight-at (- end 1)))
1037 ((and (stringp poss) flyspell-highlight-flag)
1039 (setq flyspell-word-cache-result t)
1040 (flyspell-unhighlight-at start)
1042 (flyspell-unhighlight-at (- end 1)))
1045 (setq flyspell-word-cache-result t)
1046 (flyspell-unhighlight-at start)
1048 (flyspell-unhighlight-at (- end 1)))
1050 ((or (and (< flyspell-duplicate-distance 0)
1053 (flyspell-word-search-backward
1058 (flyspell-word-search-forward
1061 (and (> flyspell-duplicate-distance 0)
1064 (flyspell-word-search-backward
1067 flyspell-duplicate-distance)))
1070 (flyspell-word-search-forward
1073 flyspell-duplicate-distance))))))
1074 ;; This is a misspelled word which occurs
1075 ;; twice within flyspell-duplicate-distance.
1076 (setq flyspell-word-cache-result nil)
1077 (if flyspell-highlight-flag
1078 (flyspell-highlight-duplicate-region
1080 (message "duplicate `%s'" word))
1083 (setq flyspell-word-cache-result nil)
1084 ;; incorrect highlight the location
1085 (if flyspell-highlight-flag
1086 (flyspell-highlight-incorrect-region
1088 (flyspell-notify-misspell word poss))
1090 ;; return to original location
1091 (goto-char cursor-location)
1092 (if ispell-quit (setq ispell-quit nil))
1095 ;;*---------------------------------------------------------------------*/
1096 ;;* flyspell-tex-math-initialized ... */
1097 ;;*---------------------------------------------------------------------*/
1098 (defvar flyspell-tex-math-initialized nil)
1100 ;;*---------------------------------------------------------------------*/
1101 ;;* flyspell-math-tex-command-p ... */
1102 ;;* ------------------------------------------------------------- */
1103 ;;* This function uses the texmathp package to check if (point) */
1104 ;;* is within a tex command. In order to avoid using */
1105 ;;* condition-case each time we use the variable */
1106 ;;* flyspell-tex-math-initialized to make a special case the first */
1107 ;;* time that function is called. */
1108 ;;*---------------------------------------------------------------------*/
1109 (defun flyspell-math-tex-command-p ()
1110 (when (fboundp 'texmathp)
1112 (flyspell-check-tex-math-command
1114 ((eq flyspell-tex-math-initialized t)
1116 ((eq flyspell-tex-math-initialized 'error)
1119 (setq flyspell-tex-math-initialized t)
1123 (setq flyspell-tex-math-initialized 'error)
1126 ;;*---------------------------------------------------------------------*/
1127 ;;* flyspell-tex-command-p ... */
1128 ;;*---------------------------------------------------------------------*/
1129 (defun flyspell-tex-command-p (word)
1130 "Return t if WORD is a TeX command."
1132 (let ((b (car (cdr word))))
1133 (and (re-search-backward "\\\\" (- (point) 100) t)
1134 (or (= (match-end 0) b)
1135 (and (goto-char (match-end 0))
1136 (looking-at flyspell-tex-command-regexp)
1137 (>= (match-end 0) b))))))
1138 (flyspell-math-tex-command-p)))
1140 ;;*---------------------------------------------------------------------*/
1141 ;;* flyspell-casechars-cache ... */
1142 ;;*---------------------------------------------------------------------*/
1143 (defvar flyspell-casechars-cache nil)
1144 (defvar flyspell-ispell-casechars-cache nil)
1145 (make-variable-buffer-local 'flyspell-casechars-cache)
1146 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1148 ;;*---------------------------------------------------------------------*/
1149 ;;* flyspell-get-casechars ... */
1150 ;;*---------------------------------------------------------------------*/
1151 (defun flyspell-get-casechars ()
1152 "This function builds a string that is the regexp of word chars.
1153 In order to avoid one useless string construction,
1154 this function changes the last char of the `ispell-casechars' string."
1155 (let ((ispell-casechars (ispell-get-casechars)))
1157 ((eq ispell-parser 'tex)
1158 (setq flyspell-ispell-casechars-cache ispell-casechars)
1159 (setq flyspell-casechars-cache
1160 (concat (substring ispell-casechars
1162 (- (length ispell-casechars) 1))
1164 flyspell-casechars-cache)
1166 (setq flyspell-ispell-casechars-cache ispell-casechars)
1167 (setq flyspell-casechars-cache ispell-casechars)
1168 flyspell-casechars-cache))))
1170 ;;*---------------------------------------------------------------------*/
1171 ;;* flyspell-get-not-casechars-cache ... */
1172 ;;*---------------------------------------------------------------------*/
1173 (defvar flyspell-not-casechars-cache nil)
1174 (defvar flyspell-ispell-not-casechars-cache nil)
1175 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1176 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1178 ;;*---------------------------------------------------------------------*/
1179 ;;* flyspell-get-not-casechars ... */
1180 ;;*---------------------------------------------------------------------*/
1181 (defun flyspell-get-not-casechars ()
1182 "This function builds a string that is the regexp of non-word chars."
1183 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1185 ((eq ispell-parser 'tex)
1186 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1187 (setq flyspell-not-casechars-cache
1188 (concat (substring ispell-not-casechars
1190 (- (length ispell-not-casechars) 1))
1192 flyspell-not-casechars-cache)
1194 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1195 (setq flyspell-not-casechars-cache ispell-not-casechars)
1196 flyspell-not-casechars-cache))))
1198 ;;*---------------------------------------------------------------------*/
1199 ;;* flyspell-get-word ... */
1200 ;;*---------------------------------------------------------------------*/
1201 (defun flyspell-get-word (following &optional extra-otherchars)
1202 "Return the word for spell-checking according to Ispell syntax.
1203 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1204 is non-nil when called interactively, then the following word
1205 \(rather than preceding\) is checked when the cursor is not over a word.
1206 Optional second argument contains otherchars that can be included in word
1209 Word syntax described by `flyspell-dictionary-alist' (which see)."
1210 (let* ((flyspell-casechars (flyspell-get-casechars))
1211 (flyspell-not-casechars (flyspell-get-not-casechars))
1212 (ispell-otherchars (ispell-get-otherchars))
1213 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1214 (word-regexp (concat flyspell-casechars
1216 (if (not (string= "" ispell-otherchars))
1217 (concat ispell-otherchars "?"))
1218 (if extra-otherchars
1219 (concat extra-otherchars "?"))
1222 (if (or ispell-many-otherchars-p
1228 (if (not (looking-at flyspell-casechars))
1230 (re-search-forward flyspell-casechars nil t)
1231 (re-search-backward flyspell-casechars nil t)))
1232 ;; move to front of word
1233 (re-search-backward flyspell-not-casechars nil 'start)
1234 (while (and (or (and (not (string= "" ispell-otherchars))
1235 (looking-at ispell-otherchars))
1236 (and extra-otherchars (looking-at extra-otherchars)))
1238 (or (not did-it-once)
1239 ispell-many-otherchars-p)
1240 (not (eq prevpt (point))))
1241 (if (and extra-otherchars (looking-at extra-otherchars))
1244 (if (looking-at flyspell-casechars)
1245 (re-search-backward flyspell-not-casechars nil 'move)))
1249 (if (looking-at flyspell-casechars)
1250 (re-search-backward flyspell-not-casechars nil 'move)
1251 (backward-char -1))))
1252 ;; Now mark the word and save to string.
1253 (if (not (re-search-forward word-regexp nil t))
1256 (setq start (match-beginning 0)
1258 word (buffer-substring-no-properties start end))
1259 (list word start end)))))
1261 ;;*---------------------------------------------------------------------*/
1262 ;;* flyspell-small-region ... */
1263 ;;*---------------------------------------------------------------------*/
1264 (defun flyspell-small-region (beg end)
1265 "Flyspell text between BEG and END."
1273 (while (< (point) end)
1274 (if (and flyspell-issue-message-flag (= count 100))
1276 (message "Spell Checking...%d%%"
1277 (* 100 (/ (float (- (point) beg)) (- end beg))))
1279 (setq count (+ 1 count)))
1282 (let ((cur (point)))
1284 (if (and (< (point) end) (> (point) (+ cur 1)))
1285 (backward-char 1)))))
1287 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1290 ;;*---------------------------------------------------------------------*/
1291 ;;* flyspell-external-ispell-process ... */
1292 ;;*---------------------------------------------------------------------*/
1293 (defvar flyspell-external-ispell-process '()
1294 "The external Flyspell Ispell process.")
1296 ;;*---------------------------------------------------------------------*/
1297 ;;* flyspell-external-ispell-buffer ... */
1298 ;;*---------------------------------------------------------------------*/
1299 (defvar flyspell-external-ispell-buffer '())
1300 (defvar flyspell-large-region-buffer '())
1301 (defvar flyspell-large-region-beg (point-min))
1302 (defvar flyspell-large-region-end (point-max))
1304 ;;*---------------------------------------------------------------------*/
1305 ;;* flyspell-external-point-words ... */
1306 ;;*---------------------------------------------------------------------*/
1307 (defun flyspell-external-point-words ()
1308 "Mark words from a buffer listing incorrect words in order of appearance.
1309 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1310 \(We finish by killing that buffer and setting the variable to nil.)
1311 The buffer to mark them in is `flyspell-large-region-buffer'."
1312 (let (words-not-found
1313 (ispell-otherchars (ispell-get-otherchars))
1314 (buffer-scan-pos flyspell-large-region-beg))
1315 (with-current-buffer flyspell-external-ispell-buffer
1316 (goto-char (point-min))
1317 ;; Loop over incorrect words, in the order they were reported,
1318 ;; which is also the order they appear in the buffer being checked.
1319 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1320 ;; Bind WORD to the next one.
1321 (let ((word (match-string 1)) (wordpos (point)))
1322 ;; Here there used to be code to see if WORD is the same
1323 ;; as the previous iteration, and count the number of consecutive
1324 ;; identical words, and the loop below would search for that many.
1325 ;; That code seemed to be incorrect, and on principle, should
1326 ;; be unnecessary too. -- rms.
1327 (if flyspell-issue-message-flag
1328 (message "Spell Checking...%d%% [%s]"
1329 (* 100 (/ (float (point)) (point-max)))
1331 (with-current-buffer flyspell-large-region-buffer
1332 (goto-char buffer-scan-pos)
1334 ;; Iterate on string search until string is found as word,
1337 (if (search-forward word
1338 flyspell-large-region-end t)
1341 ;; Move back into the match
1342 ;; so flyspell-get-word will find it.
1344 (flyspell-get-word nil)))
1345 (found (car found-list))
1346 (found-length (length found))
1347 (misspell-length (length word)))
1349 ;; Size matches, we really found it.
1350 (= found-length misspell-length)
1351 ;; Matches as part of a boundary-char separated word
1353 (split-string found ispell-otherchars))
1354 ;; Misspelling has higher length than
1355 ;; what flyspell considers the
1356 ;; word. Caused by boundary-chars
1357 ;; mismatch. Validating seems safe.
1358 (< found-length misspell-length)
1359 ;; ispell treats beginning of some TeX
1360 ;; commands as nroff control sequences
1361 ;; and strips them in the list of
1362 ;; misspelled words thus giving a
1363 ;; non-existent word. Skip if ispell
1364 ;; is used, string is a TeX command
1365 ;; (char before beginning of word is
1366 ;; backslash) and none of the previous
1368 (and (not ispell-really-aspell)
1370 (goto-char (- (nth 1 found-list) 1))
1371 (if (looking-at "[\\]" )
1376 ;; Search for next misspelled word will begin from
1377 ;; end of last validated match.
1378 (setq buffer-scan-pos (point))))
1379 ;; Record if misspelling is not found and try new one
1380 (add-to-list 'words-not-found
1381 (concat " -> " word " - "
1382 (int-to-string wordpos)))
1383 (setq keep nil)))))))
1385 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1386 ;; Warn about not found misspellings
1387 (dolist (word words-not-found)
1388 (message "%s: word not found" word))
1389 ;; Kill and forget the buffer with the list of incorrect words.
1390 (kill-buffer flyspell-external-ispell-buffer)
1391 (setq flyspell-external-ispell-buffer nil)))
1393 ;;*---------------------------------------------------------------------*/
1394 ;;* flyspell-process-localwords ... */
1395 ;;* ------------------------------------------------------------- */
1396 ;;* This function is used to prevent marking of words explicitly */
1397 ;;* declared correct. */
1398 ;;*---------------------------------------------------------------------*/
1399 (defun flyspell-process-localwords (misspellings-buffer)
1401 (ispell-casechars (ispell-get-casechars)))
1402 ;; Get localwords from the original buffer
1404 (goto-char (point-min))
1405 ;; Localwords parsing copied from ispell.el.
1406 (while (search-forward ispell-words-keyword nil t)
1407 (let ((end (save-excursion (end-of-line) (point)))
1409 ;; buffer-local words separated by a space, and can contain
1410 ;; any character other than a space. Not rigorous enough.
1411 (while (re-search-forward " *\\([^ ]+\\)" end t)
1412 (setq string (buffer-substring-no-properties (match-beginning 1)
1414 ;; This can fail when string contains a word with invalid chars.
1415 ;; Error handling needs to be added between Ispell and Emacs.
1416 (if (and (< 1 (length string))
1417 (equal 0 (string-match ispell-casechars string)))
1418 (push string localwords))))))
1419 ;; Remove localwords matches from misspellings-buffer.
1420 ;; The usual mechanism of communicating the local words to ispell
1421 ;; does not affect the special ispell process used by
1422 ;; flyspell-large-region.
1423 (with-current-buffer misspellings-buffer
1425 (dolist (word localwords)
1426 (goto-char (point-min))
1427 (let ((regexp (concat "^" word "\n")))
1428 (while (re-search-forward regexp nil t)
1429 (delete-region (match-beginning 0) (match-end 0)))))))))
1431 ;;*---------------------------------------------------------------------*/
1432 ;;* flyspell-large-region ... */
1433 ;;*---------------------------------------------------------------------*/
1434 (defun flyspell-large-region (beg end)
1435 (let* ((curbuf (current-buffer))
1436 (buffer (get-buffer-create "*flyspell-region*")))
1437 (setq flyspell-external-ispell-buffer buffer)
1438 (setq flyspell-large-region-buffer curbuf)
1439 (setq flyspell-large-region-beg beg)
1440 (setq flyspell-large-region-end end)
1441 (flyspell-accept-buffer-local-defs)
1444 ;; this is done, we can start checking...
1445 (if flyspell-issue-message-flag (message "Checking region..."))
1447 (ispell-check-version)
1448 (let ((c (apply 'call-process-region beg
1454 (if ispell-really-aspell "list" "-l")
1456 ;; Local dictionary becomes the global dictionary in use.
1457 (if ispell-local-dictionary
1458 (setq ispell-dictionary ispell-local-dictionary))
1459 (setq args (ispell-get-ispell-args))
1460 (if ispell-dictionary ; use specified dictionary
1462 (append (list "-d" ispell-dictionary) args)))
1463 (if ispell-personal-dictionary ; use specified pers dict
1468 ispell-personal-dictionary)))))
1469 (setq args (append args ispell-extra-args))
1473 (flyspell-process-localwords buffer)
1474 (with-current-buffer curbuf
1475 (flyspell-delete-region-overlays beg end))
1476 (flyspell-external-point-words))
1477 (error "Can't check region...")))))
1479 ;;*---------------------------------------------------------------------*/
1480 ;;* flyspell-region ... */
1481 ;;* ------------------------------------------------------------- */
1482 ;;* Because `ispell -a' is too slow, it is not possible to use */
1483 ;;* it on large region. Then, when ispell is invoked on a large */
1484 ;;* text region, a new `ispell -l' process is spawned. The */
1485 ;;* pointed out words are then searched in the region a checked with */
1486 ;;* regular flyspell means. */
1487 ;;*---------------------------------------------------------------------*/
1489 (defun flyspell-region (beg end)
1490 "Flyspell text between BEG and END."
1499 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1500 (flyspell-large-region beg end)
1501 (flyspell-small-region beg end)))))
1503 ;;*---------------------------------------------------------------------*/
1504 ;;* flyspell-buffer ... */
1505 ;;*---------------------------------------------------------------------*/
1507 (defun flyspell-buffer ()
1508 "Flyspell whole buffer."
1510 (flyspell-region (point-min) (point-max)))
1512 ;;*---------------------------------------------------------------------*/
1513 ;;* old next error position ... */
1514 ;;*---------------------------------------------------------------------*/
1515 (defvar flyspell-old-buffer-error nil)
1516 (defvar flyspell-old-pos-error nil)
1518 ;;*---------------------------------------------------------------------*/
1519 ;;* flyspell-goto-next-error ... */
1520 ;;*---------------------------------------------------------------------*/
1521 (defun flyspell-goto-next-error ()
1522 "Go to the next previously detected error.
1523 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1528 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1529 (eq pos flyspell-old-pos-error))
1531 (if (= flyspell-old-pos-error max)
1532 ;; goto beginning of buffer
1534 (message "Restarting from beginning of buffer")
1535 (goto-char (point-min)))
1537 (setq pos (point))))
1538 ;; seek the next error
1539 (while (and (< pos max)
1540 (let ((ovs (overlays-at pos))
1542 (while (and (not r) (consp ovs))
1543 (if (flyspell-overlay-p (car ovs))
1545 (setq ovs (cdr ovs))))
1547 (setq pos (1+ pos)))
1548 ;; save the current location for next invocation
1549 (setq flyspell-old-pos-error pos)
1550 (setq flyspell-old-buffer-error (current-buffer))
1553 (message "No more miss-spelled word!"))))
1555 ;;*---------------------------------------------------------------------*/
1556 ;;* flyspell-overlay-p ... */
1557 ;;*---------------------------------------------------------------------*/
1558 (defun flyspell-overlay-p (o)
1559 "A predicate that return true iff O is an overlay used by flyspell."
1560 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1562 ;;*---------------------------------------------------------------------*/
1563 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1564 ;;* ------------------------------------------------------------- */
1565 ;;* Remove overlays introduced by flyspell. */
1566 ;;*---------------------------------------------------------------------*/
1567 (defun flyspell-delete-region-overlays (beg end)
1568 "Delete overlays used by flyspell in a given region."
1569 (remove-overlays beg end 'flyspell-overlay t))
1572 (defun flyspell-delete-all-overlays ()
1573 "Delete all the overlays used by flyspell."
1574 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1576 ;;*---------------------------------------------------------------------*/
1577 ;;* flyspell-unhighlight-at ... */
1578 ;;*---------------------------------------------------------------------*/
1579 (defun flyspell-unhighlight-at (pos)
1580 "Remove the flyspell overlay that are located at POS."
1581 (if flyspell-persistent-highlight
1582 (let ((overlays (overlays-at pos)))
1583 (while (consp overlays)
1584 (if (flyspell-overlay-p (car overlays))
1585 (delete-overlay (car overlays)))
1586 (setq overlays (cdr overlays))))
1587 (if (flyspell-overlay-p flyspell-overlay)
1588 (delete-overlay flyspell-overlay))))
1590 ;;*---------------------------------------------------------------------*/
1591 ;;* flyspell-properties-at-p ... */
1592 ;;* ------------------------------------------------------------- */
1593 ;;* Is there an highlight properties at position pos? */
1594 ;;*---------------------------------------------------------------------*/
1595 (defun flyspell-properties-at-p (pos)
1596 "Return t if there is a text property at POS, not counting `local-map'.
1597 If variable `flyspell-highlight-properties' is set to nil,
1598 text with properties are not checked. This function is used to discover
1599 if the character at POS has any other property."
1600 (let ((prop (text-properties-at pos))
1602 (while (and keep (consp prop))
1603 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1604 (setq prop (cdr (cdr prop)))
1608 ;;*---------------------------------------------------------------------*/
1609 ;;* make-flyspell-overlay ... */
1610 ;;*---------------------------------------------------------------------*/
1611 (defun make-flyspell-overlay (beg end face mouse-face)
1612 "Allocate an overlay to highlight an incorrect word.
1613 BEG and END specify the range in the buffer of that word.
1614 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1616 (let ((overlay (make-overlay beg end nil t nil)))
1617 (overlay-put overlay 'face face)
1618 (overlay-put overlay 'mouse-face mouse-face)
1619 (overlay-put overlay 'flyspell-overlay t)
1620 (overlay-put overlay 'evaporate t)
1621 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1622 (overlay-put overlay 'keymap flyspell-mouse-map)
1623 (when (eq face 'flyspell-incorrect)
1624 (and (stringp flyspell-before-incorrect-word-string)
1625 (overlay-put overlay 'before-string
1626 flyspell-before-incorrect-word-string))
1627 (and (stringp flyspell-after-incorrect-word-string)
1628 (overlay-put overlay 'after-string
1629 flyspell-after-incorrect-word-string)))
1632 ;;*---------------------------------------------------------------------*/
1633 ;;* flyspell-highlight-incorrect-region ... */
1634 ;;*---------------------------------------------------------------------*/
1635 (defun flyspell-highlight-incorrect-region (beg end poss)
1636 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1637 POSS is usually a list of possible spelling/correction lists,
1638 as returned by `ispell-parse-output'.
1639 It can also be the symbol `doublon', in the case where the word
1640 is itself incorrect, but suspiciously repeated."
1641 (let ((inhibit-read-only t))
1642 (unless (run-hook-with-args-until-success
1643 'flyspell-incorrect-hook beg end poss)
1644 (if (or flyspell-highlight-properties
1645 (not (flyspell-properties-at-p beg)))
1647 ;; we cleanup all the overlay that are in the region, not
1648 ;; beginning at the word start position
1649 (if (< (1+ beg) end)
1650 (let ((os (overlays-in (1+ beg) end)))
1652 (if (flyspell-overlay-p (car os))
1653 (delete-overlay (car os)))
1654 (setq os (cdr os)))))
1655 ;; we cleanup current overlay at the same position
1656 (flyspell-unhighlight-at beg)
1657 ;; now we can use a new overlay
1658 (setq flyspell-overlay
1659 (make-flyspell-overlay
1660 beg end 'flyspell-incorrect 'highlight)))))))
1662 ;;*---------------------------------------------------------------------*/
1663 ;;* flyspell-highlight-duplicate-region ... */
1664 ;;*---------------------------------------------------------------------*/
1665 (defun flyspell-highlight-duplicate-region (beg end poss)
1666 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1667 POSS is a list of possible spelling/correction lists,
1668 as returned by `ispell-parse-output'."
1669 (let ((inhibit-read-only t))
1670 (unless (run-hook-with-args-until-success
1671 'flyspell-incorrect-hook beg end poss)
1672 (if (or flyspell-highlight-properties
1673 (not (flyspell-properties-at-p beg)))
1675 ;; we cleanup current overlay at the same position
1676 (flyspell-unhighlight-at beg)
1677 ;; now we can use a new overlay
1678 (setq flyspell-overlay
1679 (make-flyspell-overlay beg end
1683 ;;*---------------------------------------------------------------------*/
1684 ;;* flyspell-auto-correct-cache ... */
1685 ;;*---------------------------------------------------------------------*/
1686 (defvar flyspell-auto-correct-pos nil)
1687 (defvar flyspell-auto-correct-region nil)
1688 (defvar flyspell-auto-correct-ring nil)
1689 (defvar flyspell-auto-correct-word nil)
1690 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1691 (make-variable-buffer-local 'flyspell-auto-correct-region)
1692 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1693 (make-variable-buffer-local 'flyspell-auto-correct-word)
1695 ;;*---------------------------------------------------------------------*/
1696 ;;* flyspell-check-previous-highlighted-word ... */
1697 ;;*---------------------------------------------------------------------*/
1698 (defun flyspell-check-previous-highlighted-word (&optional arg)
1699 "Correct the closer misspelled word.
1700 This function scans a mis-spelled word before the cursor. If it finds one
1701 it proposes replacement for that word. With prefix arg, count that many
1702 misspelled words backwards."
1704 (let ((pos1 (point))
1706 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1709 (while (and (setq pos (previous-overlay-change pos))
1712 (if (> pos (point-min))
1714 (setq ovs (overlays-at (1- pos)))
1717 (setq ovs (cdr ovs))
1718 (if (and (flyspell-overlay-p ov)
1719 (= 0 (setq arg (1- arg))))
1720 (throw 'exit t)))))))
1724 (error "No word to correct before point"))))
1726 ;;*---------------------------------------------------------------------*/
1727 ;;* flyspell-display-next-corrections ... */
1728 ;;*---------------------------------------------------------------------*/
1729 (defun flyspell-display-next-corrections (corrections)
1730 (let ((string "Corrections:")
1733 (while (< (length string) 80)
1734 (if (equal (car l) flyspell-auto-correct-word)
1735 (setq pos (cons (+ 1 (length string)) pos)))
1736 (setq string (concat string " " (car l)))
1739 (let ((num (car pos)))
1740 (put-text-property num
1741 (+ num (length flyspell-auto-correct-word))
1742 'face 'flyspell-incorrect
1744 (setq pos (cdr pos)))
1745 (if (fboundp 'display-message)
1746 (display-message 'no-log string)
1747 (message "%s" string))))
1749 ;;*---------------------------------------------------------------------*/
1750 ;;* flyspell-abbrev-table ... */
1751 ;;*---------------------------------------------------------------------*/
1752 (defun flyspell-abbrev-table ()
1753 (if flyspell-use-global-abbrev-table-p
1755 (or local-abbrev-table global-abbrev-table)))
1757 ;;*---------------------------------------------------------------------*/
1758 ;;* flyspell-define-abbrev ... */
1759 ;;*---------------------------------------------------------------------*/
1760 (defun flyspell-define-abbrev (name expansion)
1761 (let ((table (flyspell-abbrev-table)))
1763 (define-abbrev table name expansion))))
1765 ;;*---------------------------------------------------------------------*/
1766 ;;* flyspell-auto-correct-word ... */
1767 ;;*---------------------------------------------------------------------*/
1768 (defun flyspell-auto-correct-word ()
1769 "Correct the current word.
1770 This command proposes various successive corrections for the current word."
1773 (old-max (point-max)))
1774 ;; use the correct dictionary
1775 (flyspell-accept-buffer-local-defs)
1776 (if (and (eq flyspell-auto-correct-pos pos)
1777 (consp flyspell-auto-correct-region))
1778 ;; we have already been using the function at the same location
1779 (let* ((start (car flyspell-auto-correct-region))
1780 (len (cdr flyspell-auto-correct-region)))
1781 (flyspell-unhighlight-at start)
1782 (delete-region start (+ start len))
1783 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1784 (let* ((word (car flyspell-auto-correct-ring))
1785 (len (length word)))
1786 (rplacd flyspell-auto-correct-region len)
1788 (if flyspell-abbrev-p
1789 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1790 flyspell-auto-correct-word)
1791 (flyspell-change-abbrev (flyspell-abbrev-table)
1792 flyspell-auto-correct-word
1794 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1795 (funcall flyspell-insert-function word)
1797 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1798 (flyspell-ajust-cursor-point pos (point) old-max)
1799 (setq flyspell-auto-correct-pos (point)))
1800 ;; fetch the word to be checked
1801 (let ((word (flyspell-get-word nil)))
1803 (let ((start (car (cdr word)))
1804 (end (car (cdr (cdr word))))
1807 (setq flyspell-auto-correct-word word)
1808 ;; now check spelling of word.
1809 (ispell-send-string "%\n") ;put in verbose mode
1810 (ispell-send-string (concat "^" word "\n"))
1811 ;; wait until ispell has processed word.
1813 (accept-process-output ispell-process)
1814 (not (string= "" (car ispell-filter)))))
1815 (setq ispell-filter (cdr ispell-filter))
1816 (if (consp ispell-filter)
1817 (setq poss (ispell-parse-output (car ispell-filter))))
1819 ((or (eq poss t) (stringp poss))
1820 ;; don't correct word
1824 (error "Ispell: error in Ispell process"))
1826 ;; the word is incorrect, we have to propose a replacement
1827 (let ((replacements (if flyspell-sort-corrections
1828 (sort (car (cdr (cdr poss))) 'string<)
1829 (car (cdr (cdr poss))))))
1830 (setq flyspell-auto-correct-region nil)
1831 (if (consp replacements)
1833 (let ((replace (car replacements)))
1834 (let ((new-word replace))
1835 (if (not (equal new-word (car poss)))
1837 ;; the save the current replacements
1838 (setq flyspell-auto-correct-region
1839 (cons start (length new-word)))
1840 (let ((l replacements))
1841 (while (consp (cdr l))
1843 (rplacd l (cons (car poss) replacements)))
1844 (setq flyspell-auto-correct-ring
1846 (flyspell-unhighlight-at start)
1847 (delete-region start end)
1848 (funcall flyspell-insert-function new-word)
1849 (if flyspell-abbrev-p
1850 (if (flyspell-already-abbrevp
1851 (flyspell-abbrev-table) word)
1852 (flyspell-change-abbrev
1853 (flyspell-abbrev-table)
1856 (flyspell-define-abbrev word
1859 (flyspell-display-next-corrections
1860 (cons new-word flyspell-auto-correct-ring))
1861 (flyspell-ajust-cursor-point pos
1864 (setq flyspell-auto-correct-pos (point))
1865 (ispell-pdict-save t)))))))
1867 ;;*---------------------------------------------------------------------*/
1868 ;;* flyspell-auto-correct-previous-pos ... */
1869 ;;*---------------------------------------------------------------------*/
1870 (defvar flyspell-auto-correct-previous-pos nil
1871 "Holds the start of the first incorrect word before point.")
1873 ;;*---------------------------------------------------------------------*/
1874 ;;* flyspell-auto-correct-previous-hook ... */
1875 ;;*---------------------------------------------------------------------*/
1876 (defun flyspell-auto-correct-previous-hook ()
1877 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1878 Sets `flyspell-auto-correct-previous-pos' to nil"
1880 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1881 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1882 (setq flyspell-auto-correct-previous-pos nil)))
1884 ;;*---------------------------------------------------------------------*/
1885 ;;* flyspell-auto-correct-previous-word ... */
1886 ;;*---------------------------------------------------------------------*/
1887 (defun flyspell-auto-correct-previous-word (position)
1888 "Auto correct the first mispelled word that occurs before point.
1889 But don't look beyond what's visible on the screen."
1894 (move-to-window-line 0)
1896 (move-to-window-line -1)
1900 (narrow-to-region top bot)
1901 (overlay-recenter (point))
1903 (add-hook 'pre-command-hook
1904 (function flyspell-auto-correct-previous-hook) t t)
1906 (unless flyspell-auto-correct-previous-pos
1907 ;; only reset if a new overlay exists
1908 (setq flyspell-auto-correct-previous-pos nil)
1910 (let ((overlay-list (overlays-in (point-min) position))
1911 (new-overlay 'dummy-value))
1913 ;; search for previous (new) flyspell overlay
1914 (while (and new-overlay
1915 (or (not (flyspell-overlay-p new-overlay))
1916 ;; check if its face has changed
1917 (not (eq (get-char-property
1918 (overlay-start new-overlay) 'face)
1919 'flyspell-incorrect))))
1920 (setq new-overlay (car-safe overlay-list))
1921 (setq overlay-list (cdr-safe overlay-list)))
1923 ;; if nothing new exits new-overlay should be nil
1924 (if new-overlay ;; the length of the word may change so go to the start
1925 (setq flyspell-auto-correct-previous-pos
1926 (overlay-start new-overlay)))))
1928 (when flyspell-auto-correct-previous-pos
1930 (goto-char flyspell-auto-correct-previous-pos)
1931 (let ((ispell-following-word t)) ;; point is at start
1932 (if (numberp flyspell-auto-correct-previous-pos)
1933 (goto-char flyspell-auto-correct-previous-pos))
1934 (flyspell-auto-correct-word))
1935 ;; the point may have moved so reset this
1936 (setq flyspell-auto-correct-previous-pos (point))))))))
1938 ;;*---------------------------------------------------------------------*/
1939 ;;* flyspell-correct-word ... */
1940 ;;*---------------------------------------------------------------------*/
1941 (defun flyspell-correct-word (event)
1942 "Pop up a menu of possible corrections for a misspelled word.
1943 The word checked is the word at the mouse position."
1945 ;; use the correct dictionary
1946 (flyspell-accept-buffer-local-defs)
1947 ;; retain cursor location (I don't know why but save-excursion here fails).
1948 (let ((save (point)))
1949 (mouse-set-point event)
1950 (let ((cursor-location (point))
1951 (word (flyspell-get-word nil)))
1953 (let ((start (car (cdr word)))
1954 (end (car (cdr (cdr word))))
1957 ;; now check spelling of word.
1958 (ispell-send-string "%\n") ;put in verbose mode
1959 (ispell-send-string (concat "^" word "\n"))
1960 ;; wait until ispell has processed word
1962 (accept-process-output ispell-process)
1963 (not (string= "" (car ispell-filter)))))
1964 (setq ispell-filter (cdr ispell-filter))
1965 (if (consp ispell-filter)
1966 (setq poss (ispell-parse-output (car ispell-filter))))
1968 ((or (eq poss t) (stringp poss))
1969 ;; don't correct word
1973 (error "Ispell: error in Ispell process"))
1975 (flyspell-xemacs-popup
1976 poss word cursor-location start end save))
1978 ;; The word is incorrect, we have to propose a replacement.
1979 (flyspell-do-correct (flyspell-emacs-popup event poss word)
1980 poss word cursor-location start end save)))
1981 (ispell-pdict-save t))))))
1983 ;;*---------------------------------------------------------------------*/
1984 ;;* flyspell-do-correct ... */
1985 ;;*---------------------------------------------------------------------*/
1986 (defun flyspell-do-correct (replace poss word cursor-location start end save)
1987 "The popup menu callback."
1988 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
1989 ;; it instead right after calling the function.
1990 (cond ((eq replace 'ignore)
1995 (ispell-send-string (concat "*" word "\n"))
1996 ;; This was added only to the XEmacs side in revision 1.18 of
1997 ;; flyspell. I assume its absence on the Emacs side was an
1998 ;; oversight. --Stef
1999 (ispell-send-string "#\n")
2000 (flyspell-unhighlight-at cursor-location)
2001 (setq ispell-pdict-modified-p '(t)))
2002 ((or (eq replace 'buffer) (eq replace 'session))
2003 (ispell-send-string (concat "@" word "\n"))
2004 (flyspell-unhighlight-at cursor-location)
2005 (if (null ispell-pdict-modified-p)
2006 (setq ispell-pdict-modified-p
2007 (list ispell-pdict-modified-p)))
2009 (if (eq replace 'buffer)
2010 (ispell-add-per-file-word-list word)))
2012 ;; This was added only to the Emacs side. I assume its absence on
2013 ;; the XEmacs side was an oversight. --Stef
2014 (flyspell-unhighlight-at cursor-location)
2015 (let ((old-max (point-max))
2016 (new-word (if (atom replace)
2019 (cursor-location (+ (- (length word) (- end start))
2021 (unless (equal new-word (car poss))
2022 (delete-region start end)
2024 (funcall flyspell-insert-function new-word)
2025 (if flyspell-abbrev-p
2026 (flyspell-define-abbrev word new-word)))
2027 ;; In the original Emacs code, this was only called in the body
2028 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2029 (flyspell-ajust-cursor-point save cursor-location old-max)))
2034 ;;*---------------------------------------------------------------------*/
2035 ;;* flyspell-ajust-cursor-point ... */
2036 ;;*---------------------------------------------------------------------*/
2037 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2038 (if (>= save cursor-location)
2039 (let ((new-pos (+ save (- (point-max) old-max))))
2041 ((< new-pos (point-min))
2043 ((> new-pos (point-max))
2048 ;;*---------------------------------------------------------------------*/
2049 ;;* flyspell-emacs-popup ... */
2050 ;;*---------------------------------------------------------------------*/
2051 (defun flyspell-emacs-popup (event poss word)
2052 "The Emacs popup menu."
2054 (let* ((mouse-pos (mouse-position))
2055 (mouse-pos (if (nth 1 mouse-pos)
2057 (set-mouse-position (car mouse-pos)
2058 (/ (frame-width) 2) 2)
2060 (setq event (list (list (car (cdr mouse-pos))
2061 (1+ (cdr (cdr mouse-pos))))
2063 (let* ((corrects (if flyspell-sort-corrections
2064 (sort (car (cdr (cdr poss))) 'string<)
2065 (car (cdr (cdr poss)))))
2066 (cor-menu (if (consp corrects)
2067 (mapcar (lambda (correct)
2068 (list correct correct))
2071 (affix (car (cdr (cdr (cdr poss)))))
2072 (base-menu (let ((save (if (consp affix)
2074 (list (concat "Save affix: " (car affix))
2076 '("Accept (session)" session)
2077 '("Accept (buffer)" buffer))
2078 '(("Save word" save)
2079 ("Accept (session)" session)
2080 ("Accept (buffer)" buffer)))))
2081 (if (consp cor-menu)
2082 (append cor-menu (cons "" save))
2084 (menu (cons "flyspell correction menu" base-menu)))
2085 (car (x-popup-menu event
2086 (list (format "%s [%s]" word (or ispell-local-dictionary
2090 ;;*---------------------------------------------------------------------*/
2091 ;;* flyspell-xemacs-popup ... */
2092 ;;*---------------------------------------------------------------------*/
2093 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2094 "The XEmacs popup menu."
2095 (let* ((corrects (if flyspell-sort-corrections
2096 (sort (car (cdr (cdr poss))) 'string<)
2097 (car (cdr (cdr poss)))))
2098 (cor-menu (if (consp corrects)
2099 (mapcar (lambda (correct)
2101 (list 'flyspell-do-correct
2112 (affix (car (cdr (cdr (cdr poss)))))
2113 (menu (let ((save (if (consp affix)
2115 (concat "Save affix: " (car affix))
2116 (list 'flyspell-do-correct
2127 (list 'flyspell-do-correct
2136 (session (vector "Accept (session)"
2137 (list 'flyspell-do-correct
2146 (buffer (vector "Accept (buffer)"
2147 (list 'flyspell-do-correct
2156 (if (consp cor-menu)
2157 (append cor-menu (list "-" save session buffer))
2158 (list save session buffer)))))
2159 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2163 ;;*---------------------------------------------------------------------*/
2164 ;;* Some example functions for real autocorrecting */
2165 ;;*---------------------------------------------------------------------*/
2166 (defun flyspell-maybe-correct-transposition (beg end poss)
2167 "Check replacements for transposed characters.
2169 If the text between BEG and END is equal to a correction suggested by
2170 Ispell, after transposing two adjacent characters, correct the text,
2173 The third arg POSS is either the symbol 'doublon' or a list of
2174 possible corrections as returned by `ispell-parse-output'.
2176 This function is meant to be added to `flyspell-incorrect-hook'."
2179 (let ((str (buffer-substring beg end))
2180 (i 0) (len (- end beg)) tmp)
2181 (while (< (1+ i) len)
2182 (setq tmp (aref str i))
2183 (aset str i (aref str (1+ i)))
2184 (aset str (1+ i) tmp)
2185 (when (member str (nth 2 poss))
2187 (goto-char (+ beg i 1))
2188 (transpose-chars 1))
2190 (setq tmp (aref str i))
2191 (aset str i (aref str (1+ i)))
2192 (aset str (1+ i) tmp)
2196 (defun flyspell-maybe-correct-doubling (beg end poss)
2197 "Check replacements for doubled characters.
2199 If the text between BEG and END is equal to a correction suggested by
2200 Ispell, after removing a pair of doubled characters, correct the text,
2203 The third arg POSS is either the symbol 'doublon' or a list of
2204 possible corrections as returned by `ispell-parse-output'.
2206 This function is meant to be added to `flyspell-incorrect-hook'."
2209 (let ((str (buffer-substring beg end))
2210 (i 0) (len (- end beg)))
2211 (while (< (1+ i) len)
2212 (when (and (= (aref str i) (aref str (1+ i)))
2213 (member (concat (substring str 0 (1+ i))
2214 (substring str (+ i 2)))
2216 (goto-char (+ beg i))
2222 ;;*---------------------------------------------------------------------*/
2223 ;;* flyspell-already-abbrevp ... */
2224 ;;*---------------------------------------------------------------------*/
2225 (defun flyspell-already-abbrevp (table word)
2226 (let ((sym (abbrev-symbol word table)))
2227 (and sym (symbolp sym))))
2229 ;;*---------------------------------------------------------------------*/
2230 ;;* flyspell-change-abbrev ... */
2231 ;;*---------------------------------------------------------------------*/
2232 (defun flyspell-change-abbrev (table old new)
2233 (set (abbrev-symbol old table) new))
2237 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2238 ;;; flyspell.el ends here