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