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