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