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