flyspell.el 95 KB

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