2 ;;; flyspell.el --- on-the-fly spell checker
4 ;; Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
6 ;; Author: Manuel Serrano <Manuel.Serrano@unice.fr>
7 ;; Keywords: convenience
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
31 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
32 ;; This applies only to the current buffer.
34 ;; To enable Flyspell in text representing computer programs, type
35 ;; M-x flyspell-prog-mode.
36 ;; In that mode only text inside comments is checked.
38 ;; Note: consider setting the variable ispell-parser to `tex' to
39 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
41 ;; Some user variables control the behavior of flyspell. They are
42 ;; those defined under the `User variables' comment.
47 ;*---------------------------------------------------------------------*/
49 ;*---------------------------------------------------------------------*/
50 (defgroup flyspell nil
51 "Spell checking on the fly."
57 ;*---------------------------------------------------------------------*/
58 ;* Which emacs are we currently running */
59 ;*---------------------------------------------------------------------*/
60 (defvar flyspell-emacs
62 ((string-match "XEmacs" emacs-version)
66 "The type of Emacs we are currently running.")
68 (defvar flyspell-use-local-map
69 (or (eq flyspell-emacs 'xemacs)
70 (not (string< emacs-version "20"))))
72 ;*---------------------------------------------------------------------*/
73 ;* User configuration ... */
74 ;*---------------------------------------------------------------------*/
75 (defcustom flyspell-highlight-flag t
76 "*How Flyspell should indicate misspelled words.
77 Non-nil means use highlight, nil means use minibuffer messages."
81 (defcustom flyspell-mark-duplications-flag t
82 "*Non-nil means Flyspell reports a repeated word as an error."
86 (defcustom flyspell-sort-corrections nil
87 "*Non-nil means, sort the corrections alphabetically before popping them."
92 (defcustom flyspell-duplicate-distance -1
93 "*The maximum distance for finding duplicates of unrecognized words.
94 This applies to the feature that when a word is not found in the dictionary,
95 if the same spelling occurs elsewhere in the buffer,
96 Flyspell uses a different face (`flyspell-duplicate-face') to highlight it.
97 This variable specifies how far to search to find such a duplicate.
98 -1 means no limit (search the whole buffer).
99 0 means do not search for duplicate unrecognized spellings."
104 (defcustom flyspell-delay 3
105 "*The number of seconds to wait before checking, after a \"delayed\" command."
109 (defcustom flyspell-persistent-highlight t
110 "*Non-nil means misspelled words remain highlighted until corrected.
111 If this variable is nil, only the most recently detected misspelled word
116 (defcustom flyspell-highlight-properties t
117 "*Non-nil means highlight incorrect words even if a property exists for this word."
121 (defcustom flyspell-default-delayed-commands
122 '(self-insert-command
124 backward-or-forward-delete-char
126 scrollbar-vertical-drag)
127 "The standard list of delayed commands for Flyspell.
128 See `flyspell-delayed-commands'."
131 :type '(repeat (symbol)))
133 (defcustom flyspell-delayed-commands nil
134 "List of commands that are \"delayed\" for Flyspell mode.
135 After these commands, Flyspell checking is delayed for a short time,
136 whose length is specified by `flyspell-delay'."
138 :type '(repeat (symbol)))
140 (defcustom flyspell-default-deplacement-commands
145 "The standard list of deplacement commands for Flyspell.
146 See `flyspell-deplacement-commands'."
149 :type '(repeat (symbol)))
151 (defcustom flyspell-deplacement-commands nil
152 "List of commands that are \"deplacement\" for Flyspell mode.
153 After these commands, Flyspell checking is performed only if the previous
154 command was not the very same command."
157 :type '(repeat (symbol)))
159 (defcustom flyspell-issue-welcome-flag t
160 "*Non-nil means that Flyspell should display a welcome message when started."
164 (defcustom flyspell-issue-message-flag t
165 "*Non-nil means that Flyspell emits messages when checking words."
169 (defcustom flyspell-incorrect-hook nil
170 "*List of functions to be called when incorrect words are encountered.
171 Each function is given three arguments: the beginning and the end
172 of the incorrect region. The third is either the symbol 'doublon' or the list
173 of possible corrections as returned by 'ispell-parse-output'.
175 If any of the functions return non-Nil, the word is not highlighted as
181 (defcustom flyspell-default-dictionary nil
182 "A string that is the name of the default dictionary.
183 This is passed to the `ispell-change-dictionary' when flyspell is started.
184 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
185 when flyspell is started, the value of that variable is used instead
186 of `flyspell-default-dictionary' to select the default dictionary.
187 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
188 Ispell's ultimate default dictionary."
191 :type '(choice string (const :tag "Default" nil)))
193 (defcustom flyspell-tex-command-regexp
194 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
195 "A string that is the regular expression that matches TeX commands."
200 (defcustom flyspell-check-tex-math-command nil
201 "*Non nil means check even inside TeX math environment.
202 TeX math environments are discovered by the TEXMATHP that implemented
203 inside the texmathp.el Emacs package. That package may be found at:
204 http://strw.leidenuniv.nl/~dominik/Tools"
208 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
209 '("francais" "deutsch8" "norsk")
210 "List of dictionary names that consider `-' as word delimiter."
213 :type '(repeat (string)))
215 (defcustom flyspell-abbrev-p
217 "*If non-nil, add correction to abbreviation table."
222 (defcustom flyspell-use-global-abbrev-table-p
224 "*If non-nil, prefer global abbrev table to local abbrev table."
230 (defcustom flyspell-mode-line-string " Fly"
231 "*String displayed on the modeline when flyspell is active.
232 Set this to nil if you don't want a modeline indicator."
234 :type '(choice string (const :tag "None" nil)))
236 (defcustom flyspell-large-region 1000
237 "*The threshold that determines if a region is small.
238 The `flyspell-region' function is invoked if the region is small, the
239 word are checked one after the other using regular flyspell check
240 means. If the region is large, a new Ispell process is spawned to get
246 (defcustom flyspell-insert-function (function insert)
247 "*Function for inserting word by flyspell upon correction."
251 (defcustom flyspell-before-incorrect-word-string nil
252 "String used to indicate an incorrect word starting."
254 :type '(choice string (const nil)))
256 (defcustom flyspell-after-incorrect-word-string nil
257 "String used to indicate an incorrect word ending."
259 :type '(choice string (const nil)))
261 (defcustom flyspell-use-meta-tab t
262 "*Non-nil means that flyspell uses META-TAB to correct word."
266 (defcustom flyspell-auto-correct-binding
268 ((eq flyspell-emacs 'xemacs)
272 "The key binding for flyspell auto correction."
275 ;*---------------------------------------------------------------------*/
276 ;* Mode specific options */
277 ;* ------------------------------------------------------------- */
278 ;* Mode specific options enable users to disable flyspell on */
279 ;* certain word depending of the emacs mode. For instance, when */
280 ;* using flyspell with mail-mode add the following expression */
281 ;* in your .emacs file: */
282 ;* (add-hook 'mail-mode */
283 ;* '(lambda () (setq flyspell-generic-check-word-p */
284 ;* 'mail-mode-flyspell-verify))) */
285 ;*---------------------------------------------------------------------*/
286 (defvar flyspell-generic-check-word-p nil
287 "Function providing per-mode customization over which words are flyspelled.
288 Returns t to continue checking, nil otherwise.
289 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
290 property of the major mode name.")
291 (make-variable-buffer-local 'flyspell-generic-check-word-p)
293 ;*--- mail mode -------------------------------------------------------*/
294 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
295 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
296 (defun mail-mode-flyspell-verify ()
297 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
298 (let ((header-end (save-excursion
299 (goto-char (point-min))
300 (re-search-forward (concat "^" (regexp-quote
301 mail-header-separator)
305 (signature-begin (save-excursion
306 (goto-char (point-max))
307 (re-search-backward message-signature-separator
310 (cond ((< (point) header-end)
311 (and (save-excursion (beginning-of-line)
312 (looking-at "^Subject:"))
313 (> (point) (match-end 0))))
314 ((> (point) signature-begin)
319 (not (looking-at "[>}|]\\|To:")))))))
321 ;*--- texinfo mode ----------------------------------------------------*/
322 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
323 (defun texinfo-mode-flyspell-verify ()
324 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
327 (not (looking-at "@"))))
329 ;*--- tex mode --------------------------------------------------------*/
330 (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
331 (defun tex-mode-flyspell-verify ()
332 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
335 (re-search-backward "^[ \t]*%%%[ \t]+Local" (point-min) t)))
337 (let ((this (point-marker))
338 (e (progn (end-of-line) (point-marker))))
340 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t)
341 (and (>= this (match-beginning 0))
342 (<= this (match-end 0)) )))))))
344 ;*--- sgml mode -------------------------------------------------------*/
345 (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
346 (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
348 (defun sgml-mode-flyspell-verify ()
349 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
351 (let ((this (point-marker))
352 (s (progn (beginning-of-line) (point-marker)))
353 (e (progn (end-of-line) (point-marker))))
356 (and (re-search-forward "[^<]*>" e t)
357 (= (match-beginning 0) this)))
360 (and (re-search-backward "<[^>]*" s t)
361 (= (match-end 0) this)))
364 (and (re-search-forward "[^&]*;" e t)
365 (= (match-beginning 0) this)))
368 (and (re-search-backward "&[^;]*" s t)
369 (= (match-end 0) this)))))))))
371 ;*---------------------------------------------------------------------*/
372 ;* Programming mode */
373 ;*---------------------------------------------------------------------*/
374 (defvar flyspell-prog-text-faces
375 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
376 "Faces corresponding to text in programming-mode buffers.")
378 (defun flyspell-generic-progmode-verify ()
379 "Used for `flyspell-generic-check-word-p' in programming modes."
380 (let ((f (get-text-property (point) 'face)))
381 (memq f flyspell-prog-text-faces)))
384 (defun flyspell-prog-mode ()
385 "Turn on `flyspell-mode' for comments and strings."
387 (setq flyspell-generic-check-word-p 'flyspell-generic-progmode-verify)
390 ;*---------------------------------------------------------------------*/
391 ;* Overlay compatibility */
392 ;*---------------------------------------------------------------------*/
393 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
394 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
395 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
396 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
397 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
398 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
399 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
400 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
402 ;*---------------------------------------------------------------------*/
403 ;* The minor mode declaration. */
404 ;*---------------------------------------------------------------------*/
405 (eval-when-compile (defvar flyspell-local-mouse-map))
408 (defvar flyspell-mode nil)
409 (make-variable-buffer-local 'flyspell-mode)
411 (defvar flyspell-mouse-map
412 (let ((map (make-sparse-keymap)))
414 ((eq flyspell-emacs 'xemacs)
415 (define-key map [(button2)] #'flyspell-correct-word)
416 (if flyspell-use-meta-tab
417 (define-key map "\M-\t" #'flyspell-auto-correct-word))
418 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
419 (define-key map [(control \,)] 'flyspell-goto-next-error)
420 (define-key map [(control \.)] 'flyspell-auto-correct-word))
421 (flyspell-use-local-map
422 (define-key map [(down-mouse-2)] #'flyspell-correct-word)
423 (if flyspell-use-meta-tab
424 (define-key map "\M-\t" #'flyspell-auto-correct-word))
425 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
426 (define-key map [?\C-\,] 'flyspell-goto-next-error)
427 (define-key map [?\C-\.] 'flyspell-auto-correct-word)))
431 (defvar flyspell-mode-map (make-sparse-keymap))
433 ;; mouse, keyboard bindings and misc definition
434 (when (or (assoc 'flyspell-mode minor-mode-map-alist)
435 (setq minor-mode-map-alist
436 (cons (cons 'flyspell-mode flyspell-mode-map)
437 minor-mode-map-alist)))
438 (if flyspell-use-meta-tab
439 (define-key flyspell-mode-map "\M-\t" 'flyspell-auto-correct-word))
441 ((eq flyspell-emacs 'xemacs)
442 (define-key flyspell-mode-map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
443 (define-key flyspell-mode-map [(control \,)] 'flyspell-goto-next-error)
444 (define-key flyspell-mode-map [(control \.)] 'flyspell-auto-correct-word))
445 (flyspell-use-local-map
446 (define-key flyspell-mode-map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
447 (define-key flyspell-mode-map [?\C-\,] 'flyspell-goto-next-error)
448 (define-key flyspell-mode-map [?\C-\.] 'flyspell-auto-correct-word))))
451 ;; the name of the overlay property that defines the keymap
452 (defvar flyspell-overlay-keymap-property-name 'keymap)
454 ;; dash character machinery
455 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
456 "*Non-nil means that the `-' char is considered as a word delimiter.")
457 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
458 (defvar flyspell-dash-dictionary nil)
459 (make-variable-buffer-local 'flyspell-dash-dictionary)
460 (defvar flyspell-dash-local-dictionary nil)
461 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
463 ;*---------------------------------------------------------------------*/
465 ;*---------------------------------------------------------------------*/
466 (defface flyspell-incorrect-face
467 (if (eq flyspell-emacs 'xemacs)
468 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
470 '((((class color)) (:foreground "OrangeRed" :weight bold :underline t))
472 "Face used for marking a misspelled word in Flyspell."
475 (defface flyspell-duplicate-face
476 (if (eq flyspell-emacs 'xemacs)
477 '((((class color)) (:foreground "Gold3" :bold t :underline t))
479 '((((class color)) (:foreground "Gold3" :weight bold :underline t))
481 "Face used for marking a misspelled word that appears twice in the buffer.
482 See also `flyspell-duplicate-distance'."
485 (defvar flyspell-overlay nil)
487 ;*---------------------------------------------------------------------*/
488 ;* flyspell-mode ... */
489 ;*---------------------------------------------------------------------*/
491 (defun flyspell-mode (&optional arg)
492 "Minor mode performing on-the-fly spelling checking.
493 Ispell is automatically spawned on background for each entered words.
494 The default flyspell behavior is to highlight incorrect words.
495 With no argument, this command toggles Flyspell mode.
496 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
499 \\[ispell-word]: correct words (using Ispell).
500 \\[flyspell-auto-correct-word]: automatically correct word.
501 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
502 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
505 This runs `flyspell-mode-hook' after flyspell is entered.
508 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
509 valid. For instance, a personal dictionary can be used by
510 invoking `ispell-change-dictionary'.
512 Consider using the `ispell-parser' to check your text. For instance
514 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
517 \\[flyspell-region] checks all words inside a region.
518 \\[flyspell-buffer] checks the whole buffer."
520 (let ((old-flyspell-mode flyspell-mode))
521 ;; Mark the mode as on or off.
522 (setq flyspell-mode (not (or (and (null arg) flyspell-mode)
523 (<= (prefix-numeric-value arg) 0))))
525 (unless (eq flyspell-mode old-flyspell-mode)
529 ;; Force modeline redisplay.
530 (set-buffer-modified-p (buffer-modified-p)))))
532 ;*---------------------------------------------------------------------*/
534 ;*---------------------------------------------------------------------*/
536 (if (fboundp 'add-minor-mode)
537 (add-minor-mode 'flyspell-mode
538 'flyspell-mode-line-string
542 (or (assoc 'flyspell-mode minor-mode-alist)
543 (setq minor-mode-alist
544 (cons '(flyspell-mode flyspell-mode-line-string)
547 (or (assoc 'flyspell-mode minor-mode-map-alist)
548 (setq minor-mode-map-alist
549 (cons (cons 'flyspell-mode flyspell-mode-map)
550 minor-mode-map-alist))))
552 ;*---------------------------------------------------------------------*/
553 ;* flyspell-buffers ... */
554 ;* ------------------------------------------------------------- */
555 ;* For remembering buffers running flyspell */
556 ;*---------------------------------------------------------------------*/
557 (defvar flyspell-buffers nil)
559 ;*---------------------------------------------------------------------*/
560 ;* flyspell-minibuffer-p ... */
561 ;*---------------------------------------------------------------------*/
562 (defun flyspell-minibuffer-p (buffer)
563 "Is BUFFER a minibuffer?"
564 (let ((ws (get-buffer-window-list buffer t)))
565 (and (consp ws) (window-minibuffer-p (car ws)))))
567 ;*---------------------------------------------------------------------*/
568 ;* flyspell-version ... */
569 ;*---------------------------------------------------------------------*/
571 (defun flyspell-version ()
572 "The flyspell version"
576 ;*---------------------------------------------------------------------*/
577 ;* flyspell-accept-buffer-local-defs ... */
578 ;*---------------------------------------------------------------------*/
579 (defun flyspell-accept-buffer-local-defs ()
580 ;; strange problem. If buffer in current window has font-lock turned on,
581 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
582 ;; call will reset the buffer to the buffer in the current window. However,
583 ;; it only happens at startup (fix by Albert L. Ting).
584 (let ((buf (current-buffer)))
585 (ispell-accept-buffer-local-defs)
587 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
588 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
589 ;; the dictionary has changed
591 (setq flyspell-dash-dictionary ispell-dictionary)
592 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
593 (if (member (or ispell-local-dictionary ispell-dictionary)
594 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
595 (setq flyspell-consider-dash-as-word-delimiter-flag t)
596 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
598 ;*---------------------------------------------------------------------*/
599 ;* flyspell-mode-on ... */
600 ;*---------------------------------------------------------------------*/
601 (defun flyspell-mode-on ()
602 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
603 (setq ispell-highlight-face 'flyspell-incorrect-face)
604 ;; local dictionaries setup
605 (ispell-change-dictionary
606 (or ispell-local-dictionary ispell-dictionary flyspell-default-dictionary))
607 ;; we have to force ispell to accept the local definition or
608 ;; otherwise it could be too late, the local dictionary may
610 (flyspell-accept-buffer-local-defs)
611 ;; we put the `flyspell-delayed' property on some commands
612 (flyspell-delay-commands)
613 ;; we put the `flyspell-deplacement' property on some commands
614 (flyspell-deplacement-commands)
615 ;; we bound flyspell action to post-command hook
616 (if (eq flyspell-emacs 'xemacs)
617 (make-local-hook 'post-command-hook))
618 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
619 ;; we bound flyspell action to pre-command hook
620 (if (eq flyspell-emacs 'xemacs)
621 (make-local-hook 'pre-command-hook))
622 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
623 ;; we bound flyspell action to after-change hook
624 (make-local-variable 'after-change-functions)
625 (setq after-change-functions
626 (cons 'flyspell-after-change-function after-change-functions))
627 ;; set flyspell-generic-check-word-p based on the major mode
628 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
630 (setq flyspell-generic-check-word-p mode-predicate)))
631 ;; work around the fact that the `local-map' text-property replaces the
632 ;; buffer's local map rather than shadowing it.
633 (set (make-local-variable 'flyspell-mouse-map)
634 (let ((map (copy-keymap flyspell-mouse-map)))
635 (set-keymap-parent map (current-local-map))
637 (set (make-local-variable 'flyspell-mode-map)
638 (let ((map (copy-keymap flyspell-mode-map)))
639 (set-keymap-parent map (current-local-map))
641 ;; the welcome message
642 (if (and flyspell-issue-message-flag
643 flyspell-issue-welcome-flag
645 (let ((binding (where-is-internal 'flyspell-auto-correct-word
649 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
650 (key-description binding))
651 "Welcome to flyspell. Use Mouse-2 to correct words."))))
652 ;; we end with the flyspell hooks
653 (run-hooks 'flyspell-mode-hook))
655 ;*---------------------------------------------------------------------*/
656 ;* flyspell-delay-commands ... */
657 ;*---------------------------------------------------------------------*/
658 (defun flyspell-delay-commands ()
659 "Install the standard set of Flyspell delayed commands."
660 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
661 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
663 ;*---------------------------------------------------------------------*/
664 ;* flyspell-delay-command ... */
665 ;*---------------------------------------------------------------------*/
666 (defun flyspell-delay-command (command)
667 "Set COMMAND to be delayed, for Flyspell.
668 When flyspell `post-command-hook' is invoked because a delayed command
669 as been used the current word is not immediately checked.
670 It will be checked only after `flyspell-delay' seconds."
671 (interactive "SDelay Flyspell after Command: ")
672 (put command 'flyspell-delayed t))
674 ;*---------------------------------------------------------------------*/
675 ;* flyspell-deplacement-commands ... */
676 ;*---------------------------------------------------------------------*/
677 (defun flyspell-deplacement-commands ()
678 "Install the standard set of Flyspell deplacement commands."
679 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
680 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
682 ;*---------------------------------------------------------------------*/
683 ;* flyspell-deplacement-command ... */
684 ;*---------------------------------------------------------------------*/
685 (defun flyspell-deplacement-command (command)
686 "Set COMMAND that implement cursor movements, for Flyspell.
687 When flyspell `post-command-hook' is invoked because of a deplacement command
688 as been used the current word is checked only if the previous command was
689 not the very same deplacement command."
690 (interactive "SDeplacement Flyspell after Command: ")
691 (put command 'flyspell-deplacement t))
693 ;*---------------------------------------------------------------------*/
694 ;* flyspell-word-cache ... */
695 ;*---------------------------------------------------------------------*/
696 (defvar flyspell-word-cache-start nil)
697 (defvar flyspell-word-cache-end nil)
698 (defvar flyspell-word-cache-word nil)
699 (make-variable-buffer-local 'flyspell-word-cache-start)
700 (make-variable-buffer-local 'flyspell-word-cache-end)
701 (make-variable-buffer-local 'flyspell-word-cache-word)
703 ;*---------------------------------------------------------------------*/
704 ;* The flyspell pre-hook, store the current position. In the */
705 ;* post command hook, we will check, if the word at this position */
706 ;* has to be spell checked. */
707 ;*---------------------------------------------------------------------*/
708 (defvar flyspell-pre-buffer nil)
709 (defvar flyspell-pre-point nil)
710 (defvar flyspell-pre-column nil)
711 (defvar flyspell-pre-pre-buffer nil)
712 (defvar flyspell-pre-pre-point nil)
714 ;*---------------------------------------------------------------------*/
715 ;* flyspell-previous-command ... */
716 ;*---------------------------------------------------------------------*/
717 (defvar flyspell-previous-command nil
718 "The last interactive command checked by Flyspell.")
720 ;*---------------------------------------------------------------------*/
721 ;* flyspell-pre-command-hook ... */
722 ;*---------------------------------------------------------------------*/
723 (defun flyspell-pre-command-hook ()
724 "Save the current buffer and point for Flyspell's post-command hook."
726 (setq flyspell-pre-buffer (current-buffer))
727 (setq flyspell-pre-point (point))
728 (setq flyspell-pre-column (current-column)))
730 ;*---------------------------------------------------------------------*/
731 ;* flyspell-mode-off ... */
732 ;*---------------------------------------------------------------------*/
734 (defun flyspell-mode-off ()
735 "Turn Flyspell mode off."
736 ;; we remove the hooks
737 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
738 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
739 (setq after-change-functions (delq 'flyspell-after-change-function
740 after-change-functions))
741 ;; we remove all the flyspell hilightings
742 (flyspell-delete-all-overlays)
743 ;; we have to erase pre cache variables
744 (setq flyspell-pre-buffer nil)
745 (setq flyspell-pre-point nil)
746 ;; we mark the mode as killed
747 (setq flyspell-mode nil))
749 ;*---------------------------------------------------------------------*/
750 ;* flyspell-check-pre-word-p ... */
751 ;*---------------------------------------------------------------------*/
752 (defun flyspell-check-pre-word-p ()
753 "Return non-nil if we should to check the word before point.
754 More precisely, it applies to the word that was before point
755 before the current command."
757 ((or (not (numberp flyspell-pre-point))
758 (not (bufferp flyspell-pre-buffer))
759 (not (buffer-live-p flyspell-pre-buffer)))
761 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
762 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
764 ((or (and (= flyspell-pre-point (- (point) 1))
765 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
766 (= flyspell-pre-point (point))
767 (= flyspell-pre-point (+ (point) 1)))
769 ((and (symbolp this-command)
770 (not executing-kbd-macro)
771 (or (get this-command 'flyspell-delayed)
772 (and (get this-command 'flyspell-deplacement)
773 (eq flyspell-previous-command this-command)))
774 (or (= (current-column) 0)
775 (= (current-column) flyspell-pre-column)
776 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
778 ((not (eq (current-buffer) flyspell-pre-buffer))
780 ((not (and (numberp flyspell-word-cache-start)
781 (numberp flyspell-word-cache-end)))
784 (or (< flyspell-pre-point flyspell-word-cache-start)
785 (> flyspell-pre-point flyspell-word-cache-end)))))
787 ;*---------------------------------------------------------------------*/
788 ;* The flyspell after-change-hook, store the change position. In */
789 ;* the post command hook, we will check, if the word at this */
790 ;* position has to be spell checked. */
791 ;*---------------------------------------------------------------------*/
792 (defvar flyspell-changes nil)
794 ;*---------------------------------------------------------------------*/
795 ;* flyspell-after-change-function ... */
796 ;*---------------------------------------------------------------------*/
797 (defun flyspell-after-change-function (start stop len)
798 "Save the current buffer and point for Flyspell's post-command hook."
800 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
802 ;*---------------------------------------------------------------------*/
803 ;* flyspell-check-changed-word-p ... */
804 ;*---------------------------------------------------------------------*/
805 (defun flyspell-check-changed-word-p (start stop)
806 "Return t when the changed word has to be checked.
807 The answer depends of several criteria.
808 Mostly we check word delimiters."
810 ((and (memq (char-after start) '(?\n ? )) (> stop start))
812 ((not (numberp flyspell-pre-point))
814 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
816 ((let ((pos (point)))
817 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
822 ;*---------------------------------------------------------------------*/
823 ;* flyspell-check-word-p ... */
824 ;*---------------------------------------------------------------------*/
825 (defun flyspell-check-word-p ()
826 "Return t when the word at `point' has to be checked.
827 The answer depends of several criteria.
828 Mostly we check word delimiters."
830 ((<= (- (point-max) 1) (point-min))
831 ;; the buffer is not filled enough
833 ((and (and (> (current-column) 0)
834 (not (eq (current-column) flyspell-pre-column)))
837 (and (looking-at (flyspell-get-not-casechars))
838 (or flyspell-consider-dash-as-word-delimiter-flag
839 (not (looking-at "\\-"))))))
840 ;; yes because we have reached or typed a word delimiter.
842 ((symbolp this-command)
844 ((get this-command 'flyspell-deplacement)
845 (not (eq flyspell-previous-command this-command)))
846 ((get this-command 'flyspell-delayed)
847 ;; the current command is not delayed, that
848 ;; is that we must check the word now
849 (if (featurep 'xemacs)
850 (sit-for flyspell-delay nil)
851 (sit-for flyspell-delay 0 nil)))
855 ;*---------------------------------------------------------------------*/
856 ;* flyspell-debug-signal-no-check ... */
857 ;*---------------------------------------------------------------------*/
858 (defun flyspell-debug-signal-no-check (msg obj)
859 (setq debug-on-error t)
861 (let ((buffer (get-buffer-create "*flyspell-debug*")))
864 (insert "NO-CHECK:\n")
865 (insert (format " %S : %S\n" msg obj)))))
867 ;*---------------------------------------------------------------------*/
868 ;* flyspell-debug-signal-pre-word-checked ... */
869 ;*---------------------------------------------------------------------*/
870 (defun flyspell-debug-signal-pre-word-checked ()
871 (setq debug-on-error t)
873 (let ((buffer (get-buffer-create "*flyspell-debug*")))
875 (insert "PRE-WORD:\n")
876 (insert (format " pre-point : %S\n" flyspell-pre-point))
877 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
878 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
879 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
880 (goto-char (point-max)))))
882 ;*---------------------------------------------------------------------*/
883 ;* flyspell-debug-signal-word-checked ... */
884 ;*---------------------------------------------------------------------*/
885 (defun flyspell-debug-signal-word-checked ()
886 (setq debug-on-error t)
888 (let ((oldbuf (current-buffer))
889 (buffer (get-buffer-create "*flyspell-debug*"))
893 (insert (format " this-cmd : %S\n" this-command))
894 (insert (format " delayed : %S\n" (and (symbolp this-command)
895 (get this-command 'flyspell-delayed))))
896 (insert (format " point : %S\n" point))
897 (insert (format " prev-char : [%c] %S\n"
900 (let ((c (if (> (point) (point-min))
903 (char-after (point)))
909 (let ((c (if (> (point) (point-min))
912 (and (and (looking-at (flyspell-get-not-casechars)) 1)
913 (and (or flyspell-consider-dash-as-word-delimiter-flag
914 (not (looking-at "\\-"))) 2))))))
917 (insert (format " because : %S\n"
919 ((not (and (symbolp this-command)
920 (get this-command 'flyspell-delayed)))
921 ;; the current command is not delayed, that
922 ;; is that we must check the word now
926 (let ((c (if (> (point) (point-min))
929 (and (looking-at (flyspell-get-not-casechars))
930 (or flyspell-consider-dash-as-word-delimiter-flag
931 (not (looking-at "\\-"))))))))
934 ;; yes because we have reached or typed a word delimiter.
936 ((not (integerp flyspell-delay))
937 ;; yes because the user had set up a no-delay configuration.
941 (goto-char (point-max)))))
943 ;*---------------------------------------------------------------------*/
944 ;* flyspell-debug-signal-changed-checked ... */
945 ;*---------------------------------------------------------------------*/
946 (defun flyspell-debug-signal-changed-checked ()
947 (setq debug-on-error t)
949 (let ((buffer (get-buffer-create "*flyspell-debug*"))
952 (insert "CHANGED WORD:\n")
953 (insert (format " point : %S\n" point))
954 (goto-char (point-max)))))
956 ;*---------------------------------------------------------------------*/
957 ;* flyspell-post-command-hook ... */
958 ;* ------------------------------------------------------------- */
959 ;* It is possible that we check several words: */
960 ;* 1- the current word is checked if the predicate */
961 ;* FLYSPELL-CHECK-WORD-P is true */
962 ;* 2- the word that used to be the current word before the */
963 ;* THIS-COMMAND is checked if: */
964 ;* a- the previous word is different from the current word */
965 ;* b- the previous word as not just been checked by the */
966 ;* previous FLYSPELL-POST-COMMAND-HOOK */
967 ;* 3- the words changed by the THIS-COMMAND that are neither the */
968 ;* previous word nor the current word */
969 ;*---------------------------------------------------------------------*/
970 (defun flyspell-post-command-hook ()
971 "The `post-command-hook' used by flyspell to check a word in-the-fly."
973 (let ((command this-command))
974 (if (flyspell-check-pre-word-p)
976 '(flyspell-debug-signal-pre-word-checked)
977 (set-buffer flyspell-pre-buffer)
979 (goto-char flyspell-pre-point)
981 (if (flyspell-check-word-p)
983 '(flyspell-debug-signal-word-checked)
985 ;; we remember which word we have just checked.
986 ;; this will be used next time we will check a word
987 ;; to compare the next current word with the word
988 ;; that as been registered in the pre-command-hook
989 ;; that is these variables are used within the predicate
990 ;; FLYSPELL-CHECK-PRE-WORD-P
991 (setq flyspell-pre-pre-buffer (current-buffer))
992 (setq flyspell-pre-pre-point (point)))
994 (setq flyspell-pre-pre-buffer nil)
995 (setq flyspell-pre-pre-point nil)
996 ;; when a word is not checked because of a delayed command
997 ;; we do not disable the ispell cache.
998 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
999 (setq flyspell-word-cache-end -1))))
1000 (while (consp flyspell-changes)
1001 (let ((start (car (car flyspell-changes)))
1002 (stop (cdr (car flyspell-changes))))
1003 (if (flyspell-check-changed-word-p start stop)
1005 '(flyspell-debug-signal-changed-checked)
1008 (setq flyspell-changes (cdr flyspell-changes))))
1009 (setq flyspell-previous-command command)))
1011 ;*---------------------------------------------------------------------*/
1012 ;* flyspell-notify-misspell ... */
1013 ;*---------------------------------------------------------------------*/
1014 (defun flyspell-notify-misspell (start end word poss)
1015 (let ((replacements (if (stringp poss)
1017 (if flyspell-sort-corrections
1018 (sort (car (cdr (cdr poss))) 'string<)
1019 (car (cdr (cdr poss)))))))
1020 (if flyspell-issue-message-flag
1021 (message (format "mispelling `%s' %S" word replacements)))))
1023 ;*---------------------------------------------------------------------*/
1024 ;* flyspell-word ... */
1025 ;*---------------------------------------------------------------------*/
1026 (defun flyspell-word (&optional following)
1027 "Spell check a word."
1028 (interactive (list current-prefix-arg))
1030 (setq following ispell-following-word))
1032 ;; use the correct dictionary
1033 (flyspell-accept-buffer-local-defs)
1034 (let* ((cursor-location (point))
1035 (flyspell-word (flyspell-get-word following))
1036 start end poss word)
1037 (if (or (eq flyspell-word nil)
1038 (and (fboundp flyspell-generic-check-word-p)
1039 (not (funcall flyspell-generic-check-word-p))))
1042 ;; destructure return flyspell-word info list.
1043 (setq start (car (cdr flyspell-word))
1044 end (car (cdr (cdr flyspell-word)))
1045 word (car flyspell-word))
1046 ;; before checking in the directory, we check for doublons.
1048 ((and (or (not (eq ispell-parser 'tex))
1049 (and (> start (point-min))
1050 (not (eq (char-after (1- start)) ?\\))))
1051 flyspell-mark-duplications-flag
1054 (word-search-backward word
1056 (+ 1 (- end start)))
1058 ;; yes, this is a doublon
1059 (flyspell-highlight-incorrect-region start end 'doublon))
1060 ((and (eq flyspell-word-cache-start start)
1061 (eq flyspell-word-cache-end end)
1062 (string-equal flyspell-word-cache-word word))
1063 ;; this word had been already checked, we skip
1065 ((and (eq ispell-parser 'tex)
1066 (flyspell-tex-command-p flyspell-word))
1067 ;; this is a correct word (because a tex command)
1068 (flyspell-unhighlight-at start)
1070 (flyspell-unhighlight-at (- end 1)))
1073 ;; we setup the cache
1074 (setq flyspell-word-cache-start start)
1075 (setq flyspell-word-cache-end end)
1076 (setq flyspell-word-cache-word word)
1077 ;; now check spelling of word.
1078 (process-send-string ispell-process "%\n")
1079 ;; put in verbose mode
1080 (process-send-string ispell-process
1081 (concat "^" word "\n"))
1082 ;; we mark the ispell process so it can be killed
1083 ;; when emacs is exited without query
1084 (if (fboundp 'process-kill-without-query)
1085 (process-kill-without-query ispell-process))
1086 ;; wait until ispell has processed word
1088 (accept-process-output ispell-process)
1089 (not (string= "" (car ispell-filter)))))
1090 ;; (process-send-string ispell-process "!\n")
1091 ;; back to terse mode.
1092 (setq ispell-filter (cdr ispell-filter))
1093 (if (consp ispell-filter)
1094 (setq poss (ispell-parse-output (car ispell-filter))))
1097 (flyspell-unhighlight-at start)
1099 (flyspell-unhighlight-at (- end 1)))
1101 ((and (stringp poss) flyspell-highlight-flag)
1103 (flyspell-unhighlight-at start)
1105 (flyspell-unhighlight-at (- end 1)))
1108 (flyspell-unhighlight-at start)
1110 (flyspell-unhighlight-at (- end 1))))
1111 ((or (and (< flyspell-duplicate-distance 0)
1114 (word-search-backward word
1119 (word-search-forward word
1122 (and (> flyspell-duplicate-distance 0)
1125 (word-search-backward
1128 flyspell-duplicate-distance)
1132 (word-search-forward
1135 flyspell-duplicate-distance)
1137 (if flyspell-highlight-flag
1138 (flyspell-highlight-duplicate-region start end)
1139 (message (format "duplicate `%s'" word))))
1141 ;; incorrect highlight the location
1142 (if flyspell-highlight-flag
1143 (flyspell-highlight-incorrect-region start end poss)
1144 (flyspell-notify-misspell start end word poss))))
1145 ;; return to original location
1146 (goto-char cursor-location)
1147 (if ispell-quit (setq ispell-quit nil)))))))))
1149 ;*---------------------------------------------------------------------*/
1150 ;* flyspell-tex-math-initialized ... */
1151 ;*---------------------------------------------------------------------*/
1152 (defvar flyspell-tex-math-initialized nil)
1154 ;*---------------------------------------------------------------------*/
1155 ;* flyspell-math-tex-command-p ... */
1156 ;* ------------------------------------------------------------- */
1157 ;* This function uses the texmathp package to check if (point) */
1158 ;* is within a tex command. In order to avoid using */
1159 ;* condition-case each time we use the variable */
1160 ;* flyspell-tex-math-initialized to make a special case the first */
1161 ;* time that function is called. */
1162 ;*---------------------------------------------------------------------*/
1163 (defun flyspell-math-tex-command-p ()
1165 (flyspell-check-tex-math-command
1167 ((eq flyspell-tex-math-initialized t)
1169 ((eq flyspell-tex-math-initialized 'error)
1172 (setq flyspell-tex-math-initialized t)
1176 (setq flyspell-tex-math-initialized 'error)
1179 ;*---------------------------------------------------------------------*/
1180 ;* flyspell-tex-command-p ... */
1181 ;*---------------------------------------------------------------------*/
1182 (defun flyspell-tex-command-p (word)
1183 "Return t if WORD is a TeX command."
1185 (let ((b (car (cdr word))))
1186 (and (re-search-backward "\\\\" (- (point) 100) t)
1187 (or (= (match-end 0) b)
1188 (and (goto-char (match-end 0))
1189 (looking-at flyspell-tex-command-regexp)
1190 (>= (match-end 0) b))))))
1191 (flyspell-math-tex-command-p)))
1193 ;*---------------------------------------------------------------------*/
1194 ;* flyspell-casechars-cache ... */
1195 ;*---------------------------------------------------------------------*/
1196 (defvar flyspell-casechars-cache nil)
1197 (defvar flyspell-ispell-casechars-cache nil)
1198 (make-variable-buffer-local 'flyspell-casechars-cache)
1199 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1201 ;*---------------------------------------------------------------------*/
1202 ;* flyspell-get-casechars ... */
1203 ;*---------------------------------------------------------------------*/
1204 (defun flyspell-get-casechars ()
1205 "This function builds a string that is the regexp of word chars.
1206 In order to avoid one useless string construction,
1207 this function changes the last char of the `ispell-casechars' string."
1208 (let ((ispell-casechars (ispell-get-casechars)))
1210 ((eq ispell-parser 'tex)
1211 (setq flyspell-ispell-casechars-cache ispell-casechars)
1212 (setq flyspell-casechars-cache
1213 (concat (substring ispell-casechars
1215 (- (length ispell-casechars) 1))
1217 flyspell-casechars-cache)
1219 (setq flyspell-ispell-casechars-cache ispell-casechars)
1220 (setq flyspell-casechars-cache ispell-casechars)
1221 flyspell-casechars-cache))))
1223 ;*---------------------------------------------------------------------*/
1224 ;* flyspell-get-not-casechars-cache ... */
1225 ;*---------------------------------------------------------------------*/
1226 (defvar flyspell-not-casechars-cache nil)
1227 (defvar flyspell-ispell-not-casechars-cache nil)
1228 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1229 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1231 ;*---------------------------------------------------------------------*/
1232 ;* flyspell-get-not-casechars ... */
1233 ;*---------------------------------------------------------------------*/
1234 (defun flyspell-get-not-casechars ()
1235 "This function builds a string that is the regexp of non-word chars."
1236 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1238 ((eq ispell-parser 'tex)
1239 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1240 (setq flyspell-not-casechars-cache
1241 (concat (substring ispell-not-casechars
1243 (- (length ispell-not-casechars) 1))
1245 flyspell-not-casechars-cache)
1247 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1248 (setq flyspell-not-casechars-cache ispell-not-casechars)
1249 flyspell-not-casechars-cache))))
1251 ;*---------------------------------------------------------------------*/
1252 ;* flyspell-get-word ... */
1253 ;*---------------------------------------------------------------------*/
1254 (defun flyspell-get-word (following)
1255 "Return the word for spell-checking according to Ispell syntax.
1256 If argument FOLLOWING is non-nil or if `ispell-following-word'
1257 is non-nil when called interactively, then the following word
1258 \(rather than preceding\) is checked when the cursor is not over a word.
1259 Optional second argument contains other chars that can be included in word
1262 Word syntax described by `ispell-dictionary-alist' (which see)."
1263 (let* ((flyspell-casechars (flyspell-get-casechars))
1264 (flyspell-not-casechars (flyspell-get-not-casechars))
1265 (ispell-otherchars (ispell-get-otherchars))
1266 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1267 (word-regexp (if (string< "" ispell-otherchars)
1268 (concat flyspell-casechars
1271 (if (> (length ispell-otherchars) 0) "?")
1274 (if ispell-many-otherchars-p
1276 (concat flyspell-casechars "+")))
1280 (if (not (looking-at flyspell-casechars))
1282 (re-search-forward flyspell-casechars (point-max) t)
1283 (re-search-backward flyspell-casechars (point-min) t)))
1284 ;; move to front of word
1285 (re-search-backward flyspell-not-casechars (point-min) 'start)
1287 (if (string< "" ispell-otherchars)
1288 (while (and (looking-at ispell-otherchars)
1290 (or (not did-it-once)
1291 ispell-many-otherchars-p)
1292 (not (eq pos (point))))
1294 (setq did-it-once t)
1296 (if (looking-at flyspell-casechars)
1297 (re-search-backward flyspell-not-casechars (point-min) 'move)
1298 (backward-char -1)))))
1299 ;; Now mark the word and save to string.
1300 (if (eq (re-search-forward word-regexp (point-max) t) nil)
1303 (setq start (match-beginning 0)
1305 word (buffer-substring-no-properties start end))
1306 (list word start end)))))
1308 ;*---------------------------------------------------------------------*/
1309 ;* flyspell-small-region ... */
1310 ;*---------------------------------------------------------------------*/
1311 (defun flyspell-small-region (beg end)
1312 "Flyspell text between BEG and END."
1320 (while (< (point) end)
1321 (if (and flyspell-issue-message-flag (= count 100))
1323 (message "Spell Checking...%d%%"
1324 (* 100 (/ (float (- (point) beg)) (- end beg))))
1326 (setq count (+ 1 count)))
1329 (let ((cur (point)))
1331 (if (and (< (point) end) (> (point) (+ cur 1)))
1332 (backward-char 1)))))
1334 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1337 ;*---------------------------------------------------------------------*/
1338 ;* flyspell-external-ispell-process ... */
1339 ;*---------------------------------------------------------------------*/
1340 (defvar flyspell-external-ispell-process '()
1341 "The external Flyspell Ispell process.")
1343 ;*---------------------------------------------------------------------*/
1344 ;* flyspell-external-ispell-buffer ... */
1345 ;*---------------------------------------------------------------------*/
1346 (defvar flyspell-external-ispell-buffer '())
1347 (defvar flyspell-large-region-buffer '())
1348 (defvar flyspell-large-region-beg (point-min))
1349 (defvar flyspell-large-region-end (point-max))
1351 ;*---------------------------------------------------------------------*/
1352 ;* flyspell-external-point-words ... */
1353 ;*---------------------------------------------------------------------*/
1354 (defun flyspell-external-point-words ()
1355 (let ((buffer flyspell-external-ispell-buffer))
1357 (beginning-of-buffer)
1358 (let ((size (- flyspell-large-region-end flyspell-large-region-beg))
1359 (start flyspell-large-region-beg))
1360 ;; now we are done with ispell, we have to find the word in
1361 ;; the initial buffer
1362 (while (< (point) (- (point-max) 1))
1363 ;; we have to fetch the incorrect word
1364 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1365 (let ((word (match-string 1)))
1366 (goto-char (match-end 0))
1367 (set-buffer flyspell-large-region-buffer)
1368 (goto-char flyspell-large-region-beg)
1369 (if flyspell-issue-message-flag
1370 (message "Spell Checking...%d%% [%s]"
1371 (* 100 (/ (float (- (point) start)) size))
1373 (if (search-forward word flyspell-large-region-end t)
1375 (setq flyspell-large-region-beg (point))
1376 (goto-char (- (point) 1))
1378 (set-buffer buffer))
1379 (goto-char (point-max)))))
1381 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1382 ;; ok, we are done with pointing out incorrect words, we just
1383 ;; have to kill the temporary buffer
1384 (kill-buffer flyspell-external-ispell-buffer)
1385 (setq flyspell-external-ispell-buffer nil)))
1387 ;*---------------------------------------------------------------------*/
1388 ;* flyspell-large-region ... */
1389 ;*---------------------------------------------------------------------*/
1390 (defun flyspell-large-region (beg end)
1391 (let* ((curbuf (current-buffer))
1392 (buffer (get-buffer-create "*flyspell-region*")))
1393 (setq flyspell-external-ispell-buffer buffer)
1394 (setq flyspell-large-region-buffer curbuf)
1395 (setq flyspell-large-region-beg beg)
1396 (setq flyspell-large-region-end end)
1399 ;; this is done, we can start checking...
1400 (if flyspell-issue-message-flag (message "Checking region..."))
1402 (let ((c (apply 'call-process-region beg
1410 ;; Local dictionary becomes the global dictionary in use.
1411 (if ispell-local-dictionary
1412 (setq ispell-dictionary ispell-local-dictionary))
1413 (setq args (ispell-get-ispell-args))
1414 (if ispell-dictionary ; use specified dictionary
1416 (append (list "-d" ispell-dictionary) args)))
1417 (if ispell-personal-dictionary ; use specified pers dict
1422 ispell-personal-dictionary)))))
1423 (setq args (append args ispell-extra-args))
1426 (flyspell-external-point-words)
1427 (error "Can't check region...")))))
1429 ;*---------------------------------------------------------------------*/
1430 ;* flyspell-region ... */
1431 ;* ------------------------------------------------------------- */
1432 ;* Because `ispell -a' is too slow, it is not possible to use */
1433 ;* it on large region. Then, when ispell is invoked on a large */
1434 ;* text region, a new `ispell -l' process is spawned. The */
1435 ;* pointed out words are then searched in the region a checked with */
1436 ;* regular flyspell means. */
1437 ;*---------------------------------------------------------------------*/
1439 (defun flyspell-region (beg end)
1440 "Flyspell text between BEG and END."
1449 (if (> (- end beg) flyspell-large-region)
1450 (flyspell-large-region beg end)
1451 (flyspell-small-region beg end)))))
1453 ;*---------------------------------------------------------------------*/
1454 ;* flyspell-buffer ... */
1455 ;*---------------------------------------------------------------------*/
1457 (defun flyspell-buffer ()
1458 "Flyspell whole buffer."
1460 (flyspell-region (point-min) (point-max)))
1462 ;*---------------------------------------------------------------------*/
1463 ;* old next error position ... */
1464 ;*---------------------------------------------------------------------*/
1465 (defvar flyspell-old-buffer-error nil)
1466 (defvar flyspell-old-pos-error nil)
1468 ;*---------------------------------------------------------------------*/
1469 ;* flyspell-goto-next-error ... */
1470 ;*---------------------------------------------------------------------*/
1471 (defun flyspell-goto-next-error ()
1472 "Go to the next previously detected error.
1473 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1478 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1479 (eq pos flyspell-old-pos-error))
1481 (if (= flyspell-old-pos-error max)
1482 ;; goto beginning of buffer
1484 (message "Restarting from beginning of buffer")
1485 (goto-char (point-min)))
1487 (setq pos (point))))
1488 ;; seek the next error
1489 (while (and (< pos max)
1490 (let ((ovs (overlays-at pos))
1492 (while (and (not r) (consp ovs))
1493 (if (flyspell-overlay-p (car ovs))
1495 (setq ovs (cdr ovs))))
1497 (setq pos (1+ pos)))
1498 ;; save the current location for next invocation
1499 (setq flyspell-old-pos-error pos)
1500 (setq flyspell-old-buffer-error (current-buffer))
1503 (message "No more miss-spelled word!"))))
1505 ;*---------------------------------------------------------------------*/
1506 ;* flyspell-overlay-p ... */
1507 ;*---------------------------------------------------------------------*/
1508 (defun flyspell-overlay-p (o)
1509 "A predicate that return true iff O is an overlay used by flyspell."
1510 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1512 ;*---------------------------------------------------------------------*/
1513 ;* flyspell-delete-all-overlays ... */
1514 ;* ------------------------------------------------------------- */
1515 ;* Remove all the overlays introduced by flyspell. */
1516 ;*---------------------------------------------------------------------*/
1517 (defun flyspell-delete-all-overlays ()
1518 "Delete all the overlays used by flyspell."
1519 (let ((l (overlays-in (point-min) (point-max))))
1522 (if (flyspell-overlay-p (car l))
1523 (delete-overlay (car l)))
1524 (setq l (cdr l))))))
1526 ;*---------------------------------------------------------------------*/
1527 ;* flyspell-unhighlight-at ... */
1528 ;*---------------------------------------------------------------------*/
1529 (defun flyspell-unhighlight-at (pos)
1530 "Remove the flyspell overlay that are located at POS."
1531 (if flyspell-persistent-highlight
1532 (let ((overlays (overlays-at pos)))
1533 (while (consp overlays)
1534 (if (flyspell-overlay-p (car overlays))
1535 (delete-overlay (car overlays)))
1536 (setq overlays (cdr overlays))))
1537 (if (flyspell-overlay-p flyspell-overlay)
1538 (delete-overlay flyspell-overlay))))
1540 ;*---------------------------------------------------------------------*/
1541 ;* flyspell-properties-at-p ... */
1542 ;* ------------------------------------------------------------- */
1543 ;* Is there an highlight properties at position pos? */
1544 ;*---------------------------------------------------------------------*/
1545 (defun flyspell-properties-at-p (pos)
1546 "Return t if there is a text property at POS, not counting `local-map'.
1547 If variable `flyspell-highlight-properties' is set to nil,
1548 text with properties are not checked. This function is used to discover
1549 if the character at POS has any other property."
1550 (let ((prop (text-properties-at pos))
1552 (while (and keep (consp prop))
1553 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1554 (setq prop (cdr (cdr prop)))
1558 ;*---------------------------------------------------------------------*/
1559 ;* make-flyspell-overlay ... */
1560 ;*---------------------------------------------------------------------*/
1561 (defun make-flyspell-overlay (beg end face mouse-face)
1562 "Allocate an overlay to highlight an incorrect word.
1563 BEG and END specify the range in the buffer of that word.
1564 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1566 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1567 (overlay-put flyspell-overlay 'face face)
1568 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1569 (overlay-put flyspell-overlay 'flyspell-overlay t)
1570 (overlay-put flyspell-overlay 'evaporate t)
1571 (overlay-put flyspell-overlay 'help-echo "mouse-2: correct word at point")
1572 (if flyspell-use-local-map
1573 (overlay-put flyspell-overlay
1574 flyspell-overlay-keymap-property-name
1575 flyspell-mouse-map))
1576 (when (eq face 'flyspell-incorrect-face)
1577 (and (stringp flyspell-before-incorrect-word-string)
1578 (overlay-put flyspell-overlay 'before-string
1579 flyspell-before-incorrect-word-string))
1580 (and (stringp flyspell-after-incorrect-word-string)
1581 (overlay-put flyspell-overlay 'after-string
1582 flyspell-after-incorrect-word-string)))
1585 ;*---------------------------------------------------------------------*/
1586 ;* flyspell-highlight-incorrect-region ... */
1587 ;*---------------------------------------------------------------------*/
1588 (defun flyspell-highlight-incorrect-region (beg end poss)
1589 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1590 (unless (run-hook-with-args-until-success
1591 'flyspell-incorrect-hook beg end poss)
1592 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1594 ;; we cleanup all the overlay that are in the region, not
1595 ;; beginning at the word start position
1596 (if (< (1+ beg) end)
1597 (let ((os (overlays-in (1+ beg) end)))
1599 (if (flyspell-overlay-p (car os))
1600 (delete-overlay (car os)))
1601 (setq os (cdr os)))))
1602 ;; we cleanup current overlay at the same position
1603 (if (and (not flyspell-persistent-highlight)
1604 (overlayp flyspell-overlay))
1605 (delete-overlay flyspell-overlay)
1606 (let ((os (overlays-at beg)))
1608 (if (flyspell-overlay-p (car os))
1609 (delete-overlay (car os)))
1610 (setq os (cdr os)))))
1611 ;; now we can use a new overlay
1612 (setq flyspell-overlay
1613 (make-flyspell-overlay beg end
1614 'flyspell-incorrect-face
1617 ;*---------------------------------------------------------------------*/
1618 ;* flyspell-highlight-duplicate-region ... */
1619 ;*---------------------------------------------------------------------*/
1620 (defun flyspell-highlight-duplicate-region (beg end)
1621 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
1622 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1624 ;; we cleanup current overlay at the same position
1625 (if (and (not flyspell-persistent-highlight)
1626 (overlayp flyspell-overlay))
1627 (delete-overlay flyspell-overlay)
1628 (let ((overlays (overlays-at beg)))
1629 (while (consp overlays)
1630 (if (flyspell-overlay-p (car overlays))
1631 (delete-overlay (car overlays)))
1632 (setq overlays (cdr overlays)))))
1633 ;; now we can use a new overlay
1634 (setq flyspell-overlay
1635 (make-flyspell-overlay beg end
1636 'flyspell-duplicate-face
1639 ;*---------------------------------------------------------------------*/
1640 ;* flyspell-auto-correct-cache ... */
1641 ;*---------------------------------------------------------------------*/
1642 (defvar flyspell-auto-correct-pos nil)
1643 (defvar flyspell-auto-correct-region nil)
1644 (defvar flyspell-auto-correct-ring nil)
1645 (defvar flyspell-auto-correct-word nil)
1646 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1647 (make-variable-buffer-local 'flyspell-auto-correct-region)
1648 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1649 (make-variable-buffer-local 'flyspell-auto-correct-word)
1651 ;*---------------------------------------------------------------------*/
1652 ;* flyspell-check-previous-highlighted-word ... */
1653 ;*---------------------------------------------------------------------*/
1654 (defun flyspell-check-previous-highlighted-word (&optional arg)
1655 "Correct the closer misspelled word.
1656 This function scans a mis-spelled word before the cursor. If it finds one
1657 it proposes replacement for that word. With prefix arg, count that many
1658 misspelled words backwards."
1660 (let ((pos1 (point))
1662 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1665 (while (and (setq pos (previous-overlay-change pos))
1668 (if (> pos (point-min))
1670 (setq ovs (overlays-at (1- pos)))
1673 (setq ovs (cdr ovs))
1674 (if (and (overlay-get ov 'flyspell-overlay)
1675 (= 0 (setq arg (1- arg))))
1676 (throw 'exit t)))))))
1680 (error "No word to correct before point"))))
1682 ;*---------------------------------------------------------------------*/
1683 ;* flyspell-display-next-corrections ... */
1684 ;*---------------------------------------------------------------------*/
1685 (defun flyspell-display-next-corrections (corrections)
1686 (let ((string "Corrections:")
1689 (while (< (length string) 80)
1690 (if (equal (car l) flyspell-auto-correct-word)
1691 (setq pos (cons (+ 1 (length string)) pos)))
1692 (setq string (concat string " " (car l)))
1695 (let ((num (car pos)))
1696 (put-text-property num
1697 (+ num (length flyspell-auto-correct-word))
1699 'flyspell-incorrect-face
1701 (setq pos (cdr pos)))
1702 (if (fboundp 'display-message)
1703 (display-message 'no-log string)
1706 ;*---------------------------------------------------------------------*/
1707 ;* flyspell-abbrev-table ... */
1708 ;*---------------------------------------------------------------------*/
1709 (defun flyspell-abbrev-table ()
1710 (if flyspell-use-global-abbrev-table-p
1712 local-abbrev-table))
1714 ;*---------------------------------------------------------------------*/
1715 ;* flyspell-define-abbrev ... */
1716 ;*---------------------------------------------------------------------*/
1717 (defun flyspell-define-abbrev (name expansion)
1718 (let ((table (flyspell-abbrev-table)))
1720 (define-abbrev table name expansion))))
1722 ;*---------------------------------------------------------------------*/
1723 ;* flyspell-auto-correct-word ... */
1724 ;*---------------------------------------------------------------------*/
1725 (defun flyspell-auto-correct-word ()
1726 "Correct the current word.
1727 This command proposes various successive corrections for the current word."
1730 (old-max (point-max)))
1731 ;; use the correct dictionary
1732 (flyspell-accept-buffer-local-defs)
1733 (if (and (eq flyspell-auto-correct-pos pos)
1734 (consp flyspell-auto-correct-region))
1735 ;; we have already been using the function at the same location
1736 (let* ((start (car flyspell-auto-correct-region))
1737 (len (cdr flyspell-auto-correct-region)))
1738 (flyspell-unhighlight-at start)
1739 (delete-region start (+ start len))
1740 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1741 (let* ((word (car flyspell-auto-correct-ring))
1742 (len (length word)))
1743 (rplacd flyspell-auto-correct-region len)
1745 (if flyspell-abbrev-p
1746 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1747 flyspell-auto-correct-word)
1748 (flyspell-change-abbrev (flyspell-abbrev-table)
1749 flyspell-auto-correct-word
1751 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1752 (funcall flyspell-insert-function word)
1754 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1755 (flyspell-ajust-cursor-point pos (point) old-max)
1756 (setq flyspell-auto-correct-pos (point)))
1757 ;; fetch the word to be checked
1758 (let ((word (flyspell-get-word nil))
1760 ;; destructure return word info list.
1761 (setq start (car (cdr word))
1762 end (car (cdr (cdr word)))
1764 (setq flyspell-auto-correct-word word)
1765 ;; now check spelling of word.
1766 (process-send-string ispell-process "%\n") ;put in verbose mode
1767 (process-send-string ispell-process (concat "^" word "\n"))
1768 ;; wait until ispell has processed word
1770 (accept-process-output ispell-process)
1771 (not (string= "" (car ispell-filter)))))
1772 (setq ispell-filter (cdr ispell-filter))
1773 (if (consp ispell-filter)
1774 (setq poss (ispell-parse-output (car ispell-filter))))
1775 (cond ((or (eq poss t) (stringp poss))
1776 ;; don't correct word
1780 (error "Ispell: error in Ispell process"))
1782 ;; the word is incorrect, we have to propose a replacement
1783 (let ((replacements (if flyspell-sort-corrections
1784 (sort (car (cdr (cdr poss))) 'string<)
1785 (car (cdr (cdr poss))))))
1786 (setq flyspell-auto-correct-region nil)
1787 (if (consp replacements)
1789 (let ((replace (car replacements)))
1790 (let ((new-word replace))
1791 (if (not (equal new-word (car poss)))
1793 ;; the save the current replacements
1794 (setq flyspell-auto-correct-region
1795 (cons start (length new-word)))
1796 (let ((l replacements))
1797 (while (consp (cdr l))
1799 (rplacd l (cons (car poss) replacements)))
1800 (setq flyspell-auto-correct-ring
1802 (flyspell-unhighlight-at start)
1803 (delete-region start end)
1804 (funcall flyspell-insert-function new-word)
1805 (if flyspell-abbrev-p
1806 (if (flyspell-already-abbrevp
1807 (flyspell-abbrev-table) word)
1808 (flyspell-change-abbrev
1809 (flyspell-abbrev-table)
1812 (flyspell-define-abbrev word new-word)))
1814 (flyspell-display-next-corrections
1815 (cons new-word flyspell-auto-correct-ring))
1816 (flyspell-ajust-cursor-point pos
1819 (setq flyspell-auto-correct-pos (point))
1820 (ispell-pdict-save t)))))
1822 ;*---------------------------------------------------------------------*/
1823 ;* flyspell-auto-correct-previous-pos ... */
1824 ;*---------------------------------------------------------------------*/
1825 (defvar flyspell-auto-correct-previous-pos nil
1826 "Holds the start of the first incorrect word before point.")
1828 ;*---------------------------------------------------------------------*/
1829 ;* flyspell-auto-correct-previous-hook ... */
1830 ;*---------------------------------------------------------------------*/
1831 (defun flyspell-auto-correct-previous-hook ()
1832 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1833 Sets flyspell-auto-correct-previous-pos to nil"
1835 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1836 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1837 (setq flyspell-auto-correct-previous-pos nil)))
1839 ;*---------------------------------------------------------------------*/
1840 ;* flyspell-auto-correct-previous-word ... */
1841 ;*---------------------------------------------------------------------*/
1842 (defun flyspell-auto-correct-previous-word (position)
1843 "*Auto correct the first mispelled word that occurs before point."
1846 (add-hook 'pre-command-hook
1847 (function flyspell-auto-correct-previous-hook) t t)
1850 (unless flyspell-auto-correct-previous-pos
1851 ;; only reset if a new overlay exists
1852 (setq flyspell-auto-correct-previous-pos nil)
1854 (let ((overlay-list (overlays-in (point-min) position))
1855 (new-overlay 'dummy-value))
1857 ;; search for previous (new) flyspell overlay
1858 (while (and new-overlay
1859 (or (not (flyspell-overlay-p new-overlay))
1860 ;; check if its face has changed
1861 (not (eq (get-char-property
1862 (overlay-start new-overlay) 'face)
1863 'flyspell-incorrect-face))))
1864 (setq new-overlay (car-safe overlay-list))
1865 (setq overlay-list (cdr-safe overlay-list)))
1867 ;; if nothing new exits new-overlay should be nil
1868 (if new-overlay;; the length of the word may change so go to the start
1869 (setq flyspell-auto-correct-previous-pos
1870 (overlay-start new-overlay)))))
1872 (when flyspell-auto-correct-previous-pos
1874 (goto-char flyspell-auto-correct-previous-pos)
1875 (let ((ispell-following-word t));; point is at start
1876 (if (numberp flyspell-auto-correct-previous-pos)
1877 (goto-char flyspell-auto-correct-previous-pos))
1878 (flyspell-auto-correct-word))
1879 ;; the point may have moved so reset this
1880 (setq flyspell-auto-correct-previous-pos (point))))))
1882 ;*---------------------------------------------------------------------*/
1883 ;* flyspell-correct-word ... */
1884 ;*---------------------------------------------------------------------*/
1885 (defun flyspell-correct-word (event)
1886 "Pop up a menu of possible corrections for a misspelled word.
1887 The word checked is the word at the mouse position."
1889 ;; use the correct dictionary
1890 (flyspell-accept-buffer-local-defs)
1891 ;; retain cursor location (I don't know why but save-excursion here fails).
1892 (let ((save (point)))
1893 (mouse-set-point event)
1894 (let ((cursor-location (point))
1895 (word (flyspell-get-word nil))
1896 start end poss replace)
1897 ;; destructure return word info list.
1898 (setq start (car (cdr word))
1899 end (car (cdr (cdr word)))
1901 ;; now check spelling of word.
1902 (process-send-string ispell-process "%\n") ;put in verbose mode
1903 (process-send-string ispell-process (concat "^" word "\n"))
1904 ;; wait until ispell has processed word
1906 (accept-process-output ispell-process)
1907 (not (string= "" (car ispell-filter)))))
1908 (setq ispell-filter (cdr ispell-filter))
1909 (if (consp ispell-filter)
1910 (setq poss (ispell-parse-output (car ispell-filter))))
1911 (cond ((or (eq poss t) (stringp poss))
1912 ;; don't correct word
1916 (error "Ispell: error in Ispell process"))
1917 ((string-match "GNU" (emacs-version))
1918 ;; the word is incorrect, we have to propose a replacement
1919 (setq replace (flyspell-emacs-popup event poss word))
1920 (cond ((eq replace 'ignore)
1925 (process-send-string ispell-process (concat "*" word "\n"))
1926 (flyspell-unhighlight-at cursor-location)
1927 (setq ispell-pdict-modified-p '(t)))
1928 ((or (eq replace 'buffer) (eq replace 'session))
1929 (process-send-string ispell-process (concat "@" word "\n"))
1930 (if (null ispell-pdict-modified-p)
1931 (setq ispell-pdict-modified-p
1932 (list ispell-pdict-modified-p)))
1933 (flyspell-unhighlight-at cursor-location)
1935 (if (eq replace 'buffer)
1936 (ispell-add-per-file-word-list word)))
1938 (flyspell-unhighlight-at cursor-location)
1939 (let ((new-word (if (atom replace)
1942 (cursor-location (+ (- (length word) (- end start))
1944 (if (not (equal new-word (car poss)))
1945 (let ((old-max (point-max)))
1946 (delete-region start end)
1947 (funcall flyspell-insert-function new-word)
1948 (if flyspell-abbrev-p
1949 (flyspell-define-abbrev word new-word))
1950 (flyspell-ajust-cursor-point save
1956 ((eq flyspell-emacs 'xemacs)
1957 (flyspell-xemacs-popup
1958 event poss word cursor-location start end save)
1960 (ispell-pdict-save t))))
1962 ;*---------------------------------------------------------------------*/
1963 ;* flyspell-xemacs-correct ... */
1964 ;*---------------------------------------------------------------------*/
1965 (defun flyspell-xemacs-correct (replace poss word cursor-location start end save)
1966 "The xemacs popup menu callback."
1967 (cond ((eq replace 'ignore)
1970 (process-send-string ispell-process (concat "*" word "\n"))
1971 (process-send-string ispell-process "#\n")
1972 (flyspell-unhighlight-at cursor-location)
1973 (setq ispell-pdict-modified-p '(t)))
1974 ((or (eq replace 'buffer) (eq replace 'session))
1975 (process-send-string ispell-process (concat "@" word "\n"))
1976 (flyspell-unhighlight-at cursor-location)
1977 (if (null ispell-pdict-modified-p)
1978 (setq ispell-pdict-modified-p
1979 (list ispell-pdict-modified-p)))
1980 (if (eq replace 'buffer)
1981 (ispell-add-per-file-word-list word)))
1983 (let ((old-max (point-max))
1984 (new-word (if (atom replace)
1987 (cursor-location (+ (- (length word) (- end start))
1989 (if (not (equal new-word (car poss)))
1991 (delete-region start end)
1993 (funcall flyspell-insert-function new-word)
1994 (if flyspell-abbrev-p
1995 (flyspell-define-abbrev word new-word))))
1996 (flyspell-ajust-cursor-point save cursor-location old-max)))))
1998 ;*---------------------------------------------------------------------*/
1999 ;* flyspell-ajust-cursor-point ... */
2000 ;*---------------------------------------------------------------------*/
2001 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2002 (if (>= save cursor-location)
2003 (let ((new-pos (+ save (- (point-max) old-max))))
2005 ((< new-pos (point-min))
2007 ((> new-pos (point-max))
2012 ;*---------------------------------------------------------------------*/
2013 ;* flyspell-emacs-popup ... */
2014 ;*---------------------------------------------------------------------*/
2015 (defun flyspell-emacs-popup (event poss word)
2016 "The Emacs popup menu."
2018 (let* ((mouse-pos (mouse-position))
2019 (mouse-pos (if (nth 1 mouse-pos)
2021 (set-mouse-position (car mouse-pos)
2022 (/ (frame-width) 2) 2)
2025 (setq event (list (list (car (cdr mouse-pos))
2026 (1+ (cdr (cdr mouse-pos))))
2028 (let* ((corrects (if flyspell-sort-corrections
2029 (sort (car (cdr (cdr poss))) 'string<)
2030 (car (cdr (cdr poss)))))
2031 (cor-menu (if (consp corrects)
2032 (mapcar (lambda (correct)
2033 (list correct correct))
2036 (affix (car (cdr (cdr (cdr poss)))))
2037 (base-menu (let ((save (if (consp affix)
2039 (list (concat "Save affix: " (car affix))
2041 '("Accept (session)" session)
2042 '("Accept (buffer)" buffer))
2043 '(("Save word" save)
2044 ("Accept (session)" session)
2045 ("Accept (buffer)" buffer)))))
2046 (if (consp cor-menu)
2047 (append cor-menu (cons "" save))
2049 (menu (cons "flyspell correction menu" base-menu)))
2050 (car (x-popup-menu event
2051 (list (format "%s [%s]" word (or ispell-local-dictionary
2055 ;*---------------------------------------------------------------------*/
2056 ;* flyspell-xemacs-popup ... */
2057 ;*---------------------------------------------------------------------*/
2058 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
2059 "The XEmacs popup menu."
2060 (let* ((corrects (if flyspell-sort-corrections
2061 (sort (car (cdr (cdr poss))) 'string<)
2062 (car (cdr (cdr poss)))))
2063 (cor-menu (if (consp corrects)
2064 (mapcar (lambda (correct)
2066 (list 'flyspell-xemacs-correct
2077 (affix (car (cdr (cdr (cdr poss)))))
2078 (menu (let ((save (if (consp affix)
2080 (concat "Save affix: " (car affix))
2081 (list 'flyspell-xemacs-correct
2092 (list 'flyspell-xemacs-correct
2101 (session (vector "Accept (session)"
2102 (list 'flyspell-xemacs-correct
2111 (buffer (vector "Accept (buffer)"
2112 (list 'flyspell-xemacs-correct
2121 (if (consp cor-menu)
2122 (append cor-menu (list "-" save session buffer))
2123 (list save session buffer)))))
2124 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2128 ;*---------------------------------------------------------------------*/
2129 ;* Some example functions for real autocorrecting */
2130 ;*---------------------------------------------------------------------*/
2131 (defun flyspell-maybe-correct-transposition (beg end poss)
2132 "Check replacements for transposed characters.
2134 If the text between BEG and END is equal to a correction suggested by
2135 Ispell, after transposing two adjacent characters, correct the text,
2138 The third arg POSS is either the symbol 'doublon' or a list of
2139 possible corrections as returned by 'ispell-parse-output'.
2141 This function is meant to be added to 'flyspell-incorrect-hook'."
2145 (goto-char (1+ beg))
2146 (while (< (point) end)
2148 (when (member (buffer-substring beg end) (car (cdr (cdr poss))))
2150 (transpose-chars -1)
2154 (defun flyspell-maybe-correct-doubling (beg end poss)
2155 "Check replacements for doubled characters.
2157 If the text between BEG and END is equal to a correction suggested by
2158 Ispell, after removing a pair of doubled characters, correct the text,
2161 The third arg POSS is either the symbol 'doublon' or a list of
2162 possible corrections as returned by 'ispell-parse-output'.
2164 This function is meant to be added to 'flyspell-incorrect-hook'."
2168 (let ((last (char-after beg))
2170 (goto-char (1+ beg))
2171 (while (< (point) end)
2172 (setq this (char-after))
2173 (if (not (char-equal this last))
2176 (when (member (buffer-substring beg (1- end)) (car (cdr (cdr poss))))
2179 (insert-char this 1))
2183 ;*---------------------------------------------------------------------*/
2184 ;* flyspell-already-abbrevp ... */
2185 ;*---------------------------------------------------------------------*/
2186 (defun flyspell-already-abbrevp (table word)
2187 (let ((sym (abbrev-symbol word table)))
2188 (and sym (symbolp sym))))
2190 ;*---------------------------------------------------------------------*/
2191 ;* flyspell-change-abbrev ... */
2192 ;*---------------------------------------------------------------------*/
2193 (defun flyspell-change-abbrev (table old new)
2194 (set (abbrev-symbol old table) new))
2197 ;;; flyspell.el ends here