1 ;;; flyspell.el --- On-the-fly spell checker -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998, 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: convenience
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
29 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
30 ;; This applies only to the current buffer.
32 ;; To enable Flyspell in text representing computer programs, type
33 ;; M-x flyspell-prog-mode.
34 ;; In that mode only text inside comments is checked.
36 ;; Some user variables control the behavior of flyspell. They are
37 ;; those defined under the `User variables' comment.
42 (eval-when-compile (require 'cl-lib
))
44 ;;*---------------------------------------------------------------------*/
46 ;;*---------------------------------------------------------------------*/
47 (defgroup flyspell nil
48 "Spell checking on the fly."
54 ;;*---------------------------------------------------------------------*/
55 ;;* User configuration ... */
56 ;;*---------------------------------------------------------------------*/
57 (defcustom flyspell-highlight-flag t
58 "How Flyspell should indicate misspelled words.
59 Non-nil means use highlight, nil means use minibuffer messages."
63 (defcustom flyspell-mark-duplications-flag t
64 "Non-nil means Flyspell reports a repeated word as an error.
65 See `flyspell-mark-duplications-exceptions' to add exceptions to this rule.
66 Detection of repeated words is not implemented in
67 \"large\" regions; see variable `flyspell-large-region'."
71 (defcustom flyspell-mark-duplications-exceptions
72 '((nil .
("that" "had")) ; Common defaults for English.
73 ("\\`francais" .
("nous" "vous")))
74 "A list of exceptions for duplicated words.
75 It should be a list of (LANGUAGE . EXCEPTION-LIST).
77 LANGUAGE is nil, which means the exceptions apply regardless of
78 the current dictionary, or a regular expression matching the
79 dictionary name (`ispell-local-dictionary' or
80 `ispell-dictionary') for which the exceptions should apply.
82 EXCEPTION-LIST is a list of strings. The checked word is
83 downcased before comparing with these exceptions."
85 :type
'(alist :key-type
(choice (const :tag
"All dictionaries" nil
)
87 :value-type
(repeat string
))
90 (defcustom flyspell-sort-corrections nil
91 "Non-nil means, sort the corrections alphabetically before popping them."
96 (defcustom flyspell-duplicate-distance -
1
97 "The maximum distance for finding duplicates of unrecognized words.
98 This applies to the feature that when a word is not found in the dictionary,
99 if the same spelling occurs elsewhere in the buffer,
100 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
101 This variable specifies how far to search to find such a duplicate.
102 -1 means no limit (search the whole buffer).
103 0 means do not search for duplicate unrecognized spellings."
106 :type
'(choice (const :tag
"no limit" -
1)
109 (defcustom flyspell-delay
3
110 "The number of seconds to wait before checking, after a \"delayed\" command."
114 (defcustom flyspell-persistent-highlight t
115 "Non-nil means misspelled words remain highlighted until corrected.
116 If this variable is nil, only the most recently detected misspelled word
121 (defcustom flyspell-highlight-properties t
122 "Non-nil means highlight incorrect words even if a property exists for this word."
126 (defcustom flyspell-default-delayed-commands
127 '(self-insert-command
129 backward-or-forward-delete-char
131 scrollbar-vertical-drag
132 backward-delete-char-untabify
)
133 "The standard list of delayed commands for Flyspell.
134 See `flyspell-delayed-commands'."
137 :type
'(repeat (symbol)))
139 (defcustom flyspell-delayed-commands nil
140 "List of commands that are \"delayed\" for Flyspell mode.
141 After these commands, Flyspell checking is delayed for a short time,
142 whose length is specified by `flyspell-delay'."
144 :type
'(repeat (symbol)))
146 (defcustom flyspell-default-deplacement-commands
147 '(next-line previous-line
148 handle-switch-frame handle-select-window
151 "The standard list of deplacement commands for Flyspell.
152 See variable `flyspell-deplacement-commands'."
155 :type
'(repeat (symbol)))
157 (defcustom flyspell-deplacement-commands nil
158 "List of commands that are \"deplacement\" for Flyspell mode.
159 After these commands, Flyspell checking is performed only if the previous
160 command was not the very same command."
163 :type
'(repeat (symbol)))
165 (defcustom flyspell-issue-welcome-flag t
166 "Non-nil means that Flyspell should display a welcome message when started."
170 (defcustom flyspell-issue-message-flag t
171 "Non-nil means that Flyspell emits messages when checking words."
175 (defcustom flyspell-incorrect-hook nil
176 "List of functions to be called when incorrect words are encountered.
177 Each function is given three arguments. The first two
178 arguments are the beginning and the end of the incorrect region.
179 The third is either the symbol `doublon' or the list
180 of possible corrections as returned by `ispell-parse-output'.
182 If any of the functions return non-nil, the word is not highlighted as
188 (defcustom flyspell-default-dictionary nil
189 "A string that is the name of the default dictionary.
190 This is passed to the `ispell-change-dictionary' when flyspell is started.
191 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
192 when flyspell is started, the value of that variable is used instead
193 of `flyspell-default-dictionary' to select the default dictionary.
194 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
195 Ispell's ultimate default dictionary."
198 :type
'(choice string
(const :tag
"Default" nil
)))
200 (defcustom flyspell-tex-command-regexp
201 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
202 "A string that is the regular expression that matches TeX commands."
207 (defcustom flyspell-check-tex-math-command nil
208 "Non-nil means check even inside TeX math environment.
209 TeX math environments are discovered by `texmathp', implemented
210 inside AUCTeX package. That package may be found at
211 URL `http://www.gnu.org/software/auctex/'"
215 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
216 '("francais" "deutsch8" "norsk")
217 "List of dictionary names that consider `-' as word delimiter."
220 :type
'(repeat (string)))
222 (defcustom flyspell-abbrev-p
224 "If non-nil, add correction to abbreviation table."
229 (defcustom flyspell-use-global-abbrev-table-p
231 "If non-nil, prefer global abbrev table to local abbrev table."
236 (defcustom flyspell-mode-line-string
" Fly"
237 "String displayed on the mode line when flyspell is active.
238 Set this to nil if you don't want a mode line indicator."
240 :type
'(choice string
(const :tag
"None" nil
)))
242 (defcustom flyspell-large-region
1000
243 "The threshold that determines if a region is small.
244 If the region is smaller than this number of characters,
245 `flyspell-region' checks the words sequentially using regular
246 flyspell methods. Else, if the region is large, a new Ispell process is
249 Doubled words are not detected in a large region, because Ispell
250 does not check for them.
252 If this variable is nil, all regions are treated as small."
255 :type
'(choice number
(const :tag
"All small" nil
)))
257 (defcustom flyspell-insert-function
(function insert
)
258 "Function for inserting word by flyspell upon correction."
262 (defcustom flyspell-before-incorrect-word-string nil
263 "String used to indicate an incorrect word starting."
265 :type
'(choice string
(const nil
)))
267 (defcustom flyspell-after-incorrect-word-string nil
268 "String used to indicate an incorrect word ending."
270 :type
'(choice string
(const nil
)))
272 (defvar flyspell-mode-map
)
274 (defcustom flyspell-use-meta-tab t
275 "Non-nil means that flyspell uses M-TAB to correct word."
278 :initialize
'custom-initialize-default
279 :set
(lambda (sym val
)
280 (define-key flyspell-mode-map
"\M-\t"
282 'flyspell-auto-correct-word
))))
284 (defcustom flyspell-auto-correct-binding
286 "The key binding for flyspell auto correction."
290 ;;*---------------------------------------------------------------------*/
291 ;;* Mode specific options */
292 ;;* ------------------------------------------------------------- */
293 ;;* Mode specific options enable users to disable flyspell on */
294 ;;* certain word depending of the emacs mode. For instance, when */
295 ;;* using flyspell with mail-mode add the following expression */
296 ;;* in your init file: */
297 ;;* (add-hook 'mail-mode */
298 ;;* (lambda () (setq flyspell-generic-check-word-predicate */
299 ;;* 'mail-mode-flyspell-verify))) */
300 ;;*---------------------------------------------------------------------*/
301 (defvar flyspell-generic-check-word-predicate nil
302 "Function providing per-mode customization over which words are flyspelled.
303 Returns t to continue checking, nil otherwise.
304 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
305 property of the major mode name.")
306 (make-variable-buffer-local 'flyspell-generic-check-word-predicate
)
307 (defvaralias 'flyspell-generic-check-word-p
308 'flyspell-generic-check-word-predicate
)
310 ;;*--- mail mode -------------------------------------------------------*/
311 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
312 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
313 (defvar message-signature-separator
)
314 (defun mail-mode-flyspell-verify ()
315 "Function used for `flyspell-generic-check-word-predicate' in Mail mode."
316 (let* ((header-end (save-excursion
317 (goto-char (point-min))
320 (regexp-quote mail-header-separator
)
325 (if (not (boundp 'message-signature-separator
))
328 (goto-char (point-max))
329 (re-search-backward message-signature-separator
330 (max header-end
(- (point) 4000)) t
)
332 (cond ((< (point) header-end
)
333 (and (save-excursion (beginning-of-line)
334 (looking-at "^Subject:"))
335 (> (point) (match-end 0))))
336 ((> (point) signature-begin
)
341 (not (looking-at "[>}|]\\|To:")))))))
343 ;;*--- texinfo mode ----------------------------------------------------*/
344 (put 'texinfo-mode
'flyspell-mode-predicate
'texinfo-mode-flyspell-verify
)
345 (defun texinfo-mode-flyspell-verify ()
346 "Function used for `flyspell-generic-check-word-predicate' in Texinfo mode."
349 (not (looking-at "@"))))
351 ;;*--- tex mode --------------------------------------------------------*/
352 (put 'tex-mode
'flyspell-mode-predicate
'tex-mode-flyspell-verify
)
353 (defun tex-mode-flyspell-verify ()
354 "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
357 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t
)))
359 (let ((this (point)))
361 (and (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}"
362 (line-end-position) t
)
363 (>= this
(match-beginning 0))
364 (<= this
(match-end 0))))))))
366 ;;*--- sgml mode -------------------------------------------------------*/
367 (put 'sgml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
368 (put 'html-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
369 (put 'nxml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
371 (autoload 'sgml-lexical-context
"sgml-mode")
373 (defun sgml-mode-flyspell-verify ()
374 "Function used for `flyspell-generic-check-word-predicate' in SGML mode.
375 Tag and attribute names are not spell checked, everything else is.
377 String values of attributes are checked because they can be text
378 like <img alt=\"Some thing.\">."
380 (not (memq (car (sgml-lexical-context))
383 ;;*---------------------------------------------------------------------*/
384 ;;* Programming mode */
385 ;;*---------------------------------------------------------------------*/
386 (defvar flyspell-prog-text-faces
387 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
388 "Faces corresponding to text in programming-mode buffers.")
390 (defun flyspell-generic-progmode-verify ()
391 "Used for `flyspell-generic-check-word-predicate' in programming modes."
392 ;; (point) is next char after the word. Must check one char before.
393 (let ((f (get-text-property (- (point) 1) 'face
)))
394 (memq f flyspell-prog-text-faces
)))
397 (defun flyspell-prog-mode ()
398 "Turn on `flyspell-mode' for comments and strings."
400 (setq flyspell-generic-check-word-predicate
401 'flyspell-generic-progmode-verify
)
403 (run-hooks 'flyspell-prog-mode-hook
))
405 ;;*---------------------------------------------------------------------*/
406 ;;* Overlay compatibility */
407 ;;*---------------------------------------------------------------------*/
408 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
409 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
410 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
411 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
412 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
413 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
414 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
415 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
417 ;;*---------------------------------------------------------------------*/
418 ;;* The minor mode declaration. */
419 ;;*---------------------------------------------------------------------*/
420 (defvar flyspell-mouse-map
421 (let ((map (make-sparse-keymap)))
422 (if (featurep 'xemacs
)
423 (define-key map
[button2] #'flyspell-correct-word)
424 (define-key map [down-mouse-2] #'flyspell-correct-word)
425 (define-key map [mouse-2] 'undefined))
427 "Keymap for Flyspell to put on erroneous words.")
429 (defvar flyspell-mode-map
430 (let ((map (make-sparse-keymap)))
431 (if flyspell-use-meta-tab
432 (define-key map "\M-\t" 'flyspell-auto-correct-word))
433 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
434 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
435 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
436 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
438 "Minor mode keymap for Flyspell mode--for the whole buffer.")
440 ;; dash character machinery
441 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
442 "Non-nil means that the `-' char is considered as a word delimiter.")
443 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
444 (defvar flyspell-dash-dictionary nil)
445 (make-variable-buffer-local 'flyspell-dash-dictionary)
446 (defvar flyspell-dash-local-dictionary nil)
447 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
449 ;;*---------------------------------------------------------------------*/
451 ;;*---------------------------------------------------------------------*/
452 (defface flyspell-incorrect
453 '((((supports :underline (:style wave)))
454 :underline (:style wave :color "Red1"))
456 :underline t :inherit error))
457 "Flyspell face for misspelled words."
461 (defface flyspell-duplicate
462 '((((supports :underline (:style wave)))
463 :underline (:style wave :color "DarkOrange"))
465 :underline t :inherit warning))
466 "Flyspell face for words that appear twice in a row.
467 See also `flyspell-duplicate-distance'."
471 (defvar flyspell-overlay nil)
473 ;;*---------------------------------------------------------------------*/
474 ;;* flyspell-mode ... */
475 ;;*---------------------------------------------------------------------*/
476 ;;;###autoload(defvar flyspell-mode nil "Non-nil if Flyspell mode is enabled.")
478 (define-minor-mode flyspell-mode
479 "Toggle on-the-fly spell checking (Flyspell mode).
480 With a prefix argument ARG, enable Flyspell mode if ARG is
481 positive, and disable it otherwise. If called from Lisp, enable
482 the mode if ARG is omitted or nil.
484 Flyspell mode is a buffer-local minor mode. When enabled, it
485 spawns a single Ispell process and checks each word. The default
486 flyspell behavior is to highlight incorrect words.
489 \\[ispell-word]: correct words (using Ispell).
490 \\[flyspell-auto-correct-word]: automatically correct word.
491 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
492 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
495 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
498 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
499 valid. For instance, a different dictionary can be used by
500 invoking `ispell-change-dictionary'.
502 Consider using the `ispell-parser' to check your text. For instance
504 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
507 \\[flyspell-region] checks all words inside a region.
508 \\[flyspell-buffer] checks the whole buffer."
509 :lighter flyspell-mode-line-string
510 :keymap flyspell-mode-map
515 (error (message "Error enabling Flyspell mode:\n%s" (cdr err))
517 (flyspell-mode-off)))
520 (defun turn-on-flyspell ()
521 "Unconditionally turn on Flyspell mode."
525 (defun turn-off-flyspell ()
526 "Unconditionally turn off Flyspell mode."
529 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
531 ;;*---------------------------------------------------------------------*/
532 ;;* flyspell-buffers ... */
533 ;;* ------------------------------------------------------------- */
534 ;;* For remembering buffers running flyspell */
535 ;;*---------------------------------------------------------------------*/
536 (defvar flyspell-buffers nil)
538 ;;*---------------------------------------------------------------------*/
539 ;;* flyspell-minibuffer-p ... */
540 ;;*---------------------------------------------------------------------*/
541 (defun flyspell-minibuffer-p (buffer)
542 "Is BUFFER a minibuffer?"
543 (let ((ws (get-buffer-window-list buffer t)))
544 (and (consp ws) (window-minibuffer-p (car ws)))))
546 ;;*---------------------------------------------------------------------*/
547 ;;* flyspell-accept-buffer-local-defs ... */
548 ;;*---------------------------------------------------------------------*/
549 (defvar flyspell-last-buffer nil
550 "The buffer in which the last flyspell operation took place.")
552 (defun flyspell-accept-buffer-local-defs (&optional force)
553 ;; When flyspell-word is used inside a loop (e.g. when processing
554 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
555 ;; up dwarfing everything else, so only do it when the buffer has changed.
556 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
557 (setq flyspell-last-buffer (current-buffer))
558 ;; Strange problem: If buffer in current window has font-lock turned on,
559 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
560 ;; call will reset the buffer to the buffer in the current window.
561 ;; However, it only happens at startup (fix by Albert L. Ting).
563 (ispell-accept-buffer-local-defs))
564 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
565 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
566 ;; The dictionary has changed
567 (setq flyspell-dash-dictionary ispell-dictionary)
568 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
569 (setq flyspell-consider-dash-as-word-delimiter-flag
570 (member (or ispell-local-dictionary ispell-dictionary)
571 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
573 (defun flyspell-hack-local-variables-hook ()
574 ;; When local variables are loaded, see if the dictionary context
576 (flyspell-accept-buffer-local-defs 'force))
578 (defun flyspell-kill-ispell-hook ()
579 (setq flyspell-last-buffer nil)
580 (dolist (buf (buffer-list))
581 (with-current-buffer buf
582 (kill-local-variable 'flyspell-word-cache-word))))
584 ;; Make sure we flush our caches when needed. Do it here rather than in
585 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
587 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
589 ;;*---------------------------------------------------------------------*/
590 ;;* flyspell-mode-on ... */
591 ;;*---------------------------------------------------------------------*/
592 (defun flyspell-mode-on ()
593 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
594 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
595 (setq ispell-highlight-face 'flyspell-incorrect)
596 ;; local dictionaries setup
597 (or ispell-local-dictionary ispell-dictionary
598 (if flyspell-default-dictionary
599 (ispell-change-dictionary flyspell-default-dictionary)))
600 ;; we have to force ispell to accept the local definition or
601 ;; otherwise it could be too late, the local dictionary may
603 ;; Pass the `force' argument for the case where flyspell was active already
604 ;; but the buffer's local-defs have been edited.
605 (flyspell-accept-buffer-local-defs 'force)
606 ;; we put the `flyspell-delayed' property on some commands
607 (flyspell-delay-commands)
608 ;; we put the `flyspell-deplacement' property on some commands
609 (flyspell-deplacement-commands)
610 ;; we bound flyspell action to post-command hook
611 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
612 ;; we bound flyspell action to pre-command hook
613 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
614 ;; we bound flyspell action to after-change hook
615 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
616 ;; we bound flyspell action to hack-local-variables-hook
617 (add-hook 'hack-local-variables-hook
618 (function flyspell-hack-local-variables-hook) t t)
619 ;; set flyspell-generic-check-word-predicate based on the major mode
620 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
622 (setq flyspell-generic-check-word-predicate mode-predicate)))
623 ;; the welcome message
624 (if (and flyspell-issue-message-flag
625 flyspell-issue-welcome-flag
626 (if (featurep 'xemacs)
627 (interactive-p) ;; XEmacs does not have (called-interactively-p)
628 (called-interactively-p 'interactive)))
629 (let ((binding (where-is-internal 'flyspell-auto-correct-word
633 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
634 (key-description binding))
635 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
637 ;;*---------------------------------------------------------------------*/
638 ;;* flyspell-delay-commands ... */
639 ;;*---------------------------------------------------------------------*/
640 (defun flyspell-delay-commands ()
641 "Install the standard set of Flyspell delayed commands."
642 (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
643 (mapc 'flyspell-delay-command flyspell-delayed-commands))
645 ;;*---------------------------------------------------------------------*/
646 ;;* flyspell-delay-command ... */
647 ;;*---------------------------------------------------------------------*/
648 (defun flyspell-delay-command (command)
649 "Set COMMAND to be delayed, for Flyspell.
650 When flyspell `post-command-hook' is invoked because a delayed command
651 has been used, the current word is not immediately checked.
652 It will be checked only after `flyspell-delay' seconds."
653 (interactive "SDelay Flyspell after Command: ")
654 (put command 'flyspell-delayed t))
656 ;;*---------------------------------------------------------------------*/
657 ;;* flyspell-deplacement-commands ... */
658 ;;*---------------------------------------------------------------------*/
659 (defun flyspell-deplacement-commands ()
660 "Install the standard set of Flyspell deplacement commands."
661 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
662 (mapc 'flyspell-deplacement-command flyspell-deplacement-commands))
664 ;;*---------------------------------------------------------------------*/
665 ;;* flyspell-deplacement-command ... */
666 ;;*---------------------------------------------------------------------*/
667 (defun flyspell-deplacement-command (command)
668 "Set COMMAND that implement cursor movements, for Flyspell.
669 When flyspell `post-command-hook' is invoked because a deplacement command
670 has been used, the current word is not checked."
671 (interactive "SDeplacement Flyspell after Command: ")
672 (put command 'flyspell-deplacement t))
674 ;;*---------------------------------------------------------------------*/
675 ;;* flyspell-word-cache ... */
676 ;;*---------------------------------------------------------------------*/
677 (defvar flyspell-word-cache-start nil)
678 (defvar flyspell-word-cache-end nil)
679 (defvar flyspell-word-cache-word nil)
680 (defvar flyspell-word-cache-result '_)
681 (make-variable-buffer-local 'flyspell-word-cache-start)
682 (make-variable-buffer-local 'flyspell-word-cache-end)
683 (make-variable-buffer-local 'flyspell-word-cache-word)
684 (make-variable-buffer-local 'flyspell-word-cache-result)
686 ;;*---------------------------------------------------------------------*/
687 ;;* The flyspell pre-hook, store the current position. In the */
688 ;;* post command hook, we will check, if the word at this position */
689 ;;* has to be spell checked. */
690 ;;*---------------------------------------------------------------------*/
691 (defvar flyspell-pre-buffer nil "Buffer current before `this-command'.")
692 (defvar flyspell-pre-point nil "Point before running `this-command'")
693 (defvar flyspell-pre-column nil "Column before running `this-command'")
694 (defvar flyspell-pre-pre-buffer nil)
695 (defvar flyspell-pre-pre-point nil)
696 (make-variable-buffer-local 'flyspell-pre-point) ;Why?? --Stef
698 ;;*---------------------------------------------------------------------*/
699 ;;* flyspell-previous-command ... */
700 ;;*---------------------------------------------------------------------*/
701 (defvar flyspell-previous-command nil
702 "The last interactive command checked by Flyspell.")
704 ;;*---------------------------------------------------------------------*/
705 ;;* flyspell-pre-command-hook ... */
706 ;;*---------------------------------------------------------------------*/
707 (defun flyspell-pre-command-hook ()
708 "Save the current buffer and point for Flyspell's post-command hook."
710 (setq flyspell-pre-buffer (current-buffer))
711 (setq flyspell-pre-point (point))
712 (setq flyspell-pre-column (current-column)))
714 ;;*---------------------------------------------------------------------*/
715 ;;* flyspell-mode-off ... */
716 ;;*---------------------------------------------------------------------*/
718 (defun flyspell-mode-off ()
719 "Turn Flyspell mode off."
720 ;; We remove the hooks.
721 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
722 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
723 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
724 (remove-hook 'hack-local-variables-hook
725 (function flyspell-hack-local-variables-hook) t)
726 ;; We remove all the flyspell highlightings.
727 (flyspell-delete-all-overlays)
728 ;; We have to erase pre cache variables.
729 (setq flyspell-pre-buffer nil)
730 (setq flyspell-pre-point nil)
731 ;; We mark the mode as killed.
732 (setq flyspell-mode nil))
734 ;;*---------------------------------------------------------------------*/
735 ;;* flyspell-check-pre-word-p ... */
736 ;;*---------------------------------------------------------------------*/
737 (defun flyspell-check-pre-word-p ()
738 "Return non-nil if we should check the word before point.
739 More precisely, it applies to the word that was before point
740 before the current command."
741 (let ((ispell-otherchars (ispell-get-otherchars)))
743 ((not (and (numberp flyspell-pre-point)
744 (eq flyspell-pre-buffer (current-buffer))))
746 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
747 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
749 ((or (and (= flyspell-pre-point (- (point) 1))
750 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
751 (and (not (string= "" ispell-otherchars))
754 (buffer-substring-no-properties
755 flyspell-pre-point (1+ flyspell-pre-point))))))
756 (= flyspell-pre-point (point))
757 (= flyspell-pre-point (+ (point) 1)))
759 ((and (symbolp this-command)
760 (not executing-kbd-macro)
761 (or (get this-command 'flyspell-delayed)
762 (and (get this-command 'flyspell-deplacement)
763 (eq flyspell-previous-command this-command)))
764 (or (= (current-column) 0)
765 (= (current-column) flyspell-pre-column)
766 ;; If other post-command-hooks change the buffer,
767 ;; flyspell-pre-point can lie past eob (bug#468).
768 (null (char-after flyspell-pre-point))
769 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
770 (and (not (string= "" ispell-otherchars))
773 (buffer-substring-no-properties
774 flyspell-pre-point (1+ flyspell-pre-point)))))))
776 ((not (eq (current-buffer) flyspell-pre-buffer))
778 ((not (and (numberp flyspell-word-cache-start)
779 (numberp flyspell-word-cache-end)))
782 (or (< flyspell-pre-point flyspell-word-cache-start)
783 (> flyspell-pre-point flyspell-word-cache-end))))))
785 ;;*---------------------------------------------------------------------*/
786 ;;* The flyspell after-change-hook, store the change position. In */
787 ;;* the post command hook, we will check, if the word at this */
788 ;;* position has to be spell checked. */
789 ;;*---------------------------------------------------------------------*/
790 (defvar flyspell-changes nil)
791 (make-variable-buffer-local 'flyspell-changes)
793 ;;*---------------------------------------------------------------------*/
794 ;;* flyspell-after-change-function ... */
795 ;;*---------------------------------------------------------------------*/
796 (defun flyspell-after-change-function (start stop _len)
797 "Save the current buffer and point for Flyspell's post-command hook."
798 (push (cons start stop) flyspell-changes))
800 ;;*---------------------------------------------------------------------*/
801 ;;* flyspell-check-changed-word-p ... */
802 ;;*---------------------------------------------------------------------*/
803 (defun flyspell-check-changed-word-p (start stop)
804 "Return non-nil when the changed word has to be checked.
805 The answer depends of several criteria.
806 Mostly we check word delimiters."
807 (not (and (not (and (memq (char-after start) '(?\n ? )) (> stop start)))
808 (numberp flyspell-pre-point)
810 (and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
812 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))))))
814 ;;*---------------------------------------------------------------------*/
815 ;;* flyspell-check-word-p ... */
816 ;;*---------------------------------------------------------------------*/
817 (defun flyspell-check-word-p ()
818 "Return t when the word at `point' has to be checked.
819 The answer depends of several criteria.
820 Mostly we check word delimiters."
821 (let ((ispell-otherchars (ispell-get-otherchars)))
823 ((<= (- (point-max) 1) (point-min))
824 ;; The buffer is not filled enough.
826 ((and (and (> (current-column) 0)
827 (not (eq (current-column) flyspell-pre-column)))
830 (and (looking-at (flyspell-get-not-casechars))
831 (or (string= "" ispell-otherchars)
832 (not (looking-at ispell-otherchars)))
833 (or flyspell-consider-dash-as-word-delimiter-flag
834 (not (looking-at "-"))))))
835 ;; Yes because we have reached or typed a word delimiter.
837 ((symbolp this-command)
839 ((get this-command 'flyspell-deplacement)
840 (not (eq flyspell-previous-command this-command)))
841 ((get this-command 'flyspell-delayed)
842 ;; The current command is not delayed, that
843 ;; is that we must check the word now.
844 (and (not unread-command-events)
845 (sit-for flyspell-delay)))
849 ;;*---------------------------------------------------------------------*/
850 ;;* flyspell-debug-signal-no-check ... */
851 ;;*---------------------------------------------------------------------*/
852 (defun flyspell-debug-signal-no-check (msg obj)
853 (setq debug-on-error t)
854 (with-current-buffer (get-buffer-create "*flyspell-debug*")
856 (insert "NO-CHECK:\n")
857 (insert (format " %S : %S\n" msg obj))))
859 ;;*---------------------------------------------------------------------*/
860 ;;* flyspell-debug-signal-pre-word-checked ... */
861 ;;*---------------------------------------------------------------------*/
862 (defun flyspell-debug-signal-pre-word-checked ()
863 (setq debug-on-error t)
864 (with-current-buffer (get-buffer-create "*flyspell-debug*")
865 (insert "PRE-WORD:\n")
866 (insert (format " pre-point : %S\n" flyspell-pre-point))
867 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
868 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
869 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
870 (goto-char (point-max))))
872 ;;*---------------------------------------------------------------------*/
873 ;;* flyspell-debug-signal-word-checked ... */
874 ;;*---------------------------------------------------------------------*/
875 (defun flyspell-debug-signal-word-checked ()
876 (setq debug-on-error t)
877 (let ((ispell-otherchars (ispell-get-otherchars))
878 (oldbuf (current-buffer))
880 (with-current-buffer (get-buffer-create "*flyspell-debug*")
883 (format " this-cmd : %S\n" this-command)
884 (format " delayed : %S\n" (and (symbolp this-command)
887 (format " point : %S\n" point)
888 (format " prev-char : [%c] %S\n"
889 (with-current-buffer oldbuf
890 (if (bobp) ?\ (char-before)))
891 (with-current-buffer oldbuf
896 (and (looking-at (flyspell-get-not-casechars))
897 (or (string= "" ispell-otherchars)
898 (not (looking-at ispell-otherchars)))
899 (or flyspell-consider-dash-as-word-delimiter-flag
900 (not (looking-at "\\-")))
902 (format " because : %S\n"
904 ((not (and (symbolp this-command)
905 (get this-command 'flyspell-delayed)))
906 ;; The current command is not delayed, that
907 ;; is that we must check the word now.
909 ((with-current-buffer oldbuf
914 (and (looking-at (flyspell-get-not-casechars))
915 (or (string= "" ispell-otherchars)
916 (not (looking-at ispell-otherchars)))
917 (or flyspell-consider-dash-as-word-delimiter-flag
918 (not (looking-at "\\-")))))))
919 ;; Yes because we have reached or typed a word delimiter.
921 ((not (integerp flyspell-delay))
922 ;; Yes because the user set up a no-delay configuration.
926 (goto-char (point-max)))))
928 ;;*---------------------------------------------------------------------*/
929 ;;* flyspell-debug-signal-changed-checked ... */
930 ;;*---------------------------------------------------------------------*/
931 (defun flyspell-debug-signal-changed-checked ()
932 (setq debug-on-error t)
933 (let ((point (point)))
934 (with-current-buffer (get-buffer-create "*flyspell-debug*")
935 (insert "CHANGED WORD:\n")
936 (insert (format " point : %S\n" point))
937 (goto-char (point-max)))))
939 ;;*---------------------------------------------------------------------*/
940 ;;* flyspell-post-command-hook ... */
941 ;;* ------------------------------------------------------------- */
942 ;;* It is possible that we check several words: */
943 ;;* 1- the current word is checked if the predicate */
944 ;;* FLYSPELL-CHECK-WORD-P is true */
945 ;;* 2- the word that used to be the current word before the */
946 ;;* THIS-COMMAND is checked if: */
947 ;;* a- the previous word is different from the current word */
948 ;;* b- the previous word has not just been checked by the */
949 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
950 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
951 ;;* previous word nor the current word */
952 ;;*---------------------------------------------------------------------*/
953 (defun flyspell-post-command-hook ()
954 "The `post-command-hook' used by flyspell to check a word on-the-fly."
958 (let ((command this-command)
959 ;; Prevent anything we do from affecting the mark.
961 (if (flyspell-check-pre-word-p)
963 '(flyspell-debug-signal-pre-word-checked)
964 (goto-char flyspell-pre-point)
966 (if (flyspell-check-word-p)
968 '(flyspell-debug-signal-word-checked)
969 ;; FIXME: This should be asynchronous!
971 ;; we remember which word we have just checked.
972 ;; this will be used next time we will check a word
973 ;; to compare the next current word with the word
974 ;; that has been registered in the pre-command-hook
975 ;; that is these variables are used within the predicate
976 ;; FLYSPELL-CHECK-PRE-WORD-P
977 (setq flyspell-pre-pre-buffer (current-buffer))
978 (setq flyspell-pre-pre-point (point)))
979 (setq flyspell-pre-pre-buffer nil)
980 (setq flyspell-pre-pre-point nil)
981 ;; when a word is not checked because of a delayed command
982 ;; we do not disable the ispell cache.
983 (when (and (symbolp this-command)
984 (get this-command 'flyspell-delayed))
985 (setq flyspell-word-cache-end -1)
986 (setq flyspell-word-cache-result '_)))
987 (while (and (not (input-pending-p)) (consp flyspell-changes))
988 (let ((start (car (car flyspell-changes)))
989 (stop (cdr (car flyspell-changes))))
990 (if (flyspell-check-changed-word-p start stop)
992 '(flyspell-debug-signal-changed-checked)
995 (setq flyspell-changes (cdr flyspell-changes))))
996 (setq flyspell-previous-command command)))))
998 ;;*---------------------------------------------------------------------*/
999 ;;* flyspell-notify-misspell ... */
1000 ;;*---------------------------------------------------------------------*/
1001 (defun flyspell-notify-misspell (word poss)
1002 (let ((replacements (if (stringp poss)
1004 (if flyspell-sort-corrections
1005 (sort (car (cdr (cdr poss))) 'string<)
1006 (car (cdr (cdr poss)))))))
1007 (if flyspell-issue-message-flag
1008 (message "misspelling `%s' %S" word replacements))))
1010 ;;*---------------------------------------------------------------------*/
1011 ;;* flyspell-word-search-backward ... */
1012 ;;*---------------------------------------------------------------------*/
1013 (defun flyspell-word-search-backward (word bound &optional ignore-case)
1016 (inhibit-point-motion-hooks t)
1018 (while (and (not r) (setq p (search-backward word bound t)))
1019 (let ((lw (flyspell-get-word)))
1022 (string-equal (downcase (car lw)) (downcase word))
1023 (string-equal (car lw) word)))
1028 ;;*---------------------------------------------------------------------*/
1029 ;;* flyspell-word-search-forward ... */
1030 ;;*---------------------------------------------------------------------*/
1031 (defun flyspell-word-search-forward (word bound)
1034 (inhibit-point-motion-hooks t)
1036 (while (and (not r) (setq p (search-forward word bound t)))
1037 (let ((lw (flyspell-get-word)))
1038 (if (and (consp lw) (string-equal (car lw) word))
1040 (goto-char (1+ p)))))
1043 ;;*---------------------------------------------------------------------*/
1044 ;;* flyspell-word ... */
1045 ;;*---------------------------------------------------------------------*/
1046 (defun flyspell-word (&optional following known-misspelling)
1047 "Spell check a word.
1048 If the optional argument FOLLOWING, or, when called interactively
1049 `ispell-following-word', is non-nil, checks the following (rather
1050 than preceding) word when the cursor is not over a word. If
1051 optional argument KNOWN-MISSPELLING is non nil considers word a
1052 misspelling and skips redundant spell-checking step."
1053 (interactive (list ispell-following-word))
1054 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1056 ;; use the correct dictionary
1057 (flyspell-accept-buffer-local-defs)
1058 (let* ((cursor-location (point))
1059 (flyspell-word (flyspell-get-word following))
1060 start end poss word ispell-filter)
1061 (if (or (eq flyspell-word nil)
1062 (and (fboundp flyspell-generic-check-word-predicate)
1063 (not (funcall flyspell-generic-check-word-predicate))))
1066 ;; destructure return flyspell-word info list.
1067 (setq start (car (cdr flyspell-word))
1068 end (car (cdr (cdr flyspell-word)))
1069 word (car flyspell-word))
1070 ;; before checking in the directory, we check for doublons.
1072 ((and (or (not (eq ispell-parser 'tex))
1073 (and (> start (point-min))
1074 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1075 flyspell-mark-duplications-flag
1076 (not (catch 'exception
1077 (let ((dict (or ispell-local-dictionary
1078 ispell-dictionary)))
1079 (dolist (except flyspell-mark-duplications-exceptions)
1080 (and (or (null (car except))
1082 (string-match (car except) dict)))
1083 (member (downcase word) (cdr except))
1084 (throw 'exception t))))))
1090 (- (skip-chars-backward " \t\n\f"))))
1091 (p (when (>= bound (point-min))
1092 (flyspell-word-search-backward word bound t))))
1093 (and p (/= p start)))))
1094 ;; yes, this is a doublon
1095 (flyspell-highlight-incorrect-region start end 'doublon)
1097 ((and (eq flyspell-word-cache-start start)
1098 (eq flyspell-word-cache-end end)
1099 (string-equal flyspell-word-cache-word word))
1100 ;; this word had been already checked, we skip
1101 flyspell-word-cache-result)
1102 ((and (eq ispell-parser 'tex)
1103 (flyspell-tex-command-p flyspell-word))
1104 ;; this is a correct word (because a tex command)
1105 (flyspell-unhighlight-at start)
1107 (flyspell-unhighlight-at (- end 1)))
1110 ;; we setup the cache
1111 (setq flyspell-word-cache-start start)
1112 (setq flyspell-word-cache-end end)
1113 (setq flyspell-word-cache-word word)
1114 ;; now check spelling of word.
1115 (if (not known-misspelling)
1117 (ispell-send-string "%\n")
1118 ;; put in verbose mode
1119 (ispell-send-string (concat "^" word "\n"))
1120 ;; we mark the ispell process so it can be killed
1121 ;; when emacs is exited without query
1122 (if (featurep 'xemacs)
1123 (process-kill-without-query ispell-process)
1124 (set-process-query-on-exit-flag ispell-process nil))
1125 ;; Wait until ispell has processed word.
1127 (accept-process-output ispell-process)
1128 (not (string= "" (car ispell-filter)))))
1129 ;; (ispell-send-string "!\n")
1130 ;; back to terse mode.
1131 ;; Remove leading empty element
1132 (setq ispell-filter (cdr ispell-filter))
1133 ;; ispell process should return something after word is sent.
1134 ;; Tag word as valid (i.e., skip) otherwise
1136 (setq ispell-filter '(*)))
1137 (if (consp ispell-filter)
1138 (setq poss (ispell-parse-output (car ispell-filter)))))
1139 ;; Else, this was a known misspelling to begin with, and
1140 ;; we should forge an ispell return value.
1141 (setq poss (list word 1 nil nil)))
1142 (let ((res (cond ((eq poss t)
1144 (setq flyspell-word-cache-result t)
1145 (flyspell-unhighlight-at start)
1147 (flyspell-unhighlight-at (- end 1)))
1149 ((and (stringp poss) flyspell-highlight-flag)
1151 (setq flyspell-word-cache-result t)
1152 (flyspell-unhighlight-at start)
1154 (flyspell-unhighlight-at (- end 1)))
1157 (setq flyspell-word-cache-result t)
1158 (flyspell-unhighlight-at start)
1160 (flyspell-unhighlight-at (- end 1)))
1162 ((or (and (< flyspell-duplicate-distance 0)
1165 (flyspell-word-search-backward
1170 (flyspell-word-search-forward
1173 (and (> flyspell-duplicate-distance 0)
1176 (flyspell-word-search-backward
1179 flyspell-duplicate-distance)))
1182 (flyspell-word-search-forward
1185 flyspell-duplicate-distance))))))
1186 ;; This is a misspelled word which occurs
1187 ;; twice within flyspell-duplicate-distance.
1188 (setq flyspell-word-cache-result nil)
1189 (if flyspell-highlight-flag
1190 (flyspell-highlight-duplicate-region
1192 (message "duplicate `%s'" word))
1195 (setq flyspell-word-cache-result nil)
1196 ;; Highlight the location as incorrect,
1197 ;; including offset specified in POSS.
1198 (if flyspell-highlight-flag
1199 (flyspell-highlight-incorrect-region
1200 (if (and (consp poss)
1201 (integerp (nth 1 poss)))
1202 (+ start (nth 1 poss) -1)
1205 (flyspell-notify-misspell word poss))
1207 ;; return to original location
1208 (goto-char cursor-location)
1209 (if ispell-quit (setq ispell-quit nil))
1212 ;;*---------------------------------------------------------------------*/
1213 ;;* flyspell-math-tex-command-p ... */
1214 ;;* ------------------------------------------------------------- */
1215 ;;* This function uses the texmathp package to check if point */
1216 ;;* is within a TeX math environment. `texmathp' can yield errors */
1217 ;;* if the document is currently not valid TeX syntax. */
1218 ;;*---------------------------------------------------------------------*/
1219 (defun flyspell-math-tex-command-p ()
1220 (when (fboundp 'texmathp)
1221 (if flyspell-check-tex-math-command
1227 ;;*---------------------------------------------------------------------*/
1228 ;;* flyspell-tex-command-p ... */
1229 ;;*---------------------------------------------------------------------*/
1230 (defun flyspell-tex-command-p (word)
1231 "Return t if WORD is a TeX command."
1233 (let ((b (car (cdr word))))
1234 (and (re-search-backward "\\\\" (- (point) 100) t)
1235 (or (= (match-end 0) b)
1236 (and (goto-char (match-end 0))
1237 (looking-at flyspell-tex-command-regexp)
1238 (>= (match-end 0) b))))))
1239 (flyspell-math-tex-command-p)))
1241 (defalias 'flyspell-get-casechars 'ispell-get-casechars)
1242 (defalias 'flyspell-get-not-casechars 'ispell-get-not-casechars)
1244 ;;*---------------------------------------------------------------------*/
1245 ;;* flyspell-get-word ... */
1246 ;;*---------------------------------------------------------------------*/
1247 (defun flyspell-get-word (&optional following extra-otherchars)
1248 "Return the word for spell-checking according to Ispell syntax.
1249 Optional argument FOLLOWING non-nil means to get the following
1250 \(rather than preceding) word when the cursor is not over a word.
1251 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1252 that may be included as part of a word (see `ispell-dictionary-alist')."
1253 (let* ((flyspell-casechars (flyspell-get-casechars))
1254 (flyspell-not-casechars (flyspell-get-not-casechars))
1255 (ispell-otherchars (ispell-get-otherchars))
1256 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1257 (word-regexp (concat flyspell-casechars
1259 (if (not (string= "" ispell-otherchars))
1260 (concat ispell-otherchars "?"))
1261 (if extra-otherchars
1262 (concat extra-otherchars "?"))
1265 (if (or ispell-many-otherchars-p
1271 (if (not (looking-at flyspell-casechars))
1273 (re-search-forward flyspell-casechars nil t)
1274 (re-search-backward flyspell-casechars nil t)))
1275 ;; move to front of word
1276 (re-search-backward flyspell-not-casechars nil 'start)
1277 (while (and (or (and (not (string= "" ispell-otherchars))
1278 (looking-at ispell-otherchars))
1279 (and extra-otherchars (looking-at extra-otherchars)))
1281 (or (not did-it-once)
1282 ispell-many-otherchars-p)
1283 (not (eq prevpt (point))))
1284 (if (and extra-otherchars (looking-at extra-otherchars))
1287 (if (looking-at flyspell-casechars)
1288 (re-search-backward flyspell-not-casechars nil 'move)))
1292 (if (looking-at flyspell-casechars)
1293 (re-search-backward flyspell-not-casechars nil 'move)
1294 (backward-char -1))))
1295 ;; Now mark the word and save to string.
1296 (if (not (re-search-forward word-regexp nil t))
1299 (setq start (match-beginning 0)
1301 word (buffer-substring-no-properties start end))
1302 (list word start end)))))
1304 ;;*---------------------------------------------------------------------*/
1305 ;;* flyspell-small-region ... */
1306 ;;*---------------------------------------------------------------------*/
1307 (defun flyspell-small-region (beg end)
1308 "Flyspell text between BEG and END."
1316 (while (< (point) end)
1317 (if (and flyspell-issue-message-flag (= count 100))
1319 (message "Spell Checking...%d%%"
1320 (* 100 (/ (float (- (point) beg)) (- end beg))))
1322 (setq count (+ 1 count)))
1325 (let ((cur (point)))
1327 (if (and (< (point) end) (> (point) (+ cur 1)))
1328 (backward-char 1)))))
1330 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1333 ;;*---------------------------------------------------------------------*/
1334 ;;* flyspell-external-ispell-process ... */
1335 ;;*---------------------------------------------------------------------*/
1336 (defvar flyspell-external-ispell-process '()
1337 "The external Flyspell Ispell process.")
1339 ;;*---------------------------------------------------------------------*/
1340 ;;* flyspell-external-ispell-buffer ... */
1341 ;;*---------------------------------------------------------------------*/
1342 (defvar flyspell-external-ispell-buffer '())
1343 (defvar flyspell-large-region-buffer '())
1344 (defvar flyspell-large-region-beg (point-min))
1345 (defvar flyspell-large-region-end (point-max))
1347 ;;*---------------------------------------------------------------------*/
1348 ;;* flyspell-external-point-words ... */
1349 ;;*---------------------------------------------------------------------*/
1350 (defun flyspell-external-point-words ()
1351 "Mark words from a buffer listing incorrect words in order of appearance.
1352 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1353 \(We finish by killing that buffer and setting the variable to nil.)
1354 The buffer to mark them in is `flyspell-large-region-buffer'."
1355 (let (words-not-found
1356 (ispell-otherchars (ispell-get-otherchars))
1357 (buffer-scan-pos flyspell-large-region-beg)
1359 (with-current-buffer flyspell-external-ispell-buffer
1360 (goto-char (point-min))
1361 ;; Loop over incorrect words, in the order they were reported,
1362 ;; which is also the order they appear in the buffer being checked.
1363 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1364 ;; Bind WORD to the next one.
1365 (let ((word (match-string 1)) (wordpos (point)))
1366 ;; Here there used to be code to see if WORD is the same
1367 ;; as the previous iteration, and count the number of consecutive
1368 ;; identical words, and the loop below would search for that many.
1369 ;; That code seemed to be incorrect, and on principle, should
1370 ;; be unnecessary too. -- rms.
1371 (if flyspell-issue-message-flag
1372 (message "Spell Checking...%d%% [%s]"
1373 (* 100 (/ (float (point)) (point-max)))
1375 (with-current-buffer flyspell-large-region-buffer
1376 (goto-char buffer-scan-pos)
1378 ;; Iterate on string search until string is found as word,
1379 ;; not as substring.
1381 (if (search-forward word
1382 flyspell-large-region-end t)
1385 ;; Move back into the match
1386 ;; so flyspell-get-word will find it.
1388 (flyspell-get-word)))
1389 (found (car found-list))
1390 (found-length (length found))
1391 (misspell-length (length word)))
1393 ;; Size matches, we really found it.
1394 (= found-length misspell-length)
1395 ;; Matches as part of a boundary-char separated
1398 (split-string found ispell-otherchars))
1399 ;; Misspelling has higher length than
1400 ;; what flyspell considers the word.
1401 ;; Caused by boundary-chars mismatch.
1402 ;; Validating seems safe.
1403 (< found-length misspell-length)
1404 ;; ispell treats beginning of some TeX
1405 ;; commands as nroff control sequences
1406 ;; and strips them in the list of
1407 ;; misspelled words thus giving a
1408 ;; non-existent word. Skip if ispell
1409 ;; is used, string is a TeX command
1410 ;; (char before beginning of word is
1411 ;; backslash) and none of the previous
1412 ;; conditions match.
1413 (and (not ispell-really-aspell)
1415 (goto-char (- (nth 1 found-list) 1))
1416 (if (looking-at "[\\]" )
1420 (flyspell-word nil t)
1421 ;; Search for next misspelled word will begin from
1422 ;; end of last validated match.
1423 (setq buffer-scan-pos (point))))
1424 ;; Record if misspelling is not found and try new one
1425 (cl-pushnew (concat " -> " word " - "
1426 (int-to-string wordpos))
1427 words-not-found :test #'equal)
1428 (setq keep nil)))))))
1430 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1431 ;; Warn about not found misspellings
1432 (dolist (word words-not-found)
1433 (message "%s: word not found" word))
1434 ;; Kill and forget the buffer with the list of incorrect words.
1435 (kill-buffer flyspell-external-ispell-buffer)
1436 (setq flyspell-external-ispell-buffer nil)))
1438 ;;*---------------------------------------------------------------------*/
1439 ;;* flyspell-process-localwords ... */
1440 ;;* ------------------------------------------------------------- */
1441 ;;* This function is used to prevent marking of words explicitly */
1442 ;;* declared correct. */
1443 ;;*---------------------------------------------------------------------*/
1444 (defun flyspell-process-localwords (misspellings-buffer)
1445 (let ((localwords ispell-buffer-session-localwords)
1447 (ispell-casechars (ispell-get-casechars)))
1448 ;; Get localwords from the original buffer
1450 (goto-char (point-min))
1451 ;; Localwords parsing copied from ispell.el.
1452 (while (search-forward ispell-words-keyword nil t)
1453 (let ((end (point-at-eol))
1455 ;; buffer-local words separated by a space, and can contain
1456 ;; any character other than a space. Not rigorous enough.
1457 (while (re-search-forward " *\\([^ ]+\\)" end t)
1458 (setq string (buffer-substring-no-properties (match-beginning 1)
1460 ;; This can fail when string contains a word with invalid chars.
1461 ;; Error handling needs to be added between Ispell and Emacs.
1462 (if (and (< 1 (length string))
1463 (equal 0 (string-match ispell-casechars string)))
1464 (push string localwords))))))
1465 ;; Remove localwords matches from misspellings-buffer.
1466 ;; The usual mechanism of communicating the local words to ispell
1467 ;; does not affect the special ispell process used by
1468 ;; flyspell-large-region.
1469 (with-current-buffer misspellings-buffer
1471 (dolist (word localwords)
1472 (goto-char (point-min))
1473 (let ((regexp (concat "^" word "\n")))
1474 (while (re-search-forward regexp nil t)
1475 (delete-region (match-beginning 0) (match-end 0)))))))))
1477 ;;* ---------------------------------------------------------------
1478 ;;* flyspell-check-region-doublons
1479 ;;* ---------------------------------------------------------------
1480 (defun flyspell-check-region-doublons (beg end)
1481 "Check for adjacent duplicated words (doublons) in the given region."
1484 (flyspell-word) ; Make sure current word is checked
1486 (while (and (< (point) end)
1487 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1493 ;;*---------------------------------------------------------------------*/
1494 ;;* flyspell-large-region ... */
1495 ;;*---------------------------------------------------------------------*/
1496 (defun flyspell-large-region (beg end)
1497 (let* ((curbuf (current-buffer))
1498 (buffer (get-buffer-create "*flyspell-region*")))
1499 (setq flyspell-external-ispell-buffer buffer)
1500 (setq flyspell-large-region-buffer curbuf)
1501 (setq flyspell-large-region-beg beg)
1502 (setq flyspell-large-region-end end)
1503 (flyspell-accept-buffer-local-defs)
1506 ;; this is done, we can start checking...
1507 (if flyspell-issue-message-flag (message "Checking region..."))
1509 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1510 ;; Local dictionary becomes the global dictionary in use.
1511 (setq ispell-current-dictionary
1512 (or ispell-local-dictionary ispell-dictionary))
1513 (setq ispell-current-personal-dictionary
1514 (or ispell-local-pdict ispell-personal-dictionary))
1515 (let ((args (ispell-get-ispell-args))
1516 (encoding (ispell-get-coding-system))
1518 (if (and ispell-current-dictionary ; use specified dictionary
1519 (not (member "-d" args))) ; only define if not overridden
1521 (append (list "-d" ispell-current-dictionary) args)))
1522 (if ispell-current-personal-dictionary ; use specified pers dict
1527 ispell-current-personal-dictionary)))))
1529 ;; Check for extended character mode
1530 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1531 (and extended-char-mode ; ~ extended character mode
1532 (string-match "[^~]+$" extended-char-mode)
1533 (cl-pushnew (concat "-T" (match-string 0 extended-char-mode))
1534 args :test #'equal)))
1536 ;; Add ispell-extra-args
1537 (setq args (append args ispell-extra-args))
1539 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1540 ;; for communication. ispell or older aspell/hunspell does not support this
1541 (if ispell-encoding8-command
1544 (if ispell-really-hunspell
1545 (list ispell-encoding8-command
1546 (upcase (symbol-name encoding)))
1547 (list (concat ispell-encoding8-command
1548 (symbol-name encoding)))))))
1550 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1551 (setq c (apply 'ispell-call-process-region beg
1557 (if ispell-really-aspell "list" "-l")
1561 (flyspell-process-localwords buffer)
1562 (with-current-buffer curbuf
1563 (flyspell-delete-region-overlays beg end)
1564 (flyspell-check-region-doublons beg end))
1565 (flyspell-external-point-words))
1566 (error "Can't check region")))))
1568 ;;*---------------------------------------------------------------------*/
1569 ;;* flyspell-region ... */
1570 ;;* ------------------------------------------------------------- */
1571 ;;* Because `ispell -a' is too slow, it is not possible to use */
1572 ;;* it on large region. Then, when ispell is invoked on a large */
1573 ;;* text region, a new `ispell -l' process is spawned. The */
1574 ;;* pointed out words are then searched in the region a checked with */
1575 ;;* regular flyspell means. */
1576 ;;*---------------------------------------------------------------------*/
1578 (defun flyspell-region (beg end)
1579 "Flyspell text between BEG and END."
1581 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1589 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1590 (flyspell-large-region beg end)
1591 (flyspell-small-region beg end)))))
1593 ;;*---------------------------------------------------------------------*/
1594 ;;* flyspell-buffer ... */
1595 ;;*---------------------------------------------------------------------*/
1597 (defun flyspell-buffer ()
1598 "Flyspell whole buffer."
1600 (flyspell-region (point-min) (point-max)))
1602 ;;*---------------------------------------------------------------------*/
1603 ;;* old next error position ... */
1604 ;;*---------------------------------------------------------------------*/
1605 (defvar flyspell-old-buffer-error nil)
1606 (defvar flyspell-old-pos-error nil)
1608 ;;*---------------------------------------------------------------------*/
1609 ;;* flyspell-goto-next-error ... */
1610 ;;*---------------------------------------------------------------------*/
1611 (defun flyspell-goto-next-error ()
1612 "Go to the next previously detected error.
1613 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1618 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1619 (eq pos flyspell-old-pos-error))
1621 (if (= flyspell-old-pos-error max)
1622 ;; goto beginning of buffer
1624 (message "Restarting from beginning of buffer")
1625 (goto-char (point-min)))
1627 (setq pos (point))))
1628 ;; seek the next error
1629 (while (and (< pos max)
1630 (let ((ovs (overlays-at pos))
1632 (while (and (not r) (consp ovs))
1633 (if (flyspell-overlay-p (car ovs))
1635 (setq ovs (cdr ovs))))
1637 (setq pos (1+ pos)))
1638 ;; save the current location for next invocation
1639 (setq flyspell-old-pos-error pos)
1640 (setq flyspell-old-buffer-error (current-buffer))
1643 (message "No more miss-spelled word!"))))
1645 ;;*---------------------------------------------------------------------*/
1646 ;;* flyspell-overlay-p ... */
1647 ;;*---------------------------------------------------------------------*/
1648 (defun flyspell-overlay-p (o)
1649 "Return true if O is an overlay used by flyspell."
1650 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1652 ;;*---------------------------------------------------------------------*/
1653 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1654 ;;* ------------------------------------------------------------- */
1655 ;;* Remove overlays introduced by flyspell. */
1656 ;;*---------------------------------------------------------------------*/
1657 (defun flyspell-delete-region-overlays (beg end)
1658 "Delete overlays used by flyspell in a given region."
1659 (if (featurep 'emacs)
1660 (remove-overlays beg end 'flyspell-overlay t)
1661 ;; XEmacs does not have `remove-overlays'
1662 (let ((l (overlays-in beg end)))
1665 (if (flyspell-overlay-p (car l))
1666 (delete-overlay (car l)))
1667 (setq l (cdr l)))))))
1669 (defun flyspell-delete-all-overlays ()
1670 "Delete all the overlays used by flyspell."
1671 (flyspell-delete-region-overlays (point-min) (point-max)))
1673 ;;*---------------------------------------------------------------------*/
1674 ;;* flyspell-unhighlight-at ... */
1675 ;;*---------------------------------------------------------------------*/
1676 (defun flyspell-unhighlight-at (pos)
1677 "Remove the flyspell overlay that are located at POS."
1678 (if flyspell-persistent-highlight
1679 (let ((overlays (overlays-at pos)))
1680 (while (consp overlays)
1681 (if (flyspell-overlay-p (car overlays))
1682 (delete-overlay (car overlays)))
1683 (setq overlays (cdr overlays))))
1684 (if (flyspell-overlay-p flyspell-overlay)
1685 (delete-overlay flyspell-overlay))))
1687 ;;*---------------------------------------------------------------------*/
1688 ;;* flyspell-properties-at-p ... */
1689 ;;* ------------------------------------------------------------- */
1690 ;;* Is there an highlight properties at position pos? */
1691 ;;*---------------------------------------------------------------------*/
1692 (defun flyspell-properties-at-p (pos)
1693 "Return t if there is a text property at POS, not counting `local-map'.
1694 If variable `flyspell-highlight-properties' is set to nil,
1695 text with properties are not checked. This function is used to discover
1696 if the character at POS has any other property."
1697 (let ((prop (text-properties-at pos))
1699 (while (and keep (consp prop))
1700 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1701 (setq prop (cdr (cdr prop)))
1705 ;;*---------------------------------------------------------------------*/
1706 ;;* make-flyspell-overlay ... */
1707 ;;*---------------------------------------------------------------------*/
1708 (defun make-flyspell-overlay (beg end face mouse-face)
1709 "Allocate an overlay to highlight an incorrect word.
1710 BEG and END specify the range in the buffer of that word.
1711 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1713 (let ((overlay (make-overlay beg end nil t nil)))
1714 (overlay-put overlay 'face face)
1715 (overlay-put overlay 'mouse-face mouse-face)
1716 (overlay-put overlay 'flyspell-overlay t)
1717 (overlay-put overlay 'evaporate t)
1718 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1719 (overlay-put overlay 'keymap flyspell-mouse-map)
1720 (when (eq face 'flyspell-incorrect)
1721 (and (stringp flyspell-before-incorrect-word-string)
1722 (overlay-put overlay 'before-string
1723 flyspell-before-incorrect-word-string))
1724 (and (stringp flyspell-after-incorrect-word-string)
1725 (overlay-put overlay 'after-string
1726 flyspell-after-incorrect-word-string)))
1729 ;;*---------------------------------------------------------------------*/
1730 ;;* flyspell-highlight-incorrect-region ... */
1731 ;;*---------------------------------------------------------------------*/
1732 (defun flyspell-highlight-incorrect-region (beg end poss)
1733 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1734 POSS is usually a list of possible spelling/correction lists,
1735 as returned by `ispell-parse-output'.
1736 It can also be the symbol `doublon', in the case where the word
1737 is itself incorrect, but suspiciously repeated."
1738 (let ((inhibit-read-only t))
1739 (unless (run-hook-with-args-until-success
1740 'flyspell-incorrect-hook beg end poss)
1741 (if (or flyspell-highlight-properties
1742 (not (flyspell-properties-at-p beg)))
1744 ;; we cleanup all the overlay that are in the region, not
1745 ;; beginning at the word start position
1746 (if (< (1+ beg) end)
1747 (let ((os (overlays-in (1+ beg) end)))
1749 (if (flyspell-overlay-p (car os))
1750 (delete-overlay (car os)))
1751 (setq os (cdr os)))))
1752 ;; we cleanup current overlay at the same position
1753 (flyspell-unhighlight-at beg)
1754 ;; now we can use a new overlay
1755 (setq flyspell-overlay
1756 (make-flyspell-overlay
1758 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1761 ;;*---------------------------------------------------------------------*/
1762 ;;* flyspell-highlight-duplicate-region ... */
1763 ;;*---------------------------------------------------------------------*/
1764 (defun flyspell-highlight-duplicate-region (beg end poss)
1765 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1766 POSS is a list of possible spelling/correction lists,
1767 as returned by `ispell-parse-output'."
1768 (let ((inhibit-read-only t))
1769 (unless (run-hook-with-args-until-success
1770 'flyspell-incorrect-hook beg end poss)
1771 (if (or flyspell-highlight-properties
1772 (not (flyspell-properties-at-p beg)))
1774 ;; we cleanup current overlay at the same position
1775 (flyspell-unhighlight-at beg)
1776 ;; now we can use a new overlay
1777 (setq flyspell-overlay
1778 (make-flyspell-overlay beg end
1782 ;;*---------------------------------------------------------------------*/
1783 ;;* flyspell-auto-correct-cache ... */
1784 ;;*---------------------------------------------------------------------*/
1785 (defvar flyspell-auto-correct-pos nil)
1786 (defvar flyspell-auto-correct-region nil)
1787 (defvar flyspell-auto-correct-ring nil)
1788 (defvar flyspell-auto-correct-word nil)
1789 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1790 (make-variable-buffer-local 'flyspell-auto-correct-region)
1791 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1792 (make-variable-buffer-local 'flyspell-auto-correct-word)
1794 ;;*---------------------------------------------------------------------*/
1795 ;;* flyspell-check-previous-highlighted-word ... */
1796 ;;*---------------------------------------------------------------------*/
1797 (defun flyspell-check-previous-highlighted-word (&optional arg)
1798 "Correct the closer misspelled word.
1799 This function scans a mis-spelled word before the cursor. If it finds one
1800 it proposes replacement for that word. With prefix arg, count that many
1801 misspelled words backwards."
1803 (let ((pos1 (point))
1805 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1808 (while (and (setq pos (previous-overlay-change pos))
1811 (if (> pos (point-min))
1813 (setq ovs (overlays-at (1- pos)))
1816 (setq ovs (cdr ovs))
1817 (if (and (flyspell-overlay-p ov)
1818 (= 0 (setq arg (1- arg))))
1819 (throw 'exit t)))))))
1823 (setq flyspell-word-cache-word nil) ;; Force flyspell-word re-check
1825 (error "No word to correct before point"))))
1827 ;;*---------------------------------------------------------------------*/
1828 ;;* flyspell-display-next-corrections ... */
1829 ;;*---------------------------------------------------------------------*/
1830 (defun flyspell-display-next-corrections (corrections)
1831 (let ((string "Corrections:")
1834 (while (< (length string) 80)
1835 (if (equal (car l) flyspell-auto-correct-word)
1836 (setq pos (cons (+ 1 (length string)) pos)))
1837 (setq string (concat string " " (car l)))
1840 (let ((num (car pos)))
1841 (put-text-property num
1842 (+ num (length flyspell-auto-correct-word))
1843 'face 'flyspell-incorrect
1845 (setq pos (cdr pos)))
1846 (if (fboundp 'display-message)
1847 (display-message 'no-log string)
1848 (message "%s" string))))
1850 ;;*---------------------------------------------------------------------*/
1851 ;;* flyspell-abbrev-table ... */
1852 ;;*---------------------------------------------------------------------*/
1853 (defun flyspell-abbrev-table ()
1854 (if flyspell-use-global-abbrev-table-p
1856 (or local-abbrev-table global-abbrev-table)))
1858 ;;*---------------------------------------------------------------------*/
1859 ;;* flyspell-define-abbrev ... */
1860 ;;*---------------------------------------------------------------------*/
1861 (defun flyspell-define-abbrev (name expansion)
1862 (let ((table (flyspell-abbrev-table)))
1864 (define-abbrev table (downcase name) expansion))))
1866 ;;*---------------------------------------------------------------------*/
1867 ;;* flyspell-auto-correct-word ... */
1868 ;;*---------------------------------------------------------------------*/
1869 (defun flyspell-auto-correct-word ()
1870 "Correct the current word.
1871 This command proposes various successive corrections for the current word."
1874 (old-max (point-max)))
1875 ;; Use the correct dictionary.
1876 (flyspell-accept-buffer-local-defs)
1877 (if (and (eq flyspell-auto-correct-pos pos)
1878 (consp flyspell-auto-correct-region))
1879 ;; We have already been using the function at the same location.
1880 (let* ((start (car flyspell-auto-correct-region))
1881 (len (cdr flyspell-auto-correct-region)))
1882 (flyspell-unhighlight-at start)
1883 (delete-region start (+ start len))
1884 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1885 (let* ((word (car flyspell-auto-correct-ring))
1886 (len (length word)))
1887 (rplacd flyspell-auto-correct-region len)
1889 (if flyspell-abbrev-p
1890 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1891 flyspell-auto-correct-word)
1892 (flyspell-change-abbrev (flyspell-abbrev-table)
1893 flyspell-auto-correct-word
1895 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1896 (funcall flyspell-insert-function word)
1898 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1899 (flyspell-ajust-cursor-point pos (point) old-max)
1900 (setq flyspell-auto-correct-pos (point)))
1901 ;; Fetch the word to be checked.
1902 (let ((word (flyspell-get-word)))
1904 (let ((start (car (cdr word)))
1905 (end (car (cdr (cdr word))))
1908 (setq flyspell-auto-correct-word word)
1909 ;; Now check spelling of word..
1910 (ispell-send-string "%\n") ;Put in verbose mode.
1911 (ispell-send-string (concat "^" word "\n"))
1912 ;; Wait until ispell has processed word.
1914 (accept-process-output ispell-process)
1915 (not (string= "" (car ispell-filter)))))
1916 ;; Remove leading empty element.
1917 (setq ispell-filter (cdr ispell-filter))
1918 ;; Ispell process should return something after word is sent.
1919 ;; Tag word as valid (i.e., skip) otherwise.
1921 (setq ispell-filter '(*)))
1922 (if (consp ispell-filter)
1923 (setq poss (ispell-parse-output (car ispell-filter))))
1925 ((or (eq poss t) (stringp poss))
1926 ;; Don't correct word.
1930 (error "Ispell: error in Ispell process"))
1932 ;; The word is incorrect, we have to propose a replacement.
1933 (let ((replacements (if flyspell-sort-corrections
1934 (sort (car (cdr (cdr poss))) 'string<)
1935 (car (cdr (cdr poss))))))
1936 (setq flyspell-auto-correct-region nil)
1937 (if (consp replacements)
1939 (let ((replace (car replacements)))
1940 (let ((new-word replace))
1941 (if (not (equal new-word (car poss)))
1943 ;; the save the current replacements
1944 (setq flyspell-auto-correct-region
1945 (cons start (length new-word)))
1946 (let ((l replacements))
1947 (while (consp (cdr l))
1949 (rplacd l (cons (car poss) replacements)))
1950 (setq flyspell-auto-correct-ring
1952 (flyspell-unhighlight-at start)
1953 (delete-region start end)
1954 (funcall flyspell-insert-function new-word)
1955 (if flyspell-abbrev-p
1956 (if (flyspell-already-abbrevp
1957 (flyspell-abbrev-table) word)
1958 (flyspell-change-abbrev
1959 (flyspell-abbrev-table)
1962 (flyspell-define-abbrev word
1965 (flyspell-display-next-corrections
1966 (cons new-word flyspell-auto-correct-ring))
1967 (flyspell-ajust-cursor-point pos
1970 (setq flyspell-auto-correct-pos (point))
1971 (ispell-pdict-save t)))))))
1973 ;;*---------------------------------------------------------------------*/
1974 ;;* flyspell-auto-correct-previous-pos ... */
1975 ;;*---------------------------------------------------------------------*/
1976 (defvar flyspell-auto-correct-previous-pos nil
1977 "Holds the start of the first incorrect word before point.")
1979 ;;*---------------------------------------------------------------------*/
1980 ;;* flyspell-auto-correct-previous-hook ... */
1981 ;;*---------------------------------------------------------------------*/
1982 (defun flyspell-auto-correct-previous-hook ()
1983 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1984 Sets `flyspell-auto-correct-previous-pos' to nil"
1986 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1987 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1988 (setq flyspell-auto-correct-previous-pos nil)))
1990 ;;*---------------------------------------------------------------------*/
1991 ;;* flyspell-auto-correct-previous-word ... */
1992 ;;*---------------------------------------------------------------------*/
1993 (defun flyspell-auto-correct-previous-word (position)
1994 "Auto correct the first misspelled word that occurs before point.
1995 But don't look beyond what's visible on the screen."
1998 (let ((top (window-start))
2002 (narrow-to-region top bot)
2003 (overlay-recenter (point))
2005 (add-hook 'pre-command-hook
2006 (function flyspell-auto-correct-previous-hook) t t)
2008 (unless flyspell-auto-correct-previous-pos
2009 ;; only reset if a new overlay exists
2010 (setq flyspell-auto-correct-previous-pos nil)
2012 (let ((overlay-list (overlays-in (point-min) position))
2013 (new-overlay 'dummy-value))
2015 ;; search for previous (new) flyspell overlay
2016 (while (and new-overlay
2017 (or (not (flyspell-overlay-p new-overlay))
2018 ;; check if its face has changed
2019 (not (eq (get-char-property
2020 (overlay-start new-overlay) 'face)
2021 'flyspell-incorrect))))
2022 (setq new-overlay (car-safe overlay-list))
2023 (setq overlay-list (cdr-safe overlay-list)))
2025 ;; if nothing new exits new-overlay should be nil
2026 (if new-overlay ;; the length of the word may change so go to the start
2027 (setq flyspell-auto-correct-previous-pos
2028 (overlay-start new-overlay)))))
2030 (when flyspell-auto-correct-previous-pos
2032 (goto-char flyspell-auto-correct-previous-pos)
2033 (let ((ispell-following-word t)) ;; point is at start
2034 (if (numberp flyspell-auto-correct-previous-pos)
2035 (goto-char flyspell-auto-correct-previous-pos))
2036 (flyspell-auto-correct-word))
2037 ;; the point may have moved so reset this
2038 (setq flyspell-auto-correct-previous-pos (point))))))))
2040 ;;*---------------------------------------------------------------------*/
2041 ;;* flyspell-correct-word ... */
2042 ;;*---------------------------------------------------------------------*/
2044 (defun flyspell-correct-word (event)
2045 "Pop up a menu of possible corrections for a misspelled word.
2046 The word checked is the word at the mouse position."
2048 (let ((save (point)))
2049 (mouse-set-point event)
2050 (flyspell-correct-word-before-point event save)))
2052 (defun flyspell-correct-word-before-point (&optional event opoint)
2053 "Pop up a menu of possible corrections for misspelled word before point.
2054 If EVENT is non-nil, it is the mouse event that invoked this operation;
2055 that controls where to put the menu.
2056 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2058 (unless (mouse-position)
2059 (error "Pop-up menus do not work on this terminal"))
2060 ;; use the correct dictionary
2061 (flyspell-accept-buffer-local-defs)
2062 (or opoint (setq opoint (point)))
2063 (let ((cursor-location (point))
2064 (word (flyspell-get-word)))
2066 (let ((start (car (cdr word)))
2067 (end (car (cdr (cdr word))))
2070 ;; now check spelling of word.
2071 (ispell-send-string "%\n") ;put in verbose mode
2072 (ispell-send-string (concat "^" word "\n"))
2073 ;; wait until ispell has processed word
2075 (accept-process-output ispell-process)
2076 (not (string= "" (car ispell-filter)))))
2077 ;; Remove leading empty element
2078 (setq ispell-filter (cdr ispell-filter))
2079 ;; ispell process should return something after word is sent.
2080 ;; Tag word as valid (i.e., skip) otherwise
2082 (setq ispell-filter '(*)))
2083 (if (consp ispell-filter)
2084 (setq poss (ispell-parse-output (car ispell-filter))))
2086 ((or (eq poss t) (stringp poss))
2087 ;; don't correct word
2091 (error "Ispell: error in Ispell process"))
2093 (flyspell-xemacs-popup
2094 poss word cursor-location start end opoint))
2096 ;; The word is incorrect, we have to propose a replacement.
2097 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2098 poss word cursor-location start end opoint)))
2099 (ispell-pdict-save t)))))
2101 ;;*---------------------------------------------------------------------*/
2102 ;;* flyspell-do-correct ... */
2103 ;;*---------------------------------------------------------------------*/
2104 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2105 "The popup menu callback."
2106 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2107 ;; it instead right after calling the function.
2108 (cond ((eq replace 'ignore)
2113 (ispell-send-string (concat "*" word "\n"))
2114 ;; This was added only to the XEmacs side in revision 1.18 of
2115 ;; flyspell. I assume its absence on the Emacs side was an
2116 ;; oversight. --Stef
2117 (ispell-send-string "#\n")
2118 (flyspell-unhighlight-at cursor-location)
2119 (setq ispell-pdict-modified-p '(t)))
2120 ((or (eq replace 'buffer) (eq replace 'session))
2121 (ispell-send-string (concat "@" word "\n"))
2122 (add-to-list 'ispell-buffer-session-localwords word)
2123 (or ispell-buffer-local-name ; session localwords might conflict
2124 (setq ispell-buffer-local-name (buffer-name)))
2125 (flyspell-unhighlight-at cursor-location)
2126 (if (null ispell-pdict-modified-p)
2127 (setq ispell-pdict-modified-p
2128 (list ispell-pdict-modified-p)))
2130 (if (eq replace 'buffer)
2131 (ispell-add-per-file-word-list word)))
2133 ;; This was added only to the Emacs side. I assume its absence on
2134 ;; the XEmacs side was an oversight. --Stef
2135 (flyspell-unhighlight-at cursor-location)
2136 (let ((old-max (point-max))
2137 (new-word (if (atom replace)
2140 (cursor-location (+ (- (length word) (- end start))
2142 (unless (equal new-word (car poss))
2143 (delete-region start end)
2145 (funcall flyspell-insert-function new-word)
2146 (if flyspell-abbrev-p
2147 (flyspell-define-abbrev word new-word)))
2148 ;; In the original Emacs code, this was only called in the body
2149 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2150 (flyspell-ajust-cursor-point save cursor-location old-max)))
2155 ;;*---------------------------------------------------------------------*/
2156 ;;* flyspell-ajust-cursor-point ... */
2157 ;;*---------------------------------------------------------------------*/
2158 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2159 (if (>= save cursor-location)
2160 (let ((new-pos (+ save (- (point-max) old-max))))
2162 ((< new-pos (point-min))
2164 ((> new-pos (point-max))
2169 ;;*---------------------------------------------------------------------*/
2170 ;;* flyspell-emacs-popup ... */
2171 ;;*---------------------------------------------------------------------*/
2172 (defun flyspell-emacs-popup (event poss word)
2173 "The Emacs popup menu."
2174 (unless window-system
2175 (error "This command requires pop-up dialogs"))
2177 (let* ((mouse-pos (mouse-position))
2178 (mouse-pos (if (nth 1 mouse-pos)
2180 (set-mouse-position (car mouse-pos)
2181 (/ (frame-width) 2) 2)
2183 (setq event (list (list (car (cdr mouse-pos))
2184 (1+ (cdr (cdr mouse-pos))))
2186 (let* ((corrects (if flyspell-sort-corrections
2187 (sort (car (cdr (cdr poss))) 'string<)
2188 (car (cdr (cdr poss)))))
2189 (cor-menu (if (consp corrects)
2190 (mapcar (lambda (correct)
2191 (list correct correct))
2194 (affix (car (cdr (cdr (cdr poss)))))
2196 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2198 (list (concat "Save affix: " (car affix))
2200 '("Accept (session)" session)
2201 '("Accept (buffer)" buffer))
2202 '(("Save word" save)
2203 ("Accept (session)" session)
2204 ("Accept (buffer)" buffer)))))
2205 (if (consp cor-menu)
2206 (append cor-menu (cons "" save))
2208 (menu (cons "flyspell correction menu" base-menu)))
2209 (car (x-popup-menu event
2210 (list (format "%s [%s]" word (or ispell-local-dictionary
2214 ;;*---------------------------------------------------------------------*/
2215 ;;* flyspell-xemacs-popup ... */
2216 ;;*---------------------------------------------------------------------*/
2217 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2218 "The XEmacs popup menu."
2219 (let* ((corrects (if flyspell-sort-corrections
2220 (sort (car (cdr (cdr poss))) 'string<)
2221 (car (cdr (cdr poss)))))
2222 (cor-menu (if (consp corrects)
2223 (mapcar (lambda (correct)
2225 (list 'flyspell-do-correct
2236 (affix (car (cdr (cdr (cdr poss)))))
2238 (menu (let ((save (if (and (consp affix) show-affix-info)
2240 (concat "Save affix: " (car affix))
2241 (list 'flyspell-do-correct
2252 (list 'flyspell-do-correct
2261 (session (vector "Accept (session)"
2262 (list 'flyspell-do-correct
2271 (buffer (vector "Accept (buffer)"
2272 (list 'flyspell-do-correct
2281 (if (consp cor-menu)
2282 (append cor-menu (list "-" save session buffer))
2283 (list save session buffer)))))
2284 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2288 ;;*---------------------------------------------------------------------*/
2289 ;;* Some example functions for real autocorrecting */
2290 ;;*---------------------------------------------------------------------*/
2291 (defun flyspell-maybe-correct-transposition (beg end poss)
2292 "Check replacements for transposed characters.
2294 If the text between BEG and END is equal to a correction suggested by
2295 Ispell, after transposing two adjacent characters, correct the text,
2298 The third arg POSS is either the symbol 'doublon' or a list of
2299 possible corrections as returned by `ispell-parse-output'.
2301 This function is meant to be added to `flyspell-incorrect-hook'."
2304 (let ((str (buffer-substring beg end))
2305 (i 0) (len (- end beg)) tmp)
2306 (while (< (1+ i) len)
2307 (setq tmp (aref str i))
2308 (aset str i (aref str (1+ i)))
2309 (aset str (1+ i) tmp)
2310 (when (member str (nth 2 poss))
2312 (goto-char (+ beg i 1))
2313 (transpose-chars 1))
2315 (setq tmp (aref str i))
2316 (aset str i (aref str (1+ i)))
2317 (aset str (1+ i) tmp)
2321 (defun flyspell-maybe-correct-doubling (beg end poss)
2322 "Check replacements for doubled characters.
2324 If the text between BEG and END is equal to a correction suggested by
2325 Ispell, after removing a pair of doubled characters, correct the text,
2328 The third arg POSS is either the symbol 'doublon' or a list of
2329 possible corrections as returned by `ispell-parse-output'.
2331 This function is meant to be added to `flyspell-incorrect-hook'."
2334 (let ((str (buffer-substring beg end))
2335 (i 0) (len (- end beg)))
2336 (while (< (1+ i) len)
2337 (when (and (= (aref str i) (aref str (1+ i)))
2338 (member (concat (substring str 0 (1+ i))
2339 (substring str (+ i 2)))
2341 (goto-char (+ beg i))
2347 ;;*---------------------------------------------------------------------*/
2348 ;;* flyspell-already-abbrevp ... */
2349 ;;*---------------------------------------------------------------------*/
2350 (defun flyspell-already-abbrevp (table word)
2351 (let ((sym (abbrev-symbol word table)))
2352 (and sym (symbolp sym))))
2354 ;;*---------------------------------------------------------------------*/
2355 ;;* flyspell-change-abbrev ... */
2356 ;;*---------------------------------------------------------------------*/
2357 (defun flyspell-change-abbrev (table old new)
2358 (set (abbrev-symbol old table) new))
2362 ;;; flyspell.el ends here