1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006 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" nil 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 (defvar flyspell-last-buffer nil
505 "The buffer in which the last flyspell operation took place.")
507 (defun flyspell-accept-buffer-local-defs (&optional force)
508 ;; When flyspell-word is used inside a loop (e.g. when processing
509 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
510 ;; up dwarfing everything else, so only do it when the buffer has changed.
511 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
512 (setq flyspell-last-buffer (current-buffer))
513 ;; Strange problem: If buffer in current window has font-lock turned on,
514 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
515 ;; call will reset the buffer to the buffer in the current window.
516 ;; However, it only happens at startup (fix by Albert L. Ting).
518 (ispell-accept-buffer-local-defs))
519 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
520 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
521 ;; The dictionary has changed
522 (setq flyspell-dash-dictionary ispell-dictionary)
523 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
524 (setq flyspell-consider-dash-as-word-delimiter-flag
525 (member (or ispell-local-dictionary ispell-dictionary)
526 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
528 (defun flyspell-kill-ispell-hook ()
529 (setq flyspell-last-buffer nil)
530 (dolist (buf (buffer-list))
531 (kill-local-variable 'flyspell-word-cache-word)))
533 ;;*---------------------------------------------------------------------*/
534 ;;* flyspell-mode-on ... */
535 ;;*---------------------------------------------------------------------*/
536 (defun flyspell-mode-on ()
537 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
538 (ispell-maybe-find-aspell-dictionaries)
539 (setq ispell-highlight-face 'flyspell-incorrect)
540 ;; local dictionaries setup
541 (or ispell-local-dictionary ispell-dictionary
542 (if flyspell-default-dictionary
543 (ispell-change-dictionary flyspell-default-dictionary)))
544 ;; Make sure we flush our caches when needed.
545 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
546 ;; we have to force ispell to accept the local definition or
547 ;; otherwise it could be too late, the local dictionary may
549 ;; Pass the `force' argument for the case where flyspell was active already
550 ;; but the buffer's local-defs have been edited.
551 (flyspell-accept-buffer-local-defs 'force)
552 ;; we put the `flyspell-delayed' property on some commands
553 (flyspell-delay-commands)
554 ;; we put the `flyspell-deplacement' property on some commands
555 (flyspell-deplacement-commands)
556 ;; we bound flyspell action to post-command hook
557 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
558 ;; we bound flyspell action to pre-command hook
559 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
560 ;; we bound flyspell action to after-change hook
561 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
562 ;; set flyspell-generic-check-word-p based on the major mode
563 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
565 (setq flyspell-generic-check-word-p mode-predicate)))
566 ;; the welcome message
567 (if (and flyspell-issue-message-flag
568 flyspell-issue-welcome-flag
570 (let ((binding (where-is-internal 'flyspell-auto-correct-word
574 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
575 (key-description binding))
576 "Welcome to flyspell. Use Mouse-2 to correct words."))))
577 ;; we end with the flyspell hooks
578 (run-hooks 'flyspell-mode-hook))
580 ;;*---------------------------------------------------------------------*/
581 ;;* flyspell-delay-commands ... */
582 ;;*---------------------------------------------------------------------*/
583 (defun flyspell-delay-commands ()
584 "Install the standard set of Flyspell delayed commands."
585 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
586 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
588 ;;*---------------------------------------------------------------------*/
589 ;;* flyspell-delay-command ... */
590 ;;*---------------------------------------------------------------------*/
591 (defun flyspell-delay-command (command)
592 "Set COMMAND to be delayed, for Flyspell.
593 When flyspell `post-command-hook' is invoked because a delayed command
594 as been used the current word is not immediately checked.
595 It will be checked only after `flyspell-delay' seconds."
596 (interactive "SDelay Flyspell after Command: ")
597 (put command 'flyspell-delayed t))
599 ;;*---------------------------------------------------------------------*/
600 ;;* flyspell-deplacement-commands ... */
601 ;;*---------------------------------------------------------------------*/
602 (defun flyspell-deplacement-commands ()
603 "Install the standard set of Flyspell deplacement commands."
604 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
605 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
607 ;;*---------------------------------------------------------------------*/
608 ;;* flyspell-deplacement-command ... */
609 ;;*---------------------------------------------------------------------*/
610 (defun flyspell-deplacement-command (command)
611 "Set COMMAND that implement cursor movements, for Flyspell.
612 When flyspell `post-command-hook' is invoked because of a deplacement command
613 as been used the current word is checked only if the previous command was
614 not the very same deplacement command."
615 (interactive "SDeplacement Flyspell after Command: ")
616 (put command 'flyspell-deplacement t))
618 ;;*---------------------------------------------------------------------*/
619 ;;* flyspell-word-cache ... */
620 ;;*---------------------------------------------------------------------*/
621 (defvar flyspell-word-cache-start nil)
622 (defvar flyspell-word-cache-end nil)
623 (defvar flyspell-word-cache-word nil)
624 (defvar flyspell-word-cache-result '_)
625 (make-variable-buffer-local 'flyspell-word-cache-start)
626 (make-variable-buffer-local 'flyspell-word-cache-end)
627 (make-variable-buffer-local 'flyspell-word-cache-word)
628 (make-variable-buffer-local 'flyspell-word-cache-result)
630 ;;*---------------------------------------------------------------------*/
631 ;;* The flyspell pre-hook, store the current position. In the */
632 ;;* post command hook, we will check, if the word at this position */
633 ;;* has to be spell checked. */
634 ;;*---------------------------------------------------------------------*/
635 (defvar flyspell-pre-buffer nil)
636 (defvar flyspell-pre-point nil)
637 (defvar flyspell-pre-column nil)
638 (defvar flyspell-pre-pre-buffer nil)
639 (defvar flyspell-pre-pre-point nil)
641 ;;*---------------------------------------------------------------------*/
642 ;;* flyspell-previous-command ... */
643 ;;*---------------------------------------------------------------------*/
644 (defvar flyspell-previous-command nil
645 "The last interactive command checked by Flyspell.")
647 ;;*---------------------------------------------------------------------*/
648 ;;* flyspell-pre-command-hook ... */
649 ;;*---------------------------------------------------------------------*/
650 (defun flyspell-pre-command-hook ()
651 "Save the current buffer and point for Flyspell's post-command hook."
653 (setq flyspell-pre-buffer (current-buffer))
654 (setq flyspell-pre-point (point))
655 (setq flyspell-pre-column (current-column)))
657 ;;*---------------------------------------------------------------------*/
658 ;;* flyspell-mode-off ... */
659 ;;*---------------------------------------------------------------------*/
661 (defun flyspell-mode-off ()
662 "Turn Flyspell mode off."
663 ;; we remove the hooks
664 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
665 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
666 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
667 ;; we remove all the flyspell hilightings
668 (flyspell-delete-all-overlays)
669 ;; we have to erase pre cache variables
670 (setq flyspell-pre-buffer nil)
671 (setq flyspell-pre-point nil)
672 ;; we mark the mode as killed
673 (setq flyspell-mode nil))
675 ;;*---------------------------------------------------------------------*/
676 ;;* flyspell-check-pre-word-p ... */
677 ;;*---------------------------------------------------------------------*/
678 (defun flyspell-check-pre-word-p ()
679 "Return non-nil if we should check the word before point.
680 More precisely, it applies to the word that was before point
681 before the current command."
683 ((or (not (numberp flyspell-pre-point))
684 (not (bufferp flyspell-pre-buffer))
685 (not (buffer-live-p flyspell-pre-buffer)))
687 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
688 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
690 ((or (and (= flyspell-pre-point (- (point) 1))
691 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
692 (= flyspell-pre-point (point))
693 (= flyspell-pre-point (+ (point) 1)))
695 ((and (symbolp this-command)
696 (not executing-kbd-macro)
697 (or (get this-command 'flyspell-delayed)
698 (and (get this-command 'flyspell-deplacement)
699 (eq flyspell-previous-command this-command)))
700 (or (= (current-column) 0)
701 (= (current-column) flyspell-pre-column)
702 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
704 ((not (eq (current-buffer) flyspell-pre-buffer))
706 ((not (and (numberp flyspell-word-cache-start)
707 (numberp flyspell-word-cache-end)))
710 (or (< flyspell-pre-point flyspell-word-cache-start)
711 (> flyspell-pre-point flyspell-word-cache-end)))))
713 ;;*---------------------------------------------------------------------*/
714 ;;* The flyspell after-change-hook, store the change position. In */
715 ;;* the post command hook, we will check, if the word at this */
716 ;;* position has to be spell checked. */
717 ;;*---------------------------------------------------------------------*/
718 (defvar flyspell-changes nil)
719 (make-variable-buffer-local 'flyspell-changes)
721 ;;*---------------------------------------------------------------------*/
722 ;;* flyspell-after-change-function ... */
723 ;;*---------------------------------------------------------------------*/
724 (defun flyspell-after-change-function (start stop len)
725 "Save the current buffer and point for Flyspell's post-command hook."
726 (push (cons start stop) flyspell-changes))
728 ;;*---------------------------------------------------------------------*/
729 ;;* flyspell-check-changed-word-p ... */
730 ;;*---------------------------------------------------------------------*/
731 (defun flyspell-check-changed-word-p (start stop)
732 "Return t when the changed word has to be checked.
733 The answer depends of several criteria.
734 Mostly we check word delimiters."
736 ((and (memq (char-after start) '(?\n ? )) (> stop start))
738 ((not (numberp flyspell-pre-point))
740 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
742 ((let ((pos (point)))
743 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
748 ;;*---------------------------------------------------------------------*/
749 ;;* flyspell-check-word-p ... */
750 ;;*---------------------------------------------------------------------*/
751 (defun flyspell-check-word-p ()
752 "Return t when the word at `point' has to be checked.
753 The answer depends of several criteria.
754 Mostly we check word delimiters."
756 ((<= (- (point-max) 1) (point-min))
757 ;; the buffer is not filled enough
759 ((and (and (> (current-column) 0)
760 (not (eq (current-column) flyspell-pre-column)))
763 (and (looking-at (flyspell-get-not-casechars))
764 (or flyspell-consider-dash-as-word-delimiter-flag
765 (not (looking-at "-"))))))
766 ;; yes because we have reached or typed a word delimiter.
768 ((symbolp this-command)
770 ((get this-command 'flyspell-deplacement)
771 (not (eq flyspell-previous-command this-command)))
772 ((get this-command 'flyspell-delayed)
773 ;; the current command is not delayed, that
774 ;; is that we must check the word now
775 (and (not unread-command-events)
776 (sit-for flyspell-delay)))
780 ;;*---------------------------------------------------------------------*/
781 ;;* flyspell-debug-signal-no-check ... */
782 ;;*---------------------------------------------------------------------*/
783 (defun flyspell-debug-signal-no-check (msg obj)
784 (setq debug-on-error t)
785 (with-current-buffer (get-buffer-create "*flyspell-debug*")
787 (insert "NO-CHECK:\n")
788 (insert (format " %S : %S\n" msg obj))))
790 ;;*---------------------------------------------------------------------*/
791 ;;* flyspell-debug-signal-pre-word-checked ... */
792 ;;*---------------------------------------------------------------------*/
793 (defun flyspell-debug-signal-pre-word-checked ()
794 (setq debug-on-error t)
795 (with-current-buffer (get-buffer-create "*flyspell-debug*")
796 (insert "PRE-WORD:\n")
797 (insert (format " pre-point : %S\n" flyspell-pre-point))
798 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
799 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
800 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
801 (goto-char (point-max))))
803 ;;*---------------------------------------------------------------------*/
804 ;;* flyspell-debug-signal-word-checked ... */
805 ;;*---------------------------------------------------------------------*/
806 (defun flyspell-debug-signal-word-checked ()
807 (setq debug-on-error t)
808 (let ((oldbuf (current-buffer))
810 (with-current-buffer (get-buffer-create "*flyspell-debug*")
812 (insert (format " this-cmd : %S\n" this-command))
813 (insert (format " delayed : %S\n" (and (symbolp this-command)
814 (get this-command 'flyspell-delayed))))
815 (insert (format " point : %S\n" point))
816 (insert (format " prev-char : [%c] %S\n"
817 (with-current-buffer oldbuf
818 (let ((c (if (> (point) (point-min))
821 (char-after (point)))
824 (with-current-buffer oldbuf
825 (let ((c (if (> (point) (point-min))
828 (and (and (looking-at (flyspell-get-not-casechars)) 1)
829 (and (or flyspell-consider-dash-as-word-delimiter-flag
830 (not (looking-at "\\-"))) 2))))))
832 (insert (format " because : %S\n"
834 ((not (and (symbolp this-command)
835 (get this-command 'flyspell-delayed)))
836 ;; the current command is not delayed, that
837 ;; is that we must check the word now
839 ((with-current-buffer oldbuf
840 (let ((c (if (> (point) (point-min))
843 (and (looking-at (flyspell-get-not-casechars))
844 (or flyspell-consider-dash-as-word-delimiter-flag
845 (not (looking-at "\\-"))))))))
847 ;; yes because we have reached or typed a word delimiter.
849 ((not (integerp flyspell-delay))
850 ;; yes because the user had set up a no-delay configuration.
854 (goto-char (point-max)))))
856 ;;*---------------------------------------------------------------------*/
857 ;;* flyspell-debug-signal-changed-checked ... */
858 ;;*---------------------------------------------------------------------*/
859 (defun flyspell-debug-signal-changed-checked ()
860 (setq debug-on-error t)
861 (let ((point (point)))
862 (with-current-buffer (get-buffer-create "*flyspell-debug*")
863 (insert "CHANGED WORD:\n")
864 (insert (format " point : %S\n" point))
865 (goto-char (point-max)))))
867 ;;*---------------------------------------------------------------------*/
868 ;;* flyspell-post-command-hook ... */
869 ;;* ------------------------------------------------------------- */
870 ;;* It is possible that we check several words: */
871 ;;* 1- the current word is checked if the predicate */
872 ;;* FLYSPELL-CHECK-WORD-P is true */
873 ;;* 2- the word that used to be the current word before the */
874 ;;* THIS-COMMAND is checked if: */
875 ;;* a- the previous word is different from the current word */
876 ;;* b- the previous word as not just been checked by the */
877 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
878 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
879 ;;* previous word nor the current word */
880 ;;*---------------------------------------------------------------------*/
881 (defun flyspell-post-command-hook ()
882 "The `post-command-hook' used by flyspell to check a word in-the-fly."
884 (let ((command this-command)
885 ;; Prevent anything we do from affecting the mark.
887 (if (flyspell-check-pre-word-p)
888 (with-current-buffer flyspell-pre-buffer
889 '(flyspell-debug-signal-pre-word-checked)
891 (goto-char flyspell-pre-point)
893 (if (flyspell-check-word-p)
895 '(flyspell-debug-signal-word-checked)
897 ;; we remember which word we have just checked.
898 ;; this will be used next time we will check a word
899 ;; to compare the next current word with the word
900 ;; that as been registered in the pre-command-hook
901 ;; that is these variables are used within the predicate
902 ;; FLYSPELL-CHECK-PRE-WORD-P
903 (setq flyspell-pre-pre-buffer (current-buffer))
904 (setq flyspell-pre-pre-point (point)))
906 (setq flyspell-pre-pre-buffer nil)
907 (setq flyspell-pre-pre-point nil)
908 ;; when a word is not checked because of a delayed command
909 ;; we do not disable the ispell cache.
910 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
912 (setq flyspell-word-cache-end -1)
913 (setq flyspell-word-cache-result '_)))))
914 (while (and (not (input-pending-p)) (consp flyspell-changes))
915 (let ((start (car (car flyspell-changes)))
916 (stop (cdr (car flyspell-changes))))
917 (if (flyspell-check-changed-word-p start stop)
919 '(flyspell-debug-signal-changed-checked)
922 (setq flyspell-changes (cdr flyspell-changes))))
923 (setq flyspell-previous-command command)))
925 ;;*---------------------------------------------------------------------*/
926 ;;* flyspell-notify-misspell ... */
927 ;;*---------------------------------------------------------------------*/
928 (defun flyspell-notify-misspell (word poss)
929 (let ((replacements (if (stringp poss)
931 (if flyspell-sort-corrections
932 (sort (car (cdr (cdr poss))) 'string<)
933 (car (cdr (cdr poss)))))))
934 (if flyspell-issue-message-flag
935 (message "misspelling `%s' %S" word replacements))))
937 ;;*---------------------------------------------------------------------*/
938 ;;* flyspell-word-search-backward ... */
939 ;;*---------------------------------------------------------------------*/
940 (defun flyspell-word-search-backward (word bound)
944 (while (and (not r) (setq p (search-backward word bound t)))
945 (let ((lw (flyspell-get-word '())))
946 (if (and (consp lw) (string-equal (car lw) word))
951 ;;*---------------------------------------------------------------------*/
952 ;;* flyspell-word-search-forward ... */
953 ;;*---------------------------------------------------------------------*/
954 (defun flyspell-word-search-forward (word bound)
958 (while (and (not r) (setq p (search-forward word bound t)))
959 (let ((lw (flyspell-get-word '())))
960 (if (and (consp lw) (string-equal (car lw) word))
962 (goto-char (1+ p)))))
965 ;;*---------------------------------------------------------------------*/
966 ;;* flyspell-word ... */
967 ;;*---------------------------------------------------------------------*/
968 (defun flyspell-word (&optional following)
969 "Spell check a word."
970 (interactive (list ispell-following-word))
972 ;; use the correct dictionary
973 (flyspell-accept-buffer-local-defs)
974 (let* ((cursor-location (point))
975 (flyspell-word (flyspell-get-word following))
977 (if (or (eq flyspell-word nil)
978 (and (fboundp flyspell-generic-check-word-p)
979 (not (funcall flyspell-generic-check-word-p))))
982 ;; destructure return flyspell-word info list.
983 (setq start (car (cdr flyspell-word))
984 end (car (cdr (cdr flyspell-word)))
985 word (car flyspell-word))
986 ;; before checking in the directory, we check for doublons.
988 ((and (or (not (eq ispell-parser 'tex))
989 (and (> start (point-min))
990 (not (memq (char-after (1- start)) '(?\} ?\\)))))
991 flyspell-mark-duplications-flag
993 (goto-char (1- start))
994 (let ((p (flyspell-word-search-backward
996 (- start (1+ (- end start))))))
997 (and p (/= p (1- start))))))
998 ;; yes, this is a doublon
999 (flyspell-highlight-incorrect-region start end 'doublon)
1001 ((and (eq flyspell-word-cache-start start)
1002 (eq flyspell-word-cache-end end)
1003 (string-equal flyspell-word-cache-word word))
1004 ;; this word had been already checked, we skip
1005 flyspell-word-cache-result)
1006 ((and (eq ispell-parser 'tex)
1007 (flyspell-tex-command-p flyspell-word))
1008 ;; this is a correct word (because a tex command)
1009 (flyspell-unhighlight-at start)
1011 (flyspell-unhighlight-at (- end 1)))
1014 ;; we setup the cache
1015 (setq flyspell-word-cache-start start)
1016 (setq flyspell-word-cache-end end)
1017 (setq flyspell-word-cache-word word)
1018 ;; now check spelling of word.
1019 (ispell-send-string "%\n")
1020 ;; put in verbose mode
1021 (ispell-send-string (concat "^" word "\n"))
1022 ;; we mark the ispell process so it can be killed
1023 ;; when emacs is exited without query
1024 (set-process-query-on-exit-flag ispell-process nil)
1025 ;; Wait until ispell has processed word. Since this code is often
1026 ;; executed from post-command-hook but the ispell process may not
1027 ;; be responsive, it's important to make sure we re-enable C-g.
1030 (accept-process-output ispell-process)
1031 (not (string= "" (car ispell-filter))))))
1032 ;; (ispell-send-string "!\n")
1033 ;; back to terse mode.
1034 (setq ispell-filter (cdr ispell-filter))
1035 (if (consp ispell-filter)
1036 (setq poss (ispell-parse-output (car ispell-filter))))
1037 (let ((res (cond ((eq poss t)
1039 (setq flyspell-word-cache-result t)
1040 (flyspell-unhighlight-at start)
1042 (flyspell-unhighlight-at (- end 1)))
1044 ((and (stringp poss) flyspell-highlight-flag)
1046 (setq flyspell-word-cache-result t)
1047 (flyspell-unhighlight-at start)
1049 (flyspell-unhighlight-at (- end 1)))
1052 (setq flyspell-word-cache-result t)
1053 (flyspell-unhighlight-at start)
1055 (flyspell-unhighlight-at (- end 1)))
1057 ((or (and (< flyspell-duplicate-distance 0)
1060 (flyspell-word-search-backward
1065 (flyspell-word-search-forward
1068 (and (> flyspell-duplicate-distance 0)
1071 (flyspell-word-search-backward
1074 flyspell-duplicate-distance)))
1077 (flyspell-word-search-forward
1080 flyspell-duplicate-distance))))))
1081 ;; This is a misspelled word which occurs
1082 ;; twice within flyspell-duplicate-distance.
1083 (setq flyspell-word-cache-result nil)
1084 (if flyspell-highlight-flag
1085 (flyspell-highlight-duplicate-region
1087 (message "duplicate `%s'" word))
1090 (setq flyspell-word-cache-result nil)
1091 ;; incorrect highlight the location
1092 (if flyspell-highlight-flag
1093 (flyspell-highlight-incorrect-region
1095 (flyspell-notify-misspell word poss))
1097 ;; return to original location
1098 (goto-char cursor-location)
1099 (if ispell-quit (setq ispell-quit nil))
1102 ;;*---------------------------------------------------------------------*/
1103 ;;* flyspell-tex-math-initialized ... */
1104 ;;*---------------------------------------------------------------------*/
1105 (defvar flyspell-tex-math-initialized nil)
1107 ;;*---------------------------------------------------------------------*/
1108 ;;* flyspell-math-tex-command-p ... */
1109 ;;* ------------------------------------------------------------- */
1110 ;;* This function uses the texmathp package to check if (point) */
1111 ;;* is within a tex command. In order to avoid using */
1112 ;;* condition-case each time we use the variable */
1113 ;;* flyspell-tex-math-initialized to make a special case the first */
1114 ;;* time that function is called. */
1115 ;;*---------------------------------------------------------------------*/
1116 (defun flyspell-math-tex-command-p ()
1117 (when (fboundp 'texmathp)
1119 (flyspell-check-tex-math-command
1121 ((eq flyspell-tex-math-initialized t)
1123 ((eq flyspell-tex-math-initialized 'error)
1126 (setq flyspell-tex-math-initialized t)
1130 (setq flyspell-tex-math-initialized 'error)
1133 ;;*---------------------------------------------------------------------*/
1134 ;;* flyspell-tex-command-p ... */
1135 ;;*---------------------------------------------------------------------*/
1136 (defun flyspell-tex-command-p (word)
1137 "Return t if WORD is a TeX command."
1139 (let ((b (car (cdr word))))
1140 (and (re-search-backward "\\\\" (- (point) 100) t)
1141 (or (= (match-end 0) b)
1142 (and (goto-char (match-end 0))
1143 (looking-at flyspell-tex-command-regexp)
1144 (>= (match-end 0) b))))))
1145 (flyspell-math-tex-command-p)))
1147 ;;*---------------------------------------------------------------------*/
1148 ;;* flyspell-casechars-cache ... */
1149 ;;*---------------------------------------------------------------------*/
1150 (defvar flyspell-casechars-cache nil)
1151 (defvar flyspell-ispell-casechars-cache nil)
1152 (make-variable-buffer-local 'flyspell-casechars-cache)
1153 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1155 ;;*---------------------------------------------------------------------*/
1156 ;;* flyspell-get-casechars ... */
1157 ;;*---------------------------------------------------------------------*/
1158 (defun flyspell-get-casechars ()
1159 "This function builds a string that is the regexp of word chars.
1160 In order to avoid one useless string construction,
1161 this function changes the last char of the `ispell-casechars' string."
1162 (let ((ispell-casechars (ispell-get-casechars)))
1164 ((eq ispell-parser 'tex)
1165 (setq flyspell-ispell-casechars-cache ispell-casechars)
1166 (setq flyspell-casechars-cache
1167 (concat (substring ispell-casechars
1169 (- (length ispell-casechars) 1))
1171 flyspell-casechars-cache)
1173 (setq flyspell-ispell-casechars-cache ispell-casechars)
1174 (setq flyspell-casechars-cache ispell-casechars)
1175 flyspell-casechars-cache))))
1177 ;;*---------------------------------------------------------------------*/
1178 ;;* flyspell-get-not-casechars-cache ... */
1179 ;;*---------------------------------------------------------------------*/
1180 (defvar flyspell-not-casechars-cache nil)
1181 (defvar flyspell-ispell-not-casechars-cache nil)
1182 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1183 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1185 ;;*---------------------------------------------------------------------*/
1186 ;;* flyspell-get-not-casechars ... */
1187 ;;*---------------------------------------------------------------------*/
1188 (defun flyspell-get-not-casechars ()
1189 "This function builds a string that is the regexp of non-word chars."
1190 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1192 ((eq ispell-parser 'tex)
1193 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1194 (setq flyspell-not-casechars-cache
1195 (concat (substring ispell-not-casechars
1197 (- (length ispell-not-casechars) 1))
1199 flyspell-not-casechars-cache)
1201 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1202 (setq flyspell-not-casechars-cache ispell-not-casechars)
1203 flyspell-not-casechars-cache))))
1205 ;;*---------------------------------------------------------------------*/
1206 ;;* flyspell-get-word ... */
1207 ;;*---------------------------------------------------------------------*/
1208 (defun flyspell-get-word (following &optional extra-otherchars)
1209 "Return the word for spell-checking according to Ispell syntax.
1210 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1211 is non-nil when called interactively, then the following word
1212 \(rather than preceding\) is checked when the cursor is not over a word.
1213 Optional second argument contains otherchars that can be included in word
1216 Word syntax described by `flyspell-dictionary-alist' (which see)."
1217 (let* ((flyspell-casechars (flyspell-get-casechars))
1218 (flyspell-not-casechars (flyspell-get-not-casechars))
1219 (ispell-otherchars (ispell-get-otherchars))
1220 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1221 (word-regexp (concat flyspell-casechars
1223 (if (not (string= "" ispell-otherchars))
1224 (concat ispell-otherchars "?"))
1225 (if extra-otherchars
1226 (concat extra-otherchars "?"))
1229 (if (or ispell-many-otherchars-p
1235 (if (not (looking-at flyspell-casechars))
1237 (re-search-forward flyspell-casechars nil t)
1238 (re-search-backward flyspell-casechars nil t)))
1239 ;; move to front of word
1240 (re-search-backward flyspell-not-casechars nil 'start)
1241 (while (and (or (and (not (string= "" ispell-otherchars))
1242 (looking-at ispell-otherchars))
1243 (and extra-otherchars (looking-at extra-otherchars)))
1245 (or (not did-it-once)
1246 ispell-many-otherchars-p)
1247 (not (eq prevpt (point))))
1248 (if (and extra-otherchars (looking-at extra-otherchars))
1251 (if (looking-at flyspell-casechars)
1252 (re-search-backward flyspell-not-casechars nil 'move)))
1256 (if (looking-at flyspell-casechars)
1257 (re-search-backward flyspell-not-casechars nil 'move)
1258 (backward-char -1))))
1259 ;; Now mark the word and save to string.
1260 (if (not (re-search-forward word-regexp nil t))
1263 (setq start (match-beginning 0)
1265 word (buffer-substring-no-properties start end))
1266 (list word start end)))))
1268 ;;*---------------------------------------------------------------------*/
1269 ;;* flyspell-small-region ... */
1270 ;;*---------------------------------------------------------------------*/
1271 (defun flyspell-small-region (beg end)
1272 "Flyspell text between BEG and END."
1280 (while (< (point) end)
1281 (if (and flyspell-issue-message-flag (= count 100))
1283 (message "Spell Checking...%d%%"
1284 (* 100 (/ (float (- (point) beg)) (- end beg))))
1286 (setq count (+ 1 count)))
1289 (let ((cur (point)))
1291 (if (and (< (point) end) (> (point) (+ cur 1)))
1292 (backward-char 1)))))
1294 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1297 ;;*---------------------------------------------------------------------*/
1298 ;;* flyspell-external-ispell-process ... */
1299 ;;*---------------------------------------------------------------------*/
1300 (defvar flyspell-external-ispell-process '()
1301 "The external Flyspell Ispell process.")
1303 ;;*---------------------------------------------------------------------*/
1304 ;;* flyspell-external-ispell-buffer ... */
1305 ;;*---------------------------------------------------------------------*/
1306 (defvar flyspell-external-ispell-buffer '())
1307 (defvar flyspell-large-region-buffer '())
1308 (defvar flyspell-large-region-beg (point-min))
1309 (defvar flyspell-large-region-end (point-max))
1311 ;;*---------------------------------------------------------------------*/
1312 ;;* flyspell-external-point-words ... */
1313 ;;*---------------------------------------------------------------------*/
1314 (defun flyspell-external-point-words ()
1315 "Mark words from a buffer listing incorrect words in order of appearance.
1316 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1317 \(We finish by killing that buffer and setting the variable to nil.)
1318 The buffer to mark them in is `flyspell-large-region-buffer'."
1319 (let (words-not-found
1320 (ispell-otherchars (ispell-get-otherchars))
1321 (buffer-scan-pos flyspell-large-region-beg))
1322 (with-current-buffer flyspell-external-ispell-buffer
1323 (goto-char (point-min))
1324 ;; Loop over incorrect words, in the order they were reported,
1325 ;; which is also the order they appear in the buffer being checked.
1326 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1327 ;; Bind WORD to the next one.
1328 (let ((word (match-string 1)) (wordpos (point)))
1329 ;; Here there used to be code to see if WORD is the same
1330 ;; as the previous iteration, and count the number of consecutive
1331 ;; identical words, and the loop below would search for that many.
1332 ;; That code seemed to be incorrect, and on principle, should
1333 ;; be unnecessary too. -- rms.
1334 (if flyspell-issue-message-flag
1335 (message "Spell Checking...%d%% [%s]"
1336 (* 100 (/ (float (point)) (point-max)))
1338 (with-current-buffer flyspell-large-region-buffer
1339 (goto-char buffer-scan-pos)
1341 ;; Iterate on string search until string is found as word,
1344 (if (search-forward word
1345 flyspell-large-region-end t)
1348 ;; Move back into the match
1349 ;; so flyspell-get-word will find it.
1351 (flyspell-get-word nil)))
1352 (found (car found-list))
1353 (found-length (length found))
1354 (misspell-length (length word)))
1356 ;; Size matches, we really found it.
1357 (= found-length misspell-length)
1358 ;; Matches as part of a boundary-char separated word
1360 (split-string found ispell-otherchars))
1361 ;; Misspelling has higher length than
1362 ;; what flyspell considers the
1363 ;; word. Caused by boundary-chars
1364 ;; mismatch. Validating seems safe.
1365 (< found-length misspell-length)
1366 ;; ispell treats beginning of some TeX
1367 ;; commands as nroff control sequences
1368 ;; and strips them in the list of
1369 ;; misspelled words thus giving a
1370 ;; non-existent word. Skip if ispell
1371 ;; is used, string is a TeX command
1372 ;; (char before beginning of word is
1373 ;; backslash) and none of the previous
1375 (and (not ispell-really-aspell)
1377 (goto-char (- (nth 1 found-list) 1))
1378 (if (looking-at "[\\]" )
1383 ;; Search for next misspelled word will begin from
1384 ;; end of last validated match.
1385 (setq buffer-scan-pos (point))))
1386 ;; Record if misspelling is not found and try new one
1387 (add-to-list 'words-not-found
1388 (concat " -> " word " - "
1389 (int-to-string wordpos)))
1390 (setq keep nil)))))))
1392 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1393 ;; Warn about not found misspellings
1394 (dolist (word words-not-found)
1395 (message "%s: word not found" word))
1396 ;; Kill and forget the buffer with the list of incorrect words.
1397 (kill-buffer flyspell-external-ispell-buffer)
1398 (setq flyspell-external-ispell-buffer nil)))
1400 ;;*---------------------------------------------------------------------*/
1401 ;;* flyspell-process-localwords ... */
1402 ;;* ------------------------------------------------------------- */
1403 ;;* This function is used to prevent marking of words explicitly */
1404 ;;* declared correct. */
1405 ;;*---------------------------------------------------------------------*/
1406 (defun flyspell-process-localwords (misspellings-buffer)
1408 (ispell-casechars (ispell-get-casechars)))
1409 ;; Get localwords from the original buffer
1411 (goto-char (point-min))
1412 ;; Localwords parsing copied from ispell.el.
1413 (while (search-forward ispell-words-keyword nil t)
1414 (let ((end (save-excursion (end-of-line) (point)))
1416 ;; buffer-local words separated by a space, and can contain
1417 ;; any character other than a space. Not rigorous enough.
1418 (while (re-search-forward " *\\([^ ]+\\)" end t)
1419 (setq string (buffer-substring-no-properties (match-beginning 1)
1421 ;; This can fail when string contains a word with invalid chars.
1422 ;; Error handling needs to be added between Ispell and Emacs.
1423 (if (and (< 1 (length string))
1424 (equal 0 (string-match ispell-casechars string)))
1425 (push string localwords))))))
1426 ;; Remove localwords matches from misspellings-buffer.
1427 ;; The usual mechanism of communicating the local words to ispell
1428 ;; does not affect the special ispell process used by
1429 ;; flyspell-large-region.
1430 (with-current-buffer misspellings-buffer
1432 (dolist (word localwords)
1433 (goto-char (point-min))
1434 (let ((regexp (concat "^" word "\n")))
1435 (while (re-search-forward regexp nil t)
1436 (delete-region (match-beginning 0) (match-end 0)))))))))
1438 ;;*---------------------------------------------------------------------*/
1439 ;;* flyspell-large-region ... */
1440 ;;*---------------------------------------------------------------------*/
1441 (defun flyspell-large-region (beg end)
1442 (let* ((curbuf (current-buffer))
1443 (buffer (get-buffer-create "*flyspell-region*")))
1444 (setq flyspell-external-ispell-buffer buffer)
1445 (setq flyspell-large-region-buffer curbuf)
1446 (setq flyspell-large-region-beg beg)
1447 (setq flyspell-large-region-end end)
1448 (flyspell-accept-buffer-local-defs)
1451 ;; this is done, we can start checking...
1452 (if flyspell-issue-message-flag (message "Checking region..."))
1454 (ispell-check-version)
1455 (let ((c (apply 'call-process-region beg
1461 (if ispell-really-aspell "list" "-l")
1463 ;; Local dictionary becomes the global dictionary in use.
1464 (if ispell-local-dictionary
1465 (setq ispell-dictionary ispell-local-dictionary))
1466 (setq args (ispell-get-ispell-args))
1467 (if ispell-dictionary ; use specified dictionary
1469 (append (list "-d" ispell-dictionary) args)))
1470 (if ispell-personal-dictionary ; use specified pers dict
1475 ispell-personal-dictionary)))))
1476 (setq args (append args ispell-extra-args))
1480 (flyspell-process-localwords buffer)
1481 (with-current-buffer curbuf
1482 (flyspell-delete-region-overlays beg end))
1483 (flyspell-external-point-words))
1484 (error "Can't check region...")))))
1486 ;;*---------------------------------------------------------------------*/
1487 ;;* flyspell-region ... */
1488 ;;* ------------------------------------------------------------- */
1489 ;;* Because `ispell -a' is too slow, it is not possible to use */
1490 ;;* it on large region. Then, when ispell is invoked on a large */
1491 ;;* text region, a new `ispell -l' process is spawned. The */
1492 ;;* pointed out words are then searched in the region a checked with */
1493 ;;* regular flyspell means. */
1494 ;;*---------------------------------------------------------------------*/
1496 (defun flyspell-region (beg end)
1497 "Flyspell text between BEG and END."
1506 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1507 (flyspell-large-region beg end)
1508 (flyspell-small-region beg end)))))
1510 ;;*---------------------------------------------------------------------*/
1511 ;;* flyspell-buffer ... */
1512 ;;*---------------------------------------------------------------------*/
1514 (defun flyspell-buffer ()
1515 "Flyspell whole buffer."
1517 (flyspell-region (point-min) (point-max)))
1519 ;;*---------------------------------------------------------------------*/
1520 ;;* old next error position ... */
1521 ;;*---------------------------------------------------------------------*/
1522 (defvar flyspell-old-buffer-error nil)
1523 (defvar flyspell-old-pos-error nil)
1525 ;;*---------------------------------------------------------------------*/
1526 ;;* flyspell-goto-next-error ... */
1527 ;;*---------------------------------------------------------------------*/
1528 (defun flyspell-goto-next-error ()
1529 "Go to the next previously detected error.
1530 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1535 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1536 (eq pos flyspell-old-pos-error))
1538 (if (= flyspell-old-pos-error max)
1539 ;; goto beginning of buffer
1541 (message "Restarting from beginning of buffer")
1542 (goto-char (point-min)))
1544 (setq pos (point))))
1545 ;; seek the next error
1546 (while (and (< pos max)
1547 (let ((ovs (overlays-at pos))
1549 (while (and (not r) (consp ovs))
1550 (if (flyspell-overlay-p (car ovs))
1552 (setq ovs (cdr ovs))))
1554 (setq pos (1+ pos)))
1555 ;; save the current location for next invocation
1556 (setq flyspell-old-pos-error pos)
1557 (setq flyspell-old-buffer-error (current-buffer))
1560 (message "No more miss-spelled word!"))))
1562 ;;*---------------------------------------------------------------------*/
1563 ;;* flyspell-overlay-p ... */
1564 ;;*---------------------------------------------------------------------*/
1565 (defun flyspell-overlay-p (o)
1566 "A predicate that return true iff O is an overlay used by flyspell."
1567 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1569 ;;*---------------------------------------------------------------------*/
1570 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1571 ;;* ------------------------------------------------------------- */
1572 ;;* Remove overlays introduced by flyspell. */
1573 ;;*---------------------------------------------------------------------*/
1574 (defun flyspell-delete-region-overlays (beg end)
1575 "Delete overlays used by flyspell in a given region."
1576 (remove-overlays beg end 'flyspell-overlay t))
1579 (defun flyspell-delete-all-overlays ()
1580 "Delete all the overlays used by flyspell."
1581 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1583 ;;*---------------------------------------------------------------------*/
1584 ;;* flyspell-unhighlight-at ... */
1585 ;;*---------------------------------------------------------------------*/
1586 (defun flyspell-unhighlight-at (pos)
1587 "Remove the flyspell overlay that are located at POS."
1588 (if flyspell-persistent-highlight
1589 (let ((overlays (overlays-at pos)))
1590 (while (consp overlays)
1591 (if (flyspell-overlay-p (car overlays))
1592 (delete-overlay (car overlays)))
1593 (setq overlays (cdr overlays))))
1594 (if (flyspell-overlay-p flyspell-overlay)
1595 (delete-overlay flyspell-overlay))))
1597 ;;*---------------------------------------------------------------------*/
1598 ;;* flyspell-properties-at-p ... */
1599 ;;* ------------------------------------------------------------- */
1600 ;;* Is there an highlight properties at position pos? */
1601 ;;*---------------------------------------------------------------------*/
1602 (defun flyspell-properties-at-p (pos)
1603 "Return t if there is a text property at POS, not counting `local-map'.
1604 If variable `flyspell-highlight-properties' is set to nil,
1605 text with properties are not checked. This function is used to discover
1606 if the character at POS has any other property."
1607 (let ((prop (text-properties-at pos))
1609 (while (and keep (consp prop))
1610 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1611 (setq prop (cdr (cdr prop)))
1615 ;;*---------------------------------------------------------------------*/
1616 ;;* make-flyspell-overlay ... */
1617 ;;*---------------------------------------------------------------------*/
1618 (defun make-flyspell-overlay (beg end face mouse-face)
1619 "Allocate an overlay to highlight an incorrect word.
1620 BEG and END specify the range in the buffer of that word.
1621 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1623 (let ((overlay (make-overlay beg end nil t nil)))
1624 (overlay-put overlay 'face face)
1625 (overlay-put overlay 'mouse-face mouse-face)
1626 (overlay-put overlay 'flyspell-overlay t)
1627 (overlay-put overlay 'evaporate t)
1628 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1629 (overlay-put overlay 'keymap flyspell-mouse-map)
1630 (when (eq face 'flyspell-incorrect)
1631 (and (stringp flyspell-before-incorrect-word-string)
1632 (overlay-put overlay 'before-string
1633 flyspell-before-incorrect-word-string))
1634 (and (stringp flyspell-after-incorrect-word-string)
1635 (overlay-put overlay 'after-string
1636 flyspell-after-incorrect-word-string)))
1639 ;;*---------------------------------------------------------------------*/
1640 ;;* flyspell-highlight-incorrect-region ... */
1641 ;;*---------------------------------------------------------------------*/
1642 (defun flyspell-highlight-incorrect-region (beg end poss)
1643 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1644 POSS is usually a list of possible spelling/correction lists,
1645 as returned by `ispell-parse-output'.
1646 It can also be the symbol `doublon', in the case where the word
1647 is itself incorrect, but suspiciously repeated."
1648 (let ((inhibit-read-only t))
1649 (unless (run-hook-with-args-until-success
1650 'flyspell-incorrect-hook beg end poss)
1651 (if (or flyspell-highlight-properties
1652 (not (flyspell-properties-at-p beg)))
1654 ;; we cleanup all the overlay that are in the region, not
1655 ;; beginning at the word start position
1656 (if (< (1+ beg) end)
1657 (let ((os (overlays-in (1+ beg) end)))
1659 (if (flyspell-overlay-p (car os))
1660 (delete-overlay (car os)))
1661 (setq os (cdr os)))))
1662 ;; we cleanup current overlay at the same position
1663 (flyspell-unhighlight-at beg)
1664 ;; now we can use a new overlay
1665 (setq flyspell-overlay
1666 (make-flyspell-overlay
1667 beg end 'flyspell-incorrect 'highlight)))))))
1669 ;;*---------------------------------------------------------------------*/
1670 ;;* flyspell-highlight-duplicate-region ... */
1671 ;;*---------------------------------------------------------------------*/
1672 (defun flyspell-highlight-duplicate-region (beg end poss)
1673 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1674 POSS is a list of possible spelling/correction lists,
1675 as returned by `ispell-parse-output'."
1676 (let ((inhibit-read-only t))
1677 (unless (run-hook-with-args-until-success
1678 'flyspell-incorrect-hook beg end poss)
1679 (if (or flyspell-highlight-properties
1680 (not (flyspell-properties-at-p beg)))
1682 ;; we cleanup current overlay at the same position
1683 (flyspell-unhighlight-at beg)
1684 ;; now we can use a new overlay
1685 (setq flyspell-overlay
1686 (make-flyspell-overlay beg end
1690 ;;*---------------------------------------------------------------------*/
1691 ;;* flyspell-auto-correct-cache ... */
1692 ;;*---------------------------------------------------------------------*/
1693 (defvar flyspell-auto-correct-pos nil)
1694 (defvar flyspell-auto-correct-region nil)
1695 (defvar flyspell-auto-correct-ring nil)
1696 (defvar flyspell-auto-correct-word nil)
1697 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1698 (make-variable-buffer-local 'flyspell-auto-correct-region)
1699 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1700 (make-variable-buffer-local 'flyspell-auto-correct-word)
1702 ;;*---------------------------------------------------------------------*/
1703 ;;* flyspell-check-previous-highlighted-word ... */
1704 ;;*---------------------------------------------------------------------*/
1705 (defun flyspell-check-previous-highlighted-word (&optional arg)
1706 "Correct the closer misspelled word.
1707 This function scans a mis-spelled word before the cursor. If it finds one
1708 it proposes replacement for that word. With prefix arg, count that many
1709 misspelled words backwards."
1711 (let ((pos1 (point))
1713 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1716 (while (and (setq pos (previous-overlay-change pos))
1719 (if (> pos (point-min))
1721 (setq ovs (overlays-at (1- pos)))
1724 (setq ovs (cdr ovs))
1725 (if (and (flyspell-overlay-p ov)
1726 (= 0 (setq arg (1- arg))))
1727 (throw 'exit t)))))))
1731 (error "No word to correct before point"))))
1733 ;;*---------------------------------------------------------------------*/
1734 ;;* flyspell-display-next-corrections ... */
1735 ;;*---------------------------------------------------------------------*/
1736 (defun flyspell-display-next-corrections (corrections)
1737 (let ((string "Corrections:")
1740 (while (< (length string) 80)
1741 (if (equal (car l) flyspell-auto-correct-word)
1742 (setq pos (cons (+ 1 (length string)) pos)))
1743 (setq string (concat string " " (car l)))
1746 (let ((num (car pos)))
1747 (put-text-property num
1748 (+ num (length flyspell-auto-correct-word))
1749 'face 'flyspell-incorrect
1751 (setq pos (cdr pos)))
1752 (if (fboundp 'display-message)
1753 (display-message 'no-log string)
1754 (message "%s" string))))
1756 ;;*---------------------------------------------------------------------*/
1757 ;;* flyspell-abbrev-table ... */
1758 ;;*---------------------------------------------------------------------*/
1759 (defun flyspell-abbrev-table ()
1760 (if flyspell-use-global-abbrev-table-p
1762 (or local-abbrev-table global-abbrev-table)))
1764 ;;*---------------------------------------------------------------------*/
1765 ;;* flyspell-define-abbrev ... */
1766 ;;*---------------------------------------------------------------------*/
1767 (defun flyspell-define-abbrev (name expansion)
1768 (let ((table (flyspell-abbrev-table)))
1770 (define-abbrev table name expansion))))
1772 ;;*---------------------------------------------------------------------*/
1773 ;;* flyspell-auto-correct-word ... */
1774 ;;*---------------------------------------------------------------------*/
1775 (defun flyspell-auto-correct-word ()
1776 "Correct the current word.
1777 This command proposes various successive corrections for the current word."
1780 (old-max (point-max)))
1781 ;; use the correct dictionary
1782 (flyspell-accept-buffer-local-defs)
1783 (if (and (eq flyspell-auto-correct-pos pos)
1784 (consp flyspell-auto-correct-region))
1785 ;; we have already been using the function at the same location
1786 (let* ((start (car flyspell-auto-correct-region))
1787 (len (cdr flyspell-auto-correct-region)))
1788 (flyspell-unhighlight-at start)
1789 (delete-region start (+ start len))
1790 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1791 (let* ((word (car flyspell-auto-correct-ring))
1792 (len (length word)))
1793 (rplacd flyspell-auto-correct-region len)
1795 (if flyspell-abbrev-p
1796 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1797 flyspell-auto-correct-word)
1798 (flyspell-change-abbrev (flyspell-abbrev-table)
1799 flyspell-auto-correct-word
1801 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1802 (funcall flyspell-insert-function word)
1804 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1805 (flyspell-ajust-cursor-point pos (point) old-max)
1806 (setq flyspell-auto-correct-pos (point)))
1807 ;; fetch the word to be checked
1808 (let ((word (flyspell-get-word nil)))
1810 (let ((start (car (cdr word)))
1811 (end (car (cdr (cdr word))))
1814 (setq flyspell-auto-correct-word word)
1815 ;; now check spelling of word.
1816 (ispell-send-string "%\n") ;put in verbose mode
1817 (ispell-send-string (concat "^" word "\n"))
1818 ;; wait until ispell has processed word.
1820 (accept-process-output ispell-process)
1821 (not (string= "" (car ispell-filter)))))
1822 (setq ispell-filter (cdr ispell-filter))
1823 (if (consp ispell-filter)
1824 (setq poss (ispell-parse-output (car ispell-filter))))
1826 ((or (eq poss t) (stringp poss))
1827 ;; don't correct word
1831 (error "Ispell: error in Ispell process"))
1833 ;; the word is incorrect, we have to propose a replacement
1834 (let ((replacements (if flyspell-sort-corrections
1835 (sort (car (cdr (cdr poss))) 'string<)
1836 (car (cdr (cdr poss))))))
1837 (setq flyspell-auto-correct-region nil)
1838 (if (consp replacements)
1840 (let ((replace (car replacements)))
1841 (let ((new-word replace))
1842 (if (not (equal new-word (car poss)))
1844 ;; the save the current replacements
1845 (setq flyspell-auto-correct-region
1846 (cons start (length new-word)))
1847 (let ((l replacements))
1848 (while (consp (cdr l))
1850 (rplacd l (cons (car poss) replacements)))
1851 (setq flyspell-auto-correct-ring
1853 (flyspell-unhighlight-at start)
1854 (delete-region start end)
1855 (funcall flyspell-insert-function new-word)
1856 (if flyspell-abbrev-p
1857 (if (flyspell-already-abbrevp
1858 (flyspell-abbrev-table) word)
1859 (flyspell-change-abbrev
1860 (flyspell-abbrev-table)
1863 (flyspell-define-abbrev word
1866 (flyspell-display-next-corrections
1867 (cons new-word flyspell-auto-correct-ring))
1868 (flyspell-ajust-cursor-point pos
1871 (setq flyspell-auto-correct-pos (point))
1872 (ispell-pdict-save t)))))))
1874 ;;*---------------------------------------------------------------------*/
1875 ;;* flyspell-auto-correct-previous-pos ... */
1876 ;;*---------------------------------------------------------------------*/
1877 (defvar flyspell-auto-correct-previous-pos nil
1878 "Holds the start of the first incorrect word before point.")
1880 ;;*---------------------------------------------------------------------*/
1881 ;;* flyspell-auto-correct-previous-hook ... */
1882 ;;*---------------------------------------------------------------------*/
1883 (defun flyspell-auto-correct-previous-hook ()
1884 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1885 Sets `flyspell-auto-correct-previous-pos' to nil"
1887 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1888 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1889 (setq flyspell-auto-correct-previous-pos nil)))
1891 ;;*---------------------------------------------------------------------*/
1892 ;;* flyspell-auto-correct-previous-word ... */
1893 ;;*---------------------------------------------------------------------*/
1894 (defun flyspell-auto-correct-previous-word (position)
1895 "Auto correct the first mispelled word that occurs before point.
1896 But don't look beyond what's visible on the screen."
1901 (move-to-window-line 0)
1903 (move-to-window-line -1)
1907 (narrow-to-region top bot)
1908 (overlay-recenter (point))
1910 (add-hook 'pre-command-hook
1911 (function flyspell-auto-correct-previous-hook) t t)
1913 (unless flyspell-auto-correct-previous-pos
1914 ;; only reset if a new overlay exists
1915 (setq flyspell-auto-correct-previous-pos nil)
1917 (let ((overlay-list (overlays-in (point-min) position))
1918 (new-overlay 'dummy-value))
1920 ;; search for previous (new) flyspell overlay
1921 (while (and new-overlay
1922 (or (not (flyspell-overlay-p new-overlay))
1923 ;; check if its face has changed
1924 (not (eq (get-char-property
1925 (overlay-start new-overlay) 'face)
1926 'flyspell-incorrect))))
1927 (setq new-overlay (car-safe overlay-list))
1928 (setq overlay-list (cdr-safe overlay-list)))
1930 ;; if nothing new exits new-overlay should be nil
1931 (if new-overlay ;; the length of the word may change so go to the start
1932 (setq flyspell-auto-correct-previous-pos
1933 (overlay-start new-overlay)))))
1935 (when flyspell-auto-correct-previous-pos
1937 (goto-char flyspell-auto-correct-previous-pos)
1938 (let ((ispell-following-word t)) ;; point is at start
1939 (if (numberp flyspell-auto-correct-previous-pos)
1940 (goto-char flyspell-auto-correct-previous-pos))
1941 (flyspell-auto-correct-word))
1942 ;; the point may have moved so reset this
1943 (setq flyspell-auto-correct-previous-pos (point))))))))
1945 ;;*---------------------------------------------------------------------*/
1946 ;;* flyspell-correct-word ... */
1947 ;;*---------------------------------------------------------------------*/
1948 (defun flyspell-correct-word (event)
1949 "Pop up a menu of possible corrections for a misspelled word.
1950 The word checked is the word at the mouse position."
1952 ;; use the correct dictionary
1953 (flyspell-accept-buffer-local-defs)
1954 ;; retain cursor location (I don't know why but save-excursion here fails).
1955 (let ((save (point)))
1956 (mouse-set-point event)
1957 (let ((cursor-location (point))
1958 (word (flyspell-get-word nil)))
1960 (let ((start (car (cdr word)))
1961 (end (car (cdr (cdr word))))
1964 ;; now check spelling of word.
1965 (ispell-send-string "%\n") ;put in verbose mode
1966 (ispell-send-string (concat "^" word "\n"))
1967 ;; wait until ispell has processed word
1969 (accept-process-output ispell-process)
1970 (not (string= "" (car ispell-filter)))))
1971 (setq ispell-filter (cdr ispell-filter))
1972 (if (consp ispell-filter)
1973 (setq poss (ispell-parse-output (car ispell-filter))))
1975 ((or (eq poss t) (stringp poss))
1976 ;; don't correct word
1980 (error "Ispell: error in Ispell process"))
1982 (flyspell-xemacs-popup
1983 poss word cursor-location start end save))
1985 ;; The word is incorrect, we have to propose a replacement.
1986 (flyspell-do-correct (flyspell-emacs-popup event poss word)
1987 poss word cursor-location start end save)))
1988 (ispell-pdict-save t))))))
1990 ;;*---------------------------------------------------------------------*/
1991 ;;* flyspell-do-correct ... */
1992 ;;*---------------------------------------------------------------------*/
1993 (defun flyspell-do-correct (replace poss word cursor-location start end save)
1994 "The popup menu callback."
1995 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
1996 ;; it instead right after calling the function.
1997 (cond ((eq replace 'ignore)
2002 (ispell-send-string (concat "*" word "\n"))
2003 ;; This was added only to the XEmacs side in revision 1.18 of
2004 ;; flyspell. I assume its absence on the Emacs side was an
2005 ;; oversight. --Stef
2006 (ispell-send-string "#\n")
2007 (flyspell-unhighlight-at cursor-location)
2008 (setq ispell-pdict-modified-p '(t)))
2009 ((or (eq replace 'buffer) (eq replace 'session))
2010 (ispell-send-string (concat "@" word "\n"))
2011 (flyspell-unhighlight-at cursor-location)
2012 (if (null ispell-pdict-modified-p)
2013 (setq ispell-pdict-modified-p
2014 (list ispell-pdict-modified-p)))
2016 (if (eq replace 'buffer)
2017 (ispell-add-per-file-word-list word)))
2019 ;; This was added only to the Emacs side. I assume its absence on
2020 ;; the XEmacs side was an oversight. --Stef
2021 (flyspell-unhighlight-at cursor-location)
2022 (let ((old-max (point-max))
2023 (new-word (if (atom replace)
2026 (cursor-location (+ (- (length word) (- end start))
2028 (unless (equal new-word (car poss))
2029 (delete-region start end)
2031 (funcall flyspell-insert-function new-word)
2032 (if flyspell-abbrev-p
2033 (flyspell-define-abbrev word new-word)))
2034 ;; In the original Emacs code, this was only called in the body
2035 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2036 (flyspell-ajust-cursor-point save cursor-location old-max)))
2041 ;;*---------------------------------------------------------------------*/
2042 ;;* flyspell-ajust-cursor-point ... */
2043 ;;*---------------------------------------------------------------------*/
2044 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2045 (if (>= save cursor-location)
2046 (let ((new-pos (+ save (- (point-max) old-max))))
2048 ((< new-pos (point-min))
2050 ((> new-pos (point-max))
2055 ;;*---------------------------------------------------------------------*/
2056 ;;* flyspell-emacs-popup ... */
2057 ;;*---------------------------------------------------------------------*/
2058 (defun flyspell-emacs-popup (event poss word)
2059 "The Emacs popup menu."
2061 (let* ((mouse-pos (mouse-position))
2062 (mouse-pos (if (nth 1 mouse-pos)
2064 (set-mouse-position (car mouse-pos)
2065 (/ (frame-width) 2) 2)
2067 (setq event (list (list (car (cdr mouse-pos))
2068 (1+ (cdr (cdr mouse-pos))))
2070 (let* ((corrects (if flyspell-sort-corrections
2071 (sort (car (cdr (cdr poss))) 'string<)
2072 (car (cdr (cdr poss)))))
2073 (cor-menu (if (consp corrects)
2074 (mapcar (lambda (correct)
2075 (list correct correct))
2078 (affix (car (cdr (cdr (cdr poss)))))
2079 (base-menu (let ((save (if (consp affix)
2081 (list (concat "Save affix: " (car affix))
2083 '("Accept (session)" session)
2084 '("Accept (buffer)" buffer))
2085 '(("Save word" save)
2086 ("Accept (session)" session)
2087 ("Accept (buffer)" buffer)))))
2088 (if (consp cor-menu)
2089 (append cor-menu (cons "" save))
2091 (menu (cons "flyspell correction menu" base-menu)))
2092 (car (x-popup-menu event
2093 (list (format "%s [%s]" word (or ispell-local-dictionary
2097 ;;*---------------------------------------------------------------------*/
2098 ;;* flyspell-xemacs-popup ... */
2099 ;;*---------------------------------------------------------------------*/
2100 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2101 "The XEmacs popup menu."
2102 (let* ((corrects (if flyspell-sort-corrections
2103 (sort (car (cdr (cdr poss))) 'string<)
2104 (car (cdr (cdr poss)))))
2105 (cor-menu (if (consp corrects)
2106 (mapcar (lambda (correct)
2108 (list 'flyspell-do-correct
2119 (affix (car (cdr (cdr (cdr poss)))))
2120 (menu (let ((save (if (consp affix)
2122 (concat "Save affix: " (car affix))
2123 (list 'flyspell-do-correct
2134 (list 'flyspell-do-correct
2143 (session (vector "Accept (session)"
2144 (list 'flyspell-do-correct
2153 (buffer (vector "Accept (buffer)"
2154 (list 'flyspell-do-correct
2163 (if (consp cor-menu)
2164 (append cor-menu (list "-" save session buffer))
2165 (list save session buffer)))))
2166 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2170 ;;*---------------------------------------------------------------------*/
2171 ;;* Some example functions for real autocorrecting */
2172 ;;*---------------------------------------------------------------------*/
2173 (defun flyspell-maybe-correct-transposition (beg end poss)
2174 "Check replacements for transposed characters.
2176 If the text between BEG and END is equal to a correction suggested by
2177 Ispell, after transposing two adjacent characters, correct the text,
2180 The third arg POSS is either the symbol 'doublon' or a list of
2181 possible corrections as returned by `ispell-parse-output'.
2183 This function is meant to be added to `flyspell-incorrect-hook'."
2186 (let ((str (buffer-substring beg end))
2187 (i 0) (len (- end beg)) tmp)
2188 (while (< (1+ i) len)
2189 (setq tmp (aref str i))
2190 (aset str i (aref str (1+ i)))
2191 (aset str (1+ i) tmp)
2192 (when (member str (nth 2 poss))
2194 (goto-char (+ beg i 1))
2195 (transpose-chars 1))
2197 (setq tmp (aref str i))
2198 (aset str i (aref str (1+ i)))
2199 (aset str (1+ i) tmp)
2203 (defun flyspell-maybe-correct-doubling (beg end poss)
2204 "Check replacements for doubled characters.
2206 If the text between BEG and END is equal to a correction suggested by
2207 Ispell, after removing a pair of doubled characters, correct the text,
2210 The third arg POSS is either the symbol 'doublon' or a list of
2211 possible corrections as returned by `ispell-parse-output'.
2213 This function is meant to be added to `flyspell-incorrect-hook'."
2216 (let ((str (buffer-substring beg end))
2217 (i 0) (len (- end beg)))
2218 (while (< (1+ i) len)
2219 (when (and (= (aref str i) (aref str (1+ i)))
2220 (member (concat (substring str 0 (1+ i))
2221 (substring str (+ i 2)))
2223 (goto-char (+ beg i))
2229 ;;*---------------------------------------------------------------------*/
2230 ;;* flyspell-already-abbrevp ... */
2231 ;;*---------------------------------------------------------------------*/
2232 (defun flyspell-already-abbrevp (table word)
2233 (let ((sym (abbrev-symbol word table)))
2234 (and sym (symbolp sym))))
2236 ;;*---------------------------------------------------------------------*/
2237 ;;* flyspell-change-abbrev ... */
2238 ;;*---------------------------------------------------------------------*/
2239 (defun flyspell-change-abbrev (table old new)
2240 (set (abbrev-symbol old table) new))
2244 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2245 ;;; flyspell.el ends here