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 ;* Which emacs are we currently running */
61 ;*---------------------------------------------------------------------*/
62 (defvar flyspell-emacs
64 ((string-match "XEmacs" emacs-version
)
68 "The type of Emacs we are currently running.")
70 ;*---------------------------------------------------------------------*/
71 ;* User configuration ... */
72 ;*---------------------------------------------------------------------*/
73 (defcustom flyspell-highlight-flag t
74 "*How Flyspell should indicate misspelled words.
75 Non-nil means use highlight, nil means use minibuffer messages."
79 (defcustom flyspell-mark-duplications-flag t
80 "*Non-nil means Flyspell reports a repeated word as an error."
84 (defcustom flyspell-sort-corrections nil
85 "*Non-nil means, sort the corrections alphabetically before popping them."
90 (defcustom flyspell-duplicate-distance -
1
91 "*The maximum distance for finding duplicates of unrecognized words.
92 This applies to the feature that when a word is not found in the dictionary,
93 if the same spelling occurs elsewhere in the buffer,
94 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
95 This variable specifies how far to search to find such a duplicate.
96 -1 means no limit (search the whole buffer).
97 0 means do not search for duplicate unrecognized spellings."
102 (defcustom flyspell-delay
3
103 "*The number of seconds to wait before checking, after a \"delayed\" command."
107 (defcustom flyspell-persistent-highlight t
108 "*Non-nil means misspelled words remain highlighted until corrected.
109 If this variable is nil, only the most recently detected misspelled word
114 (defcustom flyspell-highlight-properties t
115 "*Non-nil means highlight incorrect words even if a property exists for this word."
119 (defcustom flyspell-default-delayed-commands
120 '(self-insert-command
122 backward-or-forward-delete-char
124 scrollbar-vertical-drag
125 backward-delete-char-untabify
)
126 "The standard list of delayed commands for Flyspell.
127 See `flyspell-delayed-commands'."
130 :type
'(repeat (symbol)))
132 (defcustom flyspell-delayed-commands nil
133 "List of commands that are \"delayed\" for Flyspell mode.
134 After these commands, Flyspell checking is delayed for a short time,
135 whose length is specified by `flyspell-delay'."
137 :type
'(repeat (symbol)))
139 (defcustom flyspell-default-deplacement-commands
144 "The standard list of deplacement commands for Flyspell.
145 See `flyspell-deplacement-commands'."
148 :type
'(repeat (symbol)))
150 (defcustom flyspell-deplacement-commands nil
151 "List of commands that are \"deplacement\" for Flyspell mode.
152 After these commands, Flyspell checking is performed only if the previous
153 command was not the very same command."
156 :type
'(repeat (symbol)))
158 (defcustom flyspell-issue-welcome-flag t
159 "*Non-nil means that Flyspell should display a welcome message when started."
163 (defcustom flyspell-issue-message-flag t
164 "*Non-nil means that Flyspell emits messages when checking words."
168 (defcustom flyspell-incorrect-hook nil
169 "*List of functions to be called when incorrect words are encountered.
170 Each function is given three arguments: the beginning and the end
171 of the incorrect region. The third is either the symbol 'doublon' or the list
172 of possible corrections as returned by `ispell-parse-output'.
174 If any of the functions return non-Nil, the word is not highlighted as
180 (defcustom flyspell-default-dictionary nil
181 "A string that is the name of the default dictionary.
182 This is passed to the `ispell-change-dictionary' when flyspell is started.
183 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
184 when flyspell is started, the value of that variable is used instead
185 of `flyspell-default-dictionary' to select the default dictionary.
186 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
187 Ispell's ultimate default dictionary."
190 :type
'(choice string
(const :tag
"Default" nil
)))
192 (defcustom flyspell-tex-command-regexp
193 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
194 "A string that is the regular expression that matches TeX commands."
199 (defcustom flyspell-check-tex-math-command nil
200 "*Non nil means check even inside TeX math environment.
201 TeX math environments are discovered by the TEXMATHP that implemented
202 inside the texmathp.el Emacs package. That package may be found at:
203 http://strw.leidenuniv.nl/~dominik/Tools"
207 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
208 '("francais" "deutsch8" "norsk")
209 "List of dictionary names that consider `-' as word delimiter."
212 :type
'(repeat (string)))
214 (defcustom flyspell-abbrev-p
216 "*If non-nil, add correction to abbreviation table."
221 (defcustom flyspell-use-global-abbrev-table-p
223 "*If non-nil, prefer global abbrev table to local abbrev table."
228 (defcustom flyspell-mode-line-string
" Fly"
229 "*String displayed on the modeline when flyspell is active.
230 Set this to nil if you don't want a modeline indicator."
232 :type
'(choice string
(const :tag
"None" nil
)))
234 (defcustom flyspell-large-region
1000
235 "*The threshold that determines if a region is small.
236 If the region is smaller than this number of characters,
237 `flyspell-region' checks the words sequentially using regular
238 flyspell methods. Else, if the region is large, a new Ispell process is
241 If `flyspell-large-region' is nil, all regions are treated as small."
244 :type
'(choice number boolean
))
246 (defcustom flyspell-insert-function
(function insert
)
247 "*Function for inserting word by flyspell upon correction."
251 (defcustom flyspell-before-incorrect-word-string nil
252 "String used to indicate an incorrect word starting."
254 :type
'(choice string
(const nil
)))
256 (defcustom flyspell-after-incorrect-word-string nil
257 "String used to indicate an incorrect word ending."
259 :type
'(choice string
(const nil
)))
261 (defcustom flyspell-use-meta-tab t
262 "*Non-nil means that flyspell uses META-TAB to correct word."
266 (defcustom flyspell-auto-correct-binding
268 "The key binding for flyspell auto correction."
271 ;*---------------------------------------------------------------------*/
272 ;* Mode specific options */
273 ;* ------------------------------------------------------------- */
274 ;* Mode specific options enable users to disable flyspell on */
275 ;* certain word depending of the emacs mode. For instance, when */
276 ;* using flyspell with mail-mode add the following expression */
277 ;* in your .emacs file: */
278 ;* (add-hook 'mail-mode */
279 ;* '(lambda () (setq flyspell-generic-check-word-p */
280 ;* 'mail-mode-flyspell-verify))) */
281 ;*---------------------------------------------------------------------*/
282 (defvar flyspell-generic-check-word-p nil
283 "Function providing per-mode customization over which words are flyspelled.
284 Returns t to continue checking, nil otherwise.
285 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
286 property of the major mode name.")
287 (make-variable-buffer-local 'flyspell-generic-check-word-p
)
289 ;*--- mail mode -------------------------------------------------------*/
290 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
291 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
292 (defun mail-mode-flyspell-verify ()
293 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
294 (let ((header-end (save-excursion
295 (goto-char (point-min))
298 (regexp-quote mail-header-separator
)
302 (signature-begin (save-excursion
303 (goto-char (point-max))
304 (re-search-backward message-signature-separator
307 (cond ((< (point) header-end
)
308 (and (save-excursion (beginning-of-line)
309 (looking-at "^Subject:"))
310 (> (point) (match-end 0))))
311 ((> (point) signature-begin
)
316 (not (looking-at "[>}|]\\|To:")))))))
318 ;*--- texinfo mode ----------------------------------------------------*/
319 (put 'texinfo-mode
'flyspell-mode-predicate
'texinfo-mode-flyspell-verify
)
320 (defun texinfo-mode-flyspell-verify ()
321 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
324 (not (looking-at "@"))))
326 ;*--- tex mode --------------------------------------------------------*/
327 (put 'tex-mode
'flyspell-mode-predicate
'tex-mode-flyspell-verify
)
328 (defun tex-mode-flyspell-verify ()
329 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
332 (re-search-backward "^[ \t]*%%%[ \t]+Local" (point-min) t
)))
334 (let ((this (point-marker))
335 (e (progn (end-of-line) (point-marker))))
337 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t
)
338 (and (>= this
(match-beginning 0))
339 (<= this
(match-end 0)) )))))))
341 ;*--- sgml mode -------------------------------------------------------*/
342 (put 'sgml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
343 (put 'html-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
345 (defun sgml-mode-flyspell-verify ()
346 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
348 (let ((this (point-marker))
349 (s (progn (beginning-of-line) (point-marker)))
350 (e (progn (end-of-line) (point-marker))))
353 (and (re-search-forward "[^<]*>" e t
)
354 (= (match-beginning 0) this
)))
357 (and (re-search-backward "<[^>]*" s t
)
358 (= (match-end 0) this
)))
361 (and (re-search-forward "[^&]*;" e t
)
362 (= (match-beginning 0) this
)))
365 (and (re-search-backward "&[^;]*" s t
)
366 (= (match-end 0) this
)))))))))
368 ;*---------------------------------------------------------------------*/
369 ;* Programming mode */
370 ;*---------------------------------------------------------------------*/
371 (defvar flyspell-prog-text-faces
372 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
373 "Faces corresponding to text in programming-mode buffers.")
375 (defun flyspell-generic-progmode-verify ()
376 "Used for `flyspell-generic-check-word-p' in programming modes."
377 (let ((f (get-text-property (point) 'face
)))
378 (memq f flyspell-prog-text-faces
)))
381 (defun flyspell-prog-mode ()
382 "Turn on `flyspell-mode' for comments and strings."
384 (setq flyspell-generic-check-word-p
'flyspell-generic-progmode-verify
)
386 (run-hooks 'flyspell-prog-mode-hook
))
388 ;*---------------------------------------------------------------------*/
389 ;* Overlay compatibility */
390 ;*---------------------------------------------------------------------*/
391 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
392 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
393 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
394 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
395 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
396 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
397 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
398 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
400 ;*---------------------------------------------------------------------*/
401 ;* The minor mode declaration. */
402 ;*---------------------------------------------------------------------*/
403 (defvar flyspell-mouse-map
404 (let ((map (make-sparse-keymap)))
405 (define-key map
(if (featurep 'xemacs
) [button2] [down-mouse-2])
406 #'flyspell-correct-word)
408 "Keymap for Flyspell to put on erroneous words.")
410 (defvar flyspell-mode-map
411 (let ((map (make-sparse-keymap)))
412 (if flyspell-use-meta-tab
413 (define-key map "\M-\t" 'flyspell-auto-correct-word))
414 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
415 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
416 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
418 "Minor mode keymap for Flyspell mode--for the whole buffer.")
420 ;; dash character machinery
421 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
422 "*Non-nil means that the `-' char is considered as a word delimiter.")
423 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
424 (defvar flyspell-dash-dictionary nil)
425 (make-variable-buffer-local 'flyspell-dash-dictionary)
426 (defvar flyspell-dash-local-dictionary nil)
427 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
429 ;*---------------------------------------------------------------------*/
431 ;*---------------------------------------------------------------------*/
432 (defface flyspell-incorrect
433 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
435 "Face used for marking a misspelled word in Flyspell."
437 ;; backward-compatibility alias
438 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
440 (defface flyspell-duplicate
441 '((((class color)) (:foreground "Gold3" :bold t :underline t))
443 "Face used for marking a misspelled word that appears twice in the buffer.
444 See also `flyspell-duplicate-distance'."
446 ;; backward-compatibility alias
447 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
449 (defvar flyspell-overlay nil)
451 ;*---------------------------------------------------------------------*/
452 ;* flyspell-mode ... */
453 ;*---------------------------------------------------------------------*/
455 (define-minor-mode flyspell-mode
456 "Minor mode performing on-the-fly spelling checking.
457 This spawns a single Ispell process and checks each word.
458 The default flyspell behavior is to highlight incorrect words.
459 With no argument, this command toggles Flyspell mode.
460 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
463 \\[ispell-word]: correct words (using Ispell).
464 \\[flyspell-auto-correct-word]: automatically correct word.
465 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
466 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
469 This runs `flyspell-mode-hook' after flyspell is entered.
472 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
473 valid. For instance, a personal dictionary can be used by
474 invoking `ispell-change-dictionary'.
476 Consider using the `ispell-parser' to check your text. For instance
478 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
481 \\[flyspell-region] checks all words inside a region.
482 \\[flyspell-buffer] checks the whole buffer."
483 :lighter flyspell-mode-line-string
484 :keymap flyspell-mode-map
488 (flyspell-mode-off)))
490 ;*---------------------------------------------------------------------*/
491 ;* flyspell-buffers ... */
492 ;* ------------------------------------------------------------- */
493 ;* For remembering buffers running flyspell */
494 ;*---------------------------------------------------------------------*/
495 (defvar flyspell-buffers nil)
497 ;*---------------------------------------------------------------------*/
498 ;* flyspell-minibuffer-p ... */
499 ;*---------------------------------------------------------------------*/
500 (defun flyspell-minibuffer-p (buffer)
501 "Is BUFFER a minibuffer?"
502 (let ((ws (get-buffer-window-list buffer t)))
503 (and (consp ws) (window-minibuffer-p (car ws)))))
505 ;*---------------------------------------------------------------------*/
506 ;* flyspell-accept-buffer-local-defs ... */
507 ;*---------------------------------------------------------------------*/
508 (defun flyspell-accept-buffer-local-defs ()
509 ;; strange problem. If buffer in current window has font-lock turned on,
510 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
511 ;; call will reset the buffer to the buffer in the current window. However,
512 ;; it only happens at startup (fix by Albert L. Ting).
513 (let ((buf (current-buffer)))
514 (ispell-accept-buffer-local-defs)
516 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
517 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
518 ;; The dictionary has changed
520 (setq flyspell-dash-dictionary ispell-dictionary)
521 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
522 (if (member (or ispell-local-dictionary ispell-dictionary)
523 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
524 (setq flyspell-consider-dash-as-word-delimiter-flag t)
525 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
527 ;*---------------------------------------------------------------------*/
528 ;* flyspell-mode-on ... */
529 ;*---------------------------------------------------------------------*/
530 (defun flyspell-mode-on ()
531 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
532 (setq ispell-highlight-face 'flyspell-incorrect)
533 ;; local dictionaries setup
534 (or ispell-local-dictionary ispell-dictionary
535 (if flyspell-default-dictionary
536 (ispell-change-dictionary flyspell-default-dictionary)))
537 ;; we have to force ispell to accept the local definition or
538 ;; otherwise it could be too late, the local dictionary may
540 (flyspell-accept-buffer-local-defs)
541 ;; we put the `flyspell-delayed' property on some commands
542 (flyspell-delay-commands)
543 ;; we put the `flyspell-deplacement' property on some commands
544 (flyspell-deplacement-commands)
545 ;; we bound flyspell action to post-command hook
546 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
547 ;; we bound flyspell action to pre-command hook
548 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
549 ;; we bound flyspell action to after-change hook
550 (make-local-variable 'after-change-functions)
551 (setq after-change-functions
552 (cons 'flyspell-after-change-function after-change-functions))
553 ;; set flyspell-generic-check-word-p based on the major mode
554 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
556 (setq flyspell-generic-check-word-p mode-predicate)))
557 ;; the welcome message
558 (if (and flyspell-issue-message-flag
559 flyspell-issue-welcome-flag
561 (let ((binding (where-is-internal 'flyspell-auto-correct-word
565 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
566 (key-description binding))
567 "Welcome to flyspell. Use Mouse-2 to correct words."))))
568 ;; we end with the flyspell hooks
569 (run-hooks 'flyspell-mode-hook))
571 ;*---------------------------------------------------------------------*/
572 ;* flyspell-delay-commands ... */
573 ;*---------------------------------------------------------------------*/
574 (defun flyspell-delay-commands ()
575 "Install the standard set of Flyspell delayed commands."
576 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
577 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
579 ;*---------------------------------------------------------------------*/
580 ;* flyspell-delay-command ... */
581 ;*---------------------------------------------------------------------*/
582 (defun flyspell-delay-command (command)
583 "Set COMMAND to be delayed, for Flyspell.
584 When flyspell `post-command-hook' is invoked because a delayed command
585 as been used the current word is not immediately checked.
586 It will be checked only after `flyspell-delay' seconds."
587 (interactive "SDelay Flyspell after Command: ")
588 (put command 'flyspell-delayed t))
590 ;*---------------------------------------------------------------------*/
591 ;* flyspell-deplacement-commands ... */
592 ;*---------------------------------------------------------------------*/
593 (defun flyspell-deplacement-commands ()
594 "Install the standard set of Flyspell deplacement commands."
595 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
596 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
598 ;*---------------------------------------------------------------------*/
599 ;* flyspell-deplacement-command ... */
600 ;*---------------------------------------------------------------------*/
601 (defun flyspell-deplacement-command (command)
602 "Set COMMAND that implement cursor movements, for Flyspell.
603 When flyspell `post-command-hook' is invoked because of a deplacement command
604 as been used the current word is checked only if the previous command was
605 not the very same deplacement command."
606 (interactive "SDeplacement Flyspell after Command: ")
607 (put command 'flyspell-deplacement t))
609 ;*---------------------------------------------------------------------*/
610 ;* flyspell-word-cache ... */
611 ;*---------------------------------------------------------------------*/
612 (defvar flyspell-word-cache-start nil)
613 (defvar flyspell-word-cache-end nil)
614 (defvar flyspell-word-cache-word nil)
615 (defvar flyspell-word-cache-result '_)
616 (make-variable-buffer-local 'flyspell-word-cache-start)
617 (make-variable-buffer-local 'flyspell-word-cache-end)
618 (make-variable-buffer-local 'flyspell-word-cache-word)
619 (make-variable-buffer-local 'flyspell-word-cache-result)
621 ;*---------------------------------------------------------------------*/
622 ;* The flyspell pre-hook, store the current position. In the */
623 ;* post command hook, we will check, if the word at this position */
624 ;* has to be spell checked. */
625 ;*---------------------------------------------------------------------*/
626 (defvar flyspell-pre-buffer nil)
627 (defvar flyspell-pre-point nil)
628 (defvar flyspell-pre-column nil)
629 (defvar flyspell-pre-pre-buffer nil)
630 (defvar flyspell-pre-pre-point nil)
632 ;*---------------------------------------------------------------------*/
633 ;* flyspell-previous-command ... */
634 ;*---------------------------------------------------------------------*/
635 (defvar flyspell-previous-command nil
636 "The last interactive command checked by Flyspell.")
638 ;*---------------------------------------------------------------------*/
639 ;* flyspell-pre-command-hook ... */
640 ;*---------------------------------------------------------------------*/
641 (defun flyspell-pre-command-hook ()
642 "Save the current buffer and point for Flyspell's post-command hook."
644 (setq flyspell-pre-buffer (current-buffer))
645 (setq flyspell-pre-point (point))
646 (setq flyspell-pre-column (current-column)))
648 ;*---------------------------------------------------------------------*/
649 ;* flyspell-mode-off ... */
650 ;*---------------------------------------------------------------------*/
652 (defun flyspell-mode-off ()
653 "Turn Flyspell mode off."
654 ;; we remove the hooks
655 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
656 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
657 (setq after-change-functions (delq 'flyspell-after-change-function
658 after-change-functions))
659 ;; we remove all the flyspell hilightings
660 (flyspell-delete-all-overlays)
661 ;; we have to erase pre cache variables
662 (setq flyspell-pre-buffer nil)
663 (setq flyspell-pre-point nil)
664 ;; we mark the mode as killed
665 (setq flyspell-mode nil))
667 ;*---------------------------------------------------------------------*/
668 ;* flyspell-check-pre-word-p ... */
669 ;*---------------------------------------------------------------------*/
670 (defun flyspell-check-pre-word-p ()
671 "Return non-nil if we should check the word before point.
672 More precisely, it applies to the word that was before point
673 before the current command."
675 ((or (not (numberp flyspell-pre-point))
676 (not (bufferp flyspell-pre-buffer))
677 (not (buffer-live-p flyspell-pre-buffer)))
679 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
680 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
682 ((or (and (= flyspell-pre-point (- (point) 1))
683 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
684 (= flyspell-pre-point (point))
685 (= flyspell-pre-point (+ (point) 1)))
687 ((and (symbolp this-command)
688 (not executing-kbd-macro)
689 (or (get this-command 'flyspell-delayed)
690 (and (get this-command 'flyspell-deplacement)
691 (eq flyspell-previous-command this-command)))
692 (or (= (current-column) 0)
693 (= (current-column) flyspell-pre-column)
694 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
696 ((not (eq (current-buffer) flyspell-pre-buffer))
698 ((not (and (numberp flyspell-word-cache-start)
699 (numberp flyspell-word-cache-end)))
702 (or (< flyspell-pre-point flyspell-word-cache-start)
703 (> flyspell-pre-point flyspell-word-cache-end)))))
705 ;*---------------------------------------------------------------------*/
706 ;* The flyspell after-change-hook, store the change position. In */
707 ;* the post command hook, we will check, if the word at this */
708 ;* position has to be spell checked. */
709 ;*---------------------------------------------------------------------*/
710 (defvar flyspell-changes nil)
712 ;*---------------------------------------------------------------------*/
713 ;* flyspell-after-change-function ... */
714 ;*---------------------------------------------------------------------*/
715 (defun flyspell-after-change-function (start stop len)
716 "Save the current buffer and point for Flyspell's post-command hook."
718 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
720 ;*---------------------------------------------------------------------*/
721 ;* flyspell-check-changed-word-p ... */
722 ;*---------------------------------------------------------------------*/
723 (defun flyspell-check-changed-word-p (start stop)
724 "Return t when the changed word has to be checked.
725 The answer depends of several criteria.
726 Mostly we check word delimiters."
728 ((and (memq (char-after start) '(?\n ? )) (> stop start))
730 ((not (numberp flyspell-pre-point))
732 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
734 ((let ((pos (point)))
735 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
740 ;*---------------------------------------------------------------------*/
741 ;* flyspell-check-word-p ... */
742 ;*---------------------------------------------------------------------*/
743 (defun flyspell-check-word-p ()
744 "Return t when the word at `point' has to be checked.
745 The answer depends of several criteria.
746 Mostly we check word delimiters."
748 ((<= (- (point-max) 1) (point-min))
749 ;; the buffer is not filled enough
751 ((and (and (> (current-column) 0)
752 (not (eq (current-column) flyspell-pre-column)))
755 (and (looking-at (flyspell-get-not-casechars))
756 (or flyspell-consider-dash-as-word-delimiter-flag
757 (not (looking-at "\\-"))))))
758 ;; yes because we have reached or typed a word delimiter.
760 ((symbolp this-command)
762 ((get this-command 'flyspell-deplacement)
763 (not (eq flyspell-previous-command this-command)))
764 ((get this-command 'flyspell-delayed)
765 ;; the current command is not delayed, that
766 ;; is that we must check the word now
767 (sit-for flyspell-delay))
771 ;*---------------------------------------------------------------------*/
772 ;* flyspell-debug-signal-no-check ... */
773 ;*---------------------------------------------------------------------*/
774 (defun flyspell-debug-signal-no-check (msg obj)
775 (setq debug-on-error t)
777 (let ((buffer (get-buffer-create "*flyspell-debug*")))
780 (insert "NO-CHECK:\n")
781 (insert (format " %S : %S\n" msg obj)))))
783 ;*---------------------------------------------------------------------*/
784 ;* flyspell-debug-signal-pre-word-checked ... */
785 ;*---------------------------------------------------------------------*/
786 (defun flyspell-debug-signal-pre-word-checked ()
787 (setq debug-on-error t)
789 (let ((buffer (get-buffer-create "*flyspell-debug*")))
791 (insert "PRE-WORD:\n")
792 (insert (format " pre-point : %S\n" flyspell-pre-point))
793 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
794 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
795 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
796 (goto-char (point-max)))))
798 ;*---------------------------------------------------------------------*/
799 ;* flyspell-debug-signal-word-checked ... */
800 ;*---------------------------------------------------------------------*/
801 (defun flyspell-debug-signal-word-checked ()
802 (setq debug-on-error t)
804 (let ((oldbuf (current-buffer))
805 (buffer (get-buffer-create "*flyspell-debug*"))
809 (insert (format " this-cmd : %S\n" this-command))
810 (insert (format " delayed : %S\n" (and (symbolp this-command)
811 (get this-command 'flyspell-delayed))))
812 (insert (format " point : %S\n" point))
813 (insert (format " prev-char : [%c] %S\n"
816 (let ((c (if (> (point) (point-min))
819 (char-after (point)))
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))))))
833 (insert (format " because : %S\n"
835 ((not (and (symbolp this-command)
836 (get this-command 'flyspell-delayed)))
837 ;; the current command is not delayed, that
838 ;; is that we must check the word now
842 (let ((c (if (> (point) (point-min))
845 (and (looking-at (flyspell-get-not-casechars))
846 (or flyspell-consider-dash-as-word-delimiter-flag
847 (not (looking-at "\\-"))))))))
850 ;; yes because we have reached or typed a word delimiter.
852 ((not (integerp flyspell-delay))
853 ;; yes because the user had set up a no-delay configuration.
857 (goto-char (point-max)))))
859 ;*---------------------------------------------------------------------*/
860 ;* flyspell-debug-signal-changed-checked ... */
861 ;*---------------------------------------------------------------------*/
862 (defun flyspell-debug-signal-changed-checked ()
863 (setq debug-on-error t)
865 (let ((buffer (get-buffer-create "*flyspell-debug*"))
868 (insert "CHANGED WORD:\n")
869 (insert (format " point : %S\n" point))
870 (goto-char (point-max)))))
872 ;*---------------------------------------------------------------------*/
873 ;* flyspell-post-command-hook ... */
874 ;* ------------------------------------------------------------- */
875 ;* It is possible that we check several words: */
876 ;* 1- the current word is checked if the predicate */
877 ;* FLYSPELL-CHECK-WORD-P is true */
878 ;* 2- the word that used to be the current word before the */
879 ;* THIS-COMMAND is checked if: */
880 ;* a- the previous word is different from the current word */
881 ;* b- the previous word as not just been checked by the */
882 ;* previous FLYSPELL-POST-COMMAND-HOOK */
883 ;* 3- the words changed by the THIS-COMMAND that are neither the */
884 ;* previous word nor the current word */
885 ;*---------------------------------------------------------------------*/
886 (defun flyspell-post-command-hook ()
887 "The `post-command-hook' used by flyspell to check a word in-the-fly."
889 (let ((command this-command))
890 (if (flyspell-check-pre-word-p)
892 '(flyspell-debug-signal-pre-word-checked)
893 (set-buffer flyspell-pre-buffer)
895 (goto-char flyspell-pre-point)
897 (if (flyspell-check-word-p)
899 '(flyspell-debug-signal-word-checked)
901 ;; we remember which word we have just checked.
902 ;; this will be used next time we will check a word
903 ;; to compare the next current word with the word
904 ;; that as been registered in the pre-command-hook
905 ;; that is these variables are used within the predicate
906 ;; FLYSPELL-CHECK-PRE-WORD-P
907 (setq flyspell-pre-pre-buffer (current-buffer))
908 (setq flyspell-pre-pre-point (point)))
910 (setq flyspell-pre-pre-buffer nil)
911 (setq flyspell-pre-pre-point nil)
912 ;; when a word is not checked because of a delayed command
913 ;; we do not disable the ispell cache.
914 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
916 (setq flyspell-word-cache-end -1)
917 (setq flyspell-word-cache-result '_)))))
918 (while (consp flyspell-changes)
919 (let ((start (car (car flyspell-changes)))
920 (stop (cdr (car flyspell-changes))))
921 (if (flyspell-check-changed-word-p start stop)
923 '(flyspell-debug-signal-changed-checked)
926 (setq flyspell-changes (cdr flyspell-changes))))
927 (setq flyspell-previous-command command)))
929 ;*---------------------------------------------------------------------*/
930 ;* flyspell-notify-misspell ... */
931 ;*---------------------------------------------------------------------*/
932 (defun flyspell-notify-misspell (start end word poss)
933 (let ((replacements (if (stringp poss)
935 (if flyspell-sort-corrections
936 (sort (car (cdr (cdr poss))) 'string<)
937 (car (cdr (cdr poss)))))))
938 (if flyspell-issue-message-flag
939 (message (format "mispelling `%s' %S" word replacements)))))
941 ;*---------------------------------------------------------------------*/
942 ;* flyspell-word-search-backward ... */
943 ;*---------------------------------------------------------------------*/
944 (defun flyspell-word-search-backward (word bound)
948 (while (and (not r) (setq p (search-backward word bound t)))
949 (let ((lw (flyspell-get-word '())))
950 (if (and (consp lw) (string-equal (car lw) word))
955 ;*---------------------------------------------------------------------*/
956 ;* flyspell-word-search-forward ... */
957 ;*---------------------------------------------------------------------*/
958 (defun flyspell-word-search-forward (word bound)
962 (while (and (not r) (setq p (search-forward word bound t)))
963 (let ((lw (flyspell-get-word '())))
964 (if (and (consp lw) (string-equal (car lw) word))
966 (goto-char (1+ p)))))
969 ;*---------------------------------------------------------------------*/
970 ;* flyspell-word ... */
971 ;*---------------------------------------------------------------------*/
972 (defun flyspell-word (&optional following)
973 "Spell check a word."
974 (interactive (list ispell-following-word))
976 ;; use the correct dictionary
977 (flyspell-accept-buffer-local-defs)
978 (let* ((cursor-location (point))
979 (flyspell-word (flyspell-get-word following))
981 (if (or (eq flyspell-word nil)
982 (and (fboundp flyspell-generic-check-word-p)
983 (not (funcall flyspell-generic-check-word-p))))
986 ;; destructure return flyspell-word info list.
987 (setq start (car (cdr flyspell-word))
988 end (car (cdr (cdr flyspell-word)))
989 word (car flyspell-word))
990 ;; before checking in the directory, we check for doublons.
992 ((and (or (not (eq ispell-parser 'tex))
993 (and (> start (point-min))
994 (not (memq (char-after (1- start)) '(?\} ?\\)))))
995 flyspell-mark-duplications-flag
997 (goto-char (1- start))
998 (let ((p (flyspell-word-search-backward
1000 (- start (1+ (- end start))))))
1001 (and p (/= p (1- start))))))
1002 ;; yes, this is a doublon
1003 (flyspell-highlight-incorrect-region start end 'doublon)
1005 ((and (eq flyspell-word-cache-start start)
1006 (eq flyspell-word-cache-end end)
1007 (string-equal flyspell-word-cache-word word))
1008 ;; this word had been already checked, we skip
1009 flyspell-word-cache-result)
1010 ((and (eq ispell-parser 'tex)
1011 (flyspell-tex-command-p flyspell-word))
1012 ;; this is a correct word (because a tex command)
1013 (flyspell-unhighlight-at start)
1015 (flyspell-unhighlight-at (- end 1)))
1018 ;; we setup the cache
1019 (setq flyspell-word-cache-start start)
1020 (setq flyspell-word-cache-end end)
1021 (setq flyspell-word-cache-word word)
1022 ;; now check spelling of word.
1023 (process-send-string ispell-process "%\n")
1024 ;; put in verbose mode
1025 (process-send-string ispell-process
1026 (concat "^" word "\n"))
1027 ;; we mark the ispell process so it can be killed
1028 ;; when emacs is exited without query
1029 (set-process-query-on-exit-flag ispell-process nil)
1030 ;; wait until ispell has processed word
1032 (accept-process-output ispell-process)
1033 (not (string= "" (car ispell-filter)))))
1034 ;; (process-send-string ispell-process "!\n")
1035 ;; back to terse mode.
1036 (setq ispell-filter (cdr ispell-filter))
1037 (if (consp ispell-filter)
1038 (setq poss (ispell-parse-output (car ispell-filter))))
1039 (let ((res (cond ((eq poss t)
1041 (setq flyspell-word-cache-result t)
1042 (flyspell-unhighlight-at start)
1044 (flyspell-unhighlight-at (- end 1)))
1046 ((and (stringp poss) flyspell-highlight-flag)
1048 (setq flyspell-word-cache-result t)
1049 (flyspell-unhighlight-at start)
1051 (flyspell-unhighlight-at (- end 1)))
1054 (setq flyspell-word-cache-result t)
1055 (flyspell-unhighlight-at start)
1057 (flyspell-unhighlight-at (- end 1)))
1059 ((or (and (< flyspell-duplicate-distance 0)
1062 (flyspell-word-search-backward
1067 (flyspell-word-search-forward
1070 (and (> flyspell-duplicate-distance 0)
1073 (flyspell-word-search-backward
1076 flyspell-duplicate-distance)))
1079 (flyspell-word-search-forward
1082 flyspell-duplicate-distance))))))
1083 (setq flyspell-word-cache-result nil)
1084 (if flyspell-highlight-flag
1085 (flyspell-highlight-duplicate-region
1087 (message (format "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 start end 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 (point-max) t)
1238 (re-search-backward flyspell-casechars (point-min) t)))
1239 ;; move to front of word
1240 (re-search-backward flyspell-not-casechars (point-min) '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 (point-min) 'move)))
1256 (if (looking-at flyspell-casechars)
1257 (re-search-backward flyspell-not-casechars (point-min) 'move)
1258 (backward-char -1))))
1259 ;; Now mark the word and save to string.
1260 (if (not (re-search-forward word-regexp (point-max) 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 (let ((buffer flyspell-external-ispell-buffer))
1317 (goto-char (point-min))
1320 ;; now we are done with ispell, we have to find the word in
1321 ;; the initial buffer
1322 (while (< (point) (- (point-max) 1))
1323 ;; we have to fetch the incorrect word
1324 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1325 (let ((word (match-string 1)))
1326 (if (string= word pword)
1327 (setq pcount (1+ pcount))
1331 (goto-char (match-end 0))
1332 (if flyspell-issue-message-flag
1333 (message "Spell Checking...%d%% [%s]"
1334 (* 100 (/ (float (point)) (point-max)))
1336 (set-buffer flyspell-large-region-buffer)
1337 (goto-char flyspell-large-region-beg)
1340 (while (and (or (< n pcount) keep)
1341 (search-forward word flyspell-large-region-end t))
1343 (goto-char (- (point) 1))
1345 (setq keep (flyspell-word))))
1347 (setq flyspell-large-region-beg (point))))
1348 (set-buffer buffer))
1349 (goto-char (point-max)))))
1351 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1352 ;; ok, we are done with pointing out incorrect words, we just
1353 ;; have to kill the temporary buffer
1354 (kill-buffer flyspell-external-ispell-buffer)
1355 (setq flyspell-external-ispell-buffer nil)))
1357 ;*---------------------------------------------------------------------*/
1358 ;* flyspell-large-region ... */
1359 ;*---------------------------------------------------------------------*/
1360 (defun flyspell-large-region (beg end)
1361 (let* ((curbuf (current-buffer))
1362 (buffer (get-buffer-create "*flyspell-region*")))
1363 (setq flyspell-external-ispell-buffer buffer)
1364 (setq flyspell-large-region-buffer curbuf)
1365 (setq flyspell-large-region-beg beg)
1366 (setq flyspell-large-region-end end)
1369 ;; this is done, we can start checking...
1370 (if flyspell-issue-message-flag (message "Checking region..."))
1372 (let ((c (apply 'call-process-region beg
1378 (if ispell-really-aspell "list" "-l")
1380 ;; Local dictionary becomes the global dictionary in use.
1381 (if ispell-local-dictionary
1382 (setq ispell-dictionary ispell-local-dictionary))
1383 (setq args (ispell-get-ispell-args))
1384 (if ispell-dictionary ; use specified dictionary
1386 (append (list "-d" ispell-dictionary) args)))
1387 (if ispell-personal-dictionary ; use specified pers dict
1392 ispell-personal-dictionary)))))
1393 (setq args (append args ispell-extra-args))
1396 (flyspell-external-point-words)
1397 (error "Can't check region...")))))
1399 ;*---------------------------------------------------------------------*/
1400 ;* flyspell-region ... */
1401 ;* ------------------------------------------------------------- */
1402 ;* Because `ispell -a' is too slow, it is not possible to use */
1403 ;* it on large region. Then, when ispell is invoked on a large */
1404 ;* text region, a new `ispell -l' process is spawned. The */
1405 ;* pointed out words are then searched in the region a checked with */
1406 ;* regular flyspell means. */
1407 ;*---------------------------------------------------------------------*/
1409 (defun flyspell-region (beg end)
1410 "Flyspell text between BEG and END."
1419 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1420 (flyspell-large-region beg end)
1421 (flyspell-small-region beg end)))))
1423 ;*---------------------------------------------------------------------*/
1424 ;* flyspell-buffer ... */
1425 ;*---------------------------------------------------------------------*/
1427 (defun flyspell-buffer ()
1428 "Flyspell whole buffer."
1430 (flyspell-region (point-min) (point-max)))
1432 ;*---------------------------------------------------------------------*/
1433 ;* old next error position ... */
1434 ;*---------------------------------------------------------------------*/
1435 (defvar flyspell-old-buffer-error nil)
1436 (defvar flyspell-old-pos-error nil)
1438 ;*---------------------------------------------------------------------*/
1439 ;* flyspell-goto-next-error ... */
1440 ;*---------------------------------------------------------------------*/
1441 (defun flyspell-goto-next-error ()
1442 "Go to the next previously detected error.
1443 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1448 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1449 (eq pos flyspell-old-pos-error))
1451 (if (= flyspell-old-pos-error max)
1452 ;; goto beginning of buffer
1454 (message "Restarting from beginning of buffer")
1455 (goto-char (point-min)))
1457 (setq pos (point))))
1458 ;; seek the next error
1459 (while (and (< pos max)
1460 (let ((ovs (overlays-at pos))
1462 (while (and (not r) (consp ovs))
1463 (if (flyspell-overlay-p (car ovs))
1465 (setq ovs (cdr ovs))))
1467 (setq pos (1+ pos)))
1468 ;; save the current location for next invocation
1469 (setq flyspell-old-pos-error pos)
1470 (setq flyspell-old-buffer-error (current-buffer))
1473 (message "No more miss-spelled word!"))))
1475 ;*---------------------------------------------------------------------*/
1476 ;* flyspell-overlay-p ... */
1477 ;*---------------------------------------------------------------------*/
1478 (defun flyspell-overlay-p (o)
1479 "A predicate that return true iff O is an overlay used by flyspell."
1480 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1482 ;*---------------------------------------------------------------------*/
1483 ;* flyspell-delete-all-overlays ... */
1484 ;* ------------------------------------------------------------- */
1485 ;* Remove all the overlays introduced by flyspell. */
1486 ;*---------------------------------------------------------------------*/
1487 (defun flyspell-delete-all-overlays ()
1488 "Delete all the overlays used by flyspell."
1489 (let ((l (overlays-in (point-min) (point-max))))
1492 (if (flyspell-overlay-p (car l))
1493 (delete-overlay (car l)))
1494 (setq l (cdr l))))))
1496 ;*---------------------------------------------------------------------*/
1497 ;* flyspell-unhighlight-at ... */
1498 ;*---------------------------------------------------------------------*/
1499 (defun flyspell-unhighlight-at (pos)
1500 "Remove the flyspell overlay that are located at POS."
1501 (if flyspell-persistent-highlight
1502 (let ((overlays (overlays-at pos)))
1503 (while (consp overlays)
1504 (if (flyspell-overlay-p (car overlays))
1505 (delete-overlay (car overlays)))
1506 (setq overlays (cdr overlays))))
1507 (if (flyspell-overlay-p flyspell-overlay)
1508 (delete-overlay flyspell-overlay))))
1510 ;*---------------------------------------------------------------------*/
1511 ;* flyspell-properties-at-p ... */
1512 ;* ------------------------------------------------------------- */
1513 ;* Is there an highlight properties at position pos? */
1514 ;*---------------------------------------------------------------------*/
1515 (defun flyspell-properties-at-p (pos)
1516 "Return t if there is a text property at POS, not counting `local-map'.
1517 If variable `flyspell-highlight-properties' is set to nil,
1518 text with properties are not checked. This function is used to discover
1519 if the character at POS has any other property."
1520 (let ((prop (text-properties-at pos))
1522 (while (and keep (consp prop))
1523 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1524 (setq prop (cdr (cdr prop)))
1528 ;*---------------------------------------------------------------------*/
1529 ;* make-flyspell-overlay ... */
1530 ;*---------------------------------------------------------------------*/
1531 (defun make-flyspell-overlay (beg end face mouse-face)
1532 "Allocate an overlay to highlight an incorrect word.
1533 BEG and END specify the range in the buffer of that word.
1534 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1536 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1537 (overlay-put flyspell-overlay 'face face)
1538 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1539 (overlay-put flyspell-overlay 'flyspell-overlay t)
1540 (overlay-put flyspell-overlay 'evaporate t)
1541 (overlay-put flyspell-overlay 'help-echo "mouse-2: correct word at point")
1542 (overlay-put flyspell-overlay 'keymap flyspell-mouse-map)
1543 (when (eq face 'flyspell-incorrect)
1544 (and (stringp flyspell-before-incorrect-word-string)
1545 (overlay-put flyspell-overlay 'before-string
1546 flyspell-before-incorrect-word-string))
1547 (and (stringp flyspell-after-incorrect-word-string)
1548 (overlay-put flyspell-overlay 'after-string
1549 flyspell-after-incorrect-word-string)))
1552 ;*---------------------------------------------------------------------*/
1553 ;* flyspell-highlight-incorrect-region ... */
1554 ;*---------------------------------------------------------------------*/
1555 (defun flyspell-highlight-incorrect-region (beg end poss)
1556 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1557 (let ((inhibit-read-only t))
1558 (unless (run-hook-with-args-until-success
1559 'flyspell-incorrect-hook beg end poss)
1560 (if (or flyspell-highlight-properties
1561 (not (flyspell-properties-at-p beg)))
1563 ;; we cleanup all the overlay that are in the region, not
1564 ;; beginning at the word start position
1565 (if (< (1+ beg) end)
1566 (let ((os (overlays-in (1+ beg) end)))
1568 (if (flyspell-overlay-p (car os))
1569 (delete-overlay (car os)))
1570 (setq os (cdr os)))))
1571 ;; we cleanup current overlay at the same position
1572 (if (and (not flyspell-persistent-highlight)
1573 (overlayp flyspell-overlay))
1574 (delete-overlay flyspell-overlay)
1575 (let ((os (overlays-at beg)))
1577 (if (flyspell-overlay-p (car os))
1578 (delete-overlay (car os)))
1579 (setq os (cdr os)))))
1580 ;; now we can use a new overlay
1581 (setq flyspell-overlay
1582 (make-flyspell-overlay
1583 beg end 'flyspell-incorrect 'highlight)))))))
1585 ;*---------------------------------------------------------------------*/
1586 ;* flyspell-highlight-duplicate-region ... */
1587 ;*---------------------------------------------------------------------*/
1588 (defun flyspell-highlight-duplicate-region (beg end poss)
1589 "Set up an overlay on a duplicated word, in the buffer from BEG to END.
1590 ??? What does POSS mean?"
1591 (let ((inhibit-read-only t))
1592 (unless (run-hook-with-args-until-success
1593 'flyspell-incorrect-hook beg end poss)
1594 (if (or flyspell-highlight-properties
1595 (not (flyspell-properties-at-p beg)))
1597 ;; we cleanup current overlay at the same position
1598 (if (and (not flyspell-persistent-highlight)
1599 (overlayp flyspell-overlay))
1600 (delete-overlay flyspell-overlay)
1601 (let ((overlays (overlays-at beg)))
1602 (while (consp overlays)
1603 (if (flyspell-overlay-p (car overlays))
1604 (delete-overlay (car overlays)))
1605 (setq overlays (cdr overlays)))))
1606 ;; now we can use a new overlay
1607 (setq flyspell-overlay
1608 (make-flyspell-overlay beg end
1612 ;*---------------------------------------------------------------------*/
1613 ;* flyspell-auto-correct-cache ... */
1614 ;*---------------------------------------------------------------------*/
1615 (defvar flyspell-auto-correct-pos nil)
1616 (defvar flyspell-auto-correct-region nil)
1617 (defvar flyspell-auto-correct-ring nil)
1618 (defvar flyspell-auto-correct-word nil)
1619 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1620 (make-variable-buffer-local 'flyspell-auto-correct-region)
1621 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1622 (make-variable-buffer-local 'flyspell-auto-correct-word)
1624 ;*---------------------------------------------------------------------*/
1625 ;* flyspell-check-previous-highlighted-word ... */
1626 ;*---------------------------------------------------------------------*/
1627 (defun flyspell-check-previous-highlighted-word (&optional arg)
1628 "Correct the closer misspelled word.
1629 This function scans a mis-spelled word before the cursor. If it finds one
1630 it proposes replacement for that word. With prefix arg, count that many
1631 misspelled words backwards."
1633 (let ((pos1 (point))
1635 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1638 (while (and (setq pos (previous-overlay-change pos))
1641 (if (> pos (point-min))
1643 (setq ovs (overlays-at (1- pos)))
1646 (setq ovs (cdr ovs))
1647 (if (and (overlay-get ov 'flyspell-overlay)
1648 (= 0 (setq arg (1- arg))))
1649 (throw 'exit t)))))))
1653 (error "No word to correct before point"))))
1655 ;*---------------------------------------------------------------------*/
1656 ;* flyspell-display-next-corrections ... */
1657 ;*---------------------------------------------------------------------*/
1658 (defun flyspell-display-next-corrections (corrections)
1659 (let ((string "Corrections:")
1662 (while (< (length string) 80)
1663 (if (equal (car l) flyspell-auto-correct-word)
1664 (setq pos (cons (+ 1 (length string)) pos)))
1665 (setq string (concat string " " (car l)))
1668 (let ((num (car pos)))
1669 (put-text-property num
1670 (+ num (length flyspell-auto-correct-word))
1671 'face 'flyspell-incorrect
1673 (setq pos (cdr pos)))
1674 (if (fboundp 'display-message)
1675 (display-message 'no-log string)
1678 ;*---------------------------------------------------------------------*/
1679 ;* flyspell-abbrev-table ... */
1680 ;*---------------------------------------------------------------------*/
1681 (defun flyspell-abbrev-table ()
1682 (if flyspell-use-global-abbrev-table-p
1684 (or local-abbrev-table global-abbrev-table)))
1686 ;*---------------------------------------------------------------------*/
1687 ;* flyspell-define-abbrev ... */
1688 ;*---------------------------------------------------------------------*/
1689 (defun flyspell-define-abbrev (name expansion)
1690 (let ((table (flyspell-abbrev-table)))
1692 (define-abbrev table name expansion))))
1694 ;*---------------------------------------------------------------------*/
1695 ;* flyspell-auto-correct-word ... */
1696 ;*---------------------------------------------------------------------*/
1697 (defun flyspell-auto-correct-word ()
1698 "Correct the current word.
1699 This command proposes various successive corrections for the current word."
1702 (old-max (point-max)))
1703 ;; use the correct dictionary
1704 (flyspell-accept-buffer-local-defs)
1705 (if (and (eq flyspell-auto-correct-pos pos)
1706 (consp flyspell-auto-correct-region))
1707 ;; we have already been using the function at the same location
1708 (let* ((start (car flyspell-auto-correct-region))
1709 (len (cdr flyspell-auto-correct-region)))
1710 (flyspell-unhighlight-at start)
1711 (delete-region start (+ start len))
1712 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1713 (let* ((word (car flyspell-auto-correct-ring))
1714 (len (length word)))
1715 (rplacd flyspell-auto-correct-region len)
1717 (if flyspell-abbrev-p
1718 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1719 flyspell-auto-correct-word)
1720 (flyspell-change-abbrev (flyspell-abbrev-table)
1721 flyspell-auto-correct-word
1723 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1724 (funcall flyspell-insert-function word)
1726 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1727 (flyspell-ajust-cursor-point pos (point) old-max)
1728 (setq flyspell-auto-correct-pos (point)))
1729 ;; fetch the word to be checked
1730 (let ((word (flyspell-get-word nil)))
1732 (let ((start (car (cdr word)))
1733 (end (car (cdr (cdr word))))
1736 (setq flyspell-auto-correct-word word)
1737 ;; now check spelling of word.
1738 (process-send-string ispell-process "%\n") ;put in verbose mode
1739 (process-send-string ispell-process (concat "^" word "\n"))
1740 ;; wait until ispell has processed word
1742 (accept-process-output ispell-process)
1743 (not (string= "" (car ispell-filter)))))
1744 (setq ispell-filter (cdr ispell-filter))
1745 (if (consp ispell-filter)
1746 (setq poss (ispell-parse-output (car ispell-filter))))
1748 ((or (eq poss t) (stringp poss))
1749 ;; don't correct word
1753 (error "Ispell: error in Ispell process"))
1755 ;; the word is incorrect, we have to propose a replacement
1756 (let ((replacements (if flyspell-sort-corrections
1757 (sort (car (cdr (cdr poss))) 'string<)
1758 (car (cdr (cdr poss))))))
1759 (setq flyspell-auto-correct-region nil)
1760 (if (consp replacements)
1762 (let ((replace (car replacements)))
1763 (let ((new-word replace))
1764 (if (not (equal new-word (car poss)))
1766 ;; the save the current replacements
1767 (setq flyspell-auto-correct-region
1768 (cons start (length new-word)))
1769 (let ((l replacements))
1770 (while (consp (cdr l))
1772 (rplacd l (cons (car poss) replacements)))
1773 (setq flyspell-auto-correct-ring
1775 (flyspell-unhighlight-at start)
1776 (delete-region start end)
1777 (funcall flyspell-insert-function new-word)
1778 (if flyspell-abbrev-p
1779 (if (flyspell-already-abbrevp
1780 (flyspell-abbrev-table) word)
1781 (flyspell-change-abbrev
1782 (flyspell-abbrev-table)
1785 (flyspell-define-abbrev word
1788 (flyspell-display-next-corrections
1789 (cons new-word flyspell-auto-correct-ring))
1790 (flyspell-ajust-cursor-point pos
1793 (setq flyspell-auto-correct-pos (point))
1794 (ispell-pdict-save t)))))))
1796 ;*---------------------------------------------------------------------*/
1797 ;* flyspell-auto-correct-previous-pos ... */
1798 ;*---------------------------------------------------------------------*/
1799 (defvar flyspell-auto-correct-previous-pos nil
1800 "Holds the start of the first incorrect word before point.")
1802 ;*---------------------------------------------------------------------*/
1803 ;* flyspell-auto-correct-previous-hook ... */
1804 ;*---------------------------------------------------------------------*/
1805 (defun flyspell-auto-correct-previous-hook ()
1806 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1807 Sets `flyspell-auto-correct-previous-pos' to nil"
1809 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1810 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1811 (setq flyspell-auto-correct-previous-pos nil)))
1813 ;*---------------------------------------------------------------------*/
1814 ;* flyspell-auto-correct-previous-word ... */
1815 ;*---------------------------------------------------------------------*/
1816 (defun flyspell-auto-correct-previous-word (position)
1817 "*Auto correct the first mispelled word that occurs before point.
1818 But don't look beyond what's visible on the screen."
1823 (move-to-window-line 0)
1825 (move-to-window-line -1)
1829 (narrow-to-region top bot)
1830 (overlay-recenter (point))
1832 (add-hook 'pre-command-hook
1833 (function flyspell-auto-correct-previous-hook) t t)
1835 (unless flyspell-auto-correct-previous-pos
1836 ;; only reset if a new overlay exists
1837 (setq flyspell-auto-correct-previous-pos nil)
1839 (let ((overlay-list (overlays-in (point-min) position))
1840 (new-overlay 'dummy-value))
1842 ;; search for previous (new) flyspell overlay
1843 (while (and new-overlay
1844 (or (not (flyspell-overlay-p new-overlay))
1845 ;; check if its face has changed
1846 (not (eq (get-char-property
1847 (overlay-start new-overlay) 'face)
1848 'flyspell-incorrect))))
1849 (setq new-overlay (car-safe overlay-list))
1850 (setq overlay-list (cdr-safe overlay-list)))
1852 ;; if nothing new exits new-overlay should be nil
1853 (if new-overlay ;; the length of the word may change so go to the start
1854 (setq flyspell-auto-correct-previous-pos
1855 (overlay-start new-overlay)))))
1857 (when flyspell-auto-correct-previous-pos
1859 (goto-char flyspell-auto-correct-previous-pos)
1860 (let ((ispell-following-word t)) ;; point is at start
1861 (if (numberp flyspell-auto-correct-previous-pos)
1862 (goto-char flyspell-auto-correct-previous-pos))
1863 (flyspell-auto-correct-word))
1864 ;; the point may have moved so reset this
1865 (setq flyspell-auto-correct-previous-pos (point))))))))
1867 ;*---------------------------------------------------------------------*/
1868 ;* flyspell-correct-word ... */
1869 ;*---------------------------------------------------------------------*/
1870 (defun flyspell-correct-word (event)
1871 "Pop up a menu of possible corrections for a misspelled word.
1872 The word checked is the word at the mouse position."
1874 ;; use the correct dictionary
1875 (flyspell-accept-buffer-local-defs)
1876 ;; retain cursor location (I don't know why but save-excursion here fails).
1877 (let ((save (point)))
1878 (mouse-set-point event)
1879 (let ((cursor-location (point))
1880 (word (flyspell-get-word nil)))
1882 (let ((start (car (cdr word)))
1883 (end (car (cdr (cdr word))))
1886 ;; now check spelling of word.
1887 (process-send-string ispell-process "%\n") ;put in verbose mode
1888 (process-send-string ispell-process (concat "^" word "\n"))
1889 ;; wait until ispell has processed word
1891 (accept-process-output ispell-process)
1892 (not (string= "" (car ispell-filter)))))
1893 (setq ispell-filter (cdr ispell-filter))
1894 (if (consp ispell-filter)
1895 (setq poss (ispell-parse-output (car ispell-filter))))
1897 ((or (eq poss t) (stringp poss))
1898 ;; don't correct word
1902 (error "Ispell: error in Ispell process"))
1904 (flyspell-xemacs-popup
1905 event poss word cursor-location start end save))
1907 ;; The word is incorrect, we have to propose a replacement.
1908 (flyspell-do-correct (flyspell-emacs-popup event poss word)
1909 poss word cursor-location start end save)))
1910 (ispell-pdict-save t))))))
1912 ;*---------------------------------------------------------------------*/
1913 ;* flyspell-do-correct ... */
1914 ;*---------------------------------------------------------------------*/
1915 (defun flyspell-do-correct (replace poss word cursor-location start end save)
1916 "The popup menu callback."
1917 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
1918 ;; it instead right after calling the function.
1919 (cond ((eq replace 'ignore)
1924 (ispell-send-string (concat "*" word "\n"))
1925 ;; This was added only to the XEmacs side in revision 1.18 of
1926 ;; flyspell. I assume its absence on the Emacs side was an
1927 ;; oversight. --Stef
1928 (ispell-send-string "#\n")
1929 (flyspell-unhighlight-at cursor-location)
1930 (setq ispell-pdict-modified-p '(t)))
1931 ((or (eq replace 'buffer) (eq replace 'session))
1932 (ispell-send-string (concat "@" word "\n"))
1933 (flyspell-unhighlight-at cursor-location)
1934 (if (null ispell-pdict-modified-p)
1935 (setq ispell-pdict-modified-p
1936 (list ispell-pdict-modified-p)))
1938 (if (eq replace 'buffer)
1939 (ispell-add-per-file-word-list word)))
1941 ;; This was added only to the Emacs side. I assume its absence on
1942 ;; the XEmacs side was an oversight. --Stef
1943 (flyspell-unhighlight-at cursor-location)
1944 (let ((old-max (point-max))
1945 (new-word (if (atom replace)
1948 (cursor-location (+ (- (length word) (- end start))
1950 (unless (equal new-word (car poss))
1951 (delete-region start end)
1953 (funcall flyspell-insert-function new-word)
1954 (if flyspell-abbrev-p
1955 (flyspell-define-abbrev word new-word)))
1956 ;; In the original Emacs code, this was only called in the body
1957 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
1958 (flyspell-ajust-cursor-point save cursor-location old-max)))
1963 ;*---------------------------------------------------------------------*/
1964 ;* flyspell-ajust-cursor-point ... */
1965 ;*---------------------------------------------------------------------*/
1966 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
1967 (if (>= save cursor-location)
1968 (let ((new-pos (+ save (- (point-max) old-max))))
1970 ((< new-pos (point-min))
1972 ((> new-pos (point-max))
1977 ;*---------------------------------------------------------------------*/
1978 ;* flyspell-emacs-popup ... */
1979 ;*---------------------------------------------------------------------*/
1980 (defun flyspell-emacs-popup (event poss word)
1981 "The Emacs popup menu."
1983 (let* ((mouse-pos (mouse-position))
1984 (mouse-pos (if (nth 1 mouse-pos)
1986 (set-mouse-position (car mouse-pos)
1987 (/ (frame-width) 2) 2)
1989 (setq event (list (list (car (cdr mouse-pos))
1990 (1+ (cdr (cdr mouse-pos))))
1992 (let* ((corrects (if flyspell-sort-corrections
1993 (sort (car (cdr (cdr poss))) 'string<)
1994 (car (cdr (cdr poss)))))
1995 (cor-menu (if (consp corrects)
1996 (mapcar (lambda (correct)
1997 (list correct correct))
2000 (affix (car (cdr (cdr (cdr poss)))))
2001 (base-menu (let ((save (if (consp affix)
2003 (list (concat "Save affix: " (car affix))
2005 '("Accept (session)" session)
2006 '("Accept (buffer)" buffer))
2007 '(("Save word" save)
2008 ("Accept (session)" session)
2009 ("Accept (buffer)" buffer)))))
2010 (if (consp cor-menu)
2011 (append cor-menu (cons "" save))
2013 (menu (cons "flyspell correction menu" base-menu)))
2014 (car (x-popup-menu event
2015 (list (format "%s [%s]" word (or ispell-local-dictionary
2019 ;*---------------------------------------------------------------------*/
2020 ;* flyspell-xemacs-popup ... */
2021 ;*---------------------------------------------------------------------*/
2022 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
2023 "The XEmacs popup menu."
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)
2030 (list 'flyspell-do-correct
2041 (affix (car (cdr (cdr (cdr poss)))))
2042 (menu (let ((save (if (consp affix)
2044 (concat "Save affix: " (car affix))
2045 (list 'flyspell-do-correct
2056 (list 'flyspell-do-correct
2065 (session (vector "Accept (session)"
2066 (list 'flyspell-do-correct
2075 (buffer (vector "Accept (buffer)"
2076 (list 'flyspell-do-correct
2085 (if (consp cor-menu)
2086 (append cor-menu (list "-" save session buffer))
2087 (list save session buffer)))))
2088 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2092 ;*---------------------------------------------------------------------*/
2093 ;* Some example functions for real autocorrecting */
2094 ;*---------------------------------------------------------------------*/
2095 (defun flyspell-maybe-correct-transposition (beg end poss)
2096 "Check replacements for transposed characters.
2098 If the text between BEG and END is equal to a correction suggested by
2099 Ispell, after transposing two adjacent characters, correct the text,
2102 The third arg POSS is either the symbol 'doublon' or a list of
2103 possible corrections as returned by `ispell-parse-output'.
2105 This function is meant to be added to `flyspell-incorrect-hook'."
2108 (let ((str (buffer-substring beg end))
2109 (i 0) (len (- end beg)) tmp)
2110 (while (< (1+ i) len)
2111 (setq tmp (aref str i))
2112 (aset str i (aref str (1+ i)))
2113 (aset str (1+ i) tmp)
2114 (when (member str (nth 2 poss))
2116 (goto-char (+ beg i 1))
2117 (transpose-chars 1))
2119 (setq tmp (aref str i))
2120 (aset str i (aref str (1+ i)))
2121 (aset str (1+ i) tmp)
2125 (defun flyspell-maybe-correct-doubling (beg end poss)
2126 "Check replacements for doubled characters.
2128 If the text between BEG and END is equal to a correction suggested by
2129 Ispell, after removing a pair of doubled characters, correct the text,
2132 The third arg POSS is either the symbol 'doublon' or a list of
2133 possible corrections as returned by `ispell-parse-output'.
2135 This function is meant to be added to `flyspell-incorrect-hook'."
2138 (let ((str (buffer-substring beg end))
2139 (i 0) (len (- end beg)))
2140 (while (< (1+ i) len)
2141 (when (and (= (aref str i) (aref str (1+ i)))
2142 (member (concat (substring str 0 (1+ i))
2143 (substring str (+ i 2)))
2145 (goto-char (+ beg i))
2151 ;*---------------------------------------------------------------------*/
2152 ;* flyspell-already-abbrevp ... */
2153 ;*---------------------------------------------------------------------*/
2154 (defun flyspell-already-abbrevp (table word)
2155 (let ((sym (abbrev-symbol word table)))
2156 (and sym (symbolp sym))))
2158 ;*---------------------------------------------------------------------*/
2159 ;* flyspell-change-abbrev ... */
2160 ;*---------------------------------------------------------------------*/
2161 (defun flyspell-change-abbrev (table old new)
2162 (set (abbrev-symbol old table) new))
2166 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2167 ;;; flyspell.el ends here