1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.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.
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 If the region is smaller than this number of characters,
241 `flyspell-region' checks the words sequentially using regular
242 flyspell methods. Else, if the region is large, a new Ispell process is
245 If `flyspell-large-region' is nil, all 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 This spawns a single Ispell process and checks each word.
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 (add-minor-mode 'flyspell-mode
535 'flyspell-mode-line-string
540 ;*---------------------------------------------------------------------*/
541 ;* flyspell-buffers ... */
542 ;* ------------------------------------------------------------- */
543 ;* For remembering buffers running flyspell */
544 ;*---------------------------------------------------------------------*/
545 (defvar flyspell-buffers nil)
547 ;*---------------------------------------------------------------------*/
548 ;* flyspell-minibuffer-p ... */
549 ;*---------------------------------------------------------------------*/
550 (defun flyspell-minibuffer-p (buffer)
551 "Is BUFFER a minibuffer?"
552 (let ((ws (get-buffer-window-list buffer t)))
553 (and (consp ws) (window-minibuffer-p (car ws)))))
555 ;*---------------------------------------------------------------------*/
556 ;* flyspell-accept-buffer-local-defs ... */
557 ;*---------------------------------------------------------------------*/
558 (defun flyspell-accept-buffer-local-defs ()
559 ;; strange problem. If buffer in current window has font-lock turned on,
560 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
561 ;; call will reset the buffer to the buffer in the current window. However,
562 ;; it only happens at startup (fix by Albert L. Ting).
563 (let ((buf (current-buffer)))
564 (ispell-accept-buffer-local-defs)
566 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
567 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
568 ;; The dictionary has changed
570 (setq flyspell-dash-dictionary ispell-dictionary)
571 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
572 (if (member (or ispell-local-dictionary ispell-dictionary)
573 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
574 (setq flyspell-consider-dash-as-word-delimiter-flag t)
575 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
577 ;*---------------------------------------------------------------------*/
578 ;* flyspell-mode-on ... */
579 ;*---------------------------------------------------------------------*/
580 (defun flyspell-mode-on ()
581 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
582 (setq ispell-highlight-face 'flyspell-incorrect-face)
583 ;; local dictionaries setup
584 (or ispell-local-dictionary ispell-dictionary
585 (if flyspell-default-dictionary
586 (ispell-change-dictionary flyspell-default-dictionary)))
587 ;; we have to force ispell to accept the local definition or
588 ;; otherwise it could be too late, the local dictionary may
590 (flyspell-accept-buffer-local-defs)
591 ;; we put the `flyspell-delayed' property on some commands
592 (flyspell-delay-commands)
593 ;; we put the `flyspell-deplacement' property on some commands
594 (flyspell-deplacement-commands)
595 ;; we bound flyspell action to post-command hook
596 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
597 ;; we bound flyspell action to pre-command hook
598 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
599 ;; we bound flyspell action to after-change hook
600 (make-local-variable 'after-change-functions)
601 (setq after-change-functions
602 (cons 'flyspell-after-change-function after-change-functions))
603 ;; set flyspell-generic-check-word-p based on the major mode
604 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
606 (setq flyspell-generic-check-word-p mode-predicate)))
607 ;; work around the fact that the `local-map' text-property replaces the
608 ;; buffer's local map rather than shadowing it.
609 (set (make-local-variable 'flyspell-mouse-map)
610 (let ((map (copy-keymap flyspell-mouse-map)))
611 (set-keymap-parent map (current-local-map))
612 (if (and (eq flyspell-emacs 'emacs)
613 (not (string< emacs-version "20")))
614 (define-key map '[tool-bar] nil))
616 (set (make-local-variable 'flyspell-mode-map)
617 (let ((map (copy-keymap flyspell-mode-map)))
618 (set-keymap-parent map (current-local-map))
619 (if (and (eq flyspell-emacs 'emacs)
620 (not (string< emacs-version "20")))
621 (define-key map '[tool-bar] nil))
623 ;; the welcome message
624 (if (and flyspell-issue-message-flag
625 flyspell-issue-welcome-flag
627 (let ((binding (where-is-internal 'flyspell-auto-correct-word
631 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
632 (key-description binding))
633 "Welcome to flyspell. Use Mouse-2 to correct words."))))
634 ;; we end with the flyspell hooks
635 (run-hooks 'flyspell-mode-hook))
637 ;*---------------------------------------------------------------------*/
638 ;* flyspell-delay-commands ... */
639 ;*---------------------------------------------------------------------*/
640 (defun flyspell-delay-commands ()
641 "Install the standard set of Flyspell delayed commands."
642 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
643 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
645 ;*---------------------------------------------------------------------*/
646 ;* flyspell-delay-command ... */
647 ;*---------------------------------------------------------------------*/
648 (defun flyspell-delay-command (command)
649 "Set COMMAND to be delayed, for Flyspell.
650 When flyspell `post-command-hook' is invoked because a delayed command
651 as been used the current word is not immediately checked.
652 It will be checked only after `flyspell-delay' seconds."
653 (interactive "SDelay Flyspell after Command: ")
654 (put command 'flyspell-delayed t))
656 ;*---------------------------------------------------------------------*/
657 ;* flyspell-deplacement-commands ... */
658 ;*---------------------------------------------------------------------*/
659 (defun flyspell-deplacement-commands ()
660 "Install the standard set of Flyspell deplacement commands."
661 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
662 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
664 ;*---------------------------------------------------------------------*/
665 ;* flyspell-deplacement-command ... */
666 ;*---------------------------------------------------------------------*/
667 (defun flyspell-deplacement-command (command)
668 "Set COMMAND that implement cursor movements, for Flyspell.
669 When flyspell `post-command-hook' is invoked because of a deplacement command
670 as been used the current word is checked only if the previous command was
671 not the very same deplacement command."
672 (interactive "SDeplacement Flyspell after Command: ")
673 (put command 'flyspell-deplacement t))
675 ;*---------------------------------------------------------------------*/
676 ;* flyspell-word-cache ... */
677 ;*---------------------------------------------------------------------*/
678 (defvar flyspell-word-cache-start nil)
679 (defvar flyspell-word-cache-end nil)
680 (defvar flyspell-word-cache-word nil)
681 (defvar flyspell-word-cache-result '_)
682 (make-variable-buffer-local 'flyspell-word-cache-start)
683 (make-variable-buffer-local 'flyspell-word-cache-end)
684 (make-variable-buffer-local 'flyspell-word-cache-word)
685 (make-variable-buffer-local 'flyspell-word-cache-result)
687 ;*---------------------------------------------------------------------*/
688 ;* The flyspell pre-hook, store the current position. In the */
689 ;* post command hook, we will check, if the word at this position */
690 ;* has to be spell checked. */
691 ;*---------------------------------------------------------------------*/
692 (defvar flyspell-pre-buffer nil)
693 (defvar flyspell-pre-point nil)
694 (defvar flyspell-pre-column nil)
695 (defvar flyspell-pre-pre-buffer nil)
696 (defvar flyspell-pre-pre-point nil)
698 ;*---------------------------------------------------------------------*/
699 ;* flyspell-previous-command ... */
700 ;*---------------------------------------------------------------------*/
701 (defvar flyspell-previous-command nil
702 "The last interactive command checked by Flyspell.")
704 ;*---------------------------------------------------------------------*/
705 ;* flyspell-pre-command-hook ... */
706 ;*---------------------------------------------------------------------*/
707 (defun flyspell-pre-command-hook ()
708 "Save the current buffer and point for Flyspell's post-command hook."
710 (setq flyspell-pre-buffer (current-buffer))
711 (setq flyspell-pre-point (point))
712 (setq flyspell-pre-column (current-column)))
714 ;*---------------------------------------------------------------------*/
715 ;* flyspell-mode-off ... */
716 ;*---------------------------------------------------------------------*/
718 (defun flyspell-mode-off ()
719 "Turn Flyspell mode off."
720 ;; we remove the hooks
721 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
722 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
723 (setq after-change-functions (delq 'flyspell-after-change-function
724 after-change-functions))
725 ;; we remove all the flyspell hilightings
726 (flyspell-delete-all-overlays)
727 ;; we have to erase pre cache variables
728 (setq flyspell-pre-buffer nil)
729 (setq flyspell-pre-point nil)
730 ;; we mark the mode as killed
731 (setq flyspell-mode nil))
733 ;*---------------------------------------------------------------------*/
734 ;* flyspell-check-pre-word-p ... */
735 ;*---------------------------------------------------------------------*/
736 (defun flyspell-check-pre-word-p ()
737 "Return non-nil if we should check the word before point.
738 More precisely, it applies to the word that was before point
739 before the current command."
741 ((or (not (numberp flyspell-pre-point))
742 (not (bufferp flyspell-pre-buffer))
743 (not (buffer-live-p flyspell-pre-buffer)))
745 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
746 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
748 ((or (and (= flyspell-pre-point (- (point) 1))
749 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
750 (= flyspell-pre-point (point))
751 (= flyspell-pre-point (+ (point) 1)))
753 ((and (symbolp this-command)
754 (not executing-kbd-macro)
755 (or (get this-command 'flyspell-delayed)
756 (and (get this-command 'flyspell-deplacement)
757 (eq flyspell-previous-command this-command)))
758 (or (= (current-column) 0)
759 (= (current-column) flyspell-pre-column)
760 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
762 ((not (eq (current-buffer) flyspell-pre-buffer))
764 ((not (and (numberp flyspell-word-cache-start)
765 (numberp flyspell-word-cache-end)))
768 (or (< flyspell-pre-point flyspell-word-cache-start)
769 (> flyspell-pre-point flyspell-word-cache-end)))))
771 ;*---------------------------------------------------------------------*/
772 ;* The flyspell after-change-hook, store the change position. In */
773 ;* the post command hook, we will check, if the word at this */
774 ;* position has to be spell checked. */
775 ;*---------------------------------------------------------------------*/
776 (defvar flyspell-changes nil)
778 ;*---------------------------------------------------------------------*/
779 ;* flyspell-after-change-function ... */
780 ;*---------------------------------------------------------------------*/
781 (defun flyspell-after-change-function (start stop len)
782 "Save the current buffer and point for Flyspell's post-command hook."
784 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
786 ;*---------------------------------------------------------------------*/
787 ;* flyspell-check-changed-word-p ... */
788 ;*---------------------------------------------------------------------*/
789 (defun flyspell-check-changed-word-p (start stop)
790 "Return t when the changed word has to be checked.
791 The answer depends of several criteria.
792 Mostly we check word delimiters."
794 ((and (memq (char-after start) '(?\n ? )) (> stop start))
796 ((not (numberp flyspell-pre-point))
798 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
800 ((let ((pos (point)))
801 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
806 ;*---------------------------------------------------------------------*/
807 ;* flyspell-check-word-p ... */
808 ;*---------------------------------------------------------------------*/
809 (defun flyspell-check-word-p ()
810 "Return t when the word at `point' has to be checked.
811 The answer depends of several criteria.
812 Mostly we check word delimiters."
814 ((<= (- (point-max) 1) (point-min))
815 ;; the buffer is not filled enough
817 ((and (and (> (current-column) 0)
818 (not (eq (current-column) flyspell-pre-column)))
821 (and (looking-at (flyspell-get-not-casechars))
822 (or flyspell-consider-dash-as-word-delimiter-flag
823 (not (looking-at "\\-"))))))
824 ;; yes because we have reached or typed a word delimiter.
826 ((symbolp this-command)
828 ((get this-command 'flyspell-deplacement)
829 (not (eq flyspell-previous-command this-command)))
830 ((get this-command 'flyspell-delayed)
831 ;; the current command is not delayed, that
832 ;; is that we must check the word now
833 (if (fboundp 'about-xemacs)
834 (sit-for flyspell-delay nil)
835 (sit-for flyspell-delay 0 nil)))
839 ;*---------------------------------------------------------------------*/
840 ;* flyspell-debug-signal-no-check ... */
841 ;*---------------------------------------------------------------------*/
842 (defun flyspell-debug-signal-no-check (msg obj)
843 (setq debug-on-error t)
845 (let ((buffer (get-buffer-create "*flyspell-debug*")))
848 (insert "NO-CHECK:\n")
849 (insert (format " %S : %S\n" msg obj)))))
851 ;*---------------------------------------------------------------------*/
852 ;* flyspell-debug-signal-pre-word-checked ... */
853 ;*---------------------------------------------------------------------*/
854 (defun flyspell-debug-signal-pre-word-checked ()
855 (setq debug-on-error t)
857 (let ((buffer (get-buffer-create "*flyspell-debug*")))
859 (insert "PRE-WORD:\n")
860 (insert (format " pre-point : %S\n" flyspell-pre-point))
861 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
862 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
863 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
864 (goto-char (point-max)))))
866 ;*---------------------------------------------------------------------*/
867 ;* flyspell-debug-signal-word-checked ... */
868 ;*---------------------------------------------------------------------*/
869 (defun flyspell-debug-signal-word-checked ()
870 (setq debug-on-error t)
872 (let ((oldbuf (current-buffer))
873 (buffer (get-buffer-create "*flyspell-debug*"))
877 (insert (format " this-cmd : %S\n" this-command))
878 (insert (format " delayed : %S\n" (and (symbolp this-command)
879 (get this-command 'flyspell-delayed))))
880 (insert (format " point : %S\n" point))
881 (insert (format " prev-char : [%c] %S\n"
884 (let ((c (if (> (point) (point-min))
887 (char-after (point)))
893 (let ((c (if (> (point) (point-min))
896 (and (and (looking-at (flyspell-get-not-casechars)) 1)
897 (and (or flyspell-consider-dash-as-word-delimiter-flag
898 (not (looking-at "\\-"))) 2))))))
901 (insert (format " because : %S\n"
903 ((not (and (symbolp this-command)
904 (get this-command 'flyspell-delayed)))
905 ;; the current command is not delayed, that
906 ;; is that we must check the word now
910 (let ((c (if (> (point) (point-min))
913 (and (looking-at (flyspell-get-not-casechars))
914 (or flyspell-consider-dash-as-word-delimiter-flag
915 (not (looking-at "\\-"))))))))
918 ;; yes because we have reached or typed a word delimiter.
920 ((not (integerp flyspell-delay))
921 ;; yes because the user had set up a no-delay configuration.
925 (goto-char (point-max)))))
927 ;*---------------------------------------------------------------------*/
928 ;* flyspell-debug-signal-changed-checked ... */
929 ;*---------------------------------------------------------------------*/
930 (defun flyspell-debug-signal-changed-checked ()
931 (setq debug-on-error t)
933 (let ((buffer (get-buffer-create "*flyspell-debug*"))
936 (insert "CHANGED WORD:\n")
937 (insert (format " point : %S\n" point))
938 (goto-char (point-max)))))
940 ;*---------------------------------------------------------------------*/
941 ;* flyspell-post-command-hook ... */
942 ;* ------------------------------------------------------------- */
943 ;* It is possible that we check several words: */
944 ;* 1- the current word is checked if the predicate */
945 ;* FLYSPELL-CHECK-WORD-P is true */
946 ;* 2- the word that used to be the current word before the */
947 ;* THIS-COMMAND is checked if: */
948 ;* a- the previous word is different from the current word */
949 ;* b- the previous word as not just been checked by the */
950 ;* previous FLYSPELL-POST-COMMAND-HOOK */
951 ;* 3- the words changed by the THIS-COMMAND that are neither the */
952 ;* previous word nor the current word */
953 ;*---------------------------------------------------------------------*/
954 (defun flyspell-post-command-hook ()
955 "The `post-command-hook' used by flyspell to check a word in-the-fly."
957 (let ((command this-command))
958 (if (flyspell-check-pre-word-p)
960 '(flyspell-debug-signal-pre-word-checked)
961 (set-buffer flyspell-pre-buffer)
963 (goto-char flyspell-pre-point)
965 (if (flyspell-check-word-p)
967 '(flyspell-debug-signal-word-checked)
969 ;; we remember which word we have just checked.
970 ;; this will be used next time we will check a word
971 ;; to compare the next current word with the word
972 ;; that as been registered in the pre-command-hook
973 ;; that is these variables are used within the predicate
974 ;; FLYSPELL-CHECK-PRE-WORD-P
975 (setq flyspell-pre-pre-buffer (current-buffer))
976 (setq flyspell-pre-pre-point (point)))
978 (setq flyspell-pre-pre-buffer nil)
979 (setq flyspell-pre-pre-point nil)
980 ;; when a word is not checked because of a delayed command
981 ;; we do not disable the ispell cache.
982 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
984 (setq flyspell-word-cache-end -1)
985 (setq flyspell-word-cache-result '_)))))
986 (while (consp flyspell-changes)
987 (let ((start (car (car flyspell-changes)))
988 (stop (cdr (car flyspell-changes))))
989 (if (flyspell-check-changed-word-p start stop)
991 '(flyspell-debug-signal-changed-checked)
994 (setq flyspell-changes (cdr flyspell-changes))))
995 (setq flyspell-previous-command command)))
997 ;*---------------------------------------------------------------------*/
998 ;* flyspell-notify-misspell ... */
999 ;*---------------------------------------------------------------------*/
1000 (defun flyspell-notify-misspell (start end word poss)
1001 (let ((replacements (if (stringp poss)
1003 (if flyspell-sort-corrections
1004 (sort (car (cdr (cdr poss))) 'string<)
1005 (car (cdr (cdr poss)))))))
1006 (if flyspell-issue-message-flag
1007 (message (format "mispelling `%s' %S" word replacements)))))
1009 ;*---------------------------------------------------------------------*/
1010 ;* flyspell-word-search-backward ... */
1011 ;*---------------------------------------------------------------------*/
1012 (defun flyspell-word-search-backward (word bound)
1016 (while (and (not r) (setq p (search-backward word bound t)))
1017 (let ((lw (flyspell-get-word '())))
1018 (if (and (consp lw) (string-equal (car lw) word))
1023 ;*---------------------------------------------------------------------*/
1024 ;* flyspell-word-search-forward ... */
1025 ;*---------------------------------------------------------------------*/
1026 (defun flyspell-word-search-forward (word bound)
1030 (while (and (not r) (setq p (search-forward word bound t)))
1031 (let ((lw (flyspell-get-word '())))
1032 (if (and (consp lw) (string-equal (car lw) word))
1034 (goto-char (1+ p)))))
1037 ;*---------------------------------------------------------------------*/
1038 ;* flyspell-word ... */
1039 ;*---------------------------------------------------------------------*/
1040 (defun flyspell-word (&optional following)
1041 "Spell check a word."
1042 (interactive (list ispell-following-word))
1044 ;; use the correct dictionary
1045 (flyspell-accept-buffer-local-defs)
1046 (let* ((cursor-location (point))
1047 (flyspell-word (flyspell-get-word following))
1048 start end poss word)
1049 (if (or (eq flyspell-word nil)
1050 (and (fboundp flyspell-generic-check-word-p)
1051 (not (funcall flyspell-generic-check-word-p))))
1054 ;; destructure return flyspell-word info list.
1055 (setq start (car (cdr flyspell-word))
1056 end (car (cdr (cdr flyspell-word)))
1057 word (car flyspell-word))
1058 ;; before checking in the directory, we check for doublons.
1060 ((and (or (not (eq ispell-parser 'tex))
1061 (and (> start (point-min))
1062 (not (eq (char-after (1- start)) ?}))
1063 (not (eq (char-after (1- start)) ?\\))))
1064 flyspell-mark-duplications-flag
1066 (goto-char (1- start))
1067 (let ((p (flyspell-word-search-backward
1069 (- start (1+ (- end start))))))
1070 (and p (/= p (1- start))))))
1071 ;; yes, this is a doublon
1072 (flyspell-highlight-incorrect-region start end 'doublon)
1074 ((and (eq flyspell-word-cache-start start)
1075 (eq flyspell-word-cache-end end)
1076 (string-equal flyspell-word-cache-word word))
1077 ;; this word had been already checked, we skip
1078 flyspell-word-cache-result)
1079 ((and (eq ispell-parser 'tex)
1080 (flyspell-tex-command-p flyspell-word))
1081 ;; this is a correct word (because a tex command)
1082 (flyspell-unhighlight-at start)
1084 (flyspell-unhighlight-at (- end 1)))
1087 ;; we setup the cache
1088 (setq flyspell-word-cache-start start)
1089 (setq flyspell-word-cache-end end)
1090 (setq flyspell-word-cache-word word)
1091 ;; now check spelling of word.
1092 (process-send-string ispell-process "%\n")
1093 ;; put in verbose mode
1094 (process-send-string ispell-process
1095 (concat "^" word "\n"))
1096 ;; we mark the ispell process so it can be killed
1097 ;; when emacs is exited without query
1098 (set-process-query-on-exit-flag ispell-process nil)
1099 ;; wait until ispell has processed word
1101 (accept-process-output ispell-process)
1102 (not (string= "" (car ispell-filter)))))
1103 ;; (process-send-string ispell-process "!\n")
1104 ;; back to terse mode.
1105 (setq ispell-filter (cdr ispell-filter))
1106 (if (consp ispell-filter)
1107 (setq poss (ispell-parse-output (car ispell-filter))))
1108 (let ((res (cond ((eq poss t)
1110 (setq flyspell-word-cache-result t)
1111 (flyspell-unhighlight-at start)
1113 (flyspell-unhighlight-at (- end 1)))
1115 ((and (stringp poss) flyspell-highlight-flag)
1117 (setq flyspell-word-cache-result t)
1118 (flyspell-unhighlight-at start)
1120 (flyspell-unhighlight-at (- end 1)))
1123 (setq flyspell-word-cache-result t)
1124 (flyspell-unhighlight-at start)
1126 (flyspell-unhighlight-at (- end 1)))
1128 ((or (and (< flyspell-duplicate-distance 0)
1131 (flyspell-word-search-backward
1136 (flyspell-word-search-forward
1139 (and (> flyspell-duplicate-distance 0)
1142 (flyspell-word-search-backward
1145 flyspell-duplicate-distance)))
1148 (flyspell-word-search-forward
1151 flyspell-duplicate-distance))))))
1152 (setq flyspell-word-cache-result nil)
1153 (if flyspell-highlight-flag
1154 (flyspell-highlight-duplicate-region
1156 (message (format "duplicate `%s'" word)))
1159 (setq flyspell-word-cache-result nil)
1160 ;; incorrect highlight the location
1161 (if flyspell-highlight-flag
1162 (flyspell-highlight-incorrect-region
1164 (flyspell-notify-misspell start end word poss))
1166 ;; return to original location
1167 (goto-char cursor-location)
1168 (if ispell-quit (setq ispell-quit nil))
1171 ;*---------------------------------------------------------------------*/
1172 ;* flyspell-tex-math-initialized ... */
1173 ;*---------------------------------------------------------------------*/
1174 (defvar flyspell-tex-math-initialized nil)
1176 ;*---------------------------------------------------------------------*/
1177 ;* flyspell-math-tex-command-p ... */
1178 ;* ------------------------------------------------------------- */
1179 ;* This function uses the texmathp package to check if (point) */
1180 ;* is within a tex command. In order to avoid using */
1181 ;* condition-case each time we use the variable */
1182 ;* flyspell-tex-math-initialized to make a special case the first */
1183 ;* time that function is called. */
1184 ;*---------------------------------------------------------------------*/
1185 (defun flyspell-math-tex-command-p ()
1187 (flyspell-check-tex-math-command
1189 ((eq flyspell-tex-math-initialized t)
1191 ((eq flyspell-tex-math-initialized 'error)
1194 (setq flyspell-tex-math-initialized t)
1198 (setq flyspell-tex-math-initialized 'error)
1201 ;*---------------------------------------------------------------------*/
1202 ;* flyspell-tex-command-p ... */
1203 ;*---------------------------------------------------------------------*/
1204 (defun flyspell-tex-command-p (word)
1205 "Return t if WORD is a TeX command."
1207 (let ((b (car (cdr word))))
1208 (and (re-search-backward "\\\\" (- (point) 100) t)
1209 (or (= (match-end 0) b)
1210 (and (goto-char (match-end 0))
1211 (looking-at flyspell-tex-command-regexp)
1212 (>= (match-end 0) b))))))
1213 (flyspell-math-tex-command-p)))
1215 ;*---------------------------------------------------------------------*/
1216 ;* flyspell-casechars-cache ... */
1217 ;*---------------------------------------------------------------------*/
1218 (defvar flyspell-casechars-cache nil)
1219 (defvar flyspell-ispell-casechars-cache nil)
1220 (make-variable-buffer-local 'flyspell-casechars-cache)
1221 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1223 ;*---------------------------------------------------------------------*/
1224 ;* flyspell-get-casechars ... */
1225 ;*---------------------------------------------------------------------*/
1226 (defun flyspell-get-casechars ()
1227 "This function builds a string that is the regexp of word chars.
1228 In order to avoid one useless string construction,
1229 this function changes the last char of the `ispell-casechars' string."
1230 (let ((ispell-casechars (ispell-get-casechars)))
1232 ((eq ispell-parser 'tex)
1233 (setq flyspell-ispell-casechars-cache ispell-casechars)
1234 (setq flyspell-casechars-cache
1235 (concat (substring ispell-casechars
1237 (- (length ispell-casechars) 1))
1239 flyspell-casechars-cache)
1241 (setq flyspell-ispell-casechars-cache ispell-casechars)
1242 (setq flyspell-casechars-cache ispell-casechars)
1243 flyspell-casechars-cache))))
1245 ;*---------------------------------------------------------------------*/
1246 ;* flyspell-get-not-casechars-cache ... */
1247 ;*---------------------------------------------------------------------*/
1248 (defvar flyspell-not-casechars-cache nil)
1249 (defvar flyspell-ispell-not-casechars-cache nil)
1250 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1251 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1253 ;*---------------------------------------------------------------------*/
1254 ;* flyspell-get-not-casechars ... */
1255 ;*---------------------------------------------------------------------*/
1256 (defun flyspell-get-not-casechars ()
1257 "This function builds a string that is the regexp of non-word chars."
1258 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1260 ((eq ispell-parser 'tex)
1261 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1262 (setq flyspell-not-casechars-cache
1263 (concat (substring ispell-not-casechars
1265 (- (length ispell-not-casechars) 1))
1267 flyspell-not-casechars-cache)
1269 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1270 (setq flyspell-not-casechars-cache ispell-not-casechars)
1271 flyspell-not-casechars-cache))))
1273 ;*---------------------------------------------------------------------*/
1274 ;* flyspell-get-word ... */
1275 ;*---------------------------------------------------------------------*/
1276 (defun flyspell-get-word (following &optional extra-otherchars)
1277 "Return the word for spell-checking according to Ispell syntax.
1278 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1279 is non-nil when called interactively, then the following word
1280 \(rather than preceding\) is checked when the cursor is not over a word.
1281 Optional second argument contains otherchars that can be included in word
1284 Word syntax described by `flyspell-dictionary-alist' (which see)."
1285 (let* ((flyspell-casechars (flyspell-get-casechars))
1286 (flyspell-not-casechars (flyspell-get-not-casechars))
1287 (ispell-otherchars (ispell-get-otherchars))
1288 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1289 (word-regexp (concat flyspell-casechars
1291 (if (not (string= "" ispell-otherchars))
1292 (concat ispell-otherchars "?"))
1293 (if extra-otherchars
1294 (concat extra-otherchars "?"))
1297 (if (or ispell-many-otherchars-p
1303 (if (not (looking-at flyspell-casechars))
1305 (re-search-forward flyspell-casechars (point-max) t)
1306 (re-search-backward flyspell-casechars (point-min) t)))
1307 ;; move to front of word
1308 (re-search-backward flyspell-not-casechars (point-min) 'start)
1309 (while (and (or (and (not (string= "" ispell-otherchars))
1310 (looking-at ispell-otherchars))
1311 (and extra-otherchars (looking-at extra-otherchars)))
1313 (or (not did-it-once)
1314 ispell-many-otherchars-p)
1315 (not (eq prevpt (point))))
1316 (if (and extra-otherchars (looking-at extra-otherchars))
1319 (if (looking-at flyspell-casechars)
1320 (re-search-backward flyspell-not-casechars (point-min) 'move)))
1324 (if (looking-at flyspell-casechars)
1325 (re-search-backward flyspell-not-casechars (point-min) 'move)
1326 (backward-char -1))))
1327 ;; Now mark the word and save to string.
1328 (if (not (re-search-forward word-regexp (point-max) t))
1331 (setq start (match-beginning 0)
1333 word (buffer-substring-no-properties start end))
1334 (list word start end)))))
1336 ;*---------------------------------------------------------------------*/
1337 ;* flyspell-small-region ... */
1338 ;*---------------------------------------------------------------------*/
1339 (defun flyspell-small-region (beg end)
1340 "Flyspell text between BEG and END."
1348 (while (< (point) end)
1349 (if (and flyspell-issue-message-flag (= count 100))
1351 (message "Spell Checking...%d%%"
1352 (* 100 (/ (float (- (point) beg)) (- end beg))))
1354 (setq count (+ 1 count)))
1357 (let ((cur (point)))
1359 (if (and (< (point) end) (> (point) (+ cur 1)))
1360 (backward-char 1)))))
1362 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1365 ;*---------------------------------------------------------------------*/
1366 ;* flyspell-external-ispell-process ... */
1367 ;*---------------------------------------------------------------------*/
1368 (defvar flyspell-external-ispell-process '()
1369 "The external Flyspell Ispell process.")
1371 ;*---------------------------------------------------------------------*/
1372 ;* flyspell-external-ispell-buffer ... */
1373 ;*---------------------------------------------------------------------*/
1374 (defvar flyspell-external-ispell-buffer '())
1375 (defvar flyspell-large-region-buffer '())
1376 (defvar flyspell-large-region-beg (point-min))
1377 (defvar flyspell-large-region-end (point-max))
1379 ;*---------------------------------------------------------------------*/
1380 ;* flyspell-external-point-words ... */
1381 ;*---------------------------------------------------------------------*/
1382 (defun flyspell-external-point-words ()
1383 (let ((buffer flyspell-external-ispell-buffer))
1385 (goto-char (point-min))
1386 (let ((size (- flyspell-large-region-end flyspell-large-region-beg))
1387 (start flyspell-large-region-beg)
1390 ;; now we are done with ispell, we have to find the word in
1391 ;; the initial buffer
1392 (while (< (point) (- (point-max) 1))
1393 ;; we have to fetch the incorrect word
1394 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1395 (let ((word (match-string 1)))
1396 (if (string= word pword)
1397 (setq pcount (1+ pcount))
1401 (goto-char (match-end 0))
1402 (if flyspell-issue-message-flag
1403 (message "Spell Checking...%d%% [%s]"
1404 (* 100 (/ (float (point)) (point-max)))
1406 (set-buffer flyspell-large-region-buffer)
1407 (goto-char flyspell-large-region-beg)
1410 (while (and (or (< n pcount) keep)
1411 (search-forward word flyspell-large-region-end t))
1413 (goto-char (- (point) 1))
1415 (setq keep (flyspell-word))))
1417 (setq flyspell-large-region-beg (point))))
1418 (set-buffer buffer))
1419 (goto-char (point-max)))))
1421 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1422 ;; ok, we are done with pointing out incorrect words, we just
1423 ;; have to kill the temporary buffer
1424 (kill-buffer flyspell-external-ispell-buffer)
1425 (setq flyspell-external-ispell-buffer nil)))
1427 ;*---------------------------------------------------------------------*/
1428 ;* flyspell-large-region ... */
1429 ;*---------------------------------------------------------------------*/
1430 (defun flyspell-large-region (beg end)
1431 (let* ((curbuf (current-buffer))
1432 (buffer (get-buffer-create "*flyspell-region*")))
1433 (setq flyspell-external-ispell-buffer buffer)
1434 (setq flyspell-large-region-buffer curbuf)
1435 (setq flyspell-large-region-beg beg)
1436 (setq flyspell-large-region-end end)
1439 ;; this is done, we can start checking...
1440 (if flyspell-issue-message-flag (message "Checking region..."))
1442 (let ((c (apply 'call-process-region beg
1448 (if ispell-really-aspell "list" "-l")
1450 ;; Local dictionary becomes the global dictionary in use.
1451 (if ispell-local-dictionary
1452 (setq ispell-dictionary ispell-local-dictionary))
1453 (setq args (ispell-get-ispell-args))
1454 (if ispell-dictionary ; use specified dictionary
1456 (append (list "-d" ispell-dictionary) args)))
1457 (if ispell-personal-dictionary ; use specified pers dict
1462 ispell-personal-dictionary)))))
1463 (setq args (append args ispell-extra-args))
1466 (flyspell-external-point-words)
1467 (error "Can't check region...")))))
1469 ;*---------------------------------------------------------------------*/
1470 ;* flyspell-region ... */
1471 ;* ------------------------------------------------------------- */
1472 ;* Because `ispell -a' is too slow, it is not possible to use */
1473 ;* it on large region. Then, when ispell is invoked on a large */
1474 ;* text region, a new `ispell -l' process is spawned. The */
1475 ;* pointed out words are then searched in the region a checked with */
1476 ;* regular flyspell means. */
1477 ;*---------------------------------------------------------------------*/
1479 (defun flyspell-region (beg end)
1480 "Flyspell text between BEG and END."
1489 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1490 (flyspell-large-region beg end)
1491 (flyspell-small-region beg end)))))
1493 ;*---------------------------------------------------------------------*/
1494 ;* flyspell-buffer ... */
1495 ;*---------------------------------------------------------------------*/
1497 (defun flyspell-buffer ()
1498 "Flyspell whole buffer."
1500 (flyspell-region (point-min) (point-max)))
1502 ;*---------------------------------------------------------------------*/
1503 ;* old next error position ... */
1504 ;*---------------------------------------------------------------------*/
1505 (defvar flyspell-old-buffer-error nil)
1506 (defvar flyspell-old-pos-error nil)
1508 ;*---------------------------------------------------------------------*/
1509 ;* flyspell-goto-next-error ... */
1510 ;*---------------------------------------------------------------------*/
1511 (defun flyspell-goto-next-error ()
1512 "Go to the next previously detected error.
1513 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1518 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1519 (eq pos flyspell-old-pos-error))
1521 (if (= flyspell-old-pos-error max)
1522 ;; goto beginning of buffer
1524 (message "Restarting from beginning of buffer")
1525 (goto-char (point-min)))
1527 (setq pos (point))))
1528 ;; seek the next error
1529 (while (and (< pos max)
1530 (let ((ovs (overlays-at pos))
1532 (while (and (not r) (consp ovs))
1533 (if (flyspell-overlay-p (car ovs))
1535 (setq ovs (cdr ovs))))
1537 (setq pos (1+ pos)))
1538 ;; save the current location for next invocation
1539 (setq flyspell-old-pos-error pos)
1540 (setq flyspell-old-buffer-error (current-buffer))
1543 (message "No more miss-spelled word!"))))
1545 ;*---------------------------------------------------------------------*/
1546 ;* flyspell-overlay-p ... */
1547 ;*---------------------------------------------------------------------*/
1548 (defun flyspell-overlay-p (o)
1549 "A predicate that return true iff O is an overlay used by flyspell."
1550 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1552 ;*---------------------------------------------------------------------*/
1553 ;* flyspell-delete-all-overlays ... */
1554 ;* ------------------------------------------------------------- */
1555 ;* Remove all the overlays introduced by flyspell. */
1556 ;*---------------------------------------------------------------------*/
1557 (defun flyspell-delete-all-overlays ()
1558 "Delete all the overlays used by flyspell."
1559 (let ((l (overlays-in (point-min) (point-max))))
1562 (if (flyspell-overlay-p (car l))
1563 (delete-overlay (car l)))
1564 (setq l (cdr l))))))
1566 ;*---------------------------------------------------------------------*/
1567 ;* flyspell-unhighlight-at ... */
1568 ;*---------------------------------------------------------------------*/
1569 (defun flyspell-unhighlight-at (pos)
1570 "Remove the flyspell overlay that are located at POS."
1571 (if flyspell-persistent-highlight
1572 (let ((overlays (overlays-at pos)))
1573 (while (consp overlays)
1574 (if (flyspell-overlay-p (car overlays))
1575 (delete-overlay (car overlays)))
1576 (setq overlays (cdr overlays))))
1577 (if (flyspell-overlay-p flyspell-overlay)
1578 (delete-overlay flyspell-overlay))))
1580 ;*---------------------------------------------------------------------*/
1581 ;* flyspell-properties-at-p ... */
1582 ;* ------------------------------------------------------------- */
1583 ;* Is there an highlight properties at position pos? */
1584 ;*---------------------------------------------------------------------*/
1585 (defun flyspell-properties-at-p (pos)
1586 "Return t if there is a text property at POS, not counting `local-map'.
1587 If variable `flyspell-highlight-properties' is set to nil,
1588 text with properties are not checked. This function is used to discover
1589 if the character at POS has any other property."
1590 (let ((prop (text-properties-at pos))
1592 (while (and keep (consp prop))
1593 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1594 (setq prop (cdr (cdr prop)))
1598 ;*---------------------------------------------------------------------*/
1599 ;* make-flyspell-overlay ... */
1600 ;*---------------------------------------------------------------------*/
1601 (defun make-flyspell-overlay (beg end face mouse-face)
1602 "Allocate an overlay to highlight an incorrect word.
1603 BEG and END specify the range in the buffer of that word.
1604 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1606 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1607 (overlay-put flyspell-overlay 'face face)
1608 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1609 (overlay-put flyspell-overlay 'flyspell-overlay t)
1610 (overlay-put flyspell-overlay 'evaporate t)
1611 (overlay-put flyspell-overlay 'help-echo "mouse-2: correct word at point")
1612 (if flyspell-use-local-map
1613 (overlay-put flyspell-overlay
1614 flyspell-overlay-keymap-property-name
1615 flyspell-mouse-map))
1616 (when (eq face 'flyspell-incorrect-face)
1617 (and (stringp flyspell-before-incorrect-word-string)
1618 (overlay-put flyspell-overlay 'before-string
1619 flyspell-before-incorrect-word-string))
1620 (and (stringp flyspell-after-incorrect-word-string)
1621 (overlay-put flyspell-overlay 'after-string
1622 flyspell-after-incorrect-word-string)))
1625 ;*---------------------------------------------------------------------*/
1626 ;* flyspell-highlight-incorrect-region ... */
1627 ;*---------------------------------------------------------------------*/
1628 (defun flyspell-highlight-incorrect-region (beg end poss)
1629 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1630 (let ((inhibit-read-only t))
1631 (unless (run-hook-with-args-until-success
1632 'flyspell-incorrect-hook beg end poss)
1633 (if (or flyspell-highlight-properties
1634 (not (flyspell-properties-at-p beg)))
1636 ;; we cleanup all the overlay that are in the region, not
1637 ;; beginning at the word start position
1638 (if (< (1+ beg) end)
1639 (let ((os (overlays-in (1+ beg) end)))
1641 (if (flyspell-overlay-p (car os))
1642 (delete-overlay (car os)))
1643 (setq os (cdr os)))))
1644 ;; we cleanup current overlay at the same position
1645 (if (and (not flyspell-persistent-highlight)
1646 (overlayp flyspell-overlay))
1647 (delete-overlay flyspell-overlay)
1648 (let ((os (overlays-at beg)))
1650 (if (flyspell-overlay-p (car os))
1651 (delete-overlay (car os)))
1652 (setq os (cdr os)))))
1653 ;; now we can use a new overlay
1654 (setq flyspell-overlay
1655 (make-flyspell-overlay
1656 beg end 'flyspell-incorrect-face 'highlight)))))))
1658 ;*---------------------------------------------------------------------*/
1659 ;* flyspell-highlight-duplicate-region ... */
1660 ;*---------------------------------------------------------------------*/
1661 (defun flyspell-highlight-duplicate-region (beg end poss)
1662 "Set up an overlay on a duplicated word, in the buffer from BEG to END.
1663 ??? What does POSS mean?"
1664 (let ((inhibit-read-only t))
1665 (unless (run-hook-with-args-until-success
1666 'flyspell-incorrect-hook beg end poss)
1667 (if (or flyspell-highlight-properties
1668 (not (flyspell-properties-at-p beg)))
1670 ;; we cleanup current overlay at the same position
1671 (if (and (not flyspell-persistent-highlight)
1672 (overlayp flyspell-overlay))
1673 (delete-overlay flyspell-overlay)
1674 (let ((overlays (overlays-at beg)))
1675 (while (consp overlays)
1676 (if (flyspell-overlay-p (car overlays))
1677 (delete-overlay (car overlays)))
1678 (setq overlays (cdr overlays)))))
1679 ;; now we can use a new overlay
1680 (setq flyspell-overlay
1681 (make-flyspell-overlay beg end
1682 'flyspell-duplicate-face
1685 ;*---------------------------------------------------------------------*/
1686 ;* flyspell-auto-correct-cache ... */
1687 ;*---------------------------------------------------------------------*/
1688 (defvar flyspell-auto-correct-pos nil)
1689 (defvar flyspell-auto-correct-region nil)
1690 (defvar flyspell-auto-correct-ring nil)
1691 (defvar flyspell-auto-correct-word nil)
1692 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1693 (make-variable-buffer-local 'flyspell-auto-correct-region)
1694 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1695 (make-variable-buffer-local 'flyspell-auto-correct-word)
1697 ;*---------------------------------------------------------------------*/
1698 ;* flyspell-check-previous-highlighted-word ... */
1699 ;*---------------------------------------------------------------------*/
1700 (defun flyspell-check-previous-highlighted-word (&optional arg)
1701 "Correct the closer misspelled word.
1702 This function scans a mis-spelled word before the cursor. If it finds one
1703 it proposes replacement for that word. With prefix arg, count that many
1704 misspelled words backwards."
1706 (let ((pos1 (point))
1708 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1711 (while (and (setq pos (previous-overlay-change pos))
1714 (if (> pos (point-min))
1716 (setq ovs (overlays-at (1- pos)))
1719 (setq ovs (cdr ovs))
1720 (if (and (overlay-get ov 'flyspell-overlay)
1721 (= 0 (setq arg (1- arg))))
1722 (throw 'exit t)))))))
1726 (error "No word to correct before point"))))
1728 ;*---------------------------------------------------------------------*/
1729 ;* flyspell-display-next-corrections ... */
1730 ;*---------------------------------------------------------------------*/
1731 (defun flyspell-display-next-corrections (corrections)
1732 (let ((string "Corrections:")
1735 (while (< (length string) 80)
1736 (if (equal (car l) flyspell-auto-correct-word)
1737 (setq pos (cons (+ 1 (length string)) pos)))
1738 (setq string (concat string " " (car l)))
1741 (let ((num (car pos)))
1742 (put-text-property num
1743 (+ num (length flyspell-auto-correct-word))
1745 'flyspell-incorrect-face
1747 (setq pos (cdr pos)))
1748 (if (fboundp 'display-message)
1749 (display-message 'no-log string)
1752 ;*---------------------------------------------------------------------*/
1753 ;* flyspell-abbrev-table ... */
1754 ;*---------------------------------------------------------------------*/
1755 (defun flyspell-abbrev-table ()
1756 (if flyspell-use-global-abbrev-table-p
1758 (or local-abbrev-table global-abbrev-table)))
1760 ;*---------------------------------------------------------------------*/
1761 ;* flyspell-define-abbrev ... */
1762 ;*---------------------------------------------------------------------*/
1763 (defun flyspell-define-abbrev (name expansion)
1764 (let ((table (flyspell-abbrev-table)))
1766 (define-abbrev table name expansion))))
1768 ;*---------------------------------------------------------------------*/
1769 ;* flyspell-auto-correct-word ... */
1770 ;*---------------------------------------------------------------------*/
1771 (defun flyspell-auto-correct-word ()
1772 "Correct the current word.
1773 This command proposes various successive corrections for the current word."
1776 (old-max (point-max)))
1777 ;; use the correct dictionary
1778 (flyspell-accept-buffer-local-defs)
1779 (if (and (eq flyspell-auto-correct-pos pos)
1780 (consp flyspell-auto-correct-region))
1781 ;; we have already been using the function at the same location
1782 (let* ((start (car flyspell-auto-correct-region))
1783 (len (cdr flyspell-auto-correct-region)))
1784 (flyspell-unhighlight-at start)
1785 (delete-region start (+ start len))
1786 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1787 (let* ((word (car flyspell-auto-correct-ring))
1788 (len (length word)))
1789 (rplacd flyspell-auto-correct-region len)
1791 (if flyspell-abbrev-p
1792 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1793 flyspell-auto-correct-word)
1794 (flyspell-change-abbrev (flyspell-abbrev-table)
1795 flyspell-auto-correct-word
1797 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1798 (funcall flyspell-insert-function word)
1800 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1801 (flyspell-ajust-cursor-point pos (point) old-max)
1802 (setq flyspell-auto-correct-pos (point)))
1803 ;; fetch the word to be checked
1804 (let ((word (flyspell-get-word nil)))
1806 (let ((start (car (cdr word)))
1807 (end (car (cdr (cdr word))))
1810 (setq flyspell-auto-correct-word word)
1811 ;; now check spelling of word.
1812 (process-send-string ispell-process "%\n") ;put in verbose mode
1813 (process-send-string ispell-process (concat "^" word "\n"))
1814 ;; wait until ispell has processed word
1816 (accept-process-output ispell-process)
1817 (not (string= "" (car ispell-filter)))))
1818 (setq ispell-filter (cdr ispell-filter))
1819 (if (consp ispell-filter)
1820 (setq poss (ispell-parse-output (car ispell-filter))))
1822 ((or (eq poss t) (stringp poss))
1823 ;; don't correct word
1827 (error "Ispell: error in Ispell process"))
1829 ;; the word is incorrect, we have to propose a replacement
1830 (let ((replacements (if flyspell-sort-corrections
1831 (sort (car (cdr (cdr poss))) 'string<)
1832 (car (cdr (cdr poss))))))
1833 (setq flyspell-auto-correct-region nil)
1834 (if (consp replacements)
1836 (let ((replace (car replacements)))
1837 (let ((new-word replace))
1838 (if (not (equal new-word (car poss)))
1840 ;; the save the current replacements
1841 (setq flyspell-auto-correct-region
1842 (cons start (length new-word)))
1843 (let ((l replacements))
1844 (while (consp (cdr l))
1846 (rplacd l (cons (car poss) replacements)))
1847 (setq flyspell-auto-correct-ring
1849 (flyspell-unhighlight-at start)
1850 (delete-region start end)
1851 (funcall flyspell-insert-function new-word)
1852 (if flyspell-abbrev-p
1853 (if (flyspell-already-abbrevp
1854 (flyspell-abbrev-table) word)
1855 (flyspell-change-abbrev
1856 (flyspell-abbrev-table)
1859 (flyspell-define-abbrev word
1862 (flyspell-display-next-corrections
1863 (cons new-word flyspell-auto-correct-ring))
1864 (flyspell-ajust-cursor-point pos
1867 (setq flyspell-auto-correct-pos (point))
1868 (ispell-pdict-save t)))))))
1870 ;*---------------------------------------------------------------------*/
1871 ;* flyspell-auto-correct-previous-pos ... */
1872 ;*---------------------------------------------------------------------*/
1873 (defvar flyspell-auto-correct-previous-pos nil
1874 "Holds the start of the first incorrect word before point.")
1876 ;*---------------------------------------------------------------------*/
1877 ;* flyspell-auto-correct-previous-hook ... */
1878 ;*---------------------------------------------------------------------*/
1879 (defun flyspell-auto-correct-previous-hook ()
1880 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1881 Sets `flyspell-auto-correct-previous-pos' to nil"
1883 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1884 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1885 (setq flyspell-auto-correct-previous-pos nil)))
1887 ;*---------------------------------------------------------------------*/
1888 ;* flyspell-auto-correct-previous-word ... */
1889 ;*---------------------------------------------------------------------*/
1890 (defun flyspell-auto-correct-previous-word (position)
1891 "*Auto correct the first mispelled word that occurs before point.
1892 But don't look beyond what's visible on the screen."
1897 (move-to-window-line 0)
1899 (move-to-window-line -1)
1903 (narrow-to-region top bot)
1904 (overlay-recenter (point))
1906 (add-hook 'pre-command-hook
1907 (function flyspell-auto-correct-previous-hook) t t)
1909 (unless flyspell-auto-correct-previous-pos
1910 ;; only reset if a new overlay exists
1911 (setq flyspell-auto-correct-previous-pos nil)
1913 (let ((overlay-list (overlays-in (point-min) position))
1914 (new-overlay 'dummy-value))
1916 ;; search for previous (new) flyspell overlay
1917 (while (and new-overlay
1918 (or (not (flyspell-overlay-p new-overlay))
1919 ;; check if its face has changed
1920 (not (eq (get-char-property
1921 (overlay-start new-overlay) 'face)
1922 'flyspell-incorrect-face))))
1923 (setq new-overlay (car-safe overlay-list))
1924 (setq overlay-list (cdr-safe overlay-list)))
1926 ;; if nothing new exits new-overlay should be nil
1927 (if new-overlay ;; the length of the word may change so go to the start
1928 (setq flyspell-auto-correct-previous-pos
1929 (overlay-start new-overlay)))))
1931 (when flyspell-auto-correct-previous-pos
1933 (goto-char flyspell-auto-correct-previous-pos)
1934 (let ((ispell-following-word t)) ;; point is at start
1935 (if (numberp flyspell-auto-correct-previous-pos)
1936 (goto-char flyspell-auto-correct-previous-pos))
1937 (flyspell-auto-correct-word))
1938 ;; the point may have moved so reset this
1939 (setq flyspell-auto-correct-previous-pos (point))))))))
1941 ;*---------------------------------------------------------------------*/
1942 ;* flyspell-correct-word ... */
1943 ;*---------------------------------------------------------------------*/
1944 (defun flyspell-correct-word (event)
1945 "Pop up a menu of possible corrections for a misspelled word.
1946 The word checked is the word at the mouse position."
1948 ;; use the correct dictionary
1949 (flyspell-accept-buffer-local-defs)
1950 ;; retain cursor location (I don't know why but save-excursion here fails).
1951 (let ((save (point)))
1952 (mouse-set-point event)
1953 (let ((cursor-location (point))
1954 (word (flyspell-get-word nil)))
1956 (let ((start (car (cdr word)))
1957 (end (car (cdr (cdr word))))
1960 ;; now check spelling of word.
1961 (process-send-string ispell-process "%\n") ;put in verbose mode
1962 (process-send-string ispell-process (concat "^" word "\n"))
1963 ;; wait until ispell has processed word
1965 (accept-process-output ispell-process)
1966 (not (string= "" (car ispell-filter)))))
1967 (setq ispell-filter (cdr ispell-filter))
1968 (if (consp ispell-filter)
1969 (setq poss (ispell-parse-output (car ispell-filter))))
1971 ((or (eq poss t) (stringp poss))
1972 ;; don't correct word
1976 (error "Ispell: error in Ispell process"))
1977 ((string-match "GNU" (emacs-version))
1978 ;; the word is incorrect, we have to propose a replacement
1979 (setq replace (flyspell-emacs-popup event poss word))
1980 (cond ((eq replace 'ignore)
1985 (process-send-string ispell-process
1986 (concat "*" word "\n"))
1987 (flyspell-unhighlight-at cursor-location)
1988 (setq ispell-pdict-modified-p '(t)))
1989 ((or (eq replace 'buffer) (eq replace 'session))
1990 (process-send-string ispell-process
1991 (concat "@" word "\n"))
1992 (if (null ispell-pdict-modified-p)
1993 (setq ispell-pdict-modified-p
1994 (list ispell-pdict-modified-p)))
1995 (flyspell-unhighlight-at cursor-location)
1997 (if (eq replace 'buffer)
1998 (ispell-add-per-file-word-list word)))
2000 (flyspell-unhighlight-at cursor-location)
2001 (let ((new-word (if (atom replace)
2005 (+ (- (length word) (- end start))
2007 (if (not (equal new-word (car poss)))
2008 (let ((old-max (point-max)))
2009 (delete-region start end)
2010 (funcall flyspell-insert-function new-word)
2011 (if flyspell-abbrev-p
2012 (flyspell-define-abbrev word new-word))
2013 (flyspell-ajust-cursor-point save
2019 ((eq flyspell-emacs 'xemacs)
2020 (flyspell-xemacs-popup
2021 event poss word cursor-location start end save)
2023 (ispell-pdict-save t))))))
2025 ;*---------------------------------------------------------------------*/
2026 ;* flyspell-xemacs-correct ... */
2027 ;*---------------------------------------------------------------------*/
2028 (defun flyspell-xemacs-correct (replace poss word cursor-location start end save)
2029 "The xemacs popup menu callback."
2030 (cond ((eq replace 'ignore)
2033 (process-send-string ispell-process (concat "*" word "\n"))
2034 (process-send-string ispell-process "#\n")
2035 (flyspell-unhighlight-at cursor-location)
2036 (setq ispell-pdict-modified-p '(t)))
2037 ((or (eq replace 'buffer) (eq replace 'session))
2038 (process-send-string ispell-process (concat "@" word "\n"))
2039 (flyspell-unhighlight-at cursor-location)
2040 (if (null ispell-pdict-modified-p)
2041 (setq ispell-pdict-modified-p
2042 (list ispell-pdict-modified-p)))
2043 (if (eq replace 'buffer)
2044 (ispell-add-per-file-word-list word)))
2046 (let ((old-max (point-max))
2047 (new-word (if (atom replace)
2050 (cursor-location (+ (- (length word) (- end start))
2052 (if (not (equal new-word (car poss)))
2054 (delete-region start end)
2056 (funcall flyspell-insert-function new-word)
2057 (if flyspell-abbrev-p
2058 (flyspell-define-abbrev word new-word))))
2059 (flyspell-ajust-cursor-point save cursor-location old-max)))))
2061 ;*---------------------------------------------------------------------*/
2062 ;* flyspell-ajust-cursor-point ... */
2063 ;*---------------------------------------------------------------------*/
2064 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2065 (if (>= save cursor-location)
2066 (let ((new-pos (+ save (- (point-max) old-max))))
2068 ((< new-pos (point-min))
2070 ((> new-pos (point-max))
2075 ;*---------------------------------------------------------------------*/
2076 ;* flyspell-emacs-popup ... */
2077 ;*---------------------------------------------------------------------*/
2078 (defun flyspell-emacs-popup (event poss word)
2079 "The Emacs popup menu."
2081 (let* ((mouse-pos (mouse-position))
2082 (mouse-pos (if (nth 1 mouse-pos)
2084 (set-mouse-position (car mouse-pos)
2085 (/ (frame-width) 2) 2)
2087 (setq event (list (list (car (cdr mouse-pos))
2088 (1+ (cdr (cdr mouse-pos))))
2090 (let* ((corrects (if flyspell-sort-corrections
2091 (sort (car (cdr (cdr poss))) 'string<)
2092 (car (cdr (cdr poss)))))
2093 (cor-menu (if (consp corrects)
2094 (mapcar (lambda (correct)
2095 (list correct correct))
2098 (affix (car (cdr (cdr (cdr poss)))))
2099 (base-menu (let ((save (if (consp affix)
2101 (list (concat "Save affix: " (car affix))
2103 '("Accept (session)" session)
2104 '("Accept (buffer)" buffer))
2105 '(("Save word" save)
2106 ("Accept (session)" session)
2107 ("Accept (buffer)" buffer)))))
2108 (if (consp cor-menu)
2109 (append cor-menu (cons "" save))
2111 (menu (cons "flyspell correction menu" base-menu)))
2112 (car (x-popup-menu event
2113 (list (format "%s [%s]" word (or ispell-local-dictionary
2117 ;*---------------------------------------------------------------------*/
2118 ;* flyspell-xemacs-popup ... */
2119 ;*---------------------------------------------------------------------*/
2120 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
2121 "The XEmacs popup menu."
2122 (let* ((corrects (if flyspell-sort-corrections
2123 (sort (car (cdr (cdr poss))) 'string<)
2124 (car (cdr (cdr poss)))))
2125 (cor-menu (if (consp corrects)
2126 (mapcar (lambda (correct)
2128 (list 'flyspell-xemacs-correct
2139 (affix (car (cdr (cdr (cdr poss)))))
2140 (menu (let ((save (if (consp affix)
2142 (concat "Save affix: " (car affix))
2143 (list 'flyspell-xemacs-correct
2154 (list 'flyspell-xemacs-correct
2163 (session (vector "Accept (session)"
2164 (list 'flyspell-xemacs-correct
2173 (buffer (vector "Accept (buffer)"
2174 (list 'flyspell-xemacs-correct
2183 (if (consp cor-menu)
2184 (append cor-menu (list "-" save session buffer))
2185 (list save session buffer)))))
2186 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2190 ;*---------------------------------------------------------------------*/
2191 ;* Some example functions for real autocorrecting */
2192 ;*---------------------------------------------------------------------*/
2193 (defun flyspell-maybe-correct-transposition (beg end poss)
2194 "Check replacements for transposed characters.
2196 If the text between BEG and END is equal to a correction suggested by
2197 Ispell, after transposing two adjacent characters, correct the text,
2200 The third arg POSS is either the symbol 'doublon' or a list of
2201 possible corrections as returned by 'ispell-parse-output'.
2203 This function is meant to be added to 'flyspell-incorrect-hook'."
2206 (let ((str (buffer-substring beg end))
2207 (i 0) (len (- end beg)) tmp)
2208 (while (< (1+ i) len)
2209 (setq tmp (aref str i))
2210 (aset str i (aref str (1+ i)))
2211 (aset str (1+ i) tmp)
2212 (when (member str (nth 2 poss))
2214 (goto-char (+ beg i 1))
2215 (transpose-chars 1))
2217 (setq tmp (aref str i))
2218 (aset str i (aref str (1+ i)))
2219 (aset str (1+ i) tmp)
2223 (defun flyspell-maybe-correct-doubling (beg end poss)
2224 "Check replacements for doubled characters.
2226 If the text between BEG and END is equal to a correction suggested by
2227 Ispell, after removing a pair of doubled characters, correct the text,
2230 The third arg POSS is either the symbol 'doublon' or a list of
2231 possible corrections as returned by 'ispell-parse-output'.
2233 This function is meant to be added to 'flyspell-incorrect-hook'."
2236 (let ((str (buffer-substring beg end))
2237 (i 0) (len (- end beg)))
2238 (while (< (1+ i) len)
2239 (when (and (= (aref str i) (aref str (1+ i)))
2240 (member (concat (substring str 0 (1+ i))
2241 (substring str (+ i 2)))
2243 (goto-char (+ beg i))
2249 ;*---------------------------------------------------------------------*/
2250 ;* flyspell-already-abbrevp ... */
2251 ;*---------------------------------------------------------------------*/
2252 (defun flyspell-already-abbrevp (table word)
2253 (let ((sym (abbrev-symbol word table)))
2254 (and sym (symbolp sym))))
2256 ;*---------------------------------------------------------------------*/
2257 ;* flyspell-change-abbrev ... */
2258 ;*---------------------------------------------------------------------*/
2259 (defun flyspell-change-abbrev (table old new)
2260 (set (abbrev-symbol old table) new))
2264 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2265 ;;; flyspell.el ends here