1 ;;; flyspell.el --- On-the-fly spell checker -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998, 2000-2017 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 (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."
1107 (interactive (list ispell-following-word
))
1108 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1110 ;; use the correct dictionary
1111 (flyspell-accept-buffer-local-defs)
1112 (let* ((cursor-location (point))
1113 (flyspell-word (flyspell-get-word following
))
1114 start end poss word ispell-filter
)
1115 (if (or (eq flyspell-word nil
)
1116 (and (functionp flyspell-generic-check-word-predicate
)
1117 (not (funcall flyspell-generic-check-word-predicate
))))
1120 ;; destructure return flyspell-word info list.
1121 (setq start
(car (cdr flyspell-word
))
1122 end
(car (cdr (cdr flyspell-word
)))
1123 word
(car flyspell-word
))
1124 ;; before checking in the directory, we check for doublons.
1126 ((and (or (not (eq ispell-parser
'tex
))
1127 (and (> start
(point-min))
1128 (not (memq (char-after (1- start
)) '(?\
} ?
\\)))))
1129 flyspell-mark-duplications-flag
1130 (not (catch 'exception
1131 (let ((dict (or ispell-local-dictionary
1132 ispell-dictionary
)))
1133 (dolist (except flyspell-mark-duplications-exceptions
)
1134 (and (or (null (car except
))
1136 (string-match (car except
) dict
)))
1137 (member (downcase word
) (cdr except
))
1138 (throw 'exception t
))))))
1145 (skip-chars-backward " \t\n\f")))))
1146 (p (when (>= bound
(point-min))
1147 (flyspell-word-search-backward word bound t
))))
1148 (and p
(/= p start
)))))
1149 ;; yes, this is a doublon
1150 (flyspell-highlight-incorrect-region start end
'doublon
)
1152 ((and (eq flyspell-word-cache-start start
)
1153 (eq flyspell-word-cache-end end
)
1154 (string-equal flyspell-word-cache-word word
))
1155 ;; this word had been already checked, we skip
1156 flyspell-word-cache-result
)
1157 ((and (eq ispell-parser
'tex
)
1158 (flyspell-tex-command-p flyspell-word
))
1159 ;; this is a correct word (because a tex command)
1160 (flyspell-unhighlight-at start
)
1162 (flyspell-unhighlight-at (- end
1)))
1165 ;; we setup the cache
1166 (setq flyspell-word-cache-start start
)
1167 (setq flyspell-word-cache-end end
)
1168 (setq flyspell-word-cache-word word
)
1169 ;; now check spelling of word.
1170 (if (not known-misspelling
)
1172 (ispell-send-string "%\n")
1173 ;; put in verbose mode
1174 (ispell-send-string (concat "^" word
"\n"))
1175 ;; we mark the ispell process so it can be killed
1176 ;; when emacs is exited without query
1177 (set-process-query-on-exit-flag ispell-process nil
)
1178 ;; Wait until ispell has processed word.
1180 (accept-process-output ispell-process
)
1181 (not (string= "" (car ispell-filter
)))))
1182 ;; (ispell-send-string "!\n")
1183 ;; back to terse mode.
1184 ;; Remove leading empty element
1185 (setq ispell-filter
(cdr ispell-filter
))
1186 ;; ispell process should return something after word is sent.
1187 ;; Tag word as valid (i.e., skip) otherwise
1189 (setq ispell-filter
'(*)))
1190 (if (consp ispell-filter
)
1191 (setq poss
(ispell-parse-output (car ispell-filter
)))))
1192 ;; Else, this was a known misspelling to begin with, and
1193 ;; we should forge an ispell return value.
1194 (setq poss
(list word
1 nil nil
)))
1195 (let ((res (cond ((eq poss t
)
1197 (setq flyspell-word-cache-result t
)
1198 (flyspell-unhighlight-at start
)
1200 (flyspell-unhighlight-at (- end
1)))
1202 ((and (stringp poss
) flyspell-highlight-flag
)
1204 (setq flyspell-word-cache-result t
)
1205 (flyspell-unhighlight-at start
)
1207 (flyspell-unhighlight-at (- end
1)))
1210 (setq flyspell-word-cache-result t
)
1211 (flyspell-unhighlight-at start
)
1213 (flyspell-unhighlight-at (- end
1)))
1215 ((or (and (< flyspell-duplicate-distance
0)
1218 (flyspell-word-search-backward
1223 (flyspell-word-search-forward
1226 (and (> flyspell-duplicate-distance
0)
1229 (flyspell-word-search-backward
1232 flyspell-duplicate-distance
)))
1235 (flyspell-word-search-forward
1238 flyspell-duplicate-distance
))))))
1239 ;; This is a misspelled word which occurs
1240 ;; twice within flyspell-duplicate-distance.
1241 (setq flyspell-word-cache-result nil
)
1242 (if flyspell-highlight-flag
1243 (flyspell-highlight-duplicate-region
1245 (message "duplicate `%s'" word
))
1248 (setq flyspell-word-cache-result nil
)
1249 ;; Highlight the location as incorrect,
1250 ;; including offset specified in POSS.
1251 (if flyspell-highlight-flag
1252 (flyspell-highlight-incorrect-region
1253 (if (and (consp poss
)
1254 (integerp (nth 1 poss
)))
1255 (+ start
(nth 1 poss
) -
1)
1258 (flyspell-notify-misspell word poss
))
1260 ;; return to original location
1261 (goto-char cursor-location
)
1262 (if ispell-quit
(setq ispell-quit nil
))
1265 ;;*---------------------------------------------------------------------*/
1266 ;;* flyspell-math-tex-command-p ... */
1267 ;;* ------------------------------------------------------------- */
1268 ;;* This function uses the texmathp package to check if point */
1269 ;;* is within a TeX math environment. `texmathp' can yield errors */
1270 ;;* if the document is currently not valid TeX syntax. */
1271 ;;*---------------------------------------------------------------------*/
1272 (defun flyspell-math-tex-command-p ()
1273 (when (fboundp 'texmathp
)
1274 (if flyspell-check-tex-math-command
1280 ;;*---------------------------------------------------------------------*/
1281 ;;* flyspell-tex-command-p ... */
1282 ;;*---------------------------------------------------------------------*/
1283 (defun flyspell-tex-command-p (word)
1284 "Return t if WORD is a TeX command."
1286 (let ((b (car (cdr word
))))
1287 (and (re-search-backward "\\\\" (- (point) 100) t
)
1288 (or (= (match-end 0) b
)
1289 (and (goto-char (match-end 0))
1290 (looking-at flyspell-tex-command-regexp
)
1291 (>= (match-end 0) b
))))))
1292 (flyspell-math-tex-command-p)))
1294 (defalias 'flyspell-get-casechars
'ispell-get-casechars
)
1295 (defalias 'flyspell-get-not-casechars
'ispell-get-not-casechars
)
1297 ;;*---------------------------------------------------------------------*/
1298 ;;* flyspell-get-word ... */
1299 ;;*---------------------------------------------------------------------*/
1300 (defun flyspell-get-word (&optional following extra-otherchars
)
1301 "Return the word for spell-checking according to Ispell syntax.
1302 Optional argument FOLLOWING non-nil means to get the following
1303 \(rather than preceding) word when the cursor is not over a word.
1304 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1305 that may be included as part of a word (see `ispell-dictionary-alist')."
1306 (let* ((flyspell-casechars (flyspell-get-casechars))
1307 (flyspell-not-casechars (flyspell-get-not-casechars))
1308 (ispell-otherchars (ispell-get-otherchars))
1309 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1310 (word-regexp (concat flyspell-casechars
1312 (if (not (string= "" ispell-otherchars
))
1313 (concat ispell-otherchars
"?"))
1314 (if extra-otherchars
1315 (concat extra-otherchars
"?"))
1318 (if (or ispell-many-otherchars-p
1324 (if (not (looking-at flyspell-casechars
))
1326 (re-search-forward flyspell-casechars nil t
)
1327 (re-search-backward flyspell-casechars nil t
)))
1328 ;; move to front of word
1329 (re-search-backward flyspell-not-casechars nil
'start
)
1330 (while (and (or (and (not (string= "" ispell-otherchars
))
1331 (looking-at ispell-otherchars
))
1332 (and extra-otherchars
(looking-at extra-otherchars
)))
1334 (or (not did-it-once
)
1335 ispell-many-otherchars-p
)
1336 (not (eq prevpt
(point))))
1337 (if (and extra-otherchars
(looking-at extra-otherchars
))
1340 (if (looking-at flyspell-casechars
)
1341 (re-search-backward flyspell-not-casechars nil
'move
)))
1345 (if (looking-at flyspell-casechars
)
1346 (re-search-backward flyspell-not-casechars nil
'move
)
1347 (backward-char -
1))))
1348 ;; Now mark the word and save to string.
1349 (if (not (re-search-forward word-regexp nil t
))
1352 (setq start
(match-beginning 0)
1354 word
(buffer-substring-no-properties start end
))
1355 (list word start end
)))))
1357 ;;*---------------------------------------------------------------------*/
1358 ;;* flyspell-small-region ... */
1359 ;;*---------------------------------------------------------------------*/
1360 (defun flyspell-small-region (beg end
)
1361 "Flyspell text between BEG and END."
1369 (while (< (point) end
)
1370 (if (and flyspell-issue-message-flag
(= count
100))
1372 (message "Spell Checking...%d%%"
1373 (floor (* 100.0 (- (point) beg
)) (- end beg
)))
1375 (setq count
(+ 1 count
)))
1378 (let ((cur (point)))
1380 (if (and (< (point) end
) (> (point) (+ cur
1)))
1381 (backward-char 1)))))
1383 (if flyspell-issue-message-flag
(message "Spell Checking completed."))
1386 ;;*---------------------------------------------------------------------*/
1387 ;;* flyspell-external-ispell-process ... */
1388 ;;*---------------------------------------------------------------------*/
1389 (defvar flyspell-external-ispell-process
'()
1390 "The external Flyspell Ispell process.")
1392 ;;*---------------------------------------------------------------------*/
1393 ;;* flyspell-external-ispell-buffer ... */
1394 ;;*---------------------------------------------------------------------*/
1395 (defvar flyspell-external-ispell-buffer
'())
1396 (defvar flyspell-large-region-buffer
'())
1397 (defvar flyspell-large-region-beg
(point-min))
1398 (defvar flyspell-large-region-end
(point-max))
1400 ;;*---------------------------------------------------------------------*/
1401 ;;* flyspell-external-point-words ... */
1402 ;;*---------------------------------------------------------------------*/
1403 (defun flyspell-external-point-words ()
1404 "Mark words from a buffer listing incorrect words in order of appearance.
1405 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1406 \(We finish by killing that buffer and setting the variable to nil.)
1407 The buffer to mark them in is `flyspell-large-region-buffer'."
1408 (let (words-not-found
1409 (ispell-otherchars (ispell-get-otherchars))
1410 (buffer-scan-pos flyspell-large-region-beg
)
1412 (with-current-buffer flyspell-external-ispell-buffer
1413 (goto-char (point-min))
1414 ;; Loop over incorrect words, in the order they were reported,
1415 ;; which is also the order they appear in the buffer being checked.
1416 (while (re-search-forward "\\([^\n]+\\)\n" nil t
)
1417 ;; Bind WORD to the next one.
1418 (let ((word (match-string 1)) (wordpos (point)))
1419 ;; Here there used to be code to see if WORD is the same
1420 ;; as the previous iteration, and count the number of consecutive
1421 ;; identical words, and the loop below would search for that many.
1422 ;; That code seemed to be incorrect, and on principle, should
1423 ;; be unnecessary too. -- rms.
1424 (if flyspell-issue-message-flag
1425 (message "Spell Checking...%d%% [%s]"
1426 (floor (* 100.0 (point)) (point-max))
1428 (with-current-buffer flyspell-large-region-buffer
1429 (goto-char buffer-scan-pos
)
1431 ;; Iterate on string search until string is found as word,
1432 ;; not as substring.
1434 (if (search-forward word
1435 flyspell-large-region-end t
)
1438 ;; Move back into the match
1439 ;; so flyspell-get-word will find it.
1441 (flyspell-get-word)))
1442 (found (car found-list
))
1443 (found-length (length found
))
1444 (misspell-length (length word
)))
1446 ;; Size matches, we really found it.
1447 (= found-length misspell-length
)
1448 ;; Matches as part of a boundary-char separated
1451 (split-string found ispell-otherchars
))
1452 ;; Misspelling has higher length than
1453 ;; what flyspell considers the word.
1454 ;; Caused by boundary-chars mismatch.
1455 ;; Validating seems safe.
1456 (< found-length misspell-length
)
1457 ;; ispell treats beginning of some TeX
1458 ;; commands as nroff control sequences
1459 ;; and strips them in the list of
1460 ;; misspelled words thus giving a
1461 ;; non-existent word. Skip if ispell
1462 ;; is used, string is a TeX command
1463 ;; (char before beginning of word is
1464 ;; backslash) and none of the previous
1465 ;; conditions match.
1466 (and (not ispell-really-aspell
)
1468 (goto-char (- (nth 1 found-list
) 1))
1469 (if (looking-at "[\\]" )
1473 (flyspell-word nil t
)
1474 ;; Search for next misspelled word will begin from
1475 ;; end of last validated match.
1476 (setq buffer-scan-pos
(point))))
1477 ;; Record if misspelling is not found and try new one
1478 (cl-pushnew (concat " -> " word
" - "
1479 (int-to-string wordpos
))
1480 words-not-found
:test
#'equal
)
1481 (setq keep nil
)))))))
1483 (if flyspell-issue-message-flag
(message "Spell Checking completed.")))
1484 ;; Warn about not found misspellings
1485 (dolist (word words-not-found
)
1486 (message "%s: word not found" word
))
1487 ;; Kill and forget the buffer with the list of incorrect words.
1488 (kill-buffer flyspell-external-ispell-buffer
)
1489 (setq flyspell-external-ispell-buffer nil
)))
1491 ;;*---------------------------------------------------------------------*/
1492 ;;* flyspell-process-localwords ... */
1493 ;;* ------------------------------------------------------------- */
1494 ;;* This function is used to prevent marking of words explicitly */
1495 ;;* declared correct. */
1496 ;;*---------------------------------------------------------------------*/
1497 (defun flyspell-process-localwords (misspellings-buffer)
1498 (let ((localwords ispell-buffer-session-localwords
)
1500 (ispell-casechars (ispell-get-casechars)))
1501 ;; Get localwords from the original buffer
1503 (goto-char (point-min))
1504 ;; Localwords parsing copied from ispell.el.
1505 (while (search-forward ispell-words-keyword nil t
)
1506 (let ((end (point-at-eol))
1508 ;; buffer-local words separated by a space, and can contain
1509 ;; any character other than a space. Not rigorous enough.
1510 (while (re-search-forward " *\\([^ ]+\\)" end t
)
1511 (setq string
(buffer-substring-no-properties (match-beginning 1)
1513 ;; This can fail when string contains a word with invalid chars.
1514 ;; Error handling needs to be added between Ispell and Emacs.
1515 (if (and (< 1 (length string
))
1516 (equal 0 (string-match ispell-casechars string
)))
1517 (push string localwords
))))))
1518 ;; Remove localwords matches from misspellings-buffer.
1519 ;; The usual mechanism of communicating the local words to ispell
1520 ;; does not affect the special ispell process used by
1521 ;; flyspell-large-region.
1522 (with-current-buffer misspellings-buffer
1524 (dolist (word localwords
)
1525 (goto-char (point-min))
1526 (let ((regexp (concat "^" word
"\n")))
1527 (while (re-search-forward regexp nil t
)
1528 (delete-region (match-beginning 0) (match-end 0)))))))))
1530 ;;* ---------------------------------------------------------------
1531 ;;* flyspell-check-region-doublons
1532 ;;* ---------------------------------------------------------------
1533 (defun flyspell-check-region-doublons (beg end
)
1534 "Check for adjacent duplicated words (doublons) in the given region."
1537 (flyspell-word) ; Make sure current word is checked
1539 (while (and (< (point) end
)
1540 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1546 ;;*---------------------------------------------------------------------*/
1547 ;;* flyspell-large-region ... */
1548 ;;*---------------------------------------------------------------------*/
1549 (defun flyspell-large-region (beg end
)
1550 (let* ((curbuf (current-buffer))
1551 (buffer (get-buffer-create "*flyspell-region*")))
1552 (setq flyspell-external-ispell-buffer buffer
)
1553 (setq flyspell-large-region-buffer curbuf
)
1554 (setq flyspell-large-region-beg beg
)
1555 (setq flyspell-large-region-end end
)
1556 (flyspell-accept-buffer-local-defs)
1559 ;; this is done, we can start checking...
1560 (if flyspell-issue-message-flag
(message "Checking region..."))
1562 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1563 ;; Local dictionary becomes the global dictionary in use.
1564 (setq ispell-current-dictionary
1565 (or ispell-local-dictionary ispell-dictionary
))
1566 (setq ispell-current-personal-dictionary
1567 (or ispell-local-pdict ispell-personal-dictionary
))
1568 (let ((args (ispell-get-ispell-args))
1569 (encoding (ispell-get-coding-system))
1571 (if (and ispell-current-dictionary
; use specified dictionary
1572 (not (member "-d" args
))) ; only define if not overridden
1574 (append (list "-d" ispell-current-dictionary
) args
)))
1575 (if ispell-current-personal-dictionary
; use specified pers dict
1580 ispell-current-personal-dictionary
)))))
1582 ;; Check for extended character mode
1583 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1584 (and extended-char-mode
; ~ extended character mode
1585 (string-match "[^~]+$" extended-char-mode
)
1586 (cl-pushnew (concat "-T" (match-string 0 extended-char-mode
))
1587 args
:test
#'equal
)))
1589 ;; Add ispell-extra-args
1590 (setq args
(append args ispell-extra-args
))
1592 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1593 ;; for communication. ispell or older aspell/hunspell does not support this
1594 (if ispell-encoding8-command
1597 (if ispell-really-hunspell
1598 (list ispell-encoding8-command
1599 (upcase (symbol-name encoding
)))
1600 (list (concat ispell-encoding8-command
1601 (symbol-name encoding
)))))))
1603 (let ((process-coding-system-alist (list (cons "\\.*" encoding
))))
1604 (setq c
(apply 'ispell-call-process-region beg
1610 (if ispell-really-aspell
"list" "-l")
1614 (flyspell-process-localwords buffer
)
1615 (with-current-buffer curbuf
1616 (flyspell-delete-region-overlays beg end
)
1617 (flyspell-check-region-doublons beg end
))
1618 (flyspell-external-point-words))
1619 (error "Can't check region")))))
1621 ;;*---------------------------------------------------------------------*/
1622 ;;* flyspell-region ... */
1623 ;;* ------------------------------------------------------------- */
1624 ;;* Because `ispell -a' is too slow, it is not possible to use */
1625 ;;* it on large region. Then, when ispell is invoked on a large */
1626 ;;* text region, a new `ispell -l' process is spawned. The */
1627 ;;* pointed out words are then searched in the region a checked with */
1628 ;;* regular flyspell means. */
1629 ;;*---------------------------------------------------------------------*/
1631 (defun flyspell-region (beg end
)
1632 "Flyspell text between BEG and END."
1634 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1642 (if (and flyspell-large-region
(> (- end beg
) flyspell-large-region
))
1643 (flyspell-large-region beg end
)
1644 (flyspell-small-region beg end
)))))
1646 ;;*---------------------------------------------------------------------*/
1647 ;;* flyspell-buffer ... */
1648 ;;*---------------------------------------------------------------------*/
1650 (defun flyspell-buffer ()
1651 "Flyspell whole buffer."
1653 (flyspell-region (point-min) (point-max)))
1655 ;;*---------------------------------------------------------------------*/
1656 ;;* old next error position ... */
1657 ;;*---------------------------------------------------------------------*/
1658 (defvar flyspell-old-buffer-error nil
)
1659 (defvar flyspell-old-pos-error nil
)
1661 ;;*---------------------------------------------------------------------*/
1662 ;;* flyspell-goto-next-error ... */
1663 ;;*---------------------------------------------------------------------*/
1664 (defun flyspell-goto-next-error ()
1665 "Go to the next previously detected error.
1666 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1671 (if (and (eq (current-buffer) flyspell-old-buffer-error
)
1672 (eq pos flyspell-old-pos-error
))
1674 (if (= flyspell-old-pos-error max
)
1675 ;; goto beginning of buffer
1677 (message "Restarting from beginning of buffer")
1678 (goto-char (point-min)))
1680 (setq pos
(point))))
1681 ;; seek the next error
1682 (while (and (< pos max
)
1683 (let ((ovs (overlays-at pos
))
1685 (while (and (not r
) (consp ovs
))
1686 (if (flyspell-overlay-p (car ovs
))
1688 (setq ovs
(cdr ovs
))))
1690 (setq pos
(1+ pos
)))
1691 ;; save the current location for next invocation
1692 (setq flyspell-old-pos-error pos
)
1693 (setq flyspell-old-buffer-error
(current-buffer))
1696 (message "No more miss-spelled word!"))))
1698 ;;*---------------------------------------------------------------------*/
1699 ;;* flyspell-overlay-p ... */
1700 ;;*---------------------------------------------------------------------*/
1701 (defun flyspell-overlay-p (o)
1702 "Return true if O is an overlay used by flyspell."
1703 (and (overlayp o
) (overlay-get o
'flyspell-overlay
)))
1705 ;;*---------------------------------------------------------------------*/
1706 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1707 ;;* ------------------------------------------------------------- */
1708 ;;* Remove overlays introduced by flyspell. */
1709 ;;*---------------------------------------------------------------------*/
1710 (defun flyspell-delete-region-overlays (beg end
)
1711 "Delete overlays used by flyspell in a given region."
1712 (remove-overlays beg end
'flyspell-overlay t
))
1714 (defun flyspell-delete-all-overlays ()
1715 "Delete all the overlays used by flyspell."
1716 (flyspell-delete-region-overlays (point-min) (point-max)))
1718 ;;*---------------------------------------------------------------------*/
1719 ;;* flyspell-unhighlight-at ... */
1720 ;;*---------------------------------------------------------------------*/
1721 (defun flyspell-unhighlight-at (pos)
1722 "Remove the flyspell overlay that are located at POS."
1723 (if flyspell-persistent-highlight
1724 (let ((overlays (overlays-at pos
)))
1725 (while (consp overlays
)
1726 (if (flyspell-overlay-p (car overlays
))
1727 (delete-overlay (car overlays
)))
1728 (setq overlays
(cdr overlays
))))
1729 (if (flyspell-overlay-p flyspell-overlay
)
1730 (delete-overlay flyspell-overlay
))))
1732 ;;*---------------------------------------------------------------------*/
1733 ;;* flyspell-properties-at-p ... */
1734 ;;* ------------------------------------------------------------- */
1735 ;;* Is there an highlight properties at position pos? */
1736 ;;*---------------------------------------------------------------------*/
1737 (defun flyspell-properties-at-p (pos)
1738 "Return t if there is a text property at POS, not counting `local-map'.
1739 If variable `flyspell-highlight-properties' is set to nil,
1740 text with properties are not checked. This function is used to discover
1741 if the character at POS has any other property."
1742 (let ((prop (text-properties-at pos
))
1744 (while (and keep
(consp prop
))
1745 (if (and (eq (car prop
) 'local-map
) (consp (cdr prop
)))
1746 (setq prop
(cdr (cdr prop
)))
1750 ;;*---------------------------------------------------------------------*/
1751 ;;* make-flyspell-overlay ... */
1752 ;;*---------------------------------------------------------------------*/
1753 (defun make-flyspell-overlay (beg end face mouse-face
)
1754 "Allocate an overlay to highlight an incorrect word.
1755 BEG and END specify the range in the buffer of that word.
1756 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1758 (let ((overlay (make-overlay beg end nil t nil
)))
1759 (overlay-put overlay
'face face
)
1760 (overlay-put overlay
'mouse-face mouse-face
)
1761 (overlay-put overlay
'flyspell-overlay t
)
1762 (overlay-put overlay
'evaporate t
)
1763 (overlay-put overlay
'help-echo
"mouse-2: correct word at point")
1764 ;; If misspelled text has a 'keymap' property, let that remain in
1765 ;; effect for the bindings that flyspell-mouse-map doesn't override.
1766 (set-keymap-parent flyspell-mouse-map
(get-char-property beg
'keymap
))
1767 (overlay-put overlay
'keymap flyspell-mouse-map
)
1768 (when (eq face
'flyspell-incorrect
)
1769 (and (stringp flyspell-before-incorrect-word-string
)
1770 (overlay-put overlay
'before-string
1771 flyspell-before-incorrect-word-string
))
1772 (and (stringp flyspell-after-incorrect-word-string
)
1773 (overlay-put overlay
'after-string
1774 flyspell-after-incorrect-word-string
)))
1777 ;;*---------------------------------------------------------------------*/
1778 ;;* flyspell-highlight-incorrect-region ... */
1779 ;;*---------------------------------------------------------------------*/
1780 (defun flyspell-highlight-incorrect-region (beg end poss
)
1781 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1782 POSS is usually a list of possible spelling/correction lists,
1783 as returned by `ispell-parse-output'.
1784 It can also be the symbol `doublon', in the case where the word
1785 is itself incorrect, but suspiciously repeated."
1786 (let ((inhibit-read-only t
))
1787 (unless (run-hook-with-args-until-success
1788 'flyspell-incorrect-hook beg end poss
)
1789 (if (or flyspell-highlight-properties
1790 (not (flyspell-properties-at-p beg
)))
1792 ;; we cleanup all the overlay that are in the region, not
1793 ;; beginning at the word start position
1794 (if (< (1+ beg
) end
)
1795 (let ((os (overlays-in (1+ beg
) end
)))
1797 (if (flyspell-overlay-p (car os
))
1798 (delete-overlay (car os
)))
1799 (setq os
(cdr os
)))))
1800 ;; we cleanup current overlay at the same position
1801 (flyspell-unhighlight-at beg
)
1802 ;; now we can use a new overlay
1803 (setq flyspell-overlay
1804 (make-flyspell-overlay
1806 (if (eq poss
'doublon
) 'flyspell-duplicate
'flyspell-incorrect
)
1809 ;;*---------------------------------------------------------------------*/
1810 ;;* flyspell-highlight-duplicate-region ... */
1811 ;;*---------------------------------------------------------------------*/
1812 (defun flyspell-highlight-duplicate-region (beg end poss
)
1813 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1814 POSS is a list of possible spelling/correction lists,
1815 as returned by `ispell-parse-output'."
1816 (let ((inhibit-read-only t
))
1817 (unless (run-hook-with-args-until-success
1818 'flyspell-incorrect-hook beg end poss
)
1819 (if (or flyspell-highlight-properties
1820 (not (flyspell-properties-at-p beg
)))
1822 ;; we cleanup current overlay at the same position
1823 (flyspell-unhighlight-at beg
)
1824 ;; now we can use a new overlay
1825 (setq flyspell-overlay
1826 (make-flyspell-overlay beg end
1830 ;;*---------------------------------------------------------------------*/
1831 ;;* flyspell-auto-correct-cache ... */
1832 ;;*---------------------------------------------------------------------*/
1833 (defvar flyspell-auto-correct-pos nil
)
1834 (defvar flyspell-auto-correct-region nil
)
1835 (defvar flyspell-auto-correct-ring nil
)
1836 (defvar flyspell-auto-correct-word nil
)
1837 (make-variable-buffer-local 'flyspell-auto-correct-pos
)
1838 (make-variable-buffer-local 'flyspell-auto-correct-region
)
1839 (make-variable-buffer-local 'flyspell-auto-correct-ring
)
1840 (make-variable-buffer-local 'flyspell-auto-correct-word
)
1842 ;;*---------------------------------------------------------------------*/
1843 ;;* flyspell-check-previous-highlighted-word ... */
1844 ;;*---------------------------------------------------------------------*/
1845 (defun flyspell-check-previous-highlighted-word (&optional arg
)
1846 "Correct the closest previous word that is highlighted as misspelled.
1847 This function scans for a word which starts before point that has been
1848 highlighted by Flyspell as misspelled. If it finds one, it proposes
1849 a replacement for that word. With prefix arg N, check the Nth word
1850 before point that's highlighted as misspelled."
1852 (let ((pos1 (point))
1854 (arg (if (or (not (numberp arg
)) (< arg
1)) 1 arg
))
1857 (while (and (setq pos
(previous-overlay-change pos
))
1860 (if (> pos
(point-min))
1862 (setq ovs
(overlays-at pos
))
1865 (setq ovs
(cdr ovs
))
1866 (if (and (flyspell-overlay-p ov
)
1867 (= 0 (setq arg
(1- arg
))))
1868 (throw 'exit t
)))))))
1872 (setq flyspell-word-cache-word nil
) ;; Force flyspell-word re-check
1874 (error "No word to correct before point"))))
1876 ;;*---------------------------------------------------------------------*/
1877 ;;* flyspell-display-next-corrections ... */
1878 ;;*---------------------------------------------------------------------*/
1879 (defun flyspell-display-next-corrections (corrections)
1880 (let ((string "Corrections:")
1883 (while (< (length string
) 80)
1884 (if (equal (car l
) flyspell-auto-correct-word
)
1885 (setq pos
(cons (+ 1 (length string
)) pos
)))
1886 (setq string
(concat string
" " (car l
)))
1889 (let ((num (car pos
)))
1890 (put-text-property num
1891 (+ num
(length flyspell-auto-correct-word
))
1892 'face
'flyspell-incorrect
1894 (setq pos
(cdr pos
)))
1895 (if (fboundp 'display-message
)
1896 (display-message 'no-log string
)
1897 (message "%s" string
))))
1899 ;;*---------------------------------------------------------------------*/
1900 ;;* flyspell-abbrev-table ... */
1901 ;;*---------------------------------------------------------------------*/
1902 (defun flyspell-abbrev-table ()
1903 (if flyspell-use-global-abbrev-table-p
1905 (or local-abbrev-table global-abbrev-table
)))
1907 ;;*---------------------------------------------------------------------*/
1908 ;;* flyspell-define-abbrev ... */
1909 ;;*---------------------------------------------------------------------*/
1910 (defun flyspell-define-abbrev (name expansion
)
1911 (let ((table (flyspell-abbrev-table)))
1913 (define-abbrev table
(downcase name
) expansion
))))
1915 ;;*---------------------------------------------------------------------*/
1916 ;;* flyspell-auto-correct-word ... */
1917 ;;*---------------------------------------------------------------------*/
1918 (defun flyspell-auto-correct-word ()
1919 "Correct the current word.
1920 This command proposes various successive corrections for the current word."
1922 ;; If we are not in the construct where flyspell should be active,
1923 ;; invoke the original binding of M-TAB, if that was recorded.
1924 (if (and (local-variable-p 'flyspell--prev-meta-tab-binding
)
1925 (commandp flyspell--prev-meta-tab-binding t
)
1926 (functionp flyspell-generic-check-word-predicate
)
1927 (not (funcall flyspell-generic-check-word-predicate
))
1928 (equal (where-is-internal 'flyspell-auto-correct-word nil t
)
1930 (call-interactively flyspell--prev-meta-tab-binding
)
1932 (old-max (point-max)))
1933 ;; Use the correct dictionary.
1934 (flyspell-accept-buffer-local-defs)
1935 (if (and (eq flyspell-auto-correct-pos pos
)
1936 (consp flyspell-auto-correct-region
))
1937 ;; We have already been using the function at the same location.
1938 (let* ((start (car flyspell-auto-correct-region
))
1939 (len (cdr flyspell-auto-correct-region
)))
1940 (flyspell-unhighlight-at start
)
1941 (delete-region start
(+ start len
))
1942 (setq flyspell-auto-correct-ring
(cdr flyspell-auto-correct-ring
))
1943 (let* ((word (car flyspell-auto-correct-ring
))
1944 (len (length word
)))
1945 (rplacd flyspell-auto-correct-region len
)
1947 (if flyspell-abbrev-p
1948 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1949 flyspell-auto-correct-word
)
1950 (flyspell-change-abbrev (flyspell-abbrev-table)
1951 flyspell-auto-correct-word
1953 (flyspell-define-abbrev flyspell-auto-correct-word word
)))
1954 (funcall flyspell-insert-function word
)
1956 (flyspell-display-next-corrections flyspell-auto-correct-ring
))
1957 (flyspell-adjust-cursor-point pos
(point) old-max
)
1958 (setq flyspell-auto-correct-pos
(point)))
1959 ;; Fetch the word to be checked.
1960 (let ((word (flyspell-get-word)))
1962 (let ((start (car (cdr word
)))
1963 (end (car (cdr (cdr word
))))
1966 (setq flyspell-auto-correct-word word
)
1967 ;; Now check spelling of word..
1968 (ispell-send-string "%\n") ;Put in verbose mode.
1969 (ispell-send-string (concat "^" word
"\n"))
1970 ;; Wait until ispell has processed word.
1972 (accept-process-output ispell-process
)
1973 (not (string= "" (car ispell-filter
)))))
1974 ;; Remove leading empty element.
1975 (setq ispell-filter
(cdr ispell-filter
))
1976 ;; Ispell process should return something after word is sent.
1977 ;; Tag word as valid (i.e., skip) otherwise.
1979 (setq ispell-filter
'(*)))
1980 (if (consp ispell-filter
)
1981 (setq poss
(ispell-parse-output (car ispell-filter
))))
1983 ((or (eq poss t
) (stringp poss
))
1984 ;; Don't correct word.
1988 (error "Ispell: error in Ispell process"))
1990 ;; The word is incorrect, we have to propose a replacement.
1991 (let ((replacements (flyspell-sort (car (cdr (cdr poss
)))
1993 (setq flyspell-auto-correct-region nil
)
1994 (if (consp replacements
)
1996 (let ((replace (car replacements
)))
1997 (let ((new-word replace
))
1998 (if (not (equal new-word
(car poss
)))
2000 ;; the save the current replacements
2001 (setq flyspell-auto-correct-region
2002 (cons start
(length new-word
)))
2003 (let ((l replacements
))
2004 (while (consp (cdr l
))
2006 (rplacd l
(cons (car poss
) replacements
)))
2007 (setq flyspell-auto-correct-ring
2009 (flyspell-unhighlight-at start
)
2010 (delete-region start end
)
2011 (funcall flyspell-insert-function new-word
)
2012 (if flyspell-abbrev-p
2013 (if (flyspell-already-abbrevp
2014 (flyspell-abbrev-table) word
)
2015 (flyspell-change-abbrev
2016 (flyspell-abbrev-table)
2019 (flyspell-define-abbrev word
2022 (flyspell-display-next-corrections
2023 (cons new-word flyspell-auto-correct-ring
))
2024 (flyspell-adjust-cursor-point pos
2027 (setq flyspell-auto-correct-pos
(point))
2028 (ispell-pdict-save t
))))))))
2030 ;;*---------------------------------------------------------------------*/
2031 ;;* flyspell-auto-correct-previous-pos ... */
2032 ;;*---------------------------------------------------------------------*/
2033 (defvar flyspell-auto-correct-previous-pos nil
2034 "Holds the start of the first incorrect word before point.")
2036 ;;*---------------------------------------------------------------------*/
2037 ;;* flyspell-auto-correct-previous-hook ... */
2038 ;;*---------------------------------------------------------------------*/
2039 (defun flyspell-auto-correct-previous-hook ()
2040 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
2041 Sets `flyspell-auto-correct-previous-pos' to nil"
2043 (remove-hook 'pre-command-hook
(function flyspell-auto-correct-previous-hook
) t
)
2044 (unless (eq this-command
(function flyspell-auto-correct-previous-word
))
2045 (setq flyspell-auto-correct-previous-pos nil
)))
2047 ;;*---------------------------------------------------------------------*/
2048 ;;* flyspell-auto-correct-previous-word ... */
2049 ;;*---------------------------------------------------------------------*/
2050 (defun flyspell-auto-correct-previous-word (position)
2051 "Auto correct the first misspelled word that occurs before point.
2052 But don't look beyond what's visible on the screen."
2055 (let ((top (window-start))
2059 (narrow-to-region top bot
)
2060 (overlay-recenter (point))
2062 (add-hook 'pre-command-hook
2063 (function flyspell-auto-correct-previous-hook
) t t
)
2065 (unless flyspell-auto-correct-previous-pos
2066 ;; only reset if a new overlay exists
2067 (setq flyspell-auto-correct-previous-pos nil
)
2069 (let ((overlay-list (overlays-in (point-min) position
))
2070 (new-overlay 'dummy-value
))
2072 ;; search for previous (new) flyspell overlay
2073 (while (and new-overlay
2074 (or (not (flyspell-overlay-p new-overlay
))
2075 ;; check if its face has changed
2076 (not (eq (get-char-property
2077 (overlay-start new-overlay
) 'face
)
2078 'flyspell-incorrect
))))
2079 (setq new-overlay
(car-safe overlay-list
))
2080 (setq overlay-list
(cdr-safe overlay-list
)))
2082 ;; if nothing new exits new-overlay should be nil
2083 (if new-overlay
;; the length of the word may change so go to the start
2084 (setq flyspell-auto-correct-previous-pos
2085 (overlay-start new-overlay
)))))
2087 (when flyspell-auto-correct-previous-pos
2089 (goto-char flyspell-auto-correct-previous-pos
)
2090 (let ((ispell-following-word t
)) ;; point is at start
2091 (if (numberp flyspell-auto-correct-previous-pos
)
2092 (goto-char flyspell-auto-correct-previous-pos
))
2093 (flyspell-auto-correct-word))
2094 ;; the point may have moved so reset this
2095 (setq flyspell-auto-correct-previous-pos
(point))))))))
2097 ;;*---------------------------------------------------------------------*/
2098 ;;* flyspell-correct-word ... */
2099 ;;*---------------------------------------------------------------------*/
2101 (defun flyspell-correct-word (event)
2102 "Pop up a menu of possible corrections for a misspelled word.
2103 The word checked is the word at the mouse position."
2105 (let ((save (point)))
2106 (mouse-set-point event
)
2107 (flyspell-correct-word-before-point event save
)))
2109 (defun flyspell-correct-word-before-point (&optional event opoint
)
2110 "Pop up a menu of possible corrections for misspelled word before point.
2111 If EVENT is non-nil, it is the mouse event that invoked this operation;
2112 that controls where to put the menu.
2113 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2115 ;; use the correct dictionary
2116 (flyspell-accept-buffer-local-defs)
2117 (or opoint
(setq opoint
(point)))
2118 (let ((cursor-location (point))
2119 (word (flyspell-get-word)))
2121 (let ((start (car (cdr word
)))
2122 (end (car (cdr (cdr word
))))
2125 ;; now check spelling of word.
2126 (ispell-send-string "%\n") ;put in verbose mode
2127 (ispell-send-string (concat "^" word
"\n"))
2128 ;; wait until ispell has processed word
2130 (accept-process-output ispell-process
)
2131 (not (string= "" (car ispell-filter
)))))
2132 ;; Remove leading empty element
2133 (setq ispell-filter
(cdr ispell-filter
))
2134 ;; ispell process should return something after word is sent.
2135 ;; Tag word as valid (i.e., skip) otherwise
2137 (setq ispell-filter
'(*)))
2138 (if (consp ispell-filter
)
2139 (setq poss
(ispell-parse-output (car ispell-filter
))))
2141 ((or (eq poss t
) (stringp poss
))
2142 ;; don't correct word
2146 (error "Ispell: error in Ispell process"))
2148 ;; The word is incorrect, we have to propose a replacement.
2149 (flyspell-do-correct (flyspell-emacs-popup event poss word
)
2150 poss word cursor-location start end opoint
)))
2151 (ispell-pdict-save t
)))))
2153 ;;*---------------------------------------------------------------------*/
2154 ;;* flyspell-do-correct ... */
2155 ;;*---------------------------------------------------------------------*/
2156 (defun flyspell-do-correct (replace poss word cursor-location start end save
)
2157 "The popup menu callback."
2158 (cond ((eq replace
'ignore
)
2163 (ispell-send-string (concat "*" word
"\n"))
2164 (ispell-send-string "#\n")
2165 (flyspell-unhighlight-at cursor-location
)
2166 (setq ispell-pdict-modified-p
'(t)))
2167 ((or (eq replace
'buffer
) (eq replace
'session
))
2168 (ispell-send-string (concat "@" word
"\n"))
2169 (add-to-list 'ispell-buffer-session-localwords word
)
2170 (or ispell-buffer-local-name
; session localwords might conflict
2171 (setq ispell-buffer-local-name
(buffer-name)))
2172 (flyspell-unhighlight-at cursor-location
)
2173 (if (null ispell-pdict-modified-p
)
2174 (setq ispell-pdict-modified-p
2175 (list ispell-pdict-modified-p
)))
2177 (if (eq replace
'buffer
)
2178 (ispell-add-per-file-word-list word
)))
2180 (flyspell-unhighlight-at cursor-location
)
2181 (let ((old-max (point-max))
2182 (new-word (if (atom replace
)
2185 (cursor-location (+ (- (length word
) (- end start
))
2187 (unless (equal new-word
(car poss
))
2188 (delete-region start end
)
2190 (funcall flyspell-insert-function new-word
)
2191 (if flyspell-abbrev-p
2192 (flyspell-define-abbrev word new-word
)))
2193 (flyspell-adjust-cursor-point save cursor-location old-max
)))
2198 ;;*---------------------------------------------------------------------*/
2199 ;;* flyspell-adjust-cursor-point ... */
2200 ;;*---------------------------------------------------------------------*/
2201 (defun flyspell-adjust-cursor-point (save cursor-location old-max
)
2202 (if (>= save cursor-location
)
2203 (let ((new-pos (+ save
(- (point-max) old-max
))))
2205 ((< new-pos
(point-min))
2207 ((> new-pos
(point-max))
2212 ;;*---------------------------------------------------------------------*/
2213 ;;* flyspell-emacs-popup ... */
2214 ;;*---------------------------------------------------------------------*/
2215 (defun flyspell-emacs-popup (event poss word
)
2216 "The Emacs popup menu."
2217 (if (and (not event
)
2219 (let* ((mouse-pos (mouse-position))
2220 (mouse-pos (if (nth 1 mouse-pos
)
2222 (set-mouse-position (car mouse-pos
)
2223 (/ (frame-width) 2) 2)
2225 (setq event
(list (list (car (cdr mouse-pos
))
2226 (1+ (cdr (cdr mouse-pos
))))
2228 (let* ((corrects (flyspell-sort (car (cdr (cdr poss
))) word
))
2229 (cor-menu (if (consp corrects
)
2230 (mapcar (lambda (correct)
2231 (list correct correct
))
2234 (affix (car (cdr (cdr (cdr poss
)))))
2236 (base-menu (let ((save (if (and (consp affix
) show-affix-info
)
2238 (list (concat "Save affix: " (car affix
))
2240 '("Accept (session)" session
)
2241 '("Accept (buffer)" buffer
))
2242 '(("Save word" save
)
2243 ("Accept (session)" session
)
2244 ("Accept (buffer)" buffer
)))))
2245 (if (consp cor-menu
)
2246 (append cor-menu
(cons "" save
))
2248 (menu (cons "flyspell correction menu" base-menu
)))
2249 (car (x-popup-menu event
2250 (list (format "%s [%s]" word
(or ispell-local-dictionary
2254 ;;*---------------------------------------------------------------------*/
2255 ;;* Some example functions for real autocorrecting */
2256 ;;*---------------------------------------------------------------------*/
2257 (defun flyspell-maybe-correct-transposition (beg end poss
)
2258 "Check replacements for transposed characters.
2260 If the text between BEG and END is equal to a correction suggested by
2261 Ispell, after transposing two adjacent characters, correct the text,
2264 The third arg POSS is either the symbol `doublon' or a list of
2265 possible corrections as returned by `ispell-parse-output'.
2267 This function is meant to be added to `flyspell-incorrect-hook'."
2270 (let ((str (buffer-substring beg end
))
2271 (i 0) (len (- end beg
)) tmp
)
2272 (while (< (1+ i
) len
)
2273 (setq tmp
(aref str i
))
2274 (aset str i
(aref str
(1+ i
)))
2275 (aset str
(1+ i
) tmp
)
2276 (when (member str
(nth 2 poss
))
2278 (goto-char (+ beg i
1))
2279 (transpose-chars 1))
2281 (setq tmp
(aref str i
))
2282 (aset str i
(aref str
(1+ i
)))
2283 (aset str
(1+ i
) tmp
)
2287 (defun flyspell-maybe-correct-doubling (beg end poss
)
2288 "Check replacements for doubled characters.
2290 If the text between BEG and END is equal to a correction suggested by
2291 Ispell, after removing a pair of doubled characters, correct the text,
2294 The third arg POSS is either the symbol `doublon' or a list of
2295 possible corrections as returned by `ispell-parse-output'.
2297 This function is meant to be added to `flyspell-incorrect-hook'."
2300 (let ((str (buffer-substring beg end
))
2301 (i 0) (len (- end beg
)))
2302 (while (< (1+ i
) len
)
2303 (when (and (= (aref str i
) (aref str
(1+ i
)))
2304 (member (concat (substring str
0 (1+ i
))
2305 (substring str
(+ i
2)))
2307 (goto-char (+ beg i
))
2313 ;;*---------------------------------------------------------------------*/
2314 ;;* flyspell-already-abbrevp ... */
2315 ;;*---------------------------------------------------------------------*/
2316 (defun flyspell-already-abbrevp (table word
)
2317 (let ((sym (abbrev-symbol word table
)))
2318 (and sym
(symbolp sym
))))
2320 ;;*---------------------------------------------------------------------*/
2321 ;;* flyspell-change-abbrev ... */
2322 ;;*---------------------------------------------------------------------*/
2323 (defun flyspell-change-abbrev (table old new
)
2324 (set (abbrev-symbol old table
) new
))
2328 ;;; flyspell.el ends here