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