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-predicate */
275 ;;* 'mail-mode-flyspell-verify))) */
276 ;;*---------------------------------------------------------------------*/
277 (defvar flyspell-generic-check-word-predicate 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-predicate)
283 (defvaralias 'flyspell-generic-check-word-p
284 'flyspell-generic-check-word-predicate)
286 ;;*--- mail mode -------------------------------------------------------*/
287 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
288 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
289 (defun mail-mode-flyspell-verify ()
290 "Function used for `flyspell-generic-check-word-predicate' in Mail mode."
291 (let ((header-end (save-excursion
292 (goto-char (point-min))
295 (regexp-quote mail-header-separator)
299 (signature-begin (save-excursion
300 (goto-char (point-max))
301 (re-search-backward message-signature-separator
304 (cond ((< (point) header-end)
305 (and (save-excursion (beginning-of-line)
306 (looking-at "^Subject:"))
307 (> (point) (match-end 0))))
308 ((> (point) signature-begin)
313 (not (looking-at "[>}|]\\|To:")))))))
315 ;;*--- texinfo mode ----------------------------------------------------*/
316 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
317 (defun texinfo-mode-flyspell-verify ()
318 "Function used for `flyspell-generic-check-word-predicate' in Texinfo mode."
321 (not (looking-at "@"))))
323 ;;*--- tex mode --------------------------------------------------------*/
324 (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
325 (defun tex-mode-flyspell-verify ()
326 "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
329 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t)))
331 (let ((this (point-marker))
332 (e (progn (end-of-line) (point-marker))))
334 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t)
335 (and (>= this (match-beginning 0))
336 (<= this (match-end 0)) )))))))
338 ;;*--- sgml mode -------------------------------------------------------*/
339 (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
340 (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
342 (defun sgml-mode-flyspell-verify ()
343 "Function used for `flyspell-generic-check-word-predicate' in SGML mode."
345 (let ((this (point-marker))
346 (s (progn (beginning-of-line) (point-marker)))
347 (e (progn (end-of-line) (point-marker))))
350 (and (re-search-forward "[^<]*>" e t)
351 (= (match-beginning 0) this)))
354 (and (re-search-backward "<[^>]*" s t)
355 (= (match-end 0) this)))
358 (and (re-search-forward "[^&]*;" e t)
359 (= (match-beginning 0) this)))
362 (and (re-search-backward "&[^;]*" s t)
363 (= (match-end 0) this)))))))))
365 ;;*---------------------------------------------------------------------*/
366 ;;* Programming mode */
367 ;;*---------------------------------------------------------------------*/
368 (defvar flyspell-prog-text-faces
369 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
370 "Faces corresponding to text in programming-mode buffers.")
372 (defun flyspell-generic-progmode-verify ()
373 "Used for `flyspell-generic-check-word-predicate' in programming modes."
374 (let ((f (get-text-property (point) 'face)))
375 (memq f flyspell-prog-text-faces)))
378 (defun flyspell-prog-mode ()
379 "Turn on `flyspell-mode' for comments and strings."
381 (setq flyspell-generic-check-word-predicate
382 'flyspell-generic-progmode-verify)
384 (run-hooks 'flyspell-prog-mode-hook))
386 ;;*---------------------------------------------------------------------*/
387 ;;* Overlay compatibility */
388 ;;*---------------------------------------------------------------------*/
389 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
390 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
391 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
392 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
393 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
394 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
395 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
396 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
398 ;;*---------------------------------------------------------------------*/
399 ;;* The minor mode declaration. */
400 ;;*---------------------------------------------------------------------*/
401 (defvar flyspell-mouse-map
402 (let ((map (make-sparse-keymap)))
403 (define-key map (if (featurep 'xemacs) [button2] [down-mouse-2])
404 #'flyspell-correct-word)
406 "Keymap for Flyspell to put on erroneous words.")
408 (defvar flyspell-mode-map
409 (let ((map (make-sparse-keymap)))
410 (if flyspell-use-meta-tab
411 (define-key map "\M-\t" 'flyspell-auto-correct-word))
412 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
413 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
414 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
415 (define-key map [(meta ?\^m)] 'flyspell-correct-word-before-point)
417 "Minor mode keymap for Flyspell mode--for the whole buffer.")
419 ;; dash character machinery
420 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
421 "*Non-nil means that the `-' char is considered as a word delimiter.")
422 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
423 (defvar flyspell-dash-dictionary nil)
424 (make-variable-buffer-local 'flyspell-dash-dictionary)
425 (defvar flyspell-dash-local-dictionary nil)
426 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
428 ;;*---------------------------------------------------------------------*/
430 ;;*---------------------------------------------------------------------*/
431 (defface flyspell-incorrect
432 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
434 "Face used to display a misspelled word in Flyspell."
436 ;; backward-compatibility alias
437 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
439 (defface flyspell-duplicate
440 '((((class color)) (:foreground "Gold3" :bold t :underline t))
442 "Face used to display subsequent occurrences of a misspelled word.
443 See also `flyspell-duplicate-distance'."
445 ;; backward-compatibility alias
446 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
448 (defvar flyspell-overlay nil)
450 ;;*---------------------------------------------------------------------*/
451 ;;* flyspell-mode ... */
452 ;;*---------------------------------------------------------------------*/
453 ;;;###autoload(defvar flyspell-mode nil)
455 (define-minor-mode flyspell-mode
456 "Minor mode performing on-the-fly spelling checking.
457 This spawns a single Ispell process and checks each word.
458 The default flyspell behavior is to highlight incorrect words.
459 With no argument, this command toggles Flyspell mode.
460 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
463 \\[ispell-word]: correct words (using Ispell).
464 \\[flyspell-auto-correct-word]: automatically correct word.
465 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
466 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
469 This runs `flyspell-mode-hook' after flyspell is entered.
472 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
473 valid. For instance, a personal dictionary can be used by
474 invoking `ispell-change-dictionary'.
476 Consider using the `ispell-parser' to check your text. For instance
478 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
481 \\[flyspell-region] checks all words inside a region.
482 \\[flyspell-buffer] checks the whole buffer."
483 :lighter flyspell-mode-line-string
484 :keymap flyspell-mode-map
488 (flyspell-mode-off)))
491 (defun turn-on-flyspell ()
492 "Unconditionally turn on Flyspell mode."
496 (defun turn-off-flyspell ()
497 "Unconditionally turn off Flyspell mode."
500 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
502 ;;*---------------------------------------------------------------------*/
503 ;;* flyspell-buffers ... */
504 ;;* ------------------------------------------------------------- */
505 ;;* For remembering buffers running flyspell */
506 ;;*---------------------------------------------------------------------*/
507 (defvar flyspell-buffers nil)
509 ;;*---------------------------------------------------------------------*/
510 ;;* flyspell-minibuffer-p ... */
511 ;;*---------------------------------------------------------------------*/
512 (defun flyspell-minibuffer-p (buffer)
513 "Is BUFFER a minibuffer?"
514 (let ((ws (get-buffer-window-list buffer t)))
515 (and (consp ws) (window-minibuffer-p (car ws)))))
517 ;;*---------------------------------------------------------------------*/
518 ;;* flyspell-accept-buffer-local-defs ... */
519 ;;*---------------------------------------------------------------------*/
520 (defvar flyspell-last-buffer nil
521 "The buffer in which the last flyspell operation took place.")
523 (defun flyspell-accept-buffer-local-defs (&optional force)
524 ;; When flyspell-word is used inside a loop (e.g. when processing
525 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
526 ;; up dwarfing everything else, so only do it when the buffer has changed.
527 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
528 (setq flyspell-last-buffer (current-buffer))
529 ;; Strange problem: If buffer in current window has font-lock turned on,
530 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
531 ;; call will reset the buffer to the buffer in the current window.
532 ;; However, it only happens at startup (fix by Albert L. Ting).
534 (ispell-accept-buffer-local-defs))
535 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
536 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
537 ;; The dictionary has changed
538 (setq flyspell-dash-dictionary ispell-dictionary)
539 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
540 (setq flyspell-consider-dash-as-word-delimiter-flag
541 (member (or ispell-local-dictionary ispell-dictionary)
542 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
544 (defun flyspell-kill-ispell-hook ()
545 (setq flyspell-last-buffer nil)
546 (dolist (buf (buffer-list))
547 (with-current-buffer buf
548 (kill-local-variable 'flyspell-word-cache-word))))
550 ;; Make sure we flush our caches when needed. Do it here rather than in
551 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
553 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
555 ;;*---------------------------------------------------------------------*/
556 ;;* flyspell-mode-on ... */
557 ;;*---------------------------------------------------------------------*/
558 (defun flyspell-mode-on ()
559 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
560 (ispell-maybe-find-aspell-dictionaries)
561 (setq ispell-highlight-face 'flyspell-incorrect)
562 ;; local dictionaries setup
563 (or ispell-local-dictionary ispell-dictionary
564 (if flyspell-default-dictionary
565 (ispell-change-dictionary flyspell-default-dictionary)))
566 ;; we have to force ispell to accept the local definition or
567 ;; otherwise it could be too late, the local dictionary may
569 ;; Pass the `force' argument for the case where flyspell was active already
570 ;; but the buffer's local-defs have been edited.
571 (flyspell-accept-buffer-local-defs 'force)
572 ;; we put the `flyspell-delayed' property on some commands
573 (flyspell-delay-commands)
574 ;; we put the `flyspell-deplacement' property on some commands
575 (flyspell-deplacement-commands)
576 ;; we bound flyspell action to post-command hook
577 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
578 ;; we bound flyspell action to pre-command hook
579 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
580 ;; we bound flyspell action to after-change hook
581 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
582 ;; set flyspell-generic-check-word-predicate based on the major mode
583 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
585 (setq flyspell-generic-check-word-predicate mode-predicate)))
586 ;; the welcome message
587 (if (and flyspell-issue-message-flag
588 flyspell-issue-welcome-flag
590 (let ((binding (where-is-internal 'flyspell-auto-correct-word
594 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
595 (key-description binding))
596 "Welcome to flyspell. Use Mouse-2 to correct words."))))
597 ;; we end with the flyspell hooks
598 (run-hooks 'flyspell-mode-hook))
600 ;;*---------------------------------------------------------------------*/
601 ;;* flyspell-delay-commands ... */
602 ;;*---------------------------------------------------------------------*/
603 (defun flyspell-delay-commands ()
604 "Install the standard set of Flyspell delayed commands."
605 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
606 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
608 ;;*---------------------------------------------------------------------*/
609 ;;* flyspell-delay-command ... */
610 ;;*---------------------------------------------------------------------*/
611 (defun flyspell-delay-command (command)
612 "Set COMMAND to be delayed, for Flyspell.
613 When flyspell `post-command-hook' is invoked because a delayed command
614 as been used the current word is not immediately checked.
615 It will be checked only after `flyspell-delay' seconds."
616 (interactive "SDelay Flyspell after Command: ")
617 (put command 'flyspell-delayed t))
619 ;;*---------------------------------------------------------------------*/
620 ;;* flyspell-deplacement-commands ... */
621 ;;*---------------------------------------------------------------------*/
622 (defun flyspell-deplacement-commands ()
623 "Install the standard set of Flyspell deplacement commands."
624 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
625 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
627 ;;*---------------------------------------------------------------------*/
628 ;;* flyspell-deplacement-command ... */
629 ;;*---------------------------------------------------------------------*/
630 (defun flyspell-deplacement-command (command)
631 "Set COMMAND that implement cursor movements, for Flyspell.
632 When flyspell `post-command-hook' is invoked because of a deplacement command
633 as been used the current word is checked only if the previous command was
634 not the very same deplacement command."
635 (interactive "SDeplacement Flyspell after Command: ")
636 (put command 'flyspell-deplacement t))
638 ;;*---------------------------------------------------------------------*/
639 ;;* flyspell-word-cache ... */
640 ;;*---------------------------------------------------------------------*/
641 (defvar flyspell-word-cache-start nil)
642 (defvar flyspell-word-cache-end nil)
643 (defvar flyspell-word-cache-word nil)
644 (defvar flyspell-word-cache-result '_)
645 (make-variable-buffer-local 'flyspell-word-cache-start)
646 (make-variable-buffer-local 'flyspell-word-cache-end)
647 (make-variable-buffer-local 'flyspell-word-cache-word)
648 (make-variable-buffer-local 'flyspell-word-cache-result)
650 ;;*---------------------------------------------------------------------*/
651 ;;* The flyspell pre-hook, store the current position. In the */
652 ;;* post command hook, we will check, if the word at this position */
653 ;;* has to be spell checked. */
654 ;;*---------------------------------------------------------------------*/
655 (defvar flyspell-pre-buffer nil)
656 (defvar flyspell-pre-point nil)
657 (defvar flyspell-pre-column nil)
658 (defvar flyspell-pre-pre-buffer nil)
659 (defvar flyspell-pre-pre-point nil)
661 ;;*---------------------------------------------------------------------*/
662 ;;* flyspell-previous-command ... */
663 ;;*---------------------------------------------------------------------*/
664 (defvar flyspell-previous-command nil
665 "The last interactive command checked by Flyspell.")
667 ;;*---------------------------------------------------------------------*/
668 ;;* flyspell-pre-command-hook ... */
669 ;;*---------------------------------------------------------------------*/
670 (defun flyspell-pre-command-hook ()
671 "Save the current buffer and point for Flyspell's post-command hook."
673 (setq flyspell-pre-buffer (current-buffer))
674 (setq flyspell-pre-point (point))
675 (setq flyspell-pre-column (current-column)))
677 ;;*---------------------------------------------------------------------*/
678 ;;* flyspell-mode-off ... */
679 ;;*---------------------------------------------------------------------*/
681 (defun flyspell-mode-off ()
682 "Turn Flyspell mode off."
683 ;; we remove the hooks
684 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
685 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
686 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
687 ;; we remove all the flyspell hilightings
688 (flyspell-delete-all-overlays)
689 ;; we have to erase pre cache variables
690 (setq flyspell-pre-buffer nil)
691 (setq flyspell-pre-point nil)
692 ;; we mark the mode as killed
693 (setq flyspell-mode nil))
695 ;;*---------------------------------------------------------------------*/
696 ;;* flyspell-check-pre-word-p ... */
697 ;;*---------------------------------------------------------------------*/
698 (defun flyspell-check-pre-word-p ()
699 "Return non-nil if we should check the word before point.
700 More precisely, it applies to the word that was before point
701 before the current command."
703 ((or (not (numberp flyspell-pre-point))
704 (not (bufferp flyspell-pre-buffer))
705 (not (buffer-live-p flyspell-pre-buffer)))
707 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
708 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
710 ((or (and (= flyspell-pre-point (- (point) 1))
711 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
712 (= flyspell-pre-point (point))
713 (= flyspell-pre-point (+ (point) 1)))
715 ((and (symbolp this-command)
716 (not executing-kbd-macro)
717 (or (get this-command 'flyspell-delayed)
718 (and (get this-command 'flyspell-deplacement)
719 (eq flyspell-previous-command this-command)))
720 (or (= (current-column) 0)
721 (= (current-column) flyspell-pre-column)
722 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
724 ((not (eq (current-buffer) flyspell-pre-buffer))
726 ((not (and (numberp flyspell-word-cache-start)
727 (numberp flyspell-word-cache-end)))
730 (or (< flyspell-pre-point flyspell-word-cache-start)
731 (> flyspell-pre-point flyspell-word-cache-end)))))
733 ;;*---------------------------------------------------------------------*/
734 ;;* The flyspell after-change-hook, store the change position. In */
735 ;;* the post command hook, we will check, if the word at this */
736 ;;* position has to be spell checked. */
737 ;;*---------------------------------------------------------------------*/
738 (defvar flyspell-changes nil)
739 (make-variable-buffer-local 'flyspell-changes)
741 ;;*---------------------------------------------------------------------*/
742 ;;* flyspell-after-change-function ... */
743 ;;*---------------------------------------------------------------------*/
744 (defun flyspell-after-change-function (start stop len)
745 "Save the current buffer and point for Flyspell's post-command hook."
746 (push (cons start stop) flyspell-changes))
748 ;;*---------------------------------------------------------------------*/
749 ;;* flyspell-check-changed-word-p ... */
750 ;;*---------------------------------------------------------------------*/
751 (defun flyspell-check-changed-word-p (start stop)
752 "Return t when the changed word has to be checked.
753 The answer depends of several criteria.
754 Mostly we check word delimiters."
756 ((and (memq (char-after start) '(?\n ? )) (> stop start))
758 ((not (numberp flyspell-pre-point))
760 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
762 ((let ((pos (point)))
763 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
768 ;;*---------------------------------------------------------------------*/
769 ;;* flyspell-check-word-p ... */
770 ;;*---------------------------------------------------------------------*/
771 (defun flyspell-check-word-p ()
772 "Return t when the word at `point' has to be checked.
773 The answer depends of several criteria.
774 Mostly we check word delimiters."
776 ((<= (- (point-max) 1) (point-min))
777 ;; the buffer is not filled enough
779 ((and (and (> (current-column) 0)
780 (not (eq (current-column) flyspell-pre-column)))
783 (and (looking-at (flyspell-get-not-casechars))
784 (or flyspell-consider-dash-as-word-delimiter-flag
785 (not (looking-at "-"))))))
786 ;; yes because we have reached or typed a word delimiter.
788 ((symbolp this-command)
790 ((get this-command 'flyspell-deplacement)
791 (not (eq flyspell-previous-command this-command)))
792 ((get this-command 'flyspell-delayed)
793 ;; the current command is not delayed, that
794 ;; is that we must check the word now
795 (and (not unread-command-events)
796 (sit-for flyspell-delay)))
800 ;;*---------------------------------------------------------------------*/
801 ;;* flyspell-debug-signal-no-check ... */
802 ;;*---------------------------------------------------------------------*/
803 (defun flyspell-debug-signal-no-check (msg obj)
804 (setq debug-on-error t)
805 (with-current-buffer (get-buffer-create "*flyspell-debug*")
807 (insert "NO-CHECK:\n")
808 (insert (format " %S : %S\n" msg obj))))
810 ;;*---------------------------------------------------------------------*/
811 ;;* flyspell-debug-signal-pre-word-checked ... */
812 ;;*---------------------------------------------------------------------*/
813 (defun flyspell-debug-signal-pre-word-checked ()
814 (setq debug-on-error t)
815 (with-current-buffer (get-buffer-create "*flyspell-debug*")
816 (insert "PRE-WORD:\n")
817 (insert (format " pre-point : %S\n" flyspell-pre-point))
818 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
819 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
820 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
821 (goto-char (point-max))))
823 ;;*---------------------------------------------------------------------*/
824 ;;* flyspell-debug-signal-word-checked ... */
825 ;;*---------------------------------------------------------------------*/
826 (defun flyspell-debug-signal-word-checked ()
827 (setq debug-on-error t)
828 (let ((oldbuf (current-buffer))
830 (with-current-buffer (get-buffer-create "*flyspell-debug*")
832 (insert (format " this-cmd : %S\n" this-command))
833 (insert (format " delayed : %S\n" (and (symbolp this-command)
834 (get this-command 'flyspell-delayed))))
835 (insert (format " point : %S\n" point))
836 (insert (format " prev-char : [%c] %S\n"
837 (with-current-buffer oldbuf
838 (let ((c (if (> (point) (point-min))
841 (char-after (point)))
844 (with-current-buffer oldbuf
845 (let ((c (if (> (point) (point-min))
848 (and (and (looking-at (flyspell-get-not-casechars)) 1)
849 (and (or flyspell-consider-dash-as-word-delimiter-flag
850 (not (looking-at "\\-"))) 2))))))
852 (insert (format " because : %S\n"
854 ((not (and (symbolp this-command)
855 (get this-command 'flyspell-delayed)))
856 ;; the current command is not delayed, that
857 ;; is that we must check the word now
859 ((with-current-buffer oldbuf
860 (let ((c (if (> (point) (point-min))
863 (and (looking-at (flyspell-get-not-casechars))
864 (or flyspell-consider-dash-as-word-delimiter-flag
865 (not (looking-at "\\-"))))))))
867 ;; yes because we have reached or typed a word delimiter.
869 ((not (integerp flyspell-delay))
870 ;; yes because the user had set up a no-delay configuration.
874 (goto-char (point-max)))))
876 ;;*---------------------------------------------------------------------*/
877 ;;* flyspell-debug-signal-changed-checked ... */
878 ;;*---------------------------------------------------------------------*/
879 (defun flyspell-debug-signal-changed-checked ()
880 (setq debug-on-error t)
881 (let ((point (point)))
882 (with-current-buffer (get-buffer-create "*flyspell-debug*")
883 (insert "CHANGED WORD:\n")
884 (insert (format " point : %S\n" point))
885 (goto-char (point-max)))))
887 ;;*---------------------------------------------------------------------*/
888 ;;* flyspell-post-command-hook ... */
889 ;;* ------------------------------------------------------------- */
890 ;;* It is possible that we check several words: */
891 ;;* 1- the current word is checked if the predicate */
892 ;;* FLYSPELL-CHECK-WORD-P is true */
893 ;;* 2- the word that used to be the current word before the */
894 ;;* THIS-COMMAND is checked if: */
895 ;;* a- the previous word is different from the current word */
896 ;;* b- the previous word as not just been checked by the */
897 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
898 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
899 ;;* previous word nor the current word */
900 ;;*---------------------------------------------------------------------*/
901 (defun flyspell-post-command-hook ()
902 "The `post-command-hook' used by flyspell to check a word in-the-fly."
904 (let ((command this-command)
905 ;; Prevent anything we do from affecting the mark.
907 (if (flyspell-check-pre-word-p)
908 (with-current-buffer flyspell-pre-buffer
909 '(flyspell-debug-signal-pre-word-checked)
911 (goto-char flyspell-pre-point)
913 (if (flyspell-check-word-p)
915 '(flyspell-debug-signal-word-checked)
917 ;; we remember which word we have just checked.
918 ;; this will be used next time we will check a word
919 ;; to compare the next current word with the word
920 ;; that as been registered in the pre-command-hook
921 ;; that is these variables are used within the predicate
922 ;; FLYSPELL-CHECK-PRE-WORD-P
923 (setq flyspell-pre-pre-buffer (current-buffer))
924 (setq flyspell-pre-pre-point (point)))
926 (setq flyspell-pre-pre-buffer nil)
927 (setq flyspell-pre-pre-point nil)
928 ;; when a word is not checked because of a delayed command
929 ;; we do not disable the ispell cache.
930 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
932 (setq flyspell-word-cache-end -1)
933 (setq flyspell-word-cache-result '_)))))
934 (while (and (not (input-pending-p)) (consp flyspell-changes))
935 (let ((start (car (car flyspell-changes)))
936 (stop (cdr (car flyspell-changes))))
937 (if (flyspell-check-changed-word-p start stop)
939 '(flyspell-debug-signal-changed-checked)
942 (setq flyspell-changes (cdr flyspell-changes))))
943 (setq flyspell-previous-command command)))
945 ;;*---------------------------------------------------------------------*/
946 ;;* flyspell-notify-misspell ... */
947 ;;*---------------------------------------------------------------------*/
948 (defun flyspell-notify-misspell (word poss)
949 (let ((replacements (if (stringp poss)
951 (if flyspell-sort-corrections
952 (sort (car (cdr (cdr poss))) 'string<)
953 (car (cdr (cdr poss)))))))
954 (if flyspell-issue-message-flag
955 (message "misspelling `%s' %S" word replacements))))
957 ;;*---------------------------------------------------------------------*/
958 ;;* flyspell-word-search-backward ... */
959 ;;*---------------------------------------------------------------------*/
960 (defun flyspell-word-search-backward (word bound)
963 (inhibit-point-motion-hooks t)
965 (while (and (not r) (setq p (search-backward word bound t)))
966 (let ((lw (flyspell-get-word '())))
967 (if (and (consp lw) (string-equal (car lw) word))
972 ;;*---------------------------------------------------------------------*/
973 ;;* flyspell-word-search-forward ... */
974 ;;*---------------------------------------------------------------------*/
975 (defun flyspell-word-search-forward (word bound)
978 (inhibit-point-motion-hooks t)
980 (while (and (not r) (setq p (search-forward word bound t)))
981 (let ((lw (flyspell-get-word '())))
982 (if (and (consp lw) (string-equal (car lw) word))
984 (goto-char (1+ p)))))
987 ;;*---------------------------------------------------------------------*/
988 ;;* flyspell-word ... */
989 ;;*---------------------------------------------------------------------*/
990 (defun flyspell-word (&optional following)
991 "Spell check a word."
992 (interactive (list ispell-following-word))
994 ;; use the correct dictionary
995 (flyspell-accept-buffer-local-defs)
996 (let* ((cursor-location (point))
997 (flyspell-word (flyspell-get-word following))
998 start end poss word ispell-filter)
999 (if (or (eq flyspell-word nil)
1000 (and (fboundp flyspell-generic-check-word-predicate)
1001 (not (funcall flyspell-generic-check-word-predicate))))
1004 ;; destructure return flyspell-word info list.
1005 (setq start (car (cdr flyspell-word))
1006 end (car (cdr (cdr flyspell-word)))
1007 word (car flyspell-word))
1008 ;; before checking in the directory, we check for doublons.
1010 ((and (or (not (eq ispell-parser 'tex))
1011 (and (> start (point-min))
1012 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1013 flyspell-mark-duplications-flag
1019 (- (skip-chars-backward " \t\n\f"))))
1020 (p (when (>= bound (point-min))
1021 (flyspell-word-search-backward word bound))))
1022 (and p (/= p start)))))
1023 ;; yes, this is a doublon
1024 (flyspell-highlight-incorrect-region start end 'doublon)
1026 ((and (eq flyspell-word-cache-start start)
1027 (eq flyspell-word-cache-end end)
1028 (string-equal flyspell-word-cache-word word))
1029 ;; this word had been already checked, we skip
1030 flyspell-word-cache-result)
1031 ((and (eq ispell-parser 'tex)
1032 (flyspell-tex-command-p flyspell-word))
1033 ;; this is a correct word (because a tex command)
1034 (flyspell-unhighlight-at start)
1036 (flyspell-unhighlight-at (- end 1)))
1039 ;; we setup the cache
1040 (setq flyspell-word-cache-start start)
1041 (setq flyspell-word-cache-end end)
1042 (setq flyspell-word-cache-word word)
1043 ;; now check spelling of word.
1044 (ispell-send-string "%\n")
1045 ;; put in verbose mode
1046 (ispell-send-string (concat "^" word "\n"))
1047 ;; we mark the ispell process so it can be killed
1048 ;; when emacs is exited without query
1049 (set-process-query-on-exit-flag ispell-process nil)
1050 ;; Wait until ispell has processed word. Since this code is often
1051 ;; executed from post-command-hook but the ispell process may not
1052 ;; be responsive, it's important to make sure we re-enable C-g.
1055 (accept-process-output ispell-process)
1056 (not (string= "" (car ispell-filter))))))
1057 ;; (ispell-send-string "!\n")
1058 ;; back to terse mode.
1059 ;; Remove leading empty element
1060 (setq ispell-filter (cdr ispell-filter))
1061 ;; ispell process should return something after word is sent.
1062 ;; Tag word as valid (i.e., skip) otherwise
1064 (setq ispell-filter '(*)))
1065 (if (consp ispell-filter)
1066 (setq poss (ispell-parse-output (car ispell-filter))))
1067 (let ((res (cond ((eq poss t)
1069 (setq flyspell-word-cache-result t)
1070 (flyspell-unhighlight-at start)
1072 (flyspell-unhighlight-at (- end 1)))
1074 ((and (stringp poss) flyspell-highlight-flag)
1076 (setq flyspell-word-cache-result t)
1077 (flyspell-unhighlight-at start)
1079 (flyspell-unhighlight-at (- end 1)))
1082 (setq flyspell-word-cache-result t)
1083 (flyspell-unhighlight-at start)
1085 (flyspell-unhighlight-at (- end 1)))
1087 ((or (and (< flyspell-duplicate-distance 0)
1090 (flyspell-word-search-backward
1095 (flyspell-word-search-forward
1098 (and (> flyspell-duplicate-distance 0)
1101 (flyspell-word-search-backward
1104 flyspell-duplicate-distance)))
1107 (flyspell-word-search-forward
1110 flyspell-duplicate-distance))))))
1111 ;; This is a misspelled word which occurs
1112 ;; twice within flyspell-duplicate-distance.
1113 (setq flyspell-word-cache-result nil)
1114 (if flyspell-highlight-flag
1115 (flyspell-highlight-duplicate-region
1117 (message "duplicate `%s'" word))
1120 (setq flyspell-word-cache-result nil)
1121 ;; incorrect highlight the location
1122 (if flyspell-highlight-flag
1123 (flyspell-highlight-incorrect-region
1125 (flyspell-notify-misspell word poss))
1127 ;; return to original location
1128 (goto-char cursor-location)
1129 (if ispell-quit (setq ispell-quit nil))
1132 ;;*---------------------------------------------------------------------*/
1133 ;;* flyspell-tex-math-initialized ... */
1134 ;;*---------------------------------------------------------------------*/
1135 (defvar flyspell-tex-math-initialized nil)
1137 ;;*---------------------------------------------------------------------*/
1138 ;;* flyspell-math-tex-command-p ... */
1139 ;;* ------------------------------------------------------------- */
1140 ;;* This function uses the texmathp package to check if (point) */
1141 ;;* is within a tex command. In order to avoid using */
1142 ;;* condition-case each time we use the variable */
1143 ;;* flyspell-tex-math-initialized to make a special case the first */
1144 ;;* time that function is called. */
1145 ;;*---------------------------------------------------------------------*/
1146 (defun flyspell-math-tex-command-p ()
1147 (when (fboundp 'texmathp)
1149 (flyspell-check-tex-math-command
1151 ((eq flyspell-tex-math-initialized t)
1153 ((eq flyspell-tex-math-initialized 'error)
1156 (setq flyspell-tex-math-initialized t)
1160 (setq flyspell-tex-math-initialized 'error)
1163 ;;*---------------------------------------------------------------------*/
1164 ;;* flyspell-tex-command-p ... */
1165 ;;*---------------------------------------------------------------------*/
1166 (defun flyspell-tex-command-p (word)
1167 "Return t if WORD is a TeX command."
1169 (let ((b (car (cdr word))))
1170 (and (re-search-backward "\\\\" (- (point) 100) t)
1171 (or (= (match-end 0) b)
1172 (and (goto-char (match-end 0))
1173 (looking-at flyspell-tex-command-regexp)
1174 (>= (match-end 0) b))))))
1175 (flyspell-math-tex-command-p)))
1177 ;;*---------------------------------------------------------------------*/
1178 ;;* flyspell-casechars-cache ... */
1179 ;;*---------------------------------------------------------------------*/
1180 (defvar flyspell-casechars-cache nil)
1181 (defvar flyspell-ispell-casechars-cache nil)
1182 (make-variable-buffer-local 'flyspell-casechars-cache)
1183 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1185 ;;*---------------------------------------------------------------------*/
1186 ;;* flyspell-get-casechars ... */
1187 ;;*---------------------------------------------------------------------*/
1188 (defun flyspell-get-casechars ()
1189 "This function builds a string that is the regexp of word chars.
1190 In order to avoid one useless string construction,
1191 this function changes the last char of the `ispell-casechars' string."
1192 (let ((ispell-casechars (ispell-get-casechars)))
1194 ((eq ispell-parser 'tex)
1195 (setq flyspell-ispell-casechars-cache ispell-casechars)
1196 (setq flyspell-casechars-cache
1197 (concat (substring ispell-casechars
1199 (- (length ispell-casechars) 1))
1201 flyspell-casechars-cache)
1203 (setq flyspell-ispell-casechars-cache ispell-casechars)
1204 (setq flyspell-casechars-cache ispell-casechars)
1205 flyspell-casechars-cache))))
1207 ;;*---------------------------------------------------------------------*/
1208 ;;* flyspell-get-not-casechars-cache ... */
1209 ;;*---------------------------------------------------------------------*/
1210 (defvar flyspell-not-casechars-cache nil)
1211 (defvar flyspell-ispell-not-casechars-cache nil)
1212 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1213 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1215 ;;*---------------------------------------------------------------------*/
1216 ;;* flyspell-get-not-casechars ... */
1217 ;;*---------------------------------------------------------------------*/
1218 (defun flyspell-get-not-casechars ()
1219 "This function builds a string that is the regexp of non-word chars."
1220 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1222 ((eq ispell-parser 'tex)
1223 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1224 (setq flyspell-not-casechars-cache
1225 (concat (substring ispell-not-casechars
1227 (- (length ispell-not-casechars) 1))
1229 flyspell-not-casechars-cache)
1231 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1232 (setq flyspell-not-casechars-cache ispell-not-casechars)
1233 flyspell-not-casechars-cache))))
1235 ;;*---------------------------------------------------------------------*/
1236 ;;* flyspell-get-word ... */
1237 ;;*---------------------------------------------------------------------*/
1238 (defun flyspell-get-word (following &optional extra-otherchars)
1239 "Return the word for spell-checking according to Ispell syntax.
1240 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1241 is non-nil when called interactively, then the following word
1242 \(rather than preceding\) is checked when the cursor is not over a word.
1243 Optional second argument contains otherchars that can be included in word
1246 Word syntax described by `flyspell-dictionary-alist' (which see)."
1247 (let* ((flyspell-casechars (flyspell-get-casechars))
1248 (flyspell-not-casechars (flyspell-get-not-casechars))
1249 (ispell-otherchars (ispell-get-otherchars))
1250 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1251 (word-regexp (concat flyspell-casechars
1253 (if (not (string= "" ispell-otherchars))
1254 (concat ispell-otherchars "?"))
1255 (if extra-otherchars
1256 (concat extra-otherchars "?"))
1259 (if (or ispell-many-otherchars-p
1265 (if (not (looking-at flyspell-casechars))
1267 (re-search-forward flyspell-casechars nil t)
1268 (re-search-backward flyspell-casechars nil t)))
1269 ;; move to front of word
1270 (re-search-backward flyspell-not-casechars nil 'start)
1271 (while (and (or (and (not (string= "" ispell-otherchars))
1272 (looking-at ispell-otherchars))
1273 (and extra-otherchars (looking-at extra-otherchars)))
1275 (or (not did-it-once)
1276 ispell-many-otherchars-p)
1277 (not (eq prevpt (point))))
1278 (if (and extra-otherchars (looking-at extra-otherchars))
1281 (if (looking-at flyspell-casechars)
1282 (re-search-backward flyspell-not-casechars nil 'move)))
1286 (if (looking-at flyspell-casechars)
1287 (re-search-backward flyspell-not-casechars nil 'move)
1288 (backward-char -1))))
1289 ;; Now mark the word and save to string.
1290 (if (not (re-search-forward word-regexp nil t))
1293 (setq start (match-beginning 0)
1295 word (buffer-substring-no-properties start end))
1296 (list word start end)))))
1298 ;;*---------------------------------------------------------------------*/
1299 ;;* flyspell-small-region ... */
1300 ;;*---------------------------------------------------------------------*/
1301 (defun flyspell-small-region (beg end)
1302 "Flyspell text between BEG and END."
1310 (while (< (point) end)
1311 (if (and flyspell-issue-message-flag (= count 100))
1313 (message "Spell Checking...%d%%"
1314 (* 100 (/ (float (- (point) beg)) (- end beg))))
1316 (setq count (+ 1 count)))
1319 (let ((cur (point)))
1321 (if (and (< (point) end) (> (point) (+ cur 1)))
1322 (backward-char 1)))))
1324 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1327 ;;*---------------------------------------------------------------------*/
1328 ;;* flyspell-external-ispell-process ... */
1329 ;;*---------------------------------------------------------------------*/
1330 (defvar flyspell-external-ispell-process '()
1331 "The external Flyspell Ispell process.")
1333 ;;*---------------------------------------------------------------------*/
1334 ;;* flyspell-external-ispell-buffer ... */
1335 ;;*---------------------------------------------------------------------*/
1336 (defvar flyspell-external-ispell-buffer '())
1337 (defvar flyspell-large-region-buffer '())
1338 (defvar flyspell-large-region-beg (point-min))
1339 (defvar flyspell-large-region-end (point-max))
1341 ;;*---------------------------------------------------------------------*/
1342 ;;* flyspell-external-point-words ... */
1343 ;;*---------------------------------------------------------------------*/
1344 (defun flyspell-external-point-words ()
1345 "Mark words from a buffer listing incorrect words in order of appearance.
1346 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1347 \(We finish by killing that buffer and setting the variable to nil.)
1348 The buffer to mark them in is `flyspell-large-region-buffer'."
1349 (let (words-not-found
1350 (ispell-otherchars (ispell-get-otherchars))
1351 (buffer-scan-pos flyspell-large-region-beg)
1353 (with-current-buffer flyspell-external-ispell-buffer
1354 (goto-char (point-min))
1355 ;; Loop over incorrect words, in the order they were reported,
1356 ;; which is also the order they appear in the buffer being checked.
1357 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1358 ;; Bind WORD to the next one.
1359 (let ((word (match-string 1)) (wordpos (point)))
1360 ;; Here there used to be code to see if WORD is the same
1361 ;; as the previous iteration, and count the number of consecutive
1362 ;; identical words, and the loop below would search for that many.
1363 ;; That code seemed to be incorrect, and on principle, should
1364 ;; be unnecessary too. -- rms.
1365 (if flyspell-issue-message-flag
1366 (message "Spell Checking...%d%% [%s]"
1367 (* 100 (/ (float (point)) (point-max)))
1369 (with-current-buffer flyspell-large-region-buffer
1370 (goto-char buffer-scan-pos)
1372 ;; Iterate on string search until string is found as word,
1375 (if (search-forward word
1376 flyspell-large-region-end t)
1379 ;; Move back into the match
1380 ;; so flyspell-get-word will find it.
1382 (flyspell-get-word nil)))
1383 (found (car found-list))
1384 (found-length (length found))
1385 (misspell-length (length word)))
1387 ;; Size matches, we really found it.
1388 (= found-length misspell-length)
1389 ;; Matches as part of a boundary-char separated word
1391 (split-string found ispell-otherchars))
1392 ;; Misspelling has higher length than
1393 ;; what flyspell considers the
1394 ;; word. Caused by boundary-chars
1395 ;; mismatch. Validating seems safe.
1396 (< found-length misspell-length)
1397 ;; ispell treats beginning of some TeX
1398 ;; commands as nroff control sequences
1399 ;; and strips them in the list of
1400 ;; misspelled words thus giving a
1401 ;; non-existent word. Skip if ispell
1402 ;; is used, string is a TeX command
1403 ;; (char before beginning of word is
1404 ;; backslash) and none of the previous
1406 (and (not ispell-really-aspell)
1408 (goto-char (- (nth 1 found-list) 1))
1409 (if (looking-at "[\\]" )
1414 ;; Search for next misspelled word will begin from
1415 ;; end of last validated match.
1416 (setq buffer-scan-pos (point))))
1417 ;; Record if misspelling is not found and try new one
1418 (add-to-list 'words-not-found
1419 (concat " -> " word " - "
1420 (int-to-string wordpos)))
1421 (setq keep nil)))))))
1423 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1424 ;; Warn about not found misspellings
1425 (dolist (word words-not-found)
1426 (message "%s: word not found" word))
1427 ;; Kill and forget the buffer with the list of incorrect words.
1428 (kill-buffer flyspell-external-ispell-buffer)
1429 (setq flyspell-external-ispell-buffer nil)))
1431 ;;*---------------------------------------------------------------------*/
1432 ;;* flyspell-process-localwords ... */
1433 ;;* ------------------------------------------------------------- */
1434 ;;* This function is used to prevent marking of words explicitly */
1435 ;;* declared correct. */
1436 ;;*---------------------------------------------------------------------*/
1437 (defun flyspell-process-localwords (misspellings-buffer)
1438 (let (localwords case-fold-search
1439 (ispell-casechars (ispell-get-casechars)))
1440 ;; Get localwords from the original buffer
1442 (goto-char (point-min))
1443 ;; Localwords parsing copied from ispell.el.
1444 (while (search-forward ispell-words-keyword nil t)
1445 (let ((end (save-excursion (end-of-line) (point)))
1447 ;; buffer-local words separated by a space, and can contain
1448 ;; any character other than a space. Not rigorous enough.
1449 (while (re-search-forward " *\\([^ ]+\\)" end t)
1450 (setq string (buffer-substring-no-properties (match-beginning 1)
1452 ;; This can fail when string contains a word with invalid chars.
1453 ;; Error handling needs to be added between Ispell and Emacs.
1454 (if (and (< 1 (length string))
1455 (equal 0 (string-match ispell-casechars string)))
1456 (push string localwords))))))
1457 ;; Remove localwords matches from misspellings-buffer.
1458 ;; The usual mechanism of communicating the local words to ispell
1459 ;; does not affect the special ispell process used by
1460 ;; flyspell-large-region.
1461 (with-current-buffer misspellings-buffer
1463 (dolist (word localwords)
1464 (goto-char (point-min))
1465 (let ((regexp (concat "^" word "\n")))
1466 (while (re-search-forward regexp nil t)
1467 (delete-region (match-beginning 0) (match-end 0)))))))))
1469 ;;* ---------------------------------------------------------------
1470 ;;* flyspell-check-region-doublons
1471 ;;* ---------------------------------------------------------------
1472 (defun flyspell-check-region-doublons (beg end)
1473 "Check for adjacent duplicated words (doublons) in the given region."
1476 (flyspell-word) ; Make sure current word is checked
1478 (while (and (< (point) end)
1479 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1485 ;;*---------------------------------------------------------------------*/
1486 ;;* flyspell-large-region ... */
1487 ;;*---------------------------------------------------------------------*/
1488 (defun flyspell-large-region (beg end)
1489 (let* ((curbuf (current-buffer))
1490 (buffer (get-buffer-create "*flyspell-region*")))
1491 (setq flyspell-external-ispell-buffer buffer)
1492 (setq flyspell-large-region-buffer curbuf)
1493 (setq flyspell-large-region-beg beg)
1494 (setq flyspell-large-region-end end)
1495 (flyspell-accept-buffer-local-defs)
1498 ;; this is done, we can start checking...
1499 (if flyspell-issue-message-flag (message "Checking region..."))
1501 (ispell-check-version)
1502 (let ((c (apply 'call-process-region beg
1508 (if ispell-really-aspell "list" "-l")
1510 ;; Local dictionary becomes the global dictionary in use.
1511 (if ispell-local-dictionary
1512 (setq ispell-dictionary ispell-local-dictionary))
1513 (setq args (ispell-get-ispell-args))
1514 (if ispell-dictionary ; use specified dictionary
1516 (append (list "-d" ispell-dictionary) args)))
1517 (if ispell-personal-dictionary ; use specified pers dict
1522 ispell-personal-dictionary)))))
1523 (setq args (append args ispell-extra-args))
1527 (flyspell-process-localwords buffer)
1528 (with-current-buffer curbuf
1529 (flyspell-delete-region-overlays beg end)
1530 (flyspell-check-region-doublons beg end))
1531 (flyspell-external-point-words))
1532 (error "Can't check region...")))))
1534 ;;*---------------------------------------------------------------------*/
1535 ;;* flyspell-region ... */
1536 ;;* ------------------------------------------------------------- */
1537 ;;* Because `ispell -a' is too slow, it is not possible to use */
1538 ;;* it on large region. Then, when ispell is invoked on a large */
1539 ;;* text region, a new `ispell -l' process is spawned. The */
1540 ;;* pointed out words are then searched in the region a checked with */
1541 ;;* regular flyspell means. */
1542 ;;*---------------------------------------------------------------------*/
1544 (defun flyspell-region (beg end)
1545 "Flyspell text between BEG and END."
1554 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1555 (flyspell-large-region beg end)
1556 (flyspell-small-region beg end)))))
1558 ;;*---------------------------------------------------------------------*/
1559 ;;* flyspell-buffer ... */
1560 ;;*---------------------------------------------------------------------*/
1562 (defun flyspell-buffer ()
1563 "Flyspell whole buffer."
1565 (flyspell-region (point-min) (point-max)))
1567 ;;*---------------------------------------------------------------------*/
1568 ;;* old next error position ... */
1569 ;;*---------------------------------------------------------------------*/
1570 (defvar flyspell-old-buffer-error nil)
1571 (defvar flyspell-old-pos-error nil)
1573 ;;*---------------------------------------------------------------------*/
1574 ;;* flyspell-goto-next-error ... */
1575 ;;*---------------------------------------------------------------------*/
1576 (defun flyspell-goto-next-error ()
1577 "Go to the next previously detected error.
1578 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1583 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1584 (eq pos flyspell-old-pos-error))
1586 (if (= flyspell-old-pos-error max)
1587 ;; goto beginning of buffer
1589 (message "Restarting from beginning of buffer")
1590 (goto-char (point-min)))
1592 (setq pos (point))))
1593 ;; seek the next error
1594 (while (and (< pos max)
1595 (let ((ovs (overlays-at pos))
1597 (while (and (not r) (consp ovs))
1598 (if (flyspell-overlay-p (car ovs))
1600 (setq ovs (cdr ovs))))
1602 (setq pos (1+ pos)))
1603 ;; save the current location for next invocation
1604 (setq flyspell-old-pos-error pos)
1605 (setq flyspell-old-buffer-error (current-buffer))
1608 (message "No more miss-spelled word!"))))
1610 ;;*---------------------------------------------------------------------*/
1611 ;;* flyspell-overlay-p ... */
1612 ;;*---------------------------------------------------------------------*/
1613 (defun flyspell-overlay-p (o)
1614 "A predicate that return true iff O is an overlay used by flyspell."
1615 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1617 ;;*---------------------------------------------------------------------*/
1618 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1619 ;;* ------------------------------------------------------------- */
1620 ;;* Remove overlays introduced by flyspell. */
1621 ;;*---------------------------------------------------------------------*/
1622 (defun flyspell-delete-region-overlays (beg end)
1623 "Delete overlays used by flyspell in a given region."
1624 (remove-overlays beg end 'flyspell-overlay t))
1627 (defun flyspell-delete-all-overlays ()
1628 "Delete all the overlays used by flyspell."
1629 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1631 ;;*---------------------------------------------------------------------*/
1632 ;;* flyspell-unhighlight-at ... */
1633 ;;*---------------------------------------------------------------------*/
1634 (defun flyspell-unhighlight-at (pos)
1635 "Remove the flyspell overlay that are located at POS."
1636 (if flyspell-persistent-highlight
1637 (let ((overlays (overlays-at pos)))
1638 (while (consp overlays)
1639 (if (flyspell-overlay-p (car overlays))
1640 (delete-overlay (car overlays)))
1641 (setq overlays (cdr overlays))))
1642 (if (flyspell-overlay-p flyspell-overlay)
1643 (delete-overlay flyspell-overlay))))
1645 ;;*---------------------------------------------------------------------*/
1646 ;;* flyspell-properties-at-p ... */
1647 ;;* ------------------------------------------------------------- */
1648 ;;* Is there an highlight properties at position pos? */
1649 ;;*---------------------------------------------------------------------*/
1650 (defun flyspell-properties-at-p (pos)
1651 "Return t if there is a text property at POS, not counting `local-map'.
1652 If variable `flyspell-highlight-properties' is set to nil,
1653 text with properties are not checked. This function is used to discover
1654 if the character at POS has any other property."
1655 (let ((prop (text-properties-at pos))
1657 (while (and keep (consp prop))
1658 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1659 (setq prop (cdr (cdr prop)))
1663 ;;*---------------------------------------------------------------------*/
1664 ;;* make-flyspell-overlay ... */
1665 ;;*---------------------------------------------------------------------*/
1666 (defun make-flyspell-overlay (beg end face mouse-face)
1667 "Allocate an overlay to highlight an incorrect word.
1668 BEG and END specify the range in the buffer of that word.
1669 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1671 (let ((overlay (make-overlay beg end nil t nil)))
1672 (overlay-put overlay 'face face)
1673 (overlay-put overlay 'mouse-face mouse-face)
1674 (overlay-put overlay 'flyspell-overlay t)
1675 (overlay-put overlay 'evaporate t)
1676 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1677 (overlay-put overlay 'keymap flyspell-mouse-map)
1678 (when (eq face 'flyspell-incorrect)
1679 (and (stringp flyspell-before-incorrect-word-string)
1680 (overlay-put overlay 'before-string
1681 flyspell-before-incorrect-word-string))
1682 (and (stringp flyspell-after-incorrect-word-string)
1683 (overlay-put overlay 'after-string
1684 flyspell-after-incorrect-word-string)))
1687 ;;*---------------------------------------------------------------------*/
1688 ;;* flyspell-highlight-incorrect-region ... */
1689 ;;*---------------------------------------------------------------------*/
1690 (defun flyspell-highlight-incorrect-region (beg end poss)
1691 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1692 POSS is usually a list of possible spelling/correction lists,
1693 as returned by `ispell-parse-output'.
1694 It can also be the symbol `doublon', in the case where the word
1695 is itself incorrect, but suspiciously repeated."
1696 (let ((inhibit-read-only t))
1697 (unless (run-hook-with-args-until-success
1698 'flyspell-incorrect-hook beg end poss)
1699 (if (or flyspell-highlight-properties
1700 (not (flyspell-properties-at-p beg)))
1702 ;; we cleanup all the overlay that are in the region, not
1703 ;; beginning at the word start position
1704 (if (< (1+ beg) end)
1705 (let ((os (overlays-in (1+ beg) end)))
1707 (if (flyspell-overlay-p (car os))
1708 (delete-overlay (car os)))
1709 (setq os (cdr os)))))
1710 ;; we cleanup current overlay at the same position
1711 (flyspell-unhighlight-at beg)
1712 ;; now we can use a new overlay
1713 (setq flyspell-overlay
1714 (make-flyspell-overlay
1716 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1719 ;;*---------------------------------------------------------------------*/
1720 ;;* flyspell-highlight-duplicate-region ... */
1721 ;;*---------------------------------------------------------------------*/
1722 (defun flyspell-highlight-duplicate-region (beg end poss)
1723 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1724 POSS is a list of possible spelling/correction lists,
1725 as returned by `ispell-parse-output'."
1726 (let ((inhibit-read-only t))
1727 (unless (run-hook-with-args-until-success
1728 'flyspell-incorrect-hook beg end poss)
1729 (if (or flyspell-highlight-properties
1730 (not (flyspell-properties-at-p beg)))
1732 ;; we cleanup current overlay at the same position
1733 (flyspell-unhighlight-at beg)
1734 ;; now we can use a new overlay
1735 (setq flyspell-overlay
1736 (make-flyspell-overlay beg end
1740 ;;*---------------------------------------------------------------------*/
1741 ;;* flyspell-auto-correct-cache ... */
1742 ;;*---------------------------------------------------------------------*/
1743 (defvar flyspell-auto-correct-pos nil)
1744 (defvar flyspell-auto-correct-region nil)
1745 (defvar flyspell-auto-correct-ring nil)
1746 (defvar flyspell-auto-correct-word nil)
1747 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1748 (make-variable-buffer-local 'flyspell-auto-correct-region)
1749 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1750 (make-variable-buffer-local 'flyspell-auto-correct-word)
1752 ;;*---------------------------------------------------------------------*/
1753 ;;* flyspell-check-previous-highlighted-word ... */
1754 ;;*---------------------------------------------------------------------*/
1755 (defun flyspell-check-previous-highlighted-word (&optional arg)
1756 "Correct the closer misspelled word.
1757 This function scans a mis-spelled word before the cursor. If it finds one
1758 it proposes replacement for that word. With prefix arg, count that many
1759 misspelled words backwards."
1761 (let ((pos1 (point))
1763 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1766 (while (and (setq pos (previous-overlay-change pos))
1769 (if (> pos (point-min))
1771 (setq ovs (overlays-at (1- pos)))
1774 (setq ovs (cdr ovs))
1775 (if (and (flyspell-overlay-p ov)
1776 (= 0 (setq arg (1- arg))))
1777 (throw 'exit t)))))))
1781 (error "No word to correct before point"))))
1783 ;;*---------------------------------------------------------------------*/
1784 ;;* flyspell-display-next-corrections ... */
1785 ;;*---------------------------------------------------------------------*/
1786 (defun flyspell-display-next-corrections (corrections)
1787 (let ((string "Corrections:")
1790 (while (< (length string) 80)
1791 (if (equal (car l) flyspell-auto-correct-word)
1792 (setq pos (cons (+ 1 (length string)) pos)))
1793 (setq string (concat string " " (car l)))
1796 (let ((num (car pos)))
1797 (put-text-property num
1798 (+ num (length flyspell-auto-correct-word))
1799 'face 'flyspell-incorrect
1801 (setq pos (cdr pos)))
1802 (if (fboundp 'display-message)
1803 (display-message 'no-log string)
1804 (message "%s" string))))
1806 ;;*---------------------------------------------------------------------*/
1807 ;;* flyspell-abbrev-table ... */
1808 ;;*---------------------------------------------------------------------*/
1809 (defun flyspell-abbrev-table ()
1810 (if flyspell-use-global-abbrev-table-p
1812 (or local-abbrev-table global-abbrev-table)))
1814 ;;*---------------------------------------------------------------------*/
1815 ;;* flyspell-define-abbrev ... */
1816 ;;*---------------------------------------------------------------------*/
1817 (defun flyspell-define-abbrev (name expansion)
1818 (let ((table (flyspell-abbrev-table)))
1820 (define-abbrev table name expansion))))
1822 ;;*---------------------------------------------------------------------*/
1823 ;;* flyspell-auto-correct-word ... */
1824 ;;*---------------------------------------------------------------------*/
1825 (defun flyspell-auto-correct-word ()
1826 "Correct the current word.
1827 This command proposes various successive corrections for the current word."
1830 (old-max (point-max)))
1831 ;; use the correct dictionary
1832 (flyspell-accept-buffer-local-defs)
1833 (if (and (eq flyspell-auto-correct-pos pos)
1834 (consp flyspell-auto-correct-region))
1835 ;; we have already been using the function at the same location
1836 (let* ((start (car flyspell-auto-correct-region))
1837 (len (cdr flyspell-auto-correct-region)))
1838 (flyspell-unhighlight-at start)
1839 (delete-region start (+ start len))
1840 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1841 (let* ((word (car flyspell-auto-correct-ring))
1842 (len (length word)))
1843 (rplacd flyspell-auto-correct-region len)
1845 (if flyspell-abbrev-p
1846 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1847 flyspell-auto-correct-word)
1848 (flyspell-change-abbrev (flyspell-abbrev-table)
1849 flyspell-auto-correct-word
1851 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1852 (funcall flyspell-insert-function word)
1854 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1855 (flyspell-ajust-cursor-point pos (point) old-max)
1856 (setq flyspell-auto-correct-pos (point)))
1857 ;; fetch the word to be checked
1858 (let ((word (flyspell-get-word nil)))
1860 (let ((start (car (cdr word)))
1861 (end (car (cdr (cdr word))))
1864 (setq flyspell-auto-correct-word word)
1865 ;; now check spelling of word.
1866 (ispell-send-string "%\n") ;put in verbose mode
1867 (ispell-send-string (concat "^" word "\n"))
1868 ;; wait until ispell has processed word.
1870 (accept-process-output ispell-process)
1871 (not (string= "" (car ispell-filter)))))
1872 ;; Remove leading empty element
1873 (setq ispell-filter (cdr ispell-filter))
1874 ;; ispell process should return something after word is sent.
1875 ;; Tag word as valid (i.e., skip) otherwise
1877 (setq ispell-filter '(*)))
1878 (if (consp ispell-filter)
1879 (setq poss (ispell-parse-output (car ispell-filter))))
1881 ((or (eq poss t) (stringp poss))
1882 ;; don't correct word
1886 (error "Ispell: error in Ispell process"))
1888 ;; the word is incorrect, we have to propose a replacement
1889 (let ((replacements (if flyspell-sort-corrections
1890 (sort (car (cdr (cdr poss))) 'string<)
1891 (car (cdr (cdr poss))))))
1892 (setq flyspell-auto-correct-region nil)
1893 (if (consp replacements)
1895 (let ((replace (car replacements)))
1896 (let ((new-word replace))
1897 (if (not (equal new-word (car poss)))
1899 ;; the save the current replacements
1900 (setq flyspell-auto-correct-region
1901 (cons start (length new-word)))
1902 (let ((l replacements))
1903 (while (consp (cdr l))
1905 (rplacd l (cons (car poss) replacements)))
1906 (setq flyspell-auto-correct-ring
1908 (flyspell-unhighlight-at start)
1909 (delete-region start end)
1910 (funcall flyspell-insert-function new-word)
1911 (if flyspell-abbrev-p
1912 (if (flyspell-already-abbrevp
1913 (flyspell-abbrev-table) word)
1914 (flyspell-change-abbrev
1915 (flyspell-abbrev-table)
1918 (flyspell-define-abbrev word
1921 (flyspell-display-next-corrections
1922 (cons new-word flyspell-auto-correct-ring))
1923 (flyspell-ajust-cursor-point pos
1926 (setq flyspell-auto-correct-pos (point))
1927 (ispell-pdict-save t)))))))
1929 ;;*---------------------------------------------------------------------*/
1930 ;;* flyspell-auto-correct-previous-pos ... */
1931 ;;*---------------------------------------------------------------------*/
1932 (defvar flyspell-auto-correct-previous-pos nil
1933 "Holds the start of the first incorrect word before point.")
1935 ;;*---------------------------------------------------------------------*/
1936 ;;* flyspell-auto-correct-previous-hook ... */
1937 ;;*---------------------------------------------------------------------*/
1938 (defun flyspell-auto-correct-previous-hook ()
1939 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1940 Sets `flyspell-auto-correct-previous-pos' to nil"
1942 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1943 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1944 (setq flyspell-auto-correct-previous-pos nil)))
1946 ;;*---------------------------------------------------------------------*/
1947 ;;* flyspell-auto-correct-previous-word ... */
1948 ;;*---------------------------------------------------------------------*/
1949 (defun flyspell-auto-correct-previous-word (position)
1950 "Auto correct the first mispelled word that occurs before point.
1951 But don't look beyond what's visible on the screen."
1956 (move-to-window-line 0)
1958 (move-to-window-line -1)
1962 (narrow-to-region top bot)
1963 (overlay-recenter (point))
1965 (add-hook 'pre-command-hook
1966 (function flyspell-auto-correct-previous-hook) t t)
1968 (unless flyspell-auto-correct-previous-pos
1969 ;; only reset if a new overlay exists
1970 (setq flyspell-auto-correct-previous-pos nil)
1972 (let ((overlay-list (overlays-in (point-min) position))
1973 (new-overlay 'dummy-value))
1975 ;; search for previous (new) flyspell overlay
1976 (while (and new-overlay
1977 (or (not (flyspell-overlay-p new-overlay))
1978 ;; check if its face has changed
1979 (not (eq (get-char-property
1980 (overlay-start new-overlay) 'face)
1981 'flyspell-incorrect))))
1982 (setq new-overlay (car-safe overlay-list))
1983 (setq overlay-list (cdr-safe overlay-list)))
1985 ;; if nothing new exits new-overlay should be nil
1986 (if new-overlay ;; the length of the word may change so go to the start
1987 (setq flyspell-auto-correct-previous-pos
1988 (overlay-start new-overlay)))))
1990 (when flyspell-auto-correct-previous-pos
1992 (goto-char flyspell-auto-correct-previous-pos)
1993 (let ((ispell-following-word t)) ;; point is at start
1994 (if (numberp flyspell-auto-correct-previous-pos)
1995 (goto-char flyspell-auto-correct-previous-pos))
1996 (flyspell-auto-correct-word))
1997 ;; the point may have moved so reset this
1998 (setq flyspell-auto-correct-previous-pos (point))))))))
2000 ;;*---------------------------------------------------------------------*/
2001 ;;* flyspell-correct-word ... */
2002 ;;*---------------------------------------------------------------------*/
2004 (defun flyspell-correct-word (event)
2005 "Pop up a menu of possible corrections for a misspelled word.
2006 The word checked is the word at the mouse position."
2008 (let ((save (point)))
2009 (mouse-set-point event)
2010 (flyspell-correct-word-before-point event save)))
2012 (defun flyspell-correct-word-before-point (&optional event opoint)
2013 "Pop up a menu of possible corrections for misspelled word before point.
2014 If EVENT is non-nil, it is the mouse event that invoked this operation;
2015 that controls where to put the menu.
2016 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2018 (unless (mouse-position)
2019 (error "Pop-up menus do not work on this terminal"))
2020 ;; use the correct dictionary
2021 (flyspell-accept-buffer-local-defs)
2022 (let ((cursor-location (point))
2023 (word (flyspell-get-word nil)))
2025 (let ((start (car (cdr word)))
2026 (end (car (cdr (cdr word))))
2029 ;; now check spelling of word.
2030 (ispell-send-string "%\n") ;put in verbose mode
2031 (ispell-send-string (concat "^" word "\n"))
2032 ;; wait until ispell has processed word
2034 (accept-process-output ispell-process)
2035 (not (string= "" (car ispell-filter)))))
2036 ;; Remove leading empty element
2037 (setq ispell-filter (cdr ispell-filter))
2038 ;; ispell process should return something after word is sent.
2039 ;; Tag word as valid (i.e., skip) otherwise
2041 (setq ispell-filter '(*)))
2042 (if (consp ispell-filter)
2043 (setq poss (ispell-parse-output (car ispell-filter))))
2045 ((or (eq poss t) (stringp poss))
2046 ;; don't correct word
2050 (error "Ispell: error in Ispell process"))
2052 (flyspell-xemacs-popup
2053 poss word cursor-location start end opoint))
2055 ;; The word is incorrect, we have to propose a replacement.
2056 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2057 poss word cursor-location start end opoint)))
2058 (ispell-pdict-save t)))))
2060 ;;*---------------------------------------------------------------------*/
2061 ;;* flyspell-do-correct ... */
2062 ;;*---------------------------------------------------------------------*/
2063 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2064 "The popup menu callback."
2065 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2066 ;; it instead right after calling the function.
2067 (cond ((eq replace 'ignore)
2072 (ispell-send-string (concat "*" word "\n"))
2073 ;; This was added only to the XEmacs side in revision 1.18 of
2074 ;; flyspell. I assume its absence on the Emacs side was an
2075 ;; oversight. --Stef
2076 (ispell-send-string "#\n")
2077 (flyspell-unhighlight-at cursor-location)
2078 (setq ispell-pdict-modified-p '(t)))
2079 ((or (eq replace 'buffer) (eq replace 'session))
2080 (ispell-send-string (concat "@" word "\n"))
2081 (flyspell-unhighlight-at cursor-location)
2082 (if (null ispell-pdict-modified-p)
2083 (setq ispell-pdict-modified-p
2084 (list ispell-pdict-modified-p)))
2086 (if (eq replace 'buffer)
2087 (ispell-add-per-file-word-list word)))
2089 ;; This was added only to the Emacs side. I assume its absence on
2090 ;; the XEmacs side was an oversight. --Stef
2091 (flyspell-unhighlight-at cursor-location)
2092 (let ((old-max (point-max))
2093 (new-word (if (atom replace)
2096 (cursor-location (+ (- (length word) (- end start))
2098 (unless (equal new-word (car poss))
2099 (delete-region start end)
2101 (funcall flyspell-insert-function new-word)
2102 (if flyspell-abbrev-p
2103 (flyspell-define-abbrev word new-word)))
2104 ;; In the original Emacs code, this was only called in the body
2105 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2106 (flyspell-ajust-cursor-point save cursor-location old-max)))
2111 ;;*---------------------------------------------------------------------*/
2112 ;;* flyspell-ajust-cursor-point ... */
2113 ;;*---------------------------------------------------------------------*/
2114 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2115 (if (>= save cursor-location)
2116 (let ((new-pos (+ save (- (point-max) old-max))))
2118 ((< new-pos (point-min))
2120 ((> new-pos (point-max))
2125 ;;*---------------------------------------------------------------------*/
2126 ;;* flyspell-emacs-popup ... */
2127 ;;*---------------------------------------------------------------------*/
2128 (defun flyspell-emacs-popup (event poss word)
2129 "The Emacs popup menu."
2131 (let* ((mouse-pos (mouse-position))
2132 (mouse-pos (if (nth 1 mouse-pos)
2134 (set-mouse-position (car mouse-pos)
2135 (/ (frame-width) 2) 2)
2137 (setq event (list (list (car (cdr mouse-pos))
2138 (1+ (cdr (cdr mouse-pos))))
2140 (let* ((corrects (if flyspell-sort-corrections
2141 (sort (car (cdr (cdr poss))) 'string<)
2142 (car (cdr (cdr poss)))))
2143 (cor-menu (if (consp corrects)
2144 (mapcar (lambda (correct)
2145 (list correct correct))
2148 (affix (car (cdr (cdr (cdr poss)))))
2150 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2152 (list (concat "Save affix: " (car affix))
2154 '("Accept (session)" session)
2155 '("Accept (buffer)" buffer))
2156 '(("Save word" save)
2157 ("Accept (session)" session)
2158 ("Accept (buffer)" buffer)))))
2159 (if (consp cor-menu)
2160 (append cor-menu (cons "" save))
2162 (menu (cons "flyspell correction menu" base-menu)))
2163 (car (x-popup-menu event
2164 (list (format "%s [%s]" word (or ispell-local-dictionary
2168 ;;*---------------------------------------------------------------------*/
2169 ;;* flyspell-xemacs-popup ... */
2170 ;;*---------------------------------------------------------------------*/
2171 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2172 "The XEmacs popup menu."
2173 (let* ((corrects (if flyspell-sort-corrections
2174 (sort (car (cdr (cdr poss))) 'string<)
2175 (car (cdr (cdr poss)))))
2176 (cor-menu (if (consp corrects)
2177 (mapcar (lambda (correct)
2179 (list 'flyspell-do-correct
2190 (affix (car (cdr (cdr (cdr poss)))))
2192 (menu (let ((save (if (and (consp affix) show-affix-info)
2194 (concat "Save affix: " (car affix))
2195 (list 'flyspell-do-correct
2206 (list 'flyspell-do-correct
2215 (session (vector "Accept (session)"
2216 (list 'flyspell-do-correct
2225 (buffer (vector "Accept (buffer)"
2226 (list 'flyspell-do-correct
2235 (if (consp cor-menu)
2236 (append cor-menu (list "-" save session buffer))
2237 (list save session buffer)))))
2238 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2242 ;;*---------------------------------------------------------------------*/
2243 ;;* Some example functions for real autocorrecting */
2244 ;;*---------------------------------------------------------------------*/
2245 (defun flyspell-maybe-correct-transposition (beg end poss)
2246 "Check replacements for transposed characters.
2248 If the text between BEG and END is equal to a correction suggested by
2249 Ispell, after transposing two adjacent characters, correct the text,
2252 The third arg POSS is either the symbol 'doublon' or a list of
2253 possible corrections as returned by `ispell-parse-output'.
2255 This function is meant to be added to `flyspell-incorrect-hook'."
2258 (let ((str (buffer-substring beg end))
2259 (i 0) (len (- end beg)) tmp)
2260 (while (< (1+ i) len)
2261 (setq tmp (aref str i))
2262 (aset str i (aref str (1+ i)))
2263 (aset str (1+ i) tmp)
2264 (when (member str (nth 2 poss))
2266 (goto-char (+ beg i 1))
2267 (transpose-chars 1))
2269 (setq tmp (aref str i))
2270 (aset str i (aref str (1+ i)))
2271 (aset str (1+ i) tmp)
2275 (defun flyspell-maybe-correct-doubling (beg end poss)
2276 "Check replacements for doubled characters.
2278 If the text between BEG and END is equal to a correction suggested by
2279 Ispell, after removing a pair of doubled characters, correct the text,
2282 The third arg POSS is either the symbol 'doublon' or a list of
2283 possible corrections as returned by `ispell-parse-output'.
2285 This function is meant to be added to `flyspell-incorrect-hook'."
2288 (let ((str (buffer-substring beg end))
2289 (i 0) (len (- end beg)))
2290 (while (< (1+ i) len)
2291 (when (and (= (aref str i) (aref str (1+ i)))
2292 (member (concat (substring str 0 (1+ i))
2293 (substring str (+ i 2)))
2295 (goto-char (+ beg i))
2301 ;;*---------------------------------------------------------------------*/
2302 ;;* flyspell-already-abbrevp ... */
2303 ;;*---------------------------------------------------------------------*/
2304 (defun flyspell-already-abbrevp (table word)
2305 (let ((sym (abbrev-symbol word table)))
2306 (and sym (symbolp sym))))
2308 ;;*---------------------------------------------------------------------*/
2309 ;;* flyspell-change-abbrev ... */
2310 ;;*---------------------------------------------------------------------*/
2311 (defun flyspell-change-abbrev (table old new)
2312 (set (abbrev-symbol old table) new))
2316 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2317 ;;; flyspell.el ends here