1 ;;; flyspell.el --- On-the-fly spell checker
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
5 ;; Author: Manuel Serrano <Manuel.Serrano@unice.fr>
6 ;; Keywords: convenience
8 ;;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
30 ;; To enter the flyspell minor mode, Meta-x flyspell-mode.
32 ;; Note: consider setting the variable ispell-parser to 'tex to
33 ;; avoid TeX command checking (use `(setq ispell-parser 'tex)')
34 ;; _before_ entering flyspell.
36 ;; Some user variables control the behavior of flyspell. They are
37 ;; those defined under the `User variables' comment.
39 ;; Note: as suggested by Yaron M. Minsky, if you use flyspell when
40 ;; sending mails, you should add the following:
41 ;; (add-hook 'mail-send-hook 'flyspell-mode-off)
47 (defgroup flyspell nil
48 "Spellchecking on the fly."
53 ;*---------------------------------------------------------------------*/
54 ;* User variables ... */
55 ;*---------------------------------------------------------------------*/
56 (defcustom flyspell-highlight-flag t
57 "*Non-nil means use highlight, nil means use mini-buffer messages."
61 (defcustom flyspell-doublon-as-error-flag t
62 "*Non-nil means consider doublon as misspelling."
66 (defcustom flyspell-sort-corrections t
67 "*Non-nil means, sort the corrections alphabetically before popping them."
71 (defcustom flyspell-incorrect-color "OrangeRed"
72 "*The color used for highlighting incorrect words."
76 (defcustom flyspell-duplicate-color "Gold3"
77 "*The color used for highlighting incorrect words but appearing at least twice."
81 (defcustom flyspell-underline-p t
82 "*Non-nil means, incorrect words are underlined."
86 (defcustom flyspell-auto-correct-binding
88 "*Non-nil means that its value (a binding) will bound to the flyspell
91 :type '(choice (const nil) string))
93 (defcustom flyspell-command-hook t
94 "*Non-nil means that `post-command-hook' is used to check
99 (defcustom flyspell-duplicate-distance -1
100 "*The distance from duplication.
106 (defcustom flyspell-delay 3
107 "*The number of second before checking words on post-command-hook if
108 the current command is a delay command."
112 (defcustom flyspell-persistent-highlight t
113 "*T means that hilighted words are not removed until the word are corrected."
117 (defcustom flyspell-highlight-properties t
118 "*T means highlight incorrect words even if a property exists for this word."
122 (defcustom flyspell-default-delayed-commands
123 '(self-insert-command
126 "The list of always delayed command (that is flyspell is not activated
127 after any of these commands."
129 :type '(repeat (symbol)))
131 (defcustom flyspell-delayed-commands
133 "*If non nil, this variable must hold a list a symbol. Each symbol is
134 the name of an delayed command (that is a command that does not activate
137 :type '(repeat (symbol)))
139 (defcustom flyspell-issue-welcome-flag t
140 "*Non-nil means that flyspell issues a welcome message when started."
144 (defcustom flyspell-consider-dash-as-word-delimiter-flag nil
145 "*Non-nil means that the `-' char is considered as a word delimiter."
149 (defcustom flyspell-incorrect-hook nil
150 "*Non-nil means a list of hooks to be executed when incorrect
151 words are encountered. Each hook is a function of two arguments that are
152 location of the beginning and the end of the incorrect region."
155 (defcustom flyspell-multi-language-p t
156 "*Non-nil means that flyspell could be use with several buffers checking
157 several languages. Non-nil means that a new ispell process will be spawned
158 per buffer. If nil, only one unique ispell process will be running."
162 ;*---------------------------------------------------------------------*/
163 ;* Mode specific options */
164 ;* ------------------------------------------------------------- */
165 ;* Mode specific options enable users to disable flyspell on */
166 ;* certain word depending of the emacs mode. For instance, when */
167 ;* using flyspell with mail-mode add the following expression */
168 ;* in your .emacs file: */
169 ;* (add-hook 'mail-mode */
170 ;* '(lambda () (setq flyspell-generic-check-word-p */
171 ;* 'mail-mode-flyspell-verify))) */
172 ;*---------------------------------------------------------------------*/
173 (defvar flyspell-generic-check-word-p nil
174 "Function providing per-mode customization over which words are flyspelled.
175 Returns t to continue checking, nil otherwise.")
176 (make-variable-buffer-local 'flyspell-generic-check-word-p)
178 (defun mail-mode-flyspell-verify ()
179 "Return t if we want flyspell to check the word under point."
183 (looking-at "Subject:"))
184 (not (or (re-search-forward mail-header-separator nil t)
185 (re-search-backward message-signature-separator nil t)
188 (looking-at "[>}|]")))))))
190 (defun texinfo-mode-flyspell-verify ()
191 "Return t if we want flyspell to check the word under point."
194 (not (looking-at "@"))))
196 ;*---------------------------------------------------------------------*/
197 ;* Overlay compatibility */
198 ;*---------------------------------------------------------------------*/
199 (autoload 'make-overlay "overlay" "" t)
200 (autoload 'move-overlay "overlay" "" t)
201 (autoload 'overlayp "overlay" "" t)
202 (autoload 'overlay-properties "overlay" "" t)
203 (autoload 'overlays-in "overlay" "" t)
204 (autoload 'delete-overlay "overlay" "" t)
205 (autoload 'overlays-at "overlay" "" t)
206 (autoload 'overlay-put "overlay" "" t)
207 (autoload 'overlay-get "overlay" "" t)
209 (defun flyspell-font-lock-make-face (l)
210 "Because emacs and xemacs does not behave the same I uses my owe
211 font-lock-make-face function. This function is similar to the gnu-emacs
212 font-lock-make-face function."
213 (let ((fname (car l))
214 (color (car (cdr l)))
215 (italic (car (cdr (cdr l))))
216 (bold (car (cdr (cdr (cdr l)))))
217 (underline (car (cdr (cdr (cdr (cdr l)))))))
218 (let ((face (copy-face 'default fname)))
220 (set-face-foreground face color))
221 (if (and italic bold)
223 (make-face-bold-italic face)
228 (make-face-italic face)
232 (make-face-bold face)
236 (set-face-underline-p face t)
238 (if (not (x-display-color-p))
240 (make-face-bold face)
244 ;*---------------------------------------------------------------------*/
245 ;* Which emacs are we currently running */
246 ;*---------------------------------------------------------------------*/
247 (defvar flyspell-emacs
249 ((string-match "XEmacs" emacs-version)
253 "The Emacs we are currently running.")
255 ;*---------------------------------------------------------------------*/
256 ;* cl compatibility */
257 ;*---------------------------------------------------------------------*/
258 (defmacro push (x place)
259 "(push X PLACE): insert X at the head of the list stored in PLACE.
260 Analogous to (setf PLACE (cons X PLACE)), though more careful about
261 evaluating each argument only once and in the right order. PLACE has
263 (list 'setq place (list 'cons x place)))
265 ;*---------------------------------------------------------------------*/
266 ;* The minor mode declaration. */
267 ;*---------------------------------------------------------------------*/
268 (defvar flyspell-mode nil)
269 (make-variable-buffer-local 'flyspell-mode)
271 (defvar flyspell-mode-map (make-sparse-keymap))
272 (defvar flyspell-mouse-map (make-sparse-keymap))
274 (or (assoc 'flyspell-mode minor-mode-alist)
275 (push '(flyspell-mode " Fly") minor-mode-alist))
277 (or (assoc 'flyspell-mode minor-mode-map-alist)
278 (push (cons 'flyspell-mode flyspell-mode-map) minor-mode-map-alist))
280 (if flyspell-auto-correct-binding
281 (define-key flyspell-mode-map flyspell-auto-correct-binding
282 (function flyspell-auto-correct-word)))
285 ((eq flyspell-emacs 'xemacs)
286 (define-key flyspell-mouse-map [(button2)]
287 (function flyspell-correct-word/mouse-keymap)))
289 (define-key flyspell-mode-map [(mouse-2)]
290 (function flyspell-correct-word/local-keymap))))
292 ;; the name of the overlay property that defines the keymap
293 (defvar flyspell-overlay-keymap-property-name
294 (if (string-match "19.*XEmacs" emacs-version)
298 ;*---------------------------------------------------------------------*/
300 ;*---------------------------------------------------------------------*/
301 (flyspell-font-lock-make-face (list 'flyspell-incorrect-face
302 flyspell-incorrect-color
305 flyspell-underline-p))
306 (flyspell-font-lock-make-face (list 'flyspell-duplicate-face
307 flyspell-duplicate-color
310 flyspell-underline-p))
312 (defvar flyspell-overlay nil)
314 ;*---------------------------------------------------------------------*/
315 ;* flyspell-mode ... */
316 ;*---------------------------------------------------------------------*/
318 (defun flyspell-mode (&optional arg)
319 "Minor mode performing on-the-fly spelling checking.
320 Ispell is automatically spawned on background for each entered words.
321 The default flyspells behavior is to highlight incorrect words.
322 With prefix ARG, turn Flyspell minor mode on iff ARG is positive.
325 \\[ispell-word]: correct words (using Ispell).
326 \\[flyspell-auto-correct-word]: automatically correct word.
327 \\[flyspell-correct-word] (or mouse-2): popup correct words.
330 flyspell-mode-hook is runner after flyspell is entered.
333 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
334 valid. For instance, a personal dictionary can be used by
335 invoking `ispell-change-dictionary'.
337 Consider using the `ispell-parser' to check your text. For instance
339 (add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
342 flyspell-region checks all words inside a region.
344 flyspell-buffer checks the whole buffer."
346 ;; we set the mode on or off
347 (setq flyspell-mode (not (or (and (null arg) flyspell-mode)
348 (<= (prefix-numeric-value arg) 0))))
352 ;; we force the modeline re-printing
353 (set-buffer-modified-p (buffer-modified-p)))
355 ;*---------------------------------------------------------------------*/
356 ;* flyspell-mode-on ... */
357 ;*---------------------------------------------------------------------*/
358 (defun flyspell-mode-on ()
359 "Turn flyspell mode on. Do not use, use `flyspell-mode' instead."
360 (message "flyspell on: %S" (current-buffer))
361 (setq ispell-highlight-face 'flyspell-incorrect-face)
362 ;; ispell initialization
363 (if flyspell-multi-language-p
365 (make-variable-buffer-local 'ispell-dictionary)
366 (make-variable-buffer-local 'ispell-process)
367 (make-variable-buffer-local 'ispell-filter)
368 (make-variable-buffer-local 'ispell-filter-continue)
369 (make-variable-buffer-local 'ispell-process-directory)
370 (make-variable-buffer-local 'ispell-parser)))
371 ;; we initialize delayed commands symbol
372 (flyspell-delay-commands)
373 ;; we bound flyspell action to post-command hook
374 (if flyspell-command-hook
376 (make-local-hook 'post-command-hook)
377 (add-hook 'post-command-hook
378 (function flyspell-post-command-hook)
381 ;; we bound flyspell action to pre-command hook
382 (if flyspell-command-hook
384 (make-local-hook 'pre-command-hook)
385 (add-hook 'pre-command-hook
386 (function flyspell-pre-command-hook)
389 ;; the welcome message
390 (if flyspell-issue-welcome-flag
392 (if flyspell-auto-correct-binding
393 (format "Welcome to flyspell. Use %S or mouse-2 to correct words."
394 (key-description flyspell-auto-correct-binding))
395 "Welcome to flyspell. Use mouse-2 to correct words.")))
396 ;; we have to kill the flyspell process when the buffer is deleted.
397 ;; (thanks to Jeff Miller and Roland Rosenfeld who sent me this
399 (add-hook 'kill-buffer-hook
402 (flyspell-mode-off))))
403 ;; we end with the flyspell hooks
404 (run-hooks 'flyspell-mode-hook))
406 ;*---------------------------------------------------------------------*/
407 ;* flyspell-delay-commands ... */
408 ;*---------------------------------------------------------------------*/
409 (defun flyspell-delay-commands ()
410 "Install the delayed command."
411 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
412 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
414 ;*---------------------------------------------------------------------*/
415 ;* flyspell-delay-command ... */
416 ;*---------------------------------------------------------------------*/
417 (defun flyspell-delay-command (command)
418 "Set COMMAND to be delayed.
419 When flyspell `post-command-hook' is invoked because a delayed command
420 as been used the current word is not immediatly checked.
421 It will be checked only after flyspell-delay second."
422 (interactive "Scommand: ")
423 (put command 'flyspell-delayed t))
425 ;*---------------------------------------------------------------------*/
426 ;* flyspell-ignore-commands ... */
427 ;*---------------------------------------------------------------------*/
428 (defun flyspell-ignore-commands ()
429 "This is an obsolete function, use flyspell-delays command instead."
430 (flyspell-delay-commands))
432 ;*---------------------------------------------------------------------*/
433 ;* flyspell-ignore-command ... */
434 ;*---------------------------------------------------------------------*/
435 (defun flyspell-ignore-command (command)
436 "This is an obsolete function, use flyspell-delay command instead.
437 COMMAND is the name of the command to be delayed."
438 (flyspell-delay-command command))
440 (make-obsolete 'flyspell-ignore-commands 'flyspell-delay-commands)
441 (make-obsolete 'flyspell-ignore-command 'flyspell-delay-command)
443 ;*---------------------------------------------------------------------*/
444 ;* flyspell-word-cache ... */
445 ;*---------------------------------------------------------------------*/
446 (defvar flyspell-word-cache-start nil)
447 (defvar flyspell-word-cache-end nil)
448 (defvar flyspell-word-cache-word nil)
449 (make-variable-buffer-local 'flyspell-word-cache-start)
450 (make-variable-buffer-local 'flyspell-word-cache-end)
451 (make-variable-buffer-local 'flyspell-word-cache-word)
453 ;*---------------------------------------------------------------------*/
454 ;* The flyspell pre-hook, store the current position. In the */
455 ;* post command hook, we will check, if the word at this position */
456 ;* has to be spell checked. */
457 ;*---------------------------------------------------------------------*/
458 (defvar flyspell-pre-buffer nil)
459 (defvar flyspell-pre-point nil)
461 ;*---------------------------------------------------------------------*/
462 ;* flyspell-pre-command-hook ... */
463 ;*---------------------------------------------------------------------*/
464 (defun flyspell-pre-command-hook ()
465 "This function is internally used by Flyspell to get a cursor location
466 before a user command."
468 (setq flyspell-pre-buffer (current-buffer))
469 (setq flyspell-pre-point (point)))
471 ;*---------------------------------------------------------------------*/
472 ;* flyspell-mode-off ... */
473 ;*---------------------------------------------------------------------*/
474 (defun flyspell-mode-off ()
475 "Turn flyspell mode off. Do not use. Use `flyspell-mode' instead."
476 ;; the bye-bye message
477 (message "Quiting Flyspell...%S" (current-buffer))
478 ;; we stop the running ispell
479 (ispell-kill-ispell t)
480 ;; we remove the hooks
481 (if flyspell-command-hook
483 (remove-hook 'post-command-hook
484 (function flyspell-post-command-hook)
486 (remove-hook 'pre-command-hook
487 (function flyspell-pre-command-hook)
489 ;; we remove all the flyspell hilightings
490 (flyspell-delete-all-overlays)
491 ;; we have to erase pre cache variables
492 (setq flyspell-pre-buffer nil)
493 (setq flyspell-pre-point nil)
494 ;; we mark the mode as killed
495 (setq flyspell-mode nil))
497 ;*---------------------------------------------------------------------*/
498 ;* flyspell-check-word-p ... */
499 ;*---------------------------------------------------------------------*/
500 (defun flyspell-check-word-p ()
501 "This function returns t when the word at `point' has to be
502 checked. The answer depends of several criteria. Mostly we
503 check word delimiters."
505 ((<= (- (point-max) 1) (point-min))
506 ;; the buffer is not filled enough
508 ((not (and (symbolp this-command) (get this-command 'flyspell-delayed)))
509 ;; the current command is not delayed, that
510 ;; is that we must check the word now
512 ((and (> (point) (point-min))
515 (and (looking-at (flyspell-get-not-casechars))
516 (or flyspell-consider-dash-as-word-delimiter-flag
517 (not (looking-at "\\-"))))))
518 ;; yes because we have reached or typed a word delimiter
520 ((not (integerp flyspell-delay))
521 ;; yes because the user had settup a non delay configuration
524 (if (fboundp 'about-xemacs)
525 (sit-for flyspell-delay nil)
526 (sit-for flyspell-delay 0 nil)))))
528 ;*---------------------------------------------------------------------*/
529 ;* flyspell-check-pre-word-p ... */
530 ;*---------------------------------------------------------------------*/
531 (defun flyspell-check-pre-word-p ()
532 "When to we have to check the word that was at point before
533 the current command?"
535 ((or (not (numberp flyspell-pre-point))
536 (not (bufferp flyspell-pre-buffer))
537 (not (buffer-live-p flyspell-pre-buffer)))
539 ((or (= flyspell-pre-point (- (point) 1))
540 (= flyspell-pre-point (point))
541 (= flyspell-pre-point (+ (point) 1)))
543 ((not (eq (current-buffer) flyspell-pre-buffer))
545 ((not (and (numberp flyspell-word-cache-start)
546 (numberp flyspell-word-cache-end)))
549 (or (< flyspell-pre-point flyspell-word-cache-start)
550 (> flyspell-pre-point flyspell-word-cache-end)))))
552 ;*---------------------------------------------------------------------*/
553 ;* flyspell-post-command-hook ... */
554 ;*---------------------------------------------------------------------*/
555 (defun flyspell-post-command-hook ()
556 "The `post-command-hook' used by flyspell to check a word in-the-fly."
558 (if (flyspell-check-word-p)
560 (if (flyspell-check-pre-word-p)
562 (set-buffer flyspell-pre-buffer)
564 (goto-char flyspell-pre-point)
567 ;*---------------------------------------------------------------------*/
568 ;* flyspell-word ... */
569 ;*---------------------------------------------------------------------*/
570 (defun flyspell-word (&optional following)
571 "Spell check a word."
572 (interactive (list current-prefix-arg))
574 (setq following ispell-following-word))
576 (ispell-accept-buffer-local-defs) ; use the correct dictionary
577 (let ((cursor-location (point)) ; retain cursor location
578 (word (flyspell-get-word following))
580 (if (or (eq word nil)
581 (and (fboundp flyspell-generic-check-word-p)
582 (not (funcall flyspell-generic-check-word-p))))
585 ;; destructure return word info list.
586 (setq start (car (cdr word))
587 end (car (cdr (cdr word)))
589 ;; before checking in the directory, we check for doublons.
591 ((and flyspell-doublon-as-error-flag
594 (word-search-backward word
598 ;; yes, this is a doublon
599 (flyspell-highlight-incorrect-region start end))
600 ((and (eq flyspell-word-cache-start start)
601 (eq flyspell-word-cache-end end)
602 (string-equal flyspell-word-cache-word word))
603 ;; this word had been already checked, we skip
605 ((and (eq ispell-parser 'tex)
606 (flyspell-tex-command-p word))
607 ;; this is a correct word (because a tex command)
608 (flyspell-unhighlight-at start)
610 (flyspell-unhighlight-at (- end 1)))
613 ;; we setup the cache
614 (setq flyspell-word-cache-start start)
615 (setq flyspell-word-cache-end end)
616 (setq flyspell-word-cache-word word)
617 ;; now check spelling of word.
618 (process-send-string ispell-process "%\n")
619 ;; put in verbose mode
620 (process-send-string ispell-process
621 (concat "^" word "\n"))
622 ;; wait until ispell has processed word
624 (accept-process-output ispell-process)
625 (not (string= "" (car ispell-filter)))))
626 ;; (process-send-string ispell-process "!\n")
627 ;; back to terse mode.
628 (setq ispell-filter (cdr ispell-filter))
629 (if (listp ispell-filter)
630 (setq poss (ispell-parse-output (car ispell-filter))))
633 (flyspell-unhighlight-at start)
635 (flyspell-unhighlight-at (- end 1)))
637 ((and (stringp poss) flyspell-highlight-flag)
639 (flyspell-unhighlight-at start)
641 (flyspell-unhighlight-at (- end 1)))
644 (flyspell-unhighlight-at start)
646 (flyspell-unhighlight-at (- end 1)))
647 (message "Error in ispell process"))
648 ((or (and (< flyspell-duplicate-distance 0)
651 (word-search-backward word
656 (word-search-forward word
659 (and (> flyspell-duplicate-distance 0)
662 (word-search-backward
665 flyspell-duplicate-distance)
672 flyspell-duplicate-distance)
674 (if flyspell-highlight-flag
675 (flyspell-highlight-duplicate-region start end)
676 (message (format "misspelling duplicate `%s'"
679 ;; incorrect highlight the location
680 (if flyspell-highlight-flag
681 (flyspell-highlight-incorrect-region start end)
682 (message (format "mispelling `%s'" word)))))
683 (goto-char cursor-location) ; return to original location
684 (if ispell-quit (setq ispell-quit nil)))))))))
686 ;*---------------------------------------------------------------------*/
687 ;* flyspell-tex-command-p ... */
688 ;*---------------------------------------------------------------------*/
689 (defun flyspell-tex-command-p (word)
690 "Is a word a TeX command?"
691 (eq (aref word 0) ?\\))
693 ;*---------------------------------------------------------------------*/
694 ;* flyspell-casechars-cache ... */
695 ;*---------------------------------------------------------------------*/
696 (defvar flyspell-casechars-cache nil)
697 (defvar flyspell-ispell-casechars-cache nil)
698 (make-variable-buffer-local 'flyspell-casechars-cache)
699 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
701 ;*---------------------------------------------------------------------*/
702 ;* flyspell-get-casechars ... */
703 ;*---------------------------------------------------------------------*/
704 (defun flyspell-get-casechars ()
705 "This function builds a string that is the regexp of word chars.
707 to avoid one useless string construction, this function changes the last
708 char of the ispell-casechars string."
709 (let ((ispell-casechars (ispell-get-casechars)))
711 ((eq ispell-casechars flyspell-ispell-casechars-cache)
712 flyspell-casechars-cache)
713 ((not (eq ispell-parser 'tex))
714 (setq flyspell-ispell-casechars-cache ispell-casechars)
715 (setq flyspell-casechars-cache
716 (concat (substring ispell-casechars
718 (- (length ispell-casechars) 1))
720 flyspell-casechars-cache)
722 (setq flyspell-ispell-casechars-cache ispell-casechars)
723 (setq flyspell-casechars-cache ispell-casechars)
724 flyspell-casechars-cache))))
726 ;*---------------------------------------------------------------------*/
727 ;* flyspell-get-not-casechars-cache ... */
728 ;*---------------------------------------------------------------------*/
729 (defvar flyspell-not-casechars-cache nil)
730 (defvar flyspell-ispell-not-casechars-cache nil)
731 (make-variable-buffer-local 'flyspell-not-casechars-cache)
732 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
734 ;*---------------------------------------------------------------------*/
735 ;* flyspell-get-not-casechars ... */
736 ;*---------------------------------------------------------------------*/
737 (defun flyspell-get-not-casechars ()
738 "This function builds a string that is the regexp of non-word chars."
739 (let ((ispell-not-casechars (ispell-get-not-casechars)))
741 ((eq ispell-not-casechars flyspell-ispell-not-casechars-cache)
742 flyspell-not-casechars-cache)
743 ((not (eq ispell-parser 'tex))
744 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
745 (setq flyspell-not-casechars-cache
746 (concat (substring ispell-not-casechars
748 (- (length ispell-not-casechars) 1))
750 flyspell-not-casechars-cache)
752 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
753 (setq flyspell-not-casechars-cache ispell-not-casechars)
754 flyspell-not-casechars-cache))))
756 ;*---------------------------------------------------------------------*/
757 ;* flyspell-get-word ... */
758 ;*---------------------------------------------------------------------*/
759 (defun flyspell-get-word (following)
760 "Return the word for spell-checking according to Ispell syntax.
761 If optional argument FOLLOWING is non-nil or if `ispell-following-word'
762 is non-nil when called interactively, then the following word
763 \(rather than preceding\) is checked when the cursor is not over a word.
764 Optional second argument contains otherchars that can be included in word
767 Word syntax described by `ispell-dictionary-alist' (which see)."
768 (let* ((flyspell-casechars (flyspell-get-casechars))
769 (flyspell-not-casechars (flyspell-get-not-casechars))
770 (ispell-otherchars (ispell-get-otherchars))
771 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
772 (word-regexp (concat flyspell-casechars
778 (if ispell-many-otherchars-p
780 (tex-prelude "[\\\\{]")
781 (tex-regexp (if (eq ispell-parser 'tex)
782 (concat tex-prelude "?" word-regexp "}?")
788 (if (not (or (looking-at flyspell-casechars)
789 (and (eq ispell-parser 'tex)
790 (looking-at tex-prelude))))
792 (re-search-forward flyspell-casechars (point-max) t)
793 (re-search-backward flyspell-casechars (point-min) t)))
794 ;; move to front of word
795 (re-search-backward flyspell-not-casechars (point-min) 'start)
797 (while (and (looking-at ispell-otherchars)
799 (or (not did-it-once)
800 ispell-many-otherchars-p)
801 (not (eq pos (point))))
805 (if (looking-at flyspell-casechars)
806 (re-search-backward flyspell-not-casechars (point-min) 'move)
807 (backward-char -1))))
808 ;; Now mark the word and save to string.
809 (if (eq (re-search-forward tex-regexp (point-max) t) nil)
812 (setq start (match-beginning 0)
814 word (buffer-substring start end))
815 (list word start end)))))
817 ;*---------------------------------------------------------------------*/
818 ;* flyspell-region ... */
819 ;*---------------------------------------------------------------------*/
820 (defun flyspell-region (beg end)
821 "Flyspell text between BEG and END."
826 (while (< (point) end)
829 (message "Spell Checking...%d%%"
830 (* 100 (/ (float (point)) (- end beg))))
832 (setq count (+ 1 count)))
836 (if (and (< (point) end) (> (point) (+ cur 1)))
837 (backward-char 1)))))
839 (message "Spell Checking...done")
842 ;*---------------------------------------------------------------------*/
843 ;* flyspell-buffer ... */
844 ;*---------------------------------------------------------------------*/
845 (defun flyspell-buffer ()
846 "Flyspell whole buffer."
848 (flyspell-region (point-min) (point-max)))
850 ;*---------------------------------------------------------------------*/
851 ;* flyspell-overlay-p ... */
852 ;*---------------------------------------------------------------------*/
853 (defun flyspell-overlay-p (o)
854 "A predicate that return true iff O is an overlay used by flyspell."
855 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
857 ;*---------------------------------------------------------------------*/
858 ;* flyspell-delete-all-overlays ... */
859 ;* ------------------------------------------------------------- */
860 ;* Remove all the overlays introduced by flyspell. */
861 ;*---------------------------------------------------------------------*/
862 (defun flyspell-delete-all-overlays ()
863 "Delete all the overlays used by flyspell."
864 (let ((l (overlays-in (point-min) (point-max))))
867 (if (flyspell-overlay-p (car l))
868 (delete-overlay (car l)))
871 ;*---------------------------------------------------------------------*/
872 ;* flyspell-unhighlight-at ... */
873 ;*---------------------------------------------------------------------*/
874 (defun flyspell-unhighlight-at (pos)
875 "Remove the flyspell overlay that are located at POS."
876 (if flyspell-persistent-highlight
877 (let ((overlays (overlays-at pos)))
878 (while (consp overlays)
879 (if (flyspell-overlay-p (car overlays))
880 (delete-overlay (car overlays)))
881 (setq overlays (cdr overlays))))
882 (delete-overlay flyspell-overlay)))
884 ;*---------------------------------------------------------------------*/
885 ;* flyspell-properties-at-p ... */
886 ;* ------------------------------------------------------------- */
887 ;* Is there an highlight properties at position pos? */
888 ;*---------------------------------------------------------------------*/
889 (defun flyspell-properties-at-p (beg)
890 "Return the text property at position BEG."
891 (let ((prop (text-properties-at beg))
893 (while (and keep (consp prop))
894 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
895 (setq prop (cdr (cdr prop)))
899 ;*---------------------------------------------------------------------*/
900 ;* make-flyspell-overlay ... */
901 ;*---------------------------------------------------------------------*/
902 (defun make-flyspell-overlay (beg end face mouse-face)
903 "Allocate a new flyspell overlay that will be used to hilight
905 (let ((flyspell-overlay (make-overlay beg end)))
906 (overlay-put flyspell-overlay 'face face)
907 (overlay-put flyspell-overlay 'mouse-face mouse-face)
908 (overlay-put flyspell-overlay 'flyspell-overlay t)
909 (if (eq flyspell-emacs 'xemacs)
910 (overlay-put flyspell-overlay
911 flyspell-overlay-keymap-property-name
912 flyspell-mouse-map))))
914 ;*---------------------------------------------------------------------*/
915 ;* flyspell-highlight-incorrect-region ... */
916 ;*---------------------------------------------------------------------*/
917 (defun flyspell-highlight-incorrect-region (beg end)
918 "The setup of an overlay on a region (starting at BEG and ending at END)
919 that corresponds to an incorrect word."
920 (run-hook-with-args 'flyspell-incorrect-hook beg end)
921 (if (or (not (flyspell-properties-at-p beg)) flyspell-highlight-properties)
923 ;; we cleanup current overlay at the same position
924 (if (and (not flyspell-persistent-highlight)
925 (overlayp flyspell-overlay))
926 (delete-overlay flyspell-overlay)
927 (let ((overlays (overlays-at beg)))
928 (while (consp overlays)
929 (if (flyspell-overlay-p (car overlays))
930 (delete-overlay (car overlays)))
931 (setq overlays (cdr overlays)))))
932 ;; now we can use a new overlay
933 (setq flyspell-overlay
934 (make-flyspell-overlay beg end
935 'flyspell-incorrect-face 'highlight)))))
937 ;*---------------------------------------------------------------------*/
938 ;* flyspell-highlight-duplicate-region ... */
939 ;*---------------------------------------------------------------------*/
940 (defun flyspell-highlight-duplicate-region (beg end)
941 "The setup of an overlay on a region (starting at BEG and ending at END)
942 that corresponds to an duplicated word."
943 (if (or (not (flyspell-properties-at-p beg)) flyspell-highlight-properties)
945 ;; we cleanup current overlay at the same position
946 (if (and (not flyspell-persistent-highlight)
947 (overlayp flyspell-overlay))
948 (delete-overlay flyspell-overlay)
949 (let ((overlays (overlays-at beg)))
950 (while (consp overlays)
951 (if (flyspell-overlay-p (car overlays))
952 (delete-overlay (car overlays)))
953 (setq overlays (cdr overlays)))))
954 ;; now we can use a new overlay
955 (setq flyspell-overlay
956 (make-flyspell-overlay beg end
957 'flyspell-duplicate-face 'highlight)))))
959 ;*---------------------------------------------------------------------*/
960 ;* flyspell-auto-correct-cache ... */
961 ;*---------------------------------------------------------------------*/
962 (defvar flyspell-auto-correct-pos nil)
963 (defvar flyspell-auto-correct-region nil)
964 (defvar flyspell-auto-correct-ring nil)
966 ;*---------------------------------------------------------------------*/
967 ;* flyspell-auto-correct-word ... */
968 ;*---------------------------------------------------------------------*/
969 (defun flyspell-auto-correct-word (pos)
970 "Auto correct the word at position POS."
972 ;; use the correct dictionary
973 (ispell-accept-buffer-local-defs)
974 (if (eq flyspell-auto-correct-pos pos)
975 ;; we have already been using the function at the same location
978 (let ((start (car flyspell-auto-correct-region))
979 (len (cdr flyspell-auto-correct-region)))
980 (delete-region start (+ start len))
981 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
982 (let* ((word (car flyspell-auto-correct-ring))
984 (rplacd flyspell-auto-correct-region len)
987 (setq flyspell-auto-correct-pos (point)))
988 ;; retain cursor location
989 (let ((cursor-location pos)
990 (word (flyspell-get-word nil))
992 ;; destructure return word info list.
993 (setq start (car (cdr word))
994 end (car (cdr (cdr word)))
996 ;; now check spelling of word.
997 (process-send-string ispell-process "%\n") ;put in verbose mode
998 (process-send-string ispell-process (concat "^" word "\n"))
999 ;; wait until ispell has processed word
1001 (accept-process-output ispell-process)
1002 (not (string= "" (car ispell-filter)))))
1003 (setq ispell-filter (cdr ispell-filter))
1004 (if (listp ispell-filter)
1005 (setq poss (ispell-parse-output (car ispell-filter))))
1006 (cond ((or (eq poss t) (stringp poss))
1007 ;; don't correct word
1011 (error "Ispell: error in Ispell process"))
1013 ;; the word is incorrect, we have to propose a replacement
1014 (let ((replacements (if flyspell-sort-corrections
1015 (sort (car (cdr (cdr poss))) 'string<)
1016 (car (cdr (cdr poss))))))
1017 (if (consp replacements)
1019 (let ((replace (car replacements)))
1021 (setq cursor-location (+ (- (length word) (- end start))
1023 (if (not (equal word (car poss)))
1025 ;; the save the current replacements
1026 (setq flyspell-auto-correct-pos cursor-location)
1027 (setq flyspell-auto-correct-region
1028 (cons start (length word)))
1029 (let ((l replacements))
1030 (while (consp (cdr l))
1032 (rplacd l (cons (car poss) replacements)))
1033 (setq flyspell-auto-correct-ring
1035 (delete-region start end)
1036 (insert word)))))))))
1037 ;; return to original location
1038 (goto-char cursor-location)
1039 (ispell-pdict-save t))))
1041 ;*---------------------------------------------------------------------*/
1042 ;* flyspell-correct-word ... */
1043 ;*---------------------------------------------------------------------*/
1044 (defun flyspell-correct-word (event)
1045 "Check spelling of word under or before the cursor.
1046 If the word is not found in dictionary, display possible corrections
1047 in a popup menu allowing you to choose one.
1049 Word syntax described by `ispell-dictionary-alist' (which see).
1051 This will check or reload the dictionary. Use \\[ispell-change-dictionary]
1052 or \\[ispell-region] to update the Ispell process."
1054 (if (eq flyspell-emacs 'xemacs)
1055 (flyspell-correct-word/mouse-keymap event)
1056 (flyspell-correct-word/local-keymap event)))
1058 ;*---------------------------------------------------------------------*/
1059 ;* flyspell-correct-word/local-keymap ... */
1060 ;*---------------------------------------------------------------------*/
1061 (defun flyspell-correct-word/local-keymap (event)
1062 "emacs 19.xx seems to be buggous. Overlay keymap does not seems
1063 to work correctly with local map. That is, if a key is not
1064 defined for the overlay keymap, the current local map, is not
1065 checked. The binding is resolved with the global map. The
1066 consequence is that we can not use overlay map with flyspell."
1068 (save-window-excursion
1069 (let ((save (point)))
1070 (mouse-set-point event)
1071 ;; we look for a flyspell overlay here
1072 (let ((overlays (overlays-at (point)))
1074 (while (consp overlays)
1075 (if (flyspell-overlay-p (car overlays))
1077 (setq overlay (car overlays))
1078 (setq overlays nil))
1079 (setq overlays (cdr overlays))))
1080 ;; we return to the correct location
1082 ;; we check to see if button2 has been used overlay a
1085 ;; yes, so we use the flyspell function
1086 (flyspell-correct-word/mouse-keymap event)
1087 ;; no so we have to use the non flyspell binding
1088 (let ((flyspell-mode nil))
1089 (if (key-binding (this-command-keys))
1090 (command-execute (key-binding (this-command-keys))))))))))
1092 ;*---------------------------------------------------------------------*/
1093 ;* flyspell-correct-word ... */
1094 ;*---------------------------------------------------------------------*/
1095 (defun flyspell-correct-word/mouse-keymap (event)
1096 "Popup a menu to present possible correction. The word checked is the
1097 word at the mouse position."
1099 ;; use the correct dictionary
1100 (ispell-accept-buffer-local-defs)
1101 ;; retain cursor location (I don't know why but save-excursion here fails).
1102 (let ((save (point)))
1103 (mouse-set-point event)
1104 (let ((cursor-location (point))
1105 (word (flyspell-get-word nil))
1106 start end poss replace)
1107 ;; destructure return word info list.
1108 (setq start (car (cdr word))
1109 end (car (cdr (cdr word)))
1111 ;; now check spelling of word.
1112 (process-send-string ispell-process "%\n") ;put in verbose mode
1113 (process-send-string ispell-process (concat "^" word "\n"))
1114 ;; wait until ispell has processed word
1116 (accept-process-output ispell-process)
1117 (not (string= "" (car ispell-filter)))))
1118 (setq ispell-filter (cdr ispell-filter))
1119 (if (listp ispell-filter)
1120 (setq poss (ispell-parse-output (car ispell-filter))))
1121 (cond ((or (eq poss t) (stringp poss))
1122 ;; don't correct word
1126 (error "Ispell: error in Ispell process"))
1127 ((string-match "GNU" (emacs-version))
1128 ;; the word is incorrect, we have to propose a replacement
1129 (setq replace (flyspell-gnuemacs-popup event poss word))
1130 (cond ((eq replace 'ignore)
1133 (process-send-string ispell-process (concat "*" word "\n"))
1134 (flyspell-unhighlight-at cursor-location)
1135 (setq ispell-pdict-modified-p '(t)))
1136 ((or (eq replace 'buffer) (eq replace 'session))
1137 (process-send-string ispell-process (concat "@" word "\n"))
1138 (if (null ispell-pdict-modified-p)
1139 (setq ispell-pdict-modified-p
1140 (list ispell-pdict-modified-p)))
1141 (flyspell-unhighlight-at cursor-location)
1142 (if (eq replace 'buffer)
1143 (ispell-add-per-file-word-list word)))
1145 (setq word (if (atom replace) replace (car replace))
1146 cursor-location (+ (- (length word) (- end start))
1148 (if (not (equal word (car poss)))
1150 (delete-region start end)
1152 ((string-match "XEmacs" (emacs-version))
1153 (flyspell-xemacs-popup
1154 event poss word cursor-location start end)))
1155 (ispell-pdict-save t))
1156 (if (< save (point-max))
1158 (goto-char (point-max)))))
1160 ;*---------------------------------------------------------------------*/
1161 ;* flyspell-xemacs-correct ... */
1162 ;*---------------------------------------------------------------------*/
1163 (defun flyspell-xemacs-correct (replace poss word cursor-location start end)
1164 "The xemacs popup menu callback."
1165 (cond ((eq replace 'ignore)
1168 (process-send-string ispell-process (concat "*" word "\n"))
1169 (flyspell-unhighlight-at cursor-location)
1170 (setq ispell-pdict-modified-p '(t)))
1171 ((or (eq replace 'buffer) (eq replace 'session))
1172 (process-send-string ispell-process (concat "@" word "\n"))
1173 (flyspell-unhighlight-at cursor-location)
1174 (if (null ispell-pdict-modified-p)
1175 (setq ispell-pdict-modified-p
1176 (list ispell-pdict-modified-p)))
1177 (if (eq replace 'buffer)
1178 (ispell-add-per-file-word-list word)))
1180 (setq word (if (atom replace) replace (car replace))
1181 cursor-location (+ (- (length word) (- end start))
1183 (if (not (equal word (car poss)))
1185 (delete-region start end)
1189 ;*---------------------------------------------------------------------*/
1190 ;* flyspell-gnuemacs-popup */
1191 ;*---------------------------------------------------------------------*/
1192 (defun flyspell-gnuemacs-popup (event poss word)
1193 "The gnu-emacs popup menu."
1195 (let* ((mouse-pos (mouse-position))
1196 (mouse-pos (if (nth 1 mouse-pos)
1198 (set-mouse-position (car mouse-pos)
1199 (/ (frame-width) 2) 2)
1202 (setq event (list (list (car (cdr mouse-pos))
1203 (1+ (cdr (cdr mouse-pos))))
1205 (let* ((corrects (if flyspell-sort-corrections
1206 (sort (car (cdr (cdr poss))) 'string<)
1207 (car (cdr (cdr poss)))))
1208 (cor-menu (if (consp corrects)
1209 (mapcar (lambda (correct)
1210 (list correct correct))
1213 (affix (car (cdr (cdr (cdr poss)))))
1214 (base-menu (let ((save (if (consp affix)
1216 (list (concat "Save affix: " (car affix))
1218 '("Accept (session)" accept)
1219 '("Accept (buffer)" buffer))
1220 '(("Save word" save)
1221 ("Accept (session)" session)
1222 ("Accept (buffer)" buffer)))))
1223 (if (consp cor-menu)
1224 (append cor-menu (cons "" save))
1226 (menu (cons "flyspell correction menu" base-menu)))
1227 (car (x-popup-menu event
1228 (list (format "%s [%s]" word (or ispell-local-dictionary
1232 ;*---------------------------------------------------------------------*/
1233 ;* flyspell-xemacs-popup */
1234 ;*---------------------------------------------------------------------*/
1235 (defun flyspell-xemacs-popup (event poss word cursor-location start end)
1236 "The xemacs popup menu."
1237 (let* ((corrects (if flyspell-sort-corrections
1238 (sort (car (cdr (cdr poss))) 'string<)
1239 (car (cdr (cdr poss)))))
1240 (cor-menu (if (consp corrects)
1241 (mapcar (lambda (correct)
1243 (list 'flyspell-xemacs-correct
1253 (affix (car (cdr (cdr (cdr poss)))))
1254 (menu (let ((save (if (consp affix)
1256 (concat "Save affix: " (car affix))
1257 (list 'flyspell-xemacs-correct
1267 (list 'flyspell-xemacs-correct
1275 (session (vector "Accept (session)"
1276 (list 'flyspell-xemacs-correct
1284 (buffer (vector "Accept (buffer)"
1285 (list 'flyspell-xemacs-correct
1293 (if (consp cor-menu)
1294 (append cor-menu (list "-" save session buffer))
1295 (list save session buffer)))))
1296 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
1302 ;;; flyspell.el ends here