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