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