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