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