1 ;;; flyspell.el --- on-the-fly spell checker
3 ;; Copyright (C) 1998, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Manuel Serrano <Manuel.Serrano@sophia.inria.fr>
8 ;; Keywords: convenience
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Flyspell is a minor Emacs mode performing on-the-fly spelling
32 ;; To enable Flyspell minor mode, type M-x flyspell-mode.
33 ;; This applies only to the current buffer.
35 ;; To enable Flyspell in text representing computer programs, type
36 ;; M-x flyspell-prog-mode.
37 ;; In that mode only text inside comments is checked.
39 ;; Note: consider setting the variable ispell-parser to `tex' to
40 ;; avoid TeX command checking; use `(setq ispell-parser 'tex)'.
42 ;; Some user variables control the behavior of flyspell. They are
43 ;; those defined under the `User variables' comment.
49 ;*---------------------------------------------------------------------*/
51 ;*---------------------------------------------------------------------*/
52 (defgroup flyspell nil
53 "Spell checking on the fly."
59 ;*---------------------------------------------------------------------*/
60 ;* Which emacs are we currently running */
61 ;*---------------------------------------------------------------------*/
62 (defvar flyspell-emacs
64 ((string-match "XEmacs" emacs-version
)
68 "The type of Emacs we are currently running.")
70 ;*---------------------------------------------------------------------*/
71 ;* User configuration ... */
72 ;*---------------------------------------------------------------------*/
73 (defcustom flyspell-highlight-flag t
74 "*How Flyspell should indicate misspelled words.
75 Non-nil means use highlight, nil means use minibuffer messages."
79 (defcustom flyspell-mark-duplications-flag t
80 "*Non-nil means Flyspell reports a repeated word as an error.
81 Detection of repeated words is not implemented in
82 \"large\" regions; see `flyspell-large-region'."
86 (defcustom flyspell-sort-corrections nil
87 "*Non-nil means, sort the corrections alphabetically before popping them."
92 (defcustom flyspell-duplicate-distance -
1
93 "*The maximum distance for finding duplicates of unrecognized words.
94 This applies to the feature that when a word is not found in the dictionary,
95 if the same spelling occurs elsewhere in the buffer,
96 Flyspell uses a different face (`flyspell-duplicate') to highlight it.
97 This variable specifies how far to search to find such a duplicate.
98 -1 means no limit (search the whole buffer).
99 0 means do not search for duplicate unrecognized spellings."
104 (defcustom flyspell-delay
3
105 "*The number of seconds to wait before checking, after a \"delayed\" command."
109 (defcustom flyspell-persistent-highlight t
110 "*Non-nil means misspelled words remain highlighted until corrected.
111 If this variable is nil, only the most recently detected misspelled word
116 (defcustom flyspell-highlight-properties t
117 "*Non-nil means highlight incorrect words even if a property exists for this word."
121 (defcustom flyspell-default-delayed-commands
122 '(self-insert-command
124 backward-or-forward-delete-char
126 scrollbar-vertical-drag
127 backward-delete-char-untabify
)
128 "The standard list of delayed commands for Flyspell.
129 See `flyspell-delayed-commands'."
132 :type
'(repeat (symbol)))
134 (defcustom flyspell-delayed-commands nil
135 "List of commands that are \"delayed\" for Flyspell mode.
136 After these commands, Flyspell checking is delayed for a short time,
137 whose length is specified by `flyspell-delay'."
139 :type
'(repeat (symbol)))
141 (defcustom flyspell-default-deplacement-commands
146 "The standard list of deplacement commands for Flyspell.
147 See `flyspell-deplacement-commands'."
150 :type
'(repeat (symbol)))
152 (defcustom flyspell-deplacement-commands nil
153 "List of commands that are \"deplacement\" for Flyspell mode.
154 After these commands, Flyspell checking is performed only if the previous
155 command was not the very same command."
158 :type
'(repeat (symbol)))
160 (defcustom flyspell-issue-welcome-flag t
161 "*Non-nil means that Flyspell should display a welcome message when started."
165 (defcustom flyspell-issue-message-flag t
166 "*Non-nil means that Flyspell emits messages when checking words."
170 (defcustom flyspell-incorrect-hook nil
171 "*List of functions to be called when incorrect words are encountered.
172 Each function is given three arguments: the beginning and the end
173 of the incorrect region. The third is either the symbol 'doublon' or the list
174 of possible corrections as returned by `ispell-parse-output'.
176 If any of the functions return non-Nil, the word is not highlighted as
182 (defcustom flyspell-default-dictionary nil
183 "A string that is the name of the default dictionary.
184 This is passed to the `ispell-change-dictionary' when flyspell is started.
185 If the variable `ispell-local-dictionary' or `ispell-dictionary' is non-nil
186 when flyspell is started, the value of that variable is used instead
187 of `flyspell-default-dictionary' to select the default dictionary.
188 Otherwise, if `flyspell-default-dictionary' is nil, it means to use
189 Ispell's ultimate default dictionary."
192 :type
'(choice string
(const :tag
"Default" nil
)))
194 (defcustom flyspell-tex-command-regexp
195 "\\(\\(begin\\|end\\)[ \t]*{\\|\\(cite[a-z*]*\\|label\\|ref\\|eqref\\|usepackage\\|documentclass\\)[ \t]*\\(\\[[^]]*\\]\\)?{[^{}]*\\)"
196 "A string that is the regular expression that matches TeX commands."
201 (defcustom flyspell-check-tex-math-command nil
202 "*Non nil means check even inside TeX math environment.
203 TeX math environments are discovered by the TEXMATHP that implemented
204 inside the texmathp.el Emacs package. That package may be found at:
205 http://strw.leidenuniv.nl/~dominik/Tools"
209 (defcustom flyspell-dictionaries-that-consider-dash-as-word-delimiter
210 '("francais" "deutsch8" "norsk")
211 "List of dictionary names that consider `-' as word delimiter."
214 :type
'(repeat (string)))
216 (defcustom flyspell-abbrev-p
218 "*If non-nil, add correction to abbreviation table."
223 (defcustom flyspell-use-global-abbrev-table-p
225 "*If non-nil, prefer global abbrev table to local abbrev table."
230 (defcustom flyspell-mode-line-string
" Fly"
231 "*String displayed on the modeline when flyspell is active.
232 Set this to nil if you don't want a modeline indicator."
234 :type
'(choice string
(const :tag
"None" nil
)))
236 (defcustom flyspell-large-region
1000
237 "*The threshold that determines if a region is small.
238 If the region is smaller than this number of characters,
239 `flyspell-region' checks the words sequentially using regular
240 flyspell methods. Else, if the region is large, a new Ispell process is
243 Doubled words are not detected in a large region, because Ispell
244 does not check for them.
246 If `flyspell-large-region' is nil, all regions are treated as small."
249 :type
'(choice number
(const :tag
"All small" nil
)))
251 (defcustom flyspell-insert-function
(function insert
)
252 "*Function for inserting word by flyspell upon correction."
256 (defcustom flyspell-before-incorrect-word-string nil
257 "String used to indicate an incorrect word starting."
259 :type
'(choice string
(const nil
)))
261 (defcustom flyspell-after-incorrect-word-string nil
262 "String used to indicate an incorrect word ending."
264 :type
'(choice string
(const nil
)))
266 (defcustom flyspell-use-meta-tab t
267 "*Non-nil means that flyspell uses META-TAB to correct word."
271 (defcustom flyspell-auto-correct-binding
273 "The key binding for flyspell auto correction."
276 ;*---------------------------------------------------------------------*/
277 ;* Mode specific options */
278 ;* ------------------------------------------------------------- */
279 ;* Mode specific options enable users to disable flyspell on */
280 ;* certain word depending of the emacs mode. For instance, when */
281 ;* using flyspell with mail-mode add the following expression */
282 ;* in your .emacs file: */
283 ;* (add-hook 'mail-mode */
284 ;* '(lambda () (setq flyspell-generic-check-word-p */
285 ;* 'mail-mode-flyspell-verify))) */
286 ;*---------------------------------------------------------------------*/
287 (defvar flyspell-generic-check-word-p nil
288 "Function providing per-mode customization over which words are flyspelled.
289 Returns t to continue checking, nil otherwise.
290 Flyspell mode sets this variable to whatever is the `flyspell-mode-predicate'
291 property of the major mode name.")
292 (make-variable-buffer-local 'flyspell-generic-check-word-p
)
294 ;*--- mail mode -------------------------------------------------------*/
295 (put 'mail-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
296 (put 'message-mode
'flyspell-mode-predicate
'mail-mode-flyspell-verify
)
297 (defun mail-mode-flyspell-verify ()
298 "This function is used for `flyspell-generic-check-word-p' in Mail mode."
299 (let ((header-end (save-excursion
300 (goto-char (point-min))
303 (regexp-quote mail-header-separator
)
307 (signature-begin (save-excursion
308 (goto-char (point-max))
309 (re-search-backward message-signature-separator
312 (cond ((< (point) header-end
)
313 (and (save-excursion (beginning-of-line)
314 (looking-at "^Subject:"))
315 (> (point) (match-end 0))))
316 ((> (point) signature-begin
)
321 (not (looking-at "[>}|]\\|To:")))))))
323 ;*--- texinfo mode ----------------------------------------------------*/
324 (put 'texinfo-mode
'flyspell-mode-predicate
'texinfo-mode-flyspell-verify
)
325 (defun texinfo-mode-flyspell-verify ()
326 "This function is used for `flyspell-generic-check-word-p' in Texinfo mode."
329 (not (looking-at "@"))))
331 ;*--- tex mode --------------------------------------------------------*/
332 (put 'tex-mode
'flyspell-mode-predicate
'tex-mode-flyspell-verify
)
333 (defun tex-mode-flyspell-verify ()
334 "This function is used for `flyspell-generic-check-word-p' in LaTeX mode."
337 (re-search-backward "^[ \t]*%%%[ \t]+Local" (point-min) t
)))
339 (let ((this (point-marker))
340 (e (progn (end-of-line) (point-marker))))
342 (if (re-search-forward "\\\\\\(cite\\|label\\|ref\\){[^}]*}" e t
)
343 (and (>= this
(match-beginning 0))
344 (<= this
(match-end 0)) )))))))
346 ;*--- sgml mode -------------------------------------------------------*/
347 (put 'sgml-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
348 (put 'html-mode
'flyspell-mode-predicate
'sgml-mode-flyspell-verify
)
350 (defun sgml-mode-flyspell-verify ()
351 "This function is used for `flyspell-generic-check-word-p' in SGML mode."
353 (let ((this (point-marker))
354 (s (progn (beginning-of-line) (point-marker)))
355 (e (progn (end-of-line) (point-marker))))
358 (and (re-search-forward "[^<]*>" e t
)
359 (= (match-beginning 0) this
)))
362 (and (re-search-backward "<[^>]*" s t
)
363 (= (match-end 0) this
)))
366 (and (re-search-forward "[^&]*;" e t
)
367 (= (match-beginning 0) this
)))
370 (and (re-search-backward "&[^;]*" s t
)
371 (= (match-end 0) this
)))))))))
373 ;*---------------------------------------------------------------------*/
374 ;* Programming mode */
375 ;*---------------------------------------------------------------------*/
376 (defvar flyspell-prog-text-faces
377 '(font-lock-string-face font-lock-comment-face font-lock-doc-face
)
378 "Faces corresponding to text in programming-mode buffers.")
380 (defun flyspell-generic-progmode-verify ()
381 "Used for `flyspell-generic-check-word-p' in programming modes."
382 (let ((f (get-text-property (point) 'face
)))
383 (memq f flyspell-prog-text-faces
)))
386 (defun flyspell-prog-mode ()
387 "Turn on `flyspell-mode' for comments and strings."
389 (setq flyspell-generic-check-word-p
'flyspell-generic-progmode-verify
)
391 (run-hooks 'flyspell-prog-mode-hook
))
393 ;*---------------------------------------------------------------------*/
394 ;* Overlay compatibility */
395 ;*---------------------------------------------------------------------*/
396 (autoload 'make-overlay
"overlay" "Overlay compatibility kit." t
)
397 (autoload 'overlayp
"overlay" "Overlay compatibility kit." t
)
398 (autoload 'overlays-in
"overlay" "Overlay compatibility kit." t
)
399 (autoload 'delete-overlay
"overlay" "Overlay compatibility kit." t
)
400 (autoload 'overlays-at
"overlay" "Overlay compatibility kit." t
)
401 (autoload 'overlay-put
"overlay" "Overlay compatibility kit." t
)
402 (autoload 'overlay-get
"overlay" "Overlay compatibility kit." t
)
403 (autoload 'previous-overlay-change
"overlay" "Overlay compatibility kit." t
)
405 ;*---------------------------------------------------------------------*/
406 ;* The minor mode declaration. */
407 ;*---------------------------------------------------------------------*/
408 (defvar flyspell-mouse-map
409 (let ((map (make-sparse-keymap)))
410 (define-key map
(if (featurep 'xemacs
) [button2] [down-mouse-2])
411 #'flyspell-correct-word)
413 "Keymap for Flyspell to put on erroneous words.")
415 (defvar flyspell-mode-map
416 (let ((map (make-sparse-keymap)))
417 (if flyspell-use-meta-tab
418 (define-key map "\M-\t" 'flyspell-auto-correct-word))
419 (define-key map flyspell-auto-correct-binding 'flyspell-auto-correct-previous-word)
420 (define-key map [(control ?\,)] 'flyspell-goto-next-error)
421 (define-key map [(control ?\.)] 'flyspell-auto-correct-word)
423 "Minor mode keymap for Flyspell mode--for the whole buffer.")
425 ;; dash character machinery
426 (defvar flyspell-consider-dash-as-word-delimiter-flag nil
427 "*Non-nil means that the `-' char is considered as a word delimiter.")
428 (make-variable-buffer-local 'flyspell-consider-dash-as-word-delimiter-flag)
429 (defvar flyspell-dash-dictionary nil)
430 (make-variable-buffer-local 'flyspell-dash-dictionary)
431 (defvar flyspell-dash-local-dictionary nil)
432 (make-variable-buffer-local 'flyspell-dash-local-dictionary)
434 ;*---------------------------------------------------------------------*/
436 ;*---------------------------------------------------------------------*/
437 (defface flyspell-incorrect
438 '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
440 "Face used for marking a misspelled word in Flyspell."
442 ;; backward-compatibility alias
443 (put 'flyspell-incorrect-face 'face-alias 'flyspell-incorrect)
445 (defface flyspell-duplicate
446 '((((class color)) (:foreground "Gold3" :bold t :underline t))
448 "Face used for marking a misspelled word that appears twice in the buffer.
449 See also `flyspell-duplicate-distance'."
451 ;; backward-compatibility alias
452 (put 'flyspell-duplicate-face 'face-alias 'flyspell-duplicate)
454 (defvar flyspell-overlay nil)
456 ;*---------------------------------------------------------------------*/
457 ;* flyspell-mode ... */
458 ;*---------------------------------------------------------------------*/
459 ;;;###autoload(defvar flyspell-mode nil)
461 (define-minor-mode flyspell-mode
462 "Minor mode performing on-the-fly spelling checking.
463 This spawns a single Ispell process and checks each word.
464 The default flyspell behavior is to highlight incorrect words.
465 With no argument, this command toggles Flyspell mode.
466 With a prefix argument ARG, turn Flyspell minor mode on iff ARG is positive.
469 \\[ispell-word]: correct words (using Ispell).
470 \\[flyspell-auto-correct-word]: automatically correct word.
471 \\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.
472 \\[flyspell-correct-word] (or down-mouse-2): popup correct words.
475 This runs `flyspell-mode-hook' after flyspell is entered.
478 `flyspell-mode' uses `ispell-mode'. Thus all Ispell options are
479 valid. For instance, a personal dictionary can be used by
480 invoking `ispell-change-dictionary'.
482 Consider using the `ispell-parser' to check your text. For instance
484 \(add-hook 'tex-mode-hook (function (lambda () (setq ispell-parser 'tex))))
487 \\[flyspell-region] checks all words inside a region.
488 \\[flyspell-buffer] checks the whole buffer."
489 :lighter flyspell-mode-line-string
490 :keymap flyspell-mode-map
494 (flyspell-mode-off)))
496 ;*---------------------------------------------------------------------*/
497 ;* flyspell-buffers ... */
498 ;* ------------------------------------------------------------- */
499 ;* For remembering buffers running flyspell */
500 ;*---------------------------------------------------------------------*/
501 (defvar flyspell-buffers nil)
503 ;*---------------------------------------------------------------------*/
504 ;* flyspell-minibuffer-p ... */
505 ;*---------------------------------------------------------------------*/
506 (defun flyspell-minibuffer-p (buffer)
507 "Is BUFFER a minibuffer?"
508 (let ((ws (get-buffer-window-list buffer t)))
509 (and (consp ws) (window-minibuffer-p (car ws)))))
511 ;*---------------------------------------------------------------------*/
512 ;* flyspell-accept-buffer-local-defs ... */
513 ;*---------------------------------------------------------------------*/
514 (defun flyspell-accept-buffer-local-defs ()
515 ;; strange problem. If buffer in current window has font-lock turned on,
516 ;; but SET-BUFFER was called to point to an invisible buffer, this ispell
517 ;; call will reset the buffer to the buffer in the current window. However,
518 ;; it only happens at startup (fix by Albert L. Ting).
519 (let ((buf (current-buffer)))
520 (ispell-accept-buffer-local-defs)
522 (if (not (and (eq flyspell-dash-dictionary ispell-dictionary)
523 (eq flyspell-dash-local-dictionary ispell-local-dictionary)))
524 ;; The dictionary has changed
526 (setq flyspell-dash-dictionary ispell-dictionary)
527 (setq flyspell-dash-local-dictionary ispell-local-dictionary)
528 (if (member (or ispell-local-dictionary ispell-dictionary)
529 flyspell-dictionaries-that-consider-dash-as-word-delimiter)
530 (setq flyspell-consider-dash-as-word-delimiter-flag t)
531 (setq flyspell-consider-dash-as-word-delimiter-flag nil)))))
533 ;*---------------------------------------------------------------------*/
534 ;* flyspell-mode-on ... */
535 ;*---------------------------------------------------------------------*/
536 (defun flyspell-mode-on ()
537 "Turn Flyspell mode on. Do not use this; use `flyspell-mode' instead."
538 (setq ispell-highlight-face 'flyspell-incorrect)
539 ;; local dictionaries setup
540 (or ispell-local-dictionary ispell-dictionary
541 (if flyspell-default-dictionary
542 (ispell-change-dictionary flyspell-default-dictionary)))
543 ;; we have to force ispell to accept the local definition or
544 ;; otherwise it could be too late, the local dictionary may
546 (flyspell-accept-buffer-local-defs)
547 ;; we put the `flyspell-delayed' property on some commands
548 (flyspell-delay-commands)
549 ;; we put the `flyspell-deplacement' property on some commands
550 (flyspell-deplacement-commands)
551 ;; we bound flyspell action to post-command hook
552 (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
553 ;; we bound flyspell action to pre-command hook
554 (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
555 ;; we bound flyspell action to after-change hook
556 (make-local-variable 'after-change-functions)
557 (setq after-change-functions
558 (cons 'flyspell-after-change-function after-change-functions))
559 ;; set flyspell-generic-check-word-p based on the major mode
560 (let ((mode-predicate (get major-mode 'flyspell-mode-predicate)))
562 (setq flyspell-generic-check-word-p mode-predicate)))
563 ;; the welcome message
564 (if (and flyspell-issue-message-flag
565 flyspell-issue-welcome-flag
567 (let ((binding (where-is-internal 'flyspell-auto-correct-word
571 (format "Welcome to flyspell. Use %s or Mouse-2 to correct words."
572 (key-description binding))
573 "Welcome to flyspell. Use Mouse-2 to correct words."))))
574 ;; we end with the flyspell hooks
575 (run-hooks 'flyspell-mode-hook))
577 ;*---------------------------------------------------------------------*/
578 ;* flyspell-delay-commands ... */
579 ;*---------------------------------------------------------------------*/
580 (defun flyspell-delay-commands ()
581 "Install the standard set of Flyspell delayed commands."
582 (mapcar 'flyspell-delay-command flyspell-default-delayed-commands)
583 (mapcar 'flyspell-delay-command flyspell-delayed-commands))
585 ;*---------------------------------------------------------------------*/
586 ;* flyspell-delay-command ... */
587 ;*---------------------------------------------------------------------*/
588 (defun flyspell-delay-command (command)
589 "Set COMMAND to be delayed, for Flyspell.
590 When flyspell `post-command-hook' is invoked because a delayed command
591 as been used the current word is not immediately checked.
592 It will be checked only after `flyspell-delay' seconds."
593 (interactive "SDelay Flyspell after Command: ")
594 (put command 'flyspell-delayed t))
596 ;*---------------------------------------------------------------------*/
597 ;* flyspell-deplacement-commands ... */
598 ;*---------------------------------------------------------------------*/
599 (defun flyspell-deplacement-commands ()
600 "Install the standard set of Flyspell deplacement commands."
601 (mapcar 'flyspell-deplacement-command flyspell-default-deplacement-commands)
602 (mapcar 'flyspell-deplacement-command flyspell-deplacement-commands))
604 ;*---------------------------------------------------------------------*/
605 ;* flyspell-deplacement-command ... */
606 ;*---------------------------------------------------------------------*/
607 (defun flyspell-deplacement-command (command)
608 "Set COMMAND that implement cursor movements, for Flyspell.
609 When flyspell `post-command-hook' is invoked because of a deplacement command
610 as been used the current word is checked only if the previous command was
611 not the very same deplacement command."
612 (interactive "SDeplacement Flyspell after Command: ")
613 (put command 'flyspell-deplacement t))
615 ;*---------------------------------------------------------------------*/
616 ;* flyspell-word-cache ... */
617 ;*---------------------------------------------------------------------*/
618 (defvar flyspell-word-cache-start nil)
619 (defvar flyspell-word-cache-end nil)
620 (defvar flyspell-word-cache-word nil)
621 (defvar flyspell-word-cache-result '_)
622 (make-variable-buffer-local 'flyspell-word-cache-start)
623 (make-variable-buffer-local 'flyspell-word-cache-end)
624 (make-variable-buffer-local 'flyspell-word-cache-word)
625 (make-variable-buffer-local 'flyspell-word-cache-result)
627 ;*---------------------------------------------------------------------*/
628 ;* The flyspell pre-hook, store the current position. In the */
629 ;* post command hook, we will check, if the word at this position */
630 ;* has to be spell checked. */
631 ;*---------------------------------------------------------------------*/
632 (defvar flyspell-pre-buffer nil)
633 (defvar flyspell-pre-point nil)
634 (defvar flyspell-pre-column nil)
635 (defvar flyspell-pre-pre-buffer nil)
636 (defvar flyspell-pre-pre-point nil)
638 ;*---------------------------------------------------------------------*/
639 ;* flyspell-previous-command ... */
640 ;*---------------------------------------------------------------------*/
641 (defvar flyspell-previous-command nil
642 "The last interactive command checked by Flyspell.")
644 ;*---------------------------------------------------------------------*/
645 ;* flyspell-pre-command-hook ... */
646 ;*---------------------------------------------------------------------*/
647 (defun flyspell-pre-command-hook ()
648 "Save the current buffer and point for Flyspell's post-command hook."
650 (setq flyspell-pre-buffer (current-buffer))
651 (setq flyspell-pre-point (point))
652 (setq flyspell-pre-column (current-column)))
654 ;*---------------------------------------------------------------------*/
655 ;* flyspell-mode-off ... */
656 ;*---------------------------------------------------------------------*/
658 (defun flyspell-mode-off ()
659 "Turn Flyspell mode off."
660 ;; we remove the hooks
661 (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
662 (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
663 (setq after-change-functions (delq 'flyspell-after-change-function
664 after-change-functions))
665 ;; we remove all the flyspell hilightings
666 (flyspell-delete-all-overlays)
667 ;; we have to erase pre cache variables
668 (setq flyspell-pre-buffer nil)
669 (setq flyspell-pre-point nil)
670 ;; we mark the mode as killed
671 (setq flyspell-mode nil))
673 ;*---------------------------------------------------------------------*/
674 ;* flyspell-check-pre-word-p ... */
675 ;*---------------------------------------------------------------------*/
676 (defun flyspell-check-pre-word-p ()
677 "Return non-nil if we should check the word before point.
678 More precisely, it applies to the word that was before point
679 before the current command."
681 ((or (not (numberp flyspell-pre-point))
682 (not (bufferp flyspell-pre-buffer))
683 (not (buffer-live-p flyspell-pre-buffer)))
685 ((and (eq flyspell-pre-pre-point flyspell-pre-point)
686 (eq flyspell-pre-pre-buffer flyspell-pre-buffer))
688 ((or (and (= flyspell-pre-point (- (point) 1))
689 (eq (char-syntax (char-after flyspell-pre-point)) ?w))
690 (= flyspell-pre-point (point))
691 (= flyspell-pre-point (+ (point) 1)))
693 ((and (symbolp this-command)
694 (not executing-kbd-macro)
695 (or (get this-command 'flyspell-delayed)
696 (and (get this-command 'flyspell-deplacement)
697 (eq flyspell-previous-command this-command)))
698 (or (= (current-column) 0)
699 (= (current-column) flyspell-pre-column)
700 (eq (char-syntax (char-after flyspell-pre-point)) ?w)))
702 ((not (eq (current-buffer) flyspell-pre-buffer))
704 ((not (and (numberp flyspell-word-cache-start)
705 (numberp flyspell-word-cache-end)))
708 (or (< flyspell-pre-point flyspell-word-cache-start)
709 (> flyspell-pre-point flyspell-word-cache-end)))))
711 ;*---------------------------------------------------------------------*/
712 ;* The flyspell after-change-hook, store the change position. In */
713 ;* the post command hook, we will check, if the word at this */
714 ;* position has to be spell checked. */
715 ;*---------------------------------------------------------------------*/
716 (defvar flyspell-changes nil)
718 ;*---------------------------------------------------------------------*/
719 ;* flyspell-after-change-function ... */
720 ;*---------------------------------------------------------------------*/
721 (defun flyspell-after-change-function (start stop len)
722 "Save the current buffer and point for Flyspell's post-command hook."
724 (setq flyspell-changes (cons (cons start stop) flyspell-changes)))
726 ;*---------------------------------------------------------------------*/
727 ;* flyspell-check-changed-word-p ... */
728 ;*---------------------------------------------------------------------*/
729 (defun flyspell-check-changed-word-p (start stop)
730 "Return t when the changed word has to be checked.
731 The answer depends of several criteria.
732 Mostly we check word delimiters."
734 ((and (memq (char-after start) '(?\n ? )) (> stop start))
736 ((not (numberp flyspell-pre-point))
738 ((and (>= flyspell-pre-point start) (<= flyspell-pre-point stop))
740 ((let ((pos (point)))
741 (or (>= pos start) (<= pos stop) (= pos (1+ stop))))
746 ;*---------------------------------------------------------------------*/
747 ;* flyspell-check-word-p ... */
748 ;*---------------------------------------------------------------------*/
749 (defun flyspell-check-word-p ()
750 "Return t when the word at `point' has to be checked.
751 The answer depends of several criteria.
752 Mostly we check word delimiters."
754 ((<= (- (point-max) 1) (point-min))
755 ;; the buffer is not filled enough
757 ((and (and (> (current-column) 0)
758 (not (eq (current-column) flyspell-pre-column)))
761 (and (looking-at (flyspell-get-not-casechars))
762 (or flyspell-consider-dash-as-word-delimiter-flag
763 (not (looking-at "\\-"))))))
764 ;; yes because we have reached or typed a word delimiter.
766 ((symbolp this-command)
768 ((get this-command 'flyspell-deplacement)
769 (not (eq flyspell-previous-command this-command)))
770 ((get this-command 'flyspell-delayed)
771 ;; the current command is not delayed, that
772 ;; is that we must check the word now
773 (sit-for flyspell-delay))
777 ;*---------------------------------------------------------------------*/
778 ;* flyspell-debug-signal-no-check ... */
779 ;*---------------------------------------------------------------------*/
780 (defun flyspell-debug-signal-no-check (msg obj)
781 (setq debug-on-error t)
783 (let ((buffer (get-buffer-create "*flyspell-debug*")))
786 (insert "NO-CHECK:\n")
787 (insert (format " %S : %S\n" msg obj)))))
789 ;*---------------------------------------------------------------------*/
790 ;* flyspell-debug-signal-pre-word-checked ... */
791 ;*---------------------------------------------------------------------*/
792 (defun flyspell-debug-signal-pre-word-checked ()
793 (setq debug-on-error t)
795 (let ((buffer (get-buffer-create "*flyspell-debug*")))
797 (insert "PRE-WORD:\n")
798 (insert (format " pre-point : %S\n" flyspell-pre-point))
799 (insert (format " pre-buffer : %S\n" flyspell-pre-buffer))
800 (insert (format " cache-start: %S\n" flyspell-word-cache-start))
801 (insert (format " cache-end : %S\n" flyspell-word-cache-end))
802 (goto-char (point-max)))))
804 ;*---------------------------------------------------------------------*/
805 ;* flyspell-debug-signal-word-checked ... */
806 ;*---------------------------------------------------------------------*/
807 (defun flyspell-debug-signal-word-checked ()
808 (setq debug-on-error t)
810 (let ((oldbuf (current-buffer))
811 (buffer (get-buffer-create "*flyspell-debug*"))
815 (insert (format " this-cmd : %S\n" this-command))
816 (insert (format " delayed : %S\n" (and (symbolp this-command)
817 (get this-command 'flyspell-delayed))))
818 (insert (format " point : %S\n" point))
819 (insert (format " prev-char : [%c] %S\n"
822 (let ((c (if (> (point) (point-min))
825 (char-after (point)))
831 (let ((c (if (> (point) (point-min))
834 (and (and (looking-at (flyspell-get-not-casechars)) 1)
835 (and (or flyspell-consider-dash-as-word-delimiter-flag
836 (not (looking-at "\\-"))) 2))))))
839 (insert (format " because : %S\n"
841 ((not (and (symbolp this-command)
842 (get this-command 'flyspell-delayed)))
843 ;; the current command is not delayed, that
844 ;; is that we must check the word now
848 (let ((c (if (> (point) (point-min))
851 (and (looking-at (flyspell-get-not-casechars))
852 (or flyspell-consider-dash-as-word-delimiter-flag
853 (not (looking-at "\\-"))))))))
856 ;; yes because we have reached or typed a word delimiter.
858 ((not (integerp flyspell-delay))
859 ;; yes because the user had set up a no-delay configuration.
863 (goto-char (point-max)))))
865 ;*---------------------------------------------------------------------*/
866 ;* flyspell-debug-signal-changed-checked ... */
867 ;*---------------------------------------------------------------------*/
868 (defun flyspell-debug-signal-changed-checked ()
869 (setq debug-on-error t)
871 (let ((buffer (get-buffer-create "*flyspell-debug*"))
874 (insert "CHANGED WORD:\n")
875 (insert (format " point : %S\n" point))
876 (goto-char (point-max)))))
878 ;*---------------------------------------------------------------------*/
879 ;* flyspell-post-command-hook ... */
880 ;* ------------------------------------------------------------- */
881 ;* It is possible that we check several words: */
882 ;* 1- the current word is checked if the predicate */
883 ;* FLYSPELL-CHECK-WORD-P is true */
884 ;* 2- the word that used to be the current word before the */
885 ;* THIS-COMMAND is checked if: */
886 ;* a- the previous word is different from the current word */
887 ;* b- the previous word as not just been checked by the */
888 ;* previous FLYSPELL-POST-COMMAND-HOOK */
889 ;* 3- the words changed by the THIS-COMMAND that are neither the */
890 ;* previous word nor the current word */
891 ;*---------------------------------------------------------------------*/
892 (defun flyspell-post-command-hook ()
893 "The `post-command-hook' used by flyspell to check a word in-the-fly."
895 (let ((command this-command))
896 (if (flyspell-check-pre-word-p)
898 '(flyspell-debug-signal-pre-word-checked)
899 (set-buffer flyspell-pre-buffer)
901 (goto-char flyspell-pre-point)
903 (if (flyspell-check-word-p)
905 '(flyspell-debug-signal-word-checked)
907 ;; we remember which word we have just checked.
908 ;; this will be used next time we will check a word
909 ;; to compare the next current word with the word
910 ;; that as been registered in the pre-command-hook
911 ;; that is these variables are used within the predicate
912 ;; FLYSPELL-CHECK-PRE-WORD-P
913 (setq flyspell-pre-pre-buffer (current-buffer))
914 (setq flyspell-pre-pre-point (point)))
916 (setq flyspell-pre-pre-buffer nil)
917 (setq flyspell-pre-pre-point nil)
918 ;; when a word is not checked because of a delayed command
919 ;; we do not disable the ispell cache.
920 (if (and (symbolp this-command) (get this-command 'flyspell-delayed))
922 (setq flyspell-word-cache-end -1)
923 (setq flyspell-word-cache-result '_)))))
924 (while (consp flyspell-changes)
925 (let ((start (car (car flyspell-changes)))
926 (stop (cdr (car flyspell-changes))))
927 (if (flyspell-check-changed-word-p start stop)
929 '(flyspell-debug-signal-changed-checked)
932 (setq flyspell-changes (cdr flyspell-changes))))
933 (setq flyspell-previous-command command)))
935 ;*---------------------------------------------------------------------*/
936 ;* flyspell-notify-misspell ... */
937 ;*---------------------------------------------------------------------*/
938 (defun flyspell-notify-misspell (start end word poss)
939 (let ((replacements (if (stringp poss)
941 (if flyspell-sort-corrections
942 (sort (car (cdr (cdr poss))) 'string<)
943 (car (cdr (cdr poss)))))))
944 (if flyspell-issue-message-flag
945 (message (format "mispelling `%s' %S" word replacements)))))
947 ;*---------------------------------------------------------------------*/
948 ;* flyspell-word-search-backward ... */
949 ;*---------------------------------------------------------------------*/
950 (defun flyspell-word-search-backward (word bound)
954 (while (and (not r) (setq p (search-backward word bound t)))
955 (let ((lw (flyspell-get-word '())))
956 (if (and (consp lw) (string-equal (car lw) word))
961 ;*---------------------------------------------------------------------*/
962 ;* flyspell-word-search-forward ... */
963 ;*---------------------------------------------------------------------*/
964 (defun flyspell-word-search-forward (word bound)
968 (while (and (not r) (setq p (search-forward word bound t)))
969 (let ((lw (flyspell-get-word '())))
970 (if (and (consp lw) (string-equal (car lw) word))
972 (goto-char (1+ p)))))
975 ;*---------------------------------------------------------------------*/
976 ;* flyspell-word ... */
977 ;*---------------------------------------------------------------------*/
978 (defun flyspell-word (&optional following)
979 "Spell check a word."
980 (interactive (list ispell-following-word))
982 ;; use the correct dictionary
983 (flyspell-accept-buffer-local-defs)
984 (let* ((cursor-location (point))
985 (flyspell-word (flyspell-get-word following))
987 (if (or (eq flyspell-word nil)
988 (and (fboundp flyspell-generic-check-word-p)
989 (not (funcall flyspell-generic-check-word-p))))
992 ;; destructure return flyspell-word info list.
993 (setq start (car (cdr flyspell-word))
994 end (car (cdr (cdr flyspell-word)))
995 word (car flyspell-word))
996 ;; before checking in the directory, we check for doublons.
998 ((and (or (not (eq ispell-parser 'tex))
999 (and (> start (point-min))
1000 (not (memq (char-after (1- start)) '(?\} ?\\)))))
1001 flyspell-mark-duplications-flag
1003 (goto-char (1- start))
1004 (let ((p (flyspell-word-search-backward
1006 (- start (1+ (- end start))))))
1007 (and p (/= p (1- start))))))
1008 ;; yes, this is a doublon
1009 (flyspell-highlight-incorrect-region start end 'doublon)
1011 ((and (eq flyspell-word-cache-start start)
1012 (eq flyspell-word-cache-end end)
1013 (string-equal flyspell-word-cache-word word))
1014 ;; this word had been already checked, we skip
1015 flyspell-word-cache-result)
1016 ((and (eq ispell-parser 'tex)
1017 (flyspell-tex-command-p flyspell-word))
1018 ;; this is a correct word (because a tex command)
1019 (flyspell-unhighlight-at start)
1021 (flyspell-unhighlight-at (- end 1)))
1024 ;; we setup the cache
1025 (setq flyspell-word-cache-start start)
1026 (setq flyspell-word-cache-end end)
1027 (setq flyspell-word-cache-word word)
1028 ;; now check spelling of word.
1029 (process-send-string ispell-process "%\n")
1030 ;; put in verbose mode
1031 (process-send-string ispell-process
1032 (concat "^" word "\n"))
1033 ;; we mark the ispell process so it can be killed
1034 ;; when emacs is exited without query
1035 (set-process-query-on-exit-flag ispell-process nil)
1036 ;; wait until ispell has processed word
1038 (accept-process-output ispell-process)
1039 (not (string= "" (car ispell-filter)))))
1040 ;; (process-send-string ispell-process "!\n")
1041 ;; back to terse mode.
1042 (setq ispell-filter (cdr ispell-filter))
1043 (if (consp ispell-filter)
1044 (setq poss (ispell-parse-output (car ispell-filter))))
1045 (let ((res (cond ((eq poss t)
1047 (setq flyspell-word-cache-result t)
1048 (flyspell-unhighlight-at start)
1050 (flyspell-unhighlight-at (- end 1)))
1052 ((and (stringp poss) flyspell-highlight-flag)
1054 (setq flyspell-word-cache-result t)
1055 (flyspell-unhighlight-at start)
1057 (flyspell-unhighlight-at (- end 1)))
1060 (setq flyspell-word-cache-result t)
1061 (flyspell-unhighlight-at start)
1063 (flyspell-unhighlight-at (- end 1)))
1065 ((or (and (< flyspell-duplicate-distance 0)
1068 (flyspell-word-search-backward
1073 (flyspell-word-search-forward
1076 (and (> flyspell-duplicate-distance 0)
1079 (flyspell-word-search-backward
1082 flyspell-duplicate-distance)))
1085 (flyspell-word-search-forward
1088 flyspell-duplicate-distance))))))
1089 (setq flyspell-word-cache-result nil)
1090 (if flyspell-highlight-flag
1091 (flyspell-highlight-duplicate-region
1093 (message (format "duplicate `%s'" word)))
1096 (setq flyspell-word-cache-result nil)
1097 ;; incorrect highlight the location
1098 (if flyspell-highlight-flag
1099 (flyspell-highlight-incorrect-region
1101 (flyspell-notify-misspell start end word poss))
1103 ;; return to original location
1104 (goto-char cursor-location)
1105 (if ispell-quit (setq ispell-quit nil))
1108 ;*---------------------------------------------------------------------*/
1109 ;* flyspell-tex-math-initialized ... */
1110 ;*---------------------------------------------------------------------*/
1111 (defvar flyspell-tex-math-initialized nil)
1113 ;*---------------------------------------------------------------------*/
1114 ;* flyspell-math-tex-command-p ... */
1115 ;* ------------------------------------------------------------- */
1116 ;* This function uses the texmathp package to check if (point) */
1117 ;* is within a tex command. In order to avoid using */
1118 ;* condition-case each time we use the variable */
1119 ;* flyspell-tex-math-initialized to make a special case the first */
1120 ;* time that function is called. */
1121 ;*---------------------------------------------------------------------*/
1122 (defun flyspell-math-tex-command-p ()
1123 (when (fboundp 'texmathp)
1125 (flyspell-check-tex-math-command
1127 ((eq flyspell-tex-math-initialized t)
1129 ((eq flyspell-tex-math-initialized 'error)
1132 (setq flyspell-tex-math-initialized t)
1136 (setq flyspell-tex-math-initialized 'error)
1139 ;*---------------------------------------------------------------------*/
1140 ;* flyspell-tex-command-p ... */
1141 ;*---------------------------------------------------------------------*/
1142 (defun flyspell-tex-command-p (word)
1143 "Return t if WORD is a TeX command."
1145 (let ((b (car (cdr word))))
1146 (and (re-search-backward "\\\\" (- (point) 100) t)
1147 (or (= (match-end 0) b)
1148 (and (goto-char (match-end 0))
1149 (looking-at flyspell-tex-command-regexp)
1150 (>= (match-end 0) b))))))
1151 (flyspell-math-tex-command-p)))
1153 ;*---------------------------------------------------------------------*/
1154 ;* flyspell-casechars-cache ... */
1155 ;*---------------------------------------------------------------------*/
1156 (defvar flyspell-casechars-cache nil)
1157 (defvar flyspell-ispell-casechars-cache nil)
1158 (make-variable-buffer-local 'flyspell-casechars-cache)
1159 (make-variable-buffer-local 'flyspell-ispell-casechars-cache)
1161 ;*---------------------------------------------------------------------*/
1162 ;* flyspell-get-casechars ... */
1163 ;*---------------------------------------------------------------------*/
1164 (defun flyspell-get-casechars ()
1165 "This function builds a string that is the regexp of word chars.
1166 In order to avoid one useless string construction,
1167 this function changes the last char of the `ispell-casechars' string."
1168 (let ((ispell-casechars (ispell-get-casechars)))
1170 ((eq ispell-parser 'tex)
1171 (setq flyspell-ispell-casechars-cache ispell-casechars)
1172 (setq flyspell-casechars-cache
1173 (concat (substring ispell-casechars
1175 (- (length ispell-casechars) 1))
1177 flyspell-casechars-cache)
1179 (setq flyspell-ispell-casechars-cache ispell-casechars)
1180 (setq flyspell-casechars-cache ispell-casechars)
1181 flyspell-casechars-cache))))
1183 ;*---------------------------------------------------------------------*/
1184 ;* flyspell-get-not-casechars-cache ... */
1185 ;*---------------------------------------------------------------------*/
1186 (defvar flyspell-not-casechars-cache nil)
1187 (defvar flyspell-ispell-not-casechars-cache nil)
1188 (make-variable-buffer-local 'flyspell-not-casechars-cache)
1189 (make-variable-buffer-local 'flyspell-ispell-not-casechars-cache)
1191 ;*---------------------------------------------------------------------*/
1192 ;* flyspell-get-not-casechars ... */
1193 ;*---------------------------------------------------------------------*/
1194 (defun flyspell-get-not-casechars ()
1195 "This function builds a string that is the regexp of non-word chars."
1196 (let ((ispell-not-casechars (ispell-get-not-casechars)))
1198 ((eq ispell-parser 'tex)
1199 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1200 (setq flyspell-not-casechars-cache
1201 (concat (substring ispell-not-casechars
1203 (- (length ispell-not-casechars) 1))
1205 flyspell-not-casechars-cache)
1207 (setq flyspell-ispell-not-casechars-cache ispell-not-casechars)
1208 (setq flyspell-not-casechars-cache ispell-not-casechars)
1209 flyspell-not-casechars-cache))))
1211 ;*---------------------------------------------------------------------*/
1212 ;* flyspell-get-word ... */
1213 ;*---------------------------------------------------------------------*/
1214 (defun flyspell-get-word (following &optional extra-otherchars)
1215 "Return the word for spell-checking according to Ispell syntax.
1216 If optional argument FOLLOWING is non-nil or if `flyspell-following-word'
1217 is non-nil when called interactively, then the following word
1218 \(rather than preceding\) is checked when the cursor is not over a word.
1219 Optional second argument contains otherchars that can be included in word
1222 Word syntax described by `flyspell-dictionary-alist' (which see)."
1223 (let* ((flyspell-casechars (flyspell-get-casechars))
1224 (flyspell-not-casechars (flyspell-get-not-casechars))
1225 (ispell-otherchars (ispell-get-otherchars))
1226 (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
1227 (word-regexp (concat flyspell-casechars
1229 (if (not (string= "" ispell-otherchars))
1230 (concat ispell-otherchars "?"))
1231 (if extra-otherchars
1232 (concat extra-otherchars "?"))
1235 (if (or ispell-many-otherchars-p
1241 (if (not (looking-at flyspell-casechars))
1243 (re-search-forward flyspell-casechars (point-max) t)
1244 (re-search-backward flyspell-casechars (point-min) t)))
1245 ;; move to front of word
1246 (re-search-backward flyspell-not-casechars (point-min) 'start)
1247 (while (and (or (and (not (string= "" ispell-otherchars))
1248 (looking-at ispell-otherchars))
1249 (and extra-otherchars (looking-at extra-otherchars)))
1251 (or (not did-it-once)
1252 ispell-many-otherchars-p)
1253 (not (eq prevpt (point))))
1254 (if (and extra-otherchars (looking-at extra-otherchars))
1257 (if (looking-at flyspell-casechars)
1258 (re-search-backward flyspell-not-casechars (point-min) 'move)))
1262 (if (looking-at flyspell-casechars)
1263 (re-search-backward flyspell-not-casechars (point-min) 'move)
1264 (backward-char -1))))
1265 ;; Now mark the word and save to string.
1266 (if (not (re-search-forward word-regexp (point-max) t))
1269 (setq start (match-beginning 0)
1271 word (buffer-substring-no-properties start end))
1272 (list word start end)))))
1274 ;*---------------------------------------------------------------------*/
1275 ;* flyspell-small-region ... */
1276 ;*---------------------------------------------------------------------*/
1277 (defun flyspell-small-region (beg end)
1278 "Flyspell text between BEG and END."
1286 (while (< (point) end)
1287 (if (and flyspell-issue-message-flag (= count 100))
1289 (message "Spell Checking...%d%%"
1290 (* 100 (/ (float (- (point) beg)) (- end beg))))
1292 (setq count (+ 1 count)))
1295 (let ((cur (point)))
1297 (if (and (< (point) end) (> (point) (+ cur 1)))
1298 (backward-char 1)))))
1300 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1303 ;*---------------------------------------------------------------------*/
1304 ;* flyspell-external-ispell-process ... */
1305 ;*---------------------------------------------------------------------*/
1306 (defvar flyspell-external-ispell-process '()
1307 "The external Flyspell Ispell process.")
1309 ;*---------------------------------------------------------------------*/
1310 ;* flyspell-external-ispell-buffer ... */
1311 ;*---------------------------------------------------------------------*/
1312 (defvar flyspell-external-ispell-buffer '())
1313 (defvar flyspell-large-region-buffer '())
1314 (defvar flyspell-large-region-beg (point-min))
1315 (defvar flyspell-large-region-end (point-max))
1317 ;*---------------------------------------------------------------------*/
1318 ;* flyspell-external-point-words ... */
1319 ;*---------------------------------------------------------------------*/
1320 (defun flyspell-external-point-words ()
1321 (let ((buffer flyspell-external-ispell-buffer))
1323 (goto-char (point-min))
1326 ;; now we are done with ispell, we have to find the word in
1327 ;; the initial buffer
1328 (while (< (point) (- (point-max) 1))
1329 ;; we have to fetch the incorrect word
1330 (if (re-search-forward "\\([^\n]+\\)\n" (point-max) t)
1331 (let ((word (match-string 1)))
1332 (if (string= word pword)
1333 (setq pcount (1+ pcount))
1337 (goto-char (match-end 0))
1338 (if flyspell-issue-message-flag
1339 (message "Spell Checking...%d%% [%s]"
1340 (* 100 (/ (float (point)) (point-max)))
1342 (set-buffer flyspell-large-region-buffer)
1343 (goto-char flyspell-large-region-beg)
1346 (while (and (or (< n pcount) keep)
1347 (search-forward word flyspell-large-region-end t))
1349 (goto-char (- (point) 1))
1351 (setq keep (flyspell-word))))
1353 (setq flyspell-large-region-beg (point))))
1354 (set-buffer buffer))
1355 (goto-char (point-max)))))
1357 (if flyspell-issue-message-flag (message "Spell Checking completed."))
1358 ;; ok, we are done with pointing out incorrect words, we just
1359 ;; have to kill the temporary buffer
1360 (kill-buffer flyspell-external-ispell-buffer)
1361 (setq flyspell-external-ispell-buffer nil)))
1363 ;*---------------------------------------------------------------------*/
1364 ;* flyspell-large-region ... */
1365 ;*---------------------------------------------------------------------*/
1366 (defun flyspell-large-region (beg end)
1367 (let* ((curbuf (current-buffer))
1368 (buffer (get-buffer-create "*flyspell-region*")))
1369 (setq flyspell-external-ispell-buffer buffer)
1370 (setq flyspell-large-region-buffer curbuf)
1371 (setq flyspell-large-region-beg beg)
1372 (setq flyspell-large-region-end end)
1375 ;; this is done, we can start checking...
1376 (if flyspell-issue-message-flag (message "Checking region..."))
1378 (let ((c (apply 'call-process-region beg
1384 (if ispell-really-aspell "list" "-l")
1386 ;; Local dictionary becomes the global dictionary in use.
1387 (if ispell-local-dictionary
1388 (setq ispell-dictionary ispell-local-dictionary))
1389 (setq args (ispell-get-ispell-args))
1390 (if ispell-dictionary ; use specified dictionary
1392 (append (list "-d" ispell-dictionary) args)))
1393 (if ispell-personal-dictionary ; use specified pers dict
1398 ispell-personal-dictionary)))))
1399 (setq args (append args ispell-extra-args))
1402 (flyspell-external-point-words)
1403 (error "Can't check region...")))))
1405 ;*---------------------------------------------------------------------*/
1406 ;* flyspell-region ... */
1407 ;* ------------------------------------------------------------- */
1408 ;* Because `ispell -a' is too slow, it is not possible to use */
1409 ;* it on large region. Then, when ispell is invoked on a large */
1410 ;* text region, a new `ispell -l' process is spawned. The */
1411 ;* pointed out words are then searched in the region a checked with */
1412 ;* regular flyspell means. */
1413 ;*---------------------------------------------------------------------*/
1415 (defun flyspell-region (beg end)
1416 "Flyspell text between BEG and END."
1425 (if (and flyspell-large-region (> (- end beg) flyspell-large-region))
1426 (flyspell-large-region beg end)
1427 (flyspell-small-region beg end)))))
1429 ;*---------------------------------------------------------------------*/
1430 ;* flyspell-buffer ... */
1431 ;*---------------------------------------------------------------------*/
1433 (defun flyspell-buffer ()
1434 "Flyspell whole buffer."
1436 (flyspell-region (point-min) (point-max)))
1438 ;*---------------------------------------------------------------------*/
1439 ;* old next error position ... */
1440 ;*---------------------------------------------------------------------*/
1441 (defvar flyspell-old-buffer-error nil)
1442 (defvar flyspell-old-pos-error nil)
1444 ;*---------------------------------------------------------------------*/
1445 ;* flyspell-goto-next-error ... */
1446 ;*---------------------------------------------------------------------*/
1447 (defun flyspell-goto-next-error ()
1448 "Go to the next previously detected error.
1449 In general FLYSPELL-GOTO-NEXT-ERROR must be used after
1454 (if (and (eq (current-buffer) flyspell-old-buffer-error)
1455 (eq pos flyspell-old-pos-error))
1457 (if (= flyspell-old-pos-error max)
1458 ;; goto beginning of buffer
1460 (message "Restarting from beginning of buffer")
1461 (goto-char (point-min)))
1463 (setq pos (point))))
1464 ;; seek the next error
1465 (while (and (< pos max)
1466 (let ((ovs (overlays-at pos))
1468 (while (and (not r) (consp ovs))
1469 (if (flyspell-overlay-p (car ovs))
1471 (setq ovs (cdr ovs))))
1473 (setq pos (1+ pos)))
1474 ;; save the current location for next invocation
1475 (setq flyspell-old-pos-error pos)
1476 (setq flyspell-old-buffer-error (current-buffer))
1479 (message "No more miss-spelled word!"))))
1481 ;*---------------------------------------------------------------------*/
1482 ;* flyspell-overlay-p ... */
1483 ;*---------------------------------------------------------------------*/
1484 (defun flyspell-overlay-p (o)
1485 "A predicate that return true iff O is an overlay used by flyspell."
1486 (and (overlayp o) (overlay-get o 'flyspell-overlay)))
1488 ;*---------------------------------------------------------------------*/
1489 ;* flyspell-delete-all-overlays ... */
1490 ;* ------------------------------------------------------------- */
1491 ;* Remove all the overlays introduced by flyspell. */
1492 ;*---------------------------------------------------------------------*/
1493 (defun flyspell-delete-all-overlays ()
1494 "Delete all the overlays used by flyspell."
1495 (let ((l (overlays-in (point-min) (point-max))))
1498 (if (flyspell-overlay-p (car l))
1499 (delete-overlay (car l)))
1500 (setq l (cdr l))))))
1502 ;*---------------------------------------------------------------------*/
1503 ;* flyspell-unhighlight-at ... */
1504 ;*---------------------------------------------------------------------*/
1505 (defun flyspell-unhighlight-at (pos)
1506 "Remove the flyspell overlay that are located at POS."
1507 (if flyspell-persistent-highlight
1508 (let ((overlays (overlays-at pos)))
1509 (while (consp overlays)
1510 (if (flyspell-overlay-p (car overlays))
1511 (delete-overlay (car overlays)))
1512 (setq overlays (cdr overlays))))
1513 (if (flyspell-overlay-p flyspell-overlay)
1514 (delete-overlay flyspell-overlay))))
1516 ;*---------------------------------------------------------------------*/
1517 ;* flyspell-properties-at-p ... */
1518 ;* ------------------------------------------------------------- */
1519 ;* Is there an highlight properties at position pos? */
1520 ;*---------------------------------------------------------------------*/
1521 (defun flyspell-properties-at-p (pos)
1522 "Return t if there is a text property at POS, not counting `local-map'.
1523 If variable `flyspell-highlight-properties' is set to nil,
1524 text with properties are not checked. This function is used to discover
1525 if the character at POS has any other property."
1526 (let ((prop (text-properties-at pos))
1528 (while (and keep (consp prop))
1529 (if (and (eq (car prop) 'local-map) (consp (cdr prop)))
1530 (setq prop (cdr (cdr prop)))
1534 ;*---------------------------------------------------------------------*/
1535 ;* make-flyspell-overlay ... */
1536 ;*---------------------------------------------------------------------*/
1537 (defun make-flyspell-overlay (beg end face mouse-face)
1538 "Allocate an overlay to highlight an incorrect word.
1539 BEG and END specify the range in the buffer of that word.
1540 FACE and MOUSE-FACE specify the `face' and `mouse-face' properties
1542 (let ((flyspell-overlay (make-overlay beg end nil t nil)))
1543 (overlay-put flyspell-overlay 'face face)
1544 (overlay-put flyspell-overlay 'mouse-face mouse-face)
1545 (overlay-put flyspell-overlay 'flyspell-overlay t)
1546 (overlay-put flyspell-overlay 'evaporate t)
1547 (overlay-put flyspell-overlay 'help-echo "mouse-2: correct word at point")
1548 (overlay-put flyspell-overlay 'keymap flyspell-mouse-map)
1549 (when (eq face 'flyspell-incorrect)
1550 (and (stringp flyspell-before-incorrect-word-string)
1551 (overlay-put flyspell-overlay 'before-string
1552 flyspell-before-incorrect-word-string))
1553 (and (stringp flyspell-after-incorrect-word-string)
1554 (overlay-put flyspell-overlay 'after-string
1555 flyspell-after-incorrect-word-string)))
1558 ;*---------------------------------------------------------------------*/
1559 ;* flyspell-highlight-incorrect-region ... */
1560 ;*---------------------------------------------------------------------*/
1561 (defun flyspell-highlight-incorrect-region (beg end poss)
1562 "Set up an overlay on a misspelled word, in the buffer from BEG to END."
1563 (let ((inhibit-read-only t))
1564 (unless (run-hook-with-args-until-success
1565 'flyspell-incorrect-hook beg end poss)
1566 (if (or flyspell-highlight-properties
1567 (not (flyspell-properties-at-p beg)))
1569 ;; we cleanup all the overlay that are in the region, not
1570 ;; beginning at the word start position
1571 (if (< (1+ beg) end)
1572 (let ((os (overlays-in (1+ beg) end)))
1574 (if (flyspell-overlay-p (car os))
1575 (delete-overlay (car os)))
1576 (setq os (cdr os)))))
1577 ;; we cleanup current overlay at the same position
1578 (if (and (not flyspell-persistent-highlight)
1579 (overlayp flyspell-overlay))
1580 (delete-overlay flyspell-overlay)
1581 (let ((os (overlays-at beg)))
1583 (if (flyspell-overlay-p (car os))
1584 (delete-overlay (car os)))
1585 (setq os (cdr os)))))
1586 ;; now we can use a new overlay
1587 (setq flyspell-overlay
1588 (make-flyspell-overlay
1589 beg end 'flyspell-incorrect 'highlight)))))))
1591 ;*---------------------------------------------------------------------*/
1592 ;* flyspell-highlight-duplicate-region ... */
1593 ;*---------------------------------------------------------------------*/
1594 (defun flyspell-highlight-duplicate-region (beg end poss)
1595 "Set up an overlay on a duplicated word, in the buffer from BEG to END.
1596 ??? What does POSS mean?"
1597 (let ((inhibit-read-only t))
1598 (unless (run-hook-with-args-until-success
1599 'flyspell-incorrect-hook beg end poss)
1600 (if (or flyspell-highlight-properties
1601 (not (flyspell-properties-at-p beg)))
1603 ;; we cleanup current overlay at the same position
1604 (if (and (not flyspell-persistent-highlight)
1605 (overlayp flyspell-overlay))
1606 (delete-overlay flyspell-overlay)
1607 (let ((overlays (overlays-at beg)))
1608 (while (consp overlays)
1609 (if (flyspell-overlay-p (car overlays))
1610 (delete-overlay (car overlays)))
1611 (setq overlays (cdr overlays)))))
1612 ;; now we can use a new overlay
1613 (setq flyspell-overlay
1614 (make-flyspell-overlay beg end
1618 ;*---------------------------------------------------------------------*/
1619 ;* flyspell-auto-correct-cache ... */
1620 ;*---------------------------------------------------------------------*/
1621 (defvar flyspell-auto-correct-pos nil)
1622 (defvar flyspell-auto-correct-region nil)
1623 (defvar flyspell-auto-correct-ring nil)
1624 (defvar flyspell-auto-correct-word nil)
1625 (make-variable-buffer-local 'flyspell-auto-correct-pos)
1626 (make-variable-buffer-local 'flyspell-auto-correct-region)
1627 (make-variable-buffer-local 'flyspell-auto-correct-ring)
1628 (make-variable-buffer-local 'flyspell-auto-correct-word)
1630 ;*---------------------------------------------------------------------*/
1631 ;* flyspell-check-previous-highlighted-word ... */
1632 ;*---------------------------------------------------------------------*/
1633 (defun flyspell-check-previous-highlighted-word (&optional arg)
1634 "Correct the closer misspelled word.
1635 This function scans a mis-spelled word before the cursor. If it finds one
1636 it proposes replacement for that word. With prefix arg, count that many
1637 misspelled words backwards."
1639 (let ((pos1 (point))
1641 (arg (if (or (not (numberp arg)) (< arg 1)) 1 arg))
1644 (while (and (setq pos (previous-overlay-change pos))
1647 (if (> pos (point-min))
1649 (setq ovs (overlays-at (1- pos)))
1652 (setq ovs (cdr ovs))
1653 (if (and (overlay-get ov 'flyspell-overlay)
1654 (= 0 (setq arg (1- arg))))
1655 (throw 'exit t)))))))
1659 (error "No word to correct before point"))))
1661 ;*---------------------------------------------------------------------*/
1662 ;* flyspell-display-next-corrections ... */
1663 ;*---------------------------------------------------------------------*/
1664 (defun flyspell-display-next-corrections (corrections)
1665 (let ((string "Corrections:")
1668 (while (< (length string) 80)
1669 (if (equal (car l) flyspell-auto-correct-word)
1670 (setq pos (cons (+ 1 (length string)) pos)))
1671 (setq string (concat string " " (car l)))
1674 (let ((num (car pos)))
1675 (put-text-property num
1676 (+ num (length flyspell-auto-correct-word))
1677 'face 'flyspell-incorrect
1679 (setq pos (cdr pos)))
1680 (if (fboundp 'display-message)
1681 (display-message 'no-log string)
1684 ;*---------------------------------------------------------------------*/
1685 ;* flyspell-abbrev-table ... */
1686 ;*---------------------------------------------------------------------*/
1687 (defun flyspell-abbrev-table ()
1688 (if flyspell-use-global-abbrev-table-p
1690 (or local-abbrev-table global-abbrev-table)))
1692 ;*---------------------------------------------------------------------*/
1693 ;* flyspell-define-abbrev ... */
1694 ;*---------------------------------------------------------------------*/
1695 (defun flyspell-define-abbrev (name expansion)
1696 (let ((table (flyspell-abbrev-table)))
1698 (define-abbrev table name expansion))))
1700 ;*---------------------------------------------------------------------*/
1701 ;* flyspell-auto-correct-word ... */
1702 ;*---------------------------------------------------------------------*/
1703 (defun flyspell-auto-correct-word ()
1704 "Correct the current word.
1705 This command proposes various successive corrections for the current word."
1708 (old-max (point-max)))
1709 ;; use the correct dictionary
1710 (flyspell-accept-buffer-local-defs)
1711 (if (and (eq flyspell-auto-correct-pos pos)
1712 (consp flyspell-auto-correct-region))
1713 ;; we have already been using the function at the same location
1714 (let* ((start (car flyspell-auto-correct-region))
1715 (len (cdr flyspell-auto-correct-region)))
1716 (flyspell-unhighlight-at start)
1717 (delete-region start (+ start len))
1718 (setq flyspell-auto-correct-ring (cdr flyspell-auto-correct-ring))
1719 (let* ((word (car flyspell-auto-correct-ring))
1720 (len (length word)))
1721 (rplacd flyspell-auto-correct-region len)
1723 (if flyspell-abbrev-p
1724 (if (flyspell-already-abbrevp (flyspell-abbrev-table)
1725 flyspell-auto-correct-word)
1726 (flyspell-change-abbrev (flyspell-abbrev-table)
1727 flyspell-auto-correct-word
1729 (flyspell-define-abbrev flyspell-auto-correct-word word)))
1730 (funcall flyspell-insert-function word)
1732 (flyspell-display-next-corrections flyspell-auto-correct-ring))
1733 (flyspell-ajust-cursor-point pos (point) old-max)
1734 (setq flyspell-auto-correct-pos (point)))
1735 ;; fetch the word to be checked
1736 (let ((word (flyspell-get-word nil)))
1738 (let ((start (car (cdr word)))
1739 (end (car (cdr (cdr word))))
1742 (setq flyspell-auto-correct-word word)
1743 ;; now check spelling of word.
1744 (process-send-string ispell-process "%\n") ;put in verbose mode
1745 (process-send-string ispell-process (concat "^" word "\n"))
1746 ;; wait until ispell has processed word
1748 (accept-process-output ispell-process)
1749 (not (string= "" (car ispell-filter)))))
1750 (setq ispell-filter (cdr ispell-filter))
1751 (if (consp ispell-filter)
1752 (setq poss (ispell-parse-output (car ispell-filter))))
1754 ((or (eq poss t) (stringp poss))
1755 ;; don't correct word
1759 (error "Ispell: error in Ispell process"))
1761 ;; the word is incorrect, we have to propose a replacement
1762 (let ((replacements (if flyspell-sort-corrections
1763 (sort (car (cdr (cdr poss))) 'string<)
1764 (car (cdr (cdr poss))))))
1765 (setq flyspell-auto-correct-region nil)
1766 (if (consp replacements)
1768 (let ((replace (car replacements)))
1769 (let ((new-word replace))
1770 (if (not (equal new-word (car poss)))
1772 ;; the save the current replacements
1773 (setq flyspell-auto-correct-region
1774 (cons start (length new-word)))
1775 (let ((l replacements))
1776 (while (consp (cdr l))
1778 (rplacd l (cons (car poss) replacements)))
1779 (setq flyspell-auto-correct-ring
1781 (flyspell-unhighlight-at start)
1782 (delete-region start end)
1783 (funcall flyspell-insert-function new-word)
1784 (if flyspell-abbrev-p
1785 (if (flyspell-already-abbrevp
1786 (flyspell-abbrev-table) word)
1787 (flyspell-change-abbrev
1788 (flyspell-abbrev-table)
1791 (flyspell-define-abbrev word
1794 (flyspell-display-next-corrections
1795 (cons new-word flyspell-auto-correct-ring))
1796 (flyspell-ajust-cursor-point pos
1799 (setq flyspell-auto-correct-pos (point))
1800 (ispell-pdict-save t)))))))
1802 ;*---------------------------------------------------------------------*/
1803 ;* flyspell-auto-correct-previous-pos ... */
1804 ;*---------------------------------------------------------------------*/
1805 (defvar flyspell-auto-correct-previous-pos nil
1806 "Holds the start of the first incorrect word before point.")
1808 ;*---------------------------------------------------------------------*/
1809 ;* flyspell-auto-correct-previous-hook ... */
1810 ;*---------------------------------------------------------------------*/
1811 (defun flyspell-auto-correct-previous-hook ()
1812 "Hook to track successive calls to `flyspell-auto-correct-previous-word'.
1813 Sets `flyspell-auto-correct-previous-pos' to nil"
1815 (remove-hook 'pre-command-hook (function flyspell-auto-correct-previous-hook) t)
1816 (unless (eq this-command (function flyspell-auto-correct-previous-word))
1817 (setq flyspell-auto-correct-previous-pos nil)))
1819 ;*---------------------------------------------------------------------*/
1820 ;* flyspell-auto-correct-previous-word ... */
1821 ;*---------------------------------------------------------------------*/
1822 (defun flyspell-auto-correct-previous-word (position)
1823 "*Auto correct the first mispelled word that occurs before point.
1824 But don't look beyond what's visible on the screen."
1829 (move-to-window-line 0)
1831 (move-to-window-line -1)
1835 (narrow-to-region top bot)
1836 (overlay-recenter (point))
1838 (add-hook 'pre-command-hook
1839 (function flyspell-auto-correct-previous-hook) t t)
1841 (unless flyspell-auto-correct-previous-pos
1842 ;; only reset if a new overlay exists
1843 (setq flyspell-auto-correct-previous-pos nil)
1845 (let ((overlay-list (overlays-in (point-min) position))
1846 (new-overlay 'dummy-value))
1848 ;; search for previous (new) flyspell overlay
1849 (while (and new-overlay
1850 (or (not (flyspell-overlay-p new-overlay))
1851 ;; check if its face has changed
1852 (not (eq (get-char-property
1853 (overlay-start new-overlay) 'face)
1854 'flyspell-incorrect))))
1855 (setq new-overlay (car-safe overlay-list))
1856 (setq overlay-list (cdr-safe overlay-list)))
1858 ;; if nothing new exits new-overlay should be nil
1859 (if new-overlay ;; the length of the word may change so go to the start
1860 (setq flyspell-auto-correct-previous-pos
1861 (overlay-start new-overlay)))))
1863 (when flyspell-auto-correct-previous-pos
1865 (goto-char flyspell-auto-correct-previous-pos)
1866 (let ((ispell-following-word t)) ;; point is at start
1867 (if (numberp flyspell-auto-correct-previous-pos)
1868 (goto-char flyspell-auto-correct-previous-pos))
1869 (flyspell-auto-correct-word))
1870 ;; the point may have moved so reset this
1871 (setq flyspell-auto-correct-previous-pos (point))))))))
1873 ;*---------------------------------------------------------------------*/
1874 ;* flyspell-correct-word ... */
1875 ;*---------------------------------------------------------------------*/
1876 (defun flyspell-correct-word (event)
1877 "Pop up a menu of possible corrections for a misspelled word.
1878 The word checked is the word at the mouse position."
1880 ;; use the correct dictionary
1881 (flyspell-accept-buffer-local-defs)
1882 ;; retain cursor location (I don't know why but save-excursion here fails).
1883 (let ((save (point)))
1884 (mouse-set-point event)
1885 (let ((cursor-location (point))
1886 (word (flyspell-get-word nil)))
1888 (let ((start (car (cdr word)))
1889 (end (car (cdr (cdr word))))
1892 ;; now check spelling of word.
1893 (process-send-string ispell-process "%\n") ;put in verbose mode
1894 (process-send-string ispell-process (concat "^" word "\n"))
1895 ;; wait until ispell has processed word
1897 (accept-process-output ispell-process)
1898 (not (string= "" (car ispell-filter)))))
1899 (setq ispell-filter (cdr ispell-filter))
1900 (if (consp ispell-filter)
1901 (setq poss (ispell-parse-output (car ispell-filter))))
1903 ((or (eq poss t) (stringp poss))
1904 ;; don't correct word
1908 (error "Ispell: error in Ispell process"))
1910 (flyspell-xemacs-popup
1911 event poss word cursor-location start end save))
1913 ;; The word is incorrect, we have to propose a replacement.
1914 (flyspell-do-correct (flyspell-emacs-popup event poss word)
1915 poss word cursor-location start end save)))
1916 (ispell-pdict-save t))))))
1918 ;*---------------------------------------------------------------------*/
1919 ;* flyspell-do-correct ... */
1920 ;*---------------------------------------------------------------------*/
1921 (defun flyspell-do-correct (replace poss word cursor-location start end save)
1922 "The popup menu callback."
1923 ;; Originally, the XEmacs code didn't do the (goto-char save) here and did
1924 ;; it instead right after calling the function.
1925 (cond ((eq replace 'ignore)
1930 (ispell-send-string (concat "*" word "\n"))
1931 ;; This was added only to the XEmacs side in revision 1.18 of
1932 ;; flyspell. I assume its absence on the Emacs side was an
1933 ;; oversight. --Stef
1934 (ispell-send-string "#\n")
1935 (flyspell-unhighlight-at cursor-location)
1936 (setq ispell-pdict-modified-p '(t)))
1937 ((or (eq replace 'buffer) (eq replace 'session))
1938 (ispell-send-string (concat "@" word "\n"))
1939 (flyspell-unhighlight-at cursor-location)
1940 (if (null ispell-pdict-modified-p)
1941 (setq ispell-pdict-modified-p
1942 (list ispell-pdict-modified-p)))
1944 (if (eq replace 'buffer)
1945 (ispell-add-per-file-word-list word)))
1947 ;; This was added only to the Emacs side. I assume its absence on
1948 ;; the XEmacs side was an oversight. --Stef
1949 (flyspell-unhighlight-at cursor-location)
1950 (let ((old-max (point-max))
1951 (new-word (if (atom replace)
1954 (cursor-location (+ (- (length word) (- end start))
1956 (unless (equal new-word (car poss))
1957 (delete-region start end)
1959 (funcall flyspell-insert-function new-word)
1960 (if flyspell-abbrev-p
1961 (flyspell-define-abbrev word new-word)))
1962 ;; In the original Emacs code, this was only called in the body
1963 ;; of the if. I arbitrarily kept the XEmacs behavior instead.
1964 (flyspell-ajust-cursor-point save cursor-location old-max)))
1969 ;*---------------------------------------------------------------------*/
1970 ;* flyspell-ajust-cursor-point ... */
1971 ;*---------------------------------------------------------------------*/
1972 (defun flyspell-ajust-cursor-point (save cursor-location old-max)
1973 (if (>= save cursor-location)
1974 (let ((new-pos (+ save (- (point-max) old-max))))
1976 ((< new-pos (point-min))
1978 ((> new-pos (point-max))
1983 ;*---------------------------------------------------------------------*/
1984 ;* flyspell-emacs-popup ... */
1985 ;*---------------------------------------------------------------------*/
1986 (defun flyspell-emacs-popup (event poss word)
1987 "The Emacs popup menu."
1989 (let* ((mouse-pos (mouse-position))
1990 (mouse-pos (if (nth 1 mouse-pos)
1992 (set-mouse-position (car mouse-pos)
1993 (/ (frame-width) 2) 2)
1995 (setq event (list (list (car (cdr mouse-pos))
1996 (1+ (cdr (cdr mouse-pos))))
1998 (let* ((corrects (if flyspell-sort-corrections
1999 (sort (car (cdr (cdr poss))) 'string<)
2000 (car (cdr (cdr poss)))))
2001 (cor-menu (if (consp corrects)
2002 (mapcar (lambda (correct)
2003 (list correct correct))
2006 (affix (car (cdr (cdr (cdr poss)))))
2007 (base-menu (let ((save (if (consp affix)
2009 (list (concat "Save affix: " (car affix))
2011 '("Accept (session)" session)
2012 '("Accept (buffer)" buffer))
2013 '(("Save word" save)
2014 ("Accept (session)" session)
2015 ("Accept (buffer)" buffer)))))
2016 (if (consp cor-menu)
2017 (append cor-menu (cons "" save))
2019 (menu (cons "flyspell correction menu" base-menu)))
2020 (car (x-popup-menu event
2021 (list (format "%s [%s]" word (or ispell-local-dictionary
2025 ;*---------------------------------------------------------------------*/
2026 ;* flyspell-xemacs-popup ... */
2027 ;*---------------------------------------------------------------------*/
2028 (defun flyspell-xemacs-popup (event poss word cursor-location start end save)
2029 "The XEmacs popup menu."
2030 (let* ((corrects (if flyspell-sort-corrections
2031 (sort (car (cdr (cdr poss))) 'string<)
2032 (car (cdr (cdr poss)))))
2033 (cor-menu (if (consp corrects)
2034 (mapcar (lambda (correct)
2036 (list 'flyspell-do-correct
2047 (affix (car (cdr (cdr (cdr poss)))))
2048 (menu (let ((save (if (consp affix)
2050 (concat "Save affix: " (car affix))
2051 (list 'flyspell-do-correct
2062 (list 'flyspell-do-correct
2071 (session (vector "Accept (session)"
2072 (list 'flyspell-do-correct
2081 (buffer (vector "Accept (buffer)"
2082 (list 'flyspell-do-correct
2091 (if (consp cor-menu)
2092 (append cor-menu (list "-" save session buffer))
2093 (list save session buffer)))))
2094 (popup-menu (cons (format "%s [%s]" word (or ispell-local-dictionary
2098 ;*---------------------------------------------------------------------*/
2099 ;* Some example functions for real autocorrecting */
2100 ;*---------------------------------------------------------------------*/
2101 (defun flyspell-maybe-correct-transposition (beg end poss)
2102 "Check replacements for transposed characters.
2104 If the text between BEG and END is equal to a correction suggested by
2105 Ispell, after transposing two adjacent characters, correct the text,
2108 The third arg POSS is either the symbol 'doublon' or a list of
2109 possible corrections as returned by `ispell-parse-output'.
2111 This function is meant to be added to `flyspell-incorrect-hook'."
2114 (let ((str (buffer-substring beg end))
2115 (i 0) (len (- end beg)) tmp)
2116 (while (< (1+ i) len)
2117 (setq tmp (aref str i))
2118 (aset str i (aref str (1+ i)))
2119 (aset str (1+ i) tmp)
2120 (when (member str (nth 2 poss))
2122 (goto-char (+ beg i 1))
2123 (transpose-chars 1))
2125 (setq tmp (aref str i))
2126 (aset str i (aref str (1+ i)))
2127 (aset str (1+ i) tmp)
2131 (defun flyspell-maybe-correct-doubling (beg end poss)
2132 "Check replacements for doubled characters.
2134 If the text between BEG and END is equal to a correction suggested by
2135 Ispell, after removing a pair of doubled characters, correct the text,
2138 The third arg POSS is either the symbol 'doublon' or a list of
2139 possible corrections as returned by `ispell-parse-output'.
2141 This function is meant to be added to `flyspell-incorrect-hook'."
2144 (let ((str (buffer-substring beg end))
2145 (i 0) (len (- end beg)))
2146 (while (< (1+ i) len)
2147 (when (and (= (aref str i) (aref str (1+ i)))
2148 (member (concat (substring str 0 (1+ i))
2149 (substring str (+ i 2)))
2151 (goto-char (+ beg i))
2157 ;*---------------------------------------------------------------------*/
2158 ;* flyspell-already-abbrevp ... */
2159 ;*---------------------------------------------------------------------*/
2160 (defun flyspell-already-abbrevp (table word)
2161 (let ((sym (abbrev-symbol word table)))
2162 (and sym (symbolp sym))))
2164 ;*---------------------------------------------------------------------*/
2165 ;* flyspell-change-abbrev ... */
2166 ;*---------------------------------------------------------------------*/
2167 (defun flyspell-change-abbrev (table old new)
2168 (set (abbrev-symbol old table) new))
2172 ;; arch-tag: 05d915b9-e9cf-44fb-9137-fc28f5eaab2a
2173 ;;; flyspell.el ends here