1 ;;; flyspell.el --- On-the-fly spell checker
3 ;; Copyright (C) 1998, 2000 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 enable Flyspell minor mode, type Meta-x flyspell-mode.
31 ;; This applies only to the current buffer.
33 ;; Note: consider setting the variable ispell-parser to `tex' to
34 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'
35 ;; _before_ entering flyspell.
37 ;; Some user variables control the behavior of flyspell. They are
38 ;; those defined under the `User variables' comment.
40 ;; Note: as suggested by Yaron M. Minsky, if you use flyspell when
41 ;; sending mails, you should add the following:
42 ;; (add-hook 'mail-send-hook 'flyspell-mode-off)
47 ;*---------------------------------------------------------------------*/
49 ;*---------------------------------------------------------------------*/
50 (defgroup flyspell nil
51 "Spellchecking on the fly."
57 ;*---------------------------------------------------------------------*/
58 ;* User variables ... */
59 ;*---------------------------------------------------------------------*/
60 (defcustom flyspell-highlight-flag t
61 "*How Flyspell should indicate misspelled words.
62 Non-nil means use highlight, nil means use minibuffer messages."
66 (defcustom flyspell-mark-duplications-flag t
67 "*Non-nil means Flyspell reports a repeated word as an error."
71 (defcustom flyspell-sort-corrections t
72 "*Non-nil means, sort the corrections alphabetically before popping them."
76 (defcustom flyspell-duplicate-distance 10000
77 "*The maximum distance for finding duplicates of unrecognized words.
78 This applies to the feature that when a word is not found in the dictionary,
79 if the same spelling occurs elsewhere in the buffer,
80 Flyspell uses a different face (`flyspell-duplicate-face') to highlight it.
81 This variable specifies how far to search to find such a duplicate.
82 -1 means no limit (search the whole buffer).
83 0 means do not search for duplicate unrecognized spellings."
87 (defcustom flyspell-delay 3
88 "*The number of seconds to wait before checking, after a \"delayed\" command."
92 (defcustom flyspell-persistent-highlight t
93 "*Non-nil means misspelled words remain highlighted until corrected.
94 If this variable is nil, only the most recently detected misspelled word
99 (defcustom flyspell-highlight-properties t
100 "*Non-nil means highlight incorrect words even if a property exists for this word."
104 (defcustom flyspell-default-delayed-commands
105 '(self-insert-command
108 "The standard list of delayed commands for Flyspell.
109 See `flyspell-delayed-commands'."
111 :type '(repeat (symbol)))
113 (defcustom flyspell-delayed-commands nil
114 "List of commands that are \"delayed\" for Flyspell mode.
115 After these commands, Flyspell checking is delayed for a short time,
116 whose length is specified by `flyspell-delay'."
118 :type '(repeat (symbol)))
120 (defcustom flyspell-issue-welcome-flag t
121 "*Non-nil means that Flyspell should display a welcome message when started."
125 (defcustom flyspell-consider-dash-as-word-delimiter-flag nil
126 "*Non-nil means that the `-' char is considered as a word delimiter."
130 (defcustom flyspell-incorrect-hook nil
131 "*List of functions to be called when incorrect words are encountered.
132 Each function is given two arguments: the beginning and the end
133 of the incorrect region."
136 (defcustom flyspell-multi-language-p nil
137 "*Non-nil means that Flyspell can be used with multiple languages.
138 This mode works by starting a separate Ispell process for each buffer,
139 so that each buffer can use its own language."
143 ;*---------------------------------------------------------------------*/
144 ;* Mode specific options */
145 ;* ------------------------------------------------------------- */
146 ;* Mode specific options enable users to disable flyspell on */
147 ;* certain word depending of the emacs mode. For instance, when */
148 ;* using flyspell with mail-mode add the following expression */
149 ;* in your .emacs file: */
150 ;* (add-hook 'mail-mode */
151 ;* '(lambda () (setq flyspell-generic-check-word-p */
152 ;* 'mail-mode-flyspell-verify))) */
153 ;*---------------------------------------------------------------------*/
154 (defvar flyspell-generic-check-word-p nil
155 "Function providing per-mode customization over which words are flyspelled.
156 Returns t to continue checking, nil otherwise.
157 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
158 property of the major mode name.")
159 (make-variable-buffer-local 'flyspell-generic-check-word-p)
161 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
162 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
163 (defun mail-mode-flyspell-verify ()
164 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
168 (looking-at "Subject:"))
169 (not (or (re-search-forward mail-header-separator nil t)
170 (re-search-backward message-signature-separator nil t)
173 (looking-at "[>}|]")))))))
175 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
176 (defun texinfo-mode-flyspell-verify ()
177 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
180 (not (looking-at "@"))))
182 ;*---------------------------------------------------------------------*/
183 ;* Overlay compatibility */
184 ;*---------------------------------------------------------------------*/
185 (autoload 'make-overlay "overlay" "" t)
186 (autoload 'move-overlay "overlay" "" t)
187 (autoload 'overlayp "overlay" "" t)
188 (autoload 'overlay-properties "overlay" "" t)
189 (autoload 'overlays-in "overlay" "" t)
190 (autoload 'delete-overlay "overlay" "" t)
191 (autoload 'overlays-at "overlay" "" t)
192 (autoload 'overlay-put "overlay" "" t)
193 (autoload 'overlay-get "overlay" "" t)
195 ;*---------------------------------------------------------------------*/
196 ;* Which emacs are we currently running */
197 ;*---------------------------------------------------------------------*/
198 (defvar flyspell-emacs
200 ((string-match "XEmacs" emacs-version)
204 "The type of Emacs we are currently running.")
206 (defvar flyspell-use-local-map
207 (or (eq flyspell-emacs 'xemacs)
208 (not (string< emacs-version "20"))))
210 ;*---------------------------------------------------------------------*/
211 ;* The minor mode declaration. */
212 ;*---------------------------------------------------------------------*/
213 (defvar flyspell-mode nil)
214 (make-variable-buffer-local 'flyspell-mode)
216 (defvar flyspell-mouse-map
217 (let ((map (make-sparse-keymap)))
219 ((eq flyspell-emacs 'xemacs)
220 (define-key map [(button2)]
221 #'flyspell-correct-word/mouse-keymap)
222 (define-key flyspell-mouse-map "\M-\t" #'flyspell-auto-correct-word))
223 (flyspell-use-local-map
224 (define-key map [(mouse-2)] #'flyspell-correct-word/mouse-keymap)
225 (define-key map "\M-\t" #'flyspell-auto-correct-word)))
227 (defvar flyspell-mode-map (make-sparse-keymap))
229 (or (assoc 'flyspell-mode minor-mode-alist)
230 (setq minor-mode-alist
231 (cons '(flyspell-mode " Fly") minor-mode-alist)))
233 ;; mouse or local-map bindings
234 (when (or (assoc 'flyspell-mode minor-mode-map-alist)
235 (setq minor-mode-map-alist
236 (cons (cons 'flyspell-mode flyspell-mode-map)
237 minor-mode-map-alist)))
238 (define-key flyspell-mode-map "\M-\t" 'flyspell-auto-correct-word)
239 (define-key flyspell-mode-map [(mouse-2)]
240 (function flyspell-correct-word/local-keymap)))
242 ;; the name of the overlay property that defines the keymap
243 (defvar flyspell-overlay-keymap-property-name
244 (if (string-match "19.*XEmacs" emacs-version)
248 ;*---------------------------------------------------------------------*/
250 ;*---------------------------------------------------------------------*/
251 (defface flyspell-incorrect-face
252 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
254 "Face used for marking a misspelled word in Flyspell."
257 (defface flyspell-duplicate-face
258 '((((class color)) (:foreground "Gold3" :bold t :underline t))
260 "Face used for marking a misspelled word that appears twice in the buffer.
261 See also `flyspell-duplicate-distance'."
264 (defvar flyspell-overlay nil)
266 ;*---------------------------------------------------------------------*/
267 ;* flyspell-mode ... */
268 ;*---------------------------------------------------------------------*/
270 (defun flyspell-mode (&optional arg)
271 "Minor mode performing on-the-fly spelling checking.
272 Ispell is automatically spawned on background for each entered words.
273 The default flyspell behavior is to highlight incorrect words.
274 With no argument, this command toggles Flyspell mode.
275 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
278 \\[ispell-word]: correct words (using Ispell).
279 \\[flyspell-auto-correct-word]: automatically correct word.
280 \\[flyspell-correct-word] (or mouse-2): popup correct words.
283 flyspell-mode-hook is run after flyspell is entered.
286 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
287 valid. For instance, a personal dictionary can be used by
288 invoking `ispell-change-dictionary'.
290 Consider using the `ispell-parser' to check your text. For instance
292 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
295 flyspell-region checks all words inside a region.
297 flyspell-buffer checks the whole buffer."
299 (let ((old-flyspell-mode flyspell-mode))
300 ;; Mark the mode as on or off.
301 (setq flyspell-mode (not (or (and (null arg) flyspell-mode)
302 (<= (prefix-numeric-value arg) 0))))
304 (unless (eq flyspell-mode old-flyspell-mode)
308 ;; Force modeline redisplay.
309 (set-buffer-modified-p (buffer-modified-p)))))
311 ;*---------------------------------------------------------------------*/
312 ;* flyspell-mode-on ... */
313 ;*---------------------------------------------------------------------*/
314 (eval-when-compile (defvar flyspell-local-mouse-map))
316 (defun flyspell-mode-on ()
317 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
318 (setq ispell-highlight-face 'flyspell-incorrect-face)
319 ;; ispell initialization
320 (if flyspell-multi-language-p
322 (make-variable-buffer-local 'ispell-dictionary)
323 (make-variable-buffer-local 'ispell-process)
324 (make-variable-buffer-local 'ispell-filter)
325 (make-variable-buffer-local 'ispell-filter-continue)
326 (make-variable-buffer-local 'ispell-process-directory)
327 (make-variable-buffer-local 'ispell-parser)
328 (put 'ispell-dictionary 'permanent-local t)
329 (put 'ispell-process 'permanent-local t)
330 (put 'ispell-filter 'permanent-local t)
331 (put 'ispell-filter-continue 'permanent-local t)
332 (put 'ispell-process-directory 'permanent-local t)
333 (put 'ispell-parser 'permanent-local t)))
334 ;; We put the `flyspell-delayed' property on some commands.
335 (flyspell-delay-commands)
336 ;; we bound flyspell action to post-command hook
337 (make-local-hook 'post-command-hook)
338 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
339 ;; we bound flyspell action to pre-command hook
340 (make-local-hook 'pre-command-hook)
341 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
343 ;; Set flyspell-generic-check-word-p based on the major mode.
344 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
346 (setq flyspell-generic-check-word-p mode-predicate)))
348 ;; the welcome message
349 (if flyspell-issue-welcome-flag
350 (let ((binding (where-is-internal 'flyspell-auto-correct-word
354 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
355 (key-description binding))
356 "Welcome to flyspell. Use Mouse-2 to correct words."))))
357 ;; we have to kill the flyspell process when the buffer is deleted.
358 ;; (thanks to Jeff Miller and Roland Rosenfeld who sent me this
360 (add-hook 'kill-buffer-hook
362 (if (and flyspell-multi-language-p ispell-process)
363 (ispell-kill-ispell t))))
364 (make-local-hook 'change-major-mode-hook)
365 (add-hook 'change-major-mode-hook 'flyspell-mode-off)
366 ;; Use this so that we can still get major mode bindings at a
367 ;; misspelled word (unless they're overridden by
368 ;; `flyspell-mouse-map').
369 (set (make-local-variable 'flyspell-local-mouse-map)
370 (let ((map (copy-keymap flyspell-mouse-map)))
371 (if (eq flyspell-emacs 'xemacs)
372 (set-keymap-parents (list (current-local-map)))
373 (set-keymap-parent map (current-local-map)))
375 ;; we end with the flyspell hooks
376 (run-hooks 'flyspell-mode-hook))
378 ;*---------------------------------------------------------------------*/
379 ;* flyspell-delay-commands ... */
380 ;*---------------------------------------------------------------------*/
381 (defun flyspell-delay-commands ()
382 "Install the standard set of Flyspell delayed commands."
383 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
384 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
386 ;*---------------------------------------------------------------------*/
387 ;* flyspell-delay-command ... */
388 ;*---------------------------------------------------------------------*/
389 (defun flyspell-delay-command (command)
390 "Set COMMAND to be delayed, for Flyspell.
391 When flyspell `post-command-hook' is invoked because a delayed command
392 as been used the current word is not immediatly checked.
393 It will be checked only after `flyspell-delay' seconds."
394 (interactive "SDelay Flyspell after Command: ")
395 (put command 'flyspell-delayed t))
397 ;*---------------------------------------------------------------------*/
398 ;* flyspell-ignore-commands ... */
399 ;*---------------------------------------------------------------------*/
400 (defun flyspell-ignore-commands ()
401 "This is an obsolete function, use `flyspell-delay-commands' instead."
402 (flyspell-delay-commands))
404 ;*---------------------------------------------------------------------*/
405 ;* flyspell-ignore-command ... */
406 ;*---------------------------------------------------------------------*/
407 (defun flyspell-ignore-command (command)
408 "This is an obsolete function, use `flyspell-delay-command' instead.
409 COMMAND is the name of the command to be delayed."
410 (flyspell-delay-command command))
412 (make-obsolete 'flyspell-ignore-commands 'flyspell-delay-commands)
413 (make-obsolete 'flyspell-ignore-command 'flyspell-delay-command)
415 ;*---------------------------------------------------------------------*/
416 ;* flyspell-word-cache ... */
417 ;*---------------------------------------------------------------------*/
418 (defvar flyspell-word-cache-start nil)
419 (defvar flyspell-word-cache-end nil)
420 (defvar flyspell-word-cache-word nil)
421 (make-variable-buffer-local 'flyspell-word-cache-start)
422 (make-variable-buffer-local 'flyspell-word-cache-end)
423 (make-variable-buffer-local 'flyspell-word-cache-word)
425 ;*---------------------------------------------------------------------*/
426 ;* The flyspell pre-hook, store the current position. In the */
427 ;* post command hook, we will check, if the word at this position */
428 ;* has to be spell checked. */
429 ;*---------------------------------------------------------------------*/
430 (defvar flyspell-pre-buffer nil)
431 (defvar flyspell-pre-point nil)
433 ;*---------------------------------------------------------------------*/
434 ;* flyspell-pre-command-hook ... */
435 ;*---------------------------------------------------------------------*/
436 (defun flyspell-pre-command-hook ()
437 "Save the current buffer and point for Flyspell's post-command hook."
439 (setq flyspell-pre-buffer (current-buffer))
440 (setq flyspell-pre-point (point)))
442 ;*---------------------------------------------------------------------*/
443 ;* flyspell-mode-off ... */
444 ;*---------------------------------------------------------------------*/
446 (defun flyspell-mode-off ()
447 "Turn Flyspell mode off."
448 ;; If we have an Ispell process for each buffer,
449 ;; kill the one for this buffer.
450 (if flyspell-multi-language-p
451 (ispell-kill-ispell t))
452 ;; we remove the hooks
453 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
454 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
455 ;; we remove all the flyspell hilightings
456 (flyspell-delete-all-overlays)
457 ;; we have to erase pre cache variables
458 (setq flyspell-pre-buffer nil)
459 (setq flyspell-pre-point nil)
460 ;; we mark the mode as killed
461 (setq flyspell-mode nil))
463 ;*---------------------------------------------------------------------*/
464 ;* flyspell-check-word-p ... */
465 ;*---------------------------------------------------------------------*/
466 (defun flyspell-check-word-p ()
467 "Return t when the word at `point' has to be checked.
468 The answer depends of several criteria.
469 Mostly we check word delimiters."
471 ((<= (- (point-max) 1) (point-min))
472 ;; the buffer is not filled enough
474 ((not (and (symbolp this-command) (get this-command 'flyspell-delayed)))
475 ;; the current command is not delayed, that
476 ;; is that we must check the word now
478 ((and (> (point) (point-min))
481 (and (looking-at (flyspell-get-not-casechars))
482 (or flyspell-consider-dash-as-word-delimiter-flag
483 (not (looking-at "\\-"))))))
484 ;; yes because we have reached or typed a word delimiter.
486 ((not (integerp flyspell-delay))
487 ;; yes because the user had set up a no-delay configuration.
490 ;; Don't delay inside a keyboard macro.
493 (if (fboundp 'about-xemacs)
494 (sit-for flyspell-delay nil)
495 (sit-for flyspell-delay 0 nil)))))
497 ;*---------------------------------------------------------------------*/
498 ;* flyspell-check-pre-word-p ... */
499 ;*---------------------------------------------------------------------*/
500 (defun flyspell-check-pre-word-p ()
501 "Return non-nil if we should to check the word before point.
502 More precisely, it applies to the word that was before point
503 before the current command."
505 ((or (not (numberp flyspell-pre-point))
506 (not (bufferp flyspell-pre-buffer))
507 (not (buffer-live-p flyspell-pre-buffer)))
509 ((or (and (= flyspell-pre-point (- (point) 1))
510 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
511 (= flyspell-pre-point (point))
512 (= flyspell-pre-point (+ (point) 1)))
514 ((not (eq (current-buffer) flyspell-pre-buffer))
516 ((not (and (numberp flyspell-word-cache-start)
517 (numberp flyspell-word-cache-end)))
520 (or (< flyspell-pre-point flyspell-word-cache-start)
521 (> flyspell-pre-point flyspell-word-cache-end)))))
523 ;*---------------------------------------------------------------------*/
524 ;* flyspell-post-command-hook ... */
525 ;*---------------------------------------------------------------------*/
526 (defun flyspell-post-command-hook ()
527 "The `post-command-hook' used by flyspell to check a word in-the-fly."
529 (if (flyspell-check-word-p)
531 (if (flyspell-check-pre-word-p)
533 (set-buffer flyspell-pre-buffer)
535 (goto-char flyspell-pre-point)
538 ;*---------------------------------------------------------------------*/
539 ;* flyspell-word ... */
540 ;*---------------------------------------------------------------------*/
541 (defun flyspell-word (&optional following)
542 "Spell check a word."
543 (interactive (list current-prefix-arg))
545 (setq following ispell-following-word))
547 (ispell-accept-buffer-local-defs) ; use the correct dictionary
548 (let ((cursor-location (point)) ; retain cursor location
549 (word (flyspell-get-word following))
551 (if (or (eq word nil)
552 (and (fboundp flyspell-generic-check-word-p)
553 (not (funcall flyspell-generic-check-word-p))))
556 ;; destructure return word info list.
557 (setq start (car (cdr word))
558 end (car (cdr (cdr word)))
560 ;; before checking in the directory, we check for doublons.
562 ((and flyspell-mark-duplications-flag
565 (word-search-backward word
569 ;; yes, this is a doublon
570 (flyspell-highlight-incorrect-region start end))
571 ((and (eq flyspell-word-cache-start start)
572 (eq flyspell-word-cache-end end)
573 (string-equal flyspell-word-cache-word word))
574 ;; this word had been already checked, we skip
576 ((and (eq ispell-parser 'tex)
577 (flyspell-tex-command-p word))
578 ;; this is a correct word (because a tex command)
579 (flyspell-unhighlight-at start)
581 (flyspell-unhighlight-at (- end 1)))
584 ;; we setup the cache
585 (setq flyspell-word-cache-start start)
586 (setq flyspell-word-cache-end end)
587 (setq flyspell-word-cache-word word)
588 ;; now check spelling of word.
589 (process-send-string ispell-process "%\n")
590 ;; put in verbose mode
591 (process-send-string ispell-process
592 (concat "^" word "\n"))
593 ;; we mark the ispell process so it can be killed
594 ;; when emacs is exited without query
595 (if (fboundp 'process-kill-without-query)
596 (process-kill-without-query ispell-process))
597 ;; wait until ispell has processed word
599 (accept-process-output ispell-process)
600 (not (string= "" (car ispell-filter)))))
601 ;; (process-send-string ispell-process "!\n")
602 ;; back to terse mode.
603 (setq ispell-filter (cdr ispell-filter))
604 (if (listp ispell-filter)
605 (setq poss (ispell-parse-output (car ispell-filter))))
608 (flyspell-unhighlight-at start)
610 (flyspell-unhighlight-at (- end 1)))
612 ((and (stringp poss) flyspell-highlight-flag)
614 (flyspell-unhighlight-at start)
616 (flyspell-unhighlight-at (- end 1)))
619 (flyspell-unhighlight-at start)
621 (flyspell-unhighlight-at (- end 1)))
622 (message "Error in ispell process"))
623 ((or (and (< flyspell-duplicate-distance 0)
626 (word-search-backward word
631 (word-search-forward word
634 (and (> flyspell-duplicate-distance 0)
637 (word-search-backward
640 flyspell-duplicate-distance)
647 flyspell-duplicate-distance)
649 (if flyspell-highlight-flag
650 (flyspell-highlight-duplicate-region start end)
651 (message (format "duplicate `%s'" word))))
653 ;; incorrect highlight the location
654 (if flyspell-highlight-flag
655 (flyspell-highlight-incorrect-region start end)
656 (message (format "mispelling `%s'" word)))))
657 (goto-char cursor-location) ; return to original location
658 (if ispell-quit (setq ispell-quit nil)))))))))
660 ;*---------------------------------------------------------------------*/
661 ;* flyspell-tex-command-p ... */
662 ;*---------------------------------------------------------------------*/
663 (defun flyspell-tex-command-p (word)
664 "Return t if WORD is a TeX command."
665 (eq (aref word 0) ?\\))
667 ;*---------------------------------------------------------------------*/
668 ;* flyspell-casechars-cache ... */
669 ;*---------------------------------------------------------------------*/
670 (defvar flyspell-casechars-cache nil)
671 (defvar flyspell-ispell-casechars-cache nil)
672 (make-variable-buffer-local 'flyspell-casechars-cache)
673 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
675 ;*---------------------------------------------------------------------*/
676 ;* flyspell-get-casechars ... */
677 ;*---------------------------------------------------------------------*/
678 (defun flyspell-get-casechars ()
679 "This function builds a string that is the regexp of word chars.
680 In order to avoid one useless string construction,
681 this function changes the last char of the `ispell-casechars' string."
682 (let ((ispell-casechars (ispell-get-casechars)))
684 ((eq ispell-casechars flyspell-ispell-casechars-cache)
685 flyspell-casechars-cache)
686 ((not (eq ispell-parser 'tex))
687 (setq flyspell-ispell-casechars-cache ispell-casechars)
688 (setq flyspell-casechars-cache
689 (concat (substring ispell-casechars
691 (- (length ispell-casechars) 1))
693 flyspell-casechars-cache)
695 (setq flyspell-ispell-casechars-cache ispell-casechars)
696 (setq flyspell-casechars-cache ispell-casechars)
697 flyspell-casechars-cache))))
699 ;*---------------------------------------------------------------------*/
700 ;* flyspell-get-not-casechars-cache ... */
701 ;*---------------------------------------------------------------------*/
702 (defvar flyspell-not-casechars-cache nil)
703 (defvar flyspell-ispell-not-casechars-cache nil)
704 (make-variable-buffer-local 'flyspell-not-casechars-cache)
705 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
707 ;*---------------------------------------------------------------------*/
708 ;* flyspell-get-not-casechars ... */
709 ;*---------------------------------------------------------------------*/
710 (defun flyspell-get-not-casechars ()
711 "This function builds a string that is the regexp of non-word chars."
712 (let ((ispell-not-casechars (ispell-get-not-casechars)))
714 ((eq ispell-not-casechars flyspell-ispell-not-casechars-cache)
715 flyspell-not-casechars-cache)
716 ((not (eq ispell-parser 'tex))
717 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
718 (setq flyspell-not-casechars-cache
719 (concat (substring ispell-not-casechars
721 (- (length ispell-not-casechars) 1))
723 flyspell-not-casechars-cache)
725 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
726 (setq flyspell-not-casechars-cache ispell-not-casechars)
727 flyspell-not-casechars-cache))))
729 ;*---------------------------------------------------------------------*/
730 ;* flyspell-get-word ... */
731 ;*---------------------------------------------------------------------*/
732 (defun flyspell-get-word (following)
733 "Return the word for spell-checking according to Ispell syntax.
734 If optional argument FOLLOWING is non-nil or if `ispell-following-word'
735 is non-nil when called interactively, then the following word
736 \(rather than preceding\) is checked when the cursor is not over a word.
737 Optional second argument contains otherchars that can be included in word
740 Word syntax described by `ispell-dictionary-alist' (which see)."
741 (let* ((flyspell-casechars (flyspell-get-casechars))
742 (flyspell-not-casechars (flyspell-get-not-casechars))
743 (ispell-otherchars (ispell-get-otherchars))
744 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
745 (word-regexp (if (not (string= "" ispell-otherchars))
753 (if ispell-many-otherchars-p
755 (concat flyspell-casechars "+")))
756 (tex-prelude "[\\\\{]")
757 (tex-regexp (if (eq ispell-parser 'tex)
758 (concat tex-prelude "?" word-regexp "}?")
764 (if (not (or (looking-at flyspell-casechars)
765 (and (eq ispell-parser 'tex)
766 (looking-at tex-prelude))))
768 (re-search-forward flyspell-casechars (point-max) t)
769 (re-search-backward flyspell-casechars (point-min) t)))
770 ;; move to front of word
771 (re-search-backward flyspell-not-casechars (point-min) 'start)
772 (if (not (string= "" ispell-otherchars))
774 (while (and (looking-at ispell-otherchars)
776 (or (not did-it-once)
777 ispell-many-otherchars-p)
778 (not (eq pos (point))))
782 (if (looking-at flyspell-casechars)
783 (re-search-backward flyspell-not-casechars (point-min) 'move)
784 (backward-char -1)))))
785 ;; Now mark the word and save to string.
786 (if (eq (re-search-forward tex-regexp (point-max) t) nil)
789 (setq start (match-beginning 0)
791 word (buffer-substring start end))
792 (list word start end)))))
794 ;*---------------------------------------------------------------------*/
795 ;* flyspell-region ... */
796 ;*---------------------------------------------------------------------*/
797 (defun flyspell-region (beg end)
798 "Flyspell text between BEG and END."
807 (while (< (point) end)
810 (message "Spell Checking...%d%%"
811 (* 100 (/ (float (- (point) beg)) (- end beg))))
813 (setq count (+ 1 count)))
817 (if (and (< (point) end) (> (point) (+ cur 1)))
818 (backward-char 1)))))
820 (message "Spell Checking...done")
823 ;*---------------------------------------------------------------------*/
824 ;* flyspell-buffer ... */
825 ;*---------------------------------------------------------------------*/
826 (defun flyspell-buffer ()
827 "Flyspell whole buffer."
829 (flyspell-region (point-min) (point-max)))
831 ;*---------------------------------------------------------------------*/
832 ;* flyspell-overlay-p ... */
833 ;*---------------------------------------------------------------------*/
834 (defun flyspell-overlay-p (o)
835 "A predicate that return true iff O is an overlay used by flyspell."
836 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
838 ;*---------------------------------------------------------------------*/
839 ;* flyspell-delete-all-overlays ... */
840 ;* ------------------------------------------------------------- */
841 ;* Remove all the overlays introduced by flyspell. */
842 ;*---------------------------------------------------------------------*/
843 (defun flyspell-delete-all-overlays ()
844 "Delete all the overlays used by flyspell."
845 (let ((l (overlays-in (point-min) (point-max))))
848 (if (flyspell-overlay-p (car l))
849 (delete-overlay (car l)))
852 ;*---------------------------------------------------------------------*/
853 ;* flyspell-unhighlight-at ... */
854 ;*---------------------------------------------------------------------*/
855 (defun flyspell-unhighlight-at (pos)
856 "Remove the flyspell overlay that are located at POS."
857 (if flyspell-persistent-highlight
858 (let ((overlays (overlays-at pos)))
859 (while (consp overlays)
860 (if (flyspell-overlay-p (car overlays))
861 (delete-overlay (car overlays)))
862 (setq overlays (cdr overlays))))
863 (delete-overlay flyspell-overlay)))
865 ;*---------------------------------------------------------------------*/
866 ;* flyspell-properties-at-p ... */
867 ;* ------------------------------------------------------------- */
868 ;* Is there an highlight properties at position pos? */
869 ;*---------------------------------------------------------------------*/
870 (defun flyspell-properties-at-p (pos)
871 "Return t if there is a text property at POS, not counting `local-map'.
872 If variable `flyspell-highlight-properties' is set to nil,
873 text with properties are not checked. This function is used to discover
874 if the character at POS has any other property."
875 (let ((prop (text-properties-at pos))
877 (while (and keep (consp prop))
878 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
879 (setq prop (cdr (cdr prop)))
883 ;*---------------------------------------------------------------------*/
884 ;* make-flyspell-overlay ... */
885 ;*---------------------------------------------------------------------*/
886 (defun make-flyspell-overlay (beg end face mouse-face)
887 "Allocate an overlay to highlight an incorrect word.
888 BEG and END specify the range in the buffer of that word.
889 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
891 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
892 (overlay-put flyspell-overlay 'face face)
893 (overlay-put flyspell-overlay 'mouse-face mouse-face)
894 (overlay-put flyspell-overlay 'flyspell-overlay t)
895 (if flyspell-use-local-map
896 (overlay-put flyspell-overlay
897 flyspell-overlay-keymap-property-name
898 flyspell-local-mouse-map))))
900 ;*---------------------------------------------------------------------*/
901 ;* flyspell-highlight-incorrect-region ... */
902 ;*---------------------------------------------------------------------*/
903 (defun flyspell-highlight-incorrect-region (beg end)
904 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
905 (run-hook-with-args 'flyspell-incorrect-hook beg end)
906 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
908 ;; we cleanup current overlay at the same position
909 (if (and (not flyspell-persistent-highlight)
910 (overlayp flyspell-overlay))
911 (delete-overlay flyspell-overlay)
912 (let ((overlays (overlays-at beg)))
913 (while (consp overlays)
914 (if (flyspell-overlay-p (car overlays))
915 (delete-overlay (car overlays)))
916 (setq overlays (cdr overlays)))))
917 ;; now we can use a new overlay
918 (setq flyspell-overlay
919 (make-flyspell-overlay beg end
920 'flyspell-incorrect-face 'highlight)))))
922 ;*---------------------------------------------------------------------*/
923 ;* flyspell-highlight-duplicate-region ... */
924 ;*---------------------------------------------------------------------*/
925 (defun flyspell-highlight-duplicate-region (beg end)
926 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
927 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
929 ;; we cleanup current overlay at the same position
930 (if (and (not flyspell-persistent-highlight)
931 (overlayp flyspell-overlay))
932 (delete-overlay flyspell-overlay)
933 (let ((overlays (overlays-at beg)))
934 (while (consp overlays)
935 (if (flyspell-overlay-p (car overlays))
936 (delete-overlay (car overlays)))
937 (setq overlays (cdr overlays)))))
938 ;; now we can use a new overlay
939 (setq flyspell-overlay
940 (make-flyspell-overlay beg end
941 'flyspell-duplicate-face 'highlight)))))
943 ;*---------------------------------------------------------------------*/
944 ;* flyspell-auto-correct-cache ... */
945 ;*---------------------------------------------------------------------*/
946 (defvar flyspell-auto-correct-pos nil)
947 (defvar flyspell-auto-correct-region nil)
948 (defvar flyspell-auto-correct-ring nil)
950 ;*---------------------------------------------------------------------*/
951 ;* flyspell-auto-correct-word ... */
952 ;*---------------------------------------------------------------------*/
953 (defun flyspell-auto-correct-word (pos)
954 "Correct the word at POS.
955 This command proposes various successive corrections for the word at POS.
956 The variable `flyspell-auto-correct-binding' specifies the key to bind
959 ;; use the correct dictionary
960 (ispell-accept-buffer-local-defs)
961 (if (eq flyspell-auto-correct-pos pos)
962 ;; we have already been using the function at the same location
965 (let ((start (car flyspell-auto-correct-region))
966 (len (cdr flyspell-auto-correct-region)))
967 (delete-region start (+ start len))
968 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
969 (let* ((word (car flyspell-auto-correct-ring))
971 (rplacd flyspell-auto-correct-region len)
974 (setq flyspell-auto-correct-pos (point)))
975 ;; retain cursor location
976 (let ((cursor-location pos)
977 (word (flyspell-get-word nil))
979 ;; destructure return word info list.
980 (setq start (car (cdr word))
981 end (car (cdr (cdr word)))
983 ;; now check spelling of word.
984 (process-send-string ispell-process "%\n") ;put in verbose mode
985 (process-send-string ispell-process (concat "^" word "\n"))
986 ;; wait until ispell has processed word
988 (accept-process-output ispell-process)
989 (not (string= "" (car ispell-filter)))))
990 (setq ispell-filter (cdr ispell-filter))
991 (if (listp ispell-filter)
992 (setq poss (ispell-parse-output (car ispell-filter))))
993 (cond ((or (eq poss t) (stringp poss))
994 ;; don't correct word
998 (error "Ispell: error in Ispell process"))
1000 ;; the word is incorrect, we have to propose a replacement
1001 (let ((replacements (if flyspell-sort-corrections
1002 (sort (car (cdr (cdr poss))) 'string<)
1003 (car (cdr (cdr poss))))))
1004 (if (consp replacements)
1006 (let ((replace (car replacements)))
1008 (setq cursor-location (+ (- (length word) (- end start))
1010 (if (not (equal word (car poss)))
1012 ;; the save the current replacements
1013 (setq flyspell-auto-correct-pos cursor-location)
1014 (setq flyspell-auto-correct-region
1015 (cons start (length word)))
1016 (let ((l replacements))
1017 (while (consp (cdr l))
1019 (rplacd l (cons (car poss) replacements)))
1020 (setq flyspell-auto-correct-ring
1022 (delete-region start end)
1023 (insert word)))))))))
1024 ;; return to original location
1025 (goto-char cursor-location)
1026 (ispell-pdict-save t))))
1028 ;*---------------------------------------------------------------------*/
1029 ;* flyspell-correct-word ... */
1030 ;*---------------------------------------------------------------------*/
1031 (defun flyspell-correct-word (event)
1032 "Check spelling of word under or before the cursor.
1033 If the word is not found in dictionary, display possible corrections
1034 in a popup menu allowing you to choose one.
1036 Word syntax described by `ispell-dictionary-alist' (which see).
1038 This will check or reload the dictionary. Use \\[ispell-change-dictionary]
1039 or \\[ispell-region] to update the Ispell process."
1041 (if flyspell-use-local-map
1042 (flyspell-correct-word/mouse-keymap event)
1043 (flyspell-correct-word/local-keymap event)))
1045 ;*---------------------------------------------------------------------*/
1046 ;* flyspell-correct-word/local-keymap ... */
1047 ;*---------------------------------------------------------------------*/
1048 (defun flyspell-correct-word/local-keymap (event)
1049 "emacs 19.xx seems to be buggous. Overlay keymap does not seems
1050 to work correctly with local map. That is, if a key is not
1051 defined for the overlay keymap, the current local map, is not
1052 checked. The binding is resolved with the global map. The
1053 consequence is that we can not use overlay map with flyspell."
1055 (save-window-excursion
1056 (let ((save (point)))
1057 (mouse-set-point event)
1058 ;; we look for a flyspell overlay here
1059 (let ((overlays (overlays-at (point)))
1061 (while (consp overlays)
1062 (if (flyspell-overlay-p (car overlays))
1064 (setq overlay (car overlays))
1065 (setq overlays nil))
1066 (setq overlays (cdr overlays))))
1067 ;; we return to the correct location
1069 ;; we check to see if button2 has been used overlay a
1072 ;; yes, so we use the flyspell function
1073 (flyspell-correct-word/mouse-keymap event)
1074 ;; no so we have to use the non flyspell binding
1075 (let ((flyspell-mode nil))
1076 (if (key-binding (this-command-keys))
1077 (command-execute (key-binding (this-command-keys))))))))))
1079 ;*---------------------------------------------------------------------*/
1080 ;* flyspell-correct-word ... */
1081 ;*---------------------------------------------------------------------*/
1082 (defun flyspell-correct-word/mouse-keymap (event)
1083 "Pop up a menu of possible corrections for a misspelled word.
1084 The word checked is the word at the mouse position."
1086 ;; use the correct dictionary
1087 (ispell-accept-buffer-local-defs)
1088 ;; retain cursor location (I don't know why but save-excursion here fails).
1089 (let ((save (point)))
1090 (mouse-set-point event)
1091 (let ((cursor-location (point))
1092 (word (flyspell-get-word nil))
1093 start end poss replace)
1094 ;; destructure return word info list.
1095 (setq start (car (cdr word))
1096 end (car (cdr (cdr word)))
1098 ;; now check spelling of word.
1099 (process-send-string ispell-process "%\n") ;put in verbose mode
1100 (process-send-string ispell-process (concat "^" word "\n"))
1101 ;; wait until ispell has processed word
1103 (accept-process-output ispell-process)
1104 (not (string= "" (car ispell-filter)))))
1105 (setq ispell-filter (cdr ispell-filter))
1106 (if (listp ispell-filter)
1107 (setq poss (ispell-parse-output (car ispell-filter))))
1108 (cond ((or (eq poss t) (stringp poss))
1109 ;; don't correct word
1113 (error "Ispell: error in Ispell process"))
1114 ((string-match "GNU" (emacs-version))
1115 ;; the word is incorrect, we have to propose a replacement
1116 (setq replace (flyspell-emacs-popup event poss word))
1117 (cond ((eq replace 'ignore)
1120 (process-send-string ispell-process (concat "*" word "\n"))
1121 (flyspell-unhighlight-at cursor-location)
1122 (setq ispell-pdict-modified-p '(t)))
1123 ((or (eq replace 'buffer) (eq replace 'session))
1124 (process-send-string ispell-process (concat "@" word "\n"))
1125 (if (null ispell-pdict-modified-p)
1126 (setq ispell-pdict-modified-p
1127 (list ispell-pdict-modified-p)))
1128 (flyspell-unhighlight-at cursor-location)
1129 (if (eq replace 'buffer)
1130 (ispell-add-per-file-word-list word)))
1132 (setq word (if (atom replace) replace (car replace))
1133 cursor-location (+ (- (length word) (- end start))
1135 (if (not (equal word (car poss)))
1137 (delete-region start end)
1139 ((eq flyspell-emacs 'xemacs)
1140 (flyspell-xemacs-popup
1141 event poss word cursor-location start end)))
1142 (ispell-pdict-save t))
1143 (if (< save (point-max))
1145 (goto-char (point-max)))))
1147 ;*---------------------------------------------------------------------*/
1148 ;* flyspell-xemacs-correct ... */
1149 ;*---------------------------------------------------------------------*/
1150 (defun flyspell-xemacs-correct (replace poss word cursor-location start end)
1151 "The xemacs popup menu callback."
1152 (cond ((eq replace 'ignore)
1155 (process-send-string ispell-process (concat "*" word "\n"))
1156 (flyspell-unhighlight-at cursor-location)
1157 (setq ispell-pdict-modified-p '(t)))
1158 ((or (eq replace 'buffer) (eq replace 'session))
1159 (process-send-string ispell-process (concat "@" word "\n"))
1160 (flyspell-unhighlight-at cursor-location)
1161 (if (null ispell-pdict-modified-p)
1162 (setq ispell-pdict-modified-p
1163 (list ispell-pdict-modified-p)))
1164 (if (eq replace 'buffer)
1165 (ispell-add-per-file-word-list word)))
1167 (setq word (if (atom replace) replace (car replace))
1168 cursor-location (+ (- (length word) (- end start))
1170 (if (not (equal word (car poss)))
1172 (delete-region start end)
1176 ;*---------------------------------------------------------------------*/
1177 ;* flyspell-emacs-popup ... */
1178 ;*---------------------------------------------------------------------*/
1179 (defun flyspell-emacs-popup (event poss word)
1180 "The Emacs popup menu."
1182 (let* ((mouse-pos (mouse-position))
1183 (mouse-pos (if (nth 1 mouse-pos)
1185 (set-mouse-position (car mouse-pos)
1186 (/ (frame-width) 2) 2)
1189 (setq event (list (list (car (cdr mouse-pos))
1190 (1+ (cdr (cdr mouse-pos))))
1192 (let* ((corrects (if flyspell-sort-corrections
1193 (sort (car (cdr (cdr poss))) 'string<)
1194 (car (cdr (cdr poss)))))
1195 (cor-menu (if (consp corrects)
1196 (mapcar (lambda (correct)
1197 (list correct correct))
1200 (affix (car (cdr (cdr (cdr poss)))))
1201 (base-menu (let ((save (if (consp affix)
1203 (list (concat "Save affix: " (car affix))
1205 '("Accept (session)" accept)
1206 '("Accept (buffer)" buffer))
1207 '(("Save word" save)
1208 ("Accept (session)" session)
1209 ("Accept (buffer)" buffer)))))
1210 (if (consp cor-menu)
1211 (append cor-menu (cons "" save))
1213 (menu (cons "flyspell correction menu" base-menu)))
1214 (car (x-popup-menu event
1215 (list (format "%s [%s]" word (or ispell-local-dictionary
1219 ;*---------------------------------------------------------------------*/
1220 ;* flyspell-xemacs-popup ... */
1221 ;*---------------------------------------------------------------------*/
1222 (defun flyspell-xemacs-popup (event poss word cursor-location start end)
1223 "The xemacs popup menu."
1224 (let* ((corrects (if flyspell-sort-corrections
1225 (sort (car (cdr (cdr poss))) 'string<)
1226 (car (cdr (cdr poss)))))
1227 (cor-menu (if (consp corrects)
1228 (mapcar (lambda (correct)
1230 (list 'flyspell-xemacs-correct
1240 (affix (car (cdr (cdr (cdr poss)))))
1241 (menu (let ((save (if (consp affix)
1243 (concat "Save affix: " (car affix))
1244 (list 'flyspell-xemacs-correct
1254 (list 'flyspell-xemacs-correct
1262 (session (vector "Accept (session)"
1263 (list 'flyspell-xemacs-correct
1271 (buffer (vector "Accept (buffer)"
1272 (list 'flyspell-xemacs-correct
1280 (if (consp cor-menu)
1281 (append cor-menu (list "-" save session buffer))
1282 (list save session buffer)))))
1283 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
1289 ;;; flyspell.el ends here