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