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