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