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