home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / emacs / site-lisp / dictionaries-common / flyspell.el < prev    next >
Encoding:
Text File  |  2006-07-12  |  97.3 KB  |  2,474 lines

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