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