1 ;;; flyspell.el --- On-the-fly spell checker -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998, 2000-2016 Free Software Foundation, Inc.
5 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
6 ;; Maintainer: emacs-devel@gnu.org
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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
26 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
29 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
30 ;; This applies only to the current buffer.
32 ;; To enable Flyspell in text representing computer programs, type
33 ;; M-x flyspell-prog-mode.
34 ;; In that mode only text inside comments is checked.
36 ;; Some user variables control the behavior of flyspell. They are
37 ;; those defined under the `User variables' comment.
42 (eval-when-compile (require 'cl-lib))
44 ;;*---------------------------------------------------------------------*/
46 ;;*---------------------------------------------------------------------*/
47 (defgroup flyspell nil
48 "Spell checking on the fly."
54 ;;*---------------------------------------------------------------------*/
55 ;;* User configuration ... */
56 ;;*---------------------------------------------------------------------*/
57 (defcustom flyspell-highlight-flag t
58 "How Flyspell should indicate misspelled words.
59 Non-nil means use highlight, nil means use minibuffer messages."
63 (defcustom flyspell-mark-duplications-flag t
64 "Non-nil means Flyspell reports a repeated word as an error.
65 See `flyspell-mark-duplications-exceptions' to add exceptions to this rule.
66 Detection of repeated words is not implemented in
67 \"large\" regions; see variable `flyspell-large-region'."
71 (defcustom flyspell-mark-duplications-exceptions
72 '((nil . ("that" "had")) ; Common defaults for English.
73 ("\\`francais" . ("nous" "vous")))
74 "A list of exceptions for duplicated words.
75 It should be a list of (LANGUAGE . EXCEPTION-LIST).
77 LANGUAGE is nil, which means the exceptions apply regardless of
78 the current dictionary, or a regular expression matching the
79 dictionary name (`ispell-local-dictionary' or
80 `ispell-dictionary') for which the exceptions should apply.
82 EXCEPTION-LIST is a list of strings. The checked word is
83 downcased before comparing with these exceptions."
85 :type '(alist :key-type (choice (const :tag "All dictionaries" nil)
87 :value-type (repeat string))
90 (defcustom flyspell-sort-corrections nil
91 "Non-nil means, sort the corrections alphabetically before popping them."
96 (defcustom flyspell-duplicate-distance 400000
97 "The maximum distance for finding duplicates of unrecognized words.
98 This applies to the feature that when a word is not found in the dictionary,
99 if the same spelling occurs elsewhere in the buffer,
100 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
101 This variable specifies how far to search to find such a duplicate.
102 -1 means no limit (search the whole buffer).
103 0 means do not search for duplicate unrecognized spellings."
105 :version "24.5" ; -1 -> 400000
106 :type '(choice (const :tag "no limit" -1)
109 (defcustom flyspell-delay 3
110 "The number of seconds to wait before checking, after a \"delayed\" command."
114 (defcustom flyspell-persistent-highlight t
115 "Non-nil means misspelled words remain highlighted until corrected.
116 If this variable is nil, only the most recently detected misspelled word
121 (defcustom flyspell-highlight-properties t
122 "Non-nil means highlight incorrect words even if a property exists for this word."
126 (defcustom flyspell-default-delayed-commands
127 '(self-insert-command
129 backward-or-forward-delete-char
131 scrollbar-vertical-drag
132 backward-delete-char-untabify)
133 "The standard list of delayed commands for Flyspell.
134 See `flyspell-delayed-commands'."
137 :type '(repeat (symbol)))
139 (defcustom flyspell-delayed-commands nil
140 "List of commands that are \"delayed\" for Flyspell mode.
141 After these commands, Flyspell checking is delayed for a short time,
142 whose length is specified by `flyspell-delay'."
144 :type '(repeat (symbol)))
146 (defcustom flyspell-default-deplacement-commands
147 '(next-line previous-line
148 handle-switch-frame handle-select-window
151 "The standard list of deplacement commands for Flyspell.
152 See variable `flyspell-deplacement-commands'."
155 :type '(repeat (symbol)))
157 (defcustom flyspell-deplacement-commands nil
158 "List of commands that are \"deplacement\" for Flyspell mode.
159 After these commands, Flyspell checking is performed only if the previous
160 command was not the very same command."
163 :type '(repeat (symbol)))
165 (defcustom flyspell-issue-welcome-flag t
166 "Non-nil means that Flyspell should display a welcome message when started."
170 (defcustom flyspell-issue-message-flag t
171 "Non-nil means that Flyspell emits messages when checking words."
175 (defcustom flyspell-incorrect-hook nil
176 "List of functions to be called when incorrect words are encountered.
177 Each function is given three arguments. The first two
178 arguments are the beginning and the end of the incorrect region.
179 The third is either the symbol `doublon' or the list
180 of possible corrections as returned by `ispell-parse-output'.
182 If any of the functions return non-nil, the word is not highlighted as
188 (defcustom flyspell-default-dictionary nil
189 "A string that is the name of the default dictionary.
190 This is passed to the `ispell-change-dictionary' when flyspell is started.
191 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
192 when flyspell is started, the value of that variable is used instead
193 of `flyspell-default-dictionary' to select the default dictionary.
194 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
195 Ispell's ultimate default dictionary."
198 :type '(choice string (const :tag "Default" nil)))
200 (defcustom flyspell-tex-command-regexp
201 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
202 "A string that is the regular expression that matches TeX commands."
207 (defcustom flyspell-check-tex-math-command nil
208 "Non-nil means check even inside TeX math environment.
209 TeX math environments are discovered by `texmathp', implemented
210 inside AUCTeX package. That package may be found at
211 URL `http://www.gnu.org/software/auctex/'"
215 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
216 '("francais" "deutsch8" "norsk")
217 "List of dictionary names that consider `-' as word delimiter."
220 :type '(repeat (string)))
222 (defcustom flyspell-abbrev-p
224 "If non-nil, add correction to abbreviation table."
229 (defcustom flyspell-use-global-abbrev-table-p
231 "If non-nil, prefer global abbrev table to local abbrev table."
236 (defcustom flyspell-mode-line-string " Fly"
237 "String displayed on the mode line when flyspell is active.
238 Set this to nil if you don't want a mode line indicator."
240 :type '(choice string (const :tag "None" nil)))
242 (defcustom flyspell-large-region 1000
243 "The threshold that determines if a region is small.
244 If the region is smaller than this number of characters,
245 `flyspell-region' checks the words sequentially using regular
246 flyspell methods. Else, if the region is large, a new Ispell process is
249 Doubled words are not detected in a large region, because Ispell
250 does not check for them.
252 If this variable is nil, all regions are treated as small."
255 :type '(choice number (const :tag "All small" nil)))
257 (defcustom flyspell-insert-function (function insert)
258 "Function for inserting word by flyspell upon correction."
262 (defcustom flyspell-before-incorrect-word-string nil
263 "String used to indicate an incorrect word starting."
265 :type '(choice string (const nil)))
267 (defcustom flyspell-after-incorrect-word-string nil
268 "String used to indicate an incorrect word ending."
270 :type '(choice string (const nil)))
272 (defvar flyspell-mode-map)
274 (defcustom flyspell-use-meta-tab t
275 "Non-nil means that flyspell uses M-TAB to correct word."
278 :initialize 'custom-initialize-default
279 :set (lambda (sym val)
280 (define-key flyspell-mode-map "\M-\t"
282 'flyspell-auto-correct-word))))
284 (defcustom flyspell-auto-correct-binding
286 "The key binding for flyspell auto correction."
290 ;;*---------------------------------------------------------------------*/
291 ;;* Mode specific options */
292 ;;* ------------------------------------------------------------- */
293 ;;* Mode specific options enable users to disable flyspell on */
294 ;;* certain word depending of the emacs mode. For instance, when */
295 ;;* using flyspell with mail-mode add the following expression */
296 ;;* in your init file: */
297 ;;* (add-hook 'mail-mode */
298 ;;* (lambda () (setq flyspell-generic-check-word-predicate */
299 ;;* 'mail-mode-flyspell-verify))) */
300 ;;*---------------------------------------------------------------------*/
301 (defvar flyspell-generic-check-word-predicate nil
302 "Function providing per-mode customization over which words are flyspelled.
303 Returns t to continue checking, nil otherwise.
304 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
305 property of the major mode name.")
306 (make-variable-buffer-local 'flyspell-generic-check-word-predicate)
307 (define-obsolete-variable-alias 'flyspell-generic-check-word-p
308 'flyspell-generic-check-word-predicate "25.1")
310 ;;*--- mail mode -------------------------------------------------------*/
311 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
312 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
313 (defvar message-signature-separator)
314 (defun mail-mode-flyspell-verify ()
315 "Function used for `flyspell-generic-check-word-predicate' in Mail mode."
316 (let* ((header-end (save-excursion
317 (goto-char (point-min))
320 (regexp-quote mail-header-separator)
325 (if (not (boundp 'message-signature-separator))
328 (goto-char (point-max))
329 (re-search-backward message-signature-separator
330 (max header-end (- (point) 4000)) t)
332 (cond ((< (point) header-end)
333 (and (save-excursion (beginning-of-line)
334 (looking-at "^Subject:"))
335 (> (point) (match-end 0))))
336 ((> (point) signature-begin)
341 (not (looking-at "[>}|]\\|To:")))))))
343 ;;*--- texinfo mode ----------------------------------------------------*/
344 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
345 (defun texinfo-mode-flyspell-verify ()
346 "Function used for `flyspell-generic-check-word-predicate' in Texinfo mode."
348 (forward-word-strictly -1)
349 (not (looking-at "@"))))
351 ;;*--- tex mode --------------------------------------------------------*/
352 (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
353 (defun tex-mode-flyspell-verify ()
354 "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
357 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t)))
359 (let ((this (point)))
361 (and (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}"
362 (line-end-position) t)
363 (>= this (match-beginning 0))
364 (<= this (match-end 0))))))))
366 ;;*--- sgml mode -------------------------------------------------------*/
367 (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
368 (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
369 (put 'nxml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
371 (autoload 'sgml-lexical-context "sgml-mode")
373 (defun sgml-mode-flyspell-verify ()
374 "Function used for `flyspell-generic-check-word-predicate' in SGML mode.
375 Tag and attribute names are not spell checked, everything else is.
377 String values of attributes are checked because they can be text
378 like <img alt=\"Some thing.\">."
380 (not (memq (car (sgml-lexical-context))
383 ;;*---------------------------------------------------------------------*/
384 ;;* Programming mode */
385 ;;*---------------------------------------------------------------------*/
386 (defvar flyspell-prog-text-faces
387 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
388 "Faces corresponding to text in programming-mode buffers.")
390 (defun flyspell-generic-progmode-verify ()
391 "Used for `flyspell-generic-check-word-predicate' in programming modes."
392 ;; (point) is next char after the word. Must check one char before.
393 (let ((f (get-text-property (- (point) 1) 'face)))
394 (memq f flyspell-prog-text-faces)))
396 ;; Records the binding of M-TAB in effect before flyspell was activated.
397 (defvar flyspell--prev-meta-tab-binding)
400 (defun flyspell-prog-mode ()
401 "Turn on `flyspell-mode' for comments and strings."
403 (setq flyspell-generic-check-word-predicate
404 #'flyspell-generic-progmode-verify)
405 (setq-local flyspell--prev-meta-tab-binding
406 (or (local-key-binding "\M-\t" t)
407 (global-key-binding "\M-\t" t)))
409 (run-hooks 'flyspell-prog-mode-hook))
411 ;;*---------------------------------------------------------------------*/
412 ;;* Overlay compatibility */
413 ;;*---------------------------------------------------------------------*/
414 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
415 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
416 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
417 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
418 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
419 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
420 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
421 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
423 ;;*---------------------------------------------------------------------*/
424 ;;* The minor mode declaration. */
425 ;;*---------------------------------------------------------------------*/
426 (defvar flyspell-mouse-map
427 (let ((map (make-sparse-keymap)))
428 (if (featurep 'xemacs)
429 (define-key map [button2] #'flyspell-correct-word)
430 (define-key map [down-mouse-2] #'flyspell-correct-word)
431 (define-key map [mouse-2] 'undefined))
433 "Keymap for Flyspell to put on erroneous words.")
435 (defvar flyspell-mode-map
436 (let ((map (make-sparse-keymap)))
437 (if flyspell-use-meta-tab
438 (define-key map "\M-\t" 'flyspell-auto-correct-word))
439 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
440 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
441 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
442 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
444 "Minor mode keymap for Flyspell mode--for the whole buffer.")
446 ;; dash character machinery
447 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
448 "Non-nil means that the `-' char is considered as a word delimiter.")
449 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
450 (defvar flyspell-dash-dictionary nil)
451 (make-variable-buffer-local 'flyspell-dash-dictionary)
452 (defvar flyspell-dash-local-dictionary nil)
453 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
455 ;;*---------------------------------------------------------------------*/
457 ;;*---------------------------------------------------------------------*/
458 (defface flyspell-incorrect
459 '((((supports :underline (:style wave)))
460 :underline (:style wave :color "Red1"))
462 :underline t :inherit error))
463 "Flyspell face for misspelled words."
467 (defface flyspell-duplicate
468 '((((supports :underline (:style wave)))
469 :underline (:style wave :color "DarkOrange"))
471 :underline t :inherit warning))
472 "Flyspell face for words that appear twice in a row.
473 See also `flyspell-duplicate-distance'."
477 (defvar flyspell-overlay nil)
479 ;;*---------------------------------------------------------------------*/
480 ;;* flyspell-mode ... */
481 ;;*---------------------------------------------------------------------*/
482 ;;;###autoload(defvar flyspell-mode nil "Non-nil if Flyspell mode is enabled.")
484 (define-minor-mode flyspell-mode
485 "Toggle on-the-fly spell checking (Flyspell mode).
486 With a prefix argument ARG, enable Flyspell mode if ARG is
487 positive, and disable it otherwise. If called from Lisp, enable
488 the mode if ARG is omitted or nil.
490 Flyspell mode is a buffer-local minor mode. When enabled, it
491 spawns a single Ispell process and checks each word. The default
492 flyspell behavior is to highlight incorrect words.
495 \\[ispell-word]: correct words (using Ispell).
496 \\[flyspell-auto-correct-word]: automatically correct word.
497 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
498 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
501 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
504 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
505 valid. For instance, a different dictionary can be used by
506 invoking `ispell-change-dictionary'.
508 Consider using the `ispell-parser' to check your text. For instance
510 \(add-hook \\='tex-mode-hook (function (lambda () (setq ispell-parser \\='tex))))
513 \\[flyspell-region] checks all words inside a region.
514 \\[flyspell-buffer] checks the whole buffer."
515 :lighter flyspell-mode-line-string
516 :keymap flyspell-mode-map
521 (error (message "Error enabling Flyspell mode:\n%s" (cdr err))
523 (flyspell-mode-off)))
526 (defun turn-on-flyspell ()
527 "Unconditionally turn on Flyspell mode."
531 (defun turn-off-flyspell ()
532 "Unconditionally turn off Flyspell mode."
535 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
537 ;;*---------------------------------------------------------------------*/
538 ;;* flyspell-buffers ... */
539 ;;* ------------------------------------------------------------- */
540 ;;* For remembering buffers running flyspell */
541 ;;*---------------------------------------------------------------------*/
542 (defvar flyspell-buffers nil)
544 ;;*---------------------------------------------------------------------*/
545 ;;* flyspell-minibuffer-p ... */
546 ;;*---------------------------------------------------------------------*/
547 (defun flyspell-minibuffer-p (buffer)
548 "Is BUFFER a minibuffer?"
549 (let ((ws (get-buffer-window-list buffer t)))
550 (and (consp ws) (window-minibuffer-p (car ws)))))
552 ;;*---------------------------------------------------------------------*/
553 ;;* flyspell-accept-buffer-local-defs ... */
554 ;;*---------------------------------------------------------------------*/
555 (defvar flyspell-last-buffer nil
556 "The buffer in which the last flyspell operation took place.")
558 (defun flyspell-accept-buffer-local-defs (&optional force)
559 ;; When flyspell-word is used inside a loop (e.g. when processing
560 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
561 ;; up dwarfing everything else, so only do it when the buffer has changed.
562 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
563 (setq flyspell-last-buffer (current-buffer))
564 ;; Strange problem: If buffer in current window has font-lock turned on,
565 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
566 ;; call will reset the buffer to the buffer in the current window.
567 ;; However, it only happens at startup (fix by Albert L. Ting).
569 (ispell-accept-buffer-local-defs))
570 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
571 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
572 ;; The dictionary has changed
573 (setq flyspell-dash-dictionary ispell-dictionary)
574 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
575 (setq flyspell-consider-dash-as-word-delimiter-flag
576 (member (or ispell-local-dictionary ispell-dictionary)
577 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
579 (defun flyspell-hack-local-variables-hook ()
580 ;; When local variables are loaded, see if the dictionary context
582 (flyspell-accept-buffer-local-defs 'force))
584 (defun flyspell-kill-ispell-hook ()
585 (setq flyspell-last-buffer nil)
586 (dolist (buf (buffer-list))
587 (with-current-buffer buf
588 (kill-local-variable 'flyspell-word-cache-word))))
590 ;; Make sure we flush our caches when needed. Do it here rather than in
591 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
593 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
595 ;;*---------------------------------------------------------------------*/
596 ;;* flyspell-mode-on ... */
597 ;;*---------------------------------------------------------------------*/
598 (defun flyspell-mode-on ()
599 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
600 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
601 (setq ispell-highlight-face 'flyspell-incorrect)
602 ;; local dictionaries setup
603 (or ispell-local-dictionary ispell-dictionary
604 (if flyspell-default-dictionary
605 (ispell-change-dictionary flyspell-default-dictionary)))
606 ;; we have to force ispell to accept the local definition or
607 ;; otherwise it could be too late, the local dictionary may
609 ;; Pass the `force' argument for the case where flyspell was active already
610 ;; but the buffer's local-defs have been edited.
611 (flyspell-accept-buffer-local-defs 'force)
612 ;; we put the `flyspell-delayed' property on some commands
613 (flyspell-delay-commands)
614 ;; we put the `flyspell-deplacement' property on some commands
615 (flyspell-deplacement-commands)
616 ;; we bound flyspell action to post-command hook
617 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
618 ;; we bound flyspell action to pre-command hook
619 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
620 ;; we bound flyspell action to after-change hook
621 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
622 ;; we bound flyspell action to hack-local-variables-hook
623 (add-hook 'hack-local-variables-hook
624 (function flyspell-hack-local-variables-hook) t t)
625 ;; set flyspell-generic-check-word-predicate based on the major mode
626 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
628 (setq flyspell-generic-check-word-predicate mode-predicate)))
629 ;; the welcome message
630 (if (and flyspell-issue-message-flag
631 flyspell-issue-welcome-flag
632 (if (featurep 'xemacs)
633 (interactive-p) ;; XEmacs does not have (called-interactively-p)
634 (called-interactively-p 'interactive)))
635 (let ((binding (where-is-internal 'flyspell-auto-correct-word
639 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
640 (key-description binding))
641 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
643 ;;*---------------------------------------------------------------------*/
644 ;;* flyspell-delay-commands ... */
645 ;;*---------------------------------------------------------------------*/
646 (defun flyspell-delay-commands ()
647 "Install the standard set of Flyspell delayed commands."
648 (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
649 (mapc 'flyspell-delay-command flyspell-delayed-commands))
651 ;;*---------------------------------------------------------------------*/
652 ;;* flyspell-delay-command ... */
653 ;;*---------------------------------------------------------------------*/
654 (defun flyspell-delay-command (command)
655 "Set COMMAND to be delayed, for Flyspell.
656 When flyspell `post-command-hook' is invoked because a delayed command
657 has been used, the current word is not immediately checked.
658 It will be checked only after `flyspell-delay' seconds."
659 (interactive "SDelay Flyspell after Command: ")
660 (put command 'flyspell-delayed t))
662 ;;*---------------------------------------------------------------------*/
663 ;;* flyspell-deplacement-commands ... */
664 ;;*---------------------------------------------------------------------*/
665 (defun flyspell-deplacement-commands ()
666 "Install the standard set of Flyspell deplacement commands."
667 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
668 (mapc 'flyspell-deplacement-command flyspell-deplacement-commands))
670 ;;*---------------------------------------------------------------------*/
671 ;;* flyspell-deplacement-command ... */
672 ;;*---------------------------------------------------------------------*/
673 (defun flyspell-deplacement-command (command)
674 "Set COMMAND that implement cursor movements, for Flyspell.
675 When flyspell `post-command-hook' is invoked because a deplacement command
676 has been used, the current word is not checked."
677 (interactive "SDeplacement Flyspell after Command: ")
678 (put command 'flyspell-deplacement t))
680 ;;*---------------------------------------------------------------------*/
681 ;;* flyspell-word-cache ... */
682 ;;*---------------------------------------------------------------------*/
683 (defvar flyspell-word-cache-start nil)
684 (defvar flyspell-word-cache-end nil)
685 (defvar flyspell-word-cache-word nil)
686 (defvar flyspell-word-cache-result '_)
687 (make-variable-buffer-local 'flyspell-word-cache-start)
688 (make-variable-buffer-local 'flyspell-word-cache-end)
689 (make-variable-buffer-local 'flyspell-word-cache-word)
690 (make-variable-buffer-local 'flyspell-word-cache-result)
692 ;;*---------------------------------------------------------------------*/
693 ;;* The flyspell pre-hook, store the current position. In the */
694 ;;* post command hook, we will check, if the word at this position */
695 ;;* has to be spell checked. */
696 ;;*---------------------------------------------------------------------*/
697 (defvar flyspell-pre-buffer nil "Buffer current before `this-command'.")
698 (defvar flyspell-pre-point nil "Point before running `this-command'")
699 (defvar flyspell-pre-column nil "Column before running `this-command'")
700 (defvar flyspell-pre-pre-buffer nil)
701 (defvar flyspell-pre-pre-point nil)
702 (make-variable-buffer-local 'flyspell-pre-point) ;Why?? --Stef
704 ;;*---------------------------------------------------------------------*/
705 ;;* flyspell-previous-command ... */
706 ;;*---------------------------------------------------------------------*/
707 (defvar flyspell-previous-command nil
708 "The last interactive command checked by Flyspell.")
710 ;;*---------------------------------------------------------------------*/
711 ;;* flyspell-pre-command-hook ... */
712 ;;*---------------------------------------------------------------------*/
713 (defun flyspell-pre-command-hook ()
714 "Save the current buffer and point for Flyspell's post-command hook."
716 (setq flyspell-pre-buffer (current-buffer))
717 (setq flyspell-pre-point (point))
718 (setq flyspell-pre-column (current-column)))
720 ;;*---------------------------------------------------------------------*/
721 ;;* flyspell-mode-off ... */
722 ;;*---------------------------------------------------------------------*/
724 (defun flyspell-mode-off ()
725 "Turn Flyspell mode off."
726 ;; We remove the hooks.
727 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
728 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
729 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
730 (remove-hook 'hack-local-variables-hook
731 (function flyspell-hack-local-variables-hook) t)
732 ;; We remove all the flyspell highlightings.
733 (flyspell-delete-all-overlays)
734 ;; We have to erase pre cache variables.
735 (setq flyspell-pre-buffer nil)
736 (setq flyspell-pre-point nil)
737 ;; We mark the mode as killed.
738 (setq flyspell-mode nil))
740 ;;*---------------------------------------------------------------------*/
741 ;;* flyspell-check-pre-word-p ... */
742 ;;*---------------------------------------------------------------------*/
743 (defun flyspell-check-pre-word-p ()
744 "Return non-nil if we should check the word before point.
745 More precisely, it applies to the word that was before point
746 before the current command."
747 (let ((ispell-otherchars (ispell-get-otherchars)))
749 ((not (and (numberp flyspell-pre-point)
750 (eq flyspell-pre-buffer (current-buffer))))
752 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
753 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
755 ((or (and (= flyspell-pre-point (- (point) 1))
756 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
757 (and (not (string= "" ispell-otherchars))
760 (buffer-substring-no-properties
761 flyspell-pre-point (1+ flyspell-pre-point))))))
762 (= flyspell-pre-point (point))
763 (= flyspell-pre-point (+ (point) 1)))
765 ((and (symbolp this-command)
766 (not executing-kbd-macro)
767 (or (get this-command 'flyspell-delayed)
768 (and (get this-command 'flyspell-deplacement)
769 (eq flyspell-previous-command this-command)))
770 (or (= (current-column) 0)
771 (= (current-column) flyspell-pre-column)
772 ;; If other post-command-hooks change the buffer,
773 ;; flyspell-pre-point can lie past eob (bug#468).
774 (null (char-after flyspell-pre-point))
775 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
776 (and (not (string= "" ispell-otherchars))
779 (buffer-substring-no-properties
780 flyspell-pre-point (1+ flyspell-pre-point)))))))
782 ((not (eq (current-buffer) flyspell-pre-buffer))
784 ((not (and (numberp flyspell-word-cache-start)
785 (numberp flyspell-word-cache-end)))
788 (or (< flyspell-pre-point flyspell-word-cache-start)
789 (> flyspell-pre-point flyspell-word-cache-end))))))
791 ;;*---------------------------------------------------------------------*/
792 ;;* The flyspell after-change-hook, store the change position. In */
793 ;;* the post command hook, we will check, if the word at this */
794 ;;* position has to be spell checked. */
795 ;;*---------------------------------------------------------------------*/
796 (defvar flyspell-changes nil)
797 (make-variable-buffer-local 'flyspell-changes)
799 ;;*---------------------------------------------------------------------*/
800 ;;* flyspell-after-change-function ... */
801 ;;*---------------------------------------------------------------------*/
802 (defun flyspell-after-change-function (start stop _len)
803 "Save the current buffer and point for Flyspell's post-command hook."
804 (push (cons start stop) flyspell-changes))
806 ;;*---------------------------------------------------------------------*/
807 ;;* flyspell-check-changed-word-p ... */
808 ;;*---------------------------------------------------------------------*/
809 (defun flyspell-check-changed-word-p (start stop)
810 "Return non-nil when the changed word has to be checked.
811 The answer depends of several criteria.
812 Mostly we check word delimiters."
813 (not (and (not (and (memq (char-after start) '(?\n ? )) (> stop start)))
814 (numberp flyspell-pre-point)
816 (and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
818 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))))))
820 ;;*---------------------------------------------------------------------*/
821 ;;* flyspell-check-word-p ... */
822 ;;*---------------------------------------------------------------------*/
823 (defun flyspell-check-word-p ()
824 "Return t when the word at `point' has to be checked.
825 The answer depends of several criteria.
826 Mostly we check word delimiters."
827 (let ((ispell-otherchars (ispell-get-otherchars)))
829 ((<= (- (point-max) 1) (point-min))
830 ;; The buffer is not filled enough.
832 ((and (and (> (current-column) 0)
833 (not (eq (current-column) flyspell-pre-column)))
836 (and (looking-at (flyspell-get-not-casechars))
837 (or (string= "" ispell-otherchars)
838 (not (looking-at ispell-otherchars)))
839 (or flyspell-consider-dash-as-word-delimiter-flag
840 (not (looking-at "-"))))))
841 ;; Yes because we have reached or typed a word delimiter.
843 ((symbolp this-command)
845 ((get this-command 'flyspell-deplacement)
846 (not (eq flyspell-previous-command this-command)))
847 ((get this-command 'flyspell-delayed)
848 ;; The current command is not delayed, that
849 ;; is that we must check the word now.
850 (and (not unread-command-events)
851 (sit-for flyspell-delay)))
855 ;;*---------------------------------------------------------------------*/
856 ;;* flyspell-debug-signal-no-check ... */
857 ;;*---------------------------------------------------------------------*/
858 (defun flyspell-debug-signal-no-check (msg obj)
859 (setq debug-on-error t)
860 (with-current-buffer (get-buffer-create "*flyspell-debug*")
862 (insert "NO-CHECK:\n")
863 (insert (format " %S : %S\n" msg obj))))
865 ;;*---------------------------------------------------------------------*/
866 ;;* flyspell-debug-signal-pre-word-checked ... */
867 ;;*---------------------------------------------------------------------*/
868 (defun flyspell-debug-signal-pre-word-checked ()
869 (setq debug-on-error t)
870 (with-current-buffer (get-buffer-create "*flyspell-debug*")
871 (insert "PRE-WORD:\n")
872 (insert (format " pre-point : %S\n" flyspell-pre-point))
873 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
874 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
875 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
876 (goto-char (point-max))))
878 ;;*---------------------------------------------------------------------*/
879 ;;* flyspell-debug-signal-word-checked ... */
880 ;;*---------------------------------------------------------------------*/
881 (defun flyspell-debug-signal-word-checked ()
882 (setq debug-on-error t)
883 (let ((ispell-otherchars (ispell-get-otherchars))
884 (oldbuf (current-buffer))
886 (with-current-buffer (get-buffer-create "*flyspell-debug*")
889 (format " this-cmd : %S\n" this-command)
890 (format " delayed : %S\n" (and (symbolp this-command)
893 (format " point : %S\n" point)
894 (format " prev-char : [%c] %S\n"
895 (with-current-buffer oldbuf
896 (if (bobp) ?\ (char-before)))
897 (with-current-buffer oldbuf
902 (and (looking-at (flyspell-get-not-casechars))
903 (or (string= "" ispell-otherchars)
904 (not (looking-at ispell-otherchars)))
905 (or flyspell-consider-dash-as-word-delimiter-flag
906 (not (looking-at "\\-")))
908 (format " because : %S\n"
910 ((not (and (symbolp this-command)
911 (get this-command 'flyspell-delayed)))
912 ;; The current command is not delayed, that
913 ;; is that we must check the word now.
915 ((with-current-buffer oldbuf
920 (and (looking-at (flyspell-get-not-casechars))
921 (or (string= "" ispell-otherchars)
922 (not (looking-at ispell-otherchars)))
923 (or flyspell-consider-dash-as-word-delimiter-flag
924 (not (looking-at "\\-")))))))
925 ;; Yes because we have reached or typed a word delimiter.
927 ((not (integerp flyspell-delay))
928 ;; Yes because the user set up a no-delay configuration.
932 (goto-char (point-max)))))
934 ;;*---------------------------------------------------------------------*/
935 ;;* flyspell-debug-signal-changed-checked ... */
936 ;;*---------------------------------------------------------------------*/
937 (defun flyspell-debug-signal-changed-checked ()
938 (setq debug-on-error t)
939 (let ((point (point)))
940 (with-current-buffer (get-buffer-create "*flyspell-debug*")
941 (insert "CHANGED WORD:\n")
942 (insert (format " point : %S\n" point))
943 (goto-char (point-max)))))
945 ;;*---------------------------------------------------------------------*/
946 ;;* flyspell-post-command-hook ... */
947 ;;* ------------------------------------------------------------- */
948 ;;* It is possible that we check several words: */
949 ;;* 1- the current word is checked if the predicate */
950 ;;* FLYSPELL-CHECK-WORD-P is true */
951 ;;* 2- the word that used to be the current word before the */
952 ;;* THIS-COMMAND is checked if: */
953 ;;* a- the previous word is different from the current word */
954 ;;* b- the previous word has not just been checked by the */
955 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
956 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
957 ;;* previous word nor the current word */
958 ;;*---------------------------------------------------------------------*/
959 (defun flyspell-post-command-hook ()
960 "The `post-command-hook' used by flyspell to check a word on-the-fly."
964 (let ((command this-command)
965 ;; Prevent anything we do from affecting the mark.
967 (if (flyspell-check-pre-word-p)
969 '(flyspell-debug-signal-pre-word-checked)
970 (goto-char flyspell-pre-point)
972 (if (flyspell-check-word-p)
974 '(flyspell-debug-signal-word-checked)
975 ;; FIXME: This should be asynchronous!
977 ;; we remember which word we have just checked.
978 ;; this will be used next time we will check a word
979 ;; to compare the next current word with the word
980 ;; that has been registered in the pre-command-hook
981 ;; that is these variables are used within the predicate
982 ;; FLYSPELL-CHECK-PRE-WORD-P
983 (setq flyspell-pre-pre-buffer (current-buffer))
984 (setq flyspell-pre-pre-point (point)))
985 (setq flyspell-pre-pre-buffer nil)
986 (setq flyspell-pre-pre-point nil)
987 ;; when a word is not checked because of a delayed command
988 ;; we do not disable the ispell cache.
989 (when (and (symbolp this-command)
990 (get this-command 'flyspell-delayed))
991 (setq flyspell-word-cache-end -1)
992 (setq flyspell-word-cache-result '_)))
993 (while (and (not (input-pending-p)) (consp flyspell-changes))
994 (let ((start (car (car flyspell-changes)))
995 (stop (cdr (car flyspell-changes))))
996 (if (flyspell-check-changed-word-p start stop)
998 '(flyspell-debug-signal-changed-checked)
1001 (setq flyspell-changes (cdr flyspell-changes))))
1002 (setq flyspell-previous-command command)))))
1004 ;;*---------------------------------------------------------------------*/
1005 ;;* flyspell-notify-misspell ... */
1006 ;;*---------------------------------------------------------------------*/
1007 (defun flyspell-notify-misspell (word poss)
1008 (let ((replacements (if (stringp poss)
1010 (if flyspell-sort-corrections
1011 (sort (car (cdr (cdr poss))) 'string<)
1012 (car (cdr (cdr poss)))))))
1013 (if flyspell-issue-message-flag
1014 (message "misspelling `%s' %S" word replacements))))
1016 ;;*---------------------------------------------------------------------*/
1017 ;;* flyspell-word-search-backward ... */
1018 ;;*---------------------------------------------------------------------*/
1019 (defun flyspell-word-search-backward (word bound &optional ignore-case)
1022 (inhibit-point-motion-hooks t)
1023 (flyspell-not-casechars (flyspell-get-not-casechars))
1024 (bound (if (and bound
1025 (> bound (point-min)))
1028 "\\(?:" flyspell-not-casechars "\\|\\`\\)"
1030 flyspell-not-casechars))
1036 (re-search-backward word-re bound t)
1041 (let ((lw (flyspell-get-word)))
1044 (string-equal (downcase (car lw)) (downcase word))
1045 (string-equal (car lw) word)))
1050 ;;*---------------------------------------------------------------------*/
1051 ;;* flyspell-word-search-forward ... */
1052 ;;*---------------------------------------------------------------------*/
1053 (defun flyspell-word-search-forward (word bound)
1056 (inhibit-point-motion-hooks t)
1057 (flyspell-not-casechars (flyspell-get-not-casechars))
1058 (bound (if (and bound
1059 (< bound (point-max)))
1061 (word-re (concat flyspell-not-casechars
1063 "\\(?:" flyspell-not-casechars "\\|\\'\\)"))
1068 (re-search-forward word-re bound t)
1073 (let ((lw (flyspell-get-word)))
1074 (if (and (consp lw) (string-equal (car lw) word))
1076 (goto-char (1+ p)))))
1079 (defvar flyspell-word) ;Backward compatibility; some predicates made use of it!
1081 ;;*---------------------------------------------------------------------*/
1082 ;;* flyspell-word ... */
1083 ;;*---------------------------------------------------------------------*/
1084 (defun flyspell-word (&optional following known-misspelling)
1085 "Spell check a word.
1086 If the optional argument FOLLOWING, or, when called interactively
1087 `ispell-following-word', is non-nil, checks the following (rather
1088 than preceding) word when the cursor is not over a word. If
1089 optional argument KNOWN-MISSPELLING is non nil considers word a
1090 misspelling and skips redundant spell-checking step."
1091 (interactive (list ispell-following-word))
1092 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1094 ;; use the correct dictionary
1095 (flyspell-accept-buffer-local-defs)
1096 (let* ((cursor-location (point))
1097 (flyspell-word (flyspell-get-word following))
1098 start end poss word ispell-filter)
1099 (if (or (eq flyspell-word nil)
1100 (and (fboundp flyspell-generic-check-word-predicate)
1101 (not (funcall flyspell-generic-check-word-predicate))))
1104 ;; destructure return flyspell-word info list.
1105 (setq start (car (cdr flyspell-word))
1106 end (car (cdr (cdr flyspell-word)))
1107 word (car flyspell-word))
1108 ;; before checking in the directory, we check for doublons.
1110 ((and (or (not (eq ispell-parser 'tex))
1111 (and (> start (point-min))
1112 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1113 flyspell-mark-duplications-flag
1114 (not (catch 'exception
1115 (let ((dict (or ispell-local-dictionary
1116 ispell-dictionary)))
1117 (dolist (except flyspell-mark-duplications-exceptions)
1118 (and (or (null (car except))
1120 (string-match (car except) dict)))
1121 (member (downcase word) (cdr except))
1122 (throw 'exception t))))))
1129 (skip-chars-backward " \t\n\f")))))
1130 (p (when (>= bound (point-min))
1131 (flyspell-word-search-backward word bound t))))
1132 (and p (/= p start)))))
1133 ;; yes, this is a doublon
1134 (flyspell-highlight-incorrect-region start end 'doublon)
1136 ((and (eq flyspell-word-cache-start start)
1137 (eq flyspell-word-cache-end end)
1138 (string-equal flyspell-word-cache-word word))
1139 ;; this word had been already checked, we skip
1140 flyspell-word-cache-result)
1141 ((and (eq ispell-parser 'tex)
1142 (flyspell-tex-command-p flyspell-word))
1143 ;; this is a correct word (because a tex command)
1144 (flyspell-unhighlight-at start)
1146 (flyspell-unhighlight-at (- end 1)))
1149 ;; we setup the cache
1150 (setq flyspell-word-cache-start start)
1151 (setq flyspell-word-cache-end end)
1152 (setq flyspell-word-cache-word word)
1153 ;; now check spelling of word.
1154 (if (not known-misspelling)
1156 (ispell-send-string "%\n")
1157 ;; put in verbose mode
1158 (ispell-send-string (concat "^" word "\n"))
1159 ;; we mark the ispell process so it can be killed
1160 ;; when emacs is exited without query
1161 (if (featurep 'xemacs)
1162 (process-kill-without-query ispell-process)
1163 (set-process-query-on-exit-flag ispell-process nil))
1164 ;; Wait until ispell has processed word.
1166 (accept-process-output ispell-process)
1167 (not (string= "" (car ispell-filter)))))
1168 ;; (ispell-send-string "!\n")
1169 ;; back to terse mode.
1170 ;; Remove leading empty element
1171 (setq ispell-filter (cdr ispell-filter))
1172 ;; ispell process should return something after word is sent.
1173 ;; Tag word as valid (i.e., skip) otherwise
1175 (setq ispell-filter '(*)))
1176 (if (consp ispell-filter)
1177 (setq poss (ispell-parse-output (car ispell-filter)))))
1178 ;; Else, this was a known misspelling to begin with, and
1179 ;; we should forge an ispell return value.
1180 (setq poss (list word 1 nil nil)))
1181 (let ((res (cond ((eq poss t)
1183 (setq flyspell-word-cache-result t)
1184 (flyspell-unhighlight-at start)
1186 (flyspell-unhighlight-at (- end 1)))
1188 ((and (stringp poss) flyspell-highlight-flag)
1190 (setq flyspell-word-cache-result t)
1191 (flyspell-unhighlight-at start)
1193 (flyspell-unhighlight-at (- end 1)))
1196 (setq flyspell-word-cache-result t)
1197 (flyspell-unhighlight-at start)
1199 (flyspell-unhighlight-at (- end 1)))
1201 ((or (and (< flyspell-duplicate-distance 0)
1204 (flyspell-word-search-backward
1209 (flyspell-word-search-forward
1212 (and (> flyspell-duplicate-distance 0)
1215 (flyspell-word-search-backward
1218 flyspell-duplicate-distance)))
1221 (flyspell-word-search-forward
1224 flyspell-duplicate-distance))))))
1225 ;; This is a misspelled word which occurs
1226 ;; twice within flyspell-duplicate-distance.
1227 (setq flyspell-word-cache-result nil)
1228 (if flyspell-highlight-flag
1229 (flyspell-highlight-duplicate-region
1231 (message "duplicate `%s'" word))
1234 (setq flyspell-word-cache-result nil)
1235 ;; Highlight the location as incorrect,
1236 ;; including offset specified in POSS.
1237 (if flyspell-highlight-flag
1238 (flyspell-highlight-incorrect-region
1239 (if (and (consp poss)
1240 (integerp (nth 1 poss)))
1241 (+ start (nth 1 poss) -1)
1244 (flyspell-notify-misspell word poss))
1246 ;; return to original location
1247 (goto-char cursor-location)
1248 (if ispell-quit (setq ispell-quit nil))
1251 ;;*---------------------------------------------------------------------*/
1252 ;;* flyspell-math-tex-command-p ... */
1253 ;;* ------------------------------------------------------------- */
1254 ;;* This function uses the texmathp package to check if point */
1255 ;;* is within a TeX math environment. `texmathp' can yield errors */
1256 ;;* if the document is currently not valid TeX syntax. */
1257 ;;*---------------------------------------------------------------------*/
1258 (defun flyspell-math-tex-command-p ()
1259 (when (fboundp 'texmathp)
1260 (if flyspell-check-tex-math-command
1266 ;;*---------------------------------------------------------------------*/
1267 ;;* flyspell-tex-command-p ... */
1268 ;;*---------------------------------------------------------------------*/
1269 (defun flyspell-tex-command-p (word)
1270 "Return t if WORD is a TeX command."
1272 (let ((b (car (cdr word))))
1273 (and (re-search-backward "\\\\" (- (point) 100) t)
1274 (or (= (match-end 0) b)
1275 (and (goto-char (match-end 0))
1276 (looking-at flyspell-tex-command-regexp)
1277 (>= (match-end 0) b))))))
1278 (flyspell-math-tex-command-p)))
1280 (defalias 'flyspell-get-casechars 'ispell-get-casechars)
1281 (defalias 'flyspell-get-not-casechars 'ispell-get-not-casechars)
1283 ;;*---------------------------------------------------------------------*/
1284 ;;* flyspell-get-word ... */
1285 ;;*---------------------------------------------------------------------*/
1286 (defun flyspell-get-word (&optional following extra-otherchars)
1287 "Return the word for spell-checking according to Ispell syntax.
1288 Optional argument FOLLOWING non-nil means to get the following
1289 \(rather than preceding) word when the cursor is not over a word.
1290 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1291 that may be included as part of a word (see `ispell-dictionary-alist')."
1292 (let* ((flyspell-casechars (flyspell-get-casechars))
1293 (flyspell-not-casechars (flyspell-get-not-casechars))
1294 (ispell-otherchars (ispell-get-otherchars))
1295 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1296 (word-regexp (concat flyspell-casechars
1298 (if (not (string= "" ispell-otherchars))
1299 (concat ispell-otherchars "?"))
1300 (if extra-otherchars
1301 (concat extra-otherchars "?"))
1304 (if (or ispell-many-otherchars-p
1310 (if (not (looking-at flyspell-casechars))
1312 (re-search-forward flyspell-casechars nil t)
1313 (re-search-backward flyspell-casechars nil t)))
1314 ;; move to front of word
1315 (re-search-backward flyspell-not-casechars nil 'start)
1316 (while (and (or (and (not (string= "" ispell-otherchars))
1317 (looking-at ispell-otherchars))
1318 (and extra-otherchars (looking-at extra-otherchars)))
1320 (or (not did-it-once)
1321 ispell-many-otherchars-p)
1322 (not (eq prevpt (point))))
1323 (if (and extra-otherchars (looking-at extra-otherchars))
1326 (if (looking-at flyspell-casechars)
1327 (re-search-backward flyspell-not-casechars nil 'move)))
1331 (if (looking-at flyspell-casechars)
1332 (re-search-backward flyspell-not-casechars nil 'move)
1333 (backward-char -1))))
1334 ;; Now mark the word and save to string.
1335 (if (not (re-search-forward word-regexp nil t))
1338 (setq start (match-beginning 0)
1340 word (buffer-substring-no-properties start end))
1341 (list word start end)))))
1343 ;;*---------------------------------------------------------------------*/
1344 ;;* flyspell-small-region ... */
1345 ;;*---------------------------------------------------------------------*/
1346 (defun flyspell-small-region (beg end)
1347 "Flyspell text between BEG and END."
1355 (while (< (point) end)
1356 (if (and flyspell-issue-message-flag (= count 100))
1358 (message "Spell Checking...%d%%"
1359 (floor (* 100.0 (- (point) beg)) (- end beg)))
1361 (setq count (+ 1 count)))
1364 (let ((cur (point)))
1366 (if (and (< (point) end) (> (point) (+ cur 1)))
1367 (backward-char 1)))))
1369 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1372 ;;*---------------------------------------------------------------------*/
1373 ;;* flyspell-external-ispell-process ... */
1374 ;;*---------------------------------------------------------------------*/
1375 (defvar flyspell-external-ispell-process '()
1376 "The external Flyspell Ispell process.")
1378 ;;*---------------------------------------------------------------------*/
1379 ;;* flyspell-external-ispell-buffer ... */
1380 ;;*---------------------------------------------------------------------*/
1381 (defvar flyspell-external-ispell-buffer '())
1382 (defvar flyspell-large-region-buffer '())
1383 (defvar flyspell-large-region-beg (point-min))
1384 (defvar flyspell-large-region-end (point-max))
1386 ;;*---------------------------------------------------------------------*/
1387 ;;* flyspell-external-point-words ... */
1388 ;;*---------------------------------------------------------------------*/
1389 (defun flyspell-external-point-words ()
1390 "Mark words from a buffer listing incorrect words in order of appearance.
1391 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1392 \(We finish by killing that buffer and setting the variable to nil.)
1393 The buffer to mark them in is `flyspell-large-region-buffer'."
1394 (let (words-not-found
1395 (ispell-otherchars (ispell-get-otherchars))
1396 (buffer-scan-pos flyspell-large-region-beg)
1398 (with-current-buffer flyspell-external-ispell-buffer
1399 (goto-char (point-min))
1400 ;; Loop over incorrect words, in the order they were reported,
1401 ;; which is also the order they appear in the buffer being checked.
1402 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1403 ;; Bind WORD to the next one.
1404 (let ((word (match-string 1)) (wordpos (point)))
1405 ;; Here there used to be code to see if WORD is the same
1406 ;; as the previous iteration, and count the number of consecutive
1407 ;; identical words, and the loop below would search for that many.
1408 ;; That code seemed to be incorrect, and on principle, should
1409 ;; be unnecessary too. -- rms.
1410 (if flyspell-issue-message-flag
1411 (message "Spell Checking...%d%% [%s]"
1412 (floor (* 100.0 (point)) (point-max))
1414 (with-current-buffer flyspell-large-region-buffer
1415 (goto-char buffer-scan-pos)
1417 ;; Iterate on string search until string is found as word,
1418 ;; not as substring.
1420 (if (search-forward word
1421 flyspell-large-region-end t)
1424 ;; Move back into the match
1425 ;; so flyspell-get-word will find it.
1427 (flyspell-get-word)))
1428 (found (car found-list))
1429 (found-length (length found))
1430 (misspell-length (length word)))
1432 ;; Size matches, we really found it.
1433 (= found-length misspell-length)
1434 ;; Matches as part of a boundary-char separated
1437 (split-string found ispell-otherchars))
1438 ;; Misspelling has higher length than
1439 ;; what flyspell considers the word.
1440 ;; Caused by boundary-chars mismatch.
1441 ;; Validating seems safe.
1442 (< found-length misspell-length)
1443 ;; ispell treats beginning of some TeX
1444 ;; commands as nroff control sequences
1445 ;; and strips them in the list of
1446 ;; misspelled words thus giving a
1447 ;; non-existent word. Skip if ispell
1448 ;; is used, string is a TeX command
1449 ;; (char before beginning of word is
1450 ;; backslash) and none of the previous
1451 ;; conditions match.
1452 (and (not ispell-really-aspell)
1454 (goto-char (- (nth 1 found-list) 1))
1455 (if (looking-at "[\\]" )
1459 (flyspell-word nil t)
1460 ;; Search for next misspelled word will begin from
1461 ;; end of last validated match.
1462 (setq buffer-scan-pos (point))))
1463 ;; Record if misspelling is not found and try new one
1464 (cl-pushnew (concat " -> " word " - "
1465 (int-to-string wordpos))
1466 words-not-found :test #'equal)
1467 (setq keep nil)))))))
1469 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1470 ;; Warn about not found misspellings
1471 (dolist (word words-not-found)
1472 (message "%s: word not found" word))
1473 ;; Kill and forget the buffer with the list of incorrect words.
1474 (kill-buffer flyspell-external-ispell-buffer)
1475 (setq flyspell-external-ispell-buffer nil)))
1477 ;;*---------------------------------------------------------------------*/
1478 ;;* flyspell-process-localwords ... */
1479 ;;* ------------------------------------------------------------- */
1480 ;;* This function is used to prevent marking of words explicitly */
1481 ;;* declared correct. */
1482 ;;*---------------------------------------------------------------------*/
1483 (defun flyspell-process-localwords (misspellings-buffer)
1484 (let ((localwords ispell-buffer-session-localwords)
1486 (ispell-casechars (ispell-get-casechars)))
1487 ;; Get localwords from the original buffer
1489 (goto-char (point-min))
1490 ;; Localwords parsing copied from ispell.el.
1491 (while (search-forward ispell-words-keyword nil t)
1492 (let ((end (point-at-eol))
1494 ;; buffer-local words separated by a space, and can contain
1495 ;; any character other than a space. Not rigorous enough.
1496 (while (re-search-forward " *\\([^ ]+\\)" end t)
1497 (setq string (buffer-substring-no-properties (match-beginning 1)
1499 ;; This can fail when string contains a word with invalid chars.
1500 ;; Error handling needs to be added between Ispell and Emacs.
1501 (if (and (< 1 (length string))
1502 (equal 0 (string-match ispell-casechars string)))
1503 (push string localwords))))))
1504 ;; Remove localwords matches from misspellings-buffer.
1505 ;; The usual mechanism of communicating the local words to ispell
1506 ;; does not affect the special ispell process used by
1507 ;; flyspell-large-region.
1508 (with-current-buffer misspellings-buffer
1510 (dolist (word localwords)
1511 (goto-char (point-min))
1512 (let ((regexp (concat "^" word "\n")))
1513 (while (re-search-forward regexp nil t)
1514 (delete-region (match-beginning 0) (match-end 0)))))))))
1516 ;;* ---------------------------------------------------------------
1517 ;;* flyspell-check-region-doublons
1518 ;;* ---------------------------------------------------------------
1519 (defun flyspell-check-region-doublons (beg end)
1520 "Check for adjacent duplicated words (doublons) in the given region."
1523 (flyspell-word) ; Make sure current word is checked
1525 (while (and (< (point) end)
1526 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1532 ;;*---------------------------------------------------------------------*/
1533 ;;* flyspell-large-region ... */
1534 ;;*---------------------------------------------------------------------*/
1535 (defun flyspell-large-region (beg end)
1536 (let* ((curbuf (current-buffer))
1537 (buffer (get-buffer-create "*flyspell-region*")))
1538 (setq flyspell-external-ispell-buffer buffer)
1539 (setq flyspell-large-region-buffer curbuf)
1540 (setq flyspell-large-region-beg beg)
1541 (setq flyspell-large-region-end end)
1542 (flyspell-accept-buffer-local-defs)
1545 ;; this is done, we can start checking...
1546 (if flyspell-issue-message-flag (message "Checking region..."))
1548 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1549 ;; Local dictionary becomes the global dictionary in use.
1550 (setq ispell-current-dictionary
1551 (or ispell-local-dictionary ispell-dictionary))
1552 (setq ispell-current-personal-dictionary
1553 (or ispell-local-pdict ispell-personal-dictionary))
1554 (let ((args (ispell-get-ispell-args))
1555 (encoding (ispell-get-coding-system))
1557 (if (and ispell-current-dictionary ; use specified dictionary
1558 (not (member "-d" args))) ; only define if not overridden
1560 (append (list "-d" ispell-current-dictionary) args)))
1561 (if ispell-current-personal-dictionary ; use specified pers dict
1566 ispell-current-personal-dictionary)))))
1568 ;; Check for extended character mode
1569 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1570 (and extended-char-mode ; ~ extended character mode
1571 (string-match "[^~]+$" extended-char-mode)
1572 (cl-pushnew (concat "-T" (match-string 0 extended-char-mode))
1573 args :test #'equal)))
1575 ;; Add ispell-extra-args
1576 (setq args (append args ispell-extra-args))
1578 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1579 ;; for communication. ispell or older aspell/hunspell does not support this
1580 (if ispell-encoding8-command
1583 (if ispell-really-hunspell
1584 (list ispell-encoding8-command
1585 (upcase (symbol-name encoding)))
1586 (list (concat ispell-encoding8-command
1587 (symbol-name encoding)))))))
1589 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1590 (setq c (apply 'ispell-call-process-region beg
1596 (if ispell-really-aspell "list" "-l")
1600 (flyspell-process-localwords buffer)
1601 (with-current-buffer curbuf
1602 (flyspell-delete-region-overlays beg end)
1603 (flyspell-check-region-doublons beg end))
1604 (flyspell-external-point-words))
1605 (error "Can't check region")))))
1607 ;;*---------------------------------------------------------------------*/
1608 ;;* flyspell-region ... */
1609 ;;* ------------------------------------------------------------- */
1610 ;;* Because `ispell -a' is too slow, it is not possible to use */
1611 ;;* it on large region. Then, when ispell is invoked on a large */
1612 ;;* text region, a new `ispell -l' process is spawned. The */
1613 ;;* pointed out words are then searched in the region a checked with */
1614 ;;* regular flyspell means. */
1615 ;;*---------------------------------------------------------------------*/
1617 (defun flyspell-region (beg end)
1618 "Flyspell text between BEG and END."
1620 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1628 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1629 (flyspell-large-region beg end)
1630 (flyspell-small-region beg end)))))
1632 ;;*---------------------------------------------------------------------*/
1633 ;;* flyspell-buffer ... */
1634 ;;*---------------------------------------------------------------------*/
1636 (defun flyspell-buffer ()
1637 "Flyspell whole buffer."
1639 (flyspell-region (point-min) (point-max)))
1641 ;;*---------------------------------------------------------------------*/
1642 ;;* old next error position ... */
1643 ;;*---------------------------------------------------------------------*/
1644 (defvar flyspell-old-buffer-error nil)
1645 (defvar flyspell-old-pos-error nil)
1647 ;;*---------------------------------------------------------------------*/
1648 ;;* flyspell-goto-next-error ... */
1649 ;;*---------------------------------------------------------------------*/
1650 (defun flyspell-goto-next-error ()
1651 "Go to the next previously detected error.
1652 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1657 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1658 (eq pos flyspell-old-pos-error))
1660 (if (= flyspell-old-pos-error max)
1661 ;; goto beginning of buffer
1663 (message "Restarting from beginning of buffer")
1664 (goto-char (point-min)))
1666 (setq pos (point))))
1667 ;; seek the next error
1668 (while (and (< pos max)
1669 (let ((ovs (overlays-at pos))
1671 (while (and (not r) (consp ovs))
1672 (if (flyspell-overlay-p (car ovs))
1674 (setq ovs (cdr ovs))))
1676 (setq pos (1+ pos)))
1677 ;; save the current location for next invocation
1678 (setq flyspell-old-pos-error pos)
1679 (setq flyspell-old-buffer-error (current-buffer))
1682 (message "No more miss-spelled word!"))))
1684 ;;*---------------------------------------------------------------------*/
1685 ;;* flyspell-overlay-p ... */
1686 ;;*---------------------------------------------------------------------*/
1687 (defun flyspell-overlay-p (o)
1688 "Return true if O is an overlay used by flyspell."
1689 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1691 ;;*---------------------------------------------------------------------*/
1692 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1693 ;;* ------------------------------------------------------------- */
1694 ;;* Remove overlays introduced by flyspell. */
1695 ;;*---------------------------------------------------------------------*/
1696 (defun flyspell-delete-region-overlays (beg end)
1697 "Delete overlays used by flyspell in a given region."
1698 (if (featurep 'emacs)
1699 (remove-overlays beg end 'flyspell-overlay t)
1700 ;; XEmacs does not have `remove-overlays'
1701 (let ((l (overlays-in beg end)))
1704 (if (flyspell-overlay-p (car l))
1705 (delete-overlay (car l)))
1706 (setq l (cdr l)))))))
1708 (defun flyspell-delete-all-overlays ()
1709 "Delete all the overlays used by flyspell."
1710 (flyspell-delete-region-overlays (point-min) (point-max)))
1712 ;;*---------------------------------------------------------------------*/
1713 ;;* flyspell-unhighlight-at ... */
1714 ;;*---------------------------------------------------------------------*/
1715 (defun flyspell-unhighlight-at (pos)
1716 "Remove the flyspell overlay that are located at POS."
1717 (if flyspell-persistent-highlight
1718 (let ((overlays (overlays-at pos)))
1719 (while (consp overlays)
1720 (if (flyspell-overlay-p (car overlays))
1721 (delete-overlay (car overlays)))
1722 (setq overlays (cdr overlays))))
1723 (if (flyspell-overlay-p flyspell-overlay)
1724 (delete-overlay flyspell-overlay))))
1726 ;;*---------------------------------------------------------------------*/
1727 ;;* flyspell-properties-at-p ... */
1728 ;;* ------------------------------------------------------------- */
1729 ;;* Is there an highlight properties at position pos? */
1730 ;;*---------------------------------------------------------------------*/
1731 (defun flyspell-properties-at-p (pos)
1732 "Return t if there is a text property at POS, not counting `local-map'.
1733 If variable `flyspell-highlight-properties' is set to nil,
1734 text with properties are not checked. This function is used to discover
1735 if the character at POS has any other property."
1736 (let ((prop (text-properties-at pos))
1738 (while (and keep (consp prop))
1739 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1740 (setq prop (cdr (cdr prop)))
1744 ;;*---------------------------------------------------------------------*/
1745 ;;* make-flyspell-overlay ... */
1746 ;;*---------------------------------------------------------------------*/
1747 (defun make-flyspell-overlay (beg end face mouse-face)
1748 "Allocate an overlay to highlight an incorrect word.
1749 BEG and END specify the range in the buffer of that word.
1750 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1752 (let ((overlay (make-overlay beg end nil t nil)))
1753 (overlay-put overlay 'face face)
1754 (overlay-put overlay 'mouse-face mouse-face)
1755 (overlay-put overlay 'flyspell-overlay t)
1756 (overlay-put overlay 'evaporate t)
1757 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1758 (overlay-put overlay 'keymap flyspell-mouse-map)
1759 (when (eq face 'flyspell-incorrect)
1760 (and (stringp flyspell-before-incorrect-word-string)
1761 (overlay-put overlay 'before-string
1762 flyspell-before-incorrect-word-string))
1763 (and (stringp flyspell-after-incorrect-word-string)
1764 (overlay-put overlay 'after-string
1765 flyspell-after-incorrect-word-string)))
1768 ;;*---------------------------------------------------------------------*/
1769 ;;* flyspell-highlight-incorrect-region ... */
1770 ;;*---------------------------------------------------------------------*/
1771 (defun flyspell-highlight-incorrect-region (beg end poss)
1772 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1773 POSS is usually a list of possible spelling/correction lists,
1774 as returned by `ispell-parse-output'.
1775 It can also be the symbol `doublon', in the case where the word
1776 is itself incorrect, but suspiciously repeated."
1777 (let ((inhibit-read-only t))
1778 (unless (run-hook-with-args-until-success
1779 'flyspell-incorrect-hook beg end poss)
1780 (if (or flyspell-highlight-properties
1781 (not (flyspell-properties-at-p beg)))
1783 ;; we cleanup all the overlay that are in the region, not
1784 ;; beginning at the word start position
1785 (if (< (1+ beg) end)
1786 (let ((os (overlays-in (1+ beg) end)))
1788 (if (flyspell-overlay-p (car os))
1789 (delete-overlay (car os)))
1790 (setq os (cdr os)))))
1791 ;; we cleanup current overlay at the same position
1792 (flyspell-unhighlight-at beg)
1793 ;; now we can use a new overlay
1794 (setq flyspell-overlay
1795 (make-flyspell-overlay
1797 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1800 ;;*---------------------------------------------------------------------*/
1801 ;;* flyspell-highlight-duplicate-region ... */
1802 ;;*---------------------------------------------------------------------*/
1803 (defun flyspell-highlight-duplicate-region (beg end poss)
1804 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1805 POSS is a list of possible spelling/correction lists,
1806 as returned by `ispell-parse-output'."
1807 (let ((inhibit-read-only t))
1808 (unless (run-hook-with-args-until-success
1809 'flyspell-incorrect-hook beg end poss)
1810 (if (or flyspell-highlight-properties
1811 (not (flyspell-properties-at-p beg)))
1813 ;; we cleanup current overlay at the same position
1814 (flyspell-unhighlight-at beg)
1815 ;; now we can use a new overlay
1816 (setq flyspell-overlay
1817 (make-flyspell-overlay beg end
1821 ;;*---------------------------------------------------------------------*/
1822 ;;* flyspell-auto-correct-cache ... */
1823 ;;*---------------------------------------------------------------------*/
1824 (defvar flyspell-auto-correct-pos nil)
1825 (defvar flyspell-auto-correct-region nil)
1826 (defvar flyspell-auto-correct-ring nil)
1827 (defvar flyspell-auto-correct-word nil)
1828 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1829 (make-variable-buffer-local 'flyspell-auto-correct-region)
1830 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1831 (make-variable-buffer-local 'flyspell-auto-correct-word)
1833 ;;*---------------------------------------------------------------------*/
1834 ;;* flyspell-check-previous-highlighted-word ... */
1835 ;;*---------------------------------------------------------------------*/
1836 (defun flyspell-check-previous-highlighted-word (&optional arg)
1837 "Correct the closest previous word that is highlighted as misspelled.
1838 This function scans for a word which starts before point that has been
1839 highlighted by Flyspell as misspelled. If it finds one, it proposes
1840 a replacement for that word. With prefix arg N, check the Nth word
1841 before point that's highlighted as misspelled."
1843 (let ((pos1 (point))
1845 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1848 (while (and (setq pos (previous-overlay-change pos))
1851 (if (> pos (point-min))
1853 (setq ovs (overlays-at pos))
1856 (setq ovs (cdr ovs))
1857 (if (and (flyspell-overlay-p ov)
1858 (= 0 (setq arg (1- arg))))
1859 (throw 'exit t)))))))
1863 (setq flyspell-word-cache-word nil) ;; Force flyspell-word re-check
1865 (error "No word to correct before point"))))
1867 ;;*---------------------------------------------------------------------*/
1868 ;;* flyspell-display-next-corrections ... */
1869 ;;*---------------------------------------------------------------------*/
1870 (defun flyspell-display-next-corrections (corrections)
1871 (let ((string "Corrections:")
1874 (while (< (length string) 80)
1875 (if (equal (car l) flyspell-auto-correct-word)
1876 (setq pos (cons (+ 1 (length string)) pos)))
1877 (setq string (concat string " " (car l)))
1880 (let ((num (car pos)))
1881 (put-text-property num
1882 (+ num (length flyspell-auto-correct-word))
1883 'face 'flyspell-incorrect
1885 (setq pos (cdr pos)))
1886 (if (fboundp 'display-message)
1887 (display-message 'no-log string)
1888 (message "%s" string))))
1890 ;;*---------------------------------------------------------------------*/
1891 ;;* flyspell-abbrev-table ... */
1892 ;;*---------------------------------------------------------------------*/
1893 (defun flyspell-abbrev-table ()
1894 (if flyspell-use-global-abbrev-table-p
1896 (or local-abbrev-table global-abbrev-table)))
1898 ;;*---------------------------------------------------------------------*/
1899 ;;* flyspell-define-abbrev ... */
1900 ;;*---------------------------------------------------------------------*/
1901 (defun flyspell-define-abbrev (name expansion)
1902 (let ((table (flyspell-abbrev-table)))
1904 (define-abbrev table (downcase name) expansion))))
1906 ;;*---------------------------------------------------------------------*/
1907 ;;* flyspell-auto-correct-word ... */
1908 ;;*---------------------------------------------------------------------*/
1909 (defun flyspell-auto-correct-word ()
1910 "Correct the current word.
1911 This command proposes various successive corrections for the current word."
1913 ;; If we are not in the construct where flyspell should be active,
1914 ;; invoke the original binding of M-TAB, if that was recorded.
1915 (if (and (local-variable-p 'flyspell--prev-meta-tab-binding)
1916 (commandp flyspell--prev-meta-tab-binding t)
1917 (fboundp flyspell-generic-check-word-predicate)
1918 (not (funcall flyspell-generic-check-word-predicate))
1919 (equal (where-is-internal 'flyspell-auto-correct-word nil t)
1921 (call-interactively flyspell--prev-meta-tab-binding)
1923 (old-max (point-max)))
1924 ;; Use the correct dictionary.
1925 (flyspell-accept-buffer-local-defs)
1926 (if (and (eq flyspell-auto-correct-pos pos)
1927 (consp flyspell-auto-correct-region))
1928 ;; We have already been using the function at the same location.
1929 (let* ((start (car flyspell-auto-correct-region))
1930 (len (cdr flyspell-auto-correct-region)))
1931 (flyspell-unhighlight-at start)
1932 (delete-region start (+ start len))
1933 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1934 (let* ((word (car flyspell-auto-correct-ring))
1935 (len (length word)))
1936 (rplacd flyspell-auto-correct-region len)
1938 (if flyspell-abbrev-p
1939 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1940 flyspell-auto-correct-word)
1941 (flyspell-change-abbrev (flyspell-abbrev-table)
1942 flyspell-auto-correct-word
1944 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1945 (funcall flyspell-insert-function word)
1947 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1948 (flyspell-ajust-cursor-point pos (point) old-max)
1949 (setq flyspell-auto-correct-pos (point)))
1950 ;; Fetch the word to be checked.
1951 (let ((word (flyspell-get-word)))
1953 (let ((start (car (cdr word)))
1954 (end (car (cdr (cdr word))))
1957 (setq flyspell-auto-correct-word word)
1958 ;; Now check spelling of word..
1959 (ispell-send-string "%\n") ;Put in verbose mode.
1960 (ispell-send-string (concat "^" word "\n"))
1961 ;; Wait until ispell has processed word.
1963 (accept-process-output ispell-process)
1964 (not (string= "" (car ispell-filter)))))
1965 ;; Remove leading empty element.
1966 (setq ispell-filter (cdr ispell-filter))
1967 ;; Ispell process should return something after word is sent.
1968 ;; Tag word as valid (i.e., skip) otherwise.
1970 (setq ispell-filter '(*)))
1971 (if (consp ispell-filter)
1972 (setq poss (ispell-parse-output (car ispell-filter))))
1974 ((or (eq poss t) (stringp poss))
1975 ;; Don't correct word.
1979 (error "Ispell: error in Ispell process"))
1981 ;; The word is incorrect, we have to propose a replacement.
1982 (let ((replacements (if flyspell-sort-corrections
1983 (sort (car (cdr (cdr poss))) 'string<)
1984 (car (cdr (cdr poss))))))
1985 (setq flyspell-auto-correct-region nil)
1986 (if (consp replacements)
1988 (let ((replace (car replacements)))
1989 (let ((new-word replace))
1990 (if (not (equal new-word (car poss)))
1992 ;; the save the current replacements
1993 (setq flyspell-auto-correct-region
1994 (cons start (length new-word)))
1995 (let ((l replacements))
1996 (while (consp (cdr l))
1998 (rplacd l (cons (car poss) replacements)))
1999 (setq flyspell-auto-correct-ring
2001 (flyspell-unhighlight-at start)
2002 (delete-region start end)
2003 (funcall flyspell-insert-function new-word)
2004 (if flyspell-abbrev-p
2005 (if (flyspell-already-abbrevp
2006 (flyspell-abbrev-table) word)
2007 (flyspell-change-abbrev
2008 (flyspell-abbrev-table)
2011 (flyspell-define-abbrev word
2014 (flyspell-display-next-corrections
2015 (cons new-word flyspell-auto-correct-ring))
2016 (flyspell-ajust-cursor-point pos
2019 (setq flyspell-auto-correct-pos (point))
2020 (ispell-pdict-save t))))))))
2022 ;;*---------------------------------------------------------------------*/
2023 ;;* flyspell-auto-correct-previous-pos ... */
2024 ;;*---------------------------------------------------------------------*/
2025 (defvar flyspell-auto-correct-previous-pos nil
2026 "Holds the start of the first incorrect word before point.")
2028 ;;*---------------------------------------------------------------------*/
2029 ;;* flyspell-auto-correct-previous-hook ... */
2030 ;;*---------------------------------------------------------------------*/
2031 (defun flyspell-auto-correct-previous-hook ()
2032 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
2033 Sets `flyspell-auto-correct-previous-pos' to nil"
2035 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
2036 (unless (eq this-command (function flyspell-auto-correct-previous-word))
2037 (setq flyspell-auto-correct-previous-pos nil)))
2039 ;;*---------------------------------------------------------------------*/
2040 ;;* flyspell-auto-correct-previous-word ... */
2041 ;;*---------------------------------------------------------------------*/
2042 (defun flyspell-auto-correct-previous-word (position)
2043 "Auto correct the first misspelled word that occurs before point.
2044 But don't look beyond what's visible on the screen."
2047 (let ((top (window-start))
2051 (narrow-to-region top bot)
2052 (overlay-recenter (point))
2054 (add-hook 'pre-command-hook
2055 (function flyspell-auto-correct-previous-hook) t t)
2057 (unless flyspell-auto-correct-previous-pos
2058 ;; only reset if a new overlay exists
2059 (setq flyspell-auto-correct-previous-pos nil)
2061 (let ((overlay-list (overlays-in (point-min) position))
2062 (new-overlay 'dummy-value))
2064 ;; search for previous (new) flyspell overlay
2065 (while (and new-overlay
2066 (or (not (flyspell-overlay-p new-overlay))
2067 ;; check if its face has changed
2068 (not (eq (get-char-property
2069 (overlay-start new-overlay) 'face)
2070 'flyspell-incorrect))))
2071 (setq new-overlay (car-safe overlay-list))
2072 (setq overlay-list (cdr-safe overlay-list)))
2074 ;; if nothing new exits new-overlay should be nil
2075 (if new-overlay ;; the length of the word may change so go to the start
2076 (setq flyspell-auto-correct-previous-pos
2077 (overlay-start new-overlay)))))
2079 (when flyspell-auto-correct-previous-pos
2081 (goto-char flyspell-auto-correct-previous-pos)
2082 (let ((ispell-following-word t)) ;; point is at start
2083 (if (numberp flyspell-auto-correct-previous-pos)
2084 (goto-char flyspell-auto-correct-previous-pos))
2085 (flyspell-auto-correct-word))
2086 ;; the point may have moved so reset this
2087 (setq flyspell-auto-correct-previous-pos (point))))))))
2089 ;;*---------------------------------------------------------------------*/
2090 ;;* flyspell-correct-word ... */
2091 ;;*---------------------------------------------------------------------*/
2093 (defun flyspell-correct-word (event)
2094 "Pop up a menu of possible corrections for a misspelled word.
2095 The word checked is the word at the mouse position."
2097 (let ((save (point)))
2098 (mouse-set-point event)
2099 (flyspell-correct-word-before-point event save)))
2101 (defun flyspell-correct-word-before-point (&optional event opoint)
2102 "Pop up a menu of possible corrections for misspelled word before point.
2103 If EVENT is non-nil, it is the mouse event that invoked this operation;
2104 that controls where to put the menu.
2105 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2107 ;; use the correct dictionary
2108 (flyspell-accept-buffer-local-defs)
2109 (or opoint (setq opoint (point)))
2110 (let ((cursor-location (point))
2111 (word (flyspell-get-word)))
2113 (let ((start (car (cdr word)))
2114 (end (car (cdr (cdr word))))
2117 ;; now check spelling of word.
2118 (ispell-send-string "%\n") ;put in verbose mode
2119 (ispell-send-string (concat "^" word "\n"))
2120 ;; wait until ispell has processed word
2122 (accept-process-output ispell-process)
2123 (not (string= "" (car ispell-filter)))))
2124 ;; Remove leading empty element
2125 (setq ispell-filter (cdr ispell-filter))
2126 ;; ispell process should return something after word is sent.
2127 ;; Tag word as valid (i.e., skip) otherwise
2129 (setq ispell-filter '(*)))
2130 (if (consp ispell-filter)
2131 (setq poss (ispell-parse-output (car ispell-filter))))
2133 ((or (eq poss t) (stringp poss))
2134 ;; don't correct word
2138 (error "Ispell: error in Ispell process"))
2140 (flyspell-xemacs-popup
2141 poss word cursor-location start end opoint))
2143 ;; The word is incorrect, we have to propose a replacement.
2144 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2145 poss word cursor-location start end opoint)))
2146 (ispell-pdict-save t)))))
2148 ;;*---------------------------------------------------------------------*/
2149 ;;* flyspell-do-correct ... */
2150 ;;*---------------------------------------------------------------------*/
2151 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2152 "The popup menu callback."
2153 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2154 ;; it instead right after calling the function.
2155 (cond ((eq replace 'ignore)
2160 (ispell-send-string (concat "*" word "\n"))
2161 ;; This was added only to the XEmacs side in revision 1.18 of
2162 ;; flyspell. I assume its absence on the Emacs side was an
2163 ;; oversight. --Stef
2164 (ispell-send-string "#\n")
2165 (flyspell-unhighlight-at cursor-location)
2166 (setq ispell-pdict-modified-p '(t)))
2167 ((or (eq replace 'buffer) (eq replace 'session))
2168 (ispell-send-string (concat "@" word "\n"))
2169 (add-to-list 'ispell-buffer-session-localwords word)
2170 (or ispell-buffer-local-name ; session localwords might conflict
2171 (setq ispell-buffer-local-name (buffer-name)))
2172 (flyspell-unhighlight-at cursor-location)
2173 (if (null ispell-pdict-modified-p)
2174 (setq ispell-pdict-modified-p
2175 (list ispell-pdict-modified-p)))
2177 (if (eq replace 'buffer)
2178 (ispell-add-per-file-word-list word)))
2180 ;; This was added only to the Emacs side. I assume its absence on
2181 ;; the XEmacs side was an oversight. --Stef
2182 (flyspell-unhighlight-at cursor-location)
2183 (let ((old-max (point-max))
2184 (new-word (if (atom replace)
2187 (cursor-location (+ (- (length word) (- end start))
2189 (unless (equal new-word (car poss))
2190 (delete-region start end)
2192 (funcall flyspell-insert-function new-word)
2193 (if flyspell-abbrev-p
2194 (flyspell-define-abbrev word new-word)))
2195 ;; In the original Emacs code, this was only called in the body
2196 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2197 (flyspell-ajust-cursor-point save cursor-location old-max)))
2202 ;;*---------------------------------------------------------------------*/
2203 ;;* flyspell-ajust-cursor-point ... */
2204 ;;*---------------------------------------------------------------------*/
2205 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2206 (if (>= save cursor-location)
2207 (let ((new-pos (+ save (- (point-max) old-max))))
2209 ((< new-pos (point-min))
2211 ((> new-pos (point-max))
2216 ;;*---------------------------------------------------------------------*/
2217 ;;* flyspell-emacs-popup ... */
2218 ;;*---------------------------------------------------------------------*/
2219 (defun flyspell-emacs-popup (event poss word)
2220 "The Emacs popup menu."
2221 (if (and (not event)
2223 (let* ((mouse-pos (mouse-position))
2224 (mouse-pos (if (nth 1 mouse-pos)
2226 (set-mouse-position (car mouse-pos)
2227 (/ (frame-width) 2) 2)
2229 (setq event (list (list (car (cdr mouse-pos))
2230 (1+ (cdr (cdr mouse-pos))))
2232 (let* ((corrects (if flyspell-sort-corrections
2233 (sort (car (cdr (cdr poss))) 'string<)
2234 (car (cdr (cdr poss)))))
2235 (cor-menu (if (consp corrects)
2236 (mapcar (lambda (correct)
2237 (list correct correct))
2240 (affix (car (cdr (cdr (cdr poss)))))
2242 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2244 (list (concat "Save affix: " (car affix))
2246 '("Accept (session)" session)
2247 '("Accept (buffer)" buffer))
2248 '(("Save word" save)
2249 ("Accept (session)" session)
2250 ("Accept (buffer)" buffer)))))
2251 (if (consp cor-menu)
2252 (append cor-menu (cons "" save))
2254 (menu (cons "flyspell correction menu" base-menu)))
2255 (car (x-popup-menu event
2256 (list (format "%s [%s]" word (or ispell-local-dictionary
2260 ;;*---------------------------------------------------------------------*/
2261 ;;* flyspell-xemacs-popup ... */
2262 ;;*---------------------------------------------------------------------*/
2263 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2264 "The XEmacs popup menu."
2265 (let* ((corrects (if flyspell-sort-corrections
2266 (sort (car (cdr (cdr poss))) 'string<)
2267 (car (cdr (cdr poss)))))
2268 (cor-menu (if (consp corrects)
2269 (mapcar (lambda (correct)
2271 (list 'flyspell-do-correct
2282 (affix (car (cdr (cdr (cdr poss)))))
2284 (menu (let ((save (if (and (consp affix) show-affix-info)
2286 (concat "Save affix: " (car affix))
2287 (list 'flyspell-do-correct
2298 (list 'flyspell-do-correct
2307 (session (vector "Accept (session)"
2308 (list 'flyspell-do-correct
2317 (buffer (vector "Accept (buffer)"
2318 (list 'flyspell-do-correct
2327 (if (consp cor-menu)
2328 (append cor-menu (list "-" save session buffer))
2329 (list save session buffer)))))
2330 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2334 ;;*---------------------------------------------------------------------*/
2335 ;;* Some example functions for real autocorrecting */
2336 ;;*---------------------------------------------------------------------*/
2337 (defun flyspell-maybe-correct-transposition (beg end poss)
2338 "Check replacements for transposed characters.
2340 If the text between BEG and END is equal to a correction suggested by
2341 Ispell, after transposing two adjacent characters, correct the text,
2344 The third arg POSS is either the symbol `doublon' or a list of
2345 possible corrections as returned by `ispell-parse-output'.
2347 This function is meant to be added to `flyspell-incorrect-hook'."
2350 (let ((str (buffer-substring beg end))
2351 (i 0) (len (- end beg)) tmp)
2352 (while (< (1+ i) len)
2353 (setq tmp (aref str i))
2354 (aset str i (aref str (1+ i)))
2355 (aset str (1+ i) tmp)
2356 (when (member str (nth 2 poss))
2358 (goto-char (+ beg i 1))
2359 (transpose-chars 1))
2361 (setq tmp (aref str i))
2362 (aset str i (aref str (1+ i)))
2363 (aset str (1+ i) tmp)
2367 (defun flyspell-maybe-correct-doubling (beg end poss)
2368 "Check replacements for doubled characters.
2370 If the text between BEG and END is equal to a correction suggested by
2371 Ispell, after removing a pair of doubled characters, correct the text,
2374 The third arg POSS is either the symbol `doublon' or a list of
2375 possible corrections as returned by `ispell-parse-output'.
2377 This function is meant to be added to `flyspell-incorrect-hook'."
2380 (let ((str (buffer-substring beg end))
2381 (i 0) (len (- end beg)))
2382 (while (< (1+ i) len)
2383 (when (and (= (aref str i) (aref str (1+ i)))
2384 (member (concat (substring str 0 (1+ i))
2385 (substring str (+ i 2)))
2387 (goto-char (+ beg i))
2393 ;;*---------------------------------------------------------------------*/
2394 ;;* flyspell-already-abbrevp ... */
2395 ;;*---------------------------------------------------------------------*/
2396 (defun flyspell-already-abbrevp (table word)
2397 (let ((sym (abbrev-symbol word table)))
2398 (and sym (symbolp sym))))
2400 ;;*---------------------------------------------------------------------*/
2401 ;;* flyspell-change-abbrev ... */
2402 ;;*---------------------------------------------------------------------*/
2403 (defun flyspell-change-abbrev (table old new)
2404 (set (abbrev-symbol old table) new))
2408 ;;; flyspell.el ends here