1 ;;; flyspell.el --- On-the-fly spell checker -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998, 2000-2018 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 <https://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 `https://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 (define-key map
[mouse-2
] 'flyspell-correct-word
)
453 "Keymap for Flyspell to put on erroneous words.")
455 (defvar flyspell-mode-map
456 (let ((map (make-sparse-keymap)))
457 (if flyspell-use-meta-tab
458 (define-key map
"\M-\t" 'flyspell-auto-correct-word
))
459 (define-key map flyspell-auto-correct-binding
'flyspell-auto-correct-previous-word
)
460 (define-key map
[(control ?\
,)] 'flyspell-goto-next-error
)
461 (define-key map
[(control ?\.
)] 'flyspell-auto-correct-word
)
462 (define-key map
[?\C-c ?$
] 'flyspell-correct-word-before-point
)
464 "Minor mode keymap for Flyspell mode--for the whole buffer.")
466 ;; dash character machinery
467 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
468 "Non-nil means that the `-' char is considered as a word delimiter.")
469 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag
)
470 (defvar flyspell-dash-dictionary nil
)
471 (make-variable-buffer-local 'flyspell-dash-dictionary
)
472 (defvar flyspell-dash-local-dictionary nil
)
473 (make-variable-buffer-local 'flyspell-dash-local-dictionary
)
475 ;;*---------------------------------------------------------------------*/
477 ;;*---------------------------------------------------------------------*/
478 (defface flyspell-incorrect
479 '((((supports :underline
(:style wave
)))
480 :underline
(:style wave
:color
"Red1"))
482 :underline t
:inherit error
))
483 "Flyspell face for misspelled words."
487 (defface flyspell-duplicate
488 '((((supports :underline
(:style wave
)))
489 :underline
(:style wave
:color
"DarkOrange"))
491 :underline t
:inherit warning
))
492 "Flyspell face for words that appear twice in a row.
493 See also `flyspell-duplicate-distance'."
497 (defvar flyspell-overlay nil
)
499 ;;*---------------------------------------------------------------------*/
500 ;;* flyspell-mode ... */
501 ;;*---------------------------------------------------------------------*/
502 ;;;###autoload(defvar flyspell-mode nil "Non-nil if Flyspell mode is enabled.")
504 (define-minor-mode flyspell-mode
505 "Toggle on-the-fly spell checking (Flyspell mode).
506 With a prefix argument ARG, enable Flyspell mode if ARG is
507 positive, and disable it otherwise. If called from Lisp, enable
508 the mode if ARG is omitted or nil.
510 Flyspell mode is a buffer-local minor mode. When enabled, it
511 spawns a single Ispell process and checks each word. The default
512 flyspell behavior is to highlight incorrect words.
515 \\[ispell-word]: correct words (using Ispell).
516 \\[flyspell-auto-correct-word]: automatically correct word.
517 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
518 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
521 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
524 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
525 valid. For instance, a different dictionary can be used by
526 invoking `ispell-change-dictionary'.
528 Consider using the `ispell-parser' to check your text. For instance
530 \(add-hook \\='tex-mode-hook (function (lambda () (setq ispell-parser \\='tex))))
533 \\[flyspell-region] checks all words inside a region.
534 \\[flyspell-buffer] checks the whole buffer."
535 :lighter flyspell-mode-line-string
536 :keymap flyspell-mode-map
541 (error (message "Error enabling Flyspell mode:\n%s" (cdr err
))
543 (flyspell-mode-off)))
546 (defun turn-on-flyspell ()
547 "Unconditionally turn on Flyspell mode."
551 (defun turn-off-flyspell ()
552 "Unconditionally turn off Flyspell mode."
555 (custom-add-option 'text-mode-hook
'turn-on-flyspell
)
557 ;;*---------------------------------------------------------------------*/
558 ;;* flyspell-buffers ... */
559 ;;* ------------------------------------------------------------- */
560 ;;* For remembering buffers running flyspell */
561 ;;*---------------------------------------------------------------------*/
562 (defvar flyspell-buffers nil
)
564 ;;*---------------------------------------------------------------------*/
565 ;;* flyspell-minibuffer-p ... */
566 ;;*---------------------------------------------------------------------*/
567 (defun flyspell-minibuffer-p (buffer)
568 "Is BUFFER a minibuffer?"
569 (let ((ws (get-buffer-window-list buffer t
)))
570 (and (consp ws
) (window-minibuffer-p (car ws
)))))
572 ;;*---------------------------------------------------------------------*/
573 ;;* flyspell-accept-buffer-local-defs ... */
574 ;;*---------------------------------------------------------------------*/
575 (defvar flyspell-last-buffer nil
576 "The buffer in which the last flyspell operation took place.")
578 (defun flyspell-accept-buffer-local-defs (&optional force
)
579 ;; When flyspell-word is used inside a loop (e.g. when processing
580 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
581 ;; up dwarfing everything else, so only do it when the buffer has changed.
582 (when (or force
(not (eq flyspell-last-buffer
(current-buffer))))
583 (setq flyspell-last-buffer
(current-buffer))
584 ;; Strange problem: If buffer in current window has font-lock turned on,
585 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
586 ;; call will reset the buffer to the buffer in the current window.
587 ;; However, it only happens at startup (fix by Albert L. Ting).
589 (ispell-accept-buffer-local-defs))
590 (unless (and (eq flyspell-dash-dictionary ispell-dictionary
)
591 (eq flyspell-dash-local-dictionary ispell-local-dictionary
))
592 ;; The dictionary has changed
593 (setq flyspell-dash-dictionary ispell-dictionary
)
594 (setq flyspell-dash-local-dictionary ispell-local-dictionary
)
595 (setq flyspell-consider-dash-as-word-delimiter-flag
596 (member (or ispell-local-dictionary ispell-dictionary
)
597 flyspell-dictionaries-that-consider-dash-as-word-delimiter
)))))
599 (defun flyspell-hack-local-variables-hook ()
600 ;; When local variables are loaded, see if the dictionary context
602 (flyspell-accept-buffer-local-defs 'force
))
604 (defun flyspell-kill-ispell-hook ()
605 (setq flyspell-last-buffer nil
)
606 (dolist (buf (buffer-list))
607 (with-current-buffer buf
608 (kill-local-variable 'flyspell-word-cache-word
))))
610 ;; Make sure we flush our caches when needed. Do it here rather than in
611 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
613 (add-hook 'ispell-kill-ispell-hook
'flyspell-kill-ispell-hook
)
615 ;;*---------------------------------------------------------------------*/
616 ;;* flyspell-mode-on ... */
617 ;;*---------------------------------------------------------------------*/
618 (defun flyspell-mode-on ()
619 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
620 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
621 (setq ispell-highlight-face
'flyspell-incorrect
)
622 ;; local dictionaries setup
623 (or ispell-local-dictionary ispell-dictionary
624 (if flyspell-default-dictionary
625 (ispell-change-dictionary flyspell-default-dictionary
)))
626 ;; we have to force ispell to accept the local definition or
627 ;; otherwise it could be too late, the local dictionary may
629 ;; Pass the `force' argument for the case where flyspell was active already
630 ;; but the buffer's local-defs have been edited.
631 (flyspell-accept-buffer-local-defs 'force
)
632 ;; we put the `flyspell-delayed' property on some commands
633 (flyspell-delay-commands)
634 ;; we put the `flyspell-deplacement' property on some commands
635 (flyspell-deplacement-commands)
636 ;; we bound flyspell action to post-command hook
637 (add-hook 'post-command-hook
(function flyspell-post-command-hook
) t t
)
638 ;; we bound flyspell action to pre-command hook
639 (add-hook 'pre-command-hook
(function flyspell-pre-command-hook
) t t
)
640 ;; we bound flyspell action to after-change hook
641 (add-hook 'after-change-functions
'flyspell-after-change-function nil t
)
642 ;; we bound flyspell action to hack-local-variables-hook
643 (add-hook 'hack-local-variables-hook
644 (function flyspell-hack-local-variables-hook
) t t
)
645 ;; set flyspell-generic-check-word-predicate based on the major mode
646 (let ((mode-predicate (get major-mode
'flyspell-mode-predicate
)))
648 (setq flyspell-generic-check-word-predicate mode-predicate
)))
649 ;; the welcome message
650 (if (and flyspell-issue-message-flag
651 flyspell-issue-welcome-flag
652 (called-interactively-p 'interactive
))
653 (let ((binding (where-is-internal 'flyspell-auto-correct-word
657 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
658 (key-description binding
))
659 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
661 ;;*---------------------------------------------------------------------*/
662 ;;* flyspell-delay-commands ... */
663 ;;*---------------------------------------------------------------------*/
664 (defun flyspell-delay-commands ()
665 "Install the standard set of Flyspell delayed commands."
666 (mapc 'flyspell-delay-command flyspell-default-delayed-commands
)
667 (mapc 'flyspell-delay-command flyspell-delayed-commands
))
669 ;;*---------------------------------------------------------------------*/
670 ;;* flyspell-delay-command ... */
671 ;;*---------------------------------------------------------------------*/
672 (defun flyspell-delay-command (command)
673 "Set COMMAND to be delayed, for Flyspell.
674 When flyspell `post-command-hook' is invoked because a delayed command
675 has been used, the current word is not immediately checked.
676 It will be checked only after `flyspell-delay' seconds."
677 (interactive "SDelay Flyspell after Command: ")
678 (put command
'flyspell-delayed t
))
680 ;;*---------------------------------------------------------------------*/
681 ;;* flyspell-deplacement-commands ... */
682 ;;*---------------------------------------------------------------------*/
683 (defun flyspell-deplacement-commands ()
684 "Install the standard set of Flyspell deplacement commands."
685 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands
)
686 (mapc 'flyspell-deplacement-command flyspell-deplacement-commands
))
688 ;;*---------------------------------------------------------------------*/
689 ;;* flyspell-deplacement-command ... */
690 ;;*---------------------------------------------------------------------*/
691 (defun flyspell-deplacement-command (command)
692 "Set COMMAND that implement cursor movements, for Flyspell.
693 When flyspell `post-command-hook' is invoked because a deplacement command
694 has been used, the current word is not checked."
695 (interactive "SDeplacement Flyspell after Command: ")
696 (put command
'flyspell-deplacement t
))
698 ;;*---------------------------------------------------------------------*/
699 ;;* flyspell-word-cache ... */
700 ;;*---------------------------------------------------------------------*/
701 (defvar flyspell-word-cache-start nil
)
702 (defvar flyspell-word-cache-end nil
)
703 (defvar flyspell-word-cache-word nil
)
704 (defvar flyspell-word-cache-result
'_
)
705 (make-variable-buffer-local 'flyspell-word-cache-start
)
706 (make-variable-buffer-local 'flyspell-word-cache-end
)
707 (make-variable-buffer-local 'flyspell-word-cache-word
)
708 (make-variable-buffer-local 'flyspell-word-cache-result
)
710 ;;*---------------------------------------------------------------------*/
711 ;;* The flyspell pre-hook, store the current position. In the */
712 ;;* post command hook, we will check, if the word at this position */
713 ;;* has to be spell checked. */
714 ;;*---------------------------------------------------------------------*/
715 (defvar flyspell-pre-buffer nil
"Buffer current before `this-command'.")
716 (defvar flyspell-pre-point nil
"Point before running `this-command'")
717 (defvar flyspell-pre-column nil
"Column before running `this-command'")
718 (defvar flyspell-pre-pre-buffer nil
)
719 (defvar flyspell-pre-pre-point nil
)
720 (make-variable-buffer-local 'flyspell-pre-point
) ;Why?? --Stef
722 ;;*---------------------------------------------------------------------*/
723 ;;* flyspell-previous-command ... */
724 ;;*---------------------------------------------------------------------*/
725 (defvar flyspell-previous-command nil
726 "The last interactive command checked by Flyspell.")
728 ;;*---------------------------------------------------------------------*/
729 ;;* flyspell-pre-command-hook ... */
730 ;;*---------------------------------------------------------------------*/
731 (defun flyspell-pre-command-hook ()
732 "Save the current buffer and point for Flyspell's post-command hook."
734 (setq flyspell-pre-buffer
(current-buffer))
735 (setq flyspell-pre-point
(point))
736 (setq flyspell-pre-column
(current-column)))
738 ;;*---------------------------------------------------------------------*/
739 ;;* flyspell-mode-off ... */
740 ;;*---------------------------------------------------------------------*/
742 (defun flyspell-mode-off ()
743 "Turn Flyspell mode off."
744 ;; We remove the hooks.
745 (remove-hook 'post-command-hook
(function flyspell-post-command-hook
) t
)
746 (remove-hook 'pre-command-hook
(function flyspell-pre-command-hook
) t
)
747 (remove-hook 'after-change-functions
'flyspell-after-change-function t
)
748 (remove-hook 'hack-local-variables-hook
749 (function flyspell-hack-local-variables-hook
) t
)
750 ;; We remove all the flyspell highlightings.
751 (flyspell-delete-all-overlays)
752 ;; We have to erase pre cache variables.
753 (setq flyspell-pre-buffer nil
)
754 (setq flyspell-pre-point nil
)
755 ;; We mark the mode as killed.
756 (setq flyspell-mode nil
))
758 ;;*---------------------------------------------------------------------*/
759 ;;* flyspell-check-pre-word-p ... */
760 ;;*---------------------------------------------------------------------*/
761 (defun flyspell-check-pre-word-p ()
762 "Return non-nil if we should check the word before point.
763 More precisely, it applies to the word that was before point
764 before the current command."
765 (let ((ispell-otherchars (ispell-get-otherchars)))
767 ((not (and (numberp flyspell-pre-point
)
768 (eq flyspell-pre-buffer
(current-buffer))))
770 ((and (eq flyspell-pre-pre-point flyspell-pre-point
)
771 (eq flyspell-pre-pre-buffer flyspell-pre-buffer
))
773 ((or (and (= flyspell-pre-point
(- (point) 1))
774 (or (eq (char-syntax (char-after flyspell-pre-point
)) ?w
)
775 (and (not (string= "" ispell-otherchars
))
778 (buffer-substring-no-properties
779 flyspell-pre-point
(1+ flyspell-pre-point
))))))
780 (= flyspell-pre-point
(point))
781 (= flyspell-pre-point
(+ (point) 1)))
783 ((and (symbolp this-command
)
784 (not executing-kbd-macro
)
785 (or (get this-command
'flyspell-delayed
)
786 (and (get this-command
'flyspell-deplacement
)
787 (eq flyspell-previous-command this-command
)))
788 (or (= (current-column) 0)
789 (= (current-column) flyspell-pre-column
)
790 ;; If other post-command-hooks change the buffer,
791 ;; flyspell-pre-point can lie past eob (bug#468).
792 (null (char-after flyspell-pre-point
))
793 (or (eq (char-syntax (char-after flyspell-pre-point
)) ?w
)
794 (and (not (string= "" ispell-otherchars
))
797 (buffer-substring-no-properties
798 flyspell-pre-point
(1+ flyspell-pre-point
)))))))
800 ((not (eq (current-buffer) flyspell-pre-buffer
))
802 ((not (and (numberp flyspell-word-cache-start
)
803 (numberp flyspell-word-cache-end
)))
806 (or (< flyspell-pre-point flyspell-word-cache-start
)
807 (> flyspell-pre-point flyspell-word-cache-end
))))))
809 ;;*---------------------------------------------------------------------*/
810 ;;* The flyspell after-change-hook, store the change position. In */
811 ;;* the post command hook, we will check, if the word at this */
812 ;;* position has to be spell checked. */
813 ;;*---------------------------------------------------------------------*/
814 (defvar flyspell-changes nil
)
815 (make-variable-buffer-local 'flyspell-changes
)
817 ;;*---------------------------------------------------------------------*/
818 ;;* flyspell-after-change-function ... */
819 ;;*---------------------------------------------------------------------*/
820 (defun flyspell-after-change-function (start stop _len
)
821 "Save the current buffer and point for Flyspell's post-command hook."
822 (push (cons start stop
) flyspell-changes
))
824 ;;*---------------------------------------------------------------------*/
825 ;;* flyspell-check-changed-word-p ... */
826 ;;*---------------------------------------------------------------------*/
827 (defun flyspell-check-changed-word-p (start stop
)
828 "Return non-nil when the changed word has to be checked.
829 The answer depends of several criteria.
830 Mostly we check word delimiters."
831 (not (and (not (and (memq (char-after start
) '(?
\n ?
)) (> stop start
)))
832 (numberp flyspell-pre-point
)
834 (and (>= flyspell-pre-point start
) (<= flyspell-pre-point stop
))
836 (or (>= pos start
) (<= pos stop
) (= pos
(1+ stop
))))))))
838 ;;*---------------------------------------------------------------------*/
839 ;;* flyspell-check-word-p ... */
840 ;;*---------------------------------------------------------------------*/
841 (defun flyspell-check-word-p ()
842 "Return t when the word at `point' has to be checked.
843 The answer depends of several criteria.
844 Mostly we check word delimiters."
845 (let ((ispell-otherchars (ispell-get-otherchars)))
847 ((<= (- (point-max) 1) (point-min))
848 ;; The buffer is not filled enough.
850 ((and (and (> (current-column) 0)
851 (not (eq (current-column) flyspell-pre-column
)))
854 (and (looking-at (flyspell-get-not-casechars))
855 (or (string= "" ispell-otherchars
)
856 (not (looking-at ispell-otherchars
)))
857 (or flyspell-consider-dash-as-word-delimiter-flag
858 (not (looking-at "-"))))))
859 ;; Yes because we have reached or typed a word delimiter.
861 ((symbolp this-command
)
863 ((get this-command
'flyspell-deplacement
)
864 (not (eq flyspell-previous-command this-command
)))
865 ((get this-command
'flyspell-delayed
)
866 ;; The current command is not delayed, that
867 ;; is that we must check the word now.
868 (and (not unread-command-events
)
869 (sit-for flyspell-delay
)))
873 ;;*---------------------------------------------------------------------*/
874 ;;* flyspell-debug-signal-no-check ... */
875 ;;*---------------------------------------------------------------------*/
876 (defun flyspell-debug-signal-no-check (msg obj
)
877 (setq debug-on-error t
)
878 (with-current-buffer (get-buffer-create "*flyspell-debug*")
880 (insert "NO-CHECK:\n")
881 (insert (format " %S : %S\n" msg obj
))))
883 ;;*---------------------------------------------------------------------*/
884 ;;* flyspell-debug-signal-pre-word-checked ... */
885 ;;*---------------------------------------------------------------------*/
886 (defun flyspell-debug-signal-pre-word-checked ()
887 (setq debug-on-error t
)
888 (with-current-buffer (get-buffer-create "*flyspell-debug*")
889 (insert "PRE-WORD:\n")
890 (insert (format " pre-point : %S\n" flyspell-pre-point
))
891 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer
))
892 (insert (format " cache-start: %S\n" flyspell-word-cache-start
))
893 (insert (format " cache-end : %S\n" flyspell-word-cache-end
))
894 (goto-char (point-max))))
896 ;;*---------------------------------------------------------------------*/
897 ;;* flyspell-debug-signal-word-checked ... */
898 ;;*---------------------------------------------------------------------*/
899 (defun flyspell-debug-signal-word-checked ()
900 (setq debug-on-error t
)
901 (let ((ispell-otherchars (ispell-get-otherchars))
902 (oldbuf (current-buffer))
904 (with-current-buffer (get-buffer-create "*flyspell-debug*")
907 (format " this-cmd : %S\n" this-command
)
908 (format " delayed : %S\n" (and (symbolp this-command
)
911 (format " point : %S\n" point
)
912 (format " prev-char : [%c] %S\n"
913 (with-current-buffer oldbuf
914 (if (bobp) ?\
(char-before)))
915 (with-current-buffer oldbuf
920 (and (looking-at (flyspell-get-not-casechars))
921 (or (string= "" ispell-otherchars
)
922 (not (looking-at ispell-otherchars
)))
923 (or flyspell-consider-dash-as-word-delimiter-flag
924 (not (looking-at "\\-")))
926 (format " because : %S\n"
928 ((not (and (symbolp this-command
)
929 (get this-command
'flyspell-delayed
)))
930 ;; The current command is not delayed, that
931 ;; is that we must check the word now.
933 ((with-current-buffer oldbuf
938 (and (looking-at (flyspell-get-not-casechars))
939 (or (string= "" ispell-otherchars
)
940 (not (looking-at ispell-otherchars
)))
941 (or flyspell-consider-dash-as-word-delimiter-flag
942 (not (looking-at "\\-")))))))
943 ;; Yes because we have reached or typed a word delimiter.
945 ((not (integerp flyspell-delay
))
946 ;; Yes because the user set up a no-delay configuration.
950 (goto-char (point-max)))))
952 ;;*---------------------------------------------------------------------*/
953 ;;* flyspell-debug-signal-changed-checked ... */
954 ;;*---------------------------------------------------------------------*/
955 (defun flyspell-debug-signal-changed-checked ()
956 (setq debug-on-error t
)
957 (let ((point (point)))
958 (with-current-buffer (get-buffer-create "*flyspell-debug*")
959 (insert "CHANGED WORD:\n")
960 (insert (format " point : %S\n" point
))
961 (goto-char (point-max)))))
963 ;;*---------------------------------------------------------------------*/
964 ;;* flyspell-post-command-hook ... */
965 ;;* ------------------------------------------------------------- */
966 ;;* It is possible that we check several words: */
967 ;;* 1- the current word is checked if the predicate */
968 ;;* FLYSPELL-CHECK-WORD-P is true */
969 ;;* 2- the word that used to be the current word before the */
970 ;;* THIS-COMMAND is checked if: */
971 ;;* a- the previous word is different from the current word */
972 ;;* b- the previous word has not just been checked by the */
973 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
974 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
975 ;;* previous word nor the current word */
976 ;;*---------------------------------------------------------------------*/
977 (defun flyspell-post-command-hook ()
978 "The `post-command-hook' used by flyspell to check a word on-the-fly."
982 (let ((command this-command
)
983 ;; Prevent anything we do from affecting the mark.
985 (if (flyspell-check-pre-word-p)
987 '(flyspell-debug-signal-pre-word-checked)
988 (goto-char flyspell-pre-point
)
990 (if (flyspell-check-word-p)
992 '(flyspell-debug-signal-word-checked)
993 ;; FIXME: This should be asynchronous!
995 ;; we remember which word we have just checked.
996 ;; this will be used next time we will check a word
997 ;; to compare the next current word with the word
998 ;; that has been registered in the pre-command-hook
999 ;; that is these variables are used within the predicate
1000 ;; FLYSPELL-CHECK-PRE-WORD-P
1001 (setq flyspell-pre-pre-buffer
(current-buffer))
1002 (setq flyspell-pre-pre-point
(point)))
1003 (setq flyspell-pre-pre-buffer nil
)
1004 (setq flyspell-pre-pre-point nil
)
1005 ;; when a word is not checked because of a delayed command
1006 ;; we do not disable the ispell cache.
1007 (when (and (symbolp this-command
)
1008 (get this-command
'flyspell-delayed
))
1009 (setq flyspell-word-cache-end -
1)
1010 (setq flyspell-word-cache-result
'_
)))
1011 (while (and (not (input-pending-p)) (consp flyspell-changes
))
1012 (let ((start (car (car flyspell-changes
)))
1013 (stop (cdr (car flyspell-changes
))))
1014 (if (flyspell-check-changed-word-p start stop
)
1016 '(flyspell-debug-signal-changed-checked)
1019 (setq flyspell-changes
(cdr flyspell-changes
))))
1020 (setq flyspell-previous-command command
)))))
1022 ;;*---------------------------------------------------------------------*/
1023 ;;* flyspell-notify-misspell ... */
1024 ;;*---------------------------------------------------------------------*/
1025 (defun flyspell-notify-misspell (word poss
)
1026 (let ((replacements (if (stringp poss
)
1028 (flyspell-sort (car (cdr (cdr poss
))) word
))))
1029 (if flyspell-issue-message-flag
1030 (message "misspelling `%s' %S" word replacements
))))
1032 ;;*---------------------------------------------------------------------*/
1033 ;;* flyspell-word-search-backward ... */
1034 ;;*---------------------------------------------------------------------*/
1035 (defun flyspell-word-search-backward (word bound
&optional ignore-case
)
1038 (inhibit-point-motion-hooks t
)
1039 (flyspell-not-casechars (flyspell-get-not-casechars))
1040 (bound (if (and bound
1041 (> bound
(point-min)))
1044 "\\(?:" flyspell-not-casechars
"\\|\\`\\)"
1046 flyspell-not-casechars
))
1052 (re-search-backward word-re bound t
)
1057 (let ((lw (flyspell-get-word)))
1060 (string-equal (downcase (car lw
)) (downcase word
))
1061 (string-equal (car lw
) word
)))
1066 ;;*---------------------------------------------------------------------*/
1067 ;;* flyspell-word-search-forward ... */
1068 ;;*---------------------------------------------------------------------*/
1069 (defun flyspell-word-search-forward (word bound
)
1072 (inhibit-point-motion-hooks t
)
1073 (flyspell-not-casechars (flyspell-get-not-casechars))
1074 (bound (if (and bound
1075 (< bound
(point-max)))
1077 (word-re (concat flyspell-not-casechars
1079 "\\(?:" flyspell-not-casechars
"\\|\\'\\)"))
1084 (re-search-forward word-re bound t
)
1089 (let ((lw (flyspell-get-word)))
1090 (if (and (consp lw
) (string-equal (car lw
) word
))
1092 (goto-char (1+ p
)))))
1095 (defvar flyspell-word
) ;Backward compatibility; some predicates made use of it!
1097 ;;*---------------------------------------------------------------------*/
1098 ;;* flyspell-word ... */
1099 ;;*---------------------------------------------------------------------*/
1100 (defun flyspell-word (&optional following known-misspelling
)
1101 "Spell check a word.
1102 If the optional argument FOLLOWING, or, when called interactively
1103 `ispell-following-word', is non-nil, checks the following (rather
1104 than preceding) word when the cursor is not over a word. If
1105 optional argument KNOWN-MISSPELLING is non nil considers word a
1106 misspelling and skips redundant spell-checking step.
1108 See `flyspell-get-word' for details of how this finds the word to
1110 (interactive (list ispell-following-word
))
1111 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1113 ;; use the correct dictionary
1114 (flyspell-accept-buffer-local-defs)
1115 (let* ((cursor-location (point))
1116 (flyspell-word (flyspell-get-word following
))
1117 start end poss word ispell-filter
)
1118 (if (or (eq flyspell-word nil
)
1119 (and (functionp flyspell-generic-check-word-predicate
)
1120 (not (funcall flyspell-generic-check-word-predicate
))))
1123 ;; destructure return flyspell-word info list.
1124 (setq start
(car (cdr flyspell-word
))
1125 end
(car (cdr (cdr flyspell-word
)))
1126 word
(car flyspell-word
))
1127 ;; before checking in the directory, we check for doublons.
1129 ((and (or (not (eq ispell-parser
'tex
))
1130 (and (> start
(point-min))
1131 (not (memq (char-after (1- start
)) '(?\
} ?
\\)))))
1132 flyspell-mark-duplications-flag
1133 (not (catch 'exception
1134 (let ((dict (or ispell-local-dictionary
1135 ispell-dictionary
)))
1136 (dolist (except flyspell-mark-duplications-exceptions
)
1137 (and (or (null (car except
))
1139 (string-match (car except
) dict
)))
1140 (member (downcase word
) (cdr except
))
1141 (throw 'exception t
))))))
1148 (skip-chars-backward " \t\n\f")))))
1149 (p (when (>= bound
(point-min))
1150 (flyspell-word-search-backward word bound t
))))
1151 (and p
(/= p start
)))))
1152 ;; yes, this is a doublon
1153 (flyspell-highlight-incorrect-region start end
'doublon
)
1155 ((and (eq flyspell-word-cache-start start
)
1156 (eq flyspell-word-cache-end end
)
1157 (string-equal flyspell-word-cache-word word
))
1158 ;; this word had been already checked, we skip
1159 flyspell-word-cache-result
)
1160 ((and (eq ispell-parser
'tex
)
1161 (flyspell-tex-command-p flyspell-word
))
1162 ;; this is a correct word (because a tex command)
1163 (flyspell-unhighlight-at start
)
1165 (flyspell-unhighlight-at (- end
1)))
1168 ;; we setup the cache
1169 (setq flyspell-word-cache-start start
)
1170 (setq flyspell-word-cache-end end
)
1171 (setq flyspell-word-cache-word word
)
1172 ;; now check spelling of word.
1173 (if (not known-misspelling
)
1175 (ispell-send-string "%\n")
1176 ;; put in verbose mode
1177 (ispell-send-string (concat "^" word
"\n"))
1178 ;; we mark the ispell process so it can be killed
1179 ;; when emacs is exited without query
1180 (set-process-query-on-exit-flag ispell-process nil
)
1181 ;; Wait until ispell has processed word.
1183 (accept-process-output ispell-process
)
1184 (not (string= "" (car ispell-filter
)))))
1185 ;; (ispell-send-string "!\n")
1186 ;; back to terse mode.
1187 ;; Remove leading empty element
1188 (setq ispell-filter
(cdr ispell-filter
))
1189 ;; ispell process should return something after word is sent.
1190 ;; Tag word as valid (i.e., skip) otherwise
1192 (setq ispell-filter
'(*)))
1193 (if (consp ispell-filter
)
1194 (setq poss
(ispell-parse-output (car ispell-filter
)))))
1195 ;; Else, this was a known misspelling to begin with, and
1196 ;; we should forge an ispell return value.
1197 (setq poss
(list word
1 nil nil
)))
1198 (let ((res (cond ((eq poss t
)
1200 (setq flyspell-word-cache-result t
)
1201 (flyspell-unhighlight-at start
)
1203 (flyspell-unhighlight-at (- end
1)))
1205 ((and (stringp poss
) flyspell-highlight-flag
)
1207 (setq flyspell-word-cache-result t
)
1208 (flyspell-unhighlight-at start
)
1210 (flyspell-unhighlight-at (- end
1)))
1213 (setq flyspell-word-cache-result t
)
1214 (flyspell-unhighlight-at start
)
1216 (flyspell-unhighlight-at (- end
1)))
1218 ((or (and (< flyspell-duplicate-distance
0)
1221 (flyspell-word-search-backward
1226 (flyspell-word-search-forward
1229 (and (> flyspell-duplicate-distance
0)
1232 (flyspell-word-search-backward
1235 flyspell-duplicate-distance
)))
1238 (flyspell-word-search-forward
1241 flyspell-duplicate-distance
))))))
1242 ;; This is a misspelled word which occurs
1243 ;; twice within flyspell-duplicate-distance.
1244 (setq flyspell-word-cache-result nil
)
1245 (if flyspell-highlight-flag
1246 (flyspell-highlight-duplicate-region
1248 (message "duplicate `%s'" word
))
1251 (setq flyspell-word-cache-result nil
)
1252 ;; Highlight the location as incorrect,
1253 ;; including offset specified in POSS.
1254 (if flyspell-highlight-flag
1255 (flyspell-highlight-incorrect-region
1256 (if (and (consp poss
)
1257 (integerp (nth 1 poss
)))
1258 (+ start
(nth 1 poss
) -
1)
1261 (flyspell-notify-misspell word poss
))
1263 ;; return to original location
1264 (goto-char cursor-location
)
1265 (if ispell-quit
(setq ispell-quit nil
))
1268 ;;*---------------------------------------------------------------------*/
1269 ;;* flyspell-math-tex-command-p ... */
1270 ;;* ------------------------------------------------------------- */
1271 ;;* This function uses the texmathp package to check if point */
1272 ;;* is within a TeX math environment. `texmathp' can yield errors */
1273 ;;* if the document is currently not valid TeX syntax. */
1274 ;;*---------------------------------------------------------------------*/
1275 (defun flyspell-math-tex-command-p ()
1276 (when (fboundp 'texmathp
)
1277 (if flyspell-check-tex-math-command
1283 ;;*---------------------------------------------------------------------*/
1284 ;;* flyspell-tex-command-p ... */
1285 ;;*---------------------------------------------------------------------*/
1286 (defun flyspell-tex-command-p (word)
1287 "Return t if WORD is a TeX command."
1289 (let ((b (car (cdr word
))))
1290 (and (re-search-backward "\\\\" (- (point) 100) t
)
1291 (or (= (match-end 0) b
)
1292 (and (goto-char (match-end 0))
1293 (looking-at flyspell-tex-command-regexp
)
1294 (>= (match-end 0) b
))))))
1295 (flyspell-math-tex-command-p)))
1297 (defalias 'flyspell-get-casechars
'ispell-get-casechars
)
1298 (defalias 'flyspell-get-not-casechars
'ispell-get-not-casechars
)
1300 ;;*---------------------------------------------------------------------*/
1301 ;;* flyspell-get-word ... */
1302 ;;*---------------------------------------------------------------------*/
1303 (defun flyspell-get-word (&optional following extra-otherchars
)
1304 "Return the word for spell-checking according to Ispell syntax.
1305 Optional argument FOLLOWING non-nil means to get the following
1306 \(rather than preceding) word when the cursor is not over a word.
1307 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1308 that may be included as part of a word (see `ispell-dictionary-alist').
1310 This finds the word to spell-check by searching for CASECHARS defined
1311 in `ispell-dictionary-alist' for the current dictionary. Thus, the
1312 word could be far away from point if point is inside whitespace or
1313 punctuation characters, or in text that belongs to a different
1315 (let* ((flyspell-casechars (flyspell-get-casechars))
1316 (flyspell-not-casechars (flyspell-get-not-casechars))
1317 (ispell-otherchars (ispell-get-otherchars))
1318 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1319 (word-regexp (concat flyspell-casechars
1321 (if (not (string= "" ispell-otherchars
))
1322 (concat ispell-otherchars
"?"))
1323 (if extra-otherchars
1324 (concat extra-otherchars
"?"))
1327 (if (or ispell-many-otherchars-p
1333 (if (not (looking-at flyspell-casechars
))
1335 (re-search-forward flyspell-casechars nil t
)
1336 (re-search-backward flyspell-casechars nil t
)))
1337 ;; move to front of word
1338 (re-search-backward flyspell-not-casechars nil
'start
)
1339 (while (and (or (and (not (string= "" ispell-otherchars
))
1340 (looking-at ispell-otherchars
))
1341 (and extra-otherchars
(looking-at extra-otherchars
)))
1343 (or (not did-it-once
)
1344 ispell-many-otherchars-p
)
1345 (not (eq prevpt
(point))))
1346 (if (and extra-otherchars
(looking-at extra-otherchars
))
1349 (if (looking-at flyspell-casechars
)
1350 (re-search-backward flyspell-not-casechars nil
'move
)))
1354 (if (looking-at flyspell-casechars
)
1355 (re-search-backward flyspell-not-casechars nil
'move
)
1356 (backward-char -
1))))
1357 ;; Now mark the word and save to string.
1358 (if (not (re-search-forward word-regexp nil t
))
1361 (setq start
(match-beginning 0)
1363 word
(buffer-substring-no-properties start end
))
1364 (list word start end
)))))
1366 ;;*---------------------------------------------------------------------*/
1367 ;;* flyspell-small-region ... */
1368 ;;*---------------------------------------------------------------------*/
1369 (defun flyspell-small-region (beg end
)
1370 "Flyspell text between BEG and END."
1378 (while (< (point) end
)
1379 (if (and flyspell-issue-message-flag
(= count
100))
1381 (message "Spell Checking...%d%%"
1382 (floor (* 100.0 (- (point) beg
)) (- end beg
)))
1384 (setq count
(+ 1 count
)))
1387 (let ((cur (point)))
1389 (if (and (< (point) end
) (> (point) (+ cur
1)))
1390 (backward-char 1)))))
1392 (if flyspell-issue-message-flag
(message "Spell Checking completed."))
1395 ;;*---------------------------------------------------------------------*/
1396 ;;* flyspell-external-ispell-process ... */
1397 ;;*---------------------------------------------------------------------*/
1398 (defvar flyspell-external-ispell-process
'()
1399 "The external Flyspell Ispell process.")
1401 ;;*---------------------------------------------------------------------*/
1402 ;;* flyspell-external-ispell-buffer ... */
1403 ;;*---------------------------------------------------------------------*/
1404 (defvar flyspell-external-ispell-buffer
'())
1405 (defvar flyspell-large-region-buffer
'())
1406 (defvar flyspell-large-region-beg
(point-min))
1407 (defvar flyspell-large-region-end
(point-max))
1409 ;;*---------------------------------------------------------------------*/
1410 ;;* flyspell-external-point-words ... */
1411 ;;*---------------------------------------------------------------------*/
1412 (defun flyspell-external-point-words ()
1413 "Mark words from a buffer listing incorrect words in order of appearance.
1414 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1415 \(We finish by killing that buffer and setting the variable to nil.)
1416 The buffer to mark them in is `flyspell-large-region-buffer'."
1417 (let (words-not-found
1418 (ispell-otherchars (ispell-get-otherchars))
1419 (buffer-scan-pos flyspell-large-region-beg
)
1421 (with-current-buffer flyspell-external-ispell-buffer
1422 (goto-char (point-min))
1423 ;; Loop over incorrect words, in the order they were reported,
1424 ;; which is also the order they appear in the buffer being checked.
1425 (while (re-search-forward "\\([^\n]+\\)\n" nil t
)
1426 ;; Bind WORD to the next one.
1427 (let ((word (match-string 1)) (wordpos (point)))
1428 ;; Here there used to be code to see if WORD is the same
1429 ;; as the previous iteration, and count the number of consecutive
1430 ;; identical words, and the loop below would search for that many.
1431 ;; That code seemed to be incorrect, and on principle, should
1432 ;; be unnecessary too. -- rms.
1433 (if flyspell-issue-message-flag
1434 (message "Spell Checking...%d%% [%s]"
1435 (floor (* 100.0 (point)) (point-max))
1437 (with-current-buffer flyspell-large-region-buffer
1438 (goto-char buffer-scan-pos
)
1440 ;; Iterate on string search until string is found as word,
1441 ;; not as substring.
1443 (if (search-forward word
1444 flyspell-large-region-end t
)
1447 ;; Move back into the match
1448 ;; so flyspell-get-word will find it.
1450 (flyspell-get-word)))
1451 (found (car found-list
))
1452 (found-length (length found
))
1453 (misspell-length (length word
)))
1455 ;; Size matches, we really found it.
1456 (= found-length misspell-length
)
1457 ;; Matches as part of a boundary-char separated
1460 (split-string found ispell-otherchars
))
1461 ;; Misspelling has higher length than
1462 ;; what flyspell considers the word.
1463 ;; Caused by boundary-chars mismatch.
1464 ;; Validating seems safe.
1465 (< found-length misspell-length
)
1466 ;; ispell treats beginning of some TeX
1467 ;; commands as nroff control sequences
1468 ;; and strips them in the list of
1469 ;; misspelled words thus giving a
1470 ;; non-existent word. Skip if ispell
1471 ;; is used, string is a TeX command
1472 ;; (char before beginning of word is
1473 ;; backslash) and none of the previous
1474 ;; conditions match.
1475 (and (not ispell-really-aspell
)
1477 (goto-char (- (nth 1 found-list
) 1))
1478 (if (looking-at "[\\]" )
1482 (flyspell-word nil t
)
1483 ;; Search for next misspelled word will begin from
1484 ;; end of last validated match.
1485 (setq buffer-scan-pos
(point))))
1486 ;; Record if misspelling is not found and try new one
1487 (cl-pushnew (concat " -> " word
" - "
1488 (int-to-string wordpos
))
1489 words-not-found
:test
#'equal
)
1490 (setq keep nil
)))))))
1492 (if flyspell-issue-message-flag
(message "Spell Checking completed.")))
1493 ;; Warn about not found misspellings
1494 (dolist (word words-not-found
)
1495 (message "%s: word not found" word
))
1496 ;; Kill and forget the buffer with the list of incorrect words.
1497 (kill-buffer flyspell-external-ispell-buffer
)
1498 (setq flyspell-external-ispell-buffer nil
)))
1500 ;;*---------------------------------------------------------------------*/
1501 ;;* flyspell-process-localwords ... */
1502 ;;* ------------------------------------------------------------- */
1503 ;;* This function is used to prevent marking of words explicitly */
1504 ;;* declared correct. */
1505 ;;*---------------------------------------------------------------------*/
1506 (defun flyspell-process-localwords (misspellings-buffer)
1507 (let ((localwords ispell-buffer-session-localwords
)
1509 (ispell-casechars (ispell-get-casechars)))
1510 ;; Get localwords from the original buffer
1512 (goto-char (point-min))
1513 ;; Localwords parsing copied from ispell.el.
1514 (while (search-forward ispell-words-keyword nil t
)
1515 (let ((end (point-at-eol))
1517 ;; buffer-local words separated by a space, and can contain
1518 ;; any character other than a space. Not rigorous enough.
1519 (while (re-search-forward " *\\([^ ]+\\)" end t
)
1520 (setq string
(buffer-substring-no-properties (match-beginning 1)
1522 ;; This can fail when string contains a word with invalid chars.
1523 ;; Error handling needs to be added between Ispell and Emacs.
1524 (if (and (< 1 (length string
))
1525 (equal 0 (string-match ispell-casechars string
)))
1526 (push string localwords
))))))
1527 ;; Remove localwords matches from misspellings-buffer.
1528 ;; The usual mechanism of communicating the local words to ispell
1529 ;; does not affect the special ispell process used by
1530 ;; flyspell-large-region.
1531 (with-current-buffer misspellings-buffer
1533 (dolist (word localwords
)
1534 (goto-char (point-min))
1535 (let ((regexp (concat "^" word
"\n")))
1536 (while (re-search-forward regexp nil t
)
1537 (delete-region (match-beginning 0) (match-end 0)))))))))
1539 ;;* ---------------------------------------------------------------
1540 ;;* flyspell-check-region-doublons
1541 ;;* ---------------------------------------------------------------
1542 (defun flyspell-check-region-doublons (beg end
)
1543 "Check for adjacent duplicated words (doublons) in the given region."
1546 (flyspell-word) ; Make sure current word is checked
1548 (while (and (< (point) end
)
1549 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1555 ;;*---------------------------------------------------------------------*/
1556 ;;* flyspell-large-region ... */
1557 ;;*---------------------------------------------------------------------*/
1558 (defun flyspell-large-region (beg end
)
1559 (let* ((curbuf (current-buffer))
1560 (buffer (get-buffer-create "*flyspell-region*")))
1561 (setq flyspell-external-ispell-buffer buffer
)
1562 (setq flyspell-large-region-buffer curbuf
)
1563 (setq flyspell-large-region-beg beg
)
1564 (setq flyspell-large-region-end end
)
1565 (flyspell-accept-buffer-local-defs)
1568 ;; this is done, we can start checking...
1569 (if flyspell-issue-message-flag
(message "Checking region..."))
1571 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1572 ;; Local dictionary becomes the global dictionary in use.
1573 (setq ispell-current-dictionary
1574 (or ispell-local-dictionary ispell-dictionary
))
1575 (setq ispell-current-personal-dictionary
1576 (or ispell-local-pdict ispell-personal-dictionary
))
1577 (let ((args (ispell-get-ispell-args))
1578 (encoding (ispell-get-coding-system))
1580 (if (and ispell-current-dictionary
; use specified dictionary
1581 (not (member "-d" args
))) ; only define if not overridden
1583 (append (list "-d" ispell-current-dictionary
) args
)))
1584 (if ispell-current-personal-dictionary
; use specified pers dict
1589 ispell-current-personal-dictionary
)))))
1591 ;; Check for extended character mode
1592 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1593 (and extended-char-mode
; ~ extended character mode
1594 (string-match "[^~]+$" extended-char-mode
)
1595 (cl-pushnew (concat "-T" (match-string 0 extended-char-mode
))
1596 args
:test
#'equal
)))
1598 ;; Add ispell-extra-args
1599 (setq args
(append args ispell-extra-args
))
1601 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1602 ;; for communication. ispell or older aspell/hunspell does not support this
1603 (if ispell-encoding8-command
1606 (if ispell-really-hunspell
1607 (list ispell-encoding8-command
1608 (upcase (symbol-name encoding
)))
1609 (list (concat ispell-encoding8-command
1610 (symbol-name encoding
)))))))
1612 (let ((process-coding-system-alist (list (cons "\\.*" encoding
))))
1613 (setq c
(apply 'ispell-call-process-region beg
1619 (if ispell-really-aspell
"list" "-l")
1623 (flyspell-process-localwords buffer
)
1624 (with-current-buffer curbuf
1625 (flyspell-delete-region-overlays beg end
)
1626 (flyspell-check-region-doublons beg end
))
1627 (flyspell-external-point-words))
1628 (error "Can't check region")))))
1630 ;;*---------------------------------------------------------------------*/
1631 ;;* flyspell-region ... */
1632 ;;* ------------------------------------------------------------- */
1633 ;;* Because `ispell -a' is too slow, it is not possible to use */
1634 ;;* it on large region. Then, when ispell is invoked on a large */
1635 ;;* text region, a new `ispell -l' process is spawned. The */
1636 ;;* pointed out words are then searched in the region a checked with */
1637 ;;* regular flyspell means. */
1638 ;;*---------------------------------------------------------------------*/
1640 (defun flyspell-region (beg end
)
1641 "Flyspell text between BEG and END."
1643 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1651 (if (and flyspell-large-region
(> (- end beg
) flyspell-large-region
))
1652 (flyspell-large-region beg end
)
1653 (flyspell-small-region beg end
)))))
1655 ;;*---------------------------------------------------------------------*/
1656 ;;* flyspell-buffer ... */
1657 ;;*---------------------------------------------------------------------*/
1659 (defun flyspell-buffer ()
1660 "Flyspell whole buffer."
1662 (flyspell-region (point-min) (point-max)))
1664 ;;*---------------------------------------------------------------------*/
1665 ;;* old next error position ... */
1666 ;;*---------------------------------------------------------------------*/
1667 (defvar flyspell-old-buffer-error nil
)
1668 (defvar flyspell-old-pos-error nil
)
1670 ;;*---------------------------------------------------------------------*/
1671 ;;* flyspell-goto-next-error ... */
1672 ;;*---------------------------------------------------------------------*/
1673 (defun flyspell-goto-next-error ()
1674 "Go to the next previously detected error.
1675 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1680 (if (and (eq (current-buffer) flyspell-old-buffer-error
)
1681 (eq pos flyspell-old-pos-error
))
1683 (if (= flyspell-old-pos-error max
)
1684 ;; goto beginning of buffer
1686 (message "Restarting from beginning of buffer")
1687 (goto-char (point-min)))
1689 (setq pos
(point))))
1690 ;; seek the next error
1691 (while (and (< pos max
)
1692 (let ((ovs (overlays-at pos
))
1694 (while (and (not r
) (consp ovs
))
1695 (if (flyspell-overlay-p (car ovs
))
1697 (setq ovs
(cdr ovs
))))
1699 (setq pos
(1+ pos
)))
1700 ;; save the current location for next invocation
1701 (setq flyspell-old-pos-error pos
)
1702 (setq flyspell-old-buffer-error
(current-buffer))
1705 (message "No more miss-spelled word!"))))
1707 ;;*---------------------------------------------------------------------*/
1708 ;;* flyspell-overlay-p ... */
1709 ;;*---------------------------------------------------------------------*/
1710 (defun flyspell-overlay-p (o)
1711 "Return true if O is an overlay used by flyspell."
1712 (and (overlayp o
) (overlay-get o
'flyspell-overlay
)))
1714 ;;*---------------------------------------------------------------------*/
1715 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1716 ;;* ------------------------------------------------------------- */
1717 ;;* Remove overlays introduced by flyspell. */
1718 ;;*---------------------------------------------------------------------*/
1719 (defun flyspell-delete-region-overlays (beg end
)
1720 "Delete overlays used by flyspell in a given region."
1721 (remove-overlays beg end
'flyspell-overlay t
))
1723 (defun flyspell-delete-all-overlays ()
1724 "Delete all the overlays used by flyspell."
1725 (flyspell-delete-region-overlays (point-min) (point-max)))
1727 ;;*---------------------------------------------------------------------*/
1728 ;;* flyspell-unhighlight-at ... */
1729 ;;*---------------------------------------------------------------------*/
1730 (defun flyspell-unhighlight-at (pos)
1731 "Remove the flyspell overlay that are located at POS."
1732 (if flyspell-persistent-highlight
1733 (let ((overlays (overlays-at pos
)))
1734 (while (consp overlays
)
1735 (if (flyspell-overlay-p (car overlays
))
1736 (delete-overlay (car overlays
)))
1737 (setq overlays
(cdr overlays
))))
1738 (if (flyspell-overlay-p flyspell-overlay
)
1739 (delete-overlay flyspell-overlay
))))
1741 ;;*---------------------------------------------------------------------*/
1742 ;;* flyspell-properties-at-p ... */
1743 ;;* ------------------------------------------------------------- */
1744 ;;* Is there a highlight property at position pos? */
1745 ;;*---------------------------------------------------------------------*/
1746 (defun flyspell-properties-at-p (pos)
1747 "Return t if there is a text property at POS, not counting `local-map'.
1748 If variable `flyspell-highlight-properties' is set to nil,
1749 text with properties are not checked. This function is used to discover
1750 if the character at POS has any other property."
1751 (let ((prop (text-properties-at pos
))
1753 (while (and keep
(consp prop
))
1754 (if (and (eq (car prop
) 'local-map
) (consp (cdr prop
)))
1755 (setq prop
(cdr (cdr prop
)))
1759 ;;*---------------------------------------------------------------------*/
1760 ;;* make-flyspell-overlay ... */
1761 ;;*---------------------------------------------------------------------*/
1762 (defun make-flyspell-overlay (beg end face mouse-face
)
1763 "Allocate an overlay to highlight an incorrect word.
1764 BEG and END specify the range in the buffer of that word.
1765 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1767 (let ((overlay (make-overlay beg end nil t nil
)))
1768 (overlay-put overlay
'face face
)
1769 (overlay-put overlay
'mouse-face mouse-face
)
1770 (overlay-put overlay
'flyspell-overlay t
)
1771 (overlay-put overlay
'evaporate t
)
1772 (overlay-put overlay
'help-echo
"mouse-2: correct word at point")
1773 ;; If misspelled text has a 'keymap' property, let that remain in
1774 ;; effect for the bindings that flyspell-mouse-map doesn't override.
1775 (set-keymap-parent flyspell-mouse-map
(get-char-property beg
'keymap
))
1776 (overlay-put overlay
'keymap flyspell-mouse-map
)
1777 (when (eq face
'flyspell-incorrect
)
1778 (and (stringp flyspell-before-incorrect-word-string
)
1779 (overlay-put overlay
'before-string
1780 flyspell-before-incorrect-word-string
))
1781 (and (stringp flyspell-after-incorrect-word-string
)
1782 (overlay-put overlay
'after-string
1783 flyspell-after-incorrect-word-string
)))
1786 ;;*---------------------------------------------------------------------*/
1787 ;;* flyspell-highlight-incorrect-region ... */
1788 ;;*---------------------------------------------------------------------*/
1789 (defun flyspell-highlight-incorrect-region (beg end poss
)
1790 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1791 POSS is usually a list of possible spelling/correction lists,
1792 as returned by `ispell-parse-output'.
1793 It can also be the symbol `doublon', in the case where the word
1794 is itself incorrect, but suspiciously repeated."
1795 (let ((inhibit-read-only t
))
1796 (unless (run-hook-with-args-until-success
1797 'flyspell-incorrect-hook beg end poss
)
1798 (if (or flyspell-highlight-properties
1799 (not (flyspell-properties-at-p beg
)))
1801 ;; we cleanup all the overlay that are in the region, not
1802 ;; beginning at the word start position
1803 (if (< (1+ beg
) end
)
1804 (let ((os (overlays-in (1+ beg
) end
)))
1806 (if (flyspell-overlay-p (car os
))
1807 (delete-overlay (car os
)))
1808 (setq os
(cdr os
)))))
1809 ;; we cleanup current overlay at the same position
1810 (flyspell-unhighlight-at beg
)
1811 ;; now we can use a new overlay
1812 (setq flyspell-overlay
1813 (make-flyspell-overlay
1815 (if (eq poss
'doublon
) 'flyspell-duplicate
'flyspell-incorrect
)
1818 ;;*---------------------------------------------------------------------*/
1819 ;;* flyspell-highlight-duplicate-region ... */
1820 ;;*---------------------------------------------------------------------*/
1821 (defun flyspell-highlight-duplicate-region (beg end poss
)
1822 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1823 POSS is a list of possible spelling/correction lists,
1824 as returned by `ispell-parse-output'."
1825 (let ((inhibit-read-only t
))
1826 (unless (run-hook-with-args-until-success
1827 'flyspell-incorrect-hook beg end poss
)
1828 (if (or flyspell-highlight-properties
1829 (not (flyspell-properties-at-p beg
)))
1831 ;; we cleanup current overlay at the same position
1832 (flyspell-unhighlight-at beg
)
1833 ;; now we can use a new overlay
1834 (setq flyspell-overlay
1835 (make-flyspell-overlay beg end
1839 ;;*---------------------------------------------------------------------*/
1840 ;;* flyspell-auto-correct-cache ... */
1841 ;;*---------------------------------------------------------------------*/
1842 (defvar flyspell-auto-correct-pos nil
)
1843 (defvar flyspell-auto-correct-region nil
)
1844 (defvar flyspell-auto-correct-ring nil
)
1845 (defvar flyspell-auto-correct-word nil
)
1846 (make-variable-buffer-local 'flyspell-auto-correct-pos
)
1847 (make-variable-buffer-local 'flyspell-auto-correct-region
)
1848 (make-variable-buffer-local 'flyspell-auto-correct-ring
)
1849 (make-variable-buffer-local 'flyspell-auto-correct-word
)
1851 ;;*---------------------------------------------------------------------*/
1852 ;;* flyspell-check-previous-highlighted-word ... */
1853 ;;*---------------------------------------------------------------------*/
1854 (defun flyspell-check-previous-highlighted-word (&optional arg
)
1855 "Correct the closest previous word that is highlighted as misspelled.
1856 This function scans for a word which starts before point that has been
1857 highlighted by Flyspell as misspelled. If it finds one, it proposes
1858 a replacement for that word. With prefix arg N, check the Nth word
1859 before point that's highlighted as misspelled."
1861 (let ((pos1 (point))
1863 (arg (if (or (not (numberp arg
)) (< arg
1)) 1 arg
))
1866 (while (and (setq pos
(previous-overlay-change pos
))
1869 (if (> pos
(point-min))
1871 (setq ovs
(overlays-at pos
))
1874 (setq ovs
(cdr ovs
))
1875 (if (and (flyspell-overlay-p ov
)
1876 (= 0 (setq arg
(1- arg
))))
1877 (throw 'exit t
)))))))
1881 (setq flyspell-word-cache-word nil
) ;; Force flyspell-word re-check
1883 (error "No word to correct before point"))))
1885 ;;*---------------------------------------------------------------------*/
1886 ;;* flyspell-display-next-corrections ... */
1887 ;;*---------------------------------------------------------------------*/
1888 (defun flyspell-display-next-corrections (corrections)
1889 (let ((string "Corrections:")
1892 (while (< (length string
) 80)
1893 (if (equal (car l
) flyspell-auto-correct-word
)
1894 (setq pos
(cons (+ 1 (length string
)) pos
)))
1895 (setq string
(concat string
" " (car l
)))
1898 (let ((num (car pos
)))
1899 (put-text-property num
1900 (+ num
(length flyspell-auto-correct-word
))
1901 'face
'flyspell-incorrect
1903 (setq pos
(cdr pos
)))
1904 (if (fboundp 'display-message
)
1905 (display-message 'no-log string
)
1906 (message "%s" string
))))
1908 ;;*---------------------------------------------------------------------*/
1909 ;;* flyspell-abbrev-table ... */
1910 ;;*---------------------------------------------------------------------*/
1911 (defun flyspell-abbrev-table ()
1912 (if flyspell-use-global-abbrev-table-p
1914 (or local-abbrev-table global-abbrev-table
)))
1916 ;;*---------------------------------------------------------------------*/
1917 ;;* flyspell-define-abbrev ... */
1918 ;;*---------------------------------------------------------------------*/
1919 (defun flyspell-define-abbrev (name expansion
)
1920 (let ((table (flyspell-abbrev-table)))
1922 (define-abbrev table
(downcase name
) expansion
))))
1924 ;;*---------------------------------------------------------------------*/
1925 ;;* flyspell-auto-correct-word ... */
1926 ;;*---------------------------------------------------------------------*/
1927 (defun flyspell-auto-correct-word ()
1928 "Correct the current word.
1929 This command proposes various successive corrections for the
1930 current word. If invoked repeatedly on the same position, it
1931 cycles through the possible corrections of the current word.
1933 See `flyspell-get-word' for details of how this finds the word to
1936 ;; If we are not in the construct where flyspell should be active,
1937 ;; invoke the original binding of M-TAB, if that was recorded.
1938 (if (and (local-variable-p 'flyspell--prev-meta-tab-binding
)
1939 (commandp flyspell--prev-meta-tab-binding t
)
1940 (functionp flyspell-generic-check-word-predicate
)
1941 (not (funcall flyspell-generic-check-word-predicate
))
1942 (equal (where-is-internal 'flyspell-auto-correct-word nil t
)
1944 (call-interactively flyspell--prev-meta-tab-binding
)
1946 (old-max (point-max)))
1947 ;; Use the correct dictionary.
1948 (flyspell-accept-buffer-local-defs)
1949 (if (and (eq flyspell-auto-correct-pos pos
)
1950 (consp flyspell-auto-correct-region
))
1951 ;; We have already been using the function at the same location.
1952 (let* ((start (car flyspell-auto-correct-region
))
1953 (len (cdr flyspell-auto-correct-region
)))
1954 (flyspell-unhighlight-at start
)
1955 (delete-region start
(+ start len
))
1956 (setq flyspell-auto-correct-ring
(cdr flyspell-auto-correct-ring
))
1957 (let* ((word (car flyspell-auto-correct-ring
))
1958 (len (length word
)))
1959 (rplacd flyspell-auto-correct-region len
)
1961 (if flyspell-abbrev-p
1962 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1963 flyspell-auto-correct-word
)
1964 (flyspell-change-abbrev (flyspell-abbrev-table)
1965 flyspell-auto-correct-word
1967 (flyspell-define-abbrev flyspell-auto-correct-word word
)))
1968 (funcall flyspell-insert-function word
)
1970 (flyspell-display-next-corrections flyspell-auto-correct-ring
))
1971 (flyspell-adjust-cursor-point pos
(point) old-max
)
1972 (setq flyspell-auto-correct-pos
(point)))
1973 ;; Fetch the word to be checked.
1974 (let ((word (flyspell-get-word)))
1976 (let ((start (car (cdr word
)))
1977 (end (car (cdr (cdr word
))))
1980 (setq flyspell-auto-correct-word word
)
1981 ;; Now check spelling of word..
1982 (ispell-send-string "%\n") ;Put in verbose mode.
1983 (ispell-send-string (concat "^" word
"\n"))
1984 ;; Wait until ispell has processed word.
1986 (accept-process-output ispell-process
)
1987 (not (string= "" (car ispell-filter
)))))
1988 ;; Remove leading empty element.
1989 (setq ispell-filter
(cdr ispell-filter
))
1990 ;; Ispell process should return something after word is sent.
1991 ;; Tag word as valid (i.e., skip) otherwise.
1993 (setq ispell-filter
'(*)))
1994 (if (consp ispell-filter
)
1995 (setq poss
(ispell-parse-output (car ispell-filter
))))
1997 ((or (eq poss t
) (stringp poss
))
1998 ;; Don't correct word.
2002 (error "Ispell: error in Ispell process"))
2004 ;; The word is incorrect, we have to propose a replacement.
2005 (let ((replacements (flyspell-sort (car (cdr (cdr poss
)))
2007 (setq flyspell-auto-correct-region nil
)
2008 (if (consp replacements
)
2010 (let ((replace (car replacements
)))
2011 (let ((new-word replace
))
2012 (if (not (equal new-word
(car poss
)))
2014 ;; then save the current replacements
2015 (setq flyspell-auto-correct-region
2016 (cons start
(length new-word
)))
2017 (let ((l replacements
))
2018 (while (consp (cdr l
))
2020 (rplacd l
(cons (car poss
) replacements
)))
2021 (setq flyspell-auto-correct-ring
2023 (flyspell-unhighlight-at start
)
2024 (delete-region start end
)
2025 (funcall flyspell-insert-function new-word
)
2026 (if flyspell-abbrev-p
2027 (if (flyspell-already-abbrevp
2028 (flyspell-abbrev-table) word
)
2029 (flyspell-change-abbrev
2030 (flyspell-abbrev-table)
2033 (flyspell-define-abbrev word
2036 (flyspell-display-next-corrections
2037 (cons new-word flyspell-auto-correct-ring
))
2038 (flyspell-adjust-cursor-point pos
2041 (setq flyspell-auto-correct-pos
(point))
2042 (ispell-pdict-save t
))))))))
2044 ;;*---------------------------------------------------------------------*/
2045 ;;* flyspell-auto-correct-previous-pos ... */
2046 ;;*---------------------------------------------------------------------*/
2047 (defvar flyspell-auto-correct-previous-pos nil
2048 "Holds the start of the first incorrect word before point.")
2050 ;;*---------------------------------------------------------------------*/
2051 ;;* flyspell-auto-correct-previous-hook ... */
2052 ;;*---------------------------------------------------------------------*/
2053 (defun flyspell-auto-correct-previous-hook ()
2054 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
2055 Sets `flyspell-auto-correct-previous-pos' to nil"
2057 (remove-hook 'pre-command-hook
(function flyspell-auto-correct-previous-hook
) t
)
2058 (unless (eq this-command
(function flyspell-auto-correct-previous-word
))
2059 (setq flyspell-auto-correct-previous-pos nil
)))
2061 ;;*---------------------------------------------------------------------*/
2062 ;;* flyspell-auto-correct-previous-word ... */
2063 ;;*---------------------------------------------------------------------*/
2064 (defun flyspell-auto-correct-previous-word (position)
2065 "Auto correct the first misspelled word that occurs before point.
2066 But don't look beyond what's visible on the screen."
2069 (let ((top (window-start))
2073 (narrow-to-region top bot
)
2074 (overlay-recenter (point))
2076 (add-hook 'pre-command-hook
2077 (function flyspell-auto-correct-previous-hook
) t t
)
2079 (unless flyspell-auto-correct-previous-pos
2080 ;; only reset if a new overlay exists
2081 (setq flyspell-auto-correct-previous-pos nil
)
2083 (let ((overlay-list (overlays-in (point-min) position
))
2084 (new-overlay 'dummy-value
))
2086 ;; search for previous (new) flyspell overlay
2087 (while (and new-overlay
2088 (or (not (flyspell-overlay-p new-overlay
))
2089 ;; check if its face has changed
2090 (not (eq (get-char-property
2091 (overlay-start new-overlay
) 'face
)
2092 'flyspell-incorrect
))))
2093 (setq new-overlay
(car-safe overlay-list
))
2094 (setq overlay-list
(cdr-safe overlay-list
)))
2096 ;; if nothing new exits new-overlay should be nil
2097 (if new-overlay
;; the length of the word may change so go to the start
2098 (setq flyspell-auto-correct-previous-pos
2099 (overlay-start new-overlay
)))))
2101 (when flyspell-auto-correct-previous-pos
2103 (goto-char flyspell-auto-correct-previous-pos
)
2104 (let ((ispell-following-word t
)) ;; point is at start
2105 (if (numberp flyspell-auto-correct-previous-pos
)
2106 (goto-char flyspell-auto-correct-previous-pos
))
2107 (flyspell-auto-correct-word))
2108 ;; the point may have moved so reset this
2109 (setq flyspell-auto-correct-previous-pos
(point))))))))
2111 ;;*---------------------------------------------------------------------*/
2112 ;;* flyspell-correct-word ... */
2113 ;;*---------------------------------------------------------------------*/
2115 (defun flyspell-correct-word (event)
2116 "Pop up a menu of possible corrections for a misspelled word.
2117 The word checked is the word at the mouse position."
2119 (let ((save (point)))
2120 (mouse-set-point event
)
2121 (flyspell-correct-word-before-point event save
)))
2123 (defun flyspell-correct-word-before-point (&optional event opoint
)
2124 "Pop up a menu of possible corrections for misspelled word before point.
2125 If EVENT is non-nil, it is the mouse event that invoked this operation;
2126 that controls where to put the menu.
2127 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2129 ;; use the correct dictionary
2130 (flyspell-accept-buffer-local-defs)
2131 (or opoint
(setq opoint
(point)))
2132 (let ((cursor-location (point))
2133 (word (flyspell-get-word)))
2135 (let ((start (car (cdr word
)))
2136 (end (car (cdr (cdr word
))))
2139 ;; now check spelling of word.
2140 (ispell-send-string "%\n") ;put in verbose mode
2141 (ispell-send-string (concat "^" word
"\n"))
2142 ;; wait until ispell has processed word
2144 (accept-process-output ispell-process
)
2145 (not (string= "" (car ispell-filter
)))))
2146 ;; Remove leading empty element
2147 (setq ispell-filter
(cdr ispell-filter
))
2148 ;; ispell process should return something after word is sent.
2149 ;; Tag word as valid (i.e., skip) otherwise
2151 (setq ispell-filter
'(*)))
2152 (if (consp ispell-filter
)
2153 (setq poss
(ispell-parse-output (car ispell-filter
))))
2155 ((or (eq poss t
) (stringp poss
))
2156 ;; don't correct word
2160 (error "Ispell: error in Ispell process"))
2162 ;; The word is incorrect, we have to propose a replacement.
2163 (flyspell-do-correct (flyspell-emacs-popup event poss word
)
2164 poss word cursor-location start end opoint
)))
2165 (ispell-pdict-save t
)))))
2167 ;;*---------------------------------------------------------------------*/
2168 ;;* flyspell-do-correct ... */
2169 ;;*---------------------------------------------------------------------*/
2170 (defun flyspell-do-correct (replace poss word cursor-location start end save
)
2171 "The popup menu callback."
2172 (cond ((eq replace
'ignore
)
2177 (ispell-send-string (concat "*" word
"\n"))
2178 (ispell-send-string "#\n")
2179 (flyspell-unhighlight-at cursor-location
)
2180 (setq ispell-pdict-modified-p
'(t)))
2181 ((or (eq replace
'buffer
) (eq replace
'session
))
2182 (ispell-send-string (concat "@" word
"\n"))
2183 (add-to-list 'ispell-buffer-session-localwords word
)
2184 (or ispell-buffer-local-name
; session localwords might conflict
2185 (setq ispell-buffer-local-name
(buffer-name)))
2186 (flyspell-unhighlight-at cursor-location
)
2187 (if (null ispell-pdict-modified-p
)
2188 (setq ispell-pdict-modified-p
2189 (list ispell-pdict-modified-p
)))
2191 (if (eq replace
'buffer
)
2192 (ispell-add-per-file-word-list word
)))
2194 (flyspell-unhighlight-at cursor-location
)
2195 (let ((old-max (point-max))
2196 (new-word (if (atom replace
)
2199 (cursor-location (+ (- (length word
) (- end start
))
2201 (unless (equal new-word
(car poss
))
2202 (delete-region start end
)
2204 (funcall flyspell-insert-function new-word
)
2205 (if flyspell-abbrev-p
2206 (flyspell-define-abbrev word new-word
)))
2207 (flyspell-adjust-cursor-point save cursor-location old-max
)))
2212 ;;*---------------------------------------------------------------------*/
2213 ;;* flyspell-adjust-cursor-point ... */
2214 ;;*---------------------------------------------------------------------*/
2215 (defun flyspell-adjust-cursor-point (save cursor-location old-max
)
2216 (if (>= save cursor-location
)
2217 (let ((new-pos (+ save
(- (point-max) old-max
))))
2219 ((< new-pos
(point-min))
2221 ((> new-pos
(point-max))
2226 ;;*---------------------------------------------------------------------*/
2227 ;;* flyspell-emacs-popup ... */
2228 ;;*---------------------------------------------------------------------*/
2229 (defun flyspell-emacs-popup (event poss word
)
2230 "The Emacs popup menu."
2231 (if (and (not event
)
2233 (let* ((mouse-pos (mouse-position))
2234 (mouse-pos (if (nth 1 mouse-pos
)
2236 (set-mouse-position (car mouse-pos
)
2237 (/ (frame-width) 2) 2)
2239 (setq event
(list (list (car (cdr mouse-pos
))
2240 (1+ (cdr (cdr mouse-pos
))))
2242 (let* ((corrects (flyspell-sort (car (cdr (cdr poss
))) word
))
2243 (cor-menu (if (consp corrects
)
2244 (mapcar (lambda (correct)
2245 (list correct correct
))
2248 (affix (car (cdr (cdr (cdr poss
)))))
2250 (base-menu (let ((save (if (and (consp affix
) show-affix-info
)
2252 (list (concat "Save affix: " (car affix
))
2254 '("Accept (session)" session
)
2255 '("Accept (buffer)" buffer
))
2256 '(("Save word" save
)
2257 ("Accept (session)" session
)
2258 ("Accept (buffer)" buffer
)))))
2259 (if (consp cor-menu
)
2260 (append cor-menu
(cons "" save
))
2262 (menu (cons "flyspell correction menu" base-menu
)))
2263 (car (x-popup-menu event
2264 (list (format "%s [%s]" word
(or ispell-local-dictionary
2268 ;;*---------------------------------------------------------------------*/
2269 ;;* Some example functions for real autocorrecting */
2270 ;;*---------------------------------------------------------------------*/
2271 (defun flyspell-maybe-correct-transposition (beg end poss
)
2272 "Check replacements for transposed characters.
2274 If the text between BEG and END is equal to a correction suggested by
2275 Ispell, after transposing two adjacent characters, correct the text,
2278 The third arg POSS is either the symbol `doublon' or a list of
2279 possible corrections as returned by `ispell-parse-output'.
2281 This function is meant to be added to `flyspell-incorrect-hook'."
2284 (let ((str (buffer-substring beg end
))
2285 (i 0) (len (- end beg
)) tmp
)
2286 (while (< (1+ i
) len
)
2287 (setq tmp
(aref str i
))
2288 (aset str i
(aref str
(1+ i
)))
2289 (aset str
(1+ i
) tmp
)
2290 (when (member str
(nth 2 poss
))
2292 (goto-char (+ beg i
1))
2293 (transpose-chars 1))
2295 (setq tmp
(aref str i
))
2296 (aset str i
(aref str
(1+ i
)))
2297 (aset str
(1+ i
) tmp
)
2301 (defun flyspell-maybe-correct-doubling (beg end poss
)
2302 "Check replacements for doubled characters.
2304 If the text between BEG and END is equal to a correction suggested by
2305 Ispell, after removing a pair of doubled characters, correct the text,
2308 The third arg POSS is either the symbol `doublon' or a list of
2309 possible corrections as returned by `ispell-parse-output'.
2311 This function is meant to be added to `flyspell-incorrect-hook'."
2314 (let ((str (buffer-substring beg end
))
2315 (i 0) (len (- end beg
)))
2316 (while (< (1+ i
) len
)
2317 (when (and (= (aref str i
) (aref str
(1+ i
)))
2318 (member (concat (substring str
0 (1+ i
))
2319 (substring str
(+ i
2)))
2321 (goto-char (+ beg i
))
2327 ;;*---------------------------------------------------------------------*/
2328 ;;* flyspell-already-abbrevp ... */
2329 ;;*---------------------------------------------------------------------*/
2330 (defun flyspell-already-abbrevp (table word
)
2331 (let ((sym (abbrev-symbol word table
)))
2332 (and sym
(symbolp sym
))))
2334 ;;*---------------------------------------------------------------------*/
2335 ;;* flyspell-change-abbrev ... */
2336 ;;*---------------------------------------------------------------------*/
2337 (defun flyspell-change-abbrev (table old new
)
2338 (set (abbrev-symbol old table
) new
))
2342 ;;; flyspell.el ends here