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 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-mode-map (make-sparse-keymap))
217 (defvar flyspell-mouse-map (make-sparse-keymap))
219 (or (assoc 'flyspell-mode minor-mode-alist)
220 (setq minor-mode-alist
221 (cons '(flyspell-mode " Fly") minor-mode-alist)))
223 ;; mouse or local-map bindings
225 ((eq flyspell-emacs 'xemacs)
226 (define-key flyspell-mouse-map [(button2)]
227 (function flyspell-correct-word/mouse-keymap))
228 (define-key flyspell-mouse-map "\M-\t" 'flyspell-auto-correct-word))
229 (flyspell-use-local-map
230 (define-key flyspell-mouse-map [(mouse-2)]
231 (function flyspell-correct-word/mouse-keymap))
232 (define-key flyspell-mouse-map "\M-\t" 'flyspell-auto-correct-word))
234 (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 (defun flyspell-mode-on ()
315 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
316 (setq ispell-highlight-face 'flyspell-incorrect-face)
317 ;; ispell initialization
318 (if flyspell-multi-language-p
320 (make-variable-buffer-local 'ispell-dictionary)
321 (make-variable-buffer-local 'ispell-process)
322 (make-variable-buffer-local 'ispell-filter)
323 (make-variable-buffer-local 'ispell-filter-continue)
324 (make-variable-buffer-local 'ispell-process-directory)
325 (make-variable-buffer-local 'ispell-parser)
326 (put 'ispell-dictionary 'permanent-local t)
327 (put 'ispell-process 'permanent-local t)
328 (put 'ispell-filter 'permanent-local t)
329 (put 'ispell-filter-continue 'permanent-local t)
330 (put 'ispell-process-directory 'permanent-local t)
331 (put 'ispell-parser 'permanent-local t)))
332 ;; We put the `flyspell-delayed' property on some commands.
333 (flyspell-delay-commands)
334 ;; we bound flyspell action to post-command hook
335 (make-local-hook 'post-command-hook)
336 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
337 ;; we bound flyspell action to pre-command hook
338 (make-local-hook 'pre-command-hook)
339 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
341 ;; Set flyspell-generic-check-word-p based on the major mode.
342 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
344 (setq flyspell-generic-check-word-p mode-predicate)))
346 ;; the welcome message
347 (if flyspell-issue-welcome-flag
348 (let ((binding (where-is-internal 'flyspell-auto-correct-word
352 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
353 (key-description binding))
354 "Welcome to flyspell. Use Mouse-2 to correct words."))))
355 ;; we have to kill the flyspell process when the buffer is deleted.
356 ;; (thanks to Jeff Miller and Roland Rosenfeld who sent me this
358 (add-hook 'kill-buffer-hook
360 (if (and flyspell-multi-language-p ispell-process)
361 (ispell-kill-ispell t))))
362 (make-local-hook 'change-major-mode-hook)
363 (add-hook 'change-major-mode-hook 'flyspell-mode-off)
364 ;; we end with the flyspell hooks
365 (run-hooks 'flyspell-mode-hook))
367 ;*---------------------------------------------------------------------*/
368 ;* flyspell-delay-commands ... */
369 ;*---------------------------------------------------------------------*/
370 (defun flyspell-delay-commands ()
371 "Install the standard set of Flyspell delayed commands."
372 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
373 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
375 ;*---------------------------------------------------------------------*/
376 ;* flyspell-delay-command ... */
377 ;*---------------------------------------------------------------------*/
378 (defun flyspell-delay-command (command)
379 "Set COMMAND to be delayed, for Flyspell.
380 When flyspell `post-command-hook' is invoked because a delayed command
381 as been used the current word is not immediatly checked.
382 It will be checked only after `flyspell-delay' seconds."
383 (interactive "SDelay Flyspell after Command: ")
384 (put command 'flyspell-delayed t))
386 ;*---------------------------------------------------------------------*/
387 ;* flyspell-ignore-commands ... */
388 ;*---------------------------------------------------------------------*/
389 (defun flyspell-ignore-commands ()
390 "This is an obsolete function, use `flyspell-delay-commands' instead."
391 (flyspell-delay-commands))
393 ;*---------------------------------------------------------------------*/
394 ;* flyspell-ignore-command ... */
395 ;*---------------------------------------------------------------------*/
396 (defun flyspell-ignore-command (command)
397 "This is an obsolete function, use `flyspell-delay-command' instead.
398 COMMAND is the name of the command to be delayed."
399 (flyspell-delay-command command))
401 (make-obsolete 'flyspell-ignore-commands 'flyspell-delay-commands)
402 (make-obsolete 'flyspell-ignore-command 'flyspell-delay-command)
404 ;*---------------------------------------------------------------------*/
405 ;* flyspell-word-cache ... */
406 ;*---------------------------------------------------------------------*/
407 (defvar flyspell-word-cache-start nil)
408 (defvar flyspell-word-cache-end nil)
409 (defvar flyspell-word-cache-word nil)
410 (make-variable-buffer-local 'flyspell-word-cache-start)
411 (make-variable-buffer-local 'flyspell-word-cache-end)
412 (make-variable-buffer-local 'flyspell-word-cache-word)
414 ;*---------------------------------------------------------------------*/
415 ;* The flyspell pre-hook, store the current position. In the */
416 ;* post command hook, we will check, if the word at this position */
417 ;* has to be spell checked. */
418 ;*---------------------------------------------------------------------*/
419 (defvar flyspell-pre-buffer nil)
420 (defvar flyspell-pre-point nil)
422 ;*---------------------------------------------------------------------*/
423 ;* flyspell-pre-command-hook ... */
424 ;*---------------------------------------------------------------------*/
425 (defun flyspell-pre-command-hook ()
426 "Save the current buffer and point for Flyspell's post-command hook."
428 (setq flyspell-pre-buffer (current-buffer))
429 (setq flyspell-pre-point (point)))
431 ;*---------------------------------------------------------------------*/
432 ;* flyspell-mode-off ... */
433 ;*---------------------------------------------------------------------*/
435 (defun flyspell-mode-off ()
436 "Turn Flyspell mode off."
437 ;; If we have an Ispell process for each buffer,
438 ;; kill the one for this buffer.
439 (if flyspell-multi-language-p
440 (ispell-kill-ispell t))
441 ;; we remove the hooks
442 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
443 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
444 ;; we remove all the flyspell hilightings
445 (flyspell-delete-all-overlays)
446 ;; we have to erase pre cache variables
447 (setq flyspell-pre-buffer nil)
448 (setq flyspell-pre-point nil)
449 ;; we mark the mode as killed
450 (setq flyspell-mode nil))
452 ;*---------------------------------------------------------------------*/
453 ;* flyspell-check-word-p ... */
454 ;*---------------------------------------------------------------------*/
455 (defun flyspell-check-word-p ()
456 "Return t when the word at `point' has to be checked.
457 The answer depends of several criteria.
458 Mostly we check word delimiters."
460 ((<= (- (point-max) 1) (point-min))
461 ;; the buffer is not filled enough
463 ((not (and (symbolp this-command) (get this-command 'flyspell-delayed)))
464 ;; the current command is not delayed, that
465 ;; is that we must check the word now
467 ((and (> (point) (point-min))
470 (and (looking-at (flyspell-get-not-casechars))
471 (or flyspell-consider-dash-as-word-delimiter-flag
472 (not (looking-at "\\-"))))))
473 ;; yes because we have reached or typed a word delimiter.
475 ((not (integerp flyspell-delay))
476 ;; yes because the user had set up a no-delay configuration.
479 ;; Don't delay inside a keyboard macro.
482 (if (fboundp 'about-xemacs)
483 (sit-for flyspell-delay nil)
484 (sit-for flyspell-delay 0 nil)))))
486 ;*---------------------------------------------------------------------*/
487 ;* flyspell-check-pre-word-p ... */
488 ;*---------------------------------------------------------------------*/
489 (defun flyspell-check-pre-word-p ()
490 "Return non-nil if we should to check the word before point.
491 More precisely, it applies to the word that was before point
492 before the current command."
494 ((or (not (numberp flyspell-pre-point))
495 (not (bufferp flyspell-pre-buffer))
496 (not (buffer-live-p flyspell-pre-buffer)))
498 ((or (and (= flyspell-pre-point (- (point) 1))
499 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
500 (= flyspell-pre-point (point))
501 (= flyspell-pre-point (+ (point) 1)))
503 ((not (eq (current-buffer) flyspell-pre-buffer))
505 ((not (and (numberp flyspell-word-cache-start)
506 (numberp flyspell-word-cache-end)))
509 (or (< flyspell-pre-point flyspell-word-cache-start)
510 (> flyspell-pre-point flyspell-word-cache-end)))))
512 ;*---------------------------------------------------------------------*/
513 ;* flyspell-post-command-hook ... */
514 ;*---------------------------------------------------------------------*/
515 (defun flyspell-post-command-hook ()
516 "The `post-command-hook' used by flyspell to check a word in-the-fly."
518 (if (flyspell-check-word-p)
520 (if (flyspell-check-pre-word-p)
522 (set-buffer flyspell-pre-buffer)
524 (goto-char flyspell-pre-point)
527 ;*---------------------------------------------------------------------*/
528 ;* flyspell-word ... */
529 ;*---------------------------------------------------------------------*/
530 (defun flyspell-word (&optional following)
531 "Spell check a word."
532 (interactive (list current-prefix-arg))
534 (setq following ispell-following-word))
536 (ispell-accept-buffer-local-defs) ; use the correct dictionary
537 (let ((cursor-location (point)) ; retain cursor location
538 (word (flyspell-get-word following))
540 (if (or (eq word nil)
541 (and (fboundp flyspell-generic-check-word-p)
542 (not (funcall flyspell-generic-check-word-p))))
545 ;; destructure return word info list.
546 (setq start (car (cdr word))
547 end (car (cdr (cdr word)))
549 ;; before checking in the directory, we check for doublons.
551 ((and flyspell-mark-duplications-flag
554 (word-search-backward word
558 ;; yes, this is a doublon
559 (flyspell-highlight-incorrect-region start end))
560 ((and (eq flyspell-word-cache-start start)
561 (eq flyspell-word-cache-end end)
562 (string-equal flyspell-word-cache-word word))
563 ;; this word had been already checked, we skip
565 ((and (eq ispell-parser 'tex)
566 (flyspell-tex-command-p word))
567 ;; this is a correct word (because a tex command)
568 (flyspell-unhighlight-at start)
570 (flyspell-unhighlight-at (- end 1)))
573 ;; we setup the cache
574 (setq flyspell-word-cache-start start)
575 (setq flyspell-word-cache-end end)
576 (setq flyspell-word-cache-word word)
577 ;; now check spelling of word.
578 (process-send-string ispell-process "%\n")
579 ;; put in verbose mode
580 (process-send-string ispell-process
581 (concat "^" word "\n"))
582 ;; we mark the ispell process so it can be killed
583 ;; when emacs is exited without query
584 (if (fboundp 'process-kill-without-query)
585 (process-kill-without-query ispell-process))
586 ;; wait until ispell has processed word
588 (accept-process-output ispell-process)
589 (not (string= "" (car ispell-filter)))))
590 ;; (process-send-string ispell-process "!\n")
591 ;; back to terse mode.
592 (setq ispell-filter (cdr ispell-filter))
593 (if (listp ispell-filter)
594 (setq poss (ispell-parse-output (car ispell-filter))))
597 (flyspell-unhighlight-at start)
599 (flyspell-unhighlight-at (- end 1)))
601 ((and (stringp poss) flyspell-highlight-flag)
603 (flyspell-unhighlight-at start)
605 (flyspell-unhighlight-at (- end 1)))
608 (flyspell-unhighlight-at start)
610 (flyspell-unhighlight-at (- end 1)))
611 (message "Error in ispell process"))
612 ((or (and (< flyspell-duplicate-distance 0)
615 (word-search-backward word
620 (word-search-forward word
623 (and (> flyspell-duplicate-distance 0)
626 (word-search-backward
629 flyspell-duplicate-distance)
636 flyspell-duplicate-distance)
638 (if flyspell-highlight-flag
639 (flyspell-highlight-duplicate-region start end)
640 (message (format "duplicate `%s'" word))))
642 ;; incorrect highlight the location
643 (if flyspell-highlight-flag
644 (flyspell-highlight-incorrect-region start end)
645 (message (format "mispelling `%s'" word)))))
646 (goto-char cursor-location) ; return to original location
647 (if ispell-quit (setq ispell-quit nil)))))))))
649 ;*---------------------------------------------------------------------*/
650 ;* flyspell-tex-command-p ... */
651 ;*---------------------------------------------------------------------*/
652 (defun flyspell-tex-command-p (word)
653 "Return t if WORD is a TeX command."
654 (eq (aref word 0) ?\\))
656 ;*---------------------------------------------------------------------*/
657 ;* flyspell-casechars-cache ... */
658 ;*---------------------------------------------------------------------*/
659 (defvar flyspell-casechars-cache nil)
660 (defvar flyspell-ispell-casechars-cache nil)
661 (make-variable-buffer-local 'flyspell-casechars-cache)
662 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
664 ;*---------------------------------------------------------------------*/
665 ;* flyspell-get-casechars ... */
666 ;*---------------------------------------------------------------------*/
667 (defun flyspell-get-casechars ()
668 "This function builds a string that is the regexp of word chars.
669 In order to avoid one useless string construction,
670 this function changes the last char of the `ispell-casechars' string."
671 (let ((ispell-casechars (ispell-get-casechars)))
673 ((eq ispell-casechars flyspell-ispell-casechars-cache)
674 flyspell-casechars-cache)
675 ((not (eq ispell-parser 'tex))
676 (setq flyspell-ispell-casechars-cache ispell-casechars)
677 (setq flyspell-casechars-cache
678 (concat (substring ispell-casechars
680 (- (length ispell-casechars) 1))
682 flyspell-casechars-cache)
684 (setq flyspell-ispell-casechars-cache ispell-casechars)
685 (setq flyspell-casechars-cache ispell-casechars)
686 flyspell-casechars-cache))))
688 ;*---------------------------------------------------------------------*/
689 ;* flyspell-get-not-casechars-cache ... */
690 ;*---------------------------------------------------------------------*/
691 (defvar flyspell-not-casechars-cache nil)
692 (defvar flyspell-ispell-not-casechars-cache nil)
693 (make-variable-buffer-local 'flyspell-not-casechars-cache)
694 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
696 ;*---------------------------------------------------------------------*/
697 ;* flyspell-get-not-casechars ... */
698 ;*---------------------------------------------------------------------*/
699 (defun flyspell-get-not-casechars ()
700 "This function builds a string that is the regexp of non-word chars."
701 (let ((ispell-not-casechars (ispell-get-not-casechars)))
703 ((eq ispell-not-casechars flyspell-ispell-not-casechars-cache)
704 flyspell-not-casechars-cache)
705 ((not (eq ispell-parser 'tex))
706 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
707 (setq flyspell-not-casechars-cache
708 (concat (substring ispell-not-casechars
710 (- (length ispell-not-casechars) 1))
712 flyspell-not-casechars-cache)
714 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
715 (setq flyspell-not-casechars-cache ispell-not-casechars)
716 flyspell-not-casechars-cache))))
718 ;*---------------------------------------------------------------------*/
719 ;* flyspell-get-word ... */
720 ;*---------------------------------------------------------------------*/
721 (defun flyspell-get-word (following)
722 "Return the word for spell-checking according to Ispell syntax.
723 If optional argument FOLLOWING is non-nil or if `ispell-following-word'
724 is non-nil when called interactively, then the following word
725 \(rather than preceding\) is checked when the cursor is not over a word.
726 Optional second argument contains otherchars that can be included in word
729 Word syntax described by `ispell-dictionary-alist' (which see)."
730 (let* ((flyspell-casechars (flyspell-get-casechars))
731 (flyspell-not-casechars (flyspell-get-not-casechars))
732 (ispell-otherchars (ispell-get-otherchars))
733 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
734 (word-regexp (concat flyspell-casechars
740 (if ispell-many-otherchars-p
742 (tex-prelude "[\\\\{]")
743 (tex-regexp (if (eq ispell-parser 'tex)
744 (concat tex-prelude "?" word-regexp "}?")
750 (if (not (or (looking-at flyspell-casechars)
751 (and (eq ispell-parser 'tex)
752 (looking-at tex-prelude))))
754 (re-search-forward flyspell-casechars (point-max) t)
755 (re-search-backward flyspell-casechars (point-min) t)))
756 ;; move to front of word
757 (re-search-backward flyspell-not-casechars (point-min) 'start)
759 (while (and (looking-at ispell-otherchars)
761 (or (not did-it-once)
762 ispell-many-otherchars-p)
763 (not (eq pos (point))))
767 (if (looking-at flyspell-casechars)
768 (re-search-backward flyspell-not-casechars (point-min) 'move)
769 (backward-char -1))))
770 ;; Now mark the word and save to string.
771 (if (eq (re-search-forward tex-regexp (point-max) t) nil)
774 (setq start (match-beginning 0)
776 word (buffer-substring start end))
777 (list word start end)))))
779 ;*---------------------------------------------------------------------*/
780 ;* flyspell-region ... */
781 ;*---------------------------------------------------------------------*/
782 (defun flyspell-region (beg end)
783 "Flyspell text between BEG and END."
792 (while (< (point) end)
795 (message "Spell Checking...%d%%"
796 (* 100 (/ (float (- (point) beg)) (- end beg))))
798 (setq count (+ 1 count)))
802 (if (and (< (point) end) (> (point) (+ cur 1)))
803 (backward-char 1)))))
805 (message "Spell Checking...done")
808 ;*---------------------------------------------------------------------*/
809 ;* flyspell-buffer ... */
810 ;*---------------------------------------------------------------------*/
811 (defun flyspell-buffer ()
812 "Flyspell whole buffer."
814 (flyspell-region (point-min) (point-max)))
816 ;*---------------------------------------------------------------------*/
817 ;* flyspell-overlay-p ... */
818 ;*---------------------------------------------------------------------*/
819 (defun flyspell-overlay-p (o)
820 "A predicate that return true iff O is an overlay used by flyspell."
821 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
823 ;*---------------------------------------------------------------------*/
824 ;* flyspell-delete-all-overlays ... */
825 ;* ------------------------------------------------------------- */
826 ;* Remove all the overlays introduced by flyspell. */
827 ;*---------------------------------------------------------------------*/
828 (defun flyspell-delete-all-overlays ()
829 "Delete all the overlays used by flyspell."
830 (let ((l (overlays-in (point-min) (point-max))))
833 (if (flyspell-overlay-p (car l))
834 (delete-overlay (car l)))
837 ;*---------------------------------------------------------------------*/
838 ;* flyspell-unhighlight-at ... */
839 ;*---------------------------------------------------------------------*/
840 (defun flyspell-unhighlight-at (pos)
841 "Remove the flyspell overlay that are located at POS."
842 (if flyspell-persistent-highlight
843 (let ((overlays (overlays-at pos)))
844 (while (consp overlays)
845 (if (flyspell-overlay-p (car overlays))
846 (delete-overlay (car overlays)))
847 (setq overlays (cdr overlays))))
848 (delete-overlay flyspell-overlay)))
850 ;*---------------------------------------------------------------------*/
851 ;* flyspell-properties-at-p ... */
852 ;* ------------------------------------------------------------- */
853 ;* Is there an highlight properties at position pos? */
854 ;*---------------------------------------------------------------------*/
855 (defun flyspell-properties-at-p (pos)
856 "Return t if there is a text property at POS, not counting `local-map'.
857 If variable `flyspell-highlight-properties' is set to nil,
858 text with properties are not checked. This function is used to discover
859 if the character at POS has any other property."
860 (let ((prop (text-properties-at pos))
862 (while (and keep (consp prop))
863 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
864 (setq prop (cdr (cdr prop)))
868 ;*---------------------------------------------------------------------*/
869 ;* make-flyspell-overlay ... */
870 ;*---------------------------------------------------------------------*/
871 (defun make-flyspell-overlay (beg end face mouse-face)
872 "Allocate an overlay to highlight an incorrect word.
873 BEG and END specify the range in the buffer of that word.
874 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
876 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
877 (overlay-put flyspell-overlay 'face face)
878 (overlay-put flyspell-overlay 'mouse-face mouse-face)
879 (overlay-put flyspell-overlay 'flyspell-overlay t)
880 (if flyspell-use-local-map
881 (overlay-put flyspell-overlay
882 flyspell-overlay-keymap-property-name
883 flyspell-mouse-map))))
885 ;*---------------------------------------------------------------------*/
886 ;* flyspell-highlight-incorrect-region ... */
887 ;*---------------------------------------------------------------------*/
888 (defun flyspell-highlight-incorrect-region (beg end)
889 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
890 (run-hook-with-args 'flyspell-incorrect-hook beg end)
891 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
893 ;; we cleanup current overlay at the same position
894 (if (and (not flyspell-persistent-highlight)
895 (overlayp flyspell-overlay))
896 (delete-overlay flyspell-overlay)
897 (let ((overlays (overlays-at beg)))
898 (while (consp overlays)
899 (if (flyspell-overlay-p (car overlays))
900 (delete-overlay (car overlays)))
901 (setq overlays (cdr overlays)))))
902 ;; now we can use a new overlay
903 (setq flyspell-overlay
904 (make-flyspell-overlay beg end
905 'flyspell-incorrect-face 'highlight)))))
907 ;*---------------------------------------------------------------------*/
908 ;* flyspell-highlight-duplicate-region ... */
909 ;*---------------------------------------------------------------------*/
910 (defun flyspell-highlight-duplicate-region (beg end)
911 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
912 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
914 ;; we cleanup current overlay at the same position
915 (if (and (not flyspell-persistent-highlight)
916 (overlayp flyspell-overlay))
917 (delete-overlay flyspell-overlay)
918 (let ((overlays (overlays-at beg)))
919 (while (consp overlays)
920 (if (flyspell-overlay-p (car overlays))
921 (delete-overlay (car overlays)))
922 (setq overlays (cdr overlays)))))
923 ;; now we can use a new overlay
924 (setq flyspell-overlay
925 (make-flyspell-overlay beg end
926 'flyspell-duplicate-face 'highlight)))))
928 ;*---------------------------------------------------------------------*/
929 ;* flyspell-auto-correct-cache ... */
930 ;*---------------------------------------------------------------------*/
931 (defvar flyspell-auto-correct-pos nil)
932 (defvar flyspell-auto-correct-region nil)
933 (defvar flyspell-auto-correct-ring nil)
935 ;*---------------------------------------------------------------------*/
936 ;* flyspell-auto-correct-word ... */
937 ;*---------------------------------------------------------------------*/
938 (defun flyspell-auto-correct-word (pos)
939 "Correct the word at POS.
940 This command proposes various successive corrections for the word at POS.
941 The variable `flyspell-auto-correct-binding' specifies the key to bind
944 ;; use the correct dictionary
945 (ispell-accept-buffer-local-defs)
946 (if (eq flyspell-auto-correct-pos pos)
947 ;; we have already been using the function at the same location
950 (let ((start (car flyspell-auto-correct-region))
951 (len (cdr flyspell-auto-correct-region)))
952 (delete-region start (+ start len))
953 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
954 (let* ((word (car flyspell-auto-correct-ring))
956 (rplacd flyspell-auto-correct-region len)
959 (setq flyspell-auto-correct-pos (point)))
960 ;; retain cursor location
961 (let ((cursor-location pos)
962 (word (flyspell-get-word nil))
964 ;; destructure return word info list.
965 (setq start (car (cdr word))
966 end (car (cdr (cdr word)))
968 ;; now check spelling of word.
969 (process-send-string ispell-process "%\n") ;put in verbose mode
970 (process-send-string ispell-process (concat "^" word "\n"))
971 ;; wait until ispell has processed word
973 (accept-process-output ispell-process)
974 (not (string= "" (car ispell-filter)))))
975 (setq ispell-filter (cdr ispell-filter))
976 (if (listp ispell-filter)
977 (setq poss (ispell-parse-output (car ispell-filter))))
978 (cond ((or (eq poss t) (stringp poss))
979 ;; don't correct word
983 (error "Ispell: error in Ispell process"))
985 ;; the word is incorrect, we have to propose a replacement
986 (let ((replacements (if flyspell-sort-corrections
987 (sort (car (cdr (cdr poss))) 'string<)
988 (car (cdr (cdr poss))))))
989 (if (consp replacements)
991 (let ((replace (car replacements)))
993 (setq cursor-location (+ (- (length word) (- end start))
995 (if (not (equal word (car poss)))
997 ;; the save the current replacements
998 (setq flyspell-auto-correct-pos cursor-location)
999 (setq flyspell-auto-correct-region
1000 (cons start (length word)))
1001 (let ((l replacements))
1002 (while (consp (cdr l))
1004 (rplacd l (cons (car poss) replacements)))
1005 (setq flyspell-auto-correct-ring
1007 (delete-region start end)
1008 (insert word)))))))))
1009 ;; return to original location
1010 (goto-char cursor-location)
1011 (ispell-pdict-save t))))
1013 ;*---------------------------------------------------------------------*/
1014 ;* flyspell-correct-word ... */
1015 ;*---------------------------------------------------------------------*/
1016 (defun flyspell-correct-word (event)
1017 "Check spelling of word under or before the cursor.
1018 If the word is not found in dictionary, display possible corrections
1019 in a popup menu allowing you to choose one.
1021 Word syntax described by `ispell-dictionary-alist' (which see).
1023 This will check or reload the dictionary. Use \\[ispell-change-dictionary]
1024 or \\[ispell-region] to update the Ispell process."
1026 (if flyspell-use-local-map
1027 (flyspell-correct-word/mouse-keymap event)
1028 (flyspell-correct-word/local-keymap event)))
1030 ;*---------------------------------------------------------------------*/
1031 ;* flyspell-correct-word/local-keymap ... */
1032 ;*---------------------------------------------------------------------*/
1033 (defun flyspell-correct-word/local-keymap (event)
1034 "emacs 19.xx seems to be buggous. Overlay keymap does not seems
1035 to work correctly with local map. That is, if a key is not
1036 defined for the overlay keymap, the current local map, is not
1037 checked. The binding is resolved with the global map. The
1038 consequence is that we can not use overlay map with flyspell."
1040 (save-window-excursion
1041 (let ((save (point)))
1042 (mouse-set-point event)
1043 ;; we look for a flyspell overlay here
1044 (let ((overlays (overlays-at (point)))
1046 (while (consp overlays)
1047 (if (flyspell-overlay-p (car overlays))
1049 (setq overlay (car overlays))
1050 (setq overlays nil))
1051 (setq overlays (cdr overlays))))
1052 ;; we return to the correct location
1054 ;; we check to see if button2 has been used overlay a
1057 ;; yes, so we use the flyspell function
1058 (flyspell-correct-word/mouse-keymap event)
1059 ;; no so we have to use the non flyspell binding
1060 (let ((flyspell-mode nil))
1061 (if (key-binding (this-command-keys))
1062 (command-execute (key-binding (this-command-keys))))))))))
1064 ;*---------------------------------------------------------------------*/
1065 ;* flyspell-correct-word ... */
1066 ;*---------------------------------------------------------------------*/
1067 (defun flyspell-correct-word/mouse-keymap (event)
1068 "Pop up a menu of possible corrections for a misspelled word.
1069 The word checked is the word at the mouse position."
1071 ;; use the correct dictionary
1072 (ispell-accept-buffer-local-defs)
1073 ;; retain cursor location (I don't know why but save-excursion here fails).
1074 (let ((save (point)))
1075 (mouse-set-point event)
1076 (let ((cursor-location (point))
1077 (word (flyspell-get-word nil))
1078 start end poss replace)
1079 ;; destructure return word info list.
1080 (setq start (car (cdr word))
1081 end (car (cdr (cdr word)))
1083 ;; now check spelling of word.
1084 (process-send-string ispell-process "%\n") ;put in verbose mode
1085 (process-send-string ispell-process (concat "^" word "\n"))
1086 ;; wait until ispell has processed word
1088 (accept-process-output ispell-process)
1089 (not (string= "" (car ispell-filter)))))
1090 (setq ispell-filter (cdr ispell-filter))
1091 (if (listp ispell-filter)
1092 (setq poss (ispell-parse-output (car ispell-filter))))
1093 (cond ((or (eq poss t) (stringp poss))
1094 ;; don't correct word
1098 (error "Ispell: error in Ispell process"))
1099 ((string-match "GNU" (emacs-version))
1100 ;; the word is incorrect, we have to propose a replacement
1101 (setq replace (flyspell-emacs-popup event poss word))
1102 (cond ((eq replace 'ignore)
1105 (process-send-string ispell-process (concat "*" word "\n"))
1106 (flyspell-unhighlight-at cursor-location)
1107 (setq ispell-pdict-modified-p '(t)))
1108 ((or (eq replace 'buffer) (eq replace 'session))
1109 (process-send-string ispell-process (concat "@" word "\n"))
1110 (if (null ispell-pdict-modified-p)
1111 (setq ispell-pdict-modified-p
1112 (list ispell-pdict-modified-p)))
1113 (flyspell-unhighlight-at cursor-location)
1114 (if (eq replace 'buffer)
1115 (ispell-add-per-file-word-list word)))
1117 (setq word (if (atom replace) replace (car replace))
1118 cursor-location (+ (- (length word) (- end start))
1120 (if (not (equal word (car poss)))
1122 (delete-region start end)
1124 ((string-match "XEmacs" (emacs-version))
1125 (flyspell-xemacs-popup
1126 event poss word cursor-location start end)))
1127 (ispell-pdict-save t))
1128 (if (< save (point-max))
1130 (goto-char (point-max)))))
1132 ;*---------------------------------------------------------------------*/
1133 ;* flyspell-xemacs-correct ... */
1134 ;*---------------------------------------------------------------------*/
1135 (defun flyspell-xemacs-correct (replace poss word cursor-location start end)
1136 "The xemacs popup menu callback."
1137 (cond ((eq replace 'ignore)
1140 (process-send-string ispell-process (concat "*" word "\n"))
1141 (flyspell-unhighlight-at cursor-location)
1142 (setq ispell-pdict-modified-p '(t)))
1143 ((or (eq replace 'buffer) (eq replace 'session))
1144 (process-send-string ispell-process (concat "@" word "\n"))
1145 (flyspell-unhighlight-at cursor-location)
1146 (if (null ispell-pdict-modified-p)
1147 (setq ispell-pdict-modified-p
1148 (list ispell-pdict-modified-p)))
1149 (if (eq replace 'buffer)
1150 (ispell-add-per-file-word-list word)))
1152 (setq word (if (atom replace) replace (car replace))
1153 cursor-location (+ (- (length word) (- end start))
1155 (if (not (equal word (car poss)))
1157 (delete-region start end)
1161 ;*---------------------------------------------------------------------*/
1162 ;* flyspell-emacs-popup ... */
1163 ;*---------------------------------------------------------------------*/
1164 (defun flyspell-emacs-popup (event poss word)
1165 "The Emacs popup menu."
1167 (let* ((mouse-pos (mouse-position))
1168 (mouse-pos (if (nth 1 mouse-pos)
1170 (set-mouse-position (car mouse-pos)
1171 (/ (frame-width) 2) 2)
1174 (setq event (list (list (car (cdr mouse-pos))
1175 (1+ (cdr (cdr mouse-pos))))
1177 (let* ((corrects (if flyspell-sort-corrections
1178 (sort (car (cdr (cdr poss))) 'string<)
1179 (car (cdr (cdr poss)))))
1180 (cor-menu (if (consp corrects)
1181 (mapcar (lambda (correct)
1182 (list correct correct))
1185 (affix (car (cdr (cdr (cdr poss)))))
1186 (base-menu (let ((save (if (consp affix)
1188 (list (concat "Save affix: " (car affix))
1190 '("Accept (session)" accept)
1191 '("Accept (buffer)" buffer))
1192 '(("Save word" save)
1193 ("Accept (session)" session)
1194 ("Accept (buffer)" buffer)))))
1195 (if (consp cor-menu)
1196 (append cor-menu (cons "" save))
1198 (menu (cons "flyspell correction menu" base-menu)))
1199 (car (x-popup-menu event
1200 (list (format "%s [%s]" word (or ispell-local-dictionary
1204 ;*---------------------------------------------------------------------*/
1205 ;* flyspell-xemacs-popup ... */
1206 ;*---------------------------------------------------------------------*/
1207 (defun flyspell-xemacs-popup (event poss word cursor-location start end)
1208 "The xemacs popup menu."
1209 (let* ((corrects (if flyspell-sort-corrections
1210 (sort (car (cdr (cdr poss))) 'string<)
1211 (car (cdr (cdr poss)))))
1212 (cor-menu (if (consp corrects)
1213 (mapcar (lambda (correct)
1215 (list 'flyspell-xemacs-correct
1225 (affix (car (cdr (cdr (cdr poss)))))
1226 (menu (let ((save (if (consp affix)
1228 (concat "Save affix: " (car affix))
1229 (list 'flyspell-xemacs-correct
1239 (list 'flyspell-xemacs-correct
1247 (session (vector "Accept (session)"
1248 (list 'flyspell-xemacs-correct
1256 (buffer (vector "Accept (buffer)"
1257 (list 'flyspell-xemacs-correct
1265 (if (consp cor-menu)
1266 (append cor-menu (list "-" save session buffer))
1267 (list save session buffer)))))
1268 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
1274 ;;; flyspell.el ends here