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