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