* xscheme.el (scheme-interaction-mode): Doc fix.
[emacs.git] / lisp / textmodes / flyspell.el
blob9d553a688456a05a0acb68de50e2c873f8251db6
1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2001, 2002 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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
28 ;; checking.
30 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
31 ;; This applies only to the current buffer.
33 ;; To enable Flyspell in text representing computer programs, type
34 ;; M-x flyspell-prog-mode.
35 ;; In that mode only text inside comments is checked.
37 ;; Note: consider setting the variable ispell-parser to `tex' to
38 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
40 ;; Some user variables control the behavior of flyspell. They are
41 ;; those defined under the `User variables' comment.
43 ;;; Code:
45 (require 'ispell)
47 ;*---------------------------------------------------------------------*/
48 ;* Group ... */
49 ;*---------------------------------------------------------------------*/
50 (defgroup flyspell nil
51 "Spell checking on the fly."
52 :tag "FlySpell"
53 :prefix "flyspell-"
54 :group 'ispell
55 :group 'processes)
57 ;*---------------------------------------------------------------------*/
58 ;* User configuration ... */
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."
63 :group 'flyspell
64 :type 'boolean)
66 (defcustom flyspell-mark-duplications-flag t
67 "*Non-nil means Flyspell reports a repeated word as an error."
68 :group 'flyspell
69 :type 'boolean)
71 (defcustom flyspell-sort-corrections nil
72 "*Non-nil means, sort the corrections alphabetically before popping them."
73 :group 'flyspell
74 :version "21.1"
75 :type 'boolean)
77 (defcustom flyspell-duplicate-distance -1
78 "*The maximum distance for finding duplicates of unrecognized words.
79 This applies to the feature that when a word is not found in the dictionary,
80 if the same spelling occurs elsewhere in the buffer,
81 Flyspell uses a different face (`flyspell-duplicate-face') to highlight it.
82 This variable specifies how far to search to find such a duplicate.
83 -1 means no limit (search the whole buffer).
84 0 means do not search for duplicate unrecognized spellings."
85 :group 'flyspell
86 :version "21.1"
87 :type 'number)
89 (defcustom flyspell-delay 3
90 "*The number of seconds to wait before checking, after a \"delayed\" command."
91 :group 'flyspell
92 :type 'number)
94 (defcustom flyspell-persistent-highlight t
95 "*Non-nil means misspelled words remain highlighted until corrected.
96 If this variable is nil, only the most recently detected misspelled word
97 is highlighted."
98 :group 'flyspell
99 :type 'boolean)
101 (defcustom flyspell-highlight-properties t
102 "*Non-nil means highlight incorrect words even if a property exists for this word."
103 :group 'flyspell
104 :type 'boolean)
106 (defcustom flyspell-default-delayed-commands
107 '(self-insert-command
108 delete-backward-char
109 backward-or-forward-delete-char
110 delete-char
111 scrollbar-vertical-drag)
112 "The standard list of delayed commands for Flyspell.
113 See `flyspell-delayed-commands'."
114 :group 'flyspell
115 :version "21.1"
116 :type '(repeat (symbol)))
118 (defcustom flyspell-delayed-commands nil
119 "List of commands that are \"delayed\" for Flyspell mode.
120 After these commands, Flyspell checking is delayed for a short time,
121 whose length is specified by `flyspell-delay'."
122 :group 'flyspell
123 :type '(repeat (symbol)))
125 (defcustom flyspell-default-deplacement-commands
126 '(next-line
127 previous-line
128 scroll-up
129 scroll-down)
130 "The standard list of deplacement commands for Flyspell.
131 See `flyspell-deplacement-commands'."
132 :group 'flyspell
133 :version "21.1"
134 :type '(repeat (symbol)))
136 (defcustom flyspell-deplacement-commands nil
137 "List of commands that are \"deplacement\" for Flyspell mode.
138 After these commands, Flyspell checking is performed only if the previous
139 command was not the very same command."
140 :group 'flyspell
141 :version "21.1"
142 :type '(repeat (symbol)))
144 (defcustom flyspell-issue-welcome-flag t
145 "*Non-nil means that Flyspell should display a welcome message when started."
146 :group 'flyspell
147 :type 'boolean)
149 (defcustom flyspell-issue-message-flag t
150 "*Non-nil means that Flyspell emits messages when checking words."
151 :group 'flyspell
152 :type 'boolean)
154 (defcustom flyspell-incorrect-hook nil
155 "*List of functions to be called when incorrect words are encountered.
156 Each function is given three arguments: the beginning and the end
157 of the incorrect region. The third is either the symbol 'doublon' or the list
158 of possible corrections as returned by 'ispell-parse-output'.
160 If any of the functions return non-Nil, the word is not highlighted as
161 incorrect."
162 :group 'flyspell
163 :version "21.1"
164 :type 'hook)
166 (defcustom flyspell-default-dictionary nil
167 "A string that is the name of the default dictionary.
168 This is passed to the `ispell-change-dictionary' when flyspell is started.
169 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
170 when flyspell is started, the value of that variable is used instead
171 of `flyspell-default-dictionary' to select the default dictionary.
172 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
173 Ispell's ultimate default dictionary."
174 :group 'flyspell
175 :version "21.1"
176 :type '(choice string (const :tag "Default" nil)))
178 (defcustom flyspell-tex-command-regexp
179 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
180 "A string that is the regular expression that matches TeX commands."
181 :group 'flyspell
182 :version "21.1"
183 :type 'string)
185 (defcustom flyspell-check-tex-math-command nil
186 "*Non nil means check even inside TeX math environment.
187 TeX math environments are discovered by the TEXMATHP that implemented
188 inside the texmathp.el Emacs package. That package may be found at:
189 http://strw.leidenuniv.nl/~dominik/Tools"
190 :group 'flyspell
191 :type 'boolean)
193 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
194 '("francais" "deutsch8" "norsk")
195 "List of dictionary names that consider `-' as word delimiter."
196 :group 'flyspell
197 :version "21.1"
198 :type '(repeat (string)))
200 (defcustom flyspell-abbrev-p
202 "*If true, add correction to abbreviation table."
203 :group 'flyspell
204 :version "21.1"
205 :type 'boolean)
207 (defcustom flyspell-use-global-abbrev-table-p
209 "*If true, prefer global abbrev table to local abbrev table."
210 :group 'flyspell
211 :version "21.1"
212 :type 'boolean)
214 ;;;###autoload
215 (defcustom flyspell-mode-line-string " Fly"
216 "*String displayed on the modeline when flyspell is active.
217 Set this to nil if you don't want a modeline indicator."
218 :group 'flyspell
219 :type '(choice string (const :tag "None" nil)))
221 (defcustom flyspell-large-region 1000
222 "*The threshold that determines if a region is small.
223 The `flyspell-region' function is invoked if the region is small, the
224 word are checked one after the other using regular flyspell check
225 means. If the region is large, a new Ispell process is spawned to get
226 speed."
227 :group 'flyspell
228 :version "21.1"
229 :type 'number)
231 (defcustom flyspell-insert-function (function insert)
232 "*Function for inserting word by flyspell upon correction."
233 :group 'flyspell
234 :type 'function)
236 (defcustom flyspell-before-incorrect-word-string nil
237 "String used to indicate an incorrect word starting."
238 :group 'flyspell
239 :type '(choice string (const nil)))
241 (defcustom flyspell-after-incorrect-word-string nil
242 "String used to indicate an incorrect word ending."
243 :group 'flyspell
244 :type '(choice string (const nil)))
246 ;*---------------------------------------------------------------------*/
247 ;* Mode specific options */
248 ;* ------------------------------------------------------------- */
249 ;* Mode specific options enable users to disable flyspell on */
250 ;* certain word depending of the emacs mode. For instance, when */
251 ;* using flyspell with mail-mode add the following expression */
252 ;* in your .emacs file: */
253 ;* (add-hook 'mail-mode */
254 ;* '(lambda () (setq flyspell-generic-check-word-p */
255 ;* 'mail-mode-flyspell-verify))) */
256 ;*---------------------------------------------------------------------*/
257 (defvar flyspell-generic-check-word-p nil
258 "Function providing per-mode customization over which words are flyspelled.
259 Returns t to continue checking, nil otherwise.
260 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
261 property of the major mode name.")
262 (make-variable-buffer-local 'flyspell-generic-check-word-p)
264 ;*--- mail mode -------------------------------------------------------*/
265 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
266 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
267 (defun mail-mode-flyspell-verify ()
268 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
269 (let ((in-headers (save-excursion
270 (re-search-forward mail-header-separator nil t)))
271 (in-signature (save-excursion
272 (re-search-backward message-signature-separator nil t))))
273 (cond (in-headers
274 (and (save-excursion (beginning-of-line)
275 (looking-at "^Subject:"))
276 (> (point) (match-end 0))))
277 (in-signature
278 nil)
280 (save-excursion
281 (beginning-of-line)
282 (not (looking-at "[>}|]\\To:")))))))
284 ;*--- texinfo mode ----------------------------------------------------*/
285 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
286 (defun texinfo-mode-flyspell-verify ()
287 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
288 (save-excursion
289 (forward-word -1)
290 (not (looking-at "@"))))
292 ;*--- tex mode --------------------------------------------------------*/
293 (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
294 (defun tex-mode-flyspell-verify ()
295 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
296 (and
297 (not (save-excursion
298 (re-search-backward "^[ \t]*%%%[ \t]+Local" (point-min) t)))
299 (not (save-excursion
300 (let ((this (point-marker))
301 (e (progn (end-of-line) (point-marker))))
302 (beginning-of-line)
303 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t)
304 (and (>= this (match-beginning 0))
305 (<= this (match-end 0)) )))))))
307 ;*--- sgml mode -------------------------------------------------------*/
308 (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
309 (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
311 (defun sgml-mode-flyspell-verify ()
312 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
313 (not (save-excursion
314 (let ((this (point-marker))
315 (s (progn (beginning-of-line) (point-marker)))
316 (e (progn (end-of-line) (point-marker))))
317 (or (progn
318 (goto-char this)
319 (and (re-search-forward "[^<]*>" e t)
320 (= (match-beginning 0) this)))
321 (progn
322 (goto-char this)
323 (and (re-search-backward "<[^>]*" s t)
324 (= (match-end 0) this)))
325 (and (progn
326 (goto-char this)
327 (and (re-search-forward "[^&]*;" e t)
328 (= (match-beginning 0) this)))
329 (progn
330 (goto-char this)
331 (and (re-search-backward "&[^;]*" s t)
332 (= (match-end 0) this)))))))))
334 ;*---------------------------------------------------------------------*/
335 ;* Programming mode */
336 ;*---------------------------------------------------------------------*/
337 (defvar flyspell-prog-text-faces
338 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
339 "Faces corresponding to text in programming-mode buffers.")
341 (defun flyspell-generic-progmode-verify ()
342 "Used for `flyspell-generic-check-word-p' in programming modes."
343 (let ((f (get-text-property (point) 'face)))
344 (memq f flyspell-prog-text-faces)))
346 ;;;###autoload
347 (defun flyspell-prog-mode ()
348 "Turn on `flyspell-mode' for comments and strings."
349 (interactive)
350 (setq flyspell-generic-check-word-p 'flyspell-generic-progmode-verify)
351 (flyspell-mode 1))
353 ;*---------------------------------------------------------------------*/
354 ;* Overlay compatibility */
355 ;*---------------------------------------------------------------------*/
356 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
357 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
358 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
359 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
360 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
361 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
362 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
363 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
365 ;*---------------------------------------------------------------------*/
366 ;* Which emacs are we currently running */
367 ;*---------------------------------------------------------------------*/
368 (defvar flyspell-emacs
369 (cond
370 ((string-match "XEmacs" emacs-version)
371 'xemacs)
373 'emacs))
374 "The type of Emacs we are currently running.")
376 (defvar flyspell-use-local-map
377 (or (eq flyspell-emacs 'xemacs)
378 (not (string< emacs-version "20"))))
380 ;*---------------------------------------------------------------------*/
381 ;* The minor mode declaration. */
382 ;*---------------------------------------------------------------------*/
383 (eval-when-compile (defvar flyspell-local-mouse-map))
385 ;;;###autoload
386 (defvar flyspell-mode nil)
387 (make-variable-buffer-local 'flyspell-mode)
389 (defvar flyspell-mouse-map
390 (let ((map (make-sparse-keymap)))
391 (cond
392 ((eq flyspell-emacs 'xemacs)
393 (define-key map [(button2)] #'flyspell-correct-word)
394 (define-key map "\M-\t" #'flyspell-auto-correct-word))
395 (flyspell-use-local-map
396 (define-key map [(mouse-2)] #'flyspell-correct-word)
397 (define-key map "\M-\t" #'flyspell-auto-correct-word)))
398 map))
400 ;;;###autoload
401 (defvar flyspell-mode-map (make-sparse-keymap))
403 ;; mouse, keyboard bindings and misc definition
404 (when (or (assoc 'flyspell-mode minor-mode-map-alist)
405 (setq minor-mode-map-alist
406 (cons (cons 'flyspell-mode flyspell-mode-map)
407 minor-mode-map-alist)))
408 (define-key flyspell-mode-map "\M-\t" 'flyspell-auto-correct-word))
410 ;; the name of the overlay property that defines the keymap
411 (defvar flyspell-overlay-keymap-property-name 'keymap)
413 ;; dash character machinery
414 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
415 "*Non-nil means that the `-' char is considered as a word delimiter.")
416 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
417 (defvar flyspell-dash-dictionary nil)
418 (make-variable-buffer-local 'flyspell-dash-dictionary)
419 (defvar flyspell-dash-local-dictionary nil)
420 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
422 ;*---------------------------------------------------------------------*/
423 ;* Highlighting */
424 ;*---------------------------------------------------------------------*/
425 (defface flyspell-incorrect-face
426 (if (eq flyspell-emacs 'xemacs)
427 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
428 (t (:bold t)))
429 '((((class color)) (:foreground "OrangeRed" :weight bold :underline t))
430 (t (:weight bold))))
431 "Face used for marking a misspelled word in Flyspell."
432 :group 'flyspell)
434 (defface flyspell-duplicate-face
435 (if (eq flyspell-emacs 'xemacs)
436 '((((class color)) (:foreground "Gold3" :bold t :underline t))
437 (t (:bold t)))
438 '((((class color)) (:foreground "Gold3" :weight bold :underline t))
439 (t (:weight bold))))
440 "Face used for marking a misspelled word that appears twice in the buffer.
441 See also `flyspell-duplicate-distance'."
442 :group 'flyspell)
444 (defvar flyspell-overlay nil)
446 ;*---------------------------------------------------------------------*/
447 ;* flyspell-mode ... */
448 ;*---------------------------------------------------------------------*/
449 ;;;###autoload
450 (defun flyspell-mode (&optional arg)
451 "Minor mode performing on-the-fly spelling checking.
452 Ispell is automatically spawned on background for each entered words.
453 The default flyspell behavior is to highlight incorrect words.
454 With no argument, this command toggles Flyspell mode.
455 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
457 Bindings:
458 \\[ispell-word]: correct words (using Ispell).
459 \\[flyspell-auto-correct-word]: automatically correct word.
460 \\[flyspell-correct-word] (or mouse-2): popup correct words.
462 Hooks:
463 This runs `flyspell-mode-hook' after flyspell is entered.
465 Remark:
466 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
467 valid. For instance, a personal dictionary can be used by
468 invoking `ispell-change-dictionary'.
470 Consider using the `ispell-parser' to check your text. For instance
471 consider adding:
472 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
473 in your .emacs file.
475 \\[flyspell-region] checks all words inside a region.
476 \\[flyspell-buffer] checks the whole buffer."
477 (interactive "P")
478 (let ((old-flyspell-mode flyspell-mode))
479 ;; Mark the mode as on or off.
480 (setq flyspell-mode (not (or (and (null arg) flyspell-mode)
481 (<= (prefix-numeric-value arg) 0))))
482 ;; Do the real work.
483 (unless (eq flyspell-mode old-flyspell-mode)
484 (if flyspell-mode
485 (flyspell-mode-on)
486 (flyspell-mode-off))
487 ;; Force modeline redisplay.
488 (set-buffer-modified-p (buffer-modified-p)))))
490 ;*---------------------------------------------------------------------*/
491 ;* Autoloading */
492 ;*---------------------------------------------------------------------*/
493 ;;;###autoload
494 (add-minor-mode 'flyspell-mode
495 'flyspell-mode-line-string
496 flyspell-mode-map
498 'flyspell-mode)
500 ;*---------------------------------------------------------------------*/
501 ;* flyspell-buffers ... */
502 ;* ------------------------------------------------------------- */
503 ;* For remembering buffers running flyspell */
504 ;*---------------------------------------------------------------------*/
505 (defvar flyspell-buffers nil)
507 ;*---------------------------------------------------------------------*/
508 ;* flyspell-minibuffer-p ... */
509 ;*---------------------------------------------------------------------*/
510 (defun flyspell-minibuffer-p (buffer)
511 "Is BUFFER a minibuffer?"
512 (let ((ws (get-buffer-window-list buffer t)))
513 (and (consp ws) (window-minibuffer-p (car ws)))))
515 ;*---------------------------------------------------------------------*/
516 ;* flyspell-version ... */
517 ;*---------------------------------------------------------------------*/
518 ;;;###autoload
519 (defun flyspell-version ()
520 "The flyspell version"
521 (interactive)
522 "1.6h")
524 ;*---------------------------------------------------------------------*/
525 ;* flyspell-accept-buffer-local-defs ... */
526 ;*---------------------------------------------------------------------*/
527 (defun flyspell-accept-buffer-local-defs ()
528 (ispell-accept-buffer-local-defs)
529 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
530 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
531 ;; the dictionary has changed
532 (progn
533 (setq flyspell-dash-dictionary ispell-dictionary)
534 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
535 (if (member (or ispell-local-dictionary ispell-dictionary)
536 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
537 (setq flyspell-consider-dash-as-word-delimiter-flag t)
538 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
540 ;*---------------------------------------------------------------------*/
541 ;* flyspell-mode-on ... */
542 ;*---------------------------------------------------------------------*/
543 (defun flyspell-mode-on ()
544 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
545 (setq ispell-highlight-face 'flyspell-incorrect-face)
546 ;; local dictionaries setup
547 (ispell-change-dictionary
548 (or ispell-local-dictionary ispell-dictionary flyspell-default-dictionary))
549 ;; we have to force ispell to accept the local definition or
550 ;; otherwise it could be too late, the local dictionary may
551 ;; be forgotten!
552 (flyspell-accept-buffer-local-defs)
553 ;; we put the `flyspell-delayed' property on some commands
554 (flyspell-delay-commands)
555 ;; we put the `flyspell-deplacement' property on some commands
556 (flyspell-deplacement-commands)
557 ;; we bound flyspell action to post-command hook
558 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
559 ;; we bound flyspell action to pre-command hook
560 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
561 ;; we bound flyspell action to after-change hook
562 (make-local-variable 'after-change-functions)
563 (setq after-change-functions
564 (cons 'flyspell-after-change-function after-change-functions))
565 ;; set flyspell-generic-check-word-p based on the major mode
566 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
567 (if mode-predicate
568 (setq flyspell-generic-check-word-p mode-predicate)))
569 ;; the welcome message
570 (if (and flyspell-issue-message-flag
571 flyspell-issue-welcome-flag
572 (interactive-p))
573 (let ((binding (where-is-internal 'flyspell-auto-correct-word
574 nil 'non-ascii)))
575 (message
576 (if binding
577 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
578 (key-description binding))
579 "Welcome to flyspell. Use Mouse-2 to correct words."))))
580 ;; we end with the flyspell hooks
581 (run-hooks 'flyspell-mode-hook))
583 ;*---------------------------------------------------------------------*/
584 ;* flyspell-delay-commands ... */
585 ;*---------------------------------------------------------------------*/
586 (defun flyspell-delay-commands ()
587 "Install the standard set of Flyspell delayed commands."
588 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
589 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
591 ;*---------------------------------------------------------------------*/
592 ;* flyspell-delay-command ... */
593 ;*---------------------------------------------------------------------*/
594 (defun flyspell-delay-command (command)
595 "Set COMMAND to be delayed, for Flyspell.
596 When flyspell `post-command-hook' is invoked because a delayed command
597 as been used the current word is not immediately checked.
598 It will be checked only after `flyspell-delay' seconds."
599 (interactive "SDelay Flyspell after Command: ")
600 (put command 'flyspell-delayed t))
602 ;*---------------------------------------------------------------------*/
603 ;* flyspell-deplacement-commands ... */
604 ;*---------------------------------------------------------------------*/
605 (defun flyspell-deplacement-commands ()
606 "Install the standard set of Flyspell deplacement commands."
607 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
608 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
610 ;*---------------------------------------------------------------------*/
611 ;* flyspell-deplacement-command ... */
612 ;*---------------------------------------------------------------------*/
613 (defun flyspell-deplacement-command (command)
614 "Set COMMAND that implement cursor movements, for Flyspell.
615 When flyspell `post-command-hook' is invoked because of a deplacement command
616 as been used the current word is checked only if the previous command was
617 not the very same deplacement command."
618 (interactive "SDeplacement Flyspell after Command: ")
619 (put command 'flyspell-deplacement t))
621 ;*---------------------------------------------------------------------*/
622 ;* flyspell-word-cache ... */
623 ;*---------------------------------------------------------------------*/
624 (defvar flyspell-word-cache-start nil)
625 (defvar flyspell-word-cache-end nil)
626 (defvar flyspell-word-cache-word nil)
627 (make-variable-buffer-local 'flyspell-word-cache-start)
628 (make-variable-buffer-local 'flyspell-word-cache-end)
629 (make-variable-buffer-local 'flyspell-word-cache-word)
631 ;*---------------------------------------------------------------------*/
632 ;* The flyspell pre-hook, store the current position. In the */
633 ;* post command hook, we will check, if the word at this position */
634 ;* has to be spell checked. */
635 ;*---------------------------------------------------------------------*/
636 (defvar flyspell-pre-buffer nil)
637 (defvar flyspell-pre-point nil)
638 (defvar flyspell-pre-column nil)
639 (defvar flyspell-pre-pre-buffer nil)
640 (defvar flyspell-pre-pre-point nil)
642 ;*---------------------------------------------------------------------*/
643 ;* flyspell-previous-command ... */
644 ;*---------------------------------------------------------------------*/
645 (defvar flyspell-previous-command nil
646 "The last interactive command checked by Flyspell.")
648 ;*---------------------------------------------------------------------*/
649 ;* flyspell-pre-command-hook ... */
650 ;*---------------------------------------------------------------------*/
651 (defun flyspell-pre-command-hook ()
652 "Save the current buffer and point for Flyspell's post-command hook."
653 (interactive)
654 (setq flyspell-pre-buffer (current-buffer))
655 (setq flyspell-pre-point (point))
656 (setq flyspell-pre-column (current-column)))
658 ;*---------------------------------------------------------------------*/
659 ;* flyspell-mode-off ... */
660 ;*---------------------------------------------------------------------*/
661 ;;;###autoload
662 (defun flyspell-mode-off ()
663 "Turn Flyspell mode off."
664 ;; we remove the hooks
665 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
666 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
667 (setq after-change-functions (delq 'flyspell-after-change-function
668 after-change-functions))
669 ;; we remove all the flyspell hilightings
670 (flyspell-delete-all-overlays)
671 ;; we have to erase pre cache variables
672 (setq flyspell-pre-buffer nil)
673 (setq flyspell-pre-point nil)
674 ;; we mark the mode as killed
675 (setq flyspell-mode nil))
677 ;*---------------------------------------------------------------------*/
678 ;* flyspell-check-pre-word-p ... */
679 ;*---------------------------------------------------------------------*/
680 (defun flyspell-check-pre-word-p ()
681 "Return non-nil if we should to check the word before point.
682 More precisely, it applies to the word that was before point
683 before the current command."
684 (cond
685 ((or (not (numberp flyspell-pre-point))
686 (not (bufferp flyspell-pre-buffer))
687 (not (buffer-live-p flyspell-pre-buffer)))
688 nil)
689 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
690 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
691 nil)
692 ((or (and (= flyspell-pre-point (- (point) 1))
693 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
694 (= flyspell-pre-point (point))
695 (= flyspell-pre-point (+ (point) 1)))
696 nil)
697 ((and (symbolp this-command)
698 (not executing-kbd-macro)
699 (or (get this-command 'flyspell-delayed)
700 (and (get this-command 'flyspell-deplacement)
701 (eq flyspell-previous-command this-command)))
702 (or (= (current-column) 0)
703 (= (current-column) flyspell-pre-column)
704 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
705 nil)
706 ((not (eq (current-buffer) flyspell-pre-buffer))
708 ((not (and (numberp flyspell-word-cache-start)
709 (numberp flyspell-word-cache-end)))
712 (or (< flyspell-pre-point flyspell-word-cache-start)
713 (> flyspell-pre-point flyspell-word-cache-end)))))
715 ;*---------------------------------------------------------------------*/
716 ;* The flyspell after-change-hook, store the change position. In */
717 ;* the post command hook, we will check, if the word at this */
718 ;* position has to be spell checked. */
719 ;*---------------------------------------------------------------------*/
720 (defvar flyspell-changes nil)
722 ;*---------------------------------------------------------------------*/
723 ;* flyspell-after-change-function ... */
724 ;*---------------------------------------------------------------------*/
725 (defun flyspell-after-change-function (start stop len)
726 "Save the current buffer and point for Flyspell's post-command hook."
727 (interactive)
728 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
730 ;*---------------------------------------------------------------------*/
731 ;* flyspell-check-changed-word-p ... */
732 ;*---------------------------------------------------------------------*/
733 (defun flyspell-check-changed-word-p (start stop)
734 "Return t when the changed word has to be checked.
735 The answer depends of several criteria.
736 Mostly we check word delimiters."
737 (cond
738 ((and (eq (char-after start) ?\n) (> stop start))
740 ((not (numberp flyspell-pre-point))
742 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
743 nil)
744 ((let ((pos (point)))
745 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
746 nil)
748 t)))
750 ;*---------------------------------------------------------------------*/
751 ;* flyspell-check-word-p ... */
752 ;*---------------------------------------------------------------------*/
753 (defun flyspell-check-word-p ()
754 "Return t when the word at `point' has to be checked.
755 The answer depends of several criteria.
756 Mostly we check word delimiters."
757 (cond
758 ((<= (- (point-max) 1) (point-min))
759 ;; the buffer is not filled enough
760 nil)
761 ((and (and (> (current-column) 0)
762 (not (eq (current-column) flyspell-pre-column)))
763 (save-excursion
764 (backward-char 1)
765 (and (looking-at (flyspell-get-not-casechars))
766 (or flyspell-consider-dash-as-word-delimiter-flag
767 (not (looking-at "\\-"))))))
768 ;; yes because we have reached or typed a word delimiter.
770 ((symbolp this-command)
771 (cond
772 ((get this-command 'flyspell-deplacement)
773 (not (eq flyspell-previous-command this-command)))
774 ((get this-command 'flyspell-delayed)
775 ;; the current command is not delayed, that
776 ;; is that we must check the word now
777 (if (fboundp 'about-xemacs)
778 (sit-for flyspell-delay nil)
779 (sit-for flyspell-delay 0 nil)))
780 (t t)))
781 (t t)))
783 ;*---------------------------------------------------------------------*/
784 ;* flyspell-debug-signal-no-check ... */
785 ;*---------------------------------------------------------------------*/
786 (defun flyspell-debug-signal-no-check (msg obj)
787 (setq debug-on-error t)
788 (save-excursion
789 (let ((buffer (get-buffer-create "*flyspell-debug*")))
790 (set-buffer buffer)
791 (erase-buffer)
792 (insert "NO-CHECK:\n")
793 (insert (format " %S : %S\n" msg obj)))))
795 ;*---------------------------------------------------------------------*/
796 ;* flyspell-debug-signal-pre-word-checked ... */
797 ;*---------------------------------------------------------------------*/
798 (defun flyspell-debug-signal-pre-word-checked ()
799 (setq debug-on-error t)
800 (save-excursion
801 (let ((buffer (get-buffer-create "*flyspell-debug*")))
802 (set-buffer buffer)
803 (insert "PRE-WORD:\n")
804 (insert (format " pre-point : %S\n" flyspell-pre-point))
805 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
806 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
807 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
808 (goto-char (point-max)))))
810 ;*---------------------------------------------------------------------*/
811 ;* flyspell-debug-signal-word-checked ... */
812 ;*---------------------------------------------------------------------*/
813 (defun flyspell-debug-signal-word-checked ()
814 (setq debug-on-error t)
815 (save-excursion
816 (let ((oldbuf (current-buffer))
817 (buffer (get-buffer-create "*flyspell-debug*"))
818 (point (point)))
819 (set-buffer buffer)
820 (insert "WORD:\n")
821 (insert (format " this-cmd : %S\n" this-command))
822 (insert (format " delayed : %S\n" (and (symbolp this-command)
823 (get this-command 'flyspell-delayed))))
824 (insert (format " point : %S\n" point))
825 (insert (format " prev-char : [%c] %S\n"
826 (progn
827 (set-buffer oldbuf)
828 (let ((c (if (> (point) (point-min))
829 (save-excursion
830 (backward-char 1)
831 (char-after (point)))
832 ? )))
833 (set-buffer buffer)
835 (progn
836 (set-buffer oldbuf)
837 (let ((c (if (> (point) (point-min))
838 (save-excursion
839 (backward-char 1)
840 (and (and (looking-at (flyspell-get-not-casechars)) 1)
841 (and (or flyspell-consider-dash-as-word-delimiter-flag
842 (not (looking-at "\\-"))) 2))))))
843 (set-buffer buffer)
844 c))))
845 (insert (format " because : %S\n"
846 (cond
847 ((not (and (symbolp this-command)
848 (get this-command 'flyspell-delayed)))
849 ;; the current command is not delayed, that
850 ;; is that we must check the word now
851 'not-delayed)
852 ((progn
853 (set-buffer oldbuf)
854 (let ((c (if (> (point) (point-min))
855 (save-excursion
856 (backward-char 1)
857 (and (looking-at (flyspell-get-not-casechars))
858 (or flyspell-consider-dash-as-word-delimiter-flag
859 (not (looking-at "\\-"))))))))
860 (set-buffer buffer)
862 ;; yes because we have reached or typed a word delimiter.
863 'separator)
864 ((not (integerp flyspell-delay))
865 ;; yes because the user had set up a no-delay configuration.
866 'no-delay)
868 'sit-for))))
869 (goto-char (point-max)))))
871 ;*---------------------------------------------------------------------*/
872 ;* flyspell-debug-signal-changed-checked ... */
873 ;*---------------------------------------------------------------------*/
874 (defun flyspell-debug-signal-changed-checked ()
875 (setq debug-on-error t)
876 (save-excursion
877 (let ((buffer (get-buffer-create "*flyspell-debug*"))
878 (point (point)))
879 (set-buffer buffer)
880 (insert "CHANGED WORD:\n")
881 (insert (format " point : %S\n" point))
882 (goto-char (point-max)))))
884 ;*---------------------------------------------------------------------*/
885 ;* flyspell-post-command-hook ... */
886 ;* ------------------------------------------------------------- */
887 ;* It is possible that we check several words: */
888 ;* 1- the current word is checked if the predicate */
889 ;* FLYSPELL-CHECK-WORD-P is true */
890 ;* 2- the word that used to be the current word before the */
891 ;* THIS-COMMAND is checked if: */
892 ;* a- the previous word is different from the current word */
893 ;* b- the previous word as not just been checked by the */
894 ;* previous FLYSPELL-POST-COMMAND-HOOK */
895 ;* 3- the words changed by the THIS-COMMAND that are neither the */
896 ;* previous word nor the current word */
897 ;*---------------------------------------------------------------------*/
898 (defun flyspell-post-command-hook ()
899 "The `post-command-hook' used by flyspell to check a word in-the-fly."
900 (interactive)
901 (let ((command this-command))
902 (if (flyspell-check-pre-word-p)
903 (save-excursion
904 '(flyspell-debug-signal-pre-word-checked)
905 (set-buffer flyspell-pre-buffer)
906 (save-excursion
907 (goto-char flyspell-pre-point)
908 (flyspell-word))))
909 (if (flyspell-check-word-p)
910 (progn
911 '(flyspell-debug-signal-word-checked)
912 (flyspell-word)
913 ;; we remember which word we have just checked.
914 ;; this will be used next time we will check a word
915 ;; to compare the next current word with the word
916 ;; that as been registered in the pre-command-hook
917 ;; that is these variables are used within the predicate
918 ;; FLYSPELL-CHECK-PRE-WORD-P
919 (setq flyspell-pre-pre-buffer (current-buffer))
920 (setq flyspell-pre-pre-point (point)))
921 (progn
922 (setq flyspell-pre-pre-buffer nil)
923 (setq flyspell-pre-pre-point nil)
924 ;; when a word is not checked because of a delayed command
925 ;; we do not disable the ispell cache.
926 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
927 (setq flyspell-word-cache-end -1))))
928 (while (consp flyspell-changes)
929 (let ((start (car (car flyspell-changes)))
930 (stop (cdr (car flyspell-changes))))
931 (if (flyspell-check-changed-word-p start stop)
932 (save-excursion
933 '(flyspell-debug-signal-changed-checked)
934 (goto-char start)
935 (flyspell-word)))
936 (setq flyspell-changes (cdr flyspell-changes))))
937 (setq flyspell-previous-command command)))
939 ;*---------------------------------------------------------------------*/
940 ;* flyspell-notify-misspell ... */
941 ;*---------------------------------------------------------------------*/
942 (defun flyspell-notify-misspell (start end word poss)
943 (let ((replacements (if (stringp poss)
944 poss
945 (if flyspell-sort-corrections
946 (sort (car (cdr (cdr poss))) 'string<)
947 (car (cdr (cdr poss)))))))
948 (if flyspell-issue-message-flag
949 (message (format "mispelling `%s' %S" word replacements)))))
951 ;*---------------------------------------------------------------------*/
952 ;* flyspell-word ... */
953 ;*---------------------------------------------------------------------*/
954 (defun flyspell-word (&optional following)
955 "Spell check a word."
956 (interactive (list current-prefix-arg))
957 (if (interactive-p)
958 (setq following ispell-following-word))
959 (save-excursion
960 ;; use the correct dictionary
961 (flyspell-accept-buffer-local-defs)
962 (let* ((cursor-location (point))
963 (flyspell-word (flyspell-get-word following))
964 start end poss word)
965 (if (or (eq flyspell-word nil)
966 (and (fboundp flyspell-generic-check-word-p)
967 (not (funcall flyspell-generic-check-word-p))))
969 (progn
970 ;; destructure return flyspell-word info list.
971 (setq start (car (cdr flyspell-word))
972 end (car (cdr (cdr flyspell-word)))
973 word (car flyspell-word))
974 ;; before checking in the directory, we check for doublons.
975 (cond
976 ((and (or (not (eq ispell-parser 'tex))
977 (not (eq (char-after start) ?\\)))
978 flyspell-mark-duplications-flag
979 (save-excursion
980 (goto-char start)
981 (word-search-backward word
982 (- start
983 (+ 1 (- end start)))
984 t)))
985 ;; yes, this is a doublon
986 (flyspell-highlight-incorrect-region start end 'doublon))
987 ((and (eq flyspell-word-cache-start start)
988 (eq flyspell-word-cache-end end)
989 (string-equal flyspell-word-cache-word word))
990 ;; this word had been already checked, we skip
991 nil)
992 ((and (eq ispell-parser 'tex)
993 (flyspell-tex-command-p flyspell-word))
994 ;; this is a correct word (because a tex command)
995 (flyspell-unhighlight-at start)
996 (if (> end start)
997 (flyspell-unhighlight-at (- end 1)))
1000 ;; we setup the cache
1001 (setq flyspell-word-cache-start start)
1002 (setq flyspell-word-cache-end end)
1003 (setq flyspell-word-cache-word word)
1004 ;; now check spelling of word.
1005 (process-send-string ispell-process "%\n")
1006 ;; put in verbose mode
1007 (process-send-string ispell-process
1008 (concat "^" word "\n"))
1009 ;; we mark the ispell process so it can be killed
1010 ;; when emacs is exited without query
1011 (if (fboundp 'process-kill-without-query)
1012 (process-kill-without-query ispell-process))
1013 ;; wait until ispell has processed word
1014 (while (progn
1015 (accept-process-output ispell-process)
1016 (not (string= "" (car ispell-filter)))))
1017 ;; (process-send-string ispell-process "!\n")
1018 ;; back to terse mode.
1019 (setq ispell-filter (cdr ispell-filter))
1020 (if (consp ispell-filter)
1021 (setq poss (ispell-parse-output (car ispell-filter))))
1022 (cond ((eq poss t)
1023 ;; correct
1024 (flyspell-unhighlight-at start)
1025 (if (> end start)
1026 (flyspell-unhighlight-at (- end 1)))
1028 ((and (stringp poss) flyspell-highlight-flag)
1029 ;; correct
1030 (flyspell-unhighlight-at start)
1031 (if (> end start)
1032 (flyspell-unhighlight-at (- end 1)))
1034 ((null poss)
1035 (flyspell-unhighlight-at start)
1036 (if (> end start)
1037 (flyspell-unhighlight-at (- end 1))))
1038 ((or (and (< flyspell-duplicate-distance 0)
1039 (or (save-excursion
1040 (goto-char start)
1041 (word-search-backward word
1042 (point-min)
1044 (save-excursion
1045 (goto-char end)
1046 (word-search-forward word
1047 (point-max)
1048 t))))
1049 (and (> flyspell-duplicate-distance 0)
1050 (or (save-excursion
1051 (goto-char start)
1052 (word-search-backward
1053 word
1054 (- start
1055 flyspell-duplicate-distance)
1057 (save-excursion
1058 (goto-char end)
1059 (word-search-forward
1060 word
1061 (+ end
1062 flyspell-duplicate-distance)
1063 t)))))
1064 (if flyspell-highlight-flag
1065 (flyspell-highlight-duplicate-region start end)
1066 (message (format "duplicate `%s'" word))))
1068 ;; incorrect highlight the location
1069 (if flyspell-highlight-flag
1070 (flyspell-highlight-incorrect-region start end poss)
1071 (flyspell-notify-misspell start end word poss))))
1072 ;; return to original location
1073 (goto-char cursor-location)
1074 (if ispell-quit (setq ispell-quit nil)))))))))
1076 ;*---------------------------------------------------------------------*/
1077 ;* flyspell-tex-math-initialized ... */
1078 ;*---------------------------------------------------------------------*/
1079 (defvar flyspell-tex-math-initialized nil)
1081 ;*---------------------------------------------------------------------*/
1082 ;* flyspell-math-tex-command-p ... */
1083 ;* ------------------------------------------------------------- */
1084 ;* This function uses the texmathp package to check if (point) */
1085 ;* is within a tex command. In order to avoid using */
1086 ;* condition-case each time we use the variable */
1087 ;* flyspell-tex-math-initialized to make a special case the first */
1088 ;* time that function is called. */
1089 ;*---------------------------------------------------------------------*/
1090 (defun flyspell-math-tex-command-p ()
1091 (cond
1092 (flyspell-check-tex-math-command
1093 nil)
1094 ((eq flyspell-tex-math-initialized t)
1095 (texmathp))
1096 ((eq flyspell-tex-math-initialized 'error)
1097 nil)
1099 (setq flyspell-tex-math-initialized t)
1100 (condition-case nil
1101 (texmathp)
1102 (error (progn
1103 (setq flyspell-tex-math-initialized 'error)
1104 nil))))))
1106 ;*---------------------------------------------------------------------*/
1107 ;* flyspell-tex-command-p ... */
1108 ;*---------------------------------------------------------------------*/
1109 (defun flyspell-tex-command-p (word)
1110 "Return t if WORD is a TeX command."
1111 (or (save-excursion
1112 (let ((b (car (cdr word))))
1113 (and (re-search-backward "\\\\" (- (point) 100) t)
1114 (or (= (match-end 0) b)
1115 (and (goto-char (match-end 0))
1116 (looking-at flyspell-tex-command-regexp)
1117 (>= (match-end 0) b))))))
1118 (flyspell-math-tex-command-p)))
1120 ;*---------------------------------------------------------------------*/
1121 ;* flyspell-casechars-cache ... */
1122 ;*---------------------------------------------------------------------*/
1123 (defvar flyspell-casechars-cache nil)
1124 (defvar flyspell-ispell-casechars-cache nil)
1125 (make-variable-buffer-local 'flyspell-casechars-cache)
1126 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1128 ;*---------------------------------------------------------------------*/
1129 ;* flyspell-get-casechars ... */
1130 ;*---------------------------------------------------------------------*/
1131 (defun flyspell-get-casechars ()
1132 "This function builds a string that is the regexp of word chars.
1133 In order to avoid one useless string construction,
1134 this function changes the last char of the `ispell-casechars' string."
1135 (let ((ispell-casechars (ispell-get-casechars)))
1136 (cond
1137 ((eq ispell-parser 'tex)
1138 (setq flyspell-ispell-casechars-cache ispell-casechars)
1139 (setq flyspell-casechars-cache
1140 (concat (substring ispell-casechars
1142 (- (length ispell-casechars) 1))
1143 "]"))
1144 flyspell-casechars-cache)
1146 (setq flyspell-ispell-casechars-cache ispell-casechars)
1147 (setq flyspell-casechars-cache ispell-casechars)
1148 flyspell-casechars-cache))))
1150 ;*---------------------------------------------------------------------*/
1151 ;* flyspell-get-not-casechars-cache ... */
1152 ;*---------------------------------------------------------------------*/
1153 (defvar flyspell-not-casechars-cache nil)
1154 (defvar flyspell-ispell-not-casechars-cache nil)
1155 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1156 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1158 ;*---------------------------------------------------------------------*/
1159 ;* flyspell-get-not-casechars ... */
1160 ;*---------------------------------------------------------------------*/
1161 (defun flyspell-get-not-casechars ()
1162 "This function builds a string that is the regexp of non-word chars."
1163 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1164 (cond
1165 ((eq ispell-parser 'tex)
1166 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1167 (setq flyspell-not-casechars-cache
1168 (concat (substring ispell-not-casechars
1170 (- (length ispell-not-casechars) 1))
1171 "]"))
1172 flyspell-not-casechars-cache)
1174 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1175 (setq flyspell-not-casechars-cache ispell-not-casechars)
1176 flyspell-not-casechars-cache))))
1178 ;*---------------------------------------------------------------------*/
1179 ;* flyspell-get-word ... */
1180 ;*---------------------------------------------------------------------*/
1181 (defun flyspell-get-word (following)
1182 "Return the word for spell-checking according to Ispell syntax.
1183 If argument FOLLOWING is non-nil or if `ispell-following-word'
1184 is non-nil when called interactively, then the following word
1185 \(rather than preceding\) is checked when the cursor is not over a word.
1186 Optional second argument contains other chars that can be included in word
1187 many times.
1189 Word syntax described by `ispell-dictionary-alist' (which see)."
1190 (let* ((flyspell-casechars (flyspell-get-casechars))
1191 (flyspell-not-casechars (flyspell-get-not-casechars))
1192 (ispell-otherchars (ispell-get-otherchars))
1193 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1194 (word-regexp (if (string< "" ispell-otherchars)
1195 (concat flyspell-casechars
1196 "+\\("
1197 ispell-otherchars
1199 flyspell-casechars
1200 "+\\)"
1201 (if ispell-many-otherchars-p
1202 "*" "?"))
1203 (concat flyspell-casechars "+")))
1204 did-it-once
1205 start end word)
1206 ;; find the word
1207 (if (not (looking-at flyspell-casechars))
1208 (if following
1209 (re-search-forward flyspell-casechars (point-max) t)
1210 (re-search-backward flyspell-casechars (point-min) t)))
1211 ;; move to front of word
1212 (re-search-backward flyspell-not-casechars (point-min) 'start)
1213 (let ((pos nil))
1214 (if (string< "" ispell-otherchars)
1215 (while (and (looking-at ispell-otherchars)
1216 (not (bobp))
1217 (or (not did-it-once)
1218 ispell-many-otherchars-p)
1219 (not (eq pos (point))))
1220 (setq pos (point))
1221 (setq did-it-once t)
1222 (backward-char 1)
1223 (if (looking-at flyspell-casechars)
1224 (re-search-backward flyspell-not-casechars (point-min) 'move)
1225 (backward-char -1)))))
1226 ;; Now mark the word and save to string.
1227 (if (eq (re-search-forward word-regexp (point-max) t) nil)
1229 (progn
1230 (setq start (match-beginning 0)
1231 end (point)
1232 word (buffer-substring-no-properties start end))
1233 (list word start end)))))
1235 ;*---------------------------------------------------------------------*/
1236 ;* flyspell-small-region ... */
1237 ;*---------------------------------------------------------------------*/
1238 (defun flyspell-small-region (beg end)
1239 "Flyspell text between BEG and END."
1240 (save-excursion
1241 (if (> beg end)
1242 (let ((old beg))
1243 (setq beg end)
1244 (setq end old)))
1245 (goto-char beg)
1246 (let ((count 0))
1247 (while (< (point) end)
1248 (if (and flyspell-issue-message-flag (= count 100))
1249 (progn
1250 (message "Spell Checking...%d%%"
1251 (* 100 (/ (float (- (point) beg)) (- end beg))))
1252 (setq count 0))
1253 (setq count (+ 1 count)))
1254 (flyspell-word)
1255 (sit-for 0)
1256 (let ((cur (point)))
1257 (forward-word 1)
1258 (if (and (< (point) end) (> (point) (+ cur 1)))
1259 (backward-char 1)))))
1260 (backward-char 1)
1261 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1262 (flyspell-word)))
1264 ;*---------------------------------------------------------------------*/
1265 ;* flyspell-external-ispell-process ... */
1266 ;*---------------------------------------------------------------------*/
1267 (defvar flyspell-external-ispell-process '()
1268 "The external Flyspell Ispell process.")
1270 ;*---------------------------------------------------------------------*/
1271 ;* flyspell-external-ispell-buffer ... */
1272 ;*---------------------------------------------------------------------*/
1273 (defvar flyspell-external-ispell-buffer '())
1274 (defvar flyspell-large-region-buffer '())
1275 (defvar flyspell-large-region-beg (point-min))
1276 (defvar flyspell-large-region-end (point-max))
1278 ;*---------------------------------------------------------------------*/
1279 ;* flyspell-external-point-words ... */
1280 ;*---------------------------------------------------------------------*/
1281 (defun flyspell-external-point-words ()
1282 (let ((buffer flyspell-external-ispell-buffer))
1283 (set-buffer buffer)
1284 (beginning-of-buffer)
1285 (let ((size (- flyspell-large-region-end flyspell-large-region-beg))
1286 (start flyspell-large-region-beg))
1287 ;; now we are done with ispell, we have to find the word in
1288 ;; the initial buffer
1289 (while (< (point) (- (point-max) 1))
1290 ;; we have to fetch the incorrect word
1291 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1292 (let ((word (match-string 1)))
1293 (goto-char (match-end 0))
1294 (set-buffer flyspell-large-region-buffer)
1295 (goto-char flyspell-large-region-beg)
1296 (if flyspell-issue-message-flag
1297 (message "Spell Checking...%d%% [%s]"
1298 (* 100 (/ (float (- (point) start)) size))
1299 word))
1300 (if (search-forward word flyspell-large-region-end t)
1301 (progn
1302 (setq flyspell-large-region-beg (point))
1303 (goto-char (- (point) 1))
1304 (flyspell-word)))
1305 (set-buffer buffer))
1306 (goto-char (point-max)))))
1307 ;; we are done
1308 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1309 ;; ok, we are done with pointing out incorrect words, we just
1310 ;; have to kill the temporary buffer
1311 (kill-buffer flyspell-external-ispell-buffer)
1312 (setq flyspell-external-ispell-buffer nil)))
1314 ;*---------------------------------------------------------------------*/
1315 ;* flyspell-large-region ... */
1316 ;*---------------------------------------------------------------------*/
1317 (defun flyspell-large-region (beg end)
1318 (let* ((curbuf (current-buffer))
1319 (buffer (get-buffer-create "*flyspell-region*")))
1320 (setq flyspell-external-ispell-buffer buffer)
1321 (setq flyspell-large-region-buffer curbuf)
1322 (setq flyspell-large-region-beg beg)
1323 (setq flyspell-large-region-end end)
1324 (set-buffer buffer)
1325 (erase-buffer)
1326 ;; this is done, we can start checking...
1327 (if flyspell-issue-message-flag (message "Checking region..."))
1328 (set-buffer curbuf)
1329 (let ((c (apply 'call-process-region beg
1331 ispell-program-name
1333 buffer
1335 "-l"
1336 (let (args)
1337 ;; Local dictionary becomes the global dictionary in use.
1338 (if ispell-local-dictionary
1339 (setq ispell-dictionary ispell-local-dictionary))
1340 (setq args (ispell-get-ispell-args))
1341 (if ispell-dictionary ; use specified dictionary
1342 (setq args
1343 (append (list "-d" ispell-dictionary) args)))
1344 (if ispell-personal-dictionary ; use specified pers dict
1345 (setq args
1346 (append args
1347 (list "-p"
1348 (expand-file-name
1349 ispell-personal-dictionary)))))
1350 (setq args (append args ispell-extra-args))
1351 args))))
1352 (if (= c 0)
1353 (flyspell-external-point-words)
1354 (error "Can't check region...")))))
1356 ;*---------------------------------------------------------------------*/
1357 ;* flyspell-region ... */
1358 ;* ------------------------------------------------------------- */
1359 ;* Because `ispell -a' is too slow, it is not possible to use */
1360 ;* it on large region. Then, when ispell is invoked on a large */
1361 ;* text region, a new `ispell -l' process is spawned. The */
1362 ;* pointed out words are then searched in the region a checked with */
1363 ;* regular flyspell means. */
1364 ;*---------------------------------------------------------------------*/
1365 ;;;###autoload
1366 (defun flyspell-region (beg end)
1367 "Flyspell text between BEG and END."
1368 (interactive "r")
1369 (if (= beg end)
1371 (save-excursion
1372 (if (> beg end)
1373 (let ((old beg))
1374 (setq beg end)
1375 (setq end old)))
1376 (if (> (- end beg) flyspell-large-region)
1377 (flyspell-large-region beg end)
1378 (flyspell-small-region beg end)))))
1380 ;*---------------------------------------------------------------------*/
1381 ;* flyspell-buffer ... */
1382 ;*---------------------------------------------------------------------*/
1383 ;;;###autoload
1384 (defun flyspell-buffer ()
1385 "Flyspell whole buffer."
1386 (interactive)
1387 (flyspell-region (point-min) (point-max)))
1389 ;*---------------------------------------------------------------------*/
1390 ;* old next error position ... */
1391 ;*---------------------------------------------------------------------*/
1392 (defvar flyspell-old-buffer-error nil)
1393 (defvar flyspell-old-pos-error nil)
1395 ;*---------------------------------------------------------------------*/
1396 ;* flyspell-goto-next-error ... */
1397 ;*---------------------------------------------------------------------*/
1398 (defun flyspell-goto-next-error ()
1399 "Go to the next previously detected error.
1400 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1401 FLYSPELL-BUFFER."
1402 (interactive)
1403 (let ((pos (point))
1404 (max (point-max)))
1405 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1406 (eq pos flyspell-old-pos-error))
1407 (progn
1408 (if (= flyspell-old-pos-error max)
1409 ;; goto beginning of buffer
1410 (progn
1411 (message "Restarting from beginning of buffer")
1412 (goto-char (point-min)))
1413 (forward-word 1))
1414 (setq pos (point))))
1415 ;; seek the next error
1416 (while (and (< pos max)
1417 (let ((ovs (overlays-at pos))
1418 (r '()))
1419 (while (and (not r) (consp ovs))
1420 (if (flyspell-overlay-p (car ovs))
1421 (setq r t)
1422 (setq ovs (cdr ovs))))
1423 (not r)))
1424 (setq pos (1+ pos)))
1425 ;; save the current location for next invocation
1426 (setq flyspell-old-pos-error pos)
1427 (setq flyspell-old-buffer-error (current-buffer))
1428 (goto-char pos)
1429 (if (= pos max)
1430 (message "No more miss-spelled word!"))))
1432 ;*---------------------------------------------------------------------*/
1433 ;* flyspell-overlay-p ... */
1434 ;*---------------------------------------------------------------------*/
1435 (defun flyspell-overlay-p (o)
1436 "A predicate that return true iff O is an overlay used by flyspell."
1437 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1439 ;*---------------------------------------------------------------------*/
1440 ;* flyspell-delete-all-overlays ... */
1441 ;* ------------------------------------------------------------- */
1442 ;* Remove all the overlays introduced by flyspell. */
1443 ;*---------------------------------------------------------------------*/
1444 (defun flyspell-delete-all-overlays ()
1445 "Delete all the overlays used by flyspell."
1446 (let ((l (overlays-in (point-min) (point-max))))
1447 (while (consp l)
1448 (progn
1449 (if (flyspell-overlay-p (car l))
1450 (delete-overlay (car l)))
1451 (setq l (cdr l))))))
1453 ;*---------------------------------------------------------------------*/
1454 ;* flyspell-unhighlight-at ... */
1455 ;*---------------------------------------------------------------------*/
1456 (defun flyspell-unhighlight-at (pos)
1457 "Remove the flyspell overlay that are located at POS."
1458 (if flyspell-persistent-highlight
1459 (let ((overlays (overlays-at pos)))
1460 (while (consp overlays)
1461 (if (flyspell-overlay-p (car overlays))
1462 (delete-overlay (car overlays)))
1463 (setq overlays (cdr overlays))))
1464 (if (flyspell-overlay-p flyspell-overlay)
1465 (delete-overlay flyspell-overlay))))
1467 ;*---------------------------------------------------------------------*/
1468 ;* flyspell-properties-at-p ... */
1469 ;* ------------------------------------------------------------- */
1470 ;* Is there an highlight properties at position pos? */
1471 ;*---------------------------------------------------------------------*/
1472 (defun flyspell-properties-at-p (pos)
1473 "Return t if there is a text property at POS, not counting `local-map'.
1474 If variable `flyspell-highlight-properties' is set to nil,
1475 text with properties are not checked. This function is used to discover
1476 if the character at POS has any other property."
1477 (let ((prop (text-properties-at pos))
1478 (keep t))
1479 (while (and keep (consp prop))
1480 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1481 (setq prop (cdr (cdr prop)))
1482 (setq keep nil)))
1483 (consp prop)))
1485 ;*---------------------------------------------------------------------*/
1486 ;* make-flyspell-overlay ... */
1487 ;*---------------------------------------------------------------------*/
1488 (defun make-flyspell-overlay (beg end face mouse-face)
1489 "Allocate an overlay to highlight an incorrect word.
1490 BEG and END specify the range in the buffer of that word.
1491 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1492 for the overlay."
1493 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1494 (overlay-put flyspell-overlay 'face face)
1495 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1496 (overlay-put flyspell-overlay 'flyspell-overlay t)
1497 (overlay-put flyspell-overlay 'evaporate t)
1498 (overlay-put flyspell-overlay 'help-echo "mouse-2: correct word at point")
1499 (if flyspell-use-local-map
1500 (overlay-put flyspell-overlay
1501 flyspell-overlay-keymap-property-name
1502 flyspell-mouse-map))
1503 (when (eq face 'flyspell-incorrect-face)
1504 (and (stringp flyspell-before-incorrect-word-string)
1505 (overlay-put flyspell-overlay 'before-string
1506 flyspell-before-incorrect-word-string))
1507 (and (stringp flyspell-after-incorrect-word-string)
1508 (overlay-put flyspell-overlay 'after-string
1509 flyspell-after-incorrect-word-string)))
1510 flyspell-overlay))
1512 ;*---------------------------------------------------------------------*/
1513 ;* flyspell-highlight-incorrect-region ... */
1514 ;*---------------------------------------------------------------------*/
1515 (defun flyspell-highlight-incorrect-region (beg end poss)
1516 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1517 (unless (run-hook-with-args-until-success
1518 'flyspell-incorrect-hook beg end poss)
1519 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1520 (progn
1521 ;; we cleanup current overlay at the same position
1522 (if (and (not flyspell-persistent-highlight)
1523 (overlayp flyspell-overlay))
1524 (delete-overlay flyspell-overlay)
1525 (let ((overlays (overlays-at beg)))
1526 (while (consp overlays)
1527 (if (flyspell-overlay-p (car overlays))
1528 (delete-overlay (car overlays)))
1529 (setq overlays (cdr overlays)))))
1530 ;; now we can use a new overlay
1531 (setq flyspell-overlay
1532 (make-flyspell-overlay beg end
1533 'flyspell-incorrect-face
1534 'highlight))))))
1536 ;*---------------------------------------------------------------------*/
1537 ;* flyspell-highlight-duplicate-region ... */
1538 ;*---------------------------------------------------------------------*/
1539 (defun flyspell-highlight-duplicate-region (beg end)
1540 "Set up an overlay on a duplicated word, in the buffer from BEG to END."
1541 (if (or flyspell-highlight-properties (not (flyspell-properties-at-p beg)))
1542 (progn
1543 ;; we cleanup current overlay at the same position
1544 (if (and (not flyspell-persistent-highlight)
1545 (overlayp flyspell-overlay))
1546 (delete-overlay flyspell-overlay)
1547 (let ((overlays (overlays-at beg)))
1548 (while (consp overlays)
1549 (if (flyspell-overlay-p (car overlays))
1550 (delete-overlay (car overlays)))
1551 (setq overlays (cdr overlays)))))
1552 ;; now we can use a new overlay
1553 (setq flyspell-overlay
1554 (make-flyspell-overlay beg end
1555 'flyspell-duplicate-face
1556 'highlight)))))
1558 ;*---------------------------------------------------------------------*/
1559 ;* flyspell-auto-correct-cache ... */
1560 ;*---------------------------------------------------------------------*/
1561 (defvar flyspell-auto-correct-pos nil)
1562 (defvar flyspell-auto-correct-region nil)
1563 (defvar flyspell-auto-correct-ring nil)
1564 (defvar flyspell-auto-correct-word nil)
1565 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1566 (make-variable-buffer-local 'flyspell-auto-correct-region)
1567 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1568 (make-variable-buffer-local 'flyspell-auto-correct-word)
1570 ;*---------------------------------------------------------------------*/
1571 ;* flyspell-check-previous-highlighted-word ... */
1572 ;*---------------------------------------------------------------------*/
1573 (defun flyspell-check-previous-highlighted-word (&optional arg)
1574 "Correct the closer misspelled word.
1575 This function scans a mis-spelled word before the cursor. If it finds one
1576 it proposes replacement for that word. With prefix arg, count that many
1577 misspelled words backwards."
1578 (interactive)
1579 (let ((pos1 (point))
1580 (pos (point))
1581 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1582 ov ovs)
1583 (if (catch 'exit
1584 (while (and (setq pos (previous-overlay-change pos))
1585 (not (= pos pos1)))
1586 (setq pos1 pos)
1587 (if (> pos (point-min))
1588 (progn
1589 (setq ovs (overlays-at (1- pos)))
1590 (while (consp ovs)
1591 (setq ov (car ovs))
1592 (setq ovs (cdr ovs))
1593 (if (and (overlay-get ov 'flyspell-overlay)
1594 (= 0 (setq arg (1- arg))))
1595 (throw 'exit t)))))))
1596 (save-excursion
1597 (goto-char pos)
1598 (ispell-word))
1599 (error "No word to correct before point"))))
1601 ;*---------------------------------------------------------------------*/
1602 ;* flyspell-display-next-corrections ... */
1603 ;*---------------------------------------------------------------------*/
1604 (defun flyspell-display-next-corrections (corrections)
1605 (let ((string "Corrections:")
1606 (l corrections)
1607 (pos '()))
1608 (while (< (length string) 80)
1609 (if (equal (car l) flyspell-auto-correct-word)
1610 (setq pos (cons (+ 1 (length string)) pos)))
1611 (setq string (concat string " " (car l)))
1612 (setq l (cdr l)))
1613 (while (consp pos)
1614 (let ((num (car pos)))
1615 (put-text-property num
1616 (+ num (length flyspell-auto-correct-word))
1617 'face
1618 'flyspell-incorrect-face
1619 string))
1620 (setq pos (cdr pos)))
1621 (if (fboundp 'display-message)
1622 (display-message 'no-log string)
1623 (message string))))
1625 ;*---------------------------------------------------------------------*/
1626 ;* flyspell-abbrev-table ... */
1627 ;*---------------------------------------------------------------------*/
1628 (defun flyspell-abbrev-table ()
1629 (if flyspell-use-global-abbrev-table-p
1630 global-abbrev-table
1631 local-abbrev-table))
1633 ;*---------------------------------------------------------------------*/
1634 ;* flyspell-define-abbrev ... */
1635 ;*---------------------------------------------------------------------*/
1636 (defun flyspell-define-abbrev (name expansion)
1637 (let ((table (flyspell-abbrev-table)))
1638 (when table
1639 (define-abbrev table name expansion))))
1641 ;*---------------------------------------------------------------------*/
1642 ;* flyspell-auto-correct-word ... */
1643 ;*---------------------------------------------------------------------*/
1644 (defun flyspell-auto-correct-word ()
1645 "Correct the current word.
1646 This command proposes various successive corrections for the current word."
1647 (interactive)
1648 (let ((pos (point))
1649 (old-max (point-max)))
1650 ;; use the correct dictionary
1651 (flyspell-accept-buffer-local-defs)
1652 (if (and (eq flyspell-auto-correct-pos pos)
1653 (consp flyspell-auto-correct-region))
1654 ;; we have already been using the function at the same location
1655 (let* ((start (car flyspell-auto-correct-region))
1656 (len (cdr flyspell-auto-correct-region)))
1657 (flyspell-unhighlight-at start)
1658 (delete-region start (+ start len))
1659 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1660 (let* ((word (car flyspell-auto-correct-ring))
1661 (len (length word)))
1662 (rplacd flyspell-auto-correct-region len)
1663 (goto-char start)
1664 (if flyspell-abbrev-p
1665 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1666 flyspell-auto-correct-word)
1667 (flyspell-change-abbrev (flyspell-abbrev-table)
1668 flyspell-auto-correct-word
1669 word)
1670 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1671 (funcall flyspell-insert-function word)
1672 (flyspell-word)
1673 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1674 (flyspell-ajust-cursor-point pos (point) old-max)
1675 (setq flyspell-auto-correct-pos (point)))
1676 ;; fetch the word to be checked
1677 (let ((word (flyspell-get-word nil))
1678 start end poss)
1679 ;; destructure return word info list.
1680 (setq start (car (cdr word))
1681 end (car (cdr (cdr word)))
1682 word (car word))
1683 (setq flyspell-auto-correct-word word)
1684 ;; now check spelling of word.
1685 (process-send-string ispell-process "%\n") ;put in verbose mode
1686 (process-send-string ispell-process (concat "^" word "\n"))
1687 ;; wait until ispell has processed word
1688 (while (progn
1689 (accept-process-output ispell-process)
1690 (not (string= "" (car ispell-filter)))))
1691 (setq ispell-filter (cdr ispell-filter))
1692 (if (consp ispell-filter)
1693 (setq poss (ispell-parse-output (car ispell-filter))))
1694 (cond ((or (eq poss t) (stringp poss))
1695 ;; don't correct word
1697 ((null poss)
1698 ;; ispell error
1699 (error "Ispell: error in Ispell process"))
1701 ;; the word is incorrect, we have to propose a replacement
1702 (let ((replacements (if flyspell-sort-corrections
1703 (sort (car (cdr (cdr poss))) 'string<)
1704 (car (cdr (cdr poss))))))
1705 (setq flyspell-auto-correct-region nil)
1706 (if (consp replacements)
1707 (progn
1708 (let ((replace (car replacements)))
1709 (let ((new-word replace))
1710 (if (not (equal new-word (car poss)))
1711 (progn
1712 ;; the save the current replacements
1713 (setq flyspell-auto-correct-region
1714 (cons start (length new-word)))
1715 (let ((l replacements))
1716 (while (consp (cdr l))
1717 (setq l (cdr l)))
1718 (rplacd l (cons (car poss) replacements)))
1719 (setq flyspell-auto-correct-ring
1720 replacements)
1721 (flyspell-unhighlight-at start)
1722 (delete-region start end)
1723 (funcall flyspell-insert-function new-word)
1724 (if flyspell-abbrev-p
1725 (if (flyspell-already-abbrevp
1726 (flyspell-abbrev-table) word)
1727 (flyspell-change-abbrev
1728 (flyspell-abbrev-table)
1729 word
1730 new-word)
1731 (flyspell-define-abbrev word new-word)))
1732 (flyspell-word)
1733 (flyspell-display-next-corrections
1734 (cons new-word flyspell-auto-correct-ring))
1735 (flyspell-ajust-cursor-point pos
1736 (point)
1737 old-max))))))))))
1738 (setq flyspell-auto-correct-pos (point))
1739 (ispell-pdict-save t)))))
1741 ;*---------------------------------------------------------------------*/
1742 ;* flyspell-auto-correct-previous-pos ... */
1743 ;*---------------------------------------------------------------------*/
1744 (defvar flyspell-auto-correct-previous-pos nil
1745 "Holds the start of the first incorrect word before point.")
1747 ;*---------------------------------------------------------------------*/
1748 ;* flyspell-auto-correct-previous-hook ... */
1749 ;*---------------------------------------------------------------------*/
1750 (defun flyspell-auto-correct-previous-hook ()
1751 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1752 Sets flyspell-auto-correct-previous-pos to nil"
1753 (interactive)
1754 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1755 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1756 (setq flyspell-auto-correct-previous-pos nil)))
1758 ;*---------------------------------------------------------------------*/
1759 ;* flyspell-auto-correct-previous-word ... */
1760 ;*---------------------------------------------------------------------*/
1761 (defun flyspell-auto-correct-previous-word (position)
1762 "*Auto correct the first mispelled word that occurs before point."
1763 (interactive "d")
1765 (add-hook 'pre-command-hook
1766 (function flyspell-auto-correct-previous-hook) t t)
1768 (save-excursion
1769 (unless flyspell-auto-correct-previous-pos
1770 ;; only reset if a new overlay exists
1771 (setq flyspell-auto-correct-previous-pos nil)
1773 (let ((overlay-list (overlays-in (point-min) position))
1774 (new-overlay 'dummy-value))
1776 ;; search for previous (new) flyspell overlay
1777 (while (and new-overlay
1778 (or (not (flyspell-overlay-p new-overlay))
1779 ;; check if its face has changed
1780 (not (eq (get-char-property
1781 (overlay-start new-overlay) 'face)
1782 'flyspell-incorrect-face))))
1783 (setq new-overlay (car-safe overlay-list))
1784 (setq overlay-list (cdr-safe overlay-list)))
1786 ;; if nothing new exits new-overlay should be nil
1787 (if new-overlay;; the length of the word may change so go to the start
1788 (setq flyspell-auto-correct-previous-pos
1789 (overlay-start new-overlay)))))
1791 (when flyspell-auto-correct-previous-pos
1792 (save-excursion
1793 (goto-char flyspell-auto-correct-previous-pos)
1794 (let ((ispell-following-word t));; point is at start
1795 (if (numberp flyspell-auto-correct-previous-pos)
1796 (goto-char flyspell-auto-correct-previous-pos))
1797 (flyspell-auto-correct-word))
1798 ;; the point may have moved so reset this
1799 (setq flyspell-auto-correct-previous-pos (point))))))
1801 ;*---------------------------------------------------------------------*/
1802 ;* flyspell-correct-word ... */
1803 ;*---------------------------------------------------------------------*/
1804 (defun flyspell-correct-word (event)
1805 "Pop up a menu of possible corrections for a misspelled word.
1806 The word checked is the word at the mouse position."
1807 (interactive "e")
1808 ;; use the correct dictionary
1809 (flyspell-accept-buffer-local-defs)
1810 ;; retain cursor location (I don't know why but save-excursion here fails).
1811 (let ((save (point)))
1812 (mouse-set-point event)
1813 (let ((cursor-location (point))
1814 (word (flyspell-get-word nil))
1815 start end poss replace)
1816 ;; destructure return word info list.
1817 (setq start (car (cdr word))
1818 end (car (cdr (cdr word)))
1819 word (car word))
1820 ;; now check spelling of word.
1821 (process-send-string ispell-process "%\n") ;put in verbose mode
1822 (process-send-string ispell-process (concat "^" word "\n"))
1823 ;; wait until ispell has processed word
1824 (while (progn
1825 (accept-process-output ispell-process)
1826 (not (string= "" (car ispell-filter)))))
1827 (setq ispell-filter (cdr ispell-filter))
1828 (if (consp ispell-filter)
1829 (setq poss (ispell-parse-output (car ispell-filter))))
1830 (cond ((or (eq poss t) (stringp poss))
1831 ;; don't correct word
1833 ((null poss)
1834 ;; ispell error
1835 (error "Ispell: error in Ispell process"))
1836 ((string-match "GNU" (emacs-version))
1837 ;; the word is incorrect, we have to propose a replacement
1838 (setq replace (flyspell-emacs-popup event poss word))
1839 (cond ((eq replace 'ignore)
1840 (goto-char save)
1841 nil)
1842 ((eq replace 'save)
1843 (goto-char save)
1844 (process-send-string ispell-process (concat "*" word "\n"))
1845 (flyspell-unhighlight-at cursor-location)
1846 (setq ispell-pdict-modified-p '(t)))
1847 ((or (eq replace 'buffer) (eq replace 'session))
1848 (process-send-string ispell-process (concat "@" word "\n"))
1849 (if (null ispell-pdict-modified-p)
1850 (setq ispell-pdict-modified-p
1851 (list ispell-pdict-modified-p)))
1852 (flyspell-unhighlight-at cursor-location)
1853 (goto-char save)
1854 (if (eq replace 'buffer)
1855 (ispell-add-per-file-word-list word)))
1856 (replace
1857 (flyspell-unhighlight-at cursor-location)
1858 (let ((new-word (if (atom replace)
1859 replace
1860 (car replace)))
1861 (cursor-location (+ (- (length word) (- end start))
1862 cursor-location)))
1863 (if (not (equal new-word (car poss)))
1864 (let ((old-max (point-max)))
1865 (delete-region start end)
1866 (funcall flyspell-insert-function new-word)
1867 (if flyspell-abbrev-p
1868 (flyspell-define-abbrev word new-word))
1869 (flyspell-ajust-cursor-point save
1870 cursor-location
1871 old-max)))))
1873 (goto-char save)
1874 nil)))
1875 ((eq flyspell-emacs 'xemacs)
1876 (flyspell-xemacs-popup
1877 event poss word cursor-location start end save)
1878 (goto-char save)))
1879 (ispell-pdict-save t))))
1881 ;*---------------------------------------------------------------------*/
1882 ;* flyspell-xemacs-correct ... */
1883 ;*---------------------------------------------------------------------*/
1884 (defun flyspell-xemacs-correct (replace poss word cursor-location start end save)
1885 "The xemacs popup menu callback."
1886 (cond ((eq replace 'ignore)
1887 nil)
1888 ((eq replace 'save)
1889 (process-send-string ispell-process (concat "*" word "\n"))
1890 (process-send-string ispell-process "#\n")
1891 (flyspell-unhighlight-at cursor-location)
1892 (setq ispell-pdict-modified-p '(t)))
1893 ((or (eq replace 'buffer) (eq replace 'session))
1894 (process-send-string ispell-process (concat "@" word "\n"))
1895 (flyspell-unhighlight-at cursor-location)
1896 (if (null ispell-pdict-modified-p)
1897 (setq ispell-pdict-modified-p
1898 (list ispell-pdict-modified-p)))
1899 (if (eq replace 'buffer)
1900 (ispell-add-per-file-word-list word)))
1901 (replace
1902 (let ((old-max (point-max))
1903 (new-word (if (atom replace)
1904 replace
1905 (car replace)))
1906 (cursor-location (+ (- (length word) (- end start))
1907 cursor-location)))
1908 (if (not (equal new-word (car poss)))
1909 (progn
1910 (delete-region start end)
1911 (goto-char start)
1912 (funcall flyspell-insert-function new-word)
1913 (if flyspell-abbrev-p
1914 (flyspell-define-abbrev word new-word))))
1915 (flyspell-ajust-cursor-point save cursor-location old-max)))))
1917 ;*---------------------------------------------------------------------*/
1918 ;* flyspell-ajust-cursor-point ... */
1919 ;*---------------------------------------------------------------------*/
1920 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
1921 (if (>= save cursor-location)
1922 (let ((new-pos (+ save (- (point-max) old-max))))
1923 (goto-char (cond
1924 ((< new-pos (point-min))
1925 (point-min))
1926 ((> new-pos (point-max))
1927 (point-max))
1928 (t new-pos))))
1929 (goto-char save)))
1931 ;*---------------------------------------------------------------------*/
1932 ;* flyspell-emacs-popup ... */
1933 ;*---------------------------------------------------------------------*/
1934 (defun flyspell-emacs-popup (event poss word)
1935 "The Emacs popup menu."
1936 (if (not event)
1937 (let* ((mouse-pos (mouse-position))
1938 (mouse-pos (if (nth 1 mouse-pos)
1939 mouse-pos
1940 (set-mouse-position (car mouse-pos)
1941 (/ (frame-width) 2) 2)
1942 (unfocus-frame)
1943 (mouse-position))))
1944 (setq event (list (list (car (cdr mouse-pos))
1945 (1+ (cdr (cdr mouse-pos))))
1946 (car mouse-pos)))))
1947 (let* ((corrects (if flyspell-sort-corrections
1948 (sort (car (cdr (cdr poss))) 'string<)
1949 (car (cdr (cdr poss)))))
1950 (cor-menu (if (consp corrects)
1951 (mapcar (lambda (correct)
1952 (list correct correct))
1953 corrects)
1954 '()))
1955 (affix (car (cdr (cdr (cdr poss)))))
1956 (base-menu (let ((save (if (consp affix)
1957 (list
1958 (list (concat "Save affix: " (car affix))
1959 'save)
1960 '("Accept (session)" session)
1961 '("Accept (buffer)" buffer))
1962 '(("Save word" save)
1963 ("Accept (session)" session)
1964 ("Accept (buffer)" buffer)))))
1965 (if (consp cor-menu)
1966 (append cor-menu (cons "" save))
1967 save)))
1968 (menu (cons "flyspell correction menu" base-menu)))
1969 (car (x-popup-menu event
1970 (list (format "%s [%s]" word (or ispell-local-dictionary
1971 ispell-dictionary))
1972 menu)))))
1974 ;*---------------------------------------------------------------------*/
1975 ;* flyspell-xemacs-popup ... */
1976 ;*---------------------------------------------------------------------*/
1977 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
1978 "The XEmacs popup menu."
1979 (let* ((corrects (if flyspell-sort-corrections
1980 (sort (car (cdr (cdr poss))) 'string<)
1981 (car (cdr (cdr poss)))))
1982 (cor-menu (if (consp corrects)
1983 (mapcar (lambda (correct)
1984 (vector correct
1985 (list 'flyspell-xemacs-correct
1986 correct
1987 (list 'quote poss)
1988 word
1989 cursor-location
1990 start
1992 save)
1994 corrects)
1995 '()))
1996 (affix (car (cdr (cdr (cdr poss)))))
1997 (menu (let ((save (if (consp affix)
1998 (vector
1999 (concat "Save affix: " (car affix))
2000 (list 'flyspell-xemacs-correct
2001 ''save
2002 (list 'quote poss)
2003 word
2004 cursor-location
2005 start
2007 save)
2009 (vector
2010 "Save word"
2011 (list 'flyspell-xemacs-correct
2012 ''save
2013 (list 'quote poss)
2014 word
2015 cursor-location
2016 start
2018 save)
2019 t)))
2020 (session (vector "Accept (session)"
2021 (list 'flyspell-xemacs-correct
2022 ''session
2023 (list 'quote poss)
2024 word
2025 cursor-location
2026 start
2028 save)
2030 (buffer (vector "Accept (buffer)"
2031 (list 'flyspell-xemacs-correct
2032 ''buffer
2033 (list 'quote poss)
2034 word
2035 cursor-location
2036 start
2038 save)
2039 t)))
2040 (if (consp cor-menu)
2041 (append cor-menu (list "-" save session buffer))
2042 (list save session buffer)))))
2043 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2044 ispell-dictionary))
2045 menu))))
2047 ;*---------------------------------------------------------------------*/
2048 ;* Some example functions for real autocorrecting */
2049 ;*---------------------------------------------------------------------*/
2050 (defun flyspell-maybe-correct-transposition (beg end poss)
2051 "Check replacements for transposed characters.
2053 If the text between BEG and END is equal to a correction suggested by
2054 Ispell, after transposing two adjacent characters, correct the text,
2055 and return t.
2057 The third arg POSS is either the symbol 'doublon' or a list of
2058 possible corrections as returned by 'ispell-parse-output'.
2060 This function is meant to be added to 'flyspell-incorrect-hook'."
2061 (when (consp poss)
2062 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
2063 found)
2064 (save-excursion
2065 (copy-to-buffer temp-buffer beg end)
2066 (set-buffer temp-buffer)
2067 (goto-char (1+ (point-min)))
2068 (while (and (not (eobp)) (not found))
2069 (transpose-chars 1)
2070 (if (member (buffer-string) (nth 2 poss))
2071 (setq found (point))
2072 (transpose-chars -1)
2073 (forward-char))))
2074 (when found
2075 (save-excursion
2076 (goto-char (+ beg found -1))
2077 (transpose-chars -1)
2078 t)))))
2080 (defun flyspell-maybe-correct-doubling (beg end poss)
2081 "Check replacements for doubled characters.
2083 If the text between BEG and END is equal to a correction suggested by
2084 Ispell, after removing a pair of doubled characters, correct the text,
2085 and return t.
2087 The third arg POSS is either the symbol 'doublon' or a list of
2088 possible corrections as returned by 'ispell-parse-output'.
2090 This function is meant to be added to 'flyspell-incorrect-hook'."
2091 (when (consp poss)
2092 (let ((temp-buffer (get-buffer-create " *flyspell-temp*"))
2093 found)
2094 (save-excursion
2095 (copy-to-buffer temp-buffer beg end)
2096 (set-buffer temp-buffer)
2097 (goto-char (1+ (point-min)))
2098 (while (and (not (eobp)) (not found))
2099 (when (char-equal (char-after) (char-before))
2100 (delete-char 1)
2101 (if (member (buffer-string) (nth 2 poss))
2102 (setq found (point))
2103 (insert-char (char-before) 1)))
2104 (forward-char)))
2105 (when found
2106 (save-excursion
2107 (goto-char (+ beg found -1))
2108 (delete-char 1)
2109 t)))))
2111 ;*---------------------------------------------------------------------*/
2112 ;* flyspell-already-abbrevp ... */
2113 ;*---------------------------------------------------------------------*/
2114 (defun flyspell-already-abbrevp (table word)
2115 (let ((sym (abbrev-symbol word table)))
2116 (and sym (symbolp sym))))
2118 ;*---------------------------------------------------------------------*/
2119 ;* flyspell-change-abbrev ... */
2120 ;*---------------------------------------------------------------------*/
2121 (defun flyspell-change-abbrev (table old new)
2122 (set (abbrev-symbol old table) new))
2124 (provide 'flyspell)
2126 ;;; flyspell.el ends here