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