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