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') 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."
231 (defcustom flyspell-mode-line-string " Fly"
232 "*String displayed on the modeline when flyspell is active.
233 Set this to nil if you don't want a modeline indicator."
235 :type '(choice string (const :tag "None" nil)))
237 (defcustom flyspell-large-region 1000
238 "*The threshold that determines if a region is small.
239 If the region is smaller than this number of characters,
240 `flyspell-region' checks the words sequentially using regular
241 flyspell methods. Else, if the region is large, a new Ispell process is
244 If `flyspell-large-region' is nil, all regions are treated as small."
247 :type '(choice number boolean))
249 (defcustom flyspell-insert-function (function insert)
250 "*Function for inserting word by flyspell upon correction."
254 (defcustom flyspell-before-incorrect-word-string nil
255 "String used to indicate an incorrect word starting."
257 :type '(choice string (const nil)))
259 (defcustom flyspell-after-incorrect-word-string nil
260 "String used to indicate an incorrect word ending."
262 :type '(choice string (const nil)))
264 (defcustom flyspell-use-meta-tab t
265 "*Non-nil means that flyspell uses META-TAB to correct word."
269 (defcustom flyspell-auto-correct-binding
271 "The key binding for flyspell auto correction."
274 ;*---------------------------------------------------------------------*/
275 ;* Mode specific options */
276 ;* ------------------------------------------------------------- */
277 ;* Mode specific options enable users to disable flyspell on */
278 ;* certain word depending of the emacs mode. For instance, when */
279 ;* using flyspell with mail-mode add the following expression */
280 ;* in your .emacs file: */
281 ;* (add-hook 'mail-mode */
282 ;* '(lambda () (setq flyspell-generic-check-word-p */
283 ;* 'mail-mode-flyspell-verify))) */
284 ;*---------------------------------------------------------------------*/
285 (defvar flyspell-generic-check-word-p nil
286 "Function providing per-mode customization over which words are flyspelled.
287 Returns t to continue checking, nil otherwise.
288 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
289 property of the major mode name.")
290 (make-variable-buffer-local 'flyspell-generic-check-word-p)
292 ;*--- mail mode -------------------------------------------------------*/
293 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
294 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
295 (defun mail-mode-flyspell-verify ()
296 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
297 (let ((header-end (save-excursion
298 (goto-char (point-min))
301 (regexp-quote 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)
389 (run-hooks 'flyspell-prog-mode-hook))
391 ;*---------------------------------------------------------------------*/
392 ;* Overlay compatibility */
393 ;*---------------------------------------------------------------------*/
394 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
395 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
396 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
397 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
398 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
399 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
400 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
401 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
403 ;*---------------------------------------------------------------------*/
404 ;* The minor mode declaration. */
405 ;*---------------------------------------------------------------------*/
406 (eval-when-compile (defvar flyspell-local-mouse-map))
408 (defvar flyspell-mouse-map
409 (let ((map (make-sparse-keymap)))
410 (if flyspell-use-meta-tab
411 (define-key map "\M-\t" #'flyspell-auto-correct-word))
412 (define-key map (if (featurep 'xemacs) [button2] [down-mouse-2])
413 #'flyspell-correct-word)
414 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
415 (define-key map [(control \,)] 'flyspell-goto-next-error)
416 (define-key map [(control \.)] 'flyspell-auto-correct-word)
419 (defvar flyspell-mode-map
420 (let ((map (make-sparse-keymap)))
421 ;; mouse, keyboard bindings and misc definition
422 (if flyspell-use-meta-tab
423 (define-key map "\M-\t" 'flyspell-auto-correct-word))
425 ;; I don't understand this test, so I left it as is. --Stef
426 ((or (featurep 'xemacs) flyspell-use-local-map)
427 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
428 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
429 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)))
432 ;; the name of the overlay property that defines the keymap
433 (defvar flyspell-overlay-keymap-property-name 'keymap)
435 ;; dash character machinery
436 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
437 "*Non-nil means that the `-' char is considered as a word delimiter.")
438 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
439 (defvar flyspell-dash-dictionary nil)
440 (make-variable-buffer-local 'flyspell-dash-dictionary)
441 (defvar flyspell-dash-local-dictionary nil)
442 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
444 ;*---------------------------------------------------------------------*/
446 ;*---------------------------------------------------------------------*/
447 (defface flyspell-incorrect
448 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
450 "Face used for marking a misspelled word in Flyspell."
452 ;; backward-compatibility alias
453 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
455 (defface flyspell-duplicate
456 '((((class color)) (:foreground "Gold3" :bold t :underline t))
458 "Face used for marking a misspelled word that appears twice in the buffer.
459 See also `flyspell-duplicate-distance'."
461 ;; backward-compatibility alias
462 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
464 (defvar flyspell-overlay nil)
466 ;*---------------------------------------------------------------------*/
467 ;* flyspell-mode ... */
468 ;*---------------------------------------------------------------------*/
470 (define-minor-mode flyspell-mode
471 "Minor mode performing on-the-fly spelling checking.
472 This spawns a single Ispell process and checks each word.
473 The default flyspell behavior is to highlight incorrect words.
474 With no argument, this command toggles Flyspell mode.
475 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
478 \\[ispell-word]: correct words (using Ispell).
479 \\[flyspell-auto-correct-word]: automatically correct word.
480 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
481 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
484 This runs `flyspell-mode-hook' after flyspell is entered.
487 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
488 valid. For instance, a personal dictionary can be used by
489 invoking `ispell-change-dictionary'.
491 Consider using the `ispell-parser' to check your text. For instance
493 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
496 \\[flyspell-region] checks all words inside a region.
497 \\[flyspell-buffer] checks the whole buffer."
498 :lighter flyspell-mode-line-string
499 :keymap flyspell-mode-map
503 (flyspell-mode-off)))
505 ;*---------------------------------------------------------------------*/
506 ;* flyspell-buffers ... */
507 ;* ------------------------------------------------------------- */
508 ;* For remembering buffers running flyspell */
509 ;*---------------------------------------------------------------------*/
510 (defvar flyspell-buffers nil)
512 ;*---------------------------------------------------------------------*/
513 ;* flyspell-minibuffer-p ... */
514 ;*---------------------------------------------------------------------*/
515 (defun flyspell-minibuffer-p (buffer)
516 "Is BUFFER a minibuffer?"
517 (let ((ws (get-buffer-window-list buffer t)))
518 (and (consp ws) (window-minibuffer-p (car ws)))))
520 ;*---------------------------------------------------------------------*/
521 ;* flyspell-accept-buffer-local-defs ... */
522 ;*---------------------------------------------------------------------*/
523 (defun flyspell-accept-buffer-local-defs ()
524 ;; strange problem. If buffer in current window has font-lock turned on,
525 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
526 ;; call will reset the buffer to the buffer in the current window. However,
527 ;; it only happens at startup (fix by Albert L. Ting).
528 (let ((buf (current-buffer)))
529 (ispell-accept-buffer-local-defs)
531 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
532 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
533 ;; The dictionary has changed
535 (setq flyspell-dash-dictionary ispell-dictionary)
536 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
537 (if (member (or ispell-local-dictionary ispell-dictionary)
538 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
539 (setq flyspell-consider-dash-as-word-delimiter-flag t)
540 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
542 ;*---------------------------------------------------------------------*/
543 ;* flyspell-mode-on ... */
544 ;*---------------------------------------------------------------------*/
545 (defun flyspell-mode-on ()
546 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
547 (setq ispell-highlight-face 'flyspell-incorrect)
548 ;; local dictionaries setup
549 (or ispell-local-dictionary ispell-dictionary
550 (if flyspell-default-dictionary
551 (ispell-change-dictionary flyspell-default-dictionary)))
552 ;; we have to force ispell to accept the local definition or
553 ;; otherwise it could be too late, the local dictionary may
555 (flyspell-accept-buffer-local-defs)
556 ;; we put the `flyspell-delayed' property on some commands
557 (flyspell-delay-commands)
558 ;; we put the `flyspell-deplacement' property on some commands
559 (flyspell-deplacement-commands)
560 ;; we bound flyspell action to post-command hook
561 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
562 ;; we bound flyspell action to pre-command hook
563 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
564 ;; we bound flyspell action to after-change hook
565 (make-local-variable 'after-change-functions)
566 (setq after-change-functions
567 (cons 'flyspell-after-change-function after-change-functions))
568 ;; set flyspell-generic-check-word-p based on the major mode
569 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
571 (setq flyspell-generic-check-word-p mode-predicate)))
572 ;; work around the fact that the `local-map' text-property replaces the
573 ;; buffer's local map rather than shadowing it.
574 (set (make-local-variable 'flyspell-mouse-map)
575 (let ((map (copy-keymap flyspell-mouse-map)))
576 (set-keymap-parent map (current-local-map))
577 (if (and (eq flyspell-emacs 'emacs)
578 (not (string< emacs-version "20")))
579 (define-key map '[tool-bar] nil))
581 (set (make-local-variable 'flyspell-mode-map)
582 (let ((map (copy-keymap flyspell-mode-map)))
583 (set-keymap-parent map (current-local-map))
584 (if (and (eq flyspell-emacs 'emacs)
585 (not (string< emacs-version "20")))
586 (define-key map '[tool-bar] nil))
588 ;; the welcome message
589 (if (and flyspell-issue-message-flag
590 flyspell-issue-welcome-flag
592 (let ((binding (where-is-internal 'flyspell-auto-correct-word
596 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
597 (key-description binding))
598 "Welcome to flyspell. Use Mouse-2 to correct words."))))
599 ;; we end with the flyspell hooks
600 (run-hooks 'flyspell-mode-hook))
602 ;*---------------------------------------------------------------------*/
603 ;* flyspell-delay-commands ... */
604 ;*---------------------------------------------------------------------*/
605 (defun flyspell-delay-commands ()
606 "Install the standard set of Flyspell delayed commands."
607 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
608 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
610 ;*---------------------------------------------------------------------*/
611 ;* flyspell-delay-command ... */
612 ;*---------------------------------------------------------------------*/
613 (defun flyspell-delay-command (command)
614 "Set COMMAND to be delayed, for Flyspell.
615 When flyspell `post-command-hook' is invoked because a delayed command
616 as been used the current word is not immediately checked.
617 It will be checked only after `flyspell-delay' seconds."
618 (interactive "SDelay Flyspell after Command: ")
619 (put command 'flyspell-delayed t))
621 ;*---------------------------------------------------------------------*/
622 ;* flyspell-deplacement-commands ... */
623 ;*---------------------------------------------------------------------*/
624 (defun flyspell-deplacement-commands ()
625 "Install the standard set of Flyspell deplacement commands."
626 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
627 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
629 ;*---------------------------------------------------------------------*/
630 ;* flyspell-deplacement-command ... */
631 ;*---------------------------------------------------------------------*/
632 (defun flyspell-deplacement-command (command)
633 "Set COMMAND that implement cursor movements, for Flyspell.
634 When flyspell `post-command-hook' is invoked because of a deplacement command
635 as been used the current word is checked only if the previous command was
636 not the very same deplacement command."
637 (interactive "SDeplacement Flyspell after Command: ")
638 (put command 'flyspell-deplacement t))
640 ;*---------------------------------------------------------------------*/
641 ;* flyspell-word-cache ... */
642 ;*---------------------------------------------------------------------*/
643 (defvar flyspell-word-cache-start nil)
644 (defvar flyspell-word-cache-end nil)
645 (defvar flyspell-word-cache-word nil)
646 (defvar flyspell-word-cache-result '_)
647 (make-variable-buffer-local 'flyspell-word-cache-start)
648 (make-variable-buffer-local 'flyspell-word-cache-end)
649 (make-variable-buffer-local 'flyspell-word-cache-word)
650 (make-variable-buffer-local 'flyspell-word-cache-result)
652 ;*---------------------------------------------------------------------*/
653 ;* The flyspell pre-hook, store the current position. In the */
654 ;* post command hook, we will check, if the word at this position */
655 ;* has to be spell checked. */
656 ;*---------------------------------------------------------------------*/
657 (defvar flyspell-pre-buffer nil)
658 (defvar flyspell-pre-point nil)
659 (defvar flyspell-pre-column nil)
660 (defvar flyspell-pre-pre-buffer nil)
661 (defvar flyspell-pre-pre-point nil)
663 ;*---------------------------------------------------------------------*/
664 ;* flyspell-previous-command ... */
665 ;*---------------------------------------------------------------------*/
666 (defvar flyspell-previous-command nil
667 "The last interactive command checked by Flyspell.")
669 ;*---------------------------------------------------------------------*/
670 ;* flyspell-pre-command-hook ... */
671 ;*---------------------------------------------------------------------*/
672 (defun flyspell-pre-command-hook ()
673 "Save the current buffer and point for Flyspell's post-command hook."
675 (setq flyspell-pre-buffer (current-buffer))
676 (setq flyspell-pre-point (point))
677 (setq flyspell-pre-column (current-column)))
679 ;*---------------------------------------------------------------------*/
680 ;* flyspell-mode-off ... */
681 ;*---------------------------------------------------------------------*/
683 (defun flyspell-mode-off ()
684 "Turn Flyspell mode off."
685 ;; we remove the hooks
686 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
687 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
688 (setq after-change-functions (delq 'flyspell-after-change-function
689 after-change-functions))
690 ;; we remove all the flyspell hilightings
691 (flyspell-delete-all-overlays)
692 ;; we have to erase pre cache variables
693 (setq flyspell-pre-buffer nil)
694 (setq flyspell-pre-point nil)
695 ;; we mark the mode as killed
696 (setq flyspell-mode nil))
698 ;*---------------------------------------------------------------------*/
699 ;* flyspell-check-pre-word-p ... */
700 ;*---------------------------------------------------------------------*/
701 (defun flyspell-check-pre-word-p ()
702 "Return non-nil if we should check the word before point.
703 More precisely, it applies to the word that was before point
704 before the current command."
706 ((or (not (numberp flyspell-pre-point))
707 (not (bufferp flyspell-pre-buffer))
708 (not (buffer-live-p flyspell-pre-buffer)))
710 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
711 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
713 ((or (and (= flyspell-pre-point (- (point) 1))
714 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
715 (= flyspell-pre-point (point))
716 (= flyspell-pre-point (+ (point) 1)))
718 ((and (symbolp this-command)
719 (not executing-kbd-macro)
720 (or (get this-command 'flyspell-delayed)
721 (and (get this-command 'flyspell-deplacement)
722 (eq flyspell-previous-command this-command)))
723 (or (= (current-column) 0)
724 (= (current-column) flyspell-pre-column)
725 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
727 ((not (eq (current-buffer) flyspell-pre-buffer))
729 ((not (and (numberp flyspell-word-cache-start)
730 (numberp flyspell-word-cache-end)))
733 (or (< flyspell-pre-point flyspell-word-cache-start)
734 (> flyspell-pre-point flyspell-word-cache-end)))))
736 ;*---------------------------------------------------------------------*/
737 ;* The flyspell after-change-hook, store the change position. In */
738 ;* the post command hook, we will check, if the word at this */
739 ;* position has to be spell checked. */
740 ;*---------------------------------------------------------------------*/
741 (defvar flyspell-changes nil)
743 ;*---------------------------------------------------------------------*/
744 ;* flyspell-after-change-function ... */
745 ;*---------------------------------------------------------------------*/
746 (defun flyspell-after-change-function (start stop len)
747 "Save the current buffer and point for Flyspell's post-command hook."
749 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
751 ;*---------------------------------------------------------------------*/
752 ;* flyspell-check-changed-word-p ... */
753 ;*---------------------------------------------------------------------*/
754 (defun flyspell-check-changed-word-p (start stop)
755 "Return t when the changed word has to be checked.
756 The answer depends of several criteria.
757 Mostly we check word delimiters."
759 ((and (memq (char-after start) '(?\n ? )) (> stop start))
761 ((not (numberp flyspell-pre-point))
763 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
765 ((let ((pos (point)))
766 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
771 ;*---------------------------------------------------------------------*/
772 ;* flyspell-check-word-p ... */
773 ;*---------------------------------------------------------------------*/
774 (defun flyspell-check-word-p ()
775 "Return t when the word at `point' has to be checked.
776 The answer depends of several criteria.
777 Mostly we check word delimiters."
779 ((<= (- (point-max) 1) (point-min))
780 ;; the buffer is not filled enough
782 ((and (and (> (current-column) 0)
783 (not (eq (current-column) flyspell-pre-column)))
786 (and (looking-at (flyspell-get-not-casechars))
787 (or flyspell-consider-dash-as-word-delimiter-flag
788 (not (looking-at "\\-"))))))
789 ;; yes because we have reached or typed a word delimiter.
791 ((symbolp this-command)
793 ((get this-command 'flyspell-deplacement)
794 (not (eq flyspell-previous-command this-command)))
795 ((get this-command 'flyspell-delayed)
796 ;; the current command is not delayed, that
797 ;; is that we must check the word now
798 (sit-for flyspell-delay))
802 ;*---------------------------------------------------------------------*/
803 ;* flyspell-debug-signal-no-check ... */
804 ;*---------------------------------------------------------------------*/
805 (defun flyspell-debug-signal-no-check (msg obj)
806 (setq debug-on-error t)
808 (let ((buffer (get-buffer-create "*flyspell-debug*")))
811 (insert "NO-CHECK:\n")
812 (insert (format " %S : %S\n" msg obj)))))
814 ;*---------------------------------------------------------------------*/
815 ;* flyspell-debug-signal-pre-word-checked ... */
816 ;*---------------------------------------------------------------------*/
817 (defun flyspell-debug-signal-pre-word-checked ()
818 (setq debug-on-error t)
820 (let ((buffer (get-buffer-create "*flyspell-debug*")))
822 (insert "PRE-WORD:\n")
823 (insert (format " pre-point : %S\n" flyspell-pre-point))
824 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
825 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
826 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
827 (goto-char (point-max)))))
829 ;*---------------------------------------------------------------------*/
830 ;* flyspell-debug-signal-word-checked ... */
831 ;*---------------------------------------------------------------------*/
832 (defun flyspell-debug-signal-word-checked ()
833 (setq debug-on-error t)
835 (let ((oldbuf (current-buffer))
836 (buffer (get-buffer-create "*flyspell-debug*"))
840 (insert (format " this-cmd : %S\n" this-command))
841 (insert (format " delayed : %S\n" (and (symbolp this-command)
842 (get this-command 'flyspell-delayed))))
843 (insert (format " point : %S\n" point))
844 (insert (format " prev-char : [%c] %S\n"
847 (let ((c (if (> (point) (point-min))
850 (char-after (point)))
856 (let ((c (if (> (point) (point-min))
859 (and (and (looking-at (flyspell-get-not-casechars)) 1)
860 (and (or flyspell-consider-dash-as-word-delimiter-flag
861 (not (looking-at "\\-"))) 2))))))
864 (insert (format " because : %S\n"
866 ((not (and (symbolp this-command)
867 (get this-command 'flyspell-delayed)))
868 ;; the current command is not delayed, that
869 ;; is that we must check the word now
873 (let ((c (if (> (point) (point-min))
876 (and (looking-at (flyspell-get-not-casechars))
877 (or flyspell-consider-dash-as-word-delimiter-flag
878 (not (looking-at "\\-"))))))))
881 ;; yes because we have reached or typed a word delimiter.
883 ((not (integerp flyspell-delay))
884 ;; yes because the user had set up a no-delay configuration.
888 (goto-char (point-max)))))
890 ;*---------------------------------------------------------------------*/
891 ;* flyspell-debug-signal-changed-checked ... */
892 ;*---------------------------------------------------------------------*/
893 (defun flyspell-debug-signal-changed-checked ()
894 (setq debug-on-error t)
896 (let ((buffer (get-buffer-create "*flyspell-debug*"))
899 (insert "CHANGED WORD:\n")
900 (insert (format " point : %S\n" point))
901 (goto-char (point-max)))))
903 ;*---------------------------------------------------------------------*/
904 ;* flyspell-post-command-hook ... */
905 ;* ------------------------------------------------------------- */
906 ;* It is possible that we check several words: */
907 ;* 1- the current word is checked if the predicate */
908 ;* FLYSPELL-CHECK-WORD-P is true */
909 ;* 2- the word that used to be the current word before the */
910 ;* THIS-COMMAND is checked if: */
911 ;* a- the previous word is different from the current word */
912 ;* b- the previous word as not just been checked by the */
913 ;* previous FLYSPELL-POST-COMMAND-HOOK */
914 ;* 3- the words changed by the THIS-COMMAND that are neither the */
915 ;* previous word nor the current word */
916 ;*---------------------------------------------------------------------*/
917 (defun flyspell-post-command-hook ()
918 "The `post-command-hook' used by flyspell to check a word in-the-fly."
920 (let ((command this-command))
921 (if (flyspell-check-pre-word-p)
923 '(flyspell-debug-signal-pre-word-checked)
924 (set-buffer flyspell-pre-buffer)
926 (goto-char flyspell-pre-point)
928 (if (flyspell-check-word-p)
930 '(flyspell-debug-signal-word-checked)
932 ;; we remember which word we have just checked.
933 ;; this will be used next time we will check a word
934 ;; to compare the next current word with the word
935 ;; that as been registered in the pre-command-hook
936 ;; that is these variables are used within the predicate
937 ;; FLYSPELL-CHECK-PRE-WORD-P
938 (setq flyspell-pre-pre-buffer (current-buffer))
939 (setq flyspell-pre-pre-point (point)))
941 (setq flyspell-pre-pre-buffer nil)
942 (setq flyspell-pre-pre-point nil)
943 ;; when a word is not checked because of a delayed command
944 ;; we do not disable the ispell cache.
945 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
947 (setq flyspell-word-cache-end -1)
948 (setq flyspell-word-cache-result '_)))))
949 (while (consp flyspell-changes)
950 (let ((start (car (car flyspell-changes)))
951 (stop (cdr (car flyspell-changes))))
952 (if (flyspell-check-changed-word-p start stop)
954 '(flyspell-debug-signal-changed-checked)
957 (setq flyspell-changes (cdr flyspell-changes))))
958 (setq flyspell-previous-command command)))
960 ;*---------------------------------------------------------------------*/
961 ;* flyspell-notify-misspell ... */
962 ;*---------------------------------------------------------------------*/
963 (defun flyspell-notify-misspell (start end word poss)
964 (let ((replacements (if (stringp poss)
966 (if flyspell-sort-corrections
967 (sort (car (cdr (cdr poss))) 'string<)
968 (car (cdr (cdr poss)))))))
969 (if flyspell-issue-message-flag
970 (message (format "mispelling `%s' %S" word replacements)))))
972 ;*---------------------------------------------------------------------*/
973 ;* flyspell-word-search-backward ... */
974 ;*---------------------------------------------------------------------*/
975 (defun flyspell-word-search-backward (word bound)
979 (while (and (not r) (setq p (search-backward word bound t)))
980 (let ((lw (flyspell-get-word '())))
981 (if (and (consp lw) (string-equal (car lw) word))
986 ;*---------------------------------------------------------------------*/
987 ;* flyspell-word-search-forward ... */
988 ;*---------------------------------------------------------------------*/
989 (defun flyspell-word-search-forward (word bound)
993 (while (and (not r) (setq p (search-forward word bound t)))
994 (let ((lw (flyspell-get-word '())))
995 (if (and (consp lw) (string-equal (car lw) word))
997 (goto-char (1+ p)))))
1000 ;*---------------------------------------------------------------------*/
1001 ;* flyspell-word ... */
1002 ;*---------------------------------------------------------------------*/
1003 (defun flyspell-word (&optional following)
1004 "Spell check a word."
1005 (interactive (list ispell-following-word))
1007 ;; use the correct dictionary
1008 (flyspell-accept-buffer-local-defs)
1009 (let* ((cursor-location (point))
1010 (flyspell-word (flyspell-get-word following))
1011 start end poss word)
1012 (if (or (eq flyspell-word nil)
1013 (and (fboundp flyspell-generic-check-word-p)
1014 (not (funcall flyspell-generic-check-word-p))))
1017 ;; destructure return flyspell-word info list.
1018 (setq start (car (cdr flyspell-word))
1019 end (car (cdr (cdr flyspell-word)))
1020 word (car flyspell-word))
1021 ;; before checking in the directory, we check for doublons.
1023 ((and (or (not (eq ispell-parser 'tex))
1024 (and (> start (point-min))
1025 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1026 flyspell-mark-duplications-flag
1028 (goto-char (1- start))
1029 (let ((p (flyspell-word-search-backward
1031 (- start (1+ (- end start))))))
1032 (and p (/= p (1- start))))))
1033 ;; yes, this is a doublon
1034 (flyspell-highlight-incorrect-region start end 'doublon)
1036 ((and (eq flyspell-word-cache-start start)
1037 (eq flyspell-word-cache-end end)
1038 (string-equal flyspell-word-cache-word word))
1039 ;; this word had been already checked, we skip
1040 flyspell-word-cache-result)
1041 ((and (eq ispell-parser 'tex)
1042 (flyspell-tex-command-p flyspell-word))
1043 ;; this is a correct word (because a tex command)
1044 (flyspell-unhighlight-at start)
1046 (flyspell-unhighlight-at (- end 1)))
1049 ;; we setup the cache
1050 (setq flyspell-word-cache-start start)
1051 (setq flyspell-word-cache-end end)
1052 (setq flyspell-word-cache-word word)
1053 ;; now check spelling of word.
1054 (process-send-string ispell-process "%\n")
1055 ;; put in verbose mode
1056 (process-send-string ispell-process
1057 (concat "^" word "\n"))
1058 ;; we mark the ispell process so it can be killed
1059 ;; when emacs is exited without query
1060 (set-process-query-on-exit-flag ispell-process nil)
1061 ;; wait until ispell has processed word
1063 (accept-process-output ispell-process)
1064 (not (string= "" (car ispell-filter)))))
1065 ;; (process-send-string ispell-process "!\n")
1066 ;; back to terse mode.
1067 (setq ispell-filter (cdr ispell-filter))
1068 (if (consp ispell-filter)
1069 (setq poss (ispell-parse-output (car ispell-filter))))
1070 (let ((res (cond ((eq poss t)
1072 (setq flyspell-word-cache-result t)
1073 (flyspell-unhighlight-at start)
1075 (flyspell-unhighlight-at (- end 1)))
1077 ((and (stringp poss) flyspell-highlight-flag)
1079 (setq flyspell-word-cache-result t)
1080 (flyspell-unhighlight-at start)
1082 (flyspell-unhighlight-at (- end 1)))
1085 (setq flyspell-word-cache-result t)
1086 (flyspell-unhighlight-at start)
1088 (flyspell-unhighlight-at (- end 1)))
1090 ((or (and (< flyspell-duplicate-distance 0)
1093 (flyspell-word-search-backward
1098 (flyspell-word-search-forward
1101 (and (> flyspell-duplicate-distance 0)
1104 (flyspell-word-search-backward
1107 flyspell-duplicate-distance)))
1110 (flyspell-word-search-forward
1113 flyspell-duplicate-distance))))))
1114 (setq flyspell-word-cache-result nil)
1115 (if flyspell-highlight-flag
1116 (flyspell-highlight-duplicate-region
1118 (message (format "duplicate `%s'" word)))
1121 (setq flyspell-word-cache-result nil)
1122 ;; incorrect highlight the location
1123 (if flyspell-highlight-flag
1124 (flyspell-highlight-incorrect-region
1126 (flyspell-notify-misspell start end word poss))
1128 ;; return to original location
1129 (goto-char cursor-location)
1130 (if ispell-quit (setq ispell-quit nil))
1133 ;*---------------------------------------------------------------------*/
1134 ;* flyspell-tex-math-initialized ... */
1135 ;*---------------------------------------------------------------------*/
1136 (defvar flyspell-tex-math-initialized nil)
1138 ;*---------------------------------------------------------------------*/
1139 ;* flyspell-math-tex-command-p ... */
1140 ;* ------------------------------------------------------------- */
1141 ;* This function uses the texmathp package to check if (point) */
1142 ;* is within a tex command. In order to avoid using */
1143 ;* condition-case each time we use the variable */
1144 ;* flyspell-tex-math-initialized to make a special case the first */
1145 ;* time that function is called. */
1146 ;*---------------------------------------------------------------------*/
1147 (defun flyspell-math-tex-command-p ()
1148 (when (fboundp 'texmathp)
1150 (flyspell-check-tex-math-command
1152 ((eq flyspell-tex-math-initialized t)
1154 ((eq flyspell-tex-math-initialized 'error)
1157 (setq flyspell-tex-math-initialized t)
1161 (setq flyspell-tex-math-initialized 'error)
1164 ;*---------------------------------------------------------------------*/
1165 ;* flyspell-tex-command-p ... */
1166 ;*---------------------------------------------------------------------*/
1167 (defun flyspell-tex-command-p (word)
1168 "Return t if WORD is a TeX command."
1170 (let ((b (car (cdr word))))
1171 (and (re-search-backward "\\\\" (- (point) 100) t)
1172 (or (= (match-end 0) b)
1173 (and (goto-char (match-end 0))
1174 (looking-at flyspell-tex-command-regexp)
1175 (>= (match-end 0) b))))))
1176 (flyspell-math-tex-command-p)))
1178 ;*---------------------------------------------------------------------*/
1179 ;* flyspell-casechars-cache ... */
1180 ;*---------------------------------------------------------------------*/
1181 (defvar flyspell-casechars-cache nil)
1182 (defvar flyspell-ispell-casechars-cache nil)
1183 (make-variable-buffer-local 'flyspell-casechars-cache)
1184 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1186 ;*---------------------------------------------------------------------*/
1187 ;* flyspell-get-casechars ... */
1188 ;*---------------------------------------------------------------------*/
1189 (defun flyspell-get-casechars ()
1190 "This function builds a string that is the regexp of word chars.
1191 In order to avoid one useless string construction,
1192 this function changes the last char of the `ispell-casechars' string."
1193 (let ((ispell-casechars (ispell-get-casechars)))
1195 ((eq ispell-parser 'tex)
1196 (setq flyspell-ispell-casechars-cache ispell-casechars)
1197 (setq flyspell-casechars-cache
1198 (concat (substring ispell-casechars
1200 (- (length ispell-casechars) 1))
1202 flyspell-casechars-cache)
1204 (setq flyspell-ispell-casechars-cache ispell-casechars)
1205 (setq flyspell-casechars-cache ispell-casechars)
1206 flyspell-casechars-cache))))
1208 ;*---------------------------------------------------------------------*/
1209 ;* flyspell-get-not-casechars-cache ... */
1210 ;*---------------------------------------------------------------------*/
1211 (defvar flyspell-not-casechars-cache nil)
1212 (defvar flyspell-ispell-not-casechars-cache nil)
1213 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1214 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1216 ;*---------------------------------------------------------------------*/
1217 ;* flyspell-get-not-casechars ... */
1218 ;*---------------------------------------------------------------------*/
1219 (defun flyspell-get-not-casechars ()
1220 "This function builds a string that is the regexp of non-word chars."
1221 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1223 ((eq ispell-parser 'tex)
1224 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1225 (setq flyspell-not-casechars-cache
1226 (concat (substring ispell-not-casechars
1228 (- (length ispell-not-casechars) 1))
1230 flyspell-not-casechars-cache)
1232 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1233 (setq flyspell-not-casechars-cache ispell-not-casechars)
1234 flyspell-not-casechars-cache))))
1236 ;*---------------------------------------------------------------------*/
1237 ;* flyspell-get-word ... */
1238 ;*---------------------------------------------------------------------*/
1239 (defun flyspell-get-word (following &optional extra-otherchars)
1240 "Return the word for spell-checking according to Ispell syntax.
1241 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1242 is non-nil when called interactively, then the following word
1243 \(rather than preceding\) is checked when the cursor is not over a word.
1244 Optional second argument contains otherchars that can be included in word
1247 Word syntax described by `flyspell-dictionary-alist' (which see)."
1248 (let* ((flyspell-casechars (flyspell-get-casechars))
1249 (flyspell-not-casechars (flyspell-get-not-casechars))
1250 (ispell-otherchars (ispell-get-otherchars))
1251 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1252 (word-regexp (concat flyspell-casechars
1254 (if (not (string= "" ispell-otherchars))
1255 (concat ispell-otherchars "?"))
1256 (if extra-otherchars
1257 (concat extra-otherchars "?"))
1260 (if (or ispell-many-otherchars-p
1266 (if (not (looking-at flyspell-casechars))
1268 (re-search-forward flyspell-casechars (point-max) t)
1269 (re-search-backward flyspell-casechars (point-min) t)))
1270 ;; move to front of word
1271 (re-search-backward flyspell-not-casechars (point-min) 'start)
1272 (while (and (or (and (not (string= "" ispell-otherchars))
1273 (looking-at ispell-otherchars))
1274 (and extra-otherchars (looking-at extra-otherchars)))
1276 (or (not did-it-once)
1277 ispell-many-otherchars-p)
1278 (not (eq prevpt (point))))
1279 (if (and extra-otherchars (looking-at extra-otherchars))
1282 (if (looking-at flyspell-casechars)
1283 (re-search-backward flyspell-not-casechars (point-min) 'move)))
1287 (if (looking-at flyspell-casechars)
1288 (re-search-backward flyspell-not-casechars (point-min) 'move)
1289 (backward-char -1))))
1290 ;; Now mark the word and save to string.
1291 (if (not (re-search-forward word-regexp (point-max) t))
1294 (setq start (match-beginning 0)
1296 word (buffer-substring-no-properties start end))
1297 (list word start end)))))
1299 ;*---------------------------------------------------------------------*/
1300 ;* flyspell-small-region ... */
1301 ;*---------------------------------------------------------------------*/
1302 (defun flyspell-small-region (beg end)
1303 "Flyspell text between BEG and END."
1311 (while (< (point) end)
1312 (if (and flyspell-issue-message-flag (= count 100))
1314 (message "Spell Checking...%d%%"
1315 (* 100 (/ (float (- (point) beg)) (- end beg))))
1317 (setq count (+ 1 count)))
1320 (let ((cur (point)))
1322 (if (and (< (point) end) (> (point) (+ cur 1)))
1323 (backward-char 1)))))
1325 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1328 ;*---------------------------------------------------------------------*/
1329 ;* flyspell-external-ispell-process ... */
1330 ;*---------------------------------------------------------------------*/
1331 (defvar flyspell-external-ispell-process '()
1332 "The external Flyspell Ispell process.")
1334 ;*---------------------------------------------------------------------*/
1335 ;* flyspell-external-ispell-buffer ... */
1336 ;*---------------------------------------------------------------------*/
1337 (defvar flyspell-external-ispell-buffer '())
1338 (defvar flyspell-large-region-buffer '())
1339 (defvar flyspell-large-region-beg (point-min))
1340 (defvar flyspell-large-region-end (point-max))
1342 ;*---------------------------------------------------------------------*/
1343 ;* flyspell-external-point-words ... */
1344 ;*---------------------------------------------------------------------*/
1345 (defun flyspell-external-point-words ()
1346 (let ((buffer flyspell-external-ispell-buffer))
1348 (goto-char (point-min))
1351 ;; now we are done with ispell, we have to find the word in
1352 ;; the initial buffer
1353 (while (< (point) (- (point-max) 1))
1354 ;; we have to fetch the incorrect word
1355 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1356 (let ((word (match-string 1)))
1357 (if (string= word pword)
1358 (setq pcount (1+ pcount))
1362 (goto-char (match-end 0))
1363 (if flyspell-issue-message-flag
1364 (message "Spell Checking...%d%% [%s]"
1365 (* 100 (/ (float (point)) (point-max)))
1367 (set-buffer flyspell-large-region-buffer)
1368 (goto-char flyspell-large-region-beg)
1371 (while (and (or (< n pcount) keep)
1372 (search-forward word flyspell-large-region-end t))
1374 (goto-char (- (point) 1))
1376 (setq keep (flyspell-word))))
1378 (setq flyspell-large-region-beg (point))))
1379 (set-buffer buffer))
1380 (goto-char (point-max)))))
1382 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1383 ;; ok, we are done with pointing out incorrect words, we just
1384 ;; have to kill the temporary buffer
1385 (kill-buffer flyspell-external-ispell-buffer)
1386 (setq flyspell-external-ispell-buffer nil)))
1388 ;*---------------------------------------------------------------------*/
1389 ;* flyspell-large-region ... */
1390 ;*---------------------------------------------------------------------*/
1391 (defun flyspell-large-region (beg end)
1392 (let* ((curbuf (current-buffer))
1393 (buffer (get-buffer-create "*flyspell-region*")))
1394 (setq flyspell-external-ispell-buffer buffer)
1395 (setq flyspell-large-region-buffer curbuf)
1396 (setq flyspell-large-region-beg beg)
1397 (setq flyspell-large-region-end end)
1400 ;; this is done, we can start checking...
1401 (if flyspell-issue-message-flag (message "Checking region..."))
1403 (let ((c (apply 'call-process-region beg
1409 (if ispell-really-aspell "list" "-l")
1411 ;; Local dictionary becomes the global dictionary in use.
1412 (if ispell-local-dictionary
1413 (setq ispell-dictionary ispell-local-dictionary))
1414 (setq args (ispell-get-ispell-args))
1415 (if ispell-dictionary ; use specified dictionary
1417 (append (list "-d" ispell-dictionary) args)))
1418 (if ispell-personal-dictionary ; use specified pers dict
1423 ispell-personal-dictionary)))))
1424 (setq args (append args ispell-extra-args))
1427 (flyspell-external-point-words)
1428 (error "Can't check region...")))))
1430 ;*---------------------------------------------------------------------*/
1431 ;* flyspell-region ... */
1432 ;* ------------------------------------------------------------- */
1433 ;* Because `ispell -a' is too slow, it is not possible to use */
1434 ;* it on large region. Then, when ispell is invoked on a large */
1435 ;* text region, a new `ispell -l' process is spawned. The */
1436 ;* pointed out words are then searched in the region a checked with */
1437 ;* regular flyspell means. */
1438 ;*---------------------------------------------------------------------*/
1440 (defun flyspell-region (beg end)
1441 "Flyspell text between BEG and END."
1450 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1451 (flyspell-large-region beg end)
1452 (flyspell-small-region beg end)))))
1454 ;*---------------------------------------------------------------------*/
1455 ;* flyspell-buffer ... */
1456 ;*---------------------------------------------------------------------*/
1458 (defun flyspell-buffer ()
1459 "Flyspell whole buffer."
1461 (flyspell-region (point-min) (point-max)))
1463 ;*---------------------------------------------------------------------*/
1464 ;* old next error position ... */
1465 ;*---------------------------------------------------------------------*/
1466 (defvar flyspell-old-buffer-error nil)
1467 (defvar flyspell-old-pos-error nil)
1469 ;*---------------------------------------------------------------------*/
1470 ;* flyspell-goto-next-error ... */
1471 ;*---------------------------------------------------------------------*/
1472 (defun flyspell-goto-next-error ()
1473 "Go to the next previously detected error.
1474 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1479 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1480 (eq pos flyspell-old-pos-error))
1482 (if (= flyspell-old-pos-error max)
1483 ;; goto beginning of buffer
1485 (message "Restarting from beginning of buffer")
1486 (goto-char (point-min)))
1488 (setq pos (point))))
1489 ;; seek the next error
1490 (while (and (< pos max)
1491 (let ((ovs (overlays-at pos))
1493 (while (and (not r) (consp ovs))
1494 (if (flyspell-overlay-p (car ovs))
1496 (setq ovs (cdr ovs))))
1498 (setq pos (1+ pos)))
1499 ;; save the current location for next invocation
1500 (setq flyspell-old-pos-error pos)
1501 (setq flyspell-old-buffer-error (current-buffer))
1504 (message "No more miss-spelled word!"))))
1506 ;*---------------------------------------------------------------------*/
1507 ;* flyspell-overlay-p ... */
1508 ;*---------------------------------------------------------------------*/
1509 (defun flyspell-overlay-p (o)
1510 "A predicate that return true iff O is an overlay used by flyspell."
1511 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1513 ;*---------------------------------------------------------------------*/
1514 ;* flyspell-delete-all-overlays ... */
1515 ;* ------------------------------------------------------------- */
1516 ;* Remove all the overlays introduced by flyspell. */
1517 ;*---------------------------------------------------------------------*/
1518 (defun flyspell-delete-all-overlays ()
1519 "Delete all the overlays used by flyspell."
1520 (let ((l (overlays-in (point-min) (point-max))))
1523 (if (flyspell-overlay-p (car l))
1524 (delete-overlay (car l)))
1525 (setq l (cdr l))))))
1527 ;*---------------------------------------------------------------------*/
1528 ;* flyspell-unhighlight-at ... */
1529 ;*---------------------------------------------------------------------*/
1530 (defun flyspell-unhighlight-at (pos)
1531 "Remove the flyspell overlay that are located at POS."
1532 (if flyspell-persistent-highlight
1533 (let ((overlays (overlays-at pos)))
1534 (while (consp overlays)
1535 (if (flyspell-overlay-p (car overlays))
1536 (delete-overlay (car overlays)))
1537 (setq overlays (cdr overlays))))
1538 (if (flyspell-overlay-p flyspell-overlay)
1539 (delete-overlay flyspell-overlay))))
1541 ;*---------------------------------------------------------------------*/
1542 ;* flyspell-properties-at-p ... */
1543 ;* ------------------------------------------------------------- */
1544 ;* Is there an highlight properties at position pos? */
1545 ;*---------------------------------------------------------------------*/
1546 (defun flyspell-properties-at-p (pos)
1547 "Return t if there is a text property at POS, not counting `local-map'.
1548 If variable `flyspell-highlight-properties' is set to nil,
1549 text with properties are not checked. This function is used to discover
1550 if the character at POS has any other property."
1551 (let ((prop (text-properties-at pos))
1553 (while (and keep (consp prop))
1554 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1555 (setq prop (cdr (cdr prop)))
1559 ;*---------------------------------------------------------------------*/
1560 ;* make-flyspell-overlay ... */
1561 ;*---------------------------------------------------------------------*/
1562 (defun make-flyspell-overlay (beg end face mouse-face)
1563 "Allocate an overlay to highlight an incorrect word.
1564 BEG and END specify the range in the buffer of that word.
1565 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1567 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1568 (overlay-put flyspell-overlay 'face face)
1569 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1570 (overlay-put flyspell-overlay 'flyspell-overlay t)
1571 (overlay-put flyspell-overlay 'evaporate t)
1572 (overlay-put flyspell-overlay 'help-echo "mouse-2: correct word at point")
1573 (if flyspell-use-local-map
1574 (overlay-put flyspell-overlay
1575 flyspell-overlay-keymap-property-name
1576 flyspell-mouse-map))
1577 (when (eq face 'flyspell-incorrect)
1578 (and (stringp flyspell-before-incorrect-word-string)
1579 (overlay-put flyspell-overlay 'before-string
1580 flyspell-before-incorrect-word-string))
1581 (and (stringp flyspell-after-incorrect-word-string)
1582 (overlay-put flyspell-overlay 'after-string
1583 flyspell-after-incorrect-word-string)))
1586 ;*---------------------------------------------------------------------*/
1587 ;* flyspell-highlight-incorrect-region ... */
1588 ;*---------------------------------------------------------------------*/
1589 (defun flyspell-highlight-incorrect-region (beg end poss)
1590 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1591 (let ((inhibit-read-only t))
1592 (unless (run-hook-with-args-until-success
1593 'flyspell-incorrect-hook beg end poss)
1594 (if (or flyspell-highlight-properties
1595 (not (flyspell-properties-at-p beg)))
1597 ;; we cleanup all the overlay that are in the region, not
1598 ;; beginning at the word start position
1599 (if (< (1+ beg) end)
1600 (let ((os (overlays-in (1+ beg) end)))
1602 (if (flyspell-overlay-p (car os))
1603 (delete-overlay (car os)))
1604 (setq os (cdr os)))))
1605 ;; we cleanup current overlay at the same position
1606 (if (and (not flyspell-persistent-highlight)
1607 (overlayp flyspell-overlay))
1608 (delete-overlay flyspell-overlay)
1609 (let ((os (overlays-at beg)))
1611 (if (flyspell-overlay-p (car os))
1612 (delete-overlay (car os)))
1613 (setq os (cdr os)))))
1614 ;; now we can use a new overlay
1615 (setq flyspell-overlay
1616 (make-flyspell-overlay
1617 beg end 'flyspell-incorrect 'highlight)))))))
1619 ;*---------------------------------------------------------------------*/
1620 ;* flyspell-highlight-duplicate-region ... */
1621 ;*---------------------------------------------------------------------*/
1622 (defun flyspell-highlight-duplicate-region (beg end poss)
1623 "Set up an overlay on a duplicated word, in the buffer from BEG to END.
1624 ??? What does POSS mean?"
1625 (let ((inhibit-read-only t))
1626 (unless (run-hook-with-args-until-success
1627 'flyspell-incorrect-hook beg end poss)
1628 (if (or flyspell-highlight-properties
1629 (not (flyspell-properties-at-p beg)))
1631 ;; we cleanup current overlay at the same position
1632 (if (and (not flyspell-persistent-highlight)
1633 (overlayp flyspell-overlay))
1634 (delete-overlay flyspell-overlay)
1635 (let ((overlays (overlays-at beg)))
1636 (while (consp overlays)
1637 (if (flyspell-overlay-p (car overlays))
1638 (delete-overlay (car overlays)))
1639 (setq overlays (cdr overlays)))))
1640 ;; now we can use a new overlay
1641 (setq flyspell-overlay
1642 (make-flyspell-overlay beg end
1646 ;*---------------------------------------------------------------------*/
1647 ;* flyspell-auto-correct-cache ... */
1648 ;*---------------------------------------------------------------------*/
1649 (defvar flyspell-auto-correct-pos nil)
1650 (defvar flyspell-auto-correct-region nil)
1651 (defvar flyspell-auto-correct-ring nil)
1652 (defvar flyspell-auto-correct-word nil)
1653 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1654 (make-variable-buffer-local 'flyspell-auto-correct-region)
1655 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1656 (make-variable-buffer-local 'flyspell-auto-correct-word)
1658 ;*---------------------------------------------------------------------*/
1659 ;* flyspell-check-previous-highlighted-word ... */
1660 ;*---------------------------------------------------------------------*/
1661 (defun flyspell-check-previous-highlighted-word (&optional arg)
1662 "Correct the closer misspelled word.
1663 This function scans a mis-spelled word before the cursor. If it finds one
1664 it proposes replacement for that word. With prefix arg, count that many
1665 misspelled words backwards."
1667 (let ((pos1 (point))
1669 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1672 (while (and (setq pos (previous-overlay-change pos))
1675 (if (> pos (point-min))
1677 (setq ovs (overlays-at (1- pos)))
1680 (setq ovs (cdr ovs))
1681 (if (and (overlay-get ov 'flyspell-overlay)
1682 (= 0 (setq arg (1- arg))))
1683 (throw 'exit t)))))))
1687 (error "No word to correct before point"))))
1689 ;*---------------------------------------------------------------------*/
1690 ;* flyspell-display-next-corrections ... */
1691 ;*---------------------------------------------------------------------*/
1692 (defun flyspell-display-next-corrections (corrections)
1693 (let ((string "Corrections:")
1696 (while (< (length string) 80)
1697 (if (equal (car l) flyspell-auto-correct-word)
1698 (setq pos (cons (+ 1 (length string)) pos)))
1699 (setq string (concat string " " (car l)))
1702 (let ((num (car pos)))
1703 (put-text-property num
1704 (+ num (length flyspell-auto-correct-word))
1705 'face 'flyspell-incorrect
1707 (setq pos (cdr pos)))
1708 (if (fboundp 'display-message)
1709 (display-message 'no-log string)
1712 ;*---------------------------------------------------------------------*/
1713 ;* flyspell-abbrev-table ... */
1714 ;*---------------------------------------------------------------------*/
1715 (defun flyspell-abbrev-table ()
1716 (if flyspell-use-global-abbrev-table-p
1718 (or local-abbrev-table global-abbrev-table)))
1720 ;*---------------------------------------------------------------------*/
1721 ;* flyspell-define-abbrev ... */
1722 ;*---------------------------------------------------------------------*/
1723 (defun flyspell-define-abbrev (name expansion)
1724 (let ((table (flyspell-abbrev-table)))
1726 (define-abbrev table name expansion))))
1728 ;*---------------------------------------------------------------------*/
1729 ;* flyspell-auto-correct-word ... */
1730 ;*---------------------------------------------------------------------*/
1731 (defun flyspell-auto-correct-word ()
1732 "Correct the current word.
1733 This command proposes various successive corrections for the current word."
1736 (old-max (point-max)))
1737 ;; use the correct dictionary
1738 (flyspell-accept-buffer-local-defs)
1739 (if (and (eq flyspell-auto-correct-pos pos)
1740 (consp flyspell-auto-correct-region))
1741 ;; we have already been using the function at the same location
1742 (let* ((start (car flyspell-auto-correct-region))
1743 (len (cdr flyspell-auto-correct-region)))
1744 (flyspell-unhighlight-at start)
1745 (delete-region start (+ start len))
1746 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1747 (let* ((word (car flyspell-auto-correct-ring))
1748 (len (length word)))
1749 (rplacd flyspell-auto-correct-region len)
1751 (if flyspell-abbrev-p
1752 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1753 flyspell-auto-correct-word)
1754 (flyspell-change-abbrev (flyspell-abbrev-table)
1755 flyspell-auto-correct-word
1757 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1758 (funcall flyspell-insert-function word)
1760 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1761 (flyspell-ajust-cursor-point pos (point) old-max)
1762 (setq flyspell-auto-correct-pos (point)))
1763 ;; fetch the word to be checked
1764 (let ((word (flyspell-get-word nil)))
1766 (let ((start (car (cdr word)))
1767 (end (car (cdr (cdr word))))
1770 (setq flyspell-auto-correct-word word)
1771 ;; now check spelling of word.
1772 (process-send-string ispell-process "%\n") ;put in verbose mode
1773 (process-send-string ispell-process (concat "^" word "\n"))
1774 ;; wait until ispell has processed word
1776 (accept-process-output ispell-process)
1777 (not (string= "" (car ispell-filter)))))
1778 (setq ispell-filter (cdr ispell-filter))
1779 (if (consp ispell-filter)
1780 (setq poss (ispell-parse-output (car ispell-filter))))
1782 ((or (eq poss t) (stringp poss))
1783 ;; don't correct word
1787 (error "Ispell: error in Ispell process"))
1789 ;; the word is incorrect, we have to propose a replacement
1790 (let ((replacements (if flyspell-sort-corrections
1791 (sort (car (cdr (cdr poss))) 'string<)
1792 (car (cdr (cdr poss))))))
1793 (setq flyspell-auto-correct-region nil)
1794 (if (consp replacements)
1796 (let ((replace (car replacements)))
1797 (let ((new-word replace))
1798 (if (not (equal new-word (car poss)))
1800 ;; the save the current replacements
1801 (setq flyspell-auto-correct-region
1802 (cons start (length new-word)))
1803 (let ((l replacements))
1804 (while (consp (cdr l))
1806 (rplacd l (cons (car poss) replacements)))
1807 (setq flyspell-auto-correct-ring
1809 (flyspell-unhighlight-at start)
1810 (delete-region start end)
1811 (funcall flyspell-insert-function new-word)
1812 (if flyspell-abbrev-p
1813 (if (flyspell-already-abbrevp
1814 (flyspell-abbrev-table) word)
1815 (flyspell-change-abbrev
1816 (flyspell-abbrev-table)
1819 (flyspell-define-abbrev word
1822 (flyspell-display-next-corrections
1823 (cons new-word flyspell-auto-correct-ring))
1824 (flyspell-ajust-cursor-point pos
1827 (setq flyspell-auto-correct-pos (point))
1828 (ispell-pdict-save t)))))))
1830 ;*---------------------------------------------------------------------*/
1831 ;* flyspell-auto-correct-previous-pos ... */
1832 ;*---------------------------------------------------------------------*/
1833 (defvar flyspell-auto-correct-previous-pos nil
1834 "Holds the start of the first incorrect word before point.")
1836 ;*---------------------------------------------------------------------*/
1837 ;* flyspell-auto-correct-previous-hook ... */
1838 ;*---------------------------------------------------------------------*/
1839 (defun flyspell-auto-correct-previous-hook ()
1840 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1841 Sets `flyspell-auto-correct-previous-pos' to nil"
1843 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1844 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1845 (setq flyspell-auto-correct-previous-pos nil)))
1847 ;*---------------------------------------------------------------------*/
1848 ;* flyspell-auto-correct-previous-word ... */
1849 ;*---------------------------------------------------------------------*/
1850 (defun flyspell-auto-correct-previous-word (position)
1851 "*Auto correct the first mispelled word that occurs before point.
1852 But don't look beyond what's visible on the screen."
1857 (move-to-window-line 0)
1859 (move-to-window-line -1)
1863 (narrow-to-region top bot)
1864 (overlay-recenter (point))
1866 (add-hook 'pre-command-hook
1867 (function flyspell-auto-correct-previous-hook) t t)
1869 (unless flyspell-auto-correct-previous-pos
1870 ;; only reset if a new overlay exists
1871 (setq flyspell-auto-correct-previous-pos nil)
1873 (let ((overlay-list (overlays-in (point-min) position))
1874 (new-overlay 'dummy-value))
1876 ;; search for previous (new) flyspell overlay
1877 (while (and new-overlay
1878 (or (not (flyspell-overlay-p new-overlay))
1879 ;; check if its face has changed
1880 (not (eq (get-char-property
1881 (overlay-start new-overlay) 'face)
1882 'flyspell-incorrect))))
1883 (setq new-overlay (car-safe overlay-list))
1884 (setq overlay-list (cdr-safe overlay-list)))
1886 ;; if nothing new exits new-overlay should be nil
1887 (if new-overlay ;; the length of the word may change so go to the start
1888 (setq flyspell-auto-correct-previous-pos
1889 (overlay-start new-overlay)))))
1891 (when flyspell-auto-correct-previous-pos
1893 (goto-char flyspell-auto-correct-previous-pos)
1894 (let ((ispell-following-word t)) ;; point is at start
1895 (if (numberp flyspell-auto-correct-previous-pos)
1896 (goto-char flyspell-auto-correct-previous-pos))
1897 (flyspell-auto-correct-word))
1898 ;; the point may have moved so reset this
1899 (setq flyspell-auto-correct-previous-pos (point))))))))
1901 ;*---------------------------------------------------------------------*/
1902 ;* flyspell-correct-word ... */
1903 ;*---------------------------------------------------------------------*/
1904 (defun flyspell-correct-word (event)
1905 "Pop up a menu of possible corrections for a misspelled word.
1906 The word checked is the word at the mouse position."
1908 ;; use the correct dictionary
1909 (flyspell-accept-buffer-local-defs)
1910 ;; retain cursor location (I don't know why but save-excursion here fails).
1911 (let ((save (point)))
1912 (mouse-set-point event)
1913 (let ((cursor-location (point))
1914 (word (flyspell-get-word nil)))
1916 (let ((start (car (cdr word)))
1917 (end (car (cdr (cdr word))))
1920 ;; now check spelling of word.
1921 (process-send-string ispell-process "%\n") ;put in verbose mode
1922 (process-send-string ispell-process (concat "^" word "\n"))
1923 ;; wait until ispell has processed word
1925 (accept-process-output ispell-process)
1926 (not (string= "" (car ispell-filter)))))
1927 (setq ispell-filter (cdr ispell-filter))
1928 (if (consp ispell-filter)
1929 (setq poss (ispell-parse-output (car ispell-filter))))
1931 ((or (eq poss t) (stringp poss))
1932 ;; don't correct word
1936 (error "Ispell: error in Ispell process"))
1938 (flyspell-xemacs-popup
1939 event poss word cursor-location start end save))
1941 ;; The word is incorrect, we have to propose a replacement.
1942 (flyspell-do-correct (flyspell-emacs-popup event poss word)
1943 poss word cursor-location start end save)))
1944 (ispell-pdict-save t))))))
1946 ;*---------------------------------------------------------------------*/
1947 ;* flyspell-do-correct ... */
1948 ;*---------------------------------------------------------------------*/
1949 (defun flyspell-do-correct (replace poss word cursor-location start end save)
1950 "The popup menu callback."
1951 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
1952 ;; it instead right after calling the function.
1953 (cond ((eq replace 'ignore)
1958 (ispell-send-string (concat "*" word "\n"))
1959 ;; This was added only to the XEmacs side in revision 1.18 of
1960 ;; flyspell. I assume its absence on the Emacs side was an
1961 ;; oversight. --Stef
1962 (ispell-send-string "#\n")
1963 (flyspell-unhighlight-at cursor-location)
1964 (setq ispell-pdict-modified-p '(t)))
1965 ((or (eq replace 'buffer) (eq replace 'session))
1966 (ispell-send-string (concat "@" word "\n"))
1967 (flyspell-unhighlight-at cursor-location)
1968 (if (null ispell-pdict-modified-p)
1969 (setq ispell-pdict-modified-p
1970 (list ispell-pdict-modified-p)))
1972 (if (eq replace 'buffer)
1973 (ispell-add-per-file-word-list word)))
1975 ;; This was added only to the Emacs side. I assume its absence on
1976 ;; the XEmacs side was an oversight. --Stef
1977 (flyspell-unhighlight-at cursor-location)
1978 (let ((old-max (point-max))
1979 (new-word (if (atom replace)
1982 (cursor-location (+ (- (length word) (- end start))
1984 (unless (equal new-word (car poss))
1985 (delete-region start end)
1987 (funcall flyspell-insert-function new-word)
1988 (if flyspell-abbrev-p
1989 (flyspell-define-abbrev word new-word)))
1990 ;; In the original Emacs code, this was only called in the body
1991 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
1992 (flyspell-ajust-cursor-point save cursor-location old-max)))
1997 ;*---------------------------------------------------------------------*/
1998 ;* flyspell-ajust-cursor-point ... */
1999 ;*---------------------------------------------------------------------*/
2000 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2001 (if (>= save cursor-location)
2002 (let ((new-pos (+ save (- (point-max) old-max))))
2004 ((< new-pos (point-min))
2006 ((> new-pos (point-max))
2011 ;*---------------------------------------------------------------------*/
2012 ;* flyspell-emacs-popup ... */
2013 ;*---------------------------------------------------------------------*/
2014 (defun flyspell-emacs-popup (event poss word)
2015 "The Emacs popup menu."
2017 (let* ((mouse-pos (mouse-position))
2018 (mouse-pos (if (nth 1 mouse-pos)
2020 (set-mouse-position (car mouse-pos)
2021 (/ (frame-width) 2) 2)
2023 (setq event (list (list (car (cdr mouse-pos))
2024 (1+ (cdr (cdr mouse-pos))))
2026 (let* ((corrects (if flyspell-sort-corrections
2027 (sort (car (cdr (cdr poss))) 'string<)
2028 (car (cdr (cdr poss)))))
2029 (cor-menu (if (consp corrects)
2030 (mapcar (lambda (correct)
2031 (list correct correct))
2034 (affix (car (cdr (cdr (cdr poss)))))
2035 (base-menu (let ((save (if (consp affix)
2037 (list (concat "Save affix: " (car affix))
2039 '("Accept (session)" session)
2040 '("Accept (buffer)" buffer))
2041 '(("Save word" save)
2042 ("Accept (session)" session)
2043 ("Accept (buffer)" buffer)))))
2044 (if (consp cor-menu)
2045 (append cor-menu (cons "" save))
2047 (menu (cons "flyspell correction menu" base-menu)))
2048 (car (x-popup-menu event
2049 (list (format "%s [%s]" word (or ispell-local-dictionary
2053 ;*---------------------------------------------------------------------*/
2054 ;* flyspell-xemacs-popup ... */
2055 ;*---------------------------------------------------------------------*/
2056 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
2057 "The XEmacs popup menu."
2058 (let* ((corrects (if flyspell-sort-corrections
2059 (sort (car (cdr (cdr poss))) 'string<)
2060 (car (cdr (cdr poss)))))
2061 (cor-menu (if (consp corrects)
2062 (mapcar (lambda (correct)
2064 (list 'flyspell-do-correct
2075 (affix (car (cdr (cdr (cdr poss)))))
2076 (menu (let ((save (if (consp affix)
2078 (concat "Save affix: " (car affix))
2079 (list 'flyspell-do-correct
2090 (list 'flyspell-do-correct
2099 (session (vector "Accept (session)"
2100 (list 'flyspell-do-correct
2109 (buffer (vector "Accept (buffer)"
2110 (list 'flyspell-do-correct
2119 (if (consp cor-menu)
2120 (append cor-menu (list "-" save session buffer))
2121 (list save session buffer)))))
2122 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2126 ;*---------------------------------------------------------------------*/
2127 ;* Some example functions for real autocorrecting */
2128 ;*---------------------------------------------------------------------*/
2129 (defun flyspell-maybe-correct-transposition (beg end poss)
2130 "Check replacements for transposed characters.
2132 If the text between BEG and END is equal to a correction suggested by
2133 Ispell, after transposing two adjacent characters, correct the text,
2136 The third arg POSS is either the symbol 'doublon' or a list of
2137 possible corrections as returned by `ispell-parse-output'.
2139 This function is meant to be added to `flyspell-incorrect-hook'."
2142 (let ((str (buffer-substring beg end))
2143 (i 0) (len (- end beg)) tmp)
2144 (while (< (1+ i) len)
2145 (setq tmp (aref str i))
2146 (aset str i (aref str (1+ i)))
2147 (aset str (1+ i) tmp)
2148 (when (member str (nth 2 poss))
2150 (goto-char (+ beg i 1))
2151 (transpose-chars 1))
2153 (setq tmp (aref str i))
2154 (aset str i (aref str (1+ i)))
2155 (aset str (1+ i) tmp)
2159 (defun flyspell-maybe-correct-doubling (beg end poss)
2160 "Check replacements for doubled characters.
2162 If the text between BEG and END is equal to a correction suggested by
2163 Ispell, after removing a pair of doubled characters, correct the text,
2166 The third arg POSS is either the symbol 'doublon' or a list of
2167 possible corrections as returned by `ispell-parse-output'.
2169 This function is meant to be added to `flyspell-incorrect-hook'."
2172 (let ((str (buffer-substring beg end))
2173 (i 0) (len (- end beg)))
2174 (while (< (1+ i) len)
2175 (when (and (= (aref str i) (aref str (1+ i)))
2176 (member (concat (substring str 0 (1+ i))
2177 (substring str (+ i 2)))
2179 (goto-char (+ beg i))
2185 ;*---------------------------------------------------------------------*/
2186 ;* flyspell-already-abbrevp ... */
2187 ;*---------------------------------------------------------------------*/
2188 (defun flyspell-already-abbrevp (table word)
2189 (let ((sym (abbrev-symbol word table)))
2190 (and sym (symbolp sym))))
2192 ;*---------------------------------------------------------------------*/
2193 ;* flyspell-change-abbrev ... */
2194 ;*---------------------------------------------------------------------*/
2195 (defun flyspell-change-abbrev (table old new)
2196 (set (abbrev-symbol old table) new))
2200 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2201 ;;; flyspell.el ends here