2 ;;; flyspell.el --- on-the-fly spell checker
4 ;; Copyright (C) 1998, 2000, 2003, 2004, 2005 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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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.
48 ;*---------------------------------------------------------------------*/
50 ;*---------------------------------------------------------------------*/
51 (defgroup flyspell nil
52 "Spell checking on the fly."
58 ;*---------------------------------------------------------------------*/
59 ;* Which emacs are we currently running */
60 ;*---------------------------------------------------------------------*/
61 (defvar flyspell-emacs
63 ((string-match "XEmacs" emacs-version
)
67 "The type of Emacs we are currently running.")
69 (defvar flyspell-use-local-map
70 (or (eq flyspell-emacs
'xemacs
)
71 (not (string< emacs-version
"20"))))
73 ;*---------------------------------------------------------------------*/
74 ;* User configuration ... */
75 ;*---------------------------------------------------------------------*/
76 (defcustom flyspell-highlight-flag t
77 "*How Flyspell should indicate misspelled words.
78 Non-nil means use highlight, nil means use minibuffer messages."
82 (defcustom flyspell-mark-duplications-flag t
83 "*Non-nil means Flyspell reports a repeated word as an error."
87 (defcustom flyspell-sort-corrections nil
88 "*Non-nil means, sort the corrections alphabetically before popping them."
93 (defcustom flyspell-duplicate-distance -
1
94 "*The maximum distance for finding duplicates of unrecognized words.
95 This applies to the feature that when a word is not found in the dictionary,
96 if the same spelling occurs elsewhere in the buffer,
97 Flyspell uses a different face (`flyspell-duplicate-face') to highlight it.
98 This variable specifies how far to search to find such a duplicate.
99 -1 means no limit (search the whole buffer).
100 0 means do not search for duplicate unrecognized spellings."
105 (defcustom flyspell-delay
3
106 "*The number of seconds to wait before checking, after a \"delayed\" command."
110 (defcustom flyspell-persistent-highlight t
111 "*Non-nil means misspelled words remain highlighted until corrected.
112 If this variable is nil, only the most recently detected misspelled word
117 (defcustom flyspell-highlight-properties t
118 "*Non-nil means highlight incorrect words even if a property exists for this word."
122 (defcustom flyspell-default-delayed-commands
123 '(self-insert-command
125 backward-or-forward-delete-char
127 scrollbar-vertical-drag
128 backward-delete-char-untabify
)
129 "The standard list of delayed commands for Flyspell.
130 See `flyspell-delayed-commands'."
133 :type
'(repeat (symbol)))
135 (defcustom flyspell-delayed-commands nil
136 "List of commands that are \"delayed\" for Flyspell mode.
137 After these commands, Flyspell checking is delayed for a short time,
138 whose length is specified by `flyspell-delay'."
140 :type
'(repeat (symbol)))
142 (defcustom flyspell-default-deplacement-commands
147 "The standard list of deplacement commands for Flyspell.
148 See `flyspell-deplacement-commands'."
151 :type
'(repeat (symbol)))
153 (defcustom flyspell-deplacement-commands nil
154 "List of commands that are \"deplacement\" for Flyspell mode.
155 After these commands, Flyspell checking is performed only if the previous
156 command was not the very same command."
159 :type
'(repeat (symbol)))
161 (defcustom flyspell-issue-welcome-flag t
162 "*Non-nil means that Flyspell should display a welcome message when started."
166 (defcustom flyspell-issue-message-flag t
167 "*Non-nil means that Flyspell emits messages when checking words."
171 (defcustom flyspell-incorrect-hook nil
172 "*List of functions to be called when incorrect words are encountered.
173 Each function is given three arguments: the beginning and the end
174 of the incorrect region. The third is either the symbol 'doublon' or the list
175 of possible corrections as returned by 'ispell-parse-output'.
177 If any of the functions return non-Nil, the word is not highlighted as
183 (defcustom flyspell-default-dictionary nil
184 "A string that is the name of the default dictionary.
185 This is passed to the `ispell-change-dictionary' when flyspell is started.
186 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
187 when flyspell is started, the value of that variable is used instead
188 of `flyspell-default-dictionary' to select the default dictionary.
189 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
190 Ispell's ultimate default dictionary."
193 :type
'(choice string
(const :tag
"Default" nil
)))
195 (defcustom flyspell-tex-command-regexp
196 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
197 "A string that is the regular expression that matches TeX commands."
202 (defcustom flyspell-check-tex-math-command nil
203 "*Non nil means check even inside TeX math environment.
204 TeX math environments are discovered by the TEXMATHP that implemented
205 inside the texmathp.el Emacs package. That package may be found at:
206 http://strw.leidenuniv.nl/~dominik/Tools"
210 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
211 '("francais" "deutsch8" "norsk")
212 "List of dictionary names that consider `-' as word delimiter."
215 :type
'(repeat (string)))
217 (defcustom flyspell-abbrev-p
219 "*If non-nil, add correction to abbreviation table."
224 (defcustom flyspell-use-global-abbrev-table-p
226 "*If non-nil, prefer global abbrev table to local abbrev table."
232 (defcustom flyspell-mode-line-string
" Fly"
233 "*String displayed on the modeline when flyspell is active.
234 Set this to nil if you don't want a modeline indicator."
236 :type
'(choice string
(const :tag
"None" nil
)))
238 (defcustom flyspell-large-region
1000
239 "*The threshold that determines if a region is small.
240 The `flyspell-region' function is invoked if the region is small, the
241 word are checked one after the other using regular flyspell check
242 means. If the region is large, a new Ispell process is spawned to get
245 if flyspell-large-region is nil, regions are treated as small."
248 :type
'(choice number boolean
))
250 (defcustom flyspell-insert-function
(function insert
)
251 "*Function for inserting word by flyspell upon correction."
255 (defcustom flyspell-before-incorrect-word-string nil
256 "String used to indicate an incorrect word starting."
258 :type
'(choice string
(const nil
)))
260 (defcustom flyspell-after-incorrect-word-string nil
261 "String used to indicate an incorrect word ending."
263 :type
'(choice string
(const nil
)))
265 (defcustom flyspell-use-meta-tab t
266 "*Non-nil means that flyspell uses META-TAB to correct word."
270 (defcustom flyspell-auto-correct-binding
272 ((eq flyspell-emacs
'xemacs
)
276 "The key binding for flyspell auto correction."
279 ;*---------------------------------------------------------------------*/
280 ;* Mode specific options */
281 ;* ------------------------------------------------------------- */
282 ;* Mode specific options enable users to disable flyspell on */
283 ;* certain word depending of the emacs mode. For instance, when */
284 ;* using flyspell with mail-mode add the following expression */
285 ;* in your .emacs file: */
286 ;* (add-hook 'mail-mode */
287 ;* '(lambda () (setq flyspell-generic-check-word-p */
288 ;* 'mail-mode-flyspell-verify))) */
289 ;*---------------------------------------------------------------------*/
290 (defvar flyspell-generic-check-word-p nil
291 "Function providing per-mode customization over which words are flyspelled.
292 Returns t to continue checking, nil otherwise.
293 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
294 property of the major mode name.")
295 (make-variable-buffer-local 'flyspell-generic-check-word-p
)
297 ;*--- mail mode -------------------------------------------------------*/
298 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
299 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
300 (defun mail-mode-flyspell-verify ()
301 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
302 (let ((header-end (save-excursion
303 (goto-char (point-min))
306 (regexp-quote mail-header-separator
)
310 (signature-begin (save-excursion
311 (goto-char (point-max))
312 (re-search-backward message-signature-separator
315 (cond ((< (point) header-end
)
316 (and (save-excursion (beginning-of-line)
317 (looking-at "^Subject:"))
318 (> (point) (match-end 0))))
319 ((> (point) signature-begin
)
324 (not (looking-at "[>}|]\\|To:")))))))
326 ;*--- texinfo mode ----------------------------------------------------*/
327 (put 'texinfo-mode
'flyspell-mode-predicate
'texinfo-mode-flyspell-verify
)
328 (defun texinfo-mode-flyspell-verify ()
329 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
332 (not (looking-at "@"))))
334 ;*--- tex mode --------------------------------------------------------*/
335 (put 'tex-mode
'flyspell-mode-predicate
'tex-mode-flyspell-verify
)
336 (defun tex-mode-flyspell-verify ()
337 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
340 (re-search-backward "^[ \t]*%%%[ \t]+Local" (point-min) t
)))
342 (let ((this (point-marker))
343 (e (progn (end-of-line) (point-marker))))
345 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t
)
346 (and (>= this
(match-beginning 0))
347 (<= this
(match-end 0)) )))))))
349 ;*--- sgml mode -------------------------------------------------------*/
350 (put 'sgml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
351 (put 'html-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
353 (defun sgml-mode-flyspell-verify ()
354 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
356 (let ((this (point-marker))
357 (s (progn (beginning-of-line) (point-marker)))
358 (e (progn (end-of-line) (point-marker))))
361 (and (re-search-forward "[^<]*>" e t
)
362 (= (match-beginning 0) this
)))
365 (and (re-search-backward "<[^>]*" s t
)
366 (= (match-end 0) this
)))
369 (and (re-search-forward "[^&]*;" e t
)
370 (= (match-beginning 0) this
)))
373 (and (re-search-backward "&[^;]*" s t
)
374 (= (match-end 0) this
)))))))))
376 ;*---------------------------------------------------------------------*/
377 ;* Programming mode */
378 ;*---------------------------------------------------------------------*/
379 (defvar flyspell-prog-text-faces
380 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
381 "Faces corresponding to text in programming-mode buffers.")
383 (defun flyspell-generic-progmode-verify ()
384 "Used for `flyspell-generic-check-word-p' in programming modes."
385 (let ((f (get-text-property (point) 'face
)))
386 (memq f flyspell-prog-text-faces
)))
389 (defun flyspell-prog-mode ()
390 "Turn on `flyspell-mode' for comments and strings."
392 (setq flyspell-generic-check-word-p
'flyspell-generic-progmode-verify
)
394 (run-hooks 'flyspell-prog-mode-hook
))
396 ;*---------------------------------------------------------------------*/
397 ;* Overlay compatibility */
398 ;*---------------------------------------------------------------------*/
399 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
400 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
401 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
402 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
403 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
404 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
405 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
406 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
408 ;*---------------------------------------------------------------------*/
409 ;* The minor mode declaration. */
410 ;*---------------------------------------------------------------------*/
411 (eval-when-compile (defvar flyspell-local-mouse-map
))
414 (defvar flyspell-mode nil
)
415 (make-variable-buffer-local 'flyspell-mode
)
417 (defvar flyspell-mouse-map
418 (let ((map (make-sparse-keymap)))
419 (if flyspell-use-meta-tab
420 (define-key map
"\M-\t" #'flyspell-auto-correct-word
))
421 (define-key map
(if (featurep 'xemacs
) [button2] [down-mouse-2])
422 #'flyspell-correct-word)
423 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
424 (define-key map [(control \,)] 'flyspell-goto-next-error)
425 (define-key map [(control \.)] 'flyspell-auto-correct-word)
429 (defvar flyspell-mode-map (make-sparse-keymap))
431 ;; mouse, keyboard bindings and misc definition
432 (when (or (assoc 'flyspell-mode minor-mode-map-alist)
433 (setq minor-mode-map-alist
434 (cons (cons 'flyspell-mode flyspell-mode-map)
435 minor-mode-map-alist)))
436 (if flyspell-use-meta-tab
437 (define-key flyspell-mode-map "\M-\t" 'flyspell-auto-correct-word))
439 ((eq flyspell-emacs 'xemacs)
440 (define-key flyspell-mode-map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
441 (define-key flyspell-mode-map [(control \,)] 'flyspell-goto-next-error)
442 (define-key flyspell-mode-map [(control \.)] 'flyspell-auto-correct-word))
443 (flyspell-use-local-map
444 (define-key flyspell-mode-map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
445 (define-key flyspell-mode-map [?\C-\,] 'flyspell-goto-next-error)
446 (define-key flyspell-mode-map [?\C-\.] 'flyspell-auto-correct-word))))
449 ;; the name of the overlay property that defines the keymap
450 (defvar flyspell-overlay-keymap-property-name 'keymap)
452 ;; dash character machinery
453 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
454 "*Non-nil means that the `-' char is considered as a word delimiter.")
455 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
456 (defvar flyspell-dash-dictionary nil)
457 (make-variable-buffer-local 'flyspell-dash-dictionary)
458 (defvar flyspell-dash-local-dictionary nil)
459 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
461 ;*---------------------------------------------------------------------*/
463 ;*---------------------------------------------------------------------*/
464 (defface flyspell-incorrect-face
465 (if (eq flyspell-emacs 'xemacs)
466 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
468 '((((class color)) (:foreground "OrangeRed" :weight bold :underline t))
470 "Face used for marking a misspelled word in Flyspell."
473 (defface flyspell-duplicate-face
474 (if (eq flyspell-emacs 'xemacs)
475 '((((class color)) (:foreground "Gold3" :bold t :underline t))
477 '((((class color)) (:foreground "Gold3" :weight bold :underline t))
479 "Face used for marking a misspelled word that appears twice in the buffer.
480 See also `flyspell-duplicate-distance'."
483 (defvar flyspell-overlay nil)
485 ;*---------------------------------------------------------------------*/
486 ;* flyspell-mode ... */
487 ;*---------------------------------------------------------------------*/
489 (defun flyspell-mode (&optional arg)
490 "Minor mode performing on-the-fly spelling checking.
491 Ispell is automatically spawned on background for each entered words.
492 The default flyspell behavior is to highlight incorrect words.
493 With no argument, this command toggles Flyspell mode.
494 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
497 \\[ispell-word]: correct words (using Ispell).
498 \\[flyspell-auto-correct-word]: automatically correct word.
499 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
500 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
503 This runs `flyspell-mode-hook' after flyspell is entered.
506 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
507 valid. For instance, a personal dictionary can be used by
508 invoking `ispell-change-dictionary'.
510 Consider using the `ispell-parser' to check your text. For instance
512 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
515 \\[flyspell-region] checks all words inside a region.
516 \\[flyspell-buffer] checks the whole buffer."
518 (let ((old-flyspell-mode flyspell-mode))
519 ;; Mark the mode as on or off.
520 (setq flyspell-mode (not (or (and (null arg) flyspell-mode)
521 (<= (prefix-numeric-value arg) 0))))
523 (unless (eq flyspell-mode old-flyspell-mode)
527 ;; Force modeline redisplay.
528 (set-buffer-modified-p (buffer-modified-p)))))
530 ;*---------------------------------------------------------------------*/
532 ;*---------------------------------------------------------------------*/
534 (if (fboundp 'add-minor-mode)
535 (add-minor-mode 'flyspell-mode
536 'flyspell-mode-line-string
540 (or (assoc 'flyspell-mode minor-mode-alist)
541 (setq minor-mode-alist
542 (cons '(flyspell-mode flyspell-mode-line-string)
545 (or (assoc 'flyspell-mode minor-mode-map-alist)
546 (setq minor-mode-map-alist
547 (cons (cons 'flyspell-mode flyspell-mode-map)
548 minor-mode-map-alist))))
550 ;*---------------------------------------------------------------------*/
551 ;* flyspell-buffers ... */
552 ;* ------------------------------------------------------------- */
553 ;* For remembering buffers running flyspell */
554 ;*---------------------------------------------------------------------*/
555 (defvar flyspell-buffers nil)
557 ;*---------------------------------------------------------------------*/
558 ;* flyspell-minibuffer-p ... */
559 ;*---------------------------------------------------------------------*/
560 (defun flyspell-minibuffer-p (buffer)
561 "Is BUFFER a minibuffer?"
562 (let ((ws (get-buffer-window-list buffer t)))
563 (and (consp ws) (window-minibuffer-p (car ws)))))
565 ;*---------------------------------------------------------------------*/
566 ;* flyspell-version ... */
567 ;*---------------------------------------------------------------------*/
569 (defun flyspell-version ()
570 "The flyspell version"
574 ;*---------------------------------------------------------------------*/
575 ;* flyspell-accept-buffer-local-defs ... */
576 ;*---------------------------------------------------------------------*/
577 (defun flyspell-accept-buffer-local-defs ()
578 ;; strange problem. If buffer in current window has font-lock turned on,
579 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
580 ;; call will reset the buffer to the buffer in the current window. However,
581 ;; it only happens at startup (fix by Albert L. Ting).
582 (let ((buf (current-buffer)))
583 (ispell-accept-buffer-local-defs)
585 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
586 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
587 ;; the dictionary has changed
589 (setq flyspell-dash-dictionary ispell-dictionary)
590 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
591 (if (member (or ispell-local-dictionary ispell-dictionary)
592 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
593 (setq flyspell-consider-dash-as-word-delimiter-flag t)
594 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
596 ;*---------------------------------------------------------------------*/
597 ;* flyspell-mode-on ... */
598 ;*---------------------------------------------------------------------*/
599 (defun flyspell-mode-on ()
600 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
601 (setq ispell-highlight-face 'flyspell-incorrect-face)
602 ;; local dictionaries setup
603 (ispell-change-dictionary
604 (or ispell-local-dictionary ispell-dictionary flyspell-default-dictionary))
605 ;; we have to force ispell to accept the local definition or
606 ;; otherwise it could be too late, the local dictionary may
608 (flyspell-accept-buffer-local-defs)
609 ;; we put the `flyspell-delayed' property on some commands
610 (flyspell-delay-commands)
611 ;; we put the `flyspell-deplacement' property on some commands
612 (flyspell-deplacement-commands)
613 ;; we bound flyspell action to post-command hook
614 (if (eq flyspell-emacs 'xemacs)
615 (make-local-hook 'post-command-hook))
616 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
617 ;; we bound flyspell action to pre-command hook
618 (if (eq flyspell-emacs 'xemacs)
619 (make-local-hook 'pre-command-hook))
620 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
621 ;; we bound flyspell action to after-change hook
622 (make-local-variable 'after-change-functions)
623 (setq after-change-functions
624 (cons 'flyspell-after-change-function after-change-functions))
625 ;; set flyspell-generic-check-word-p based on the major mode
626 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
628 (setq flyspell-generic-check-word-p mode-predicate)))
629 ;; work around the fact that the `local-map' text-property replaces the
630 ;; buffer's local map rather than shadowing it.
631 (set (make-local-variable 'flyspell-mouse-map)
632 (let ((map (copy-keymap flyspell-mouse-map)))
633 (set-keymap-parent map (current-local-map))
634 (if (and (eq flyspell-emacs 'emacs)
635 (not (string< emacs-version "20")))
636 (define-key map '[tool-bar] nil))
638 (set (make-local-variable 'flyspell-mode-map)
639 (let ((map (copy-keymap flyspell-mode-map)))
640 (set-keymap-parent map (current-local-map))
641 (if (and (eq flyspell-emacs 'emacs)
642 (not (string< emacs-version "20")))
643 (define-key map '[tool-bar] nil))
645 ;; the welcome message
646 (if (and flyspell-issue-message-flag
647 flyspell-issue-welcome-flag
649 (let ((binding (where-is-internal 'flyspell-auto-correct-word
653 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
654 (key-description binding))
655 "Welcome to flyspell. Use Mouse-2 to correct words."))))
656 ;; we end with the flyspell hooks
657 (run-hooks 'flyspell-mode-hook))
659 ;*---------------------------------------------------------------------*/
660 ;* flyspell-delay-commands ... */
661 ;*---------------------------------------------------------------------*/
662 (defun flyspell-delay-commands ()
663 "Install the standard set of Flyspell delayed commands."
664 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
665 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
667 ;*---------------------------------------------------------------------*/
668 ;* flyspell-delay-command ... */
669 ;*---------------------------------------------------------------------*/
670 (defun flyspell-delay-command (command)
671 "Set COMMAND to be delayed, for Flyspell.
672 When flyspell `post-command-hook' is invoked because a delayed command
673 as been used the current word is not immediately checked.
674 It will be checked only after `flyspell-delay' seconds."
675 (interactive "SDelay Flyspell after Command: ")
676 (put command 'flyspell-delayed t))
678 ;*---------------------------------------------------------------------*/
679 ;* flyspell-deplacement-commands ... */
680 ;*---------------------------------------------------------------------*/
681 (defun flyspell-deplacement-commands ()
682 "Install the standard set of Flyspell deplacement commands."
683 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
684 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
686 ;*---------------------------------------------------------------------*/
687 ;* flyspell-deplacement-command ... */
688 ;*---------------------------------------------------------------------*/
689 (defun flyspell-deplacement-command (command)
690 "Set COMMAND that implement cursor movements, for Flyspell.
691 When flyspell `post-command-hook' is invoked because of a deplacement command
692 as been used the current word is checked only if the previous command was
693 not the very same deplacement command."
694 (interactive "SDeplacement Flyspell after Command: ")
695 (put command 'flyspell-deplacement t))
697 ;*---------------------------------------------------------------------*/
698 ;* flyspell-word-cache ... */
699 ;*---------------------------------------------------------------------*/
700 (defvar flyspell-word-cache-start nil)
701 (defvar flyspell-word-cache-end nil)
702 (defvar flyspell-word-cache-word nil)
703 (defvar flyspell-word-cache-result '_)
704 (make-variable-buffer-local 'flyspell-word-cache-start)
705 (make-variable-buffer-local 'flyspell-word-cache-end)
706 (make-variable-buffer-local 'flyspell-word-cache-word)
707 (make-variable-buffer-local 'flyspell-word-cache-result)
709 ;*---------------------------------------------------------------------*/
710 ;* The flyspell pre-hook, store the current position. In the */
711 ;* post command hook, we will check, if the word at this position */
712 ;* has to be spell checked. */
713 ;*---------------------------------------------------------------------*/
714 (defvar flyspell-pre-buffer nil)
715 (defvar flyspell-pre-point nil)
716 (defvar flyspell-pre-column nil)
717 (defvar flyspell-pre-pre-buffer nil)
718 (defvar flyspell-pre-pre-point nil)
720 ;*---------------------------------------------------------------------*/
721 ;* flyspell-previous-command ... */
722 ;*---------------------------------------------------------------------*/
723 (defvar flyspell-previous-command nil
724 "The last interactive command checked by Flyspell.")
726 ;*---------------------------------------------------------------------*/
727 ;* flyspell-pre-command-hook ... */
728 ;*---------------------------------------------------------------------*/
729 (defun flyspell-pre-command-hook ()
730 "Save the current buffer and point for Flyspell's post-command hook."
732 (setq flyspell-pre-buffer (current-buffer))
733 (setq flyspell-pre-point (point))
734 (setq flyspell-pre-column (current-column)))
736 ;*---------------------------------------------------------------------*/
737 ;* flyspell-mode-off ... */
738 ;*---------------------------------------------------------------------*/
740 (defun flyspell-mode-off ()
741 "Turn Flyspell mode off."
742 ;; we remove the hooks
743 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
744 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
745 (setq after-change-functions (delq 'flyspell-after-change-function
746 after-change-functions))
747 ;; we remove all the flyspell hilightings
748 (flyspell-delete-all-overlays)
749 ;; we have to erase pre cache variables
750 (setq flyspell-pre-buffer nil)
751 (setq flyspell-pre-point nil)
752 ;; we mark the mode as killed
753 (setq flyspell-mode nil))
755 ;*---------------------------------------------------------------------*/
756 ;* flyspell-check-pre-word-p ... */
757 ;*---------------------------------------------------------------------*/
758 (defun flyspell-check-pre-word-p ()
759 "Return non-nil if we should check the word before point.
760 More precisely, it applies to the word that was before point
761 before the current command."
763 ((or (not (numberp flyspell-pre-point))
764 (not (bufferp flyspell-pre-buffer))
765 (not (buffer-live-p flyspell-pre-buffer)))
767 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
768 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
770 ((or (and (= flyspell-pre-point (- (point) 1))
771 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
772 (= flyspell-pre-point (point))
773 (= flyspell-pre-point (+ (point) 1)))
775 ((and (symbolp this-command)
776 (not executing-kbd-macro)
777 (or (get this-command 'flyspell-delayed)
778 (and (get this-command 'flyspell-deplacement)
779 (eq flyspell-previous-command this-command)))
780 (or (= (current-column) 0)
781 (= (current-column) flyspell-pre-column)
782 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
784 ((not (eq (current-buffer) flyspell-pre-buffer))
786 ((not (and (numberp flyspell-word-cache-start)
787 (numberp flyspell-word-cache-end)))
790 (or (< flyspell-pre-point flyspell-word-cache-start)
791 (> flyspell-pre-point flyspell-word-cache-end)))))
793 ;*---------------------------------------------------------------------*/
794 ;* The flyspell after-change-hook, store the change position. In */
795 ;* the post command hook, we will check, if the word at this */
796 ;* position has to be spell checked. */
797 ;*---------------------------------------------------------------------*/
798 (defvar flyspell-changes nil)
800 ;*---------------------------------------------------------------------*/
801 ;* flyspell-after-change-function ... */
802 ;*---------------------------------------------------------------------*/
803 (defun flyspell-after-change-function (start stop len)
804 "Save the current buffer and point for Flyspell's post-command hook."
806 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
808 ;*---------------------------------------------------------------------*/
809 ;* flyspell-check-changed-word-p ... */
810 ;*---------------------------------------------------------------------*/
811 (defun flyspell-check-changed-word-p (start stop)
812 "Return t when the changed word has to be checked.
813 The answer depends of several criteria.
814 Mostly we check word delimiters."
816 ((and (memq (char-after start) '(?\n ? )) (> stop start))
818 ((not (numberp flyspell-pre-point))
820 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
822 ((let ((pos (point)))
823 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
828 ;*---------------------------------------------------------------------*/
829 ;* flyspell-check-word-p ... */
830 ;*---------------------------------------------------------------------*/
831 (defun flyspell-check-word-p ()
832 "Return t when the word at `point' has to be checked.
833 The answer depends of several criteria.
834 Mostly we check word delimiters."
836 ((<= (- (point-max) 1) (point-min))
837 ;; the buffer is not filled enough
839 ((and (and (> (current-column) 0)
840 (not (eq (current-column) flyspell-pre-column)))
843 (and (looking-at (flyspell-get-not-casechars))
844 (or flyspell-consider-dash-as-word-delimiter-flag
845 (not (looking-at "\\-"))))))
846 ;; yes because we have reached or typed a word delimiter.
848 ((symbolp this-command)
850 ((get this-command 'flyspell-deplacement)
851 (not (eq flyspell-previous-command this-command)))
852 ((get this-command 'flyspell-delayed)
853 ;; the current command is not delayed, that
854 ;; is that we must check the word now
855 (if (or (fboundp 'about-xemacs) (featurep 'xemacs))
856 (sit-for flyspell-delay nil)
857 (sit-for flyspell-delay 0 nil)))
861 ;*---------------------------------------------------------------------*/
862 ;* flyspell-debug-signal-no-check ... */
863 ;*---------------------------------------------------------------------*/
864 (defun flyspell-debug-signal-no-check (msg obj)
865 (setq debug-on-error t)
867 (let ((buffer (get-buffer-create "*flyspell-debug*")))
870 (insert "NO-CHECK:\n")
871 (insert (format " %S : %S\n" msg obj)))))
873 ;*---------------------------------------------------------------------*/
874 ;* flyspell-debug-signal-pre-word-checked ... */
875 ;*---------------------------------------------------------------------*/
876 (defun flyspell-debug-signal-pre-word-checked ()
877 (setq debug-on-error t)
879 (let ((buffer (get-buffer-create "*flyspell-debug*")))
881 (insert "PRE-WORD:\n")
882 (insert (format " pre-point : %S\n" flyspell-pre-point))
883 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
884 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
885 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
886 (goto-char (point-max)))))
888 ;*---------------------------------------------------------------------*/
889 ;* flyspell-debug-signal-word-checked ... */
890 ;*---------------------------------------------------------------------*/
891 (defun flyspell-debug-signal-word-checked ()
892 (setq debug-on-error t)
894 (let ((oldbuf (current-buffer))
895 (buffer (get-buffer-create "*flyspell-debug*"))
899 (insert (format " this-cmd : %S\n" this-command))
900 (insert (format " delayed : %S\n" (and (symbolp this-command)
901 (get this-command 'flyspell-delayed))))
902 (insert (format " point : %S\n" point))
903 (insert (format " prev-char : [%c] %S\n"
906 (let ((c (if (> (point) (point-min))
909 (char-after (point)))
915 (let ((c (if (> (point) (point-min))
918 (and (and (looking-at (flyspell-get-not-casechars)) 1)
919 (and (or flyspell-consider-dash-as-word-delimiter-flag
920 (not (looking-at "\\-"))) 2))))))
923 (insert (format " because : %S\n"
925 ((not (and (symbolp this-command)
926 (get this-command 'flyspell-delayed)))
927 ;; the current command is not delayed, that
928 ;; is that we must check the word now
932 (let ((c (if (> (point) (point-min))
935 (and (looking-at (flyspell-get-not-casechars))
936 (or flyspell-consider-dash-as-word-delimiter-flag
937 (not (looking-at "\\-"))))))))
940 ;; yes because we have reached or typed a word delimiter.
942 ((not (integerp flyspell-delay))
943 ;; yes because the user had set up a no-delay configuration.
947 (goto-char (point-max)))))
949 ;*---------------------------------------------------------------------*/
950 ;* flyspell-debug-signal-changed-checked ... */
951 ;*---------------------------------------------------------------------*/
952 (defun flyspell-debug-signal-changed-checked ()
953 (setq debug-on-error t)
955 (let ((buffer (get-buffer-create "*flyspell-debug*"))
958 (insert "CHANGED WORD:\n")
959 (insert (format " point : %S\n" point))
960 (goto-char (point-max)))))
962 ;*---------------------------------------------------------------------*/
963 ;* flyspell-post-command-hook ... */
964 ;* ------------------------------------------------------------- */
965 ;* It is possible that we check several words: */
966 ;* 1- the current word is checked if the predicate */
967 ;* FLYSPELL-CHECK-WORD-P is true */
968 ;* 2- the word that used to be the current word before the */
969 ;* THIS-COMMAND is checked if: */
970 ;* a- the previous word is different from the current word */
971 ;* b- the previous word as not just been checked by the */
972 ;* previous FLYSPELL-POST-COMMAND-HOOK */
973 ;* 3- the words changed by the THIS-COMMAND that are neither the */
974 ;* previous word nor the current word */
975 ;*---------------------------------------------------------------------*/
976 (defun flyspell-post-command-hook ()
977 "The `post-command-hook' used by flyspell to check a word in-the-fly."
979 (let ((command this-command))
980 (if (flyspell-check-pre-word-p)
982 '(flyspell-debug-signal-pre-word-checked)
983 (set-buffer flyspell-pre-buffer)
985 (goto-char flyspell-pre-point)
987 (if (flyspell-check-word-p)
989 '(flyspell-debug-signal-word-checked)
991 ;; we remember which word we have just checked.
992 ;; this will be used next time we will check a word
993 ;; to compare the next current word with the word
994 ;; that as been registered in the pre-command-hook
995 ;; that is these variables are used within the predicate
996 ;; FLYSPELL-CHECK-PRE-WORD-P
997 (setq flyspell-pre-pre-buffer (current-buffer))
998 (setq flyspell-pre-pre-point (point)))
1000 (setq flyspell-pre-pre-buffer nil)
1001 (setq flyspell-pre-pre-point nil)
1002 ;; when a word is not checked because of a delayed command
1003 ;; we do not disable the ispell cache.
1004 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
1006 (setq flyspell-word-cache-end -1)
1007 (setq flyspell-word-cache-result '_)))))
1008 (while (consp flyspell-changes)
1009 (let ((start (car (car flyspell-changes)))
1010 (stop (cdr (car flyspell-changes))))
1011 (if (flyspell-check-changed-word-p start stop)
1013 '(flyspell-debug-signal-changed-checked)
1016 (setq flyspell-changes (cdr flyspell-changes))))
1017 (setq flyspell-previous-command command)))
1019 ;*---------------------------------------------------------------------*/
1020 ;* flyspell-notify-misspell ... */
1021 ;*---------------------------------------------------------------------*/
1022 (defun flyspell-notify-misspell (start end word poss)
1023 (let ((replacements (if (stringp poss)
1025 (if flyspell-sort-corrections
1026 (sort (car (cdr (cdr poss))) 'string<)
1027 (car (cdr (cdr poss)))))))
1028 (if flyspell-issue-message-flag
1029 (message (format "mispelling `%s' %S" word replacements)))))
1031 ;*---------------------------------------------------------------------*/
1032 ;* flyspell-word-search-backward ... */
1033 ;*---------------------------------------------------------------------*/
1034 (defun flyspell-word-search-backward (word bound)
1038 (while (and (not r) (setq p (search-backward word bound t)))
1039 (let ((lw (flyspell-get-word '())))
1040 (if (and (consp lw) (string-equal (car lw) word))
1045 ;*---------------------------------------------------------------------*/
1046 ;* flyspell-word-search-forward ... */
1047 ;*---------------------------------------------------------------------*/
1048 (defun flyspell-word-search-forward (word bound)
1052 (while (and (not r) (setq p (search-forward word bound t)))
1053 (let ((lw (flyspell-get-word '())))
1054 (if (and (consp lw) (string-equal (car lw) word))
1056 (goto-char (1+ p)))))
1059 ;*---------------------------------------------------------------------*/
1060 ;* flyspell-word ... */
1061 ;*---------------------------------------------------------------------*/
1062 (defun flyspell-word (&optional following)
1063 "Spell check a word."
1064 (interactive (list current-prefix-arg))
1066 (setq following ispell-following-word))
1068 ;; use the correct dictionary
1069 (flyspell-accept-buffer-local-defs)
1070 (let* ((cursor-location (point))
1071 (flyspell-word (flyspell-get-word following))
1072 start end poss word)
1073 (if (or (eq flyspell-word nil)
1074 (and (fboundp flyspell-generic-check-word-p)
1075 (not (funcall flyspell-generic-check-word-p))))
1078 ;; destructure return flyspell-word info list.
1079 (setq start (car (cdr flyspell-word))
1080 end (car (cdr (cdr flyspell-word)))
1081 word (car flyspell-word))
1082 ;; before checking in the directory, we check for doublons.
1084 ((and (or (not (eq ispell-parser 'tex))
1085 (and (> start (point-min))
1086 (not (eq (char-after (1- start)) ?}))
1087 (not (eq (char-after (1- start)) ?\\))))
1088 flyspell-mark-duplications-flag
1090 (goto-char (1- start))
1091 (let ((p (flyspell-word-search-backward
1093 (- start (1+ (- end start))))))
1094 (and p (/= p (1- start))))))
1095 ;; yes, this is a doublon
1096 (flyspell-highlight-incorrect-region start end 'doublon)
1098 ((and (eq flyspell-word-cache-start start)
1099 (eq flyspell-word-cache-end end)
1100 (string-equal flyspell-word-cache-word word))
1101 ;; this word had been already checked, we skip
1102 flyspell-word-cache-result)
1103 ((and (eq ispell-parser 'tex)
1104 (flyspell-tex-command-p flyspell-word))
1105 ;; this is a correct word (because a tex command)
1106 (flyspell-unhighlight-at start)
1108 (flyspell-unhighlight-at (- end 1)))
1111 ;; we setup the cache
1112 (setq flyspell-word-cache-start start)
1113 (setq flyspell-word-cache-end end)
1114 (setq flyspell-word-cache-word word)
1115 ;; now check spelling of word.
1116 (process-send-string ispell-process "%\n")
1117 ;; put in verbose mode
1118 (process-send-string ispell-process
1119 (concat "^" word "\n"))
1120 ;; we mark the ispell process so it can be killed
1121 ;; when emacs is exited without query
1122 (if (fboundp 'process-kill-without-query)
1123 (process-kill-without-query ispell-process))
1124 ;; wait until ispell has processed word
1126 (accept-process-output ispell-process)
1127 (not (string= "" (car ispell-filter)))))
1128 ;; (process-send-string ispell-process "!\n")
1129 ;; back to terse mode.
1130 (setq ispell-filter (cdr ispell-filter))
1131 (if (consp ispell-filter)
1132 (setq poss (ispell-parse-output (car ispell-filter))))
1133 (let ((res (cond ((eq poss t)
1135 (setq flyspell-word-cache-result t)
1136 (flyspell-unhighlight-at start)
1138 (flyspell-unhighlight-at (- end 1)))
1140 ((and (stringp poss) flyspell-highlight-flag)
1142 (setq flyspell-word-cache-result t)
1143 (flyspell-unhighlight-at start)
1145 (flyspell-unhighlight-at (- end 1)))
1148 (setq flyspell-word-cache-result t)
1149 (flyspell-unhighlight-at start)
1151 (flyspell-unhighlight-at (- end 1)))
1153 ((or (and (< flyspell-duplicate-distance 0)
1156 (flyspell-word-search-backward
1161 (flyspell-word-search-forward
1164 (and (> flyspell-duplicate-distance 0)
1167 (flyspell-word-search-backward
1170 flyspell-duplicate-distance)))
1173 (flyspell-word-search-forward
1176 flyspell-duplicate-distance))))))
1177 (setq flyspell-word-cache-result nil)
1178 (if flyspell-highlight-flag
1179 (flyspell-highlight-duplicate-region
1181 (message (format "duplicate `%s'" word)))
1184 (setq flyspell-word-cache-result nil)
1185 ;; incorrect highlight the location
1186 (if flyspell-highlight-flag
1187 (flyspell-highlight-incorrect-region
1189 (flyspell-notify-misspell start end word poss))
1191 ;; return to original location
1192 (goto-char cursor-location)
1193 (if ispell-quit (setq ispell-quit nil))
1196 ;*---------------------------------------------------------------------*/
1197 ;* flyspell-tex-math-initialized ... */
1198 ;*---------------------------------------------------------------------*/
1199 (defvar flyspell-tex-math-initialized nil)
1201 ;*---------------------------------------------------------------------*/
1202 ;* flyspell-math-tex-command-p ... */
1203 ;* ------------------------------------------------------------- */
1204 ;* This function uses the texmathp package to check if (point) */
1205 ;* is within a tex command. In order to avoid using */
1206 ;* condition-case each time we use the variable */
1207 ;* flyspell-tex-math-initialized to make a special case the first */
1208 ;* time that function is called. */
1209 ;*---------------------------------------------------------------------*/
1210 (defun flyspell-math-tex-command-p ()
1212 (flyspell-check-tex-math-command
1214 ((eq flyspell-tex-math-initialized t)
1216 ((eq flyspell-tex-math-initialized 'error)
1219 (setq flyspell-tex-math-initialized t)
1223 (setq flyspell-tex-math-initialized 'error)
1226 ;*---------------------------------------------------------------------*/
1227 ;* flyspell-tex-command-p ... */
1228 ;*---------------------------------------------------------------------*/
1229 (defun flyspell-tex-command-p (word)
1230 "Return t if WORD is a TeX command."
1232 (let ((b (car (cdr word))))
1233 (and (re-search-backward "\\\\" (- (point) 100) t)
1234 (or (= (match-end 0) b)
1235 (and (goto-char (match-end 0))
1236 (looking-at flyspell-tex-command-regexp)
1237 (>= (match-end 0) b))))))
1238 (flyspell-math-tex-command-p)))
1240 ;*---------------------------------------------------------------------*/
1241 ;* flyspell-casechars-cache ... */
1242 ;*---------------------------------------------------------------------*/
1243 (defvar flyspell-casechars-cache nil)
1244 (defvar flyspell-ispell-casechars-cache nil)
1245 (make-variable-buffer-local 'flyspell-casechars-cache)
1246 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1248 ;*---------------------------------------------------------------------*/
1249 ;* flyspell-get-casechars ... */
1250 ;*---------------------------------------------------------------------*/
1251 (defun flyspell-get-casechars ()
1252 "This function builds a string that is the regexp of word chars.
1253 In order to avoid one useless string construction,
1254 this function changes the last char of the `ispell-casechars' string."
1255 (let ((ispell-casechars (ispell-get-casechars)))
1257 ((eq ispell-parser 'tex)
1258 (setq flyspell-ispell-casechars-cache ispell-casechars)
1259 (setq flyspell-casechars-cache
1260 (concat (substring ispell-casechars
1262 (- (length ispell-casechars) 1))
1264 flyspell-casechars-cache)
1266 (setq flyspell-ispell-casechars-cache ispell-casechars)
1267 (setq flyspell-casechars-cache ispell-casechars)
1268 flyspell-casechars-cache))))
1270 ;*---------------------------------------------------------------------*/
1271 ;* flyspell-get-not-casechars-cache ... */
1272 ;*---------------------------------------------------------------------*/
1273 (defvar flyspell-not-casechars-cache nil)
1274 (defvar flyspell-ispell-not-casechars-cache nil)
1275 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1276 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1278 ;*---------------------------------------------------------------------*/
1279 ;* flyspell-get-not-casechars ... */
1280 ;*---------------------------------------------------------------------*/
1281 (defun flyspell-get-not-casechars ()
1282 "This function builds a string that is the regexp of non-word chars."
1283 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1285 ((eq ispell-parser 'tex)
1286 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1287 (setq flyspell-not-casechars-cache
1288 (concat (substring ispell-not-casechars
1290 (- (length ispell-not-casechars) 1))
1292 flyspell-not-casechars-cache)
1294 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1295 (setq flyspell-not-casechars-cache ispell-not-casechars)
1296 flyspell-not-casechars-cache))))
1298 ;*---------------------------------------------------------------------*/
1299 ;* flyspell-get-word ... */
1300 ;*---------------------------------------------------------------------*/
1301 (defun flyspell-get-word (following &optional extra-otherchars)
1302 "Return the word for spell-checking according to Ispell syntax.
1303 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1304 is non-nil when called interactively, then the following word
1305 \(rather than preceding\) is checked when the cursor is not over a word.
1306 Optional second argument contains otherchars that can be included in word
1309 Word syntax described by `flyspell-dictionary-alist' (which see)."
1310 (let* ((flyspell-casechars (flyspell-get-casechars))
1311 (flyspell-not-casechars (flyspell-get-not-casechars))
1312 (ispell-otherchars (ispell-get-otherchars))
1313 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1314 (word-regexp (concat flyspell-casechars
1316 (if (not (string= "" ispell-otherchars))
1317 (concat ispell-otherchars "?"))
1318 (if extra-otherchars
1319 (concat extra-otherchars "?"))
1322 (if (or ispell-many-otherchars-p
1328 (if (not (looking-at flyspell-casechars))
1330 (re-search-forward flyspell-casechars (point-max) t)
1331 (re-search-backward flyspell-casechars (point-min) t)))
1332 ;; move to front of word
1333 (re-search-backward flyspell-not-casechars (point-min) 'start)
1334 (while (and (or (and (not (string= "" ispell-otherchars))
1335 (looking-at ispell-otherchars))
1336 (and extra-otherchars (looking-at extra-otherchars)))
1338 (or (not did-it-once)
1339 ispell-many-otherchars-p)
1340 (not (eq prevpt (point))))
1341 (if (and extra-otherchars (looking-at extra-otherchars))
1344 (if (looking-at flyspell-casechars)
1345 (re-search-backward flyspell-not-casechars (point-min) 'move)))
1349 (if (looking-at flyspell-casechars)
1350 (re-search-backward flyspell-not-casechars (point-min) 'move)
1351 (backward-char -1))))
1352 ;; Now mark the word and save to string.
1353 (if (not (re-search-forward word-regexp (point-max) t))
1356 (setq start (match-beginning 0)
1358 word (buffer-substring-no-properties start end))
1359 (list word start end)))))
1361 (defun flyspell-get-word.old (following)
1362 "Return the word for spell-checking according to Ispell syntax.
1363 If argument FOLLOWING is non-nil or if `ispell-following-word'
1364 is non-nil when called interactively, then the following word
1365 \(rather than preceding\) is checked when the cursor is not over a word.
1366 Optional second argument contains other chars that can be included in word
1369 Word syntax described by `ispell-dictionary-alist' (which see)."
1370 (let* ((flyspell-casechars (flyspell-get-casechars))
1371 (flyspell-not-casechars (flyspell-get-not-casechars))
1372 (ispell-otherchars (ispell-get-otherchars))
1373 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1374 (word-regexp (if (string< "" ispell-otherchars)
1375 (concat flyspell-casechars
1378 (if (> (length ispell-otherchars) 0) "?")
1381 (if ispell-many-otherchars-p
1383 (concat flyspell-casechars "+")))
1387 (if (not (looking-at flyspell-casechars))
1389 (re-search-forward flyspell-casechars (point-max) t)
1390 (re-search-backward flyspell-casechars (point-min) t)))
1391 ;; move to front of word
1392 (re-search-backward flyspell-not-casechars (point-min) 'start)
1394 (if (string< "" ispell-otherchars)
1395 (while (and (looking-at ispell-otherchars)
1397 (or (not did-it-once)
1398 ispell-many-otherchars-p)
1399 (not (eq pos (point))))
1401 (setq did-it-once t)
1403 (if (looking-at flyspell-casechars)
1404 (re-search-backward flyspell-not-casechars (point-min) 'move)
1405 (backward-char -1)))))
1406 ;; Now mark the word and save to string.
1407 (if (eq (re-search-forward word-regexp (point-max) t) nil)
1410 (setq start (match-beginning 0)
1412 word (buffer-substring-no-properties start end))
1413 (list word start end)))))
1415 ;*---------------------------------------------------------------------*/
1416 ;* flyspell-small-region ... */
1417 ;*---------------------------------------------------------------------*/
1418 (defun flyspell-small-region (beg end)
1419 "Flyspell text between BEG and END."
1427 (while (< (point) end)
1428 (if (and flyspell-issue-message-flag (= count 100))
1430 (message "Spell Checking...%d%%"
1431 (* 100 (/ (float (- (point) beg)) (- end beg))))
1433 (setq count (+ 1 count)))
1436 (let ((cur (point)))
1438 (if (and (< (point) end) (> (point) (+ cur 1)))
1439 (backward-char 1)))))
1441 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1444 ;*---------------------------------------------------------------------*/
1445 ;* flyspell-external-ispell-process ... */
1446 ;*---------------------------------------------------------------------*/
1447 (defvar flyspell-external-ispell-process '()
1448 "The external Flyspell Ispell process.")
1450 ;*---------------------------------------------------------------------*/
1451 ;* flyspell-external-ispell-buffer ... */
1452 ;*---------------------------------------------------------------------*/
1453 (defvar flyspell-external-ispell-buffer '())
1454 (defvar flyspell-large-region-buffer '())
1455 (defvar flyspell-large-region-beg (point-min))
1456 (defvar flyspell-large-region-end (point-max))
1458 ;*---------------------------------------------------------------------*/
1459 ;* flyspell-external-point-words ... */
1460 ;*---------------------------------------------------------------------*/
1461 (defun flyspell-external-point-words ()
1462 (let ((buffer flyspell-external-ispell-buffer))
1464 (beginning-of-buffer)
1465 (let ((size (- flyspell-large-region-end flyspell-large-region-beg))
1466 (start flyspell-large-region-beg)
1469 ;; now we are done with ispell, we have to find the word in
1470 ;; the initial buffer
1471 (while (< (point) (- (point-max) 1))
1472 ;; we have to fetch the incorrect word
1473 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1474 (let ((word (match-string 1)))
1475 (if (string= word pword)
1476 (setq pcount (1+ pcount))
1480 (goto-char (match-end 0))
1481 (if flyspell-issue-message-flag
1482 (message "Spell Checking...%d%% [%s]"
1483 (* 100 (/ (float (point)) (point-max)))
1485 (set-buffer flyspell-large-region-buffer)
1486 (goto-char flyspell-large-region-beg)
1489 (while (and (or (< n pcount) keep)
1490 (search-forward word flyspell-large-region-end t))
1492 (goto-char (- (point) 1))
1494 (setq keep (flyspell-word))))
1496 (setq flyspell-large-region-beg (point))))
1497 (set-buffer buffer))
1498 (goto-char (point-max)))))
1500 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1501 ;; ok, we are done with pointing out incorrect words, we just
1502 ;; have to kill the temporary buffer
1503 (kill-buffer flyspell-external-ispell-buffer)
1504 (setq flyspell-external-ispell-buffer nil)))
1506 ;*---------------------------------------------------------------------*/
1507 ;* flyspell-large-region ... */
1508 ;*---------------------------------------------------------------------*/
1509 (defun flyspell-large-region (beg end)
1510 (let* ((curbuf (current-buffer))
1511 (buffer (get-buffer-create "*flyspell-region*")))
1512 (setq flyspell-external-ispell-buffer buffer)
1513 (setq flyspell-large-region-buffer curbuf)
1514 (setq flyspell-large-region-beg beg)
1515 (setq flyspell-large-region-end end)
1518 ;; this is done, we can start checking...
1519 (if flyspell-issue-message-flag (message "Checking region..."))
1521 (let ((c (apply 'call-process-region beg
1529 ;; Local dictionary becomes the global dictionary in use.
1530 (if ispell-local-dictionary
1531 (setq ispell-dictionary ispell-local-dictionary))
1532 (setq args (ispell-get-ispell-args))
1533 (if ispell-dictionary ; use specified dictionary
1535 (append (list "-d" ispell-dictionary) args)))
1536 (if ispell-personal-dictionary ; use specified pers dict
1541 ispell-personal-dictionary)))))
1542 (setq args (append args ispell-extra-args))
1545 (flyspell-external-point-words)
1546 (error "Can't check region...")))))
1548 ;*---------------------------------------------------------------------*/
1549 ;* flyspell-region ... */
1550 ;* ------------------------------------------------------------- */
1551 ;* Because `ispell -a' is too slow, it is not possible to use */
1552 ;* it on large region. Then, when ispell is invoked on a large */
1553 ;* text region, a new `ispell -l' process is spawned. The */
1554 ;* pointed out words are then searched in the region a checked with */
1555 ;* regular flyspell means. */
1556 ;*---------------------------------------------------------------------*/
1558 (defun flyspell-region (beg end)
1559 "Flyspell text between BEG and END."
1568 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1569 (flyspell-large-region beg end)
1570 (flyspell-small-region beg end)))))
1572 ;*---------------------------------------------------------------------*/
1573 ;* flyspell-buffer ... */
1574 ;*---------------------------------------------------------------------*/
1576 (defun flyspell-buffer ()
1577 "Flyspell whole buffer."
1579 (flyspell-region (point-min) (point-max)))
1581 ;*---------------------------------------------------------------------*/
1582 ;* old next error position ... */
1583 ;*---------------------------------------------------------------------*/
1584 (defvar flyspell-old-buffer-error nil)
1585 (defvar flyspell-old-pos-error nil)
1587 ;*---------------------------------------------------------------------*/
1588 ;* flyspell-goto-next-error ... */
1589 ;*---------------------------------------------------------------------*/
1590 (defun flyspell-goto-next-error ()
1591 "Go to the next previously detected error.
1592 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1597 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1598 (eq pos flyspell-old-pos-error))
1600 (if (= flyspell-old-pos-error max)
1601 ;; goto beginning of buffer
1603 (message "Restarting from beginning of buffer")
1604 (goto-char (point-min)))
1606 (setq pos (point))))
1607 ;; seek the next error
1608 (while (and (< pos max)
1609 (let ((ovs (overlays-at pos))
1611 (while (and (not r) (consp ovs))
1612 (if (flyspell-overlay-p (car ovs))
1614 (setq ovs (cdr ovs))))
1616 (setq pos (1+ pos)))
1617 ;; save the current location for next invocation
1618 (setq flyspell-old-pos-error pos)
1619 (setq flyspell-old-buffer-error (current-buffer))
1622 (message "No more miss-spelled word!"))))
1624 ;*---------------------------------------------------------------------*/
1625 ;* flyspell-overlay-p ... */
1626 ;*---------------------------------------------------------------------*/
1627 (defun flyspell-overlay-p (o)
1628 "A predicate that return true iff O is an overlay used by flyspell."
1629 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1631 ;*---------------------------------------------------------------------*/
1632 ;* flyspell-delete-all-overlays ... */
1633 ;* ------------------------------------------------------------- */
1634 ;* Remove all the overlays introduced by flyspell. */
1635 ;*---------------------------------------------------------------------*/
1636 (defun flyspell-delete-all-overlays ()
1637 "Delete all the overlays used by flyspell."
1638 (let ((l (overlays-in (point-min) (point-max))))
1641 (if (flyspell-overlay-p (car l))
1642 (delete-overlay (car l)))
1643 (setq l (cdr l))))))
1645 ;*---------------------------------------------------------------------*/
1646 ;* flyspell-unhighlight-at ... */
1647 ;*---------------------------------------------------------------------*/
1648 (defun flyspell-unhighlight-at (pos)
1649 "Remove the flyspell overlay that are located at POS."
1650 (if flyspell-persistent-highlight
1651 (let ((overlays (overlays-at pos)))
1652 (while (consp overlays)
1653 (if (flyspell-overlay-p (car overlays))
1654 (delete-overlay (car overlays)))
1655 (setq overlays (cdr overlays))))
1656 (if (flyspell-overlay-p flyspell-overlay)
1657 (delete-overlay flyspell-overlay))))
1659 ;*---------------------------------------------------------------------*/
1660 ;* flyspell-properties-at-p ... */
1661 ;* ------------------------------------------------------------- */
1662 ;* Is there an highlight properties at position pos? */
1663 ;*---------------------------------------------------------------------*/
1664 (defun flyspell-properties-at-p (pos)
1665 "Return t if there is a text property at POS, not counting `local-map'.
1666 If variable `flyspell-highlight-properties' is set to nil,
1667 text with properties are not checked. This function is used to discover
1668 if the character at POS has any other property."
1669 (let ((prop (text-properties-at pos))
1671 (while (and keep (consp prop))
1672 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1673 (setq prop (cdr (cdr prop)))
1677 ;*---------------------------------------------------------------------*/
1678 ;* make-flyspell-overlay ... */
1679 ;*---------------------------------------------------------------------*/
1680 (defun make-flyspell-overlay (beg end face mouse-face)
1681 "Allocate an overlay to highlight an incorrect word.
1682 BEG and END specify the range in the buffer of that word.
1683 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1685 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1686 (overlay-put flyspell-overlay 'face face)
1687 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1688 (overlay-put flyspell-overlay 'flyspell-overlay t)
1689 (overlay-put flyspell-overlay 'evaporate t)
1690 (overlay-put flyspell-overlay 'help-echo "mouse-2: correct word at point")
1691 (if flyspell-use-local-map
1692 (overlay-put flyspell-overlay
1693 flyspell-overlay-keymap-property-name
1694 flyspell-mouse-map))
1695 (when (eq face 'flyspell-incorrect-face)
1696 (and (stringp flyspell-before-incorrect-word-string)
1697 (overlay-put flyspell-overlay 'before-string
1698 flyspell-before-incorrect-word-string))
1699 (and (stringp flyspell-after-incorrect-word-string)
1700 (overlay-put flyspell-overlay 'after-string
1701 flyspell-after-incorrect-word-string)))
1704 ;*---------------------------------------------------------------------*/
1705 ;* flyspell-highlight-incorrect-region ... */
1706 ;*---------------------------------------------------------------------*/
1707 (defun flyspell-highlight-incorrect-region (beg end poss)
1708 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1709 (unless (run-hook-with-args-until-success
1710 'flyspell-incorrect-hook beg end poss)
1711 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1713 ;; we cleanup all the overlay that are in the region, not
1714 ;; beginning at the word start position
1715 (if (< (1+ beg) end)
1716 (let ((os (overlays-in (1+ beg) end)))
1718 (if (flyspell-overlay-p (car os))
1719 (delete-overlay (car os)))
1720 (setq os (cdr os)))))
1721 ;; we cleanup current overlay at the same position
1722 (if (and (not flyspell-persistent-highlight)
1723 (overlayp flyspell-overlay))
1724 (delete-overlay flyspell-overlay)
1725 (let ((os (overlays-at beg)))
1727 (if (flyspell-overlay-p (car os))
1728 (delete-overlay (car os)))
1729 (setq os (cdr os)))))
1730 ;; now we can use a new overlay
1731 (setq flyspell-overlay
1732 (make-flyspell-overlay beg end
1733 'flyspell-incorrect-face
1736 ;*---------------------------------------------------------------------*/
1737 ;* flyspell-highlight-duplicate-region ... */
1738 ;*---------------------------------------------------------------------*/
1739 (defun flyspell-highlight-duplicate-region (beg end)
1740 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
1741 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1743 ;; we cleanup current overlay at the same position
1744 (if (and (not flyspell-persistent-highlight)
1745 (overlayp flyspell-overlay))
1746 (delete-overlay flyspell-overlay)
1747 (let ((overlays (overlays-at beg)))
1748 (while (consp overlays)
1749 (if (flyspell-overlay-p (car overlays))
1750 (delete-overlay (car overlays)))
1751 (setq overlays (cdr overlays)))))
1752 ;; now we can use a new overlay
1753 (setq flyspell-overlay
1754 (make-flyspell-overlay beg end
1755 'flyspell-duplicate-face
1758 ;*---------------------------------------------------------------------*/
1759 ;* flyspell-auto-correct-cache ... */
1760 ;*---------------------------------------------------------------------*/
1761 (defvar flyspell-auto-correct-pos nil)
1762 (defvar flyspell-auto-correct-region nil)
1763 (defvar flyspell-auto-correct-ring nil)
1764 (defvar flyspell-auto-correct-word nil)
1765 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1766 (make-variable-buffer-local 'flyspell-auto-correct-region)
1767 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1768 (make-variable-buffer-local 'flyspell-auto-correct-word)
1770 ;*---------------------------------------------------------------------*/
1771 ;* flyspell-check-previous-highlighted-word ... */
1772 ;*---------------------------------------------------------------------*/
1773 (defun flyspell-check-previous-highlighted-word (&optional arg)
1774 "Correct the closer misspelled word.
1775 This function scans a mis-spelled word before the cursor. If it finds one
1776 it proposes replacement for that word. With prefix arg, count that many
1777 misspelled words backwards."
1779 (let ((pos1 (point))
1781 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1784 (while (and (setq pos (previous-overlay-change pos))
1787 (if (> pos (point-min))
1789 (setq ovs (overlays-at (1- pos)))
1792 (setq ovs (cdr ovs))
1793 (if (and (overlay-get ov 'flyspell-overlay)
1794 (= 0 (setq arg (1- arg))))
1795 (throw 'exit t)))))))
1799 (error "No word to correct before point"))))
1801 ;*---------------------------------------------------------------------*/
1802 ;* flyspell-display-next-corrections ... */
1803 ;*---------------------------------------------------------------------*/
1804 (defun flyspell-display-next-corrections (corrections)
1805 (let ((string "Corrections:")
1808 (while (< (length string) 80)
1809 (if (equal (car l) flyspell-auto-correct-word)
1810 (setq pos (cons (+ 1 (length string)) pos)))
1811 (setq string (concat string " " (car l)))
1814 (let ((num (car pos)))
1815 (put-text-property num
1816 (+ num (length flyspell-auto-correct-word))
1818 'flyspell-incorrect-face
1820 (setq pos (cdr pos)))
1821 (if (fboundp 'display-message)
1822 (display-message 'no-log string)
1825 ;*---------------------------------------------------------------------*/
1826 ;* flyspell-abbrev-table ... */
1827 ;*---------------------------------------------------------------------*/
1828 (defun flyspell-abbrev-table ()
1829 (if flyspell-use-global-abbrev-table-p
1831 local-abbrev-table))
1833 ;*---------------------------------------------------------------------*/
1834 ;* flyspell-define-abbrev ... */
1835 ;*---------------------------------------------------------------------*/
1836 (defun flyspell-define-abbrev (name expansion)
1837 (let ((table (flyspell-abbrev-table)))
1839 (define-abbrev table name expansion))))
1841 ;*---------------------------------------------------------------------*/
1842 ;* flyspell-auto-correct-word ... */
1843 ;*---------------------------------------------------------------------*/
1844 (defun flyspell-auto-correct-word ()
1845 "Correct the current word.
1846 This command proposes various successive corrections for the current word."
1849 (old-max (point-max)))
1850 ;; use the correct dictionary
1851 (flyspell-accept-buffer-local-defs)
1852 (if (and (eq flyspell-auto-correct-pos pos)
1853 (consp flyspell-auto-correct-region))
1854 ;; we have already been using the function at the same location
1855 (let* ((start (car flyspell-auto-correct-region))
1856 (len (cdr flyspell-auto-correct-region)))
1857 (flyspell-unhighlight-at start)
1858 (delete-region start (+ start len))
1859 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1860 (let* ((word (car flyspell-auto-correct-ring))
1861 (len (length word)))
1862 (rplacd flyspell-auto-correct-region len)
1864 (if flyspell-abbrev-p
1865 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1866 flyspell-auto-correct-word)
1867 (flyspell-change-abbrev (flyspell-abbrev-table)
1868 flyspell-auto-correct-word
1870 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1871 (funcall flyspell-insert-function word)
1873 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1874 (flyspell-ajust-cursor-point pos (point) old-max)
1875 (setq flyspell-auto-correct-pos (point)))
1876 ;; fetch the word to be checked
1877 (let ((word (flyspell-get-word nil)))
1879 (let ((start (car (cdr word)))
1880 (end (car (cdr (cdr word))))
1883 (setq flyspell-auto-correct-word word)
1884 ;; now check spelling of word.
1885 (process-send-string ispell-process "%\n") ;put in verbose mode
1886 (process-send-string ispell-process (concat "^" word "\n"))
1887 ;; wait until ispell has processed word
1889 (accept-process-output ispell-process)
1890 (not (string= "" (car ispell-filter)))))
1891 (setq ispell-filter (cdr ispell-filter))
1892 (if (consp ispell-filter)
1893 (setq poss (ispell-parse-output (car ispell-filter))))
1895 ((or (eq poss t) (stringp poss))
1896 ;; don't correct word
1900 (error "Ispell: error in Ispell process"))
1902 ;; the word is incorrect, we have to propose a replacement
1903 (let ((replacements (if flyspell-sort-corrections
1904 (sort (car (cdr (cdr poss))) 'string<)
1905 (car (cdr (cdr poss))))))
1906 (setq flyspell-auto-correct-region nil)
1907 (if (consp replacements)
1909 (let ((replace (car replacements)))
1910 (let ((new-word replace))
1911 (if (not (equal new-word (car poss)))
1913 ;; the save the current replacements
1914 (setq flyspell-auto-correct-region
1915 (cons start (length new-word)))
1916 (let ((l replacements))
1917 (while (consp (cdr l))
1919 (rplacd l (cons (car poss) replacements)))
1920 (setq flyspell-auto-correct-ring
1922 (flyspell-unhighlight-at start)
1923 (delete-region start end)
1924 (funcall flyspell-insert-function new-word)
1925 (if flyspell-abbrev-p
1926 (if (flyspell-already-abbrevp
1927 (flyspell-abbrev-table) word)
1928 (flyspell-change-abbrev
1929 (flyspell-abbrev-table)
1932 (flyspell-define-abbrev word
1935 (flyspell-display-next-corrections
1936 (cons new-word flyspell-auto-correct-ring))
1937 (flyspell-ajust-cursor-point pos
1940 (setq flyspell-auto-correct-pos (point))
1941 (ispell-pdict-save t)))))))
1943 ;*---------------------------------------------------------------------*/
1944 ;* flyspell-auto-correct-previous-pos ... */
1945 ;*---------------------------------------------------------------------*/
1946 (defvar flyspell-auto-correct-previous-pos nil
1947 "Holds the start of the first incorrect word before point.")
1949 ;*---------------------------------------------------------------------*/
1950 ;* flyspell-auto-correct-previous-hook ... */
1951 ;*---------------------------------------------------------------------*/
1952 (defun flyspell-auto-correct-previous-hook ()
1953 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1954 Sets flyspell-auto-correct-previous-pos to nil"
1956 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1957 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1958 (setq flyspell-auto-correct-previous-pos nil)))
1960 ;*---------------------------------------------------------------------*/
1961 ;* flyspell-auto-correct-previous-word ... */
1962 ;*---------------------------------------------------------------------*/
1963 (defun flyspell-auto-correct-previous-word (position)
1964 "*Auto correct the first mispelled word that occurs before point."
1967 (add-hook 'pre-command-hook
1968 (function flyspell-auto-correct-previous-hook) t t)
1971 (unless flyspell-auto-correct-previous-pos
1972 ;; only reset if a new overlay exists
1973 (setq flyspell-auto-correct-previous-pos nil)
1975 (let ((overlay-list (overlays-in (point-min) position))
1976 (new-overlay 'dummy-value))
1978 ;; search for previous (new) flyspell overlay
1979 (while (and new-overlay
1980 (or (not (flyspell-overlay-p new-overlay))
1981 ;; check if its face has changed
1982 (not (eq (get-char-property
1983 (overlay-start new-overlay) 'face)
1984 'flyspell-incorrect-face))))
1985 (setq new-overlay (car-safe overlay-list))
1986 (setq overlay-list (cdr-safe overlay-list)))
1988 ;; if nothing new exits new-overlay should be nil
1989 (if new-overlay;; the length of the word may change so go to the start
1990 (setq flyspell-auto-correct-previous-pos
1991 (overlay-start new-overlay)))))
1993 (when flyspell-auto-correct-previous-pos
1995 (goto-char flyspell-auto-correct-previous-pos)
1996 (let ((ispell-following-word t));; point is at start
1997 (if (numberp flyspell-auto-correct-previous-pos)
1998 (goto-char flyspell-auto-correct-previous-pos))
1999 (flyspell-auto-correct-word))
2000 ;; the point may have moved so reset this
2001 (setq flyspell-auto-correct-previous-pos (point))))))
2003 ;*---------------------------------------------------------------------*/
2004 ;* flyspell-correct-word ... */
2005 ;*---------------------------------------------------------------------*/
2006 (defun flyspell-correct-word (event)
2007 "Pop up a menu of possible corrections for a misspelled word.
2008 The word checked is the word at the mouse position."
2010 ;; use the correct dictionary
2011 (flyspell-accept-buffer-local-defs)
2012 ;; retain cursor location (I don't know why but save-excursion here fails).
2013 (let ((save (point)))
2014 (mouse-set-point event)
2015 (let ((cursor-location (point))
2016 (word (flyspell-get-word nil)))
2018 (let ((start (car (cdr word)))
2019 (end (car (cdr (cdr word))))
2022 ;; now check spelling of word.
2023 (process-send-string ispell-process "%\n") ;put in verbose mode
2024 (process-send-string ispell-process (concat "^" word "\n"))
2025 ;; wait until ispell has processed word
2027 (accept-process-output ispell-process)
2028 (not (string= "" (car ispell-filter)))))
2029 (setq ispell-filter (cdr ispell-filter))
2030 (if (consp ispell-filter)
2031 (setq poss (ispell-parse-output (car ispell-filter))))
2033 ((or (eq poss t) (stringp poss))
2034 ;; don't correct word
2038 (error "Ispell: error in Ispell process"))
2039 ((string-match "GNU" (emacs-version))
2040 ;; the word is incorrect, we have to propose a replacement
2041 (setq replace (flyspell-emacs-popup event poss word))
2042 (cond ((eq replace 'ignore)
2047 (process-send-string ispell-process
2048 (concat "*" word "\n"))
2049 (flyspell-unhighlight-at cursor-location)
2050 (setq ispell-pdict-modified-p '(t)))
2051 ((or (eq replace 'buffer) (eq replace 'session))
2052 (process-send-string ispell-process
2053 (concat "@" word "\n"))
2054 (if (null ispell-pdict-modified-p)
2055 (setq ispell-pdict-modified-p
2056 (list ispell-pdict-modified-p)))
2057 (flyspell-unhighlight-at cursor-location)
2059 (if (eq replace 'buffer)
2060 (ispell-add-per-file-word-list word)))
2062 (flyspell-unhighlight-at cursor-location)
2063 (let ((new-word (if (atom replace)
2067 (+ (- (length word) (- end start))
2069 (if (not (equal new-word (car poss)))
2070 (let ((old-max (point-max)))
2071 (delete-region start end)
2072 (funcall flyspell-insert-function new-word)
2073 (if flyspell-abbrev-p
2074 (flyspell-define-abbrev word new-word))
2075 (flyspell-ajust-cursor-point save
2081 ((eq flyspell-emacs 'xemacs)
2082 (flyspell-xemacs-popup
2083 event poss word cursor-location start end save)
2085 (ispell-pdict-save t))))))
2087 ;*---------------------------------------------------------------------*/
2088 ;* flyspell-xemacs-correct ... */
2089 ;*---------------------------------------------------------------------*/
2090 (defun flyspell-xemacs-correct (replace poss word cursor-location start end save)
2091 "The xemacs popup menu callback."
2092 (cond ((eq replace 'ignore)
2095 (process-send-string ispell-process (concat "*" word "\n"))
2096 (process-send-string ispell-process "#\n")
2097 (flyspell-unhighlight-at cursor-location)
2098 (setq ispell-pdict-modified-p '(t)))
2099 ((or (eq replace 'buffer) (eq replace 'session))
2100 (process-send-string ispell-process (concat "@" word "\n"))
2101 (flyspell-unhighlight-at cursor-location)
2102 (if (null ispell-pdict-modified-p)
2103 (setq ispell-pdict-modified-p
2104 (list ispell-pdict-modified-p)))
2105 (if (eq replace 'buffer)
2106 (ispell-add-per-file-word-list word)))
2108 (let ((old-max (point-max))
2109 (new-word (if (atom replace)
2112 (cursor-location (+ (- (length word) (- end start))
2114 (if (not (equal new-word (car poss)))
2116 (delete-region start end)
2118 (funcall flyspell-insert-function new-word)
2119 (if flyspell-abbrev-p
2120 (flyspell-define-abbrev word new-word))))
2121 (flyspell-ajust-cursor-point save cursor-location old-max)))))
2123 ;*---------------------------------------------------------------------*/
2124 ;* flyspell-ajust-cursor-point ... */
2125 ;*---------------------------------------------------------------------*/
2126 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2127 (if (>= save cursor-location)
2128 (let ((new-pos (+ save (- (point-max) old-max))))
2130 ((< new-pos (point-min))
2132 ((> new-pos (point-max))
2137 ;*---------------------------------------------------------------------*/
2138 ;* flyspell-emacs-popup ... */
2139 ;*---------------------------------------------------------------------*/
2140 (defun flyspell-emacs-popup (event poss word)
2141 "The Emacs popup menu."
2143 (let* ((mouse-pos (mouse-position))
2144 (mouse-pos (if (nth 1 mouse-pos)
2146 (set-mouse-position (car mouse-pos)
2147 (/ (frame-width) 2) 2)
2150 (setq event (list (list (car (cdr mouse-pos))
2151 (1+ (cdr (cdr mouse-pos))))
2153 (let* ((corrects (if flyspell-sort-corrections
2154 (sort (car (cdr (cdr poss))) 'string<)
2155 (car (cdr (cdr poss)))))
2156 (cor-menu (if (consp corrects)
2157 (mapcar (lambda (correct)
2158 (list correct correct))
2161 (affix (car (cdr (cdr (cdr poss)))))
2162 (base-menu (let ((save (if (consp affix)
2164 (list (concat "Save affix: " (car affix))
2166 '("Accept (session)" session)
2167 '("Accept (buffer)" buffer))
2168 '(("Save word" save)
2169 ("Accept (session)" session)
2170 ("Accept (buffer)" buffer)))))
2171 (if (consp cor-menu)
2172 (append cor-menu (cons "" save))
2174 (menu (cons "flyspell correction menu" base-menu)))
2175 (car (x-popup-menu event
2176 (list (format "%s [%s]" word (or ispell-local-dictionary
2180 ;*---------------------------------------------------------------------*/
2181 ;* flyspell-xemacs-popup ... */
2182 ;*---------------------------------------------------------------------*/
2183 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
2184 "The XEmacs popup menu."
2185 (let* ((corrects (if flyspell-sort-corrections
2186 (sort (car (cdr (cdr poss))) 'string<)
2187 (car (cdr (cdr poss)))))
2188 (cor-menu (if (consp corrects)
2189 (mapcar (lambda (correct)
2191 (list 'flyspell-xemacs-correct
2202 (affix (car (cdr (cdr (cdr poss)))))
2203 (menu (let ((save (if (consp affix)
2205 (concat "Save affix: " (car affix))
2206 (list 'flyspell-xemacs-correct
2217 (list 'flyspell-xemacs-correct
2226 (session (vector "Accept (session)"
2227 (list 'flyspell-xemacs-correct
2236 (buffer (vector "Accept (buffer)"
2237 (list 'flyspell-xemacs-correct
2246 (if (consp cor-menu)
2247 (append cor-menu (list "-" save session buffer))
2248 (list save session buffer)))))
2249 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2253 ;*---------------------------------------------------------------------*/
2254 ;* Some example functions for real autocorrecting */
2255 ;*---------------------------------------------------------------------*/
2256 (defun flyspell-maybe-correct-transposition (beg end poss)
2257 "Check replacements for transposed characters.
2259 If the text between BEG and END is equal to a correction suggested by
2260 Ispell, after transposing two adjacent characters, correct the text,
2263 The third arg POSS is either the symbol 'doublon' or a list of
2264 possible corrections as returned by 'ispell-parse-output'.
2266 This function is meant to be added to 'flyspell-incorrect-hook'."
2270 (goto-char (1+ beg))
2271 (while (< (point) end)
2273 (when (member (buffer-substring beg end) (car (cdr (cdr poss))))
2275 (transpose-chars -1)
2279 (defun flyspell-maybe-correct-doubling (beg end poss)
2280 "Check replacements for doubled characters.
2282 If the text between BEG and END is equal to a correction suggested by
2283 Ispell, after removing a pair of doubled characters, correct the text,
2286 The third arg POSS is either the symbol 'doublon' or a list of
2287 possible corrections as returned by 'ispell-parse-output'.
2289 This function is meant to be added to 'flyspell-incorrect-hook'."
2293 (let ((last (char-after beg))
2295 (goto-char (1+ beg))
2296 (while (< (point) end)
2297 (setq this (char-after))
2298 (if (not (char-equal this last))
2301 (when (member (buffer-substring beg (1- end)) (car (cdr (cdr poss))))
2304 (insert-char this 1))
2308 ;*---------------------------------------------------------------------*/
2309 ;* flyspell-already-abbrevp ... */
2310 ;*---------------------------------------------------------------------*/
2311 (defun flyspell-already-abbrevp (table word)
2312 (let ((sym (abbrev-symbol word table)))
2313 (and sym (symbolp sym))))
2315 ;*---------------------------------------------------------------------*/
2316 ;* flyspell-change-abbrev ... */
2317 ;*---------------------------------------------------------------------*/
2318 (defun flyspell-change-abbrev (table old new)
2319 (set (abbrev-symbol old table) new))
2321 ;*---------------------------------------------------------------------*/
2322 ;* flyspell-auto-correct-previous-word advice ... */
2323 ;*---------------------------------------------------------------------*/
2324 (defadvice flyspell-auto-correct-previous-word
2325 (around easymacs-flyspell-auto-correct)
2326 "Correct current word if misspelled, else previous
2327 misspelling. Protect against accidentally changing a word
2328 that cannot be seen, because it is somewhere off the screen."
2331 (move-to-window-line 0)
2333 (move-to-window-line -1)
2336 (narrow-to-region top bot)
2338 (re-search-forward "\\s \\|\\'" nil t)
2339 (overlay-recenter (point))
2342 (ad-activate 'flyspell-auto-correct-previous-word)
2345 ;;; flyspell.el ends here