e07491806116ca9ceefc824c57949bca9752d263
[emacs.git] / lisp / textmodes / flyspell.el
blobe07491806116ca9ceefc824c57949bca9752d263
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 (- (save-excursion
1123 (skip-chars-backward " \t\n\f")))))
1124 (p (when (>= bound (point-min))
1125 (flyspell-word-search-backward word bound t))))
1126 (and p (/= p start)))))
1127 ;; yes, this is a doublon
1128 (flyspell-highlight-incorrect-region start end 'doublon)
1129 nil)
1130 ((and (eq flyspell-word-cache-start start)
1131 (eq flyspell-word-cache-end end)
1132 (string-equal flyspell-word-cache-word word))
1133 ;; this word had been already checked, we skip
1134 flyspell-word-cache-result)
1135 ((and (eq ispell-parser 'tex)
1136 (flyspell-tex-command-p flyspell-word))
1137 ;; this is a correct word (because a tex command)
1138 (flyspell-unhighlight-at start)
1139 (if (> end start)
1140 (flyspell-unhighlight-at (- end 1)))
1143 ;; we setup the cache
1144 (setq flyspell-word-cache-start start)
1145 (setq flyspell-word-cache-end end)
1146 (setq flyspell-word-cache-word word)
1147 ;; now check spelling of word.
1148 (if (not known-misspelling)
1149 (progn
1150 (ispell-send-string "%\n")
1151 ;; put in verbose mode
1152 (ispell-send-string (concat "^" word "\n"))
1153 ;; we mark the ispell process so it can be killed
1154 ;; when emacs is exited without query
1155 (if (featurep 'xemacs)
1156 (process-kill-without-query ispell-process)
1157 (set-process-query-on-exit-flag ispell-process nil))
1158 ;; Wait until ispell has processed word.
1159 (while (progn
1160 (accept-process-output ispell-process)
1161 (not (string= "" (car ispell-filter)))))
1162 ;; (ispell-send-string "!\n")
1163 ;; back to terse mode.
1164 ;; Remove leading empty element
1165 (setq ispell-filter (cdr ispell-filter))
1166 ;; ispell process should return something after word is sent.
1167 ;; Tag word as valid (i.e., skip) otherwise
1168 (or ispell-filter
1169 (setq ispell-filter '(*)))
1170 (if (consp ispell-filter)
1171 (setq poss (ispell-parse-output (car ispell-filter)))))
1172 ;; Else, this was a known misspelling to begin with, and
1173 ;; we should forge an ispell return value.
1174 (setq poss (list word 1 nil nil)))
1175 (let ((res (cond ((eq poss t)
1176 ;; correct
1177 (setq flyspell-word-cache-result t)
1178 (flyspell-unhighlight-at start)
1179 (if (> end start)
1180 (flyspell-unhighlight-at (- end 1)))
1182 ((and (stringp poss) flyspell-highlight-flag)
1183 ;; correct
1184 (setq flyspell-word-cache-result t)
1185 (flyspell-unhighlight-at start)
1186 (if (> end start)
1187 (flyspell-unhighlight-at (- end 1)))
1189 ((null poss)
1190 (setq flyspell-word-cache-result t)
1191 (flyspell-unhighlight-at start)
1192 (if (> end start)
1193 (flyspell-unhighlight-at (- end 1)))
1195 ((or (and (< flyspell-duplicate-distance 0)
1196 (or (save-excursion
1197 (goto-char start)
1198 (flyspell-word-search-backward
1199 word
1200 (point-min)))
1201 (save-excursion
1202 (goto-char end)
1203 (flyspell-word-search-forward
1204 word
1205 (point-max)))))
1206 (and (> flyspell-duplicate-distance 0)
1207 (or (save-excursion
1208 (goto-char start)
1209 (flyspell-word-search-backward
1210 word
1211 (- start
1212 flyspell-duplicate-distance)))
1213 (save-excursion
1214 (goto-char end)
1215 (flyspell-word-search-forward
1216 word
1217 (+ end
1218 flyspell-duplicate-distance))))))
1219 ;; This is a misspelled word which occurs
1220 ;; twice within flyspell-duplicate-distance.
1221 (setq flyspell-word-cache-result nil)
1222 (if flyspell-highlight-flag
1223 (flyspell-highlight-duplicate-region
1224 start end poss)
1225 (message "duplicate `%s'" word))
1226 nil)
1228 (setq flyspell-word-cache-result nil)
1229 ;; Highlight the location as incorrect,
1230 ;; including offset specified in POSS.
1231 (if flyspell-highlight-flag
1232 (flyspell-highlight-incorrect-region
1233 (if (and (consp poss)
1234 (integerp (nth 1 poss)))
1235 (+ start (nth 1 poss) -1)
1236 start)
1237 end poss)
1238 (flyspell-notify-misspell word poss))
1239 nil))))
1240 ;; return to original location
1241 (goto-char cursor-location)
1242 (if ispell-quit (setq ispell-quit nil))
1243 res))))))))
1245 ;;*---------------------------------------------------------------------*/
1246 ;;* flyspell-math-tex-command-p ... */
1247 ;;* ------------------------------------------------------------- */
1248 ;;* This function uses the texmathp package to check if point */
1249 ;;* is within a TeX math environment. `texmathp' can yield errors */
1250 ;;* if the document is currently not valid TeX syntax. */
1251 ;;*---------------------------------------------------------------------*/
1252 (defun flyspell-math-tex-command-p ()
1253 (when (fboundp 'texmathp)
1254 (if flyspell-check-tex-math-command
1256 (condition-case nil
1257 (texmathp)
1258 (error nil)))))
1260 ;;*---------------------------------------------------------------------*/
1261 ;;* flyspell-tex-command-p ... */
1262 ;;*---------------------------------------------------------------------*/
1263 (defun flyspell-tex-command-p (word)
1264 "Return t if WORD is a TeX command."
1265 (or (save-excursion
1266 (let ((b (car (cdr word))))
1267 (and (re-search-backward "\\\\" (- (point) 100) t)
1268 (or (= (match-end 0) b)
1269 (and (goto-char (match-end 0))
1270 (looking-at flyspell-tex-command-regexp)
1271 (>= (match-end 0) b))))))
1272 (flyspell-math-tex-command-p)))
1274 (defalias 'flyspell-get-casechars 'ispell-get-casechars)
1275 (defalias 'flyspell-get-not-casechars 'ispell-get-not-casechars)
1277 ;;*---------------------------------------------------------------------*/
1278 ;;* flyspell-get-word ... */
1279 ;;*---------------------------------------------------------------------*/
1280 (defun flyspell-get-word (&optional following extra-otherchars)
1281 "Return the word for spell-checking according to Ispell syntax.
1282 Optional argument FOLLOWING non-nil means to get the following
1283 \(rather than preceding) word when the cursor is not over a word.
1284 Optional second argument EXTRA-OTHERCHARS is a regexp of characters
1285 that may be included as part of a word (see `ispell-dictionary-alist')."
1286 (let* ((flyspell-casechars (flyspell-get-casechars))
1287 (flyspell-not-casechars (flyspell-get-not-casechars))
1288 (ispell-otherchars (ispell-get-otherchars))
1289 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1290 (word-regexp (concat flyspell-casechars
1291 "+\\("
1292 (if (not (string= "" ispell-otherchars))
1293 (concat ispell-otherchars "?"))
1294 (if extra-otherchars
1295 (concat extra-otherchars "?"))
1296 flyspell-casechars
1297 "+\\)"
1298 (if (or ispell-many-otherchars-p
1299 extra-otherchars)
1300 "*" "?")))
1301 did-it-once prevpt
1302 start end word)
1303 ;; find the word
1304 (if (not (looking-at flyspell-casechars))
1305 (if following
1306 (re-search-forward flyspell-casechars nil t)
1307 (re-search-backward flyspell-casechars nil t)))
1308 ;; move to front of word
1309 (re-search-backward flyspell-not-casechars nil 'start)
1310 (while (and (or (and (not (string= "" ispell-otherchars))
1311 (looking-at ispell-otherchars))
1312 (and extra-otherchars (looking-at extra-otherchars)))
1313 (not (bobp))
1314 (or (not did-it-once)
1315 ispell-many-otherchars-p)
1316 (not (eq prevpt (point))))
1317 (if (and extra-otherchars (looking-at extra-otherchars))
1318 (progn
1319 (backward-char 1)
1320 (if (looking-at flyspell-casechars)
1321 (re-search-backward flyspell-not-casechars nil 'move)))
1322 (setq did-it-once t
1323 prevpt (point))
1324 (backward-char 1)
1325 (if (looking-at flyspell-casechars)
1326 (re-search-backward flyspell-not-casechars nil 'move)
1327 (backward-char -1))))
1328 ;; Now mark the word and save to string.
1329 (if (not (re-search-forward word-regexp nil t))
1331 (progn
1332 (setq start (match-beginning 0)
1333 end (point)
1334 word (buffer-substring-no-properties start end))
1335 (list word start end)))))
1337 ;;*---------------------------------------------------------------------*/
1338 ;;* flyspell-small-region ... */
1339 ;;*---------------------------------------------------------------------*/
1340 (defun flyspell-small-region (beg end)
1341 "Flyspell text between BEG and END."
1342 (save-excursion
1343 (if (> beg end)
1344 (let ((old beg))
1345 (setq beg end)
1346 (setq end old)))
1347 (goto-char beg)
1348 (let ((count 0))
1349 (while (< (point) end)
1350 (if (and flyspell-issue-message-flag (= count 100))
1351 (progn
1352 (message "Spell Checking...%d%%"
1353 (* 100 (/ (float (- (point) beg)) (- end beg))))
1354 (setq count 0))
1355 (setq count (+ 1 count)))
1356 (flyspell-word)
1357 (sit-for 0)
1358 (let ((cur (point)))
1359 (forward-word 1)
1360 (if (and (< (point) end) (> (point) (+ cur 1)))
1361 (backward-char 1)))))
1362 (backward-char 1)
1363 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1364 (flyspell-word)))
1366 ;;*---------------------------------------------------------------------*/
1367 ;;* flyspell-external-ispell-process ... */
1368 ;;*---------------------------------------------------------------------*/
1369 (defvar flyspell-external-ispell-process '()
1370 "The external Flyspell Ispell process.")
1372 ;;*---------------------------------------------------------------------*/
1373 ;;* flyspell-external-ispell-buffer ... */
1374 ;;*---------------------------------------------------------------------*/
1375 (defvar flyspell-external-ispell-buffer '())
1376 (defvar flyspell-large-region-buffer '())
1377 (defvar flyspell-large-region-beg (point-min))
1378 (defvar flyspell-large-region-end (point-max))
1380 ;;*---------------------------------------------------------------------*/
1381 ;;* flyspell-external-point-words ... */
1382 ;;*---------------------------------------------------------------------*/
1383 (defun flyspell-external-point-words ()
1384 "Mark words from a buffer listing incorrect words in order of appearance.
1385 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1386 \(We finish by killing that buffer and setting the variable to nil.)
1387 The buffer to mark them in is `flyspell-large-region-buffer'."
1388 (let (words-not-found
1389 (ispell-otherchars (ispell-get-otherchars))
1390 (buffer-scan-pos flyspell-large-region-beg)
1391 case-fold-search)
1392 (with-current-buffer flyspell-external-ispell-buffer
1393 (goto-char (point-min))
1394 ;; Loop over incorrect words, in the order they were reported,
1395 ;; which is also the order they appear in the buffer being checked.
1396 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1397 ;; Bind WORD to the next one.
1398 (let ((word (match-string 1)) (wordpos (point)))
1399 ;; Here there used to be code to see if WORD is the same
1400 ;; as the previous iteration, and count the number of consecutive
1401 ;; identical words, and the loop below would search for that many.
1402 ;; That code seemed to be incorrect, and on principle, should
1403 ;; be unnecessary too. -- rms.
1404 (if flyspell-issue-message-flag
1405 (message "Spell Checking...%d%% [%s]"
1406 (* 100 (/ (float (point)) (point-max)))
1407 word))
1408 (with-current-buffer flyspell-large-region-buffer
1409 (goto-char buffer-scan-pos)
1410 (let ((keep t))
1411 ;; Iterate on string search until string is found as word,
1412 ;; not as substring.
1413 (while keep
1414 (if (search-forward word
1415 flyspell-large-region-end t)
1416 (let* ((found-list
1417 (save-excursion
1418 ;; Move back into the match
1419 ;; so flyspell-get-word will find it.
1420 (forward-char -1)
1421 (flyspell-get-word)))
1422 (found (car found-list))
1423 (found-length (length found))
1424 (misspell-length (length word)))
1425 (when (or
1426 ;; Size matches, we really found it.
1427 (= found-length misspell-length)
1428 ;; Matches as part of a boundary-char separated
1429 ;; word.
1430 (member word
1431 (split-string found ispell-otherchars))
1432 ;; Misspelling has higher length than
1433 ;; what flyspell considers the word.
1434 ;; Caused by boundary-chars mismatch.
1435 ;; Validating seems safe.
1436 (< found-length misspell-length)
1437 ;; ispell treats beginning of some TeX
1438 ;; commands as nroff control sequences
1439 ;; and strips them in the list of
1440 ;; misspelled words thus giving a
1441 ;; non-existent word. Skip if ispell
1442 ;; is used, string is a TeX command
1443 ;; (char before beginning of word is
1444 ;; backslash) and none of the previous
1445 ;; conditions match.
1446 (and (not ispell-really-aspell)
1447 (save-excursion
1448 (goto-char (- (nth 1 found-list) 1))
1449 (if (looking-at "[\\]" )
1451 nil))))
1452 (setq keep nil)
1453 (flyspell-word nil t)
1454 ;; Search for next misspelled word will begin from
1455 ;; end of last validated match.
1456 (setq buffer-scan-pos (point))))
1457 ;; Record if misspelling is not found and try new one
1458 (cl-pushnew (concat " -> " word " - "
1459 (int-to-string wordpos))
1460 words-not-found :test #'equal)
1461 (setq keep nil)))))))
1462 ;; we are done
1463 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1464 ;; Warn about not found misspellings
1465 (dolist (word words-not-found)
1466 (message "%s: word not found" word))
1467 ;; Kill and forget the buffer with the list of incorrect words.
1468 (kill-buffer flyspell-external-ispell-buffer)
1469 (setq flyspell-external-ispell-buffer nil)))
1471 ;;*---------------------------------------------------------------------*/
1472 ;;* flyspell-process-localwords ... */
1473 ;;* ------------------------------------------------------------- */
1474 ;;* This function is used to prevent marking of words explicitly */
1475 ;;* declared correct. */
1476 ;;*---------------------------------------------------------------------*/
1477 (defun flyspell-process-localwords (misspellings-buffer)
1478 (let ((localwords ispell-buffer-session-localwords)
1479 case-fold-search
1480 (ispell-casechars (ispell-get-casechars)))
1481 ;; Get localwords from the original buffer
1482 (save-excursion
1483 (goto-char (point-min))
1484 ;; Localwords parsing copied from ispell.el.
1485 (while (search-forward ispell-words-keyword nil t)
1486 (let ((end (point-at-eol))
1487 string)
1488 ;; buffer-local words separated by a space, and can contain
1489 ;; any character other than a space. Not rigorous enough.
1490 (while (re-search-forward " *\\([^ ]+\\)" end t)
1491 (setq string (buffer-substring-no-properties (match-beginning 1)
1492 (match-end 1)))
1493 ;; This can fail when string contains a word with invalid chars.
1494 ;; Error handling needs to be added between Ispell and Emacs.
1495 (if (and (< 1 (length string))
1496 (equal 0 (string-match ispell-casechars string)))
1497 (push string localwords))))))
1498 ;; Remove localwords matches from misspellings-buffer.
1499 ;; The usual mechanism of communicating the local words to ispell
1500 ;; does not affect the special ispell process used by
1501 ;; flyspell-large-region.
1502 (with-current-buffer misspellings-buffer
1503 (save-excursion
1504 (dolist (word localwords)
1505 (goto-char (point-min))
1506 (let ((regexp (concat "^" word "\n")))
1507 (while (re-search-forward regexp nil t)
1508 (delete-region (match-beginning 0) (match-end 0)))))))))
1510 ;;* ---------------------------------------------------------------
1511 ;;* flyspell-check-region-doublons
1512 ;;* ---------------------------------------------------------------
1513 (defun flyspell-check-region-doublons (beg end)
1514 "Check for adjacent duplicated words (doublons) in the given region."
1515 (save-excursion
1516 (goto-char beg)
1517 (flyspell-word) ; Make sure current word is checked
1518 (backward-word 1)
1519 (while (and (< (point) end)
1520 (re-search-forward "\\<\\(\\w+\\)\\>[ \n\t\f]+\\1\\>"
1521 end 'move))
1522 (flyspell-word)
1523 (backward-word 1))
1524 (flyspell-word)))
1526 ;;*---------------------------------------------------------------------*/
1527 ;;* flyspell-large-region ... */
1528 ;;*---------------------------------------------------------------------*/
1529 (defun flyspell-large-region (beg end)
1530 (let* ((curbuf (current-buffer))
1531 (buffer (get-buffer-create "*flyspell-region*")))
1532 (setq flyspell-external-ispell-buffer buffer)
1533 (setq flyspell-large-region-buffer curbuf)
1534 (setq flyspell-large-region-beg beg)
1535 (setq flyspell-large-region-end end)
1536 (flyspell-accept-buffer-local-defs)
1537 (set-buffer buffer)
1538 (erase-buffer)
1539 ;; this is done, we can start checking...
1540 (if flyspell-issue-message-flag (message "Checking region..."))
1541 (set-buffer curbuf)
1542 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1543 ;; Local dictionary becomes the global dictionary in use.
1544 (setq ispell-current-dictionary
1545 (or ispell-local-dictionary ispell-dictionary))
1546 (setq ispell-current-personal-dictionary
1547 (or ispell-local-pdict ispell-personal-dictionary))
1548 (let ((args (ispell-get-ispell-args))
1549 (encoding (ispell-get-coding-system))
1551 (if (and ispell-current-dictionary ; use specified dictionary
1552 (not (member "-d" args))) ; only define if not overridden
1553 (setq args
1554 (append (list "-d" ispell-current-dictionary) args)))
1555 (if ispell-current-personal-dictionary ; use specified pers dict
1556 (setq args
1557 (append args
1558 (list "-p"
1559 (expand-file-name
1560 ispell-current-personal-dictionary)))))
1562 ;; Check for extended character mode
1563 (let ((extended-char-mode (ispell-get-extended-character-mode)))
1564 (and extended-char-mode ; ~ extended character mode
1565 (string-match "[^~]+$" extended-char-mode)
1566 (cl-pushnew (concat "-T" (match-string 0 extended-char-mode))
1567 args :test #'equal)))
1569 ;; Add ispell-extra-args
1570 (setq args (append args ispell-extra-args))
1572 ;; If we are using recent aspell or hunspell, make sure we use the right encoding
1573 ;; for communication. ispell or older aspell/hunspell does not support this
1574 (if ispell-encoding8-command
1575 (setq args
1576 (append args
1577 (if ispell-really-hunspell
1578 (list ispell-encoding8-command
1579 (upcase (symbol-name encoding)))
1580 (list (concat ispell-encoding8-command
1581 (symbol-name encoding)))))))
1583 (let ((process-coding-system-alist (list (cons "\\.*" encoding))))
1584 (setq c (apply 'ispell-call-process-region beg
1586 ispell-program-name
1588 buffer
1590 (if ispell-really-aspell "list" "-l")
1591 args)))
1592 (if (eq c 0)
1593 (progn
1594 (flyspell-process-localwords buffer)
1595 (with-current-buffer curbuf
1596 (flyspell-delete-region-overlays beg end)
1597 (flyspell-check-region-doublons beg end))
1598 (flyspell-external-point-words))
1599 (error "Can't check region")))))
1601 ;;*---------------------------------------------------------------------*/
1602 ;;* flyspell-region ... */
1603 ;;* ------------------------------------------------------------- */
1604 ;;* Because `ispell -a' is too slow, it is not possible to use */
1605 ;;* it on large region. Then, when ispell is invoked on a large */
1606 ;;* text region, a new `ispell -l' process is spawned. The */
1607 ;;* pointed out words are then searched in the region a checked with */
1608 ;;* regular flyspell means. */
1609 ;;*---------------------------------------------------------------------*/
1610 ;;;###autoload
1611 (defun flyspell-region (beg end)
1612 "Flyspell text between BEG and END."
1613 (interactive "r")
1614 (ispell-set-spellchecker-params) ; Initialize variables and dicts alists
1615 (if (= beg end)
1617 (save-excursion
1618 (if (> beg end)
1619 (let ((old beg))
1620 (setq beg end)
1621 (setq end old)))
1622 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1623 (flyspell-large-region beg end)
1624 (flyspell-small-region beg end)))))
1626 ;;*---------------------------------------------------------------------*/
1627 ;;* flyspell-buffer ... */
1628 ;;*---------------------------------------------------------------------*/
1629 ;;;###autoload
1630 (defun flyspell-buffer ()
1631 "Flyspell whole buffer."
1632 (interactive)
1633 (flyspell-region (point-min) (point-max)))
1635 ;;*---------------------------------------------------------------------*/
1636 ;;* old next error position ... */
1637 ;;*---------------------------------------------------------------------*/
1638 (defvar flyspell-old-buffer-error nil)
1639 (defvar flyspell-old-pos-error nil)
1641 ;;*---------------------------------------------------------------------*/
1642 ;;* flyspell-goto-next-error ... */
1643 ;;*---------------------------------------------------------------------*/
1644 (defun flyspell-goto-next-error ()
1645 "Go to the next previously detected error.
1646 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1647 FLYSPELL-BUFFER."
1648 (interactive)
1649 (let ((pos (point))
1650 (max (point-max)))
1651 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1652 (eq pos flyspell-old-pos-error))
1653 (progn
1654 (if (= flyspell-old-pos-error max)
1655 ;; goto beginning of buffer
1656 (progn
1657 (message "Restarting from beginning of buffer")
1658 (goto-char (point-min)))
1659 (forward-word 1))
1660 (setq pos (point))))
1661 ;; seek the next error
1662 (while (and (< pos max)
1663 (let ((ovs (overlays-at pos))
1664 (r '()))
1665 (while (and (not r) (consp ovs))
1666 (if (flyspell-overlay-p (car ovs))
1667 (setq r t)
1668 (setq ovs (cdr ovs))))
1669 (not r)))
1670 (setq pos (1+ pos)))
1671 ;; save the current location for next invocation
1672 (setq flyspell-old-pos-error pos)
1673 (setq flyspell-old-buffer-error (current-buffer))
1674 (goto-char pos)
1675 (if (= pos max)
1676 (message "No more miss-spelled word!"))))
1678 ;;*---------------------------------------------------------------------*/
1679 ;;* flyspell-overlay-p ... */
1680 ;;*---------------------------------------------------------------------*/
1681 (defun flyspell-overlay-p (o)
1682 "Return true if O is an overlay used by flyspell."
1683 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1685 ;;*---------------------------------------------------------------------*/
1686 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1687 ;;* ------------------------------------------------------------- */
1688 ;;* Remove overlays introduced by flyspell. */
1689 ;;*---------------------------------------------------------------------*/
1690 (defun flyspell-delete-region-overlays (beg end)
1691 "Delete overlays used by flyspell in a given region."
1692 (if (featurep 'emacs)
1693 (remove-overlays beg end 'flyspell-overlay t)
1694 ;; XEmacs does not have `remove-overlays'
1695 (let ((l (overlays-in beg end)))
1696 (while (consp l)
1697 (progn
1698 (if (flyspell-overlay-p (car l))
1699 (delete-overlay (car l)))
1700 (setq l (cdr l)))))))
1702 (defun flyspell-delete-all-overlays ()
1703 "Delete all the overlays used by flyspell."
1704 (flyspell-delete-region-overlays (point-min) (point-max)))
1706 ;;*---------------------------------------------------------------------*/
1707 ;;* flyspell-unhighlight-at ... */
1708 ;;*---------------------------------------------------------------------*/
1709 (defun flyspell-unhighlight-at (pos)
1710 "Remove the flyspell overlay that are located at POS."
1711 (if flyspell-persistent-highlight
1712 (let ((overlays (overlays-at pos)))
1713 (while (consp overlays)
1714 (if (flyspell-overlay-p (car overlays))
1715 (delete-overlay (car overlays)))
1716 (setq overlays (cdr overlays))))
1717 (if (flyspell-overlay-p flyspell-overlay)
1718 (delete-overlay flyspell-overlay))))
1720 ;;*---------------------------------------------------------------------*/
1721 ;;* flyspell-properties-at-p ... */
1722 ;;* ------------------------------------------------------------- */
1723 ;;* Is there an highlight properties at position pos? */
1724 ;;*---------------------------------------------------------------------*/
1725 (defun flyspell-properties-at-p (pos)
1726 "Return t if there is a text property at POS, not counting `local-map'.
1727 If variable `flyspell-highlight-properties' is set to nil,
1728 text with properties are not checked. This function is used to discover
1729 if the character at POS has any other property."
1730 (let ((prop (text-properties-at pos))
1731 (keep t))
1732 (while (and keep (consp prop))
1733 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1734 (setq prop (cdr (cdr prop)))
1735 (setq keep nil)))
1736 (consp prop)))
1738 ;;*---------------------------------------------------------------------*/
1739 ;;* make-flyspell-overlay ... */
1740 ;;*---------------------------------------------------------------------*/
1741 (defun make-flyspell-overlay (beg end face mouse-face)
1742 "Allocate an overlay to highlight an incorrect word.
1743 BEG and END specify the range in the buffer of that word.
1744 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1745 for the overlay."
1746 (let ((overlay (make-overlay beg end nil t nil)))
1747 (overlay-put overlay 'face face)
1748 (overlay-put overlay 'mouse-face mouse-face)
1749 (overlay-put overlay 'flyspell-overlay t)
1750 (overlay-put overlay 'evaporate t)
1751 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1752 (overlay-put overlay 'keymap flyspell-mouse-map)
1753 (when (eq face 'flyspell-incorrect)
1754 (and (stringp flyspell-before-incorrect-word-string)
1755 (overlay-put overlay 'before-string
1756 flyspell-before-incorrect-word-string))
1757 (and (stringp flyspell-after-incorrect-word-string)
1758 (overlay-put overlay 'after-string
1759 flyspell-after-incorrect-word-string)))
1760 overlay))
1762 ;;*---------------------------------------------------------------------*/
1763 ;;* flyspell-highlight-incorrect-region ... */
1764 ;;*---------------------------------------------------------------------*/
1765 (defun flyspell-highlight-incorrect-region (beg end poss)
1766 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1767 POSS is usually a list of possible spelling/correction lists,
1768 as returned by `ispell-parse-output'.
1769 It can also be the symbol `doublon', in the case where the word
1770 is itself incorrect, but suspiciously repeated."
1771 (let ((inhibit-read-only t))
1772 (unless (run-hook-with-args-until-success
1773 'flyspell-incorrect-hook beg end poss)
1774 (if (or flyspell-highlight-properties
1775 (not (flyspell-properties-at-p beg)))
1776 (progn
1777 ;; we cleanup all the overlay that are in the region, not
1778 ;; beginning at the word start position
1779 (if (< (1+ beg) end)
1780 (let ((os (overlays-in (1+ beg) end)))
1781 (while (consp os)
1782 (if (flyspell-overlay-p (car os))
1783 (delete-overlay (car os)))
1784 (setq os (cdr os)))))
1785 ;; we cleanup current overlay at the same position
1786 (flyspell-unhighlight-at beg)
1787 ;; now we can use a new overlay
1788 (setq flyspell-overlay
1789 (make-flyspell-overlay
1790 beg end
1791 (if (eq poss 'doublon) 'flyspell-duplicate 'flyspell-incorrect)
1792 'highlight)))))))
1794 ;;*---------------------------------------------------------------------*/
1795 ;;* flyspell-highlight-duplicate-region ... */
1796 ;;*---------------------------------------------------------------------*/
1797 (defun flyspell-highlight-duplicate-region (beg end poss)
1798 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1799 POSS is a list of possible spelling/correction lists,
1800 as returned by `ispell-parse-output'."
1801 (let ((inhibit-read-only t))
1802 (unless (run-hook-with-args-until-success
1803 'flyspell-incorrect-hook beg end poss)
1804 (if (or flyspell-highlight-properties
1805 (not (flyspell-properties-at-p beg)))
1806 (progn
1807 ;; we cleanup current overlay at the same position
1808 (flyspell-unhighlight-at beg)
1809 ;; now we can use a new overlay
1810 (setq flyspell-overlay
1811 (make-flyspell-overlay beg end
1812 'flyspell-duplicate
1813 'highlight)))))))
1815 ;;*---------------------------------------------------------------------*/
1816 ;;* flyspell-auto-correct-cache ... */
1817 ;;*---------------------------------------------------------------------*/
1818 (defvar flyspell-auto-correct-pos nil)
1819 (defvar flyspell-auto-correct-region nil)
1820 (defvar flyspell-auto-correct-ring nil)
1821 (defvar flyspell-auto-correct-word nil)
1822 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1823 (make-variable-buffer-local 'flyspell-auto-correct-region)
1824 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1825 (make-variable-buffer-local 'flyspell-auto-correct-word)
1827 ;;*---------------------------------------------------------------------*/
1828 ;;* flyspell-check-previous-highlighted-word ... */
1829 ;;*---------------------------------------------------------------------*/
1830 (defun flyspell-check-previous-highlighted-word (&optional arg)
1831 "Correct the closest previous word that is highlighted as misspelled.
1832 This function scans for a word which starts before point that has been
1833 highlighted by Flyspell as misspelled. If it finds one, it proposes
1834 a replacement for that word. With prefix arg N, check the Nth word
1835 before point that's highlighted as misspelled."
1836 (interactive "P")
1837 (let ((pos1 (point))
1838 (pos (point))
1839 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1840 ov ovs)
1841 (if (catch 'exit
1842 (while (and (setq pos (previous-overlay-change pos))
1843 (not (= pos pos1)))
1844 (setq pos1 pos)
1845 (if (> pos (point-min))
1846 (progn
1847 (setq ovs (overlays-at pos))
1848 (while (consp ovs)
1849 (setq ov (car ovs))
1850 (setq ovs (cdr ovs))
1851 (if (and (flyspell-overlay-p ov)
1852 (= 0 (setq arg (1- arg))))
1853 (throw 'exit t)))))))
1854 (save-excursion
1855 (goto-char pos)
1856 (ispell-word)
1857 (setq flyspell-word-cache-word nil) ;; Force flyspell-word re-check
1858 (flyspell-word))
1859 (error "No word to correct before point"))))
1861 ;;*---------------------------------------------------------------------*/
1862 ;;* flyspell-display-next-corrections ... */
1863 ;;*---------------------------------------------------------------------*/
1864 (defun flyspell-display-next-corrections (corrections)
1865 (let ((string "Corrections:")
1866 (l corrections)
1867 (pos '()))
1868 (while (< (length string) 80)
1869 (if (equal (car l) flyspell-auto-correct-word)
1870 (setq pos (cons (+ 1 (length string)) pos)))
1871 (setq string (concat string " " (car l)))
1872 (setq l (cdr l)))
1873 (while (consp pos)
1874 (let ((num (car pos)))
1875 (put-text-property num
1876 (+ num (length flyspell-auto-correct-word))
1877 'face 'flyspell-incorrect
1878 string))
1879 (setq pos (cdr pos)))
1880 (if (fboundp 'display-message)
1881 (display-message 'no-log string)
1882 (message "%s" string))))
1884 ;;*---------------------------------------------------------------------*/
1885 ;;* flyspell-abbrev-table ... */
1886 ;;*---------------------------------------------------------------------*/
1887 (defun flyspell-abbrev-table ()
1888 (if flyspell-use-global-abbrev-table-p
1889 global-abbrev-table
1890 (or local-abbrev-table global-abbrev-table)))
1892 ;;*---------------------------------------------------------------------*/
1893 ;;* flyspell-define-abbrev ... */
1894 ;;*---------------------------------------------------------------------*/
1895 (defun flyspell-define-abbrev (name expansion)
1896 (let ((table (flyspell-abbrev-table)))
1897 (when table
1898 (define-abbrev table (downcase name) expansion))))
1900 ;;*---------------------------------------------------------------------*/
1901 ;;* flyspell-auto-correct-word ... */
1902 ;;*---------------------------------------------------------------------*/
1903 (defun flyspell-auto-correct-word ()
1904 "Correct the current word.
1905 This command proposes various successive corrections for the current word."
1906 (interactive)
1907 (let ((pos (point))
1908 (old-max (point-max)))
1909 ;; Use the correct dictionary.
1910 (flyspell-accept-buffer-local-defs)
1911 (if (and (eq flyspell-auto-correct-pos pos)
1912 (consp flyspell-auto-correct-region))
1913 ;; We have already been using the function at the same location.
1914 (let* ((start (car flyspell-auto-correct-region))
1915 (len (cdr flyspell-auto-correct-region)))
1916 (flyspell-unhighlight-at start)
1917 (delete-region start (+ start len))
1918 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1919 (let* ((word (car flyspell-auto-correct-ring))
1920 (len (length word)))
1921 (rplacd flyspell-auto-correct-region len)
1922 (goto-char start)
1923 (if flyspell-abbrev-p
1924 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1925 flyspell-auto-correct-word)
1926 (flyspell-change-abbrev (flyspell-abbrev-table)
1927 flyspell-auto-correct-word
1928 word)
1929 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1930 (funcall flyspell-insert-function word)
1931 (flyspell-word)
1932 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1933 (flyspell-ajust-cursor-point pos (point) old-max)
1934 (setq flyspell-auto-correct-pos (point)))
1935 ;; Fetch the word to be checked.
1936 (let ((word (flyspell-get-word)))
1937 (if (consp word)
1938 (let ((start (car (cdr word)))
1939 (end (car (cdr (cdr word))))
1940 (word (car word))
1941 poss ispell-filter)
1942 (setq flyspell-auto-correct-word word)
1943 ;; Now check spelling of word..
1944 (ispell-send-string "%\n") ;Put in verbose mode.
1945 (ispell-send-string (concat "^" word "\n"))
1946 ;; Wait until ispell has processed word.
1947 (while (progn
1948 (accept-process-output ispell-process)
1949 (not (string= "" (car ispell-filter)))))
1950 ;; Remove leading empty element.
1951 (setq ispell-filter (cdr ispell-filter))
1952 ;; Ispell process should return something after word is sent.
1953 ;; Tag word as valid (i.e., skip) otherwise.
1954 (or ispell-filter
1955 (setq ispell-filter '(*)))
1956 (if (consp ispell-filter)
1957 (setq poss (ispell-parse-output (car ispell-filter))))
1958 (cond
1959 ((or (eq poss t) (stringp poss))
1960 ;; Don't correct word.
1962 ((null poss)
1963 ;; Ispell error.
1964 (error "Ispell: error in Ispell process"))
1966 ;; The word is incorrect, we have to propose a replacement.
1967 (let ((replacements (if flyspell-sort-corrections
1968 (sort (car (cdr (cdr poss))) 'string<)
1969 (car (cdr (cdr poss))))))
1970 (setq flyspell-auto-correct-region nil)
1971 (if (consp replacements)
1972 (progn
1973 (let ((replace (car replacements)))
1974 (let ((new-word replace))
1975 (if (not (equal new-word (car poss)))
1976 (progn
1977 ;; the save the current replacements
1978 (setq flyspell-auto-correct-region
1979 (cons start (length new-word)))
1980 (let ((l replacements))
1981 (while (consp (cdr l))
1982 (setq l (cdr l)))
1983 (rplacd l (cons (car poss) replacements)))
1984 (setq flyspell-auto-correct-ring
1985 replacements)
1986 (flyspell-unhighlight-at start)
1987 (delete-region start end)
1988 (funcall flyspell-insert-function new-word)
1989 (if flyspell-abbrev-p
1990 (if (flyspell-already-abbrevp
1991 (flyspell-abbrev-table) word)
1992 (flyspell-change-abbrev
1993 (flyspell-abbrev-table)
1994 word
1995 new-word)
1996 (flyspell-define-abbrev word
1997 new-word)))
1998 (flyspell-word)
1999 (flyspell-display-next-corrections
2000 (cons new-word flyspell-auto-correct-ring))
2001 (flyspell-ajust-cursor-point pos
2002 (point)
2003 old-max))))))))))
2004 (setq flyspell-auto-correct-pos (point))
2005 (ispell-pdict-save t)))))))
2007 ;;*---------------------------------------------------------------------*/
2008 ;;* flyspell-auto-correct-previous-pos ... */
2009 ;;*---------------------------------------------------------------------*/
2010 (defvar flyspell-auto-correct-previous-pos nil
2011 "Holds the start of the first incorrect word before point.")
2013 ;;*---------------------------------------------------------------------*/
2014 ;;* flyspell-auto-correct-previous-hook ... */
2015 ;;*---------------------------------------------------------------------*/
2016 (defun flyspell-auto-correct-previous-hook ()
2017 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
2018 Sets `flyspell-auto-correct-previous-pos' to nil"
2019 (interactive)
2020 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
2021 (unless (eq this-command (function flyspell-auto-correct-previous-word))
2022 (setq flyspell-auto-correct-previous-pos nil)))
2024 ;;*---------------------------------------------------------------------*/
2025 ;;* flyspell-auto-correct-previous-word ... */
2026 ;;*---------------------------------------------------------------------*/
2027 (defun flyspell-auto-correct-previous-word (position)
2028 "Auto correct the first misspelled word that occurs before point.
2029 But don't look beyond what's visible on the screen."
2030 (interactive "d")
2032 (let ((top (window-start))
2033 (bot (window-end)))
2034 (save-excursion
2035 (save-restriction
2036 (narrow-to-region top bot)
2037 (overlay-recenter (point))
2039 (add-hook 'pre-command-hook
2040 (function flyspell-auto-correct-previous-hook) t t)
2042 (unless flyspell-auto-correct-previous-pos
2043 ;; only reset if a new overlay exists
2044 (setq flyspell-auto-correct-previous-pos nil)
2046 (let ((overlay-list (overlays-in (point-min) position))
2047 (new-overlay 'dummy-value))
2049 ;; search for previous (new) flyspell overlay
2050 (while (and new-overlay
2051 (or (not (flyspell-overlay-p new-overlay))
2052 ;; check if its face has changed
2053 (not (eq (get-char-property
2054 (overlay-start new-overlay) 'face)
2055 'flyspell-incorrect))))
2056 (setq new-overlay (car-safe overlay-list))
2057 (setq overlay-list (cdr-safe overlay-list)))
2059 ;; if nothing new exits new-overlay should be nil
2060 (if new-overlay ;; the length of the word may change so go to the start
2061 (setq flyspell-auto-correct-previous-pos
2062 (overlay-start new-overlay)))))
2064 (when flyspell-auto-correct-previous-pos
2065 (save-excursion
2066 (goto-char flyspell-auto-correct-previous-pos)
2067 (let ((ispell-following-word t)) ;; point is at start
2068 (if (numberp flyspell-auto-correct-previous-pos)
2069 (goto-char flyspell-auto-correct-previous-pos))
2070 (flyspell-auto-correct-word))
2071 ;; the point may have moved so reset this
2072 (setq flyspell-auto-correct-previous-pos (point))))))))
2074 ;;*---------------------------------------------------------------------*/
2075 ;;* flyspell-correct-word ... */
2076 ;;*---------------------------------------------------------------------*/
2078 (defun flyspell-correct-word (event)
2079 "Pop up a menu of possible corrections for a misspelled word.
2080 The word checked is the word at the mouse position."
2081 (interactive "e")
2082 (let ((save (point)))
2083 (mouse-set-point event)
2084 (flyspell-correct-word-before-point event save)))
2086 (defun flyspell-correct-word-before-point (&optional event opoint)
2087 "Pop up a menu of possible corrections for misspelled word before point.
2088 If EVENT is non-nil, it is the mouse event that invoked this operation;
2089 that controls where to put the menu.
2090 If OPOINT is non-nil, restore point there after adjusting it for replacement."
2091 (interactive)
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 (if (and (not event)
2207 (display-mouse-p))
2208 (let* ((mouse-pos (mouse-position))
2209 (mouse-pos (if (nth 1 mouse-pos)
2210 mouse-pos
2211 (set-mouse-position (car mouse-pos)
2212 (/ (frame-width) 2) 2)
2213 (mouse-position))))
2214 (setq event (list (list (car (cdr mouse-pos))
2215 (1+ (cdr (cdr mouse-pos))))
2216 (car mouse-pos)))))
2217 (let* ((corrects (if flyspell-sort-corrections
2218 (sort (car (cdr (cdr poss))) 'string<)
2219 (car (cdr (cdr poss)))))
2220 (cor-menu (if (consp corrects)
2221 (mapcar (lambda (correct)
2222 (list correct correct))
2223 corrects)
2224 '()))
2225 (affix (car (cdr (cdr (cdr poss)))))
2226 show-affix-info
2227 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2228 (list
2229 (list (concat "Save affix: " (car affix))
2230 'save)
2231 '("Accept (session)" session)
2232 '("Accept (buffer)" buffer))
2233 '(("Save word" save)
2234 ("Accept (session)" session)
2235 ("Accept (buffer)" buffer)))))
2236 (if (consp cor-menu)
2237 (append cor-menu (cons "" save))
2238 save)))
2239 (menu (cons "flyspell correction menu" base-menu)))
2240 (car (x-popup-menu event
2241 (list (format "%s [%s]" word (or ispell-local-dictionary
2242 ispell-dictionary))
2243 menu)))))
2245 ;;*---------------------------------------------------------------------*/
2246 ;;* flyspell-xemacs-popup ... */
2247 ;;*---------------------------------------------------------------------*/
2248 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2249 "The XEmacs popup menu."
2250 (let* ((corrects (if flyspell-sort-corrections
2251 (sort (car (cdr (cdr poss))) 'string<)
2252 (car (cdr (cdr poss)))))
2253 (cor-menu (if (consp corrects)
2254 (mapcar (lambda (correct)
2255 (vector correct
2256 (list 'flyspell-do-correct
2257 correct
2258 (list 'quote poss)
2259 word
2260 cursor-location
2261 start
2263 save)
2265 corrects)
2266 '()))
2267 (affix (car (cdr (cdr (cdr poss)))))
2268 show-affix-info
2269 (menu (let ((save (if (and (consp affix) show-affix-info)
2270 (vector
2271 (concat "Save affix: " (car affix))
2272 (list 'flyspell-do-correct
2273 ''save
2274 (list 'quote poss)
2275 word
2276 cursor-location
2277 start
2279 save)
2281 (vector
2282 "Save word"
2283 (list 'flyspell-do-correct
2284 ''save
2285 (list 'quote poss)
2286 word
2287 cursor-location
2288 start
2290 save)
2291 t)))
2292 (session (vector "Accept (session)"
2293 (list 'flyspell-do-correct
2294 ''session
2295 (list 'quote poss)
2296 word
2297 cursor-location
2298 start
2300 save)
2302 (buffer (vector "Accept (buffer)"
2303 (list 'flyspell-do-correct
2304 ''buffer
2305 (list 'quote poss)
2306 word
2307 cursor-location
2308 start
2310 save)
2311 t)))
2312 (if (consp cor-menu)
2313 (append cor-menu (list "-" save session buffer))
2314 (list save session buffer)))))
2315 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2316 ispell-dictionary))
2317 menu))))
2319 ;;*---------------------------------------------------------------------*/
2320 ;;* Some example functions for real autocorrecting */
2321 ;;*---------------------------------------------------------------------*/
2322 (defun flyspell-maybe-correct-transposition (beg end poss)
2323 "Check replacements for transposed characters.
2325 If the text between BEG and END is equal to a correction suggested by
2326 Ispell, after transposing two adjacent characters, correct the text,
2327 and return t.
2329 The third arg POSS is either the symbol 'doublon' or a list of
2330 possible corrections as returned by `ispell-parse-output'.
2332 This function is meant to be added to `flyspell-incorrect-hook'."
2333 (when (consp poss)
2334 (catch 'done
2335 (let ((str (buffer-substring beg end))
2336 (i 0) (len (- end beg)) tmp)
2337 (while (< (1+ i) len)
2338 (setq tmp (aref str i))
2339 (aset str i (aref str (1+ i)))
2340 (aset str (1+ i) tmp)
2341 (when (member str (nth 2 poss))
2342 (save-excursion
2343 (goto-char (+ beg i 1))
2344 (transpose-chars 1))
2345 (throw 'done t))
2346 (setq tmp (aref str i))
2347 (aset str i (aref str (1+ i)))
2348 (aset str (1+ i) tmp)
2349 (setq i (1+ i))))
2350 nil)))
2352 (defun flyspell-maybe-correct-doubling (beg end poss)
2353 "Check replacements for doubled characters.
2355 If the text between BEG and END is equal to a correction suggested by
2356 Ispell, after removing a pair of doubled characters, correct the text,
2357 and return t.
2359 The third arg POSS is either the symbol 'doublon' or a list of
2360 possible corrections as returned by `ispell-parse-output'.
2362 This function is meant to be added to `flyspell-incorrect-hook'."
2363 (when (consp poss)
2364 (catch 'done
2365 (let ((str (buffer-substring beg end))
2366 (i 0) (len (- end beg)))
2367 (while (< (1+ i) len)
2368 (when (and (= (aref str i) (aref str (1+ i)))
2369 (member (concat (substring str 0 (1+ i))
2370 (substring str (+ i 2)))
2371 (nth 2 poss)))
2372 (goto-char (+ beg i))
2373 (delete-char 1)
2374 (throw 'done t))
2375 (setq i (1+ i))))
2376 nil)))
2378 ;;*---------------------------------------------------------------------*/
2379 ;;* flyspell-already-abbrevp ... */
2380 ;;*---------------------------------------------------------------------*/
2381 (defun flyspell-already-abbrevp (table word)
2382 (let ((sym (abbrev-symbol word table)))
2383 (and sym (symbolp sym))))
2385 ;;*---------------------------------------------------------------------*/
2386 ;;* flyspell-change-abbrev ... */
2387 ;;*---------------------------------------------------------------------*/
2388 (defun flyspell-change-abbrev (table old new)
2389 (set (abbrev-symbol old table) new))
2391 (provide 'flyspell)
2393 ;;; flyspell.el ends here