1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
8 ;; Keywords: convenience
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
32 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
33 ;; This applies only to the current buffer.
35 ;; To enable Flyspell in text representing computer programs, type
36 ;; M-x flyspell-prog-mode.
37 ;; In that mode only text inside comments is checked.
39 ;; Note: consider setting the variable ispell-parser to `tex' to
40 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
42 ;; Some user variables control the behavior of flyspell. They are
43 ;; those defined under the `User variables' comment.
49 ;;*---------------------------------------------------------------------*/
51 ;;*---------------------------------------------------------------------*/
52 (defgroup flyspell nil
53 "Spell checking on the fly."
59 ;;*---------------------------------------------------------------------*/
60 ;;* User configuration ... */
61 ;;*---------------------------------------------------------------------*/
62 (defcustom flyspell-highlight-flag t
63 "How Flyspell should indicate misspelled words.
64 Non-nil means use highlight, nil means use minibuffer messages."
68 (defcustom flyspell-mark-duplications-flag t
69 "Non-nil means Flyspell reports a repeated word as an error.
70 See `flyspell-mark-duplications-exceptions' to add exceptions to this rule.
71 Detection of repeated words is not implemented in
72 \"large\" regions; see `flyspell-large-region'."
76 (defcustom flyspell-mark-duplications-exceptions
77 '(("francais" .
("nous" "vous")))
78 "A list of exceptions for duplicated words.
79 It should be a list of (LANGUAGE . EXCEPTION-LIST). LANGUAGE is matched
80 against the current dictionary and EXCEPTION-LIST is a list of strings.
81 The duplicated word is downcased before it is compared with the exceptions."
83 :type
'(alist :key-type string
:value-type
(repeat string
)))
85 (defcustom flyspell-sort-corrections nil
86 "Non-nil means, sort the corrections alphabetically before popping them."
91 (defcustom flyspell-duplicate-distance -
1
92 "The maximum distance for finding duplicates of unrecognized words.
93 This applies to the feature that when a word is not found in the dictionary,
94 if the same spelling occurs elsewhere in the buffer,
95 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
96 This variable specifies how far to search to find such a duplicate.
97 -1 means no limit (search the whole buffer).
98 0 means do not search for duplicate unrecognized spellings."
103 (defcustom flyspell-delay
3
104 "The number of seconds to wait before checking, after a \"delayed\" command."
108 (defcustom flyspell-persistent-highlight t
109 "Non-nil means misspelled words remain highlighted until corrected.
110 If this variable is nil, only the most recently detected misspelled word
115 (defcustom flyspell-highlight-properties t
116 "Non-nil means highlight incorrect words even if a property exists for this word."
120 (defcustom flyspell-default-delayed-commands
121 '(self-insert-command
123 backward-or-forward-delete-char
125 scrollbar-vertical-drag
126 backward-delete-char-untabify
)
127 "The standard list of delayed commands for Flyspell.
128 See `flyspell-delayed-commands'."
131 :type
'(repeat (symbol)))
133 (defcustom flyspell-delayed-commands nil
134 "List of commands that are \"delayed\" for Flyspell mode.
135 After these commands, Flyspell checking is delayed for a short time,
136 whose length is specified by `flyspell-delay'."
138 :type
'(repeat (symbol)))
140 (defcustom flyspell-default-deplacement-commands
145 "The standard list of deplacement commands for Flyspell.
146 See `flyspell-deplacement-commands'."
149 :type
'(repeat (symbol)))
151 (defcustom flyspell-deplacement-commands nil
152 "List of commands that are \"deplacement\" for Flyspell mode.
153 After these commands, Flyspell checking is performed only if the previous
154 command was not the very same command."
157 :type
'(repeat (symbol)))
159 (defcustom flyspell-issue-welcome-flag t
160 "Non-nil means that Flyspell should display a welcome message when started."
164 (defcustom flyspell-issue-message-flag t
165 "Non-nil means that Flyspell emits messages when checking words."
169 (defcustom flyspell-incorrect-hook nil
170 "List of functions to be called when incorrect words are encountered.
171 Each function is given three arguments. The first two
172 arguments are the beginning and the end of the incorrect region.
173 The third is either the symbol `doublon' or the list
174 of possible corrections as returned by `ispell-parse-output'.
176 If any of the functions return non-nil, the word is not highlighted as
182 (defcustom flyspell-default-dictionary nil
183 "A string that is the name of the default dictionary.
184 This is passed to the `ispell-change-dictionary' when flyspell is started.
185 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
186 when flyspell is started, the value of that variable is used instead
187 of `flyspell-default-dictionary' to select the default dictionary.
188 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
189 Ispell's ultimate default dictionary."
192 :type
'(choice string
(const :tag
"Default" nil
)))
194 (defcustom flyspell-tex-command-regexp
195 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
196 "A string that is the regular expression that matches TeX commands."
201 (defcustom flyspell-check-tex-math-command nil
202 "Non-nil means check even inside TeX math environment.
203 TeX math environments are discovered by the TEXMATHP that implemented
204 inside the texmathp.el Emacs package. That package may be found at:
205 http://strw.leidenuniv.nl/~dominik/Tools"
209 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
210 '("francais" "deutsch8" "norsk")
211 "List of dictionary names that consider `-' as word delimiter."
214 :type
'(repeat (string)))
216 (defcustom flyspell-abbrev-p
218 "If non-nil, add correction to abbreviation table."
223 (defcustom flyspell-use-global-abbrev-table-p
225 "If non-nil, prefer global abbrev table to local abbrev table."
230 (defcustom flyspell-mode-line-string
" Fly"
231 "String displayed on the modeline when flyspell is active.
232 Set this to nil if you don't want a modeline indicator."
234 :type
'(choice string
(const :tag
"None" nil
)))
236 (defcustom flyspell-large-region
1000
237 "The threshold that determines if a region is small.
238 If the region is smaller than this number of characters,
239 `flyspell-region' checks the words sequentially using regular
240 flyspell methods. Else, if the region is large, a new Ispell process is
243 Doubled words are not detected in a large region, because Ispell
244 does not check for them.
246 If `flyspell-large-region' is nil, all regions are treated as small."
249 :type
'(choice number
(const :tag
"All small" nil
)))
251 (defcustom flyspell-insert-function
(function insert
)
252 "Function for inserting word by flyspell upon correction."
256 (defcustom flyspell-before-incorrect-word-string nil
257 "String used to indicate an incorrect word starting."
259 :type
'(choice string
(const nil
)))
261 (defcustom flyspell-after-incorrect-word-string nil
262 "String used to indicate an incorrect word ending."
264 :type
'(choice string
(const nil
)))
266 (defcustom flyspell-use-meta-tab t
267 "Non-nil means that flyspell uses M-TAB to correct word."
271 (defcustom flyspell-auto-correct-binding
273 "The key binding for flyspell auto correction."
276 ;;*---------------------------------------------------------------------*/
277 ;;* Mode specific options */
278 ;;* ------------------------------------------------------------- */
279 ;;* Mode specific options enable users to disable flyspell on */
280 ;;* certain word depending of the emacs mode. For instance, when */
281 ;;* using flyspell with mail-mode add the following expression */
282 ;;* in your .emacs file: */
283 ;;* (add-hook 'mail-mode */
284 ;;* '(lambda () (setq flyspell-generic-check-word-predicate */
285 ;;* 'mail-mode-flyspell-verify))) */
286 ;;*---------------------------------------------------------------------*/
287 (defvar flyspell-generic-check-word-predicate nil
288 "Function providing per-mode customization over which words are flyspelled.
289 Returns t to continue checking, nil otherwise.
290 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
291 property of the major mode name.")
292 (make-variable-buffer-local 'flyspell-generic-check-word-predicate
)
293 (defvaralias 'flyspell-generic-check-word-p
294 'flyspell-generic-check-word-predicate
)
296 ;;*--- mail mode -------------------------------------------------------*/
297 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
298 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
299 (defvar message-signature-separator
)
300 (defun mail-mode-flyspell-verify ()
301 "Function used for `flyspell-generic-check-word-predicate' 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 "Function used for `flyspell-generic-check-word-predicate' 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 "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
340 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil 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
)
352 (put 'nxml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
354 (defun sgml-mode-flyspell-verify ()
355 "Function used for `flyspell-generic-check-word-predicate' in SGML mode."
357 (let ((this (point-marker))
358 (s (progn (beginning-of-line) (point-marker)))
359 (e (progn (end-of-line) (point-marker))))
362 (and (re-search-forward "[^<]*>" e t
)
363 (= (match-beginning 0) this
)))
366 (and (re-search-backward "<[^>]*" s t
)
367 (= (match-end 0) this
)))
370 (and (re-search-forward "[^&]*;" e t
)
371 (= (match-beginning 0) this
)))
374 (and (re-search-backward "&[^;]*" s t
)
375 (= (match-end 0) this
)))))))))
377 ;;*---------------------------------------------------------------------*/
378 ;;* Programming mode */
379 ;;*---------------------------------------------------------------------*/
380 (defvar flyspell-prog-text-faces
381 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
382 "Faces corresponding to text in programming-mode buffers.")
384 (defun flyspell-generic-progmode-verify ()
385 "Used for `flyspell-generic-check-word-predicate' in programming modes."
386 (let ((f (get-text-property (point) 'face
)))
387 (memq f flyspell-prog-text-faces
)))
390 (defun flyspell-prog-mode ()
391 "Turn on `flyspell-mode' for comments and strings."
393 (setq flyspell-generic-check-word-predicate
394 'flyspell-generic-progmode-verify
)
396 (run-hooks 'flyspell-prog-mode-hook
))
398 ;;*---------------------------------------------------------------------*/
399 ;;* Overlay compatibility */
400 ;;*---------------------------------------------------------------------*/
401 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
402 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
403 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
404 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
405 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
406 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
407 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
408 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
410 ;;*---------------------------------------------------------------------*/
411 ;;* The minor mode declaration. */
412 ;;*---------------------------------------------------------------------*/
413 (defvar flyspell-mouse-map
414 (let ((map (make-sparse-keymap)))
415 (define-key map
(if (featurep 'xemacs
) [button2] [down-mouse-2])
416 #'flyspell-correct-word)
418 "Keymap for Flyspell to put on erroneous words.")
420 (defvar flyspell-mode-map
421 (let ((map (make-sparse-keymap)))
422 (if flyspell-use-meta-tab
423 (define-key map "\M-\t" 'flyspell-auto-correct-word))
424 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
425 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
426 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
427 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
429 "Minor mode keymap for Flyspell mode--for the whole buffer.")
431 ;; dash character machinery
432 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
433 "*Non-nil means that the `-' char is considered as a word delimiter.")
434 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
435 (defvar flyspell-dash-dictionary nil)
436 (make-variable-buffer-local 'flyspell-dash-dictionary)
437 (defvar flyspell-dash-local-dictionary nil)
438 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
440 ;;*---------------------------------------------------------------------*/
442 ;;*---------------------------------------------------------------------*/
443 (defface flyspell-incorrect
444 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
446 "Face used for marking a misspelled word in Flyspell."
448 ;; backward-compatibility alias
449 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
451 (defface flyspell-duplicate
452 '((((class color)) (:foreground "Gold3" :bold t :underline t))
454 "Face used for marking a misspelled word that appears twice in the buffer.
455 See also `flyspell-duplicate-distance'."
457 ;; backward-compatibility alias
458 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
460 (defvar flyspell-overlay nil)
462 ;;*---------------------------------------------------------------------*/
463 ;;* flyspell-mode ... */
464 ;;*---------------------------------------------------------------------*/
465 ;;;###autoload(defvar flyspell-mode nil)
467 (define-minor-mode flyspell-mode
468 "Minor mode performing on-the-fly spelling checking.
469 This spawns a single Ispell process and checks each word.
470 The default flyspell behavior is to highlight incorrect words.
471 With no argument, this command toggles Flyspell mode.
472 With a prefix argument ARG, turn Flyspell minor mode on if ARG is positive,
473 otherwise turn it off.
476 \\[ispell-word]: correct words (using Ispell).
477 \\[flyspell-auto-correct-word]: automatically correct word.
478 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
479 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
482 This runs `flyspell-mode-hook' after flyspell is entered.
485 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
486 valid. For instance, a personal dictionary can be used by
487 invoking `ispell-change-dictionary'.
489 Consider using the `ispell-parser' to check your text. For instance
491 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
494 \\[flyspell-region] checks all words inside a region.
495 \\[flyspell-buffer] checks the whole buffer."
496 :lighter flyspell-mode-line-string
497 :keymap flyspell-mode-map
502 (error (message "Enabling Flyspell mode gave an error")
504 (flyspell-mode-off)))
507 (defun turn-on-flyspell ()
508 "Unconditionally turn on Flyspell mode."
512 (defun turn-off-flyspell ()
513 "Unconditionally turn off Flyspell mode."
516 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
518 ;;*---------------------------------------------------------------------*/
519 ;;* flyspell-buffers ... */
520 ;;* ------------------------------------------------------------- */
521 ;;* For remembering buffers running flyspell */
522 ;;*---------------------------------------------------------------------*/
523 (defvar flyspell-buffers nil)
525 ;;*---------------------------------------------------------------------*/
526 ;;* flyspell-minibuffer-p ... */
527 ;;*---------------------------------------------------------------------*/
528 (defun flyspell-minibuffer-p (buffer)
529 "Is BUFFER a minibuffer?"
530 (let ((ws (get-buffer-window-list buffer t)))
531 (and (consp ws) (window-minibuffer-p (car ws)))))
533 ;;*---------------------------------------------------------------------*/
534 ;;* flyspell-accept-buffer-local-defs ... */
535 ;;*---------------------------------------------------------------------*/
536 (defvar flyspell-last-buffer nil
537 "The buffer in which the last flyspell operation took place.")
539 (defun flyspell-accept-buffer-local-defs (&optional force)
540 ;; When flyspell-word is used inside a loop (e.g. when processing
541 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
542 ;; up dwarfing everything else, so only do it when the buffer has changed.
543 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
544 (setq flyspell-last-buffer (current-buffer))
545 ;; Strange problem: If buffer in current window has font-lock turned on,
546 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
547 ;; call will reset the buffer to the buffer in the current window.
548 ;; However, it only happens at startup (fix by Albert L. Ting).
550 (ispell-accept-buffer-local-defs))
551 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
552 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
553 ;; The dictionary has changed
554 (setq flyspell-dash-dictionary ispell-dictionary)
555 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
556 (setq flyspell-consider-dash-as-word-delimiter-flag
557 (member (or ispell-local-dictionary ispell-dictionary)
558 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
560 (defun flyspell-hack-local-variables-hook ()
561 ;; When local variables are loaded, see if the dictionary context
563 (flyspell-accept-buffer-local-defs 'force))
565 (defun flyspell-kill-ispell-hook ()
566 (setq flyspell-last-buffer nil)
567 (dolist (buf (buffer-list))
568 (with-current-buffer buf
569 (kill-local-variable 'flyspell-word-cache-word))))
571 ;; Make sure we flush our caches when needed. Do it here rather than in
572 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
574 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
576 ;;*---------------------------------------------------------------------*/
577 ;;* flyspell-mode-on ... */
578 ;;*---------------------------------------------------------------------*/
579 (defun flyspell-mode-on ()
580 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
581 (ispell-maybe-find-aspell-dictionaries)
582 (setq ispell-highlight-face 'flyspell-incorrect)
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 ;; Pass the `force' argument for the case where flyspell was active already
591 ;; but the buffer's local-defs have been edited.
592 (flyspell-accept-buffer-local-defs 'force)
593 ;; we put the `flyspell-delayed' property on some commands
594 (flyspell-delay-commands)
595 ;; we put the `flyspell-deplacement' property on some commands
596 (flyspell-deplacement-commands)
597 ;; we bound flyspell action to post-command hook
598 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
599 ;; we bound flyspell action to pre-command hook
600 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
601 ;; we bound flyspell action to after-change hook
602 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
603 ;; we bound flyspell action to hack-local-variables-hook
604 (add-hook 'hack-local-variables-hook
605 (function flyspell-hack-local-variables-hook) t t)
606 ;; set flyspell-generic-check-word-predicate based on the major mode
607 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
609 (setq flyspell-generic-check-word-predicate mode-predicate)))
610 ;; the welcome message
611 (if (and flyspell-issue-message-flag
612 flyspell-issue-welcome-flag
614 (let ((binding (where-is-internal 'flyspell-auto-correct-word
618 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
619 (key-description binding))
620 "Welcome to flyspell. Use Mouse-2 to correct words."))))
621 ;; we end with the flyspell hooks
622 (run-hooks 'flyspell-mode-hook))
624 ;;*---------------------------------------------------------------------*/
625 ;;* flyspell-delay-commands ... */
626 ;;*---------------------------------------------------------------------*/
627 (defun flyspell-delay-commands ()
628 "Install the standard set of Flyspell delayed commands."
629 (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
630 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
632 ;;*---------------------------------------------------------------------*/
633 ;;* flyspell-delay-command ... */
634 ;;*---------------------------------------------------------------------*/
635 (defun flyspell-delay-command (command)
636 "Set COMMAND to be delayed, for Flyspell.
637 When flyspell `post-command-hook' is invoked because a delayed command
638 as been used the current word is not immediately checked.
639 It will be checked only after `flyspell-delay' seconds."
640 (interactive "SDelay Flyspell after Command: ")
641 (put command 'flyspell-delayed t))
643 ;;*---------------------------------------------------------------------*/
644 ;;* flyspell-deplacement-commands ... */
645 ;;*---------------------------------------------------------------------*/
646 (defun flyspell-deplacement-commands ()
647 "Install the standard set of Flyspell deplacement commands."
648 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
649 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
651 ;;*---------------------------------------------------------------------*/
652 ;;* flyspell-deplacement-command ... */
653 ;;*---------------------------------------------------------------------*/
654 (defun flyspell-deplacement-command (command)
655 "Set COMMAND that implement cursor movements, for Flyspell.
656 When flyspell `post-command-hook' is invoked because of a deplacement command
657 as been used the current word is checked only if the previous command was
658 not the very same deplacement command."
659 (interactive "SDeplacement Flyspell after Command: ")
660 (put command 'flyspell-deplacement t))
662 ;;*---------------------------------------------------------------------*/
663 ;;* flyspell-word-cache ... */
664 ;;*---------------------------------------------------------------------*/
665 (defvar flyspell-word-cache-start nil)
666 (defvar flyspell-word-cache-end nil)
667 (defvar flyspell-word-cache-word nil)
668 (defvar flyspell-word-cache-result '_)
669 (make-variable-buffer-local 'flyspell-word-cache-start)
670 (make-variable-buffer-local 'flyspell-word-cache-end)
671 (make-variable-buffer-local 'flyspell-word-cache-word)
672 (make-variable-buffer-local 'flyspell-word-cache-result)
674 ;;*---------------------------------------------------------------------*/
675 ;;* The flyspell pre-hook, store the current position. In the */
676 ;;* post command hook, we will check, if the word at this position */
677 ;;* has to be spell checked. */
678 ;;*---------------------------------------------------------------------*/
679 (defvar flyspell-pre-buffer nil)
680 (defvar flyspell-pre-point nil)
681 (defvar flyspell-pre-column nil)
682 (defvar flyspell-pre-pre-buffer nil)
683 (defvar flyspell-pre-pre-point nil)
685 ;;*---------------------------------------------------------------------*/
686 ;;* flyspell-previous-command ... */
687 ;;*---------------------------------------------------------------------*/
688 (defvar flyspell-previous-command nil
689 "The last interactive command checked by Flyspell.")
691 ;;*---------------------------------------------------------------------*/
692 ;;* flyspell-pre-command-hook ... */
693 ;;*---------------------------------------------------------------------*/
694 (defun flyspell-pre-command-hook ()
695 "Save the current buffer and point for Flyspell's post-command hook."
697 (setq flyspell-pre-buffer (current-buffer))
698 (setq flyspell-pre-point (point))
699 (setq flyspell-pre-column (current-column)))
701 ;;*---------------------------------------------------------------------*/
702 ;;* flyspell-mode-off ... */
703 ;;*---------------------------------------------------------------------*/
705 (defun flyspell-mode-off ()
706 "Turn Flyspell mode off."
707 ;; we remove the hooks
708 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
709 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
710 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
711 (remove-hook 'hack-local-variables-hook
712 (function flyspell-hack-local-variables-hook) t)
713 ;; we remove all the flyspell hilightings
714 (flyspell-delete-all-overlays)
715 ;; we have to erase pre cache variables
716 (setq flyspell-pre-buffer nil)
717 (setq flyspell-pre-point nil)
718 ;; we mark the mode as killed
719 (setq flyspell-mode nil))
721 ;;*---------------------------------------------------------------------*/
722 ;;* flyspell-check-pre-word-p ... */
723 ;;*---------------------------------------------------------------------*/
724 (defun flyspell-check-pre-word-p ()
725 "Return non-nil if we should check the word before point.
726 More precisely, it applies to the word that was before point
727 before the current command."
729 ((or (not (numberp flyspell-pre-point))
730 (not (bufferp flyspell-pre-buffer))
731 (not (buffer-live-p flyspell-pre-buffer)))
733 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
734 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
736 ((or (and (= flyspell-pre-point (- (point) 1))
737 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
738 (= flyspell-pre-point (point))
739 (= flyspell-pre-point (+ (point) 1)))
741 ((and (symbolp this-command)
742 (not executing-kbd-macro)
743 (or (get this-command 'flyspell-delayed)
744 (and (get this-command 'flyspell-deplacement)
745 (eq flyspell-previous-command this-command)))
746 (or (= (current-column) 0)
747 (= (current-column) flyspell-pre-column)
748 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
750 ((not (eq (current-buffer) flyspell-pre-buffer))
752 ((not (and (numberp flyspell-word-cache-start)
753 (numberp flyspell-word-cache-end)))
756 (or (< flyspell-pre-point flyspell-word-cache-start)
757 (> flyspell-pre-point flyspell-word-cache-end)))))
759 ;;*---------------------------------------------------------------------*/
760 ;;* The flyspell after-change-hook, store the change position. In */
761 ;;* the post command hook, we will check, if the word at this */
762 ;;* position has to be spell checked. */
763 ;;*---------------------------------------------------------------------*/
764 (defvar flyspell-changes nil)
765 (make-variable-buffer-local 'flyspell-changes)
767 ;;*---------------------------------------------------------------------*/
768 ;;* flyspell-after-change-function ... */
769 ;;*---------------------------------------------------------------------*/
770 (defun flyspell-after-change-function (start stop len)
771 "Save the current buffer and point for Flyspell's post-command hook."
772 (push (cons start stop) flyspell-changes))
774 ;;*---------------------------------------------------------------------*/
775 ;;* flyspell-check-changed-word-p ... */
776 ;;*---------------------------------------------------------------------*/
777 (defun flyspell-check-changed-word-p (start stop)
778 "Return t when the changed word has to be checked.
779 The answer depends of several criteria.
780 Mostly we check word delimiters."
782 ((and (memq (char-after start) '(?\n ? )) (> stop start))
784 ((not (numberp flyspell-pre-point))
786 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
788 ((let ((pos (point)))
789 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
794 ;;*---------------------------------------------------------------------*/
795 ;;* flyspell-check-word-p ... */
796 ;;*---------------------------------------------------------------------*/
797 (defun flyspell-check-word-p ()
798 "Return t when the word at `point' has to be checked.
799 The answer depends of several criteria.
800 Mostly we check word delimiters."
802 ((<= (- (point-max) 1) (point-min))
803 ;; the buffer is not filled enough
805 ((and (and (> (current-column) 0)
806 (not (eq (current-column) flyspell-pre-column)))
809 (and (looking-at (flyspell-get-not-casechars))
810 (or flyspell-consider-dash-as-word-delimiter-flag
811 (not (looking-at "-"))))))
812 ;; yes because we have reached or typed a word delimiter.
814 ((symbolp this-command)
816 ((get this-command 'flyspell-deplacement)
817 (not (eq flyspell-previous-command this-command)))
818 ((get this-command 'flyspell-delayed)
819 ;; the current command is not delayed, that
820 ;; is that we must check the word now
821 (and (not unread-command-events)
822 (sit-for flyspell-delay)))
826 ;;*---------------------------------------------------------------------*/
827 ;;* flyspell-debug-signal-no-check ... */
828 ;;*---------------------------------------------------------------------*/
829 (defun flyspell-debug-signal-no-check (msg obj)
830 (setq debug-on-error t)
831 (with-current-buffer (get-buffer-create "*flyspell-debug*")
833 (insert "NO-CHECK:\n")
834 (insert (format " %S : %S\n" msg obj))))
836 ;;*---------------------------------------------------------------------*/
837 ;;* flyspell-debug-signal-pre-word-checked ... */
838 ;;*---------------------------------------------------------------------*/
839 (defun flyspell-debug-signal-pre-word-checked ()
840 (setq debug-on-error t)
841 (with-current-buffer (get-buffer-create "*flyspell-debug*")
842 (insert "PRE-WORD:\n")
843 (insert (format " pre-point : %S\n" flyspell-pre-point))
844 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
845 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
846 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
847 (goto-char (point-max))))
849 ;;*---------------------------------------------------------------------*/
850 ;;* flyspell-debug-signal-word-checked ... */
851 ;;*---------------------------------------------------------------------*/
852 (defun flyspell-debug-signal-word-checked ()
853 (setq debug-on-error t)
854 (let ((oldbuf (current-buffer))
856 (with-current-buffer (get-buffer-create "*flyspell-debug*")
858 (insert (format " this-cmd : %S\n" this-command))
859 (insert (format " delayed : %S\n" (and (symbolp this-command)
860 (get this-command 'flyspell-delayed))))
861 (insert (format " point : %S\n" point))
862 (insert (format " prev-char : [%c] %S\n"
863 (with-current-buffer oldbuf
864 (let ((c (if (> (point) (point-min))
867 (char-after (point)))
870 (with-current-buffer oldbuf
871 (let ((c (if (> (point) (point-min))
874 (and (and (looking-at (flyspell-get-not-casechars)) 1)
875 (and (or flyspell-consider-dash-as-word-delimiter-flag
876 (not (looking-at "\\-"))) 2))))))
878 (insert (format " because : %S\n"
880 ((not (and (symbolp this-command)
881 (get this-command 'flyspell-delayed)))
882 ;; the current command is not delayed, that
883 ;; is that we must check the word now
885 ((with-current-buffer oldbuf
886 (let ((c (if (> (point) (point-min))
889 (and (looking-at (flyspell-get-not-casechars))
890 (or flyspell-consider-dash-as-word-delimiter-flag
891 (not (looking-at "\\-"))))))))
893 ;; yes because we have reached or typed a word delimiter.
895 ((not (integerp flyspell-delay))
896 ;; yes because the user had set up a no-delay configuration.
900 (goto-char (point-max)))))
902 ;;*---------------------------------------------------------------------*/
903 ;;* flyspell-debug-signal-changed-checked ... */
904 ;;*---------------------------------------------------------------------*/
905 (defun flyspell-debug-signal-changed-checked ()
906 (setq debug-on-error t)
907 (let ((point (point)))
908 (with-current-buffer (get-buffer-create "*flyspell-debug*")
909 (insert "CHANGED WORD:\n")
910 (insert (format " point : %S\n" point))
911 (goto-char (point-max)))))
913 ;;*---------------------------------------------------------------------*/
914 ;;* flyspell-post-command-hook ... */
915 ;;* ------------------------------------------------------------- */
916 ;;* It is possible that we check several words: */
917 ;;* 1- the current word is checked if the predicate */
918 ;;* FLYSPELL-CHECK-WORD-P is true */
919 ;;* 2- the word that used to be the current word before the */
920 ;;* THIS-COMMAND is checked if: */
921 ;;* a- the previous word is different from the current word */
922 ;;* b- the previous word as not just been checked by the */
923 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
924 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
925 ;;* previous word nor the current word */
926 ;;*---------------------------------------------------------------------*/
927 (defun flyspell-post-command-hook ()
928 "The `post-command-hook' used by flyspell to check a word in-the-fly."
930 (let ((command this-command)
931 ;; Prevent anything we do from affecting the mark.
933 (if (flyspell-check-pre-word-p)
934 (with-current-buffer flyspell-pre-buffer
935 '(flyspell-debug-signal-pre-word-checked)
937 (goto-char flyspell-pre-point)
939 (if (flyspell-check-word-p)
941 '(flyspell-debug-signal-word-checked)
943 ;; we remember which word we have just checked.
944 ;; this will be used next time we will check a word
945 ;; to compare the next current word with the word
946 ;; that as been registered in the pre-command-hook
947 ;; that is these variables are used within the predicate
948 ;; FLYSPELL-CHECK-PRE-WORD-P
949 (setq flyspell-pre-pre-buffer (current-buffer))
950 (setq flyspell-pre-pre-point (point)))
952 (setq flyspell-pre-pre-buffer nil)
953 (setq flyspell-pre-pre-point nil)
954 ;; when a word is not checked because of a delayed command
955 ;; we do not disable the ispell cache.
956 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
958 (setq flyspell-word-cache-end -1)
959 (setq flyspell-word-cache-result '_)))))
960 (while (and (not (input-pending-p)) (consp flyspell-changes))
961 (let ((start (car (car flyspell-changes)))
962 (stop (cdr (car flyspell-changes))))
963 (if (flyspell-check-changed-word-p start stop)
965 '(flyspell-debug-signal-changed-checked)
968 (setq flyspell-changes (cdr flyspell-changes))))
969 (setq flyspell-previous-command command)))
971 ;;*---------------------------------------------------------------------*/
972 ;;* flyspell-notify-misspell ... */
973 ;;*---------------------------------------------------------------------*/
974 (defun flyspell-notify-misspell (word poss)
975 (let ((replacements (if (stringp poss)
977 (if flyspell-sort-corrections
978 (sort (car (cdr (cdr poss))) 'string<)
979 (car (cdr (cdr poss)))))))
980 (if flyspell-issue-message-flag
981 (message "misspelling `%s' %S" word replacements))))
983 ;;*---------------------------------------------------------------------*/
984 ;;* flyspell-word-search-backward ... */
985 ;;*---------------------------------------------------------------------*/
986 (defun flyspell-word-search-backward (word bound)
989 (inhibit-point-motion-hooks t)
991 (while (and (not r) (setq p (search-backward word bound t)))
992 (let ((lw (flyspell-get-word '())))
993 (if (and (consp lw) (string-equal (car lw) word))
998 ;;*---------------------------------------------------------------------*/
999 ;;* flyspell-word-search-forward ... */
1000 ;;*---------------------------------------------------------------------*/
1001 (defun flyspell-word-search-forward (word bound)
1004 (inhibit-point-motion-hooks t)
1006 (while (and (not r) (setq p (search-forward word bound t)))
1007 (let ((lw (flyspell-get-word '())))
1008 (if (and (consp lw) (string-equal (car lw) word))
1010 (goto-char (1+ p)))))
1013 ;;*---------------------------------------------------------------------*/
1014 ;;* flyspell-word ... */
1015 ;;*---------------------------------------------------------------------*/
1016 (defun flyspell-word (&optional following)
1017 "Spell check a word."
1018 (interactive (list ispell-following-word))
1020 ;; use the correct dictionary
1021 (flyspell-accept-buffer-local-defs)
1022 (let* ((cursor-location (point))
1023 (flyspell-word (flyspell-get-word following))
1024 start end poss word ispell-filter)
1025 (if (or (eq flyspell-word nil)
1026 (and (fboundp flyspell-generic-check-word-predicate)
1027 (not (funcall flyspell-generic-check-word-predicate))))
1030 ;; destructure return flyspell-word info list.
1031 (setq start (car (cdr flyspell-word))
1032 end (car (cdr (cdr flyspell-word)))
1033 word (car flyspell-word))
1034 ;; before checking in the directory, we check for doublons.
1036 ((and (or (not (eq ispell-parser 'tex))
1037 (and (> start (point-min))
1038 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1039 flyspell-mark-duplications-flag
1040 (not (catch 'exception
1041 (dolist (except flyspell-mark-duplications-exceptions)
1042 (and (string= (or ispell-local-dictionary
1045 (member (downcase word) (cdr except))
1046 (throw 'exception t)))))
1052 (- (skip-chars-backward " \t\n\f"))))
1053 (p (when (>= bound (point-min))
1054 (flyspell-word-search-backward word bound))))
1055 (and p (/= p start)))))
1056 ;; yes, this is a doublon
1057 (flyspell-highlight-incorrect-region start end 'doublon)
1059 ((and (eq flyspell-word-cache-start start)
1060 (eq flyspell-word-cache-end end)
1061 (string-equal flyspell-word-cache-word word))
1062 ;; this word had been already checked, we skip
1063 flyspell-word-cache-result)
1064 ((and (eq ispell-parser 'tex)
1065 (flyspell-tex-command-p flyspell-word))
1066 ;; this is a correct word (because a tex command)
1067 (flyspell-unhighlight-at start)
1069 (flyspell-unhighlight-at (- end 1)))
1072 ;; we setup the cache
1073 (setq flyspell-word-cache-start start)
1074 (setq flyspell-word-cache-end end)
1075 (setq flyspell-word-cache-word word)
1076 ;; now check spelling of word.
1077 (ispell-send-string "%\n")
1078 ;; put in verbose mode
1079 (ispell-send-string (concat "^" word "\n"))
1080 ;; we mark the ispell process so it can be killed
1081 ;; when emacs is exited without query
1082 (set-process-query-on-exit-flag ispell-process nil)
1083 ;; Wait until ispell has processed word. Since this code is often
1084 ;; executed from post-command-hook but the ispell process may not
1085 ;; be responsive, it's important to make sure we re-enable C-g.
1088 (accept-process-output ispell-process)
1089 (not (string= "" (car ispell-filter))))))
1090 ;; (ispell-send-string "!\n")
1091 ;; back to terse mode.
1092 ;; Remove leading empty element
1093 (setq ispell-filter (cdr ispell-filter))
1094 ;; ispell process should return something after word is sent.
1095 ;; Tag word as valid (i.e., skip) otherwise
1097 (setq ispell-filter '(*)))
1098 (if (consp ispell-filter)
1099 (setq poss (ispell-parse-output (car ispell-filter))))
1100 (let ((res (cond ((eq poss t)
1102 (setq flyspell-word-cache-result t)
1103 (flyspell-unhighlight-at start)
1105 (flyspell-unhighlight-at (- end 1)))
1107 ((and (stringp poss) flyspell-highlight-flag)
1109 (setq flyspell-word-cache-result t)
1110 (flyspell-unhighlight-at start)
1112 (flyspell-unhighlight-at (- end 1)))
1115 (setq flyspell-word-cache-result t)
1116 (flyspell-unhighlight-at start)
1118 (flyspell-unhighlight-at (- end 1)))
1120 ((or (and (< flyspell-duplicate-distance 0)
1123 (flyspell-word-search-backward
1128 (flyspell-word-search-forward
1131 (and (> flyspell-duplicate-distance 0)
1134 (flyspell-word-search-backward
1137 flyspell-duplicate-distance)))
1140 (flyspell-word-search-forward
1143 flyspell-duplicate-distance))))))
1144 ;; This is a misspelled word which occurs
1145 ;; twice within flyspell-duplicate-distance.
1146 (setq flyspell-word-cache-result nil)
1147 (if flyspell-highlight-flag
1148 (flyspell-highlight-duplicate-region
1150 (message "duplicate `%s'" word))
1153 (setq flyspell-word-cache-result nil)
1154 ;; incorrect highlight the location
1155 (if flyspell-highlight-flag
1156 (flyspell-highlight-incorrect-region
1158 (flyspell-notify-misspell word poss))
1160 ;; return to original location
1161 (goto-char cursor-location)
1162 (if ispell-quit (setq ispell-quit nil))
1165 ;;*---------------------------------------------------------------------*/
1166 ;;* flyspell-tex-math-initialized ... */
1167 ;;*---------------------------------------------------------------------*/
1168 (defvar flyspell-tex-math-initialized nil)
1170 ;;*---------------------------------------------------------------------*/
1171 ;;* flyspell-math-tex-command-p ... */
1172 ;;* ------------------------------------------------------------- */
1173 ;;* This function uses the texmathp package to check if (point) */
1174 ;;* is within a tex command. In order to avoid using */
1175 ;;* condition-case each time we use the variable */
1176 ;;* flyspell-tex-math-initialized to make a special case the first */
1177 ;;* time that function is called. */
1178 ;;*---------------------------------------------------------------------*/
1179 (defun flyspell-math-tex-command-p ()
1180 (when (fboundp 'texmathp)
1182 (flyspell-check-tex-math-command
1184 ((eq flyspell-tex-math-initialized t)
1186 ((eq flyspell-tex-math-initialized 'error)
1189 (setq flyspell-tex-math-initialized t)
1193 (setq flyspell-tex-math-initialized 'error)
1196 ;;*---------------------------------------------------------------------*/
1197 ;;* flyspell-tex-command-p ... */
1198 ;;*---------------------------------------------------------------------*/
1199 (defun flyspell-tex-command-p (word)
1200 "Return t if WORD is a TeX command."
1202 (let ((b (car (cdr word))))
1203 (and (re-search-backward "\\\\" (- (point) 100) t)
1204 (or (= (match-end 0) b)
1205 (and (goto-char (match-end 0))
1206 (looking-at flyspell-tex-command-regexp)
1207 (>= (match-end 0) b))))))
1208 (flyspell-math-tex-command-p)))
1210 ;;*---------------------------------------------------------------------*/
1211 ;;* flyspell-casechars-cache ... */
1212 ;;*---------------------------------------------------------------------*/
1213 (defvar flyspell-casechars-cache nil)
1214 (defvar flyspell-ispell-casechars-cache nil)
1215 (make-variable-buffer-local 'flyspell-casechars-cache)
1216 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1218 ;;*---------------------------------------------------------------------*/
1219 ;;* flyspell-get-casechars ... */
1220 ;;*---------------------------------------------------------------------*/
1221 (defun flyspell-get-casechars ()
1222 "This function builds a string that is the regexp of word chars.
1223 In order to avoid one useless string construction,
1224 this function changes the last char of the `ispell-casechars' string."
1225 (let ((ispell-casechars (ispell-get-casechars)))
1227 ((eq ispell-parser 'tex)
1228 (setq flyspell-ispell-casechars-cache ispell-casechars)
1229 (setq flyspell-casechars-cache
1230 (concat (substring ispell-casechars
1232 (- (length ispell-casechars) 1))
1234 flyspell-casechars-cache)
1236 (setq flyspell-ispell-casechars-cache ispell-casechars)
1237 (setq flyspell-casechars-cache ispell-casechars)
1238 flyspell-casechars-cache))))
1240 ;;*---------------------------------------------------------------------*/
1241 ;;* flyspell-get-not-casechars-cache ... */
1242 ;;*---------------------------------------------------------------------*/
1243 (defvar flyspell-not-casechars-cache nil)
1244 (defvar flyspell-ispell-not-casechars-cache nil)
1245 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1246 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1248 ;;*---------------------------------------------------------------------*/
1249 ;;* flyspell-get-not-casechars ... */
1250 ;;*---------------------------------------------------------------------*/
1251 (defun flyspell-get-not-casechars ()
1252 "This function builds a string that is the regexp of non-word chars."
1253 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1255 ((eq ispell-parser 'tex)
1256 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1257 (setq flyspell-not-casechars-cache
1258 (concat (substring ispell-not-casechars
1260 (- (length ispell-not-casechars) 1))
1262 flyspell-not-casechars-cache)
1264 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1265 (setq flyspell-not-casechars-cache ispell-not-casechars)
1266 flyspell-not-casechars-cache))))
1268 ;;*---------------------------------------------------------------------*/
1269 ;;* flyspell-get-word ... */
1270 ;;*---------------------------------------------------------------------*/
1271 (defun flyspell-get-word (following &optional extra-otherchars)
1272 "Return the word for spell-checking according to Ispell syntax.
1273 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1274 is non-nil when called interactively, then the following word
1275 \(rather than preceding\) is checked when the cursor is not over a word.
1276 Optional second argument contains otherchars that can be included in word
1279 Word syntax described by `flyspell-dictionary-alist' (which see)."
1280 (let* ((flyspell-casechars (flyspell-get-casechars))
1281 (flyspell-not-casechars (flyspell-get-not-casechars))
1282 (ispell-otherchars (ispell-get-otherchars))
1283 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1284 (word-regexp (concat flyspell-casechars
1286 (if (not (string= "" ispell-otherchars))
1287 (concat ispell-otherchars "?"))
1288 (if extra-otherchars
1289 (concat extra-otherchars "?"))
1292 (if (or ispell-many-otherchars-p
1298 (if (not (looking-at flyspell-casechars))
1300 (re-search-forward flyspell-casechars nil t)
1301 (re-search-backward flyspell-casechars nil t)))
1302 ;; move to front of word
1303 (re-search-backward flyspell-not-casechars nil 'start)
1304 (while (and (or (and (not (string= "" ispell-otherchars))
1305 (looking-at ispell-otherchars))
1306 (and extra-otherchars (looking-at extra-otherchars)))
1308 (or (not did-it-once)
1309 ispell-many-otherchars-p)
1310 (not (eq prevpt (point))))
1311 (if (and extra-otherchars (looking-at extra-otherchars))
1314 (if (looking-at flyspell-casechars)
1315 (re-search-backward flyspell-not-casechars nil 'move)))
1319 (if (looking-at flyspell-casechars)
1320 (re-search-backward flyspell-not-casechars nil 'move)
1321 (backward-char -1))))
1322 ;; Now mark the word and save to string.
1323 (if (not (re-search-forward word-regexp nil t))
1326 (setq start (match-beginning 0)
1328 word (buffer-substring-no-properties start end))
1329 (list word start end)))))
1331 ;;*---------------------------------------------------------------------*/
1332 ;;* flyspell-small-region ... */
1333 ;;*---------------------------------------------------------------------*/
1334 (defun flyspell-small-region (beg end)
1335 "Flyspell text between BEG and END."
1343 (while (< (point) end)
1344 (if (and flyspell-issue-message-flag (= count 100))
1346 (message "Spell Checking...%d%%"
1347 (* 100 (/ (float (- (point) beg)) (- end beg))))
1349 (setq count (+ 1 count)))
1352 (let ((cur (point)))
1354 (if (and (< (point) end) (> (point) (+ cur 1)))
1355 (backward-char 1)))))
1357 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1360 ;;*---------------------------------------------------------------------*/
1361 ;;* flyspell-external-ispell-process ... */
1362 ;;*---------------------------------------------------------------------*/
1363 (defvar flyspell-external-ispell-process '()
1364 "The external Flyspell Ispell process.")
1366 ;;*---------------------------------------------------------------------*/
1367 ;;* flyspell-external-ispell-buffer ... */
1368 ;;*---------------------------------------------------------------------*/
1369 (defvar flyspell-external-ispell-buffer '())
1370 (defvar flyspell-large-region-buffer '())
1371 (defvar flyspell-large-region-beg (point-min))
1372 (defvar flyspell-large-region-end (point-max))
1374 ;;*---------------------------------------------------------------------*/
1375 ;;* flyspell-external-point-words ... */
1376 ;;*---------------------------------------------------------------------*/
1377 (defun flyspell-external-point-words ()
1378 "Mark words from a buffer listing incorrect words in order of appearance.
1379 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1380 \(We finish by killing that buffer and setting the variable to nil.)
1381 The buffer to mark them in is `flyspell-large-region-buffer'."
1382 (let (words-not-found
1383 (ispell-otherchars (ispell-get-otherchars))
1384 (buffer-scan-pos flyspell-large-region-beg)
1386 (with-current-buffer flyspell-external-ispell-buffer
1387 (goto-char (point-min))
1388 ;; Loop over incorrect words, in the order they were reported,
1389 ;; which is also the order they appear in the buffer being checked.
1390 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1391 ;; Bind WORD to the next one.
1392 (let ((word (match-string 1)) (wordpos (point)))
1393 ;; Here there used to be code to see if WORD is the same
1394 ;; as the previous iteration, and count the number of consecutive
1395 ;; identical words, and the loop below would search for that many.
1396 ;; That code seemed to be incorrect, and on principle, should
1397 ;; be unnecessary too. -- rms.
1398 (if flyspell-issue-message-flag
1399 (message "Spell Checking...%d%% [%s]"
1400 (* 100 (/ (float (point)) (point-max)))
1402 (with-current-buffer flyspell-large-region-buffer
1403 (goto-char buffer-scan-pos)
1405 ;; Iterate on string search until string is found as word,
1408 (if (search-forward word
1409 flyspell-large-region-end t)
1412 ;; Move back into the match
1413 ;; so flyspell-get-word will find it.
1415 (flyspell-get-word nil)))
1416 (found (car found-list))
1417 (found-length (length found))
1418 (misspell-length (length word)))
1420 ;; Size matches, we really found it.
1421 (= found-length misspell-length)
1422 ;; Matches as part of a boundary-char separated word
1424 (split-string found ispell-otherchars))
1425 ;; Misspelling has higher length than
1426 ;; what flyspell considers the
1427 ;; word. Caused by boundary-chars
1428 ;; mismatch. Validating seems safe.
1429 (< found-length misspell-length)
1430 ;; ispell treats beginning of some TeX
1431 ;; commands as nroff control sequences
1432 ;; and strips them in the list of
1433 ;; misspelled words thus giving a
1434 ;; non-existent word. Skip if ispell
1435 ;; is used, string is a TeX command
1436 ;; (char before beginning of word is
1437 ;; backslash) and none of the previous
1439 (and (not ispell-really-aspell)
1441 (goto-char (- (nth 1 found-list) 1))
1442 (if (looking-at "[\\]" )
1447 ;; Search for next misspelled word will begin from
1448 ;; end of last validated match.
1449 (setq buffer-scan-pos (point))))
1450 ;; Record if misspelling is not found and try new one
1451 (add-to-list 'words-not-found
1452 (concat " -> " word " - "
1453 (int-to-string wordpos)))
1454 (setq keep nil)))))))
1456 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1457 ;; Warn about not found misspellings
1458 (dolist (word words-not-found)
1459 (message "%s: word not found" word))
1460 ;; Kill and forget the buffer with the list of incorrect words.
1461 (kill-buffer flyspell-external-ispell-buffer)
1462 (setq flyspell-external-ispell-buffer nil)))
1464 ;;*---------------------------------------------------------------------*/
1465 ;;* flyspell-process-localwords ... */
1466 ;;* ------------------------------------------------------------- */
1467 ;;* This function is used to prevent marking of words explicitly */
1468 ;;* declared correct. */
1469 ;;*---------------------------------------------------------------------*/
1470 (defun flyspell-process-localwords (misspellings-buffer)
1471 (let (localwords case-fold-search
1472 (ispell-casechars (ispell-get-casechars)))
1473 ;; Get localwords from the original buffer
1475 (goto-char (point-min))
1476 ;; Localwords parsing copied from ispell.el.
1477 (while (search-forward ispell-words-keyword nil t)
1478 (let ((end (save-excursion (end-of-line) (point)))
1480 ;; buffer-local words separated by a space, and can contain
1481 ;; any character other than a space. Not rigorous enough.
1482 (while (re-search-forward " *\\([^ ]+\\)" end t)
1483 (setq string (buffer-substring-no-properties (match-beginning 1)
1485 ;; This can fail when string contains a word with invalid chars.
1486 ;; Error handling needs to be added between Ispell and Emacs.
1487 (if (and (< 1 (length string))
1488 (equal 0 (string-match ispell-casechars string)))
1489 (push string localwords))))))
1490 ;; Remove localwords matches from misspellings-buffer.
1491 ;; The usual mechanism of communicating the local words to ispell
1492 ;; does not affect the special ispell process used by
1493 ;; flyspell-large-region.
1494 (with-current-buffer misspellings-buffer
1496 (dolist (word localwords)
1497 (goto-char (point-min))
1498 (let ((regexp (concat "^" word "\n")))
1499 (while (re-search-forward regexp nil t)
1500 (delete-region (match-beginning 0) (match-end 0)))))))))
1502 ;;* ---------------------------------------------------------------
1503 ;;* flyspell-check-region-doublons
1504 ;;* ---------------------------------------------------------------
1505 (defun flyspell-check-region-doublons (beg end)
1506 "Check for adjacent duplicated words (doublons) in the given region."
1509 (flyspell-word) ; Make sure current word is checked
1511 (while (and (< (point) end)
1512 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1518 ;;*---------------------------------------------------------------------*/
1519 ;;* flyspell-large-region ... */
1520 ;;*---------------------------------------------------------------------*/
1521 (defun flyspell-large-region (beg end)
1522 (let* ((curbuf (current-buffer))
1523 (buffer (get-buffer-create "*flyspell-region*")))
1524 (setq flyspell-external-ispell-buffer buffer)
1525 (setq flyspell-large-region-buffer curbuf)
1526 (setq flyspell-large-region-beg beg)
1527 (setq flyspell-large-region-end end)
1528 (flyspell-accept-buffer-local-defs)
1531 ;; this is done, we can start checking...
1532 (if flyspell-issue-message-flag (message "Checking region..."))
1534 (ispell-check-version)
1535 ;; Local dictionary becomes the global dictionary in use.
1536 (setq ispell-current-dictionary
1537 (or ispell-local-dictionary ispell-dictionary))
1538 (setq ispell-current-personal-dictionary
1539 (or ispell-local-pdict ispell-personal-dictionary))
1540 (let ((args (ispell-get-ispell-args))
1541 (encoding (ispell-get-coding-system))
1543 (if (and ispell-current-dictionary ; use specified dictionary
1544 (not (member "-d" args))) ; only define if not overridden
1546 (append (list "-d" ispell-current-dictionary) args)))
1547 (if ispell-current-personal-dictionary ; use specified pers dict
1552 ispell-current-personal-dictionary)))))
1553 (setq args (append args ispell-extra-args))
1554 (if (and ispell-really-aspell
1555 ispell-aspell-supports-utf8)
1559 (concat "--encoding="
1562 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1563 (setq c (apply 'ispell-call-process-region beg
1569 (if ispell-really-aspell "list" "-l")
1573 (flyspell-process-localwords buffer)
1574 (with-current-buffer curbuf
1575 (flyspell-delete-region-overlays beg end)
1576 (flyspell-check-region-doublons beg end))
1577 (flyspell-external-point-words))
1578 (error "Can't check region...")))))
1580 ;;*---------------------------------------------------------------------*/
1581 ;;* flyspell-region ... */
1582 ;;* ------------------------------------------------------------- */
1583 ;;* Because `ispell -a' is too slow, it is not possible to use */
1584 ;;* it on large region. Then, when ispell is invoked on a large */
1585 ;;* text region, a new `ispell -l' process is spawned. The */
1586 ;;* pointed out words are then searched in the region a checked with */
1587 ;;* regular flyspell means. */
1588 ;;*---------------------------------------------------------------------*/
1590 (defun flyspell-region (beg end)
1591 "Flyspell text between BEG and END."
1600 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1601 (flyspell-large-region beg end)
1602 (flyspell-small-region beg end)))))
1604 ;;*---------------------------------------------------------------------*/
1605 ;;* flyspell-buffer ... */
1606 ;;*---------------------------------------------------------------------*/
1608 (defun flyspell-buffer ()
1609 "Flyspell whole buffer."
1611 (flyspell-region (point-min) (point-max)))
1613 ;;*---------------------------------------------------------------------*/
1614 ;;* old next error position ... */
1615 ;;*---------------------------------------------------------------------*/
1616 (defvar flyspell-old-buffer-error nil)
1617 (defvar flyspell-old-pos-error nil)
1619 ;;*---------------------------------------------------------------------*/
1620 ;;* flyspell-goto-next-error ... */
1621 ;;*---------------------------------------------------------------------*/
1622 (defun flyspell-goto-next-error ()
1623 "Go to the next previously detected error.
1624 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1629 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1630 (eq pos flyspell-old-pos-error))
1632 (if (= flyspell-old-pos-error max)
1633 ;; goto beginning of buffer
1635 (message "Restarting from beginning of buffer")
1636 (goto-char (point-min)))
1638 (setq pos (point))))
1639 ;; seek the next error
1640 (while (and (< pos max)
1641 (let ((ovs (overlays-at pos))
1643 (while (and (not r) (consp ovs))
1644 (if (flyspell-overlay-p (car ovs))
1646 (setq ovs (cdr ovs))))
1648 (setq pos (1+ pos)))
1649 ;; save the current location for next invocation
1650 (setq flyspell-old-pos-error pos)
1651 (setq flyspell-old-buffer-error (current-buffer))
1654 (message "No more miss-spelled word!"))))
1656 ;;*---------------------------------------------------------------------*/
1657 ;;* flyspell-overlay-p ... */
1658 ;;*---------------------------------------------------------------------*/
1659 (defun flyspell-overlay-p (o)
1660 "Return true if O is an overlay used by flyspell."
1661 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1663 ;;*---------------------------------------------------------------------*/
1664 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1665 ;;* ------------------------------------------------------------- */
1666 ;;* Remove overlays introduced by flyspell. */
1667 ;;*---------------------------------------------------------------------*/
1668 (defun flyspell-delete-region-overlays (beg end)
1669 "Delete overlays used by flyspell in a given region."
1670 (remove-overlays beg end 'flyspell-overlay t))
1673 (defun flyspell-delete-all-overlays ()
1674 "Delete all the overlays used by flyspell."
1675 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1677 ;;*---------------------------------------------------------------------*/
1678 ;;* flyspell-unhighlight-at ... */
1679 ;;*---------------------------------------------------------------------*/
1680 (defun flyspell-unhighlight-at (pos)
1681 "Remove the flyspell overlay that are located at POS."
1682 (if flyspell-persistent-highlight
1683 (let ((overlays (overlays-at pos)))
1684 (while (consp overlays)
1685 (if (flyspell-overlay-p (car overlays))
1686 (delete-overlay (car overlays)))
1687 (setq overlays (cdr overlays))))
1688 (if (flyspell-overlay-p flyspell-overlay)
1689 (delete-overlay flyspell-overlay))))
1691 ;;*---------------------------------------------------------------------*/
1692 ;;* flyspell-properties-at-p ... */
1693 ;;* ------------------------------------------------------------- */
1694 ;;* Is there an highlight properties at position pos? */
1695 ;;*---------------------------------------------------------------------*/
1696 (defun flyspell-properties-at-p (pos)
1697 "Return t if there is a text property at POS, not counting `local-map'.
1698 If variable `flyspell-highlight-properties' is set to nil,
1699 text with properties are not checked. This function is used to discover
1700 if the character at POS has any other property."
1701 (let ((prop (text-properties-at pos))
1703 (while (and keep (consp prop))
1704 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1705 (setq prop (cdr (cdr prop)))
1709 ;;*---------------------------------------------------------------------*/
1710 ;;* make-flyspell-overlay ... */
1711 ;;*---------------------------------------------------------------------*/
1712 (defun make-flyspell-overlay (beg end face mouse-face)
1713 "Allocate an overlay to highlight an incorrect word.
1714 BEG and END specify the range in the buffer of that word.
1715 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1717 (let ((overlay (make-overlay beg end nil t nil)))
1718 (overlay-put overlay 'face face)
1719 (overlay-put overlay 'mouse-face mouse-face)
1720 (overlay-put overlay 'flyspell-overlay t)
1721 (overlay-put overlay 'evaporate t)
1722 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1723 (overlay-put overlay 'keymap flyspell-mouse-map)
1724 (when (eq face 'flyspell-incorrect)
1725 (and (stringp flyspell-before-incorrect-word-string)
1726 (overlay-put overlay 'before-string
1727 flyspell-before-incorrect-word-string))
1728 (and (stringp flyspell-after-incorrect-word-string)
1729 (overlay-put overlay 'after-string
1730 flyspell-after-incorrect-word-string)))
1733 ;;*---------------------------------------------------------------------*/
1734 ;;* flyspell-highlight-incorrect-region ... */
1735 ;;*---------------------------------------------------------------------*/
1736 (defun flyspell-highlight-incorrect-region (beg end poss)
1737 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1738 POSS is usually a list of possible spelling/correction lists,
1739 as returned by `ispell-parse-output'.
1740 It can also be the symbol `doublon', in the case where the word
1741 is itself incorrect, but suspiciously repeated."
1742 (let ((inhibit-read-only t))
1743 (unless (run-hook-with-args-until-success
1744 'flyspell-incorrect-hook beg end poss)
1745 (if (or flyspell-highlight-properties
1746 (not (flyspell-properties-at-p beg)))
1748 ;; we cleanup all the overlay that are in the region, not
1749 ;; beginning at the word start position
1750 (if (< (1+ beg) end)
1751 (let ((os (overlays-in (1+ beg) end)))
1753 (if (flyspell-overlay-p (car os))
1754 (delete-overlay (car os)))
1755 (setq os (cdr os)))))
1756 ;; we cleanup current overlay at the same position
1757 (flyspell-unhighlight-at beg)
1758 ;; now we can use a new overlay
1759 (setq flyspell-overlay
1760 (make-flyspell-overlay
1762 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1765 ;;*---------------------------------------------------------------------*/
1766 ;;* flyspell-highlight-duplicate-region ... */
1767 ;;*---------------------------------------------------------------------*/
1768 (defun flyspell-highlight-duplicate-region (beg end poss)
1769 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1770 POSS is a list of possible spelling/correction lists,
1771 as returned by `ispell-parse-output'."
1772 (let ((inhibit-read-only t))
1773 (unless (run-hook-with-args-until-success
1774 'flyspell-incorrect-hook beg end poss)
1775 (if (or flyspell-highlight-properties
1776 (not (flyspell-properties-at-p beg)))
1778 ;; we cleanup current overlay at the same position
1779 (flyspell-unhighlight-at beg)
1780 ;; now we can use a new overlay
1781 (setq flyspell-overlay
1782 (make-flyspell-overlay beg end
1786 ;;*---------------------------------------------------------------------*/
1787 ;;* flyspell-auto-correct-cache ... */
1788 ;;*---------------------------------------------------------------------*/
1789 (defvar flyspell-auto-correct-pos nil)
1790 (defvar flyspell-auto-correct-region nil)
1791 (defvar flyspell-auto-correct-ring nil)
1792 (defvar flyspell-auto-correct-word nil)
1793 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1794 (make-variable-buffer-local 'flyspell-auto-correct-region)
1795 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1796 (make-variable-buffer-local 'flyspell-auto-correct-word)
1798 ;;*---------------------------------------------------------------------*/
1799 ;;* flyspell-check-previous-highlighted-word ... */
1800 ;;*---------------------------------------------------------------------*/
1801 (defun flyspell-check-previous-highlighted-word (&optional arg)
1802 "Correct the closer misspelled word.
1803 This function scans a mis-spelled word before the cursor. If it finds one
1804 it proposes replacement for that word. With prefix arg, count that many
1805 misspelled words backwards."
1807 (let ((pos1 (point))
1809 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1812 (while (and (setq pos (previous-overlay-change pos))
1815 (if (> pos (point-min))
1817 (setq ovs (overlays-at (1- pos)))
1820 (setq ovs (cdr ovs))
1821 (if (and (flyspell-overlay-p ov)
1822 (= 0 (setq arg (1- arg))))
1823 (throw 'exit t)))))))
1827 (error "No word to correct before point"))))
1829 ;;*---------------------------------------------------------------------*/
1830 ;;* flyspell-display-next-corrections ... */
1831 ;;*---------------------------------------------------------------------*/
1832 (defun flyspell-display-next-corrections (corrections)
1833 (let ((string "Corrections:")
1836 (while (< (length string) 80)
1837 (if (equal (car l) flyspell-auto-correct-word)
1838 (setq pos (cons (+ 1 (length string)) pos)))
1839 (setq string (concat string " " (car l)))
1842 (let ((num (car pos)))
1843 (put-text-property num
1844 (+ num (length flyspell-auto-correct-word))
1845 'face 'flyspell-incorrect
1847 (setq pos (cdr pos)))
1848 (if (fboundp 'display-message)
1849 (display-message 'no-log string)
1850 (message "%s" string))))
1852 ;;*---------------------------------------------------------------------*/
1853 ;;* flyspell-abbrev-table ... */
1854 ;;*---------------------------------------------------------------------*/
1855 (defun flyspell-abbrev-table ()
1856 (if flyspell-use-global-abbrev-table-p
1858 (or local-abbrev-table global-abbrev-table)))
1860 ;;*---------------------------------------------------------------------*/
1861 ;;* flyspell-define-abbrev ... */
1862 ;;*---------------------------------------------------------------------*/
1863 (defun flyspell-define-abbrev (name expansion)
1864 (let ((table (flyspell-abbrev-table)))
1866 (define-abbrev table (downcase name) expansion))))
1868 ;;*---------------------------------------------------------------------*/
1869 ;;* flyspell-auto-correct-word ... */
1870 ;;*---------------------------------------------------------------------*/
1871 (defun flyspell-auto-correct-word ()
1872 "Correct the current word.
1873 This command proposes various successive corrections for the current word."
1876 (old-max (point-max)))
1877 ;; use the correct dictionary
1878 (flyspell-accept-buffer-local-defs)
1879 (if (and (eq flyspell-auto-correct-pos pos)
1880 (consp flyspell-auto-correct-region))
1881 ;; we have already been using the function at the same location
1882 (let* ((start (car flyspell-auto-correct-region))
1883 (len (cdr flyspell-auto-correct-region)))
1884 (flyspell-unhighlight-at start)
1885 (delete-region start (+ start len))
1886 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1887 (let* ((word (car flyspell-auto-correct-ring))
1888 (len (length word)))
1889 (rplacd flyspell-auto-correct-region len)
1891 (if flyspell-abbrev-p
1892 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1893 flyspell-auto-correct-word)
1894 (flyspell-change-abbrev (flyspell-abbrev-table)
1895 flyspell-auto-correct-word
1897 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1898 (funcall flyspell-insert-function word)
1900 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1901 (flyspell-ajust-cursor-point pos (point) old-max)
1902 (setq flyspell-auto-correct-pos (point)))
1903 ;; fetch the word to be checked
1904 (let ((word (flyspell-get-word nil)))
1906 (let ((start (car (cdr word)))
1907 (end (car (cdr (cdr word))))
1910 (setq flyspell-auto-correct-word word)
1911 ;; now check spelling of word.
1912 (ispell-send-string "%\n") ;put in verbose mode
1913 (ispell-send-string (concat "^" word "\n"))
1914 ;; wait until ispell has processed word.
1916 (accept-process-output ispell-process)
1917 (not (string= "" (car ispell-filter)))))
1918 ;; Remove leading empty element
1919 (setq ispell-filter (cdr ispell-filter))
1920 ;; ispell process should return something after word is sent.
1921 ;; Tag word as valid (i.e., skip) otherwise
1923 (setq ispell-filter '(*)))
1924 (if (consp ispell-filter)
1925 (setq poss (ispell-parse-output (car ispell-filter))))
1927 ((or (eq poss t) (stringp poss))
1928 ;; don't correct word
1932 (error "Ispell: error in Ispell process"))
1934 ;; the word is incorrect, we have to propose a replacement
1935 (let ((replacements (if flyspell-sort-corrections
1936 (sort (car (cdr (cdr poss))) 'string<)
1937 (car (cdr (cdr poss))))))
1938 (setq flyspell-auto-correct-region nil)
1939 (if (consp replacements)
1941 (let ((replace (car replacements)))
1942 (let ((new-word replace))
1943 (if (not (equal new-word (car poss)))
1945 ;; the save the current replacements
1946 (setq flyspell-auto-correct-region
1947 (cons start (length new-word)))
1948 (let ((l replacements))
1949 (while (consp (cdr l))
1951 (rplacd l (cons (car poss) replacements)))
1952 (setq flyspell-auto-correct-ring
1954 (flyspell-unhighlight-at start)
1955 (delete-region start end)
1956 (funcall flyspell-insert-function new-word)
1957 (if flyspell-abbrev-p
1958 (if (flyspell-already-abbrevp
1959 (flyspell-abbrev-table) word)
1960 (flyspell-change-abbrev
1961 (flyspell-abbrev-table)
1964 (flyspell-define-abbrev word
1967 (flyspell-display-next-corrections
1968 (cons new-word flyspell-auto-correct-ring))
1969 (flyspell-ajust-cursor-point pos
1972 (setq flyspell-auto-correct-pos (point))
1973 (ispell-pdict-save t)))))))
1975 ;;*---------------------------------------------------------------------*/
1976 ;;* flyspell-auto-correct-previous-pos ... */
1977 ;;*---------------------------------------------------------------------*/
1978 (defvar flyspell-auto-correct-previous-pos nil
1979 "Holds the start of the first incorrect word before point.")
1981 ;;*---------------------------------------------------------------------*/
1982 ;;* flyspell-auto-correct-previous-hook ... */
1983 ;;*---------------------------------------------------------------------*/
1984 (defun flyspell-auto-correct-previous-hook ()
1985 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1986 Sets `flyspell-auto-correct-previous-pos' to nil"
1988 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1989 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1990 (setq flyspell-auto-correct-previous-pos nil)))
1992 ;;*---------------------------------------------------------------------*/
1993 ;;* flyspell-auto-correct-previous-word ... */
1994 ;;*---------------------------------------------------------------------*/
1995 (defun flyspell-auto-correct-previous-word (position)
1996 "Auto correct the first mispelled word that occurs before point.
1997 But don't look beyond what's visible on the screen."
2000 (let ((top (window-start))
2004 (narrow-to-region top bot)
2005 (overlay-recenter (point))
2007 (add-hook 'pre-command-hook
2008 (function flyspell-auto-correct-previous-hook) t t)
2010 (unless flyspell-auto-correct-previous-pos
2011 ;; only reset if a new overlay exists
2012 (setq flyspell-auto-correct-previous-pos nil)
2014 (let ((overlay-list (overlays-in (point-min) position))
2015 (new-overlay 'dummy-value))
2017 ;; search for previous (new) flyspell overlay
2018 (while (and new-overlay
2019 (or (not (flyspell-overlay-p new-overlay))
2020 ;; check if its face has changed
2021 (not (eq (get-char-property
2022 (overlay-start new-overlay) 'face)
2023 'flyspell-incorrect))))
2024 (setq new-overlay (car-safe overlay-list))
2025 (setq overlay-list (cdr-safe overlay-list)))
2027 ;; if nothing new exits new-overlay should be nil
2028 (if new-overlay ;; the length of the word may change so go to the start
2029 (setq flyspell-auto-correct-previous-pos
2030 (overlay-start new-overlay)))))
2032 (when flyspell-auto-correct-previous-pos
2034 (goto-char flyspell-auto-correct-previous-pos)
2035 (let ((ispell-following-word t)) ;; point is at start
2036 (if (numberp flyspell-auto-correct-previous-pos)
2037 (goto-char flyspell-auto-correct-previous-pos))
2038 (flyspell-auto-correct-word))
2039 ;; the point may have moved so reset this
2040 (setq flyspell-auto-correct-previous-pos (point))))))))
2042 ;;*---------------------------------------------------------------------*/
2043 ;;* flyspell-correct-word ... */
2044 ;;*---------------------------------------------------------------------*/
2046 (defun flyspell-correct-word (event)
2047 "Pop up a menu of possible corrections for a misspelled word.
2048 The word checked is the word at the mouse position."
2050 (let ((save (point)))
2051 (mouse-set-point event)
2052 (flyspell-correct-word-before-point event save)))
2054 (defun flyspell-correct-word-before-point (&optional event opoint)
2055 "Pop up a menu of possible corrections for misspelled word before point.
2056 If EVENT is non-nil, it is the mouse event that invoked this operation;
2057 that controls where to put the menu.
2058 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2060 (unless (mouse-position)
2061 (error "Pop-up menus do not work on this terminal"))
2062 ;; use the correct dictionary
2063 (flyspell-accept-buffer-local-defs)
2064 (or opoint (setq opoint (point-marker)))
2065 (let ((cursor-location (point))
2066 (word (flyspell-get-word nil)))
2068 (let ((start (car (cdr word)))
2069 (end (car (cdr (cdr word))))
2072 ;; now check spelling of word.
2073 (ispell-send-string "%\n") ;put in verbose mode
2074 (ispell-send-string (concat "^" word "\n"))
2075 ;; wait until ispell has processed word
2077 (accept-process-output ispell-process)
2078 (not (string= "" (car ispell-filter)))))
2079 ;; Remove leading empty element
2080 (setq ispell-filter (cdr ispell-filter))
2081 ;; ispell process should return something after word is sent.
2082 ;; Tag word as valid (i.e., skip) otherwise
2084 (setq ispell-filter '(*)))
2085 (if (consp ispell-filter)
2086 (setq poss (ispell-parse-output (car ispell-filter))))
2088 ((or (eq poss t) (stringp poss))
2089 ;; don't correct word
2093 (error "Ispell: error in Ispell process"))
2095 (flyspell-xemacs-popup
2096 poss word cursor-location start end opoint))
2098 ;; The word is incorrect, we have to propose a replacement.
2099 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2100 poss word cursor-location start end opoint)))
2101 (ispell-pdict-save t)))))
2103 ;;*---------------------------------------------------------------------*/
2104 ;;* flyspell-do-correct ... */
2105 ;;*---------------------------------------------------------------------*/
2106 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2107 "The popup menu callback."
2108 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2109 ;; it instead right after calling the function.
2110 (cond ((eq replace 'ignore)
2115 (ispell-send-string (concat "*" word "\n"))
2116 ;; This was added only to the XEmacs side in revision 1.18 of
2117 ;; flyspell. I assume its absence on the Emacs side was an
2118 ;; oversight. --Stef
2119 (ispell-send-string "#\n")
2120 (flyspell-unhighlight-at cursor-location)
2121 (setq ispell-pdict-modified-p '(t)))
2122 ((or (eq replace 'buffer) (eq replace 'session))
2123 (ispell-send-string (concat "@" word "\n"))
2124 (flyspell-unhighlight-at cursor-location)
2125 (if (null ispell-pdict-modified-p)
2126 (setq ispell-pdict-modified-p
2127 (list ispell-pdict-modified-p)))
2129 (if (eq replace 'buffer)
2130 (ispell-add-per-file-word-list word)))
2132 ;; This was added only to the Emacs side. I assume its absence on
2133 ;; the XEmacs side was an oversight. --Stef
2134 (flyspell-unhighlight-at cursor-location)
2135 (let ((old-max (point-max))
2136 (new-word (if (atom replace)
2139 (cursor-location (+ (- (length word) (- end start))
2141 (unless (equal new-word (car poss))
2142 (delete-region start end)
2144 (funcall flyspell-insert-function new-word)
2145 (if flyspell-abbrev-p
2146 (flyspell-define-abbrev word new-word)))
2147 ;; In the original Emacs code, this was only called in the body
2148 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2149 (flyspell-ajust-cursor-point save cursor-location old-max)))
2154 ;;*---------------------------------------------------------------------*/
2155 ;;* flyspell-ajust-cursor-point ... */
2156 ;;*---------------------------------------------------------------------*/
2157 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2158 (if (>= save cursor-location)
2159 (let ((new-pos (+ save (- (point-max) old-max))))
2161 ((< new-pos (point-min))
2163 ((> new-pos (point-max))
2168 ;;*---------------------------------------------------------------------*/
2169 ;;* flyspell-emacs-popup ... */
2170 ;;*---------------------------------------------------------------------*/
2171 (defun flyspell-emacs-popup (event poss word)
2172 "The Emacs popup menu."
2173 (unless window-system
2174 (error "This command requires pop-up dialogs"))
2176 (let* ((mouse-pos (mouse-position))
2177 (mouse-pos (if (nth 1 mouse-pos)
2179 (set-mouse-position (car mouse-pos)
2180 (/ (frame-width) 2) 2)
2182 (setq event (list (list (car (cdr mouse-pos))
2183 (1+ (cdr (cdr mouse-pos))))
2185 (let* ((corrects (if flyspell-sort-corrections
2186 (sort (car (cdr (cdr poss))) 'string<)
2187 (car (cdr (cdr poss)))))
2188 (cor-menu (if (consp corrects)
2189 (mapcar (lambda (correct)
2190 (list correct correct))
2193 (affix (car (cdr (cdr (cdr poss)))))
2195 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2197 (list (concat "Save affix: " (car affix))
2199 '("Accept (session)" session)
2200 '("Accept (buffer)" buffer))
2201 '(("Save word" save)
2202 ("Accept (session)" session)
2203 ("Accept (buffer)" buffer)))))
2204 (if (consp cor-menu)
2205 (append cor-menu (cons "" save))
2207 (menu (cons "flyspell correction menu" base-menu)))
2208 (car (x-popup-menu event
2209 (list (format "%s [%s]" word (or ispell-local-dictionary
2213 ;;*---------------------------------------------------------------------*/
2214 ;;* flyspell-xemacs-popup ... */
2215 ;;*---------------------------------------------------------------------*/
2216 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2217 "The XEmacs popup menu."
2218 (let* ((corrects (if flyspell-sort-corrections
2219 (sort (car (cdr (cdr poss))) 'string<)
2220 (car (cdr (cdr poss)))))
2221 (cor-menu (if (consp corrects)
2222 (mapcar (lambda (correct)
2224 (list 'flyspell-do-correct
2235 (affix (car (cdr (cdr (cdr poss)))))
2237 (menu (let ((save (if (and (consp affix) show-affix-info)
2239 (concat "Save affix: " (car affix))
2240 (list 'flyspell-do-correct
2251 (list 'flyspell-do-correct
2260 (session (vector "Accept (session)"
2261 (list 'flyspell-do-correct
2270 (buffer (vector "Accept (buffer)"
2271 (list 'flyspell-do-correct
2280 (if (consp cor-menu)
2281 (append cor-menu (list "-" save session buffer))
2282 (list save session buffer)))))
2283 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2287 ;;*---------------------------------------------------------------------*/
2288 ;;* Some example functions for real autocorrecting */
2289 ;;*---------------------------------------------------------------------*/
2290 (defun flyspell-maybe-correct-transposition (beg end poss)
2291 "Check replacements for transposed characters.
2293 If the text between BEG and END is equal to a correction suggested by
2294 Ispell, after transposing two adjacent characters, correct the text,
2297 The third arg POSS is either the symbol 'doublon' or a list of
2298 possible corrections as returned by `ispell-parse-output'.
2300 This function is meant to be added to `flyspell-incorrect-hook'."
2303 (let ((str (buffer-substring beg end))
2304 (i 0) (len (- end beg)) tmp)
2305 (while (< (1+ i) len)
2306 (setq tmp (aref str i))
2307 (aset str i (aref str (1+ i)))
2308 (aset str (1+ i) tmp)
2309 (when (member str (nth 2 poss))
2311 (goto-char (+ beg i 1))
2312 (transpose-chars 1))
2314 (setq tmp (aref str i))
2315 (aset str i (aref str (1+ i)))
2316 (aset str (1+ i) tmp)
2320 (defun flyspell-maybe-correct-doubling (beg end poss)
2321 "Check replacements for doubled characters.
2323 If the text between BEG and END is equal to a correction suggested by
2324 Ispell, after removing a pair of doubled characters, correct the text,
2327 The third arg POSS is either the symbol 'doublon' or a list of
2328 possible corrections as returned by `ispell-parse-output'.
2330 This function is meant to be added to `flyspell-incorrect-hook'."
2333 (let ((str (buffer-substring beg end))
2334 (i 0) (len (- end beg)))
2335 (while (< (1+ i) len)
2336 (when (and (= (aref str i) (aref str (1+ i)))
2337 (member (concat (substring str 0 (1+ i))
2338 (substring str (+ i 2)))
2340 (goto-char (+ beg i))
2346 ;;*---------------------------------------------------------------------*/
2347 ;;* flyspell-already-abbrevp ... */
2348 ;;*---------------------------------------------------------------------*/
2349 (defun flyspell-already-abbrevp (table word)
2350 (let ((sym (abbrev-symbol word table)))
2351 (and sym (symbolp sym))))
2353 ;;*---------------------------------------------------------------------*/
2354 ;;* flyspell-change-abbrev ... */
2355 ;;*---------------------------------------------------------------------*/
2356 (defun flyspell-change-abbrev (table old new)
2357 (set (abbrev-symbol old table) new))
2361 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2362 ;;; flyspell.el ends here