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)
416 "Minor mode keymap for Flyspell mode--for the whole buffer.")
418 ;; dash character machinery
419 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
420 "*Non-nil means that the `-' char is considered as a word delimiter.")
421 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
422 (defvar flyspell-dash-dictionary nil)
423 (make-variable-buffer-local 'flyspell-dash-dictionary)
424 (defvar flyspell-dash-local-dictionary nil)
425 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
427 ;;*---------------------------------------------------------------------*/
429 ;;*---------------------------------------------------------------------*/
430 (defface flyspell-incorrect
431 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
433 "Face used to display a misspelled word in Flyspell."
435 ;; backward-compatibility alias
436 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
438 (defface flyspell-duplicate
439 '((((class color)) (:foreground "Gold3" :bold t :underline t))
441 "Face used to display subsequent occurrences of a misspelled word.
442 See also `flyspell-duplicate-distance'."
444 ;; backward-compatibility alias
445 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
447 (defvar flyspell-overlay nil)
449 ;;*---------------------------------------------------------------------*/
450 ;;* flyspell-mode ... */
451 ;;*---------------------------------------------------------------------*/
452 ;;;###autoload(defvar flyspell-mode nil)
454 (define-minor-mode flyspell-mode
455 "Minor mode performing on-the-fly spelling checking.
456 This spawns a single Ispell process and checks each word.
457 The default flyspell behavior is to highlight incorrect words.
458 With no argument, this command toggles Flyspell mode.
459 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
462 \\[ispell-word]: correct words (using Ispell).
463 \\[flyspell-auto-correct-word]: automatically correct word.
464 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
465 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
468 This runs `flyspell-mode-hook' after flyspell is entered.
471 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
472 valid. For instance, a personal dictionary can be used by
473 invoking `ispell-change-dictionary'.
475 Consider using the `ispell-parser' to check your text. For instance
477 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
480 \\[flyspell-region] checks all words inside a region.
481 \\[flyspell-buffer] checks the whole buffer."
482 :lighter flyspell-mode-line-string
483 :keymap flyspell-mode-map
487 (flyspell-mode-off)))
489 ;;*---------------------------------------------------------------------*/
490 ;;* flyspell-buffers ... */
491 ;;* ------------------------------------------------------------- */
492 ;;* For remembering buffers running flyspell */
493 ;;*---------------------------------------------------------------------*/
494 (defvar flyspell-buffers nil)
496 ;;*---------------------------------------------------------------------*/
497 ;;* flyspell-minibuffer-p ... */
498 ;;*---------------------------------------------------------------------*/
499 (defun flyspell-minibuffer-p (buffer)
500 "Is BUFFER a minibuffer?"
501 (let ((ws (get-buffer-window-list buffer t)))
502 (and (consp ws) (window-minibuffer-p (car ws)))))
504 ;;*---------------------------------------------------------------------*/
505 ;;* flyspell-accept-buffer-local-defs ... */
506 ;;*---------------------------------------------------------------------*/
507 (defvar flyspell-last-buffer nil
508 "The buffer in which the last flyspell operation took place.")
510 (defun flyspell-accept-buffer-local-defs (&optional force)
511 ;; When flyspell-word is used inside a loop (e.g. when processing
512 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
513 ;; up dwarfing everything else, so only do it when the buffer has changed.
514 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
515 (setq flyspell-last-buffer (current-buffer))
516 ;; Strange problem: If buffer in current window has font-lock turned on,
517 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
518 ;; call will reset the buffer to the buffer in the current window.
519 ;; However, it only happens at startup (fix by Albert L. Ting).
521 (ispell-accept-buffer-local-defs))
522 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
523 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
524 ;; The dictionary has changed
525 (setq flyspell-dash-dictionary ispell-dictionary)
526 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
527 (setq flyspell-consider-dash-as-word-delimiter-flag
528 (member (or ispell-local-dictionary ispell-dictionary)
529 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
531 (defun flyspell-kill-ispell-hook ()
532 (setq flyspell-last-buffer nil)
533 (dolist (buf (buffer-list))
534 (with-current-buffer buf
535 (kill-local-variable 'flyspell-word-cache-word))))
537 ;; Make sure we flush our caches when needed. Do it here rather than in
538 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
540 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
542 ;;*---------------------------------------------------------------------*/
543 ;;* flyspell-mode-on ... */
544 ;;*---------------------------------------------------------------------*/
545 (defun flyspell-mode-on ()
546 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
547 (ispell-maybe-find-aspell-dictionaries)
548 (setq ispell-highlight-face 'flyspell-incorrect)
549 ;; local dictionaries setup
550 (or ispell-local-dictionary ispell-dictionary
551 (if flyspell-default-dictionary
552 (ispell-change-dictionary flyspell-default-dictionary)))
553 ;; we have to force ispell to accept the local definition or
554 ;; otherwise it could be too late, the local dictionary may
556 ;; Pass the `force' argument for the case where flyspell was active already
557 ;; but the buffer's local-defs have been edited.
558 (flyspell-accept-buffer-local-defs 'force)
559 ;; we put the `flyspell-delayed' property on some commands
560 (flyspell-delay-commands)
561 ;; we put the `flyspell-deplacement' property on some commands
562 (flyspell-deplacement-commands)
563 ;; we bound flyspell action to post-command hook
564 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
565 ;; we bound flyspell action to pre-command hook
566 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
567 ;; we bound flyspell action to after-change hook
568 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
569 ;; set flyspell-generic-check-word-predicate based on the major mode
570 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
572 (setq flyspell-generic-check-word-predicate mode-predicate)))
573 ;; the welcome message
574 (if (and flyspell-issue-message-flag
575 flyspell-issue-welcome-flag
577 (let ((binding (where-is-internal 'flyspell-auto-correct-word
581 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
582 (key-description binding))
583 "Welcome to flyspell. Use Mouse-2 to correct words."))))
584 ;; we end with the flyspell hooks
585 (run-hooks 'flyspell-mode-hook))
587 ;;*---------------------------------------------------------------------*/
588 ;;* flyspell-delay-commands ... */
589 ;;*---------------------------------------------------------------------*/
590 (defun flyspell-delay-commands ()
591 "Install the standard set of Flyspell delayed commands."
592 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
593 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
595 ;;*---------------------------------------------------------------------*/
596 ;;* flyspell-delay-command ... */
597 ;;*---------------------------------------------------------------------*/
598 (defun flyspell-delay-command (command)
599 "Set COMMAND to be delayed, for Flyspell.
600 When flyspell `post-command-hook' is invoked because a delayed command
601 as been used the current word is not immediately checked.
602 It will be checked only after `flyspell-delay' seconds."
603 (interactive "SDelay Flyspell after Command: ")
604 (put command 'flyspell-delayed t))
606 ;;*---------------------------------------------------------------------*/
607 ;;* flyspell-deplacement-commands ... */
608 ;;*---------------------------------------------------------------------*/
609 (defun flyspell-deplacement-commands ()
610 "Install the standard set of Flyspell deplacement commands."
611 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
612 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
614 ;;*---------------------------------------------------------------------*/
615 ;;* flyspell-deplacement-command ... */
616 ;;*---------------------------------------------------------------------*/
617 (defun flyspell-deplacement-command (command)
618 "Set COMMAND that implement cursor movements, for Flyspell.
619 When flyspell `post-command-hook' is invoked because of a deplacement command
620 as been used the current word is checked only if the previous command was
621 not the very same deplacement command."
622 (interactive "SDeplacement Flyspell after Command: ")
623 (put command 'flyspell-deplacement t))
625 ;;*---------------------------------------------------------------------*/
626 ;;* flyspell-word-cache ... */
627 ;;*---------------------------------------------------------------------*/
628 (defvar flyspell-word-cache-start nil)
629 (defvar flyspell-word-cache-end nil)
630 (defvar flyspell-word-cache-word nil)
631 (defvar flyspell-word-cache-result '_)
632 (make-variable-buffer-local 'flyspell-word-cache-start)
633 (make-variable-buffer-local 'flyspell-word-cache-end)
634 (make-variable-buffer-local 'flyspell-word-cache-word)
635 (make-variable-buffer-local 'flyspell-word-cache-result)
637 ;;*---------------------------------------------------------------------*/
638 ;;* The flyspell pre-hook, store the current position. In the */
639 ;;* post command hook, we will check, if the word at this position */
640 ;;* has to be spell checked. */
641 ;;*---------------------------------------------------------------------*/
642 (defvar flyspell-pre-buffer nil)
643 (defvar flyspell-pre-point nil)
644 (defvar flyspell-pre-column nil)
645 (defvar flyspell-pre-pre-buffer nil)
646 (defvar flyspell-pre-pre-point nil)
648 ;;*---------------------------------------------------------------------*/
649 ;;* flyspell-previous-command ... */
650 ;;*---------------------------------------------------------------------*/
651 (defvar flyspell-previous-command nil
652 "The last interactive command checked by Flyspell.")
654 ;;*---------------------------------------------------------------------*/
655 ;;* flyspell-pre-command-hook ... */
656 ;;*---------------------------------------------------------------------*/
657 (defun flyspell-pre-command-hook ()
658 "Save the current buffer and point for Flyspell's post-command hook."
660 (setq flyspell-pre-buffer (current-buffer))
661 (setq flyspell-pre-point (point))
662 (setq flyspell-pre-column (current-column)))
664 ;;*---------------------------------------------------------------------*/
665 ;;* flyspell-mode-off ... */
666 ;;*---------------------------------------------------------------------*/
668 (defun flyspell-mode-off ()
669 "Turn Flyspell mode off."
670 ;; we remove the hooks
671 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
672 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
673 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
674 ;; we remove all the flyspell hilightings
675 (flyspell-delete-all-overlays)
676 ;; we have to erase pre cache variables
677 (setq flyspell-pre-buffer nil)
678 (setq flyspell-pre-point nil)
679 ;; we mark the mode as killed
680 (setq flyspell-mode nil))
682 ;;*---------------------------------------------------------------------*/
683 ;;* flyspell-check-pre-word-p ... */
684 ;;*---------------------------------------------------------------------*/
685 (defun flyspell-check-pre-word-p ()
686 "Return non-nil if we should check the word before point.
687 More precisely, it applies to the word that was before point
688 before the current command."
690 ((or (not (numberp flyspell-pre-point))
691 (not (bufferp flyspell-pre-buffer))
692 (not (buffer-live-p flyspell-pre-buffer)))
694 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
695 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
697 ((or (and (= flyspell-pre-point (- (point) 1))
698 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
699 (= flyspell-pre-point (point))
700 (= flyspell-pre-point (+ (point) 1)))
702 ((and (symbolp this-command)
703 (not executing-kbd-macro)
704 (or (get this-command 'flyspell-delayed)
705 (and (get this-command 'flyspell-deplacement)
706 (eq flyspell-previous-command this-command)))
707 (or (= (current-column) 0)
708 (= (current-column) flyspell-pre-column)
709 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
711 ((not (eq (current-buffer) flyspell-pre-buffer))
713 ((not (and (numberp flyspell-word-cache-start)
714 (numberp flyspell-word-cache-end)))
717 (or (< flyspell-pre-point flyspell-word-cache-start)
718 (> flyspell-pre-point flyspell-word-cache-end)))))
720 ;;*---------------------------------------------------------------------*/
721 ;;* The flyspell after-change-hook, store the change position. In */
722 ;;* the post command hook, we will check, if the word at this */
723 ;;* position has to be spell checked. */
724 ;;*---------------------------------------------------------------------*/
725 (defvar flyspell-changes nil)
726 (make-variable-buffer-local 'flyspell-changes)
728 ;;*---------------------------------------------------------------------*/
729 ;;* flyspell-after-change-function ... */
730 ;;*---------------------------------------------------------------------*/
731 (defun flyspell-after-change-function (start stop len)
732 "Save the current buffer and point for Flyspell's post-command hook."
733 (push (cons start stop) flyspell-changes))
735 ;;*---------------------------------------------------------------------*/
736 ;;* flyspell-check-changed-word-p ... */
737 ;;*---------------------------------------------------------------------*/
738 (defun flyspell-check-changed-word-p (start stop)
739 "Return t when the changed word has to be checked.
740 The answer depends of several criteria.
741 Mostly we check word delimiters."
743 ((and (memq (char-after start) '(?\n ? )) (> stop start))
745 ((not (numberp flyspell-pre-point))
747 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
749 ((let ((pos (point)))
750 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
755 ;;*---------------------------------------------------------------------*/
756 ;;* flyspell-check-word-p ... */
757 ;;*---------------------------------------------------------------------*/
758 (defun flyspell-check-word-p ()
759 "Return t when the word at `point' has to be checked.
760 The answer depends of several criteria.
761 Mostly we check word delimiters."
763 ((<= (- (point-max) 1) (point-min))
764 ;; the buffer is not filled enough
766 ((and (and (> (current-column) 0)
767 (not (eq (current-column) flyspell-pre-column)))
770 (and (looking-at (flyspell-get-not-casechars))
771 (or flyspell-consider-dash-as-word-delimiter-flag
772 (not (looking-at "-"))))))
773 ;; yes because we have reached or typed a word delimiter.
775 ((symbolp this-command)
777 ((get this-command 'flyspell-deplacement)
778 (not (eq flyspell-previous-command this-command)))
779 ((get this-command 'flyspell-delayed)
780 ;; the current command is not delayed, that
781 ;; is that we must check the word now
782 (and (not unread-command-events)
783 (sit-for flyspell-delay)))
787 ;;*---------------------------------------------------------------------*/
788 ;;* flyspell-debug-signal-no-check ... */
789 ;;*---------------------------------------------------------------------*/
790 (defun flyspell-debug-signal-no-check (msg obj)
791 (setq debug-on-error t)
792 (with-current-buffer (get-buffer-create "*flyspell-debug*")
794 (insert "NO-CHECK:\n")
795 (insert (format " %S : %S\n" msg obj))))
797 ;;*---------------------------------------------------------------------*/
798 ;;* flyspell-debug-signal-pre-word-checked ... */
799 ;;*---------------------------------------------------------------------*/
800 (defun flyspell-debug-signal-pre-word-checked ()
801 (setq debug-on-error t)
802 (with-current-buffer (get-buffer-create "*flyspell-debug*")
803 (insert "PRE-WORD:\n")
804 (insert (format " pre-point : %S\n" flyspell-pre-point))
805 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
806 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
807 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
808 (goto-char (point-max))))
810 ;;*---------------------------------------------------------------------*/
811 ;;* flyspell-debug-signal-word-checked ... */
812 ;;*---------------------------------------------------------------------*/
813 (defun flyspell-debug-signal-word-checked ()
814 (setq debug-on-error t)
815 (let ((oldbuf (current-buffer))
817 (with-current-buffer (get-buffer-create "*flyspell-debug*")
819 (insert (format " this-cmd : %S\n" this-command))
820 (insert (format " delayed : %S\n" (and (symbolp this-command)
821 (get this-command 'flyspell-delayed))))
822 (insert (format " point : %S\n" point))
823 (insert (format " prev-char : [%c] %S\n"
824 (with-current-buffer oldbuf
825 (let ((c (if (> (point) (point-min))
828 (char-after (point)))
831 (with-current-buffer oldbuf
832 (let ((c (if (> (point) (point-min))
835 (and (and (looking-at (flyspell-get-not-casechars)) 1)
836 (and (or flyspell-consider-dash-as-word-delimiter-flag
837 (not (looking-at "\\-"))) 2))))))
839 (insert (format " because : %S\n"
841 ((not (and (symbolp this-command)
842 (get this-command 'flyspell-delayed)))
843 ;; the current command is not delayed, that
844 ;; is that we must check the word now
846 ((with-current-buffer oldbuf
847 (let ((c (if (> (point) (point-min))
850 (and (looking-at (flyspell-get-not-casechars))
851 (or flyspell-consider-dash-as-word-delimiter-flag
852 (not (looking-at "\\-"))))))))
854 ;; yes because we have reached or typed a word delimiter.
856 ((not (integerp flyspell-delay))
857 ;; yes because the user had set up a no-delay configuration.
861 (goto-char (point-max)))))
863 ;;*---------------------------------------------------------------------*/
864 ;;* flyspell-debug-signal-changed-checked ... */
865 ;;*---------------------------------------------------------------------*/
866 (defun flyspell-debug-signal-changed-checked ()
867 (setq debug-on-error t)
868 (let ((point (point)))
869 (with-current-buffer (get-buffer-create "*flyspell-debug*")
870 (insert "CHANGED WORD:\n")
871 (insert (format " point : %S\n" point))
872 (goto-char (point-max)))))
874 ;;*---------------------------------------------------------------------*/
875 ;;* flyspell-post-command-hook ... */
876 ;;* ------------------------------------------------------------- */
877 ;;* It is possible that we check several words: */
878 ;;* 1- the current word is checked if the predicate */
879 ;;* FLYSPELL-CHECK-WORD-P is true */
880 ;;* 2- the word that used to be the current word before the */
881 ;;* THIS-COMMAND is checked if: */
882 ;;* a- the previous word is different from the current word */
883 ;;* b- the previous word as not just been checked by the */
884 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
885 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
886 ;;* previous word nor the current word */
887 ;;*---------------------------------------------------------------------*/
888 (defun flyspell-post-command-hook ()
889 "The `post-command-hook' used by flyspell to check a word in-the-fly."
891 (let ((command this-command)
892 ;; Prevent anything we do from affecting the mark.
894 (if (flyspell-check-pre-word-p)
895 (with-current-buffer flyspell-pre-buffer
896 '(flyspell-debug-signal-pre-word-checked)
898 (goto-char flyspell-pre-point)
900 (if (flyspell-check-word-p)
902 '(flyspell-debug-signal-word-checked)
904 ;; we remember which word we have just checked.
905 ;; this will be used next time we will check a word
906 ;; to compare the next current word with the word
907 ;; that as been registered in the pre-command-hook
908 ;; that is these variables are used within the predicate
909 ;; FLYSPELL-CHECK-PRE-WORD-P
910 (setq flyspell-pre-pre-buffer (current-buffer))
911 (setq flyspell-pre-pre-point (point)))
913 (setq flyspell-pre-pre-buffer nil)
914 (setq flyspell-pre-pre-point nil)
915 ;; when a word is not checked because of a delayed command
916 ;; we do not disable the ispell cache.
917 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
919 (setq flyspell-word-cache-end -1)
920 (setq flyspell-word-cache-result '_)))))
921 (while (and (not (input-pending-p)) (consp flyspell-changes))
922 (let ((start (car (car flyspell-changes)))
923 (stop (cdr (car flyspell-changes))))
924 (if (flyspell-check-changed-word-p start stop)
926 '(flyspell-debug-signal-changed-checked)
929 (setq flyspell-changes (cdr flyspell-changes))))
930 (setq flyspell-previous-command command)))
932 ;;*---------------------------------------------------------------------*/
933 ;;* flyspell-notify-misspell ... */
934 ;;*---------------------------------------------------------------------*/
935 (defun flyspell-notify-misspell (word poss)
936 (let ((replacements (if (stringp poss)
938 (if flyspell-sort-corrections
939 (sort (car (cdr (cdr poss))) 'string<)
940 (car (cdr (cdr poss)))))))
941 (if flyspell-issue-message-flag
942 (message "misspelling `%s' %S" word replacements))))
944 ;;*---------------------------------------------------------------------*/
945 ;;* flyspell-word-search-backward ... */
946 ;;*---------------------------------------------------------------------*/
947 (defun flyspell-word-search-backward (word bound)
951 (while (and (not r) (setq p (search-backward word bound t)))
952 (let ((lw (flyspell-get-word '())))
953 (if (and (consp lw) (string-equal (car lw) word))
958 ;;*---------------------------------------------------------------------*/
959 ;;* flyspell-word-search-forward ... */
960 ;;*---------------------------------------------------------------------*/
961 (defun flyspell-word-search-forward (word bound)
965 (while (and (not r) (setq p (search-forward word bound t)))
966 (let ((lw (flyspell-get-word '())))
967 (if (and (consp lw) (string-equal (car lw) word))
969 (goto-char (1+ p)))))
972 ;;*---------------------------------------------------------------------*/
973 ;;* flyspell-word ... */
974 ;;*---------------------------------------------------------------------*/
975 (defun flyspell-word (&optional following)
976 "Spell check a word."
977 (interactive (list ispell-following-word))
979 ;; use the correct dictionary
980 (flyspell-accept-buffer-local-defs)
981 (let* ((cursor-location (point))
982 (flyspell-word (flyspell-get-word following))
984 (if (or (eq flyspell-word nil)
985 (and (fboundp flyspell-generic-check-word-predicate)
986 (not (funcall flyspell-generic-check-word-predicate))))
989 ;; destructure return flyspell-word info list.
990 (setq start (car (cdr flyspell-word))
991 end (car (cdr (cdr flyspell-word)))
992 word (car flyspell-word))
993 ;; before checking in the directory, we check for doublons.
995 ((and (or (not (eq ispell-parser 'tex))
996 (and (> start (point-min))
997 (not (memq (char-after (1- start)) '(?\} ?\\)))))
998 flyspell-mark-duplications-flag
1000 (goto-char (1- start))
1001 (let ((p (flyspell-word-search-backward
1003 (- start (1+ (- end start))))))
1004 (and p (/= p (1- start))))))
1005 ;; yes, this is a doublon
1006 (flyspell-highlight-incorrect-region start end 'doublon)
1008 ((and (eq flyspell-word-cache-start start)
1009 (eq flyspell-word-cache-end end)
1010 (string-equal flyspell-word-cache-word word))
1011 ;; this word had been already checked, we skip
1012 flyspell-word-cache-result)
1013 ((and (eq ispell-parser 'tex)
1014 (flyspell-tex-command-p flyspell-word))
1015 ;; this is a correct word (because a tex command)
1016 (flyspell-unhighlight-at start)
1018 (flyspell-unhighlight-at (- end 1)))
1021 ;; we setup the cache
1022 (setq flyspell-word-cache-start start)
1023 (setq flyspell-word-cache-end end)
1024 (setq flyspell-word-cache-word word)
1025 ;; now check spelling of word.
1026 (ispell-send-string "%\n")
1027 ;; put in verbose mode
1028 (ispell-send-string (concat "^" word "\n"))
1029 ;; we mark the ispell process so it can be killed
1030 ;; when emacs is exited without query
1031 (set-process-query-on-exit-flag ispell-process nil)
1032 ;; Wait until ispell has processed word. Since this code is often
1033 ;; executed from post-command-hook but the ispell process may not
1034 ;; be responsive, it's important to make sure we re-enable C-g.
1037 (accept-process-output ispell-process)
1038 (not (string= "" (car ispell-filter))))))
1039 ;; (ispell-send-string "!\n")
1040 ;; back to terse mode.
1041 (setq ispell-filter (cdr ispell-filter))
1042 (if (consp ispell-filter)
1043 (setq poss (ispell-parse-output (car ispell-filter))))
1044 (let ((res (cond ((eq poss t)
1046 (setq flyspell-word-cache-result t)
1047 (flyspell-unhighlight-at start)
1049 (flyspell-unhighlight-at (- end 1)))
1051 ((and (stringp poss) flyspell-highlight-flag)
1053 (setq flyspell-word-cache-result t)
1054 (flyspell-unhighlight-at start)
1056 (flyspell-unhighlight-at (- end 1)))
1059 (setq flyspell-word-cache-result t)
1060 (flyspell-unhighlight-at start)
1062 (flyspell-unhighlight-at (- end 1)))
1064 ((or (and (< flyspell-duplicate-distance 0)
1067 (flyspell-word-search-backward
1072 (flyspell-word-search-forward
1075 (and (> flyspell-duplicate-distance 0)
1078 (flyspell-word-search-backward
1081 flyspell-duplicate-distance)))
1084 (flyspell-word-search-forward
1087 flyspell-duplicate-distance))))))
1088 ;; This is a misspelled word which occurs
1089 ;; twice within flyspell-duplicate-distance.
1090 (setq flyspell-word-cache-result nil)
1091 (if flyspell-highlight-flag
1092 (flyspell-highlight-duplicate-region
1094 (message "duplicate `%s'" word))
1097 (setq flyspell-word-cache-result nil)
1098 ;; incorrect highlight the location
1099 (if flyspell-highlight-flag
1100 (flyspell-highlight-incorrect-region
1102 (flyspell-notify-misspell word poss))
1104 ;; return to original location
1105 (goto-char cursor-location)
1106 (if ispell-quit (setq ispell-quit nil))
1109 ;;*---------------------------------------------------------------------*/
1110 ;;* flyspell-tex-math-initialized ... */
1111 ;;*---------------------------------------------------------------------*/
1112 (defvar flyspell-tex-math-initialized nil)
1114 ;;*---------------------------------------------------------------------*/
1115 ;;* flyspell-math-tex-command-p ... */
1116 ;;* ------------------------------------------------------------- */
1117 ;;* This function uses the texmathp package to check if (point) */
1118 ;;* is within a tex command. In order to avoid using */
1119 ;;* condition-case each time we use the variable */
1120 ;;* flyspell-tex-math-initialized to make a special case the first */
1121 ;;* time that function is called. */
1122 ;;*---------------------------------------------------------------------*/
1123 (defun flyspell-math-tex-command-p ()
1124 (when (fboundp 'texmathp)
1126 (flyspell-check-tex-math-command
1128 ((eq flyspell-tex-math-initialized t)
1130 ((eq flyspell-tex-math-initialized 'error)
1133 (setq flyspell-tex-math-initialized t)
1137 (setq flyspell-tex-math-initialized 'error)
1140 ;;*---------------------------------------------------------------------*/
1141 ;;* flyspell-tex-command-p ... */
1142 ;;*---------------------------------------------------------------------*/
1143 (defun flyspell-tex-command-p (word)
1144 "Return t if WORD is a TeX command."
1146 (let ((b (car (cdr word))))
1147 (and (re-search-backward "\\\\" (- (point) 100) t)
1148 (or (= (match-end 0) b)
1149 (and (goto-char (match-end 0))
1150 (looking-at flyspell-tex-command-regexp)
1151 (>= (match-end 0) b))))))
1152 (flyspell-math-tex-command-p)))
1154 ;;*---------------------------------------------------------------------*/
1155 ;;* flyspell-casechars-cache ... */
1156 ;;*---------------------------------------------------------------------*/
1157 (defvar flyspell-casechars-cache nil)
1158 (defvar flyspell-ispell-casechars-cache nil)
1159 (make-variable-buffer-local 'flyspell-casechars-cache)
1160 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1162 ;;*---------------------------------------------------------------------*/
1163 ;;* flyspell-get-casechars ... */
1164 ;;*---------------------------------------------------------------------*/
1165 (defun flyspell-get-casechars ()
1166 "This function builds a string that is the regexp of word chars.
1167 In order to avoid one useless string construction,
1168 this function changes the last char of the `ispell-casechars' string."
1169 (let ((ispell-casechars (ispell-get-casechars)))
1171 ((eq ispell-parser 'tex)
1172 (setq flyspell-ispell-casechars-cache ispell-casechars)
1173 (setq flyspell-casechars-cache
1174 (concat (substring ispell-casechars
1176 (- (length ispell-casechars) 1))
1178 flyspell-casechars-cache)
1180 (setq flyspell-ispell-casechars-cache ispell-casechars)
1181 (setq flyspell-casechars-cache ispell-casechars)
1182 flyspell-casechars-cache))))
1184 ;;*---------------------------------------------------------------------*/
1185 ;;* flyspell-get-not-casechars-cache ... */
1186 ;;*---------------------------------------------------------------------*/
1187 (defvar flyspell-not-casechars-cache nil)
1188 (defvar flyspell-ispell-not-casechars-cache nil)
1189 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1190 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1192 ;;*---------------------------------------------------------------------*/
1193 ;;* flyspell-get-not-casechars ... */
1194 ;;*---------------------------------------------------------------------*/
1195 (defun flyspell-get-not-casechars ()
1196 "This function builds a string that is the regexp of non-word chars."
1197 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1199 ((eq ispell-parser 'tex)
1200 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1201 (setq flyspell-not-casechars-cache
1202 (concat (substring ispell-not-casechars
1204 (- (length ispell-not-casechars) 1))
1206 flyspell-not-casechars-cache)
1208 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1209 (setq flyspell-not-casechars-cache ispell-not-casechars)
1210 flyspell-not-casechars-cache))))
1212 ;;*---------------------------------------------------------------------*/
1213 ;;* flyspell-get-word ... */
1214 ;;*---------------------------------------------------------------------*/
1215 (defun flyspell-get-word (following &optional extra-otherchars)
1216 "Return the word for spell-checking according to Ispell syntax.
1217 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1218 is non-nil when called interactively, then the following word
1219 \(rather than preceding\) is checked when the cursor is not over a word.
1220 Optional second argument contains otherchars that can be included in word
1223 Word syntax described by `flyspell-dictionary-alist' (which see)."
1224 (let* ((flyspell-casechars (flyspell-get-casechars))
1225 (flyspell-not-casechars (flyspell-get-not-casechars))
1226 (ispell-otherchars (ispell-get-otherchars))
1227 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1228 (word-regexp (concat flyspell-casechars
1230 (if (not (string= "" ispell-otherchars))
1231 (concat ispell-otherchars "?"))
1232 (if extra-otherchars
1233 (concat extra-otherchars "?"))
1236 (if (or ispell-many-otherchars-p
1242 (if (not (looking-at flyspell-casechars))
1244 (re-search-forward flyspell-casechars nil t)
1245 (re-search-backward flyspell-casechars nil t)))
1246 ;; move to front of word
1247 (re-search-backward flyspell-not-casechars nil 'start)
1248 (while (and (or (and (not (string= "" ispell-otherchars))
1249 (looking-at ispell-otherchars))
1250 (and extra-otherchars (looking-at extra-otherchars)))
1252 (or (not did-it-once)
1253 ispell-many-otherchars-p)
1254 (not (eq prevpt (point))))
1255 (if (and extra-otherchars (looking-at extra-otherchars))
1258 (if (looking-at flyspell-casechars)
1259 (re-search-backward flyspell-not-casechars nil 'move)))
1263 (if (looking-at flyspell-casechars)
1264 (re-search-backward flyspell-not-casechars nil 'move)
1265 (backward-char -1))))
1266 ;; Now mark the word and save to string.
1267 (if (not (re-search-forward word-regexp nil t))
1270 (setq start (match-beginning 0)
1272 word (buffer-substring-no-properties start end))
1273 (list word start end)))))
1275 ;;*---------------------------------------------------------------------*/
1276 ;;* flyspell-small-region ... */
1277 ;;*---------------------------------------------------------------------*/
1278 (defun flyspell-small-region (beg end)
1279 "Flyspell text between BEG and END."
1287 (while (< (point) end)
1288 (if (and flyspell-issue-message-flag (= count 100))
1290 (message "Spell Checking...%d%%"
1291 (* 100 (/ (float (- (point) beg)) (- end beg))))
1293 (setq count (+ 1 count)))
1296 (let ((cur (point)))
1298 (if (and (< (point) end) (> (point) (+ cur 1)))
1299 (backward-char 1)))))
1301 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1304 ;;*---------------------------------------------------------------------*/
1305 ;;* flyspell-external-ispell-process ... */
1306 ;;*---------------------------------------------------------------------*/
1307 (defvar flyspell-external-ispell-process '()
1308 "The external Flyspell Ispell process.")
1310 ;;*---------------------------------------------------------------------*/
1311 ;;* flyspell-external-ispell-buffer ... */
1312 ;;*---------------------------------------------------------------------*/
1313 (defvar flyspell-external-ispell-buffer '())
1314 (defvar flyspell-large-region-buffer '())
1315 (defvar flyspell-large-region-beg (point-min))
1316 (defvar flyspell-large-region-end (point-max))
1318 ;;*---------------------------------------------------------------------*/
1319 ;;* flyspell-external-point-words ... */
1320 ;;*---------------------------------------------------------------------*/
1321 (defun flyspell-external-point-words ()
1322 "Mark words from a buffer listing incorrect words in order of appearance.
1323 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1324 \(We finish by killing that buffer and setting the variable to nil.)
1325 The buffer to mark them in is `flyspell-large-region-buffer'."
1326 (let (words-not-found
1327 (ispell-otherchars (ispell-get-otherchars))
1328 (buffer-scan-pos flyspell-large-region-beg)
1330 (with-current-buffer flyspell-external-ispell-buffer
1331 (goto-char (point-min))
1332 ;; Loop over incorrect words, in the order they were reported,
1333 ;; which is also the order they appear in the buffer being checked.
1334 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1335 ;; Bind WORD to the next one.
1336 (let ((word (match-string 1)) (wordpos (point)))
1337 ;; Here there used to be code to see if WORD is the same
1338 ;; as the previous iteration, and count the number of consecutive
1339 ;; identical words, and the loop below would search for that many.
1340 ;; That code seemed to be incorrect, and on principle, should
1341 ;; be unnecessary too. -- rms.
1342 (if flyspell-issue-message-flag
1343 (message "Spell Checking...%d%% [%s]"
1344 (* 100 (/ (float (point)) (point-max)))
1346 (with-current-buffer flyspell-large-region-buffer
1347 (goto-char buffer-scan-pos)
1349 ;; Iterate on string search until string is found as word,
1352 (if (search-forward word
1353 flyspell-large-region-end t)
1356 ;; Move back into the match
1357 ;; so flyspell-get-word will find it.
1359 (flyspell-get-word nil)))
1360 (found (car found-list))
1361 (found-length (length found))
1362 (misspell-length (length word)))
1364 ;; Size matches, we really found it.
1365 (= found-length misspell-length)
1366 ;; Matches as part of a boundary-char separated word
1368 (split-string found ispell-otherchars))
1369 ;; Misspelling has higher length than
1370 ;; what flyspell considers the
1371 ;; word. Caused by boundary-chars
1372 ;; mismatch. Validating seems safe.
1373 (< found-length misspell-length)
1374 ;; ispell treats beginning of some TeX
1375 ;; commands as nroff control sequences
1376 ;; and strips them in the list of
1377 ;; misspelled words thus giving a
1378 ;; non-existent word. Skip if ispell
1379 ;; is used, string is a TeX command
1380 ;; (char before beginning of word is
1381 ;; backslash) and none of the previous
1383 (and (not ispell-really-aspell)
1385 (goto-char (- (nth 1 found-list) 1))
1386 (if (looking-at "[\\]" )
1391 ;; Search for next misspelled word will begin from
1392 ;; end of last validated match.
1393 (setq buffer-scan-pos (point))))
1394 ;; Record if misspelling is not found and try new one
1395 (add-to-list 'words-not-found
1396 (concat " -> " word " - "
1397 (int-to-string wordpos)))
1398 (setq keep nil)))))))
1400 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1401 ;; Warn about not found misspellings
1402 (dolist (word words-not-found)
1403 (message "%s: word not found" word))
1404 ;; Kill and forget the buffer with the list of incorrect words.
1405 (kill-buffer flyspell-external-ispell-buffer)
1406 (setq flyspell-external-ispell-buffer nil)))
1408 ;;*---------------------------------------------------------------------*/
1409 ;;* flyspell-process-localwords ... */
1410 ;;* ------------------------------------------------------------- */
1411 ;;* This function is used to prevent marking of words explicitly */
1412 ;;* declared correct. */
1413 ;;*---------------------------------------------------------------------*/
1414 (defun flyspell-process-localwords (misspellings-buffer)
1415 (let (localwords case-fold-search
1416 (ispell-casechars (ispell-get-casechars)))
1417 ;; Get localwords from the original buffer
1419 (goto-char (point-min))
1420 ;; Localwords parsing copied from ispell.el.
1421 (while (search-forward ispell-words-keyword nil t)
1422 (let ((end (save-excursion (end-of-line) (point)))
1424 ;; buffer-local words separated by a space, and can contain
1425 ;; any character other than a space. Not rigorous enough.
1426 (while (re-search-forward " *\\([^ ]+\\)" end t)
1427 (setq string (buffer-substring-no-properties (match-beginning 1)
1429 ;; This can fail when string contains a word with invalid chars.
1430 ;; Error handling needs to be added between Ispell and Emacs.
1431 (if (and (< 1 (length string))
1432 (equal 0 (string-match ispell-casechars string)))
1433 (push string localwords))))))
1434 ;; Remove localwords matches from misspellings-buffer.
1435 ;; The usual mechanism of communicating the local words to ispell
1436 ;; does not affect the special ispell process used by
1437 ;; flyspell-large-region.
1438 (with-current-buffer misspellings-buffer
1440 (dolist (word localwords)
1441 (goto-char (point-min))
1442 (let ((regexp (concat "^" word "\n")))
1443 (while (re-search-forward regexp nil t)
1444 (delete-region (match-beginning 0) (match-end 0)))))))))
1446 ;;*---------------------------------------------------------------------*/
1447 ;;* flyspell-large-region ... */
1448 ;;*---------------------------------------------------------------------*/
1449 (defun flyspell-large-region (beg end)
1450 (let* ((curbuf (current-buffer))
1451 (buffer (get-buffer-create "*flyspell-region*")))
1452 (setq flyspell-external-ispell-buffer buffer)
1453 (setq flyspell-large-region-buffer curbuf)
1454 (setq flyspell-large-region-beg beg)
1455 (setq flyspell-large-region-end end)
1456 (flyspell-accept-buffer-local-defs)
1459 ;; this is done, we can start checking...
1460 (if flyspell-issue-message-flag (message "Checking region..."))
1462 (ispell-check-version)
1463 (let ((c (apply 'call-process-region beg
1469 (if ispell-really-aspell "list" "-l")
1471 ;; Local dictionary becomes the global dictionary in use.
1472 (if ispell-local-dictionary
1473 (setq ispell-dictionary ispell-local-dictionary))
1474 (setq args (ispell-get-ispell-args))
1475 (if ispell-dictionary ; use specified dictionary
1477 (append (list "-d" ispell-dictionary) args)))
1478 (if ispell-personal-dictionary ; use specified pers dict
1483 ispell-personal-dictionary)))))
1484 (setq args (append args ispell-extra-args))
1488 (flyspell-process-localwords buffer)
1489 (with-current-buffer curbuf
1490 (flyspell-delete-region-overlays beg end))
1491 (flyspell-external-point-words))
1492 (error "Can't check region...")))))
1494 ;;*---------------------------------------------------------------------*/
1495 ;;* flyspell-region ... */
1496 ;;* ------------------------------------------------------------- */
1497 ;;* Because `ispell -a' is too slow, it is not possible to use */
1498 ;;* it on large region. Then, when ispell is invoked on a large */
1499 ;;* text region, a new `ispell -l' process is spawned. The */
1500 ;;* pointed out words are then searched in the region a checked with */
1501 ;;* regular flyspell means. */
1502 ;;*---------------------------------------------------------------------*/
1504 (defun flyspell-region (beg end)
1505 "Flyspell text between BEG and END."
1514 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1515 (flyspell-large-region beg end)
1516 (flyspell-small-region beg end)))))
1518 ;;*---------------------------------------------------------------------*/
1519 ;;* flyspell-buffer ... */
1520 ;;*---------------------------------------------------------------------*/
1522 (defun flyspell-buffer ()
1523 "Flyspell whole buffer."
1525 (flyspell-region (point-min) (point-max)))
1527 ;;*---------------------------------------------------------------------*/
1528 ;;* old next error position ... */
1529 ;;*---------------------------------------------------------------------*/
1530 (defvar flyspell-old-buffer-error nil)
1531 (defvar flyspell-old-pos-error nil)
1533 ;;*---------------------------------------------------------------------*/
1534 ;;* flyspell-goto-next-error ... */
1535 ;;*---------------------------------------------------------------------*/
1536 (defun flyspell-goto-next-error ()
1537 "Go to the next previously detected error.
1538 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1543 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1544 (eq pos flyspell-old-pos-error))
1546 (if (= flyspell-old-pos-error max)
1547 ;; goto beginning of buffer
1549 (message "Restarting from beginning of buffer")
1550 (goto-char (point-min)))
1552 (setq pos (point))))
1553 ;; seek the next error
1554 (while (and (< pos max)
1555 (let ((ovs (overlays-at pos))
1557 (while (and (not r) (consp ovs))
1558 (if (flyspell-overlay-p (car ovs))
1560 (setq ovs (cdr ovs))))
1562 (setq pos (1+ pos)))
1563 ;; save the current location for next invocation
1564 (setq flyspell-old-pos-error pos)
1565 (setq flyspell-old-buffer-error (current-buffer))
1568 (message "No more miss-spelled word!"))))
1570 ;;*---------------------------------------------------------------------*/
1571 ;;* flyspell-overlay-p ... */
1572 ;;*---------------------------------------------------------------------*/
1573 (defun flyspell-overlay-p (o)
1574 "A predicate that return true iff O is an overlay used by flyspell."
1575 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1577 ;;*---------------------------------------------------------------------*/
1578 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1579 ;;* ------------------------------------------------------------- */
1580 ;;* Remove overlays introduced by flyspell. */
1581 ;;*---------------------------------------------------------------------*/
1582 (defun flyspell-delete-region-overlays (beg end)
1583 "Delete overlays used by flyspell in a given region."
1584 (remove-overlays beg end 'flyspell-overlay t))
1587 (defun flyspell-delete-all-overlays ()
1588 "Delete all the overlays used by flyspell."
1589 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1591 ;;*---------------------------------------------------------------------*/
1592 ;;* flyspell-unhighlight-at ... */
1593 ;;*---------------------------------------------------------------------*/
1594 (defun flyspell-unhighlight-at (pos)
1595 "Remove the flyspell overlay that are located at POS."
1596 (if flyspell-persistent-highlight
1597 (let ((overlays (overlays-at pos)))
1598 (while (consp overlays)
1599 (if (flyspell-overlay-p (car overlays))
1600 (delete-overlay (car overlays)))
1601 (setq overlays (cdr overlays))))
1602 (if (flyspell-overlay-p flyspell-overlay)
1603 (delete-overlay flyspell-overlay))))
1605 ;;*---------------------------------------------------------------------*/
1606 ;;* flyspell-properties-at-p ... */
1607 ;;* ------------------------------------------------------------- */
1608 ;;* Is there an highlight properties at position pos? */
1609 ;;*---------------------------------------------------------------------*/
1610 (defun flyspell-properties-at-p (pos)
1611 "Return t if there is a text property at POS, not counting `local-map'.
1612 If variable `flyspell-highlight-properties' is set to nil,
1613 text with properties are not checked. This function is used to discover
1614 if the character at POS has any other property."
1615 (let ((prop (text-properties-at pos))
1617 (while (and keep (consp prop))
1618 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1619 (setq prop (cdr (cdr prop)))
1623 ;;*---------------------------------------------------------------------*/
1624 ;;* make-flyspell-overlay ... */
1625 ;;*---------------------------------------------------------------------*/
1626 (defun make-flyspell-overlay (beg end face mouse-face)
1627 "Allocate an overlay to highlight an incorrect word.
1628 BEG and END specify the range in the buffer of that word.
1629 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1631 (let ((overlay (make-overlay beg end nil t nil)))
1632 (overlay-put overlay 'face face)
1633 (overlay-put overlay 'mouse-face mouse-face)
1634 (overlay-put overlay 'flyspell-overlay t)
1635 (overlay-put overlay 'evaporate t)
1636 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1637 (overlay-put overlay 'keymap flyspell-mouse-map)
1638 (when (eq face 'flyspell-incorrect)
1639 (and (stringp flyspell-before-incorrect-word-string)
1640 (overlay-put overlay 'before-string
1641 flyspell-before-incorrect-word-string))
1642 (and (stringp flyspell-after-incorrect-word-string)
1643 (overlay-put overlay 'after-string
1644 flyspell-after-incorrect-word-string)))
1647 ;;*---------------------------------------------------------------------*/
1648 ;;* flyspell-highlight-incorrect-region ... */
1649 ;;*---------------------------------------------------------------------*/
1650 (defun flyspell-highlight-incorrect-region (beg end poss)
1651 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1652 POSS is usually a list of possible spelling/correction lists,
1653 as returned by `ispell-parse-output'.
1654 It can also be the symbol `doublon', in the case where the word
1655 is itself incorrect, but suspiciously repeated."
1656 (let ((inhibit-read-only t))
1657 (unless (run-hook-with-args-until-success
1658 'flyspell-incorrect-hook beg end poss)
1659 (if (or flyspell-highlight-properties
1660 (not (flyspell-properties-at-p beg)))
1662 ;; we cleanup all the overlay that are in the region, not
1663 ;; beginning at the word start position
1664 (if (< (1+ beg) end)
1665 (let ((os (overlays-in (1+ beg) end)))
1667 (if (flyspell-overlay-p (car os))
1668 (delete-overlay (car os)))
1669 (setq os (cdr os)))))
1670 ;; we cleanup current overlay at the same position
1671 (flyspell-unhighlight-at beg)
1672 ;; now we can use a new overlay
1673 (setq flyspell-overlay
1674 (make-flyspell-overlay
1675 beg end 'flyspell-incorrect 'highlight)))))))
1677 ;;*---------------------------------------------------------------------*/
1678 ;;* flyspell-highlight-duplicate-region ... */
1679 ;;*---------------------------------------------------------------------*/
1680 (defun flyspell-highlight-duplicate-region (beg end poss)
1681 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1682 POSS is a list of possible spelling/correction lists,
1683 as returned by `ispell-parse-output'."
1684 (let ((inhibit-read-only t))
1685 (unless (run-hook-with-args-until-success
1686 'flyspell-incorrect-hook beg end poss)
1687 (if (or flyspell-highlight-properties
1688 (not (flyspell-properties-at-p beg)))
1690 ;; we cleanup current overlay at the same position
1691 (flyspell-unhighlight-at beg)
1692 ;; now we can use a new overlay
1693 (setq flyspell-overlay
1694 (make-flyspell-overlay beg end
1698 ;;*---------------------------------------------------------------------*/
1699 ;;* flyspell-auto-correct-cache ... */
1700 ;;*---------------------------------------------------------------------*/
1701 (defvar flyspell-auto-correct-pos nil)
1702 (defvar flyspell-auto-correct-region nil)
1703 (defvar flyspell-auto-correct-ring nil)
1704 (defvar flyspell-auto-correct-word nil)
1705 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1706 (make-variable-buffer-local 'flyspell-auto-correct-region)
1707 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1708 (make-variable-buffer-local 'flyspell-auto-correct-word)
1710 ;;*---------------------------------------------------------------------*/
1711 ;;* flyspell-check-previous-highlighted-word ... */
1712 ;;*---------------------------------------------------------------------*/
1713 (defun flyspell-check-previous-highlighted-word (&optional arg)
1714 "Correct the closer misspelled word.
1715 This function scans a mis-spelled word before the cursor. If it finds one
1716 it proposes replacement for that word. With prefix arg, count that many
1717 misspelled words backwards."
1719 (let ((pos1 (point))
1721 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1724 (while (and (setq pos (previous-overlay-change pos))
1727 (if (> pos (point-min))
1729 (setq ovs (overlays-at (1- pos)))
1732 (setq ovs (cdr ovs))
1733 (if (and (flyspell-overlay-p ov)
1734 (= 0 (setq arg (1- arg))))
1735 (throw 'exit t)))))))
1739 (error "No word to correct before point"))))
1741 ;;*---------------------------------------------------------------------*/
1742 ;;* flyspell-display-next-corrections ... */
1743 ;;*---------------------------------------------------------------------*/
1744 (defun flyspell-display-next-corrections (corrections)
1745 (let ((string "Corrections:")
1748 (while (< (length string) 80)
1749 (if (equal (car l) flyspell-auto-correct-word)
1750 (setq pos (cons (+ 1 (length string)) pos)))
1751 (setq string (concat string " " (car l)))
1754 (let ((num (car pos)))
1755 (put-text-property num
1756 (+ num (length flyspell-auto-correct-word))
1757 'face 'flyspell-incorrect
1759 (setq pos (cdr pos)))
1760 (if (fboundp 'display-message)
1761 (display-message 'no-log string)
1762 (message "%s" string))))
1764 ;;*---------------------------------------------------------------------*/
1765 ;;* flyspell-abbrev-table ... */
1766 ;;*---------------------------------------------------------------------*/
1767 (defun flyspell-abbrev-table ()
1768 (if flyspell-use-global-abbrev-table-p
1770 (or local-abbrev-table global-abbrev-table)))
1772 ;;*---------------------------------------------------------------------*/
1773 ;;* flyspell-define-abbrev ... */
1774 ;;*---------------------------------------------------------------------*/
1775 (defun flyspell-define-abbrev (name expansion)
1776 (let ((table (flyspell-abbrev-table)))
1778 (define-abbrev table name expansion))))
1780 ;;*---------------------------------------------------------------------*/
1781 ;;* flyspell-auto-correct-word ... */
1782 ;;*---------------------------------------------------------------------*/
1783 (defun flyspell-auto-correct-word ()
1784 "Correct the current word.
1785 This command proposes various successive corrections for the current word."
1788 (old-max (point-max)))
1789 ;; use the correct dictionary
1790 (flyspell-accept-buffer-local-defs)
1791 (if (and (eq flyspell-auto-correct-pos pos)
1792 (consp flyspell-auto-correct-region))
1793 ;; we have already been using the function at the same location
1794 (let* ((start (car flyspell-auto-correct-region))
1795 (len (cdr flyspell-auto-correct-region)))
1796 (flyspell-unhighlight-at start)
1797 (delete-region start (+ start len))
1798 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1799 (let* ((word (car flyspell-auto-correct-ring))
1800 (len (length word)))
1801 (rplacd flyspell-auto-correct-region len)
1803 (if flyspell-abbrev-p
1804 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1805 flyspell-auto-correct-word)
1806 (flyspell-change-abbrev (flyspell-abbrev-table)
1807 flyspell-auto-correct-word
1809 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1810 (funcall flyspell-insert-function word)
1812 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1813 (flyspell-ajust-cursor-point pos (point) old-max)
1814 (setq flyspell-auto-correct-pos (point)))
1815 ;; fetch the word to be checked
1816 (let ((word (flyspell-get-word nil)))
1818 (let ((start (car (cdr word)))
1819 (end (car (cdr (cdr word))))
1822 (setq flyspell-auto-correct-word word)
1823 ;; now check spelling of word.
1824 (ispell-send-string "%\n") ;put in verbose mode
1825 (ispell-send-string (concat "^" word "\n"))
1826 ;; wait until ispell has processed word.
1828 (accept-process-output ispell-process)
1829 (not (string= "" (car ispell-filter)))))
1830 (setq ispell-filter (cdr ispell-filter))
1831 (if (consp ispell-filter)
1832 (setq poss (ispell-parse-output (car ispell-filter))))
1834 ((or (eq poss t) (stringp poss))
1835 ;; don't correct word
1839 (error "Ispell: error in Ispell process"))
1841 ;; the word is incorrect, we have to propose a replacement
1842 (let ((replacements (if flyspell-sort-corrections
1843 (sort (car (cdr (cdr poss))) 'string<)
1844 (car (cdr (cdr poss))))))
1845 (setq flyspell-auto-correct-region nil)
1846 (if (consp replacements)
1848 (let ((replace (car replacements)))
1849 (let ((new-word replace))
1850 (if (not (equal new-word (car poss)))
1852 ;; the save the current replacements
1853 (setq flyspell-auto-correct-region
1854 (cons start (length new-word)))
1855 (let ((l replacements))
1856 (while (consp (cdr l))
1858 (rplacd l (cons (car poss) replacements)))
1859 (setq flyspell-auto-correct-ring
1861 (flyspell-unhighlight-at start)
1862 (delete-region start end)
1863 (funcall flyspell-insert-function new-word)
1864 (if flyspell-abbrev-p
1865 (if (flyspell-already-abbrevp
1866 (flyspell-abbrev-table) word)
1867 (flyspell-change-abbrev
1868 (flyspell-abbrev-table)
1871 (flyspell-define-abbrev word
1874 (flyspell-display-next-corrections
1875 (cons new-word flyspell-auto-correct-ring))
1876 (flyspell-ajust-cursor-point pos
1879 (setq flyspell-auto-correct-pos (point))
1880 (ispell-pdict-save t)))))))
1882 ;;*---------------------------------------------------------------------*/
1883 ;;* flyspell-auto-correct-previous-pos ... */
1884 ;;*---------------------------------------------------------------------*/
1885 (defvar flyspell-auto-correct-previous-pos nil
1886 "Holds the start of the first incorrect word before point.")
1888 ;;*---------------------------------------------------------------------*/
1889 ;;* flyspell-auto-correct-previous-hook ... */
1890 ;;*---------------------------------------------------------------------*/
1891 (defun flyspell-auto-correct-previous-hook ()
1892 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1893 Sets `flyspell-auto-correct-previous-pos' to nil"
1895 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1896 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1897 (setq flyspell-auto-correct-previous-pos nil)))
1899 ;;*---------------------------------------------------------------------*/
1900 ;;* flyspell-auto-correct-previous-word ... */
1901 ;;*---------------------------------------------------------------------*/
1902 (defun flyspell-auto-correct-previous-word (position)
1903 "Auto correct the first mispelled word that occurs before point.
1904 But don't look beyond what's visible on the screen."
1909 (move-to-window-line 0)
1911 (move-to-window-line -1)
1915 (narrow-to-region top bot)
1916 (overlay-recenter (point))
1918 (add-hook 'pre-command-hook
1919 (function flyspell-auto-correct-previous-hook) t t)
1921 (unless flyspell-auto-correct-previous-pos
1922 ;; only reset if a new overlay exists
1923 (setq flyspell-auto-correct-previous-pos nil)
1925 (let ((overlay-list (overlays-in (point-min) position))
1926 (new-overlay 'dummy-value))
1928 ;; search for previous (new) flyspell overlay
1929 (while (and new-overlay
1930 (or (not (flyspell-overlay-p new-overlay))
1931 ;; check if its face has changed
1932 (not (eq (get-char-property
1933 (overlay-start new-overlay) 'face)
1934 'flyspell-incorrect))))
1935 (setq new-overlay (car-safe overlay-list))
1936 (setq overlay-list (cdr-safe overlay-list)))
1938 ;; if nothing new exits new-overlay should be nil
1939 (if new-overlay ;; the length of the word may change so go to the start
1940 (setq flyspell-auto-correct-previous-pos
1941 (overlay-start new-overlay)))))
1943 (when flyspell-auto-correct-previous-pos
1945 (goto-char flyspell-auto-correct-previous-pos)
1946 (let ((ispell-following-word t)) ;; point is at start
1947 (if (numberp flyspell-auto-correct-previous-pos)
1948 (goto-char flyspell-auto-correct-previous-pos))
1949 (flyspell-auto-correct-word))
1950 ;; the point may have moved so reset this
1951 (setq flyspell-auto-correct-previous-pos (point))))))))
1953 ;;*---------------------------------------------------------------------*/
1954 ;;* flyspell-correct-word ... */
1955 ;;*---------------------------------------------------------------------*/
1956 (defun flyspell-correct-word (event)
1957 "Pop up a menu of possible corrections for a misspelled word.
1958 The word checked is the word at the mouse position."
1960 ;; use the correct dictionary
1961 (flyspell-accept-buffer-local-defs)
1962 ;; retain cursor location (I don't know why but save-excursion here fails).
1963 (let ((save (point)))
1964 (mouse-set-point event)
1965 (let ((cursor-location (point))
1966 (word (flyspell-get-word nil)))
1968 (let ((start (car (cdr word)))
1969 (end (car (cdr (cdr word))))
1972 ;; now check spelling of word.
1973 (ispell-send-string "%\n") ;put in verbose mode
1974 (ispell-send-string (concat "^" word "\n"))
1975 ;; wait until ispell has processed word
1977 (accept-process-output ispell-process)
1978 (not (string= "" (car ispell-filter)))))
1979 (setq ispell-filter (cdr ispell-filter))
1980 (if (consp ispell-filter)
1981 (setq poss (ispell-parse-output (car ispell-filter))))
1983 ((or (eq poss t) (stringp poss))
1984 ;; don't correct word
1988 (error "Ispell: error in Ispell process"))
1990 (flyspell-xemacs-popup
1991 poss word cursor-location start end save))
1993 ;; The word is incorrect, we have to propose a replacement.
1994 (flyspell-do-correct (flyspell-emacs-popup event poss word)
1995 poss word cursor-location start end save)))
1996 (ispell-pdict-save t))))))
1998 ;;*---------------------------------------------------------------------*/
1999 ;;* flyspell-do-correct ... */
2000 ;;*---------------------------------------------------------------------*/
2001 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2002 "The popup menu callback."
2003 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2004 ;; it instead right after calling the function.
2005 (cond ((eq replace 'ignore)
2010 (ispell-send-string (concat "*" word "\n"))
2011 ;; This was added only to the XEmacs side in revision 1.18 of
2012 ;; flyspell. I assume its absence on the Emacs side was an
2013 ;; oversight. --Stef
2014 (ispell-send-string "#\n")
2015 (flyspell-unhighlight-at cursor-location)
2016 (setq ispell-pdict-modified-p '(t)))
2017 ((or (eq replace 'buffer) (eq replace 'session))
2018 (ispell-send-string (concat "@" word "\n"))
2019 (flyspell-unhighlight-at cursor-location)
2020 (if (null ispell-pdict-modified-p)
2021 (setq ispell-pdict-modified-p
2022 (list ispell-pdict-modified-p)))
2024 (if (eq replace 'buffer)
2025 (ispell-add-per-file-word-list word)))
2027 ;; This was added only to the Emacs side. I assume its absence on
2028 ;; the XEmacs side was an oversight. --Stef
2029 (flyspell-unhighlight-at cursor-location)
2030 (let ((old-max (point-max))
2031 (new-word (if (atom replace)
2034 (cursor-location (+ (- (length word) (- end start))
2036 (unless (equal new-word (car poss))
2037 (delete-region start end)
2039 (funcall flyspell-insert-function new-word)
2040 (if flyspell-abbrev-p
2041 (flyspell-define-abbrev word new-word)))
2042 ;; In the original Emacs code, this was only called in the body
2043 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2044 (flyspell-ajust-cursor-point save cursor-location old-max)))
2049 ;;*---------------------------------------------------------------------*/
2050 ;;* flyspell-ajust-cursor-point ... */
2051 ;;*---------------------------------------------------------------------*/
2052 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2053 (if (>= save cursor-location)
2054 (let ((new-pos (+ save (- (point-max) old-max))))
2056 ((< new-pos (point-min))
2058 ((> new-pos (point-max))
2063 ;;*---------------------------------------------------------------------*/
2064 ;;* flyspell-emacs-popup ... */
2065 ;;*---------------------------------------------------------------------*/
2066 (defun flyspell-emacs-popup (event poss word)
2067 "The Emacs popup menu."
2069 (let* ((mouse-pos (mouse-position))
2070 (mouse-pos (if (nth 1 mouse-pos)
2072 (set-mouse-position (car mouse-pos)
2073 (/ (frame-width) 2) 2)
2075 (setq event (list (list (car (cdr mouse-pos))
2076 (1+ (cdr (cdr mouse-pos))))
2078 (let* ((corrects (if flyspell-sort-corrections
2079 (sort (car (cdr (cdr poss))) 'string<)
2080 (car (cdr (cdr poss)))))
2081 (cor-menu (if (consp corrects)
2082 (mapcar (lambda (correct)
2083 (list correct correct))
2086 (affix (car (cdr (cdr (cdr poss)))))
2088 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2090 (list (concat "Save affix: " (car affix))
2092 '("Accept (session)" session)
2093 '("Accept (buffer)" buffer))
2094 '(("Save word" save)
2095 ("Accept (session)" session)
2096 ("Accept (buffer)" buffer)))))
2097 (if (consp cor-menu)
2098 (append cor-menu (cons "" save))
2100 (menu (cons "flyspell correction menu" base-menu)))
2101 (car (x-popup-menu event
2102 (list (format "%s [%s]" word (or ispell-local-dictionary
2106 ;;*---------------------------------------------------------------------*/
2107 ;;* flyspell-xemacs-popup ... */
2108 ;;*---------------------------------------------------------------------*/
2109 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2110 "The XEmacs popup menu."
2111 (let* ((corrects (if flyspell-sort-corrections
2112 (sort (car (cdr (cdr poss))) 'string<)
2113 (car (cdr (cdr poss)))))
2114 (cor-menu (if (consp corrects)
2115 (mapcar (lambda (correct)
2117 (list 'flyspell-do-correct
2128 (affix (car (cdr (cdr (cdr poss)))))
2130 (menu (let ((save (if (and (consp affix) show-affix-info)
2132 (concat "Save affix: " (car affix))
2133 (list 'flyspell-do-correct
2144 (list 'flyspell-do-correct
2153 (session (vector "Accept (session)"
2154 (list 'flyspell-do-correct
2163 (buffer (vector "Accept (buffer)"
2164 (list 'flyspell-do-correct
2173 (if (consp cor-menu)
2174 (append cor-menu (list "-" save session buffer))
2175 (list save session buffer)))))
2176 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2180 ;;*---------------------------------------------------------------------*/
2181 ;;* Some example functions for real autocorrecting */
2182 ;;*---------------------------------------------------------------------*/
2183 (defun flyspell-maybe-correct-transposition (beg end poss)
2184 "Check replacements for transposed characters.
2186 If the text between BEG and END is equal to a correction suggested by
2187 Ispell, after transposing two adjacent characters, correct the text,
2190 The third arg POSS is either the symbol 'doublon' or a list of
2191 possible corrections as returned by `ispell-parse-output'.
2193 This function is meant to be added to `flyspell-incorrect-hook'."
2196 (let ((str (buffer-substring beg end))
2197 (i 0) (len (- end beg)) tmp)
2198 (while (< (1+ i) len)
2199 (setq tmp (aref str i))
2200 (aset str i (aref str (1+ i)))
2201 (aset str (1+ i) tmp)
2202 (when (member str (nth 2 poss))
2204 (goto-char (+ beg i 1))
2205 (transpose-chars 1))
2207 (setq tmp (aref str i))
2208 (aset str i (aref str (1+ i)))
2209 (aset str (1+ i) tmp)
2213 (defun flyspell-maybe-correct-doubling (beg end poss)
2214 "Check replacements for doubled characters.
2216 If the text between BEG and END is equal to a correction suggested by
2217 Ispell, after removing a pair of doubled characters, correct the text,
2220 The third arg POSS is either the symbol 'doublon' or a list of
2221 possible corrections as returned by `ispell-parse-output'.
2223 This function is meant to be added to `flyspell-incorrect-hook'."
2226 (let ((str (buffer-substring beg end))
2227 (i 0) (len (- end beg)))
2228 (while (< (1+ i) len)
2229 (when (and (= (aref str i) (aref str (1+ i)))
2230 (member (concat (substring str 0 (1+ i))
2231 (substring str (+ i 2)))
2233 (goto-char (+ beg i))
2239 ;;*---------------------------------------------------------------------*/
2240 ;;* flyspell-already-abbrevp ... */
2241 ;;*---------------------------------------------------------------------*/
2242 (defun flyspell-already-abbrevp (table word)
2243 (let ((sym (abbrev-symbol word table)))
2244 (and sym (symbolp sym))))
2246 ;;*---------------------------------------------------------------------*/
2247 ;;* flyspell-change-abbrev ... */
2248 ;;*---------------------------------------------------------------------*/
2249 (defun flyspell-change-abbrev (table old new)
2250 (set (abbrev-symbol old table) new))
2254 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2255 ;;; flyspell.el ends here