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."
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)))
397 (defun flyspell-prog-mode ()
398 "Turn on `flyspell-mode' for comments and strings."
400 (setq flyspell-generic-check-word-predicate
401 #'flyspell-generic-progmode-verify)
402 (setq-local flyspell--prev-meta-tab-binding
403 (or (local-key-binding "\M-\t" t)
404 (global-key-binding "\M-\t" t)))
406 (run-hooks 'flyspell-prog-mode-hook))
408 ;;*---------------------------------------------------------------------*/
409 ;;* Overlay compatibility */
410 ;;*---------------------------------------------------------------------*/
411 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
412 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
413 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
414 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
415 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
416 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
417 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
418 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
420 ;;*---------------------------------------------------------------------*/
421 ;;* The minor mode declaration. */
422 ;;*---------------------------------------------------------------------*/
423 (defvar flyspell-mouse-map
424 (let ((map (make-sparse-keymap)))
425 (if (featurep 'xemacs)
426 (define-key map [button2] #'flyspell-correct-word)
427 (define-key map [down-mouse-2] #'flyspell-correct-word)
428 (define-key map [mouse-2] 'undefined))
430 "Keymap for Flyspell to put on erroneous words.")
432 (defvar flyspell-mode-map
433 (let ((map (make-sparse-keymap)))
434 (if flyspell-use-meta-tab
435 (define-key map "\M-\t" 'flyspell-auto-correct-word))
436 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
437 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
438 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
439 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
441 "Minor mode keymap for Flyspell mode--for the whole buffer.")
443 ;; dash character machinery
444 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
445 "Non-nil means that the `-' char is considered as a word delimiter.")
446 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
447 (defvar flyspell-dash-dictionary nil)
448 (make-variable-buffer-local 'flyspell-dash-dictionary)
449 (defvar flyspell-dash-local-dictionary nil)
450 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
452 ;;*---------------------------------------------------------------------*/
454 ;;*---------------------------------------------------------------------*/
455 (defface flyspell-incorrect
456 '((((supports :underline (:style wave)))
457 :underline (:style wave :color "Red1"))
459 :underline t :inherit error))
460 "Flyspell face for misspelled words."
464 (defface flyspell-duplicate
465 '((((supports :underline (:style wave)))
466 :underline (:style wave :color "DarkOrange"))
468 :underline t :inherit warning))
469 "Flyspell face for words that appear twice in a row.
470 See also `flyspell-duplicate-distance'."
474 (defvar flyspell-overlay nil)
476 ;;*---------------------------------------------------------------------*/
477 ;;* flyspell-mode ... */
478 ;;*---------------------------------------------------------------------*/
479 ;;;###autoload(defvar flyspell-mode nil "Non-nil if Flyspell mode is enabled.")
481 (define-minor-mode flyspell-mode
482 "Toggle on-the-fly spell checking (Flyspell mode).
483 With a prefix argument ARG, enable Flyspell mode if ARG is
484 positive, and disable it otherwise. If called from Lisp, enable
485 the mode if ARG is omitted or nil.
487 Flyspell mode is a buffer-local minor mode. When enabled, it
488 spawns a single Ispell process and checks each word. The default
489 flyspell behavior is to highlight incorrect words.
492 \\[ispell-word]: correct words (using Ispell).
493 \\[flyspell-auto-correct-word]: automatically correct word.
494 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
495 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
498 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
501 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
502 valid. For instance, a different dictionary can be used by
503 invoking `ispell-change-dictionary'.
505 Consider using the `ispell-parser' to check your text. For instance
507 \(add-hook \\='tex-mode-hook (function (lambda () (setq ispell-parser \\='tex))))
510 \\[flyspell-region] checks all words inside a region.
511 \\[flyspell-buffer] checks the whole buffer."
512 :lighter flyspell-mode-line-string
513 :keymap flyspell-mode-map
518 (error (message "Error enabling Flyspell mode:\n%s" (cdr err))
520 (flyspell-mode-off)))
523 (defun turn-on-flyspell ()
524 "Unconditionally turn on Flyspell mode."
528 (defun turn-off-flyspell ()
529 "Unconditionally turn off Flyspell mode."
532 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
534 ;;*---------------------------------------------------------------------*/
535 ;;* flyspell-buffers ... */
536 ;;* ------------------------------------------------------------- */
537 ;;* For remembering buffers running flyspell */
538 ;;*---------------------------------------------------------------------*/
539 (defvar flyspell-buffers nil)
541 ;;*---------------------------------------------------------------------*/
542 ;;* flyspell-minibuffer-p ... */
543 ;;*---------------------------------------------------------------------*/
544 (defun flyspell-minibuffer-p (buffer)
545 "Is BUFFER a minibuffer?"
546 (let ((ws (get-buffer-window-list buffer t)))
547 (and (consp ws) (window-minibuffer-p (car ws)))))
549 ;;*---------------------------------------------------------------------*/
550 ;;* flyspell-accept-buffer-local-defs ... */
551 ;;*---------------------------------------------------------------------*/
552 (defvar flyspell-last-buffer nil
553 "The buffer in which the last flyspell operation took place.")
555 (defun flyspell-accept-buffer-local-defs (&optional force)
556 ;; When flyspell-word is used inside a loop (e.g. when processing
557 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
558 ;; up dwarfing everything else, so only do it when the buffer has changed.
559 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
560 (setq flyspell-last-buffer (current-buffer))
561 ;; Strange problem: If buffer in current window has font-lock turned on,
562 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
563 ;; call will reset the buffer to the buffer in the current window.
564 ;; However, it only happens at startup (fix by Albert L. Ting).
566 (ispell-accept-buffer-local-defs))
567 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
568 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
569 ;; The dictionary has changed
570 (setq flyspell-dash-dictionary ispell-dictionary)
571 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
572 (setq flyspell-consider-dash-as-word-delimiter-flag
573 (member (or ispell-local-dictionary ispell-dictionary)
574 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
576 (defun flyspell-hack-local-variables-hook ()
577 ;; When local variables are loaded, see if the dictionary context
579 (flyspell-accept-buffer-local-defs 'force))
581 (defun flyspell-kill-ispell-hook ()
582 (setq flyspell-last-buffer nil)
583 (dolist (buf (buffer-list))
584 (with-current-buffer buf
585 (kill-local-variable 'flyspell-word-cache-word))))
587 ;; Make sure we flush our caches when needed. Do it here rather than in
588 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
590 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
592 ;;*---------------------------------------------------------------------*/
593 ;;* flyspell-mode-on ... */
594 ;;*---------------------------------------------------------------------*/
595 (defun flyspell-mode-on ()
596 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
597 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
598 (setq ispell-highlight-face 'flyspell-incorrect)
599 ;; local dictionaries setup
600 (or ispell-local-dictionary ispell-dictionary
601 (if flyspell-default-dictionary
602 (ispell-change-dictionary flyspell-default-dictionary)))
603 ;; we have to force ispell to accept the local definition or
604 ;; otherwise it could be too late, the local dictionary may
606 ;; Pass the `force' argument for the case where flyspell was active already
607 ;; but the buffer's local-defs have been edited.
608 (flyspell-accept-buffer-local-defs 'force)
609 ;; we put the `flyspell-delayed' property on some commands
610 (flyspell-delay-commands)
611 ;; we put the `flyspell-deplacement' property on some commands
612 (flyspell-deplacement-commands)
613 ;; we bound flyspell action to post-command hook
614 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
615 ;; we bound flyspell action to pre-command hook
616 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
617 ;; we bound flyspell action to after-change hook
618 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
619 ;; we bound flyspell action to hack-local-variables-hook
620 (add-hook 'hack-local-variables-hook
621 (function flyspell-hack-local-variables-hook) t t)
622 ;; set flyspell-generic-check-word-predicate based on the major mode
623 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
625 (setq flyspell-generic-check-word-predicate mode-predicate)))
626 ;; the welcome message
627 (if (and flyspell-issue-message-flag
628 flyspell-issue-welcome-flag
629 (if (featurep 'xemacs)
630 (interactive-p) ;; XEmacs does not have (called-interactively-p)
631 (called-interactively-p 'interactive)))
632 (let ((binding (where-is-internal 'flyspell-auto-correct-word
636 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
637 (key-description binding))
638 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
640 ;;*---------------------------------------------------------------------*/
641 ;;* flyspell-delay-commands ... */
642 ;;*---------------------------------------------------------------------*/
643 (defun flyspell-delay-commands ()
644 "Install the standard set of Flyspell delayed commands."
645 (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
646 (mapc 'flyspell-delay-command flyspell-delayed-commands))
648 ;;*---------------------------------------------------------------------*/
649 ;;* flyspell-delay-command ... */
650 ;;*---------------------------------------------------------------------*/
651 (defun flyspell-delay-command (command)
652 "Set COMMAND to be delayed, for Flyspell.
653 When flyspell `post-command-hook' is invoked because a delayed command
654 has been used, the current word is not immediately checked.
655 It will be checked only after `flyspell-delay' seconds."
656 (interactive "SDelay Flyspell after Command: ")
657 (put command 'flyspell-delayed t))
659 ;;*---------------------------------------------------------------------*/
660 ;;* flyspell-deplacement-commands ... */
661 ;;*---------------------------------------------------------------------*/
662 (defun flyspell-deplacement-commands ()
663 "Install the standard set of Flyspell deplacement commands."
664 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
665 (mapc 'flyspell-deplacement-command flyspell-deplacement-commands))
667 ;;*---------------------------------------------------------------------*/
668 ;;* flyspell-deplacement-command ... */
669 ;;*---------------------------------------------------------------------*/
670 (defun flyspell-deplacement-command (command)
671 "Set COMMAND that implement cursor movements, for Flyspell.
672 When flyspell `post-command-hook' is invoked because a deplacement command
673 has been used, the current word is not checked."
674 (interactive "SDeplacement Flyspell after Command: ")
675 (put command 'flyspell-deplacement t))
677 ;;*---------------------------------------------------------------------*/
678 ;;* flyspell-word-cache ... */
679 ;;*---------------------------------------------------------------------*/
680 (defvar flyspell-word-cache-start nil)
681 (defvar flyspell-word-cache-end nil)
682 (defvar flyspell-word-cache-word nil)
683 (defvar flyspell-word-cache-result '_)
684 (make-variable-buffer-local 'flyspell-word-cache-start)
685 (make-variable-buffer-local 'flyspell-word-cache-end)
686 (make-variable-buffer-local 'flyspell-word-cache-word)
687 (make-variable-buffer-local 'flyspell-word-cache-result)
689 ;;*---------------------------------------------------------------------*/
690 ;;* The flyspell pre-hook, store the current position. In the */
691 ;;* post command hook, we will check, if the word at this position */
692 ;;* has to be spell checked. */
693 ;;*---------------------------------------------------------------------*/
694 (defvar flyspell-pre-buffer nil "Buffer current before `this-command'.")
695 (defvar flyspell-pre-point nil "Point before running `this-command'")
696 (defvar flyspell-pre-column nil "Column before running `this-command'")
697 (defvar flyspell-pre-pre-buffer nil)
698 (defvar flyspell-pre-pre-point nil)
699 (make-variable-buffer-local 'flyspell-pre-point) ;Why?? --Stef
701 ;;*---------------------------------------------------------------------*/
702 ;;* flyspell-previous-command ... */
703 ;;*---------------------------------------------------------------------*/
704 (defvar flyspell-previous-command nil
705 "The last interactive command checked by Flyspell.")
707 ;;*---------------------------------------------------------------------*/
708 ;;* flyspell-pre-command-hook ... */
709 ;;*---------------------------------------------------------------------*/
710 (defun flyspell-pre-command-hook ()
711 "Save the current buffer and point for Flyspell's post-command hook."
713 (setq flyspell-pre-buffer (current-buffer))
714 (setq flyspell-pre-point (point))
715 (setq flyspell-pre-column (current-column)))
717 ;;*---------------------------------------------------------------------*/
718 ;;* flyspell-mode-off ... */
719 ;;*---------------------------------------------------------------------*/
721 (defun flyspell-mode-off ()
722 "Turn Flyspell mode off."
723 ;; We remove the hooks.
724 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
725 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
726 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
727 (remove-hook 'hack-local-variables-hook
728 (function flyspell-hack-local-variables-hook) t)
729 ;; We remove all the flyspell highlightings.
730 (flyspell-delete-all-overlays)
731 ;; We have to erase pre cache variables.
732 (setq flyspell-pre-buffer nil)
733 (setq flyspell-pre-point nil)
734 ;; We mark the mode as killed.
735 (setq flyspell-mode nil))
737 ;;*---------------------------------------------------------------------*/
738 ;;* flyspell-check-pre-word-p ... */
739 ;;*---------------------------------------------------------------------*/
740 (defun flyspell-check-pre-word-p ()
741 "Return non-nil if we should check the word before point.
742 More precisely, it applies to the word that was before point
743 before the current command."
744 (let ((ispell-otherchars (ispell-get-otherchars)))
746 ((not (and (numberp flyspell-pre-point)
747 (eq flyspell-pre-buffer (current-buffer))))
749 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
750 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
752 ((or (and (= flyspell-pre-point (- (point) 1))
753 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
754 (and (not (string= "" ispell-otherchars))
757 (buffer-substring-no-properties
758 flyspell-pre-point (1+ flyspell-pre-point))))))
759 (= flyspell-pre-point (point))
760 (= flyspell-pre-point (+ (point) 1)))
762 ((and (symbolp this-command)
763 (not executing-kbd-macro)
764 (or (get this-command 'flyspell-delayed)
765 (and (get this-command 'flyspell-deplacement)
766 (eq flyspell-previous-command this-command)))
767 (or (= (current-column) 0)
768 (= (current-column) flyspell-pre-column)
769 ;; If other post-command-hooks change the buffer,
770 ;; flyspell-pre-point can lie past eob (bug#468).
771 (null (char-after flyspell-pre-point))
772 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
773 (and (not (string= "" ispell-otherchars))
776 (buffer-substring-no-properties
777 flyspell-pre-point (1+ flyspell-pre-point)))))))
779 ((not (eq (current-buffer) flyspell-pre-buffer))
781 ((not (and (numberp flyspell-word-cache-start)
782 (numberp flyspell-word-cache-end)))
785 (or (< flyspell-pre-point flyspell-word-cache-start)
786 (> flyspell-pre-point flyspell-word-cache-end))))))
788 ;;*---------------------------------------------------------------------*/
789 ;;* The flyspell after-change-hook, store the change position. In */
790 ;;* the post command hook, we will check, if the word at this */
791 ;;* position has to be spell checked. */
792 ;;*---------------------------------------------------------------------*/
793 (defvar flyspell-changes nil)
794 (make-variable-buffer-local 'flyspell-changes)
796 ;;*---------------------------------------------------------------------*/
797 ;;* flyspell-after-change-function ... */
798 ;;*---------------------------------------------------------------------*/
799 (defun flyspell-after-change-function (start stop _len)
800 "Save the current buffer and point for Flyspell's post-command hook."
801 (push (cons start stop) flyspell-changes))
803 ;;*---------------------------------------------------------------------*/
804 ;;* flyspell-check-changed-word-p ... */
805 ;;*---------------------------------------------------------------------*/
806 (defun flyspell-check-changed-word-p (start stop)
807 "Return non-nil when the changed word has to be checked.
808 The answer depends of several criteria.
809 Mostly we check word delimiters."
810 (not (and (not (and (memq (char-after start) '(?\n ? )) (> stop start)))
811 (numberp flyspell-pre-point)
813 (and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
815 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))))))
817 ;;*---------------------------------------------------------------------*/
818 ;;* flyspell-check-word-p ... */
819 ;;*---------------------------------------------------------------------*/
820 (defun flyspell-check-word-p ()
821 "Return t when the word at `point' has to be checked.
822 The answer depends of several criteria.
823 Mostly we check word delimiters."
824 (let ((ispell-otherchars (ispell-get-otherchars)))
826 ((<= (- (point-max) 1) (point-min))
827 ;; The buffer is not filled enough.
829 ((and (and (> (current-column) 0)
830 (not (eq (current-column) flyspell-pre-column)))
833 (and (looking-at (flyspell-get-not-casechars))
834 (or (string= "" ispell-otherchars)
835 (not (looking-at ispell-otherchars)))
836 (or flyspell-consider-dash-as-word-delimiter-flag
837 (not (looking-at "-"))))))
838 ;; Yes because we have reached or typed a word delimiter.
840 ((symbolp this-command)
842 ((get this-command 'flyspell-deplacement)
843 (not (eq flyspell-previous-command this-command)))
844 ((get this-command 'flyspell-delayed)
845 ;; The current command is not delayed, that
846 ;; is that we must check the word now.
847 (and (not unread-command-events)
848 (sit-for flyspell-delay)))
852 ;;*---------------------------------------------------------------------*/
853 ;;* flyspell-debug-signal-no-check ... */
854 ;;*---------------------------------------------------------------------*/
855 (defun flyspell-debug-signal-no-check (msg obj)
856 (setq debug-on-error t)
857 (with-current-buffer (get-buffer-create "*flyspell-debug*")
859 (insert "NO-CHECK:\n")
860 (insert (format " %S : %S\n" msg obj))))
862 ;;*---------------------------------------------------------------------*/
863 ;;* flyspell-debug-signal-pre-word-checked ... */
864 ;;*---------------------------------------------------------------------*/
865 (defun flyspell-debug-signal-pre-word-checked ()
866 (setq debug-on-error t)
867 (with-current-buffer (get-buffer-create "*flyspell-debug*")
868 (insert "PRE-WORD:\n")
869 (insert (format " pre-point : %S\n" flyspell-pre-point))
870 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
871 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
872 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
873 (goto-char (point-max))))
875 ;;*---------------------------------------------------------------------*/
876 ;;* flyspell-debug-signal-word-checked ... */
877 ;;*---------------------------------------------------------------------*/
878 (defun flyspell-debug-signal-word-checked ()
879 (setq debug-on-error t)
880 (let ((ispell-otherchars (ispell-get-otherchars))
881 (oldbuf (current-buffer))
883 (with-current-buffer (get-buffer-create "*flyspell-debug*")
886 (format " this-cmd : %S\n" this-command)
887 (format " delayed : %S\n" (and (symbolp this-command)
890 (format " point : %S\n" point)
891 (format " prev-char : [%c] %S\n"
892 (with-current-buffer oldbuf
893 (if (bobp) ?\ (char-before)))
894 (with-current-buffer oldbuf
899 (and (looking-at (flyspell-get-not-casechars))
900 (or (string= "" ispell-otherchars)
901 (not (looking-at ispell-otherchars)))
902 (or flyspell-consider-dash-as-word-delimiter-flag
903 (not (looking-at "\\-")))
905 (format " because : %S\n"
907 ((not (and (symbolp this-command)
908 (get this-command 'flyspell-delayed)))
909 ;; The current command is not delayed, that
910 ;; is that we must check the word now.
912 ((with-current-buffer oldbuf
917 (and (looking-at (flyspell-get-not-casechars))
918 (or (string= "" ispell-otherchars)
919 (not (looking-at ispell-otherchars)))
920 (or flyspell-consider-dash-as-word-delimiter-flag
921 (not (looking-at "\\-")))))))
922 ;; Yes because we have reached or typed a word delimiter.
924 ((not (integerp flyspell-delay))
925 ;; Yes because the user set up a no-delay configuration.
929 (goto-char (point-max)))))
931 ;;*---------------------------------------------------------------------*/
932 ;;* flyspell-debug-signal-changed-checked ... */
933 ;;*---------------------------------------------------------------------*/
934 (defun flyspell-debug-signal-changed-checked ()
935 (setq debug-on-error t)
936 (let ((point (point)))
937 (with-current-buffer (get-buffer-create "*flyspell-debug*")
938 (insert "CHANGED WORD:\n")
939 (insert (format " point : %S\n" point))
940 (goto-char (point-max)))))
942 ;;*---------------------------------------------------------------------*/
943 ;;* flyspell-post-command-hook ... */
944 ;;* ------------------------------------------------------------- */
945 ;;* It is possible that we check several words: */
946 ;;* 1- the current word is checked if the predicate */
947 ;;* FLYSPELL-CHECK-WORD-P is true */
948 ;;* 2- the word that used to be the current word before the */
949 ;;* THIS-COMMAND is checked if: */
950 ;;* a- the previous word is different from the current word */
951 ;;* b- the previous word has not just been checked by the */
952 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
953 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
954 ;;* previous word nor the current word */
955 ;;*---------------------------------------------------------------------*/
956 (defun flyspell-post-command-hook ()
957 "The `post-command-hook' used by flyspell to check a word on-the-fly."
961 (let ((command this-command)
962 ;; Prevent anything we do from affecting the mark.
964 (if (flyspell-check-pre-word-p)
966 '(flyspell-debug-signal-pre-word-checked)
967 (goto-char flyspell-pre-point)
969 (if (flyspell-check-word-p)
971 '(flyspell-debug-signal-word-checked)
972 ;; FIXME: This should be asynchronous!
974 ;; we remember which word we have just checked.
975 ;; this will be used next time we will check a word
976 ;; to compare the next current word with the word
977 ;; that has been registered in the pre-command-hook
978 ;; that is these variables are used within the predicate
979 ;; FLYSPELL-CHECK-PRE-WORD-P
980 (setq flyspell-pre-pre-buffer (current-buffer))
981 (setq flyspell-pre-pre-point (point)))
982 (setq flyspell-pre-pre-buffer nil)
983 (setq flyspell-pre-pre-point nil)
984 ;; when a word is not checked because of a delayed command
985 ;; we do not disable the ispell cache.
986 (when (and (symbolp this-command)
987 (get this-command 'flyspell-delayed))
988 (setq flyspell-word-cache-end -1)
989 (setq flyspell-word-cache-result '_)))
990 (while (and (not (input-pending-p)) (consp flyspell-changes))
991 (let ((start (car (car flyspell-changes)))
992 (stop (cdr (car flyspell-changes))))
993 (if (flyspell-check-changed-word-p start stop)
995 '(flyspell-debug-signal-changed-checked)
998 (setq flyspell-changes (cdr flyspell-changes))))
999 (setq flyspell-previous-command command)))))
1001 ;;*---------------------------------------------------------------------*/
1002 ;;* flyspell-notify-misspell ... */
1003 ;;*---------------------------------------------------------------------*/
1004 (defun flyspell-notify-misspell (word poss)
1005 (let ((replacements (if (stringp poss)
1007 (if flyspell-sort-corrections
1008 (sort (car (cdr (cdr poss))) 'string<)
1009 (car (cdr (cdr poss)))))))
1010 (if flyspell-issue-message-flag
1011 (message "misspelling `%s' %S" word replacements))))
1013 ;;*---------------------------------------------------------------------*/
1014 ;;* flyspell-word-search-backward ... */
1015 ;;*---------------------------------------------------------------------*/
1016 (defun flyspell-word-search-backward (word bound &optional ignore-case)
1019 (inhibit-point-motion-hooks t)
1020 (flyspell-not-casechars (flyspell-get-not-casechars))
1021 (bound (if (and bound
1022 (> bound (point-min)))
1025 "\\(?:" flyspell-not-casechars "\\|\\`\\)"
1027 flyspell-not-casechars))
1033 (re-search-backward word-re bound t)
1038 (let ((lw (flyspell-get-word)))
1041 (string-equal (downcase (car lw)) (downcase word))
1042 (string-equal (car lw) word)))
1047 ;;*---------------------------------------------------------------------*/
1048 ;;* flyspell-word-search-forward ... */
1049 ;;*---------------------------------------------------------------------*/
1050 (defun flyspell-word-search-forward (word bound)
1053 (inhibit-point-motion-hooks t)
1054 (flyspell-not-casechars (flyspell-get-not-casechars))
1055 (bound (if (and bound
1056 (< bound (point-max)))
1058 (word-re (concat flyspell-not-casechars
1060 "\\(?:" flyspell-not-casechars "\\|\\'\\)"))
1065 (re-search-forward word-re bound t)
1070 (let ((lw (flyspell-get-word)))
1071 (if (and (consp lw) (string-equal (car lw) word))
1073 (goto-char (1+ p)))))
1076 (defvar flyspell-word) ;Backward compatibility; some predicates made use of it!
1078 ;;*---------------------------------------------------------------------*/
1079 ;;* flyspell-word ... */
1080 ;;*---------------------------------------------------------------------*/
1081 (defun flyspell-word (&optional following known-misspelling)
1082 "Spell check a word.
1083 If the optional argument FOLLOWING, or, when called interactively
1084 `ispell-following-word', is non-nil, checks the following (rather
1085 than preceding) word when the cursor is not over a word. If
1086 optional argument KNOWN-MISSPELLING is non nil considers word a
1087 misspelling and skips redundant spell-checking step."
1088 (interactive (list ispell-following-word))
1089 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1091 ;; use the correct dictionary
1092 (flyspell-accept-buffer-local-defs)
1093 (let* ((cursor-location (point))
1094 (flyspell-word (flyspell-get-word following))
1095 start end poss word ispell-filter)
1096 (if (or (eq flyspell-word nil)
1097 (and (fboundp flyspell-generic-check-word-predicate)
1098 (not (funcall flyspell-generic-check-word-predicate))))
1101 ;; destructure return flyspell-word info list.
1102 (setq start (car (cdr flyspell-word))
1103 end (car (cdr (cdr flyspell-word)))
1104 word (car flyspell-word))
1105 ;; before checking in the directory, we check for doublons.
1107 ((and (or (not (eq ispell-parser 'tex))
1108 (and (> start (point-min))
1109 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1110 flyspell-mark-duplications-flag
1111 (not (catch 'exception
1112 (let ((dict (or ispell-local-dictionary
1113 ispell-dictionary)))
1114 (dolist (except flyspell-mark-duplications-exceptions)
1115 (and (or (null (car except))
1117 (string-match (car except) dict)))
1118 (member (downcase word) (cdr except))
1119 (throw 'exception t))))))
1126 (skip-chars-backward " \t\n\f")))))
1127 (p (when (>= bound (point-min))
1128 (flyspell-word-search-backward word bound t))))
1129 (and p (/= p start)))))
1130 ;; yes, this is a doublon
1131 (flyspell-highlight-incorrect-region start end 'doublon)
1133 ((and (eq flyspell-word-cache-start start)
1134 (eq flyspell-word-cache-end end)
1135 (string-equal flyspell-word-cache-word word))
1136 ;; this word had been already checked, we skip
1137 flyspell-word-cache-result)
1138 ((and (eq ispell-parser 'tex)
1139 (flyspell-tex-command-p flyspell-word))
1140 ;; this is a correct word (because a tex command)
1141 (flyspell-unhighlight-at start)
1143 (flyspell-unhighlight-at (- end 1)))
1146 ;; we setup the cache
1147 (setq flyspell-word-cache-start start)
1148 (setq flyspell-word-cache-end end)
1149 (setq flyspell-word-cache-word word)
1150 ;; now check spelling of word.
1151 (if (not known-misspelling)
1153 (ispell-send-string "%\n")
1154 ;; put in verbose mode
1155 (ispell-send-string (concat "^" word "\n"))
1156 ;; we mark the ispell process so it can be killed
1157 ;; when emacs is exited without query
1158 (if (featurep 'xemacs)
1159 (process-kill-without-query ispell-process)
1160 (set-process-query-on-exit-flag ispell-process nil))
1161 ;; Wait until ispell has processed word.
1163 (accept-process-output ispell-process)
1164 (not (string= "" (car ispell-filter)))))
1165 ;; (ispell-send-string "!\n")
1166 ;; back to terse mode.
1167 ;; Remove leading empty element
1168 (setq ispell-filter (cdr ispell-filter))
1169 ;; ispell process should return something after word is sent.
1170 ;; Tag word as valid (i.e., skip) otherwise
1172 (setq ispell-filter '(*)))
1173 (if (consp ispell-filter)
1174 (setq poss (ispell-parse-output (car ispell-filter)))))
1175 ;; Else, this was a known misspelling to begin with, and
1176 ;; we should forge an ispell return value.
1177 (setq poss (list word 1 nil nil)))
1178 (let ((res (cond ((eq poss t)
1180 (setq flyspell-word-cache-result t)
1181 (flyspell-unhighlight-at start)
1183 (flyspell-unhighlight-at (- end 1)))
1185 ((and (stringp poss) flyspell-highlight-flag)
1187 (setq flyspell-word-cache-result t)
1188 (flyspell-unhighlight-at start)
1190 (flyspell-unhighlight-at (- end 1)))
1193 (setq flyspell-word-cache-result t)
1194 (flyspell-unhighlight-at start)
1196 (flyspell-unhighlight-at (- end 1)))
1198 ((or (and (< flyspell-duplicate-distance 0)
1201 (flyspell-word-search-backward
1206 (flyspell-word-search-forward
1209 (and (> flyspell-duplicate-distance 0)
1212 (flyspell-word-search-backward
1215 flyspell-duplicate-distance)))
1218 (flyspell-word-search-forward
1221 flyspell-duplicate-distance))))))
1222 ;; This is a misspelled word which occurs
1223 ;; twice within flyspell-duplicate-distance.
1224 (setq flyspell-word-cache-result nil)
1225 (if flyspell-highlight-flag
1226 (flyspell-highlight-duplicate-region
1228 (message "duplicate `%s'" word))
1231 (setq flyspell-word-cache-result nil)
1232 ;; Highlight the location as incorrect,
1233 ;; including offset specified in POSS.
1234 (if flyspell-highlight-flag
1235 (flyspell-highlight-incorrect-region
1236 (if (and (consp poss)
1237 (integerp (nth 1 poss)))
1238 (+ start (nth 1 poss) -1)
1241 (flyspell-notify-misspell word poss))
1243 ;; return to original location
1244 (goto-char cursor-location)
1245 (if ispell-quit (setq ispell-quit nil))
1248 ;;*---------------------------------------------------------------------*/
1249 ;;* flyspell-math-tex-command-p ... */
1250 ;;* ------------------------------------------------------------- */
1251 ;;* This function uses the texmathp package to check if point */
1252 ;;* is within a TeX math environment. `texmathp' can yield errors */
1253 ;;* if the document is currently not valid TeX syntax. */
1254 ;;*---------------------------------------------------------------------*/
1255 (defun flyspell-math-tex-command-p ()
1256 (when (fboundp 'texmathp)
1257 (if flyspell-check-tex-math-command
1263 ;;*---------------------------------------------------------------------*/
1264 ;;* flyspell-tex-command-p ... */
1265 ;;*---------------------------------------------------------------------*/
1266 (defun flyspell-tex-command-p (word)
1267 "Return t if WORD is a TeX command."
1269 (let ((b (car (cdr word))))
1270 (and (re-search-backward "\\\\" (- (point) 100) t)
1271 (or (= (match-end 0) b)
1272 (and (goto-char (match-end 0))
1273 (looking-at flyspell-tex-command-regexp)
1274 (>= (match-end 0) b))))))
1275 (flyspell-math-tex-command-p)))
1277 (defalias 'flyspell-get-casechars 'ispell-get-casechars)
1278 (defalias 'flyspell-get-not-casechars 'ispell-get-not-casechars)
1280 ;;*---------------------------------------------------------------------*/
1281 ;;* flyspell-get-word ... */
1282 ;;*---------------------------------------------------------------------*/
1283 (defun flyspell-get-word (&optional following extra-otherchars)
1284 "Return the word for spell-checking according to Ispell syntax.
1285 Optional argument FOLLOWING non-nil means to get the following
1286 \(rather than preceding) word when the cursor is not over a word.
1287 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1288 that may be included as part of a word (see `ispell-dictionary-alist')."
1289 (let* ((flyspell-casechars (flyspell-get-casechars))
1290 (flyspell-not-casechars (flyspell-get-not-casechars))
1291 (ispell-otherchars (ispell-get-otherchars))
1292 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1293 (word-regexp (concat flyspell-casechars
1295 (if (not (string= "" ispell-otherchars))
1296 (concat ispell-otherchars "?"))
1297 (if extra-otherchars
1298 (concat extra-otherchars "?"))
1301 (if (or ispell-many-otherchars-p
1307 (if (not (looking-at flyspell-casechars))
1309 (re-search-forward flyspell-casechars nil t)
1310 (re-search-backward flyspell-casechars nil t)))
1311 ;; move to front of word
1312 (re-search-backward flyspell-not-casechars nil 'start)
1313 (while (and (or (and (not (string= "" ispell-otherchars))
1314 (looking-at ispell-otherchars))
1315 (and extra-otherchars (looking-at extra-otherchars)))
1317 (or (not did-it-once)
1318 ispell-many-otherchars-p)
1319 (not (eq prevpt (point))))
1320 (if (and extra-otherchars (looking-at extra-otherchars))
1323 (if (looking-at flyspell-casechars)
1324 (re-search-backward flyspell-not-casechars nil 'move)))
1328 (if (looking-at flyspell-casechars)
1329 (re-search-backward flyspell-not-casechars nil 'move)
1330 (backward-char -1))))
1331 ;; Now mark the word and save to string.
1332 (if (not (re-search-forward word-regexp nil t))
1335 (setq start (match-beginning 0)
1337 word (buffer-substring-no-properties start end))
1338 (list word start end)))))
1340 ;;*---------------------------------------------------------------------*/
1341 ;;* flyspell-small-region ... */
1342 ;;*---------------------------------------------------------------------*/
1343 (defun flyspell-small-region (beg end)
1344 "Flyspell text between BEG and END."
1352 (while (< (point) end)
1353 (if (and flyspell-issue-message-flag (= count 100))
1355 (message "Spell Checking...%d%%"
1356 (floor (* 100.0 (- (point) beg)) (- end beg)))
1358 (setq count (+ 1 count)))
1361 (let ((cur (point)))
1363 (if (and (< (point) end) (> (point) (+ cur 1)))
1364 (backward-char 1)))))
1366 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1369 ;;*---------------------------------------------------------------------*/
1370 ;;* flyspell-external-ispell-process ... */
1371 ;;*---------------------------------------------------------------------*/
1372 (defvar flyspell-external-ispell-process '()
1373 "The external Flyspell Ispell process.")
1375 ;;*---------------------------------------------------------------------*/
1376 ;;* flyspell-external-ispell-buffer ... */
1377 ;;*---------------------------------------------------------------------*/
1378 (defvar flyspell-external-ispell-buffer '())
1379 (defvar flyspell-large-region-buffer '())
1380 (defvar flyspell-large-region-beg (point-min))
1381 (defvar flyspell-large-region-end (point-max))
1383 ;;*---------------------------------------------------------------------*/
1384 ;;* flyspell-external-point-words ... */
1385 ;;*---------------------------------------------------------------------*/
1386 (defun flyspell-external-point-words ()
1387 "Mark words from a buffer listing incorrect words in order of appearance.
1388 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1389 \(We finish by killing that buffer and setting the variable to nil.)
1390 The buffer to mark them in is `flyspell-large-region-buffer'."
1391 (let (words-not-found
1392 (ispell-otherchars (ispell-get-otherchars))
1393 (buffer-scan-pos flyspell-large-region-beg)
1395 (with-current-buffer flyspell-external-ispell-buffer
1396 (goto-char (point-min))
1397 ;; Loop over incorrect words, in the order they were reported,
1398 ;; which is also the order they appear in the buffer being checked.
1399 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1400 ;; Bind WORD to the next one.
1401 (let ((word (match-string 1)) (wordpos (point)))
1402 ;; Here there used to be code to see if WORD is the same
1403 ;; as the previous iteration, and count the number of consecutive
1404 ;; identical words, and the loop below would search for that many.
1405 ;; That code seemed to be incorrect, and on principle, should
1406 ;; be unnecessary too. -- rms.
1407 (if flyspell-issue-message-flag
1408 (message "Spell Checking...%d%% [%s]"
1409 (floor (* 100.0 (point)) (point-max))
1411 (with-current-buffer flyspell-large-region-buffer
1412 (goto-char buffer-scan-pos)
1414 ;; Iterate on string search until string is found as word,
1415 ;; not as substring.
1417 (if (search-forward word
1418 flyspell-large-region-end t)
1421 ;; Move back into the match
1422 ;; so flyspell-get-word will find it.
1424 (flyspell-get-word)))
1425 (found (car found-list))
1426 (found-length (length found))
1427 (misspell-length (length word)))
1429 ;; Size matches, we really found it.
1430 (= found-length misspell-length)
1431 ;; Matches as part of a boundary-char separated
1434 (split-string found ispell-otherchars))
1435 ;; Misspelling has higher length than
1436 ;; what flyspell considers the word.
1437 ;; Caused by boundary-chars mismatch.
1438 ;; Validating seems safe.
1439 (< found-length misspell-length)
1440 ;; ispell treats beginning of some TeX
1441 ;; commands as nroff control sequences
1442 ;; and strips them in the list of
1443 ;; misspelled words thus giving a
1444 ;; non-existent word. Skip if ispell
1445 ;; is used, string is a TeX command
1446 ;; (char before beginning of word is
1447 ;; backslash) and none of the previous
1448 ;; conditions match.
1449 (and (not ispell-really-aspell)
1451 (goto-char (- (nth 1 found-list) 1))
1452 (if (looking-at "[\\]" )
1456 (flyspell-word nil t)
1457 ;; Search for next misspelled word will begin from
1458 ;; end of last validated match.
1459 (setq buffer-scan-pos (point))))
1460 ;; Record if misspelling is not found and try new one
1461 (cl-pushnew (concat " -> " word " - "
1462 (int-to-string wordpos))
1463 words-not-found :test #'equal)
1464 (setq keep nil)))))))
1466 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1467 ;; Warn about not found misspellings
1468 (dolist (word words-not-found)
1469 (message "%s: word not found" word))
1470 ;; Kill and forget the buffer with the list of incorrect words.
1471 (kill-buffer flyspell-external-ispell-buffer)
1472 (setq flyspell-external-ispell-buffer nil)))
1474 ;;*---------------------------------------------------------------------*/
1475 ;;* flyspell-process-localwords ... */
1476 ;;* ------------------------------------------------------------- */
1477 ;;* This function is used to prevent marking of words explicitly */
1478 ;;* declared correct. */
1479 ;;*---------------------------------------------------------------------*/
1480 (defun flyspell-process-localwords (misspellings-buffer)
1481 (let ((localwords ispell-buffer-session-localwords)
1483 (ispell-casechars (ispell-get-casechars)))
1484 ;; Get localwords from the original buffer
1486 (goto-char (point-min))
1487 ;; Localwords parsing copied from ispell.el.
1488 (while (search-forward ispell-words-keyword nil t)
1489 (let ((end (point-at-eol))
1491 ;; buffer-local words separated by a space, and can contain
1492 ;; any character other than a space. Not rigorous enough.
1493 (while (re-search-forward " *\\([^ ]+\\)" end t)
1494 (setq string (buffer-substring-no-properties (match-beginning 1)
1496 ;; This can fail when string contains a word with invalid chars.
1497 ;; Error handling needs to be added between Ispell and Emacs.
1498 (if (and (< 1 (length string))
1499 (equal 0 (string-match ispell-casechars string)))
1500 (push string localwords))))))
1501 ;; Remove localwords matches from misspellings-buffer.
1502 ;; The usual mechanism of communicating the local words to ispell
1503 ;; does not affect the special ispell process used by
1504 ;; flyspell-large-region.
1505 (with-current-buffer misspellings-buffer
1507 (dolist (word localwords)
1508 (goto-char (point-min))
1509 (let ((regexp (concat "^" word "\n")))
1510 (while (re-search-forward regexp nil t)
1511 (delete-region (match-beginning 0) (match-end 0)))))))))
1513 ;;* ---------------------------------------------------------------
1514 ;;* flyspell-check-region-doublons
1515 ;;* ---------------------------------------------------------------
1516 (defun flyspell-check-region-doublons (beg end)
1517 "Check for adjacent duplicated words (doublons) in the given region."
1520 (flyspell-word) ; Make sure current word is checked
1522 (while (and (< (point) end)
1523 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1529 ;;*---------------------------------------------------------------------*/
1530 ;;* flyspell-large-region ... */
1531 ;;*---------------------------------------------------------------------*/
1532 (defun flyspell-large-region (beg end)
1533 (let* ((curbuf (current-buffer))
1534 (buffer (get-buffer-create "*flyspell-region*")))
1535 (setq flyspell-external-ispell-buffer buffer)
1536 (setq flyspell-large-region-buffer curbuf)
1537 (setq flyspell-large-region-beg beg)
1538 (setq flyspell-large-region-end end)
1539 (flyspell-accept-buffer-local-defs)
1542 ;; this is done, we can start checking...
1543 (if flyspell-issue-message-flag (message "Checking region..."))
1545 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1546 ;; Local dictionary becomes the global dictionary in use.
1547 (setq ispell-current-dictionary
1548 (or ispell-local-dictionary ispell-dictionary))
1549 (setq ispell-current-personal-dictionary
1550 (or ispell-local-pdict ispell-personal-dictionary))
1551 (let ((args (ispell-get-ispell-args))
1552 (encoding (ispell-get-coding-system))
1554 (if (and ispell-current-dictionary ; use specified dictionary
1555 (not (member "-d" args))) ; only define if not overridden
1557 (append (list "-d" ispell-current-dictionary) args)))
1558 (if ispell-current-personal-dictionary ; use specified pers dict
1563 ispell-current-personal-dictionary)))))
1565 ;; Check for extended character mode
1566 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1567 (and extended-char-mode ; ~ extended character mode
1568 (string-match "[^~]+$" extended-char-mode)
1569 (cl-pushnew (concat "-T" (match-string 0 extended-char-mode))
1570 args :test #'equal)))
1572 ;; Add ispell-extra-args
1573 (setq args (append args ispell-extra-args))
1575 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1576 ;; for communication. ispell or older aspell/hunspell does not support this
1577 (if ispell-encoding8-command
1580 (if ispell-really-hunspell
1581 (list ispell-encoding8-command
1582 (upcase (symbol-name encoding)))
1583 (list (concat ispell-encoding8-command
1584 (symbol-name encoding)))))))
1586 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1587 (setq c (apply 'ispell-call-process-region beg
1593 (if ispell-really-aspell "list" "-l")
1597 (flyspell-process-localwords buffer)
1598 (with-current-buffer curbuf
1599 (flyspell-delete-region-overlays beg end)
1600 (flyspell-check-region-doublons beg end))
1601 (flyspell-external-point-words))
1602 (error "Can't check region")))))
1604 ;;*---------------------------------------------------------------------*/
1605 ;;* flyspell-region ... */
1606 ;;* ------------------------------------------------------------- */
1607 ;;* Because `ispell -a' is too slow, it is not possible to use */
1608 ;;* it on large region. Then, when ispell is invoked on a large */
1609 ;;* text region, a new `ispell -l' process is spawned. The */
1610 ;;* pointed out words are then searched in the region a checked with */
1611 ;;* regular flyspell means. */
1612 ;;*---------------------------------------------------------------------*/
1614 (defun flyspell-region (beg end)
1615 "Flyspell text between BEG and END."
1617 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1625 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1626 (flyspell-large-region beg end)
1627 (flyspell-small-region beg end)))))
1629 ;;*---------------------------------------------------------------------*/
1630 ;;* flyspell-buffer ... */
1631 ;;*---------------------------------------------------------------------*/
1633 (defun flyspell-buffer ()
1634 "Flyspell whole buffer."
1636 (flyspell-region (point-min) (point-max)))
1638 ;;*---------------------------------------------------------------------*/
1639 ;;* old next error position ... */
1640 ;;*---------------------------------------------------------------------*/
1641 (defvar flyspell-old-buffer-error nil)
1642 (defvar flyspell-old-pos-error nil)
1644 ;;*---------------------------------------------------------------------*/
1645 ;;* flyspell-goto-next-error ... */
1646 ;;*---------------------------------------------------------------------*/
1647 (defun flyspell-goto-next-error ()
1648 "Go to the next previously detected error.
1649 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1654 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1655 (eq pos flyspell-old-pos-error))
1657 (if (= flyspell-old-pos-error max)
1658 ;; goto beginning of buffer
1660 (message "Restarting from beginning of buffer")
1661 (goto-char (point-min)))
1663 (setq pos (point))))
1664 ;; seek the next error
1665 (while (and (< pos max)
1666 (let ((ovs (overlays-at pos))
1668 (while (and (not r) (consp ovs))
1669 (if (flyspell-overlay-p (car ovs))
1671 (setq ovs (cdr ovs))))
1673 (setq pos (1+ pos)))
1674 ;; save the current location for next invocation
1675 (setq flyspell-old-pos-error pos)
1676 (setq flyspell-old-buffer-error (current-buffer))
1679 (message "No more miss-spelled word!"))))
1681 ;;*---------------------------------------------------------------------*/
1682 ;;* flyspell-overlay-p ... */
1683 ;;*---------------------------------------------------------------------*/
1684 (defun flyspell-overlay-p (o)
1685 "Return true if O is an overlay used by flyspell."
1686 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1688 ;;*---------------------------------------------------------------------*/
1689 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1690 ;;* ------------------------------------------------------------- */
1691 ;;* Remove overlays introduced by flyspell. */
1692 ;;*---------------------------------------------------------------------*/
1693 (defun flyspell-delete-region-overlays (beg end)
1694 "Delete overlays used by flyspell in a given region."
1695 (if (featurep 'emacs)
1696 (remove-overlays beg end 'flyspell-overlay t)
1697 ;; XEmacs does not have `remove-overlays'
1698 (let ((l (overlays-in beg end)))
1701 (if (flyspell-overlay-p (car l))
1702 (delete-overlay (car l)))
1703 (setq l (cdr l)))))))
1705 (defun flyspell-delete-all-overlays ()
1706 "Delete all the overlays used by flyspell."
1707 (flyspell-delete-region-overlays (point-min) (point-max)))
1709 ;;*---------------------------------------------------------------------*/
1710 ;;* flyspell-unhighlight-at ... */
1711 ;;*---------------------------------------------------------------------*/
1712 (defun flyspell-unhighlight-at (pos)
1713 "Remove the flyspell overlay that are located at POS."
1714 (if flyspell-persistent-highlight
1715 (let ((overlays (overlays-at pos)))
1716 (while (consp overlays)
1717 (if (flyspell-overlay-p (car overlays))
1718 (delete-overlay (car overlays)))
1719 (setq overlays (cdr overlays))))
1720 (if (flyspell-overlay-p flyspell-overlay)
1721 (delete-overlay flyspell-overlay))))
1723 ;;*---------------------------------------------------------------------*/
1724 ;;* flyspell-properties-at-p ... */
1725 ;;* ------------------------------------------------------------- */
1726 ;;* Is there an highlight properties at position pos? */
1727 ;;*---------------------------------------------------------------------*/
1728 (defun flyspell-properties-at-p (pos)
1729 "Return t if there is a text property at POS, not counting `local-map'.
1730 If variable `flyspell-highlight-properties' is set to nil,
1731 text with properties are not checked. This function is used to discover
1732 if the character at POS has any other property."
1733 (let ((prop (text-properties-at pos))
1735 (while (and keep (consp prop))
1736 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1737 (setq prop (cdr (cdr prop)))
1741 ;;*---------------------------------------------------------------------*/
1742 ;;* make-flyspell-overlay ... */
1743 ;;*---------------------------------------------------------------------*/
1744 (defun make-flyspell-overlay (beg end face mouse-face)
1745 "Allocate an overlay to highlight an incorrect word.
1746 BEG and END specify the range in the buffer of that word.
1747 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1749 (let ((overlay (make-overlay beg end nil t nil)))
1750 (overlay-put overlay 'face face)
1751 (overlay-put overlay 'mouse-face mouse-face)
1752 (overlay-put overlay 'flyspell-overlay t)
1753 (overlay-put overlay 'evaporate t)
1754 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1755 (overlay-put overlay 'keymap flyspell-mouse-map)
1756 (when (eq face 'flyspell-incorrect)
1757 (and (stringp flyspell-before-incorrect-word-string)
1758 (overlay-put overlay 'before-string
1759 flyspell-before-incorrect-word-string))
1760 (and (stringp flyspell-after-incorrect-word-string)
1761 (overlay-put overlay 'after-string
1762 flyspell-after-incorrect-word-string)))
1765 ;;*---------------------------------------------------------------------*/
1766 ;;* flyspell-highlight-incorrect-region ... */
1767 ;;*---------------------------------------------------------------------*/
1768 (defun flyspell-highlight-incorrect-region (beg end poss)
1769 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1770 POSS is usually a list of possible spelling/correction lists,
1771 as returned by `ispell-parse-output'.
1772 It can also be the symbol `doublon', in the case where the word
1773 is itself incorrect, but suspiciously repeated."
1774 (let ((inhibit-read-only t))
1775 (unless (run-hook-with-args-until-success
1776 'flyspell-incorrect-hook beg end poss)
1777 (if (or flyspell-highlight-properties
1778 (not (flyspell-properties-at-p beg)))
1780 ;; we cleanup all the overlay that are in the region, not
1781 ;; beginning at the word start position
1782 (if (< (1+ beg) end)
1783 (let ((os (overlays-in (1+ beg) end)))
1785 (if (flyspell-overlay-p (car os))
1786 (delete-overlay (car os)))
1787 (setq os (cdr os)))))
1788 ;; we cleanup current overlay at the same position
1789 (flyspell-unhighlight-at beg)
1790 ;; now we can use a new overlay
1791 (setq flyspell-overlay
1792 (make-flyspell-overlay
1794 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1797 ;;*---------------------------------------------------------------------*/
1798 ;;* flyspell-highlight-duplicate-region ... */
1799 ;;*---------------------------------------------------------------------*/
1800 (defun flyspell-highlight-duplicate-region (beg end poss)
1801 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1802 POSS is a list of possible spelling/correction lists,
1803 as returned by `ispell-parse-output'."
1804 (let ((inhibit-read-only t))
1805 (unless (run-hook-with-args-until-success
1806 'flyspell-incorrect-hook beg end poss)
1807 (if (or flyspell-highlight-properties
1808 (not (flyspell-properties-at-p beg)))
1810 ;; we cleanup current overlay at the same position
1811 (flyspell-unhighlight-at beg)
1812 ;; now we can use a new overlay
1813 (setq flyspell-overlay
1814 (make-flyspell-overlay beg end
1818 ;;*---------------------------------------------------------------------*/
1819 ;;* flyspell-auto-correct-cache ... */
1820 ;;*---------------------------------------------------------------------*/
1821 (defvar flyspell-auto-correct-pos nil)
1822 (defvar flyspell-auto-correct-region nil)
1823 (defvar flyspell-auto-correct-ring nil)
1824 (defvar flyspell-auto-correct-word nil)
1825 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1826 (make-variable-buffer-local 'flyspell-auto-correct-region)
1827 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1828 (make-variable-buffer-local 'flyspell-auto-correct-word)
1830 ;;*---------------------------------------------------------------------*/
1831 ;;* flyspell-check-previous-highlighted-word ... */
1832 ;;*---------------------------------------------------------------------*/
1833 (defun flyspell-check-previous-highlighted-word (&optional arg)
1834 "Correct the closest previous word that is highlighted as misspelled.
1835 This function scans for a word which starts before point that has been
1836 highlighted by Flyspell as misspelled. If it finds one, it proposes
1837 a replacement for that word. With prefix arg N, check the Nth word
1838 before point that's highlighted as misspelled."
1840 (let ((pos1 (point))
1842 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1845 (while (and (setq pos (previous-overlay-change pos))
1848 (if (> pos (point-min))
1850 (setq ovs (overlays-at pos))
1853 (setq ovs (cdr ovs))
1854 (if (and (flyspell-overlay-p ov)
1855 (= 0 (setq arg (1- arg))))
1856 (throw 'exit t)))))))
1860 (setq flyspell-word-cache-word nil) ;; Force flyspell-word re-check
1862 (error "No word to correct before point"))))
1864 ;;*---------------------------------------------------------------------*/
1865 ;;* flyspell-display-next-corrections ... */
1866 ;;*---------------------------------------------------------------------*/
1867 (defun flyspell-display-next-corrections (corrections)
1868 (let ((string "Corrections:")
1871 (while (< (length string) 80)
1872 (if (equal (car l) flyspell-auto-correct-word)
1873 (setq pos (cons (+ 1 (length string)) pos)))
1874 (setq string (concat string " " (car l)))
1877 (let ((num (car pos)))
1878 (put-text-property num
1879 (+ num (length flyspell-auto-correct-word))
1880 'face 'flyspell-incorrect
1882 (setq pos (cdr pos)))
1883 (if (fboundp 'display-message)
1884 (display-message 'no-log string)
1885 (message "%s" string))))
1887 ;;*---------------------------------------------------------------------*/
1888 ;;* flyspell-abbrev-table ... */
1889 ;;*---------------------------------------------------------------------*/
1890 (defun flyspell-abbrev-table ()
1891 (if flyspell-use-global-abbrev-table-p
1893 (or local-abbrev-table global-abbrev-table)))
1895 ;;*---------------------------------------------------------------------*/
1896 ;;* flyspell-define-abbrev ... */
1897 ;;*---------------------------------------------------------------------*/
1898 (defun flyspell-define-abbrev (name expansion)
1899 (let ((table (flyspell-abbrev-table)))
1901 (define-abbrev table (downcase name) expansion))))
1903 ;;*---------------------------------------------------------------------*/
1904 ;;* flyspell-auto-correct-word ... */
1905 ;;*---------------------------------------------------------------------*/
1906 (defun flyspell-auto-correct-word ()
1907 "Correct the current word.
1908 This command proposes various successive corrections for the current word."
1910 ;; If we are not in the construct where flyspell should be active,
1911 ;; invoke the original binding of M-TAB, if that was recorded.
1912 (if (and (local-variable-p 'flyspell--prev-meta-tab-binding)
1913 (commandp flyspell--prev-meta-tab-binding t)
1914 (fboundp flyspell-generic-check-word-predicate)
1915 (not (funcall flyspell-generic-check-word-predicate))
1916 (equal (where-is-internal 'flyspell-auto-correct-word nil t)
1918 (call-interactively flyspell--prev-meta-tab-binding)
1920 (old-max (point-max)))
1921 ;; Use the correct dictionary.
1922 (flyspell-accept-buffer-local-defs)
1923 (if (and (eq flyspell-auto-correct-pos pos)
1924 (consp flyspell-auto-correct-region))
1925 ;; We have already been using the function at the same location.
1926 (let* ((start (car flyspell-auto-correct-region))
1927 (len (cdr flyspell-auto-correct-region)))
1928 (flyspell-unhighlight-at start)
1929 (delete-region start (+ start len))
1930 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1931 (let* ((word (car flyspell-auto-correct-ring))
1932 (len (length word)))
1933 (rplacd flyspell-auto-correct-region len)
1935 (if flyspell-abbrev-p
1936 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1937 flyspell-auto-correct-word)
1938 (flyspell-change-abbrev (flyspell-abbrev-table)
1939 flyspell-auto-correct-word
1941 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1942 (funcall flyspell-insert-function word)
1944 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1945 (flyspell-ajust-cursor-point pos (point) old-max)
1946 (setq flyspell-auto-correct-pos (point)))
1947 ;; Fetch the word to be checked.
1948 (let ((word (flyspell-get-word)))
1950 (let ((start (car (cdr word)))
1951 (end (car (cdr (cdr word))))
1954 (setq flyspell-auto-correct-word word)
1955 ;; Now check spelling of word..
1956 (ispell-send-string "%\n") ;Put in verbose mode.
1957 (ispell-send-string (concat "^" word "\n"))
1958 ;; Wait until ispell has processed word.
1960 (accept-process-output ispell-process)
1961 (not (string= "" (car ispell-filter)))))
1962 ;; Remove leading empty element.
1963 (setq ispell-filter (cdr ispell-filter))
1964 ;; Ispell process should return something after word is sent.
1965 ;; Tag word as valid (i.e., skip) otherwise.
1967 (setq ispell-filter '(*)))
1968 (if (consp ispell-filter)
1969 (setq poss (ispell-parse-output (car ispell-filter))))
1971 ((or (eq poss t) (stringp poss))
1972 ;; Don't correct word.
1976 (error "Ispell: error in Ispell process"))
1978 ;; The word is incorrect, we have to propose a replacement.
1979 (let ((replacements (if flyspell-sort-corrections
1980 (sort (car (cdr (cdr poss))) 'string<)
1981 (car (cdr (cdr poss))))))
1982 (setq flyspell-auto-correct-region nil)
1983 (if (consp replacements)
1985 (let ((replace (car replacements)))
1986 (let ((new-word replace))
1987 (if (not (equal new-word (car poss)))
1989 ;; the save the current replacements
1990 (setq flyspell-auto-correct-region
1991 (cons start (length new-word)))
1992 (let ((l replacements))
1993 (while (consp (cdr l))
1995 (rplacd l (cons (car poss) replacements)))
1996 (setq flyspell-auto-correct-ring
1998 (flyspell-unhighlight-at start)
1999 (delete-region start end)
2000 (funcall flyspell-insert-function new-word)
2001 (if flyspell-abbrev-p
2002 (if (flyspell-already-abbrevp
2003 (flyspell-abbrev-table) word)
2004 (flyspell-change-abbrev
2005 (flyspell-abbrev-table)
2008 (flyspell-define-abbrev word
2011 (flyspell-display-next-corrections
2012 (cons new-word flyspell-auto-correct-ring))
2013 (flyspell-ajust-cursor-point pos
2016 (setq flyspell-auto-correct-pos (point))
2017 (ispell-pdict-save t))))))))
2019 ;;*---------------------------------------------------------------------*/
2020 ;;* flyspell-auto-correct-previous-pos ... */
2021 ;;*---------------------------------------------------------------------*/
2022 (defvar flyspell-auto-correct-previous-pos nil
2023 "Holds the start of the first incorrect word before point.")
2025 ;;*---------------------------------------------------------------------*/
2026 ;;* flyspell-auto-correct-previous-hook ... */
2027 ;;*---------------------------------------------------------------------*/
2028 (defun flyspell-auto-correct-previous-hook ()
2029 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
2030 Sets `flyspell-auto-correct-previous-pos' to nil"
2032 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
2033 (unless (eq this-command (function flyspell-auto-correct-previous-word))
2034 (setq flyspell-auto-correct-previous-pos nil)))
2036 ;;*---------------------------------------------------------------------*/
2037 ;;* flyspell-auto-correct-previous-word ... */
2038 ;;*---------------------------------------------------------------------*/
2039 (defun flyspell-auto-correct-previous-word (position)
2040 "Auto correct the first misspelled word that occurs before point.
2041 But don't look beyond what's visible on the screen."
2044 (let ((top (window-start))
2048 (narrow-to-region top bot)
2049 (overlay-recenter (point))
2051 (add-hook 'pre-command-hook
2052 (function flyspell-auto-correct-previous-hook) t t)
2054 (unless flyspell-auto-correct-previous-pos
2055 ;; only reset if a new overlay exists
2056 (setq flyspell-auto-correct-previous-pos nil)
2058 (let ((overlay-list (overlays-in (point-min) position))
2059 (new-overlay 'dummy-value))
2061 ;; search for previous (new) flyspell overlay
2062 (while (and new-overlay
2063 (or (not (flyspell-overlay-p new-overlay))
2064 ;; check if its face has changed
2065 (not (eq (get-char-property
2066 (overlay-start new-overlay) 'face)
2067 'flyspell-incorrect))))
2068 (setq new-overlay (car-safe overlay-list))
2069 (setq overlay-list (cdr-safe overlay-list)))
2071 ;; if nothing new exits new-overlay should be nil
2072 (if new-overlay ;; the length of the word may change so go to the start
2073 (setq flyspell-auto-correct-previous-pos
2074 (overlay-start new-overlay)))))
2076 (when flyspell-auto-correct-previous-pos
2078 (goto-char flyspell-auto-correct-previous-pos)
2079 (let ((ispell-following-word t)) ;; point is at start
2080 (if (numberp flyspell-auto-correct-previous-pos)
2081 (goto-char flyspell-auto-correct-previous-pos))
2082 (flyspell-auto-correct-word))
2083 ;; the point may have moved so reset this
2084 (setq flyspell-auto-correct-previous-pos (point))))))))
2086 ;;*---------------------------------------------------------------------*/
2087 ;;* flyspell-correct-word ... */
2088 ;;*---------------------------------------------------------------------*/
2090 (defun flyspell-correct-word (event)
2091 "Pop up a menu of possible corrections for a misspelled word.
2092 The word checked is the word at the mouse position."
2094 (let ((save (point)))
2095 (mouse-set-point event)
2096 (flyspell-correct-word-before-point event save)))
2098 (defun flyspell-correct-word-before-point (&optional event opoint)
2099 "Pop up a menu of possible corrections for misspelled word before point.
2100 If EVENT is non-nil, it is the mouse event that invoked this operation;
2101 that controls where to put the menu.
2102 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2104 ;; use the correct dictionary
2105 (flyspell-accept-buffer-local-defs)
2106 (or opoint (setq opoint (point)))
2107 (let ((cursor-location (point))
2108 (word (flyspell-get-word)))
2110 (let ((start (car (cdr word)))
2111 (end (car (cdr (cdr word))))
2114 ;; now check spelling of word.
2115 (ispell-send-string "%\n") ;put in verbose mode
2116 (ispell-send-string (concat "^" word "\n"))
2117 ;; wait until ispell has processed word
2119 (accept-process-output ispell-process)
2120 (not (string= "" (car ispell-filter)))))
2121 ;; Remove leading empty element
2122 (setq ispell-filter (cdr ispell-filter))
2123 ;; ispell process should return something after word is sent.
2124 ;; Tag word as valid (i.e., skip) otherwise
2126 (setq ispell-filter '(*)))
2127 (if (consp ispell-filter)
2128 (setq poss (ispell-parse-output (car ispell-filter))))
2130 ((or (eq poss t) (stringp poss))
2131 ;; don't correct word
2135 (error "Ispell: error in Ispell process"))
2137 (flyspell-xemacs-popup
2138 poss word cursor-location start end opoint))
2140 ;; The word is incorrect, we have to propose a replacement.
2141 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2142 poss word cursor-location start end opoint)))
2143 (ispell-pdict-save t)))))
2145 ;;*---------------------------------------------------------------------*/
2146 ;;* flyspell-do-correct ... */
2147 ;;*---------------------------------------------------------------------*/
2148 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2149 "The popup menu callback."
2150 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2151 ;; it instead right after calling the function.
2152 (cond ((eq replace 'ignore)
2157 (ispell-send-string (concat "*" word "\n"))
2158 ;; This was added only to the XEmacs side in revision 1.18 of
2159 ;; flyspell. I assume its absence on the Emacs side was an
2160 ;; oversight. --Stef
2161 (ispell-send-string "#\n")
2162 (flyspell-unhighlight-at cursor-location)
2163 (setq ispell-pdict-modified-p '(t)))
2164 ((or (eq replace 'buffer) (eq replace 'session))
2165 (ispell-send-string (concat "@" word "\n"))
2166 (add-to-list 'ispell-buffer-session-localwords word)
2167 (or ispell-buffer-local-name ; session localwords might conflict
2168 (setq ispell-buffer-local-name (buffer-name)))
2169 (flyspell-unhighlight-at cursor-location)
2170 (if (null ispell-pdict-modified-p)
2171 (setq ispell-pdict-modified-p
2172 (list ispell-pdict-modified-p)))
2174 (if (eq replace 'buffer)
2175 (ispell-add-per-file-word-list word)))
2177 ;; This was added only to the Emacs side. I assume its absence on
2178 ;; the XEmacs side was an oversight. --Stef
2179 (flyspell-unhighlight-at cursor-location)
2180 (let ((old-max (point-max))
2181 (new-word (if (atom replace)
2184 (cursor-location (+ (- (length word) (- end start))
2186 (unless (equal new-word (car poss))
2187 (delete-region start end)
2189 (funcall flyspell-insert-function new-word)
2190 (if flyspell-abbrev-p
2191 (flyspell-define-abbrev word new-word)))
2192 ;; In the original Emacs code, this was only called in the body
2193 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2194 (flyspell-ajust-cursor-point save cursor-location old-max)))
2199 ;;*---------------------------------------------------------------------*/
2200 ;;* flyspell-ajust-cursor-point ... */
2201 ;;*---------------------------------------------------------------------*/
2202 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2203 (if (>= save cursor-location)
2204 (let ((new-pos (+ save (- (point-max) old-max))))
2206 ((< new-pos (point-min))
2208 ((> new-pos (point-max))
2213 ;;*---------------------------------------------------------------------*/
2214 ;;* flyspell-emacs-popup ... */
2215 ;;*---------------------------------------------------------------------*/
2216 (defun flyspell-emacs-popup (event poss word)
2217 "The Emacs popup menu."
2218 (if (and (not event)
2220 (let* ((mouse-pos (mouse-position))
2221 (mouse-pos (if (nth 1 mouse-pos)
2223 (set-mouse-position (car mouse-pos)
2224 (/ (frame-width) 2) 2)
2226 (setq event (list (list (car (cdr mouse-pos))
2227 (1+ (cdr (cdr mouse-pos))))
2229 (let* ((corrects (if flyspell-sort-corrections
2230 (sort (car (cdr (cdr poss))) 'string<)
2231 (car (cdr (cdr poss)))))
2232 (cor-menu (if (consp corrects)
2233 (mapcar (lambda (correct)
2234 (list correct correct))
2237 (affix (car (cdr (cdr (cdr poss)))))
2239 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2241 (list (concat "Save affix: " (car affix))
2243 '("Accept (session)" session)
2244 '("Accept (buffer)" buffer))
2245 '(("Save word" save)
2246 ("Accept (session)" session)
2247 ("Accept (buffer)" buffer)))))
2248 (if (consp cor-menu)
2249 (append cor-menu (cons "" save))
2251 (menu (cons "flyspell correction menu" base-menu)))
2252 (car (x-popup-menu event
2253 (list (format "%s [%s]" word (or ispell-local-dictionary
2257 ;;*---------------------------------------------------------------------*/
2258 ;;* flyspell-xemacs-popup ... */
2259 ;;*---------------------------------------------------------------------*/
2260 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2261 "The XEmacs popup menu."
2262 (let* ((corrects (if flyspell-sort-corrections
2263 (sort (car (cdr (cdr poss))) 'string<)
2264 (car (cdr (cdr poss)))))
2265 (cor-menu (if (consp corrects)
2266 (mapcar (lambda (correct)
2268 (list 'flyspell-do-correct
2279 (affix (car (cdr (cdr (cdr poss)))))
2281 (menu (let ((save (if (and (consp affix) show-affix-info)
2283 (concat "Save affix: " (car affix))
2284 (list 'flyspell-do-correct
2295 (list 'flyspell-do-correct
2304 (session (vector "Accept (session)"
2305 (list 'flyspell-do-correct
2314 (buffer (vector "Accept (buffer)"
2315 (list 'flyspell-do-correct
2324 (if (consp cor-menu)
2325 (append cor-menu (list "-" save session buffer))
2326 (list save session buffer)))))
2327 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2331 ;;*---------------------------------------------------------------------*/
2332 ;;* Some example functions for real autocorrecting */
2333 ;;*---------------------------------------------------------------------*/
2334 (defun flyspell-maybe-correct-transposition (beg end poss)
2335 "Check replacements for transposed characters.
2337 If the text between BEG and END is equal to a correction suggested by
2338 Ispell, after transposing two adjacent characters, correct the text,
2341 The third arg POSS is either the symbol `doublon' or a list of
2342 possible corrections as returned by `ispell-parse-output'.
2344 This function is meant to be added to `flyspell-incorrect-hook'."
2347 (let ((str (buffer-substring beg end))
2348 (i 0) (len (- end beg)) tmp)
2349 (while (< (1+ i) len)
2350 (setq tmp (aref str i))
2351 (aset str i (aref str (1+ i)))
2352 (aset str (1+ i) tmp)
2353 (when (member str (nth 2 poss))
2355 (goto-char (+ beg i 1))
2356 (transpose-chars 1))
2358 (setq tmp (aref str i))
2359 (aset str i (aref str (1+ i)))
2360 (aset str (1+ i) tmp)
2364 (defun flyspell-maybe-correct-doubling (beg end poss)
2365 "Check replacements for doubled characters.
2367 If the text between BEG and END is equal to a correction suggested by
2368 Ispell, after removing a pair of doubled characters, correct the text,
2371 The third arg POSS is either the symbol `doublon' or a list of
2372 possible corrections as returned by `ispell-parse-output'.
2374 This function is meant to be added to `flyspell-incorrect-hook'."
2377 (let ((str (buffer-substring beg end))
2378 (i 0) (len (- end beg)))
2379 (while (< (1+ i) len)
2380 (when (and (= (aref str i) (aref str (1+ i)))
2381 (member (concat (substring str 0 (1+ i))
2382 (substring str (+ i 2)))
2384 (goto-char (+ beg i))
2390 ;;*---------------------------------------------------------------------*/
2391 ;;* flyspell-already-abbrevp ... */
2392 ;;*---------------------------------------------------------------------*/
2393 (defun flyspell-already-abbrevp (table word)
2394 (let ((sym (abbrev-symbol word table)))
2395 (and sym (symbolp sym))))
2397 ;;*---------------------------------------------------------------------*/
2398 ;;* flyspell-change-abbrev ... */
2399 ;;*---------------------------------------------------------------------*/
2400 (defun flyspell-change-abbrev (table old new)
2401 (set (abbrev-symbol old table) new))
2405 ;;; flyspell.el ends here