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