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