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