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