flyspell.el 97 KB

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