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