]> code.delx.au - gnu-emacs/blob - lisp/textmodes/flyspell.el
* textmodes/flyspell.el (flyspell-word-search-backward): Set
[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 (inhibit-point-motion-hooks t)
963 p)
964 (while (and (not r) (setq p (search-backward word bound t)))
965 (let ((lw (flyspell-get-word '())))
966 (if (and (consp lw) (string-equal (car lw) word))
967 (setq r p)
968 (goto-char p))))
969 r)))
970
971 ;;*---------------------------------------------------------------------*/
972 ;;* flyspell-word-search-forward ... */
973 ;;*---------------------------------------------------------------------*/
974 (defun flyspell-word-search-forward (word bound)
975 (save-excursion
976 (let ((r '())
977 (inhibit-point-motion-hooks t)
978 p)
979 (while (and (not r) (setq p (search-forward word bound t)))
980 (let ((lw (flyspell-get-word '())))
981 (if (and (consp lw) (string-equal (car lw) word))
982 (setq r p)
983 (goto-char (1+ p)))))
984 r)))
985
986 ;;*---------------------------------------------------------------------*/
987 ;;* flyspell-word ... */
988 ;;*---------------------------------------------------------------------*/
989 (defun flyspell-word (&optional following)
990 "Spell check a word."
991 (interactive (list ispell-following-word))
992 (save-excursion
993 ;; use the correct dictionary
994 (flyspell-accept-buffer-local-defs)
995 (let* ((cursor-location (point))
996 (flyspell-word (flyspell-get-word following))
997 start end poss word ispell-filter)
998 (if (or (eq flyspell-word nil)
999 (and (fboundp flyspell-generic-check-word-predicate)
1000 (not (funcall flyspell-generic-check-word-predicate))))
1001 t
1002 (progn
1003 ;; destructure return flyspell-word info list.
1004 (setq start (car (cdr flyspell-word))
1005 end (car (cdr (cdr flyspell-word)))
1006 word (car flyspell-word))
1007 ;; before checking in the directory, we check for doublons.
1008 (cond
1009 ((and (or (not (eq ispell-parser 'tex))
1010 (and (> start (point-min))
1011 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1012 flyspell-mark-duplications-flag
1013 (save-excursion
1014 (goto-char (1- start))
1015 (let ((p (flyspell-word-search-backward
1016 word
1017 (- start (1+ (- end start))))))
1018 (and p (/= p (1- start))))))
1019 ;; yes, this is a doublon
1020 (flyspell-highlight-incorrect-region start end 'doublon)
1021 nil)
1022 ((and (eq flyspell-word-cache-start start)
1023 (eq flyspell-word-cache-end end)
1024 (string-equal flyspell-word-cache-word word))
1025 ;; this word had been already checked, we skip
1026 flyspell-word-cache-result)
1027 ((and (eq ispell-parser 'tex)
1028 (flyspell-tex-command-p flyspell-word))
1029 ;; this is a correct word (because a tex command)
1030 (flyspell-unhighlight-at start)
1031 (if (> end start)
1032 (flyspell-unhighlight-at (- end 1)))
1033 t)
1034 (t
1035 ;; we setup the cache
1036 (setq flyspell-word-cache-start start)
1037 (setq flyspell-word-cache-end end)
1038 (setq flyspell-word-cache-word word)
1039 ;; now check spelling of word.
1040 (ispell-send-string "%\n")
1041 ;; put in verbose mode
1042 (ispell-send-string (concat "^" word "\n"))
1043 ;; we mark the ispell process so it can be killed
1044 ;; when emacs is exited without query
1045 (set-process-query-on-exit-flag ispell-process nil)
1046 ;; Wait until ispell has processed word. Since this code is often
1047 ;; executed from post-command-hook but the ispell process may not
1048 ;; be responsive, it's important to make sure we re-enable C-g.
1049 (with-local-quit
1050 (while (progn
1051 (accept-process-output ispell-process)
1052 (not (string= "" (car ispell-filter))))))
1053 ;; (ispell-send-string "!\n")
1054 ;; back to terse mode.
1055 ;; Remove leading empty element
1056 (setq ispell-filter (cdr ispell-filter))
1057 ;; ispell process should return something after word is sent.
1058 ;; Tag word as valid (i.e., skip) otherwise
1059 (or ispell-filter
1060 (setq ispell-filter '(*)))
1061 (if (consp ispell-filter)
1062 (setq poss (ispell-parse-output (car ispell-filter))))
1063 (let ((res (cond ((eq poss t)
1064 ;; correct
1065 (setq flyspell-word-cache-result t)
1066 (flyspell-unhighlight-at start)
1067 (if (> end start)
1068 (flyspell-unhighlight-at (- end 1)))
1069 t)
1070 ((and (stringp poss) flyspell-highlight-flag)
1071 ;; correct
1072 (setq flyspell-word-cache-result t)
1073 (flyspell-unhighlight-at start)
1074 (if (> end start)
1075 (flyspell-unhighlight-at (- end 1)))
1076 t)
1077 ((null poss)
1078 (setq flyspell-word-cache-result t)
1079 (flyspell-unhighlight-at start)
1080 (if (> end start)
1081 (flyspell-unhighlight-at (- end 1)))
1082 t)
1083 ((or (and (< flyspell-duplicate-distance 0)
1084 (or (save-excursion
1085 (goto-char start)
1086 (flyspell-word-search-backward
1087 word
1088 (point-min)))
1089 (save-excursion
1090 (goto-char end)
1091 (flyspell-word-search-forward
1092 word
1093 (point-max)))))
1094 (and (> flyspell-duplicate-distance 0)
1095 (or (save-excursion
1096 (goto-char start)
1097 (flyspell-word-search-backward
1098 word
1099 (- start
1100 flyspell-duplicate-distance)))
1101 (save-excursion
1102 (goto-char end)
1103 (flyspell-word-search-forward
1104 word
1105 (+ end
1106 flyspell-duplicate-distance))))))
1107 ;; This is a misspelled word which occurs
1108 ;; twice within flyspell-duplicate-distance.
1109 (setq flyspell-word-cache-result nil)
1110 (if flyspell-highlight-flag
1111 (flyspell-highlight-duplicate-region
1112 start end poss)
1113 (message "duplicate `%s'" word))
1114 nil)
1115 (t
1116 (setq flyspell-word-cache-result nil)
1117 ;; incorrect highlight the location
1118 (if flyspell-highlight-flag
1119 (flyspell-highlight-incorrect-region
1120 start end poss)
1121 (flyspell-notify-misspell word poss))
1122 nil))))
1123 ;; return to original location
1124 (goto-char cursor-location)
1125 (if ispell-quit (setq ispell-quit nil))
1126 res))))))))
1127
1128 ;;*---------------------------------------------------------------------*/
1129 ;;* flyspell-tex-math-initialized ... */
1130 ;;*---------------------------------------------------------------------*/
1131 (defvar flyspell-tex-math-initialized nil)
1132
1133 ;;*---------------------------------------------------------------------*/
1134 ;;* flyspell-math-tex-command-p ... */
1135 ;;* ------------------------------------------------------------- */
1136 ;;* This function uses the texmathp package to check if (point) */
1137 ;;* is within a tex command. In order to avoid using */
1138 ;;* condition-case each time we use the variable */
1139 ;;* flyspell-tex-math-initialized to make a special case the first */
1140 ;;* time that function is called. */
1141 ;;*---------------------------------------------------------------------*/
1142 (defun flyspell-math-tex-command-p ()
1143 (when (fboundp 'texmathp)
1144 (cond
1145 (flyspell-check-tex-math-command
1146 nil)
1147 ((eq flyspell-tex-math-initialized t)
1148 (texmathp))
1149 ((eq flyspell-tex-math-initialized 'error)
1150 nil)
1151 (t
1152 (setq flyspell-tex-math-initialized t)
1153 (condition-case nil
1154 (texmathp)
1155 (error (progn
1156 (setq flyspell-tex-math-initialized 'error)
1157 nil)))))))
1158
1159 ;;*---------------------------------------------------------------------*/
1160 ;;* flyspell-tex-command-p ... */
1161 ;;*---------------------------------------------------------------------*/
1162 (defun flyspell-tex-command-p (word)
1163 "Return t if WORD is a TeX command."
1164 (or (save-excursion
1165 (let ((b (car (cdr word))))
1166 (and (re-search-backward "\\\\" (- (point) 100) t)
1167 (or (= (match-end 0) b)
1168 (and (goto-char (match-end 0))
1169 (looking-at flyspell-tex-command-regexp)
1170 (>= (match-end 0) b))))))
1171 (flyspell-math-tex-command-p)))
1172
1173 ;;*---------------------------------------------------------------------*/
1174 ;;* flyspell-casechars-cache ... */
1175 ;;*---------------------------------------------------------------------*/
1176 (defvar flyspell-casechars-cache nil)
1177 (defvar flyspell-ispell-casechars-cache nil)
1178 (make-variable-buffer-local 'flyspell-casechars-cache)
1179 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1180
1181 ;;*---------------------------------------------------------------------*/
1182 ;;* flyspell-get-casechars ... */
1183 ;;*---------------------------------------------------------------------*/
1184 (defun flyspell-get-casechars ()
1185 "This function builds a string that is the regexp of word chars.
1186 In order to avoid one useless string construction,
1187 this function changes the last char of the `ispell-casechars' string."
1188 (let ((ispell-casechars (ispell-get-casechars)))
1189 (cond
1190 ((eq ispell-parser 'tex)
1191 (setq flyspell-ispell-casechars-cache ispell-casechars)
1192 (setq flyspell-casechars-cache
1193 (concat (substring ispell-casechars
1194 0
1195 (- (length ispell-casechars) 1))
1196 "]"))
1197 flyspell-casechars-cache)
1198 (t
1199 (setq flyspell-ispell-casechars-cache ispell-casechars)
1200 (setq flyspell-casechars-cache ispell-casechars)
1201 flyspell-casechars-cache))))
1202
1203 ;;*---------------------------------------------------------------------*/
1204 ;;* flyspell-get-not-casechars-cache ... */
1205 ;;*---------------------------------------------------------------------*/
1206 (defvar flyspell-not-casechars-cache nil)
1207 (defvar flyspell-ispell-not-casechars-cache nil)
1208 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1209 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1210
1211 ;;*---------------------------------------------------------------------*/
1212 ;;* flyspell-get-not-casechars ... */
1213 ;;*---------------------------------------------------------------------*/
1214 (defun flyspell-get-not-casechars ()
1215 "This function builds a string that is the regexp of non-word chars."
1216 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1217 (cond
1218 ((eq ispell-parser 'tex)
1219 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1220 (setq flyspell-not-casechars-cache
1221 (concat (substring ispell-not-casechars
1222 0
1223 (- (length ispell-not-casechars) 1))
1224 "]"))
1225 flyspell-not-casechars-cache)
1226 (t
1227 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1228 (setq flyspell-not-casechars-cache ispell-not-casechars)
1229 flyspell-not-casechars-cache))))
1230
1231 ;;*---------------------------------------------------------------------*/
1232 ;;* flyspell-get-word ... */
1233 ;;*---------------------------------------------------------------------*/
1234 (defun flyspell-get-word (following &optional extra-otherchars)
1235 "Return the word for spell-checking according to Ispell syntax.
1236 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1237 is non-nil when called interactively, then the following word
1238 \(rather than preceding\) is checked when the cursor is not over a word.
1239 Optional second argument contains otherchars that can be included in word
1240 many times.
1241
1242 Word syntax described by `flyspell-dictionary-alist' (which see)."
1243 (let* ((flyspell-casechars (flyspell-get-casechars))
1244 (flyspell-not-casechars (flyspell-get-not-casechars))
1245 (ispell-otherchars (ispell-get-otherchars))
1246 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1247 (word-regexp (concat flyspell-casechars
1248 "+\\("
1249 (if (not (string= "" ispell-otherchars))
1250 (concat ispell-otherchars "?"))
1251 (if extra-otherchars
1252 (concat extra-otherchars "?"))
1253 flyspell-casechars
1254 "+\\)"
1255 (if (or ispell-many-otherchars-p
1256 extra-otherchars)
1257 "*" "?")))
1258 did-it-once prevpt
1259 start end word)
1260 ;; find the word
1261 (if (not (looking-at flyspell-casechars))
1262 (if following
1263 (re-search-forward flyspell-casechars nil t)
1264 (re-search-backward flyspell-casechars nil t)))
1265 ;; move to front of word
1266 (re-search-backward flyspell-not-casechars nil 'start)
1267 (while (and (or (and (not (string= "" ispell-otherchars))
1268 (looking-at ispell-otherchars))
1269 (and extra-otherchars (looking-at extra-otherchars)))
1270 (not (bobp))
1271 (or (not did-it-once)
1272 ispell-many-otherchars-p)
1273 (not (eq prevpt (point))))
1274 (if (and extra-otherchars (looking-at extra-otherchars))
1275 (progn
1276 (backward-char 1)
1277 (if (looking-at flyspell-casechars)
1278 (re-search-backward flyspell-not-casechars nil 'move)))
1279 (setq did-it-once t
1280 prevpt (point))
1281 (backward-char 1)
1282 (if (looking-at flyspell-casechars)
1283 (re-search-backward flyspell-not-casechars nil 'move)
1284 (backward-char -1))))
1285 ;; Now mark the word and save to string.
1286 (if (not (re-search-forward word-regexp nil t))
1287 nil
1288 (progn
1289 (setq start (match-beginning 0)
1290 end (point)
1291 word (buffer-substring-no-properties start end))
1292 (list word start end)))))
1293
1294 ;;*---------------------------------------------------------------------*/
1295 ;;* flyspell-small-region ... */
1296 ;;*---------------------------------------------------------------------*/
1297 (defun flyspell-small-region (beg end)
1298 "Flyspell text between BEG and END."
1299 (save-excursion
1300 (if (> beg end)
1301 (let ((old beg))
1302 (setq beg end)
1303 (setq end old)))
1304 (goto-char beg)
1305 (let ((count 0))
1306 (while (< (point) end)
1307 (if (and flyspell-issue-message-flag (= count 100))
1308 (progn
1309 (message "Spell Checking...%d%%"
1310 (* 100 (/ (float (- (point) beg)) (- end beg))))
1311 (setq count 0))
1312 (setq count (+ 1 count)))
1313 (flyspell-word)
1314 (sit-for 0)
1315 (let ((cur (point)))
1316 (forward-word 1)
1317 (if (and (< (point) end) (> (point) (+ cur 1)))
1318 (backward-char 1)))))
1319 (backward-char 1)
1320 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1321 (flyspell-word)))
1322
1323 ;;*---------------------------------------------------------------------*/
1324 ;;* flyspell-external-ispell-process ... */
1325 ;;*---------------------------------------------------------------------*/
1326 (defvar flyspell-external-ispell-process '()
1327 "The external Flyspell Ispell process.")
1328
1329 ;;*---------------------------------------------------------------------*/
1330 ;;* flyspell-external-ispell-buffer ... */
1331 ;;*---------------------------------------------------------------------*/
1332 (defvar flyspell-external-ispell-buffer '())
1333 (defvar flyspell-large-region-buffer '())
1334 (defvar flyspell-large-region-beg (point-min))
1335 (defvar flyspell-large-region-end (point-max))
1336
1337 ;;*---------------------------------------------------------------------*/
1338 ;;* flyspell-external-point-words ... */
1339 ;;*---------------------------------------------------------------------*/
1340 (defun flyspell-external-point-words ()
1341 "Mark words from a buffer listing incorrect words in order of appearance.
1342 The list of incorrect words should be in `flyspell-external-ispell-buffer'.
1343 \(We finish by killing that buffer and setting the variable to nil.)
1344 The buffer to mark them in is `flyspell-large-region-buffer'."
1345 (let (words-not-found
1346 (ispell-otherchars (ispell-get-otherchars))
1347 (buffer-scan-pos flyspell-large-region-beg)
1348 case-fold-search)
1349 (with-current-buffer flyspell-external-ispell-buffer
1350 (goto-char (point-min))
1351 ;; Loop over incorrect words, in the order they were reported,
1352 ;; which is also the order they appear in the buffer being checked.
1353 (while (re-search-forward "\\([^\n]+\\)\n" nil t)
1354 ;; Bind WORD to the next one.
1355 (let ((word (match-string 1)) (wordpos (point)))
1356 ;; Here there used to be code to see if WORD is the same
1357 ;; as the previous iteration, and count the number of consecutive
1358 ;; identical words, and the loop below would search for that many.
1359 ;; That code seemed to be incorrect, and on principle, should
1360 ;; be unnecessary too. -- rms.
1361 (if flyspell-issue-message-flag
1362 (message "Spell Checking...%d%% [%s]"
1363 (* 100 (/ (float (point)) (point-max)))
1364 word))
1365 (with-current-buffer flyspell-large-region-buffer
1366 (goto-char buffer-scan-pos)
1367 (let ((keep t))
1368 ;; Iterate on string search until string is found as word,
1369 ;; not as substring
1370 (while keep
1371 (if (search-forward word
1372 flyspell-large-region-end t)
1373 (let* ((found-list
1374 (save-excursion
1375 ;; Move back into the match
1376 ;; so flyspell-get-word will find it.
1377 (forward-char -1)
1378 (flyspell-get-word nil)))
1379 (found (car found-list))
1380 (found-length (length found))
1381 (misspell-length (length word)))
1382 (when (or
1383 ;; Size matches, we really found it.
1384 (= found-length misspell-length)
1385 ;; Matches as part of a boundary-char separated word
1386 (member word
1387 (split-string found ispell-otherchars))
1388 ;; Misspelling has higher length than
1389 ;; what flyspell considers the
1390 ;; word. Caused by boundary-chars
1391 ;; mismatch. Validating seems safe.
1392 (< found-length misspell-length)
1393 ;; ispell treats beginning of some TeX
1394 ;; commands as nroff control sequences
1395 ;; and strips them in the list of
1396 ;; misspelled words thus giving a
1397 ;; non-existent word. Skip if ispell
1398 ;; is used, string is a TeX command
1399 ;; (char before beginning of word is
1400 ;; backslash) and none of the previous
1401 ;; contitions match
1402 (and (not ispell-really-aspell)
1403 (save-excursion
1404 (goto-char (- (nth 1 found-list) 1))
1405 (if (looking-at "[\\]" )
1406 t
1407 nil))))
1408 (setq keep nil)
1409 (flyspell-word)
1410 ;; Search for next misspelled word will begin from
1411 ;; end of last validated match.
1412 (setq buffer-scan-pos (point))))
1413 ;; Record if misspelling is not found and try new one
1414 (add-to-list 'words-not-found
1415 (concat " -> " word " - "
1416 (int-to-string wordpos)))
1417 (setq keep nil)))))))
1418 ;; we are done
1419 (if flyspell-issue-message-flag (message "Spell Checking completed.")))
1420 ;; Warn about not found misspellings
1421 (dolist (word words-not-found)
1422 (message "%s: word not found" word))
1423 ;; Kill and forget the buffer with the list of incorrect words.
1424 (kill-buffer flyspell-external-ispell-buffer)
1425 (setq flyspell-external-ispell-buffer nil)))
1426
1427 ;;*---------------------------------------------------------------------*/
1428 ;;* flyspell-process-localwords ... */
1429 ;;* ------------------------------------------------------------- */
1430 ;;* This function is used to prevent marking of words explicitly */
1431 ;;* declared correct. */
1432 ;;*---------------------------------------------------------------------*/
1433 (defun flyspell-process-localwords (misspellings-buffer)
1434 (let (localwords case-fold-search
1435 (ispell-casechars (ispell-get-casechars)))
1436 ;; Get localwords from the original buffer
1437 (save-excursion
1438 (goto-char (point-min))
1439 ;; Localwords parsing copied from ispell.el.
1440 (while (search-forward ispell-words-keyword nil t)
1441 (let ((end (save-excursion (end-of-line) (point)))
1442 string)
1443 ;; buffer-local words separated by a space, and can contain
1444 ;; any character other than a space. Not rigorous enough.
1445 (while (re-search-forward " *\\([^ ]+\\)" end t)
1446 (setq string (buffer-substring-no-properties (match-beginning 1)
1447 (match-end 1)))
1448 ;; This can fail when string contains a word with invalid chars.
1449 ;; Error handling needs to be added between Ispell and Emacs.
1450 (if (and (< 1 (length string))
1451 (equal 0 (string-match ispell-casechars string)))
1452 (push string localwords))))))
1453 ;; Remove localwords matches from misspellings-buffer.
1454 ;; The usual mechanism of communicating the local words to ispell
1455 ;; does not affect the special ispell process used by
1456 ;; flyspell-large-region.
1457 (with-current-buffer misspellings-buffer
1458 (save-excursion
1459 (dolist (word localwords)
1460 (goto-char (point-min))
1461 (let ((regexp (concat "^" word "\n")))
1462 (while (re-search-forward regexp nil t)
1463 (delete-region (match-beginning 0) (match-end 0)))))))))
1464
1465 ;;* ---------------------------------------------------------------
1466 ;;* flyspell-check-region-doublons
1467 ;;* ---------------------------------------------------------------
1468 (defun flyspell-check-region-doublons (beg end)
1469 "Check for adjacent duplicated words (doublons) in the given region."
1470 (save-excursion
1471 (goto-char beg)
1472 (flyspell-word) ; Make sure current word is checked
1473 (backward-word 1)
1474 (while (and (< (point) end)
1475 (re-search-forward "\\b\\([^ \n\t]+\\)[ \n\t]+\\1\\b"
1476 end 'move))
1477 (flyspell-word)
1478 (backward-word 1))
1479 (flyspell-word)))
1480
1481 ;;*---------------------------------------------------------------------*/
1482 ;;* flyspell-large-region ... */
1483 ;;*---------------------------------------------------------------------*/
1484 (defun flyspell-large-region (beg end)
1485 (let* ((curbuf (current-buffer))
1486 (buffer (get-buffer-create "*flyspell-region*")))
1487 (setq flyspell-external-ispell-buffer buffer)
1488 (setq flyspell-large-region-buffer curbuf)
1489 (setq flyspell-large-region-beg beg)
1490 (setq flyspell-large-region-end end)
1491 (flyspell-accept-buffer-local-defs)
1492 (set-buffer buffer)
1493 (erase-buffer)
1494 ;; this is done, we can start checking...
1495 (if flyspell-issue-message-flag (message "Checking region..."))
1496 (set-buffer curbuf)
1497 (ispell-check-version)
1498 (let ((c (apply 'call-process-region beg
1499 end
1500 ispell-program-name
1501 nil
1502 buffer
1503 nil
1504 (if ispell-really-aspell "list" "-l")
1505 (let (args)
1506 ;; Local dictionary becomes the global dictionary in use.
1507 (if ispell-local-dictionary
1508 (setq ispell-dictionary ispell-local-dictionary))
1509 (setq args (ispell-get-ispell-args))
1510 (if ispell-dictionary ; use specified dictionary
1511 (setq args
1512 (append (list "-d" ispell-dictionary) args)))
1513 (if ispell-personal-dictionary ; use specified pers dict
1514 (setq args
1515 (append args
1516 (list "-p"
1517 (expand-file-name
1518 ispell-personal-dictionary)))))
1519 (setq args (append args ispell-extra-args))
1520 args))))
1521 (if (eq c 0)
1522 (progn
1523 (flyspell-process-localwords buffer)
1524 (with-current-buffer curbuf
1525 (flyspell-delete-region-overlays beg end)
1526 (flyspell-check-region-doublons beg end))
1527 (flyspell-external-point-words))
1528 (error "Can't check region...")))))
1529
1530 ;;*---------------------------------------------------------------------*/
1531 ;;* flyspell-region ... */
1532 ;;* ------------------------------------------------------------- */
1533 ;;* Because `ispell -a' is too slow, it is not possible to use */
1534 ;;* it on large region. Then, when ispell is invoked on a large */
1535 ;;* text region, a new `ispell -l' process is spawned. The */
1536 ;;* pointed out words are then searched in the region a checked with */
1537 ;;* regular flyspell means. */
1538 ;;*---------------------------------------------------------------------*/
1539 ;;;###autoload
1540 (defun flyspell-region (beg end)
1541 "Flyspell text between BEG and END."
1542 (interactive "r")
1543 (if (= beg end)
1544 ()
1545 (save-excursion
1546 (if (> beg end)
1547 (let ((old beg))
1548 (setq beg end)
1549 (setq end old)))
1550 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1551 (flyspell-large-region beg end)
1552 (flyspell-small-region beg end)))))
1553
1554 ;;*---------------------------------------------------------------------*/
1555 ;;* flyspell-buffer ... */
1556 ;;*---------------------------------------------------------------------*/
1557 ;;;###autoload
1558 (defun flyspell-buffer ()
1559 "Flyspell whole buffer."
1560 (interactive)
1561 (flyspell-region (point-min) (point-max)))
1562
1563 ;;*---------------------------------------------------------------------*/
1564 ;;* old next error position ... */
1565 ;;*---------------------------------------------------------------------*/
1566 (defvar flyspell-old-buffer-error nil)
1567 (defvar flyspell-old-pos-error nil)
1568
1569 ;;*---------------------------------------------------------------------*/
1570 ;;* flyspell-goto-next-error ... */
1571 ;;*---------------------------------------------------------------------*/
1572 (defun flyspell-goto-next-error ()
1573 "Go to the next previously detected error.
1574 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1575 FLYSPELL-BUFFER."
1576 (interactive)
1577 (let ((pos (point))
1578 (max (point-max)))
1579 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1580 (eq pos flyspell-old-pos-error))
1581 (progn
1582 (if (= flyspell-old-pos-error max)
1583 ;; goto beginning of buffer
1584 (progn
1585 (message "Restarting from beginning of buffer")
1586 (goto-char (point-min)))
1587 (forward-word 1))
1588 (setq pos (point))))
1589 ;; seek the next error
1590 (while (and (< pos max)
1591 (let ((ovs (overlays-at pos))
1592 (r '()))
1593 (while (and (not r) (consp ovs))
1594 (if (flyspell-overlay-p (car ovs))
1595 (setq r t)
1596 (setq ovs (cdr ovs))))
1597 (not r)))
1598 (setq pos (1+ pos)))
1599 ;; save the current location for next invocation
1600 (setq flyspell-old-pos-error pos)
1601 (setq flyspell-old-buffer-error (current-buffer))
1602 (goto-char pos)
1603 (if (= pos max)
1604 (message "No more miss-spelled word!"))))
1605
1606 ;;*---------------------------------------------------------------------*/
1607 ;;* flyspell-overlay-p ... */
1608 ;;*---------------------------------------------------------------------*/
1609 (defun flyspell-overlay-p (o)
1610 "A predicate that return true iff O is an overlay used by flyspell."
1611 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1612
1613 ;;*---------------------------------------------------------------------*/
1614 ;;* flyspell-delete-region-overlays, flyspell-delete-all-overlays */
1615 ;;* ------------------------------------------------------------- */
1616 ;;* Remove overlays introduced by flyspell. */
1617 ;;*---------------------------------------------------------------------*/
1618 (defun flyspell-delete-region-overlays (beg end)
1619 "Delete overlays used by flyspell in a given region."
1620 (remove-overlays beg end 'flyspell-overlay t))
1621
1622
1623 (defun flyspell-delete-all-overlays ()
1624 "Delete all the overlays used by flyspell."
1625 (remove-overlays (point-min) (point-max) 'flyspell-overlay t))
1626
1627 ;;*---------------------------------------------------------------------*/
1628 ;;* flyspell-unhighlight-at ... */
1629 ;;*---------------------------------------------------------------------*/
1630 (defun flyspell-unhighlight-at (pos)
1631 "Remove the flyspell overlay that are located at POS."
1632 (if flyspell-persistent-highlight
1633 (let ((overlays (overlays-at pos)))
1634 (while (consp overlays)
1635 (if (flyspell-overlay-p (car overlays))
1636 (delete-overlay (car overlays)))
1637 (setq overlays (cdr overlays))))
1638 (if (flyspell-overlay-p flyspell-overlay)
1639 (delete-overlay flyspell-overlay))))
1640
1641 ;;*---------------------------------------------------------------------*/
1642 ;;* flyspell-properties-at-p ... */
1643 ;;* ------------------------------------------------------------- */
1644 ;;* Is there an highlight properties at position pos? */
1645 ;;*---------------------------------------------------------------------*/
1646 (defun flyspell-properties-at-p (pos)
1647 "Return t if there is a text property at POS, not counting `local-map'.
1648 If variable `flyspell-highlight-properties' is set to nil,
1649 text with properties are not checked. This function is used to discover
1650 if the character at POS has any other property."
1651 (let ((prop (text-properties-at pos))
1652 (keep t))
1653 (while (and keep (consp prop))
1654 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1655 (setq prop (cdr (cdr prop)))
1656 (setq keep nil)))
1657 (consp prop)))
1658
1659 ;;*---------------------------------------------------------------------*/
1660 ;;* make-flyspell-overlay ... */
1661 ;;*---------------------------------------------------------------------*/
1662 (defun make-flyspell-overlay (beg end face mouse-face)
1663 "Allocate an overlay to highlight an incorrect word.
1664 BEG and END specify the range in the buffer of that word.
1665 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1666 for the overlay."
1667 (let ((overlay (make-overlay beg end nil t nil)))
1668 (overlay-put overlay 'face face)
1669 (overlay-put overlay 'mouse-face mouse-face)
1670 (overlay-put overlay 'flyspell-overlay t)
1671 (overlay-put overlay 'evaporate t)
1672 (overlay-put overlay 'help-echo "mouse-2: correct word at point")
1673 (overlay-put overlay 'keymap flyspell-mouse-map)
1674 (when (eq face 'flyspell-incorrect)
1675 (and (stringp flyspell-before-incorrect-word-string)
1676 (overlay-put overlay 'before-string
1677 flyspell-before-incorrect-word-string))
1678 (and (stringp flyspell-after-incorrect-word-string)
1679 (overlay-put overlay 'after-string
1680 flyspell-after-incorrect-word-string)))
1681 overlay))
1682
1683 ;;*---------------------------------------------------------------------*/
1684 ;;* flyspell-highlight-incorrect-region ... */
1685 ;;*---------------------------------------------------------------------*/
1686 (defun flyspell-highlight-incorrect-region (beg end poss)
1687 "Set up an overlay on a misspelled word, in the buffer from BEG to END.
1688 POSS is usually a list of possible spelling/correction lists,
1689 as returned by `ispell-parse-output'.
1690 It can also be the symbol `doublon', in the case where the word
1691 is itself incorrect, but suspiciously repeated."
1692 (let ((inhibit-read-only t))
1693 (unless (run-hook-with-args-until-success
1694 'flyspell-incorrect-hook beg end poss)
1695 (if (or flyspell-highlight-properties
1696 (not (flyspell-properties-at-p beg)))
1697 (progn
1698 ;; we cleanup all the overlay that are in the region, not
1699 ;; beginning at the word start position
1700 (if (< (1+ beg) end)
1701 (let ((os (overlays-in (1+ beg) end)))
1702 (while (consp os)
1703 (if (flyspell-overlay-p (car os))
1704 (delete-overlay (car os)))
1705 (setq os (cdr os)))))
1706 ;; we cleanup current overlay at the same position
1707 (flyspell-unhighlight-at beg)
1708 ;; now we can use a new overlay
1709 (setq flyspell-overlay
1710 (make-flyspell-overlay
1711 beg end 'flyspell-incorrect 'highlight)))))))
1712
1713 ;;*---------------------------------------------------------------------*/
1714 ;;* flyspell-highlight-duplicate-region ... */
1715 ;;*---------------------------------------------------------------------*/
1716 (defun flyspell-highlight-duplicate-region (beg end poss)
1717 "Set up an overlay on a duplicate misspelled word, in the buffer from BEG to END.
1718 POSS is a list of possible spelling/correction lists,
1719 as returned by `ispell-parse-output'."
1720 (let ((inhibit-read-only t))
1721 (unless (run-hook-with-args-until-success
1722 'flyspell-incorrect-hook beg end poss)
1723 (if (or flyspell-highlight-properties
1724 (not (flyspell-properties-at-p beg)))
1725 (progn
1726 ;; we cleanup current overlay at the same position
1727 (flyspell-unhighlight-at beg)
1728 ;; now we can use a new overlay
1729 (setq flyspell-overlay
1730 (make-flyspell-overlay beg end
1731 'flyspell-duplicate
1732 'highlight)))))))
1733
1734 ;;*---------------------------------------------------------------------*/
1735 ;;* flyspell-auto-correct-cache ... */
1736 ;;*---------------------------------------------------------------------*/
1737 (defvar flyspell-auto-correct-pos nil)
1738 (defvar flyspell-auto-correct-region nil)
1739 (defvar flyspell-auto-correct-ring nil)
1740 (defvar flyspell-auto-correct-word nil)
1741 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1742 (make-variable-buffer-local 'flyspell-auto-correct-region)
1743 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1744 (make-variable-buffer-local 'flyspell-auto-correct-word)
1745
1746 ;;*---------------------------------------------------------------------*/
1747 ;;* flyspell-check-previous-highlighted-word ... */
1748 ;;*---------------------------------------------------------------------*/
1749 (defun flyspell-check-previous-highlighted-word (&optional arg)
1750 "Correct the closer misspelled word.
1751 This function scans a mis-spelled word before the cursor. If it finds one
1752 it proposes replacement for that word. With prefix arg, count that many
1753 misspelled words backwards."
1754 (interactive)
1755 (let ((pos1 (point))
1756 (pos (point))
1757 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1758 ov ovs)
1759 (if (catch 'exit
1760 (while (and (setq pos (previous-overlay-change pos))
1761 (not (= pos pos1)))
1762 (setq pos1 pos)
1763 (if (> pos (point-min))
1764 (progn
1765 (setq ovs (overlays-at (1- pos)))
1766 (while (consp ovs)
1767 (setq ov (car ovs))
1768 (setq ovs (cdr ovs))
1769 (if (and (flyspell-overlay-p ov)
1770 (= 0 (setq arg (1- arg))))
1771 (throw 'exit t)))))))
1772 (save-excursion
1773 (goto-char pos)
1774 (ispell-word))
1775 (error "No word to correct before point"))))
1776
1777 ;;*---------------------------------------------------------------------*/
1778 ;;* flyspell-display-next-corrections ... */
1779 ;;*---------------------------------------------------------------------*/
1780 (defun flyspell-display-next-corrections (corrections)
1781 (let ((string "Corrections:")
1782 (l corrections)
1783 (pos '()))
1784 (while (< (length string) 80)
1785 (if (equal (car l) flyspell-auto-correct-word)
1786 (setq pos (cons (+ 1 (length string)) pos)))
1787 (setq string (concat string " " (car l)))
1788 (setq l (cdr l)))
1789 (while (consp pos)
1790 (let ((num (car pos)))
1791 (put-text-property num
1792 (+ num (length flyspell-auto-correct-word))
1793 'face 'flyspell-incorrect
1794 string))
1795 (setq pos (cdr pos)))
1796 (if (fboundp 'display-message)
1797 (display-message 'no-log string)
1798 (message "%s" string))))
1799
1800 ;;*---------------------------------------------------------------------*/
1801 ;;* flyspell-abbrev-table ... */
1802 ;;*---------------------------------------------------------------------*/
1803 (defun flyspell-abbrev-table ()
1804 (if flyspell-use-global-abbrev-table-p
1805 global-abbrev-table
1806 (or local-abbrev-table global-abbrev-table)))
1807
1808 ;;*---------------------------------------------------------------------*/
1809 ;;* flyspell-define-abbrev ... */
1810 ;;*---------------------------------------------------------------------*/
1811 (defun flyspell-define-abbrev (name expansion)
1812 (let ((table (flyspell-abbrev-table)))
1813 (when table
1814 (define-abbrev table name expansion))))
1815
1816 ;;*---------------------------------------------------------------------*/
1817 ;;* flyspell-auto-correct-word ... */
1818 ;;*---------------------------------------------------------------------*/
1819 (defun flyspell-auto-correct-word ()
1820 "Correct the current word.
1821 This command proposes various successive corrections for the current word."
1822 (interactive)
1823 (let ((pos (point))
1824 (old-max (point-max)))
1825 ;; use the correct dictionary
1826 (flyspell-accept-buffer-local-defs)
1827 (if (and (eq flyspell-auto-correct-pos pos)
1828 (consp flyspell-auto-correct-region))
1829 ;; we have already been using the function at the same location
1830 (let* ((start (car flyspell-auto-correct-region))
1831 (len (cdr flyspell-auto-correct-region)))
1832 (flyspell-unhighlight-at start)
1833 (delete-region start (+ start len))
1834 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1835 (let* ((word (car flyspell-auto-correct-ring))
1836 (len (length word)))
1837 (rplacd flyspell-auto-correct-region len)
1838 (goto-char start)
1839 (if flyspell-abbrev-p
1840 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1841 flyspell-auto-correct-word)
1842 (flyspell-change-abbrev (flyspell-abbrev-table)
1843 flyspell-auto-correct-word
1844 word)
1845 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1846 (funcall flyspell-insert-function word)
1847 (flyspell-word)
1848 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1849 (flyspell-ajust-cursor-point pos (point) old-max)
1850 (setq flyspell-auto-correct-pos (point)))
1851 ;; fetch the word to be checked
1852 (let ((word (flyspell-get-word nil)))
1853 (if (consp word)
1854 (let ((start (car (cdr word)))
1855 (end (car (cdr (cdr word))))
1856 (word (car word))
1857 poss ispell-filter)
1858 (setq flyspell-auto-correct-word word)
1859 ;; now check spelling of word.
1860 (ispell-send-string "%\n") ;put in verbose mode
1861 (ispell-send-string (concat "^" word "\n"))
1862 ;; wait until ispell has processed word.
1863 (while (progn
1864 (accept-process-output ispell-process)
1865 (not (string= "" (car ispell-filter)))))
1866 ;; Remove leading empty element
1867 (setq ispell-filter (cdr ispell-filter))
1868 ;; ispell process should return something after word is sent.
1869 ;; Tag word as valid (i.e., skip) otherwise
1870 (or ispell-filter
1871 (setq ispell-filter '(*)))
1872 (if (consp ispell-filter)
1873 (setq poss (ispell-parse-output (car ispell-filter))))
1874 (cond
1875 ((or (eq poss t) (stringp poss))
1876 ;; don't correct word
1877 t)
1878 ((null poss)
1879 ;; ispell error
1880 (error "Ispell: error in Ispell process"))
1881 (t
1882 ;; the word is incorrect, we have to propose a replacement
1883 (let ((replacements (if flyspell-sort-corrections
1884 (sort (car (cdr (cdr poss))) 'string<)
1885 (car (cdr (cdr poss))))))
1886 (setq flyspell-auto-correct-region nil)
1887 (if (consp replacements)
1888 (progn
1889 (let ((replace (car replacements)))
1890 (let ((new-word replace))
1891 (if (not (equal new-word (car poss)))
1892 (progn
1893 ;; the save the current replacements
1894 (setq flyspell-auto-correct-region
1895 (cons start (length new-word)))
1896 (let ((l replacements))
1897 (while (consp (cdr l))
1898 (setq l (cdr l)))
1899 (rplacd l (cons (car poss) replacements)))
1900 (setq flyspell-auto-correct-ring
1901 replacements)
1902 (flyspell-unhighlight-at start)
1903 (delete-region start end)
1904 (funcall flyspell-insert-function new-word)
1905 (if flyspell-abbrev-p
1906 (if (flyspell-already-abbrevp
1907 (flyspell-abbrev-table) word)
1908 (flyspell-change-abbrev
1909 (flyspell-abbrev-table)
1910 word
1911 new-word)
1912 (flyspell-define-abbrev word
1913 new-word)))
1914 (flyspell-word)
1915 (flyspell-display-next-corrections
1916 (cons new-word flyspell-auto-correct-ring))
1917 (flyspell-ajust-cursor-point pos
1918 (point)
1919 old-max))))))))))
1920 (setq flyspell-auto-correct-pos (point))
1921 (ispell-pdict-save t)))))))
1922
1923 ;;*---------------------------------------------------------------------*/
1924 ;;* flyspell-auto-correct-previous-pos ... */
1925 ;;*---------------------------------------------------------------------*/
1926 (defvar flyspell-auto-correct-previous-pos nil
1927 "Holds the start of the first incorrect word before point.")
1928
1929 ;;*---------------------------------------------------------------------*/
1930 ;;* flyspell-auto-correct-previous-hook ... */
1931 ;;*---------------------------------------------------------------------*/
1932 (defun flyspell-auto-correct-previous-hook ()
1933 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1934 Sets `flyspell-auto-correct-previous-pos' to nil"
1935 (interactive)
1936 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1937 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1938 (setq flyspell-auto-correct-previous-pos nil)))
1939
1940 ;;*---------------------------------------------------------------------*/
1941 ;;* flyspell-auto-correct-previous-word ... */
1942 ;;*---------------------------------------------------------------------*/
1943 (defun flyspell-auto-correct-previous-word (position)
1944 "Auto correct the first mispelled word that occurs before point.
1945 But don't look beyond what's visible on the screen."
1946 (interactive "d")
1947
1948 (let (top bot)
1949 (save-excursion
1950 (move-to-window-line 0)
1951 (setq top (point))
1952 (move-to-window-line -1)
1953 (setq bot (point)))
1954 (save-excursion
1955 (save-restriction
1956 (narrow-to-region top bot)
1957 (overlay-recenter (point))
1958
1959 (add-hook 'pre-command-hook
1960 (function flyspell-auto-correct-previous-hook) t t)
1961
1962 (unless flyspell-auto-correct-previous-pos
1963 ;; only reset if a new overlay exists
1964 (setq flyspell-auto-correct-previous-pos nil)
1965
1966 (let ((overlay-list (overlays-in (point-min) position))
1967 (new-overlay 'dummy-value))
1968
1969 ;; search for previous (new) flyspell overlay
1970 (while (and new-overlay
1971 (or (not (flyspell-overlay-p new-overlay))
1972 ;; check if its face has changed
1973 (not (eq (get-char-property
1974 (overlay-start new-overlay) 'face)
1975 'flyspell-incorrect))))
1976 (setq new-overlay (car-safe overlay-list))
1977 (setq overlay-list (cdr-safe overlay-list)))
1978
1979 ;; if nothing new exits new-overlay should be nil
1980 (if new-overlay ;; the length of the word may change so go to the start
1981 (setq flyspell-auto-correct-previous-pos
1982 (overlay-start new-overlay)))))
1983
1984 (when flyspell-auto-correct-previous-pos
1985 (save-excursion
1986 (goto-char flyspell-auto-correct-previous-pos)
1987 (let ((ispell-following-word t)) ;; point is at start
1988 (if (numberp flyspell-auto-correct-previous-pos)
1989 (goto-char flyspell-auto-correct-previous-pos))
1990 (flyspell-auto-correct-word))
1991 ;; the point may have moved so reset this
1992 (setq flyspell-auto-correct-previous-pos (point))))))))
1993
1994 ;;*---------------------------------------------------------------------*/
1995 ;;* flyspell-correct-word ... */
1996 ;;*---------------------------------------------------------------------*/
1997 (defun flyspell-correct-word (event)
1998 "Pop up a menu of possible corrections for a misspelled word.
1999 The word checked is the word at the mouse position."
2000 (interactive "e")
2001 ;; use the correct dictionary
2002 (flyspell-accept-buffer-local-defs)
2003 ;; retain cursor location (I don't know why but save-excursion here fails).
2004 (let ((save (point)))
2005 (mouse-set-point event)
2006 (let ((cursor-location (point))
2007 (word (flyspell-get-word nil)))
2008 (if (consp word)
2009 (let ((start (car (cdr word)))
2010 (end (car (cdr (cdr word))))
2011 (word (car word))
2012 poss ispell-filter)
2013 ;; now check spelling of word.
2014 (ispell-send-string "%\n") ;put in verbose mode
2015 (ispell-send-string (concat "^" word "\n"))
2016 ;; wait until ispell has processed word
2017 (while (progn
2018 (accept-process-output ispell-process)
2019 (not (string= "" (car ispell-filter)))))
2020 ;; Remove leading empty element
2021 (setq ispell-filter (cdr ispell-filter))
2022 ;; ispell process should return something after word is sent.
2023 ;; Tag word as valid (i.e., skip) otherwise
2024 (or ispell-filter
2025 (setq ispell-filter '(*)))
2026 (if (consp ispell-filter)
2027 (setq poss (ispell-parse-output (car ispell-filter))))
2028 (cond
2029 ((or (eq poss t) (stringp poss))
2030 ;; don't correct word
2031 t)
2032 ((null poss)
2033 ;; ispell error
2034 (error "Ispell: error in Ispell process"))
2035 ((featurep 'xemacs)
2036 (flyspell-xemacs-popup
2037 poss word cursor-location start end save))
2038 (t
2039 ;; The word is incorrect, we have to propose a replacement.
2040 (flyspell-do-correct (flyspell-emacs-popup event poss word)
2041 poss word cursor-location start end save)))
2042 (ispell-pdict-save t))))))
2043
2044 ;;*---------------------------------------------------------------------*/
2045 ;;* flyspell-do-correct ... */
2046 ;;*---------------------------------------------------------------------*/
2047 (defun flyspell-do-correct (replace poss word cursor-location start end save)
2048 "The popup menu callback."
2049 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
2050 ;; it instead right after calling the function.
2051 (cond ((eq replace 'ignore)
2052 (goto-char save)
2053 nil)
2054 ((eq replace 'save)
2055 (goto-char save)
2056 (ispell-send-string (concat "*" word "\n"))
2057 ;; This was added only to the XEmacs side in revision 1.18 of
2058 ;; flyspell. I assume its absence on the Emacs side was an
2059 ;; oversight. --Stef
2060 (ispell-send-string "#\n")
2061 (flyspell-unhighlight-at cursor-location)
2062 (setq ispell-pdict-modified-p '(t)))
2063 ((or (eq replace 'buffer) (eq replace 'session))
2064 (ispell-send-string (concat "@" word "\n"))
2065 (flyspell-unhighlight-at cursor-location)
2066 (if (null ispell-pdict-modified-p)
2067 (setq ispell-pdict-modified-p
2068 (list ispell-pdict-modified-p)))
2069 (goto-char save)
2070 (if (eq replace 'buffer)
2071 (ispell-add-per-file-word-list word)))
2072 (replace
2073 ;; This was added only to the Emacs side. I assume its absence on
2074 ;; the XEmacs side was an oversight. --Stef
2075 (flyspell-unhighlight-at cursor-location)
2076 (let ((old-max (point-max))
2077 (new-word (if (atom replace)
2078 replace
2079 (car replace)))
2080 (cursor-location (+ (- (length word) (- end start))
2081 cursor-location)))
2082 (unless (equal new-word (car poss))
2083 (delete-region start end)
2084 (goto-char start)
2085 (funcall flyspell-insert-function new-word)
2086 (if flyspell-abbrev-p
2087 (flyspell-define-abbrev word new-word)))
2088 ;; In the original Emacs code, this was only called in the body
2089 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
2090 (flyspell-ajust-cursor-point save cursor-location old-max)))
2091 (t
2092 (goto-char save)
2093 nil)))
2094
2095 ;;*---------------------------------------------------------------------*/
2096 ;;* flyspell-ajust-cursor-point ... */
2097 ;;*---------------------------------------------------------------------*/
2098 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
2099 (if (>= save cursor-location)
2100 (let ((new-pos (+ save (- (point-max) old-max))))
2101 (goto-char (cond
2102 ((< new-pos (point-min))
2103 (point-min))
2104 ((> new-pos (point-max))
2105 (point-max))
2106 (t new-pos))))
2107 (goto-char save)))
2108
2109 ;;*---------------------------------------------------------------------*/
2110 ;;* flyspell-emacs-popup ... */
2111 ;;*---------------------------------------------------------------------*/
2112 (defun flyspell-emacs-popup (event poss word)
2113 "The Emacs popup menu."
2114 (if (not event)
2115 (let* ((mouse-pos (mouse-position))
2116 (mouse-pos (if (nth 1 mouse-pos)
2117 mouse-pos
2118 (set-mouse-position (car mouse-pos)
2119 (/ (frame-width) 2) 2)
2120 (mouse-position))))
2121 (setq event (list (list (car (cdr mouse-pos))
2122 (1+ (cdr (cdr mouse-pos))))
2123 (car mouse-pos)))))
2124 (let* ((corrects (if flyspell-sort-corrections
2125 (sort (car (cdr (cdr poss))) 'string<)
2126 (car (cdr (cdr poss)))))
2127 (cor-menu (if (consp corrects)
2128 (mapcar (lambda (correct)
2129 (list correct correct))
2130 corrects)
2131 '()))
2132 (affix (car (cdr (cdr (cdr poss)))))
2133 show-affix-info
2134 (base-menu (let ((save (if (and (consp affix) show-affix-info)
2135 (list
2136 (list (concat "Save affix: " (car affix))
2137 'save)
2138 '("Accept (session)" session)
2139 '("Accept (buffer)" buffer))
2140 '(("Save word" save)
2141 ("Accept (session)" session)
2142 ("Accept (buffer)" buffer)))))
2143 (if (consp cor-menu)
2144 (append cor-menu (cons "" save))
2145 save)))
2146 (menu (cons "flyspell correction menu" base-menu)))
2147 (car (x-popup-menu event
2148 (list (format "%s [%s]" word (or ispell-local-dictionary
2149 ispell-dictionary))
2150 menu)))))
2151
2152 ;;*---------------------------------------------------------------------*/
2153 ;;* flyspell-xemacs-popup ... */
2154 ;;*---------------------------------------------------------------------*/
2155 (defun flyspell-xemacs-popup (poss word cursor-location start end save)
2156 "The XEmacs popup menu."
2157 (let* ((corrects (if flyspell-sort-corrections
2158 (sort (car (cdr (cdr poss))) 'string<)
2159 (car (cdr (cdr poss)))))
2160 (cor-menu (if (consp corrects)
2161 (mapcar (lambda (correct)
2162 (vector correct
2163 (list 'flyspell-do-correct
2164 correct
2165 (list 'quote poss)
2166 word
2167 cursor-location
2168 start
2169 end
2170 save)
2171 t))
2172 corrects)
2173 '()))
2174 (affix (car (cdr (cdr (cdr poss)))))
2175 show-affix-info
2176 (menu (let ((save (if (and (consp affix) show-affix-info)
2177 (vector
2178 (concat "Save affix: " (car affix))
2179 (list 'flyspell-do-correct
2180 ''save
2181 (list 'quote poss)
2182 word
2183 cursor-location
2184 start
2185 end
2186 save)
2187 t)
2188 (vector
2189 "Save word"
2190 (list 'flyspell-do-correct
2191 ''save
2192 (list 'quote poss)
2193 word
2194 cursor-location
2195 start
2196 end
2197 save)
2198 t)))
2199 (session (vector "Accept (session)"
2200 (list 'flyspell-do-correct
2201 ''session
2202 (list 'quote poss)
2203 word
2204 cursor-location
2205 start
2206 end
2207 save)
2208 t))
2209 (buffer (vector "Accept (buffer)"
2210 (list 'flyspell-do-correct
2211 ''buffer
2212 (list 'quote poss)
2213 word
2214 cursor-location
2215 start
2216 end
2217 save)
2218 t)))
2219 (if (consp cor-menu)
2220 (append cor-menu (list "-" save session buffer))
2221 (list save session buffer)))))
2222 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2223 ispell-dictionary))
2224 menu))))
2225
2226 ;;*---------------------------------------------------------------------*/
2227 ;;* Some example functions for real autocorrecting */
2228 ;;*---------------------------------------------------------------------*/
2229 (defun flyspell-maybe-correct-transposition (beg end poss)
2230 "Check replacements for transposed characters.
2231
2232 If the text between BEG and END is equal to a correction suggested by
2233 Ispell, after transposing two adjacent characters, correct the text,
2234 and return t.
2235
2236 The third arg POSS is either the symbol 'doublon' or a list of
2237 possible corrections as returned by `ispell-parse-output'.
2238
2239 This function is meant to be added to `flyspell-incorrect-hook'."
2240 (when (consp poss)
2241 (catch 'done
2242 (let ((str (buffer-substring beg end))
2243 (i 0) (len (- end beg)) tmp)
2244 (while (< (1+ i) len)
2245 (setq tmp (aref str i))
2246 (aset str i (aref str (1+ i)))
2247 (aset str (1+ i) tmp)
2248 (when (member str (nth 2 poss))
2249 (save-excursion
2250 (goto-char (+ beg i 1))
2251 (transpose-chars 1))
2252 (throw 'done t))
2253 (setq tmp (aref str i))
2254 (aset str i (aref str (1+ i)))
2255 (aset str (1+ i) tmp)
2256 (setq i (1+ i))))
2257 nil)))
2258
2259 (defun flyspell-maybe-correct-doubling (beg end poss)
2260 "Check replacements for doubled characters.
2261
2262 If the text between BEG and END is equal to a correction suggested by
2263 Ispell, after removing a pair of doubled characters, correct the text,
2264 and return t.
2265
2266 The third arg POSS is either the symbol 'doublon' or a list of
2267 possible corrections as returned by `ispell-parse-output'.
2268
2269 This function is meant to be added to `flyspell-incorrect-hook'."
2270 (when (consp poss)
2271 (catch 'done
2272 (let ((str (buffer-substring beg end))
2273 (i 0) (len (- end beg)))
2274 (while (< (1+ i) len)
2275 (when (and (= (aref str i) (aref str (1+ i)))
2276 (member (concat (substring str 0 (1+ i))
2277 (substring str (+ i 2)))
2278 (nth 2 poss)))
2279 (goto-char (+ beg i))
2280 (delete-char 1)
2281 (throw 'done t))
2282 (setq i (1+ i))))
2283 nil)))
2284
2285 ;;*---------------------------------------------------------------------*/
2286 ;;* flyspell-already-abbrevp ... */
2287 ;;*---------------------------------------------------------------------*/
2288 (defun flyspell-already-abbrevp (table word)
2289 (let ((sym (abbrev-symbol word table)))
2290 (and sym (symbolp sym))))
2291
2292 ;;*---------------------------------------------------------------------*/
2293 ;;* flyspell-change-abbrev ... */
2294 ;;*---------------------------------------------------------------------*/
2295 (defun flyspell-change-abbrev (table old new)
2296 (set (abbrev-symbol old table) new))
2297
2298 (provide 'flyspell)
2299
2300 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2301 ;;; flyspell.el ends here