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 "If non-nil, sort the corrections before popping them.
92 The sorting is controlled by the `flyspell-sort-corrections-function'
93 variable, and defaults to sorting alphabetically."
98 (defcustom flyspell-sort-corrections-function
99 'flyspell-sort-corrections-alphabetically
100 "The function used to sort corrections.
101 This only happens if `flyspell-sort-corrections' is non-nil. The
102 function takes three parameters -- the two correction candidates
103 to be sorted, and the third parameter is the word that's being
109 (defun flyspell-sort-corrections-alphabetically (corr1 corr2 _
)
110 (string< corr1 corr2
))
112 (defun flyspell-sort (corrs word
)
113 (if flyspell-sort-corrections
116 (funcall flyspell-sort-corrections-function c1 c2 word
)))
119 (defcustom flyspell-duplicate-distance
400000
120 "The maximum distance for finding duplicates of unrecognized words.
121 This applies to the feature that when a word is not found in the dictionary,
122 if the same spelling occurs elsewhere in the buffer,
123 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
124 This variable specifies how far to search to find such a duplicate.
125 -1 means no limit (search the whole buffer).
126 0 means do not search for duplicate unrecognized spellings."
128 :version
"24.5" ; -1 -> 400000
129 :type
'(choice (const :tag
"no limit" -
1)
132 (defcustom flyspell-delay
3
133 "The number of seconds to wait before checking, after a \"delayed\" command."
137 (defcustom flyspell-persistent-highlight t
138 "Non-nil means misspelled words remain highlighted until corrected.
139 If this variable is nil, only the most recently detected misspelled word
144 (defcustom flyspell-highlight-properties t
145 "Non-nil means highlight incorrect words even if a property exists for this word."
149 (defcustom flyspell-default-delayed-commands
150 '(self-insert-command
152 backward-or-forward-delete-char
154 scrollbar-vertical-drag
155 backward-delete-char-untabify
)
156 "The standard list of delayed commands for Flyspell.
157 See `flyspell-delayed-commands'."
160 :type
'(repeat (symbol)))
162 (defcustom flyspell-delayed-commands nil
163 "List of commands that are \"delayed\" for Flyspell mode.
164 After these commands, Flyspell checking is delayed for a short time,
165 whose length is specified by `flyspell-delay'."
167 :type
'(repeat (symbol)))
169 (defcustom flyspell-default-deplacement-commands
170 '(next-line previous-line
171 handle-switch-frame handle-select-window
174 "The standard list of deplacement commands for Flyspell.
175 See variable `flyspell-deplacement-commands'."
178 :type
'(repeat (symbol)))
180 (defcustom flyspell-deplacement-commands nil
181 "List of commands that are \"deplacement\" for Flyspell mode.
182 After these commands, Flyspell checking is performed only if the previous
183 command was not the very same command."
186 :type
'(repeat (symbol)))
188 (defcustom flyspell-issue-welcome-flag t
189 "Non-nil means that Flyspell should display a welcome message when started."
193 (defcustom flyspell-issue-message-flag t
194 "Non-nil means that Flyspell emits messages when checking words."
198 (defcustom flyspell-incorrect-hook nil
199 "List of functions to be called when incorrect words are encountered.
200 Each function is given three arguments. The first two
201 arguments are the beginning and the end of the incorrect region.
202 The third is either the symbol `doublon' or the list
203 of possible corrections as returned by `ispell-parse-output'.
205 If any of the functions return non-nil, the word is not highlighted as
211 (defcustom flyspell-default-dictionary nil
212 "A string that is the name of the default dictionary.
213 This is passed to the `ispell-change-dictionary' when flyspell is started.
214 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
215 when flyspell is started, the value of that variable is used instead
216 of `flyspell-default-dictionary' to select the default dictionary.
217 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
218 Ispell's ultimate default dictionary."
221 :type
'(choice string
(const :tag
"Default" nil
)))
223 (defcustom flyspell-tex-command-regexp
224 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
225 "A string that is the regular expression that matches TeX commands."
230 (defcustom flyspell-check-tex-math-command nil
231 "Non-nil means check even inside TeX math environment.
232 TeX math environments are discovered by `texmathp', implemented
233 inside AUCTeX package. That package may be found at
234 URL `http://www.gnu.org/software/auctex/'"
238 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
239 '("francais" "deutsch8" "norsk")
240 "List of dictionary names that consider `-' as word delimiter."
243 :type
'(repeat (string)))
245 (defcustom flyspell-abbrev-p
247 "If non-nil, add correction to abbreviation table."
252 (defcustom flyspell-use-global-abbrev-table-p
254 "If non-nil, prefer global abbrev table to local abbrev table."
259 (defcustom flyspell-mode-line-string
" Fly"
260 "String displayed on the mode line when flyspell is active.
261 Set this to nil if you don't want a mode line indicator."
263 :type
'(choice string
(const :tag
"None" nil
)))
265 (defcustom flyspell-large-region
1000
266 "The threshold that determines if a region is small.
267 If the region is smaller than this number of characters,
268 `flyspell-region' checks the words sequentially using regular
269 flyspell methods. Else, if the region is large, a new Ispell process is
272 Doubled words are not detected in a large region, because Ispell
273 does not check for them.
275 If this variable is nil, all regions are treated as small."
278 :type
'(choice number
(const :tag
"All small" nil
)))
280 (defcustom flyspell-insert-function
(function insert
)
281 "Function for inserting word by flyspell upon correction."
285 (defcustom flyspell-before-incorrect-word-string nil
286 "String used to indicate an incorrect word starting."
288 :type
'(choice string
(const nil
)))
290 (defcustom flyspell-after-incorrect-word-string nil
291 "String used to indicate an incorrect word ending."
293 :type
'(choice string
(const nil
)))
295 (defvar flyspell-mode-map
)
297 (defcustom flyspell-use-meta-tab t
298 "Non-nil means that flyspell uses M-TAB to correct word."
301 :initialize
'custom-initialize-default
302 :set
(lambda (sym val
)
303 (define-key flyspell-mode-map
"\M-\t"
305 'flyspell-auto-correct-word
))))
307 (defcustom flyspell-auto-correct-binding
309 "The key binding for flyspell auto correction."
313 ;;*---------------------------------------------------------------------*/
314 ;;* Mode specific options */
315 ;;* ------------------------------------------------------------- */
316 ;;* Mode specific options enable users to disable flyspell on */
317 ;;* certain word depending of the emacs mode. For instance, when */
318 ;;* using flyspell with mail-mode add the following expression */
319 ;;* in your init file: */
320 ;;* (add-hook 'mail-mode */
321 ;;* (lambda () (setq flyspell-generic-check-word-predicate */
322 ;;* 'mail-mode-flyspell-verify))) */
323 ;;*---------------------------------------------------------------------*/
324 (defvar flyspell-generic-check-word-predicate nil
325 "Function providing per-mode customization over which words are flyspelled.
326 Returns t to continue checking, nil otherwise.
327 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
328 property of the major mode name.")
329 (make-variable-buffer-local 'flyspell-generic-check-word-predicate
)
330 (define-obsolete-variable-alias 'flyspell-generic-check-word-p
331 'flyspell-generic-check-word-predicate
"25.1")
333 ;;*--- mail mode -------------------------------------------------------*/
334 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
335 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
336 (defvar message-signature-separator
)
337 (defun mail-mode-flyspell-verify ()
338 "Function used for `flyspell-generic-check-word-predicate' in Mail mode."
339 (let* ((header-end (save-excursion
340 (goto-char (point-min))
343 (regexp-quote mail-header-separator
)
348 (if (not (boundp 'message-signature-separator
))
351 (goto-char (point-max))
352 (re-search-backward message-signature-separator
353 (max header-end
(- (point) 4000)) t
)
355 (cond ((< (point) header-end
)
356 (and (save-excursion (beginning-of-line)
357 (looking-at "^Subject:"))
358 (> (point) (match-end 0))))
359 ((> (point) signature-begin
)
364 (not (looking-at "[>}|]\\|To:")))))))
366 ;;*--- texinfo mode ----------------------------------------------------*/
367 (put 'texinfo-mode
'flyspell-mode-predicate
'texinfo-mode-flyspell-verify
)
368 (defun texinfo-mode-flyspell-verify ()
369 "Function used for `flyspell-generic-check-word-predicate' in Texinfo mode."
371 (forward-word-strictly -
1)
372 (not (looking-at "@"))))
374 ;;*--- tex mode --------------------------------------------------------*/
375 (put 'tex-mode
'flyspell-mode-predicate
'tex-mode-flyspell-verify
)
376 (defun tex-mode-flyspell-verify ()
377 "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
380 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t
)))
382 (let ((this (point)))
384 (and (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}"
385 (line-end-position) t
)
386 (>= this
(match-beginning 0))
387 (<= this
(match-end 0))))))))
389 ;;*--- sgml mode -------------------------------------------------------*/
390 (put 'sgml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
391 (put 'html-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
392 (put 'nxml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
394 (autoload 'sgml-lexical-context
"sgml-mode")
396 (defun sgml-mode-flyspell-verify ()
397 "Function used for `flyspell-generic-check-word-predicate' in SGML mode.
398 Tag and attribute names are not spell checked, everything else is.
400 String values of attributes are checked because they can be text
401 like <img alt=\"Some thing.\">."
403 (not (memq (car (sgml-lexical-context))
406 ;;*---------------------------------------------------------------------*/
407 ;;* Programming mode */
408 ;;*---------------------------------------------------------------------*/
409 (defvar flyspell-prog-text-faces
410 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
411 "Faces corresponding to text in programming-mode buffers.")
413 (defun flyspell-generic-progmode-verify ()
414 "Used for `flyspell-generic-check-word-predicate' in programming modes."
415 ;; (point) is next char after the word. Must check one char before.
416 (let ((f (get-text-property (- (point) 1) 'face
)))
417 (memq f flyspell-prog-text-faces
)))
419 ;; Records the binding of M-TAB in effect before flyspell was activated.
420 (defvar flyspell--prev-meta-tab-binding
)
423 (defun flyspell-prog-mode ()
424 "Turn on `flyspell-mode' for comments and strings."
426 (setq flyspell-generic-check-word-predicate
427 #'flyspell-generic-progmode-verify
)
428 (setq-local flyspell--prev-meta-tab-binding
429 (or (local-key-binding "\M-\t" t
)
430 (global-key-binding "\M-\t" t
)))
432 (run-hooks 'flyspell-prog-mode-hook
))
434 ;;*---------------------------------------------------------------------*/
435 ;;* Overlay compatibility */
436 ;;*---------------------------------------------------------------------*/
437 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
438 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
439 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
440 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
441 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
442 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
443 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
444 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
446 ;;*---------------------------------------------------------------------*/
447 ;;* The minor mode declaration. */
448 ;;*---------------------------------------------------------------------*/
449 (defvar flyspell-mouse-map
450 (let ((map (make-sparse-keymap)))
451 (if (featurep 'xemacs
)
452 (define-key map
[button2] #'flyspell-correct-word)
453 (define-key map [down-mouse-2] #'flyspell-correct-word)
454 (define-key map [mouse-2] 'undefined))
456 "Keymap for Flyspell to put on erroneous words.")
458 (defvar flyspell-mode-map
459 (let ((map (make-sparse-keymap)))
460 (if flyspell-use-meta-tab
461 (define-key map "\M-\t" 'flyspell-auto-correct-word))
462 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
463 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
464 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
465 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
467 "Minor mode keymap for Flyspell mode--for the whole buffer.")
469 ;; dash character machinery
470 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
471 "Non-nil means that the `-' char is considered as a word delimiter.")
472 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
473 (defvar flyspell-dash-dictionary nil)
474 (make-variable-buffer-local 'flyspell-dash-dictionary)
475 (defvar flyspell-dash-local-dictionary nil)
476 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
478 ;;*---------------------------------------------------------------------*/
480 ;;*---------------------------------------------------------------------*/
481 (defface flyspell-incorrect
482 '((((supports :underline (:style wave)))
483 :underline (:style wave :color "Red1"))
485 :underline t :inherit error))
486 "Flyspell face for misspelled words."
490 (defface flyspell-duplicate
491 '((((supports :underline (:style wave)))
492 :underline (:style wave :color "DarkOrange"))
494 :underline t :inherit warning))
495 "Flyspell face for words that appear twice in a row.
496 See also `flyspell-duplicate-distance'."
500 (defvar flyspell-overlay nil)
502 ;;*---------------------------------------------------------------------*/
503 ;;* flyspell-mode ... */
504 ;;*---------------------------------------------------------------------*/
505 ;;;###autoload(defvar flyspell-mode nil "Non-nil if Flyspell mode is enabled.")
507 (define-minor-mode flyspell-mode
508 "Toggle on-the-fly spell checking (Flyspell mode).
509 With a prefix argument ARG, enable Flyspell mode if ARG is
510 positive, and disable it otherwise. If called from Lisp, enable
511 the mode if ARG is omitted or nil.
513 Flyspell mode is a buffer-local minor mode. When enabled, it
514 spawns a single Ispell process and checks each word. The default
515 flyspell behavior is to highlight incorrect words.
518 \\[ispell-word]: correct words (using Ispell).
519 \\[flyspell-auto-correct-word]: automatically correct word.
520 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
521 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
524 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
527 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
528 valid. For instance, a different dictionary can be used by
529 invoking `ispell-change-dictionary'.
531 Consider using the `ispell-parser' to check your text. For instance
533 \(add-hook \\='tex-mode-hook (function (lambda () (setq ispell-parser \\='tex))))
536 \\[flyspell-region] checks all words inside a region.
537 \\[flyspell-buffer] checks the whole buffer."
538 :lighter flyspell-mode-line-string
539 :keymap flyspell-mode-map
544 (error (message "Error enabling Flyspell mode:\n%s" (cdr err))
546 (flyspell-mode-off)))
549 (defun turn-on-flyspell ()
550 "Unconditionally turn on Flyspell mode."
554 (defun turn-off-flyspell ()
555 "Unconditionally turn off Flyspell mode."
558 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
560 ;;*---------------------------------------------------------------------*/
561 ;;* flyspell-buffers ... */
562 ;;* ------------------------------------------------------------- */
563 ;;* For remembering buffers running flyspell */
564 ;;*---------------------------------------------------------------------*/
565 (defvar flyspell-buffers nil)
567 ;;*---------------------------------------------------------------------*/
568 ;;* flyspell-minibuffer-p ... */
569 ;;*---------------------------------------------------------------------*/
570 (defun flyspell-minibuffer-p (buffer)
571 "Is BUFFER a minibuffer?"
572 (let ((ws (get-buffer-window-list buffer t)))
573 (and (consp ws) (window-minibuffer-p (car ws)))))
575 ;;*---------------------------------------------------------------------*/
576 ;;* flyspell-accept-buffer-local-defs ... */
577 ;;*---------------------------------------------------------------------*/
578 (defvar flyspell-last-buffer nil
579 "The buffer in which the last flyspell operation took place.")
581 (defun flyspell-accept-buffer-local-defs (&optional force)
582 ;; When flyspell-word is used inside a loop (e.g. when processing
583 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
584 ;; up dwarfing everything else, so only do it when the buffer has changed.
585 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
586 (setq flyspell-last-buffer (current-buffer))
587 ;; Strange problem: If buffer in current window has font-lock turned on,
588 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
589 ;; call will reset the buffer to the buffer in the current window.
590 ;; However, it only happens at startup (fix by Albert L. Ting).
592 (ispell-accept-buffer-local-defs))
593 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
594 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
595 ;; The dictionary has changed
596 (setq flyspell-dash-dictionary ispell-dictionary)
597 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
598 (setq flyspell-consider-dash-as-word-delimiter-flag
599 (member (or ispell-local-dictionary ispell-dictionary)
600 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
602 (defun flyspell-hack-local-variables-hook ()
603 ;; When local variables are loaded, see if the dictionary context
605 (flyspell-accept-buffer-local-defs 'force))
607 (defun flyspell-kill-ispell-hook ()
608 (setq flyspell-last-buffer nil)
609 (dolist (buf (buffer-list))
610 (with-current-buffer buf
611 (kill-local-variable 'flyspell-word-cache-word))))
613 ;; Make sure we flush our caches when needed. Do it here rather than in
614 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
616 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
618 ;;*---------------------------------------------------------------------*/
619 ;;* flyspell-mode-on ... */
620 ;;*---------------------------------------------------------------------*/
621 (defun flyspell-mode-on ()
622 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
623 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
624 (setq ispell-highlight-face 'flyspell-incorrect)
625 ;; local dictionaries setup
626 (or ispell-local-dictionary ispell-dictionary
627 (if flyspell-default-dictionary
628 (ispell-change-dictionary flyspell-default-dictionary)))
629 ;; we have to force ispell to accept the local definition or
630 ;; otherwise it could be too late, the local dictionary may
632 ;; Pass the `force' argument for the case where flyspell was active already
633 ;; but the buffer's local-defs have been edited.
634 (flyspell-accept-buffer-local-defs 'force)
635 ;; we put the `flyspell-delayed' property on some commands
636 (flyspell-delay-commands)
637 ;; we put the `flyspell-deplacement' property on some commands
638 (flyspell-deplacement-commands)
639 ;; we bound flyspell action to post-command hook
640 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
641 ;; we bound flyspell action to pre-command hook
642 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
643 ;; we bound flyspell action to after-change hook
644 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
645 ;; we bound flyspell action to hack-local-variables-hook
646 (add-hook 'hack-local-variables-hook
647 (function flyspell-hack-local-variables-hook) t t)
648 ;; set flyspell-generic-check-word-predicate based on the major mode
649 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
651 (setq flyspell-generic-check-word-predicate mode-predicate)))
652 ;; the welcome message
653 (if (and flyspell-issue-message-flag
654 flyspell-issue-welcome-flag
655 (if (featurep 'xemacs)
656 (interactive-p) ;; XEmacs does not have (called-interactively-p)
657 (called-interactively-p 'interactive)))
658 (let ((binding (where-is-internal 'flyspell-auto-correct-word
662 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
663 (key-description binding))
664 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
666 ;;*---------------------------------------------------------------------*/
667 ;;* flyspell-delay-commands ... */
668 ;;*---------------------------------------------------------------------*/
669 (defun flyspell-delay-commands ()
670 "Install the standard set of Flyspell delayed commands."
671 (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
672 (mapc 'flyspell-delay-command flyspell-delayed-commands))
674 ;;*---------------------------------------------------------------------*/
675 ;;* flyspell-delay-command ... */
676 ;;*---------------------------------------------------------------------*/
677 (defun flyspell-delay-command (command)
678 "Set COMMAND to be delayed, for Flyspell.
679 When flyspell `post-command-hook' is invoked because a delayed command
680 has been used, the current word is not immediately checked.
681 It will be checked only after `flyspell-delay' seconds."
682 (interactive "SDelay Flyspell after Command: ")
683 (put command 'flyspell-delayed t))
685 ;;*---------------------------------------------------------------------*/
686 ;;* flyspell-deplacement-commands ... */
687 ;;*---------------------------------------------------------------------*/
688 (defun flyspell-deplacement-commands ()
689 "Install the standard set of Flyspell deplacement commands."
690 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
691 (mapc 'flyspell-deplacement-command flyspell-deplacement-commands))
693 ;;*---------------------------------------------------------------------*/
694 ;;* flyspell-deplacement-command ... */
695 ;;*---------------------------------------------------------------------*/
696 (defun flyspell-deplacement-command (command)
697 "Set COMMAND that implement cursor movements, for Flyspell.
698 When flyspell `post-command-hook' is invoked because a deplacement command
699 has been used, the current word is not checked."
700 (interactive "SDeplacement Flyspell after Command: ")
701 (put command 'flyspell-deplacement t))
703 ;;*---------------------------------------------------------------------*/
704 ;;* flyspell-word-cache ... */
705 ;;*---------------------------------------------------------------------*/
706 (defvar flyspell-word-cache-start nil)
707 (defvar flyspell-word-cache-end nil)
708 (defvar flyspell-word-cache-word nil)
709 (defvar flyspell-word-cache-result '_)
710 (make-variable-buffer-local 'flyspell-word-cache-start)
711 (make-variable-buffer-local 'flyspell-word-cache-end)
712 (make-variable-buffer-local 'flyspell-word-cache-word)
713 (make-variable-buffer-local 'flyspell-word-cache-result)
715 ;;*---------------------------------------------------------------------*/
716 ;;* The flyspell pre-hook, store the current position. In the */
717 ;;* post command hook, we will check, if the word at this position */
718 ;;* has to be spell checked. */
719 ;;*---------------------------------------------------------------------*/
720 (defvar flyspell-pre-buffer nil "Buffer current before `this-command'.")
721 (defvar flyspell-pre-point nil "Point before running `this-command'")
722 (defvar flyspell-pre-column nil "Column before running `this-command'")
723 (defvar flyspell-pre-pre-buffer nil)
724 (defvar flyspell-pre-pre-point nil)
725 (make-variable-buffer-local 'flyspell-pre-point) ;Why?? --Stef
727 ;;*---------------------------------------------------------------------*/
728 ;;* flyspell-previous-command ... */
729 ;;*---------------------------------------------------------------------*/
730 (defvar flyspell-previous-command nil
731 "The last interactive command checked by Flyspell.")
733 ;;*---------------------------------------------------------------------*/
734 ;;* flyspell-pre-command-hook ... */
735 ;;*---------------------------------------------------------------------*/
736 (defun flyspell-pre-command-hook ()
737 "Save the current buffer and point for Flyspell's post-command hook."
739 (setq flyspell-pre-buffer (current-buffer))
740 (setq flyspell-pre-point (point))
741 (setq flyspell-pre-column (current-column)))
743 ;;*---------------------------------------------------------------------*/
744 ;;* flyspell-mode-off ... */
745 ;;*---------------------------------------------------------------------*/
747 (defun flyspell-mode-off ()
748 "Turn Flyspell mode off."
749 ;; We remove the hooks.
750 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
751 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
752 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
753 (remove-hook 'hack-local-variables-hook
754 (function flyspell-hack-local-variables-hook) t)
755 ;; We remove all the flyspell highlightings.
756 (flyspell-delete-all-overlays)
757 ;; We have to erase pre cache variables.
758 (setq flyspell-pre-buffer nil)
759 (setq flyspell-pre-point nil)
760 ;; We mark the mode as killed.
761 (setq flyspell-mode nil))
763 ;;*---------------------------------------------------------------------*/
764 ;;* flyspell-check-pre-word-p ... */
765 ;;*---------------------------------------------------------------------*/
766 (defun flyspell-check-pre-word-p ()
767 "Return non-nil if we should check the word before point.
768 More precisely, it applies to the word that was before point
769 before the current command."
770 (let ((ispell-otherchars (ispell-get-otherchars)))
772 ((not (and (numberp flyspell-pre-point)
773 (eq flyspell-pre-buffer (current-buffer))))
775 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
776 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
778 ((or (and (= flyspell-pre-point (- (point) 1))
779 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
780 (and (not (string= "" ispell-otherchars))
783 (buffer-substring-no-properties
784 flyspell-pre-point (1+ flyspell-pre-point))))))
785 (= flyspell-pre-point (point))
786 (= flyspell-pre-point (+ (point) 1)))
788 ((and (symbolp this-command)
789 (not executing-kbd-macro)
790 (or (get this-command 'flyspell-delayed)
791 (and (get this-command 'flyspell-deplacement)
792 (eq flyspell-previous-command this-command)))
793 (or (= (current-column) 0)
794 (= (current-column) flyspell-pre-column)
795 ;; If other post-command-hooks change the buffer,
796 ;; flyspell-pre-point can lie past eob (bug#468).
797 (null (char-after flyspell-pre-point))
798 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
799 (and (not (string= "" ispell-otherchars))
802 (buffer-substring-no-properties
803 flyspell-pre-point (1+ flyspell-pre-point)))))))
805 ((not (eq (current-buffer) flyspell-pre-buffer))
807 ((not (and (numberp flyspell-word-cache-start)
808 (numberp flyspell-word-cache-end)))
811 (or (< flyspell-pre-point flyspell-word-cache-start)
812 (> flyspell-pre-point flyspell-word-cache-end))))))
814 ;;*---------------------------------------------------------------------*/
815 ;;* The flyspell after-change-hook, store the change position. In */
816 ;;* the post command hook, we will check, if the word at this */
817 ;;* position has to be spell checked. */
818 ;;*---------------------------------------------------------------------*/
819 (defvar flyspell-changes nil)
820 (make-variable-buffer-local 'flyspell-changes)
822 ;;*---------------------------------------------------------------------*/
823 ;;* flyspell-after-change-function ... */
824 ;;*---------------------------------------------------------------------*/
825 (defun flyspell-after-change-function (start stop _len)
826 "Save the current buffer and point for Flyspell's post-command hook."
827 (push (cons start stop) flyspell-changes))
829 ;;*---------------------------------------------------------------------*/
830 ;;* flyspell-check-changed-word-p ... */
831 ;;*---------------------------------------------------------------------*/
832 (defun flyspell-check-changed-word-p (start stop)
833 "Return non-nil when the changed word has to be checked.
834 The answer depends of several criteria.
835 Mostly we check word delimiters."
836 (not (and (not (and (memq (char-after start) '(?\n ? )) (> stop start)))
837 (numberp flyspell-pre-point)
839 (and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
841 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))))))
843 ;;*---------------------------------------------------------------------*/
844 ;;* flyspell-check-word-p ... */
845 ;;*---------------------------------------------------------------------*/
846 (defun flyspell-check-word-p ()
847 "Return t when the word at `point' has to be checked.
848 The answer depends of several criteria.
849 Mostly we check word delimiters."
850 (let ((ispell-otherchars (ispell-get-otherchars)))
852 ((<= (- (point-max) 1) (point-min))
853 ;; The buffer is not filled enough.
855 ((and (and (> (current-column) 0)
856 (not (eq (current-column) flyspell-pre-column)))
859 (and (looking-at (flyspell-get-not-casechars))
860 (or (string= "" ispell-otherchars)
861 (not (looking-at ispell-otherchars)))
862 (or flyspell-consider-dash-as-word-delimiter-flag
863 (not (looking-at "-"))))))
864 ;; Yes because we have reached or typed a word delimiter.
866 ((symbolp this-command)
868 ((get this-command 'flyspell-deplacement)
869 (not (eq flyspell-previous-command this-command)))
870 ((get this-command 'flyspell-delayed)
871 ;; The current command is not delayed, that
872 ;; is that we must check the word now.
873 (and (not unread-command-events)
874 (sit-for flyspell-delay)))
878 ;;*---------------------------------------------------------------------*/
879 ;;* flyspell-debug-signal-no-check ... */
880 ;;*---------------------------------------------------------------------*/
881 (defun flyspell-debug-signal-no-check (msg obj)
882 (setq debug-on-error t)
883 (with-current-buffer (get-buffer-create "*flyspell-debug*")
885 (insert "NO-CHECK:\n")
886 (insert (format " %S : %S\n" msg obj))))
888 ;;*---------------------------------------------------------------------*/
889 ;;* flyspell-debug-signal-pre-word-checked ... */
890 ;;*---------------------------------------------------------------------*/
891 (defun flyspell-debug-signal-pre-word-checked ()
892 (setq debug-on-error t)
893 (with-current-buffer (get-buffer-create "*flyspell-debug*")
894 (insert "PRE-WORD:\n")
895 (insert (format " pre-point : %S\n" flyspell-pre-point))
896 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
897 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
898 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
899 (goto-char (point-max))))
901 ;;*---------------------------------------------------------------------*/
902 ;;* flyspell-debug-signal-word-checked ... */
903 ;;*---------------------------------------------------------------------*/
904 (defun flyspell-debug-signal-word-checked ()
905 (setq debug-on-error t)
906 (let ((ispell-otherchars (ispell-get-otherchars))
907 (oldbuf (current-buffer))
909 (with-current-buffer (get-buffer-create "*flyspell-debug*")
912 (format " this-cmd : %S\n" this-command)
913 (format " delayed : %S\n" (and (symbolp this-command)
916 (format " point : %S\n" point)
917 (format " prev-char : [%c] %S\n"
918 (with-current-buffer oldbuf
919 (if (bobp) ?\ (char-before)))
920 (with-current-buffer oldbuf
925 (and (looking-at (flyspell-get-not-casechars))
926 (or (string= "" ispell-otherchars)
927 (not (looking-at ispell-otherchars)))
928 (or flyspell-consider-dash-as-word-delimiter-flag
929 (not (looking-at "\\-")))
931 (format " because : %S\n"
933 ((not (and (symbolp this-command)
934 (get this-command 'flyspell-delayed)))
935 ;; The current command is not delayed, that
936 ;; is that we must check the word now.
938 ((with-current-buffer oldbuf
943 (and (looking-at (flyspell-get-not-casechars))
944 (or (string= "" ispell-otherchars)
945 (not (looking-at ispell-otherchars)))
946 (or flyspell-consider-dash-as-word-delimiter-flag
947 (not (looking-at "\\-")))))))
948 ;; Yes because we have reached or typed a word delimiter.
950 ((not (integerp flyspell-delay))
951 ;; Yes because the user set up a no-delay configuration.
955 (goto-char (point-max)))))
957 ;;*---------------------------------------------------------------------*/
958 ;;* flyspell-debug-signal-changed-checked ... */
959 ;;*---------------------------------------------------------------------*/
960 (defun flyspell-debug-signal-changed-checked ()
961 (setq debug-on-error t)
962 (let ((point (point)))
963 (with-current-buffer (get-buffer-create "*flyspell-debug*")
964 (insert "CHANGED WORD:\n")
965 (insert (format " point : %S\n" point))
966 (goto-char (point-max)))))
968 ;;*---------------------------------------------------------------------*/
969 ;;* flyspell-post-command-hook ... */
970 ;;* ------------------------------------------------------------- */
971 ;;* It is possible that we check several words: */
972 ;;* 1- the current word is checked if the predicate */
973 ;;* FLYSPELL-CHECK-WORD-P is true */
974 ;;* 2- the word that used to be the current word before the */
975 ;;* THIS-COMMAND is checked if: */
976 ;;* a- the previous word is different from the current word */
977 ;;* b- the previous word has not just been checked by the */
978 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
979 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
980 ;;* previous word nor the current word */
981 ;;*---------------------------------------------------------------------*/
982 (defun flyspell-post-command-hook ()
983 "The `post-command-hook' used by flyspell to check a word on-the-fly."
987 (let ((command this-command)
988 ;; Prevent anything we do from affecting the mark.
990 (if (flyspell-check-pre-word-p)
992 '(flyspell-debug-signal-pre-word-checked)
993 (goto-char flyspell-pre-point)
995 (if (flyspell-check-word-p)
997 '(flyspell-debug-signal-word-checked)
998 ;; FIXME: This should be asynchronous!
1000 ;; we remember which word we have just checked.
1001 ;; this will be used next time we will check a word
1002 ;; to compare the next current word with the word
1003 ;; that has been registered in the pre-command-hook
1004 ;; that is these variables are used within the predicate
1005 ;; FLYSPELL-CHECK-PRE-WORD-P
1006 (setq flyspell-pre-pre-buffer (current-buffer))
1007 (setq flyspell-pre-pre-point (point)))
1008 (setq flyspell-pre-pre-buffer nil)
1009 (setq flyspell-pre-pre-point nil)
1010 ;; when a word is not checked because of a delayed command
1011 ;; we do not disable the ispell cache.
1012 (when (and (symbolp this-command)
1013 (get this-command 'flyspell-delayed))
1014 (setq flyspell-word-cache-end -1)
1015 (setq flyspell-word-cache-result '_)))
1016 (while (and (not (input-pending-p)) (consp flyspell-changes))
1017 (let ((start (car (car flyspell-changes)))
1018 (stop (cdr (car flyspell-changes))))
1019 (if (flyspell-check-changed-word-p start stop)
1021 '(flyspell-debug-signal-changed-checked)
1024 (setq flyspell-changes (cdr flyspell-changes))))
1025 (setq flyspell-previous-command command)))))
1027 ;;*---------------------------------------------------------------------*/
1028 ;;* flyspell-notify-misspell ... */
1029 ;;*---------------------------------------------------------------------*/
1030 (defun flyspell-notify-misspell (word poss)
1031 (let ((replacements (if (stringp poss)
1033 (flyspell-sort (car (cdr (cdr poss))) word))))
1034 (if flyspell-issue-message-flag
1035 (message "misspelling `%s' %S" word replacements))))
1037 ;;*---------------------------------------------------------------------*/
1038 ;;* flyspell-word-search-backward ... */
1039 ;;*---------------------------------------------------------------------*/
1040 (defun flyspell-word-search-backward (word bound &optional ignore-case)
1043 (inhibit-point-motion-hooks t)
1044 (flyspell-not-casechars (flyspell-get-not-casechars))
1045 (bound (if (and bound
1046 (> bound (point-min)))
1049 "\\(?:" flyspell-not-casechars "\\|\\`\\)"
1051 flyspell-not-casechars))
1057 (re-search-backward word-re bound t)
1062 (let ((lw (flyspell-get-word)))
1065 (string-equal (downcase (car lw)) (downcase word))
1066 (string-equal (car lw) word)))
1071 ;;*---------------------------------------------------------------------*/
1072 ;;* flyspell-word-search-forward ... */
1073 ;;*---------------------------------------------------------------------*/
1074 (defun flyspell-word-search-forward (word bound)
1077 (inhibit-point-motion-hooks t)
1078 (flyspell-not-casechars (flyspell-get-not-casechars))
1079 (bound (if (and bound
1080 (< bound (point-max)))
1082 (word-re (concat flyspell-not-casechars
1084 "\\(?:" flyspell-not-casechars "\\|\\'\\)"))
1089 (re-search-forward word-re bound t)
1094 (let ((lw (flyspell-get-word)))
1095 (if (and (consp lw) (string-equal (car lw) word))
1097 (goto-char (1+ p)))))
1100 (defvar flyspell-word) ;Backward compatibility; some predicates made use of it!
1102 ;;*---------------------------------------------------------------------*/
1103 ;;* flyspell-word ... */
1104 ;;*---------------------------------------------------------------------*/
1105 (defun flyspell-word (&optional following known-misspelling)
1106 "Spell check a word.
1107 If the optional argument FOLLOWING, or, when called interactively
1108 `ispell-following-word', is non-nil, checks the following (rather
1109 than preceding) word when the cursor is not over a word. If
1110 optional argument KNOWN-MISSPELLING is non nil considers word a
1111 misspelling and skips redundant spell-checking step."
1112 (interactive (list ispell-following-word))
1113 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1115 ;; use the correct dictionary
1116 (flyspell-accept-buffer-local-defs)
1117 (let* ((cursor-location (point))
1118 (flyspell-word (flyspell-get-word following))
1119 start end poss word ispell-filter)
1120 (if (or (eq flyspell-word nil)
1121 (and (fboundp flyspell-generic-check-word-predicate)
1122 (not (funcall flyspell-generic-check-word-predicate))))
1125 ;; destructure return flyspell-word info list.
1126 (setq start (car (cdr flyspell-word))
1127 end (car (cdr (cdr flyspell-word)))
1128 word (car flyspell-word))
1129 ;; before checking in the directory, we check for doublons.
1131 ((and (or (not (eq ispell-parser 'tex))
1132 (and (> start (point-min))
1133 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1134 flyspell-mark-duplications-flag
1135 (not (catch 'exception
1136 (let ((dict (or ispell-local-dictionary
1137 ispell-dictionary)))
1138 (dolist (except flyspell-mark-duplications-exceptions)
1139 (and (or (null (car except))
1141 (string-match (car except) dict)))
1142 (member (downcase word) (cdr except))
1143 (throw 'exception t))))))
1150 (skip-chars-backward " \t\n\f")))))
1151 (p (when (>= bound (point-min))
1152 (flyspell-word-search-backward word bound t))))
1153 (and p (/= p start)))))
1154 ;; yes, this is a doublon
1155 (flyspell-highlight-incorrect-region start end 'doublon)
1157 ((and (eq flyspell-word-cache-start start)
1158 (eq flyspell-word-cache-end end)
1159 (string-equal flyspell-word-cache-word word))
1160 ;; this word had been already checked, we skip
1161 flyspell-word-cache-result)
1162 ((and (eq ispell-parser 'tex)
1163 (flyspell-tex-command-p flyspell-word))
1164 ;; this is a correct word (because a tex command)
1165 (flyspell-unhighlight-at start)
1167 (flyspell-unhighlight-at (- end 1)))
1170 ;; we setup the cache
1171 (setq flyspell-word-cache-start start)
1172 (setq flyspell-word-cache-end end)
1173 (setq flyspell-word-cache-word word)
1174 ;; now check spelling of word.
1175 (if (not known-misspelling)
1177 (ispell-send-string "%\n")
1178 ;; put in verbose mode
1179 (ispell-send-string (concat "^" word "\n"))
1180 ;; we mark the ispell process so it can be killed
1181 ;; when emacs is exited without query
1182 (if (featurep 'xemacs)
1183 (process-kill-without-query ispell-process)
1184 (set-process-query-on-exit-flag ispell-process nil))
1185 ;; Wait until ispell has processed word.
1187 (accept-process-output ispell-process)
1188 (not (string= "" (car ispell-filter)))))
1189 ;; (ispell-send-string "!\n")
1190 ;; back to terse mode.
1191 ;; Remove leading empty element
1192 (setq ispell-filter (cdr ispell-filter))
1193 ;; ispell process should return something after word is sent.
1194 ;; Tag word as valid (i.e., skip) otherwise
1196 (setq ispell-filter '(*)))
1197 (if (consp ispell-filter)
1198 (setq poss (ispell-parse-output (car ispell-filter)))))
1199 ;; Else, this was a known misspelling to begin with, and
1200 ;; we should forge an ispell return value.
1201 (setq poss (list word 1 nil nil)))
1202 (let ((res (cond ((eq poss t)
1204 (setq flyspell-word-cache-result t)
1205 (flyspell-unhighlight-at start)
1207 (flyspell-unhighlight-at (- end 1)))
1209 ((and (stringp poss) flyspell-highlight-flag)
1211 (setq flyspell-word-cache-result t)
1212 (flyspell-unhighlight-at start)
1214 (flyspell-unhighlight-at (- end 1)))
1217 (setq flyspell-word-cache-result t)
1218 (flyspell-unhighlight-at start)
1220 (flyspell-unhighlight-at (- end 1)))
1222 ((or (and (< flyspell-duplicate-distance 0)
1225 (flyspell-word-search-backward
1230 (flyspell-word-search-forward
1233 (and (> flyspell-duplicate-distance 0)
1236 (flyspell-word-search-backward
1239 flyspell-duplicate-distance)))
1242 (flyspell-word-search-forward
1245 flyspell-duplicate-distance))))))
1246 ;; This is a misspelled word which occurs
1247 ;; twice within flyspell-duplicate-distance.
1248 (setq flyspell-word-cache-result nil)
1249 (if flyspell-highlight-flag
1250 (flyspell-highlight-duplicate-region
1252 (message "duplicate `%s'" word))
1255 (setq flyspell-word-cache-result nil)
1256 ;; Highlight the location as incorrect,
1257 ;; including offset specified in POSS.
1258 (if flyspell-highlight-flag
1259 (flyspell-highlight-incorrect-region
1260 (if (and (consp poss)
1261 (integerp (nth 1 poss)))
1262 (+ start (nth 1 poss) -1)
1265 (flyspell-notify-misspell word poss))
1267 ;; return to original location
1268 (goto-char cursor-location)
1269 (if ispell-quit (setq ispell-quit nil))
1272 ;;*---------------------------------------------------------------------*/
1273 ;;* flyspell-math-tex-command-p ... */
1274 ;;* ------------------------------------------------------------- */
1275 ;;* This function uses the texmathp package to check if point */
1276 ;;* is within a TeX math environment. `texmathp' can yield errors */
1277 ;;* if the document is currently not valid TeX syntax. */
1278 ;;*---------------------------------------------------------------------*/
1279 (defun flyspell-math-tex-command-p ()
1280 (when (fboundp 'texmathp)
1281 (if flyspell-check-tex-math-command
1287 ;;*---------------------------------------------------------------------*/
1288 ;;* flyspell-tex-command-p ... */
1289 ;;*---------------------------------------------------------------------*/
1290 (defun flyspell-tex-command-p (word)
1291 "Return t if WORD is a TeX command."
1293 (let ((b (car (cdr word))))
1294 (and (re-search-backward "\\\\" (- (point) 100) t)
1295 (or (= (match-end 0) b)
1296 (and (goto-char (match-end 0))
1297 (looking-at flyspell-tex-command-regexp)
1298 (>= (match-end 0) b))))))
1299 (flyspell-math-tex-command-p)))
1301 (defalias 'flyspell-get-casechars 'ispell-get-casechars)
1302 (defalias 'flyspell-get-not-casechars 'ispell-get-not-casechars)
1304 ;;*---------------------------------------------------------------------*/
1305 ;;* flyspell-get-word ... */
1306 ;;*---------------------------------------------------------------------*/
1307 (defun flyspell-get-word (&optional following extra-otherchars)
1308 "Return the word for spell-checking according to Ispell syntax.
1309 Optional argument FOLLOWING non-nil means to get the following
1310 \(rather than preceding) word when the cursor is not over a word.
1311 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1312 that may be included as part of a word (see `ispell-dictionary-alist')."
1313 (let* ((flyspell-casechars (flyspell-get-casechars))
1314 (flyspell-not-casechars (flyspell-get-not-casechars))
1315 (ispell-otherchars (ispell-get-otherchars))
1316 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1317 (word-regexp (concat flyspell-casechars
1319 (if (not (string= "" ispell-otherchars))
1320 (concat ispell-otherchars "?"))
1321 (if extra-otherchars
1322 (concat extra-otherchars "?"))
1325 (if (or ispell-many-otherchars-p
1331 (if (not (looking-at flyspell-casechars))
1333 (re-search-forward flyspell-casechars nil t)
1334 (re-search-backward flyspell-casechars nil t)))
1335 ;; move to front of word
1336 (re-search-backward flyspell-not-casechars nil 'start)
1337 (while (and (or (and (not (string= "" ispell-otherchars))
1338 (looking-at ispell-otherchars))
1339 (and extra-otherchars (looking-at extra-otherchars)))
1341 (or (not did-it-once)
1342 ispell-many-otherchars-p)
1343 (not (eq prevpt (point))))
1344 (if (and extra-otherchars (looking-at extra-otherchars))
1347 (if (looking-at flyspell-casechars)
1348 (re-search-backward flyspell-not-casechars nil 'move)))
1352 (if (looking-at flyspell-casechars)
1353 (re-search-backward flyspell-not-casechars nil 'move)
1354 (backward-char -1))))
1355 ;; Now mark the word and save to string.
1356 (if (not (re-search-forward word-regexp nil t))
1359 (setq start (match-beginning 0)
1361 word (buffer-substring-no-properties start end))
1362 (list word start end)))))
1364 ;;*---------------------------------------------------------------------*/
1365 ;;* flyspell-small-region ... */
1366 ;;*---------------------------------------------------------------------*/
1367 (defun flyspell-small-region (beg end)
1368 "Flyspell text between BEG and END."
1376 (while (< (point) end)
1377 (if (and flyspell-issue-message-flag (= count 100))
1379 (message "Spell Checking...%d%%"
1380 (floor (* 100.0 (- (point) beg)) (- end beg)))
1382 (setq count (+ 1 count)))
1385 (let ((cur (point)))
1387 (if (and (< (point) end) (> (point) (+ cur 1)))
1388 (backward-char 1)))))
1390 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1393 ;;*---------------------------------------------------------------------*/
1394 ;;* flyspell-external-ispell-process ... */
1395 ;;*---------------------------------------------------------------------*/
1396 (defvar flyspell-external-ispell-process '()
1397 "The external Flyspell Ispell process.")
1399 ;;*---------------------------------------------------------------------*/
1400 ;;* flyspell-external-ispell-buffer ... */
1401 ;;*---------------------------------------------------------------------*/
1402 (defvar flyspell-external-ispell-buffer '())
1403 (defvar flyspell-large-region-buffer '())
1404 (defvar flyspell-large-region-beg (point-min))
1405 (defvar flyspell-large-region-end (point-max))
1407 ;;*---------------------------------------------------------------------*/
1408 ;;* flyspell-external-point-words ... */
1409 ;;*---------------------------------------------------------------------*/
1410 (defun flyspell-external-point-words ()
1411 "Mark words from a buffer listing incorrect words in order of appearance.
1412 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1413 \(We finish by killing that buffer and setting the variable to nil.)
1414 The buffer to mark them in is `flyspell-large-region-buffer'."
1415 (let (words-not-found
1416 (ispell-otherchars (ispell-get-otherchars))
1417 (buffer-scan-pos flyspell-large-region-beg)
1419 (with-current-buffer flyspell-external-ispell-buffer
1420 (goto-char (point-min))
1421 ;; Loop over incorrect words, in the order they were reported,
1422 ;; which is also the order they appear in the buffer being checked.
1423 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1424 ;; Bind WORD to the next one.
1425 (let ((word (match-string 1)) (wordpos (point)))
1426 ;; Here there used to be code to see if WORD is the same
1427 ;; as the previous iteration, and count the number of consecutive
1428 ;; identical words, and the loop below would search for that many.
1429 ;; That code seemed to be incorrect, and on principle, should
1430 ;; be unnecessary too. -- rms.
1431 (if flyspell-issue-message-flag
1432 (message "Spell Checking...%d%% [%s]"
1433 (floor (* 100.0 (point)) (point-max))
1435 (with-current-buffer flyspell-large-region-buffer
1436 (goto-char buffer-scan-pos)
1438 ;; Iterate on string search until string is found as word,
1439 ;; not as substring.
1441 (if (search-forward word
1442 flyspell-large-region-end t)
1445 ;; Move back into the match
1446 ;; so flyspell-get-word will find it.
1448 (flyspell-get-word)))
1449 (found (car found-list))
1450 (found-length (length found))
1451 (misspell-length (length word)))
1453 ;; Size matches, we really found it.
1454 (= found-length misspell-length)
1455 ;; Matches as part of a boundary-char separated
1458 (split-string found ispell-otherchars))
1459 ;; Misspelling has higher length than
1460 ;; what flyspell considers the word.
1461 ;; Caused by boundary-chars mismatch.
1462 ;; Validating seems safe.
1463 (< found-length misspell-length)
1464 ;; ispell treats beginning of some TeX
1465 ;; commands as nroff control sequences
1466 ;; and strips them in the list of
1467 ;; misspelled words thus giving a
1468 ;; non-existent word. Skip if ispell
1469 ;; is used, string is a TeX command
1470 ;; (char before beginning of word is
1471 ;; backslash) and none of the previous
1472 ;; conditions match.
1473 (and (not ispell-really-aspell)
1475 (goto-char (- (nth 1 found-list) 1))
1476 (if (looking-at "[\\]" )
1480 (flyspell-word nil t)
1481 ;; Search for next misspelled word will begin from
1482 ;; end of last validated match.
1483 (setq buffer-scan-pos (point))))
1484 ;; Record if misspelling is not found and try new one
1485 (cl-pushnew (concat " -> " word " - "
1486 (int-to-string wordpos))
1487 words-not-found :test #'equal)
1488 (setq keep nil)))))))
1490 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1491 ;; Warn about not found misspellings
1492 (dolist (word words-not-found)
1493 (message "%s: word not found" word))
1494 ;; Kill and forget the buffer with the list of incorrect words.
1495 (kill-buffer flyspell-external-ispell-buffer)
1496 (setq flyspell-external-ispell-buffer nil)))
1498 ;;*---------------------------------------------------------------------*/
1499 ;;* flyspell-process-localwords ... */
1500 ;;* ------------------------------------------------------------- */
1501 ;;* This function is used to prevent marking of words explicitly */
1502 ;;* declared correct. */
1503 ;;*---------------------------------------------------------------------*/
1504 (defun flyspell-process-localwords (misspellings-buffer)
1505 (let ((localwords ispell-buffer-session-localwords)
1507 (ispell-casechars (ispell-get-casechars)))
1508 ;; Get localwords from the original buffer
1510 (goto-char (point-min))
1511 ;; Localwords parsing copied from ispell.el.
1512 (while (search-forward ispell-words-keyword nil t)
1513 (let ((end (point-at-eol))
1515 ;; buffer-local words separated by a space, and can contain
1516 ;; any character other than a space. Not rigorous enough.
1517 (while (re-search-forward " *\\([^ ]+\\)" end t)
1518 (setq string (buffer-substring-no-properties (match-beginning 1)
1520 ;; This can fail when string contains a word with invalid chars.
1521 ;; Error handling needs to be added between Ispell and Emacs.
1522 (if (and (< 1 (length string))
1523 (equal 0 (string-match ispell-casechars string)))
1524 (push string localwords))))))
1525 ;; Remove localwords matches from misspellings-buffer.
1526 ;; The usual mechanism of communicating the local words to ispell
1527 ;; does not affect the special ispell process used by
1528 ;; flyspell-large-region.
1529 (with-current-buffer misspellings-buffer
1531 (dolist (word localwords)
1532 (goto-char (point-min))
1533 (let ((regexp (concat "^" word "\n")))
1534 (while (re-search-forward regexp nil t)
1535 (delete-region (match-beginning 0) (match-end 0)))))))))
1537 ;;* ---------------------------------------------------------------
1538 ;;* flyspell-check-region-doublons
1539 ;;* ---------------------------------------------------------------
1540 (defun flyspell-check-region-doublons (beg end)
1541 "Check for adjacent duplicated words (doublons) in the given region."
1544 (flyspell-word) ; Make sure current word is checked
1546 (while (and (< (point) end)
1547 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1553 ;;*---------------------------------------------------------------------*/
1554 ;;* flyspell-large-region ... */
1555 ;;*---------------------------------------------------------------------*/
1556 (defun flyspell-large-region (beg end)
1557 (let* ((curbuf (current-buffer))
1558 (buffer (get-buffer-create "*flyspell-region*")))
1559 (setq flyspell-external-ispell-buffer buffer)
1560 (setq flyspell-large-region-buffer curbuf)
1561 (setq flyspell-large-region-beg beg)
1562 (setq flyspell-large-region-end end)
1563 (flyspell-accept-buffer-local-defs)
1566 ;; this is done, we can start checking...
1567 (if flyspell-issue-message-flag (message "Checking region..."))
1569 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1570 ;; Local dictionary becomes the global dictionary in use.
1571 (setq ispell-current-dictionary
1572 (or ispell-local-dictionary ispell-dictionary))
1573 (setq ispell-current-personal-dictionary
1574 (or ispell-local-pdict ispell-personal-dictionary))
1575 (let ((args (ispell-get-ispell-args))
1576 (encoding (ispell-get-coding-system))
1578 (if (and ispell-current-dictionary ; use specified dictionary
1579 (not (member "-d" args))) ; only define if not overridden
1581 (append (list "-d" ispell-current-dictionary) args)))
1582 (if ispell-current-personal-dictionary ; use specified pers dict
1587 ispell-current-personal-dictionary)))))
1589 ;; Check for extended character mode
1590 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1591 (and extended-char-mode ; ~ extended character mode
1592 (string-match "[^~]+$" extended-char-mode)
1593 (cl-pushnew (concat "-T" (match-string 0 extended-char-mode))
1594 args :test #'equal)))
1596 ;; Add ispell-extra-args
1597 (setq args (append args ispell-extra-args))
1599 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1600 ;; for communication. ispell or older aspell/hunspell does not support this
1601 (if ispell-encoding8-command
1604 (if ispell-really-hunspell
1605 (list ispell-encoding8-command
1606 (upcase (symbol-name encoding)))
1607 (list (concat ispell-encoding8-command
1608 (symbol-name encoding)))))))
1610 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1611 (setq c (apply 'ispell-call-process-region beg
1617 (if ispell-really-aspell "list" "-l")
1621 (flyspell-process-localwords buffer)
1622 (with-current-buffer curbuf
1623 (flyspell-delete-region-overlays beg end)
1624 (flyspell-check-region-doublons beg end))
1625 (flyspell-external-point-words))
1626 (error "Can't check region")))))
1628 ;;*---------------------------------------------------------------------*/
1629 ;;* flyspell-region ... */
1630 ;;* ------------------------------------------------------------- */
1631 ;;* Because `ispell -a' is too slow, it is not possible to use */
1632 ;;* it on large region. Then, when ispell is invoked on a large */
1633 ;;* text region, a new `ispell -l' process is spawned. The */
1634 ;;* pointed out words are then searched in the region a checked with */
1635 ;;* regular flyspell means. */
1636 ;;*---------------------------------------------------------------------*/
1638 (defun flyspell-region (beg end)
1639 "Flyspell text between BEG and END."
1641 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1649 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1650 (flyspell-large-region beg end)
1651 (flyspell-small-region beg end)))))
1653 ;;*---------------------------------------------------------------------*/
1654 ;;* flyspell-buffer ... */
1655 ;;*---------------------------------------------------------------------*/
1657 (defun flyspell-buffer ()
1658 "Flyspell whole buffer."
1660 (flyspell-region (point-min) (point-max)))
1662 ;;*---------------------------------------------------------------------*/
1663 ;;* old next error position ... */
1664 ;;*---------------------------------------------------------------------*/
1665 (defvar flyspell-old-buffer-error nil)
1666 (defvar flyspell-old-pos-error nil)
1668 ;;*---------------------------------------------------------------------*/
1669 ;;* flyspell-goto-next-error ... */
1670 ;;*---------------------------------------------------------------------*/
1671 (defun flyspell-goto-next-error ()
1672 "Go to the next previously detected error.
1673 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1678 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1679 (eq pos flyspell-old-pos-error))
1681 (if (= flyspell-old-pos-error max)
1682 ;; goto beginning of buffer
1684 (message "Restarting from beginning of buffer")
1685 (goto-char (point-min)))
1687 (setq pos (point))))
1688 ;; seek the next error
1689 (while (and (< pos max)
1690 (let ((ovs (overlays-at pos))
1692 (while (and (not r) (consp ovs))
1693 (if (flyspell-overlay-p (car ovs))
1695 (setq ovs (cdr ovs))))
1697 (setq pos (1+ pos)))
1698 ;; save the current location for next invocation
1699 (setq flyspell-old-pos-error pos)
1700 (setq flyspell-old-buffer-error (current-buffer))
1703 (message "No more miss-spelled word!"))))
1705 ;;*---------------------------------------------------------------------*/
1706 ;;* flyspell-overlay-p ... */
1707 ;;*---------------------------------------------------------------------*/
1708 (defun flyspell-overlay-p (o)
1709 "Return true if O is an overlay used by flyspell."
1710 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1712 ;;*---------------------------------------------------------------------*/
1713 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1714 ;;* ------------------------------------------------------------- */
1715 ;;* Remove overlays introduced by flyspell. */
1716 ;;*---------------------------------------------------------------------*/
1717 (defun flyspell-delete-region-overlays (beg end)
1718 "Delete overlays used by flyspell in a given region."
1719 (if (featurep 'emacs)
1720 (remove-overlays beg end 'flyspell-overlay t)
1721 ;; XEmacs does not have `remove-overlays'
1722 (let ((l (overlays-in beg end)))
1725 (if (flyspell-overlay-p (car l))
1726 (delete-overlay (car l)))
1727 (setq l (cdr l)))))))
1729 (defun flyspell-delete-all-overlays ()
1730 "Delete all the overlays used by flyspell."
1731 (flyspell-delete-region-overlays (point-min) (point-max)))
1733 ;;*---------------------------------------------------------------------*/
1734 ;;* flyspell-unhighlight-at ... */
1735 ;;*---------------------------------------------------------------------*/
1736 (defun flyspell-unhighlight-at (pos)
1737 "Remove the flyspell overlay that are located at POS."
1738 (if flyspell-persistent-highlight
1739 (let ((overlays (overlays-at pos)))
1740 (while (consp overlays)
1741 (if (flyspell-overlay-p (car overlays))
1742 (delete-overlay (car overlays)))
1743 (setq overlays (cdr overlays))))
1744 (if (flyspell-overlay-p flyspell-overlay)
1745 (delete-overlay flyspell-overlay))))
1747 ;;*---------------------------------------------------------------------*/
1748 ;;* flyspell-properties-at-p ... */
1749 ;;* ------------------------------------------------------------- */
1750 ;;* Is there an highlight properties at position pos? */
1751 ;;*---------------------------------------------------------------------*/
1752 (defun flyspell-properties-at-p (pos)
1753 "Return t if there is a text property at POS, not counting `local-map'.
1754 If variable `flyspell-highlight-properties' is set to nil,
1755 text with properties are not checked. This function is used to discover
1756 if the character at POS has any other property."
1757 (let ((prop (text-properties-at pos))
1759 (while (and keep (consp prop))
1760 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1761 (setq prop (cdr (cdr prop)))
1765 ;;*---------------------------------------------------------------------*/
1766 ;;* make-flyspell-overlay ... */
1767 ;;*---------------------------------------------------------------------*/
1768 (defun make-flyspell-overlay (beg end face mouse-face)
1769 "Allocate an overlay to highlight an incorrect word.
1770 BEG and END specify the range in the buffer of that word.
1771 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1773 (let ((overlay (make-overlay beg end nil t nil)))
1774 (overlay-put overlay 'face face)
1775 (overlay-put overlay 'mouse-face mouse-face)
1776 (overlay-put overlay 'flyspell-overlay t)
1777 (overlay-put overlay 'evaporate t)
1778 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1779 (overlay-put overlay 'keymap flyspell-mouse-map)
1780 (when (eq face 'flyspell-incorrect)
1781 (and (stringp flyspell-before-incorrect-word-string)
1782 (overlay-put overlay 'before-string
1783 flyspell-before-incorrect-word-string))
1784 (and (stringp flyspell-after-incorrect-word-string)
1785 (overlay-put overlay 'after-string
1786 flyspell-after-incorrect-word-string)))
1789 ;;*---------------------------------------------------------------------*/
1790 ;;* flyspell-highlight-incorrect-region ... */
1791 ;;*---------------------------------------------------------------------*/
1792 (defun flyspell-highlight-incorrect-region (beg end poss)
1793 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1794 POSS is usually a list of possible spelling/correction lists,
1795 as returned by `ispell-parse-output'.
1796 It can also be the symbol `doublon', in the case where the word
1797 is itself incorrect, but suspiciously repeated."
1798 (let ((inhibit-read-only t))
1799 (unless (run-hook-with-args-until-success
1800 'flyspell-incorrect-hook beg end poss)
1801 (if (or flyspell-highlight-properties
1802 (not (flyspell-properties-at-p beg)))
1804 ;; we cleanup all the overlay that are in the region, not
1805 ;; beginning at the word start position
1806 (if (< (1+ beg) end)
1807 (let ((os (overlays-in (1+ beg) end)))
1809 (if (flyspell-overlay-p (car os))
1810 (delete-overlay (car os)))
1811 (setq os (cdr os)))))
1812 ;; we cleanup current overlay at the same position
1813 (flyspell-unhighlight-at beg)
1814 ;; now we can use a new overlay
1815 (setq flyspell-overlay
1816 (make-flyspell-overlay
1818 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1821 ;;*---------------------------------------------------------------------*/
1822 ;;* flyspell-highlight-duplicate-region ... */
1823 ;;*---------------------------------------------------------------------*/
1824 (defun flyspell-highlight-duplicate-region (beg end poss)
1825 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1826 POSS is a list of possible spelling/correction lists,
1827 as returned by `ispell-parse-output'."
1828 (let ((inhibit-read-only t))
1829 (unless (run-hook-with-args-until-success
1830 'flyspell-incorrect-hook beg end poss)
1831 (if (or flyspell-highlight-properties
1832 (not (flyspell-properties-at-p beg)))
1834 ;; we cleanup current overlay at the same position
1835 (flyspell-unhighlight-at beg)
1836 ;; now we can use a new overlay
1837 (setq flyspell-overlay
1838 (make-flyspell-overlay beg end
1842 ;;*---------------------------------------------------------------------*/
1843 ;;* flyspell-auto-correct-cache ... */
1844 ;;*---------------------------------------------------------------------*/
1845 (defvar flyspell-auto-correct-pos nil)
1846 (defvar flyspell-auto-correct-region nil)
1847 (defvar flyspell-auto-correct-ring nil)
1848 (defvar flyspell-auto-correct-word nil)
1849 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1850 (make-variable-buffer-local 'flyspell-auto-correct-region)
1851 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1852 (make-variable-buffer-local 'flyspell-auto-correct-word)
1854 ;;*---------------------------------------------------------------------*/
1855 ;;* flyspell-check-previous-highlighted-word ... */
1856 ;;*---------------------------------------------------------------------*/
1857 (defun flyspell-check-previous-highlighted-word (&optional arg)
1858 "Correct the closest previous word that is highlighted as misspelled.
1859 This function scans for a word which starts before point that has been
1860 highlighted by Flyspell as misspelled. If it finds one, it proposes
1861 a replacement for that word. With prefix arg N, check the Nth word
1862 before point that's highlighted as misspelled."
1864 (let ((pos1 (point))
1866 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1869 (while (and (setq pos (previous-overlay-change pos))
1872 (if (> pos (point-min))
1874 (setq ovs (overlays-at pos))
1877 (setq ovs (cdr ovs))
1878 (if (and (flyspell-overlay-p ov)
1879 (= 0 (setq arg (1- arg))))
1880 (throw 'exit t)))))))
1884 (setq flyspell-word-cache-word nil) ;; Force flyspell-word re-check
1886 (error "No word to correct before point"))))
1888 ;;*---------------------------------------------------------------------*/
1889 ;;* flyspell-display-next-corrections ... */
1890 ;;*---------------------------------------------------------------------*/
1891 (defun flyspell-display-next-corrections (corrections)
1892 (let ((string "Corrections:")
1895 (while (< (length string) 80)
1896 (if (equal (car l) flyspell-auto-correct-word)
1897 (setq pos (cons (+ 1 (length string)) pos)))
1898 (setq string (concat string " " (car l)))
1901 (let ((num (car pos)))
1902 (put-text-property num
1903 (+ num (length flyspell-auto-correct-word))
1904 'face 'flyspell-incorrect
1906 (setq pos (cdr pos)))
1907 (if (fboundp 'display-message)
1908 (display-message 'no-log string)
1909 (message "%s" string))))
1911 ;;*---------------------------------------------------------------------*/
1912 ;;* flyspell-abbrev-table ... */
1913 ;;*---------------------------------------------------------------------*/
1914 (defun flyspell-abbrev-table ()
1915 (if flyspell-use-global-abbrev-table-p
1917 (or local-abbrev-table global-abbrev-table)))
1919 ;;*---------------------------------------------------------------------*/
1920 ;;* flyspell-define-abbrev ... */
1921 ;;*---------------------------------------------------------------------*/
1922 (defun flyspell-define-abbrev (name expansion)
1923 (let ((table (flyspell-abbrev-table)))
1925 (define-abbrev table (downcase name) expansion))))
1927 ;;*---------------------------------------------------------------------*/
1928 ;;* flyspell-auto-correct-word ... */
1929 ;;*---------------------------------------------------------------------*/
1930 (defun flyspell-auto-correct-word ()
1931 "Correct the current word.
1932 This command proposes various successive corrections for the current word."
1934 ;; If we are not in the construct where flyspell should be active,
1935 ;; invoke the original binding of M-TAB, if that was recorded.
1936 (if (and (local-variable-p 'flyspell--prev-meta-tab-binding)
1937 (commandp flyspell--prev-meta-tab-binding t)
1938 (fboundp flyspell-generic-check-word-predicate)
1939 (not (funcall flyspell-generic-check-word-predicate))
1940 (equal (where-is-internal 'flyspell-auto-correct-word nil t)
1942 (call-interactively flyspell--prev-meta-tab-binding)
1944 (old-max (point-max)))
1945 ;; Use the correct dictionary.
1946 (flyspell-accept-buffer-local-defs)
1947 (if (and (eq flyspell-auto-correct-pos pos)
1948 (consp flyspell-auto-correct-region))
1949 ;; We have already been using the function at the same location.
1950 (let* ((start (car flyspell-auto-correct-region))
1951 (len (cdr flyspell-auto-correct-region)))
1952 (flyspell-unhighlight-at start)
1953 (delete-region start (+ start len))
1954 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1955 (let* ((word (car flyspell-auto-correct-ring))
1956 (len (length word)))
1957 (rplacd flyspell-auto-correct-region len)
1959 (if flyspell-abbrev-p
1960 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1961 flyspell-auto-correct-word)
1962 (flyspell-change-abbrev (flyspell-abbrev-table)
1963 flyspell-auto-correct-word
1965 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1966 (funcall flyspell-insert-function word)
1968 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1969 (flyspell-ajust-cursor-point pos (point) old-max)
1970 (setq flyspell-auto-correct-pos (point)))
1971 ;; Fetch the word to be checked.
1972 (let ((word (flyspell-get-word)))
1974 (let ((start (car (cdr word)))
1975 (end (car (cdr (cdr word))))
1978 (setq flyspell-auto-correct-word word)
1979 ;; Now check spelling of word..
1980 (ispell-send-string "%\n") ;Put in verbose mode.
1981 (ispell-send-string (concat "^" word "\n"))
1982 ;; Wait until ispell has processed word.
1984 (accept-process-output ispell-process)
1985 (not (string= "" (car ispell-filter)))))
1986 ;; Remove leading empty element.
1987 (setq ispell-filter (cdr ispell-filter))
1988 ;; Ispell process should return something after word is sent.
1989 ;; Tag word as valid (i.e., skip) otherwise.
1991 (setq ispell-filter '(*)))
1992 (if (consp ispell-filter)
1993 (setq poss (ispell-parse-output (car ispell-filter))))
1995 ((or (eq poss t) (stringp poss))
1996 ;; Don't correct word.
2000 (error "Ispell: error in Ispell process"))
2002 ;; The word is incorrect, we have to propose a replacement.
2003 (let ((replacements (flyspell-sort (car (cdr (cdr poss)))
2005 (setq flyspell-auto-correct-region nil)
2006 (if (consp replacements)
2008 (let ((replace (car replacements)))
2009 (let ((new-word replace))
2010 (if (not (equal new-word (car poss)))
2012 ;; the save the current replacements
2013 (setq flyspell-auto-correct-region
2014 (cons start (length new-word)))
2015 (let ((l replacements))
2016 (while (consp (cdr l))
2018 (rplacd l (cons (car poss) replacements)))
2019 (setq flyspell-auto-correct-ring
2021 (flyspell-unhighlight-at start)
2022 (delete-region start end)
2023 (funcall flyspell-insert-function new-word)
2024 (if flyspell-abbrev-p
2025 (if (flyspell-already-abbrevp
2026 (flyspell-abbrev-table) word)
2027 (flyspell-change-abbrev
2028 (flyspell-abbrev-table)
2031 (flyspell-define-abbrev word
2034 (flyspell-display-next-corrections
2035 (cons new-word flyspell-auto-correct-ring))
2036 (flyspell-ajust-cursor-point pos
2039 (setq flyspell-auto-correct-pos (point))
2040 (ispell-pdict-save t))))))))
2042 ;;*---------------------------------------------------------------------*/
2043 ;;* flyspell-auto-correct-previous-pos ... */
2044 ;;*---------------------------------------------------------------------*/
2045 (defvar flyspell-auto-correct-previous-pos nil
2046 "Holds the start of the first incorrect word before point.")
2048 ;;*---------------------------------------------------------------------*/
2049 ;;* flyspell-auto-correct-previous-hook ... */
2050 ;;*---------------------------------------------------------------------*/
2051 (defun flyspell-auto-correct-previous-hook ()
2052 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
2053 Sets `flyspell-auto-correct-previous-pos' to nil"
2055 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
2056 (unless (eq this-command (function flyspell-auto-correct-previous-word))
2057 (setq flyspell-auto-correct-previous-pos nil)))
2059 ;;*---------------------------------------------------------------------*/
2060 ;;* flyspell-auto-correct-previous-word ... */
2061 ;;*---------------------------------------------------------------------*/
2062 (defun flyspell-auto-correct-previous-word (position)
2063 "Auto correct the first misspelled word that occurs before point.
2064 But don't look beyond what's visible on the screen."
2067 (let ((top (window-start))
2071 (narrow-to-region top bot)
2072 (overlay-recenter (point))
2074 (add-hook 'pre-command-hook
2075 (function flyspell-auto-correct-previous-hook) t t)
2077 (unless flyspell-auto-correct-previous-pos
2078 ;; only reset if a new overlay exists
2079 (setq flyspell-auto-correct-previous-pos nil)
2081 (let ((overlay-list (overlays-in (point-min) position))
2082 (new-overlay 'dummy-value))
2084 ;; search for previous (new) flyspell overlay
2085 (while (and new-overlay
2086 (or (not (flyspell-overlay-p new-overlay))
2087 ;; check if its face has changed
2088 (not (eq (get-char-property
2089 (overlay-start new-overlay) 'face)
2090 'flyspell-incorrect))))
2091 (setq new-overlay (car-safe overlay-list))
2092 (setq overlay-list (cdr-safe overlay-list)))
2094 ;; if nothing new exits new-overlay should be nil
2095 (if new-overlay ;; the length of the word may change so go to the start
2096 (setq flyspell-auto-correct-previous-pos
2097 (overlay-start new-overlay)))))
2099 (when flyspell-auto-correct-previous-pos
2101 (goto-char flyspell-auto-correct-previous-pos)
2102 (let ((ispell-following-word t)) ;; point is at start
2103 (if (numberp flyspell-auto-correct-previous-pos)
2104 (goto-char flyspell-auto-correct-previous-pos))
2105 (flyspell-auto-correct-word))
2106 ;; the point may have moved so reset this
2107 (setq flyspell-auto-correct-previous-pos (point))))))))
2109 ;;*---------------------------------------------------------------------*/
2110 ;;* flyspell-correct-word ... */
2111 ;;*---------------------------------------------------------------------*/
2113 (defun flyspell-correct-word (event)
2114 "Pop up a menu of possible corrections for a misspelled word.
2115 The word checked is the word at the mouse position."
2117 (let ((save (point)))
2118 (mouse-set-point event)
2119 (flyspell-correct-word-before-point event save)))
2121 (defun flyspell-correct-word-before-point (&optional event opoint)
2122 "Pop up a menu of possible corrections for misspelled word before point.
2123 If EVENT is non-nil, it is the mouse event that invoked this operation;
2124 that controls where to put the menu.
2125 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2127 ;; use the correct dictionary
2128 (flyspell-accept-buffer-local-defs)
2129 (or opoint (setq opoint (point)))
2130 (let ((cursor-location (point))
2131 (word (flyspell-get-word)))
2133 (let ((start (car (cdr word)))
2134 (end (car (cdr (cdr word))))
2137 ;; now check spelling of word.
2138 (ispell-send-string "%\n") ;put in verbose mode
2139 (ispell-send-string (concat "^" word "\n"))
2140 ;; wait until ispell has processed word
2142 (accept-process-output ispell-process)
2143 (not (string= "" (car ispell-filter)))))
2144 ;; Remove leading empty element
2145 (setq ispell-filter (cdr ispell-filter))
2146 ;; ispell process should return something after word is sent.
2147 ;; Tag word as valid (i.e., skip) otherwise
2149 (setq ispell-filter '(*)))
2150 (if (consp ispell-filter)
2151 (setq poss (ispell-parse-output (car ispell-filter))))
2153 ((or (eq poss t) (stringp poss))
2154 ;; don't correct word
2158 (error "Ispell: error in Ispell process"))
2160 (flyspell-xemacs-popup
2161 poss word cursor-location start end opoint))
2163 ;; The word is incorrect, we have to propose a replacement.
2164 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2165 poss word cursor-location start end opoint)))
2166 (ispell-pdict-save t)))))
2168 ;;*---------------------------------------------------------------------*/
2169 ;;* flyspell-do-correct ... */
2170 ;;*---------------------------------------------------------------------*/
2171 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2172 "The popup menu callback."
2173 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2174 ;; it instead right after calling the function.
2175 (cond ((eq replace 'ignore)
2180 (ispell-send-string (concat "*" word "\n"))
2181 ;; This was added only to the XEmacs side in revision 1.18 of
2182 ;; flyspell. I assume its absence on the Emacs side was an
2183 ;; oversight. --Stef
2184 (ispell-send-string "#\n")
2185 (flyspell-unhighlight-at cursor-location)
2186 (setq ispell-pdict-modified-p '(t)))
2187 ((or (eq replace 'buffer) (eq replace 'session))
2188 (ispell-send-string (concat "@" word "\n"))
2189 (add-to-list 'ispell-buffer-session-localwords word)
2190 (or ispell-buffer-local-name ; session localwords might conflict
2191 (setq ispell-buffer-local-name (buffer-name)))
2192 (flyspell-unhighlight-at cursor-location)
2193 (if (null ispell-pdict-modified-p)
2194 (setq ispell-pdict-modified-p
2195 (list ispell-pdict-modified-p)))
2197 (if (eq replace 'buffer)
2198 (ispell-add-per-file-word-list word)))
2200 ;; This was added only to the Emacs side. I assume its absence on
2201 ;; the XEmacs side was an oversight. --Stef
2202 (flyspell-unhighlight-at cursor-location)
2203 (let ((old-max (point-max))
2204 (new-word (if (atom replace)
2207 (cursor-location (+ (- (length word) (- end start))
2209 (unless (equal new-word (car poss))
2210 (delete-region start end)
2212 (funcall flyspell-insert-function new-word)
2213 (if flyspell-abbrev-p
2214 (flyspell-define-abbrev word new-word)))
2215 ;; In the original Emacs code, this was only called in the body
2216 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2217 (flyspell-ajust-cursor-point save cursor-location old-max)))
2222 ;;*---------------------------------------------------------------------*/
2223 ;;* flyspell-ajust-cursor-point ... */
2224 ;;*---------------------------------------------------------------------*/
2225 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2226 (if (>= save cursor-location)
2227 (let ((new-pos (+ save (- (point-max) old-max))))
2229 ((< new-pos (point-min))
2231 ((> new-pos (point-max))
2236 ;;*---------------------------------------------------------------------*/
2237 ;;* flyspell-emacs-popup ... */
2238 ;;*---------------------------------------------------------------------*/
2239 (defun flyspell-emacs-popup (event poss word)
2240 "The Emacs popup menu."
2241 (if (and (not event)
2243 (let* ((mouse-pos (mouse-position))
2244 (mouse-pos (if (nth 1 mouse-pos)
2246 (set-mouse-position (car mouse-pos)
2247 (/ (frame-width) 2) 2)
2249 (setq event (list (list (car (cdr mouse-pos))
2250 (1+ (cdr (cdr mouse-pos))))
2252 (let* ((corrects (flyspell-sort (car (cdr (cdr poss))) word))
2253 (cor-menu (if (consp corrects)
2254 (mapcar (lambda (correct)
2255 (list correct correct))
2258 (affix (car (cdr (cdr (cdr poss)))))
2260 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2262 (list (concat "Save affix: " (car affix))
2264 '("Accept (session)" session)
2265 '("Accept (buffer)" buffer))
2266 '(("Save word" save)
2267 ("Accept (session)" session)
2268 ("Accept (buffer)" buffer)))))
2269 (if (consp cor-menu)
2270 (append cor-menu (cons "" save))
2272 (menu (cons "flyspell correction menu" base-menu)))
2273 (car (x-popup-menu event
2274 (list (format "%s [%s]" word (or ispell-local-dictionary
2278 ;;*---------------------------------------------------------------------*/
2279 ;;* flyspell-xemacs-popup ... */
2280 ;;*---------------------------------------------------------------------*/
2281 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2282 "The XEmacs popup menu."
2283 (let* ((corrects (flyspell-sort (car (cdr (cdr poss))) word))
2284 (cor-menu (if (consp corrects)
2285 (mapcar (lambda (correct)
2287 (list 'flyspell-do-correct
2298 (affix (car (cdr (cdr (cdr poss)))))
2300 (menu (let ((save (if (and (consp affix) show-affix-info)
2302 (concat "Save affix: " (car affix))
2303 (list 'flyspell-do-correct
2314 (list 'flyspell-do-correct
2323 (session (vector "Accept (session)"
2324 (list 'flyspell-do-correct
2333 (buffer (vector "Accept (buffer)"
2334 (list 'flyspell-do-correct
2343 (if (consp cor-menu)
2344 (append cor-menu (list "-" save session buffer))
2345 (list save session buffer)))))
2346 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2350 ;;*---------------------------------------------------------------------*/
2351 ;;* Some example functions for real autocorrecting */
2352 ;;*---------------------------------------------------------------------*/
2353 (defun flyspell-maybe-correct-transposition (beg end poss)
2354 "Check replacements for transposed characters.
2356 If the text between BEG and END is equal to a correction suggested by
2357 Ispell, after transposing two adjacent characters, correct the text,
2360 The third arg POSS is either the symbol `doublon' or a list of
2361 possible corrections as returned by `ispell-parse-output'.
2363 This function is meant to be added to `flyspell-incorrect-hook'."
2366 (let ((str (buffer-substring beg end))
2367 (i 0) (len (- end beg)) tmp)
2368 (while (< (1+ i) len)
2369 (setq tmp (aref str i))
2370 (aset str i (aref str (1+ i)))
2371 (aset str (1+ i) tmp)
2372 (when (member str (nth 2 poss))
2374 (goto-char (+ beg i 1))
2375 (transpose-chars 1))
2377 (setq tmp (aref str i))
2378 (aset str i (aref str (1+ i)))
2379 (aset str (1+ i) tmp)
2383 (defun flyspell-maybe-correct-doubling (beg end poss)
2384 "Check replacements for doubled characters.
2386 If the text between BEG and END is equal to a correction suggested by
2387 Ispell, after removing a pair of doubled characters, correct the text,
2390 The third arg POSS is either the symbol `doublon' or a list of
2391 possible corrections as returned by `ispell-parse-output'.
2393 This function is meant to be added to `flyspell-incorrect-hook'."
2396 (let ((str (buffer-substring beg end))
2397 (i 0) (len (- end beg)))
2398 (while (< (1+ i) len)
2399 (when (and (= (aref str i) (aref str (1+ i)))
2400 (member (concat (substring str 0 (1+ i))
2401 (substring str (+ i 2)))
2403 (goto-char (+ beg i))
2409 ;;*---------------------------------------------------------------------*/
2410 ;;* flyspell-already-abbrevp ... */
2411 ;;*---------------------------------------------------------------------*/
2412 (defun flyspell-already-abbrevp (table word)
2413 (let ((sym (abbrev-symbol word table)))
2414 (and sym (symbolp sym))))
2416 ;;*---------------------------------------------------------------------*/
2417 ;;* flyspell-change-abbrev ... */
2418 ;;*---------------------------------------------------------------------*/
2419 (defun flyspell-change-abbrev (table old new)
2420 (set (abbrev-symbol old table) new))
2424 ;;; flyspell.el ends here