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