(flyspell-word): Revert last change.
[emacs.git] / lisp / textmodes / flyspell.el
blob1e7602cc31cb2d425948cc8304c6de4ad172651e
1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 3, 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 if ARG is positive,
461 otherwise turn it off.
463 Bindings:
464 \\[ispell-word]: correct words (using Ispell).
465 \\[flyspell-auto-correct-word]: automatically correct word.
466 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
467 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
469 Hooks:
470 This runs `flyspell-mode-hook' after flyspell is entered.
472 Remark:
473 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
474 valid. For instance, a personal dictionary can be used by
475 invoking `ispell-change-dictionary'.
477 Consider using the `ispell-parser' to check your text. For instance
478 consider adding:
479 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
480 in your .emacs file.
482 \\[flyspell-region] checks all words inside a region.
483 \\[flyspell-buffer] checks the whole buffer."
484 :lighter flyspell-mode-line-string
485 :keymap flyspell-mode-map
486 :group 'flyspell
487 (if flyspell-mode
488 (condition-case ()
489 (flyspell-mode-on)
490 (error (message "Enabling Flyspell mode gave an error")
491 (flyspell-mode -1)))
492 (flyspell-mode-off)))
494 ;;;###autoload
495 (defun turn-on-flyspell ()
496 "Unconditionally turn on Flyspell mode."
497 (flyspell-mode 1))
499 ;;;###autoload
500 (defun turn-off-flyspell ()
501 "Unconditionally turn off Flyspell mode."
502 (flyspell-mode -1))
504 (custom-add-option 'text-mode-hook 'turn-on-flyspell)
506 ;;*---------------------------------------------------------------------*/
507 ;;* flyspell-buffers ... */
508 ;;* ------------------------------------------------------------- */
509 ;;* For remembering buffers running flyspell */
510 ;;*---------------------------------------------------------------------*/
511 (defvar flyspell-buffers nil)
513 ;;*---------------------------------------------------------------------*/
514 ;;* flyspell-minibuffer-p ... */
515 ;;*---------------------------------------------------------------------*/
516 (defun flyspell-minibuffer-p (buffer)
517 "Is BUFFER a minibuffer?"
518 (let ((ws (get-buffer-window-list buffer t)))
519 (and (consp ws) (window-minibuffer-p (car ws)))))
521 ;;*---------------------------------------------------------------------*/
522 ;;* flyspell-accept-buffer-local-defs ... */
523 ;;*---------------------------------------------------------------------*/
524 (defvar flyspell-last-buffer nil
525 "The buffer in which the last flyspell operation took place.")
527 (defun flyspell-accept-buffer-local-defs (&optional force)
528 ;; When flyspell-word is used inside a loop (e.g. when processing
529 ;; flyspell-changes), the calls to `ispell-accept-buffer-local-defs' end
530 ;; up dwarfing everything else, so only do it when the buffer has changed.
531 (when (or force (not (eq flyspell-last-buffer (current-buffer))))
532 (setq flyspell-last-buffer (current-buffer))
533 ;; Strange problem: If buffer in current window has font-lock turned on,
534 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
535 ;; call will reset the buffer to the buffer in the current window.
536 ;; However, it only happens at startup (fix by Albert L. Ting).
537 (save-current-buffer
538 (ispell-accept-buffer-local-defs))
539 (unless (and (eq flyspell-dash-dictionary ispell-dictionary)
540 (eq flyspell-dash-local-dictionary ispell-local-dictionary))
541 ;; The dictionary has changed
542 (setq flyspell-dash-dictionary ispell-dictionary)
543 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
544 (setq flyspell-consider-dash-as-word-delimiter-flag
545 (member (or ispell-local-dictionary ispell-dictionary)
546 flyspell-dictionaries-that-consider-dash-as-word-delimiter)))))
548 (defun flyspell-hack-local-variables-hook ()
549 ;; When local variables are loaded, see if the dictionary context
550 ;; has changed.
551 (flyspell-accept-buffer-local-defs 'force))
553 (defun flyspell-kill-ispell-hook ()
554 (setq flyspell-last-buffer nil)
555 (dolist (buf (buffer-list))
556 (with-current-buffer buf
557 (kill-local-variable 'flyspell-word-cache-word))))
559 ;; Make sure we flush our caches when needed. Do it here rather than in
560 ;; flyspell-mode-on, since flyspell-region may be used without ever turning
561 ;; on flyspell-mode.
562 (add-hook 'ispell-kill-ispell-hook 'flyspell-kill-ispell-hook)
564 ;;*---------------------------------------------------------------------*/
565 ;;* flyspell-mode-on ... */
566 ;;*---------------------------------------------------------------------*/
567 (defun flyspell-mode-on ()
568 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
569 (ispell-maybe-find-aspell-dictionaries)
570 (setq ispell-highlight-face 'flyspell-incorrect)
571 ;; local dictionaries setup
572 (or ispell-local-dictionary ispell-dictionary
573 (if flyspell-default-dictionary
574 (ispell-change-dictionary flyspell-default-dictionary)))
575 ;; we have to force ispell to accept the local definition or
576 ;; otherwise it could be too late, the local dictionary may
577 ;; be forgotten!
578 ;; Pass the `force' argument for the case where flyspell was active already
579 ;; but the buffer's local-defs have been edited.
580 (flyspell-accept-buffer-local-defs 'force)
581 ;; we put the `flyspell-delayed' property on some commands
582 (flyspell-delay-commands)
583 ;; we put the `flyspell-deplacement' property on some commands
584 (flyspell-deplacement-commands)
585 ;; we bound flyspell action to post-command hook
586 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
587 ;; we bound flyspell action to pre-command hook
588 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
589 ;; we bound flyspell action to after-change hook
590 (add-hook 'after-change-functions 'flyspell-after-change-function nil t)
591 ;; we bound flyspell action to hack-local-variables-hook
592 (add-hook 'hack-local-variables-hook
593 (function flyspell-hack-local-variables-hook) t t)
594 ;; set flyspell-generic-check-word-predicate based on the major mode
595 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
596 (if mode-predicate
597 (setq flyspell-generic-check-word-predicate mode-predicate)))
598 ;; the welcome message
599 (if (and flyspell-issue-message-flag
600 flyspell-issue-welcome-flag
601 (interactive-p))
602 (let ((binding (where-is-internal 'flyspell-auto-correct-word
603 nil 'non-ascii)))
604 (message "%s"
605 (if binding
606 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
607 (key-description binding))
608 "Welcome to flyspell. Use Mouse-2 to correct words."))))
609 ;; we end with the flyspell hooks
610 (run-hooks 'flyspell-mode-hook))
612 ;;*---------------------------------------------------------------------*/
613 ;;* flyspell-delay-commands ... */
614 ;;*---------------------------------------------------------------------*/
615 (defun flyspell-delay-commands ()
616 "Install the standard set of Flyspell delayed commands."
617 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
618 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
620 ;;*---------------------------------------------------------------------*/
621 ;;* flyspell-delay-command ... */
622 ;;*---------------------------------------------------------------------*/
623 (defun flyspell-delay-command (command)
624 "Set COMMAND to be delayed, for Flyspell.
625 When flyspell `post-command-hook' is invoked because a delayed command
626 as been used the current word is not immediately checked.
627 It will be checked only after `flyspell-delay' seconds."
628 (interactive "SDelay Flyspell after Command: ")
629 (put command 'flyspell-delayed t))
631 ;;*---------------------------------------------------------------------*/
632 ;;* flyspell-deplacement-commands ... */
633 ;;*---------------------------------------------------------------------*/
634 (defun flyspell-deplacement-commands ()
635 "Install the standard set of Flyspell deplacement commands."
636 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
637 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
639 ;;*---------------------------------------------------------------------*/
640 ;;* flyspell-deplacement-command ... */
641 ;;*---------------------------------------------------------------------*/
642 (defun flyspell-deplacement-command (command)
643 "Set COMMAND that implement cursor movements, for Flyspell.
644 When flyspell `post-command-hook' is invoked because of a deplacement command
645 as been used the current word is checked only if the previous command was
646 not the very same deplacement command."
647 (interactive "SDeplacement Flyspell after Command: ")
648 (put command 'flyspell-deplacement t))
650 ;;*---------------------------------------------------------------------*/
651 ;;* flyspell-word-cache ... */
652 ;;*---------------------------------------------------------------------*/
653 (defvar flyspell-word-cache-start nil)
654 (defvar flyspell-word-cache-end nil)
655 (defvar flyspell-word-cache-word nil)
656 (defvar flyspell-word-cache-result '_)
657 (make-variable-buffer-local 'flyspell-word-cache-start)
658 (make-variable-buffer-local 'flyspell-word-cache-end)
659 (make-variable-buffer-local 'flyspell-word-cache-word)
660 (make-variable-buffer-local 'flyspell-word-cache-result)
662 ;;*---------------------------------------------------------------------*/
663 ;;* The flyspell pre-hook, store the current position. In the */
664 ;;* post command hook, we will check, if the word at this position */
665 ;;* has to be spell checked. */
666 ;;*---------------------------------------------------------------------*/
667 (defvar flyspell-pre-buffer nil)
668 (defvar flyspell-pre-point nil)
669 (defvar flyspell-pre-column nil)
670 (defvar flyspell-pre-pre-buffer nil)
671 (defvar flyspell-pre-pre-point nil)
673 ;;*---------------------------------------------------------------------*/
674 ;;* flyspell-previous-command ... */
675 ;;*---------------------------------------------------------------------*/
676 (defvar flyspell-previous-command nil
677 "The last interactive command checked by Flyspell.")
679 ;;*---------------------------------------------------------------------*/
680 ;;* flyspell-pre-command-hook ... */
681 ;;*---------------------------------------------------------------------*/
682 (defun flyspell-pre-command-hook ()
683 "Save the current buffer and point for Flyspell's post-command hook."
684 (interactive)
685 (setq flyspell-pre-buffer (current-buffer))
686 (setq flyspell-pre-point (point))
687 (setq flyspell-pre-column (current-column)))
689 ;;*---------------------------------------------------------------------*/
690 ;;* flyspell-mode-off ... */
691 ;;*---------------------------------------------------------------------*/
692 ;;;###autoload
693 (defun flyspell-mode-off ()
694 "Turn Flyspell mode off."
695 ;; we remove the hooks
696 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
697 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
698 (remove-hook 'after-change-functions 'flyspell-after-change-function t)
699 (remove-hook 'hack-local-variables-hook
700 (function flyspell-hack-local-variables-hook) t)
701 ;; we remove all the flyspell hilightings
702 (flyspell-delete-all-overlays)
703 ;; we have to erase pre cache variables
704 (setq flyspell-pre-buffer nil)
705 (setq flyspell-pre-point nil)
706 ;; we mark the mode as killed
707 (setq flyspell-mode nil))
709 ;;*---------------------------------------------------------------------*/
710 ;;* flyspell-check-pre-word-p ... */
711 ;;*---------------------------------------------------------------------*/
712 (defun flyspell-check-pre-word-p ()
713 "Return non-nil if we should check the word before point.
714 More precisely, it applies to the word that was before point
715 before the current command."
716 (cond
717 ((or (not (numberp flyspell-pre-point))
718 (not (bufferp flyspell-pre-buffer))
719 (not (buffer-live-p flyspell-pre-buffer)))
720 nil)
721 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
722 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
723 nil)
724 ((or (and (= flyspell-pre-point (- (point) 1))
725 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
726 (= flyspell-pre-point (point))
727 (= flyspell-pre-point (+ (point) 1)))
728 nil)
729 ((and (symbolp this-command)
730 (not executing-kbd-macro)
731 (or (get this-command 'flyspell-delayed)
732 (and (get this-command 'flyspell-deplacement)
733 (eq flyspell-previous-command this-command)))
734 (or (= (current-column) 0)
735 (= (current-column) flyspell-pre-column)
736 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
737 nil)
738 ((not (eq (current-buffer) flyspell-pre-buffer))
740 ((not (and (numberp flyspell-word-cache-start)
741 (numberp flyspell-word-cache-end)))
744 (or (< flyspell-pre-point flyspell-word-cache-start)
745 (> flyspell-pre-point flyspell-word-cache-end)))))
747 ;;*---------------------------------------------------------------------*/
748 ;;* The flyspell after-change-hook, store the change position. In */
749 ;;* the post command hook, we will check, if the word at this */
750 ;;* position has to be spell checked. */
751 ;;*---------------------------------------------------------------------*/
752 (defvar flyspell-changes nil)
753 (make-variable-buffer-local 'flyspell-changes)
755 ;;*---------------------------------------------------------------------*/
756 ;;* flyspell-after-change-function ... */
757 ;;*---------------------------------------------------------------------*/
758 (defun flyspell-after-change-function (start stop len)
759 "Save the current buffer and point for Flyspell's post-command hook."
760 (push (cons start stop) flyspell-changes))
762 ;;*---------------------------------------------------------------------*/
763 ;;* flyspell-check-changed-word-p ... */
764 ;;*---------------------------------------------------------------------*/
765 (defun flyspell-check-changed-word-p (start stop)
766 "Return t when the changed word has to be checked.
767 The answer depends of several criteria.
768 Mostly we check word delimiters."
769 (cond
770 ((and (memq (char-after start) '(?\n ? )) (> stop start))
772 ((not (numberp flyspell-pre-point))
774 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
775 nil)
776 ((let ((pos (point)))
777 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
778 nil)
780 t)))
782 ;;*---------------------------------------------------------------------*/
783 ;;* flyspell-check-word-p ... */
784 ;;*---------------------------------------------------------------------*/
785 (defun flyspell-check-word-p ()
786 "Return t when the word at `point' has to be checked.
787 The answer depends of several criteria.
788 Mostly we check word delimiters."
789 (cond
790 ((<= (- (point-max) 1) (point-min))
791 ;; the buffer is not filled enough
792 nil)
793 ((and (and (> (current-column) 0)
794 (not (eq (current-column) flyspell-pre-column)))
795 (save-excursion
796 (backward-char 1)
797 (and (looking-at (flyspell-get-not-casechars))
798 (or flyspell-consider-dash-as-word-delimiter-flag
799 (not (looking-at "-"))))))
800 ;; yes because we have reached or typed a word delimiter.
802 ((symbolp this-command)
803 (cond
804 ((get this-command 'flyspell-deplacement)
805 (not (eq flyspell-previous-command this-command)))
806 ((get this-command 'flyspell-delayed)
807 ;; the current command is not delayed, that
808 ;; is that we must check the word now
809 (and (not unread-command-events)
810 (sit-for flyspell-delay)))
811 (t t)))
812 (t t)))
814 ;;*---------------------------------------------------------------------*/
815 ;;* flyspell-debug-signal-no-check ... */
816 ;;*---------------------------------------------------------------------*/
817 (defun flyspell-debug-signal-no-check (msg obj)
818 (setq debug-on-error t)
819 (with-current-buffer (get-buffer-create "*flyspell-debug*")
820 (erase-buffer)
821 (insert "NO-CHECK:\n")
822 (insert (format " %S : %S\n" msg obj))))
824 ;;*---------------------------------------------------------------------*/
825 ;;* flyspell-debug-signal-pre-word-checked ... */
826 ;;*---------------------------------------------------------------------*/
827 (defun flyspell-debug-signal-pre-word-checked ()
828 (setq debug-on-error t)
829 (with-current-buffer (get-buffer-create "*flyspell-debug*")
830 (insert "PRE-WORD:\n")
831 (insert (format " pre-point : %S\n" flyspell-pre-point))
832 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
833 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
834 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
835 (goto-char (point-max))))
837 ;;*---------------------------------------------------------------------*/
838 ;;* flyspell-debug-signal-word-checked ... */
839 ;;*---------------------------------------------------------------------*/
840 (defun flyspell-debug-signal-word-checked ()
841 (setq debug-on-error t)
842 (let ((oldbuf (current-buffer))
843 (point (point)))
844 (with-current-buffer (get-buffer-create "*flyspell-debug*")
845 (insert "WORD:\n")
846 (insert (format " this-cmd : %S\n" this-command))
847 (insert (format " delayed : %S\n" (and (symbolp this-command)
848 (get this-command 'flyspell-delayed))))
849 (insert (format " point : %S\n" point))
850 (insert (format " prev-char : [%c] %S\n"
851 (with-current-buffer oldbuf
852 (let ((c (if (> (point) (point-min))
853 (save-excursion
854 (backward-char 1)
855 (char-after (point)))
856 ? )))
858 (with-current-buffer oldbuf
859 (let ((c (if (> (point) (point-min))
860 (save-excursion
861 (backward-char 1)
862 (and (and (looking-at (flyspell-get-not-casechars)) 1)
863 (and (or flyspell-consider-dash-as-word-delimiter-flag
864 (not (looking-at "\\-"))) 2))))))
865 c))))
866 (insert (format " because : %S\n"
867 (cond
868 ((not (and (symbolp this-command)
869 (get this-command 'flyspell-delayed)))
870 ;; the current command is not delayed, that
871 ;; is that we must check the word now
872 'not-delayed)
873 ((with-current-buffer oldbuf
874 (let ((c (if (> (point) (point-min))
875 (save-excursion
876 (backward-char 1)
877 (and (looking-at (flyspell-get-not-casechars))
878 (or flyspell-consider-dash-as-word-delimiter-flag
879 (not (looking-at "\\-"))))))))
881 ;; yes because we have reached or typed a word delimiter.
882 'separator)
883 ((not (integerp flyspell-delay))
884 ;; yes because the user had set up a no-delay configuration.
885 'no-delay)
887 'sit-for))))
888 (goto-char (point-max)))))
890 ;;*---------------------------------------------------------------------*/
891 ;;* flyspell-debug-signal-changed-checked ... */
892 ;;*---------------------------------------------------------------------*/
893 (defun flyspell-debug-signal-changed-checked ()
894 (setq debug-on-error t)
895 (let ((point (point)))
896 (with-current-buffer (get-buffer-create "*flyspell-debug*")
897 (insert "CHANGED WORD:\n")
898 (insert (format " point : %S\n" point))
899 (goto-char (point-max)))))
901 ;;*---------------------------------------------------------------------*/
902 ;;* flyspell-post-command-hook ... */
903 ;;* ------------------------------------------------------------- */
904 ;;* It is possible that we check several words: */
905 ;;* 1- the current word is checked if the predicate */
906 ;;* FLYSPELL-CHECK-WORD-P is true */
907 ;;* 2- the word that used to be the current word before the */
908 ;;* THIS-COMMAND is checked if: */
909 ;;* a- the previous word is different from the current word */
910 ;;* b- the previous word as not just been checked by the */
911 ;;* previous FLYSPELL-POST-COMMAND-HOOK */
912 ;;* 3- the words changed by the THIS-COMMAND that are neither the */
913 ;;* previous word nor the current word */
914 ;;*---------------------------------------------------------------------*/
915 (defun flyspell-post-command-hook ()
916 "The `post-command-hook' used by flyspell to check a word in-the-fly."
917 (interactive)
918 (let ((command this-command)
919 ;; Prevent anything we do from affecting the mark.
920 deactivate-mark)
921 (if (flyspell-check-pre-word-p)
922 (with-current-buffer flyspell-pre-buffer
923 '(flyspell-debug-signal-pre-word-checked)
924 (save-excursion
925 (goto-char flyspell-pre-point)
926 (flyspell-word))))
927 (if (flyspell-check-word-p)
928 (progn
929 '(flyspell-debug-signal-word-checked)
930 (flyspell-word)
931 ;; we remember which word we have just checked.
932 ;; this will be used next time we will check a word
933 ;; to compare the next current word with the word
934 ;; that as been registered in the pre-command-hook
935 ;; that is these variables are used within the predicate
936 ;; FLYSPELL-CHECK-PRE-WORD-P
937 (setq flyspell-pre-pre-buffer (current-buffer))
938 (setq flyspell-pre-pre-point (point)))
939 (progn
940 (setq flyspell-pre-pre-buffer nil)
941 (setq flyspell-pre-pre-point nil)
942 ;; when a word is not checked because of a delayed command
943 ;; we do not disable the ispell cache.
944 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
945 (progn
946 (setq flyspell-word-cache-end -1)
947 (setq flyspell-word-cache-result '_)))))
948 (while (and (not (input-pending-p)) (consp flyspell-changes))
949 (let ((start (car (car flyspell-changes)))
950 (stop (cdr (car flyspell-changes))))
951 (if (flyspell-check-changed-word-p start stop)
952 (save-excursion
953 '(flyspell-debug-signal-changed-checked)
954 (goto-char start)
955 (flyspell-word)))
956 (setq flyspell-changes (cdr flyspell-changes))))
957 (setq flyspell-previous-command command)))
959 ;;*---------------------------------------------------------------------*/
960 ;;* flyspell-notify-misspell ... */
961 ;;*---------------------------------------------------------------------*/
962 (defun flyspell-notify-misspell (word poss)
963 (let ((replacements (if (stringp poss)
964 poss
965 (if flyspell-sort-corrections
966 (sort (car (cdr (cdr poss))) 'string<)
967 (car (cdr (cdr poss)))))))
968 (if flyspell-issue-message-flag
969 (message "misspelling `%s' %S" word replacements))))
971 ;;*---------------------------------------------------------------------*/
972 ;;* flyspell-word-search-backward ... */
973 ;;*---------------------------------------------------------------------*/
974 (defun flyspell-word-search-backward (word bound)
975 (save-excursion
976 (let ((r '())
977 (inhibit-point-motion-hooks t)
979 (while (and (not r) (setq p (search-backward word bound t)))
980 (let ((lw (flyspell-get-word '())))
981 (if (and (consp lw) (string-equal (car lw) word))
982 (setq r p)
983 (goto-char p))))
984 r)))
986 ;;*---------------------------------------------------------------------*/
987 ;;* flyspell-word-search-forward ... */
988 ;;*---------------------------------------------------------------------*/
989 (defun flyspell-word-search-forward (word bound)
990 (save-excursion
991 (let ((r '())
992 (inhibit-point-motion-hooks t)
994 (while (and (not r) (setq p (search-forward word bound t)))
995 (let ((lw (flyspell-get-word '())))
996 (if (and (consp lw) (string-equal (car lw) word))
997 (setq r p)
998 (goto-char (1+ p)))))
999 r)))
1001 ;;*---------------------------------------------------------------------*/
1002 ;;* flyspell-word ... */
1003 ;;*---------------------------------------------------------------------*/
1004 (defun flyspell-word (&optional following)
1005 "Spell check a word."
1006 (interactive (list ispell-following-word))
1007 (save-excursion
1008 ;; use the correct dictionary
1009 (flyspell-accept-buffer-local-defs)
1010 (let* ((cursor-location (point))
1011 (flyspell-word (flyspell-get-word following))
1012 start end poss word ispell-filter)
1013 (if (or (eq flyspell-word nil)
1014 (and (fboundp flyspell-generic-check-word-predicate)
1015 (not (funcall flyspell-generic-check-word-predicate))))
1017 (progn
1018 ;; destructure return flyspell-word info list.
1019 (setq start (car (cdr flyspell-word))
1020 end (car (cdr (cdr flyspell-word)))
1021 word (car flyspell-word))
1022 ;; before checking in the directory, we check for doublons.
1023 (cond
1024 ((and (or (not (eq ispell-parser 'tex))
1025 (and (> start (point-min))
1026 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1027 flyspell-mark-duplications-flag
1028 (save-excursion
1029 (goto-char start)
1030 (let* ((bound
1031 (- start
1032 (- end start)
1033 (- (skip-chars-backward " \t\n\f"))))
1034 (p (when (>= bound (point-min))
1035 (flyspell-word-search-backward word bound))))
1036 (and p (/= p start)))))
1037 ;; yes, this is a doublon
1038 (flyspell-highlight-incorrect-region start end 'doublon)
1039 nil)
1040 ((and (eq flyspell-word-cache-start start)
1041 (eq flyspell-word-cache-end end)
1042 (string-equal flyspell-word-cache-word word))
1043 ;; this word had been already checked, we skip
1044 flyspell-word-cache-result)
1045 ((and (eq ispell-parser 'tex)
1046 (flyspell-tex-command-p flyspell-word))
1047 ;; this is a correct word (because a tex command)
1048 (flyspell-unhighlight-at start)
1049 (if (> end start)
1050 (flyspell-unhighlight-at (- end 1)))
1053 ;; we setup the cache
1054 (setq flyspell-word-cache-start start)
1055 (setq flyspell-word-cache-end end)
1056 (setq flyspell-word-cache-word word)
1057 ;; now check spelling of word.
1058 (ispell-send-string "%\n")
1059 ;; put in verbose mode
1060 (ispell-send-string (concat "^" word "\n"))
1061 ;; we mark the ispell process so it can be killed
1062 ;; when emacs is exited without query
1063 (set-process-query-on-exit-flag ispell-process nil)
1064 ;; Wait until ispell has processed word. Since this code is often
1065 ;; executed from post-command-hook but the ispell process may not
1066 ;; be responsive, it's important to make sure we re-enable C-g.
1067 (with-local-quit
1068 (while (progn
1069 (accept-process-output ispell-process)
1070 (not (string= "" (car ispell-filter))))))
1071 ;; (ispell-send-string "!\n")
1072 ;; back to terse mode.
1073 ;; Remove leading empty element
1074 (setq ispell-filter (cdr ispell-filter))
1075 ;; ispell process should return something after word is sent.
1076 ;; Tag word as valid (i.e., skip) otherwise
1077 (or ispell-filter
1078 (setq ispell-filter '(*)))
1079 (if (consp ispell-filter)
1080 (setq poss (ispell-parse-output (car ispell-filter))))
1081 (let ((res (cond ((eq poss t)
1082 ;; correct
1083 (setq flyspell-word-cache-result t)
1084 (flyspell-unhighlight-at start)
1085 (if (> end start)
1086 (flyspell-unhighlight-at (- end 1)))
1088 ((and (stringp poss) flyspell-highlight-flag)
1089 ;; correct
1090 (setq flyspell-word-cache-result t)
1091 (flyspell-unhighlight-at start)
1092 (if (> end start)
1093 (flyspell-unhighlight-at (- end 1)))
1095 ((null poss)
1096 (setq flyspell-word-cache-result t)
1097 (flyspell-unhighlight-at start)
1098 (if (> end start)
1099 (flyspell-unhighlight-at (- end 1)))
1101 ((or (and (< flyspell-duplicate-distance 0)
1102 (or (save-excursion
1103 (goto-char start)
1104 (flyspell-word-search-backward
1105 word
1106 (point-min)))
1107 (save-excursion
1108 (goto-char end)
1109 (flyspell-word-search-forward
1110 word
1111 (point-max)))))
1112 (and (> flyspell-duplicate-distance 0)
1113 (or (save-excursion
1114 (goto-char start)
1115 (flyspell-word-search-backward
1116 word
1117 (- start
1118 flyspell-duplicate-distance)))
1119 (save-excursion
1120 (goto-char end)
1121 (flyspell-word-search-forward
1122 word
1123 (+ end
1124 flyspell-duplicate-distance))))))
1125 ;; This is a misspelled word which occurs
1126 ;; twice within flyspell-duplicate-distance.
1127 (setq flyspell-word-cache-result nil)
1128 (if flyspell-highlight-flag
1129 (flyspell-highlight-duplicate-region
1130 start end poss)
1131 (message "duplicate `%s'" word))
1132 nil)
1134 (setq flyspell-word-cache-result nil)
1135 ;; incorrect highlight the location
1136 (if flyspell-highlight-flag
1137 (flyspell-highlight-incorrect-region
1138 start end poss)
1139 (flyspell-notify-misspell word poss))
1140 nil))))
1141 ;; return to original location
1142 (goto-char cursor-location)
1143 (if ispell-quit (setq ispell-quit nil))
1144 res))))))))
1146 ;;*---------------------------------------------------------------------*/
1147 ;;* flyspell-tex-math-initialized ... */
1148 ;;*---------------------------------------------------------------------*/
1149 (defvar flyspell-tex-math-initialized nil)
1151 ;;*---------------------------------------------------------------------*/
1152 ;;* flyspell-math-tex-command-p ... */
1153 ;;* ------------------------------------------------------------- */
1154 ;;* This function uses the texmathp package to check if (point) */
1155 ;;* is within a tex command. In order to avoid using */
1156 ;;* condition-case each time we use the variable */
1157 ;;* flyspell-tex-math-initialized to make a special case the first */
1158 ;;* time that function is called. */
1159 ;;*---------------------------------------------------------------------*/
1160 (defun flyspell-math-tex-command-p ()
1161 (when (fboundp 'texmathp)
1162 (cond
1163 (flyspell-check-tex-math-command
1164 nil)
1165 ((eq flyspell-tex-math-initialized t)
1166 (texmathp))
1167 ((eq flyspell-tex-math-initialized 'error)
1168 nil)
1170 (setq flyspell-tex-math-initialized t)
1171 (condition-case nil
1172 (texmathp)
1173 (error (progn
1174 (setq flyspell-tex-math-initialized 'error)
1175 nil)))))))
1177 ;;*---------------------------------------------------------------------*/
1178 ;;* flyspell-tex-command-p ... */
1179 ;;*---------------------------------------------------------------------*/
1180 (defun flyspell-tex-command-p (word)
1181 "Return t if WORD is a TeX command."
1182 (or (save-excursion
1183 (let ((b (car (cdr word))))
1184 (and (re-search-backward "\\\\" (- (point) 100) t)
1185 (or (= (match-end 0) b)
1186 (and (goto-char (match-end 0))
1187 (looking-at flyspell-tex-command-regexp)
1188 (>= (match-end 0) b))))))
1189 (flyspell-math-tex-command-p)))
1191 ;;*---------------------------------------------------------------------*/
1192 ;;* flyspell-casechars-cache ... */
1193 ;;*---------------------------------------------------------------------*/
1194 (defvar flyspell-casechars-cache nil)
1195 (defvar flyspell-ispell-casechars-cache nil)
1196 (make-variable-buffer-local 'flyspell-casechars-cache)
1197 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1199 ;;*---------------------------------------------------------------------*/
1200 ;;* flyspell-get-casechars ... */
1201 ;;*---------------------------------------------------------------------*/
1202 (defun flyspell-get-casechars ()
1203 "This function builds a string that is the regexp of word chars.
1204 In order to avoid one useless string construction,
1205 this function changes the last char of the `ispell-casechars' string."
1206 (let ((ispell-casechars (ispell-get-casechars)))
1207 (cond
1208 ((eq ispell-parser 'tex)
1209 (setq flyspell-ispell-casechars-cache ispell-casechars)
1210 (setq flyspell-casechars-cache
1211 (concat (substring ispell-casechars
1213 (- (length ispell-casechars) 1))
1214 "]"))
1215 flyspell-casechars-cache)
1217 (setq flyspell-ispell-casechars-cache ispell-casechars)
1218 (setq flyspell-casechars-cache ispell-casechars)
1219 flyspell-casechars-cache))))
1221 ;;*---------------------------------------------------------------------*/
1222 ;;* flyspell-get-not-casechars-cache ... */
1223 ;;*---------------------------------------------------------------------*/
1224 (defvar flyspell-not-casechars-cache nil)
1225 (defvar flyspell-ispell-not-casechars-cache nil)
1226 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1227 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1229 ;;*---------------------------------------------------------------------*/
1230 ;;* flyspell-get-not-casechars ... */
1231 ;;*---------------------------------------------------------------------*/
1232 (defun flyspell-get-not-casechars ()
1233 "This function builds a string that is the regexp of non-word chars."
1234 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1235 (cond
1236 ((eq ispell-parser 'tex)
1237 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1238 (setq flyspell-not-casechars-cache
1239 (concat (substring ispell-not-casechars
1241 (- (length ispell-not-casechars) 1))
1242 "]"))
1243 flyspell-not-casechars-cache)
1245 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1246 (setq flyspell-not-casechars-cache ispell-not-casechars)
1247 flyspell-not-casechars-cache))))
1249 ;;*---------------------------------------------------------------------*/
1250 ;;* flyspell-get-word ... */
1251 ;;*---------------------------------------------------------------------*/
1252 (defun flyspell-get-word (following &optional extra-otherchars)
1253 "Return the word for spell-checking according to Ispell syntax.
1254 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1255 is non-nil when called interactively, then the following word
1256 \(rather than preceding\) is checked when the cursor is not over a word.
1257 Optional second argument contains otherchars that can be included in word
1258 many times.
1260 Word syntax described by `flyspell-dictionary-alist' (which see)."
1261 (let* ((flyspell-casechars (flyspell-get-casechars))
1262 (flyspell-not-casechars (flyspell-get-not-casechars))
1263 (ispell-otherchars (ispell-get-otherchars))
1264 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1265 (word-regexp (concat flyspell-casechars
1266 "+\\("
1267 (if (not (string= "" ispell-otherchars))
1268 (concat ispell-otherchars "?"))
1269 (if extra-otherchars
1270 (concat extra-otherchars "?"))
1271 flyspell-casechars
1272 "+\\)"
1273 (if (or ispell-many-otherchars-p
1274 extra-otherchars)
1275 "*" "?")))
1276 did-it-once prevpt
1277 start end word)
1278 ;; find the word
1279 (if (not (looking-at flyspell-casechars))
1280 (if following
1281 (re-search-forward flyspell-casechars nil t)
1282 (re-search-backward flyspell-casechars nil t)))
1283 ;; move to front of word
1284 (re-search-backward flyspell-not-casechars nil 'start)
1285 (while (and (or (and (not (string= "" ispell-otherchars))
1286 (looking-at ispell-otherchars))
1287 (and extra-otherchars (looking-at extra-otherchars)))
1288 (not (bobp))
1289 (or (not did-it-once)
1290 ispell-many-otherchars-p)
1291 (not (eq prevpt (point))))
1292 (if (and extra-otherchars (looking-at extra-otherchars))
1293 (progn
1294 (backward-char 1)
1295 (if (looking-at flyspell-casechars)
1296 (re-search-backward flyspell-not-casechars nil 'move)))
1297 (setq did-it-once t
1298 prevpt (point))
1299 (backward-char 1)
1300 (if (looking-at flyspell-casechars)
1301 (re-search-backward flyspell-not-casechars nil 'move)
1302 (backward-char -1))))
1303 ;; Now mark the word and save to string.
1304 (if (not (re-search-forward word-regexp nil t))
1306 (progn
1307 (setq start (match-beginning 0)
1308 end (point)
1309 word (buffer-substring-no-properties start end))
1310 (list word start end)))))
1312 ;;*---------------------------------------------------------------------*/
1313 ;;* flyspell-small-region ... */
1314 ;;*---------------------------------------------------------------------*/
1315 (defun flyspell-small-region (beg end)
1316 "Flyspell text between BEG and END."
1317 (save-excursion
1318 (if (> beg end)
1319 (let ((old beg))
1320 (setq beg end)
1321 (setq end old)))
1322 (goto-char beg)
1323 (let ((count 0))
1324 (while (< (point) end)
1325 (if (and flyspell-issue-message-flag (= count 100))
1326 (progn
1327 (message "Spell Checking...%d%%"
1328 (* 100 (/ (float (- (point) beg)) (- end beg))))
1329 (setq count 0))
1330 (setq count (+ 1 count)))
1331 (flyspell-word)
1332 (sit-for 0)
1333 (let ((cur (point)))
1334 (forward-word 1)
1335 (if (and (< (point) end) (> (point) (+ cur 1)))
1336 (backward-char 1)))))
1337 (backward-char 1)
1338 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1339 (flyspell-word)))
1341 ;;*---------------------------------------------------------------------*/
1342 ;;* flyspell-external-ispell-process ... */
1343 ;;*---------------------------------------------------------------------*/
1344 (defvar flyspell-external-ispell-process '()
1345 "The external Flyspell Ispell process.")
1347 ;;*---------------------------------------------------------------------*/
1348 ;;* flyspell-external-ispell-buffer ... */
1349 ;;*---------------------------------------------------------------------*/
1350 (defvar flyspell-external-ispell-buffer '())
1351 (defvar flyspell-large-region-buffer '())
1352 (defvar flyspell-large-region-beg (point-min))
1353 (defvar flyspell-large-region-end (point-max))
1355 ;;*---------------------------------------------------------------------*/
1356 ;;* flyspell-external-point-words ... */
1357 ;;*---------------------------------------------------------------------*/
1358 (defun flyspell-external-point-words ()
1359 "Mark words from a buffer listing incorrect words in order of appearance.
1360 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1361 \(We finish by killing that buffer and setting the variable to nil.)
1362 The buffer to mark them in is `flyspell-large-region-buffer'."
1363 (let (words-not-found
1364 (ispell-otherchars (ispell-get-otherchars))
1365 (buffer-scan-pos flyspell-large-region-beg)
1366 case-fold-search)
1367 (with-current-buffer flyspell-external-ispell-buffer
1368 (goto-char (point-min))
1369 ;; Loop over incorrect words, in the order they were reported,
1370 ;; which is also the order they appear in the buffer being checked.
1371 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1372 ;; Bind WORD to the next one.
1373 (let ((word (match-string 1)) (wordpos (point)))
1374 ;; Here there used to be code to see if WORD is the same
1375 ;; as the previous iteration, and count the number of consecutive
1376 ;; identical words, and the loop below would search for that many.
1377 ;; That code seemed to be incorrect, and on principle, should
1378 ;; be unnecessary too. -- rms.
1379 (if flyspell-issue-message-flag
1380 (message "Spell Checking...%d%% [%s]"
1381 (* 100 (/ (float (point)) (point-max)))
1382 word))
1383 (with-current-buffer flyspell-large-region-buffer
1384 (goto-char buffer-scan-pos)
1385 (let ((keep t))
1386 ;; Iterate on string search until string is found as word,
1387 ;; not as substring
1388 (while keep
1389 (if (search-forward word
1390 flyspell-large-region-end t)
1391 (let* ((found-list
1392 (save-excursion
1393 ;; Move back into the match
1394 ;; so flyspell-get-word will find it.
1395 (forward-char -1)
1396 (flyspell-get-word nil)))
1397 (found (car found-list))
1398 (found-length (length found))
1399 (misspell-length (length word)))
1400 (when (or
1401 ;; Size matches, we really found it.
1402 (= found-length misspell-length)
1403 ;; Matches as part of a boundary-char separated word
1404 (member word
1405 (split-string found ispell-otherchars))
1406 ;; Misspelling has higher length than
1407 ;; what flyspell considers the
1408 ;; word. Caused by boundary-chars
1409 ;; mismatch. Validating seems safe.
1410 (< found-length misspell-length)
1411 ;; ispell treats beginning of some TeX
1412 ;; commands as nroff control sequences
1413 ;; and strips them in the list of
1414 ;; misspelled words thus giving a
1415 ;; non-existent word. Skip if ispell
1416 ;; is used, string is a TeX command
1417 ;; (char before beginning of word is
1418 ;; backslash) and none of the previous
1419 ;; contitions match
1420 (and (not ispell-really-aspell)
1421 (save-excursion
1422 (goto-char (- (nth 1 found-list) 1))
1423 (if (looking-at "[\\]" )
1425 nil))))
1426 (setq keep nil)
1427 (flyspell-word)
1428 ;; Search for next misspelled word will begin from
1429 ;; end of last validated match.
1430 (setq buffer-scan-pos (point))))
1431 ;; Record if misspelling is not found and try new one
1432 (add-to-list 'words-not-found
1433 (concat " -> " word " - "
1434 (int-to-string wordpos)))
1435 (setq keep nil)))))))
1436 ;; we are done
1437 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1438 ;; Warn about not found misspellings
1439 (dolist (word words-not-found)
1440 (message "%s: word not found" word))
1441 ;; Kill and forget the buffer with the list of incorrect words.
1442 (kill-buffer flyspell-external-ispell-buffer)
1443 (setq flyspell-external-ispell-buffer nil)))
1445 ;;*---------------------------------------------------------------------*/
1446 ;;* flyspell-process-localwords ... */
1447 ;;* ------------------------------------------------------------- */
1448 ;;* This function is used to prevent marking of words explicitly */
1449 ;;* declared correct. */
1450 ;;*---------------------------------------------------------------------*/
1451 (defun flyspell-process-localwords (misspellings-buffer)
1452 (let (localwords case-fold-search
1453 (ispell-casechars (ispell-get-casechars)))
1454 ;; Get localwords from the original buffer
1455 (save-excursion
1456 (goto-char (point-min))
1457 ;; Localwords parsing copied from ispell.el.
1458 (while (search-forward ispell-words-keyword nil t)
1459 (let ((end (save-excursion (end-of-line) (point)))
1460 string)
1461 ;; buffer-local words separated by a space, and can contain
1462 ;; any character other than a space. Not rigorous enough.
1463 (while (re-search-forward " *\\([^ ]+\\)" end t)
1464 (setq string (buffer-substring-no-properties (match-beginning 1)
1465 (match-end 1)))
1466 ;; This can fail when string contains a word with invalid chars.
1467 ;; Error handling needs to be added between Ispell and Emacs.
1468 (if (and (< 1 (length string))
1469 (equal 0 (string-match ispell-casechars string)))
1470 (push string localwords))))))
1471 ;; Remove localwords matches from misspellings-buffer.
1472 ;; The usual mechanism of communicating the local words to ispell
1473 ;; does not affect the special ispell process used by
1474 ;; flyspell-large-region.
1475 (with-current-buffer misspellings-buffer
1476 (save-excursion
1477 (dolist (word localwords)
1478 (goto-char (point-min))
1479 (let ((regexp (concat "^" word "\n")))
1480 (while (re-search-forward regexp nil t)
1481 (delete-region (match-beginning 0) (match-end 0)))))))))
1483 ;;* ---------------------------------------------------------------
1484 ;;* flyspell-check-region-doublons
1485 ;;* ---------------------------------------------------------------
1486 (defun flyspell-check-region-doublons (beg end)
1487 "Check for adjacent duplicated words (doublons) in the given region."
1488 (save-excursion
1489 (goto-char beg)
1490 (flyspell-word) ; Make sure current word is checked
1491 (backward-word 1)
1492 (while (and (< (point) end)
1493 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1494 end 'move))
1495 (flyspell-word)
1496 (backward-word 1))
1497 (flyspell-word)))
1499 ;;*---------------------------------------------------------------------*/
1500 ;;* flyspell-large-region ... */
1501 ;;*---------------------------------------------------------------------*/
1502 (defun flyspell-large-region (beg end)
1503 (let* ((curbuf (current-buffer))
1504 (buffer (get-buffer-create "*flyspell-region*")))
1505 (setq flyspell-external-ispell-buffer buffer)
1506 (setq flyspell-large-region-buffer curbuf)
1507 (setq flyspell-large-region-beg beg)
1508 (setq flyspell-large-region-end end)
1509 (flyspell-accept-buffer-local-defs)
1510 (set-buffer buffer)
1511 (erase-buffer)
1512 ;; this is done, we can start checking...
1513 (if flyspell-issue-message-flag (message "Checking region..."))
1514 (set-buffer curbuf)
1515 (ispell-maybe-find-aspell-dictionaries)
1516 (let ((c (apply 'ispell-call-process-region beg
1518 ispell-program-name
1520 buffer
1522 (if ispell-really-aspell "list" "-l")
1523 (let (args)
1524 ;; Local dictionary becomes the global dictionary in use.
1525 (if ispell-local-dictionary
1526 (setq ispell-dictionary ispell-local-dictionary))
1527 (setq args (ispell-get-ispell-args))
1528 (if ispell-dictionary ; use specified dictionary
1529 (setq args
1530 (append (list "-d" ispell-dictionary) args)))
1531 (if ispell-personal-dictionary ; use specified pers dict
1532 (setq args
1533 (append args
1534 (list "-p"
1535 (expand-file-name
1536 ispell-personal-dictionary)))))
1537 (setq args (append args ispell-extra-args))
1538 args))))
1539 (if (eq c 0)
1540 (progn
1541 (flyspell-process-localwords buffer)
1542 (with-current-buffer curbuf
1543 (flyspell-delete-region-overlays beg end)
1544 (flyspell-check-region-doublons beg end))
1545 (flyspell-external-point-words))
1546 (error "Can't check region...")))))
1548 ;;*---------------------------------------------------------------------*/
1549 ;;* flyspell-region ... */
1550 ;;* ------------------------------------------------------------- */
1551 ;;* Because `ispell -a' is too slow, it is not possible to use */
1552 ;;* it on large region. Then, when ispell is invoked on a large */
1553 ;;* text region, a new `ispell -l' process is spawned. The */
1554 ;;* pointed out words are then searched in the region a checked with */
1555 ;;* regular flyspell means. */
1556 ;;*---------------------------------------------------------------------*/
1557 ;;;###autoload
1558 (defun flyspell-region (beg end)
1559 "Flyspell text between BEG and END."
1560 (interactive "r")
1561 (ispell-maybe-find-aspell-dictionaries)
1562 (if (= beg end)
1564 (save-excursion
1565 (if (> beg end)
1566 (let ((old beg))
1567 (setq beg end)
1568 (setq end old)))
1569 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1570 (flyspell-large-region beg end)
1571 (flyspell-small-region beg end)))))
1573 ;;*---------------------------------------------------------------------*/
1574 ;;* flyspell-buffer ... */
1575 ;;*---------------------------------------------------------------------*/
1576 ;;;###autoload
1577 (defun flyspell-buffer ()
1578 "Flyspell whole buffer."
1579 (interactive)
1580 (flyspell-region (point-min) (point-max)))
1582 ;;*---------------------------------------------------------------------*/
1583 ;;* old next error position ... */
1584 ;;*---------------------------------------------------------------------*/
1585 (defvar flyspell-old-buffer-error nil)
1586 (defvar flyspell-old-pos-error nil)
1588 ;;*---------------------------------------------------------------------*/
1589 ;;* flyspell-goto-next-error ... */
1590 ;;*---------------------------------------------------------------------*/
1591 (defun flyspell-goto-next-error ()
1592 "Go to the next previously detected error.
1593 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1594 FLYSPELL-BUFFER."
1595 (interactive)
1596 (let ((pos (point))
1597 (max (point-max)))
1598 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1599 (eq pos flyspell-old-pos-error))
1600 (progn
1601 (if (= flyspell-old-pos-error max)
1602 ;; goto beginning of buffer
1603 (progn
1604 (message "Restarting from beginning of buffer")
1605 (goto-char (point-min)))
1606 (forward-word 1))
1607 (setq pos (point))))
1608 ;; seek the next error
1609 (while (and (< pos max)
1610 (let ((ovs (overlays-at pos))
1611 (r '()))
1612 (while (and (not r) (consp ovs))
1613 (if (flyspell-overlay-p (car ovs))
1614 (setq r t)
1615 (setq ovs (cdr ovs))))
1616 (not r)))
1617 (setq pos (1+ pos)))
1618 ;; save the current location for next invocation
1619 (setq flyspell-old-pos-error pos)
1620 (setq flyspell-old-buffer-error (current-buffer))
1621 (goto-char pos)
1622 (if (= pos max)
1623 (message "No more miss-spelled word!"))))
1625 ;;*---------------------------------------------------------------------*/
1626 ;;* flyspell-overlay-p ... */
1627 ;;*---------------------------------------------------------------------*/
1628 (defun flyspell-overlay-p (o)
1629 "Return true if O is an overlay used by flyspell."
1630 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1632 ;;*---------------------------------------------------------------------*/
1633 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1634 ;;* ------------------------------------------------------------- */
1635 ;;* Remove overlays introduced by flyspell. */
1636 ;;*---------------------------------------------------------------------*/
1637 (defun flyspell-delete-region-overlays (beg end)
1638 "Delete overlays used by flyspell in a given region."
1639 (remove-overlays beg end 'flyspell-overlay t))
1642 (defun flyspell-delete-all-overlays ()
1643 "Delete all the overlays used by flyspell."
1644 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1646 ;;*---------------------------------------------------------------------*/
1647 ;;* flyspell-unhighlight-at ... */
1648 ;;*---------------------------------------------------------------------*/
1649 (defun flyspell-unhighlight-at (pos)
1650 "Remove the flyspell overlay that are located at POS."
1651 (if flyspell-persistent-highlight
1652 (let ((overlays (overlays-at pos)))
1653 (while (consp overlays)
1654 (if (flyspell-overlay-p (car overlays))
1655 (delete-overlay (car overlays)))
1656 (setq overlays (cdr overlays))))
1657 (if (flyspell-overlay-p flyspell-overlay)
1658 (delete-overlay flyspell-overlay))))
1660 ;;*---------------------------------------------------------------------*/
1661 ;;* flyspell-properties-at-p ... */
1662 ;;* ------------------------------------------------------------- */
1663 ;;* Is there an highlight properties at position pos? */
1664 ;;*---------------------------------------------------------------------*/
1665 (defun flyspell-properties-at-p (pos)
1666 "Return t if there is a text property at POS, not counting `local-map'.
1667 If variable `flyspell-highlight-properties' is set to nil,
1668 text with properties are not checked. This function is used to discover
1669 if the character at POS has any other property."
1670 (let ((prop (text-properties-at pos))
1671 (keep t))
1672 (while (and keep (consp prop))
1673 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1674 (setq prop (cdr (cdr prop)))
1675 (setq keep nil)))
1676 (consp prop)))
1678 ;;*---------------------------------------------------------------------*/
1679 ;;* make-flyspell-overlay ... */
1680 ;;*---------------------------------------------------------------------*/
1681 (defun make-flyspell-overlay (beg end face mouse-face)
1682 "Allocate an overlay to highlight an incorrect word.
1683 BEG and END specify the range in the buffer of that word.
1684 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1685 for the overlay."
1686 (let ((overlay (make-overlay beg end nil t nil)))
1687 (overlay-put overlay 'face face)
1688 (overlay-put overlay 'mouse-face mouse-face)
1689 (overlay-put overlay 'flyspell-overlay t)
1690 (overlay-put overlay 'evaporate t)
1691 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1692 (overlay-put overlay 'keymap flyspell-mouse-map)
1693 (when (eq face 'flyspell-incorrect)
1694 (and (stringp flyspell-before-incorrect-word-string)
1695 (overlay-put overlay 'before-string
1696 flyspell-before-incorrect-word-string))
1697 (and (stringp flyspell-after-incorrect-word-string)
1698 (overlay-put overlay 'after-string
1699 flyspell-after-incorrect-word-string)))
1700 overlay))
1702 ;;*---------------------------------------------------------------------*/
1703 ;;* flyspell-highlight-incorrect-region ... */
1704 ;;*---------------------------------------------------------------------*/
1705 (defun flyspell-highlight-incorrect-region (beg end poss)
1706 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1707 POSS is usually a list of possible spelling/correction lists,
1708 as returned by `ispell-parse-output'.
1709 It can also be the symbol `doublon', in the case where the word
1710 is itself incorrect, but suspiciously repeated."
1711 (let ((inhibit-read-only t))
1712 (unless (run-hook-with-args-until-success
1713 'flyspell-incorrect-hook beg end poss)
1714 (if (or flyspell-highlight-properties
1715 (not (flyspell-properties-at-p beg)))
1716 (progn
1717 ;; we cleanup all the overlay that are in the region, not
1718 ;; beginning at the word start position
1719 (if (< (1+ beg) end)
1720 (let ((os (overlays-in (1+ beg) end)))
1721 (while (consp os)
1722 (if (flyspell-overlay-p (car os))
1723 (delete-overlay (car os)))
1724 (setq os (cdr os)))))
1725 ;; we cleanup current overlay at the same position
1726 (flyspell-unhighlight-at beg)
1727 ;; now we can use a new overlay
1728 (setq flyspell-overlay
1729 (make-flyspell-overlay
1730 beg end
1731 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1732 'highlight)))))))
1734 ;;*---------------------------------------------------------------------*/
1735 ;;* flyspell-highlight-duplicate-region ... */
1736 ;;*---------------------------------------------------------------------*/
1737 (defun flyspell-highlight-duplicate-region (beg end poss)
1738 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1739 POSS is a list of possible spelling/correction lists,
1740 as returned by `ispell-parse-output'."
1741 (let ((inhibit-read-only t))
1742 (unless (run-hook-with-args-until-success
1743 'flyspell-incorrect-hook beg end poss)
1744 (if (or flyspell-highlight-properties
1745 (not (flyspell-properties-at-p beg)))
1746 (progn
1747 ;; we cleanup current overlay at the same position
1748 (flyspell-unhighlight-at beg)
1749 ;; now we can use a new overlay
1750 (setq flyspell-overlay
1751 (make-flyspell-overlay beg end
1752 'flyspell-duplicate
1753 'highlight)))))))
1755 ;;*---------------------------------------------------------------------*/
1756 ;;* flyspell-auto-correct-cache ... */
1757 ;;*---------------------------------------------------------------------*/
1758 (defvar flyspell-auto-correct-pos nil)
1759 (defvar flyspell-auto-correct-region nil)
1760 (defvar flyspell-auto-correct-ring nil)
1761 (defvar flyspell-auto-correct-word nil)
1762 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1763 (make-variable-buffer-local 'flyspell-auto-correct-region)
1764 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1765 (make-variable-buffer-local 'flyspell-auto-correct-word)
1767 ;;*---------------------------------------------------------------------*/
1768 ;;* flyspell-check-previous-highlighted-word ... */
1769 ;;*---------------------------------------------------------------------*/
1770 (defun flyspell-check-previous-highlighted-word (&optional arg)
1771 "Correct the closer misspelled word.
1772 This function scans a mis-spelled word before the cursor. If it finds one
1773 it proposes replacement for that word. With prefix arg, count that many
1774 misspelled words backwards."
1775 (interactive)
1776 (let ((pos1 (point))
1777 (pos (point))
1778 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1779 ov ovs)
1780 (if (catch 'exit
1781 (while (and (setq pos (previous-overlay-change pos))
1782 (not (= pos pos1)))
1783 (setq pos1 pos)
1784 (if (> pos (point-min))
1785 (progn
1786 (setq ovs (overlays-at (1- pos)))
1787 (while (consp ovs)
1788 (setq ov (car ovs))
1789 (setq ovs (cdr ovs))
1790 (if (and (flyspell-overlay-p ov)
1791 (= 0 (setq arg (1- arg))))
1792 (throw 'exit t)))))))
1793 (save-excursion
1794 (goto-char pos)
1795 (ispell-word))
1796 (error "No word to correct before point"))))
1798 ;;*---------------------------------------------------------------------*/
1799 ;;* flyspell-display-next-corrections ... */
1800 ;;*---------------------------------------------------------------------*/
1801 (defun flyspell-display-next-corrections (corrections)
1802 (let ((string "Corrections:")
1803 (l corrections)
1804 (pos '()))
1805 (while (< (length string) 80)
1806 (if (equal (car l) flyspell-auto-correct-word)
1807 (setq pos (cons (+ 1 (length string)) pos)))
1808 (setq string (concat string " " (car l)))
1809 (setq l (cdr l)))
1810 (while (consp pos)
1811 (let ((num (car pos)))
1812 (put-text-property num
1813 (+ num (length flyspell-auto-correct-word))
1814 'face 'flyspell-incorrect
1815 string))
1816 (setq pos (cdr pos)))
1817 (if (fboundp 'display-message)
1818 (display-message 'no-log string)
1819 (message "%s" string))))
1821 ;;*---------------------------------------------------------------------*/
1822 ;;* flyspell-abbrev-table ... */
1823 ;;*---------------------------------------------------------------------*/
1824 (defun flyspell-abbrev-table ()
1825 (if flyspell-use-global-abbrev-table-p
1826 global-abbrev-table
1827 (or local-abbrev-table global-abbrev-table)))
1829 ;;*---------------------------------------------------------------------*/
1830 ;;* flyspell-define-abbrev ... */
1831 ;;*---------------------------------------------------------------------*/
1832 (defun flyspell-define-abbrev (name expansion)
1833 (let ((table (flyspell-abbrev-table)))
1834 (when table
1835 (define-abbrev table (downcase name) expansion))))
1837 ;;*---------------------------------------------------------------------*/
1838 ;;* flyspell-auto-correct-word ... */
1839 ;;*---------------------------------------------------------------------*/
1840 (defun flyspell-auto-correct-word ()
1841 "Correct the current word.
1842 This command proposes various successive corrections for the current word."
1843 (interactive)
1844 (let ((pos (point))
1845 (old-max (point-max)))
1846 ;; use the correct dictionary
1847 (flyspell-accept-buffer-local-defs)
1848 (if (and (eq flyspell-auto-correct-pos pos)
1849 (consp flyspell-auto-correct-region))
1850 ;; we have already been using the function at the same location
1851 (let* ((start (car flyspell-auto-correct-region))
1852 (len (cdr flyspell-auto-correct-region)))
1853 (flyspell-unhighlight-at start)
1854 (delete-region start (+ start len))
1855 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1856 (let* ((word (car flyspell-auto-correct-ring))
1857 (len (length word)))
1858 (rplacd flyspell-auto-correct-region len)
1859 (goto-char start)
1860 (if flyspell-abbrev-p
1861 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1862 flyspell-auto-correct-word)
1863 (flyspell-change-abbrev (flyspell-abbrev-table)
1864 flyspell-auto-correct-word
1865 word)
1866 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1867 (funcall flyspell-insert-function word)
1868 (flyspell-word)
1869 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1870 (flyspell-ajust-cursor-point pos (point) old-max)
1871 (setq flyspell-auto-correct-pos (point)))
1872 ;; fetch the word to be checked
1873 (let ((word (flyspell-get-word nil)))
1874 (if (consp word)
1875 (let ((start (car (cdr word)))
1876 (end (car (cdr (cdr word))))
1877 (word (car word))
1878 poss ispell-filter)
1879 (setq flyspell-auto-correct-word word)
1880 ;; now check spelling of word.
1881 (ispell-send-string "%\n") ;put in verbose mode
1882 (ispell-send-string (concat "^" word "\n"))
1883 ;; wait until ispell has processed word.
1884 (while (progn
1885 (accept-process-output ispell-process)
1886 (not (string= "" (car ispell-filter)))))
1887 ;; Remove leading empty element
1888 (setq ispell-filter (cdr ispell-filter))
1889 ;; ispell process should return something after word is sent.
1890 ;; Tag word as valid (i.e., skip) otherwise
1891 (or ispell-filter
1892 (setq ispell-filter '(*)))
1893 (if (consp ispell-filter)
1894 (setq poss (ispell-parse-output (car ispell-filter))))
1895 (cond
1896 ((or (eq poss t) (stringp poss))
1897 ;; don't correct word
1899 ((null poss)
1900 ;; ispell error
1901 (error "Ispell: error in Ispell process"))
1903 ;; the word is incorrect, we have to propose a replacement
1904 (let ((replacements (if flyspell-sort-corrections
1905 (sort (car (cdr (cdr poss))) 'string<)
1906 (car (cdr (cdr poss))))))
1907 (setq flyspell-auto-correct-region nil)
1908 (if (consp replacements)
1909 (progn
1910 (let ((replace (car replacements)))
1911 (let ((new-word replace))
1912 (if (not (equal new-word (car poss)))
1913 (progn
1914 ;; the save the current replacements
1915 (setq flyspell-auto-correct-region
1916 (cons start (length new-word)))
1917 (let ((l replacements))
1918 (while (consp (cdr l))
1919 (setq l (cdr l)))
1920 (rplacd l (cons (car poss) replacements)))
1921 (setq flyspell-auto-correct-ring
1922 replacements)
1923 (flyspell-unhighlight-at start)
1924 (delete-region start end)
1925 (funcall flyspell-insert-function new-word)
1926 (if flyspell-abbrev-p
1927 (if (flyspell-already-abbrevp
1928 (flyspell-abbrev-table) word)
1929 (flyspell-change-abbrev
1930 (flyspell-abbrev-table)
1931 word
1932 new-word)
1933 (flyspell-define-abbrev word
1934 new-word)))
1935 (flyspell-word)
1936 (flyspell-display-next-corrections
1937 (cons new-word flyspell-auto-correct-ring))
1938 (flyspell-ajust-cursor-point pos
1939 (point)
1940 old-max))))))))))
1941 (setq flyspell-auto-correct-pos (point))
1942 (ispell-pdict-save t)))))))
1944 ;;*---------------------------------------------------------------------*/
1945 ;;* flyspell-auto-correct-previous-pos ... */
1946 ;;*---------------------------------------------------------------------*/
1947 (defvar flyspell-auto-correct-previous-pos nil
1948 "Holds the start of the first incorrect word before point.")
1950 ;;*---------------------------------------------------------------------*/
1951 ;;* flyspell-auto-correct-previous-hook ... */
1952 ;;*---------------------------------------------------------------------*/
1953 (defun flyspell-auto-correct-previous-hook ()
1954 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1955 Sets `flyspell-auto-correct-previous-pos' to nil"
1956 (interactive)
1957 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1958 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1959 (setq flyspell-auto-correct-previous-pos nil)))
1961 ;;*---------------------------------------------------------------------*/
1962 ;;* flyspell-auto-correct-previous-word ... */
1963 ;;*---------------------------------------------------------------------*/
1964 (defun flyspell-auto-correct-previous-word (position)
1965 "Auto correct the first mispelled word that occurs before point.
1966 But don't look beyond what's visible on the screen."
1967 (interactive "d")
1969 (let ((top (window-start))
1970 (bot (window-end)))
1971 (save-excursion
1972 (save-restriction
1973 (narrow-to-region top bot)
1974 (overlay-recenter (point))
1976 (add-hook 'pre-command-hook
1977 (function flyspell-auto-correct-previous-hook) t t)
1979 (unless flyspell-auto-correct-previous-pos
1980 ;; only reset if a new overlay exists
1981 (setq flyspell-auto-correct-previous-pos nil)
1983 (let ((overlay-list (overlays-in (point-min) position))
1984 (new-overlay 'dummy-value))
1986 ;; search for previous (new) flyspell overlay
1987 (while (and new-overlay
1988 (or (not (flyspell-overlay-p new-overlay))
1989 ;; check if its face has changed
1990 (not (eq (get-char-property
1991 (overlay-start new-overlay) 'face)
1992 'flyspell-incorrect))))
1993 (setq new-overlay (car-safe overlay-list))
1994 (setq overlay-list (cdr-safe overlay-list)))
1996 ;; if nothing new exits new-overlay should be nil
1997 (if new-overlay ;; the length of the word may change so go to the start
1998 (setq flyspell-auto-correct-previous-pos
1999 (overlay-start new-overlay)))))
2001 (when flyspell-auto-correct-previous-pos
2002 (save-excursion
2003 (goto-char flyspell-auto-correct-previous-pos)
2004 (let ((ispell-following-word t)) ;; point is at start
2005 (if (numberp flyspell-auto-correct-previous-pos)
2006 (goto-char flyspell-auto-correct-previous-pos))
2007 (flyspell-auto-correct-word))
2008 ;; the point may have moved so reset this
2009 (setq flyspell-auto-correct-previous-pos (point))))))))
2011 ;;*---------------------------------------------------------------------*/
2012 ;;* flyspell-correct-word ... */
2013 ;;*---------------------------------------------------------------------*/
2015 (defun flyspell-correct-word (event)
2016 "Pop up a menu of possible corrections for a misspelled word.
2017 The word checked is the word at the mouse position."
2018 (interactive "e")
2019 (let ((save (point)))
2020 (mouse-set-point event)
2021 (flyspell-correct-word-before-point event save)))
2023 (defun flyspell-correct-word-before-point (&optional event opoint)
2024 "Pop up a menu of possible corrections for misspelled word before point.
2025 If EVENT is non-nil, it is the mouse event that invoked this operation;
2026 that controls where to put the menu.
2027 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2028 (interactive)
2029 (unless (mouse-position)
2030 (error "Pop-up menus do not work on this terminal"))
2031 ;; use the correct dictionary
2032 (flyspell-accept-buffer-local-defs)
2033 (or opoint (setq opoint (point-marker)))
2034 (let ((cursor-location (point))
2035 (word (flyspell-get-word nil)))
2036 (if (consp word)
2037 (let ((start (car (cdr word)))
2038 (end (car (cdr (cdr word))))
2039 (word (car word))
2040 poss ispell-filter)
2041 ;; now check spelling of word.
2042 (ispell-send-string "%\n") ;put in verbose mode
2043 (ispell-send-string (concat "^" word "\n"))
2044 ;; wait until ispell has processed word
2045 (while (progn
2046 (accept-process-output ispell-process)
2047 (not (string= "" (car ispell-filter)))))
2048 ;; Remove leading empty element
2049 (setq ispell-filter (cdr ispell-filter))
2050 ;; ispell process should return something after word is sent.
2051 ;; Tag word as valid (i.e., skip) otherwise
2052 (or ispell-filter
2053 (setq ispell-filter '(*)))
2054 (if (consp ispell-filter)
2055 (setq poss (ispell-parse-output (car ispell-filter))))
2056 (cond
2057 ((or (eq poss t) (stringp poss))
2058 ;; don't correct word
2060 ((null poss)
2061 ;; ispell error
2062 (error "Ispell: error in Ispell process"))
2063 ((featurep 'xemacs)
2064 (flyspell-xemacs-popup
2065 poss word cursor-location start end opoint))
2067 ;; The word is incorrect, we have to propose a replacement.
2068 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2069 poss word cursor-location start end opoint)))
2070 (ispell-pdict-save t)))))
2072 ;;*---------------------------------------------------------------------*/
2073 ;;* flyspell-do-correct ... */
2074 ;;*---------------------------------------------------------------------*/
2075 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2076 "The popup menu callback."
2077 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2078 ;; it instead right after calling the function.
2079 (cond ((eq replace 'ignore)
2080 (goto-char save)
2081 nil)
2082 ((eq replace 'save)
2083 (goto-char save)
2084 (ispell-send-string (concat "*" word "\n"))
2085 ;; This was added only to the XEmacs side in revision 1.18 of
2086 ;; flyspell. I assume its absence on the Emacs side was an
2087 ;; oversight. --Stef
2088 (ispell-send-string "#\n")
2089 (flyspell-unhighlight-at cursor-location)
2090 (setq ispell-pdict-modified-p '(t)))
2091 ((or (eq replace 'buffer) (eq replace 'session))
2092 (ispell-send-string (concat "@" word "\n"))
2093 (flyspell-unhighlight-at cursor-location)
2094 (if (null ispell-pdict-modified-p)
2095 (setq ispell-pdict-modified-p
2096 (list ispell-pdict-modified-p)))
2097 (goto-char save)
2098 (if (eq replace 'buffer)
2099 (ispell-add-per-file-word-list word)))
2100 (replace
2101 ;; This was added only to the Emacs side. I assume its absence on
2102 ;; the XEmacs side was an oversight. --Stef
2103 (flyspell-unhighlight-at cursor-location)
2104 (let ((old-max (point-max))
2105 (new-word (if (atom replace)
2106 replace
2107 (car replace)))
2108 (cursor-location (+ (- (length word) (- end start))
2109 cursor-location)))
2110 (unless (equal new-word (car poss))
2111 (delete-region start end)
2112 (goto-char start)
2113 (funcall flyspell-insert-function new-word)
2114 (if flyspell-abbrev-p
2115 (flyspell-define-abbrev word new-word)))
2116 ;; In the original Emacs code, this was only called in the body
2117 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2118 (flyspell-ajust-cursor-point save cursor-location old-max)))
2120 (goto-char save)
2121 nil)))
2123 ;;*---------------------------------------------------------------------*/
2124 ;;* flyspell-ajust-cursor-point ... */
2125 ;;*---------------------------------------------------------------------*/
2126 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2127 (if (>= save cursor-location)
2128 (let ((new-pos (+ save (- (point-max) old-max))))
2129 (goto-char (cond
2130 ((< new-pos (point-min))
2131 (point-min))
2132 ((> new-pos (point-max))
2133 (point-max))
2134 (t new-pos))))
2135 (goto-char save)))
2137 ;;*---------------------------------------------------------------------*/
2138 ;;* flyspell-emacs-popup ... */
2139 ;;*---------------------------------------------------------------------*/
2140 (defun flyspell-emacs-popup (event poss word)
2141 "The Emacs popup menu."
2142 (unless window-system
2143 (error "This command requires pop-up dialogs"))
2144 (if (not event)
2145 (let* ((mouse-pos (mouse-position))
2146 (mouse-pos (if (nth 1 mouse-pos)
2147 mouse-pos
2148 (set-mouse-position (car mouse-pos)
2149 (/ (frame-width) 2) 2)
2150 (mouse-position))))
2151 (setq event (list (list (car (cdr mouse-pos))
2152 (1+ (cdr (cdr mouse-pos))))
2153 (car mouse-pos)))))
2154 (let* ((corrects (if flyspell-sort-corrections
2155 (sort (car (cdr (cdr poss))) 'string<)
2156 (car (cdr (cdr poss)))))
2157 (cor-menu (if (consp corrects)
2158 (mapcar (lambda (correct)
2159 (list correct correct))
2160 corrects)
2161 '()))
2162 (affix (car (cdr (cdr (cdr poss)))))
2163 show-affix-info
2164 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2165 (list
2166 (list (concat "Save affix: " (car affix))
2167 'save)
2168 '("Accept (session)" session)
2169 '("Accept (buffer)" buffer))
2170 '(("Save word" save)
2171 ("Accept (session)" session)
2172 ("Accept (buffer)" buffer)))))
2173 (if (consp cor-menu)
2174 (append cor-menu (cons "" save))
2175 save)))
2176 (menu (cons "flyspell correction menu" base-menu)))
2177 (car (x-popup-menu event
2178 (list (format "%s [%s]" word (or ispell-local-dictionary
2179 ispell-dictionary))
2180 menu)))))
2182 ;;*---------------------------------------------------------------------*/
2183 ;;* flyspell-xemacs-popup ... */
2184 ;;*---------------------------------------------------------------------*/
2185 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2186 "The XEmacs popup menu."
2187 (let* ((corrects (if flyspell-sort-corrections
2188 (sort (car (cdr (cdr poss))) 'string<)
2189 (car (cdr (cdr poss)))))
2190 (cor-menu (if (consp corrects)
2191 (mapcar (lambda (correct)
2192 (vector correct
2193 (list 'flyspell-do-correct
2194 correct
2195 (list 'quote poss)
2196 word
2197 cursor-location
2198 start
2200 save)
2202 corrects)
2203 '()))
2204 (affix (car (cdr (cdr (cdr poss)))))
2205 show-affix-info
2206 (menu (let ((save (if (and (consp affix) show-affix-info)
2207 (vector
2208 (concat "Save affix: " (car affix))
2209 (list 'flyspell-do-correct
2210 ''save
2211 (list 'quote poss)
2212 word
2213 cursor-location
2214 start
2216 save)
2218 (vector
2219 "Save word"
2220 (list 'flyspell-do-correct
2221 ''save
2222 (list 'quote poss)
2223 word
2224 cursor-location
2225 start
2227 save)
2228 t)))
2229 (session (vector "Accept (session)"
2230 (list 'flyspell-do-correct
2231 ''session
2232 (list 'quote poss)
2233 word
2234 cursor-location
2235 start
2237 save)
2239 (buffer (vector "Accept (buffer)"
2240 (list 'flyspell-do-correct
2241 ''buffer
2242 (list 'quote poss)
2243 word
2244 cursor-location
2245 start
2247 save)
2248 t)))
2249 (if (consp cor-menu)
2250 (append cor-menu (list "-" save session buffer))
2251 (list save session buffer)))))
2252 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2253 ispell-dictionary))
2254 menu))))
2256 ;;*---------------------------------------------------------------------*/
2257 ;;* Some example functions for real autocorrecting */
2258 ;;*---------------------------------------------------------------------*/
2259 (defun flyspell-maybe-correct-transposition (beg end poss)
2260 "Check replacements for transposed characters.
2262 If the text between BEG and END is equal to a correction suggested by
2263 Ispell, after transposing two adjacent characters, correct the text,
2264 and return t.
2266 The third arg POSS is either the symbol 'doublon' or a list of
2267 possible corrections as returned by `ispell-parse-output'.
2269 This function is meant to be added to `flyspell-incorrect-hook'."
2270 (when (consp poss)
2271 (catch 'done
2272 (let ((str (buffer-substring beg end))
2273 (i 0) (len (- end beg)) tmp)
2274 (while (< (1+ i) len)
2275 (setq tmp (aref str i))
2276 (aset str i (aref str (1+ i)))
2277 (aset str (1+ i) tmp)
2278 (when (member str (nth 2 poss))
2279 (save-excursion
2280 (goto-char (+ beg i 1))
2281 (transpose-chars 1))
2282 (throw 'done t))
2283 (setq tmp (aref str i))
2284 (aset str i (aref str (1+ i)))
2285 (aset str (1+ i) tmp)
2286 (setq i (1+ i))))
2287 nil)))
2289 (defun flyspell-maybe-correct-doubling (beg end poss)
2290 "Check replacements for doubled characters.
2292 If the text between BEG and END is equal to a correction suggested by
2293 Ispell, after removing a pair of doubled characters, correct the text,
2294 and return t.
2296 The third arg POSS is either the symbol 'doublon' or a list of
2297 possible corrections as returned by `ispell-parse-output'.
2299 This function is meant to be added to `flyspell-incorrect-hook'."
2300 (when (consp poss)
2301 (catch 'done
2302 (let ((str (buffer-substring beg end))
2303 (i 0) (len (- end beg)))
2304 (while (< (1+ i) len)
2305 (when (and (= (aref str i) (aref str (1+ i)))
2306 (member (concat (substring str 0 (1+ i))
2307 (substring str (+ i 2)))
2308 (nth 2 poss)))
2309 (goto-char (+ beg i))
2310 (delete-char 1)
2311 (throw 'done t))
2312 (setq i (1+ i))))
2313 nil)))
2315 ;;*---------------------------------------------------------------------*/
2316 ;;* flyspell-already-abbrevp ... */
2317 ;;*---------------------------------------------------------------------*/
2318 (defun flyspell-already-abbrevp (table word)
2319 (let ((sym (abbrev-symbol word table)))
2320 (and sym (symbolp sym))))
2322 ;;*---------------------------------------------------------------------*/
2323 ;;* flyspell-change-abbrev ... */
2324 ;;*---------------------------------------------------------------------*/
2325 (defun flyspell-change-abbrev (table old new)
2326 (set (abbrev-symbol old table) new))
2328 (provide 'flyspell)
2330 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2331 ;;; flyspell.el ends here