flyspell.el 96 KB

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