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