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