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
451 "Keymap for Flyspell to put on erroneous words.")
453 (defvar flyspell-mode-map
454 (let ((map (make-sparse-keymap)))
455 (if flyspell-use-meta-tab
456 (define-key map
"\M-\t" 'flyspell-auto-correct-word
))
457 (define-key map flyspell-auto-correct-binding
'flyspell-auto-correct-previous-word
)
458 (define-key map
[(control ?\
,)] 'flyspell-goto-next-error
)
459 (define-key map
[(control ?\.
)] 'flyspell-auto-correct-word
)
460 (define-key map
[?\C-c ?$
] 'flyspell-correct-word-before-point
)
462 "Minor mode keymap for Flyspell mode--for the whole buffer.")
464 ;; dash character machinery
465 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
466 "Non-nil means that the `-' char is considered as a word delimiter.")
467 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag
)
468 (defvar flyspell-dash-dictionary nil
)
469 (make-variable-buffer-local 'flyspell-dash-dictionary
)
470 (defvar flyspell-dash-local-dictionary nil
)
471 (make-variable-buffer-local 'flyspell-dash-local-dictionary
)
473 ;;*---------------------------------------------------------------------*/
475 ;;*---------------------------------------------------------------------*/
476 (defface flyspell-incorrect
477 '((((supports :underline
(:style wave
)))
478 :underline
(:style wave
:color
"Red1"))
480 :underline t
:inherit error
))
481 "Flyspell face for misspelled words."
485 (defface flyspell-duplicate
486 '((((supports :underline
(:style wave
)))
487 :underline
(:style wave
:color
"DarkOrange"))
489 :underline t
:inherit warning
))
490 "Flyspell face for words that appear twice in a row.
491 See also `flyspell-duplicate-distance'."
495 (defvar flyspell-overlay nil
)
497 ;;*---------------------------------------------------------------------*/
498 ;;* flyspell-mode ... */
499 ;;*---------------------------------------------------------------------*/
500 ;;;###autoload(defvar flyspell-mode nil "Non-nil if Flyspell mode is enabled.")
502 (define-minor-mode flyspell-mode
503 "Toggle on-the-fly spell checking (Flyspell mode).
504 With a prefix argument ARG, enable Flyspell mode if ARG is
505 positive, and disable it otherwise. If called from Lisp, enable
506 the mode if ARG is omitted or nil.
508 Flyspell mode is a buffer-local minor mode. When enabled, it
509 spawns a single Ispell process and checks each word. The default
510 flyspell behavior is to highlight incorrect words.
513 \\[ispell-word]: correct words (using Ispell).
514 \\[flyspell-auto-correct-word]: automatically correct word.
515 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
516 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
519 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
522 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
523 valid. For instance, a different dictionary can be used by
524 invoking `ispell-change-dictionary'.
526 Consider using the `ispell-parser' to check your text. For instance
528 \(add-hook \\='tex-mode-hook (function (lambda () (setq ispell-parser \\='tex))))
531 \\[flyspell-region] checks all words inside a region.
532 \\[flyspell-buffer] checks the whole buffer."
533 :lighter flyspell-mode-line-string
534 :keymap flyspell-mode-map
539 (error (message "Error enabling Flyspell mode:\n%s" (cdr err
))
541 (flyspell-mode-off)))
544 (defun turn-on-flyspell ()
545 "Unconditionally turn on Flyspell mode."
549 (defun turn-off-flyspell ()
550 "Unconditionally turn off Flyspell mode."
553 (custom-add-option 'text-mode-hook
'turn-on-flyspell
)
555 ;;*---------------------------------------------------------------------*/
556 ;;* flyspell-buffers ... */
557 ;;* ------------------------------------------------------------- */
558 ;;* For remembering buffers running flyspell */
559 ;;*---------------------------------------------------------------------*/
560 (defvar flyspell-buffers nil
)
562 ;;*---------------------------------------------------------------------*/
563 ;;* flyspell-minibuffer-p ... */
564 ;;*---------------------------------------------------------------------*/
565 (defun flyspell-minibuffer-p (buffer)
566 "Is BUFFER a minibuffer?"
567 (let ((ws (get-buffer-window-list buffer t
)))
568 (and (consp ws
) (window-minibuffer-p (car ws
)))))
570 ;;*---------------------------------------------------------------------*/
571 ;;* flyspell-accept-buffer-local-defs ... */
572 ;;*---------------------------------------------------------------------*/
573 (defvar flyspell-last-buffer nil
574 "The buffer in which the last flyspell operation took place.")
576 (defun flyspell-accept-buffer-local-defs (&optional force
)
577 ;; When flyspell-word is used inside a loop (e.g. when processing
578 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
579 ;; up dwarfing everything else, so only do it when the buffer has changed.
580 (when (or force
(not (eq flyspell-last-buffer
(current-buffer))))
581 (setq flyspell-last-buffer
(current-buffer))
582 ;; Strange problem: If buffer in current window has font-lock turned on,
583 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
584 ;; call will reset the buffer to the buffer in the current window.
585 ;; However, it only happens at startup (fix by Albert L. Ting).
587 (ispell-accept-buffer-local-defs))
588 (unless (and (eq flyspell-dash-dictionary ispell-dictionary
)
589 (eq flyspell-dash-local-dictionary ispell-local-dictionary
))
590 ;; The dictionary has changed
591 (setq flyspell-dash-dictionary ispell-dictionary
)
592 (setq flyspell-dash-local-dictionary ispell-local-dictionary
)
593 (setq flyspell-consider-dash-as-word-delimiter-flag
594 (member (or ispell-local-dictionary ispell-dictionary
)
595 flyspell-dictionaries-that-consider-dash-as-word-delimiter
)))))
597 (defun flyspell-hack-local-variables-hook ()
598 ;; When local variables are loaded, see if the dictionary context
600 (flyspell-accept-buffer-local-defs 'force
))
602 (defun flyspell-kill-ispell-hook ()
603 (setq flyspell-last-buffer nil
)
604 (dolist (buf (buffer-list))
605 (with-current-buffer buf
606 (kill-local-variable 'flyspell-word-cache-word
))))
608 ;; Make sure we flush our caches when needed. Do it here rather than in
609 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
611 (add-hook 'ispell-kill-ispell-hook
'flyspell-kill-ispell-hook
)
613 ;;*---------------------------------------------------------------------*/
614 ;;* flyspell-mode-on ... */
615 ;;*---------------------------------------------------------------------*/
616 (defun flyspell-mode-on ()
617 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
618 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
619 (setq ispell-highlight-face
'flyspell-incorrect
)
620 ;; local dictionaries setup
621 (or ispell-local-dictionary ispell-dictionary
622 (if flyspell-default-dictionary
623 (ispell-change-dictionary flyspell-default-dictionary
)))
624 ;; we have to force ispell to accept the local definition or
625 ;; otherwise it could be too late, the local dictionary may
627 ;; Pass the `force' argument for the case where flyspell was active already
628 ;; but the buffer's local-defs have been edited.
629 (flyspell-accept-buffer-local-defs 'force
)
630 ;; we put the `flyspell-delayed' property on some commands
631 (flyspell-delay-commands)
632 ;; we put the `flyspell-deplacement' property on some commands
633 (flyspell-deplacement-commands)
634 ;; we bound flyspell action to post-command hook
635 (add-hook 'post-command-hook
(function flyspell-post-command-hook
) t t
)
636 ;; we bound flyspell action to pre-command hook
637 (add-hook 'pre-command-hook
(function flyspell-pre-command-hook
) t t
)
638 ;; we bound flyspell action to after-change hook
639 (add-hook 'after-change-functions
'flyspell-after-change-function nil t
)
640 ;; we bound flyspell action to hack-local-variables-hook
641 (add-hook 'hack-local-variables-hook
642 (function flyspell-hack-local-variables-hook
) t t
)
643 ;; set flyspell-generic-check-word-predicate based on the major mode
644 (let ((mode-predicate (get major-mode
'flyspell-mode-predicate
)))
646 (setq flyspell-generic-check-word-predicate mode-predicate
)))
647 ;; the welcome message
648 (if (and flyspell-issue-message-flag
649 flyspell-issue-welcome-flag
650 (called-interactively-p 'interactive
))
651 (let ((binding (where-is-internal 'flyspell-auto-correct-word
655 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
656 (key-description binding
))
657 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
659 ;;*---------------------------------------------------------------------*/
660 ;;* flyspell-delay-commands ... */
661 ;;*---------------------------------------------------------------------*/
662 (defun flyspell-delay-commands ()
663 "Install the standard set of Flyspell delayed commands."
664 (mapc 'flyspell-delay-command flyspell-default-delayed-commands
)
665 (mapc 'flyspell-delay-command flyspell-delayed-commands
))
667 ;;*---------------------------------------------------------------------*/
668 ;;* flyspell-delay-command ... */
669 ;;*---------------------------------------------------------------------*/
670 (defun flyspell-delay-command (command)
671 "Set COMMAND to be delayed, for Flyspell.
672 When flyspell `post-command-hook' is invoked because a delayed command
673 has been used, the current word is not immediately checked.
674 It will be checked only after `flyspell-delay' seconds."
675 (interactive "SDelay Flyspell after Command: ")
676 (put command
'flyspell-delayed t
))
678 ;;*---------------------------------------------------------------------*/
679 ;;* flyspell-deplacement-commands ... */
680 ;;*---------------------------------------------------------------------*/
681 (defun flyspell-deplacement-commands ()
682 "Install the standard set of Flyspell deplacement commands."
683 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands
)
684 (mapc 'flyspell-deplacement-command flyspell-deplacement-commands
))
686 ;;*---------------------------------------------------------------------*/
687 ;;* flyspell-deplacement-command ... */
688 ;;*---------------------------------------------------------------------*/
689 (defun flyspell-deplacement-command (command)
690 "Set COMMAND that implement cursor movements, for Flyspell.
691 When flyspell `post-command-hook' is invoked because a deplacement command
692 has been used, the current word is not checked."
693 (interactive "SDeplacement Flyspell after Command: ")
694 (put command
'flyspell-deplacement t
))
696 ;;*---------------------------------------------------------------------*/
697 ;;* flyspell-word-cache ... */
698 ;;*---------------------------------------------------------------------*/
699 (defvar flyspell-word-cache-start nil
)
700 (defvar flyspell-word-cache-end nil
)
701 (defvar flyspell-word-cache-word nil
)
702 (defvar flyspell-word-cache-result
'_
)
703 (make-variable-buffer-local 'flyspell-word-cache-start
)
704 (make-variable-buffer-local 'flyspell-word-cache-end
)
705 (make-variable-buffer-local 'flyspell-word-cache-word
)
706 (make-variable-buffer-local 'flyspell-word-cache-result
)
708 ;;*---------------------------------------------------------------------*/
709 ;;* The flyspell pre-hook, store the current position. In the */
710 ;;* post command hook, we will check, if the word at this position */
711 ;;* has to be spell checked. */
712 ;;*---------------------------------------------------------------------*/
713 (defvar flyspell-pre-buffer nil
"Buffer current before `this-command'.")
714 (defvar flyspell-pre-point nil
"Point before running `this-command'")
715 (defvar flyspell-pre-column nil
"Column before running `this-command'")
716 (defvar flyspell-pre-pre-buffer nil
)
717 (defvar flyspell-pre-pre-point nil
)
718 (make-variable-buffer-local 'flyspell-pre-point
) ;Why?? --Stef
720 ;;*---------------------------------------------------------------------*/
721 ;;* flyspell-previous-command ... */
722 ;;*---------------------------------------------------------------------*/
723 (defvar flyspell-previous-command nil
724 "The last interactive command checked by Flyspell.")
726 ;;*---------------------------------------------------------------------*/
727 ;;* flyspell-pre-command-hook ... */
728 ;;*---------------------------------------------------------------------*/
729 (defun flyspell-pre-command-hook ()
730 "Save the current buffer and point for Flyspell's post-command hook."
732 (setq flyspell-pre-buffer
(current-buffer))
733 (setq flyspell-pre-point
(point))
734 (setq flyspell-pre-column
(current-column)))
736 ;;*---------------------------------------------------------------------*/
737 ;;* flyspell-mode-off ... */
738 ;;*---------------------------------------------------------------------*/
740 (defun flyspell-mode-off ()
741 "Turn Flyspell mode off."
742 ;; We remove the hooks.
743 (remove-hook 'post-command-hook
(function flyspell-post-command-hook
) t
)
744 (remove-hook 'pre-command-hook
(function flyspell-pre-command-hook
) t
)
745 (remove-hook 'after-change-functions
'flyspell-after-change-function t
)
746 (remove-hook 'hack-local-variables-hook
747 (function flyspell-hack-local-variables-hook
) t
)
748 ;; We remove all the flyspell highlightings.
749 (flyspell-delete-all-overlays)
750 ;; We have to erase pre cache variables.
751 (setq flyspell-pre-buffer nil
)
752 (setq flyspell-pre-point nil
)
753 ;; We mark the mode as killed.
754 (setq flyspell-mode nil
))
756 ;;*---------------------------------------------------------------------*/
757 ;;* flyspell-check-pre-word-p ... */
758 ;;*---------------------------------------------------------------------*/
759 (defun flyspell-check-pre-word-p ()
760 "Return non-nil if we should check the word before point.
761 More precisely, it applies to the word that was before point
762 before the current command."
763 (let ((ispell-otherchars (ispell-get-otherchars)))
765 ((not (and (numberp flyspell-pre-point
)
766 (eq flyspell-pre-buffer
(current-buffer))))
768 ((and (eq flyspell-pre-pre-point flyspell-pre-point
)
769 (eq flyspell-pre-pre-buffer flyspell-pre-buffer
))
771 ((or (and (= flyspell-pre-point
(- (point) 1))
772 (or (eq (char-syntax (char-after flyspell-pre-point
)) ?w
)
773 (and (not (string= "" ispell-otherchars
))
776 (buffer-substring-no-properties
777 flyspell-pre-point
(1+ flyspell-pre-point
))))))
778 (= flyspell-pre-point
(point))
779 (= flyspell-pre-point
(+ (point) 1)))
781 ((and (symbolp this-command
)
782 (not executing-kbd-macro
)
783 (or (get this-command
'flyspell-delayed
)
784 (and (get this-command
'flyspell-deplacement
)
785 (eq flyspell-previous-command this-command
)))
786 (or (= (current-column) 0)
787 (= (current-column) flyspell-pre-column
)
788 ;; If other post-command-hooks change the buffer,
789 ;; flyspell-pre-point can lie past eob (bug#468).
790 (null (char-after flyspell-pre-point
))
791 (or (eq (char-syntax (char-after flyspell-pre-point
)) ?w
)
792 (and (not (string= "" ispell-otherchars
))
795 (buffer-substring-no-properties
796 flyspell-pre-point
(1+ flyspell-pre-point
)))))))
798 ((not (eq (current-buffer) flyspell-pre-buffer
))
800 ((not (and (numberp flyspell-word-cache-start
)
801 (numberp flyspell-word-cache-end
)))
804 (or (< flyspell-pre-point flyspell-word-cache-start
)
805 (> flyspell-pre-point flyspell-word-cache-end
))))))
807 ;;*---------------------------------------------------------------------*/
808 ;;* The flyspell after-change-hook, store the change position. In */
809 ;;* the post command hook, we will check, if the word at this */
810 ;;* position has to be spell checked. */
811 ;;*---------------------------------------------------------------------*/
812 (defvar flyspell-changes nil
)
813 (make-variable-buffer-local 'flyspell-changes
)
815 ;;*---------------------------------------------------------------------*/
816 ;;* flyspell-after-change-function ... */
817 ;;*---------------------------------------------------------------------*/
818 (defun flyspell-after-change-function (start stop _len
)
819 "Save the current buffer and point for Flyspell's post-command hook."
820 (push (cons start stop
) flyspell-changes
))
822 ;;*---------------------------------------------------------------------*/
823 ;;* flyspell-check-changed-word-p ... */
824 ;;*---------------------------------------------------------------------*/
825 (defun flyspell-check-changed-word-p (start stop
)
826 "Return non-nil when the changed word has to be checked.
827 The answer depends of several criteria.
828 Mostly we check word delimiters."
829 (not (and (not (and (memq (char-after start
) '(?
\n ?
)) (> stop start
)))
830 (numberp flyspell-pre-point
)
832 (and (>= flyspell-pre-point start
) (<= flyspell-pre-point stop
))
834 (or (>= pos start
) (<= pos stop
) (= pos
(1+ stop
))))))))
836 ;;*---------------------------------------------------------------------*/
837 ;;* flyspell-check-word-p ... */
838 ;;*---------------------------------------------------------------------*/
839 (defun flyspell-check-word-p ()
840 "Return t when the word at `point' has to be checked.
841 The answer depends of several criteria.
842 Mostly we check word delimiters."
843 (let ((ispell-otherchars (ispell-get-otherchars)))
845 ((<= (- (point-max) 1) (point-min))
846 ;; The buffer is not filled enough.
848 ((and (and (> (current-column) 0)
849 (not (eq (current-column) flyspell-pre-column
)))
852 (and (looking-at (flyspell-get-not-casechars))
853 (or (string= "" ispell-otherchars
)
854 (not (looking-at ispell-otherchars
)))
855 (or flyspell-consider-dash-as-word-delimiter-flag
856 (not (looking-at "-"))))))
857 ;; Yes because we have reached or typed a word delimiter.
859 ((symbolp this-command
)
861 ((get this-command
'flyspell-deplacement
)
862 (not (eq flyspell-previous-command this-command
)))
863 ((get this-command
'flyspell-delayed
)
864 ;; The current command is not delayed, that
865 ;; is that we must check the word now.
866 (and (not unread-command-events
)
867 (sit-for flyspell-delay
)))
871 ;;*---------------------------------------------------------------------*/
872 ;;* flyspell-debug-signal-no-check ... */
873 ;;*---------------------------------------------------------------------*/
874 (defun flyspell-debug-signal-no-check (msg obj
)
875 (setq debug-on-error t
)
876 (with-current-buffer (get-buffer-create "*flyspell-debug*")
878 (insert "NO-CHECK:\n")
879 (insert (format " %S : %S\n" msg obj
))))
881 ;;*---------------------------------------------------------------------*/
882 ;;* flyspell-debug-signal-pre-word-checked ... */
883 ;;*---------------------------------------------------------------------*/
884 (defun flyspell-debug-signal-pre-word-checked ()
885 (setq debug-on-error t
)
886 (with-current-buffer (get-buffer-create "*flyspell-debug*")
887 (insert "PRE-WORD:\n")
888 (insert (format " pre-point : %S\n" flyspell-pre-point
))
889 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer
))
890 (insert (format " cache-start: %S\n" flyspell-word-cache-start
))
891 (insert (format " cache-end : %S\n" flyspell-word-cache-end
))
892 (goto-char (point-max))))
894 ;;*---------------------------------------------------------------------*/
895 ;;* flyspell-debug-signal-word-checked ... */
896 ;;*---------------------------------------------------------------------*/
897 (defun flyspell-debug-signal-word-checked ()
898 (setq debug-on-error t
)
899 (let ((ispell-otherchars (ispell-get-otherchars))
900 (oldbuf (current-buffer))
902 (with-current-buffer (get-buffer-create "*flyspell-debug*")
905 (format " this-cmd : %S\n" this-command
)
906 (format " delayed : %S\n" (and (symbolp this-command
)
909 (format " point : %S\n" point
)
910 (format " prev-char : [%c] %S\n"
911 (with-current-buffer oldbuf
912 (if (bobp) ?\
(char-before)))
913 (with-current-buffer oldbuf
918 (and (looking-at (flyspell-get-not-casechars))
919 (or (string= "" ispell-otherchars
)
920 (not (looking-at ispell-otherchars
)))
921 (or flyspell-consider-dash-as-word-delimiter-flag
922 (not (looking-at "\\-")))
924 (format " because : %S\n"
926 ((not (and (symbolp this-command
)
927 (get this-command
'flyspell-delayed
)))
928 ;; The current command is not delayed, that
929 ;; is that we must check the word now.
931 ((with-current-buffer oldbuf
936 (and (looking-at (flyspell-get-not-casechars))
937 (or (string= "" ispell-otherchars
)
938 (not (looking-at ispell-otherchars
)))
939 (or flyspell-consider-dash-as-word-delimiter-flag
940 (not (looking-at "\\-")))))))
941 ;; Yes because we have reached or typed a word delimiter.
943 ((not (integerp flyspell-delay
))
944 ;; Yes because the user set up a no-delay configuration.
948 (goto-char (point-max)))))
950 ;;*---------------------------------------------------------------------*/
951 ;;* flyspell-debug-signal-changed-checked ... */
952 ;;*---------------------------------------------------------------------*/
953 (defun flyspell-debug-signal-changed-checked ()
954 (setq debug-on-error t
)
955 (let ((point (point)))
956 (with-current-buffer (get-buffer-create "*flyspell-debug*")
957 (insert "CHANGED WORD:\n")
958 (insert (format " point : %S\n" point
))
959 (goto-char (point-max)))))
961 ;;*---------------------------------------------------------------------*/
962 ;;* flyspell-post-command-hook ... */
963 ;;* ------------------------------------------------------------- */
964 ;;* It is possible that we check several words: */
965 ;;* 1- the current word is checked if the predicate */
966 ;;* FLYSPELL-CHECK-WORD-P is true */
967 ;;* 2- the word that used to be the current word before the */
968 ;;* THIS-COMMAND is checked if: */
969 ;;* a- the previous word is different from the current word */
970 ;;* b- the previous word has not just been checked by the */
971 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
972 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
973 ;;* previous word nor the current word */
974 ;;*---------------------------------------------------------------------*/
975 (defun flyspell-post-command-hook ()
976 "The `post-command-hook' used by flyspell to check a word on-the-fly."
980 (let ((command this-command
)
981 ;; Prevent anything we do from affecting the mark.
983 (if (flyspell-check-pre-word-p)
985 '(flyspell-debug-signal-pre-word-checked)
986 (goto-char flyspell-pre-point
)
988 (if (flyspell-check-word-p)
990 '(flyspell-debug-signal-word-checked)
991 ;; FIXME: This should be asynchronous!
993 ;; we remember which word we have just checked.
994 ;; this will be used next time we will check a word
995 ;; to compare the next current word with the word
996 ;; that has been registered in the pre-command-hook
997 ;; that is these variables are used within the predicate
998 ;; FLYSPELL-CHECK-PRE-WORD-P
999 (setq flyspell-pre-pre-buffer
(current-buffer))
1000 (setq flyspell-pre-pre-point
(point)))
1001 (setq flyspell-pre-pre-buffer nil
)
1002 (setq flyspell-pre-pre-point nil
)
1003 ;; when a word is not checked because of a delayed command
1004 ;; we do not disable the ispell cache.
1005 (when (and (symbolp this-command
)
1006 (get this-command
'flyspell-delayed
))
1007 (setq flyspell-word-cache-end -
1)
1008 (setq flyspell-word-cache-result
'_
)))
1009 (while (and (not (input-pending-p)) (consp flyspell-changes
))
1010 (let ((start (car (car flyspell-changes
)))
1011 (stop (cdr (car flyspell-changes
))))
1012 (if (flyspell-check-changed-word-p start stop
)
1014 '(flyspell-debug-signal-changed-checked)
1017 (setq flyspell-changes
(cdr flyspell-changes
))))
1018 (setq flyspell-previous-command command
)))))
1020 ;;*---------------------------------------------------------------------*/
1021 ;;* flyspell-notify-misspell ... */
1022 ;;*---------------------------------------------------------------------*/
1023 (defun flyspell-notify-misspell (word poss
)
1024 (let ((replacements (if (stringp poss
)
1026 (flyspell-sort (car (cdr (cdr poss
))) word
))))
1027 (if flyspell-issue-message-flag
1028 (message "misspelling `%s' %S" word replacements
))))
1030 ;;*---------------------------------------------------------------------*/
1031 ;;* flyspell-word-search-backward ... */
1032 ;;*---------------------------------------------------------------------*/
1033 (defun flyspell-word-search-backward (word bound
&optional ignore-case
)
1036 (inhibit-point-motion-hooks t
)
1037 (flyspell-not-casechars (flyspell-get-not-casechars))
1038 (bound (if (and bound
1039 (> bound
(point-min)))
1042 "\\(?:" flyspell-not-casechars
"\\|\\`\\)"
1044 flyspell-not-casechars
))
1050 (re-search-backward word-re bound t
)
1055 (let ((lw (flyspell-get-word)))
1058 (string-equal (downcase (car lw
)) (downcase word
))
1059 (string-equal (car lw
) word
)))
1064 ;;*---------------------------------------------------------------------*/
1065 ;;* flyspell-word-search-forward ... */
1066 ;;*---------------------------------------------------------------------*/
1067 (defun flyspell-word-search-forward (word bound
)
1070 (inhibit-point-motion-hooks t
)
1071 (flyspell-not-casechars (flyspell-get-not-casechars))
1072 (bound (if (and bound
1073 (< bound
(point-max)))
1075 (word-re (concat flyspell-not-casechars
1077 "\\(?:" flyspell-not-casechars
"\\|\\'\\)"))
1082 (re-search-forward word-re bound t
)
1087 (let ((lw (flyspell-get-word)))
1088 (if (and (consp lw
) (string-equal (car lw
) word
))
1090 (goto-char (1+ p
)))))
1093 (defvar flyspell-word
) ;Backward compatibility; some predicates made use of it!
1095 ;;*---------------------------------------------------------------------*/
1096 ;;* flyspell-word ... */
1097 ;;*---------------------------------------------------------------------*/
1098 (defun flyspell-word (&optional following known-misspelling
)
1099 "Spell check a word.
1100 If the optional argument FOLLOWING, or, when called interactively
1101 `ispell-following-word', is non-nil, checks the following (rather
1102 than preceding) word when the cursor is not over a word. If
1103 optional argument KNOWN-MISSPELLING is non nil considers word a
1104 misspelling and skips redundant spell-checking step."
1105 (interactive (list ispell-following-word
))
1106 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1108 ;; use the correct dictionary
1109 (flyspell-accept-buffer-local-defs)
1110 (let* ((cursor-location (point))
1111 (flyspell-word (flyspell-get-word following
))
1112 start end poss word ispell-filter
)
1113 (if (or (eq flyspell-word nil
)
1114 (and (functionp flyspell-generic-check-word-predicate
)
1115 (not (funcall flyspell-generic-check-word-predicate
))))
1118 ;; destructure return flyspell-word info list.
1119 (setq start
(car (cdr flyspell-word
))
1120 end
(car (cdr (cdr flyspell-word
)))
1121 word
(car flyspell-word
))
1122 ;; before checking in the directory, we check for doublons.
1124 ((and (or (not (eq ispell-parser
'tex
))
1125 (and (> start
(point-min))
1126 (not (memq (char-after (1- start
)) '(?\
} ?
\\)))))
1127 flyspell-mark-duplications-flag
1128 (not (catch 'exception
1129 (let ((dict (or ispell-local-dictionary
1130 ispell-dictionary
)))
1131 (dolist (except flyspell-mark-duplications-exceptions
)
1132 (and (or (null (car except
))
1134 (string-match (car except
) dict
)))
1135 (member (downcase word
) (cdr except
))
1136 (throw 'exception t
))))))
1143 (skip-chars-backward " \t\n\f")))))
1144 (p (when (>= bound
(point-min))
1145 (flyspell-word-search-backward word bound t
))))
1146 (and p
(/= p start
)))))
1147 ;; yes, this is a doublon
1148 (flyspell-highlight-incorrect-region start end
'doublon
)
1150 ((and (eq flyspell-word-cache-start start
)
1151 (eq flyspell-word-cache-end end
)
1152 (string-equal flyspell-word-cache-word word
))
1153 ;; this word had been already checked, we skip
1154 flyspell-word-cache-result
)
1155 ((and (eq ispell-parser
'tex
)
1156 (flyspell-tex-command-p flyspell-word
))
1157 ;; this is a correct word (because a tex command)
1158 (flyspell-unhighlight-at start
)
1160 (flyspell-unhighlight-at (- end
1)))
1163 ;; we setup the cache
1164 (setq flyspell-word-cache-start start
)
1165 (setq flyspell-word-cache-end end
)
1166 (setq flyspell-word-cache-word word
)
1167 ;; now check spelling of word.
1168 (if (not known-misspelling
)
1170 (ispell-send-string "%\n")
1171 ;; put in verbose mode
1172 (ispell-send-string (concat "^" word
"\n"))
1173 ;; we mark the ispell process so it can be killed
1174 ;; when emacs is exited without query
1175 (set-process-query-on-exit-flag ispell-process nil
)
1176 ;; Wait until ispell has processed word.
1178 (accept-process-output ispell-process
)
1179 (not (string= "" (car ispell-filter
)))))
1180 ;; (ispell-send-string "!\n")
1181 ;; back to terse mode.
1182 ;; Remove leading empty element
1183 (setq ispell-filter
(cdr ispell-filter
))
1184 ;; ispell process should return something after word is sent.
1185 ;; Tag word as valid (i.e., skip) otherwise
1187 (setq ispell-filter
'(*)))
1188 (if (consp ispell-filter
)
1189 (setq poss
(ispell-parse-output (car ispell-filter
)))))
1190 ;; Else, this was a known misspelling to begin with, and
1191 ;; we should forge an ispell return value.
1192 (setq poss
(list word
1 nil nil
)))
1193 (let ((res (cond ((eq poss t
)
1195 (setq flyspell-word-cache-result t
)
1196 (flyspell-unhighlight-at start
)
1198 (flyspell-unhighlight-at (- end
1)))
1200 ((and (stringp poss
) flyspell-highlight-flag
)
1202 (setq flyspell-word-cache-result t
)
1203 (flyspell-unhighlight-at start
)
1205 (flyspell-unhighlight-at (- end
1)))
1208 (setq flyspell-word-cache-result t
)
1209 (flyspell-unhighlight-at start
)
1211 (flyspell-unhighlight-at (- end
1)))
1213 ((or (and (< flyspell-duplicate-distance
0)
1216 (flyspell-word-search-backward
1221 (flyspell-word-search-forward
1224 (and (> flyspell-duplicate-distance
0)
1227 (flyspell-word-search-backward
1230 flyspell-duplicate-distance
)))
1233 (flyspell-word-search-forward
1236 flyspell-duplicate-distance
))))))
1237 ;; This is a misspelled word which occurs
1238 ;; twice within flyspell-duplicate-distance.
1239 (setq flyspell-word-cache-result nil
)
1240 (if flyspell-highlight-flag
1241 (flyspell-highlight-duplicate-region
1243 (message "duplicate `%s'" word
))
1246 (setq flyspell-word-cache-result nil
)
1247 ;; Highlight the location as incorrect,
1248 ;; including offset specified in POSS.
1249 (if flyspell-highlight-flag
1250 (flyspell-highlight-incorrect-region
1251 (if (and (consp poss
)
1252 (integerp (nth 1 poss
)))
1253 (+ start
(nth 1 poss
) -
1)
1256 (flyspell-notify-misspell word poss
))
1258 ;; return to original location
1259 (goto-char cursor-location
)
1260 (if ispell-quit
(setq ispell-quit nil
))
1263 ;;*---------------------------------------------------------------------*/
1264 ;;* flyspell-math-tex-command-p ... */
1265 ;;* ------------------------------------------------------------- */
1266 ;;* This function uses the texmathp package to check if point */
1267 ;;* is within a TeX math environment. `texmathp' can yield errors */
1268 ;;* if the document is currently not valid TeX syntax. */
1269 ;;*---------------------------------------------------------------------*/
1270 (defun flyspell-math-tex-command-p ()
1271 (when (fboundp 'texmathp
)
1272 (if flyspell-check-tex-math-command
1278 ;;*---------------------------------------------------------------------*/
1279 ;;* flyspell-tex-command-p ... */
1280 ;;*---------------------------------------------------------------------*/
1281 (defun flyspell-tex-command-p (word)
1282 "Return t if WORD is a TeX command."
1284 (let ((b (car (cdr word
))))
1285 (and (re-search-backward "\\\\" (- (point) 100) t
)
1286 (or (= (match-end 0) b
)
1287 (and (goto-char (match-end 0))
1288 (looking-at flyspell-tex-command-regexp
)
1289 (>= (match-end 0) b
))))))
1290 (flyspell-math-tex-command-p)))
1292 (defalias 'flyspell-get-casechars
'ispell-get-casechars
)
1293 (defalias 'flyspell-get-not-casechars
'ispell-get-not-casechars
)
1295 ;;*---------------------------------------------------------------------*/
1296 ;;* flyspell-get-word ... */
1297 ;;*---------------------------------------------------------------------*/
1298 (defun flyspell-get-word (&optional following extra-otherchars
)
1299 "Return the word for spell-checking according to Ispell syntax.
1300 Optional argument FOLLOWING non-nil means to get the following
1301 \(rather than preceding) word when the cursor is not over a word.
1302 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1303 that may be included as part of a word (see `ispell-dictionary-alist')."
1304 (let* ((flyspell-casechars (flyspell-get-casechars))
1305 (flyspell-not-casechars (flyspell-get-not-casechars))
1306 (ispell-otherchars (ispell-get-otherchars))
1307 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1308 (word-regexp (concat flyspell-casechars
1310 (if (not (string= "" ispell-otherchars
))
1311 (concat ispell-otherchars
"?"))
1312 (if extra-otherchars
1313 (concat extra-otherchars
"?"))
1316 (if (or ispell-many-otherchars-p
1322 (if (not (looking-at flyspell-casechars
))
1324 (re-search-forward flyspell-casechars nil t
)
1325 (re-search-backward flyspell-casechars nil t
)))
1326 ;; move to front of word
1327 (re-search-backward flyspell-not-casechars nil
'start
)
1328 (while (and (or (and (not (string= "" ispell-otherchars
))
1329 (looking-at ispell-otherchars
))
1330 (and extra-otherchars
(looking-at extra-otherchars
)))
1332 (or (not did-it-once
)
1333 ispell-many-otherchars-p
)
1334 (not (eq prevpt
(point))))
1335 (if (and extra-otherchars
(looking-at extra-otherchars
))
1338 (if (looking-at flyspell-casechars
)
1339 (re-search-backward flyspell-not-casechars nil
'move
)))
1343 (if (looking-at flyspell-casechars
)
1344 (re-search-backward flyspell-not-casechars nil
'move
)
1345 (backward-char -
1))))
1346 ;; Now mark the word and save to string.
1347 (if (not (re-search-forward word-regexp nil t
))
1350 (setq start
(match-beginning 0)
1352 word
(buffer-substring-no-properties start end
))
1353 (list word start end
)))))
1355 ;;*---------------------------------------------------------------------*/
1356 ;;* flyspell-small-region ... */
1357 ;;*---------------------------------------------------------------------*/
1358 (defun flyspell-small-region (beg end
)
1359 "Flyspell text between BEG and END."
1367 (while (< (point) end
)
1368 (if (and flyspell-issue-message-flag
(= count
100))
1370 (message "Spell Checking...%d%%"
1371 (floor (* 100.0 (- (point) beg
)) (- end beg
)))
1373 (setq count
(+ 1 count
)))
1376 (let ((cur (point)))
1378 (if (and (< (point) end
) (> (point) (+ cur
1)))
1379 (backward-char 1)))))
1381 (if flyspell-issue-message-flag
(message "Spell Checking completed."))
1384 ;;*---------------------------------------------------------------------*/
1385 ;;* flyspell-external-ispell-process ... */
1386 ;;*---------------------------------------------------------------------*/
1387 (defvar flyspell-external-ispell-process
'()
1388 "The external Flyspell Ispell process.")
1390 ;;*---------------------------------------------------------------------*/
1391 ;;* flyspell-external-ispell-buffer ... */
1392 ;;*---------------------------------------------------------------------*/
1393 (defvar flyspell-external-ispell-buffer
'())
1394 (defvar flyspell-large-region-buffer
'())
1395 (defvar flyspell-large-region-beg
(point-min))
1396 (defvar flyspell-large-region-end
(point-max))
1398 ;;*---------------------------------------------------------------------*/
1399 ;;* flyspell-external-point-words ... */
1400 ;;*---------------------------------------------------------------------*/
1401 (defun flyspell-external-point-words ()
1402 "Mark words from a buffer listing incorrect words in order of appearance.
1403 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1404 \(We finish by killing that buffer and setting the variable to nil.)
1405 The buffer to mark them in is `flyspell-large-region-buffer'."
1406 (let (words-not-found
1407 (ispell-otherchars (ispell-get-otherchars))
1408 (buffer-scan-pos flyspell-large-region-beg
)
1410 (with-current-buffer flyspell-external-ispell-buffer
1411 (goto-char (point-min))
1412 ;; Loop over incorrect words, in the order they were reported,
1413 ;; which is also the order they appear in the buffer being checked.
1414 (while (re-search-forward "\\([^\n]+\\)\n" nil t
)
1415 ;; Bind WORD to the next one.
1416 (let ((word (match-string 1)) (wordpos (point)))
1417 ;; Here there used to be code to see if WORD is the same
1418 ;; as the previous iteration, and count the number of consecutive
1419 ;; identical words, and the loop below would search for that many.
1420 ;; That code seemed to be incorrect, and on principle, should
1421 ;; be unnecessary too. -- rms.
1422 (if flyspell-issue-message-flag
1423 (message "Spell Checking...%d%% [%s]"
1424 (floor (* 100.0 (point)) (point-max))
1426 (with-current-buffer flyspell-large-region-buffer
1427 (goto-char buffer-scan-pos
)
1429 ;; Iterate on string search until string is found as word,
1430 ;; not as substring.
1432 (if (search-forward word
1433 flyspell-large-region-end t
)
1436 ;; Move back into the match
1437 ;; so flyspell-get-word will find it.
1439 (flyspell-get-word)))
1440 (found (car found-list
))
1441 (found-length (length found
))
1442 (misspell-length (length word
)))
1444 ;; Size matches, we really found it.
1445 (= found-length misspell-length
)
1446 ;; Matches as part of a boundary-char separated
1449 (split-string found ispell-otherchars
))
1450 ;; Misspelling has higher length than
1451 ;; what flyspell considers the word.
1452 ;; Caused by boundary-chars mismatch.
1453 ;; Validating seems safe.
1454 (< found-length misspell-length
)
1455 ;; ispell treats beginning of some TeX
1456 ;; commands as nroff control sequences
1457 ;; and strips them in the list of
1458 ;; misspelled words thus giving a
1459 ;; non-existent word. Skip if ispell
1460 ;; is used, string is a TeX command
1461 ;; (char before beginning of word is
1462 ;; backslash) and none of the previous
1463 ;; conditions match.
1464 (and (not ispell-really-aspell
)
1466 (goto-char (- (nth 1 found-list
) 1))
1467 (if (looking-at "[\\]" )
1471 (flyspell-word nil t
)
1472 ;; Search for next misspelled word will begin from
1473 ;; end of last validated match.
1474 (setq buffer-scan-pos
(point))))
1475 ;; Record if misspelling is not found and try new one
1476 (cl-pushnew (concat " -> " word
" - "
1477 (int-to-string wordpos
))
1478 words-not-found
:test
#'equal
)
1479 (setq keep nil
)))))))
1481 (if flyspell-issue-message-flag
(message "Spell Checking completed.")))
1482 ;; Warn about not found misspellings
1483 (dolist (word words-not-found
)
1484 (message "%s: word not found" word
))
1485 ;; Kill and forget the buffer with the list of incorrect words.
1486 (kill-buffer flyspell-external-ispell-buffer
)
1487 (setq flyspell-external-ispell-buffer nil
)))
1489 ;;*---------------------------------------------------------------------*/
1490 ;;* flyspell-process-localwords ... */
1491 ;;* ------------------------------------------------------------- */
1492 ;;* This function is used to prevent marking of words explicitly */
1493 ;;* declared correct. */
1494 ;;*---------------------------------------------------------------------*/
1495 (defun flyspell-process-localwords (misspellings-buffer)
1496 (let ((localwords ispell-buffer-session-localwords
)
1498 (ispell-casechars (ispell-get-casechars)))
1499 ;; Get localwords from the original buffer
1501 (goto-char (point-min))
1502 ;; Localwords parsing copied from ispell.el.
1503 (while (search-forward ispell-words-keyword nil t
)
1504 (let ((end (point-at-eol))
1506 ;; buffer-local words separated by a space, and can contain
1507 ;; any character other than a space. Not rigorous enough.
1508 (while (re-search-forward " *\\([^ ]+\\)" end t
)
1509 (setq string
(buffer-substring-no-properties (match-beginning 1)
1511 ;; This can fail when string contains a word with invalid chars.
1512 ;; Error handling needs to be added between Ispell and Emacs.
1513 (if (and (< 1 (length string
))
1514 (equal 0 (string-match ispell-casechars string
)))
1515 (push string localwords
))))))
1516 ;; Remove localwords matches from misspellings-buffer.
1517 ;; The usual mechanism of communicating the local words to ispell
1518 ;; does not affect the special ispell process used by
1519 ;; flyspell-large-region.
1520 (with-current-buffer misspellings-buffer
1522 (dolist (word localwords
)
1523 (goto-char (point-min))
1524 (let ((regexp (concat "^" word
"\n")))
1525 (while (re-search-forward regexp nil t
)
1526 (delete-region (match-beginning 0) (match-end 0)))))))))
1528 ;;* ---------------------------------------------------------------
1529 ;;* flyspell-check-region-doublons
1530 ;;* ---------------------------------------------------------------
1531 (defun flyspell-check-region-doublons (beg end
)
1532 "Check for adjacent duplicated words (doublons) in the given region."
1535 (flyspell-word) ; Make sure current word is checked
1537 (while (and (< (point) end
)
1538 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1544 ;;*---------------------------------------------------------------------*/
1545 ;;* flyspell-large-region ... */
1546 ;;*---------------------------------------------------------------------*/
1547 (defun flyspell-large-region (beg end
)
1548 (let* ((curbuf (current-buffer))
1549 (buffer (get-buffer-create "*flyspell-region*")))
1550 (setq flyspell-external-ispell-buffer buffer
)
1551 (setq flyspell-large-region-buffer curbuf
)
1552 (setq flyspell-large-region-beg beg
)
1553 (setq flyspell-large-region-end end
)
1554 (flyspell-accept-buffer-local-defs)
1557 ;; this is done, we can start checking...
1558 (if flyspell-issue-message-flag
(message "Checking region..."))
1560 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1561 ;; Local dictionary becomes the global dictionary in use.
1562 (setq ispell-current-dictionary
1563 (or ispell-local-dictionary ispell-dictionary
))
1564 (setq ispell-current-personal-dictionary
1565 (or ispell-local-pdict ispell-personal-dictionary
))
1566 (let ((args (ispell-get-ispell-args))
1567 (encoding (ispell-get-coding-system))
1569 (if (and ispell-current-dictionary
; use specified dictionary
1570 (not (member "-d" args
))) ; only define if not overridden
1572 (append (list "-d" ispell-current-dictionary
) args
)))
1573 (if ispell-current-personal-dictionary
; use specified pers dict
1578 ispell-current-personal-dictionary
)))))
1580 ;; Check for extended character mode
1581 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1582 (and extended-char-mode
; ~ extended character mode
1583 (string-match "[^~]+$" extended-char-mode
)
1584 (cl-pushnew (concat "-T" (match-string 0 extended-char-mode
))
1585 args
:test
#'equal
)))
1587 ;; Add ispell-extra-args
1588 (setq args
(append args ispell-extra-args
))
1590 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1591 ;; for communication. ispell or older aspell/hunspell does not support this
1592 (if ispell-encoding8-command
1595 (if ispell-really-hunspell
1596 (list ispell-encoding8-command
1597 (upcase (symbol-name encoding
)))
1598 (list (concat ispell-encoding8-command
1599 (symbol-name encoding
)))))))
1601 (let ((process-coding-system-alist (list (cons "\\.*" encoding
))))
1602 (setq c
(apply 'ispell-call-process-region beg
1608 (if ispell-really-aspell
"list" "-l")
1612 (flyspell-process-localwords buffer
)
1613 (with-current-buffer curbuf
1614 (flyspell-delete-region-overlays beg end
)
1615 (flyspell-check-region-doublons beg end
))
1616 (flyspell-external-point-words))
1617 (error "Can't check region")))))
1619 ;;*---------------------------------------------------------------------*/
1620 ;;* flyspell-region ... */
1621 ;;* ------------------------------------------------------------- */
1622 ;;* Because `ispell -a' is too slow, it is not possible to use */
1623 ;;* it on large region. Then, when ispell is invoked on a large */
1624 ;;* text region, a new `ispell -l' process is spawned. The */
1625 ;;* pointed out words are then searched in the region a checked with */
1626 ;;* regular flyspell means. */
1627 ;;*---------------------------------------------------------------------*/
1629 (defun flyspell-region (beg end
)
1630 "Flyspell text between BEG and END."
1632 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1640 (if (and flyspell-large-region
(> (- end beg
) flyspell-large-region
))
1641 (flyspell-large-region beg end
)
1642 (flyspell-small-region beg end
)))))
1644 ;;*---------------------------------------------------------------------*/
1645 ;;* flyspell-buffer ... */
1646 ;;*---------------------------------------------------------------------*/
1648 (defun flyspell-buffer ()
1649 "Flyspell whole buffer."
1651 (flyspell-region (point-min) (point-max)))
1653 ;;*---------------------------------------------------------------------*/
1654 ;;* old next error position ... */
1655 ;;*---------------------------------------------------------------------*/
1656 (defvar flyspell-old-buffer-error nil
)
1657 (defvar flyspell-old-pos-error nil
)
1659 ;;*---------------------------------------------------------------------*/
1660 ;;* flyspell-goto-next-error ... */
1661 ;;*---------------------------------------------------------------------*/
1662 (defun flyspell-goto-next-error ()
1663 "Go to the next previously detected error.
1664 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1669 (if (and (eq (current-buffer) flyspell-old-buffer-error
)
1670 (eq pos flyspell-old-pos-error
))
1672 (if (= flyspell-old-pos-error max
)
1673 ;; goto beginning of buffer
1675 (message "Restarting from beginning of buffer")
1676 (goto-char (point-min)))
1678 (setq pos
(point))))
1679 ;; seek the next error
1680 (while (and (< pos max
)
1681 (let ((ovs (overlays-at pos
))
1683 (while (and (not r
) (consp ovs
))
1684 (if (flyspell-overlay-p (car ovs
))
1686 (setq ovs
(cdr ovs
))))
1688 (setq pos
(1+ pos
)))
1689 ;; save the current location for next invocation
1690 (setq flyspell-old-pos-error pos
)
1691 (setq flyspell-old-buffer-error
(current-buffer))
1694 (message "No more miss-spelled word!"))))
1696 ;;*---------------------------------------------------------------------*/
1697 ;;* flyspell-overlay-p ... */
1698 ;;*---------------------------------------------------------------------*/
1699 (defun flyspell-overlay-p (o)
1700 "Return true if O is an overlay used by flyspell."
1701 (and (overlayp o
) (overlay-get o
'flyspell-overlay
)))
1703 ;;*---------------------------------------------------------------------*/
1704 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1705 ;;* ------------------------------------------------------------- */
1706 ;;* Remove overlays introduced by flyspell. */
1707 ;;*---------------------------------------------------------------------*/
1708 (defun flyspell-delete-region-overlays (beg end
)
1709 "Delete overlays used by flyspell in a given region."
1710 (remove-overlays beg end
'flyspell-overlay t
))
1712 (defun flyspell-delete-all-overlays ()
1713 "Delete all the overlays used by flyspell."
1714 (flyspell-delete-region-overlays (point-min) (point-max)))
1716 ;;*---------------------------------------------------------------------*/
1717 ;;* flyspell-unhighlight-at ... */
1718 ;;*---------------------------------------------------------------------*/
1719 (defun flyspell-unhighlight-at (pos)
1720 "Remove the flyspell overlay that are located at POS."
1721 (if flyspell-persistent-highlight
1722 (let ((overlays (overlays-at pos
)))
1723 (while (consp overlays
)
1724 (if (flyspell-overlay-p (car overlays
))
1725 (delete-overlay (car overlays
)))
1726 (setq overlays
(cdr overlays
))))
1727 (if (flyspell-overlay-p flyspell-overlay
)
1728 (delete-overlay flyspell-overlay
))))
1730 ;;*---------------------------------------------------------------------*/
1731 ;;* flyspell-properties-at-p ... */
1732 ;;* ------------------------------------------------------------- */
1733 ;;* Is there an highlight properties at position pos? */
1734 ;;*---------------------------------------------------------------------*/
1735 (defun flyspell-properties-at-p (pos)
1736 "Return t if there is a text property at POS, not counting `local-map'.
1737 If variable `flyspell-highlight-properties' is set to nil,
1738 text with properties are not checked. This function is used to discover
1739 if the character at POS has any other property."
1740 (let ((prop (text-properties-at pos
))
1742 (while (and keep
(consp prop
))
1743 (if (and (eq (car prop
) 'local-map
) (consp (cdr prop
)))
1744 (setq prop
(cdr (cdr prop
)))
1748 ;;*---------------------------------------------------------------------*/
1749 ;;* make-flyspell-overlay ... */
1750 ;;*---------------------------------------------------------------------*/
1751 (defun make-flyspell-overlay (beg end face mouse-face
)
1752 "Allocate an overlay to highlight an incorrect word.
1753 BEG and END specify the range in the buffer of that word.
1754 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1756 (let ((overlay (make-overlay beg end nil t nil
)))
1757 (overlay-put overlay
'face face
)
1758 (overlay-put overlay
'mouse-face mouse-face
)
1759 (overlay-put overlay
'flyspell-overlay t
)
1760 (overlay-put overlay
'evaporate t
)
1761 (overlay-put overlay
'help-echo
"mouse-2: correct word at point")
1762 (overlay-put overlay
'keymap flyspell-mouse-map
)
1763 (when (eq face
'flyspell-incorrect
)
1764 (and (stringp flyspell-before-incorrect-word-string
)
1765 (overlay-put overlay
'before-string
1766 flyspell-before-incorrect-word-string
))
1767 (and (stringp flyspell-after-incorrect-word-string
)
1768 (overlay-put overlay
'after-string
1769 flyspell-after-incorrect-word-string
)))
1772 ;;*---------------------------------------------------------------------*/
1773 ;;* flyspell-highlight-incorrect-region ... */
1774 ;;*---------------------------------------------------------------------*/
1775 (defun flyspell-highlight-incorrect-region (beg end poss
)
1776 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1777 POSS is usually a list of possible spelling/correction lists,
1778 as returned by `ispell-parse-output'.
1779 It can also be the symbol `doublon', in the case where the word
1780 is itself incorrect, but suspiciously repeated."
1781 (let ((inhibit-read-only t
))
1782 (unless (run-hook-with-args-until-success
1783 'flyspell-incorrect-hook beg end poss
)
1784 (if (or flyspell-highlight-properties
1785 (not (flyspell-properties-at-p beg
)))
1787 ;; we cleanup all the overlay that are in the region, not
1788 ;; beginning at the word start position
1789 (if (< (1+ beg
) end
)
1790 (let ((os (overlays-in (1+ beg
) end
)))
1792 (if (flyspell-overlay-p (car os
))
1793 (delete-overlay (car os
)))
1794 (setq os
(cdr os
)))))
1795 ;; we cleanup current overlay at the same position
1796 (flyspell-unhighlight-at beg
)
1797 ;; now we can use a new overlay
1798 (setq flyspell-overlay
1799 (make-flyspell-overlay
1801 (if (eq poss
'doublon
) 'flyspell-duplicate
'flyspell-incorrect
)
1804 ;;*---------------------------------------------------------------------*/
1805 ;;* flyspell-highlight-duplicate-region ... */
1806 ;;*---------------------------------------------------------------------*/
1807 (defun flyspell-highlight-duplicate-region (beg end poss
)
1808 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1809 POSS is a list of possible spelling/correction lists,
1810 as returned by `ispell-parse-output'."
1811 (let ((inhibit-read-only t
))
1812 (unless (run-hook-with-args-until-success
1813 'flyspell-incorrect-hook beg end poss
)
1814 (if (or flyspell-highlight-properties
1815 (not (flyspell-properties-at-p beg
)))
1817 ;; we cleanup current overlay at the same position
1818 (flyspell-unhighlight-at beg
)
1819 ;; now we can use a new overlay
1820 (setq flyspell-overlay
1821 (make-flyspell-overlay beg end
1825 ;;*---------------------------------------------------------------------*/
1826 ;;* flyspell-auto-correct-cache ... */
1827 ;;*---------------------------------------------------------------------*/
1828 (defvar flyspell-auto-correct-pos nil
)
1829 (defvar flyspell-auto-correct-region nil
)
1830 (defvar flyspell-auto-correct-ring nil
)
1831 (defvar flyspell-auto-correct-word nil
)
1832 (make-variable-buffer-local 'flyspell-auto-correct-pos
)
1833 (make-variable-buffer-local 'flyspell-auto-correct-region
)
1834 (make-variable-buffer-local 'flyspell-auto-correct-ring
)
1835 (make-variable-buffer-local 'flyspell-auto-correct-word
)
1837 ;;*---------------------------------------------------------------------*/
1838 ;;* flyspell-check-previous-highlighted-word ... */
1839 ;;*---------------------------------------------------------------------*/
1840 (defun flyspell-check-previous-highlighted-word (&optional arg
)
1841 "Correct the closest previous word that is highlighted as misspelled.
1842 This function scans for a word which starts before point that has been
1843 highlighted by Flyspell as misspelled. If it finds one, it proposes
1844 a replacement for that word. With prefix arg N, check the Nth word
1845 before point that's highlighted as misspelled."
1847 (let ((pos1 (point))
1849 (arg (if (or (not (numberp arg
)) (< arg
1)) 1 arg
))
1852 (while (and (setq pos
(previous-overlay-change pos
))
1855 (if (> pos
(point-min))
1857 (setq ovs
(overlays-at pos
))
1860 (setq ovs
(cdr ovs
))
1861 (if (and (flyspell-overlay-p ov
)
1862 (= 0 (setq arg
(1- arg
))))
1863 (throw 'exit t
)))))))
1867 (setq flyspell-word-cache-word nil
) ;; Force flyspell-word re-check
1869 (error "No word to correct before point"))))
1871 ;;*---------------------------------------------------------------------*/
1872 ;;* flyspell-display-next-corrections ... */
1873 ;;*---------------------------------------------------------------------*/
1874 (defun flyspell-display-next-corrections (corrections)
1875 (let ((string "Corrections:")
1878 (while (< (length string
) 80)
1879 (if (equal (car l
) flyspell-auto-correct-word
)
1880 (setq pos
(cons (+ 1 (length string
)) pos
)))
1881 (setq string
(concat string
" " (car l
)))
1884 (let ((num (car pos
)))
1885 (put-text-property num
1886 (+ num
(length flyspell-auto-correct-word
))
1887 'face
'flyspell-incorrect
1889 (setq pos
(cdr pos
)))
1890 (if (fboundp 'display-message
)
1891 (display-message 'no-log string
)
1892 (message "%s" string
))))
1894 ;;*---------------------------------------------------------------------*/
1895 ;;* flyspell-abbrev-table ... */
1896 ;;*---------------------------------------------------------------------*/
1897 (defun flyspell-abbrev-table ()
1898 (if flyspell-use-global-abbrev-table-p
1900 (or local-abbrev-table global-abbrev-table
)))
1902 ;;*---------------------------------------------------------------------*/
1903 ;;* flyspell-define-abbrev ... */
1904 ;;*---------------------------------------------------------------------*/
1905 (defun flyspell-define-abbrev (name expansion
)
1906 (let ((table (flyspell-abbrev-table)))
1908 (define-abbrev table
(downcase name
) expansion
))))
1910 ;;*---------------------------------------------------------------------*/
1911 ;;* flyspell-auto-correct-word ... */
1912 ;;*---------------------------------------------------------------------*/
1913 (defun flyspell-auto-correct-word ()
1914 "Correct the current word.
1915 This command proposes various successive corrections for the current word."
1917 ;; If we are not in the construct where flyspell should be active,
1918 ;; invoke the original binding of M-TAB, if that was recorded.
1919 (if (and (local-variable-p 'flyspell--prev-meta-tab-binding
)
1920 (commandp flyspell--prev-meta-tab-binding t
)
1921 (functionp flyspell-generic-check-word-predicate
)
1922 (not (funcall flyspell-generic-check-word-predicate
))
1923 (equal (where-is-internal 'flyspell-auto-correct-word nil t
)
1925 (call-interactively flyspell--prev-meta-tab-binding
)
1927 (old-max (point-max)))
1928 ;; Use the correct dictionary.
1929 (flyspell-accept-buffer-local-defs)
1930 (if (and (eq flyspell-auto-correct-pos pos
)
1931 (consp flyspell-auto-correct-region
))
1932 ;; We have already been using the function at the same location.
1933 (let* ((start (car flyspell-auto-correct-region
))
1934 (len (cdr flyspell-auto-correct-region
)))
1935 (flyspell-unhighlight-at start
)
1936 (delete-region start
(+ start len
))
1937 (setq flyspell-auto-correct-ring
(cdr flyspell-auto-correct-ring
))
1938 (let* ((word (car flyspell-auto-correct-ring
))
1939 (len (length word
)))
1940 (rplacd flyspell-auto-correct-region len
)
1942 (if flyspell-abbrev-p
1943 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1944 flyspell-auto-correct-word
)
1945 (flyspell-change-abbrev (flyspell-abbrev-table)
1946 flyspell-auto-correct-word
1948 (flyspell-define-abbrev flyspell-auto-correct-word word
)))
1949 (funcall flyspell-insert-function word
)
1951 (flyspell-display-next-corrections flyspell-auto-correct-ring
))
1952 (flyspell-adjust-cursor-point pos
(point) old-max
)
1953 (setq flyspell-auto-correct-pos
(point)))
1954 ;; Fetch the word to be checked.
1955 (let ((word (flyspell-get-word)))
1957 (let ((start (car (cdr word
)))
1958 (end (car (cdr (cdr word
))))
1961 (setq flyspell-auto-correct-word word
)
1962 ;; Now check spelling of word..
1963 (ispell-send-string "%\n") ;Put in verbose mode.
1964 (ispell-send-string (concat "^" word
"\n"))
1965 ;; Wait until ispell has processed word.
1967 (accept-process-output ispell-process
)
1968 (not (string= "" (car ispell-filter
)))))
1969 ;; Remove leading empty element.
1970 (setq ispell-filter
(cdr ispell-filter
))
1971 ;; Ispell process should return something after word is sent.
1972 ;; Tag word as valid (i.e., skip) otherwise.
1974 (setq ispell-filter
'(*)))
1975 (if (consp ispell-filter
)
1976 (setq poss
(ispell-parse-output (car ispell-filter
))))
1978 ((or (eq poss t
) (stringp poss
))
1979 ;; Don't correct word.
1983 (error "Ispell: error in Ispell process"))
1985 ;; The word is incorrect, we have to propose a replacement.
1986 (let ((replacements (flyspell-sort (car (cdr (cdr poss
)))
1988 (setq flyspell-auto-correct-region nil
)
1989 (if (consp replacements
)
1991 (let ((replace (car replacements
)))
1992 (let ((new-word replace
))
1993 (if (not (equal new-word
(car poss
)))
1995 ;; the save the current replacements
1996 (setq flyspell-auto-correct-region
1997 (cons start
(length new-word
)))
1998 (let ((l replacements
))
1999 (while (consp (cdr l
))
2001 (rplacd l
(cons (car poss
) replacements
)))
2002 (setq flyspell-auto-correct-ring
2004 (flyspell-unhighlight-at start
)
2005 (delete-region start end
)
2006 (funcall flyspell-insert-function new-word
)
2007 (if flyspell-abbrev-p
2008 (if (flyspell-already-abbrevp
2009 (flyspell-abbrev-table) word
)
2010 (flyspell-change-abbrev
2011 (flyspell-abbrev-table)
2014 (flyspell-define-abbrev word
2017 (flyspell-display-next-corrections
2018 (cons new-word flyspell-auto-correct-ring
))
2019 (flyspell-adjust-cursor-point pos
2022 (setq flyspell-auto-correct-pos
(point))
2023 (ispell-pdict-save t
))))))))
2025 ;;*---------------------------------------------------------------------*/
2026 ;;* flyspell-auto-correct-previous-pos ... */
2027 ;;*---------------------------------------------------------------------*/
2028 (defvar flyspell-auto-correct-previous-pos nil
2029 "Holds the start of the first incorrect word before point.")
2031 ;;*---------------------------------------------------------------------*/
2032 ;;* flyspell-auto-correct-previous-hook ... */
2033 ;;*---------------------------------------------------------------------*/
2034 (defun flyspell-auto-correct-previous-hook ()
2035 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
2036 Sets `flyspell-auto-correct-previous-pos' to nil"
2038 (remove-hook 'pre-command-hook
(function flyspell-auto-correct-previous-hook
) t
)
2039 (unless (eq this-command
(function flyspell-auto-correct-previous-word
))
2040 (setq flyspell-auto-correct-previous-pos nil
)))
2042 ;;*---------------------------------------------------------------------*/
2043 ;;* flyspell-auto-correct-previous-word ... */
2044 ;;*---------------------------------------------------------------------*/
2045 (defun flyspell-auto-correct-previous-word (position)
2046 "Auto correct the first misspelled word that occurs before point.
2047 But don't look beyond what's visible on the screen."
2050 (let ((top (window-start))
2054 (narrow-to-region top bot
)
2055 (overlay-recenter (point))
2057 (add-hook 'pre-command-hook
2058 (function flyspell-auto-correct-previous-hook
) t t
)
2060 (unless flyspell-auto-correct-previous-pos
2061 ;; only reset if a new overlay exists
2062 (setq flyspell-auto-correct-previous-pos nil
)
2064 (let ((overlay-list (overlays-in (point-min) position
))
2065 (new-overlay 'dummy-value
))
2067 ;; search for previous (new) flyspell overlay
2068 (while (and new-overlay
2069 (or (not (flyspell-overlay-p new-overlay
))
2070 ;; check if its face has changed
2071 (not (eq (get-char-property
2072 (overlay-start new-overlay
) 'face
)
2073 'flyspell-incorrect
))))
2074 (setq new-overlay
(car-safe overlay-list
))
2075 (setq overlay-list
(cdr-safe overlay-list
)))
2077 ;; if nothing new exits new-overlay should be nil
2078 (if new-overlay
;; the length of the word may change so go to the start
2079 (setq flyspell-auto-correct-previous-pos
2080 (overlay-start new-overlay
)))))
2082 (when flyspell-auto-correct-previous-pos
2084 (goto-char flyspell-auto-correct-previous-pos
)
2085 (let ((ispell-following-word t
)) ;; point is at start
2086 (if (numberp flyspell-auto-correct-previous-pos
)
2087 (goto-char flyspell-auto-correct-previous-pos
))
2088 (flyspell-auto-correct-word))
2089 ;; the point may have moved so reset this
2090 (setq flyspell-auto-correct-previous-pos
(point))))))))
2092 ;;*---------------------------------------------------------------------*/
2093 ;;* flyspell-correct-word ... */
2094 ;;*---------------------------------------------------------------------*/
2096 (defun flyspell-correct-word (event)
2097 "Pop up a menu of possible corrections for a misspelled word.
2098 The word checked is the word at the mouse position."
2100 (let ((save (point)))
2101 (mouse-set-point event
)
2102 (flyspell-correct-word-before-point event save
)))
2104 (defun flyspell-correct-word-before-point (&optional event opoint
)
2105 "Pop up a menu of possible corrections for misspelled word before point.
2106 If EVENT is non-nil, it is the mouse event that invoked this operation;
2107 that controls where to put the menu.
2108 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2110 ;; use the correct dictionary
2111 (flyspell-accept-buffer-local-defs)
2112 (or opoint
(setq opoint
(point)))
2113 (let ((cursor-location (point))
2114 (word (flyspell-get-word)))
2116 (let ((start (car (cdr word
)))
2117 (end (car (cdr (cdr word
))))
2120 ;; now check spelling of word.
2121 (ispell-send-string "%\n") ;put in verbose mode
2122 (ispell-send-string (concat "^" word
"\n"))
2123 ;; wait until ispell has processed word
2125 (accept-process-output ispell-process
)
2126 (not (string= "" (car ispell-filter
)))))
2127 ;; Remove leading empty element
2128 (setq ispell-filter
(cdr ispell-filter
))
2129 ;; ispell process should return something after word is sent.
2130 ;; Tag word as valid (i.e., skip) otherwise
2132 (setq ispell-filter
'(*)))
2133 (if (consp ispell-filter
)
2134 (setq poss
(ispell-parse-output (car ispell-filter
))))
2136 ((or (eq poss t
) (stringp poss
))
2137 ;; don't correct word
2141 (error "Ispell: error in Ispell process"))
2143 ;; The word is incorrect, we have to propose a replacement.
2144 (flyspell-do-correct (flyspell-emacs-popup event poss word
)
2145 poss word cursor-location start end opoint
)))
2146 (ispell-pdict-save t
)))))
2148 ;;*---------------------------------------------------------------------*/
2149 ;;* flyspell-do-correct ... */
2150 ;;*---------------------------------------------------------------------*/
2151 (defun flyspell-do-correct (replace poss word cursor-location start end save
)
2152 "The popup menu callback."
2153 (cond ((eq replace
'ignore
)
2158 (ispell-send-string (concat "*" word
"\n"))
2159 (ispell-send-string "#\n")
2160 (flyspell-unhighlight-at cursor-location
)
2161 (setq ispell-pdict-modified-p
'(t)))
2162 ((or (eq replace
'buffer
) (eq replace
'session
))
2163 (ispell-send-string (concat "@" word
"\n"))
2164 (add-to-list 'ispell-buffer-session-localwords word
)
2165 (or ispell-buffer-local-name
; session localwords might conflict
2166 (setq ispell-buffer-local-name
(buffer-name)))
2167 (flyspell-unhighlight-at cursor-location
)
2168 (if (null ispell-pdict-modified-p
)
2169 (setq ispell-pdict-modified-p
2170 (list ispell-pdict-modified-p
)))
2172 (if (eq replace
'buffer
)
2173 (ispell-add-per-file-word-list word
)))
2175 (flyspell-unhighlight-at cursor-location
)
2176 (let ((old-max (point-max))
2177 (new-word (if (atom replace
)
2180 (cursor-location (+ (- (length word
) (- end start
))
2182 (unless (equal new-word
(car poss
))
2183 (delete-region start end
)
2185 (funcall flyspell-insert-function new-word
)
2186 (if flyspell-abbrev-p
2187 (flyspell-define-abbrev word new-word
)))
2188 (flyspell-adjust-cursor-point save cursor-location old-max
)))
2193 ;;*---------------------------------------------------------------------*/
2194 ;;* flyspell-adjust-cursor-point ... */
2195 ;;*---------------------------------------------------------------------*/
2196 (defun flyspell-adjust-cursor-point (save cursor-location old-max
)
2197 (if (>= save cursor-location
)
2198 (let ((new-pos (+ save
(- (point-max) old-max
))))
2200 ((< new-pos
(point-min))
2202 ((> new-pos
(point-max))
2207 ;;*---------------------------------------------------------------------*/
2208 ;;* flyspell-emacs-popup ... */
2209 ;;*---------------------------------------------------------------------*/
2210 (defun flyspell-emacs-popup (event poss word
)
2211 "The Emacs popup menu."
2212 (if (and (not event
)
2214 (let* ((mouse-pos (mouse-position))
2215 (mouse-pos (if (nth 1 mouse-pos
)
2217 (set-mouse-position (car mouse-pos
)
2218 (/ (frame-width) 2) 2)
2220 (setq event
(list (list (car (cdr mouse-pos
))
2221 (1+ (cdr (cdr mouse-pos
))))
2223 (let* ((corrects (flyspell-sort (car (cdr (cdr poss
))) word
))
2224 (cor-menu (if (consp corrects
)
2225 (mapcar (lambda (correct)
2226 (list correct correct
))
2229 (affix (car (cdr (cdr (cdr poss
)))))
2231 (base-menu (let ((save (if (and (consp affix
) show-affix-info
)
2233 (list (concat "Save affix: " (car affix
))
2235 '("Accept (session)" session
)
2236 '("Accept (buffer)" buffer
))
2237 '(("Save word" save
)
2238 ("Accept (session)" session
)
2239 ("Accept (buffer)" buffer
)))))
2240 (if (consp cor-menu
)
2241 (append cor-menu
(cons "" save
))
2243 (menu (cons "flyspell correction menu" base-menu
)))
2244 (car (x-popup-menu event
2245 (list (format "%s [%s]" word
(or ispell-local-dictionary
2249 ;;*---------------------------------------------------------------------*/
2250 ;;* Some example functions for real autocorrecting */
2251 ;;*---------------------------------------------------------------------*/
2252 (defun flyspell-maybe-correct-transposition (beg end poss
)
2253 "Check replacements for transposed characters.
2255 If the text between BEG and END is equal to a correction suggested by
2256 Ispell, after transposing two adjacent characters, correct the text,
2259 The third arg POSS is either the symbol `doublon' or a list of
2260 possible corrections as returned by `ispell-parse-output'.
2262 This function is meant to be added to `flyspell-incorrect-hook'."
2265 (let ((str (buffer-substring beg end
))
2266 (i 0) (len (- end beg
)) tmp
)
2267 (while (< (1+ i
) len
)
2268 (setq tmp
(aref str i
))
2269 (aset str i
(aref str
(1+ i
)))
2270 (aset str
(1+ i
) tmp
)
2271 (when (member str
(nth 2 poss
))
2273 (goto-char (+ beg i
1))
2274 (transpose-chars 1))
2276 (setq tmp
(aref str i
))
2277 (aset str i
(aref str
(1+ i
)))
2278 (aset str
(1+ i
) tmp
)
2282 (defun flyspell-maybe-correct-doubling (beg end poss
)
2283 "Check replacements for doubled characters.
2285 If the text between BEG and END is equal to a correction suggested by
2286 Ispell, after removing a pair of doubled characters, correct the text,
2289 The third arg POSS is either the symbol `doublon' or a list of
2290 possible corrections as returned by `ispell-parse-output'.
2292 This function is meant to be added to `flyspell-incorrect-hook'."
2295 (let ((str (buffer-substring beg end
))
2296 (i 0) (len (- end beg
)))
2297 (while (< (1+ i
) len
)
2298 (when (and (= (aref str i
) (aref str
(1+ i
)))
2299 (member (concat (substring str
0 (1+ i
))
2300 (substring str
(+ i
2)))
2302 (goto-char (+ beg i
))
2308 ;;*---------------------------------------------------------------------*/
2309 ;;* flyspell-already-abbrevp ... */
2310 ;;*---------------------------------------------------------------------*/
2311 (defun flyspell-already-abbrevp (table word
)
2312 (let ((sym (abbrev-symbol word table
)))
2313 (and sym
(symbolp sym
))))
2315 ;;*---------------------------------------------------------------------*/
2316 ;;* flyspell-change-abbrev ... */
2317 ;;*---------------------------------------------------------------------*/
2318 (defun flyspell-change-abbrev (table old new
)
2319 (set (abbrev-symbol old table
) new
))
2323 ;;; flyspell.el ends here