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