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