(flyspell-duplicate, flyspell-incorrect): Revert 2006-01-27 change.
[emacs.git] / lisp / textmodes / flyspell.el
blob339160db9bdb3b0e549d70db34f337702e8f6087
1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 ;;* User configuration ... */
61 ;;*---------------------------------------------------------------------*/
62 (defcustom flyspell-highlight-flag t
63 "How Flyspell should indicate misspelled words.
64 Non-nil means use highlight, nil means use minibuffer messages."
65 :group 'flyspell
66 :type 'boolean)
68 (defcustom flyspell-mark-duplications-flag t
69 "Non-nil means Flyspell reports a repeated word as an error.
70 Detection of repeated words is not implemented in
71 \"large\" regions; see `flyspell-large-region'."
72 :group 'flyspell
73 :type 'boolean)
75 (defcustom flyspell-sort-corrections nil
76 "Non-nil means, sort the corrections alphabetically before popping them."
77 :group 'flyspell
78 :version "21.1"
79 :type 'boolean)
81 (defcustom flyspell-duplicate-distance -1
82 "The maximum distance for finding duplicates of unrecognized words.
83 This applies to the feature that when a word is not found in the dictionary,
84 if the same spelling occurs elsewhere in the buffer,
85 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
86 This variable specifies how far to search to find such a duplicate.
87 -1 means no limit (search the whole buffer).
88 0 means do not search for duplicate unrecognized spellings."
89 :group 'flyspell
90 :version "21.1"
91 :type 'number)
93 (defcustom flyspell-delay 3
94 "The number of seconds to wait before checking, after a \"delayed\" command."
95 :group 'flyspell
96 :type 'number)
98 (defcustom flyspell-persistent-highlight t
99 "Non-nil means misspelled words remain highlighted until corrected.
100 If this variable is nil, only the most recently detected misspelled word
101 is highlighted."
102 :group 'flyspell
103 :type 'boolean)
105 (defcustom flyspell-highlight-properties t
106 "Non-nil means highlight incorrect words even if a property exists for this word."
107 :group 'flyspell
108 :type 'boolean)
110 (defcustom flyspell-default-delayed-commands
111 '(self-insert-command
112 delete-backward-char
113 backward-or-forward-delete-char
114 delete-char
115 scrollbar-vertical-drag
116 backward-delete-char-untabify)
117 "The standard list of delayed commands for Flyspell.
118 See `flyspell-delayed-commands'."
119 :group 'flyspell
120 :version "21.1"
121 :type '(repeat (symbol)))
123 (defcustom flyspell-delayed-commands nil
124 "List of commands that are \"delayed\" for Flyspell mode.
125 After these commands, Flyspell checking is delayed for a short time,
126 whose length is specified by `flyspell-delay'."
127 :group 'flyspell
128 :type '(repeat (symbol)))
130 (defcustom flyspell-default-deplacement-commands
131 '(next-line
132 previous-line
133 scroll-up
134 scroll-down)
135 "The standard list of deplacement commands for Flyspell.
136 See `flyspell-deplacement-commands'."
137 :group 'flyspell
138 :version "21.1"
139 :type '(repeat (symbol)))
141 (defcustom flyspell-deplacement-commands nil
142 "List of commands that are \"deplacement\" for Flyspell mode.
143 After these commands, Flyspell checking is performed only if the previous
144 command was not the very same command."
145 :group 'flyspell
146 :version "21.1"
147 :type '(repeat (symbol)))
149 (defcustom flyspell-issue-welcome-flag t
150 "Non-nil means that Flyspell should display a welcome message when started."
151 :group 'flyspell
152 :type 'boolean)
154 (defcustom flyspell-issue-message-flag t
155 "Non-nil means that Flyspell emits messages when checking words."
156 :group 'flyspell
157 :type 'boolean)
159 (defcustom flyspell-incorrect-hook nil
160 "List of functions to be called when incorrect words are encountered.
161 Each function is given three arguments. The first two
162 arguments are the beginning and the end of the incorrect region.
163 The third is either the symbol `doublon' or the list
164 of possible corrections as returned by `ispell-parse-output'.
166 If any of the functions return non-nil, the word is not highlighted as
167 incorrect."
168 :group 'flyspell
169 :version "21.1"
170 :type 'hook)
172 (defcustom flyspell-default-dictionary nil
173 "A string that is the name of the default dictionary.
174 This is passed to the `ispell-change-dictionary' when flyspell is started.
175 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
176 when flyspell is started, the value of that variable is used instead
177 of `flyspell-default-dictionary' to select the default dictionary.
178 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
179 Ispell's ultimate default dictionary."
180 :group 'flyspell
181 :version "21.1"
182 :type '(choice string (const :tag "Default" nil)))
184 (defcustom flyspell-tex-command-regexp
185 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
186 "A string that is the regular expression that matches TeX commands."
187 :group 'flyspell
188 :version "21.1"
189 :type 'string)
191 (defcustom flyspell-check-tex-math-command nil
192 "Non-nil means check even inside TeX math environment.
193 TeX math environments are discovered by the TEXMATHP that implemented
194 inside the texmathp.el Emacs package. That package may be found at:
195 http://strw.leidenuniv.nl/~dominik/Tools"
196 :group 'flyspell
197 :type 'boolean)
199 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
200 '("francais" "deutsch8" "norsk")
201 "List of dictionary names that consider `-' as word delimiter."
202 :group 'flyspell
203 :version "21.1"
204 :type '(repeat (string)))
206 (defcustom flyspell-abbrev-p
208 "If non-nil, add correction to abbreviation table."
209 :group 'flyspell
210 :version "21.1"
211 :type 'boolean)
213 (defcustom flyspell-use-global-abbrev-table-p
215 "If non-nil, prefer global abbrev table to local abbrev table."
216 :group 'flyspell
217 :version "21.1"
218 :type 'boolean)
220 (defcustom flyspell-mode-line-string " Fly"
221 "String displayed on the modeline when flyspell is active.
222 Set this to nil if you don't want a modeline indicator."
223 :group 'flyspell
224 :type '(choice string (const :tag "None" nil)))
226 (defcustom flyspell-large-region 1000
227 "The threshold that determines if a region is small.
228 If the region is smaller than this number of characters,
229 `flyspell-region' checks the words sequentially using regular
230 flyspell methods. Else, if the region is large, a new Ispell process is
231 spawned for speed.
233 Doubled words are not detected in a large region, because Ispell
234 does not check for them.
236 If `flyspell-large-region' is nil, all regions are treated as small."
237 :group 'flyspell
238 :version "21.1"
239 :type '(choice number (const :tag "All small" nil)))
241 (defcustom flyspell-insert-function (function insert)
242 "Function for inserting word by flyspell upon correction."
243 :group 'flyspell
244 :type 'function)
246 (defcustom flyspell-before-incorrect-word-string nil
247 "String used to indicate an incorrect word starting."
248 :group 'flyspell
249 :type '(choice string (const nil)))
251 (defcustom flyspell-after-incorrect-word-string nil
252 "String used to indicate an incorrect word ending."
253 :group 'flyspell
254 :type '(choice string (const nil)))
256 (defcustom flyspell-use-meta-tab t
257 "Non-nil means that flyspell uses M-TAB to correct word."
258 :group 'flyspell
259 :type 'boolean)
261 (defcustom flyspell-auto-correct-binding
262 [(control ?\;)]
263 "The key binding for flyspell auto correction."
264 :group 'flyspell)
266 ;;*---------------------------------------------------------------------*/
267 ;;* Mode specific options */
268 ;;* ------------------------------------------------------------- */
269 ;;* Mode specific options enable users to disable flyspell on */
270 ;;* certain word depending of the emacs mode. For instance, when */
271 ;;* using flyspell with mail-mode add the following expression */
272 ;;* in your .emacs file: */
273 ;;* (add-hook 'mail-mode */
274 ;;* '(lambda () (setq flyspell-generic-check-word-predicate */
275 ;;* 'mail-mode-flyspell-verify))) */
276 ;;*---------------------------------------------------------------------*/
277 (defvar flyspell-generic-check-word-predicate nil
278 "Function providing per-mode customization over which words are flyspelled.
279 Returns t to continue checking, nil otherwise.
280 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
281 property of the major mode name.")
282 (make-variable-buffer-local 'flyspell-generic-check-word-predicate)
283 (defvaralias 'flyspell-generic-check-word-p
284 'flyspell-generic-check-word-predicate)
286 ;;*--- mail mode -------------------------------------------------------*/
287 (put 'mail-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
288 (put 'message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
289 (defun mail-mode-flyspell-verify ()
290 "Function used for `flyspell-generic-check-word-predicate' in Mail mode."
291 (let ((header-end (save-excursion
292 (goto-char (point-min))
293 (re-search-forward
294 (concat "^"
295 (regexp-quote mail-header-separator)
296 "$")
297 nil t)
298 (point)))
299 (signature-begin (save-excursion
300 (goto-char (point-max))
301 (re-search-backward message-signature-separator
302 nil t)
303 (point))))
304 (cond ((< (point) header-end)
305 (and (save-excursion (beginning-of-line)
306 (looking-at "^Subject:"))
307 (> (point) (match-end 0))))
308 ((> (point) signature-begin)
309 nil)
311 (save-excursion
312 (beginning-of-line)
313 (not (looking-at "[>}|]\\|To:")))))))
315 ;;*--- texinfo mode ----------------------------------------------------*/
316 (put 'texinfo-mode 'flyspell-mode-predicate 'texinfo-mode-flyspell-verify)
317 (defun texinfo-mode-flyspell-verify ()
318 "Function used for `flyspell-generic-check-word-predicate' in Texinfo mode."
319 (save-excursion
320 (forward-word -1)
321 (not (looking-at "@"))))
323 ;;*--- tex mode --------------------------------------------------------*/
324 (put 'tex-mode 'flyspell-mode-predicate 'tex-mode-flyspell-verify)
325 (defun tex-mode-flyspell-verify ()
326 "Function used for `flyspell-generic-check-word-predicate' in LaTeX mode."
327 (and
328 (not (save-excursion
329 (re-search-backward "^[ \t]*%%%[ \t]+Local" nil t)))
330 (not (save-excursion
331 (let ((this (point-marker))
332 (e (progn (end-of-line) (point-marker))))
333 (beginning-of-line)
334 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t)
335 (and (>= this (match-beginning 0))
336 (<= this (match-end 0)) )))))))
338 ;;*--- sgml mode -------------------------------------------------------*/
339 (put 'sgml-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
340 (put 'html-mode 'flyspell-mode-predicate 'sgml-mode-flyspell-verify)
342 (defun sgml-mode-flyspell-verify ()
343 "Function used for `flyspell-generic-check-word-predicate' in SGML mode."
344 (not (save-excursion
345 (let ((this (point-marker))
346 (s (progn (beginning-of-line) (point-marker)))
347 (e (progn (end-of-line) (point-marker))))
348 (or (progn
349 (goto-char this)
350 (and (re-search-forward "[^<]*>" e t)
351 (= (match-beginning 0) this)))
352 (progn
353 (goto-char this)
354 (and (re-search-backward "<[^>]*" s t)
355 (= (match-end 0) this)))
356 (and (progn
357 (goto-char this)
358 (and (re-search-forward "[^&]*;" e t)
359 (= (match-beginning 0) this)))
360 (progn
361 (goto-char this)
362 (and (re-search-backward "&[^;]*" s t)
363 (= (match-end 0) this)))))))))
365 ;;*---------------------------------------------------------------------*/
366 ;;* Programming mode */
367 ;;*---------------------------------------------------------------------*/
368 (defvar flyspell-prog-text-faces
369 '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
370 "Faces corresponding to text in programming-mode buffers.")
372 (defun flyspell-generic-progmode-verify ()
373 "Used for `flyspell-generic-check-word-predicate' in programming modes."
374 (let ((f (get-text-property (point) 'face)))
375 (memq f flyspell-prog-text-faces)))
377 ;;;###autoload
378 (defun flyspell-prog-mode ()
379 "Turn on `flyspell-mode' for comments and strings."
380 (interactive)
381 (setq flyspell-generic-check-word-predicate
382 'flyspell-generic-progmode-verify)
383 (flyspell-mode 1)
384 (run-hooks 'flyspell-prog-mode-hook))
386 ;;*---------------------------------------------------------------------*/
387 ;;* Overlay compatibility */
388 ;;*---------------------------------------------------------------------*/
389 (autoload 'make-overlay "overlay" "Overlay compatibility kit." t)
390 (autoload 'overlayp "overlay" "Overlay compatibility kit." t)
391 (autoload 'overlays-in "overlay" "Overlay compatibility kit." t)
392 (autoload 'delete-overlay "overlay" "Overlay compatibility kit." t)
393 (autoload 'overlays-at "overlay" "Overlay compatibility kit." t)
394 (autoload 'overlay-put "overlay" "Overlay compatibility kit." t)
395 (autoload 'overlay-get "overlay" "Overlay compatibility kit." t)
396 (autoload 'previous-overlay-change "overlay" "Overlay compatibility kit." t)
398 ;;*---------------------------------------------------------------------*/
399 ;;* The minor mode declaration. */
400 ;;*---------------------------------------------------------------------*/
401 (defvar flyspell-mouse-map
402 (let ((map (make-sparse-keymap)))
403 (define-key map (if (featurep 'xemacs) [button2] [down-mouse-2])
404 #'flyspell-correct-word)
405 map)
406 "Keymap for Flyspell to put on erroneous words.")
408 (defvar flyspell-mode-map
409 (let ((map (make-sparse-keymap)))
410 (if flyspell-use-meta-tab
411 (define-key map "\M-\t" 'flyspell-auto-correct-word))
412 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
413 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
414 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
415 (define-key map [?\C-c ?$] 'flyspell-correct-word-before-point)
416 map)
417 "Minor mode keymap for Flyspell mode--for the whole buffer.")
419 ;; dash character machinery
420 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
421 "*Non-nil means that the `-' char is considered as a word delimiter.")
422 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
423 (defvar flyspell-dash-dictionary nil)
424 (make-variable-buffer-local 'flyspell-dash-dictionary)
425 (defvar flyspell-dash-local-dictionary nil)
426 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
428 ;;*---------------------------------------------------------------------*/
429 ;;* Highlighting */
430 ;;*---------------------------------------------------------------------*/
431 (defface flyspell-incorrect
432 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
433 (t (:bold t)))
434 "Face used for marking a misspelled word in Flyspell."
435 :group 'flyspell)
436 ;; backward-compatibility alias
437 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
439 (defface flyspell-duplicate
440 '((((class color)) (:foreground "Gold3" :bold t :underline t))
441 (t (:bold t)))
442 "Face used for marking a misspelled word that appears twice in the buffer.
443 See also `flyspell-duplicate-distance'."
444 :group 'flyspell)
445 ;; backward-compatibility alias
446 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
448 (defvar flyspell-overlay nil)
450 ;;*---------------------------------------------------------------------*/
451 ;;* flyspell-mode ... */
452 ;;*---------------------------------------------------------------------*/
453 ;;;###autoload(defvar flyspell-mode nil)
454 ;;;###autoload
455 (define-minor-mode flyspell-mode
456 "Minor mode performing on-the-fly spelling checking.
457 This spawns a single Ispell process and checks each word.
458 The default flyspell behavior is to highlight incorrect words.
459 With no argument, this command toggles Flyspell mode.
460 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
462 Bindings:
463 \\[ispell-word]: correct words (using Ispell).
464 \\[flyspell-auto-correct-word]: automatically correct word.
465 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
466 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
468 Hooks:
469 This runs `flyspell-mode-hook' after flyspell is entered.
471 Remark:
472 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
473 valid. For instance, a personal dictionary can be used by
474 invoking `ispell-change-dictionary'.
476 Consider using the `ispell-parser' to check your text. For instance
477 consider adding:
478 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
479 in your .emacs file.
481 \\[flyspell-region] checks all words inside a region.
482 \\[flyspell-buffer] checks the whole buffer."
483 :lighter flyspell-mode-line-string
484 :keymap flyspell-mode-map
485 :group 'flyspell
486 (if flyspell-mode
487 (flyspell-mode-on)
488 (flyspell-mode-off)))
490 ;;;###autoload
491 (defun turn-on-flyspell ()
492 "Unconditionally turn on Flyspell mode."
493 (flyspell-mode 1))
495 ;;;###autoload
496 (defun turn-off-flyspell ()
497 "Unconditionally turn off Flyspell mode."
498 (flyspell-mode -1))
500 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
502 ;;*---------------------------------------------------------------------*/
503 ;;* flyspell-buffers ... */
504 ;;* ------------------------------------------------------------- */
505 ;;* For remembering buffers running flyspell */
506 ;;*---------------------------------------------------------------------*/
507 (defvar flyspell-buffers nil)
509 ;;*---------------------------------------------------------------------*/
510 ;;* flyspell-minibuffer-p ... */
511 ;;*---------------------------------------------------------------------*/
512 (defun flyspell-minibuffer-p (buffer)
513 "Is BUFFER a minibuffer?"
514 (let ((ws (get-buffer-window-list buffer t)))
515 (and (consp ws) (window-minibuffer-p (car ws)))))
517 ;;*---------------------------------------------------------------------*/
518 ;;* flyspell-accept-buffer-local-defs ... */
519 ;;*---------------------------------------------------------------------*/
520 (defvar flyspell-last-buffer nil
521 "The buffer in which the last flyspell operation took place.")
523 (defun flyspell-accept-buffer-local-defs (&optional force)
524 ;; When flyspell-word is used inside a loop (e.g. when processing
525 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
526 ;; up dwarfing everything else, so only do it when the buffer has changed.
527 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
528 (setq flyspell-last-buffer (current-buffer))
529 ;; Strange problem: If buffer in current window has font-lock turned on,
530 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
531 ;; call will reset the buffer to the buffer in the current window.
532 ;; However, it only happens at startup (fix by Albert L. Ting).
533 (save-current-buffer
534 (ispell-accept-buffer-local-defs))
535 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
536 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
537 ;; The dictionary has changed
538 (setq flyspell-dash-dictionary ispell-dictionary)
539 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
540 (setq flyspell-consider-dash-as-word-delimiter-flag
541 (member (or ispell-local-dictionary ispell-dictionary)
542 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
544 (defun flyspell-hack-local-variables-hook ()
545 ;; When local variables are loaded, see if the dictionary context
546 ;; has changed.
547 (flyspell-accept-buffer-local-defs 'force))
549 (defun flyspell-kill-ispell-hook ()
550 (setq flyspell-last-buffer nil)
551 (dolist (buf (buffer-list))
552 (with-current-buffer buf
553 (kill-local-variable 'flyspell-word-cache-word))))
555 ;; Make sure we flush our caches when needed. Do it here rather than in
556 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
557 ;; on flyspell-mode.
558 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
560 ;;*---------------------------------------------------------------------*/
561 ;;* flyspell-mode-on ... */
562 ;;*---------------------------------------------------------------------*/
563 (defun flyspell-mode-on ()
564 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
565 (ispell-maybe-find-aspell-dictionaries)
566 (setq ispell-highlight-face 'flyspell-incorrect)
567 ;; local dictionaries setup
568 (or ispell-local-dictionary ispell-dictionary
569 (if flyspell-default-dictionary
570 (ispell-change-dictionary flyspell-default-dictionary)))
571 ;; we have to force ispell to accept the local definition or
572 ;; otherwise it could be too late, the local dictionary may
573 ;; be forgotten!
574 ;; Pass the `force' argument for the case where flyspell was active already
575 ;; but the buffer's local-defs have been edited.
576 (flyspell-accept-buffer-local-defs 'force)
577 ;; we put the `flyspell-delayed' property on some commands
578 (flyspell-delay-commands)
579 ;; we put the `flyspell-deplacement' property on some commands
580 (flyspell-deplacement-commands)
581 ;; we bound flyspell action to post-command hook
582 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
583 ;; we bound flyspell action to pre-command hook
584 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
585 ;; we bound flyspell action to after-change hook
586 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
587 ;; we bound flyspell action to hack-local-variables-hook
588 (add-hook 'hack-local-variables-hook
589 (function flyspell-hack-local-variables-hook) t t)
590 ;; set flyspell-generic-check-word-predicate based on the major mode
591 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
592 (if mode-predicate
593 (setq flyspell-generic-check-word-predicate mode-predicate)))
594 ;; the welcome message
595 (if (and flyspell-issue-message-flag
596 flyspell-issue-welcome-flag
597 (interactive-p))
598 (let ((binding (where-is-internal 'flyspell-auto-correct-word
599 nil 'non-ascii)))
600 (message "%s"
601 (if binding
602 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
603 (key-description binding))
604 "Welcome to flyspell. Use Mouse-2 to correct words."))))
605 ;; we end with the flyspell hooks
606 (run-hooks 'flyspell-mode-hook))
608 ;;*---------------------------------------------------------------------*/
609 ;;* flyspell-delay-commands ... */
610 ;;*---------------------------------------------------------------------*/
611 (defun flyspell-delay-commands ()
612 "Install the standard set of Flyspell delayed commands."
613 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
614 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
616 ;;*---------------------------------------------------------------------*/
617 ;;* flyspell-delay-command ... */
618 ;;*---------------------------------------------------------------------*/
619 (defun flyspell-delay-command (command)
620 "Set COMMAND to be delayed, for Flyspell.
621 When flyspell `post-command-hook' is invoked because a delayed command
622 as been used the current word is not immediately checked.
623 It will be checked only after `flyspell-delay' seconds."
624 (interactive "SDelay Flyspell after Command: ")
625 (put command 'flyspell-delayed t))
627 ;;*---------------------------------------------------------------------*/
628 ;;* flyspell-deplacement-commands ... */
629 ;;*---------------------------------------------------------------------*/
630 (defun flyspell-deplacement-commands ()
631 "Install the standard set of Flyspell deplacement commands."
632 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
633 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
635 ;;*---------------------------------------------------------------------*/
636 ;;* flyspell-deplacement-command ... */
637 ;;*---------------------------------------------------------------------*/
638 (defun flyspell-deplacement-command (command)
639 "Set COMMAND that implement cursor movements, for Flyspell.
640 When flyspell `post-command-hook' is invoked because of a deplacement command
641 as been used the current word is checked only if the previous command was
642 not the very same deplacement command."
643 (interactive "SDeplacement Flyspell after Command: ")
644 (put command 'flyspell-deplacement t))
646 ;;*---------------------------------------------------------------------*/
647 ;;* flyspell-word-cache ... */
648 ;;*---------------------------------------------------------------------*/
649 (defvar flyspell-word-cache-start nil)
650 (defvar flyspell-word-cache-end nil)
651 (defvar flyspell-word-cache-word nil)
652 (defvar flyspell-word-cache-result '_)
653 (make-variable-buffer-local 'flyspell-word-cache-start)
654 (make-variable-buffer-local 'flyspell-word-cache-end)
655 (make-variable-buffer-local 'flyspell-word-cache-word)
656 (make-variable-buffer-local 'flyspell-word-cache-result)
658 ;;*---------------------------------------------------------------------*/
659 ;;* The flyspell pre-hook, store the current position. In the */
660 ;;* post command hook, we will check, if the word at this position */
661 ;;* has to be spell checked. */
662 ;;*---------------------------------------------------------------------*/
663 (defvar flyspell-pre-buffer nil)
664 (defvar flyspell-pre-point nil)
665 (defvar flyspell-pre-column nil)
666 (defvar flyspell-pre-pre-buffer nil)
667 (defvar flyspell-pre-pre-point nil)
669 ;;*---------------------------------------------------------------------*/
670 ;;* flyspell-previous-command ... */
671 ;;*---------------------------------------------------------------------*/
672 (defvar flyspell-previous-command nil
673 "The last interactive command checked by Flyspell.")
675 ;;*---------------------------------------------------------------------*/
676 ;;* flyspell-pre-command-hook ... */
677 ;;*---------------------------------------------------------------------*/
678 (defun flyspell-pre-command-hook ()
679 "Save the current buffer and point for Flyspell's post-command hook."
680 (interactive)
681 (setq flyspell-pre-buffer (current-buffer))
682 (setq flyspell-pre-point (point))
683 (setq flyspell-pre-column (current-column)))
685 ;;*---------------------------------------------------------------------*/
686 ;;* flyspell-mode-off ... */
687 ;;*---------------------------------------------------------------------*/
688 ;;;###autoload
689 (defun flyspell-mode-off ()
690 "Turn Flyspell mode off."
691 ;; we remove the hooks
692 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
693 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
694 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
695 (remove-hook 'hack-local-variables-hook
696 (function flyspell-hack-local-variables-hook) t)
697 ;; we remove all the flyspell hilightings
698 (flyspell-delete-all-overlays)
699 ;; we have to erase pre cache variables
700 (setq flyspell-pre-buffer nil)
701 (setq flyspell-pre-point nil)
702 ;; we mark the mode as killed
703 (setq flyspell-mode nil))
705 ;;*---------------------------------------------------------------------*/
706 ;;* flyspell-check-pre-word-p ... */
707 ;;*---------------------------------------------------------------------*/
708 (defun flyspell-check-pre-word-p ()
709 "Return non-nil if we should check the word before point.
710 More precisely, it applies to the word that was before point
711 before the current command."
712 (cond
713 ((or (not (numberp flyspell-pre-point))
714 (not (bufferp flyspell-pre-buffer))
715 (not (buffer-live-p flyspell-pre-buffer)))
716 nil)
717 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
718 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
719 nil)
720 ((or (and (= flyspell-pre-point (- (point) 1))
721 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
722 (= flyspell-pre-point (point))
723 (= flyspell-pre-point (+ (point) 1)))
724 nil)
725 ((and (symbolp this-command)
726 (not executing-kbd-macro)
727 (or (get this-command 'flyspell-delayed)
728 (and (get this-command 'flyspell-deplacement)
729 (eq flyspell-previous-command this-command)))
730 (or (= (current-column) 0)
731 (= (current-column) flyspell-pre-column)
732 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
733 nil)
734 ((not (eq (current-buffer) flyspell-pre-buffer))
736 ((not (and (numberp flyspell-word-cache-start)
737 (numberp flyspell-word-cache-end)))
740 (or (< flyspell-pre-point flyspell-word-cache-start)
741 (> flyspell-pre-point flyspell-word-cache-end)))))
743 ;;*---------------------------------------------------------------------*/
744 ;;* The flyspell after-change-hook, store the change position. In */
745 ;;* the post command hook, we will check, if the word at this */
746 ;;* position has to be spell checked. */
747 ;;*---------------------------------------------------------------------*/
748 (defvar flyspell-changes nil)
749 (make-variable-buffer-local 'flyspell-changes)
751 ;;*---------------------------------------------------------------------*/
752 ;;* flyspell-after-change-function ... */
753 ;;*---------------------------------------------------------------------*/
754 (defun flyspell-after-change-function (start stop len)
755 "Save the current buffer and point for Flyspell's post-command hook."
756 (push (cons start stop) flyspell-changes))
758 ;;*---------------------------------------------------------------------*/
759 ;;* flyspell-check-changed-word-p ... */
760 ;;*---------------------------------------------------------------------*/
761 (defun flyspell-check-changed-word-p (start stop)
762 "Return t when the changed word has to be checked.
763 The answer depends of several criteria.
764 Mostly we check word delimiters."
765 (cond
766 ((and (memq (char-after start) '(?\n ? )) (> stop start))
768 ((not (numberp flyspell-pre-point))
770 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
771 nil)
772 ((let ((pos (point)))
773 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
774 nil)
776 t)))
778 ;;*---------------------------------------------------------------------*/
779 ;;* flyspell-check-word-p ... */
780 ;;*---------------------------------------------------------------------*/
781 (defun flyspell-check-word-p ()
782 "Return t when the word at `point' has to be checked.
783 The answer depends of several criteria.
784 Mostly we check word delimiters."
785 (cond
786 ((<= (- (point-max) 1) (point-min))
787 ;; the buffer is not filled enough
788 nil)
789 ((and (and (> (current-column) 0)
790 (not (eq (current-column) flyspell-pre-column)))
791 (save-excursion
792 (backward-char 1)
793 (and (looking-at (flyspell-get-not-casechars))
794 (or flyspell-consider-dash-as-word-delimiter-flag
795 (not (looking-at "-"))))))
796 ;; yes because we have reached or typed a word delimiter.
798 ((symbolp this-command)
799 (cond
800 ((get this-command 'flyspell-deplacement)
801 (not (eq flyspell-previous-command this-command)))
802 ((get this-command 'flyspell-delayed)
803 ;; the current command is not delayed, that
804 ;; is that we must check the word now
805 (and (not unread-command-events)
806 (sit-for flyspell-delay)))
807 (t t)))
808 (t t)))
810 ;;*---------------------------------------------------------------------*/
811 ;;* flyspell-debug-signal-no-check ... */
812 ;;*---------------------------------------------------------------------*/
813 (defun flyspell-debug-signal-no-check (msg obj)
814 (setq debug-on-error t)
815 (with-current-buffer (get-buffer-create "*flyspell-debug*")
816 (erase-buffer)
817 (insert "NO-CHECK:\n")
818 (insert (format " %S : %S\n" msg obj))))
820 ;;*---------------------------------------------------------------------*/
821 ;;* flyspell-debug-signal-pre-word-checked ... */
822 ;;*---------------------------------------------------------------------*/
823 (defun flyspell-debug-signal-pre-word-checked ()
824 (setq debug-on-error t)
825 (with-current-buffer (get-buffer-create "*flyspell-debug*")
826 (insert "PRE-WORD:\n")
827 (insert (format " pre-point : %S\n" flyspell-pre-point))
828 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
829 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
830 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
831 (goto-char (point-max))))
833 ;;*---------------------------------------------------------------------*/
834 ;;* flyspell-debug-signal-word-checked ... */
835 ;;*---------------------------------------------------------------------*/
836 (defun flyspell-debug-signal-word-checked ()
837 (setq debug-on-error t)
838 (let ((oldbuf (current-buffer))
839 (point (point)))
840 (with-current-buffer (get-buffer-create "*flyspell-debug*")
841 (insert "WORD:\n")
842 (insert (format " this-cmd : %S\n" this-command))
843 (insert (format " delayed : %S\n" (and (symbolp this-command)
844 (get this-command 'flyspell-delayed))))
845 (insert (format " point : %S\n" point))
846 (insert (format " prev-char : [%c] %S\n"
847 (with-current-buffer oldbuf
848 (let ((c (if (> (point) (point-min))
849 (save-excursion
850 (backward-char 1)
851 (char-after (point)))
852 ? )))
854 (with-current-buffer oldbuf
855 (let ((c (if (> (point) (point-min))
856 (save-excursion
857 (backward-char 1)
858 (and (and (looking-at (flyspell-get-not-casechars)) 1)
859 (and (or flyspell-consider-dash-as-word-delimiter-flag
860 (not (looking-at "\\-"))) 2))))))
861 c))))
862 (insert (format " because : %S\n"
863 (cond
864 ((not (and (symbolp this-command)
865 (get this-command 'flyspell-delayed)))
866 ;; the current command is not delayed, that
867 ;; is that we must check the word now
868 'not-delayed)
869 ((with-current-buffer oldbuf
870 (let ((c (if (> (point) (point-min))
871 (save-excursion
872 (backward-char 1)
873 (and (looking-at (flyspell-get-not-casechars))
874 (or flyspell-consider-dash-as-word-delimiter-flag
875 (not (looking-at "\\-"))))))))
877 ;; yes because we have reached or typed a word delimiter.
878 'separator)
879 ((not (integerp flyspell-delay))
880 ;; yes because the user had set up a no-delay configuration.
881 'no-delay)
883 'sit-for))))
884 (goto-char (point-max)))))
886 ;;*---------------------------------------------------------------------*/
887 ;;* flyspell-debug-signal-changed-checked ... */
888 ;;*---------------------------------------------------------------------*/
889 (defun flyspell-debug-signal-changed-checked ()
890 (setq debug-on-error t)
891 (let ((point (point)))
892 (with-current-buffer (get-buffer-create "*flyspell-debug*")
893 (insert "CHANGED WORD:\n")
894 (insert (format " point : %S\n" point))
895 (goto-char (point-max)))))
897 ;;*---------------------------------------------------------------------*/
898 ;;* flyspell-post-command-hook ... */
899 ;;* ------------------------------------------------------------- */
900 ;;* It is possible that we check several words: */
901 ;;* 1- the current word is checked if the predicate */
902 ;;* FLYSPELL-CHECK-WORD-P is true */
903 ;;* 2- the word that used to be the current word before the */
904 ;;* THIS-COMMAND is checked if: */
905 ;;* a- the previous word is different from the current word */
906 ;;* b- the previous word as not just been checked by the */
907 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
908 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
909 ;;* previous word nor the current word */
910 ;;*---------------------------------------------------------------------*/
911 (defun flyspell-post-command-hook ()
912 "The `post-command-hook' used by flyspell to check a word in-the-fly."
913 (interactive)
914 (let ((command this-command)
915 ;; Prevent anything we do from affecting the mark.
916 deactivate-mark)
917 (if (flyspell-check-pre-word-p)
918 (with-current-buffer flyspell-pre-buffer
919 '(flyspell-debug-signal-pre-word-checked)
920 (save-excursion
921 (goto-char flyspell-pre-point)
922 (flyspell-word))))
923 (if (flyspell-check-word-p)
924 (progn
925 '(flyspell-debug-signal-word-checked)
926 (flyspell-word)
927 ;; we remember which word we have just checked.
928 ;; this will be used next time we will check a word
929 ;; to compare the next current word with the word
930 ;; that as been registered in the pre-command-hook
931 ;; that is these variables are used within the predicate
932 ;; FLYSPELL-CHECK-PRE-WORD-P
933 (setq flyspell-pre-pre-buffer (current-buffer))
934 (setq flyspell-pre-pre-point (point)))
935 (progn
936 (setq flyspell-pre-pre-buffer nil)
937 (setq flyspell-pre-pre-point nil)
938 ;; when a word is not checked because of a delayed command
939 ;; we do not disable the ispell cache.
940 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
941 (progn
942 (setq flyspell-word-cache-end -1)
943 (setq flyspell-word-cache-result '_)))))
944 (while (and (not (input-pending-p)) (consp flyspell-changes))
945 (let ((start (car (car flyspell-changes)))
946 (stop (cdr (car flyspell-changes))))
947 (if (flyspell-check-changed-word-p start stop)
948 (save-excursion
949 '(flyspell-debug-signal-changed-checked)
950 (goto-char start)
951 (flyspell-word)))
952 (setq flyspell-changes (cdr flyspell-changes))))
953 (setq flyspell-previous-command command)))
955 ;;*---------------------------------------------------------------------*/
956 ;;* flyspell-notify-misspell ... */
957 ;;*---------------------------------------------------------------------*/
958 (defun flyspell-notify-misspell (word poss)
959 (let ((replacements (if (stringp poss)
960 poss
961 (if flyspell-sort-corrections
962 (sort (car (cdr (cdr poss))) 'string<)
963 (car (cdr (cdr poss)))))))
964 (if flyspell-issue-message-flag
965 (message "misspelling `%s' %S" word replacements))))
967 ;;*---------------------------------------------------------------------*/
968 ;;* flyspell-word-search-backward ... */
969 ;;*---------------------------------------------------------------------*/
970 (defun flyspell-word-search-backward (word bound)
971 (save-excursion
972 (let ((r '())
973 (inhibit-point-motion-hooks t)
975 (while (and (not r) (setq p (search-backward word bound t)))
976 (let ((lw (flyspell-get-word '())))
977 (if (and (consp lw) (string-equal (car lw) word))
978 (setq r p)
979 (goto-char p))))
980 r)))
982 ;;*---------------------------------------------------------------------*/
983 ;;* flyspell-word-search-forward ... */
984 ;;*---------------------------------------------------------------------*/
985 (defun flyspell-word-search-forward (word bound)
986 (save-excursion
987 (let ((r '())
988 (inhibit-point-motion-hooks t)
990 (while (and (not r) (setq p (search-forward word bound t)))
991 (let ((lw (flyspell-get-word '())))
992 (if (and (consp lw) (string-equal (car lw) word))
993 (setq r p)
994 (goto-char (1+ p)))))
995 r)))
997 ;;*---------------------------------------------------------------------*/
998 ;;* flyspell-word ... */
999 ;;*---------------------------------------------------------------------*/
1000 (defun flyspell-word (&optional following)
1001 "Spell check a word."
1002 (interactive (list ispell-following-word))
1003 (save-excursion
1004 ;; use the correct dictionary
1005 (flyspell-accept-buffer-local-defs)
1006 (let* ((cursor-location (point))
1007 (flyspell-word (flyspell-get-word following))
1008 start end poss word ispell-filter)
1009 (if (or (eq flyspell-word nil)
1010 (and (fboundp flyspell-generic-check-word-predicate)
1011 (not (funcall flyspell-generic-check-word-predicate))))
1013 (progn
1014 ;; destructure return flyspell-word info list.
1015 (setq start (car (cdr flyspell-word))
1016 end (car (cdr (cdr flyspell-word)))
1017 word (car flyspell-word))
1018 ;; before checking in the directory, we check for doublons.
1019 (cond
1020 ((and (or (not (eq ispell-parser 'tex))
1021 (and (> start (point-min))
1022 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1023 flyspell-mark-duplications-flag
1024 (save-excursion
1025 (goto-char start)
1026 (let* ((bound
1027 (- start
1028 (- end start)
1029 (- (skip-chars-backward " \t\n\f"))))
1030 (p (when (>= bound (point-min))
1031 (flyspell-word-search-backward word bound))))
1032 (and p (/= p start)))))
1033 ;; yes, this is a doublon
1034 (flyspell-highlight-incorrect-region start end 'doublon)
1035 nil)
1036 ((and (eq flyspell-word-cache-start start)
1037 (eq flyspell-word-cache-end end)
1038 (string-equal flyspell-word-cache-word word))
1039 ;; this word had been already checked, we skip
1040 flyspell-word-cache-result)
1041 ((and (eq ispell-parser 'tex)
1042 (flyspell-tex-command-p flyspell-word))
1043 ;; this is a correct word (because a tex command)
1044 (flyspell-unhighlight-at start)
1045 (if (> end start)
1046 (flyspell-unhighlight-at (- end 1)))
1049 ;; we setup the cache
1050 (setq flyspell-word-cache-start start)
1051 (setq flyspell-word-cache-end end)
1052 (setq flyspell-word-cache-word word)
1053 ;; now check spelling of word.
1054 (ispell-send-string "%\n")
1055 ;; put in verbose mode
1056 (ispell-send-string (concat "^" word "\n"))
1057 ;; we mark the ispell process so it can be killed
1058 ;; when emacs is exited without query
1059 (set-process-query-on-exit-flag ispell-process nil)
1060 ;; Wait until ispell has processed word. Since this code is often
1061 ;; executed from post-command-hook but the ispell process may not
1062 ;; be responsive, it's important to make sure we re-enable C-g.
1063 (with-local-quit
1064 (while (progn
1065 (accept-process-output ispell-process)
1066 (not (string= "" (car ispell-filter))))))
1067 ;; (ispell-send-string "!\n")
1068 ;; back to terse mode.
1069 ;; Remove leading empty element
1070 (setq ispell-filter (cdr ispell-filter))
1071 ;; ispell process should return something after word is sent.
1072 ;; Tag word as valid (i.e., skip) otherwise
1073 (or ispell-filter
1074 (setq ispell-filter '(*)))
1075 (if (consp ispell-filter)
1076 (setq poss (ispell-parse-output (car ispell-filter))))
1077 (let ((res (cond ((eq poss t)
1078 ;; correct
1079 (setq flyspell-word-cache-result t)
1080 (flyspell-unhighlight-at start)
1081 (if (> end start)
1082 (flyspell-unhighlight-at (- end 1)))
1084 ((and (stringp poss) flyspell-highlight-flag)
1085 ;; correct
1086 (setq flyspell-word-cache-result t)
1087 (flyspell-unhighlight-at start)
1088 (if (> end start)
1089 (flyspell-unhighlight-at (- end 1)))
1091 ((null poss)
1092 (setq flyspell-word-cache-result t)
1093 (flyspell-unhighlight-at start)
1094 (if (> end start)
1095 (flyspell-unhighlight-at (- end 1)))
1097 ((or (and (< flyspell-duplicate-distance 0)
1098 (or (save-excursion
1099 (goto-char start)
1100 (flyspell-word-search-backward
1101 word
1102 (point-min)))
1103 (save-excursion
1104 (goto-char end)
1105 (flyspell-word-search-forward
1106 word
1107 (point-max)))))
1108 (and (> flyspell-duplicate-distance 0)
1109 (or (save-excursion
1110 (goto-char start)
1111 (flyspell-word-search-backward
1112 word
1113 (- start
1114 flyspell-duplicate-distance)))
1115 (save-excursion
1116 (goto-char end)
1117 (flyspell-word-search-forward
1118 word
1119 (+ end
1120 flyspell-duplicate-distance))))))
1121 ;; This is a misspelled word which occurs
1122 ;; twice within flyspell-duplicate-distance.
1123 (setq flyspell-word-cache-result nil)
1124 (if flyspell-highlight-flag
1125 (flyspell-highlight-duplicate-region
1126 start end poss)
1127 (message "duplicate `%s'" word))
1128 nil)
1130 (setq flyspell-word-cache-result nil)
1131 ;; incorrect highlight the location
1132 (if flyspell-highlight-flag
1133 (flyspell-highlight-incorrect-region
1134 start end poss)
1135 (flyspell-notify-misspell word poss))
1136 nil))))
1137 ;; return to original location
1138 (goto-char cursor-location)
1139 (if ispell-quit (setq ispell-quit nil))
1140 res))))))))
1142 ;;*---------------------------------------------------------------------*/
1143 ;;* flyspell-tex-math-initialized ... */
1144 ;;*---------------------------------------------------------------------*/
1145 (defvar flyspell-tex-math-initialized nil)
1147 ;;*---------------------------------------------------------------------*/
1148 ;;* flyspell-math-tex-command-p ... */
1149 ;;* ------------------------------------------------------------- */
1150 ;;* This function uses the texmathp package to check if (point) */
1151 ;;* is within a tex command. In order to avoid using */
1152 ;;* condition-case each time we use the variable */
1153 ;;* flyspell-tex-math-initialized to make a special case the first */
1154 ;;* time that function is called. */
1155 ;;*---------------------------------------------------------------------*/
1156 (defun flyspell-math-tex-command-p ()
1157 (when (fboundp 'texmathp)
1158 (cond
1159 (flyspell-check-tex-math-command
1160 nil)
1161 ((eq flyspell-tex-math-initialized t)
1162 (texmathp))
1163 ((eq flyspell-tex-math-initialized 'error)
1164 nil)
1166 (setq flyspell-tex-math-initialized t)
1167 (condition-case nil
1168 (texmathp)
1169 (error (progn
1170 (setq flyspell-tex-math-initialized 'error)
1171 nil)))))))
1173 ;;*---------------------------------------------------------------------*/
1174 ;;* flyspell-tex-command-p ... */
1175 ;;*---------------------------------------------------------------------*/
1176 (defun flyspell-tex-command-p (word)
1177 "Return t if WORD is a TeX command."
1178 (or (save-excursion
1179 (let ((b (car (cdr word))))
1180 (and (re-search-backward "\\\\" (- (point) 100) t)
1181 (or (= (match-end 0) b)
1182 (and (goto-char (match-end 0))
1183 (looking-at flyspell-tex-command-regexp)
1184 (>= (match-end 0) b))))))
1185 (flyspell-math-tex-command-p)))
1187 ;;*---------------------------------------------------------------------*/
1188 ;;* flyspell-casechars-cache ... */
1189 ;;*---------------------------------------------------------------------*/
1190 (defvar flyspell-casechars-cache nil)
1191 (defvar flyspell-ispell-casechars-cache nil)
1192 (make-variable-buffer-local 'flyspell-casechars-cache)
1193 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1195 ;;*---------------------------------------------------------------------*/
1196 ;;* flyspell-get-casechars ... */
1197 ;;*---------------------------------------------------------------------*/
1198 (defun flyspell-get-casechars ()
1199 "This function builds a string that is the regexp of word chars.
1200 In order to avoid one useless string construction,
1201 this function changes the last char of the `ispell-casechars' string."
1202 (let ((ispell-casechars (ispell-get-casechars)))
1203 (cond
1204 ((eq ispell-parser 'tex)
1205 (setq flyspell-ispell-casechars-cache ispell-casechars)
1206 (setq flyspell-casechars-cache
1207 (concat (substring ispell-casechars
1209 (- (length ispell-casechars) 1))
1210 "]"))
1211 flyspell-casechars-cache)
1213 (setq flyspell-ispell-casechars-cache ispell-casechars)
1214 (setq flyspell-casechars-cache ispell-casechars)
1215 flyspell-casechars-cache))))
1217 ;;*---------------------------------------------------------------------*/
1218 ;;* flyspell-get-not-casechars-cache ... */
1219 ;;*---------------------------------------------------------------------*/
1220 (defvar flyspell-not-casechars-cache nil)
1221 (defvar flyspell-ispell-not-casechars-cache nil)
1222 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1223 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1225 ;;*---------------------------------------------------------------------*/
1226 ;;* flyspell-get-not-casechars ... */
1227 ;;*---------------------------------------------------------------------*/
1228 (defun flyspell-get-not-casechars ()
1229 "This function builds a string that is the regexp of non-word chars."
1230 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1231 (cond
1232 ((eq ispell-parser 'tex)
1233 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1234 (setq flyspell-not-casechars-cache
1235 (concat (substring ispell-not-casechars
1237 (- (length ispell-not-casechars) 1))
1238 "]"))
1239 flyspell-not-casechars-cache)
1241 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1242 (setq flyspell-not-casechars-cache ispell-not-casechars)
1243 flyspell-not-casechars-cache))))
1245 ;;*---------------------------------------------------------------------*/
1246 ;;* flyspell-get-word ... */
1247 ;;*---------------------------------------------------------------------*/
1248 (defun flyspell-get-word (following &optional extra-otherchars)
1249 "Return the word for spell-checking according to Ispell syntax.
1250 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1251 is non-nil when called interactively, then the following word
1252 \(rather than preceding\) is checked when the cursor is not over a word.
1253 Optional second argument contains otherchars that can be included in word
1254 many times.
1256 Word syntax described by `flyspell-dictionary-alist' (which see)."
1257 (let* ((flyspell-casechars (flyspell-get-casechars))
1258 (flyspell-not-casechars (flyspell-get-not-casechars))
1259 (ispell-otherchars (ispell-get-otherchars))
1260 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1261 (word-regexp (concat flyspell-casechars
1262 "+\\("
1263 (if (not (string= "" ispell-otherchars))
1264 (concat ispell-otherchars "?"))
1265 (if extra-otherchars
1266 (concat extra-otherchars "?"))
1267 flyspell-casechars
1268 "+\\)"
1269 (if (or ispell-many-otherchars-p
1270 extra-otherchars)
1271 "*" "?")))
1272 did-it-once prevpt
1273 start end word)
1274 ;; find the word
1275 (if (not (looking-at flyspell-casechars))
1276 (if following
1277 (re-search-forward flyspell-casechars nil t)
1278 (re-search-backward flyspell-casechars nil t)))
1279 ;; move to front of word
1280 (re-search-backward flyspell-not-casechars nil 'start)
1281 (while (and (or (and (not (string= "" ispell-otherchars))
1282 (looking-at ispell-otherchars))
1283 (and extra-otherchars (looking-at extra-otherchars)))
1284 (not (bobp))
1285 (or (not did-it-once)
1286 ispell-many-otherchars-p)
1287 (not (eq prevpt (point))))
1288 (if (and extra-otherchars (looking-at extra-otherchars))
1289 (progn
1290 (backward-char 1)
1291 (if (looking-at flyspell-casechars)
1292 (re-search-backward flyspell-not-casechars nil 'move)))
1293 (setq did-it-once t
1294 prevpt (point))
1295 (backward-char 1)
1296 (if (looking-at flyspell-casechars)
1297 (re-search-backward flyspell-not-casechars nil 'move)
1298 (backward-char -1))))
1299 ;; Now mark the word and save to string.
1300 (if (not (re-search-forward word-regexp nil t))
1302 (progn
1303 (setq start (match-beginning 0)
1304 end (point)
1305 word (buffer-substring-no-properties start end))
1306 (list word start end)))))
1308 ;;*---------------------------------------------------------------------*/
1309 ;;* flyspell-small-region ... */
1310 ;;*---------------------------------------------------------------------*/
1311 (defun flyspell-small-region (beg end)
1312 "Flyspell text between BEG and END."
1313 (save-excursion
1314 (if (> beg end)
1315 (let ((old beg))
1316 (setq beg end)
1317 (setq end old)))
1318 (goto-char beg)
1319 (let ((count 0))
1320 (while (< (point) end)
1321 (if (and flyspell-issue-message-flag (= count 100))
1322 (progn
1323 (message "Spell Checking...%d%%"
1324 (* 100 (/ (float (- (point) beg)) (- end beg))))
1325 (setq count 0))
1326 (setq count (+ 1 count)))
1327 (flyspell-word)
1328 (sit-for 0)
1329 (let ((cur (point)))
1330 (forward-word 1)
1331 (if (and (< (point) end) (> (point) (+ cur 1)))
1332 (backward-char 1)))))
1333 (backward-char 1)
1334 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1335 (flyspell-word)))
1337 ;;*---------------------------------------------------------------------*/
1338 ;;* flyspell-external-ispell-process ... */
1339 ;;*---------------------------------------------------------------------*/
1340 (defvar flyspell-external-ispell-process '()
1341 "The external Flyspell Ispell process.")
1343 ;;*---------------------------------------------------------------------*/
1344 ;;* flyspell-external-ispell-buffer ... */
1345 ;;*---------------------------------------------------------------------*/
1346 (defvar flyspell-external-ispell-buffer '())
1347 (defvar flyspell-large-region-buffer '())
1348 (defvar flyspell-large-region-beg (point-min))
1349 (defvar flyspell-large-region-end (point-max))
1351 ;;*---------------------------------------------------------------------*/
1352 ;;* flyspell-external-point-words ... */
1353 ;;*---------------------------------------------------------------------*/
1354 (defun flyspell-external-point-words ()
1355 "Mark words from a buffer listing incorrect words in order of appearance.
1356 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1357 \(We finish by killing that buffer and setting the variable to nil.)
1358 The buffer to mark them in is `flyspell-large-region-buffer'."
1359 (let (words-not-found
1360 (ispell-otherchars (ispell-get-otherchars))
1361 (buffer-scan-pos flyspell-large-region-beg)
1362 case-fold-search)
1363 (with-current-buffer flyspell-external-ispell-buffer
1364 (goto-char (point-min))
1365 ;; Loop over incorrect words, in the order they were reported,
1366 ;; which is also the order they appear in the buffer being checked.
1367 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1368 ;; Bind WORD to the next one.
1369 (let ((word (match-string 1)) (wordpos (point)))
1370 ;; Here there used to be code to see if WORD is the same
1371 ;; as the previous iteration, and count the number of consecutive
1372 ;; identical words, and the loop below would search for that many.
1373 ;; That code seemed to be incorrect, and on principle, should
1374 ;; be unnecessary too. -- rms.
1375 (if flyspell-issue-message-flag
1376 (message "Spell Checking...%d%% [%s]"
1377 (* 100 (/ (float (point)) (point-max)))
1378 word))
1379 (with-current-buffer flyspell-large-region-buffer
1380 (goto-char buffer-scan-pos)
1381 (let ((keep t))
1382 ;; Iterate on string search until string is found as word,
1383 ;; not as substring
1384 (while keep
1385 (if (search-forward word
1386 flyspell-large-region-end t)
1387 (let* ((found-list
1388 (save-excursion
1389 ;; Move back into the match
1390 ;; so flyspell-get-word will find it.
1391 (forward-char -1)
1392 (flyspell-get-word nil)))
1393 (found (car found-list))
1394 (found-length (length found))
1395 (misspell-length (length word)))
1396 (when (or
1397 ;; Size matches, we really found it.
1398 (= found-length misspell-length)
1399 ;; Matches as part of a boundary-char separated word
1400 (member word
1401 (split-string found ispell-otherchars))
1402 ;; Misspelling has higher length than
1403 ;; what flyspell considers the
1404 ;; word. Caused by boundary-chars
1405 ;; mismatch. Validating seems safe.
1406 (< found-length misspell-length)
1407 ;; ispell treats beginning of some TeX
1408 ;; commands as nroff control sequences
1409 ;; and strips them in the list of
1410 ;; misspelled words thus giving a
1411 ;; non-existent word. Skip if ispell
1412 ;; is used, string is a TeX command
1413 ;; (char before beginning of word is
1414 ;; backslash) and none of the previous
1415 ;; contitions match
1416 (and (not ispell-really-aspell)
1417 (save-excursion
1418 (goto-char (- (nth 1 found-list) 1))
1419 (if (looking-at "[\\]" )
1421 nil))))
1422 (setq keep nil)
1423 (flyspell-word)
1424 ;; Search for next misspelled word will begin from
1425 ;; end of last validated match.
1426 (setq buffer-scan-pos (point))))
1427 ;; Record if misspelling is not found and try new one
1428 (add-to-list 'words-not-found
1429 (concat " -> " word " - "
1430 (int-to-string wordpos)))
1431 (setq keep nil)))))))
1432 ;; we are done
1433 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1434 ;; Warn about not found misspellings
1435 (dolist (word words-not-found)
1436 (message "%s: word not found" word))
1437 ;; Kill and forget the buffer with the list of incorrect words.
1438 (kill-buffer flyspell-external-ispell-buffer)
1439 (setq flyspell-external-ispell-buffer nil)))
1441 ;;*---------------------------------------------------------------------*/
1442 ;;* flyspell-process-localwords ... */
1443 ;;* ------------------------------------------------------------- */
1444 ;;* This function is used to prevent marking of words explicitly */
1445 ;;* declared correct. */
1446 ;;*---------------------------------------------------------------------*/
1447 (defun flyspell-process-localwords (misspellings-buffer)
1448 (let (localwords case-fold-search
1449 (ispell-casechars (ispell-get-casechars)))
1450 ;; Get localwords from the original buffer
1451 (save-excursion
1452 (goto-char (point-min))
1453 ;; Localwords parsing copied from ispell.el.
1454 (while (search-forward ispell-words-keyword nil t)
1455 (let ((end (save-excursion (end-of-line) (point)))
1456 string)
1457 ;; buffer-local words separated by a space, and can contain
1458 ;; any character other than a space. Not rigorous enough.
1459 (while (re-search-forward " *\\([^ ]+\\)" end t)
1460 (setq string (buffer-substring-no-properties (match-beginning 1)
1461 (match-end 1)))
1462 ;; This can fail when string contains a word with invalid chars.
1463 ;; Error handling needs to be added between Ispell and Emacs.
1464 (if (and (< 1 (length string))
1465 (equal 0 (string-match ispell-casechars string)))
1466 (push string localwords))))))
1467 ;; Remove localwords matches from misspellings-buffer.
1468 ;; The usual mechanism of communicating the local words to ispell
1469 ;; does not affect the special ispell process used by
1470 ;; flyspell-large-region.
1471 (with-current-buffer misspellings-buffer
1472 (save-excursion
1473 (dolist (word localwords)
1474 (goto-char (point-min))
1475 (let ((regexp (concat "^" word "\n")))
1476 (while (re-search-forward regexp nil t)
1477 (delete-region (match-beginning 0) (match-end 0)))))))))
1479 ;;* ---------------------------------------------------------------
1480 ;;* flyspell-check-region-doublons
1481 ;;* ---------------------------------------------------------------
1482 (defun flyspell-check-region-doublons (beg end)
1483 "Check for adjacent duplicated words (doublons) in the given region."
1484 (save-excursion
1485 (goto-char beg)
1486 (flyspell-word) ; Make sure current word is checked
1487 (backward-word 1)
1488 (while (and (< (point) end)
1489 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1490 end 'move))
1491 (flyspell-word)
1492 (backward-word 1))
1493 (flyspell-word)))
1495 ;;*---------------------------------------------------------------------*/
1496 ;;* flyspell-large-region ... */
1497 ;;*---------------------------------------------------------------------*/
1498 (defun flyspell-large-region (beg end)
1499 (let* ((curbuf (current-buffer))
1500 (buffer (get-buffer-create "*flyspell-region*")))
1501 (setq flyspell-external-ispell-buffer buffer)
1502 (setq flyspell-large-region-buffer curbuf)
1503 (setq flyspell-large-region-beg beg)
1504 (setq flyspell-large-region-end end)
1505 (flyspell-accept-buffer-local-defs)
1506 (set-buffer buffer)
1507 (erase-buffer)
1508 ;; this is done, we can start checking...
1509 (if flyspell-issue-message-flag (message "Checking region..."))
1510 (set-buffer curbuf)
1511 (ispell-check-version)
1512 (let ((c (apply 'ispell-call-process-region beg
1514 ispell-program-name
1516 buffer
1518 (if ispell-really-aspell "list" "-l")
1519 (let (args)
1520 ;; Local dictionary becomes the global dictionary in use.
1521 (if ispell-local-dictionary
1522 (setq ispell-dictionary ispell-local-dictionary))
1523 (setq args (ispell-get-ispell-args))
1524 (if ispell-dictionary ; use specified dictionary
1525 (setq args
1526 (append (list "-d" ispell-dictionary) args)))
1527 (if ispell-personal-dictionary ; use specified pers dict
1528 (setq args
1529 (append args
1530 (list "-p"
1531 (expand-file-name
1532 ispell-personal-dictionary)))))
1533 (setq args (append args ispell-extra-args))
1534 args))))
1535 (if (eq c 0)
1536 (progn
1537 (flyspell-process-localwords buffer)
1538 (with-current-buffer curbuf
1539 (flyspell-delete-region-overlays beg end)
1540 (flyspell-check-region-doublons beg end))
1541 (flyspell-external-point-words))
1542 (error "Can't check region...")))))
1544 ;;*---------------------------------------------------------------------*/
1545 ;;* flyspell-region ... */
1546 ;;* ------------------------------------------------------------- */
1547 ;;* Because `ispell -a' is too slow, it is not possible to use */
1548 ;;* it on large region. Then, when ispell is invoked on a large */
1549 ;;* text region, a new `ispell -l' process is spawned. The */
1550 ;;* pointed out words are then searched in the region a checked with */
1551 ;;* regular flyspell means. */
1552 ;;*---------------------------------------------------------------------*/
1553 ;;;###autoload
1554 (defun flyspell-region (beg end)
1555 "Flyspell text between BEG and END."
1556 (interactive "r")
1557 (if (= beg end)
1559 (save-excursion
1560 (if (> beg end)
1561 (let ((old beg))
1562 (setq beg end)
1563 (setq end old)))
1564 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1565 (flyspell-large-region beg end)
1566 (flyspell-small-region beg end)))))
1568 ;;*---------------------------------------------------------------------*/
1569 ;;* flyspell-buffer ... */
1570 ;;*---------------------------------------------------------------------*/
1571 ;;;###autoload
1572 (defun flyspell-buffer ()
1573 "Flyspell whole buffer."
1574 (interactive)
1575 (flyspell-region (point-min) (point-max)))
1577 ;;*---------------------------------------------------------------------*/
1578 ;;* old next error position ... */
1579 ;;*---------------------------------------------------------------------*/
1580 (defvar flyspell-old-buffer-error nil)
1581 (defvar flyspell-old-pos-error nil)
1583 ;;*---------------------------------------------------------------------*/
1584 ;;* flyspell-goto-next-error ... */
1585 ;;*---------------------------------------------------------------------*/
1586 (defun flyspell-goto-next-error ()
1587 "Go to the next previously detected error.
1588 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1589 FLYSPELL-BUFFER."
1590 (interactive)
1591 (let ((pos (point))
1592 (max (point-max)))
1593 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1594 (eq pos flyspell-old-pos-error))
1595 (progn
1596 (if (= flyspell-old-pos-error max)
1597 ;; goto beginning of buffer
1598 (progn
1599 (message "Restarting from beginning of buffer")
1600 (goto-char (point-min)))
1601 (forward-word 1))
1602 (setq pos (point))))
1603 ;; seek the next error
1604 (while (and (< pos max)
1605 (let ((ovs (overlays-at pos))
1606 (r '()))
1607 (while (and (not r) (consp ovs))
1608 (if (flyspell-overlay-p (car ovs))
1609 (setq r t)
1610 (setq ovs (cdr ovs))))
1611 (not r)))
1612 (setq pos (1+ pos)))
1613 ;; save the current location for next invocation
1614 (setq flyspell-old-pos-error pos)
1615 (setq flyspell-old-buffer-error (current-buffer))
1616 (goto-char pos)
1617 (if (= pos max)
1618 (message "No more miss-spelled word!"))))
1620 ;;*---------------------------------------------------------------------*/
1621 ;;* flyspell-overlay-p ... */
1622 ;;*---------------------------------------------------------------------*/
1623 (defun flyspell-overlay-p (o)
1624 "A predicate that return true iff O is an overlay used by flyspell."
1625 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1627 ;;*---------------------------------------------------------------------*/
1628 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1629 ;;* ------------------------------------------------------------- */
1630 ;;* Remove overlays introduced by flyspell. */
1631 ;;*---------------------------------------------------------------------*/
1632 (defun flyspell-delete-region-overlays (beg end)
1633 "Delete overlays used by flyspell in a given region."
1634 (remove-overlays beg end 'flyspell-overlay t))
1637 (defun flyspell-delete-all-overlays ()
1638 "Delete all the overlays used by flyspell."
1639 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1641 ;;*---------------------------------------------------------------------*/
1642 ;;* flyspell-unhighlight-at ... */
1643 ;;*---------------------------------------------------------------------*/
1644 (defun flyspell-unhighlight-at (pos)
1645 "Remove the flyspell overlay that are located at POS."
1646 (if flyspell-persistent-highlight
1647 (let ((overlays (overlays-at pos)))
1648 (while (consp overlays)
1649 (if (flyspell-overlay-p (car overlays))
1650 (delete-overlay (car overlays)))
1651 (setq overlays (cdr overlays))))
1652 (if (flyspell-overlay-p flyspell-overlay)
1653 (delete-overlay flyspell-overlay))))
1655 ;;*---------------------------------------------------------------------*/
1656 ;;* flyspell-properties-at-p ... */
1657 ;;* ------------------------------------------------------------- */
1658 ;;* Is there an highlight properties at position pos? */
1659 ;;*---------------------------------------------------------------------*/
1660 (defun flyspell-properties-at-p (pos)
1661 "Return t if there is a text property at POS, not counting `local-map'.
1662 If variable `flyspell-highlight-properties' is set to nil,
1663 text with properties are not checked. This function is used to discover
1664 if the character at POS has any other property."
1665 (let ((prop (text-properties-at pos))
1666 (keep t))
1667 (while (and keep (consp prop))
1668 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1669 (setq prop (cdr (cdr prop)))
1670 (setq keep nil)))
1671 (consp prop)))
1673 ;;*---------------------------------------------------------------------*/
1674 ;;* make-flyspell-overlay ... */
1675 ;;*---------------------------------------------------------------------*/
1676 (defun make-flyspell-overlay (beg end face mouse-face)
1677 "Allocate an overlay to highlight an incorrect word.
1678 BEG and END specify the range in the buffer of that word.
1679 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1680 for the overlay."
1681 (let ((overlay (make-overlay beg end nil t nil)))
1682 (overlay-put overlay 'face face)
1683 (overlay-put overlay 'mouse-face mouse-face)
1684 (overlay-put overlay 'flyspell-overlay t)
1685 (overlay-put overlay 'evaporate t)
1686 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1687 (overlay-put overlay 'keymap flyspell-mouse-map)
1688 (when (eq face 'flyspell-incorrect)
1689 (and (stringp flyspell-before-incorrect-word-string)
1690 (overlay-put overlay 'before-string
1691 flyspell-before-incorrect-word-string))
1692 (and (stringp flyspell-after-incorrect-word-string)
1693 (overlay-put overlay 'after-string
1694 flyspell-after-incorrect-word-string)))
1695 overlay))
1697 ;;*---------------------------------------------------------------------*/
1698 ;;* flyspell-highlight-incorrect-region ... */
1699 ;;*---------------------------------------------------------------------*/
1700 (defun flyspell-highlight-incorrect-region (beg end poss)
1701 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1702 POSS is usually a list of possible spelling/correction lists,
1703 as returned by `ispell-parse-output'.
1704 It can also be the symbol `doublon', in the case where the word
1705 is itself incorrect, but suspiciously repeated."
1706 (let ((inhibit-read-only t))
1707 (unless (run-hook-with-args-until-success
1708 'flyspell-incorrect-hook beg end poss)
1709 (if (or flyspell-highlight-properties
1710 (not (flyspell-properties-at-p beg)))
1711 (progn
1712 ;; we cleanup all the overlay that are in the region, not
1713 ;; beginning at the word start position
1714 (if (< (1+ beg) end)
1715 (let ((os (overlays-in (1+ beg) end)))
1716 (while (consp os)
1717 (if (flyspell-overlay-p (car os))
1718 (delete-overlay (car os)))
1719 (setq os (cdr os)))))
1720 ;; we cleanup current overlay at the same position
1721 (flyspell-unhighlight-at beg)
1722 ;; now we can use a new overlay
1723 (setq flyspell-overlay
1724 (make-flyspell-overlay
1725 beg end
1726 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1727 'highlight)))))))
1729 ;;*---------------------------------------------------------------------*/
1730 ;;* flyspell-highlight-duplicate-region ... */
1731 ;;*---------------------------------------------------------------------*/
1732 (defun flyspell-highlight-duplicate-region (beg end poss)
1733 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1734 POSS is a list of possible spelling/correction lists,
1735 as returned by `ispell-parse-output'."
1736 (let ((inhibit-read-only t))
1737 (unless (run-hook-with-args-until-success
1738 'flyspell-incorrect-hook beg end poss)
1739 (if (or flyspell-highlight-properties
1740 (not (flyspell-properties-at-p beg)))
1741 (progn
1742 ;; we cleanup current overlay at the same position
1743 (flyspell-unhighlight-at beg)
1744 ;; now we can use a new overlay
1745 (setq flyspell-overlay
1746 (make-flyspell-overlay beg end
1747 'flyspell-duplicate
1748 'highlight)))))))
1750 ;;*---------------------------------------------------------------------*/
1751 ;;* flyspell-auto-correct-cache ... */
1752 ;;*---------------------------------------------------------------------*/
1753 (defvar flyspell-auto-correct-pos nil)
1754 (defvar flyspell-auto-correct-region nil)
1755 (defvar flyspell-auto-correct-ring nil)
1756 (defvar flyspell-auto-correct-word nil)
1757 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1758 (make-variable-buffer-local 'flyspell-auto-correct-region)
1759 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1760 (make-variable-buffer-local 'flyspell-auto-correct-word)
1762 ;;*---------------------------------------------------------------------*/
1763 ;;* flyspell-check-previous-highlighted-word ... */
1764 ;;*---------------------------------------------------------------------*/
1765 (defun flyspell-check-previous-highlighted-word (&optional arg)
1766 "Correct the closer misspelled word.
1767 This function scans a mis-spelled word before the cursor. If it finds one
1768 it proposes replacement for that word. With prefix arg, count that many
1769 misspelled words backwards."
1770 (interactive)
1771 (let ((pos1 (point))
1772 (pos (point))
1773 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1774 ov ovs)
1775 (if (catch 'exit
1776 (while (and (setq pos (previous-overlay-change pos))
1777 (not (= pos pos1)))
1778 (setq pos1 pos)
1779 (if (> pos (point-min))
1780 (progn
1781 (setq ovs (overlays-at (1- pos)))
1782 (while (consp ovs)
1783 (setq ov (car ovs))
1784 (setq ovs (cdr ovs))
1785 (if (and (flyspell-overlay-p ov)
1786 (= 0 (setq arg (1- arg))))
1787 (throw 'exit t)))))))
1788 (save-excursion
1789 (goto-char pos)
1790 (ispell-word))
1791 (error "No word to correct before point"))))
1793 ;;*---------------------------------------------------------------------*/
1794 ;;* flyspell-display-next-corrections ... */
1795 ;;*---------------------------------------------------------------------*/
1796 (defun flyspell-display-next-corrections (corrections)
1797 (let ((string "Corrections:")
1798 (l corrections)
1799 (pos '()))
1800 (while (< (length string) 80)
1801 (if (equal (car l) flyspell-auto-correct-word)
1802 (setq pos (cons (+ 1 (length string)) pos)))
1803 (setq string (concat string " " (car l)))
1804 (setq l (cdr l)))
1805 (while (consp pos)
1806 (let ((num (car pos)))
1807 (put-text-property num
1808 (+ num (length flyspell-auto-correct-word))
1809 'face 'flyspell-incorrect
1810 string))
1811 (setq pos (cdr pos)))
1812 (if (fboundp 'display-message)
1813 (display-message 'no-log string)
1814 (message "%s" string))))
1816 ;;*---------------------------------------------------------------------*/
1817 ;;* flyspell-abbrev-table ... */
1818 ;;*---------------------------------------------------------------------*/
1819 (defun flyspell-abbrev-table ()
1820 (if flyspell-use-global-abbrev-table-p
1821 global-abbrev-table
1822 (or local-abbrev-table global-abbrev-table)))
1824 ;;*---------------------------------------------------------------------*/
1825 ;;* flyspell-define-abbrev ... */
1826 ;;*---------------------------------------------------------------------*/
1827 (defun flyspell-define-abbrev (name expansion)
1828 (let ((table (flyspell-abbrev-table)))
1829 (when table
1830 (define-abbrev table name expansion))))
1832 ;;*---------------------------------------------------------------------*/
1833 ;;* flyspell-auto-correct-word ... */
1834 ;;*---------------------------------------------------------------------*/
1835 (defun flyspell-auto-correct-word ()
1836 "Correct the current word.
1837 This command proposes various successive corrections for the current word."
1838 (interactive)
1839 (let ((pos (point))
1840 (old-max (point-max)))
1841 ;; use the correct dictionary
1842 (flyspell-accept-buffer-local-defs)
1843 (if (and (eq flyspell-auto-correct-pos pos)
1844 (consp flyspell-auto-correct-region))
1845 ;; we have already been using the function at the same location
1846 (let* ((start (car flyspell-auto-correct-region))
1847 (len (cdr flyspell-auto-correct-region)))
1848 (flyspell-unhighlight-at start)
1849 (delete-region start (+ start len))
1850 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1851 (let* ((word (car flyspell-auto-correct-ring))
1852 (len (length word)))
1853 (rplacd flyspell-auto-correct-region len)
1854 (goto-char start)
1855 (if flyspell-abbrev-p
1856 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1857 flyspell-auto-correct-word)
1858 (flyspell-change-abbrev (flyspell-abbrev-table)
1859 flyspell-auto-correct-word
1860 word)
1861 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1862 (funcall flyspell-insert-function word)
1863 (flyspell-word)
1864 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1865 (flyspell-ajust-cursor-point pos (point) old-max)
1866 (setq flyspell-auto-correct-pos (point)))
1867 ;; fetch the word to be checked
1868 (let ((word (flyspell-get-word nil)))
1869 (if (consp word)
1870 (let ((start (car (cdr word)))
1871 (end (car (cdr (cdr word))))
1872 (word (car word))
1873 poss ispell-filter)
1874 (setq flyspell-auto-correct-word word)
1875 ;; now check spelling of word.
1876 (ispell-send-string "%\n") ;put in verbose mode
1877 (ispell-send-string (concat "^" word "\n"))
1878 ;; wait until ispell has processed word.
1879 (while (progn
1880 (accept-process-output ispell-process)
1881 (not (string= "" (car ispell-filter)))))
1882 ;; Remove leading empty element
1883 (setq ispell-filter (cdr ispell-filter))
1884 ;; ispell process should return something after word is sent.
1885 ;; Tag word as valid (i.e., skip) otherwise
1886 (or ispell-filter
1887 (setq ispell-filter '(*)))
1888 (if (consp ispell-filter)
1889 (setq poss (ispell-parse-output (car ispell-filter))))
1890 (cond
1891 ((or (eq poss t) (stringp poss))
1892 ;; don't correct word
1894 ((null poss)
1895 ;; ispell error
1896 (error "Ispell: error in Ispell process"))
1898 ;; the word is incorrect, we have to propose a replacement
1899 (let ((replacements (if flyspell-sort-corrections
1900 (sort (car (cdr (cdr poss))) 'string<)
1901 (car (cdr (cdr poss))))))
1902 (setq flyspell-auto-correct-region nil)
1903 (if (consp replacements)
1904 (progn
1905 (let ((replace (car replacements)))
1906 (let ((new-word replace))
1907 (if (not (equal new-word (car poss)))
1908 (progn
1909 ;; the save the current replacements
1910 (setq flyspell-auto-correct-region
1911 (cons start (length new-word)))
1912 (let ((l replacements))
1913 (while (consp (cdr l))
1914 (setq l (cdr l)))
1915 (rplacd l (cons (car poss) replacements)))
1916 (setq flyspell-auto-correct-ring
1917 replacements)
1918 (flyspell-unhighlight-at start)
1919 (delete-region start end)
1920 (funcall flyspell-insert-function new-word)
1921 (if flyspell-abbrev-p
1922 (if (flyspell-already-abbrevp
1923 (flyspell-abbrev-table) word)
1924 (flyspell-change-abbrev
1925 (flyspell-abbrev-table)
1926 word
1927 new-word)
1928 (flyspell-define-abbrev word
1929 new-word)))
1930 (flyspell-word)
1931 (flyspell-display-next-corrections
1932 (cons new-word flyspell-auto-correct-ring))
1933 (flyspell-ajust-cursor-point pos
1934 (point)
1935 old-max))))))))))
1936 (setq flyspell-auto-correct-pos (point))
1937 (ispell-pdict-save t)))))))
1939 ;;*---------------------------------------------------------------------*/
1940 ;;* flyspell-auto-correct-previous-pos ... */
1941 ;;*---------------------------------------------------------------------*/
1942 (defvar flyspell-auto-correct-previous-pos nil
1943 "Holds the start of the first incorrect word before point.")
1945 ;;*---------------------------------------------------------------------*/
1946 ;;* flyspell-auto-correct-previous-hook ... */
1947 ;;*---------------------------------------------------------------------*/
1948 (defun flyspell-auto-correct-previous-hook ()
1949 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1950 Sets `flyspell-auto-correct-previous-pos' to nil"
1951 (interactive)
1952 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1953 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1954 (setq flyspell-auto-correct-previous-pos nil)))
1956 ;;*---------------------------------------------------------------------*/
1957 ;;* flyspell-auto-correct-previous-word ... */
1958 ;;*---------------------------------------------------------------------*/
1959 (defun flyspell-auto-correct-previous-word (position)
1960 "Auto correct the first mispelled word that occurs before point.
1961 But don't look beyond what's visible on the screen."
1962 (interactive "d")
1964 (let (top bot)
1965 (save-excursion
1966 (move-to-window-line 0)
1967 (setq top (point))
1968 (move-to-window-line -1)
1969 (setq bot (point)))
1970 (save-excursion
1971 (save-restriction
1972 (narrow-to-region top bot)
1973 (overlay-recenter (point))
1975 (add-hook 'pre-command-hook
1976 (function flyspell-auto-correct-previous-hook) t t)
1978 (unless flyspell-auto-correct-previous-pos
1979 ;; only reset if a new overlay exists
1980 (setq flyspell-auto-correct-previous-pos nil)
1982 (let ((overlay-list (overlays-in (point-min) position))
1983 (new-overlay 'dummy-value))
1985 ;; search for previous (new) flyspell overlay
1986 (while (and new-overlay
1987 (or (not (flyspell-overlay-p new-overlay))
1988 ;; check if its face has changed
1989 (not (eq (get-char-property
1990 (overlay-start new-overlay) 'face)
1991 'flyspell-incorrect))))
1992 (setq new-overlay (car-safe overlay-list))
1993 (setq overlay-list (cdr-safe overlay-list)))
1995 ;; if nothing new exits new-overlay should be nil
1996 (if new-overlay ;; the length of the word may change so go to the start
1997 (setq flyspell-auto-correct-previous-pos
1998 (overlay-start new-overlay)))))
2000 (when flyspell-auto-correct-previous-pos
2001 (save-excursion
2002 (goto-char flyspell-auto-correct-previous-pos)
2003 (let ((ispell-following-word t)) ;; point is at start
2004 (if (numberp flyspell-auto-correct-previous-pos)
2005 (goto-char flyspell-auto-correct-previous-pos))
2006 (flyspell-auto-correct-word))
2007 ;; the point may have moved so reset this
2008 (setq flyspell-auto-correct-previous-pos (point))))))))
2010 ;;*---------------------------------------------------------------------*/
2011 ;;* flyspell-correct-word ... */
2012 ;;*---------------------------------------------------------------------*/
2014 (defun flyspell-correct-word (event)
2015 "Pop up a menu of possible corrections for a misspelled word.
2016 The word checked is the word at the mouse position."
2017 (interactive "e")
2018 (let ((save (point)))
2019 (mouse-set-point event)
2020 (flyspell-correct-word-before-point event save)))
2022 (defun flyspell-correct-word-before-point (&optional event opoint)
2023 "Pop up a menu of possible corrections for misspelled word before point.
2024 If EVENT is non-nil, it is the mouse event that invoked this operation;
2025 that controls where to put the menu.
2026 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2027 (interactive)
2028 (unless (mouse-position)
2029 (error "Pop-up menus do not work on this terminal"))
2030 ;; use the correct dictionary
2031 (flyspell-accept-buffer-local-defs)
2032 (let ((cursor-location (point))
2033 (word (flyspell-get-word nil)))
2034 (if (consp word)
2035 (let ((start (car (cdr word)))
2036 (end (car (cdr (cdr word))))
2037 (word (car word))
2038 poss ispell-filter)
2039 ;; now check spelling of word.
2040 (ispell-send-string "%\n") ;put in verbose mode
2041 (ispell-send-string (concat "^" word "\n"))
2042 ;; wait until ispell has processed word
2043 (while (progn
2044 (accept-process-output ispell-process)
2045 (not (string= "" (car ispell-filter)))))
2046 ;; Remove leading empty element
2047 (setq ispell-filter (cdr ispell-filter))
2048 ;; ispell process should return something after word is sent.
2049 ;; Tag word as valid (i.e., skip) otherwise
2050 (or ispell-filter
2051 (setq ispell-filter '(*)))
2052 (if (consp ispell-filter)
2053 (setq poss (ispell-parse-output (car ispell-filter))))
2054 (cond
2055 ((or (eq poss t) (stringp poss))
2056 ;; don't correct word
2058 ((null poss)
2059 ;; ispell error
2060 (error "Ispell: error in Ispell process"))
2061 ((featurep 'xemacs)
2062 (flyspell-xemacs-popup
2063 poss word cursor-location start end opoint))
2065 ;; The word is incorrect, we have to propose a replacement.
2066 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2067 poss word cursor-location start end opoint)))
2068 (ispell-pdict-save t)))))
2070 ;;*---------------------------------------------------------------------*/
2071 ;;* flyspell-do-correct ... */
2072 ;;*---------------------------------------------------------------------*/
2073 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2074 "The popup menu callback."
2075 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2076 ;; it instead right after calling the function.
2077 (cond ((eq replace 'ignore)
2078 (goto-char save)
2079 nil)
2080 ((eq replace 'save)
2081 (goto-char save)
2082 (ispell-send-string (concat "*" word "\n"))
2083 ;; This was added only to the XEmacs side in revision 1.18 of
2084 ;; flyspell. I assume its absence on the Emacs side was an
2085 ;; oversight. --Stef
2086 (ispell-send-string "#\n")
2087 (flyspell-unhighlight-at cursor-location)
2088 (setq ispell-pdict-modified-p '(t)))
2089 ((or (eq replace 'buffer) (eq replace 'session))
2090 (ispell-send-string (concat "@" word "\n"))
2091 (flyspell-unhighlight-at cursor-location)
2092 (if (null ispell-pdict-modified-p)
2093 (setq ispell-pdict-modified-p
2094 (list ispell-pdict-modified-p)))
2095 (goto-char save)
2096 (if (eq replace 'buffer)
2097 (ispell-add-per-file-word-list word)))
2098 (replace
2099 ;; This was added only to the Emacs side. I assume its absence on
2100 ;; the XEmacs side was an oversight. --Stef
2101 (flyspell-unhighlight-at cursor-location)
2102 (let ((old-max (point-max))
2103 (new-word (if (atom replace)
2104 replace
2105 (car replace)))
2106 (cursor-location (+ (- (length word) (- end start))
2107 cursor-location)))
2108 (unless (equal new-word (car poss))
2109 (delete-region start end)
2110 (goto-char start)
2111 (funcall flyspell-insert-function new-word)
2112 (if flyspell-abbrev-p
2113 (flyspell-define-abbrev word new-word)))
2114 ;; In the original Emacs code, this was only called in the body
2115 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2116 (flyspell-ajust-cursor-point save cursor-location old-max)))
2118 (goto-char save)
2119 nil)))
2121 ;;*---------------------------------------------------------------------*/
2122 ;;* flyspell-ajust-cursor-point ... */
2123 ;;*---------------------------------------------------------------------*/
2124 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2125 (if (>= save cursor-location)
2126 (let ((new-pos (+ save (- (point-max) old-max))))
2127 (goto-char (cond
2128 ((< new-pos (point-min))
2129 (point-min))
2130 ((> new-pos (point-max))
2131 (point-max))
2132 (t new-pos))))
2133 (goto-char save)))
2135 ;;*---------------------------------------------------------------------*/
2136 ;;* flyspell-emacs-popup ... */
2137 ;;*---------------------------------------------------------------------*/
2138 (defun flyspell-emacs-popup (event poss word)
2139 "The Emacs popup menu."
2140 (if (not event)
2141 (let* ((mouse-pos (mouse-position))
2142 (mouse-pos (if (nth 1 mouse-pos)
2143 mouse-pos
2144 (set-mouse-position (car mouse-pos)
2145 (/ (frame-width) 2) 2)
2146 (mouse-position))))
2147 (setq event (list (list (car (cdr mouse-pos))
2148 (1+ (cdr (cdr mouse-pos))))
2149 (car mouse-pos)))))
2150 (let* ((corrects (if flyspell-sort-corrections
2151 (sort (car (cdr (cdr poss))) 'string<)
2152 (car (cdr (cdr poss)))))
2153 (cor-menu (if (consp corrects)
2154 (mapcar (lambda (correct)
2155 (list correct correct))
2156 corrects)
2157 '()))
2158 (affix (car (cdr (cdr (cdr poss)))))
2159 show-affix-info
2160 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2161 (list
2162 (list (concat "Save affix: " (car affix))
2163 'save)
2164 '("Accept (session)" session)
2165 '("Accept (buffer)" buffer))
2166 '(("Save word" save)
2167 ("Accept (session)" session)
2168 ("Accept (buffer)" buffer)))))
2169 (if (consp cor-menu)
2170 (append cor-menu (cons "" save))
2171 save)))
2172 (menu (cons "flyspell correction menu" base-menu)))
2173 (car (x-popup-menu event
2174 (list (format "%s [%s]" word (or ispell-local-dictionary
2175 ispell-dictionary))
2176 menu)))))
2178 ;;*---------------------------------------------------------------------*/
2179 ;;* flyspell-xemacs-popup ... */
2180 ;;*---------------------------------------------------------------------*/
2181 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2182 "The XEmacs popup menu."
2183 (let* ((corrects (if flyspell-sort-corrections
2184 (sort (car (cdr (cdr poss))) 'string<)
2185 (car (cdr (cdr poss)))))
2186 (cor-menu (if (consp corrects)
2187 (mapcar (lambda (correct)
2188 (vector correct
2189 (list 'flyspell-do-correct
2190 correct
2191 (list 'quote poss)
2192 word
2193 cursor-location
2194 start
2196 save)
2198 corrects)
2199 '()))
2200 (affix (car (cdr (cdr (cdr poss)))))
2201 show-affix-info
2202 (menu (let ((save (if (and (consp affix) show-affix-info)
2203 (vector
2204 (concat "Save affix: " (car affix))
2205 (list 'flyspell-do-correct
2206 ''save
2207 (list 'quote poss)
2208 word
2209 cursor-location
2210 start
2212 save)
2214 (vector
2215 "Save word"
2216 (list 'flyspell-do-correct
2217 ''save
2218 (list 'quote poss)
2219 word
2220 cursor-location
2221 start
2223 save)
2224 t)))
2225 (session (vector "Accept (session)"
2226 (list 'flyspell-do-correct
2227 ''session
2228 (list 'quote poss)
2229 word
2230 cursor-location
2231 start
2233 save)
2235 (buffer (vector "Accept (buffer)"
2236 (list 'flyspell-do-correct
2237 ''buffer
2238 (list 'quote poss)
2239 word
2240 cursor-location
2241 start
2243 save)
2244 t)))
2245 (if (consp cor-menu)
2246 (append cor-menu (list "-" save session buffer))
2247 (list save session buffer)))))
2248 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2249 ispell-dictionary))
2250 menu))))
2252 ;;*---------------------------------------------------------------------*/
2253 ;;* Some example functions for real autocorrecting */
2254 ;;*---------------------------------------------------------------------*/
2255 (defun flyspell-maybe-correct-transposition (beg end poss)
2256 "Check replacements for transposed characters.
2258 If the text between BEG and END is equal to a correction suggested by
2259 Ispell, after transposing two adjacent characters, correct the text,
2260 and return t.
2262 The third arg POSS is either the symbol 'doublon' or a list of
2263 possible corrections as returned by `ispell-parse-output'.
2265 This function is meant to be added to `flyspell-incorrect-hook'."
2266 (when (consp poss)
2267 (catch 'done
2268 (let ((str (buffer-substring beg end))
2269 (i 0) (len (- end beg)) tmp)
2270 (while (< (1+ i) len)
2271 (setq tmp (aref str i))
2272 (aset str i (aref str (1+ i)))
2273 (aset str (1+ i) tmp)
2274 (when (member str (nth 2 poss))
2275 (save-excursion
2276 (goto-char (+ beg i 1))
2277 (transpose-chars 1))
2278 (throw 'done t))
2279 (setq tmp (aref str i))
2280 (aset str i (aref str (1+ i)))
2281 (aset str (1+ i) tmp)
2282 (setq i (1+ i))))
2283 nil)))
2285 (defun flyspell-maybe-correct-doubling (beg end poss)
2286 "Check replacements for doubled characters.
2288 If the text between BEG and END is equal to a correction suggested by
2289 Ispell, after removing a pair of doubled characters, correct the text,
2290 and return t.
2292 The third arg POSS is either the symbol 'doublon' or a list of
2293 possible corrections as returned by `ispell-parse-output'.
2295 This function is meant to be added to `flyspell-incorrect-hook'."
2296 (when (consp poss)
2297 (catch 'done
2298 (let ((str (buffer-substring beg end))
2299 (i 0) (len (- end beg)))
2300 (while (< (1+ i) len)
2301 (when (and (= (aref str i) (aref str (1+ i)))
2302 (member (concat (substring str 0 (1+ i))
2303 (substring str (+ i 2)))
2304 (nth 2 poss)))
2305 (goto-char (+ beg i))
2306 (delete-char 1)
2307 (throw 'done t))
2308 (setq i (1+ i))))
2309 nil)))
2311 ;;*---------------------------------------------------------------------*/
2312 ;;* flyspell-already-abbrevp ... */
2313 ;;*---------------------------------------------------------------------*/
2314 (defun flyspell-already-abbrevp (table word)
2315 (let ((sym (abbrev-symbol word table)))
2316 (and sym (symbolp sym))))
2318 ;;*---------------------------------------------------------------------*/
2319 ;;* flyspell-change-abbrev ... */
2320 ;;*---------------------------------------------------------------------*/
2321 (defun flyspell-change-abbrev (table old new)
2322 (set (abbrev-symbol old table) new))
2324 (provide 'flyspell)
2326 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2327 ;;; flyspell.el ends here