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