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