1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000-2012 Free Software Foundation, Inc.
5 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
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.
43 ;;*---------------------------------------------------------------------*/
45 ;;*---------------------------------------------------------------------*/
46 (defgroup flyspell nil
47 "Spell checking on the fly."
53 ;;*---------------------------------------------------------------------*/
54 ;;* User configuration ... */
55 ;;*---------------------------------------------------------------------*/
56 (defcustom flyspell-highlight-flag t
57 "How Flyspell should indicate misspelled words.
58 Non-nil means use highlight, nil means use minibuffer messages."
62 (defcustom flyspell-mark-duplications-flag t
63 "Non-nil means Flyspell reports a repeated word as an error.
64 See `flyspell-mark-duplications-exceptions' to add exceptions to this rule.
65 Detection of repeated words is not implemented in
66 \"large\" regions; see `flyspell-large-region'."
70 (defcustom flyspell-mark-duplications-exceptions
71 '((nil .
("that" "had")) ; Common defaults for English.
72 ("\\`francais" .
("nous" "vous")))
73 "A list of exceptions for duplicated words.
74 It should be a list of (LANGUAGE . EXCEPTION-LIST).
76 LANGUAGE is nil, which means the exceptions apply regardless of
77 the current dictionary, or a regular expression matching the
78 dictionary name (`ispell-local-dictionary' or
79 `ispell-dictionary') for which the exceptions should apply.
81 EXCEPTION-LIST is a list of strings. The checked word is
82 downcased before comparing with these exceptions."
84 :type
'(alist :key-type
(choice (const :tag
"All dictionaries" nil
)
86 :value-type
(repeat string
))
89 (defcustom flyspell-sort-corrections nil
90 "Non-nil means, sort the corrections alphabetically before popping them."
95 (defcustom flyspell-duplicate-distance -
1
96 "The maximum distance for finding duplicates of unrecognized words.
97 This applies to the feature that when a word is not found in the dictionary,
98 if the same spelling occurs elsewhere in the buffer,
99 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
100 This variable specifies how far to search to find such a duplicate.
101 -1 means no limit (search the whole buffer).
102 0 means do not search for duplicate unrecognized spellings."
105 :type
'(choice (const :tag
"no limit" -
1)
108 (defcustom flyspell-delay
3
109 "The number of seconds to wait before checking, after a \"delayed\" command."
113 (defcustom flyspell-persistent-highlight t
114 "Non-nil means misspelled words remain highlighted until corrected.
115 If this variable is nil, only the most recently detected misspelled word
120 (defcustom flyspell-highlight-properties t
121 "Non-nil means highlight incorrect words even if a property exists for this word."
125 (defcustom flyspell-default-delayed-commands
126 '(self-insert-command
128 backward-or-forward-delete-char
130 scrollbar-vertical-drag
131 backward-delete-char-untabify
)
132 "The standard list of delayed commands for Flyspell.
133 See `flyspell-delayed-commands'."
136 :type
'(repeat (symbol)))
138 (defcustom flyspell-delayed-commands nil
139 "List of commands that are \"delayed\" for Flyspell mode.
140 After these commands, Flyspell checking is delayed for a short time,
141 whose length is specified by `flyspell-delay'."
143 :type
'(repeat (symbol)))
145 (defcustom flyspell-default-deplacement-commands
150 "The standard list of deplacement commands for Flyspell.
151 See `flyspell-deplacement-commands'."
154 :type
'(repeat (symbol)))
156 (defcustom flyspell-deplacement-commands nil
157 "List of commands that are \"deplacement\" for Flyspell mode.
158 After these commands, Flyspell checking is performed only if the previous
159 command was not the very same command."
162 :type
'(repeat (symbol)))
164 (defcustom flyspell-issue-welcome-flag t
165 "Non-nil means that Flyspell should display a welcome message when started."
169 (defcustom flyspell-issue-message-flag t
170 "Non-nil means that Flyspell emits messages when checking words."
174 (defcustom flyspell-incorrect-hook nil
175 "List of functions to be called when incorrect words are encountered.
176 Each function is given three arguments. The first two
177 arguments are the beginning and the end of the incorrect region.
178 The third is either the symbol `doublon' or the list
179 of possible corrections as returned by `ispell-parse-output'.
181 If any of the functions return non-nil, the word is not highlighted as
187 (defcustom flyspell-default-dictionary nil
188 "A string that is the name of the default dictionary.
189 This is passed to the `ispell-change-dictionary' when flyspell is started.
190 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
191 when flyspell is started, the value of that variable is used instead
192 of `flyspell-default-dictionary' to select the default dictionary.
193 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
194 Ispell's ultimate default dictionary."
197 :type
'(choice string
(const :tag
"Default" nil
)))
199 (defcustom flyspell-tex-command-regexp
200 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
201 "A string that is the regular expression that matches TeX commands."
206 (defcustom flyspell-check-tex-math-command nil
207 "Non-nil means check even inside TeX math environment.
208 TeX math environments are discovered by `texmathp', implemented
209 inside AUCTeX package. That package may be found at
210 URL `http://www.gnu.org/software/auctex/'"
214 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
215 '("francais" "deutsch8" "norsk")
216 "List of dictionary names that consider `-' as word delimiter."
219 :type
'(repeat (string)))
221 (defcustom flyspell-abbrev-p
223 "If non-nil, add correction to abbreviation table."
228 (defcustom flyspell-use-global-abbrev-table-p
230 "If non-nil, prefer global abbrev table to local abbrev table."
235 (defcustom flyspell-mode-line-string
" Fly"
236 "String displayed on the modeline when flyspell is active.
237 Set this to nil if you don't want a modeline indicator."
239 :type
'(choice string
(const :tag
"None" nil
)))
241 (defcustom flyspell-large-region
1000
242 "The threshold that determines if a region is small.
243 If the region is smaller than this number of characters,
244 `flyspell-region' checks the words sequentially using regular
245 flyspell methods. Else, if the region is large, a new Ispell process is
248 Doubled words are not detected in a large region, because Ispell
249 does not check for them.
251 If this variable is nil, all regions are treated as small."
254 :type
'(choice number
(const :tag
"All small" nil
)))
256 (defcustom flyspell-insert-function
(function insert
)
257 "Function for inserting word by flyspell upon correction."
261 (defcustom flyspell-before-incorrect-word-string nil
262 "String used to indicate an incorrect word starting."
264 :type
'(choice string
(const nil
)))
266 (defcustom flyspell-after-incorrect-word-string nil
267 "String used to indicate an incorrect word ending."
269 :type
'(choice string
(const nil
)))
271 (defvar flyspell-mode-map
)
273 (defcustom flyspell-use-meta-tab t
274 "Non-nil means that flyspell uses M-TAB to correct word."
277 :initialize
'custom-initialize-default
278 :set
(lambda (sym val
)
279 (define-key flyspell-mode-map
"\M-\t"
281 'flyspell-auto-correct-word
))))
283 (defcustom flyspell-auto-correct-binding
285 "The key binding for flyspell auto correction."
288 ;;*---------------------------------------------------------------------*/
289 ;;* Mode specific options */
290 ;;* ------------------------------------------------------------- */
291 ;;* Mode specific options enable users to disable flyspell on */
292 ;;* certain word depending of the emacs mode. For instance, when */
293 ;;* using flyspell with mail-mode add the following expression */
294 ;;* in your .emacs file: */
295 ;;* (add-hook 'mail-mode */
296 ;;* (lambda () (setq flyspell-generic-check-word-predicate */
297 ;;* 'mail-mode-flyspell-verify))) */
298 ;;*---------------------------------------------------------------------*/
299 (defvar flyspell-generic-check-word-predicate nil
300 "Function providing per-mode customization over which words are flyspelled.
301 Returns t to continue checking, nil otherwise.
302 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
303 property of the major mode name.")
304 (make-variable-buffer-local 'flyspell-generic-check-word-predicate
)
305 (defvaralias 'flyspell-generic-check-word-p
306 'flyspell-generic-check-word-predicate
)
308 ;;*--- mail mode -------------------------------------------------------*/
309 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
310 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
311 (defvar message-signature-separator
)
312 (defun mail-mode-flyspell-verify ()
313 "Function used for `flyspell-generic-check-word-predicate' in Mail mode."
314 (let ((header-end (save-excursion
315 (goto-char (point-min))
318 (regexp-quote mail-header-separator
)
323 (if (not (boundp 'message-signature-separator
))
326 (goto-char (point-max))
327 (re-search-backward message-signature-separator nil t
)
329 (cond ((< (point) header-end
)
330 (and (save-excursion (beginning-of-line)
331 (looking-at "^Subject:"))
332 (> (point) (match-end 0))))
333 ((> (point) signature-begin
)
338 (not (looking-at "[>}|]\\|To:")))))))
340 ;;*--- texinfo mode ----------------------------------------------------*/
341 (put 'texinfo-mode
'flyspell-mode-predicate
'texinfo-mode-flyspell-verify
)
342 (defun texinfo-mode-flyspell-verify ()
343 "Function used for `flyspell-generic-check-word-predicate' in Texinfo mode."
346 (not (looking-at "@"))))
348 ;;*--- tex mode --------------------------------------------------------*/
349 (put 'tex-mode
'flyspell-mode-predicate
'tex-mode-flyspell-verify
)
350 (defun tex-mode-flyspell-verify ()
351 "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
354 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t
)))
356 (let ((this (point)))
358 (and (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}"
359 (line-end-position) t
)
360 (>= this
(match-beginning 0))
361 (<= this
(match-end 0))))))))
363 ;;*--- sgml mode -------------------------------------------------------*/
364 (put 'sgml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
365 (put 'html-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
366 (put 'nxml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
368 (autoload 'sgml-lexical-context
"sgml-mode")
370 (defun sgml-mode-flyspell-verify ()
371 "Function used for `flyspell-generic-check-word-predicate' in SGML mode.
372 Tag and attribute names are not spell checked, everything else is.
374 String values of attributes are checked because they can be text
375 like <img alt=\"Some thing.\">."
377 (not (memq (car (sgml-lexical-context))
380 ;;*---------------------------------------------------------------------*/
381 ;;* Programming mode */
382 ;;*---------------------------------------------------------------------*/
383 (defvar flyspell-prog-text-faces
384 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
385 "Faces corresponding to text in programming-mode buffers.")
387 (defun flyspell-generic-progmode-verify ()
388 "Used for `flyspell-generic-check-word-predicate' in programming modes."
389 ;; (point) is next char after the word. Must check one char before.
390 (let ((f (get-text-property (- (point) 1) 'face
)))
391 (memq f flyspell-prog-text-faces
)))
394 (defun flyspell-prog-mode ()
395 "Turn on `flyspell-mode' for comments and strings."
397 (setq flyspell-generic-check-word-predicate
398 'flyspell-generic-progmode-verify
)
400 (run-hooks 'flyspell-prog-mode-hook
))
402 ;;*---------------------------------------------------------------------*/
403 ;;* Overlay compatibility */
404 ;;*---------------------------------------------------------------------*/
405 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
406 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
407 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
408 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
409 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
410 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
411 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
412 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
414 ;;*---------------------------------------------------------------------*/
415 ;;* The minor mode declaration. */
416 ;;*---------------------------------------------------------------------*/
417 (defvar flyspell-mouse-map
418 (let ((map (make-sparse-keymap)))
419 (if (featurep 'xemacs
)
420 (define-key map
[button2] #'flyspell-correct-word)
421 (define-key map [down-mouse-2] #'flyspell-correct-word)
422 (define-key map [mouse-2] 'undefined))
424 "Keymap for Flyspell to put on erroneous words.")
426 (defvar flyspell-mode-map
427 (let ((map (make-sparse-keymap)))
428 (if flyspell-use-meta-tab
429 (define-key map "\M-\t" 'flyspell-auto-correct-word))
430 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
431 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
432 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
433 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
435 "Minor mode keymap for Flyspell mode--for the whole buffer.")
437 ;; dash character machinery
438 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
439 "Non-nil means that the `-' char is considered as a word delimiter.")
440 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
441 (defvar flyspell-dash-dictionary nil)
442 (make-variable-buffer-local 'flyspell-dash-dictionary)
443 (defvar flyspell-dash-local-dictionary nil)
444 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
446 ;;*---------------------------------------------------------------------*/
448 ;;*---------------------------------------------------------------------*/
449 (defface flyspell-incorrect
450 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
452 "Face used for marking a misspelled word in Flyspell."
454 (define-obsolete-face-alias 'flyspell-incorrect-face 'flyspell-incorrect "22.1")
456 (defface flyspell-duplicate
457 '((((class color)) (:foreground "Gold3" :bold t :underline t))
459 "Face used for marking a misspelled word that appears twice in the buffer.
460 See also `flyspell-duplicate-distance'."
462 (define-obsolete-face-alias 'flyspell-duplicate-face 'flyspell-duplicate "22.1")
464 (defvar flyspell-overlay nil)
466 ;;*---------------------------------------------------------------------*/
467 ;;* flyspell-mode ... */
468 ;;*---------------------------------------------------------------------*/
469 ;;;###autoload(defvar flyspell-mode nil "Non-nil if Flyspell mode is enabled.")
471 (define-minor-mode flyspell-mode
472 "Toggle on-the-fly spell checking (Flyspell mode).
473 With a prefix argument ARG, enable Flyspell mode if ARG is
474 positive, and disable it otherwise. If called from Lisp, enable
475 the mode if ARG is omitted or nil.
477 Flyspell mode is a buffer-local minor mode. When enabled, it
478 spawns a single Ispell process and checks each word. The default
479 flyspell behavior is to highlight incorrect words.
482 \\[ispell-word]: correct words (using Ispell).
483 \\[flyspell-auto-correct-word]: automatically correct word.
484 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
485 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
488 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
491 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
492 valid. For instance, a different dictionary can be used by
493 invoking `ispell-change-dictionary'.
495 Consider using the `ispell-parser' to check your text. For instance
497 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
500 \\[flyspell-region] checks all words inside a region.
501 \\[flyspell-buffer] checks the whole buffer."
502 :lighter flyspell-mode-line-string
503 :keymap flyspell-mode-map
508 (error (message "Error enabling Flyspell mode:\n%s" (cdr err))
510 (flyspell-mode-off)))
513 (defun turn-on-flyspell ()
514 "Unconditionally turn on Flyspell mode."
518 (defun turn-off-flyspell ()
519 "Unconditionally turn off Flyspell mode."
522 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
524 ;;*---------------------------------------------------------------------*/
525 ;;* flyspell-buffers ... */
526 ;;* ------------------------------------------------------------- */
527 ;;* For remembering buffers running flyspell */
528 ;;*---------------------------------------------------------------------*/
529 (defvar flyspell-buffers nil)
531 ;;*---------------------------------------------------------------------*/
532 ;;* flyspell-minibuffer-p ... */
533 ;;*---------------------------------------------------------------------*/
534 (defun flyspell-minibuffer-p (buffer)
535 "Is BUFFER a minibuffer?"
536 (let ((ws (get-buffer-window-list buffer t)))
537 (and (consp ws) (window-minibuffer-p (car ws)))))
539 ;;*---------------------------------------------------------------------*/
540 ;;* flyspell-accept-buffer-local-defs ... */
541 ;;*---------------------------------------------------------------------*/
542 (defvar flyspell-last-buffer nil
543 "The buffer in which the last flyspell operation took place.")
545 (defun flyspell-accept-buffer-local-defs (&optional force)
546 ;; When flyspell-word is used inside a loop (e.g. when processing
547 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
548 ;; up dwarfing everything else, so only do it when the buffer has changed.
549 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
550 (setq flyspell-last-buffer (current-buffer))
551 ;; Strange problem: If buffer in current window has font-lock turned on,
552 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
553 ;; call will reset the buffer to the buffer in the current window.
554 ;; However, it only happens at startup (fix by Albert L. Ting).
556 (ispell-accept-buffer-local-defs))
557 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
558 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
559 ;; The dictionary has changed
560 (setq flyspell-dash-dictionary ispell-dictionary)
561 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
562 (setq flyspell-consider-dash-as-word-delimiter-flag
563 (member (or ispell-local-dictionary ispell-dictionary)
564 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
566 (defun flyspell-hack-local-variables-hook ()
567 ;; When local variables are loaded, see if the dictionary context
569 (flyspell-accept-buffer-local-defs 'force))
571 (defun flyspell-kill-ispell-hook ()
572 (setq flyspell-last-buffer nil)
573 (dolist (buf (buffer-list))
574 (with-current-buffer buf
575 (kill-local-variable 'flyspell-word-cache-word))))
577 ;; Make sure we flush our caches when needed. Do it here rather than in
578 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
580 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
582 ;;*---------------------------------------------------------------------*/
583 ;;* flyspell-mode-on ... */
584 ;;*---------------------------------------------------------------------*/
585 (defun flyspell-mode-on ()
586 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
587 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
588 (setq ispell-highlight-face 'flyspell-incorrect)
589 ;; local dictionaries setup
590 (or ispell-local-dictionary ispell-dictionary
591 (if flyspell-default-dictionary
592 (ispell-change-dictionary flyspell-default-dictionary)))
593 ;; we have to force ispell to accept the local definition or
594 ;; otherwise it could be too late, the local dictionary may
596 ;; Pass the `force' argument for the case where flyspell was active already
597 ;; but the buffer's local-defs have been edited.
598 (flyspell-accept-buffer-local-defs 'force)
599 ;; we put the `flyspell-delayed' property on some commands
600 (flyspell-delay-commands)
601 ;; we put the `flyspell-deplacement' property on some commands
602 (flyspell-deplacement-commands)
603 ;; we bound flyspell action to post-command hook
604 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
605 ;; we bound flyspell action to pre-command hook
606 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
607 ;; we bound flyspell action to after-change hook
608 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
609 ;; we bound flyspell action to hack-local-variables-hook
610 (add-hook 'hack-local-variables-hook
611 (function flyspell-hack-local-variables-hook) t t)
612 ;; set flyspell-generic-check-word-predicate based on the major mode
613 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
615 (setq flyspell-generic-check-word-predicate mode-predicate)))
616 ;; the welcome message
617 (if (and flyspell-issue-message-flag
618 flyspell-issue-welcome-flag
619 (called-interactively-p 'interactive))
620 (let ((binding (where-is-internal 'flyspell-auto-correct-word
624 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
625 (key-description binding))
626 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
628 ;;*---------------------------------------------------------------------*/
629 ;;* flyspell-delay-commands ... */
630 ;;*---------------------------------------------------------------------*/
631 (defun flyspell-delay-commands ()
632 "Install the standard set of Flyspell delayed commands."
633 (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
634 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
636 ;;*---------------------------------------------------------------------*/
637 ;;* flyspell-delay-command ... */
638 ;;*---------------------------------------------------------------------*/
639 (defun flyspell-delay-command (command)
640 "Set COMMAND to be delayed, for Flyspell.
641 When flyspell `post-command-hook' is invoked because a delayed command
642 as been used the current word is not immediately checked.
643 It will be checked only after `flyspell-delay' seconds."
644 (interactive "SDelay Flyspell after Command: ")
645 (put command 'flyspell-delayed t))
647 ;;*---------------------------------------------------------------------*/
648 ;;* flyspell-deplacement-commands ... */
649 ;;*---------------------------------------------------------------------*/
650 (defun flyspell-deplacement-commands ()
651 "Install the standard set of Flyspell deplacement commands."
652 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
653 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
655 ;;*---------------------------------------------------------------------*/
656 ;;* flyspell-deplacement-command ... */
657 ;;*---------------------------------------------------------------------*/
658 (defun flyspell-deplacement-command (command)
659 "Set COMMAND that implement cursor movements, for Flyspell.
660 When flyspell `post-command-hook' is invoked because of a deplacement command
661 as been used the current word is checked only if the previous command was
662 not the very same deplacement command."
663 (interactive "SDeplacement Flyspell after Command: ")
664 (put command 'flyspell-deplacement t))
666 ;;*---------------------------------------------------------------------*/
667 ;;* flyspell-word-cache ... */
668 ;;*---------------------------------------------------------------------*/
669 (defvar flyspell-word-cache-start nil)
670 (defvar flyspell-word-cache-end nil)
671 (defvar flyspell-word-cache-word nil)
672 (defvar flyspell-word-cache-result '_)
673 (make-variable-buffer-local 'flyspell-word-cache-start)
674 (make-variable-buffer-local 'flyspell-word-cache-end)
675 (make-variable-buffer-local 'flyspell-word-cache-word)
676 (make-variable-buffer-local 'flyspell-word-cache-result)
678 ;;*---------------------------------------------------------------------*/
679 ;;* The flyspell pre-hook, store the current position. In the */
680 ;;* post command hook, we will check, if the word at this position */
681 ;;* has to be spell checked. */
682 ;;*---------------------------------------------------------------------*/
683 (defvar flyspell-pre-buffer nil)
684 (defvar flyspell-pre-point nil)
685 (defvar flyspell-pre-column nil)
686 (defvar flyspell-pre-pre-buffer nil)
687 (defvar flyspell-pre-pre-point nil)
688 (make-variable-buffer-local 'flyspell-pre-point)
690 ;;*---------------------------------------------------------------------*/
691 ;;* flyspell-previous-command ... */
692 ;;*---------------------------------------------------------------------*/
693 (defvar flyspell-previous-command nil
694 "The last interactive command checked by Flyspell.")
696 ;;*---------------------------------------------------------------------*/
697 ;;* flyspell-pre-command-hook ... */
698 ;;*---------------------------------------------------------------------*/
699 (defun flyspell-pre-command-hook ()
700 "Save the current buffer and point for Flyspell's post-command hook."
702 (setq flyspell-pre-buffer (current-buffer))
703 (setq flyspell-pre-point (point))
704 (setq flyspell-pre-column (current-column)))
706 ;;*---------------------------------------------------------------------*/
707 ;;* flyspell-mode-off ... */
708 ;;*---------------------------------------------------------------------*/
710 (defun flyspell-mode-off ()
711 "Turn Flyspell mode off."
712 ;; we remove the hooks
713 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
714 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
715 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
716 (remove-hook 'hack-local-variables-hook
717 (function flyspell-hack-local-variables-hook) t)
718 ;; we remove all the flyspell highlightings
719 (flyspell-delete-all-overlays)
720 ;; we have to erase pre cache variables
721 (setq flyspell-pre-buffer nil)
722 (setq flyspell-pre-point nil)
723 ;; we mark the mode as killed
724 (setq flyspell-mode nil))
726 ;;*---------------------------------------------------------------------*/
727 ;;* flyspell-check-pre-word-p ... */
728 ;;*---------------------------------------------------------------------*/
729 (defun flyspell-check-pre-word-p ()
730 "Return non-nil if we should check the word before point.
731 More precisely, it applies to the word that was before point
732 before the current command."
734 ((or (not (numberp flyspell-pre-point))
735 (not (bufferp flyspell-pre-buffer))
736 (not (buffer-live-p flyspell-pre-buffer)))
738 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
739 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
741 ((or (and (= flyspell-pre-point (- (point) 1))
742 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
743 (string-match-p (ispell-get-otherchars)
744 (buffer-substring-no-properties
745 flyspell-pre-point (1+ flyspell-pre-point)))))
746 (= flyspell-pre-point (point))
747 (= flyspell-pre-point (+ (point) 1)))
749 ((and (symbolp this-command)
750 (not executing-kbd-macro)
751 (or (get this-command 'flyspell-delayed)
752 (and (get this-command 'flyspell-deplacement)
753 (eq flyspell-previous-command this-command)))
754 (or (= (current-column) 0)
755 (= (current-column) flyspell-pre-column)
756 ;; If other post-command-hooks change the buffer,
757 ;; flyspell-pre-point can lie past eob (bug#468).
758 (null (char-after flyspell-pre-point))
759 (or (eq (char-syntax (char-after flyspell-pre-point)) ?w)
760 (string-match-p (ispell-get-otherchars)
761 (buffer-substring-no-properties
762 flyspell-pre-point (1+ flyspell-pre-point))))))
764 ((not (eq (current-buffer) flyspell-pre-buffer))
766 ((not (and (numberp flyspell-word-cache-start)
767 (numberp flyspell-word-cache-end)))
770 (or (< flyspell-pre-point flyspell-word-cache-start)
771 (> flyspell-pre-point flyspell-word-cache-end)))))
773 ;;*---------------------------------------------------------------------*/
774 ;;* The flyspell after-change-hook, store the change position. In */
775 ;;* the post command hook, we will check, if the word at this */
776 ;;* position has to be spell checked. */
777 ;;*---------------------------------------------------------------------*/
778 (defvar flyspell-changes nil)
779 (make-variable-buffer-local 'flyspell-changes)
781 ;;*---------------------------------------------------------------------*/
782 ;;* flyspell-after-change-function ... */
783 ;;*---------------------------------------------------------------------*/
784 (defun flyspell-after-change-function (start stop len)
785 "Save the current buffer and point for Flyspell's post-command hook."
786 (push (cons start stop) flyspell-changes))
788 ;;*---------------------------------------------------------------------*/
789 ;;* flyspell-check-changed-word-p ... */
790 ;;*---------------------------------------------------------------------*/
791 (defun flyspell-check-changed-word-p (start stop)
792 "Return t when the changed word has to be checked.
793 The answer depends of several criteria.
794 Mostly we check word delimiters."
796 ((and (memq (char-after start) '(?\n ? )) (> stop start))
798 ((not (numberp flyspell-pre-point))
800 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
802 ((let ((pos (point)))
803 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
808 ;;*---------------------------------------------------------------------*/
809 ;;* flyspell-check-word-p ... */
810 ;;*---------------------------------------------------------------------*/
811 (defun flyspell-check-word-p ()
812 "Return t when the word at `point' has to be checked.
813 The answer depends of several criteria.
814 Mostly we check word delimiters."
816 ((<= (- (point-max) 1) (point-min))
817 ;; the buffer is not filled enough
819 ((and (and (> (current-column) 0)
820 (not (eq (current-column) flyspell-pre-column)))
823 (and (looking-at (flyspell-get-not-casechars))
824 (not (looking-at (ispell-get-otherchars)))
825 (or flyspell-consider-dash-as-word-delimiter-flag
826 (not (looking-at "-"))))))
827 ;; yes because we have reached or typed a word delimiter.
829 ((symbolp this-command)
831 ((get this-command 'flyspell-deplacement)
832 (not (eq flyspell-previous-command this-command)))
833 ((get this-command 'flyspell-delayed)
834 ;; the current command is not delayed, that
835 ;; is that we must check the word now
836 (and (not unread-command-events)
837 (sit-for flyspell-delay)))
841 ;;*---------------------------------------------------------------------*/
842 ;;* flyspell-debug-signal-no-check ... */
843 ;;*---------------------------------------------------------------------*/
844 (defun flyspell-debug-signal-no-check (msg obj)
845 (setq debug-on-error t)
846 (with-current-buffer (get-buffer-create "*flyspell-debug*")
848 (insert "NO-CHECK:\n")
849 (insert (format " %S : %S\n" msg obj))))
851 ;;*---------------------------------------------------------------------*/
852 ;;* flyspell-debug-signal-pre-word-checked ... */
853 ;;*---------------------------------------------------------------------*/
854 (defun flyspell-debug-signal-pre-word-checked ()
855 (setq debug-on-error t)
856 (with-current-buffer (get-buffer-create "*flyspell-debug*")
857 (insert "PRE-WORD:\n")
858 (insert (format " pre-point : %S\n" flyspell-pre-point))
859 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
860 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
861 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
862 (goto-char (point-max))))
864 ;;*---------------------------------------------------------------------*/
865 ;;* flyspell-debug-signal-word-checked ... */
866 ;;*---------------------------------------------------------------------*/
867 (defun flyspell-debug-signal-word-checked ()
868 (setq debug-on-error t)
869 (let ((oldbuf (current-buffer))
871 (with-current-buffer (get-buffer-create "*flyspell-debug*")
873 (insert (format " this-cmd : %S\n" this-command))
874 (insert (format " delayed : %S\n" (and (symbolp this-command)
875 (get this-command 'flyspell-delayed))))
876 (insert (format " point : %S\n" point))
877 (insert (format " prev-char : [%c] %S\n"
878 (with-current-buffer oldbuf
879 (let ((c (if (> (point) (point-min))
882 (char-after (point)))
885 (with-current-buffer oldbuf
886 (let ((c (if (> (point) (point-min))
889 (and (and (looking-at (flyspell-get-not-casechars)) 1)
890 (not (looking-at (ispell-get-otherchars)))
891 (and (or flyspell-consider-dash-as-word-delimiter-flag
892 (not (looking-at "\\-"))) 2))))))
894 (insert (format " because : %S\n"
896 ((not (and (symbolp this-command)
897 (get this-command 'flyspell-delayed)))
898 ;; the current command is not delayed, that
899 ;; is that we must check the word now
901 ((with-current-buffer oldbuf
902 (let ((c (if (> (point) (point-min))
905 (and (looking-at (flyspell-get-not-casechars))
906 (not (looking-at (ispell-get-otherchars)))
907 (or flyspell-consider-dash-as-word-delimiter-flag
908 (not (looking-at "\\-"))))))))
910 ;; yes because we have reached or typed a word delimiter.
912 ((not (integerp flyspell-delay))
913 ;; yes because the user had set up a no-delay configuration.
917 (goto-char (point-max)))))
919 ;;*---------------------------------------------------------------------*/
920 ;;* flyspell-debug-signal-changed-checked ... */
921 ;;*---------------------------------------------------------------------*/
922 (defun flyspell-debug-signal-changed-checked ()
923 (setq debug-on-error t)
924 (let ((point (point)))
925 (with-current-buffer (get-buffer-create "*flyspell-debug*")
926 (insert "CHANGED WORD:\n")
927 (insert (format " point : %S\n" point))
928 (goto-char (point-max)))))
930 ;;*---------------------------------------------------------------------*/
931 ;;* flyspell-post-command-hook ... */
932 ;;* ------------------------------------------------------------- */
933 ;;* It is possible that we check several words: */
934 ;;* 1- the current word is checked if the predicate */
935 ;;* FLYSPELL-CHECK-WORD-P is true */
936 ;;* 2- the word that used to be the current word before the */
937 ;;* THIS-COMMAND is checked if: */
938 ;;* a- the previous word is different from the current word */
939 ;;* b- the previous word as not just been checked by the */
940 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
941 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
942 ;;* previous word nor the current word */
943 ;;*---------------------------------------------------------------------*/
944 (defun flyspell-post-command-hook ()
945 "The `post-command-hook' used by flyspell to check a word on-the-fly."
949 (let ((command this-command)
950 ;; Prevent anything we do from affecting the mark.
952 (if (flyspell-check-pre-word-p)
953 (with-current-buffer flyspell-pre-buffer
954 '(flyspell-debug-signal-pre-word-checked)
956 (goto-char flyspell-pre-point)
958 (if (flyspell-check-word-p)
960 '(flyspell-debug-signal-word-checked)
961 ;; FIXME: This should be asynchronous!
963 ;; we remember which word we have just checked.
964 ;; this will be used next time we will check a word
965 ;; to compare the next current word with the word
966 ;; that as been registered in the pre-command-hook
967 ;; that is these variables are used within the predicate
968 ;; FLYSPELL-CHECK-PRE-WORD-P
969 (setq flyspell-pre-pre-buffer (current-buffer))
970 (setq flyspell-pre-pre-point (point)))
972 (setq flyspell-pre-pre-buffer nil)
973 (setq flyspell-pre-pre-point nil)
974 ;; when a word is not checked because of a delayed command
975 ;; we do not disable the ispell cache.
976 (if (and (symbolp this-command)
977 (get this-command 'flyspell-delayed))
979 (setq flyspell-word-cache-end -1)
980 (setq flyspell-word-cache-result '_)))))
981 (while (and (not (input-pending-p)) (consp flyspell-changes))
982 (let ((start (car (car flyspell-changes)))
983 (stop (cdr (car flyspell-changes))))
984 (if (flyspell-check-changed-word-p start stop)
986 '(flyspell-debug-signal-changed-checked)
989 (setq flyspell-changes (cdr flyspell-changes))))
990 (setq flyspell-previous-command command)))))
992 ;;*---------------------------------------------------------------------*/
993 ;;* flyspell-notify-misspell ... */
994 ;;*---------------------------------------------------------------------*/
995 (defun flyspell-notify-misspell (word poss)
996 (let ((replacements (if (stringp poss)
998 (if flyspell-sort-corrections
999 (sort (car (cdr (cdr poss))) 'string<)
1000 (car (cdr (cdr poss)))))))
1001 (if flyspell-issue-message-flag
1002 (message "misspelling `%s' %S" word replacements))))
1004 ;;*---------------------------------------------------------------------*/
1005 ;;* flyspell-word-search-backward ... */
1006 ;;*---------------------------------------------------------------------*/
1007 (defun flyspell-word-search-backward (word bound &optional ignore-case)
1010 (inhibit-point-motion-hooks t)
1012 (while (and (not r) (setq p (search-backward word bound t)))
1013 (let ((lw (flyspell-get-word)))
1016 (string-equal (downcase (car lw)) (downcase word))
1017 (string-equal (car lw) word)))
1022 ;;*---------------------------------------------------------------------*/
1023 ;;* flyspell-word-search-forward ... */
1024 ;;*---------------------------------------------------------------------*/
1025 (defun flyspell-word-search-forward (word bound)
1028 (inhibit-point-motion-hooks t)
1030 (while (and (not r) (setq p (search-forward word bound t)))
1031 (let ((lw (flyspell-get-word)))
1032 (if (and (consp lw) (string-equal (car lw) word))
1034 (goto-char (1+ p)))))
1037 ;;*---------------------------------------------------------------------*/
1038 ;;* flyspell-word ... */
1039 ;;*---------------------------------------------------------------------*/
1040 (defun flyspell-word (&optional following known-misspelling)
1041 "Spell check a word.
1042 If the optional argument FOLLOWING, or, when called interactively
1043 `ispell-following-word', is non-nil, checks the following (rather
1044 than preceding) word when the cursor is not over a word. If
1045 optional argument KNOWN-MISSPELLING is non nil considers word a
1046 misspelling and skips redundant spell-checking step."
1047 (interactive (list ispell-following-word))
1048 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1050 ;; use the correct dictionary
1051 (flyspell-accept-buffer-local-defs)
1052 (let* ((cursor-location (point))
1053 (flyspell-word (flyspell-get-word following))
1054 start end poss word ispell-filter)
1055 (if (or (eq flyspell-word nil)
1056 (and (fboundp flyspell-generic-check-word-predicate)
1057 (not (funcall flyspell-generic-check-word-predicate))))
1060 ;; destructure return flyspell-word info list.
1061 (setq start (car (cdr flyspell-word))
1062 end (car (cdr (cdr flyspell-word)))
1063 word (car flyspell-word))
1064 ;; before checking in the directory, we check for doublons.
1066 ((and (or (not (eq ispell-parser 'tex))
1067 (and (> start (point-min))
1068 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1069 flyspell-mark-duplications-flag
1070 (not (catch 'exception
1071 (let ((dict (or ispell-local-dictionary
1072 ispell-dictionary)))
1073 (dolist (except flyspell-mark-duplications-exceptions)
1074 (and (or (null (car except))
1076 (string-match (car except) dict)))
1077 (member (downcase word) (cdr except))
1078 (throw 'exception t))))))
1084 (- (skip-chars-backward " \t\n\f"))))
1085 (p (when (>= bound (point-min))
1086 (flyspell-word-search-backward word bound t))))
1087 (and p (/= p start)))))
1088 ;; yes, this is a doublon
1089 (flyspell-highlight-incorrect-region start end 'doublon)
1091 ((and (eq flyspell-word-cache-start start)
1092 (eq flyspell-word-cache-end end)
1093 (string-equal flyspell-word-cache-word word))
1094 ;; this word had been already checked, we skip
1095 flyspell-word-cache-result)
1096 ((and (eq ispell-parser 'tex)
1097 (flyspell-tex-command-p flyspell-word))
1098 ;; this is a correct word (because a tex command)
1099 (flyspell-unhighlight-at start)
1101 (flyspell-unhighlight-at (- end 1)))
1104 ;; we setup the cache
1105 (setq flyspell-word-cache-start start)
1106 (setq flyspell-word-cache-end end)
1107 (setq flyspell-word-cache-word word)
1108 ;; now check spelling of word.
1109 (if (not known-misspelling)
1111 (ispell-send-string "%\n")
1112 ;; put in verbose mode
1113 (ispell-send-string (concat "^" word "\n"))
1114 ;; we mark the ispell process so it can be killed
1115 ;; when emacs is exited without query
1116 (set-process-query-on-exit-flag ispell-process nil)
1117 ;; Wait until ispell has processed word.
1119 (accept-process-output ispell-process)
1120 (not (string= "" (car ispell-filter)))))
1121 ;; (ispell-send-string "!\n")
1122 ;; back to terse mode.
1123 ;; Remove leading empty element
1124 (setq ispell-filter (cdr ispell-filter))
1125 ;; ispell process should return something after word is sent.
1126 ;; Tag word as valid (i.e., skip) otherwise
1128 (setq ispell-filter '(*)))
1129 (if (consp ispell-filter)
1130 (setq poss (ispell-parse-output (car ispell-filter)))))
1131 ;; Else, this was a known misspelling to begin with, and
1132 ;; we should forge an ispell return value.
1133 (setq poss (list word 1 nil nil)))
1134 (let ((res (cond ((eq poss t)
1136 (setq flyspell-word-cache-result t)
1137 (flyspell-unhighlight-at start)
1139 (flyspell-unhighlight-at (- end 1)))
1141 ((and (stringp poss) flyspell-highlight-flag)
1143 (setq flyspell-word-cache-result t)
1144 (flyspell-unhighlight-at start)
1146 (flyspell-unhighlight-at (- end 1)))
1149 (setq flyspell-word-cache-result t)
1150 (flyspell-unhighlight-at start)
1152 (flyspell-unhighlight-at (- end 1)))
1154 ((or (and (< flyspell-duplicate-distance 0)
1157 (flyspell-word-search-backward
1162 (flyspell-word-search-forward
1165 (and (> flyspell-duplicate-distance 0)
1168 (flyspell-word-search-backward
1171 flyspell-duplicate-distance)))
1174 (flyspell-word-search-forward
1177 flyspell-duplicate-distance))))))
1178 ;; This is a misspelled word which occurs
1179 ;; twice within flyspell-duplicate-distance.
1180 (setq flyspell-word-cache-result nil)
1181 (if flyspell-highlight-flag
1182 (flyspell-highlight-duplicate-region
1184 (message "duplicate `%s'" word))
1187 (setq flyspell-word-cache-result nil)
1188 ;; Highlight the location as incorrect,
1189 ;; including offset specified in POSS.
1190 (if flyspell-highlight-flag
1191 (flyspell-highlight-incorrect-region
1192 (if (and (consp poss)
1193 (integerp (nth 1 poss)))
1194 (+ start (nth 1 poss) -1)
1197 (flyspell-notify-misspell word poss))
1199 ;; return to original location
1200 (goto-char cursor-location)
1201 (if ispell-quit (setq ispell-quit nil))
1204 ;;*---------------------------------------------------------------------*/
1205 ;;* flyspell-math-tex-command-p ... */
1206 ;;* ------------------------------------------------------------- */
1207 ;;* This function uses the texmathp package to check if point */
1208 ;;* is within a TeX math environment. `texmathp' can yield errors */
1209 ;;* if the document is currently not valid TeX syntax. */
1210 ;;*---------------------------------------------------------------------*/
1211 (defun flyspell-math-tex-command-p ()
1212 (when (fboundp 'texmathp)
1213 (if flyspell-check-tex-math-command
1219 ;;*---------------------------------------------------------------------*/
1220 ;;* flyspell-tex-command-p ... */
1221 ;;*---------------------------------------------------------------------*/
1222 (defun flyspell-tex-command-p (word)
1223 "Return t if WORD is a TeX command."
1225 (let ((b (car (cdr word))))
1226 (and (re-search-backward "\\\\" (- (point) 100) t)
1227 (or (= (match-end 0) b)
1228 (and (goto-char (match-end 0))
1229 (looking-at flyspell-tex-command-regexp)
1230 (>= (match-end 0) b))))))
1231 (flyspell-math-tex-command-p)))
1233 ;;*---------------------------------------------------------------------*/
1234 ;;* flyspell-casechars-cache ... */
1235 ;;*---------------------------------------------------------------------*/
1236 (defvar flyspell-casechars-cache nil)
1237 (defvar flyspell-ispell-casechars-cache nil)
1238 (make-variable-buffer-local 'flyspell-casechars-cache)
1239 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1241 ;;*---------------------------------------------------------------------*/
1242 ;;* flyspell-get-casechars ... */
1243 ;;*---------------------------------------------------------------------*/
1244 (defun flyspell-get-casechars ()
1245 "This function builds a string that is the regexp of word chars.
1246 In order to avoid one useless string construction,
1247 this function changes the last char of the `ispell-casechars' string."
1248 (let ((ispell-casechars (ispell-get-casechars)))
1250 ((eq ispell-parser 'tex)
1251 (setq flyspell-ispell-casechars-cache ispell-casechars)
1252 (setq flyspell-casechars-cache
1253 (concat (substring ispell-casechars
1255 (- (length ispell-casechars) 1))
1257 flyspell-casechars-cache)
1259 (setq flyspell-ispell-casechars-cache ispell-casechars)
1260 (setq flyspell-casechars-cache ispell-casechars)
1261 flyspell-casechars-cache))))
1263 ;;*---------------------------------------------------------------------*/
1264 ;;* flyspell-get-not-casechars-cache ... */
1265 ;;*---------------------------------------------------------------------*/
1266 (defvar flyspell-not-casechars-cache nil)
1267 (defvar flyspell-ispell-not-casechars-cache nil)
1268 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1269 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1271 ;;*---------------------------------------------------------------------*/
1272 ;;* flyspell-get-not-casechars ... */
1273 ;;*---------------------------------------------------------------------*/
1274 (defun flyspell-get-not-casechars ()
1275 "This function builds a string that is the regexp of non-word chars."
1276 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1278 ((eq ispell-parser 'tex)
1279 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1280 (setq flyspell-not-casechars-cache
1281 (concat (substring ispell-not-casechars
1283 (- (length ispell-not-casechars) 1))
1285 flyspell-not-casechars-cache)
1287 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1288 (setq flyspell-not-casechars-cache ispell-not-casechars)
1289 flyspell-not-casechars-cache))))
1291 ;;*---------------------------------------------------------------------*/
1292 ;;* flyspell-get-word ... */
1293 ;;*---------------------------------------------------------------------*/
1294 (defun flyspell-get-word (&optional following extra-otherchars)
1295 "Return the word for spell-checking according to Ispell syntax.
1296 Optional argument FOLLOWING non-nil means to get the following
1297 \(rather than preceding) word when the cursor is not over a word.
1298 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1299 that may be included as part of a word (see `ispell-dictionary-alist')."
1300 (let* ((flyspell-casechars (flyspell-get-casechars))
1301 (flyspell-not-casechars (flyspell-get-not-casechars))
1302 (ispell-otherchars (ispell-get-otherchars))
1303 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1304 (word-regexp (concat flyspell-casechars
1306 (if (not (string= "" ispell-otherchars))
1307 (concat ispell-otherchars "?"))
1308 (if extra-otherchars
1309 (concat extra-otherchars "?"))
1312 (if (or ispell-many-otherchars-p
1318 (if (not (looking-at flyspell-casechars))
1320 (re-search-forward flyspell-casechars nil t)
1321 (re-search-backward flyspell-casechars nil t)))
1322 ;; move to front of word
1323 (re-search-backward flyspell-not-casechars nil 'start)
1324 (while (and (or (and (not (string= "" ispell-otherchars))
1325 (looking-at ispell-otherchars))
1326 (and extra-otherchars (looking-at extra-otherchars)))
1328 (or (not did-it-once)
1329 ispell-many-otherchars-p)
1330 (not (eq prevpt (point))))
1331 (if (and extra-otherchars (looking-at extra-otherchars))
1334 (if (looking-at flyspell-casechars)
1335 (re-search-backward flyspell-not-casechars nil 'move)))
1339 (if (looking-at flyspell-casechars)
1340 (re-search-backward flyspell-not-casechars nil 'move)
1341 (backward-char -1))))
1342 ;; Now mark the word and save to string.
1343 (if (not (re-search-forward word-regexp nil t))
1346 (setq start (match-beginning 0)
1348 word (buffer-substring-no-properties start end))
1349 (list word start end)))))
1351 ;;*---------------------------------------------------------------------*/
1352 ;;* flyspell-small-region ... */
1353 ;;*---------------------------------------------------------------------*/
1354 (defun flyspell-small-region (beg end)
1355 "Flyspell text between BEG and END."
1363 (while (< (point) end)
1364 (if (and flyspell-issue-message-flag (= count 100))
1366 (message "Spell Checking...%d%%"
1367 (* 100 (/ (float (- (point) beg)) (- end beg))))
1369 (setq count (+ 1 count)))
1372 (let ((cur (point)))
1374 (if (and (< (point) end) (> (point) (+ cur 1)))
1375 (backward-char 1)))))
1377 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1380 ;;*---------------------------------------------------------------------*/
1381 ;;* flyspell-external-ispell-process ... */
1382 ;;*---------------------------------------------------------------------*/
1383 (defvar flyspell-external-ispell-process '()
1384 "The external Flyspell Ispell process.")
1386 ;;*---------------------------------------------------------------------*/
1387 ;;* flyspell-external-ispell-buffer ... */
1388 ;;*---------------------------------------------------------------------*/
1389 (defvar flyspell-external-ispell-buffer '())
1390 (defvar flyspell-large-region-buffer '())
1391 (defvar flyspell-large-region-beg (point-min))
1392 (defvar flyspell-large-region-end (point-max))
1394 ;;*---------------------------------------------------------------------*/
1395 ;;* flyspell-external-point-words ... */
1396 ;;*---------------------------------------------------------------------*/
1397 (defun flyspell-external-point-words ()
1398 "Mark words from a buffer listing incorrect words in order of appearance.
1399 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1400 \(We finish by killing that buffer and setting the variable to nil.)
1401 The buffer to mark them in is `flyspell-large-region-buffer'."
1402 (let (words-not-found
1403 (ispell-otherchars (ispell-get-otherchars))
1404 (buffer-scan-pos flyspell-large-region-beg)
1406 (with-current-buffer flyspell-external-ispell-buffer
1407 (goto-char (point-min))
1408 ;; Loop over incorrect words, in the order they were reported,
1409 ;; which is also the order they appear in the buffer being checked.
1410 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1411 ;; Bind WORD to the next one.
1412 (let ((word (match-string 1)) (wordpos (point)))
1413 ;; Here there used to be code to see if WORD is the same
1414 ;; as the previous iteration, and count the number of consecutive
1415 ;; identical words, and the loop below would search for that many.
1416 ;; That code seemed to be incorrect, and on principle, should
1417 ;; be unnecessary too. -- rms.
1418 (if flyspell-issue-message-flag
1419 (message "Spell Checking...%d%% [%s]"
1420 (* 100 (/ (float (point)) (point-max)))
1422 (with-current-buffer flyspell-large-region-buffer
1423 (goto-char buffer-scan-pos)
1425 ;; Iterate on string search until string is found as word,
1428 (if (search-forward word
1429 flyspell-large-region-end t)
1432 ;; Move back into the match
1433 ;; so flyspell-get-word will find it.
1435 (flyspell-get-word)))
1436 (found (car found-list))
1437 (found-length (length found))
1438 (misspell-length (length word)))
1440 ;; Size matches, we really found it.
1441 (= found-length misspell-length)
1442 ;; Matches as part of a boundary-char separated word
1444 (split-string found ispell-otherchars))
1445 ;; Misspelling has higher length than
1446 ;; what flyspell considers the
1447 ;; word. Caused by boundary-chars
1448 ;; mismatch. Validating seems safe.
1449 (< found-length misspell-length)
1450 ;; ispell treats beginning of some TeX
1451 ;; commands as nroff control sequences
1452 ;; and strips them in the list of
1453 ;; misspelled words thus giving a
1454 ;; non-existent word. Skip if ispell
1455 ;; is used, string is a TeX command
1456 ;; (char before beginning of word is
1457 ;; backslash) and none of the previous
1458 ;; conditions match.
1459 (and (not ispell-really-aspell)
1461 (goto-char (- (nth 1 found-list) 1))
1462 (if (looking-at "[\\]" )
1466 (flyspell-word nil t)
1467 ;; Search for next misspelled word will begin from
1468 ;; end of last validated match.
1469 (setq buffer-scan-pos (point))))
1470 ;; Record if misspelling is not found and try new one
1471 (add-to-list 'words-not-found
1472 (concat " -> " word " - "
1473 (int-to-string wordpos)))
1474 (setq keep nil)))))))
1476 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1477 ;; Warn about not found misspellings
1478 (dolist (word words-not-found)
1479 (message "%s: word not found" word))
1480 ;; Kill and forget the buffer with the list of incorrect words.
1481 (kill-buffer flyspell-external-ispell-buffer)
1482 (setq flyspell-external-ispell-buffer nil)))
1484 ;;*---------------------------------------------------------------------*/
1485 ;;* flyspell-process-localwords ... */
1486 ;;* ------------------------------------------------------------- */
1487 ;;* This function is used to prevent marking of words explicitly */
1488 ;;* declared correct. */
1489 ;;*---------------------------------------------------------------------*/
1490 (defun flyspell-process-localwords (misspellings-buffer)
1491 (let ((localwords ispell-buffer-session-localwords)
1493 (ispell-casechars (ispell-get-casechars)))
1494 ;; Get localwords from the original buffer
1496 (goto-char (point-min))
1497 ;; Localwords parsing copied from ispell.el.
1498 (while (search-forward ispell-words-keyword nil t)
1499 (let ((end (point-at-eol))
1501 ;; buffer-local words separated by a space, and can contain
1502 ;; any character other than a space. Not rigorous enough.
1503 (while (re-search-forward " *\\([^ ]+\\)" end t)
1504 (setq string (buffer-substring-no-properties (match-beginning 1)
1506 ;; This can fail when string contains a word with invalid chars.
1507 ;; Error handling needs to be added between Ispell and Emacs.
1508 (if (and (< 1 (length string))
1509 (equal 0 (string-match ispell-casechars string)))
1510 (push string localwords))))))
1511 ;; Remove localwords matches from misspellings-buffer.
1512 ;; The usual mechanism of communicating the local words to ispell
1513 ;; does not affect the special ispell process used by
1514 ;; flyspell-large-region.
1515 (with-current-buffer misspellings-buffer
1517 (dolist (word localwords)
1518 (goto-char (point-min))
1519 (let ((regexp (concat "^" word "\n")))
1520 (while (re-search-forward regexp nil t)
1521 (delete-region (match-beginning 0) (match-end 0)))))))))
1523 ;;* ---------------------------------------------------------------
1524 ;;* flyspell-check-region-doublons
1525 ;;* ---------------------------------------------------------------
1526 (defun flyspell-check-region-doublons (beg end)
1527 "Check for adjacent duplicated words (doublons) in the given region."
1530 (flyspell-word) ; Make sure current word is checked
1532 (while (and (< (point) end)
1533 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1539 ;;*---------------------------------------------------------------------*/
1540 ;;* flyspell-large-region ... */
1541 ;;*---------------------------------------------------------------------*/
1542 (defun flyspell-large-region (beg end)
1543 (let* ((curbuf (current-buffer))
1544 (buffer (get-buffer-create "*flyspell-region*")))
1545 (setq flyspell-external-ispell-buffer buffer)
1546 (setq flyspell-large-region-buffer curbuf)
1547 (setq flyspell-large-region-beg beg)
1548 (setq flyspell-large-region-end end)
1549 (flyspell-accept-buffer-local-defs)
1552 ;; this is done, we can start checking...
1553 (if flyspell-issue-message-flag (message "Checking region..."))
1555 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1556 ;; Local dictionary becomes the global dictionary in use.
1557 (setq ispell-current-dictionary
1558 (or ispell-local-dictionary ispell-dictionary))
1559 (setq ispell-current-personal-dictionary
1560 (or ispell-local-pdict ispell-personal-dictionary))
1561 (let ((args (ispell-get-ispell-args))
1562 (encoding (ispell-get-coding-system))
1564 (if (and ispell-current-dictionary ; use specified dictionary
1565 (not (member "-d" args))) ; only define if not overridden
1567 (append (list "-d" ispell-current-dictionary) args)))
1568 (if ispell-current-personal-dictionary ; use specified pers dict
1573 ispell-current-personal-dictionary)))))
1575 ;; Check for extended character mode
1576 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1577 (and extended-char-mode ; ~ extended character mode
1578 (string-match "[^~]+$" extended-char-mode)
1579 (add-to-list 'args (concat "-T" (match-string 0 extended-char-mode)))))
1581 ;; Add ispell-extra-args
1582 (setq args (append args ispell-extra-args))
1584 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1585 ;; for communication. ispell or older aspell/hunspell does not support this
1586 (if ispell-encoding8-command
1589 (if ispell-really-hunspell
1590 (list ispell-encoding8-command
1591 (upcase (symbol-name encoding)))
1592 (list (concat ispell-encoding8-command
1593 (symbol-name encoding)))))))
1595 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1596 (setq c (apply 'ispell-call-process-region beg
1602 (if ispell-really-aspell "list" "-l")
1606 (flyspell-process-localwords buffer)
1607 (with-current-buffer curbuf
1608 (flyspell-delete-region-overlays beg end)
1609 (flyspell-check-region-doublons beg end))
1610 (flyspell-external-point-words))
1611 (error "Can't check region")))))
1613 ;;*---------------------------------------------------------------------*/
1614 ;;* flyspell-region ... */
1615 ;;* ------------------------------------------------------------- */
1616 ;;* Because `ispell -a' is too slow, it is not possible to use */
1617 ;;* it on large region. Then, when ispell is invoked on a large */
1618 ;;* text region, a new `ispell -l' process is spawned. The */
1619 ;;* pointed out words are then searched in the region a checked with */
1620 ;;* regular flyspell means. */
1621 ;;*---------------------------------------------------------------------*/
1623 (defun flyspell-region (beg end)
1624 "Flyspell text between BEG and END."
1626 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1634 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1635 (flyspell-large-region beg end)
1636 (flyspell-small-region beg end)))))
1638 ;;*---------------------------------------------------------------------*/
1639 ;;* flyspell-buffer ... */
1640 ;;*---------------------------------------------------------------------*/
1642 (defun flyspell-buffer ()
1643 "Flyspell whole buffer."
1645 (flyspell-region (point-min) (point-max)))
1647 ;;*---------------------------------------------------------------------*/
1648 ;;* old next error position ... */
1649 ;;*---------------------------------------------------------------------*/
1650 (defvar flyspell-old-buffer-error nil)
1651 (defvar flyspell-old-pos-error nil)
1653 ;;*---------------------------------------------------------------------*/
1654 ;;* flyspell-goto-next-error ... */
1655 ;;*---------------------------------------------------------------------*/
1656 (defun flyspell-goto-next-error ()
1657 "Go to the next previously detected error.
1658 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1663 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1664 (eq pos flyspell-old-pos-error))
1666 (if (= flyspell-old-pos-error max)
1667 ;; goto beginning of buffer
1669 (message "Restarting from beginning of buffer")
1670 (goto-char (point-min)))
1672 (setq pos (point))))
1673 ;; seek the next error
1674 (while (and (< pos max)
1675 (let ((ovs (overlays-at pos))
1677 (while (and (not r) (consp ovs))
1678 (if (flyspell-overlay-p (car ovs))
1680 (setq ovs (cdr ovs))))
1682 (setq pos (1+ pos)))
1683 ;; save the current location for next invocation
1684 (setq flyspell-old-pos-error pos)
1685 (setq flyspell-old-buffer-error (current-buffer))
1688 (message "No more miss-spelled word!"))))
1690 ;;*---------------------------------------------------------------------*/
1691 ;;* flyspell-overlay-p ... */
1692 ;;*---------------------------------------------------------------------*/
1693 (defun flyspell-overlay-p (o)
1694 "Return true if O is an overlay used by flyspell."
1695 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1697 ;;*---------------------------------------------------------------------*/
1698 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1699 ;;* ------------------------------------------------------------- */
1700 ;;* Remove overlays introduced by flyspell. */
1701 ;;*---------------------------------------------------------------------*/
1702 (defun flyspell-delete-region-overlays (beg end)
1703 "Delete overlays used by flyspell in a given region."
1704 (remove-overlays beg end 'flyspell-overlay t))
1707 (defun flyspell-delete-all-overlays ()
1708 "Delete all the overlays used by flyspell."
1709 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1711 ;;*---------------------------------------------------------------------*/
1712 ;;* flyspell-unhighlight-at ... */
1713 ;;*---------------------------------------------------------------------*/
1714 (defun flyspell-unhighlight-at (pos)
1715 "Remove the flyspell overlay that are located at POS."
1716 (if flyspell-persistent-highlight
1717 (let ((overlays (overlays-at pos)))
1718 (while (consp overlays)
1719 (if (flyspell-overlay-p (car overlays))
1720 (delete-overlay (car overlays)))
1721 (setq overlays (cdr overlays))))
1722 (if (flyspell-overlay-p flyspell-overlay)
1723 (delete-overlay flyspell-overlay))))
1725 ;;*---------------------------------------------------------------------*/
1726 ;;* flyspell-properties-at-p ... */
1727 ;;* ------------------------------------------------------------- */
1728 ;;* Is there an highlight properties at position pos? */
1729 ;;*---------------------------------------------------------------------*/
1730 (defun flyspell-properties-at-p (pos)
1731 "Return t if there is a text property at POS, not counting `local-map'.
1732 If variable `flyspell-highlight-properties' is set to nil,
1733 text with properties are not checked. This function is used to discover
1734 if the character at POS has any other property."
1735 (let ((prop (text-properties-at pos))
1737 (while (and keep (consp prop))
1738 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1739 (setq prop (cdr (cdr prop)))
1743 ;;*---------------------------------------------------------------------*/
1744 ;;* make-flyspell-overlay ... */
1745 ;;*---------------------------------------------------------------------*/
1746 (defun make-flyspell-overlay (beg end face mouse-face)
1747 "Allocate an overlay to highlight an incorrect word.
1748 BEG and END specify the range in the buffer of that word.
1749 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1751 (let ((overlay (make-overlay beg end nil t nil)))
1752 (overlay-put overlay 'face face)
1753 (overlay-put overlay 'mouse-face mouse-face)
1754 (overlay-put overlay 'flyspell-overlay t)
1755 (overlay-put overlay 'evaporate t)
1756 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1757 (overlay-put overlay 'keymap flyspell-mouse-map)
1758 (when (eq face 'flyspell-incorrect)
1759 (and (stringp flyspell-before-incorrect-word-string)
1760 (overlay-put overlay 'before-string
1761 flyspell-before-incorrect-word-string))
1762 (and (stringp flyspell-after-incorrect-word-string)
1763 (overlay-put overlay 'after-string
1764 flyspell-after-incorrect-word-string)))
1767 ;;*---------------------------------------------------------------------*/
1768 ;;* flyspell-highlight-incorrect-region ... */
1769 ;;*---------------------------------------------------------------------*/
1770 (defun flyspell-highlight-incorrect-region (beg end poss)
1771 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1772 POSS is usually a list of possible spelling/correction lists,
1773 as returned by `ispell-parse-output'.
1774 It can also be the symbol `doublon', in the case where the word
1775 is itself incorrect, but suspiciously repeated."
1776 (let ((inhibit-read-only t))
1777 (unless (run-hook-with-args-until-success
1778 'flyspell-incorrect-hook beg end poss)
1779 (if (or flyspell-highlight-properties
1780 (not (flyspell-properties-at-p beg)))
1782 ;; we cleanup all the overlay that are in the region, not
1783 ;; beginning at the word start position
1784 (if (< (1+ beg) end)
1785 (let ((os (overlays-in (1+ beg) end)))
1787 (if (flyspell-overlay-p (car os))
1788 (delete-overlay (car os)))
1789 (setq os (cdr os)))))
1790 ;; we cleanup current overlay at the same position
1791 (flyspell-unhighlight-at beg)
1792 ;; now we can use a new overlay
1793 (setq flyspell-overlay
1794 (make-flyspell-overlay
1796 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1799 ;;*---------------------------------------------------------------------*/
1800 ;;* flyspell-highlight-duplicate-region ... */
1801 ;;*---------------------------------------------------------------------*/
1802 (defun flyspell-highlight-duplicate-region (beg end poss)
1803 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1804 POSS is a list of possible spelling/correction lists,
1805 as returned by `ispell-parse-output'."
1806 (let ((inhibit-read-only t))
1807 (unless (run-hook-with-args-until-success
1808 'flyspell-incorrect-hook beg end poss)
1809 (if (or flyspell-highlight-properties
1810 (not (flyspell-properties-at-p beg)))
1812 ;; we cleanup current overlay at the same position
1813 (flyspell-unhighlight-at beg)
1814 ;; now we can use a new overlay
1815 (setq flyspell-overlay
1816 (make-flyspell-overlay beg end
1820 ;;*---------------------------------------------------------------------*/
1821 ;;* flyspell-auto-correct-cache ... */
1822 ;;*---------------------------------------------------------------------*/
1823 (defvar flyspell-auto-correct-pos nil)
1824 (defvar flyspell-auto-correct-region nil)
1825 (defvar flyspell-auto-correct-ring nil)
1826 (defvar flyspell-auto-correct-word nil)
1827 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1828 (make-variable-buffer-local 'flyspell-auto-correct-region)
1829 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1830 (make-variable-buffer-local 'flyspell-auto-correct-word)
1832 ;;*---------------------------------------------------------------------*/
1833 ;;* flyspell-check-previous-highlighted-word ... */
1834 ;;*---------------------------------------------------------------------*/
1835 (defun flyspell-check-previous-highlighted-word (&optional arg)
1836 "Correct the closer misspelled word.
1837 This function scans a mis-spelled word before the cursor. If it finds one
1838 it proposes replacement for that word. With prefix arg, count that many
1839 misspelled words backwards."
1841 (let ((pos1 (point))
1843 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1846 (while (and (setq pos (previous-overlay-change pos))
1849 (if (> pos (point-min))
1851 (setq ovs (overlays-at (1- pos)))
1854 (setq ovs (cdr ovs))
1855 (if (and (flyspell-overlay-p ov)
1856 (= 0 (setq arg (1- arg))))
1857 (throw 'exit t)))))))
1861 (setq flyspell-word-cache-word nil) ;; Force flyspell-word re-check
1863 (error "No word to correct before point"))))
1865 ;;*---------------------------------------------------------------------*/
1866 ;;* flyspell-display-next-corrections ... */
1867 ;;*---------------------------------------------------------------------*/
1868 (defun flyspell-display-next-corrections (corrections)
1869 (let ((string "Corrections:")
1872 (while (< (length string) 80)
1873 (if (equal (car l) flyspell-auto-correct-word)
1874 (setq pos (cons (+ 1 (length string)) pos)))
1875 (setq string (concat string " " (car l)))
1878 (let ((num (car pos)))
1879 (put-text-property num
1880 (+ num (length flyspell-auto-correct-word))
1881 'face 'flyspell-incorrect
1883 (setq pos (cdr pos)))
1884 (if (fboundp 'display-message)
1885 (display-message 'no-log string)
1886 (message "%s" string))))
1888 ;;*---------------------------------------------------------------------*/
1889 ;;* flyspell-abbrev-table ... */
1890 ;;*---------------------------------------------------------------------*/
1891 (defun flyspell-abbrev-table ()
1892 (if flyspell-use-global-abbrev-table-p
1894 (or local-abbrev-table global-abbrev-table)))
1896 ;;*---------------------------------------------------------------------*/
1897 ;;* flyspell-define-abbrev ... */
1898 ;;*---------------------------------------------------------------------*/
1899 (defun flyspell-define-abbrev (name expansion)
1900 (let ((table (flyspell-abbrev-table)))
1902 (define-abbrev table (downcase name) expansion))))
1904 ;;*---------------------------------------------------------------------*/
1905 ;;* flyspell-auto-correct-word ... */
1906 ;;*---------------------------------------------------------------------*/
1907 (defun flyspell-auto-correct-word ()
1908 "Correct the current word.
1909 This command proposes various successive corrections for the current word."
1912 (old-max (point-max)))
1913 ;; use the correct dictionary
1914 (flyspell-accept-buffer-local-defs)
1915 (if (and (eq flyspell-auto-correct-pos pos)
1916 (consp flyspell-auto-correct-region))
1917 ;; we have already been using the function at the same location
1918 (let* ((start (car flyspell-auto-correct-region))
1919 (len (cdr flyspell-auto-correct-region)))
1920 (flyspell-unhighlight-at start)
1921 (delete-region start (+ start len))
1922 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1923 (let* ((word (car flyspell-auto-correct-ring))
1924 (len (length word)))
1925 (rplacd flyspell-auto-correct-region len)
1927 (if flyspell-abbrev-p
1928 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1929 flyspell-auto-correct-word)
1930 (flyspell-change-abbrev (flyspell-abbrev-table)
1931 flyspell-auto-correct-word
1933 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1934 (funcall flyspell-insert-function word)
1936 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1937 (flyspell-ajust-cursor-point pos (point) old-max)
1938 (setq flyspell-auto-correct-pos (point)))
1939 ;; fetch the word to be checked
1940 (let ((word (flyspell-get-word)))
1942 (let ((start (car (cdr word)))
1943 (end (car (cdr (cdr word))))
1946 (setq flyspell-auto-correct-word word)
1947 ;; now check spelling of word.
1948 (ispell-send-string "%\n") ;put in verbose mode
1949 (ispell-send-string (concat "^" word "\n"))
1950 ;; wait until ispell has processed word.
1952 (accept-process-output ispell-process)
1953 (not (string= "" (car ispell-filter)))))
1954 ;; Remove leading empty element
1955 (setq ispell-filter (cdr ispell-filter))
1956 ;; ispell process should return something after word is sent.
1957 ;; Tag word as valid (i.e., skip) otherwise
1959 (setq ispell-filter '(*)))
1960 (if (consp ispell-filter)
1961 (setq poss (ispell-parse-output (car ispell-filter))))
1963 ((or (eq poss t) (stringp poss))
1964 ;; don't correct word
1968 (error "Ispell: error in Ispell process"))
1970 ;; the word is incorrect, we have to propose a replacement
1971 (let ((replacements (if flyspell-sort-corrections
1972 (sort (car (cdr (cdr poss))) 'string<)
1973 (car (cdr (cdr poss))))))
1974 (setq flyspell-auto-correct-region nil)
1975 (if (consp replacements)
1977 (let ((replace (car replacements)))
1978 (let ((new-word replace))
1979 (if (not (equal new-word (car poss)))
1981 ;; the save the current replacements
1982 (setq flyspell-auto-correct-region
1983 (cons start (length new-word)))
1984 (let ((l replacements))
1985 (while (consp (cdr l))
1987 (rplacd l (cons (car poss) replacements)))
1988 (setq flyspell-auto-correct-ring
1990 (flyspell-unhighlight-at start)
1991 (delete-region start end)
1992 (funcall flyspell-insert-function new-word)
1993 (if flyspell-abbrev-p
1994 (if (flyspell-already-abbrevp
1995 (flyspell-abbrev-table) word)
1996 (flyspell-change-abbrev
1997 (flyspell-abbrev-table)
2000 (flyspell-define-abbrev word
2003 (flyspell-display-next-corrections
2004 (cons new-word flyspell-auto-correct-ring))
2005 (flyspell-ajust-cursor-point pos
2008 (setq flyspell-auto-correct-pos (point))
2009 (ispell-pdict-save t)))))))
2011 ;;*---------------------------------------------------------------------*/
2012 ;;* flyspell-auto-correct-previous-pos ... */
2013 ;;*---------------------------------------------------------------------*/
2014 (defvar flyspell-auto-correct-previous-pos nil
2015 "Holds the start of the first incorrect word before point.")
2017 ;;*---------------------------------------------------------------------*/
2018 ;;* flyspell-auto-correct-previous-hook ... */
2019 ;;*---------------------------------------------------------------------*/
2020 (defun flyspell-auto-correct-previous-hook ()
2021 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
2022 Sets `flyspell-auto-correct-previous-pos' to nil"
2024 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
2025 (unless (eq this-command (function flyspell-auto-correct-previous-word))
2026 (setq flyspell-auto-correct-previous-pos nil)))
2028 ;;*---------------------------------------------------------------------*/
2029 ;;* flyspell-auto-correct-previous-word ... */
2030 ;;*---------------------------------------------------------------------*/
2031 (defun flyspell-auto-correct-previous-word (position)
2032 "Auto correct the first misspelled word that occurs before point.
2033 But don't look beyond what's visible on the screen."
2036 (let ((top (window-start))
2040 (narrow-to-region top bot)
2041 (overlay-recenter (point))
2043 (add-hook 'pre-command-hook
2044 (function flyspell-auto-correct-previous-hook) t t)
2046 (unless flyspell-auto-correct-previous-pos
2047 ;; only reset if a new overlay exists
2048 (setq flyspell-auto-correct-previous-pos nil)
2050 (let ((overlay-list (overlays-in (point-min) position))
2051 (new-overlay 'dummy-value))
2053 ;; search for previous (new) flyspell overlay
2054 (while (and new-overlay
2055 (or (not (flyspell-overlay-p new-overlay))
2056 ;; check if its face has changed
2057 (not (eq (get-char-property
2058 (overlay-start new-overlay) 'face)
2059 'flyspell-incorrect))))
2060 (setq new-overlay (car-safe overlay-list))
2061 (setq overlay-list (cdr-safe overlay-list)))
2063 ;; if nothing new exits new-overlay should be nil
2064 (if new-overlay ;; the length of the word may change so go to the start
2065 (setq flyspell-auto-correct-previous-pos
2066 (overlay-start new-overlay)))))
2068 (when flyspell-auto-correct-previous-pos
2070 (goto-char flyspell-auto-correct-previous-pos)
2071 (let ((ispell-following-word t)) ;; point is at start
2072 (if (numberp flyspell-auto-correct-previous-pos)
2073 (goto-char flyspell-auto-correct-previous-pos))
2074 (flyspell-auto-correct-word))
2075 ;; the point may have moved so reset this
2076 (setq flyspell-auto-correct-previous-pos (point))))))))
2078 ;;*---------------------------------------------------------------------*/
2079 ;;* flyspell-correct-word ... */
2080 ;;*---------------------------------------------------------------------*/
2082 (defun flyspell-correct-word (event)
2083 "Pop up a menu of possible corrections for a misspelled word.
2084 The word checked is the word at the mouse position."
2086 (let ((save (point)))
2087 (mouse-set-point event)
2088 (flyspell-correct-word-before-point event save)))
2090 (defun flyspell-correct-word-before-point (&optional event opoint)
2091 "Pop up a menu of possible corrections for misspelled word before point.
2092 If EVENT is non-nil, it is the mouse event that invoked this operation;
2093 that controls where to put the menu.
2094 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2096 (unless (mouse-position)
2097 (error "Pop-up menus do not work on this terminal"))
2098 ;; use the correct dictionary
2099 (flyspell-accept-buffer-local-defs)
2100 (or opoint (setq opoint (point)))
2101 (let ((cursor-location (point))
2102 (word (flyspell-get-word)))
2104 (let ((start (car (cdr word)))
2105 (end (car (cdr (cdr word))))
2108 ;; now check spelling of word.
2109 (ispell-send-string "%\n") ;put in verbose mode
2110 (ispell-send-string (concat "^" word "\n"))
2111 ;; wait until ispell has processed word
2113 (accept-process-output ispell-process)
2114 (not (string= "" (car ispell-filter)))))
2115 ;; Remove leading empty element
2116 (setq ispell-filter (cdr ispell-filter))
2117 ;; ispell process should return something after word is sent.
2118 ;; Tag word as valid (i.e., skip) otherwise
2120 (setq ispell-filter '(*)))
2121 (if (consp ispell-filter)
2122 (setq poss (ispell-parse-output (car ispell-filter))))
2124 ((or (eq poss t) (stringp poss))
2125 ;; don't correct word
2129 (error "Ispell: error in Ispell process"))
2131 (flyspell-xemacs-popup
2132 poss word cursor-location start end opoint))
2134 ;; The word is incorrect, we have to propose a replacement.
2135 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2136 poss word cursor-location start end opoint)))
2137 (ispell-pdict-save t)))))
2139 ;;*---------------------------------------------------------------------*/
2140 ;;* flyspell-do-correct ... */
2141 ;;*---------------------------------------------------------------------*/
2142 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2143 "The popup menu callback."
2144 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2145 ;; it instead right after calling the function.
2146 (cond ((eq replace 'ignore)
2151 (ispell-send-string (concat "*" word "\n"))
2152 ;; This was added only to the XEmacs side in revision 1.18 of
2153 ;; flyspell. I assume its absence on the Emacs side was an
2154 ;; oversight. --Stef
2155 (ispell-send-string "#\n")
2156 (flyspell-unhighlight-at cursor-location)
2157 (setq ispell-pdict-modified-p '(t)))
2158 ((or (eq replace 'buffer) (eq replace 'session))
2159 (ispell-send-string (concat "@" word "\n"))
2160 (add-to-list 'ispell-buffer-session-localwords word)
2161 (or ispell-buffer-local-name ; session localwords might conflict
2162 (setq ispell-buffer-local-name (buffer-name)))
2163 (flyspell-unhighlight-at cursor-location)
2164 (if (null ispell-pdict-modified-p)
2165 (setq ispell-pdict-modified-p
2166 (list ispell-pdict-modified-p)))
2168 (if (eq replace 'buffer)
2169 (ispell-add-per-file-word-list word)))
2171 ;; This was added only to the Emacs side. I assume its absence on
2172 ;; the XEmacs side was an oversight. --Stef
2173 (flyspell-unhighlight-at cursor-location)
2174 (let ((old-max (point-max))
2175 (new-word (if (atom replace)
2178 (cursor-location (+ (- (length word) (- end start))
2180 (unless (equal new-word (car poss))
2181 (delete-region start end)
2183 (funcall flyspell-insert-function new-word)
2184 (if flyspell-abbrev-p
2185 (flyspell-define-abbrev word new-word)))
2186 ;; In the original Emacs code, this was only called in the body
2187 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2188 (flyspell-ajust-cursor-point save cursor-location old-max)))
2193 ;;*---------------------------------------------------------------------*/
2194 ;;* flyspell-ajust-cursor-point ... */
2195 ;;*---------------------------------------------------------------------*/
2196 (defun flyspell-ajust-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 (unless window-system
2213 (error "This command requires pop-up dialogs"))
2215 (let* ((mouse-pos (mouse-position))
2216 (mouse-pos (if (nth 1 mouse-pos)
2218 (set-mouse-position (car mouse-pos)
2219 (/ (frame-width) 2) 2)
2221 (setq event (list (list (car (cdr mouse-pos))
2222 (1+ (cdr (cdr mouse-pos))))
2224 (let* ((corrects (if flyspell-sort-corrections
2225 (sort (car (cdr (cdr poss))) 'string<)
2226 (car (cdr (cdr poss)))))
2227 (cor-menu (if (consp corrects)
2228 (mapcar (lambda (correct)
2229 (list correct correct))
2232 (affix (car (cdr (cdr (cdr poss)))))
2234 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2236 (list (concat "Save affix: " (car affix))
2238 '("Accept (session)" session)
2239 '("Accept (buffer)" buffer))
2240 '(("Save word" save)
2241 ("Accept (session)" session)
2242 ("Accept (buffer)" buffer)))))
2243 (if (consp cor-menu)
2244 (append cor-menu (cons "" save))
2246 (menu (cons "flyspell correction menu" base-menu)))
2247 (car (x-popup-menu event
2248 (list (format "%s [%s]" word (or ispell-local-dictionary
2252 ;;*---------------------------------------------------------------------*/
2253 ;;* flyspell-xemacs-popup ... */
2254 ;;*---------------------------------------------------------------------*/
2255 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2256 "The XEmacs popup menu."
2257 (let* ((corrects (if flyspell-sort-corrections
2258 (sort (car (cdr (cdr poss))) 'string<)
2259 (car (cdr (cdr poss)))))
2260 (cor-menu (if (consp corrects)
2261 (mapcar (lambda (correct)
2263 (list 'flyspell-do-correct
2274 (affix (car (cdr (cdr (cdr poss)))))
2276 (menu (let ((save (if (and (consp affix) show-affix-info)
2278 (concat "Save affix: " (car affix))
2279 (list 'flyspell-do-correct
2290 (list 'flyspell-do-correct
2299 (session (vector "Accept (session)"
2300 (list 'flyspell-do-correct
2309 (buffer (vector "Accept (buffer)"
2310 (list 'flyspell-do-correct
2319 (if (consp cor-menu)
2320 (append cor-menu (list "-" save session buffer))
2321 (list save session buffer)))))
2322 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2326 ;;*---------------------------------------------------------------------*/
2327 ;;* Some example functions for real autocorrecting */
2328 ;;*---------------------------------------------------------------------*/
2329 (defun flyspell-maybe-correct-transposition (beg end poss)
2330 "Check replacements for transposed characters.
2332 If the text between BEG and END is equal to a correction suggested by
2333 Ispell, after transposing two adjacent characters, correct the text,
2336 The third arg POSS is either the symbol 'doublon' or a list of
2337 possible corrections as returned by `ispell-parse-output'.
2339 This function is meant to be added to `flyspell-incorrect-hook'."
2342 (let ((str (buffer-substring beg end))
2343 (i 0) (len (- end beg)) tmp)
2344 (while (< (1+ i) len)
2345 (setq tmp (aref str i))
2346 (aset str i (aref str (1+ i)))
2347 (aset str (1+ i) tmp)
2348 (when (member str (nth 2 poss))
2350 (goto-char (+ beg i 1))
2351 (transpose-chars 1))
2353 (setq tmp (aref str i))
2354 (aset str i (aref str (1+ i)))
2355 (aset str (1+ i) tmp)
2359 (defun flyspell-maybe-correct-doubling (beg end poss)
2360 "Check replacements for doubled characters.
2362 If the text between BEG and END is equal to a correction suggested by
2363 Ispell, after removing a pair of doubled characters, correct the text,
2366 The third arg POSS is either the symbol 'doublon' or a list of
2367 possible corrections as returned by `ispell-parse-output'.
2369 This function is meant to be added to `flyspell-incorrect-hook'."
2372 (let ((str (buffer-substring beg end))
2373 (i 0) (len (- end beg)))
2374 (while (< (1+ i) len)
2375 (when (and (= (aref str i) (aref str (1+ i)))
2376 (member (concat (substring str 0 (1+ i))
2377 (substring str (+ i 2)))
2379 (goto-char (+ beg i))
2385 ;;*---------------------------------------------------------------------*/
2386 ;;* flyspell-already-abbrevp ... */
2387 ;;*---------------------------------------------------------------------*/
2388 (defun flyspell-already-abbrevp (table word)
2389 (let ((sym (abbrev-symbol word table)))
2390 (and sym (symbolp sym))))
2392 ;;*---------------------------------------------------------------------*/
2393 ;;* flyspell-change-abbrev ... */
2394 ;;*---------------------------------------------------------------------*/
2395 (defun flyspell-change-abbrev (table old new)
2396 (set (abbrev-symbol old table) new))
2400 ;;; flyspell.el ends here