1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2002, 2003, 2004,
4 ;; 2005 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 2, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
32 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
33 ;; This applies only to the current buffer.
35 ;; To enable Flyspell in text representing computer programs, type
36 ;; M-x flyspell-prog-mode.
37 ;; In that mode only text inside comments is checked.
39 ;; Note: consider setting the variable ispell-parser to `tex' to
40 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
42 ;; Some user variables control the behavior of flyspell. They are
43 ;; those defined under the `User variables' comment.
49 ;;*---------------------------------------------------------------------*/
51 ;;*---------------------------------------------------------------------*/
52 (defgroup flyspell nil
53 "Spell checking on the fly."
59 ;;*---------------------------------------------------------------------*/
60 ;;* User configuration ... */
61 ;;*---------------------------------------------------------------------*/
62 (defcustom flyspell-highlight-flag t
63 "How Flyspell should indicate misspelled words.
64 Non-nil means use highlight, nil means use minibuffer messages."
68 (defcustom flyspell-mark-duplications-flag t
69 "Non-nil means Flyspell reports a repeated word as an error.
70 Detection of repeated words is not implemented in
71 \"large\" regions; see `flyspell-large-region'."
75 (defcustom flyspell-sort-corrections nil
76 "Non-nil means, sort the corrections alphabetically before popping them."
81 (defcustom flyspell-duplicate-distance -
1
82 "The maximum distance for finding duplicates of unrecognized words.
83 This applies to the feature that when a word is not found in the dictionary,
84 if the same spelling occurs elsewhere in the buffer,
85 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
86 This variable specifies how far to search to find such a duplicate.
87 -1 means no limit (search the whole buffer).
88 0 means do not search for duplicate unrecognized spellings."
93 (defcustom flyspell-delay
3
94 "The number of seconds to wait before checking, after a \"delayed\" command."
98 (defcustom flyspell-persistent-highlight t
99 "Non-nil means misspelled words remain highlighted until corrected.
100 If this variable is nil, only the most recently detected misspelled word
105 (defcustom flyspell-highlight-properties t
106 "Non-nil means highlight incorrect words even if a property exists for this word."
110 (defcustom flyspell-default-delayed-commands
111 '(self-insert-command
113 backward-or-forward-delete-char
115 scrollbar-vertical-drag
116 backward-delete-char-untabify
)
117 "The standard list of delayed commands for Flyspell.
118 See `flyspell-delayed-commands'."
121 :type
'(repeat (symbol)))
123 (defcustom flyspell-delayed-commands nil
124 "List of commands that are \"delayed\" for Flyspell mode.
125 After these commands, Flyspell checking is delayed for a short time,
126 whose length is specified by `flyspell-delay'."
128 :type
'(repeat (symbol)))
130 (defcustom flyspell-default-deplacement-commands
135 "The standard list of deplacement commands for Flyspell.
136 See `flyspell-deplacement-commands'."
139 :type
'(repeat (symbol)))
141 (defcustom flyspell-deplacement-commands nil
142 "List of commands that are \"deplacement\" for Flyspell mode.
143 After these commands, Flyspell checking is performed only if the previous
144 command was not the very same command."
147 :type
'(repeat (symbol)))
149 (defcustom flyspell-issue-welcome-flag t
150 "Non-nil means that Flyspell should display a welcome message when started."
154 (defcustom flyspell-issue-message-flag t
155 "Non-nil means that Flyspell emits messages when checking words."
159 (defcustom flyspell-incorrect-hook nil
160 "List of functions to be called when incorrect words are encountered.
161 Each function is given three arguments. The first two
162 arguments are the beginning and the end of the incorrect region.
163 The third is either the symbol `doublon' or the list
164 of possible corrections as returned by `ispell-parse-output'.
166 If any of the functions return non-nil, the word is not highlighted as
172 (defcustom flyspell-default-dictionary nil
173 "A string that is the name of the default dictionary.
174 This is passed to the `ispell-change-dictionary' when flyspell is started.
175 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
176 when flyspell is started, the value of that variable is used instead
177 of `flyspell-default-dictionary' to select the default dictionary.
178 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
179 Ispell's ultimate default dictionary."
182 :type
'(choice string
(const :tag
"Default" nil
)))
184 (defcustom flyspell-tex-command-regexp
185 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
186 "A string that is the regular expression that matches TeX commands."
191 (defcustom flyspell-check-tex-math-command nil
192 "Non nil means check even inside TeX math environment.
193 TeX math environments are discovered by the TEXMATHP that implemented
194 inside the texmathp.el Emacs package. That package may be found at:
195 http://strw.leidenuniv.nl/~dominik/Tools"
199 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
200 '("francais" "deutsch8" "norsk")
201 "List of dictionary names that consider `-' as word delimiter."
204 :type
'(repeat (string)))
206 (defcustom flyspell-abbrev-p
208 "If non-nil, add correction to abbreviation table."
213 (defcustom flyspell-use-global-abbrev-table-p
215 "If non-nil, prefer global abbrev table to local abbrev table."
220 (defcustom flyspell-mode-line-string
" Fly"
221 "String displayed on the modeline when flyspell is active.
222 Set this to nil if you don't want a modeline indicator."
224 :type
'(choice string
(const :tag
"None" nil
)))
226 (defcustom flyspell-large-region
1000
227 "The threshold that determines if a region is small.
228 If the region is smaller than this number of characters,
229 `flyspell-region' checks the words sequentially using regular
230 flyspell methods. Else, if the region is large, a new Ispell process is
233 Doubled words are not detected in a large region, because Ispell
234 does not check for them.
236 If `flyspell-large-region' is nil, all regions are treated as small."
239 :type
'(choice number
(const :tag
"All small" nil
)))
241 (defcustom flyspell-insert-function
(function insert
)
242 "Function for inserting word by flyspell upon correction."
246 (defcustom flyspell-before-incorrect-word-string nil
247 "String used to indicate an incorrect word starting."
249 :type
'(choice string
(const nil
)))
251 (defcustom flyspell-after-incorrect-word-string nil
252 "String used to indicate an incorrect word ending."
254 :type
'(choice string
(const nil
)))
256 (defcustom flyspell-use-meta-tab t
257 "Non-nil means that flyspell uses M-TAB to correct word."
261 (defcustom flyspell-auto-correct-binding
263 "The key binding for flyspell auto correction."
266 ;;*---------------------------------------------------------------------*/
267 ;;* Mode specific options */
268 ;;* ------------------------------------------------------------- */
269 ;;* Mode specific options enable users to disable flyspell on */
270 ;;* certain word depending of the emacs mode. For instance, when */
271 ;;* using flyspell with mail-mode add the following expression */
272 ;;* in your .emacs file: */
273 ;;* (add-hook 'mail-mode */
274 ;;* '(lambda () (setq flyspell-generic-check-word-p */
275 ;;* 'mail-mode-flyspell-verify))) */
276 ;;*---------------------------------------------------------------------*/
277 (defvar flyspell-generic-check-word-p nil
278 "Function providing per-mode customization over which words are flyspelled.
279 Returns t to continue checking, nil otherwise.
280 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
281 property of the major mode name.")
282 (make-variable-buffer-local 'flyspell-generic-check-word-p
)
284 ;;*--- mail mode -------------------------------------------------------*/
285 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
286 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
287 (defun mail-mode-flyspell-verify ()
288 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
289 (let ((header-end (save-excursion
290 (goto-char (point-min))
293 (regexp-quote mail-header-separator
)
297 (signature-begin (save-excursion
298 (goto-char (point-max))
299 (re-search-backward message-signature-separator
302 (cond ((< (point) header-end
)
303 (and (save-excursion (beginning-of-line)
304 (looking-at "^Subject:"))
305 (> (point) (match-end 0))))
306 ((> (point) signature-begin
)
311 (not (looking-at "[>}|]\\|To:")))))))
313 ;;*--- texinfo mode ----------------------------------------------------*/
314 (put 'texinfo-mode
'flyspell-mode-predicate
'texinfo-mode-flyspell-verify
)
315 (defun texinfo-mode-flyspell-verify ()
316 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
319 (not (looking-at "@"))))
321 ;;*--- tex mode --------------------------------------------------------*/
322 (put 'tex-mode
'flyspell-mode-predicate
'tex-mode-flyspell-verify
)
323 (defun tex-mode-flyspell-verify ()
324 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
327 (re-search-backward "^[ \t]*%%%[ \t]+Local" (point-min) t
)))
329 (let ((this (point-marker))
330 (e (progn (end-of-line) (point-marker))))
332 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t
)
333 (and (>= this
(match-beginning 0))
334 (<= this
(match-end 0)) )))))))
336 ;;*--- sgml mode -------------------------------------------------------*/
337 (put 'sgml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
338 (put 'html-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
340 (defun sgml-mode-flyspell-verify ()
341 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
343 (let ((this (point-marker))
344 (s (progn (beginning-of-line) (point-marker)))
345 (e (progn (end-of-line) (point-marker))))
348 (and (re-search-forward "[^<]*>" e t
)
349 (= (match-beginning 0) this
)))
352 (and (re-search-backward "<[^>]*" s t
)
353 (= (match-end 0) this
)))
356 (and (re-search-forward "[^&]*;" e t
)
357 (= (match-beginning 0) this
)))
360 (and (re-search-backward "&[^;]*" s t
)
361 (= (match-end 0) this
)))))))))
363 ;;*---------------------------------------------------------------------*/
364 ;;* Programming mode */
365 ;;*---------------------------------------------------------------------*/
366 (defvar flyspell-prog-text-faces
367 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
368 "Faces corresponding to text in programming-mode buffers.")
370 (defun flyspell-generic-progmode-verify ()
371 "Used for `flyspell-generic-check-word-p' in programming modes."
372 (let ((f (get-text-property (point) 'face
)))
373 (memq f flyspell-prog-text-faces
)))
376 (defun flyspell-prog-mode ()
377 "Turn on `flyspell-mode' for comments and strings."
379 (setq flyspell-generic-check-word-p
'flyspell-generic-progmode-verify
)
381 (run-hooks 'flyspell-prog-mode-hook
))
383 ;;*---------------------------------------------------------------------*/
384 ;;* Overlay compatibility */
385 ;;*---------------------------------------------------------------------*/
386 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
387 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
388 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
389 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
390 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
391 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
392 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
393 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
395 ;;*---------------------------------------------------------------------*/
396 ;;* The minor mode declaration. */
397 ;;*---------------------------------------------------------------------*/
398 (defvar flyspell-mouse-map
399 (let ((map (make-sparse-keymap)))
400 (define-key map
(if (featurep 'xemacs
) [button2] [down-mouse-2])
401 #'flyspell-correct-word)
403 "Keymap for Flyspell to put on erroneous words.")
405 (defvar flyspell-mode-map
406 (let ((map (make-sparse-keymap)))
407 (if flyspell-use-meta-tab
408 (define-key map "\M-\t" 'flyspell-auto-correct-word))
409 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
410 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
411 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
413 "Minor mode keymap for Flyspell mode--for the whole buffer.")
415 ;; dash character machinery
416 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
417 "*Non-nil means that the `-' char is considered as a word delimiter.")
418 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
419 (defvar flyspell-dash-dictionary nil)
420 (make-variable-buffer-local 'flyspell-dash-dictionary)
421 (defvar flyspell-dash-local-dictionary nil)
422 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
424 ;;*---------------------------------------------------------------------*/
426 ;;*---------------------------------------------------------------------*/
427 (defface flyspell-incorrect
428 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
430 "Face used for marking a misspelled word in Flyspell."
432 ;; backward-compatibility alias
433 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
435 (defface flyspell-duplicate
436 '((((class color)) (:foreground "Gold3" :bold t :underline t))
438 "Face used for marking a misspelled word that appears twice in the buffer.
439 See also `flyspell-duplicate-distance'."
441 ;; backward-compatibility alias
442 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
444 (defvar flyspell-overlay nil)
446 ;;*---------------------------------------------------------------------*/
447 ;;* flyspell-mode ... */
448 ;;*---------------------------------------------------------------------*/
449 ;;;###autoload(defvar flyspell-mode nil)
451 (define-minor-mode flyspell-mode
452 "Minor mode performing on-the-fly spelling checking.
453 This spawns a single Ispell process and checks each word.
454 The default flyspell behavior is to highlight incorrect words.
455 With no argument, this command toggles Flyspell mode.
456 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
459 \\[ispell-word]: correct words (using Ispell).
460 \\[flyspell-auto-correct-word]: automatically correct word.
461 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
462 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
465 This runs `flyspell-mode-hook' after flyspell is entered.
468 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
469 valid. For instance, a personal dictionary can be used by
470 invoking `ispell-change-dictionary'.
472 Consider using the `ispell-parser' to check your text. For instance
474 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
477 \\[flyspell-region] checks all words inside a region.
478 \\[flyspell-buffer] checks the whole buffer."
479 :lighter flyspell-mode-line-string
480 :keymap flyspell-mode-map
484 (flyspell-mode-off)))
486 ;;*---------------------------------------------------------------------*/
487 ;;* flyspell-buffers ... */
488 ;;* ------------------------------------------------------------- */
489 ;;* For remembering buffers running flyspell */
490 ;;*---------------------------------------------------------------------*/
491 (defvar flyspell-buffers nil)
493 ;;*---------------------------------------------------------------------*/
494 ;;* flyspell-minibuffer-p ... */
495 ;;*---------------------------------------------------------------------*/
496 (defun flyspell-minibuffer-p (buffer)
497 "Is BUFFER a minibuffer?"
498 (let ((ws (get-buffer-window-list buffer t)))
499 (and (consp ws) (window-minibuffer-p (car ws)))))
501 ;;*---------------------------------------------------------------------*/
502 ;;* flyspell-accept-buffer-local-defs ... */
503 ;;*---------------------------------------------------------------------*/
504 (defun flyspell-accept-buffer-local-defs ()
505 ;; strange problem. If buffer in current window has font-lock turned on,
506 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
507 ;; call will reset the buffer to the buffer in the current window. However,
508 ;; it only happens at startup (fix by Albert L. Ting).
510 (ispell-accept-buffer-local-defs))
511 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
512 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
513 ;; The dictionary has changed
515 (setq flyspell-dash-dictionary ispell-dictionary)
516 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
517 (if (member (or ispell-local-dictionary ispell-dictionary)
518 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
519 (setq flyspell-consider-dash-as-word-delimiter-flag t)
520 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
522 ;;*---------------------------------------------------------------------*/
523 ;;* flyspell-mode-on ... */
524 ;;*---------------------------------------------------------------------*/
525 (defun flyspell-mode-on ()
526 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
527 (ispell-maybe-find-aspell-dictionaries)
528 (setq ispell-highlight-face 'flyspell-incorrect)
529 ;; local dictionaries setup
530 (or ispell-local-dictionary ispell-dictionary
531 (if flyspell-default-dictionary
532 (ispell-change-dictionary flyspell-default-dictionary)))
533 ;; we have to force ispell to accept the local definition or
534 ;; otherwise it could be too late, the local dictionary may
536 (flyspell-accept-buffer-local-defs)
537 ;; we put the `flyspell-delayed' property on some commands
538 (flyspell-delay-commands)
539 ;; we put the `flyspell-deplacement' property on some commands
540 (flyspell-deplacement-commands)
541 ;; we bound flyspell action to post-command hook
542 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
543 ;; we bound flyspell action to pre-command hook
544 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
545 ;; we bound flyspell action to after-change hook
546 (make-local-variable 'after-change-functions)
547 (setq after-change-functions
548 (cons 'flyspell-after-change-function after-change-functions))
549 ;; set flyspell-generic-check-word-p based on the major mode
550 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
552 (setq flyspell-generic-check-word-p mode-predicate)))
553 ;; the welcome message
554 (if (and flyspell-issue-message-flag
555 flyspell-issue-welcome-flag
557 (let ((binding (where-is-internal 'flyspell-auto-correct-word
561 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
562 (key-description binding))
563 "Welcome to flyspell. Use Mouse-2 to correct words."))))
564 ;; we end with the flyspell hooks
565 (run-hooks 'flyspell-mode-hook))
567 ;;*---------------------------------------------------------------------*/
568 ;;* flyspell-delay-commands ... */
569 ;;*---------------------------------------------------------------------*/
570 (defun flyspell-delay-commands ()
571 "Install the standard set of Flyspell delayed commands."
572 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
573 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
575 ;;*---------------------------------------------------------------------*/
576 ;;* flyspell-delay-command ... */
577 ;;*---------------------------------------------------------------------*/
578 (defun flyspell-delay-command (command)
579 "Set COMMAND to be delayed, for Flyspell.
580 When flyspell `post-command-hook' is invoked because a delayed command
581 as been used the current word is not immediately checked.
582 It will be checked only after `flyspell-delay' seconds."
583 (interactive "SDelay Flyspell after Command: ")
584 (put command 'flyspell-delayed t))
586 ;;*---------------------------------------------------------------------*/
587 ;;* flyspell-deplacement-commands ... */
588 ;;*---------------------------------------------------------------------*/
589 (defun flyspell-deplacement-commands ()
590 "Install the standard set of Flyspell deplacement commands."
591 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
592 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
594 ;;*---------------------------------------------------------------------*/
595 ;;* flyspell-deplacement-command ... */
596 ;;*---------------------------------------------------------------------*/
597 (defun flyspell-deplacement-command (command)
598 "Set COMMAND that implement cursor movements, for Flyspell.
599 When flyspell `post-command-hook' is invoked because of a deplacement command
600 as been used the current word is checked only if the previous command was
601 not the very same deplacement command."
602 (interactive "SDeplacement Flyspell after Command: ")
603 (put command 'flyspell-deplacement t))
605 ;;*---------------------------------------------------------------------*/
606 ;;* flyspell-word-cache ... */
607 ;;*---------------------------------------------------------------------*/
608 (defvar flyspell-word-cache-start nil)
609 (defvar flyspell-word-cache-end nil)
610 (defvar flyspell-word-cache-word nil)
611 (defvar flyspell-word-cache-result '_)
612 (make-variable-buffer-local 'flyspell-word-cache-start)
613 (make-variable-buffer-local 'flyspell-word-cache-end)
614 (make-variable-buffer-local 'flyspell-word-cache-word)
615 (make-variable-buffer-local 'flyspell-word-cache-result)
617 ;;*---------------------------------------------------------------------*/
618 ;;* The flyspell pre-hook, store the current position. In the */
619 ;;* post command hook, we will check, if the word at this position */
620 ;;* has to be spell checked. */
621 ;;*---------------------------------------------------------------------*/
622 (defvar flyspell-pre-buffer nil)
623 (defvar flyspell-pre-point nil)
624 (defvar flyspell-pre-column nil)
625 (defvar flyspell-pre-pre-buffer nil)
626 (defvar flyspell-pre-pre-point nil)
628 ;;*---------------------------------------------------------------------*/
629 ;;* flyspell-previous-command ... */
630 ;;*---------------------------------------------------------------------*/
631 (defvar flyspell-previous-command nil
632 "The last interactive command checked by Flyspell.")
634 ;;*---------------------------------------------------------------------*/
635 ;;* flyspell-pre-command-hook ... */
636 ;;*---------------------------------------------------------------------*/
637 (defun flyspell-pre-command-hook ()
638 "Save the current buffer and point for Flyspell's post-command hook."
640 (setq flyspell-pre-buffer (current-buffer))
641 (setq flyspell-pre-point (point))
642 (setq flyspell-pre-column (current-column)))
644 ;;*---------------------------------------------------------------------*/
645 ;;* flyspell-mode-off ... */
646 ;;*---------------------------------------------------------------------*/
648 (defun flyspell-mode-off ()
649 "Turn Flyspell mode off."
650 ;; we remove the hooks
651 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
652 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
653 (setq after-change-functions (delq 'flyspell-after-change-function
654 after-change-functions))
655 ;; we remove all the flyspell hilightings
656 (flyspell-delete-all-overlays)
657 ;; we have to erase pre cache variables
658 (setq flyspell-pre-buffer nil)
659 (setq flyspell-pre-point nil)
660 ;; we mark the mode as killed
661 (setq flyspell-mode nil))
663 ;;*---------------------------------------------------------------------*/
664 ;;* flyspell-check-pre-word-p ... */
665 ;;*---------------------------------------------------------------------*/
666 (defun flyspell-check-pre-word-p ()
667 "Return non-nil if we should check the word before point.
668 More precisely, it applies to the word that was before point
669 before the current command."
671 ((or (not (numberp flyspell-pre-point))
672 (not (bufferp flyspell-pre-buffer))
673 (not (buffer-live-p flyspell-pre-buffer)))
675 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
676 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
678 ((or (and (= flyspell-pre-point (- (point) 1))
679 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
680 (= flyspell-pre-point (point))
681 (= flyspell-pre-point (+ (point) 1)))
683 ((and (symbolp this-command)
684 (not executing-kbd-macro)
685 (or (get this-command 'flyspell-delayed)
686 (and (get this-command 'flyspell-deplacement)
687 (eq flyspell-previous-command this-command)))
688 (or (= (current-column) 0)
689 (= (current-column) flyspell-pre-column)
690 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
692 ((not (eq (current-buffer) flyspell-pre-buffer))
694 ((not (and (numberp flyspell-word-cache-start)
695 (numberp flyspell-word-cache-end)))
698 (or (< flyspell-pre-point flyspell-word-cache-start)
699 (> flyspell-pre-point flyspell-word-cache-end)))))
701 ;;*---------------------------------------------------------------------*/
702 ;;* The flyspell after-change-hook, store the change position. In */
703 ;;* the post command hook, we will check, if the word at this */
704 ;;* position has to be spell checked. */
705 ;;*---------------------------------------------------------------------*/
706 (defvar flyspell-changes nil)
708 ;;*---------------------------------------------------------------------*/
709 ;;* flyspell-after-change-function ... */
710 ;;*---------------------------------------------------------------------*/
711 (defun flyspell-after-change-function (start stop len)
712 "Save the current buffer and point for Flyspell's post-command hook."
714 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
716 ;;*---------------------------------------------------------------------*/
717 ;;* flyspell-check-changed-word-p ... */
718 ;;*---------------------------------------------------------------------*/
719 (defun flyspell-check-changed-word-p (start stop)
720 "Return t when the changed word has to be checked.
721 The answer depends of several criteria.
722 Mostly we check word delimiters."
724 ((and (memq (char-after start) '(?\n ? )) (> stop start))
726 ((not (numberp flyspell-pre-point))
728 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
730 ((let ((pos (point)))
731 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
736 ;;*---------------------------------------------------------------------*/
737 ;;* flyspell-check-word-p ... */
738 ;;*---------------------------------------------------------------------*/
739 (defun flyspell-check-word-p ()
740 "Return t when the word at `point' has to be checked.
741 The answer depends of several criteria.
742 Mostly we check word delimiters."
744 ((<= (- (point-max) 1) (point-min))
745 ;; the buffer is not filled enough
747 ((and (and (> (current-column) 0)
748 (not (eq (current-column) flyspell-pre-column)))
751 (and (looking-at (flyspell-get-not-casechars))
752 (or flyspell-consider-dash-as-word-delimiter-flag
753 (not (looking-at "\\-"))))))
754 ;; yes because we have reached or typed a word delimiter.
756 ((symbolp this-command)
758 ((get this-command 'flyspell-deplacement)
759 (not (eq flyspell-previous-command this-command)))
760 ((get this-command 'flyspell-delayed)
761 ;; the current command is not delayed, that
762 ;; is that we must check the word now
763 (and (not unread-command-events)
764 (sit-for flyspell-delay)))
768 ;;*---------------------------------------------------------------------*/
769 ;;* flyspell-debug-signal-no-check ... */
770 ;;*---------------------------------------------------------------------*/
771 (defun flyspell-debug-signal-no-check (msg obj)
772 (setq debug-on-error t)
773 (with-current-buffer (get-buffer-create "*flyspell-debug*")
775 (insert "NO-CHECK:\n")
776 (insert (format " %S : %S\n" msg obj))))
778 ;;*---------------------------------------------------------------------*/
779 ;;* flyspell-debug-signal-pre-word-checked ... */
780 ;;*---------------------------------------------------------------------*/
781 (defun flyspell-debug-signal-pre-word-checked ()
782 (setq debug-on-error t)
783 (with-current-buffer (get-buffer-create "*flyspell-debug*")
784 (insert "PRE-WORD:\n")
785 (insert (format " pre-point : %S\n" flyspell-pre-point))
786 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
787 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
788 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
789 (goto-char (point-max))))
791 ;;*---------------------------------------------------------------------*/
792 ;;* flyspell-debug-signal-word-checked ... */
793 ;;*---------------------------------------------------------------------*/
794 (defun flyspell-debug-signal-word-checked ()
795 (setq debug-on-error t)
796 (let ((oldbuf (current-buffer))
798 (with-current-buffer (get-buffer-create "*flyspell-debug*")
800 (insert (format " this-cmd : %S\n" this-command))
801 (insert (format " delayed : %S\n" (and (symbolp this-command)
802 (get this-command 'flyspell-delayed))))
803 (insert (format " point : %S\n" point))
804 (insert (format " prev-char : [%c] %S\n"
805 (with-current-buffer oldbuf
806 (let ((c (if (> (point) (point-min))
809 (char-after (point)))
812 (with-current-buffer oldbuf
813 (let ((c (if (> (point) (point-min))
816 (and (and (looking-at (flyspell-get-not-casechars)) 1)
817 (and (or flyspell-consider-dash-as-word-delimiter-flag
818 (not (looking-at "\\-"))) 2))))))
820 (insert (format " because : %S\n"
822 ((not (and (symbolp this-command)
823 (get this-command 'flyspell-delayed)))
824 ;; the current command is not delayed, that
825 ;; is that we must check the word now
827 ((with-current-buffer oldbuf
828 (let ((c (if (> (point) (point-min))
831 (and (looking-at (flyspell-get-not-casechars))
832 (or flyspell-consider-dash-as-word-delimiter-flag
833 (not (looking-at "\\-"))))))))
835 ;; yes because we have reached or typed a word delimiter.
837 ((not (integerp flyspell-delay))
838 ;; yes because the user had set up a no-delay configuration.
842 (goto-char (point-max)))))
844 ;;*---------------------------------------------------------------------*/
845 ;;* flyspell-debug-signal-changed-checked ... */
846 ;;*---------------------------------------------------------------------*/
847 (defun flyspell-debug-signal-changed-checked ()
848 (setq debug-on-error t)
849 (let ((point (point)))
850 (with-current-buffer (get-buffer-create "*flyspell-debug*")
851 (insert "CHANGED WORD:\n")
852 (insert (format " point : %S\n" point))
853 (goto-char (point-max)))))
855 ;;*---------------------------------------------------------------------*/
856 ;;* flyspell-post-command-hook ... */
857 ;;* ------------------------------------------------------------- */
858 ;;* It is possible that we check several words: */
859 ;;* 1- the current word is checked if the predicate */
860 ;;* FLYSPELL-CHECK-WORD-P is true */
861 ;;* 2- the word that used to be the current word before the */
862 ;;* THIS-COMMAND is checked if: */
863 ;;* a- the previous word is different from the current word */
864 ;;* b- the previous word as not just been checked by the */
865 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
866 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
867 ;;* previous word nor the current word */
868 ;;*---------------------------------------------------------------------*/
869 (defun flyspell-post-command-hook ()
870 "The `post-command-hook' used by flyspell to check a word in-the-fly."
872 (let ((command this-command)
873 ;; Prevent anything we do from affecting the mark.
875 (if (flyspell-check-pre-word-p)
876 (with-current-buffer flyspell-pre-buffer
877 '(flyspell-debug-signal-pre-word-checked)
879 (goto-char flyspell-pre-point)
881 (if (flyspell-check-word-p)
883 '(flyspell-debug-signal-word-checked)
885 ;; we remember which word we have just checked.
886 ;; this will be used next time we will check a word
887 ;; to compare the next current word with the word
888 ;; that as been registered in the pre-command-hook
889 ;; that is these variables are used within the predicate
890 ;; FLYSPELL-CHECK-PRE-WORD-P
891 (setq flyspell-pre-pre-buffer (current-buffer))
892 (setq flyspell-pre-pre-point (point)))
894 (setq flyspell-pre-pre-buffer nil)
895 (setq flyspell-pre-pre-point nil)
896 ;; when a word is not checked because of a delayed command
897 ;; we do not disable the ispell cache.
898 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
900 (setq flyspell-word-cache-end -1)
901 (setq flyspell-word-cache-result '_)))))
902 (while (consp flyspell-changes)
903 (let ((start (car (car flyspell-changes)))
904 (stop (cdr (car flyspell-changes))))
905 (if (flyspell-check-changed-word-p start stop)
907 '(flyspell-debug-signal-changed-checked)
910 (setq flyspell-changes (cdr flyspell-changes))))
911 (setq flyspell-previous-command command)))
913 ;;*---------------------------------------------------------------------*/
914 ;;* flyspell-notify-misspell ... */
915 ;;*---------------------------------------------------------------------*/
916 (defun flyspell-notify-misspell (word poss)
917 (let ((replacements (if (stringp poss)
919 (if flyspell-sort-corrections
920 (sort (car (cdr (cdr poss))) 'string<)
921 (car (cdr (cdr poss)))))))
922 (if flyspell-issue-message-flag
923 (message "misspelling `%s' %S" word replacements))))
925 ;;*---------------------------------------------------------------------*/
926 ;;* flyspell-word-search-backward ... */
927 ;;*---------------------------------------------------------------------*/
928 (defun flyspell-word-search-backward (word bound)
932 (while (and (not r) (setq p (search-backward word bound t)))
933 (let ((lw (flyspell-get-word '())))
934 (if (and (consp lw) (string-equal (car lw) word))
939 ;;*---------------------------------------------------------------------*/
940 ;;* flyspell-word-search-forward ... */
941 ;;*---------------------------------------------------------------------*/
942 (defun flyspell-word-search-forward (word bound)
946 (while (and (not r) (setq p (search-forward word bound t)))
947 (let ((lw (flyspell-get-word '())))
948 (if (and (consp lw) (string-equal (car lw) word))
950 (goto-char (1+ p)))))
953 ;;*---------------------------------------------------------------------*/
954 ;;* flyspell-word ... */
955 ;;*---------------------------------------------------------------------*/
956 (defun flyspell-word (&optional following)
957 "Spell check a word."
958 (interactive (list ispell-following-word))
960 ;; use the correct dictionary
961 (flyspell-accept-buffer-local-defs)
962 (let* ((cursor-location (point))
963 (flyspell-word (flyspell-get-word following))
965 (if (or (eq flyspell-word nil)
966 (and (fboundp flyspell-generic-check-word-p)
967 (not (funcall flyspell-generic-check-word-p))))
970 ;; destructure return flyspell-word info list.
971 (setq start (car (cdr flyspell-word))
972 end (car (cdr (cdr flyspell-word)))
973 word (car flyspell-word))
974 ;; before checking in the directory, we check for doublons.
976 ((and (or (not (eq ispell-parser 'tex))
977 (and (> start (point-min))
978 (not (memq (char-after (1- start)) '(?\} ?\\)))))
979 flyspell-mark-duplications-flag
981 (goto-char (1- start))
982 (let ((p (flyspell-word-search-backward
984 (- start (1+ (- end start))))))
985 (and p (/= p (1- start))))))
986 ;; yes, this is a doublon
987 (flyspell-highlight-incorrect-region start end 'doublon)
989 ((and (eq flyspell-word-cache-start start)
990 (eq flyspell-word-cache-end end)
991 (string-equal flyspell-word-cache-word word))
992 ;; this word had been already checked, we skip
993 flyspell-word-cache-result)
994 ((and (eq ispell-parser 'tex)
995 (flyspell-tex-command-p flyspell-word))
996 ;; this is a correct word (because a tex command)
997 (flyspell-unhighlight-at start)
999 (flyspell-unhighlight-at (- end 1)))
1002 ;; we setup the cache
1003 (setq flyspell-word-cache-start start)
1004 (setq flyspell-word-cache-end end)
1005 (setq flyspell-word-cache-word word)
1006 ;; now check spelling of word.
1007 (ispell-send-string "%\n")
1008 ;; put in verbose mode
1009 (ispell-send-string (concat "^" word "\n"))
1010 ;; we mark the ispell process so it can be killed
1011 ;; when emacs is exited without query
1012 (set-process-query-on-exit-flag ispell-process nil)
1013 ;; Wait until ispell has processed word. Since this code is often
1014 ;; executed rom post-command-hook but the ispell process may not
1015 ;; be responsive, it's important to make sure we re-enable C-g.
1018 (accept-process-output ispell-process)
1019 (not (string= "" (car ispell-filter))))))
1020 ;; (ispell-send-string "!\n")
1021 ;; back to terse mode.
1022 (setq ispell-filter (cdr ispell-filter))
1023 (if (consp ispell-filter)
1024 (setq poss (ispell-parse-output (car ispell-filter))))
1025 (let ((res (cond ((eq poss t)
1027 (setq flyspell-word-cache-result t)
1028 (flyspell-unhighlight-at start)
1030 (flyspell-unhighlight-at (- end 1)))
1032 ((and (stringp poss) flyspell-highlight-flag)
1034 (setq flyspell-word-cache-result t)
1035 (flyspell-unhighlight-at start)
1037 (flyspell-unhighlight-at (- end 1)))
1040 (setq flyspell-word-cache-result t)
1041 (flyspell-unhighlight-at start)
1043 (flyspell-unhighlight-at (- end 1)))
1045 ((or (and (< flyspell-duplicate-distance 0)
1048 (flyspell-word-search-backward
1053 (flyspell-word-search-forward
1056 (and (> flyspell-duplicate-distance 0)
1059 (flyspell-word-search-backward
1062 flyspell-duplicate-distance)))
1065 (flyspell-word-search-forward
1068 flyspell-duplicate-distance))))))
1069 ;; This is a misspelled word which occurs
1070 ;; twice within flyspell-duplicate-distance.
1071 (setq flyspell-word-cache-result nil)
1072 (if flyspell-highlight-flag
1073 (flyspell-highlight-duplicate-region
1075 (message "duplicate `%s'" word))
1078 (setq flyspell-word-cache-result nil)
1079 ;; incorrect highlight the location
1080 (if flyspell-highlight-flag
1081 (flyspell-highlight-incorrect-region
1083 (flyspell-notify-misspell word poss))
1085 ;; return to original location
1086 (goto-char cursor-location)
1087 (if ispell-quit (setq ispell-quit nil))
1090 ;;*---------------------------------------------------------------------*/
1091 ;;* flyspell-tex-math-initialized ... */
1092 ;;*---------------------------------------------------------------------*/
1093 (defvar flyspell-tex-math-initialized nil)
1095 ;;*---------------------------------------------------------------------*/
1096 ;;* flyspell-math-tex-command-p ... */
1097 ;;* ------------------------------------------------------------- */
1098 ;;* This function uses the texmathp package to check if (point) */
1099 ;;* is within a tex command. In order to avoid using */
1100 ;;* condition-case each time we use the variable */
1101 ;;* flyspell-tex-math-initialized to make a special case the first */
1102 ;;* time that function is called. */
1103 ;;*---------------------------------------------------------------------*/
1104 (defun flyspell-math-tex-command-p ()
1105 (when (fboundp 'texmathp)
1107 (flyspell-check-tex-math-command
1109 ((eq flyspell-tex-math-initialized t)
1111 ((eq flyspell-tex-math-initialized 'error)
1114 (setq flyspell-tex-math-initialized t)
1118 (setq flyspell-tex-math-initialized 'error)
1121 ;;*---------------------------------------------------------------------*/
1122 ;;* flyspell-tex-command-p ... */
1123 ;;*---------------------------------------------------------------------*/
1124 (defun flyspell-tex-command-p (word)
1125 "Return t if WORD is a TeX command."
1127 (let ((b (car (cdr word))))
1128 (and (re-search-backward "\\\\" (- (point) 100) t)
1129 (or (= (match-end 0) b)
1130 (and (goto-char (match-end 0))
1131 (looking-at flyspell-tex-command-regexp)
1132 (>= (match-end 0) b))))))
1133 (flyspell-math-tex-command-p)))
1135 ;;*---------------------------------------------------------------------*/
1136 ;;* flyspell-casechars-cache ... */
1137 ;;*---------------------------------------------------------------------*/
1138 (defvar flyspell-casechars-cache nil)
1139 (defvar flyspell-ispell-casechars-cache nil)
1140 (make-variable-buffer-local 'flyspell-casechars-cache)
1141 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1143 ;;*---------------------------------------------------------------------*/
1144 ;;* flyspell-get-casechars ... */
1145 ;;*---------------------------------------------------------------------*/
1146 (defun flyspell-get-casechars ()
1147 "This function builds a string that is the regexp of word chars.
1148 In order to avoid one useless string construction,
1149 this function changes the last char of the `ispell-casechars' string."
1150 (let ((ispell-casechars (ispell-get-casechars)))
1152 ((eq ispell-parser 'tex)
1153 (setq flyspell-ispell-casechars-cache ispell-casechars)
1154 (setq flyspell-casechars-cache
1155 (concat (substring ispell-casechars
1157 (- (length ispell-casechars) 1))
1159 flyspell-casechars-cache)
1161 (setq flyspell-ispell-casechars-cache ispell-casechars)
1162 (setq flyspell-casechars-cache ispell-casechars)
1163 flyspell-casechars-cache))))
1165 ;;*---------------------------------------------------------------------*/
1166 ;;* flyspell-get-not-casechars-cache ... */
1167 ;;*---------------------------------------------------------------------*/
1168 (defvar flyspell-not-casechars-cache nil)
1169 (defvar flyspell-ispell-not-casechars-cache nil)
1170 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1171 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1173 ;;*---------------------------------------------------------------------*/
1174 ;;* flyspell-get-not-casechars ... */
1175 ;;*---------------------------------------------------------------------*/
1176 (defun flyspell-get-not-casechars ()
1177 "This function builds a string that is the regexp of non-word chars."
1178 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1180 ((eq ispell-parser 'tex)
1181 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1182 (setq flyspell-not-casechars-cache
1183 (concat (substring ispell-not-casechars
1185 (- (length ispell-not-casechars) 1))
1187 flyspell-not-casechars-cache)
1189 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1190 (setq flyspell-not-casechars-cache ispell-not-casechars)
1191 flyspell-not-casechars-cache))))
1193 ;;*---------------------------------------------------------------------*/
1194 ;;* flyspell-get-word ... */
1195 ;;*---------------------------------------------------------------------*/
1196 (defun flyspell-get-word (following &optional extra-otherchars)
1197 "Return the word for spell-checking according to Ispell syntax.
1198 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1199 is non-nil when called interactively, then the following word
1200 \(rather than preceding\) is checked when the cursor is not over a word.
1201 Optional second argument contains otherchars that can be included in word
1204 Word syntax described by `flyspell-dictionary-alist' (which see)."
1205 (let* ((flyspell-casechars (flyspell-get-casechars))
1206 (flyspell-not-casechars (flyspell-get-not-casechars))
1207 (ispell-otherchars (ispell-get-otherchars))
1208 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1209 (word-regexp (concat flyspell-casechars
1211 (if (not (string= "" ispell-otherchars))
1212 (concat ispell-otherchars "?"))
1213 (if extra-otherchars
1214 (concat extra-otherchars "?"))
1217 (if (or ispell-many-otherchars-p
1223 (if (not (looking-at flyspell-casechars))
1225 (re-search-forward flyspell-casechars (point-max) t)
1226 (re-search-backward flyspell-casechars (point-min) t)))
1227 ;; move to front of word
1228 (re-search-backward flyspell-not-casechars (point-min) 'start)
1229 (while (and (or (and (not (string= "" ispell-otherchars))
1230 (looking-at ispell-otherchars))
1231 (and extra-otherchars (looking-at extra-otherchars)))
1233 (or (not did-it-once)
1234 ispell-many-otherchars-p)
1235 (not (eq prevpt (point))))
1236 (if (and extra-otherchars (looking-at extra-otherchars))
1239 (if (looking-at flyspell-casechars)
1240 (re-search-backward flyspell-not-casechars (point-min) 'move)))
1244 (if (looking-at flyspell-casechars)
1245 (re-search-backward flyspell-not-casechars (point-min) 'move)
1246 (backward-char -1))))
1247 ;; Now mark the word and save to string.
1248 (if (not (re-search-forward word-regexp (point-max) t))
1251 (setq start (match-beginning 0)
1253 word (buffer-substring-no-properties start end))
1254 (list word start end)))))
1256 ;;*---------------------------------------------------------------------*/
1257 ;;* flyspell-small-region ... */
1258 ;;*---------------------------------------------------------------------*/
1259 (defun flyspell-small-region (beg end)
1260 "Flyspell text between BEG and END."
1268 (while (< (point) end)
1269 (if (and flyspell-issue-message-flag (= count 100))
1271 (message "Spell Checking...%d%%"
1272 (* 100 (/ (float (- (point) beg)) (- end beg))))
1274 (setq count (+ 1 count)))
1277 (let ((cur (point)))
1279 (if (and (< (point) end) (> (point) (+ cur 1)))
1280 (backward-char 1)))))
1282 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1285 ;;*---------------------------------------------------------------------*/
1286 ;;* flyspell-external-ispell-process ... */
1287 ;;*---------------------------------------------------------------------*/
1288 (defvar flyspell-external-ispell-process '()
1289 "The external Flyspell Ispell process.")
1291 ;;*---------------------------------------------------------------------*/
1292 ;;* flyspell-external-ispell-buffer ... */
1293 ;;*---------------------------------------------------------------------*/
1294 (defvar flyspell-external-ispell-buffer '())
1295 (defvar flyspell-large-region-buffer '())
1296 (defvar flyspell-large-region-beg (point-min))
1297 (defvar flyspell-large-region-end (point-max))
1299 ;;*---------------------------------------------------------------------*/
1300 ;;* flyspell-external-point-words ... */
1301 ;;*---------------------------------------------------------------------*/
1302 (defun flyspell-external-point-words ()
1303 "Mark words from a buffer listing incorrect words in order of appearance.
1304 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1305 \(We finish by killing that buffer and setting the variable to nil.)
1306 The buffer to mark them in is `flyspell-large-region-buffer'."
1308 (with-current-buffer flyspell-external-ispell-buffer
1309 (goto-char (point-min))
1310 ;; Loop over incorrect words.
1311 (while (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1312 ;; Bind WORD to the next one.
1313 (let ((word (match-string 1)) (wordpos (point)))
1314 ;; Here there used to be code to see if WORD is the same
1315 ;; as the previous iteration, and count the number of consecutive
1316 ;; identical words, and the loop below would search for that many.
1317 ;; That code seemed to be incorrect, and on principle, should
1318 ;; be unnecessary too. -- rms.
1319 (if flyspell-issue-message-flag
1320 (message "Spell Checking...%d%% [%s]"
1321 (* 100 (/ (float (point)) (point-max)))
1323 ;; Search the other buffer for occurrences of this word,
1324 ;; and check them. Stop when we find one that reports "incorrect".
1325 ;; (I don't understand the reason for that logic,
1326 ;; but I didn't want to change it. -- rms.)
1327 (with-current-buffer flyspell-large-region-buffer
1328 (goto-char flyspell-large-region-beg)
1331 (if (search-forward word
1332 flyspell-large-region-end t)
1334 (setq flyspell-large-region-beg (point))
1335 (goto-char (- (point) 1))
1337 ;; Detect when WORD can't be checked properly
1338 ;; because flyspell-get-word finds
1339 ;; just part of it, and treat that as ok.
1340 (if (< (length (car (flyspell-get-word nil)))
1344 (error "Bug: misspelled word `%s' (output pos %d) not found in buffer"
1347 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1348 ;; Kill and forget the buffer with the list of incorrect words.
1349 (kill-buffer flyspell-external-ispell-buffer)
1350 (setq flyspell-external-ispell-buffer nil))
1352 ;;*---------------------------------------------------------------------*/
1353 ;;* flyspell-process-localwords ... */
1354 ;;* ------------------------------------------------------------- */
1355 ;;* This function is used to prevent marking of words explicitly */
1356 ;;* declared correct. */
1357 ;;*---------------------------------------------------------------------*/
1358 (defun flyspell-process-localwords (misspellings-buffer)
1360 (ispell-casechars (ispell-get-casechars)))
1361 ;; Get localwords from the original buffer
1363 (goto-char (point-min))
1364 ;; Localwords parsing copied from ispell.el.
1365 (while (search-forward ispell-words-keyword nil t)
1366 (let ((end (save-excursion (end-of-line) (point)))
1368 ;; buffer-local words separated by a space, and can contain
1369 ;; any character other than a space. Not rigorous enough.
1370 (while (re-search-forward " *\\([^ ]+\\)" end t)
1371 (setq string (buffer-substring-no-properties (match-beginning 1)
1373 ;; This can fail when string contains a word with invalid chars.
1374 ;; Error handling needs to be added between Ispell and Emacs.
1375 (if (and (< 1 (length string))
1376 (equal 0 (string-match ispell-casechars string)))
1377 (push string localwords))))))
1378 ;; Remove localwords matches from misspellings-buffer.
1379 ;; The usual mechanism of communicating the local words to ispell
1380 ;; does not affect the special ispell process used by
1381 ;; flyspell-large-region.
1382 (with-current-buffer misspellings-buffer
1384 (dolist (word localwords)
1385 (goto-char (point-min))
1386 (let ((regexp (concat "^" word "\n")))
1387 (while (re-search-forward regexp nil t)
1388 (delete-region (match-beginning 0) (match-end 0)))))))))
1390 ;;*---------------------------------------------------------------------*/
1391 ;;* flyspell-large-region ... */
1392 ;;*---------------------------------------------------------------------*/
1393 (defun flyspell-large-region (beg end)
1394 (let* ((curbuf (current-buffer))
1395 (buffer (get-buffer-create "*flyspell-region*")))
1396 (setq flyspell-external-ispell-buffer buffer)
1397 (setq flyspell-large-region-buffer curbuf)
1398 (setq flyspell-large-region-beg beg)
1399 (setq flyspell-large-region-end end)
1400 (flyspell-accept-buffer-local-defs)
1403 ;; this is done, we can start checking...
1404 (if flyspell-issue-message-flag (message "Checking region..."))
1406 (ispell-check-version)
1407 (let ((c (apply 'call-process-region beg
1413 (if ispell-really-aspell "list" "-l")
1415 ;; Local dictionary becomes the global dictionary in use.
1416 (if ispell-local-dictionary
1417 (setq ispell-dictionary ispell-local-dictionary))
1418 (setq args (ispell-get-ispell-args))
1419 (if (eq ispell-parser 'tex)
1420 (setq args (cons "-t" args)))
1421 (if ispell-dictionary ; use specified dictionary
1423 (append (list "-d" ispell-dictionary) args)))
1424 (if ispell-personal-dictionary ; use specified pers dict
1429 ispell-personal-dictionary)))))
1430 (setq args (append args ispell-extra-args))
1434 (flyspell-process-localwords buffer)
1435 (with-current-buffer curbuf
1436 (flyspell-delete-region-overlays beg end))
1437 (flyspell-external-point-words))
1438 (error "Can't check region...")))))
1440 ;;*---------------------------------------------------------------------*/
1441 ;;* flyspell-region ... */
1442 ;;* ------------------------------------------------------------- */
1443 ;;* Because `ispell -a' is too slow, it is not possible to use */
1444 ;;* it on large region. Then, when ispell is invoked on a large */
1445 ;;* text region, a new `ispell -l' process is spawned. The */
1446 ;;* pointed out words are then searched in the region a checked with */
1447 ;;* regular flyspell means. */
1448 ;;*---------------------------------------------------------------------*/
1450 (defun flyspell-region (beg end)
1451 "Flyspell text between BEG and END."
1460 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1461 (flyspell-large-region beg end)
1462 (flyspell-small-region beg end)))))
1464 ;;*---------------------------------------------------------------------*/
1465 ;;* flyspell-buffer ... */
1466 ;;*---------------------------------------------------------------------*/
1468 (defun flyspell-buffer ()
1469 "Flyspell whole buffer."
1471 (flyspell-region (point-min) (point-max)))
1473 ;;*---------------------------------------------------------------------*/
1474 ;;* old next error position ... */
1475 ;;*---------------------------------------------------------------------*/
1476 (defvar flyspell-old-buffer-error nil)
1477 (defvar flyspell-old-pos-error nil)
1479 ;;*---------------------------------------------------------------------*/
1480 ;;* flyspell-goto-next-error ... */
1481 ;;*---------------------------------------------------------------------*/
1482 (defun flyspell-goto-next-error ()
1483 "Go to the next previously detected error.
1484 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1489 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1490 (eq pos flyspell-old-pos-error))
1492 (if (= flyspell-old-pos-error max)
1493 ;; goto beginning of buffer
1495 (message "Restarting from beginning of buffer")
1496 (goto-char (point-min)))
1498 (setq pos (point))))
1499 ;; seek the next error
1500 (while (and (< pos max)
1501 (let ((ovs (overlays-at pos))
1503 (while (and (not r) (consp ovs))
1504 (if (flyspell-overlay-p (car ovs))
1506 (setq ovs (cdr ovs))))
1508 (setq pos (1+ pos)))
1509 ;; save the current location for next invocation
1510 (setq flyspell-old-pos-error pos)
1511 (setq flyspell-old-buffer-error (current-buffer))
1514 (message "No more miss-spelled word!"))))
1516 ;;*---------------------------------------------------------------------*/
1517 ;;* flyspell-overlay-p ... */
1518 ;;*---------------------------------------------------------------------*/
1519 (defun flyspell-overlay-p (o)
1520 "A predicate that return true iff O is an overlay used by flyspell."
1521 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1523 ;;*---------------------------------------------------------------------*/
1524 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1525 ;;* ------------------------------------------------------------- */
1526 ;;* Remove overlays introduced by flyspell. */
1527 ;;*---------------------------------------------------------------------*/
1528 (defun flyspell-delete-region-overlays (beg end)
1529 "Delete overlays used by flyspell in a given region."
1530 (remove-overlays beg end 'flyspell-overlay t))
1533 (defun flyspell-delete-all-overlays ()
1534 "Delete all the overlays used by flyspell."
1535 (flyspell-delete-region-overlays (point-min) (point-max)))
1537 ;;*---------------------------------------------------------------------*/
1538 ;;* flyspell-unhighlight-at ... */
1539 ;;*---------------------------------------------------------------------*/
1540 (defun flyspell-unhighlight-at (pos)
1541 "Remove the flyspell overlay that are located at POS."
1542 (if flyspell-persistent-highlight
1543 (let ((overlays (overlays-at pos)))
1544 (while (consp overlays)
1545 (if (flyspell-overlay-p (car overlays))
1546 (delete-overlay (car overlays)))
1547 (setq overlays (cdr overlays))))
1548 (if (flyspell-overlay-p flyspell-overlay)
1549 (delete-overlay flyspell-overlay))))
1551 ;;*---------------------------------------------------------------------*/
1552 ;;* flyspell-properties-at-p ... */
1553 ;;* ------------------------------------------------------------- */
1554 ;;* Is there an highlight properties at position pos? */
1555 ;;*---------------------------------------------------------------------*/
1556 (defun flyspell-properties-at-p (pos)
1557 "Return t if there is a text property at POS, not counting `local-map'.
1558 If variable `flyspell-highlight-properties' is set to nil,
1559 text with properties are not checked. This function is used to discover
1560 if the character at POS has any other property."
1561 (let ((prop (text-properties-at pos))
1563 (while (and keep (consp prop))
1564 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1565 (setq prop (cdr (cdr prop)))
1569 ;;*---------------------------------------------------------------------*/
1570 ;;* make-flyspell-overlay ... */
1571 ;;*---------------------------------------------------------------------*/
1572 (defun make-flyspell-overlay (beg end face mouse-face)
1573 "Allocate an overlay to highlight an incorrect word.
1574 BEG and END specify the range in the buffer of that word.
1575 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1577 (let ((overlay (make-overlay beg end nil t nil)))
1578 (overlay-put overlay 'face face)
1579 (overlay-put overlay 'mouse-face mouse-face)
1580 (overlay-put overlay 'flyspell-overlay t)
1581 (overlay-put overlay 'evaporate t)
1582 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1583 (overlay-put overlay 'keymap flyspell-mouse-map)
1584 (when (eq face 'flyspell-incorrect)
1585 (and (stringp flyspell-before-incorrect-word-string)
1586 (overlay-put overlay 'before-string
1587 flyspell-before-incorrect-word-string))
1588 (and (stringp flyspell-after-incorrect-word-string)
1589 (overlay-put overlay 'after-string
1590 flyspell-after-incorrect-word-string)))
1593 ;;*---------------------------------------------------------------------*/
1594 ;;* flyspell-highlight-incorrect-region ... */
1595 ;;*---------------------------------------------------------------------*/
1596 (defun flyspell-highlight-incorrect-region (beg end poss)
1597 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1598 POSS is usually a list of possible spelling/correction lists,
1599 as returned by `ispell-parse-output'.
1600 It can also be the symbol `doublon', in the case where the word
1601 is itself incorrect, but suspiciously repeated."
1602 (let ((inhibit-read-only t))
1603 (unless (run-hook-with-args-until-success
1604 'flyspell-incorrect-hook beg end poss)
1605 (if (or flyspell-highlight-properties
1606 (not (flyspell-properties-at-p beg)))
1608 ;; we cleanup all the overlay that are in the region, not
1609 ;; beginning at the word start position
1610 (if (< (1+ beg) end)
1611 (let ((os (overlays-in (1+ beg) end)))
1613 (if (flyspell-overlay-p (car os))
1614 (delete-overlay (car os)))
1615 (setq os (cdr os)))))
1616 ;; we cleanup current overlay at the same position
1617 (flyspell-unhighlight-at beg)
1618 ;; now we can use a new overlay
1619 (setq flyspell-overlay
1620 (make-flyspell-overlay
1621 beg end 'flyspell-incorrect 'highlight)))))))
1623 ;;*---------------------------------------------------------------------*/
1624 ;;* flyspell-highlight-duplicate-region ... */
1625 ;;*---------------------------------------------------------------------*/
1626 (defun flyspell-highlight-duplicate-region (beg end poss)
1627 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1628 POSS is a list of possible spelling/correction lists,
1629 as returned by `ispell-parse-output'."
1630 (let ((inhibit-read-only t))
1631 (unless (run-hook-with-args-until-success
1632 'flyspell-incorrect-hook beg end poss)
1633 (if (or flyspell-highlight-properties
1634 (not (flyspell-properties-at-p beg)))
1636 ;; we cleanup current overlay at the same position
1637 (flyspell-unhighlight-at beg)
1638 ;; now we can use a new overlay
1639 (setq flyspell-overlay
1640 (make-flyspell-overlay beg end
1644 ;;*---------------------------------------------------------------------*/
1645 ;;* flyspell-auto-correct-cache ... */
1646 ;;*---------------------------------------------------------------------*/
1647 (defvar flyspell-auto-correct-pos nil)
1648 (defvar flyspell-auto-correct-region nil)
1649 (defvar flyspell-auto-correct-ring nil)
1650 (defvar flyspell-auto-correct-word nil)
1651 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1652 (make-variable-buffer-local 'flyspell-auto-correct-region)
1653 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1654 (make-variable-buffer-local 'flyspell-auto-correct-word)
1656 ;;*---------------------------------------------------------------------*/
1657 ;;* flyspell-check-previous-highlighted-word ... */
1658 ;;*---------------------------------------------------------------------*/
1659 (defun flyspell-check-previous-highlighted-word (&optional arg)
1660 "Correct the closer misspelled word.
1661 This function scans a mis-spelled word before the cursor. If it finds one
1662 it proposes replacement for that word. With prefix arg, count that many
1663 misspelled words backwards."
1665 (let ((pos1 (point))
1667 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1670 (while (and (setq pos (previous-overlay-change pos))
1673 (if (> pos (point-min))
1675 (setq ovs (overlays-at (1- pos)))
1678 (setq ovs (cdr ovs))
1679 (if (and (flyspell-overlay-p ov)
1680 (= 0 (setq arg (1- arg))))
1681 (throw 'exit t)))))))
1685 (error "No word to correct before point"))))
1687 ;;*---------------------------------------------------------------------*/
1688 ;;* flyspell-display-next-corrections ... */
1689 ;;*---------------------------------------------------------------------*/
1690 (defun flyspell-display-next-corrections (corrections)
1691 (let ((string "Corrections:")
1694 (while (< (length string) 80)
1695 (if (equal (car l) flyspell-auto-correct-word)
1696 (setq pos (cons (+ 1 (length string)) pos)))
1697 (setq string (concat string " " (car l)))
1700 (let ((num (car pos)))
1701 (put-text-property num
1702 (+ num (length flyspell-auto-correct-word))
1703 'face 'flyspell-incorrect
1705 (setq pos (cdr pos)))
1706 (if (fboundp 'display-message)
1707 (display-message 'no-log string)
1708 (message "%s" string))))
1710 ;;*---------------------------------------------------------------------*/
1711 ;;* flyspell-abbrev-table ... */
1712 ;;*---------------------------------------------------------------------*/
1713 (defun flyspell-abbrev-table ()
1714 (if flyspell-use-global-abbrev-table-p
1716 (or local-abbrev-table global-abbrev-table)))
1718 ;;*---------------------------------------------------------------------*/
1719 ;;* flyspell-define-abbrev ... */
1720 ;;*---------------------------------------------------------------------*/
1721 (defun flyspell-define-abbrev (name expansion)
1722 (let ((table (flyspell-abbrev-table)))
1724 (define-abbrev table name expansion))))
1726 ;;*---------------------------------------------------------------------*/
1727 ;;* flyspell-auto-correct-word ... */
1728 ;;*---------------------------------------------------------------------*/
1729 (defun flyspell-auto-correct-word ()
1730 "Correct the current word.
1731 This command proposes various successive corrections for the current word."
1734 (old-max (point-max)))
1735 ;; use the correct dictionary
1736 (flyspell-accept-buffer-local-defs)
1737 (if (and (eq flyspell-auto-correct-pos pos)
1738 (consp flyspell-auto-correct-region))
1739 ;; we have already been using the function at the same location
1740 (let* ((start (car flyspell-auto-correct-region))
1741 (len (cdr flyspell-auto-correct-region)))
1742 (flyspell-unhighlight-at start)
1743 (delete-region start (+ start len))
1744 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1745 (let* ((word (car flyspell-auto-correct-ring))
1746 (len (length word)))
1747 (rplacd flyspell-auto-correct-region len)
1749 (if flyspell-abbrev-p
1750 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1751 flyspell-auto-correct-word)
1752 (flyspell-change-abbrev (flyspell-abbrev-table)
1753 flyspell-auto-correct-word
1755 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1756 (funcall flyspell-insert-function word)
1758 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1759 (flyspell-ajust-cursor-point pos (point) old-max)
1760 (setq flyspell-auto-correct-pos (point)))
1761 ;; fetch the word to be checked
1762 (let ((word (flyspell-get-word nil)))
1764 (let ((start (car (cdr word)))
1765 (end (car (cdr (cdr word))))
1768 (setq flyspell-auto-correct-word word)
1769 ;; now check spelling of word.
1770 (ispell-send-string "%\n") ;put in verbose mode
1771 (ispell-send-string (concat "^" word "\n"))
1772 ;; wait until ispell has processed word.
1774 (accept-process-output ispell-process)
1775 (not (string= "" (car ispell-filter)))))
1776 (setq ispell-filter (cdr ispell-filter))
1777 (if (consp ispell-filter)
1778 (setq poss (ispell-parse-output (car ispell-filter))))
1780 ((or (eq poss t) (stringp poss))
1781 ;; don't correct word
1785 (error "Ispell: error in Ispell process"))
1787 ;; the word is incorrect, we have to propose a replacement
1788 (let ((replacements (if flyspell-sort-corrections
1789 (sort (car (cdr (cdr poss))) 'string<)
1790 (car (cdr (cdr poss))))))
1791 (setq flyspell-auto-correct-region nil)
1792 (if (consp replacements)
1794 (let ((replace (car replacements)))
1795 (let ((new-word replace))
1796 (if (not (equal new-word (car poss)))
1798 ;; the save the current replacements
1799 (setq flyspell-auto-correct-region
1800 (cons start (length new-word)))
1801 (let ((l replacements))
1802 (while (consp (cdr l))
1804 (rplacd l (cons (car poss) replacements)))
1805 (setq flyspell-auto-correct-ring
1807 (flyspell-unhighlight-at start)
1808 (delete-region start end)
1809 (funcall flyspell-insert-function new-word)
1810 (if flyspell-abbrev-p
1811 (if (flyspell-already-abbrevp
1812 (flyspell-abbrev-table) word)
1813 (flyspell-change-abbrev
1814 (flyspell-abbrev-table)
1817 (flyspell-define-abbrev word
1820 (flyspell-display-next-corrections
1821 (cons new-word flyspell-auto-correct-ring))
1822 (flyspell-ajust-cursor-point pos
1825 (setq flyspell-auto-correct-pos (point))
1826 (ispell-pdict-save t)))))))
1828 ;;*---------------------------------------------------------------------*/
1829 ;;* flyspell-auto-correct-previous-pos ... */
1830 ;;*---------------------------------------------------------------------*/
1831 (defvar flyspell-auto-correct-previous-pos nil
1832 "Holds the start of the first incorrect word before point.")
1834 ;;*---------------------------------------------------------------------*/
1835 ;;* flyspell-auto-correct-previous-hook ... */
1836 ;;*---------------------------------------------------------------------*/
1837 (defun flyspell-auto-correct-previous-hook ()
1838 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1839 Sets `flyspell-auto-correct-previous-pos' to nil"
1841 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1842 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1843 (setq flyspell-auto-correct-previous-pos nil)))
1845 ;;*---------------------------------------------------------------------*/
1846 ;;* flyspell-auto-correct-previous-word ... */
1847 ;;*---------------------------------------------------------------------*/
1848 (defun flyspell-auto-correct-previous-word (position)
1849 "Auto correct the first mispelled word that occurs before point.
1850 But don't look beyond what's visible on the screen."
1855 (move-to-window-line 0)
1857 (move-to-window-line -1)
1861 (narrow-to-region top bot)
1862 (overlay-recenter (point))
1864 (add-hook 'pre-command-hook
1865 (function flyspell-auto-correct-previous-hook) t t)
1867 (unless flyspell-auto-correct-previous-pos
1868 ;; only reset if a new overlay exists
1869 (setq flyspell-auto-correct-previous-pos nil)
1871 (let ((overlay-list (overlays-in (point-min) position))
1872 (new-overlay 'dummy-value))
1874 ;; search for previous (new) flyspell overlay
1875 (while (and new-overlay
1876 (or (not (flyspell-overlay-p new-overlay))
1877 ;; check if its face has changed
1878 (not (eq (get-char-property
1879 (overlay-start new-overlay) 'face)
1880 'flyspell-incorrect))))
1881 (setq new-overlay (car-safe overlay-list))
1882 (setq overlay-list (cdr-safe overlay-list)))
1884 ;; if nothing new exits new-overlay should be nil
1885 (if new-overlay ;; the length of the word may change so go to the start
1886 (setq flyspell-auto-correct-previous-pos
1887 (overlay-start new-overlay)))))
1889 (when flyspell-auto-correct-previous-pos
1891 (goto-char flyspell-auto-correct-previous-pos)
1892 (let ((ispell-following-word t)) ;; point is at start
1893 (if (numberp flyspell-auto-correct-previous-pos)
1894 (goto-char flyspell-auto-correct-previous-pos))
1895 (flyspell-auto-correct-word))
1896 ;; the point may have moved so reset this
1897 (setq flyspell-auto-correct-previous-pos (point))))))))
1899 ;;*---------------------------------------------------------------------*/
1900 ;;* flyspell-correct-word ... */
1901 ;;*---------------------------------------------------------------------*/
1902 (defun flyspell-correct-word (event)
1903 "Pop up a menu of possible corrections for a misspelled word.
1904 The word checked is the word at the mouse position."
1906 ;; use the correct dictionary
1907 (flyspell-accept-buffer-local-defs)
1908 ;; retain cursor location (I don't know why but save-excursion here fails).
1909 (let ((save (point)))
1910 (mouse-set-point event)
1911 (let ((cursor-location (point))
1912 (word (flyspell-get-word nil)))
1914 (let ((start (car (cdr word)))
1915 (end (car (cdr (cdr word))))
1918 ;; now check spelling of word.
1919 (ispell-send-string "%\n") ;put in verbose mode
1920 (ispell-send-string (concat "^" word "\n"))
1921 ;; wait until ispell has processed word
1923 (accept-process-output ispell-process)
1924 (not (string= "" (car ispell-filter)))))
1925 (setq ispell-filter (cdr ispell-filter))
1926 (if (consp ispell-filter)
1927 (setq poss (ispell-parse-output (car ispell-filter))))
1929 ((or (eq poss t) (stringp poss))
1930 ;; don't correct word
1934 (error "Ispell: error in Ispell process"))
1936 (flyspell-xemacs-popup
1937 poss word cursor-location start end save))
1939 ;; The word is incorrect, we have to propose a replacement.
1940 (flyspell-do-correct (flyspell-emacs-popup event poss word)
1941 poss word cursor-location start end save)))
1942 (ispell-pdict-save t))))))
1944 ;;*---------------------------------------------------------------------*/
1945 ;;* flyspell-do-correct ... */
1946 ;;*---------------------------------------------------------------------*/
1947 (defun flyspell-do-correct (replace poss word cursor-location start end save)
1948 "The popup menu callback."
1949 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
1950 ;; it instead right after calling the function.
1951 (cond ((eq replace 'ignore)
1956 (ispell-send-string (concat "*" word "\n"))
1957 ;; This was added only to the XEmacs side in revision 1.18 of
1958 ;; flyspell. I assume its absence on the Emacs side was an
1959 ;; oversight. --Stef
1960 (ispell-send-string "#\n")
1961 (flyspell-unhighlight-at cursor-location)
1962 (setq ispell-pdict-modified-p '(t)))
1963 ((or (eq replace 'buffer) (eq replace 'session))
1964 (ispell-send-string (concat "@" word "\n"))
1965 (flyspell-unhighlight-at cursor-location)
1966 (if (null ispell-pdict-modified-p)
1967 (setq ispell-pdict-modified-p
1968 (list ispell-pdict-modified-p)))
1970 (if (eq replace 'buffer)
1971 (ispell-add-per-file-word-list word)))
1973 ;; This was added only to the Emacs side. I assume its absence on
1974 ;; the XEmacs side was an oversight. --Stef
1975 (flyspell-unhighlight-at cursor-location)
1976 (let ((old-max (point-max))
1977 (new-word (if (atom replace)
1980 (cursor-location (+ (- (length word) (- end start))
1982 (unless (equal new-word (car poss))
1983 (delete-region start end)
1985 (funcall flyspell-insert-function new-word)
1986 (if flyspell-abbrev-p
1987 (flyspell-define-abbrev word new-word)))
1988 ;; In the original Emacs code, this was only called in the body
1989 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
1990 (flyspell-ajust-cursor-point save cursor-location old-max)))
1995 ;;*---------------------------------------------------------------------*/
1996 ;;* flyspell-ajust-cursor-point ... */
1997 ;;*---------------------------------------------------------------------*/
1998 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
1999 (if (>= save cursor-location)
2000 (let ((new-pos (+ save (- (point-max) old-max))))
2002 ((< new-pos (point-min))
2004 ((> new-pos (point-max))
2009 ;;*---------------------------------------------------------------------*/
2010 ;;* flyspell-emacs-popup ... */
2011 ;;*---------------------------------------------------------------------*/
2012 (defun flyspell-emacs-popup (event poss word)
2013 "The Emacs popup menu."
2015 (let* ((mouse-pos (mouse-position))
2016 (mouse-pos (if (nth 1 mouse-pos)
2018 (set-mouse-position (car mouse-pos)
2019 (/ (frame-width) 2) 2)
2021 (setq event (list (list (car (cdr mouse-pos))
2022 (1+ (cdr (cdr mouse-pos))))
2024 (let* ((corrects (if flyspell-sort-corrections
2025 (sort (car (cdr (cdr poss))) 'string<)
2026 (car (cdr (cdr poss)))))
2027 (cor-menu (if (consp corrects)
2028 (mapcar (lambda (correct)
2029 (list correct correct))
2032 (affix (car (cdr (cdr (cdr poss)))))
2033 (base-menu (let ((save (if (consp affix)
2035 (list (concat "Save affix: " (car affix))
2037 '("Accept (session)" session)
2038 '("Accept (buffer)" buffer))
2039 '(("Save word" save)
2040 ("Accept (session)" session)
2041 ("Accept (buffer)" buffer)))))
2042 (if (consp cor-menu)
2043 (append cor-menu (cons "" save))
2045 (menu (cons "flyspell correction menu" base-menu)))
2046 (car (x-popup-menu event
2047 (list (format "%s [%s]" word (or ispell-local-dictionary
2051 ;;*---------------------------------------------------------------------*/
2052 ;;* flyspell-xemacs-popup ... */
2053 ;;*---------------------------------------------------------------------*/
2054 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2055 "The XEmacs popup menu."
2056 (let* ((corrects (if flyspell-sort-corrections
2057 (sort (car (cdr (cdr poss))) 'string<)
2058 (car (cdr (cdr poss)))))
2059 (cor-menu (if (consp corrects)
2060 (mapcar (lambda (correct)
2062 (list 'flyspell-do-correct
2073 (affix (car (cdr (cdr (cdr poss)))))
2074 (menu (let ((save (if (consp affix)
2076 (concat "Save affix: " (car affix))
2077 (list 'flyspell-do-correct
2088 (list 'flyspell-do-correct
2097 (session (vector "Accept (session)"
2098 (list 'flyspell-do-correct
2107 (buffer (vector "Accept (buffer)"
2108 (list 'flyspell-do-correct
2117 (if (consp cor-menu)
2118 (append cor-menu (list "-" save session buffer))
2119 (list save session buffer)))))
2120 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2124 ;;*---------------------------------------------------------------------*/
2125 ;;* Some example functions for real autocorrecting */
2126 ;;*---------------------------------------------------------------------*/
2127 (defun flyspell-maybe-correct-transposition (beg end poss)
2128 "Check replacements for transposed characters.
2130 If the text between BEG and END is equal to a correction suggested by
2131 Ispell, after transposing two adjacent characters, correct the text,
2134 The third arg POSS is either the symbol 'doublon' or a list of
2135 possible corrections as returned by `ispell-parse-output'.
2137 This function is meant to be added to `flyspell-incorrect-hook'."
2140 (let ((str (buffer-substring beg end))
2141 (i 0) (len (- end beg)) tmp)
2142 (while (< (1+ i) len)
2143 (setq tmp (aref str i))
2144 (aset str i (aref str (1+ i)))
2145 (aset str (1+ i) tmp)
2146 (when (member str (nth 2 poss))
2148 (goto-char (+ beg i 1))
2149 (transpose-chars 1))
2151 (setq tmp (aref str i))
2152 (aset str i (aref str (1+ i)))
2153 (aset str (1+ i) tmp)
2157 (defun flyspell-maybe-correct-doubling (beg end poss)
2158 "Check replacements for doubled characters.
2160 If the text between BEG and END is equal to a correction suggested by
2161 Ispell, after removing a pair of doubled characters, correct the text,
2164 The third arg POSS is either the symbol 'doublon' or a list of
2165 possible corrections as returned by `ispell-parse-output'.
2167 This function is meant to be added to `flyspell-incorrect-hook'."
2170 (let ((str (buffer-substring beg end))
2171 (i 0) (len (- end beg)))
2172 (while (< (1+ i) len)
2173 (when (and (= (aref str i) (aref str (1+ i)))
2174 (member (concat (substring str 0 (1+ i))
2175 (substring str (+ i 2)))
2177 (goto-char (+ beg i))
2183 ;;*---------------------------------------------------------------------*/
2184 ;;* flyspell-already-abbrevp ... */
2185 ;;*---------------------------------------------------------------------*/
2186 (defun flyspell-already-abbrevp (table word)
2187 (let ((sym (abbrev-symbol word table)))
2188 (and sym (symbolp sym))))
2190 ;;*---------------------------------------------------------------------*/
2191 ;;* flyspell-change-abbrev ... */
2192 ;;*---------------------------------------------------------------------*/
2193 (defun flyspell-change-abbrev (table old new)
2194 (set (abbrev-symbol old table) new))
2198 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2199 ;;; flyspell.el ends here