1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
8 ;; Keywords: convenience
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
30 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
31 ;; This applies only to the current buffer.
33 ;; To enable Flyspell in text representing computer programs, type
34 ;; M-x flyspell-prog-mode.
35 ;; In that mode only text inside comments is checked.
37 ;; Note: consider setting the variable ispell-parser to `tex' to
38 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
40 ;; Some user variables control the behavior of flyspell. They are
41 ;; those defined under the `User variables' comment.
47 ;;*---------------------------------------------------------------------*/
49 ;;*---------------------------------------------------------------------*/
50 (defgroup flyspell nil
51 "Spell checking on the fly."
57 ;;*---------------------------------------------------------------------*/
58 ;;* User configuration ... */
59 ;;*---------------------------------------------------------------------*/
60 (defcustom flyspell-highlight-flag t
61 "How Flyspell should indicate misspelled words.
62 Non-nil means use highlight, nil means use minibuffer messages."
66 (defcustom flyspell-mark-duplications-flag t
67 "Non-nil means Flyspell reports a repeated word as an error.
68 See `flyspell-mark-duplications-exceptions' to add exceptions to this rule.
69 Detection of repeated words is not implemented in
70 \"large\" regions; see `flyspell-large-region'."
74 (defcustom flyspell-mark-duplications-exceptions
75 '(("francais" .
("nous" "vous")))
76 "A list of exceptions for duplicated words.
77 It should be a list of (LANGUAGE . EXCEPTION-LIST). LANGUAGE is matched
78 against the current dictionary and EXCEPTION-LIST is a list of strings.
79 The duplicated word is downcased before it is compared with the exceptions."
81 :type
'(alist :key-type string
:value-type
(repeat string
)))
83 (defcustom flyspell-sort-corrections nil
84 "Non-nil means, sort the corrections alphabetically before popping them."
89 (defcustom flyspell-duplicate-distance -
1
90 "The maximum distance for finding duplicates of unrecognized words.
91 This applies to the feature that when a word is not found in the dictionary,
92 if the same spelling occurs elsewhere in the buffer,
93 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
94 This variable specifies how far to search to find such a duplicate.
95 -1 means no limit (search the whole buffer).
96 0 means do not search for duplicate unrecognized spellings."
99 :type
'(choice (const :tag
"no limit" -
1)
102 (defcustom flyspell-delay
3
103 "The number of seconds to wait before checking, after a \"delayed\" command."
107 (defcustom flyspell-persistent-highlight t
108 "Non-nil means misspelled words remain highlighted until corrected.
109 If this variable is nil, only the most recently detected misspelled word
114 (defcustom flyspell-highlight-properties t
115 "Non-nil means highlight incorrect words even if a property exists for this word."
119 (defcustom flyspell-default-delayed-commands
120 '(self-insert-command
122 backward-or-forward-delete-char
124 scrollbar-vertical-drag
125 backward-delete-char-untabify
)
126 "The standard list of delayed commands for Flyspell.
127 See `flyspell-delayed-commands'."
130 :type
'(repeat (symbol)))
132 (defcustom flyspell-delayed-commands nil
133 "List of commands that are \"delayed\" for Flyspell mode.
134 After these commands, Flyspell checking is delayed for a short time,
135 whose length is specified by `flyspell-delay'."
137 :type
'(repeat (symbol)))
139 (defcustom flyspell-default-deplacement-commands
144 "The standard list of deplacement commands for Flyspell.
145 See `flyspell-deplacement-commands'."
148 :type
'(repeat (symbol)))
150 (defcustom flyspell-deplacement-commands nil
151 "List of commands that are \"deplacement\" for Flyspell mode.
152 After these commands, Flyspell checking is performed only if the previous
153 command was not the very same command."
156 :type
'(repeat (symbol)))
158 (defcustom flyspell-issue-welcome-flag t
159 "Non-nil means that Flyspell should display a welcome message when started."
163 (defcustom flyspell-issue-message-flag t
164 "Non-nil means that Flyspell emits messages when checking words."
168 (defcustom flyspell-incorrect-hook nil
169 "List of functions to be called when incorrect words are encountered.
170 Each function is given three arguments. The first two
171 arguments are the beginning and the end of the incorrect region.
172 The third is either the symbol `doublon' or the list
173 of possible corrections as returned by `ispell-parse-output'.
175 If any of the functions return non-nil, the word is not highlighted as
181 (defcustom flyspell-default-dictionary nil
182 "A string that is the name of the default dictionary.
183 This is passed to the `ispell-change-dictionary' when flyspell is started.
184 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
185 when flyspell is started, the value of that variable is used instead
186 of `flyspell-default-dictionary' to select the default dictionary.
187 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
188 Ispell's ultimate default dictionary."
191 :type
'(choice string
(const :tag
"Default" nil
)))
193 (defcustom flyspell-tex-command-regexp
194 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
195 "A string that is the regular expression that matches TeX commands."
200 (defcustom flyspell-check-tex-math-command nil
201 "Non-nil means check even inside TeX math environment.
202 TeX math environments are discovered by the TEXMATHP that implemented
203 inside the texmathp.el Emacs package. That package may be found at:
204 http://strw.leidenuniv.nl/~dominik/Tools"
208 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
209 '("francais" "deutsch8" "norsk")
210 "List of dictionary names that consider `-' as word delimiter."
213 :type
'(repeat (string)))
215 (defcustom flyspell-abbrev-p
217 "If non-nil, add correction to abbreviation table."
222 (defcustom flyspell-use-global-abbrev-table-p
224 "If non-nil, prefer global abbrev table to local abbrev table."
229 (defcustom flyspell-mode-line-string
" Fly"
230 "String displayed on the modeline when flyspell is active.
231 Set this to nil if you don't want a modeline indicator."
233 :type
'(choice string
(const :tag
"None" nil
)))
235 (defcustom flyspell-large-region
1000
236 "The threshold that determines if a region is small.
237 If the region is smaller than this number of characters,
238 `flyspell-region' checks the words sequentially using regular
239 flyspell methods. Else, if the region is large, a new Ispell process is
242 Doubled words are not detected in a large region, because Ispell
243 does not check for them.
245 If `flyspell-large-region' is nil, all regions are treated as small."
248 :type
'(choice number
(const :tag
"All small" nil
)))
250 (defcustom flyspell-insert-function
(function insert
)
251 "Function for inserting word by flyspell upon correction."
255 (defcustom flyspell-before-incorrect-word-string nil
256 "String used to indicate an incorrect word starting."
258 :type
'(choice string
(const nil
)))
260 (defcustom flyspell-after-incorrect-word-string nil
261 "String used to indicate an incorrect word ending."
263 :type
'(choice string
(const nil
)))
265 (defcustom flyspell-use-meta-tab t
266 "Non-nil means that flyspell uses M-TAB to correct word."
270 (defcustom flyspell-auto-correct-binding
272 "The key binding for flyspell auto correction."
275 ;;*---------------------------------------------------------------------*/
276 ;;* Mode specific options */
277 ;;* ------------------------------------------------------------- */
278 ;;* Mode specific options enable users to disable flyspell on */
279 ;;* certain word depending of the emacs mode. For instance, when */
280 ;;* using flyspell with mail-mode add the following expression */
281 ;;* in your .emacs file: */
282 ;;* (add-hook 'mail-mode */
283 ;;* '(lambda () (setq flyspell-generic-check-word-predicate */
284 ;;* 'mail-mode-flyspell-verify))) */
285 ;;*---------------------------------------------------------------------*/
286 (defvar flyspell-generic-check-word-predicate nil
287 "Function providing per-mode customization over which words are flyspelled.
288 Returns t to continue checking, nil otherwise.
289 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
290 property of the major mode name.")
291 (make-variable-buffer-local 'flyspell-generic-check-word-predicate
)
292 (defvaralias 'flyspell-generic-check-word-p
293 'flyspell-generic-check-word-predicate
)
295 ;;*--- mail mode -------------------------------------------------------*/
296 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
297 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
298 (defvar message-signature-separator
)
299 (defun mail-mode-flyspell-verify ()
300 "Function used for `flyspell-generic-check-word-predicate' in Mail mode."
301 (let ((header-end (save-excursion
302 (goto-char (point-min))
305 (regexp-quote mail-header-separator
)
310 (if (not (boundp 'message-signature-separator
))
313 (goto-char (point-max))
314 (re-search-backward message-signature-separator nil t
)
316 (cond ((< (point) header-end
)
317 (and (save-excursion (beginning-of-line)
318 (looking-at "^Subject:"))
319 (> (point) (match-end 0))))
320 ((> (point) signature-begin
)
325 (not (looking-at "[>}|]\\|To:")))))))
327 ;;*--- texinfo mode ----------------------------------------------------*/
328 (put 'texinfo-mode
'flyspell-mode-predicate
'texinfo-mode-flyspell-verify
)
329 (defun texinfo-mode-flyspell-verify ()
330 "Function used for `flyspell-generic-check-word-predicate' in Texinfo mode."
333 (not (looking-at "@"))))
335 ;;*--- tex mode --------------------------------------------------------*/
336 (put 'tex-mode
'flyspell-mode-predicate
'tex-mode-flyspell-verify
)
337 (defun tex-mode-flyspell-verify ()
338 "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
341 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t
)))
343 (let ((this (point-marker))
344 (e (progn (end-of-line) (point-marker))))
346 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t
)
347 (and (>= this
(match-beginning 0))
348 (<= this
(match-end 0)) )))))))
350 ;;*--- sgml mode -------------------------------------------------------*/
351 (put 'sgml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
352 (put 'html-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
353 (put 'nxml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
355 (defun sgml-mode-flyspell-verify ()
356 "Function used for `flyspell-generic-check-word-predicate' in SGML mode."
358 (let ((this (point-marker))
359 (s (progn (beginning-of-line) (point-marker)))
360 (e (progn (end-of-line) (point-marker))))
363 (and (re-search-forward "[^<]*>" e t
)
364 (= (match-beginning 0) this
)))
367 (and (re-search-backward "<[^>]*" s t
)
368 (= (match-end 0) this
)))
371 (and (re-search-forward "[^&]*;" e t
)
372 (= (match-beginning 0) this
)))
375 (and (re-search-backward "&[^;]*" s t
)
376 (= (match-end 0) this
)))))))))
378 ;;*---------------------------------------------------------------------*/
379 ;;* Programming mode */
380 ;;*---------------------------------------------------------------------*/
381 (defvar flyspell-prog-text-faces
382 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
383 "Faces corresponding to text in programming-mode buffers.")
385 (defun flyspell-generic-progmode-verify ()
386 "Used for `flyspell-generic-check-word-predicate' in programming modes."
387 (let ((f (get-text-property (point) 'face
)))
388 (memq f flyspell-prog-text-faces
)))
391 (defun flyspell-prog-mode ()
392 "Turn on `flyspell-mode' for comments and strings."
394 (setq flyspell-generic-check-word-predicate
395 'flyspell-generic-progmode-verify
)
397 (run-hooks 'flyspell-prog-mode-hook
))
399 ;;*---------------------------------------------------------------------*/
400 ;;* Overlay compatibility */
401 ;;*---------------------------------------------------------------------*/
402 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
403 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
404 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
405 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
406 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
407 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
408 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
409 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
411 ;;*---------------------------------------------------------------------*/
412 ;;* The minor mode declaration. */
413 ;;*---------------------------------------------------------------------*/
414 (defvar flyspell-mouse-map
415 (let ((map (make-sparse-keymap)))
416 (define-key map
(if (featurep 'xemacs
) [button2] [down-mouse-2])
417 #'flyspell-correct-word)
419 "Keymap for Flyspell to put on erroneous words.")
421 (defvar flyspell-mode-map
422 (let ((map (make-sparse-keymap)))
423 (if flyspell-use-meta-tab
424 (define-key map "\M-\t" 'flyspell-auto-correct-word))
425 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
426 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
427 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
428 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
430 "Minor mode keymap for Flyspell mode--for the whole buffer.")
432 ;; dash character machinery
433 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
434 "*Non-nil means that the `-' char is considered as a word delimiter.")
435 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
436 (defvar flyspell-dash-dictionary nil)
437 (make-variable-buffer-local 'flyspell-dash-dictionary)
438 (defvar flyspell-dash-local-dictionary nil)
439 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
441 ;;*---------------------------------------------------------------------*/
443 ;;*---------------------------------------------------------------------*/
444 (defface flyspell-incorrect
445 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
447 "Face used for marking a misspelled word in Flyspell."
449 ;; backward-compatibility alias
450 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
452 (defface flyspell-duplicate
453 '((((class color)) (:foreground "Gold3" :bold t :underline t))
455 "Face used for marking a misspelled word that appears twice in the buffer.
456 See also `flyspell-duplicate-distance'."
458 ;; backward-compatibility alias
459 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
461 (defvar flyspell-overlay nil)
463 ;;*---------------------------------------------------------------------*/
464 ;;* flyspell-mode ... */
465 ;;*---------------------------------------------------------------------*/
466 ;;;###autoload(defvar flyspell-mode nil)
468 (define-minor-mode flyspell-mode
469 "Minor mode performing on-the-fly spelling checking.
470 This spawns a single Ispell process and checks each word.
471 The default flyspell behavior is to highlight incorrect words.
472 With no argument, this command toggles Flyspell mode.
473 With a prefix argument ARG, turn Flyspell minor mode on if ARG is positive,
474 otherwise turn it off.
477 \\[ispell-word]: correct words (using Ispell).
478 \\[flyspell-auto-correct-word]: automatically correct word.
479 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
480 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
483 This runs `flyspell-mode-hook' after flyspell mode is entered or exit.
486 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
487 valid. For instance, a different dictionary can be used by
488 invoking `ispell-change-dictionary'.
490 Consider using the `ispell-parser' to check your text. For instance
492 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
495 \\[flyspell-region] checks all words inside a region.
496 \\[flyspell-buffer] checks the whole buffer."
497 :lighter flyspell-mode-line-string
498 :keymap flyspell-mode-map
503 (error (message "Enabling Flyspell mode gave an error")
505 (flyspell-mode-off)))
508 (defun turn-on-flyspell ()
509 "Unconditionally turn on Flyspell mode."
513 (defun turn-off-flyspell ()
514 "Unconditionally turn off Flyspell mode."
517 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
519 ;;*---------------------------------------------------------------------*/
520 ;;* flyspell-buffers ... */
521 ;;* ------------------------------------------------------------- */
522 ;;* For remembering buffers running flyspell */
523 ;;*---------------------------------------------------------------------*/
524 (defvar flyspell-buffers nil)
526 ;;*---------------------------------------------------------------------*/
527 ;;* flyspell-minibuffer-p ... */
528 ;;*---------------------------------------------------------------------*/
529 (defun flyspell-minibuffer-p (buffer)
530 "Is BUFFER a minibuffer?"
531 (let ((ws (get-buffer-window-list buffer t)))
532 (and (consp ws) (window-minibuffer-p (car ws)))))
534 ;;*---------------------------------------------------------------------*/
535 ;;* flyspell-accept-buffer-local-defs ... */
536 ;;*---------------------------------------------------------------------*/
537 (defvar flyspell-last-buffer nil
538 "The buffer in which the last flyspell operation took place.")
540 (defun flyspell-accept-buffer-local-defs (&optional force)
541 ;; When flyspell-word is used inside a loop (e.g. when processing
542 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
543 ;; up dwarfing everything else, so only do it when the buffer has changed.
544 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
545 (setq flyspell-last-buffer (current-buffer))
546 ;; Strange problem: If buffer in current window has font-lock turned on,
547 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
548 ;; call will reset the buffer to the buffer in the current window.
549 ;; However, it only happens at startup (fix by Albert L. Ting).
551 (ispell-accept-buffer-local-defs))
552 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
553 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
554 ;; The dictionary has changed
555 (setq flyspell-dash-dictionary ispell-dictionary)
556 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
557 (setq flyspell-consider-dash-as-word-delimiter-flag
558 (member (or ispell-local-dictionary ispell-dictionary)
559 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
561 (defun flyspell-hack-local-variables-hook ()
562 ;; When local variables are loaded, see if the dictionary context
564 (flyspell-accept-buffer-local-defs 'force))
566 (defun flyspell-kill-ispell-hook ()
567 (setq flyspell-last-buffer nil)
568 (dolist (buf (buffer-list))
569 (with-current-buffer buf
570 (kill-local-variable 'flyspell-word-cache-word))))
572 ;; Make sure we flush our caches when needed. Do it here rather than in
573 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
575 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
577 ;;*---------------------------------------------------------------------*/
578 ;;* flyspell-mode-on ... */
579 ;;*---------------------------------------------------------------------*/
580 (defun flyspell-mode-on ()
581 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
582 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
583 (setq ispell-highlight-face 'flyspell-incorrect)
584 ;; local dictionaries setup
585 (or ispell-local-dictionary ispell-dictionary
586 (if flyspell-default-dictionary
587 (ispell-change-dictionary flyspell-default-dictionary)))
588 ;; we have to force ispell to accept the local definition or
589 ;; otherwise it could be too late, the local dictionary may
591 ;; Pass the `force' argument for the case where flyspell was active already
592 ;; but the buffer's local-defs have been edited.
593 (flyspell-accept-buffer-local-defs 'force)
594 ;; we put the `flyspell-delayed' property on some commands
595 (flyspell-delay-commands)
596 ;; we put the `flyspell-deplacement' property on some commands
597 (flyspell-deplacement-commands)
598 ;; we bound flyspell action to post-command hook
599 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
600 ;; we bound flyspell action to pre-command hook
601 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
602 ;; we bound flyspell action to after-change hook
603 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
604 ;; we bound flyspell action to hack-local-variables-hook
605 (add-hook 'hack-local-variables-hook
606 (function flyspell-hack-local-variables-hook) t t)
607 ;; set flyspell-generic-check-word-predicate based on the major mode
608 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
610 (setq flyspell-generic-check-word-predicate mode-predicate)))
611 ;; the welcome message
612 (if (and flyspell-issue-message-flag
613 flyspell-issue-welcome-flag
615 (let ((binding (where-is-internal 'flyspell-auto-correct-word
619 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
620 (key-description binding))
621 "Welcome to flyspell. Use Mouse-2 to correct words.")))))
623 ;;*---------------------------------------------------------------------*/
624 ;;* flyspell-delay-commands ... */
625 ;;*---------------------------------------------------------------------*/
626 (defun flyspell-delay-commands ()
627 "Install the standard set of Flyspell delayed commands."
628 (mapc 'flyspell-delay-command flyspell-default-delayed-commands)
629 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
631 ;;*---------------------------------------------------------------------*/
632 ;;* flyspell-delay-command ... */
633 ;;*---------------------------------------------------------------------*/
634 (defun flyspell-delay-command (command)
635 "Set COMMAND to be delayed, for Flyspell.
636 When flyspell `post-command-hook' is invoked because a delayed command
637 as been used the current word is not immediately checked.
638 It will be checked only after `flyspell-delay' seconds."
639 (interactive "SDelay Flyspell after Command: ")
640 (put command 'flyspell-delayed t))
642 ;;*---------------------------------------------------------------------*/
643 ;;* flyspell-deplacement-commands ... */
644 ;;*---------------------------------------------------------------------*/
645 (defun flyspell-deplacement-commands ()
646 "Install the standard set of Flyspell deplacement commands."
647 (mapc 'flyspell-deplacement-command flyspell-default-deplacement-commands)
648 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
650 ;;*---------------------------------------------------------------------*/
651 ;;* flyspell-deplacement-command ... */
652 ;;*---------------------------------------------------------------------*/
653 (defun flyspell-deplacement-command (command)
654 "Set COMMAND that implement cursor movements, for Flyspell.
655 When flyspell `post-command-hook' is invoked because of a deplacement command
656 as been used the current word is checked only if the previous command was
657 not the very same deplacement command."
658 (interactive "SDeplacement Flyspell after Command: ")
659 (put command 'flyspell-deplacement t))
661 ;;*---------------------------------------------------------------------*/
662 ;;* flyspell-word-cache ... */
663 ;;*---------------------------------------------------------------------*/
664 (defvar flyspell-word-cache-start nil)
665 (defvar flyspell-word-cache-end nil)
666 (defvar flyspell-word-cache-word nil)
667 (defvar flyspell-word-cache-result '_)
668 (make-variable-buffer-local 'flyspell-word-cache-start)
669 (make-variable-buffer-local 'flyspell-word-cache-end)
670 (make-variable-buffer-local 'flyspell-word-cache-word)
671 (make-variable-buffer-local 'flyspell-word-cache-result)
673 ;;*---------------------------------------------------------------------*/
674 ;;* The flyspell pre-hook, store the current position. In the */
675 ;;* post command hook, we will check, if the word at this position */
676 ;;* has to be spell checked. */
677 ;;*---------------------------------------------------------------------*/
678 (defvar flyspell-pre-buffer nil)
679 (defvar flyspell-pre-point nil)
680 (defvar flyspell-pre-column nil)
681 (defvar flyspell-pre-pre-buffer nil)
682 (defvar flyspell-pre-pre-point nil)
684 ;;*---------------------------------------------------------------------*/
685 ;;* flyspell-previous-command ... */
686 ;;*---------------------------------------------------------------------*/
687 (defvar flyspell-previous-command nil
688 "The last interactive command checked by Flyspell.")
690 ;;*---------------------------------------------------------------------*/
691 ;;* flyspell-pre-command-hook ... */
692 ;;*---------------------------------------------------------------------*/
693 (defun flyspell-pre-command-hook ()
694 "Save the current buffer and point for Flyspell's post-command hook."
696 (setq flyspell-pre-buffer (current-buffer))
697 (setq flyspell-pre-point (point))
698 (setq flyspell-pre-column (current-column)))
700 ;;*---------------------------------------------------------------------*/
701 ;;* flyspell-mode-off ... */
702 ;;*---------------------------------------------------------------------*/
704 (defun flyspell-mode-off ()
705 "Turn Flyspell mode off."
706 ;; we remove the hooks
707 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
708 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
709 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
710 (remove-hook 'hack-local-variables-hook
711 (function flyspell-hack-local-variables-hook) t)
712 ;; we remove all the flyspell hilightings
713 (flyspell-delete-all-overlays)
714 ;; we have to erase pre cache variables
715 (setq flyspell-pre-buffer nil)
716 (setq flyspell-pre-point nil)
717 ;; we mark the mode as killed
718 (setq flyspell-mode nil))
720 ;;*---------------------------------------------------------------------*/
721 ;;* flyspell-check-pre-word-p ... */
722 ;;*---------------------------------------------------------------------*/
723 (defun flyspell-check-pre-word-p ()
724 "Return non-nil if we should check the word before point.
725 More precisely, it applies to the word that was before point
726 before the current command."
728 ((or (not (numberp flyspell-pre-point))
729 (not (bufferp flyspell-pre-buffer))
730 (not (buffer-live-p flyspell-pre-buffer)))
732 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
733 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
735 ((or (and (= flyspell-pre-point (- (point) 1))
736 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
737 (= flyspell-pre-point (point))
738 (= flyspell-pre-point (+ (point) 1)))
740 ((and (symbolp this-command)
741 (not executing-kbd-macro)
742 (or (get this-command 'flyspell-delayed)
743 (and (get this-command 'flyspell-deplacement)
744 (eq flyspell-previous-command this-command)))
745 (or (= (current-column) 0)
746 (= (current-column) flyspell-pre-column)
747 ;; If other post-command-hooks change the buffer,
748 ;; flyspell-pre-point can lie past eob (bug#468).
749 (null (char-after flyspell-pre-point))
750 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
752 ((not (eq (current-buffer) flyspell-pre-buffer))
754 ((not (and (numberp flyspell-word-cache-start)
755 (numberp flyspell-word-cache-end)))
758 (or (< flyspell-pre-point flyspell-word-cache-start)
759 (> flyspell-pre-point flyspell-word-cache-end)))))
761 ;;*---------------------------------------------------------------------*/
762 ;;* The flyspell after-change-hook, store the change position. In */
763 ;;* the post command hook, we will check, if the word at this */
764 ;;* position has to be spell checked. */
765 ;;*---------------------------------------------------------------------*/
766 (defvar flyspell-changes nil)
767 (make-variable-buffer-local 'flyspell-changes)
769 ;;*---------------------------------------------------------------------*/
770 ;;* flyspell-after-change-function ... */
771 ;;*---------------------------------------------------------------------*/
772 (defun flyspell-after-change-function (start stop len)
773 "Save the current buffer and point for Flyspell's post-command hook."
774 (push (cons start stop) flyspell-changes))
776 ;;*---------------------------------------------------------------------*/
777 ;;* flyspell-check-changed-word-p ... */
778 ;;*---------------------------------------------------------------------*/
779 (defun flyspell-check-changed-word-p (start stop)
780 "Return t when the changed word has to be checked.
781 The answer depends of several criteria.
782 Mostly we check word delimiters."
784 ((and (memq (char-after start) '(?\n ? )) (> stop start))
786 ((not (numberp flyspell-pre-point))
788 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
790 ((let ((pos (point)))
791 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
796 ;;*---------------------------------------------------------------------*/
797 ;;* flyspell-check-word-p ... */
798 ;;*---------------------------------------------------------------------*/
799 (defun flyspell-check-word-p ()
800 "Return t when the word at `point' has to be checked.
801 The answer depends of several criteria.
802 Mostly we check word delimiters."
804 ((<= (- (point-max) 1) (point-min))
805 ;; the buffer is not filled enough
807 ((and (and (> (current-column) 0)
808 (not (eq (current-column) flyspell-pre-column)))
811 (and (looking-at (flyspell-get-not-casechars))
812 (or flyspell-consider-dash-as-word-delimiter-flag
813 (not (looking-at "-"))))))
814 ;; yes because we have reached or typed a word delimiter.
816 ((symbolp this-command)
818 ((get this-command 'flyspell-deplacement)
819 (not (eq flyspell-previous-command this-command)))
820 ((get this-command 'flyspell-delayed)
821 ;; the current command is not delayed, that
822 ;; is that we must check the word now
823 (and (not unread-command-events)
824 (sit-for flyspell-delay)))
828 ;;*---------------------------------------------------------------------*/
829 ;;* flyspell-debug-signal-no-check ... */
830 ;;*---------------------------------------------------------------------*/
831 (defun flyspell-debug-signal-no-check (msg obj)
832 (setq debug-on-error t)
833 (with-current-buffer (get-buffer-create "*flyspell-debug*")
835 (insert "NO-CHECK:\n")
836 (insert (format " %S : %S\n" msg obj))))
838 ;;*---------------------------------------------------------------------*/
839 ;;* flyspell-debug-signal-pre-word-checked ... */
840 ;;*---------------------------------------------------------------------*/
841 (defun flyspell-debug-signal-pre-word-checked ()
842 (setq debug-on-error t)
843 (with-current-buffer (get-buffer-create "*flyspell-debug*")
844 (insert "PRE-WORD:\n")
845 (insert (format " pre-point : %S\n" flyspell-pre-point))
846 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
847 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
848 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
849 (goto-char (point-max))))
851 ;;*---------------------------------------------------------------------*/
852 ;;* flyspell-debug-signal-word-checked ... */
853 ;;*---------------------------------------------------------------------*/
854 (defun flyspell-debug-signal-word-checked ()
855 (setq debug-on-error t)
856 (let ((oldbuf (current-buffer))
858 (with-current-buffer (get-buffer-create "*flyspell-debug*")
860 (insert (format " this-cmd : %S\n" this-command))
861 (insert (format " delayed : %S\n" (and (symbolp this-command)
862 (get this-command 'flyspell-delayed))))
863 (insert (format " point : %S\n" point))
864 (insert (format " prev-char : [%c] %S\n"
865 (with-current-buffer oldbuf
866 (let ((c (if (> (point) (point-min))
869 (char-after (point)))
872 (with-current-buffer oldbuf
873 (let ((c (if (> (point) (point-min))
876 (and (and (looking-at (flyspell-get-not-casechars)) 1)
877 (and (or flyspell-consider-dash-as-word-delimiter-flag
878 (not (looking-at "\\-"))) 2))))))
880 (insert (format " because : %S\n"
882 ((not (and (symbolp this-command)
883 (get this-command 'flyspell-delayed)))
884 ;; the current command is not delayed, that
885 ;; is that we must check the word now
887 ((with-current-buffer oldbuf
888 (let ((c (if (> (point) (point-min))
891 (and (looking-at (flyspell-get-not-casechars))
892 (or flyspell-consider-dash-as-word-delimiter-flag
893 (not (looking-at "\\-"))))))))
895 ;; yes because we have reached or typed a word delimiter.
897 ((not (integerp flyspell-delay))
898 ;; yes because the user had set up a no-delay configuration.
902 (goto-char (point-max)))))
904 ;;*---------------------------------------------------------------------*/
905 ;;* flyspell-debug-signal-changed-checked ... */
906 ;;*---------------------------------------------------------------------*/
907 (defun flyspell-debug-signal-changed-checked ()
908 (setq debug-on-error t)
909 (let ((point (point)))
910 (with-current-buffer (get-buffer-create "*flyspell-debug*")
911 (insert "CHANGED WORD:\n")
912 (insert (format " point : %S\n" point))
913 (goto-char (point-max)))))
915 ;;*---------------------------------------------------------------------*/
916 ;;* flyspell-post-command-hook ... */
917 ;;* ------------------------------------------------------------- */
918 ;;* It is possible that we check several words: */
919 ;;* 1- the current word is checked if the predicate */
920 ;;* FLYSPELL-CHECK-WORD-P is true */
921 ;;* 2- the word that used to be the current word before the */
922 ;;* THIS-COMMAND is checked if: */
923 ;;* a- the previous word is different from the current word */
924 ;;* b- the previous word as not just been checked by the */
925 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
926 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
927 ;;* previous word nor the current word */
928 ;;*---------------------------------------------------------------------*/
929 (defun flyspell-post-command-hook ()
930 "The `post-command-hook' used by flyspell to check a word in-the-fly."
932 (let ((command this-command)
933 ;; Prevent anything we do from affecting the mark.
935 (if (flyspell-check-pre-word-p)
936 (with-current-buffer flyspell-pre-buffer
937 '(flyspell-debug-signal-pre-word-checked)
939 (goto-char flyspell-pre-point)
941 (if (flyspell-check-word-p)
943 '(flyspell-debug-signal-word-checked)
945 ;; we remember which word we have just checked.
946 ;; this will be used next time we will check a word
947 ;; to compare the next current word with the word
948 ;; that as been registered in the pre-command-hook
949 ;; that is these variables are used within the predicate
950 ;; FLYSPELL-CHECK-PRE-WORD-P
951 (setq flyspell-pre-pre-buffer (current-buffer))
952 (setq flyspell-pre-pre-point (point)))
954 (setq flyspell-pre-pre-buffer nil)
955 (setq flyspell-pre-pre-point nil)
956 ;; when a word is not checked because of a delayed command
957 ;; we do not disable the ispell cache.
958 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
960 (setq flyspell-word-cache-end -1)
961 (setq flyspell-word-cache-result '_)))))
962 (while (and (not (input-pending-p)) (consp flyspell-changes))
963 (let ((start (car (car flyspell-changes)))
964 (stop (cdr (car flyspell-changes))))
965 (if (flyspell-check-changed-word-p start stop)
967 '(flyspell-debug-signal-changed-checked)
970 (setq flyspell-changes (cdr flyspell-changes))))
971 (setq flyspell-previous-command command)))
973 ;;*---------------------------------------------------------------------*/
974 ;;* flyspell-notify-misspell ... */
975 ;;*---------------------------------------------------------------------*/
976 (defun flyspell-notify-misspell (word poss)
977 (let ((replacements (if (stringp poss)
979 (if flyspell-sort-corrections
980 (sort (car (cdr (cdr poss))) 'string<)
981 (car (cdr (cdr poss)))))))
982 (if flyspell-issue-message-flag
983 (message "misspelling `%s' %S" word replacements))))
985 ;;*---------------------------------------------------------------------*/
986 ;;* flyspell-word-search-backward ... */
987 ;;*---------------------------------------------------------------------*/
988 (defun flyspell-word-search-backward (word bound)
991 (inhibit-point-motion-hooks t)
993 (while (and (not r) (setq p (search-backward word bound t)))
994 (let ((lw (flyspell-get-word '())))
995 (if (and (consp lw) (string-equal (car lw) word))
1000 ;;*---------------------------------------------------------------------*/
1001 ;;* flyspell-word-search-forward ... */
1002 ;;*---------------------------------------------------------------------*/
1003 (defun flyspell-word-search-forward (word bound)
1006 (inhibit-point-motion-hooks t)
1008 (while (and (not r) (setq p (search-forward word bound t)))
1009 (let ((lw (flyspell-get-word '())))
1010 (if (and (consp lw) (string-equal (car lw) word))
1012 (goto-char (1+ p)))))
1015 ;;*---------------------------------------------------------------------*/
1016 ;;* flyspell-word ... */
1017 ;;*---------------------------------------------------------------------*/
1018 (defun flyspell-word (&optional following)
1019 "Spell check a word."
1020 (interactive (list ispell-following-word))
1021 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1023 ;; use the correct dictionary
1024 (flyspell-accept-buffer-local-defs)
1025 (let* ((cursor-location (point))
1026 (flyspell-word (flyspell-get-word following))
1027 start end poss word ispell-filter)
1028 (if (or (eq flyspell-word nil)
1029 (and (fboundp flyspell-generic-check-word-predicate)
1030 (not (funcall flyspell-generic-check-word-predicate))))
1033 ;; destructure return flyspell-word info list.
1034 (setq start (car (cdr flyspell-word))
1035 end (car (cdr (cdr flyspell-word)))
1036 word (car flyspell-word))
1037 ;; before checking in the directory, we check for doublons.
1039 ((and (or (not (eq ispell-parser 'tex))
1040 (and (> start (point-min))
1041 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1042 flyspell-mark-duplications-flag
1043 (not (catch 'exception
1044 (dolist (except flyspell-mark-duplications-exceptions)
1045 (and (string= (or ispell-local-dictionary
1048 (member (downcase word) (cdr except))
1049 (throw 'exception t)))))
1055 (- (skip-chars-backward " \t\n\f"))))
1056 (p (when (>= bound (point-min))
1057 (flyspell-word-search-backward word bound))))
1058 (and p (/= p start)))))
1059 ;; yes, this is a doublon
1060 (flyspell-highlight-incorrect-region start end 'doublon)
1062 ((and (eq flyspell-word-cache-start start)
1063 (eq flyspell-word-cache-end end)
1064 (string-equal flyspell-word-cache-word word))
1065 ;; this word had been already checked, we skip
1066 flyspell-word-cache-result)
1067 ((and (eq ispell-parser 'tex)
1068 (flyspell-tex-command-p flyspell-word))
1069 ;; this is a correct word (because a tex command)
1070 (flyspell-unhighlight-at start)
1072 (flyspell-unhighlight-at (- end 1)))
1075 ;; we setup the cache
1076 (setq flyspell-word-cache-start start)
1077 (setq flyspell-word-cache-end end)
1078 (setq flyspell-word-cache-word word)
1079 ;; now check spelling of word.
1080 (ispell-send-string "%\n")
1081 ;; put in verbose mode
1082 (ispell-send-string (concat "^" word "\n"))
1083 ;; we mark the ispell process so it can be killed
1084 ;; when emacs is exited without query
1085 (set-process-query-on-exit-flag ispell-process nil)
1086 ;; Wait until ispell has processed word. Since this code is often
1087 ;; executed from post-command-hook but the ispell process may not
1088 ;; be responsive, it's important to make sure we re-enable C-g.
1091 (accept-process-output ispell-process)
1092 (not (string= "" (car ispell-filter))))))
1093 ;; (ispell-send-string "!\n")
1094 ;; back to terse mode.
1095 ;; Remove leading empty element
1096 (setq ispell-filter (cdr ispell-filter))
1097 ;; ispell process should return something after word is sent.
1098 ;; Tag word as valid (i.e., skip) otherwise
1100 (setq ispell-filter '(*)))
1101 (if (consp ispell-filter)
1102 (setq poss (ispell-parse-output (car ispell-filter))))
1103 (let ((res (cond ((eq poss t)
1105 (setq flyspell-word-cache-result t)
1106 (flyspell-unhighlight-at start)
1108 (flyspell-unhighlight-at (- end 1)))
1110 ((and (stringp poss) flyspell-highlight-flag)
1112 (setq flyspell-word-cache-result t)
1113 (flyspell-unhighlight-at start)
1115 (flyspell-unhighlight-at (- end 1)))
1118 (setq flyspell-word-cache-result t)
1119 (flyspell-unhighlight-at start)
1121 (flyspell-unhighlight-at (- end 1)))
1123 ((or (and (< flyspell-duplicate-distance 0)
1126 (flyspell-word-search-backward
1131 (flyspell-word-search-forward
1134 (and (> flyspell-duplicate-distance 0)
1137 (flyspell-word-search-backward
1140 flyspell-duplicate-distance)))
1143 (flyspell-word-search-forward
1146 flyspell-duplicate-distance))))))
1147 ;; This is a misspelled word which occurs
1148 ;; twice within flyspell-duplicate-distance.
1149 (setq flyspell-word-cache-result nil)
1150 (if flyspell-highlight-flag
1151 (flyspell-highlight-duplicate-region
1153 (message "duplicate `%s'" word))
1156 (setq flyspell-word-cache-result nil)
1157 ;; incorrect highlight the location
1158 (if flyspell-highlight-flag
1159 (flyspell-highlight-incorrect-region
1161 (flyspell-notify-misspell word poss))
1163 ;; return to original location
1164 (goto-char cursor-location)
1165 (if ispell-quit (setq ispell-quit nil))
1168 ;;*---------------------------------------------------------------------*/
1169 ;;* flyspell-math-tex-command-p ... */
1170 ;;* ------------------------------------------------------------- */
1171 ;;* This function uses the texmathp package to check if point */
1172 ;;* is within a TeX math environment. `texmathp' can yield errors */
1173 ;;* if the document is currently not valid TeX syntax. */
1174 ;;*---------------------------------------------------------------------*/
1175 (defun flyspell-math-tex-command-p ()
1176 (when (fboundp 'texmathp)
1177 (if flyspell-check-tex-math-command
1183 ;;*---------------------------------------------------------------------*/
1184 ;;* flyspell-tex-command-p ... */
1185 ;;*---------------------------------------------------------------------*/
1186 (defun flyspell-tex-command-p (word)
1187 "Return t if WORD is a TeX command."
1189 (let ((b (car (cdr word))))
1190 (and (re-search-backward "\\\\" (- (point) 100) t)
1191 (or (= (match-end 0) b)
1192 (and (goto-char (match-end 0))
1193 (looking-at flyspell-tex-command-regexp)
1194 (>= (match-end 0) b))))))
1195 (flyspell-math-tex-command-p)))
1197 ;;*---------------------------------------------------------------------*/
1198 ;;* flyspell-casechars-cache ... */
1199 ;;*---------------------------------------------------------------------*/
1200 (defvar flyspell-casechars-cache nil)
1201 (defvar flyspell-ispell-casechars-cache nil)
1202 (make-variable-buffer-local 'flyspell-casechars-cache)
1203 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1205 ;;*---------------------------------------------------------------------*/
1206 ;;* flyspell-get-casechars ... */
1207 ;;*---------------------------------------------------------------------*/
1208 (defun flyspell-get-casechars ()
1209 "This function builds a string that is the regexp of word chars.
1210 In order to avoid one useless string construction,
1211 this function changes the last char of the `ispell-casechars' string."
1212 (let ((ispell-casechars (ispell-get-casechars)))
1214 ((eq ispell-parser 'tex)
1215 (setq flyspell-ispell-casechars-cache ispell-casechars)
1216 (setq flyspell-casechars-cache
1217 (concat (substring ispell-casechars
1219 (- (length ispell-casechars) 1))
1221 flyspell-casechars-cache)
1223 (setq flyspell-ispell-casechars-cache ispell-casechars)
1224 (setq flyspell-casechars-cache ispell-casechars)
1225 flyspell-casechars-cache))))
1227 ;;*---------------------------------------------------------------------*/
1228 ;;* flyspell-get-not-casechars-cache ... */
1229 ;;*---------------------------------------------------------------------*/
1230 (defvar flyspell-not-casechars-cache nil)
1231 (defvar flyspell-ispell-not-casechars-cache nil)
1232 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1233 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1235 ;;*---------------------------------------------------------------------*/
1236 ;;* flyspell-get-not-casechars ... */
1237 ;;*---------------------------------------------------------------------*/
1238 (defun flyspell-get-not-casechars ()
1239 "This function builds a string that is the regexp of non-word chars."
1240 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1242 ((eq ispell-parser 'tex)
1243 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1244 (setq flyspell-not-casechars-cache
1245 (concat (substring ispell-not-casechars
1247 (- (length ispell-not-casechars) 1))
1249 flyspell-not-casechars-cache)
1251 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1252 (setq flyspell-not-casechars-cache ispell-not-casechars)
1253 flyspell-not-casechars-cache))))
1255 ;;*---------------------------------------------------------------------*/
1256 ;;* flyspell-get-word ... */
1257 ;;*---------------------------------------------------------------------*/
1258 (defun flyspell-get-word (following &optional extra-otherchars)
1259 "Return the word for spell-checking according to Ispell syntax.
1260 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1261 is non-nil when called interactively, then the following word
1262 \(rather than preceding\) is checked when the cursor is not over a word.
1263 Optional second argument contains otherchars that can be included in word
1266 Word syntax described by `flyspell-dictionary-alist' (which see)."
1267 (let* ((flyspell-casechars (flyspell-get-casechars))
1268 (flyspell-not-casechars (flyspell-get-not-casechars))
1269 (ispell-otherchars (ispell-get-otherchars))
1270 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1271 (word-regexp (concat flyspell-casechars
1273 (if (not (string= "" ispell-otherchars))
1274 (concat ispell-otherchars "?"))
1275 (if extra-otherchars
1276 (concat extra-otherchars "?"))
1279 (if (or ispell-many-otherchars-p
1285 (if (not (looking-at flyspell-casechars))
1287 (re-search-forward flyspell-casechars nil t)
1288 (re-search-backward flyspell-casechars nil t)))
1289 ;; move to front of word
1290 (re-search-backward flyspell-not-casechars nil 'start)
1291 (while (and (or (and (not (string= "" ispell-otherchars))
1292 (looking-at ispell-otherchars))
1293 (and extra-otherchars (looking-at extra-otherchars)))
1295 (or (not did-it-once)
1296 ispell-many-otherchars-p)
1297 (not (eq prevpt (point))))
1298 (if (and extra-otherchars (looking-at extra-otherchars))
1301 (if (looking-at flyspell-casechars)
1302 (re-search-backward flyspell-not-casechars nil 'move)))
1306 (if (looking-at flyspell-casechars)
1307 (re-search-backward flyspell-not-casechars nil 'move)
1308 (backward-char -1))))
1309 ;; Now mark the word and save to string.
1310 (if (not (re-search-forward word-regexp nil t))
1313 (setq start (match-beginning 0)
1315 word (buffer-substring-no-properties start end))
1316 (list word start end)))))
1318 ;;*---------------------------------------------------------------------*/
1319 ;;* flyspell-small-region ... */
1320 ;;*---------------------------------------------------------------------*/
1321 (defun flyspell-small-region (beg end)
1322 "Flyspell text between BEG and END."
1330 (while (< (point) end)
1331 (if (and flyspell-issue-message-flag (= count 100))
1333 (message "Spell Checking...%d%%"
1334 (* 100 (/ (float (- (point) beg)) (- end beg))))
1336 (setq count (+ 1 count)))
1339 (let ((cur (point)))
1341 (if (and (< (point) end) (> (point) (+ cur 1)))
1342 (backward-char 1)))))
1344 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1347 ;;*---------------------------------------------------------------------*/
1348 ;;* flyspell-external-ispell-process ... */
1349 ;;*---------------------------------------------------------------------*/
1350 (defvar flyspell-external-ispell-process '()
1351 "The external Flyspell Ispell process.")
1353 ;;*---------------------------------------------------------------------*/
1354 ;;* flyspell-external-ispell-buffer ... */
1355 ;;*---------------------------------------------------------------------*/
1356 (defvar flyspell-external-ispell-buffer '())
1357 (defvar flyspell-large-region-buffer '())
1358 (defvar flyspell-large-region-beg (point-min))
1359 (defvar flyspell-large-region-end (point-max))
1361 ;;*---------------------------------------------------------------------*/
1362 ;;* flyspell-external-point-words ... */
1363 ;;*---------------------------------------------------------------------*/
1364 (defun flyspell-external-point-words ()
1365 "Mark words from a buffer listing incorrect words in order of appearance.
1366 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1367 \(We finish by killing that buffer and setting the variable to nil.)
1368 The buffer to mark them in is `flyspell-large-region-buffer'."
1369 (let (words-not-found
1370 (ispell-otherchars (ispell-get-otherchars))
1371 (buffer-scan-pos flyspell-large-region-beg)
1373 (with-current-buffer flyspell-external-ispell-buffer
1374 (goto-char (point-min))
1375 ;; Loop over incorrect words, in the order they were reported,
1376 ;; which is also the order they appear in the buffer being checked.
1377 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1378 ;; Bind WORD to the next one.
1379 (let ((word (match-string 1)) (wordpos (point)))
1380 ;; Here there used to be code to see if WORD is the same
1381 ;; as the previous iteration, and count the number of consecutive
1382 ;; identical words, and the loop below would search for that many.
1383 ;; That code seemed to be incorrect, and on principle, should
1384 ;; be unnecessary too. -- rms.
1385 (if flyspell-issue-message-flag
1386 (message "Spell Checking...%d%% [%s]"
1387 (* 100 (/ (float (point)) (point-max)))
1389 (with-current-buffer flyspell-large-region-buffer
1390 (goto-char buffer-scan-pos)
1392 ;; Iterate on string search until string is found as word,
1395 (if (search-forward word
1396 flyspell-large-region-end t)
1399 ;; Move back into the match
1400 ;; so flyspell-get-word will find it.
1402 (flyspell-get-word nil)))
1403 (found (car found-list))
1404 (found-length (length found))
1405 (misspell-length (length word)))
1407 ;; Size matches, we really found it.
1408 (= found-length misspell-length)
1409 ;; Matches as part of a boundary-char separated word
1411 (split-string found ispell-otherchars))
1412 ;; Misspelling has higher length than
1413 ;; what flyspell considers the
1414 ;; word. Caused by boundary-chars
1415 ;; mismatch. Validating seems safe.
1416 (< found-length misspell-length)
1417 ;; ispell treats beginning of some TeX
1418 ;; commands as nroff control sequences
1419 ;; and strips them in the list of
1420 ;; misspelled words thus giving a
1421 ;; non-existent word. Skip if ispell
1422 ;; is used, string is a TeX command
1423 ;; (char before beginning of word is
1424 ;; backslash) and none of the previous
1426 (and (not ispell-really-aspell)
1428 (goto-char (- (nth 1 found-list) 1))
1429 (if (looking-at "[\\]" )
1434 ;; Search for next misspelled word will begin from
1435 ;; end of last validated match.
1436 (setq buffer-scan-pos (point))))
1437 ;; Record if misspelling is not found and try new one
1438 (add-to-list 'words-not-found
1439 (concat " -> " word " - "
1440 (int-to-string wordpos)))
1441 (setq keep nil)))))))
1443 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1444 ;; Warn about not found misspellings
1445 (dolist (word words-not-found)
1446 (message "%s: word not found" word))
1447 ;; Kill and forget the buffer with the list of incorrect words.
1448 (kill-buffer flyspell-external-ispell-buffer)
1449 (setq flyspell-external-ispell-buffer nil)))
1451 ;;*---------------------------------------------------------------------*/
1452 ;;* flyspell-process-localwords ... */
1453 ;;* ------------------------------------------------------------- */
1454 ;;* This function is used to prevent marking of words explicitly */
1455 ;;* declared correct. */
1456 ;;*---------------------------------------------------------------------*/
1457 (defun flyspell-process-localwords (misspellings-buffer)
1458 (let (localwords case-fold-search
1459 (ispell-casechars (ispell-get-casechars)))
1460 ;; Get localwords from the original buffer
1462 (goto-char (point-min))
1463 ;; Localwords parsing copied from ispell.el.
1464 (while (search-forward ispell-words-keyword nil t)
1465 (let ((end (save-excursion (end-of-line) (point)))
1467 ;; buffer-local words separated by a space, and can contain
1468 ;; any character other than a space. Not rigorous enough.
1469 (while (re-search-forward " *\\([^ ]+\\)" end t)
1470 (setq string (buffer-substring-no-properties (match-beginning 1)
1472 ;; This can fail when string contains a word with invalid chars.
1473 ;; Error handling needs to be added between Ispell and Emacs.
1474 (if (and (< 1 (length string))
1475 (equal 0 (string-match ispell-casechars string)))
1476 (push string localwords))))))
1477 ;; Remove localwords matches from misspellings-buffer.
1478 ;; The usual mechanism of communicating the local words to ispell
1479 ;; does not affect the special ispell process used by
1480 ;; flyspell-large-region.
1481 (with-current-buffer misspellings-buffer
1483 (dolist (word localwords)
1484 (goto-char (point-min))
1485 (let ((regexp (concat "^" word "\n")))
1486 (while (re-search-forward regexp nil t)
1487 (delete-region (match-beginning 0) (match-end 0)))))))))
1489 ;;* ---------------------------------------------------------------
1490 ;;* flyspell-check-region-doublons
1491 ;;* ---------------------------------------------------------------
1492 (defun flyspell-check-region-doublons (beg end)
1493 "Check for adjacent duplicated words (doublons) in the given region."
1496 (flyspell-word) ; Make sure current word is checked
1498 (while (and (< (point) end)
1499 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1505 ;;*---------------------------------------------------------------------*/
1506 ;;* flyspell-large-region ... */
1507 ;;*---------------------------------------------------------------------*/
1508 (defun flyspell-large-region (beg end)
1509 (let* ((curbuf (current-buffer))
1510 (buffer (get-buffer-create "*flyspell-region*")))
1511 (setq flyspell-external-ispell-buffer buffer)
1512 (setq flyspell-large-region-buffer curbuf)
1513 (setq flyspell-large-region-beg beg)
1514 (setq flyspell-large-region-end end)
1515 (flyspell-accept-buffer-local-defs)
1518 ;; this is done, we can start checking...
1519 (if flyspell-issue-message-flag (message "Checking region..."))
1521 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1522 ;; Local dictionary becomes the global dictionary in use.
1523 (setq ispell-current-dictionary
1524 (or ispell-local-dictionary ispell-dictionary))
1525 (setq ispell-current-personal-dictionary
1526 (or ispell-local-pdict ispell-personal-dictionary))
1527 (let ((args (ispell-get-ispell-args))
1528 (encoding (ispell-get-coding-system))
1530 (if (and ispell-current-dictionary ; use specified dictionary
1531 (not (member "-d" args))) ; only define if not overridden
1533 (append (list "-d" ispell-current-dictionary) args)))
1534 (if ispell-current-personal-dictionary ; use specified pers dict
1539 ispell-current-personal-dictionary)))))
1540 (setq args (append args ispell-extra-args))
1541 (if (and ispell-really-aspell
1542 ispell-aspell-supports-utf8)
1546 (concat "--encoding="
1549 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1550 (setq c (apply 'ispell-call-process-region beg
1556 (if ispell-really-aspell "list" "-l")
1560 (flyspell-process-localwords buffer)
1561 (with-current-buffer curbuf
1562 (flyspell-delete-region-overlays beg end)
1563 (flyspell-check-region-doublons beg end))
1564 (flyspell-external-point-words))
1565 (error "Can't check region...")))))
1567 ;;*---------------------------------------------------------------------*/
1568 ;;* flyspell-region ... */
1569 ;;* ------------------------------------------------------------- */
1570 ;;* Because `ispell -a' is too slow, it is not possible to use */
1571 ;;* it on large region. Then, when ispell is invoked on a large */
1572 ;;* text region, a new `ispell -l' process is spawned. The */
1573 ;;* pointed out words are then searched in the region a checked with */
1574 ;;* regular flyspell means. */
1575 ;;*---------------------------------------------------------------------*/
1577 (defun flyspell-region (beg end)
1578 "Flyspell text between BEG and END."
1580 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1588 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1589 (flyspell-large-region beg end)
1590 (flyspell-small-region beg end)))))
1592 ;;*---------------------------------------------------------------------*/
1593 ;;* flyspell-buffer ... */
1594 ;;*---------------------------------------------------------------------*/
1596 (defun flyspell-buffer ()
1597 "Flyspell whole buffer."
1599 (flyspell-region (point-min) (point-max)))
1601 ;;*---------------------------------------------------------------------*/
1602 ;;* old next error position ... */
1603 ;;*---------------------------------------------------------------------*/
1604 (defvar flyspell-old-buffer-error nil)
1605 (defvar flyspell-old-pos-error nil)
1607 ;;*---------------------------------------------------------------------*/
1608 ;;* flyspell-goto-next-error ... */
1609 ;;*---------------------------------------------------------------------*/
1610 (defun flyspell-goto-next-error ()
1611 "Go to the next previously detected error.
1612 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1617 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1618 (eq pos flyspell-old-pos-error))
1620 (if (= flyspell-old-pos-error max)
1621 ;; goto beginning of buffer
1623 (message "Restarting from beginning of buffer")
1624 (goto-char (point-min)))
1626 (setq pos (point))))
1627 ;; seek the next error
1628 (while (and (< pos max)
1629 (let ((ovs (overlays-at pos))
1631 (while (and (not r) (consp ovs))
1632 (if (flyspell-overlay-p (car ovs))
1634 (setq ovs (cdr ovs))))
1636 (setq pos (1+ pos)))
1637 ;; save the current location for next invocation
1638 (setq flyspell-old-pos-error pos)
1639 (setq flyspell-old-buffer-error (current-buffer))
1642 (message "No more miss-spelled word!"))))
1644 ;;*---------------------------------------------------------------------*/
1645 ;;* flyspell-overlay-p ... */
1646 ;;*---------------------------------------------------------------------*/
1647 (defun flyspell-overlay-p (o)
1648 "Return true if O is an overlay used by flyspell."
1649 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1651 ;;*---------------------------------------------------------------------*/
1652 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1653 ;;* ------------------------------------------------------------- */
1654 ;;* Remove overlays introduced by flyspell. */
1655 ;;*---------------------------------------------------------------------*/
1656 (defun flyspell-delete-region-overlays (beg end)
1657 "Delete overlays used by flyspell in a given region."
1658 (remove-overlays beg end 'flyspell-overlay t))
1661 (defun flyspell-delete-all-overlays ()
1662 "Delete all the overlays used by flyspell."
1663 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1665 ;;*---------------------------------------------------------------------*/
1666 ;;* flyspell-unhighlight-at ... */
1667 ;;*---------------------------------------------------------------------*/
1668 (defun flyspell-unhighlight-at (pos)
1669 "Remove the flyspell overlay that are located at POS."
1670 (if flyspell-persistent-highlight
1671 (let ((overlays (overlays-at pos)))
1672 (while (consp overlays)
1673 (if (flyspell-overlay-p (car overlays))
1674 (delete-overlay (car overlays)))
1675 (setq overlays (cdr overlays))))
1676 (if (flyspell-overlay-p flyspell-overlay)
1677 (delete-overlay flyspell-overlay))))
1679 ;;*---------------------------------------------------------------------*/
1680 ;;* flyspell-properties-at-p ... */
1681 ;;* ------------------------------------------------------------- */
1682 ;;* Is there an highlight properties at position pos? */
1683 ;;*---------------------------------------------------------------------*/
1684 (defun flyspell-properties-at-p (pos)
1685 "Return t if there is a text property at POS, not counting `local-map'.
1686 If variable `flyspell-highlight-properties' is set to nil,
1687 text with properties are not checked. This function is used to discover
1688 if the character at POS has any other property."
1689 (let ((prop (text-properties-at pos))
1691 (while (and keep (consp prop))
1692 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1693 (setq prop (cdr (cdr prop)))
1697 ;;*---------------------------------------------------------------------*/
1698 ;;* make-flyspell-overlay ... */
1699 ;;*---------------------------------------------------------------------*/
1700 (defun make-flyspell-overlay (beg end face mouse-face)
1701 "Allocate an overlay to highlight an incorrect word.
1702 BEG and END specify the range in the buffer of that word.
1703 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1705 (let ((overlay (make-overlay beg end nil t nil)))
1706 (overlay-put overlay 'face face)
1707 (overlay-put overlay 'mouse-face mouse-face)
1708 (overlay-put overlay 'flyspell-overlay t)
1709 (overlay-put overlay 'evaporate t)
1710 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1711 (overlay-put overlay 'keymap flyspell-mouse-map)
1712 (when (eq face 'flyspell-incorrect)
1713 (and (stringp flyspell-before-incorrect-word-string)
1714 (overlay-put overlay 'before-string
1715 flyspell-before-incorrect-word-string))
1716 (and (stringp flyspell-after-incorrect-word-string)
1717 (overlay-put overlay 'after-string
1718 flyspell-after-incorrect-word-string)))
1721 ;;*---------------------------------------------------------------------*/
1722 ;;* flyspell-highlight-incorrect-region ... */
1723 ;;*---------------------------------------------------------------------*/
1724 (defun flyspell-highlight-incorrect-region (beg end poss)
1725 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1726 POSS is usually a list of possible spelling/correction lists,
1727 as returned by `ispell-parse-output'.
1728 It can also be the symbol `doublon', in the case where the word
1729 is itself incorrect, but suspiciously repeated."
1730 (let ((inhibit-read-only t))
1731 (unless (run-hook-with-args-until-success
1732 'flyspell-incorrect-hook beg end poss)
1733 (if (or flyspell-highlight-properties
1734 (not (flyspell-properties-at-p beg)))
1736 ;; we cleanup all the overlay that are in the region, not
1737 ;; beginning at the word start position
1738 (if (< (1+ beg) end)
1739 (let ((os (overlays-in (1+ beg) end)))
1741 (if (flyspell-overlay-p (car os))
1742 (delete-overlay (car os)))
1743 (setq os (cdr os)))))
1744 ;; we cleanup current overlay at the same position
1745 (flyspell-unhighlight-at beg)
1746 ;; now we can use a new overlay
1747 (setq flyspell-overlay
1748 (make-flyspell-overlay
1750 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1753 ;;*---------------------------------------------------------------------*/
1754 ;;* flyspell-highlight-duplicate-region ... */
1755 ;;*---------------------------------------------------------------------*/
1756 (defun flyspell-highlight-duplicate-region (beg end poss)
1757 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1758 POSS is a list of possible spelling/correction lists,
1759 as returned by `ispell-parse-output'."
1760 (let ((inhibit-read-only t))
1761 (unless (run-hook-with-args-until-success
1762 'flyspell-incorrect-hook beg end poss)
1763 (if (or flyspell-highlight-properties
1764 (not (flyspell-properties-at-p beg)))
1766 ;; we cleanup current overlay at the same position
1767 (flyspell-unhighlight-at beg)
1768 ;; now we can use a new overlay
1769 (setq flyspell-overlay
1770 (make-flyspell-overlay beg end
1774 ;;*---------------------------------------------------------------------*/
1775 ;;* flyspell-auto-correct-cache ... */
1776 ;;*---------------------------------------------------------------------*/
1777 (defvar flyspell-auto-correct-pos nil)
1778 (defvar flyspell-auto-correct-region nil)
1779 (defvar flyspell-auto-correct-ring nil)
1780 (defvar flyspell-auto-correct-word nil)
1781 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1782 (make-variable-buffer-local 'flyspell-auto-correct-region)
1783 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1784 (make-variable-buffer-local 'flyspell-auto-correct-word)
1786 ;;*---------------------------------------------------------------------*/
1787 ;;* flyspell-check-previous-highlighted-word ... */
1788 ;;*---------------------------------------------------------------------*/
1789 (defun flyspell-check-previous-highlighted-word (&optional arg)
1790 "Correct the closer misspelled word.
1791 This function scans a mis-spelled word before the cursor. If it finds one
1792 it proposes replacement for that word. With prefix arg, count that many
1793 misspelled words backwards."
1795 (let ((pos1 (point))
1797 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1800 (while (and (setq pos (previous-overlay-change pos))
1803 (if (> pos (point-min))
1805 (setq ovs (overlays-at (1- pos)))
1808 (setq ovs (cdr ovs))
1809 (if (and (flyspell-overlay-p ov)
1810 (= 0 (setq arg (1- arg))))
1811 (throw 'exit t)))))))
1815 (error "No word to correct before point"))))
1817 ;;*---------------------------------------------------------------------*/
1818 ;;* flyspell-display-next-corrections ... */
1819 ;;*---------------------------------------------------------------------*/
1820 (defun flyspell-display-next-corrections (corrections)
1821 (let ((string "Corrections:")
1824 (while (< (length string) 80)
1825 (if (equal (car l) flyspell-auto-correct-word)
1826 (setq pos (cons (+ 1 (length string)) pos)))
1827 (setq string (concat string " " (car l)))
1830 (let ((num (car pos)))
1831 (put-text-property num
1832 (+ num (length flyspell-auto-correct-word))
1833 'face 'flyspell-incorrect
1835 (setq pos (cdr pos)))
1836 (if (fboundp 'display-message)
1837 (display-message 'no-log string)
1838 (message "%s" string))))
1840 ;;*---------------------------------------------------------------------*/
1841 ;;* flyspell-abbrev-table ... */
1842 ;;*---------------------------------------------------------------------*/
1843 (defun flyspell-abbrev-table ()
1844 (if flyspell-use-global-abbrev-table-p
1846 (or local-abbrev-table global-abbrev-table)))
1848 ;;*---------------------------------------------------------------------*/
1849 ;;* flyspell-define-abbrev ... */
1850 ;;*---------------------------------------------------------------------*/
1851 (defun flyspell-define-abbrev (name expansion)
1852 (let ((table (flyspell-abbrev-table)))
1854 (define-abbrev table (downcase name) expansion))))
1856 ;;*---------------------------------------------------------------------*/
1857 ;;* flyspell-auto-correct-word ... */
1858 ;;*---------------------------------------------------------------------*/
1859 (defun flyspell-auto-correct-word ()
1860 "Correct the current word.
1861 This command proposes various successive corrections for the current word."
1864 (old-max (point-max)))
1865 ;; use the correct dictionary
1866 (flyspell-accept-buffer-local-defs)
1867 (if (and (eq flyspell-auto-correct-pos pos)
1868 (consp flyspell-auto-correct-region))
1869 ;; we have already been using the function at the same location
1870 (let* ((start (car flyspell-auto-correct-region))
1871 (len (cdr flyspell-auto-correct-region)))
1872 (flyspell-unhighlight-at start)
1873 (delete-region start (+ start len))
1874 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1875 (let* ((word (car flyspell-auto-correct-ring))
1876 (len (length word)))
1877 (rplacd flyspell-auto-correct-region len)
1879 (if flyspell-abbrev-p
1880 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1881 flyspell-auto-correct-word)
1882 (flyspell-change-abbrev (flyspell-abbrev-table)
1883 flyspell-auto-correct-word
1885 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1886 (funcall flyspell-insert-function word)
1888 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1889 (flyspell-ajust-cursor-point pos (point) old-max)
1890 (setq flyspell-auto-correct-pos (point)))
1891 ;; fetch the word to be checked
1892 (let ((word (flyspell-get-word nil)))
1894 (let ((start (car (cdr word)))
1895 (end (car (cdr (cdr word))))
1898 (setq flyspell-auto-correct-word word)
1899 ;; now check spelling of word.
1900 (ispell-send-string "%\n") ;put in verbose mode
1901 (ispell-send-string (concat "^" word "\n"))
1902 ;; wait until ispell has processed word.
1904 (accept-process-output ispell-process)
1905 (not (string= "" (car ispell-filter)))))
1906 ;; Remove leading empty element
1907 (setq ispell-filter (cdr ispell-filter))
1908 ;; ispell process should return something after word is sent.
1909 ;; Tag word as valid (i.e., skip) otherwise
1911 (setq ispell-filter '(*)))
1912 (if (consp ispell-filter)
1913 (setq poss (ispell-parse-output (car ispell-filter))))
1915 ((or (eq poss t) (stringp poss))
1916 ;; don't correct word
1920 (error "Ispell: error in Ispell process"))
1922 ;; the word is incorrect, we have to propose a replacement
1923 (let ((replacements (if flyspell-sort-corrections
1924 (sort (car (cdr (cdr poss))) 'string<)
1925 (car (cdr (cdr poss))))))
1926 (setq flyspell-auto-correct-region nil)
1927 (if (consp replacements)
1929 (let ((replace (car replacements)))
1930 (let ((new-word replace))
1931 (if (not (equal new-word (car poss)))
1933 ;; the save the current replacements
1934 (setq flyspell-auto-correct-region
1935 (cons start (length new-word)))
1936 (let ((l replacements))
1937 (while (consp (cdr l))
1939 (rplacd l (cons (car poss) replacements)))
1940 (setq flyspell-auto-correct-ring
1942 (flyspell-unhighlight-at start)
1943 (delete-region start end)
1944 (funcall flyspell-insert-function new-word)
1945 (if flyspell-abbrev-p
1946 (if (flyspell-already-abbrevp
1947 (flyspell-abbrev-table) word)
1948 (flyspell-change-abbrev
1949 (flyspell-abbrev-table)
1952 (flyspell-define-abbrev word
1955 (flyspell-display-next-corrections
1956 (cons new-word flyspell-auto-correct-ring))
1957 (flyspell-ajust-cursor-point pos
1960 (setq flyspell-auto-correct-pos (point))
1961 (ispell-pdict-save t)))))))
1963 ;;*---------------------------------------------------------------------*/
1964 ;;* flyspell-auto-correct-previous-pos ... */
1965 ;;*---------------------------------------------------------------------*/
1966 (defvar flyspell-auto-correct-previous-pos nil
1967 "Holds the start of the first incorrect word before point.")
1969 ;;*---------------------------------------------------------------------*/
1970 ;;* flyspell-auto-correct-previous-hook ... */
1971 ;;*---------------------------------------------------------------------*/
1972 (defun flyspell-auto-correct-previous-hook ()
1973 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1974 Sets `flyspell-auto-correct-previous-pos' to nil"
1976 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1977 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1978 (setq flyspell-auto-correct-previous-pos nil)))
1980 ;;*---------------------------------------------------------------------*/
1981 ;;* flyspell-auto-correct-previous-word ... */
1982 ;;*---------------------------------------------------------------------*/
1983 (defun flyspell-auto-correct-previous-word (position)
1984 "Auto correct the first mispelled word that occurs before point.
1985 But don't look beyond what's visible on the screen."
1988 (let ((top (window-start))
1992 (narrow-to-region top bot)
1993 (overlay-recenter (point))
1995 (add-hook 'pre-command-hook
1996 (function flyspell-auto-correct-previous-hook) t t)
1998 (unless flyspell-auto-correct-previous-pos
1999 ;; only reset if a new overlay exists
2000 (setq flyspell-auto-correct-previous-pos nil)
2002 (let ((overlay-list (overlays-in (point-min) position))
2003 (new-overlay 'dummy-value))
2005 ;; search for previous (new) flyspell overlay
2006 (while (and new-overlay
2007 (or (not (flyspell-overlay-p new-overlay))
2008 ;; check if its face has changed
2009 (not (eq (get-char-property
2010 (overlay-start new-overlay) 'face)
2011 'flyspell-incorrect))))
2012 (setq new-overlay (car-safe overlay-list))
2013 (setq overlay-list (cdr-safe overlay-list)))
2015 ;; if nothing new exits new-overlay should be nil
2016 (if new-overlay ;; the length of the word may change so go to the start
2017 (setq flyspell-auto-correct-previous-pos
2018 (overlay-start new-overlay)))))
2020 (when flyspell-auto-correct-previous-pos
2022 (goto-char flyspell-auto-correct-previous-pos)
2023 (let ((ispell-following-word t)) ;; point is at start
2024 (if (numberp flyspell-auto-correct-previous-pos)
2025 (goto-char flyspell-auto-correct-previous-pos))
2026 (flyspell-auto-correct-word))
2027 ;; the point may have moved so reset this
2028 (setq flyspell-auto-correct-previous-pos (point))))))))
2030 ;;*---------------------------------------------------------------------*/
2031 ;;* flyspell-correct-word ... */
2032 ;;*---------------------------------------------------------------------*/
2034 (defun flyspell-correct-word (event)
2035 "Pop up a menu of possible corrections for a misspelled word.
2036 The word checked is the word at the mouse position."
2038 (let ((save (point)))
2039 (mouse-set-point event)
2040 (flyspell-correct-word-before-point event save)))
2042 (defun flyspell-correct-word-before-point (&optional event opoint)
2043 "Pop up a menu of possible corrections for misspelled word before point.
2044 If EVENT is non-nil, it is the mouse event that invoked this operation;
2045 that controls where to put the menu.
2046 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2048 (unless (mouse-position)
2049 (error "Pop-up menus do not work on this terminal"))
2050 ;; use the correct dictionary
2051 (flyspell-accept-buffer-local-defs)
2052 (or opoint (setq opoint (point-marker)))
2053 (let ((cursor-location (point))
2054 (word (flyspell-get-word nil)))
2056 (let ((start (car (cdr word)))
2057 (end (car (cdr (cdr word))))
2060 ;; now check spelling of word.
2061 (ispell-send-string "%\n") ;put in verbose mode
2062 (ispell-send-string (concat "^" word "\n"))
2063 ;; wait until ispell has processed word
2065 (accept-process-output ispell-process)
2066 (not (string= "" (car ispell-filter)))))
2067 ;; Remove leading empty element
2068 (setq ispell-filter (cdr ispell-filter))
2069 ;; ispell process should return something after word is sent.
2070 ;; Tag word as valid (i.e., skip) otherwise
2072 (setq ispell-filter '(*)))
2073 (if (consp ispell-filter)
2074 (setq poss (ispell-parse-output (car ispell-filter))))
2076 ((or (eq poss t) (stringp poss))
2077 ;; don't correct word
2081 (error "Ispell: error in Ispell process"))
2083 (flyspell-xemacs-popup
2084 poss word cursor-location start end opoint))
2086 ;; The word is incorrect, we have to propose a replacement.
2087 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2088 poss word cursor-location start end opoint)))
2089 (ispell-pdict-save t)))))
2091 ;;*---------------------------------------------------------------------*/
2092 ;;* flyspell-do-correct ... */
2093 ;;*---------------------------------------------------------------------*/
2094 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2095 "The popup menu callback."
2096 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2097 ;; it instead right after calling the function.
2098 (cond ((eq replace 'ignore)
2103 (ispell-send-string (concat "*" word "\n"))
2104 ;; This was added only to the XEmacs side in revision 1.18 of
2105 ;; flyspell. I assume its absence on the Emacs side was an
2106 ;; oversight. --Stef
2107 (ispell-send-string "#\n")
2108 (flyspell-unhighlight-at cursor-location)
2109 (setq ispell-pdict-modified-p '(t)))
2110 ((or (eq replace 'buffer) (eq replace 'session))
2111 (ispell-send-string (concat "@" word "\n"))
2112 (flyspell-unhighlight-at cursor-location)
2113 (if (null ispell-pdict-modified-p)
2114 (setq ispell-pdict-modified-p
2115 (list ispell-pdict-modified-p)))
2117 (if (eq replace 'buffer)
2118 (ispell-add-per-file-word-list word)))
2120 ;; This was added only to the Emacs side. I assume its absence on
2121 ;; the XEmacs side was an oversight. --Stef
2122 (flyspell-unhighlight-at cursor-location)
2123 (let ((old-max (point-max))
2124 (new-word (if (atom replace)
2127 (cursor-location (+ (- (length word) (- end start))
2129 (unless (equal new-word (car poss))
2130 (delete-region start end)
2132 (funcall flyspell-insert-function new-word)
2133 (if flyspell-abbrev-p
2134 (flyspell-define-abbrev word new-word)))
2135 ;; In the original Emacs code, this was only called in the body
2136 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2137 (flyspell-ajust-cursor-point save cursor-location old-max)))
2142 ;;*---------------------------------------------------------------------*/
2143 ;;* flyspell-ajust-cursor-point ... */
2144 ;;*---------------------------------------------------------------------*/
2145 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2146 (if (>= save cursor-location)
2147 (let ((new-pos (+ save (- (point-max) old-max))))
2149 ((< new-pos (point-min))
2151 ((> new-pos (point-max))
2156 ;;*---------------------------------------------------------------------*/
2157 ;;* flyspell-emacs-popup ... */
2158 ;;*---------------------------------------------------------------------*/
2159 (defun flyspell-emacs-popup (event poss word)
2160 "The Emacs popup menu."
2161 (unless window-system
2162 (error "This command requires pop-up dialogs"))
2164 (let* ((mouse-pos (mouse-position))
2165 (mouse-pos (if (nth 1 mouse-pos)
2167 (set-mouse-position (car mouse-pos)
2168 (/ (frame-width) 2) 2)
2170 (setq event (list (list (car (cdr mouse-pos))
2171 (1+ (cdr (cdr mouse-pos))))
2173 (let* ((corrects (if flyspell-sort-corrections
2174 (sort (car (cdr (cdr poss))) 'string<)
2175 (car (cdr (cdr poss)))))
2176 (cor-menu (if (consp corrects)
2177 (mapcar (lambda (correct)
2178 (list correct correct))
2181 (affix (car (cdr (cdr (cdr poss)))))
2183 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2185 (list (concat "Save affix: " (car affix))
2187 '("Accept (session)" session)
2188 '("Accept (buffer)" buffer))
2189 '(("Save word" save)
2190 ("Accept (session)" session)
2191 ("Accept (buffer)" buffer)))))
2192 (if (consp cor-menu)
2193 (append cor-menu (cons "" save))
2195 (menu (cons "flyspell correction menu" base-menu)))
2196 (car (x-popup-menu event
2197 (list (format "%s [%s]" word (or ispell-local-dictionary
2201 ;;*---------------------------------------------------------------------*/
2202 ;;* flyspell-xemacs-popup ... */
2203 ;;*---------------------------------------------------------------------*/
2204 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2205 "The XEmacs popup menu."
2206 (let* ((corrects (if flyspell-sort-corrections
2207 (sort (car (cdr (cdr poss))) 'string<)
2208 (car (cdr (cdr poss)))))
2209 (cor-menu (if (consp corrects)
2210 (mapcar (lambda (correct)
2212 (list 'flyspell-do-correct
2223 (affix (car (cdr (cdr (cdr poss)))))
2225 (menu (let ((save (if (and (consp affix) show-affix-info)
2227 (concat "Save affix: " (car affix))
2228 (list 'flyspell-do-correct
2239 (list 'flyspell-do-correct
2248 (session (vector "Accept (session)"
2249 (list 'flyspell-do-correct
2258 (buffer (vector "Accept (buffer)"
2259 (list 'flyspell-do-correct
2268 (if (consp cor-menu)
2269 (append cor-menu (list "-" save session buffer))
2270 (list save session buffer)))))
2271 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2275 ;;*---------------------------------------------------------------------*/
2276 ;;* Some example functions for real autocorrecting */
2277 ;;*---------------------------------------------------------------------*/
2278 (defun flyspell-maybe-correct-transposition (beg end poss)
2279 "Check replacements for transposed characters.
2281 If the text between BEG and END is equal to a correction suggested by
2282 Ispell, after transposing two adjacent characters, correct the text,
2285 The third arg POSS is either the symbol 'doublon' or a list of
2286 possible corrections as returned by `ispell-parse-output'.
2288 This function is meant to be added to `flyspell-incorrect-hook'."
2291 (let ((str (buffer-substring beg end))
2292 (i 0) (len (- end beg)) tmp)
2293 (while (< (1+ i) len)
2294 (setq tmp (aref str i))
2295 (aset str i (aref str (1+ i)))
2296 (aset str (1+ i) tmp)
2297 (when (member str (nth 2 poss))
2299 (goto-char (+ beg i 1))
2300 (transpose-chars 1))
2302 (setq tmp (aref str i))
2303 (aset str i (aref str (1+ i)))
2304 (aset str (1+ i) tmp)
2308 (defun flyspell-maybe-correct-doubling (beg end poss)
2309 "Check replacements for doubled characters.
2311 If the text between BEG and END is equal to a correction suggested by
2312 Ispell, after removing a pair of doubled characters, correct the text,
2315 The third arg POSS is either the symbol 'doublon' or a list of
2316 possible corrections as returned by `ispell-parse-output'.
2318 This function is meant to be added to `flyspell-incorrect-hook'."
2321 (let ((str (buffer-substring beg end))
2322 (i 0) (len (- end beg)))
2323 (while (< (1+ i) len)
2324 (when (and (= (aref str i) (aref str (1+ i)))
2325 (member (concat (substring str 0 (1+ i))
2326 (substring str (+ i 2)))
2328 (goto-char (+ beg i))
2334 ;;*---------------------------------------------------------------------*/
2335 ;;* flyspell-already-abbrevp ... */
2336 ;;*---------------------------------------------------------------------*/
2337 (defun flyspell-already-abbrevp (table word)
2338 (let ((sym (abbrev-symbol word table)))
2339 (and sym (symbolp sym))))
2341 ;;*---------------------------------------------------------------------*/
2342 ;;* flyspell-change-abbrev ... */
2343 ;;*---------------------------------------------------------------------*/
2344 (defun flyspell-change-abbrev (table old new)
2345 (set (abbrev-symbol old table) new))
2349 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2350 ;;; flyspell.el ends here