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