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