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