whitespace.el 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672
  1. ;;; whitespace.el --- minor mode to visualize TAB, (HARD) SPACE, NEWLINE
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
  4. ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
  5. ;; Keywords: data, wp
  6. ;; Version: 13.2.2
  7. ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;; Introduction
  23. ;; ------------
  24. ;;
  25. ;; This package is a minor mode to visualize blanks (TAB, (HARD) SPACE
  26. ;; and NEWLINE).
  27. ;;
  28. ;; whitespace uses two ways to visualize blanks: faces and display
  29. ;; table.
  30. ;;
  31. ;; * Faces are used to highlight the background with a color.
  32. ;; whitespace uses font-lock to highlight blank characters.
  33. ;;
  34. ;; * Display table changes the way a character is displayed, that is,
  35. ;; it provides a visual mark for characters, for example, at the end
  36. ;; of line (?\xB6), at SPACEs (?\xB7) and at TABs (?\xBB).
  37. ;;
  38. ;; The `whitespace-style' variable selects which way blanks are
  39. ;; visualized.
  40. ;;
  41. ;; Note that when whitespace is turned on, whitespace saves the
  42. ;; font-lock state, that is, if font-lock is on or off. And
  43. ;; whitespace restores the font-lock state when it is turned off. So,
  44. ;; if whitespace is turned on and font-lock is off, whitespace also
  45. ;; turns on the font-lock to highlight blanks, but the font-lock will
  46. ;; be turned off when whitespace is turned off. Thus, turn on
  47. ;; font-lock before whitespace is on, if you want that font-lock
  48. ;; continues on after whitespace is turned off.
  49. ;;
  50. ;; When whitespace is on, it takes care of highlighting some special
  51. ;; characters over the default mechanism of `nobreak-char-display'
  52. ;; (which see) and `show-trailing-whitespace' (which see).
  53. ;;
  54. ;; The trailing spaces are not highlighted while point is at end of line.
  55. ;; Also the spaces at beginning of buffer are not highlighted while point is at
  56. ;; beginning of buffer; and the spaces at end of buffer are not highlighted
  57. ;; while point is at end of buffer.
  58. ;;
  59. ;; There are two ways of using whitespace: local and global.
  60. ;;
  61. ;; * Local whitespace affects only the current buffer.
  62. ;;
  63. ;; * Global whitespace affects all current and future buffers. That
  64. ;; is, if you turn on global whitespace and then create a new
  65. ;; buffer, the new buffer will also have whitespace on. The
  66. ;; `whitespace-global-modes' variable controls which major-mode will
  67. ;; be automagically turned on.
  68. ;;
  69. ;; You can mix the local and global usage without any conflict. But
  70. ;; local whitespace has priority over global whitespace. Whitespace
  71. ;; mode is active in a buffer if you have enabled it in that buffer or
  72. ;; if you have enabled it globally.
  73. ;;
  74. ;; When global and local whitespace are on:
  75. ;;
  76. ;; * if local whitespace is turned off, whitespace is turned off for
  77. ;; the current buffer only.
  78. ;;
  79. ;; * if global whitespace is turned off, whitespace continues on only
  80. ;; in the buffers in which local whitespace is on.
  81. ;;
  82. ;; To use whitespace, insert in your ~/.emacs:
  83. ;;
  84. ;; (require 'whitespace)
  85. ;;
  86. ;; Or autoload at least one of the commands`whitespace-mode',
  87. ;; `whitespace-toggle-options', `global-whitespace-mode' or
  88. ;; `global-whitespace-toggle-options'. For example:
  89. ;;
  90. ;; (autoload 'whitespace-mode "whitespace"
  91. ;; "Toggle whitespace visualization." t)
  92. ;; (autoload 'whitespace-toggle-options "whitespace"
  93. ;; "Toggle local `whitespace-mode' options." t)
  94. ;;
  95. ;; whitespace was inspired by:
  96. ;;
  97. ;; whitespace.el Rajesh Vaidheeswarran <rv@gnu.org>
  98. ;; Warn about and clean bogus whitespaces in the file
  99. ;; (inspired the idea to warn and clean some blanks)
  100. ;; This was the original `whitespace.el' which was replaced by
  101. ;; `blank-mode.el'. And later `blank-mode.el' was renamed to
  102. ;; `whitespace.el'.
  103. ;;
  104. ;; show-whitespace-mode.el Aurelien Tisne <aurelien.tisne@free.fr>
  105. ;; Simple mode to highlight whitespaces
  106. ;; (inspired the idea to use font-lock)
  107. ;;
  108. ;; whitespace-mode.el Lawrence Mitchell <wence@gmx.li>
  109. ;; Major mode for editing Whitespace
  110. ;; (inspired the idea to use display table)
  111. ;;
  112. ;; visws.el Miles Bader <miles@gnu.org>
  113. ;; Make whitespace visible
  114. ;; (handle display table, his code was modified, but the main
  115. ;; idea was kept)
  116. ;;
  117. ;;
  118. ;; Using whitespace
  119. ;; ----------------
  120. ;;
  121. ;; There is no problem if you mix local and global minor mode usage.
  122. ;;
  123. ;; * LOCAL whitespace:
  124. ;; + To toggle whitespace options locally, type:
  125. ;;
  126. ;; M-x whitespace-toggle-options RET
  127. ;;
  128. ;; + To activate whitespace locally, type:
  129. ;;
  130. ;; C-u 1 M-x whitespace-mode RET
  131. ;;
  132. ;; + To deactivate whitespace locally, type:
  133. ;;
  134. ;; C-u 0 M-x whitespace-mode RET
  135. ;;
  136. ;; + To toggle whitespace locally, type:
  137. ;;
  138. ;; M-x whitespace-mode RET
  139. ;;
  140. ;; * GLOBAL whitespace:
  141. ;; + To toggle whitespace options globally, type:
  142. ;;
  143. ;; M-x global-whitespace-toggle-options RET
  144. ;;
  145. ;; + To activate whitespace globally, type:
  146. ;;
  147. ;; C-u 1 M-x global-whitespace-mode RET
  148. ;;
  149. ;; + To deactivate whitespace globally, type:
  150. ;;
  151. ;; C-u 0 M-x global-whitespace-mode RET
  152. ;;
  153. ;; + To toggle whitespace globally, type:
  154. ;;
  155. ;; M-x global-whitespace-mode RET
  156. ;;
  157. ;; There are also the following useful commands:
  158. ;;
  159. ;; `whitespace-newline-mode'
  160. ;; Toggle NEWLINE minor mode visualization ("nl" on modeline).
  161. ;;
  162. ;; `global-whitespace-newline-mode'
  163. ;; Toggle NEWLINE global minor mode visualization ("NL" on modeline).
  164. ;;
  165. ;; `whitespace-report'
  166. ;; Report some blank problems in buffer.
  167. ;;
  168. ;; `whitespace-report-region'
  169. ;; Report some blank problems in a region.
  170. ;;
  171. ;; `whitespace-cleanup'
  172. ;; Cleanup some blank problems in all buffer or at region.
  173. ;;
  174. ;; `whitespace-cleanup-region'
  175. ;; Cleanup some blank problems at region.
  176. ;;
  177. ;; The problems, which are cleaned up, are:
  178. ;;
  179. ;; 1. empty lines at beginning of buffer.
  180. ;; 2. empty lines at end of buffer.
  181. ;; If `whitespace-style' includes the value `empty', remove all
  182. ;; empty lines at beginning and/or end of buffer.
  183. ;;
  184. ;; 3. 8 or more SPACEs at beginning of line.
  185. ;; If `whitespace-style' includes the value `indentation':
  186. ;; replace 8 or more SPACEs at beginning of line by TABs, if
  187. ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs by
  188. ;; SPACEs.
  189. ;; If `whitespace-style' includes the value `indentation::tab',
  190. ;; replace 8 or more SPACEs at beginning of line by TABs.
  191. ;; If `whitespace-style' includes the value `indentation::space',
  192. ;; replace TABs by SPACEs.
  193. ;;
  194. ;; 4. SPACEs before TAB.
  195. ;; If `whitespace-style' includes the value `space-before-tab':
  196. ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  197. ;; otherwise, replace TABs by SPACEs.
  198. ;; If `whitespace-style' includes the value
  199. ;; `space-before-tab::tab', replace SPACEs by TABs.
  200. ;; If `whitespace-style' includes the value
  201. ;; `space-before-tab::space', replace TABs by SPACEs.
  202. ;;
  203. ;; 5. SPACEs or TABs at end of line.
  204. ;; If `whitespace-style' includes the value `trailing', remove all
  205. ;; SPACEs or TABs at end of line.
  206. ;;
  207. ;; 6. 8 or more SPACEs after TAB.
  208. ;; If `whitespace-style' includes the value `space-after-tab':
  209. ;; replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  210. ;; otherwise, replace TABs by SPACEs.
  211. ;; If `whitespace-style' includes the value `space-after-tab::tab',
  212. ;; replace SPACEs by TABs.
  213. ;; If `whitespace-style' includes the value
  214. ;; `space-after-tab::space', replace TABs by SPACEs.
  215. ;;
  216. ;;
  217. ;; Hooks
  218. ;; -----
  219. ;;
  220. ;; whitespace has the following hook variables:
  221. ;;
  222. ;; `whitespace-mode-hook'
  223. ;; It is evaluated always when whitespace is turned on locally.
  224. ;;
  225. ;; `global-whitespace-mode-hook'
  226. ;; It is evaluated always when whitespace is turned on globally.
  227. ;;
  228. ;; `whitespace-load-hook'
  229. ;; It is evaluated after whitespace package is loaded.
  230. ;;
  231. ;;
  232. ;; Options
  233. ;; -------
  234. ;;
  235. ;; Below it's shown a brief description of whitespace options, please,
  236. ;; see the options declaration in the code for a long documentation.
  237. ;;
  238. ;; `whitespace-style' Specify which kind of blank is
  239. ;; visualized.
  240. ;;
  241. ;; `whitespace-space' Face used to visualize SPACE.
  242. ;;
  243. ;; `whitespace-hspace' Face used to visualize HARD SPACE.
  244. ;;
  245. ;; `whitespace-tab' Face used to visualize TAB.
  246. ;;
  247. ;; `whitespace-newline' Face used to visualize NEWLINE char
  248. ;; mapping.
  249. ;;
  250. ;; `whitespace-trailing' Face used to visualize trailing
  251. ;; blanks.
  252. ;;
  253. ;; `whitespace-line' Face used to visualize "long" lines.
  254. ;;
  255. ;; `whitespace-space-before-tab' Face used to visualize SPACEs
  256. ;; before TAB.
  257. ;;
  258. ;; `whitespace-indentation' Face used to visualize 8 or more
  259. ;; SPACEs at beginning of line.
  260. ;;
  261. ;; `whitespace-empty' Face used to visualize empty lines at
  262. ;; beginning and/or end of buffer.
  263. ;;
  264. ;; `whitespace-space-after-tab' Face used to visualize 8 or more
  265. ;; SPACEs after TAB.
  266. ;;
  267. ;; `whitespace-space-regexp' Specify SPACE characters regexp.
  268. ;;
  269. ;; `whitespace-hspace-regexp' Specify HARD SPACE characters regexp.
  270. ;;
  271. ;; `whitespace-tab-regexp' Specify TAB characters regexp.
  272. ;;
  273. ;; `whitespace-trailing-regexp' Specify trailing characters regexp.
  274. ;;
  275. ;; `whitespace-space-before-tab-regexp' Specify SPACEs before TAB
  276. ;; regexp.
  277. ;;
  278. ;; `whitespace-indentation-regexp' Specify regexp for 8 or more
  279. ;; SPACEs at beginning of line.
  280. ;;
  281. ;; `whitespace-empty-at-bob-regexp' Specify regexp for empty lines
  282. ;; at beginning of buffer.
  283. ;;
  284. ;; `whitespace-empty-at-eob-regexp' Specify regexp for empty lines
  285. ;; at end of buffer.
  286. ;;
  287. ;; `whitespace-space-after-tab-regexp' Specify regexp for 8 or more
  288. ;; SPACEs after TAB.
  289. ;;
  290. ;; `whitespace-line-column' Specify column beyond which the line
  291. ;; is highlighted.
  292. ;;
  293. ;; `whitespace-display-mappings' Specify an alist of mappings
  294. ;; for displaying characters.
  295. ;;
  296. ;; `whitespace-global-modes' Modes for which global
  297. ;; `whitespace-mode' is automagically
  298. ;; turned on.
  299. ;;
  300. ;; `whitespace-action' Specify which action is taken when a
  301. ;; buffer is visited or written.
  302. ;;
  303. ;;
  304. ;; Acknowledgements
  305. ;; ----------------
  306. ;;
  307. ;; Thanks to felix (EmacsWiki) for keeping highlight when switching between
  308. ;; major modes on a file.
  309. ;;
  310. ;; Thanks to David Reitter <david.reitter@gmail.com> for suggesting a
  311. ;; `whitespace-newline' initialization with low contrast relative to
  312. ;; the background color.
  313. ;;
  314. ;; Thanks to Stephen Deasey <sdeasey@gmail.com> for the
  315. ;; `indent-tabs-mode' usage suggestion.
  316. ;;
  317. ;; Thanks to Eric Cooper <ecc@cmu.edu> for the suggestion to have hook
  318. ;; actions when buffer is written as the original whitespace package
  319. ;; had.
  320. ;;
  321. ;; Thanks to nschum (EmacsWiki) for the idea about highlight "long"
  322. ;; lines tail. See EightyColumnRule (EmacsWiki).
  323. ;;
  324. ;; Thanks to Juri Linkov <juri@jurta.org> for suggesting:
  325. ;; * `define-minor-mode'.
  326. ;; * `global-whitespace-*' name for global commands.
  327. ;;
  328. ;; Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.
  329. ;;
  330. ;; Thanks to Drew Adams <drew.adams@oracle.com> for toggle commands
  331. ;; suggestion.
  332. ;;
  333. ;; Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for
  334. ;; helping to fix `find-file-hooks' reference.
  335. ;;
  336. ;; Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for
  337. ;; indicating defface byte-compilation warnings.
  338. ;;
  339. ;; Thanks to TimOCallaghan (EmacsWiki) for the idea about highlight
  340. ;; "long" lines. See EightyColumnRule (EmacsWiki).
  341. ;;
  342. ;; Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new
  343. ;; NEWLINE character mapping.
  344. ;;
  345. ;; Thanks to Pete Forman <pete.forman@westgeo.com> for indicating
  346. ;; whitespace-mode.el on XEmacs.
  347. ;;
  348. ;; Thanks to Miles Bader <miles@gnu.org> for handling display table via
  349. ;; visws.el (his code was modified, but the main idea was kept).
  350. ;;
  351. ;; Thanks to:
  352. ;; Rajesh Vaidheeswarran <rv@gnu.org> (original) whitespace.el
  353. ;; Aurelien Tisne <aurelien.tisne@free.fr> show-whitespace-mode.el
  354. ;; Lawrence Mitchell <wence@gmx.li> whitespace-mode.el
  355. ;; Miles Bader <miles@gnu.org> visws.el
  356. ;; And to all people who contributed with them.
  357. ;;
  358. ;;
  359. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  360. ;;; code:
  361. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  362. ;;;; User Variables:
  363. ;;; Interface to the command system
  364. (defgroup whitespace nil
  365. "Visualize blanks (TAB, (HARD) SPACE and NEWLINE)."
  366. :link '(emacs-library-link :tag "Source Lisp File" "whitespace.el")
  367. :version "23.1"
  368. :group 'convenience)
  369. (defcustom whitespace-style
  370. '(face
  371. tabs spaces trailing lines space-before-tab newline
  372. indentation empty space-after-tab
  373. space-mark tab-mark newline-mark)
  374. "Specify which kind of blank is visualized.
  375. It's a list containing some or all of the following values:
  376. face enable all visualization via faces (see below).
  377. trailing trailing blanks are visualized via faces.
  378. It has effect only if `face' (see above)
  379. is present in `whitespace-style'.
  380. tabs TABs are visualized via faces.
  381. It has effect only if `face' (see above)
  382. is present in `whitespace-style'.
  383. spaces SPACEs and HARD SPACEs are visualized via
  384. faces.
  385. It has effect only if `face' (see above)
  386. is present in `whitespace-style'.
  387. lines lines which have columns beyond
  388. `whitespace-line-column' are highlighted via
  389. faces.
  390. Whole line is highlighted.
  391. It has precedence over `lines-tail' (see
  392. below).
  393. It has effect only if `face' (see above)
  394. is present in `whitespace-style'.
  395. lines-tail lines which have columns beyond
  396. `whitespace-line-column' are highlighted via
  397. faces.
  398. But only the part of line which goes
  399. beyond `whitespace-line-column' column.
  400. It has effect only if `lines' (see above)
  401. is not present in `whitespace-style'
  402. and if `face' (see above) is present in
  403. `whitespace-style'.
  404. newline NEWLINEs are visualized via faces.
  405. It has effect only if `face' (see above)
  406. is present in `whitespace-style'.
  407. empty empty lines at beginning and/or end of buffer
  408. are visualized via faces.
  409. It has effect only if `face' (see above)
  410. is present in `whitespace-style'.
  411. indentation::tab 8 or more SPACEs at beginning of line are
  412. visualized via faces.
  413. It has effect only if `face' (see above)
  414. is present in `whitespace-style'.
  415. indentation::space TABs at beginning of line are visualized via
  416. faces.
  417. It has effect only if `face' (see above)
  418. is present in `whitespace-style'.
  419. indentation 8 or more SPACEs at beginning of line are
  420. visualized, if `indent-tabs-mode' (which see)
  421. is non-nil; otherwise, TABs at beginning of
  422. line are visualized via faces.
  423. It has effect only if `face' (see above)
  424. is present in `whitespace-style'.
  425. space-after-tab::tab 8 or more SPACEs after a TAB are
  426. visualized via faces.
  427. It has effect only if `face' (see above)
  428. is present in `whitespace-style'.
  429. space-after-tab::space TABs are visualized when 8 or more
  430. SPACEs occur after a TAB, via faces.
  431. It has effect only if `face' (see above)
  432. is present in `whitespace-style'.
  433. space-after-tab 8 or more SPACEs after a TAB are
  434. visualized, if `indent-tabs-mode'
  435. (which see) is non-nil; otherwise,
  436. the TABs are visualized via faces.
  437. It has effect only if `face' (see above)
  438. is present in `whitespace-style'.
  439. space-before-tab::tab SPACEs before TAB are visualized via
  440. faces.
  441. It has effect only if `face' (see above)
  442. is present in `whitespace-style'.
  443. space-before-tab::space TABs are visualized when SPACEs occur
  444. before TAB, via faces.
  445. It has effect only if `face' (see above)
  446. is present in `whitespace-style'.
  447. space-before-tab SPACEs before TAB are visualized, if
  448. `indent-tabs-mode' (which see) is
  449. non-nil; otherwise, the TABs are
  450. visualized via faces.
  451. It has effect only if `face' (see above)
  452. is present in `whitespace-style'.
  453. space-mark SPACEs and HARD SPACEs are visualized via
  454. display table.
  455. tab-mark TABs are visualized via display table.
  456. newline-mark NEWLINEs are visualized via display table.
  457. Any other value is ignored.
  458. If nil, don't visualize TABs, (HARD) SPACEs and NEWLINEs via faces and
  459. via display table.
  460. There is an evaluation order for some values, if they are
  461. included in `whitespace-style' list. For example, if
  462. indentation, indentation::tab and/or indentation::space are
  463. included in `whitespace-style' list. The evaluation order for
  464. these values is:
  465. * For indentation:
  466. 1. indentation
  467. 2. indentation::tab
  468. 3. indentation::space
  469. * For SPACEs after TABs:
  470. 1. space-after-tab
  471. 2. space-after-tab::tab
  472. 3. space-after-tab::space
  473. * For SPACEs before TABs:
  474. 1. space-before-tab
  475. 2. space-before-tab::tab
  476. 3. space-before-tab::space
  477. So, for example, if indentation and indentation::space are
  478. included in `whitespace-style' list, the indentation value is
  479. evaluated instead of indentation::space value.
  480. One reason for not visualize spaces via faces (if `face' is not
  481. included in `whitespace-style') is to use exclusively for
  482. cleaning up a buffer. See `whitespace-cleanup' and
  483. `whitespace-cleanup-region' for documentation.
  484. See also `whitespace-display-mappings' for documentation."
  485. :type '(repeat :tag "Kind of Blank"
  486. (choice :tag "Kind of Blank Face"
  487. (const :tag "(Face) Face visualization"
  488. face)
  489. (const :tag "(Face) Trailing TABs, SPACEs and HARD SPACEs"
  490. trailing)
  491. (const :tag "(Face) SPACEs and HARD SPACEs"
  492. spaces)
  493. (const :tag "(Face) TABs" tabs)
  494. (const :tag "(Face) Lines" lines)
  495. (const :tag "(Face) SPACEs before TAB"
  496. space-before-tab)
  497. (const :tag "(Face) NEWLINEs" newline)
  498. (const :tag "(Face) Indentation SPACEs"
  499. indentation)
  500. (const :tag "(Face) Empty Lines At BOB And/Or EOB"
  501. empty)
  502. (const :tag "(Face) SPACEs after TAB"
  503. space-after-tab)
  504. (const :tag "(Mark) SPACEs and HARD SPACEs"
  505. space-mark)
  506. (const :tag "(Mark) TABs" tab-mark)
  507. (const :tag "(Mark) NEWLINEs" newline-mark)))
  508. :group 'whitespace)
  509. (defcustom whitespace-space 'whitespace-space
  510. "Symbol face used to visualize SPACE.
  511. Used when `whitespace-style' includes the value `spaces'."
  512. :type 'face
  513. :group 'whitespace)
  514. (defface whitespace-space
  515. '((((class color) (background dark))
  516. (:background "grey20" :foreground "darkgray"))
  517. (((class color) (background light))
  518. (:background "LightYellow" :foreground "lightgray"))
  519. (t (:inverse-video t)))
  520. "Face used to visualize SPACE."
  521. :group 'whitespace)
  522. (defcustom whitespace-hspace 'whitespace-hspace
  523. "Symbol face used to visualize HARD SPACE.
  524. Used when `whitespace-style' includes the value `spaces'."
  525. :type 'face
  526. :group 'whitespace)
  527. (defface whitespace-hspace ; 'nobreak-space
  528. '((((class color) (background dark))
  529. (:background "grey24" :foreground "darkgray"))
  530. (((class color) (background light))
  531. (:background "LemonChiffon3" :foreground "lightgray"))
  532. (t (:inverse-video t)))
  533. "Face used to visualize HARD SPACE."
  534. :group 'whitespace)
  535. (defcustom whitespace-tab 'whitespace-tab
  536. "Symbol face used to visualize TAB.
  537. Used when `whitespace-style' includes the value `tabs'."
  538. :type 'face
  539. :group 'whitespace)
  540. (defface whitespace-tab
  541. '((((class color) (background dark))
  542. (:background "grey22" :foreground "darkgray"))
  543. (((class color) (background light))
  544. (:background "beige" :foreground "lightgray"))
  545. (t (:inverse-video t)))
  546. "Face used to visualize TAB."
  547. :group 'whitespace)
  548. (defcustom whitespace-newline 'whitespace-newline
  549. "Symbol face used to visualize NEWLINE char mapping.
  550. See `whitespace-display-mappings'.
  551. Used when `whitespace-style' includes the values `newline-mark'
  552. and `newline'."
  553. :type 'face
  554. :group 'whitespace)
  555. (defface whitespace-newline
  556. '((((class color) (background dark))
  557. (:foreground "darkgray" :bold nil))
  558. (((class color) (min-colors 88) (background light))
  559. (:foreground "lightgray" :bold nil))
  560. ;; Displays with 16 colors use lightgray as background, so using a
  561. ;; lightgray foreground makes the newline mark invisible.
  562. (((class color) (background light))
  563. (:foreground "brown" :bold nil))
  564. (t (:underline t :bold nil)))
  565. "Face used to visualize NEWLINE char mapping.
  566. See `whitespace-display-mappings'."
  567. :group 'whitespace)
  568. (defcustom whitespace-trailing 'whitespace-trailing
  569. "Symbol face used to visualize trailing blanks.
  570. Used when `whitespace-style' includes the value `trailing'."
  571. :type 'face
  572. :group 'whitespace)
  573. (defface whitespace-trailing ; 'trailing-whitespace
  574. '((((class mono)) (:inverse-video t :bold t :underline t))
  575. (t (:background "red1" :foreground "yellow" :bold t)))
  576. "Face used to visualize trailing blanks."
  577. :group 'whitespace)
  578. (defcustom whitespace-line 'whitespace-line
  579. "Symbol face used to visualize \"long\" lines.
  580. See `whitespace-line-column'.
  581. Used when `whitespace-style' includes the value `line'."
  582. :type 'face
  583. :group 'whitespace)
  584. (defface whitespace-line
  585. '((((class mono)) (:inverse-video t :bold t :underline t))
  586. (t (:background "gray20" :foreground "violet")))
  587. "Face used to visualize \"long\" lines.
  588. See `whitespace-line-column'."
  589. :group 'whitespace)
  590. (defcustom whitespace-space-before-tab 'whitespace-space-before-tab
  591. "Symbol face used to visualize SPACEs before TAB.
  592. Used when `whitespace-style' includes the value `space-before-tab'."
  593. :type 'face
  594. :group 'whitespace)
  595. (defface whitespace-space-before-tab
  596. '((((class mono)) (:inverse-video t :bold t :underline t))
  597. (t (:background "DarkOrange" :foreground "firebrick")))
  598. "Face used to visualize SPACEs before TAB."
  599. :group 'whitespace)
  600. (defcustom whitespace-indentation 'whitespace-indentation
  601. "Symbol face used to visualize 8 or more SPACEs at beginning of line.
  602. Used when `whitespace-style' includes the value `indentation'."
  603. :type 'face
  604. :group 'whitespace)
  605. (defface whitespace-indentation
  606. '((((class mono)) (:inverse-video t :bold t :underline t))
  607. (t (:background "yellow" :foreground "firebrick")))
  608. "Face used to visualize 8 or more SPACEs at beginning of line."
  609. :group 'whitespace)
  610. (defcustom whitespace-empty 'whitespace-empty
  611. "Symbol face used to visualize empty lines at beginning and/or end of buffer.
  612. Used when `whitespace-style' includes the value `empty'."
  613. :type 'face
  614. :group 'whitespace)
  615. (defface whitespace-empty
  616. '((((class mono)) (:inverse-video t :bold t :underline t))
  617. (t (:background "yellow" :foreground "firebrick")))
  618. "Face used to visualize empty lines at beginning and/or end of buffer."
  619. :group 'whitespace)
  620. (defcustom whitespace-space-after-tab 'whitespace-space-after-tab
  621. "Symbol face used to visualize 8 or more SPACEs after TAB.
  622. Used when `whitespace-style' includes the value `space-after-tab'."
  623. :type 'face
  624. :group 'whitespace)
  625. (defface whitespace-space-after-tab
  626. '((((class mono)) (:inverse-video t :bold t :underline t))
  627. (t (:background "yellow" :foreground "firebrick")))
  628. "Face used to visualize 8 or more SPACEs after TAB."
  629. :group 'whitespace)
  630. (defcustom whitespace-hspace-regexp
  631. "\\(\\(\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)"
  632. "Specify HARD SPACE characters regexp.
  633. If you're using `mule' package, there may be other characters besides:
  634. \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \"\\xF20\"
  635. that should be considered HARD SPACE.
  636. Here are some examples:
  637. \"\\\\(^\\xA0+\\\\)\" \
  638. visualize only leading HARD SPACEs.
  639. \"\\\\(\\xA0+$\\\\)\" \
  640. visualize only trailing HARD SPACEs.
  641. \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
  642. visualize leading and/or trailing HARD SPACEs.
  643. \"\\t\\\\(\\xA0+\\\\)\\t\" \
  644. visualize only HARD SPACEs between TABs.
  645. NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
  646. Use exactly one pair of enclosing \\\\( and \\\\).
  647. Used when `whitespace-style' includes `spaces'."
  648. :type '(regexp :tag "HARD SPACE Chars")
  649. :group 'whitespace)
  650. (defcustom whitespace-space-regexp "\\( +\\)"
  651. "Specify SPACE characters regexp.
  652. If you're using `mule' package, there may be other characters
  653. besides \" \" that should be considered SPACE.
  654. Here are some examples:
  655. \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
  656. \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
  657. \"\\\\(^ +\\\\| +$\\\\)\" \
  658. visualize leading and/or trailing SPACEs.
  659. \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
  660. NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
  661. Use exactly one pair of enclosing \\\\( and \\\\).
  662. Used when `whitespace-style' includes `spaces'."
  663. :type '(regexp :tag "SPACE Chars")
  664. :group 'whitespace)
  665. (defcustom whitespace-tab-regexp "\\(\t+\\)"
  666. "Specify TAB characters regexp.
  667. If you're using `mule' package, there may be other characters
  668. besides \"\\t\" that should be considered TAB.
  669. Here are some examples:
  670. \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
  671. \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
  672. \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
  673. visualize leading and/or trailing TABs.
  674. \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
  675. NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
  676. Use exactly one pair of enclosing \\\\( and \\\\).
  677. Used when `whitespace-style' includes `tabs'."
  678. :type '(regexp :tag "TAB Chars")
  679. :group 'whitespace)
  680. (defcustom whitespace-trailing-regexp
  681. "\\([\t \u00A0]+\\)$"
  682. "Specify trailing characters regexp.
  683. If you're using `mule' package, there may be other characters besides:
  684. \" \" \"\\t\" \"\\u00A0\"
  685. that should be considered blank.
  686. NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
  687. Use exactly one pair of enclosing elements above.
  688. Used when `whitespace-style' includes `trailing'."
  689. :type '(regexp :tag "Trailing Chars")
  690. :group 'whitespace)
  691. (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
  692. "Specify SPACEs before TAB regexp.
  693. If you're using `mule' package, there may be other characters besides:
  694. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  695. \"\\xF20\"
  696. that should be considered blank.
  697. Used when `whitespace-style' includes `space-before-tab',
  698. `space-before-tab::tab' or `space-before-tab::space'."
  699. :type '(regexp :tag "SPACEs Before TAB")
  700. :group 'whitespace)
  701. (defcustom whitespace-indentation-regexp
  702. '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
  703. . "^ *\\(\t+\\)[^\n]")
  704. "Specify regexp for 8 or more SPACEs at beginning of line.
  705. It is a cons where the cons car is used for SPACEs visualization
  706. and the cons cdr is used for TABs visualization.
  707. If you're using `mule' package, there may be other characters besides:
  708. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  709. \"\\xF20\"
  710. that should be considered blank.
  711. Used when `whitespace-style' includes `indentation',
  712. `indentation::tab' or `indentation::space'."
  713. :type '(cons (regexp :tag "Indentation SPACEs")
  714. (regexp :tag "Indentation TABs"))
  715. :group 'whitespace)
  716. (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)"
  717. "Specify regexp for empty lines at beginning of buffer.
  718. If you're using `mule' package, there may be other characters besides:
  719. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  720. \"\\xF20\"
  721. that should be considered blank.
  722. Used when `whitespace-style' includes `empty'."
  723. :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
  724. :group 'whitespace)
  725. (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)"
  726. "Specify regexp for empty lines at end of buffer.
  727. If you're using `mule' package, there may be other characters besides:
  728. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  729. \"\\xF20\"
  730. that should be considered blank.
  731. Used when `whitespace-style' includes `empty'."
  732. :type '(regexp :tag "Empty Lines At End Of Buffer")
  733. :group 'whitespace)
  734. (defcustom whitespace-space-after-tab-regexp
  735. '("\t+\\(\\( \\{%d\\}\\)+\\)"
  736. . "\\(\t+\\) +")
  737. "Specify regexp for 8 or more SPACEs after TAB.
  738. It is a cons where the cons car is used for SPACEs visualization
  739. and the cons cdr is used for TABs visualization.
  740. If you're using `mule' package, there may be other characters besides:
  741. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  742. \"\\xF20\"
  743. that should be considered blank.
  744. Used when `whitespace-style' includes `space-after-tab',
  745. `space-after-tab::tab' or `space-after-tab::space'."
  746. :type '(regexp :tag "SPACEs After TAB")
  747. :group 'whitespace)
  748. (defcustom whitespace-line-column 80
  749. "Specify column beyond which the line is highlighted.
  750. It must be an integer or nil. If nil, the `fill-column' variable value is
  751. used.
  752. Used when `whitespace-style' includes `lines' or `lines-tail'."
  753. :type '(choice :tag "Line Length Limit"
  754. (integer :tag "Line Length")
  755. (const :tag "Use fill-column" nil))
  756. :group 'whitespace)
  757. ;; Hacked from `visible-whitespace-mappings' in visws.el
  758. (defcustom whitespace-display-mappings
  759. '(
  760. (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
  761. (space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency
  762. (space-mark ?\x8A0 [?\x8A4] [?_]) ; hard space - currency
  763. (space-mark ?\x920 [?\x924] [?_]) ; hard space - currency
  764. (space-mark ?\xE20 [?\xE24] [?_]) ; hard space - currency
  765. (space-mark ?\xF20 [?\xF24] [?_]) ; hard space - currency
  766. ;; NEWLINE is displayed using the face `whitespace-newline'
  767. (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
  768. ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
  769. ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
  770. ;; (newline-mark ?\n [?\x8AF ?\n] [?$ ?\n]) ; eol - overscore
  771. ;; (newline-mark ?\n [?\x8AC ?\n] [?$ ?\n]) ; eol - negation
  772. ;; (newline-mark ?\n [?\x8B0 ?\n] [?$ ?\n]) ; eol - grade
  773. ;;
  774. ;; WARNING: the mapping below has a problem.
  775. ;; When a TAB occupies exactly one column, it will display the
  776. ;; character ?\xBB at that column followed by a TAB which goes to
  777. ;; the next TAB column.
  778. ;; If this is a problem for you, please, comment the line below.
  779. (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; tab - left quote mark
  780. )
  781. "Specify an alist of mappings for displaying characters.
  782. Each element has the following form:
  783. (KIND CHAR VECTOR...)
  784. Where:
  785. KIND is the kind of character.
  786. It can be one of the following symbols:
  787. tab-mark for TAB character
  788. space-mark for SPACE or HARD SPACE character
  789. newline-mark for NEWLINE character
  790. CHAR is the character to be mapped.
  791. VECTOR is a vector of characters to be displayed in place of CHAR.
  792. The first display vector that can be displayed is used;
  793. if no display vector for a mapping can be displayed, then
  794. that character is displayed unmodified.
  795. The NEWLINE character is displayed using the face given by
  796. `whitespace-newline' variable.
  797. Used when `whitespace-style' includes `tab-mark', `space-mark' or
  798. `newline-mark'."
  799. :type '(repeat
  800. (list :tag "Character Mapping"
  801. (choice :tag "Char Kind"
  802. (const :tag "Tab" tab-mark)
  803. (const :tag "Space" space-mark)
  804. (const :tag "Newline" newline-mark))
  805. (character :tag "Char")
  806. (repeat :inline t :tag "Vector List"
  807. (vector :tag ""
  808. (repeat :inline t
  809. :tag "Vector Characters"
  810. (character :tag "Char"))))))
  811. :group 'whitespace)
  812. (defcustom whitespace-global-modes t
  813. "Modes for which global `whitespace-mode' is automagically turned on.
  814. Global `whitespace-mode' is controlled by the command
  815. `global-whitespace-mode'.
  816. If nil, means no modes have `whitespace-mode' automatically
  817. turned on.
  818. If t, all modes that support `whitespace-mode' have it
  819. automatically turned on.
  820. Else it should be a list of `major-mode' symbol names for which
  821. `whitespace-mode' should be automatically turned on. The sense
  822. of the list is negated if it begins with `not'. For example:
  823. (c-mode c++-mode)
  824. means that `whitespace-mode' is turned on for buffers in C and
  825. C++ modes only."
  826. :type '(choice :tag "Global Modes"
  827. (const :tag "None" nil)
  828. (const :tag "All" t)
  829. (set :menu-tag "Mode Specific" :tag "Modes"
  830. :value (not)
  831. (const :tag "Except" not)
  832. (repeat :inline t
  833. (symbol :tag "Mode"))))
  834. :group 'whitespace)
  835. (defcustom whitespace-action nil
  836. "Specify which action is taken when a buffer is visited or written.
  837. It's a list containing some or all of the following values:
  838. nil no action is taken.
  839. cleanup cleanup any bogus whitespace always when local
  840. whitespace is turned on.
  841. See `whitespace-cleanup' and
  842. `whitespace-cleanup-region'.
  843. report-on-bogus report if there is any bogus whitespace always
  844. when local whitespace is turned on.
  845. auto-cleanup cleanup any bogus whitespace when buffer is
  846. written.
  847. See `whitespace-cleanup' and
  848. `whitespace-cleanup-region'.
  849. abort-on-bogus abort if there is any bogus whitespace and the
  850. buffer is written.
  851. warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
  852. is included in `whitespace-action' and the
  853. buffer is read-only.
  854. Any other value is treated as nil."
  855. :type '(choice :tag "Actions"
  856. (const :tag "None" nil)
  857. (repeat :tag "Action List"
  858. (choice :tag "Action"
  859. (const :tag "Cleanup When On" cleanup)
  860. (const :tag "Report On Bogus" report-on-bogus)
  861. (const :tag "Auto Cleanup" auto-cleanup)
  862. (const :tag "Abort On Bogus" abort-on-bogus)
  863. (const :tag "Warn If Read-Only" warn-if-read-only))))
  864. :group 'whitespace)
  865. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  866. ;;;; User commands - Local mode
  867. ;;;###autoload
  868. (define-minor-mode whitespace-mode
  869. "Toggle whitespace visualization (Whitespace mode).
  870. With a prefix argument ARG, enable Whitespace mode if ARG is
  871. positive, and disable it otherwise. If called from Lisp, enable
  872. the mode if ARG is omitted or nil.
  873. See also `whitespace-style', `whitespace-newline' and
  874. `whitespace-display-mappings'."
  875. :lighter " ws"
  876. :init-value nil
  877. :global nil
  878. :group 'whitespace
  879. (cond
  880. (noninteractive ; running a batch job
  881. (setq whitespace-mode nil))
  882. (whitespace-mode ; whitespace-mode on
  883. (whitespace-turn-on)
  884. (whitespace-action-when-on))
  885. (t ; whitespace-mode off
  886. (whitespace-turn-off))))
  887. ;;;###autoload
  888. (define-minor-mode whitespace-newline-mode
  889. "Toggle newline visualization (Whitespace Newline mode).
  890. With a prefix argument ARG, enable Whitespace Newline mode if ARG
  891. is positive, and disable it otherwise. If called from Lisp,
  892. enable the mode if ARG is omitted or nil.
  893. Use `whitespace-newline-mode' only for NEWLINE visualization
  894. exclusively. For other visualizations, including NEWLINE
  895. visualization together with (HARD) SPACEs and/or TABs, please,
  896. use `whitespace-mode'.
  897. See also `whitespace-newline' and `whitespace-display-mappings'."
  898. :lighter " nl"
  899. :init-value nil
  900. :global nil
  901. :group 'whitespace
  902. (let ((whitespace-style '(face newline-mark newline)))
  903. (whitespace-mode (if whitespace-newline-mode
  904. 1 -1)))
  905. ;; sync states (running a batch job)
  906. (setq whitespace-newline-mode whitespace-mode))
  907. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  908. ;;;; User commands - Global mode
  909. ;;;###autoload
  910. (define-minor-mode global-whitespace-mode
  911. "Toggle whitespace visualization globally (Global Whitespace mode).
  912. With a prefix argument ARG, enable Global Whitespace mode if ARG
  913. is positive, and disable it otherwise. If called from Lisp,
  914. enable it if ARG is omitted or nil.
  915. See also `whitespace-style', `whitespace-newline' and
  916. `whitespace-display-mappings'."
  917. :lighter " WS"
  918. :init-value nil
  919. :global t
  920. :group 'whitespace
  921. (cond
  922. (noninteractive ; running a batch job
  923. (setq global-whitespace-mode nil))
  924. (global-whitespace-mode ; global-whitespace-mode on
  925. (save-current-buffer
  926. (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
  927. (add-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
  928. (dolist (buffer (buffer-list)) ; adjust all local mode
  929. (set-buffer buffer)
  930. (unless whitespace-mode
  931. (whitespace-turn-on-if-enabled)))))
  932. (t ; global-whitespace-mode off
  933. (save-current-buffer
  934. (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
  935. (remove-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
  936. (dolist (buffer (buffer-list)) ; adjust all local mode
  937. (set-buffer buffer)
  938. (unless whitespace-mode
  939. (whitespace-turn-off)))))))
  940. (defun whitespace-turn-on-if-enabled ()
  941. (when (cond
  942. ((eq whitespace-global-modes t))
  943. ((listp whitespace-global-modes)
  944. (if (eq (car-safe whitespace-global-modes) 'not)
  945. (not (memq major-mode (cdr whitespace-global-modes)))
  946. (memq major-mode whitespace-global-modes)))
  947. (t nil))
  948. (let (inhibit-quit)
  949. ;; Don't turn on whitespace mode if...
  950. (or
  951. ;; ...we don't have a display (we're running a batch job)
  952. noninteractive
  953. ;; ...or if the buffer is invisible (name starts with a space)
  954. (eq (aref (buffer-name) 0) ?\ )
  955. ;; ...or if the buffer is temporary (name starts with *)
  956. (and (eq (aref (buffer-name) 0) ?*)
  957. ;; except the scratch buffer.
  958. (not (string= (buffer-name) "*scratch*")))
  959. ;; Otherwise, turn on whitespace mode.
  960. (whitespace-turn-on)))))
  961. ;;;###autoload
  962. (define-minor-mode global-whitespace-newline-mode
  963. "Toggle global newline visualization (Global Whitespace Newline mode).
  964. With a prefix argument ARG, enable Global Whitespace Newline mode
  965. if ARG is positive, and disable it otherwise. If called from
  966. Lisp, enable it if ARG is omitted or nil.
  967. Use `global-whitespace-newline-mode' only for NEWLINE
  968. visualization exclusively. For other visualizations, including
  969. NEWLINE visualization together with (HARD) SPACEs and/or TABs,
  970. please use `global-whitespace-mode'.
  971. See also `whitespace-newline' and `whitespace-display-mappings'."
  972. :lighter " NL"
  973. :init-value nil
  974. :global t
  975. :group 'whitespace
  976. (let ((whitespace-style '(newline-mark newline)))
  977. (global-whitespace-mode (if global-whitespace-newline-mode
  978. 1 -1))
  979. ;; sync states (running a batch job)
  980. (setq global-whitespace-newline-mode global-whitespace-mode)))
  981. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  982. ;;;; User commands - Toggle
  983. (defconst whitespace-style-value-list
  984. '(face
  985. tabs
  986. spaces
  987. trailing
  988. lines
  989. lines-tail
  990. newline
  991. empty
  992. indentation
  993. indentation::tab
  994. indentation::space
  995. space-after-tab
  996. space-after-tab::tab
  997. space-after-tab::space
  998. space-before-tab
  999. space-before-tab::tab
  1000. space-before-tab::space
  1001. help-newline ; value used by `whitespace-insert-option-mark'
  1002. tab-mark
  1003. space-mark
  1004. newline-mark
  1005. )
  1006. "List of valid `whitespace-style' values.")
  1007. (defconst whitespace-toggle-option-alist
  1008. '((?f . face)
  1009. (?t . tabs)
  1010. (?s . spaces)
  1011. (?r . trailing)
  1012. (?l . lines)
  1013. (?L . lines-tail)
  1014. (?n . newline)
  1015. (?e . empty)
  1016. (?\C-i . indentation)
  1017. (?I . indentation::tab)
  1018. (?i . indentation::space)
  1019. (?\C-a . space-after-tab)
  1020. (?A . space-after-tab::tab)
  1021. (?a . space-after-tab::space)
  1022. (?\C-b . space-before-tab)
  1023. (?B . space-before-tab::tab)
  1024. (?b . space-before-tab::space)
  1025. (?T . tab-mark)
  1026. (?S . space-mark)
  1027. (?N . newline-mark)
  1028. (?x . whitespace-style)
  1029. )
  1030. "Alist of toggle options.
  1031. Each element has the form:
  1032. (CHAR . SYMBOL)
  1033. Where:
  1034. CHAR is a char which the user will have to type.
  1035. SYMBOL is a valid symbol associated with CHAR.
  1036. See `whitespace-style-value-list'.")
  1037. (defvar whitespace-active-style nil
  1038. "Used to save locally `whitespace-style' value.")
  1039. (defvar whitespace-indent-tabs-mode indent-tabs-mode
  1040. "Used to save locally `indent-tabs-mode' value.")
  1041. (defvar whitespace-tab-width tab-width
  1042. "Used to save locally `tab-width' value.")
  1043. (defvar whitespace-point (point)
  1044. "Used to save locally current point value.
  1045. Used by `whitespace-trailing-regexp' function (which see).")
  1046. (defvar whitespace-font-lock-refontify nil
  1047. "Used to save locally the font-lock refontify state.
  1048. Used by `whitespace-post-command-hook' function (which see).")
  1049. (defvar whitespace-bob-marker nil
  1050. "Used to save locally the bob marker value.
  1051. Used by `whitespace-post-command-hook' function (which see).")
  1052. (defvar whitespace-eob-marker nil
  1053. "Used to save locally the eob marker value.
  1054. Used by `whitespace-post-command-hook' function (which see).")
  1055. (defvar whitespace-buffer-changed nil
  1056. "Used to indicate locally if buffer changed.
  1057. Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
  1058. functions (which see).")
  1059. ;;;###autoload
  1060. (defun whitespace-toggle-options (arg)
  1061. "Toggle local `whitespace-mode' options.
  1062. If local whitespace-mode is off, toggle the option given by ARG
  1063. and turn on local whitespace-mode.
  1064. If local whitespace-mode is on, toggle the option given by ARG
  1065. and restart local whitespace-mode.
  1066. Interactively, it reads one of the following chars:
  1067. CHAR MEANING
  1068. (VIA FACES)
  1069. f toggle face visualization
  1070. t toggle TAB visualization
  1071. s toggle SPACE and HARD SPACE visualization
  1072. r toggle trailing blanks visualization
  1073. l toggle \"long lines\" visualization
  1074. L toggle \"long lines\" tail visualization
  1075. n toggle NEWLINE visualization
  1076. e toggle empty line at bob and/or eob visualization
  1077. C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
  1078. I toggle indentation SPACEs visualization
  1079. i toggle indentation TABs visualization
  1080. C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
  1081. A toggle SPACEs after TAB: SPACEs visualization
  1082. a toggle SPACEs after TAB: TABs visualization
  1083. C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
  1084. B toggle SPACEs before TAB: SPACEs visualization
  1085. b toggle SPACEs before TAB: TABs visualization
  1086. (VIA DISPLAY TABLE)
  1087. T toggle TAB visualization
  1088. S toggle SPACEs before TAB visualization
  1089. N toggle NEWLINE visualization
  1090. x restore `whitespace-style' value
  1091. ? display brief help
  1092. Non-interactively, ARG should be a symbol or a list of symbols.
  1093. The valid symbols are:
  1094. face toggle face visualization
  1095. tabs toggle TAB visualization
  1096. spaces toggle SPACE and HARD SPACE visualization
  1097. trailing toggle trailing blanks visualization
  1098. lines toggle \"long lines\" visualization
  1099. lines-tail toggle \"long lines\" tail visualization
  1100. newline toggle NEWLINE visualization
  1101. empty toggle empty line at bob and/or eob visualization
  1102. indentation toggle indentation SPACEs visualization
  1103. indentation::tab toggle indentation SPACEs visualization
  1104. indentation::space toggle indentation TABs visualization
  1105. space-after-tab toggle SPACEs after TAB visualization
  1106. space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
  1107. space-after-tab::space toggle SPACEs after TAB: TABs visualization
  1108. space-before-tab toggle SPACEs before TAB visualization
  1109. space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
  1110. space-before-tab::space toggle SPACEs before TAB: TABs visualization
  1111. tab-mark toggle TAB visualization
  1112. space-mark toggle SPACEs before TAB visualization
  1113. newline-mark toggle NEWLINE visualization
  1114. whitespace-style restore `whitespace-style' value
  1115. See `whitespace-style' and `indent-tabs-mode' for documentation."
  1116. (interactive (whitespace-interactive-char t))
  1117. (let ((whitespace-style
  1118. (whitespace-toggle-list t arg whitespace-active-style)))
  1119. (whitespace-mode 0)
  1120. (whitespace-mode 1)))
  1121. (defvar whitespace-toggle-style nil
  1122. "Used to toggle the global `whitespace-style' value.")
  1123. ;;;###autoload
  1124. (defun global-whitespace-toggle-options (arg)
  1125. "Toggle global `whitespace-mode' options.
  1126. If global whitespace-mode is off, toggle the option given by ARG
  1127. and turn on global whitespace-mode.
  1128. If global whitespace-mode is on, toggle the option given by ARG
  1129. and restart global whitespace-mode.
  1130. Interactively, it accepts one of the following chars:
  1131. CHAR MEANING
  1132. (VIA FACES)
  1133. f toggle face visualization
  1134. t toggle TAB visualization
  1135. s toggle SPACE and HARD SPACE visualization
  1136. r toggle trailing blanks visualization
  1137. l toggle \"long lines\" visualization
  1138. L toggle \"long lines\" tail visualization
  1139. n toggle NEWLINE visualization
  1140. e toggle empty line at bob and/or eob visualization
  1141. C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
  1142. I toggle indentation SPACEs visualization
  1143. i toggle indentation TABs visualization
  1144. C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
  1145. A toggle SPACEs after TAB: SPACEs visualization
  1146. a toggle SPACEs after TAB: TABs visualization
  1147. C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
  1148. B toggle SPACEs before TAB: SPACEs visualization
  1149. b toggle SPACEs before TAB: TABs visualization
  1150. (VIA DISPLAY TABLE)
  1151. T toggle TAB visualization
  1152. S toggle SPACEs before TAB visualization
  1153. N toggle NEWLINE visualization
  1154. x restore `whitespace-style' value
  1155. ? display brief help
  1156. Non-interactively, ARG should be a symbol or a list of symbols.
  1157. The valid symbols are:
  1158. face toggle face visualization
  1159. tabs toggle TAB visualization
  1160. spaces toggle SPACE and HARD SPACE visualization
  1161. trailing toggle trailing blanks visualization
  1162. lines toggle \"long lines\" visualization
  1163. lines-tail toggle \"long lines\" tail visualization
  1164. newline toggle NEWLINE visualization
  1165. empty toggle empty line at bob and/or eob visualization
  1166. indentation toggle indentation SPACEs visualization
  1167. indentation::tab toggle indentation SPACEs visualization
  1168. indentation::space toggle indentation TABs visualization
  1169. space-after-tab toggle SPACEs after TAB visualization
  1170. space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
  1171. space-after-tab::space toggle SPACEs after TAB: TABs visualization
  1172. space-before-tab toggle SPACEs before TAB visualization
  1173. space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
  1174. space-before-tab::space toggle SPACEs before TAB: TABs visualization
  1175. tab-mark toggle TAB visualization
  1176. space-mark toggle SPACEs before TAB visualization
  1177. newline-mark toggle NEWLINE visualization
  1178. whitespace-style restore `whitespace-style' value
  1179. See `whitespace-style' and `indent-tabs-mode' for documentation."
  1180. (interactive (whitespace-interactive-char nil))
  1181. (let ((whitespace-style
  1182. (whitespace-toggle-list nil arg whitespace-toggle-style)))
  1183. (setq whitespace-toggle-style whitespace-style)
  1184. (global-whitespace-mode 0)
  1185. (global-whitespace-mode 1)))
  1186. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1187. ;;;; User commands - Cleanup
  1188. ;;;###autoload
  1189. (defun whitespace-cleanup ()
  1190. "Cleanup some blank problems in all buffer or at region.
  1191. It usually applies to the whole buffer, but in transient mark
  1192. mode when the mark is active, it applies to the region. It also
  1193. applies to the region when it is not in transient mark mode, the
  1194. mark is active and \\[universal-argument] was pressed just before
  1195. calling `whitespace-cleanup' interactively.
  1196. See also `whitespace-cleanup-region'.
  1197. The problems cleaned up are:
  1198. 1. empty lines at beginning of buffer.
  1199. 2. empty lines at end of buffer.
  1200. If `whitespace-style' includes the value `empty', remove all
  1201. empty lines at beginning and/or end of buffer.
  1202. 3. 8 or more SPACEs at beginning of line.
  1203. If `whitespace-style' includes the value `indentation':
  1204. replace 8 or more SPACEs at beginning of line by TABs, if
  1205. `indent-tabs-mode' is non-nil; otherwise, replace TABs by
  1206. SPACEs.
  1207. If `whitespace-style' includes the value `indentation::tab',
  1208. replace 8 or more SPACEs at beginning of line by TABs.
  1209. If `whitespace-style' includes the value `indentation::space',
  1210. replace TABs by SPACEs.
  1211. 4. SPACEs before TAB.
  1212. If `whitespace-style' includes the value `space-before-tab':
  1213. replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  1214. otherwise, replace TABs by SPACEs.
  1215. If `whitespace-style' includes the value
  1216. `space-before-tab::tab', replace SPACEs by TABs.
  1217. If `whitespace-style' includes the value
  1218. `space-before-tab::space', replace TABs by SPACEs.
  1219. 5. SPACEs or TABs at end of line.
  1220. If `whitespace-style' includes the value `trailing', remove
  1221. all SPACEs or TABs at end of line.
  1222. 6. 8 or more SPACEs after TAB.
  1223. If `whitespace-style' includes the value `space-after-tab':
  1224. replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  1225. otherwise, replace TABs by SPACEs.
  1226. If `whitespace-style' includes the value
  1227. `space-after-tab::tab', replace SPACEs by TABs.
  1228. If `whitespace-style' includes the value
  1229. `space-after-tab::space', replace TABs by SPACEs.
  1230. See `whitespace-style', `indent-tabs-mode' and `tab-width' for
  1231. documentation."
  1232. (interactive "@")
  1233. (cond
  1234. ;; read-only buffer
  1235. (buffer-read-only
  1236. (whitespace-warn-read-only "cleanup"))
  1237. ;; region active
  1238. ((and (or transient-mark-mode
  1239. current-prefix-arg)
  1240. mark-active)
  1241. ;; PROBLEMs 1 and 2 are not handled in region
  1242. ;; PROBLEM 3: 8 or more SPACEs at bol
  1243. ;; PROBLEM 4: SPACEs before TAB
  1244. ;; PROBLEM 5: SPACEs or TABs at eol
  1245. ;; PROBLEM 6: 8 or more SPACEs after TAB
  1246. (whitespace-cleanup-region (region-beginning) (region-end)))
  1247. ;; whole buffer
  1248. (t
  1249. (save-excursion
  1250. (save-match-data ;FIXME: Why?
  1251. ;; PROBLEM 1: empty lines at bob
  1252. ;; PROBLEM 2: empty lines at eob
  1253. ;; ACTION: remove all empty lines at bob and/or eob
  1254. (when (memq 'empty whitespace-style)
  1255. (let (overwrite-mode) ; enforce no overwrite
  1256. (goto-char (point-min))
  1257. (when (re-search-forward
  1258. (concat "\\`" whitespace-empty-at-bob-regexp) nil t)
  1259. (delete-region (match-beginning 1) (match-end 1)))
  1260. (when (re-search-forward
  1261. (concat whitespace-empty-at-eob-regexp "\\'") nil t)
  1262. (delete-region (match-beginning 1) (match-end 1)))))))
  1263. ;; PROBLEM 3: 8 or more SPACEs at bol
  1264. ;; PROBLEM 4: SPACEs before TAB
  1265. ;; PROBLEM 5: SPACEs or TABs at eol
  1266. ;; PROBLEM 6: 8 or more SPACEs after TAB
  1267. (whitespace-cleanup-region (point-min) (point-max)))))
  1268. ;;;###autoload
  1269. (defun whitespace-cleanup-region (start end)
  1270. "Cleanup some blank problems at region.
  1271. The problems cleaned up are:
  1272. 1. 8 or more SPACEs at beginning of line.
  1273. If `whitespace-style' includes the value `indentation':
  1274. replace 8 or more SPACEs at beginning of line by TABs, if
  1275. `indent-tabs-mode' is non-nil; otherwise, replace TABs by
  1276. SPACEs.
  1277. If `whitespace-style' includes the value `indentation::tab',
  1278. replace 8 or more SPACEs at beginning of line by TABs.
  1279. If `whitespace-style' includes the value `indentation::space',
  1280. replace TABs by SPACEs.
  1281. 2. SPACEs before TAB.
  1282. If `whitespace-style' includes the value `space-before-tab':
  1283. replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  1284. otherwise, replace TABs by SPACEs.
  1285. If `whitespace-style' includes the value
  1286. `space-before-tab::tab', replace SPACEs by TABs.
  1287. If `whitespace-style' includes the value
  1288. `space-before-tab::space', replace TABs by SPACEs.
  1289. 3. SPACEs or TABs at end of line.
  1290. If `whitespace-style' includes the value `trailing', remove
  1291. all SPACEs or TABs at end of line.
  1292. 4. 8 or more SPACEs after TAB.
  1293. If `whitespace-style' includes the value `space-after-tab':
  1294. replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  1295. otherwise, replace TABs by SPACEs.
  1296. If `whitespace-style' includes the value
  1297. `space-after-tab::tab', replace SPACEs by TABs.
  1298. If `whitespace-style' includes the value
  1299. `space-after-tab::space', replace TABs by SPACEs.
  1300. See `whitespace-style', `indent-tabs-mode' and `tab-width' for
  1301. documentation."
  1302. (interactive "@r")
  1303. (if buffer-read-only
  1304. ;; read-only buffer
  1305. (whitespace-warn-read-only "cleanup region")
  1306. ;; non-read-only buffer
  1307. (let ((rstart (min start end))
  1308. (rend (copy-marker (max start end)))
  1309. (indent-tabs-mode whitespace-indent-tabs-mode)
  1310. (tab-width whitespace-tab-width)
  1311. overwrite-mode ; enforce no overwrite
  1312. tmp)
  1313. (save-excursion
  1314. (save-match-data ;FIXME: Why?
  1315. ;; PROBLEM 1: 8 or more SPACEs at bol
  1316. (cond
  1317. ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
  1318. ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
  1319. ;; by SPACEs.
  1320. ((memq 'indentation whitespace-style)
  1321. (let ((regexp (whitespace-indentation-regexp)))
  1322. (goto-char rstart)
  1323. (while (re-search-forward regexp rend t)
  1324. (setq tmp (current-indentation))
  1325. (goto-char (match-beginning 0))
  1326. (delete-horizontal-space)
  1327. (unless (eolp)
  1328. (indent-to tmp)))))
  1329. ;; ACTION: replace 8 or more SPACEs at bol by TABs.
  1330. ((memq 'indentation::tab whitespace-style)
  1331. (whitespace-replace-action
  1332. 'tabify rstart rend
  1333. (whitespace-indentation-regexp 'tab) 0))
  1334. ;; ACTION: replace TABs by SPACEs.
  1335. ((memq 'indentation::space whitespace-style)
  1336. (whitespace-replace-action
  1337. 'untabify rstart rend
  1338. (whitespace-indentation-regexp 'space) 0)))
  1339. ;; PROBLEM 3: SPACEs or TABs at eol
  1340. ;; ACTION: remove all SPACEs or TABs at eol
  1341. (when (memq 'trailing whitespace-style)
  1342. (whitespace-replace-action
  1343. 'delete-region rstart rend
  1344. whitespace-trailing-regexp 1))
  1345. ;; PROBLEM 4: 8 or more SPACEs after TAB
  1346. (cond
  1347. ;; ACTION: replace 8 or more SPACEs by TABs, if
  1348. ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
  1349. ;; by SPACEs.
  1350. ((memq 'space-after-tab whitespace-style)
  1351. (whitespace-replace-action
  1352. (if whitespace-indent-tabs-mode 'tabify 'untabify)
  1353. rstart rend (whitespace-space-after-tab-regexp) 1))
  1354. ;; ACTION: replace 8 or more SPACEs by TABs.
  1355. ((memq 'space-after-tab::tab whitespace-style)
  1356. (whitespace-replace-action
  1357. 'tabify rstart rend
  1358. (whitespace-space-after-tab-regexp 'tab) 1))
  1359. ;; ACTION: replace TABs by SPACEs.
  1360. ((memq 'space-after-tab::space whitespace-style)
  1361. (whitespace-replace-action
  1362. 'untabify rstart rend
  1363. (whitespace-space-after-tab-regexp 'space) 1)))
  1364. ;; PROBLEM 2: SPACEs before TAB
  1365. (cond
  1366. ;; ACTION: replace SPACEs before TAB by TABs, if
  1367. ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
  1368. ;; by SPACEs.
  1369. ((memq 'space-before-tab whitespace-style)
  1370. (whitespace-replace-action
  1371. (if whitespace-indent-tabs-mode 'tabify 'untabify)
  1372. rstart rend whitespace-space-before-tab-regexp
  1373. (if whitespace-indent-tabs-mode 0 2)))
  1374. ;; ACTION: replace SPACEs before TAB by TABs.
  1375. ((memq 'space-before-tab::tab whitespace-style)
  1376. (whitespace-replace-action
  1377. 'tabify rstart rend
  1378. whitespace-space-before-tab-regexp 0))
  1379. ;; ACTION: replace TABs by SPACEs.
  1380. ((memq 'space-before-tab::space whitespace-style)
  1381. (whitespace-replace-action
  1382. 'untabify rstart rend
  1383. whitespace-space-before-tab-regexp 2)))))
  1384. (set-marker rend nil)))) ; point marker to nowhere
  1385. (defun whitespace-replace-action (action rstart rend regexp index)
  1386. "Do ACTION in the string matched by REGEXP between RSTART and REND.
  1387. INDEX is the level group matched by REGEXP and used by ACTION.
  1388. See also `tab-width'."
  1389. (goto-char rstart)
  1390. (while (re-search-forward regexp rend t)
  1391. (goto-char (match-end index))
  1392. (funcall action (match-beginning index) (match-end index))))
  1393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1394. ;;;; User command - report
  1395. (defun whitespace-regexp (regexp &optional kind)
  1396. "Return REGEXP depending on `whitespace-indent-tabs-mode'."
  1397. (cond
  1398. ((or (eq kind 'tab)
  1399. whitespace-indent-tabs-mode)
  1400. (format (car regexp) whitespace-tab-width))
  1401. ((or (eq kind 'space)
  1402. (not whitespace-indent-tabs-mode))
  1403. (cdr regexp))))
  1404. (defun whitespace-indentation-regexp (&optional kind)
  1405. "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
  1406. (whitespace-regexp whitespace-indentation-regexp kind))
  1407. (defun whitespace-space-after-tab-regexp (&optional kind)
  1408. "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
  1409. (whitespace-regexp whitespace-space-after-tab-regexp kind))
  1410. (defconst whitespace-report-list
  1411. (list
  1412. (cons 'empty whitespace-empty-at-bob-regexp)
  1413. (cons 'empty whitespace-empty-at-eob-regexp)
  1414. (cons 'trailing whitespace-trailing-regexp)
  1415. (cons 'indentation nil)
  1416. (cons 'indentation::tab nil)
  1417. (cons 'indentation::space nil)
  1418. (cons 'space-before-tab whitespace-space-before-tab-regexp)
  1419. (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
  1420. (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
  1421. (cons 'space-after-tab nil)
  1422. (cons 'space-after-tab::tab nil)
  1423. (cons 'space-after-tab::space nil)
  1424. )
  1425. "List of whitespace bogus symbol and corresponding regexp.")
  1426. (defconst whitespace-report-text
  1427. '( ;; `indent-tabs-mode' has non-nil value
  1428. "\
  1429. Whitespace Report
  1430. Current Setting Whitespace Problem
  1431. empty [] [] empty lines at beginning of buffer
  1432. empty [] [] empty lines at end of buffer
  1433. trailing [] [] SPACEs or TABs at end of line
  1434. indentation [] [] 8 or more SPACEs at beginning of line
  1435. indentation::tab [] [] 8 or more SPACEs at beginning of line
  1436. indentation::space [] [] TABs at beginning of line
  1437. space-before-tab [] [] SPACEs before TAB
  1438. space-before-tab::tab [] [] SPACEs before TAB: SPACEs
  1439. space-before-tab::space [] [] SPACEs before TAB: TABs
  1440. space-after-tab [] [] 8 or more SPACEs after TAB
  1441. space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
  1442. space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
  1443. indent-tabs-mode =
  1444. tab-width = \n\n"
  1445. . ;; `indent-tabs-mode' has nil value
  1446. "\
  1447. Whitespace Report
  1448. Current Setting Whitespace Problem
  1449. empty [] [] empty lines at beginning of buffer
  1450. empty [] [] empty lines at end of buffer
  1451. trailing [] [] SPACEs or TABs at end of line
  1452. indentation [] [] TABs at beginning of line
  1453. indentation::tab [] [] 8 or more SPACEs at beginning of line
  1454. indentation::space [] [] TABs at beginning of line
  1455. space-before-tab [] [] SPACEs before TAB
  1456. space-before-tab::tab [] [] SPACEs before TAB: SPACEs
  1457. space-before-tab::space [] [] SPACEs before TAB: TABs
  1458. space-after-tab [] [] 8 or more SPACEs after TAB
  1459. space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
  1460. space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
  1461. indent-tabs-mode =
  1462. tab-width = \n\n")
  1463. "Text for whitespace bogus report.
  1464. It is a cons of strings, where the car part is used when
  1465. `indent-tabs-mode' is non-nil, and the cdr part is used when
  1466. `indent-tabs-mode' is nil.")
  1467. (defconst whitespace-report-buffer-name "*Whitespace Report*"
  1468. "The buffer name for whitespace bogus report.")
  1469. ;;;###autoload
  1470. (defun whitespace-report (&optional force report-if-bogus)
  1471. "Report some whitespace problems in buffer.
  1472. Return nil if there is no whitespace problem; otherwise, return
  1473. non-nil.
  1474. If FORCE is non-nil or \\[universal-argument] was pressed just
  1475. before calling `whitespace-report' interactively, it forces
  1476. `whitespace-style' to have:
  1477. empty
  1478. trailing
  1479. indentation
  1480. space-before-tab
  1481. space-after-tab
  1482. If REPORT-IF-BOGUS is non-nil, it reports only when there are any
  1483. whitespace problems in buffer.
  1484. Report if some of the following whitespace problems exist:
  1485. * If `indent-tabs-mode' is non-nil:
  1486. empty 1. empty lines at beginning of buffer.
  1487. empty 2. empty lines at end of buffer.
  1488. trailing 3. SPACEs or TABs at end of line.
  1489. indentation 4. 8 or more SPACEs at beginning of line.
  1490. space-before-tab 5. SPACEs before TAB.
  1491. space-after-tab 6. 8 or more SPACEs after TAB.
  1492. * If `indent-tabs-mode' is nil:
  1493. empty 1. empty lines at beginning of buffer.
  1494. empty 2. empty lines at end of buffer.
  1495. trailing 3. SPACEs or TABs at end of line.
  1496. indentation 4. TABS at beginning of line.
  1497. space-before-tab 5. SPACEs before TAB.
  1498. space-after-tab 6. 8 or more SPACEs after TAB.
  1499. See `whitespace-style' for documentation.
  1500. See also `whitespace-cleanup' and `whitespace-cleanup-region' for
  1501. cleaning up these problems."
  1502. (interactive (list current-prefix-arg))
  1503. (whitespace-report-region (point-min) (point-max)
  1504. force report-if-bogus))
  1505. ;;;###autoload
  1506. (defun whitespace-report-region (start end &optional force report-if-bogus)
  1507. "Report some whitespace problems in a region.
  1508. Return nil if there is no whitespace problem; otherwise, return
  1509. non-nil.
  1510. If FORCE is non-nil or \\[universal-argument] was pressed just
  1511. before calling `whitespace-report-region' interactively, it
  1512. forces `whitespace-style' to have:
  1513. empty
  1514. indentation
  1515. space-before-tab
  1516. trailing
  1517. space-after-tab
  1518. If REPORT-IF-BOGUS is non-nil, it reports only when there are any
  1519. whitespace problems in buffer.
  1520. Report if some of the following whitespace problems exist:
  1521. * If `indent-tabs-mode' is non-nil:
  1522. empty 1. empty lines at beginning of buffer.
  1523. empty 2. empty lines at end of buffer.
  1524. trailing 3. SPACEs or TABs at end of line.
  1525. indentation 4. 8 or more SPACEs at beginning of line.
  1526. space-before-tab 5. SPACEs before TAB.
  1527. space-after-tab 6. 8 or more SPACEs after TAB.
  1528. * If `indent-tabs-mode' is nil:
  1529. empty 1. empty lines at beginning of buffer.
  1530. empty 2. empty lines at end of buffer.
  1531. trailing 3. SPACEs or TABs at end of line.
  1532. indentation 4. TABS at beginning of line.
  1533. space-before-tab 5. SPACEs before TAB.
  1534. space-after-tab 6. 8 or more SPACEs after TAB.
  1535. See `whitespace-style' for documentation.
  1536. See also `whitespace-cleanup' and `whitespace-cleanup-region' for
  1537. cleaning up these problems."
  1538. (interactive "r")
  1539. (setq force (or current-prefix-arg force))
  1540. (save-excursion
  1541. (save-match-data ;FIXME: Why?
  1542. (let* ((has-bogus nil)
  1543. (rstart (min start end))
  1544. (rend (max start end))
  1545. (bogus-list
  1546. (mapcar
  1547. #'(lambda (option)
  1548. (when force
  1549. (add-to-list 'whitespace-style (car option)))
  1550. (goto-char rstart)
  1551. (let ((regexp
  1552. (cond
  1553. ((eq (car option) 'indentation)
  1554. (whitespace-indentation-regexp))
  1555. ((eq (car option) 'indentation::tab)
  1556. (whitespace-indentation-regexp 'tab))
  1557. ((eq (car option) 'indentation::space)
  1558. (whitespace-indentation-regexp 'space))
  1559. ((eq (car option) 'space-after-tab)
  1560. (whitespace-space-after-tab-regexp))
  1561. ((eq (car option) 'space-after-tab::tab)
  1562. (whitespace-space-after-tab-regexp 'tab))
  1563. ((eq (car option) 'space-after-tab::space)
  1564. (whitespace-space-after-tab-regexp 'space))
  1565. (t
  1566. (cdr option)))))
  1567. (and (re-search-forward regexp rend t)
  1568. (setq has-bogus t))))
  1569. whitespace-report-list)))
  1570. (when (if report-if-bogus has-bogus t)
  1571. (whitespace-kill-buffer whitespace-report-buffer-name)
  1572. ;; `whitespace-indent-tabs-mode' is local to current buffer
  1573. ;; `whitespace-tab-width' is local to current buffer
  1574. (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
  1575. (ws-tab-width whitespace-tab-width))
  1576. (with-current-buffer (get-buffer-create
  1577. whitespace-report-buffer-name)
  1578. (erase-buffer)
  1579. (insert (if ws-indent-tabs-mode
  1580. (car whitespace-report-text)
  1581. (cdr whitespace-report-text)))
  1582. (goto-char (point-min))
  1583. (forward-line 3)
  1584. (dolist (option whitespace-report-list)
  1585. (forward-line 1)
  1586. (whitespace-mark-x
  1587. 27 (memq (car option) whitespace-style))
  1588. (whitespace-mark-x 7 (car bogus-list))
  1589. (setq bogus-list (cdr bogus-list)))
  1590. (forward-line 1)
  1591. (whitespace-insert-value ws-indent-tabs-mode)
  1592. (whitespace-insert-value ws-tab-width)
  1593. (when has-bogus
  1594. (goto-char (point-max))
  1595. (insert " Type `M-x whitespace-cleanup'"
  1596. " to cleanup the buffer.\n\n"
  1597. " Type `M-x whitespace-cleanup-region'"
  1598. " to cleanup a region.\n\n"))
  1599. (whitespace-display-window (current-buffer)))))
  1600. has-bogus))))
  1601. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1602. ;;;; Internal functions
  1603. (defvar whitespace-font-lock-mode nil
  1604. "Used to remember whether a buffer had font lock mode on or not.")
  1605. (defvar whitespace-font-lock nil
  1606. "Used to remember whether a buffer initially had font lock on or not.")
  1607. (defvar whitespace-font-lock-keywords nil
  1608. "Used to save locally `font-lock-keywords' value.")
  1609. (defconst whitespace-help-text
  1610. "\
  1611. Whitespace Toggle Options | scroll up : SPC or > |
  1612. | scroll down: M-SPC or < |
  1613. FACES \\__________________________/
  1614. [] f - toggle face visualization
  1615. [] t - toggle TAB visualization
  1616. [] s - toggle SPACE and HARD SPACE visualization
  1617. [] r - toggle trailing blanks visualization
  1618. [] l - toggle \"long lines\" visualization
  1619. [] L - toggle \"long lines\" tail visualization
  1620. [] n - toggle NEWLINE visualization
  1621. [] e - toggle empty line at bob and/or eob visualization
  1622. [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
  1623. [] I - toggle indentation SPACEs visualization
  1624. [] i - toggle indentation TABs visualization
  1625. [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
  1626. [] A - toggle SPACEs after TAB: SPACEs visualization
  1627. [] a - toggle SPACEs after TAB: TABs visualization
  1628. [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
  1629. [] B - toggle SPACEs before TAB: SPACEs visualization
  1630. [] b - toggle SPACEs before TAB: TABs visualization
  1631. DISPLAY TABLE
  1632. [] T - toggle TAB visualization
  1633. [] S - toggle SPACE and HARD SPACE visualization
  1634. [] N - toggle NEWLINE visualization
  1635. x - restore `whitespace-style' value
  1636. ? - display this text\n\n"
  1637. "Text for whitespace toggle options.")
  1638. (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
  1639. "The buffer name for whitespace toggle options.")
  1640. (defun whitespace-insert-value (value)
  1641. "Insert VALUE at column 20 of next line."
  1642. (forward-line 1)
  1643. (move-to-column 20 t)
  1644. (insert (format "%s" value)))
  1645. (defun whitespace-mark-x (nchars condition)
  1646. "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
  1647. (forward-char nchars)
  1648. (insert (if condition "X" " ")))
  1649. (defun whitespace-insert-option-mark (the-list the-value)
  1650. "Insert the option mark ('X' or ' ') in toggle options buffer."
  1651. (goto-char (point-min))
  1652. (forward-line 2)
  1653. (dolist (sym the-list)
  1654. (if (eq sym 'help-newline)
  1655. (forward-line 2)
  1656. (forward-line 1)
  1657. (whitespace-mark-x 2 (memq sym the-value)))))
  1658. (defun whitespace-help-on (style)
  1659. "Display the whitespace toggle options."
  1660. (unless (get-buffer whitespace-help-buffer-name)
  1661. (delete-other-windows)
  1662. (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
  1663. (with-current-buffer buffer
  1664. (erase-buffer)
  1665. (insert whitespace-help-text)
  1666. (whitespace-insert-option-mark
  1667. whitespace-style-value-list style)
  1668. (whitespace-display-window buffer)))))
  1669. (defun whitespace-display-window (buffer)
  1670. "Display BUFFER in a new window."
  1671. (goto-char (point-min))
  1672. (set-buffer-modified-p nil)
  1673. (when (< (window-height) (* 2 window-min-height))
  1674. (kill-buffer buffer)
  1675. (error "Window height is too small; \
  1676. can't split window to display whitespace toggle options"))
  1677. (let ((win (split-window)))
  1678. (set-window-buffer win buffer)
  1679. (shrink-window-if-larger-than-buffer win)))
  1680. (defun whitespace-kill-buffer (buffer-name)
  1681. "Kill buffer BUFFER-NAME and windows related with it."
  1682. (let ((buffer (get-buffer buffer-name)))
  1683. (when buffer
  1684. (delete-windows-on buffer)
  1685. (kill-buffer buffer))))
  1686. (defun whitespace-help-off ()
  1687. "Remove the buffer and window of the whitespace toggle options."
  1688. (whitespace-kill-buffer whitespace-help-buffer-name))
  1689. (defun whitespace-help-scroll (&optional up)
  1690. "Scroll help window, if it exists.
  1691. If UP is non-nil, scroll up; otherwise, scroll down."
  1692. (condition-case nil
  1693. (let ((buffer (get-buffer whitespace-help-buffer-name)))
  1694. (if buffer
  1695. (with-selected-window (get-buffer-window buffer)
  1696. (if up
  1697. (scroll-up 3)
  1698. (scroll-down 3)))
  1699. (ding)))
  1700. ;; handler
  1701. ((error)
  1702. ;; just ignore error
  1703. )))
  1704. (defun whitespace-interactive-char (local-p)
  1705. "Interactive function to read a char and return a symbol.
  1706. If LOCAL-P is non-nil, it uses a local context; otherwise, it
  1707. uses a global context.
  1708. It accepts one of the following chars:
  1709. CHAR MEANING
  1710. (VIA FACES)
  1711. f toggle face visualization
  1712. t toggle TAB visualization
  1713. s toggle SPACE and HARD SPACE visualization
  1714. r toggle trailing blanks visualization
  1715. l toggle \"long lines\" visualization
  1716. L toggle \"long lines\" tail visualization
  1717. n toggle NEWLINE visualization
  1718. e toggle empty line at bob and/or eob visualization
  1719. C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
  1720. I toggle indentation SPACEs visualization
  1721. i toggle indentation TABs visualization
  1722. C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
  1723. A toggle SPACEs after TAB: SPACEs visualization
  1724. a toggle SPACEs after TAB: TABs visualization
  1725. C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
  1726. B toggle SPACEs before TAB: SPACEs visualization
  1727. b toggle SPACEs before TAB: TABs visualization
  1728. (VIA DISPLAY TABLE)
  1729. T toggle TAB visualization
  1730. S toggle SPACE and HARD SPACE visualization
  1731. N toggle NEWLINE visualization
  1732. x restore `whitespace-style' value
  1733. ? display brief help
  1734. See also `whitespace-toggle-option-alist'."
  1735. (let* ((is-off (not (if local-p
  1736. whitespace-mode
  1737. global-whitespace-mode)))
  1738. (style (cond (is-off whitespace-style) ; use default value
  1739. (local-p whitespace-active-style)
  1740. (t whitespace-toggle-style)))
  1741. (prompt
  1742. (format "Whitespace Toggle %s (type ? for further options)-"
  1743. (if local-p "Local" "Global")))
  1744. ch sym)
  1745. ;; read a valid option and get the corresponding symbol
  1746. (save-window-excursion
  1747. (condition-case data
  1748. (progn
  1749. (while
  1750. ;; while condition
  1751. (progn
  1752. (setq ch (read-char prompt))
  1753. (not
  1754. (setq sym
  1755. (cdr
  1756. (assq ch whitespace-toggle-option-alist)))))
  1757. ;; while body
  1758. (cond
  1759. ((eq ch ?\?) (whitespace-help-on style))
  1760. ((eq ch ?\ ) (whitespace-help-scroll t))
  1761. ((eq ch ?\M- ) (whitespace-help-scroll))
  1762. ((eq ch ?>) (whitespace-help-scroll t))
  1763. ((eq ch ?<) (whitespace-help-scroll))
  1764. (t (ding))))
  1765. (whitespace-help-off)
  1766. (message " ")) ; clean echo area
  1767. ;; handler
  1768. ((quit error)
  1769. (whitespace-help-off)
  1770. (error (error-message-string data)))))
  1771. (list sym))) ; return the appropriate symbol
  1772. (defun whitespace-toggle-list (local-p arg the-list)
  1773. "Toggle options in THE-LIST based on list ARG.
  1774. If LOCAL-P is non-nil, it uses a local context; otherwise, it
  1775. uses a global context.
  1776. ARG is a list of options to be toggled.
  1777. THE-LIST is a list of options. This list will be toggled and the
  1778. resultant list will be returned."
  1779. (unless (if local-p whitespace-mode global-whitespace-mode)
  1780. (setq the-list whitespace-style))
  1781. (setq the-list (copy-sequence the-list)) ; keep original list
  1782. (dolist (sym (if (listp arg) arg (list arg)))
  1783. (cond
  1784. ;; ignore help value
  1785. ((eq sym 'help-newline))
  1786. ;; restore default values
  1787. ((eq sym 'whitespace-style)
  1788. (setq the-list whitespace-style))
  1789. ;; toggle valid values
  1790. ((memq sym whitespace-style-value-list)
  1791. (setq the-list (if (memq sym the-list)
  1792. (delq sym the-list)
  1793. (cons sym the-list))))))
  1794. the-list)
  1795. (defvar whitespace-display-table nil
  1796. "Used to save a local display table.")
  1797. (defvar whitespace-display-table-was-local nil
  1798. "Used to remember whether a buffer initially had a local display table.")
  1799. (defun whitespace-turn-on ()
  1800. "Turn on whitespace visualization."
  1801. ;; prepare local hooks
  1802. (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
  1803. ;; create whitespace local buffer environment
  1804. (set (make-local-variable 'whitespace-font-lock-mode) nil)
  1805. (set (make-local-variable 'whitespace-font-lock) nil)
  1806. (set (make-local-variable 'whitespace-font-lock-keywords) nil)
  1807. (set (make-local-variable 'whitespace-display-table) nil)
  1808. (set (make-local-variable 'whitespace-display-table-was-local) nil)
  1809. (set (make-local-variable 'whitespace-active-style)
  1810. (if (listp whitespace-style)
  1811. whitespace-style
  1812. (list whitespace-style)))
  1813. (set (make-local-variable 'whitespace-indent-tabs-mode)
  1814. indent-tabs-mode)
  1815. (set (make-local-variable 'whitespace-tab-width)
  1816. tab-width)
  1817. ;; turn on whitespace
  1818. (when whitespace-active-style
  1819. (whitespace-color-on)
  1820. (whitespace-display-char-on)))
  1821. (defun whitespace-turn-off ()
  1822. "Turn off whitespace visualization."
  1823. (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
  1824. (when whitespace-active-style
  1825. (whitespace-color-off)
  1826. (whitespace-display-char-off)))
  1827. (defun whitespace-style-face-p ()
  1828. "Return t if there is some visualization via face."
  1829. (and (memq 'face whitespace-active-style)
  1830. (or (memq 'tabs whitespace-active-style)
  1831. (memq 'spaces whitespace-active-style)
  1832. (memq 'trailing whitespace-active-style)
  1833. (memq 'lines whitespace-active-style)
  1834. (memq 'lines-tail whitespace-active-style)
  1835. (memq 'newline whitespace-active-style)
  1836. (memq 'empty whitespace-active-style)
  1837. (memq 'indentation whitespace-active-style)
  1838. (memq 'indentation::tab whitespace-active-style)
  1839. (memq 'indentation::space whitespace-active-style)
  1840. (memq 'space-after-tab whitespace-active-style)
  1841. (memq 'space-after-tab::tab whitespace-active-style)
  1842. (memq 'space-after-tab::space whitespace-active-style)
  1843. (memq 'space-before-tab whitespace-active-style)
  1844. (memq 'space-before-tab::tab whitespace-active-style)
  1845. (memq 'space-before-tab::space whitespace-active-style))))
  1846. (defun whitespace-color-on ()
  1847. "Turn on color visualization."
  1848. (when (whitespace-style-face-p)
  1849. (unless whitespace-font-lock
  1850. (setq whitespace-font-lock t
  1851. whitespace-font-lock-keywords
  1852. (copy-sequence font-lock-keywords)))
  1853. ;; save current point and refontify when necessary
  1854. (set (make-local-variable 'whitespace-point)
  1855. (point))
  1856. (set (make-local-variable 'whitespace-font-lock-refontify)
  1857. 0)
  1858. (set (make-local-variable 'whitespace-bob-marker)
  1859. (point-min-marker))
  1860. (set (make-local-variable 'whitespace-eob-marker)
  1861. (point-max-marker))
  1862. (set (make-local-variable 'whitespace-buffer-changed)
  1863. nil)
  1864. (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
  1865. (add-hook 'before-change-functions #'whitespace-buffer-changed nil t)
  1866. ;; turn off font lock
  1867. (set (make-local-variable 'whitespace-font-lock-mode)
  1868. font-lock-mode)
  1869. (font-lock-mode 0)
  1870. ;; add whitespace-mode color into font lock
  1871. (when (memq 'spaces whitespace-active-style)
  1872. (font-lock-add-keywords
  1873. nil
  1874. (list
  1875. ;; Show SPACEs
  1876. (list whitespace-space-regexp 1 whitespace-space t)
  1877. ;; Show HARD SPACEs
  1878. (list whitespace-hspace-regexp 1 whitespace-hspace t))
  1879. t))
  1880. (when (memq 'tabs whitespace-active-style)
  1881. (font-lock-add-keywords
  1882. nil
  1883. (list
  1884. ;; Show TABs
  1885. (list whitespace-tab-regexp 1 whitespace-tab t))
  1886. t))
  1887. (when (memq 'trailing whitespace-active-style)
  1888. (font-lock-add-keywords
  1889. nil
  1890. (list
  1891. ;; Show trailing blanks
  1892. (list #'whitespace-trailing-regexp 1 whitespace-trailing t))
  1893. t))
  1894. (when (or (memq 'lines whitespace-active-style)
  1895. (memq 'lines-tail whitespace-active-style))
  1896. (font-lock-add-keywords
  1897. nil
  1898. (list
  1899. ;; Show "long" lines
  1900. (list
  1901. (let ((line-column (or whitespace-line-column fill-column)))
  1902. (format
  1903. "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
  1904. whitespace-tab-width
  1905. (1- whitespace-tab-width)
  1906. (/ line-column whitespace-tab-width)
  1907. (let ((rem (% line-column whitespace-tab-width)))
  1908. (if (zerop rem)
  1909. ""
  1910. (format ".\\{%d\\}" rem)))))
  1911. (if (memq 'lines whitespace-active-style)
  1912. 0 ; whole line
  1913. 2) ; line tail
  1914. whitespace-line t))
  1915. t))
  1916. (cond
  1917. ((memq 'space-before-tab whitespace-active-style)
  1918. (font-lock-add-keywords
  1919. nil
  1920. (list
  1921. ;; Show SPACEs before TAB (indent-tabs-mode)
  1922. (list whitespace-space-before-tab-regexp
  1923. (if whitespace-indent-tabs-mode 1 2)
  1924. whitespace-space-before-tab t))
  1925. t))
  1926. ((memq 'space-before-tab::tab whitespace-active-style)
  1927. (font-lock-add-keywords
  1928. nil
  1929. (list
  1930. ;; Show SPACEs before TAB (SPACEs)
  1931. (list whitespace-space-before-tab-regexp
  1932. 1 whitespace-space-before-tab t))
  1933. t))
  1934. ((memq 'space-before-tab::space whitespace-active-style)
  1935. (font-lock-add-keywords
  1936. nil
  1937. (list
  1938. ;; Show SPACEs before TAB (TABs)
  1939. (list whitespace-space-before-tab-regexp
  1940. 2 whitespace-space-before-tab t))
  1941. t)))
  1942. (cond
  1943. ((memq 'indentation whitespace-active-style)
  1944. (font-lock-add-keywords
  1945. nil
  1946. (list
  1947. ;; Show indentation SPACEs (indent-tabs-mode)
  1948. (list (whitespace-indentation-regexp)
  1949. 1 whitespace-indentation t))
  1950. t))
  1951. ((memq 'indentation::tab whitespace-active-style)
  1952. (font-lock-add-keywords
  1953. nil
  1954. (list
  1955. ;; Show indentation SPACEs (SPACEs)
  1956. (list (whitespace-indentation-regexp 'tab)
  1957. 1 whitespace-indentation t))
  1958. t))
  1959. ((memq 'indentation::space whitespace-active-style)
  1960. (font-lock-add-keywords
  1961. nil
  1962. (list
  1963. ;; Show indentation SPACEs (TABs)
  1964. (list (whitespace-indentation-regexp 'space)
  1965. 1 whitespace-indentation t))
  1966. t)))
  1967. (when (memq 'empty whitespace-active-style)
  1968. (font-lock-add-keywords
  1969. nil
  1970. (list
  1971. ;; Show empty lines at beginning of buffer
  1972. (list #'whitespace-empty-at-bob-regexp
  1973. 1 whitespace-empty t))
  1974. t)
  1975. (font-lock-add-keywords
  1976. nil
  1977. (list
  1978. ;; Show empty lines at end of buffer
  1979. (list #'whitespace-empty-at-eob-regexp
  1980. 1 whitespace-empty t))
  1981. t))
  1982. (cond
  1983. ((memq 'space-after-tab whitespace-active-style)
  1984. (font-lock-add-keywords
  1985. nil
  1986. (list
  1987. ;; Show SPACEs after TAB (indent-tabs-mode)
  1988. (list (whitespace-space-after-tab-regexp)
  1989. 1 whitespace-space-after-tab t))
  1990. t))
  1991. ((memq 'space-after-tab::tab whitespace-active-style)
  1992. (font-lock-add-keywords
  1993. nil
  1994. (list
  1995. ;; Show SPACEs after TAB (SPACEs)
  1996. (list (whitespace-space-after-tab-regexp 'tab)
  1997. 1 whitespace-space-after-tab t))
  1998. t))
  1999. ((memq 'space-after-tab::space whitespace-active-style)
  2000. (font-lock-add-keywords
  2001. nil
  2002. (list
  2003. ;; Show SPACEs after TAB (TABs)
  2004. (list (whitespace-space-after-tab-regexp 'space)
  2005. 1 whitespace-space-after-tab t))
  2006. t)))
  2007. ;; now turn on font lock and highlight blanks
  2008. (font-lock-mode 1)))
  2009. (defun whitespace-color-off ()
  2010. "Turn off color visualization."
  2011. ;; turn off font lock
  2012. (when (whitespace-style-face-p)
  2013. (font-lock-mode 0)
  2014. (remove-hook 'post-command-hook #'whitespace-post-command-hook t)
  2015. (remove-hook 'before-change-functions #'whitespace-buffer-changed t)
  2016. (when whitespace-font-lock
  2017. (setq whitespace-font-lock nil
  2018. font-lock-keywords whitespace-font-lock-keywords))
  2019. ;; restore original font lock state
  2020. (font-lock-mode whitespace-font-lock-mode)))
  2021. (defun whitespace-trailing-regexp (limit)
  2022. "Match trailing spaces which do not contain the point at end of line."
  2023. (let ((status t))
  2024. (while (if (re-search-forward whitespace-trailing-regexp limit t)
  2025. (= whitespace-point (match-end 1)) ;; loop if point at eol
  2026. (setq status nil))) ;; end of buffer
  2027. status))
  2028. (defun whitespace-empty-at-bob-regexp (limit)
  2029. "Match spaces at beginning of buffer which do not contain the point at \
  2030. beginning of buffer."
  2031. (let ((b (point))
  2032. r)
  2033. (cond
  2034. ;; at bob
  2035. ((= b 1)
  2036. (setq r (and (/= whitespace-point 1)
  2037. (looking-at whitespace-empty-at-bob-regexp)))
  2038. (set-marker whitespace-bob-marker (if r (match-end 1) b)))
  2039. ;; inside bob empty region
  2040. ((<= limit whitespace-bob-marker)
  2041. (setq r (looking-at whitespace-empty-at-bob-regexp))
  2042. (if r
  2043. (when (< (match-end 1) limit)
  2044. (set-marker whitespace-bob-marker (match-end 1)))
  2045. (set-marker whitespace-bob-marker b)))
  2046. ;; intersection with end of bob empty region
  2047. ((<= b whitespace-bob-marker)
  2048. (setq r (looking-at whitespace-empty-at-bob-regexp))
  2049. (set-marker whitespace-bob-marker (if r (match-end 1) b)))
  2050. ;; it is not inside bob empty region
  2051. (t
  2052. (setq r nil)))
  2053. ;; move to end of matching
  2054. (and r (goto-char (match-end 1)))
  2055. r))
  2056. (defsubst whitespace-looking-back (regexp limit)
  2057. (save-excursion
  2058. (when (/= 0 (skip-chars-backward " \t\n" limit))
  2059. (unless (bolp)
  2060. (forward-line 1))
  2061. (looking-at regexp))))
  2062. (defun whitespace-empty-at-eob-regexp (limit)
  2063. "Match spaces at end of buffer which do not contain the point at end of \
  2064. buffer."
  2065. (let ((b (point))
  2066. (e (1+ (buffer-size)))
  2067. r)
  2068. (cond
  2069. ;; at eob
  2070. ((= limit e)
  2071. (when (/= whitespace-point e)
  2072. (goto-char limit)
  2073. (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)))
  2074. (if r
  2075. (set-marker whitespace-eob-marker (match-beginning 1))
  2076. (set-marker whitespace-eob-marker limit)
  2077. (goto-char b))) ; return back to initial position
  2078. ;; inside eob empty region
  2079. ((>= b whitespace-eob-marker)
  2080. (goto-char limit)
  2081. (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
  2082. (if r
  2083. (when (> (match-beginning 1) b)
  2084. (set-marker whitespace-eob-marker (match-beginning 1)))
  2085. (set-marker whitespace-eob-marker limit)
  2086. (goto-char b))) ; return back to initial position
  2087. ;; intersection with beginning of eob empty region
  2088. ((>= limit whitespace-eob-marker)
  2089. (goto-char limit)
  2090. (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
  2091. (if r
  2092. (set-marker whitespace-eob-marker (match-beginning 1))
  2093. (set-marker whitespace-eob-marker limit)
  2094. (goto-char b))) ; return back to initial position
  2095. ;; it is not inside eob empty region
  2096. (t
  2097. (setq r nil)))
  2098. r))
  2099. (defun whitespace-buffer-changed (_beg _end)
  2100. "Set `whitespace-buffer-changed' variable to t."
  2101. (setq whitespace-buffer-changed t))
  2102. (defun whitespace-post-command-hook ()
  2103. "Save current point into `whitespace-point' variable.
  2104. Also refontify when necessary."
  2105. (setq whitespace-point (point)) ; current point position
  2106. (let ((refontify
  2107. (or
  2108. ;; it is at end of line ...
  2109. (and (eolp)
  2110. ;; ... with trailing SPACE or TAB
  2111. (or (= (preceding-char) ?\ )
  2112. (= (preceding-char) ?\t)))
  2113. ;; it is at beginning of buffer (bob)
  2114. (= whitespace-point 1)
  2115. ;; the buffer was modified and ...
  2116. (and whitespace-buffer-changed
  2117. (or
  2118. ;; ... or inside bob whitespace region
  2119. (<= whitespace-point whitespace-bob-marker)
  2120. ;; ... or at bob whitespace region border
  2121. (and (= whitespace-point (1+ whitespace-bob-marker))
  2122. (= (preceding-char) ?\n))))
  2123. ;; it is at end of buffer (eob)
  2124. (= whitespace-point (1+ (buffer-size)))
  2125. ;; the buffer was modified and ...
  2126. (and whitespace-buffer-changed
  2127. (or
  2128. ;; ... or inside eob whitespace region
  2129. (>= whitespace-point whitespace-eob-marker)
  2130. ;; ... or at eob whitespace region border
  2131. (and (= whitespace-point (1- whitespace-eob-marker))
  2132. (= (following-char) ?\n)))))))
  2133. (when (or refontify (> whitespace-font-lock-refontify 0))
  2134. (setq whitespace-buffer-changed nil)
  2135. ;; adjust refontify counter
  2136. (setq whitespace-font-lock-refontify
  2137. (if refontify
  2138. 1
  2139. (1- whitespace-font-lock-refontify)))
  2140. ;; refontify
  2141. (jit-lock-refontify))))
  2142. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2143. ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
  2144. (defun whitespace-style-mark-p ()
  2145. "Return t if there is some visualization via display table."
  2146. (or (memq 'tab-mark whitespace-active-style)
  2147. (memq 'space-mark whitespace-active-style)
  2148. (memq 'newline-mark whitespace-active-style)))
  2149. (defsubst whitespace-char-valid-p (char)
  2150. ;; This check should be improved!!!
  2151. (or (< char 256)
  2152. (characterp char)))
  2153. (defun whitespace-display-vector-p (vec)
  2154. "Return true if every character in vector VEC can be displayed."
  2155. (let ((i (length vec)))
  2156. (when (> i 0)
  2157. (while (and (>= (setq i (1- i)) 0)
  2158. (whitespace-char-valid-p (aref vec i))))
  2159. (< i 0))))
  2160. (defun whitespace-display-char-on ()
  2161. "Turn on character display mapping."
  2162. (when (and whitespace-display-mappings
  2163. (whitespace-style-mark-p))
  2164. (let (vecs vec)
  2165. ;; Remember whether a buffer has a local display table.
  2166. (unless whitespace-display-table-was-local
  2167. (setq whitespace-display-table-was-local t
  2168. whitespace-display-table
  2169. (copy-sequence buffer-display-table))
  2170. ;; Assure `buffer-display-table' is unique
  2171. ;; when two or more windows are visible.
  2172. (setq buffer-display-table
  2173. (copy-sequence buffer-display-table)))
  2174. (unless buffer-display-table
  2175. (setq buffer-display-table (make-display-table)))
  2176. (dolist (entry whitespace-display-mappings)
  2177. ;; check if it is to display this mark
  2178. (when (memq (car entry) whitespace-style)
  2179. ;; Get a displayable mapping.
  2180. (setq vecs (cddr entry))
  2181. (while (and vecs
  2182. (not (whitespace-display-vector-p (car vecs))))
  2183. (setq vecs (cdr vecs)))
  2184. ;; Display a valid mapping.
  2185. (when vecs
  2186. (setq vec (copy-sequence (car vecs)))
  2187. ;; NEWLINE char
  2188. (when (and (eq (cadr entry) ?\n)
  2189. (memq 'newline whitespace-active-style))
  2190. ;; Only insert face bits on NEWLINE char mapping to avoid
  2191. ;; obstruction of other faces like TABs and (HARD) SPACEs
  2192. ;; faces, font-lock faces, etc.
  2193. (dotimes (i (length vec))
  2194. (or (eq (aref vec i) ?\n)
  2195. (aset vec i
  2196. (make-glyph-code (aref vec i)
  2197. whitespace-newline)))))
  2198. ;; Display mapping
  2199. (aset buffer-display-table (cadr entry) vec)))))))
  2200. (defun whitespace-display-char-off ()
  2201. "Turn off character display mapping."
  2202. (and whitespace-display-mappings
  2203. (whitespace-style-mark-p)
  2204. whitespace-display-table-was-local
  2205. (setq whitespace-display-table-was-local nil
  2206. buffer-display-table whitespace-display-table)))
  2207. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2208. ;;;; Hook
  2209. (defun whitespace-action-when-on ()
  2210. "Action to be taken always when local whitespace is turned on."
  2211. (cond ((memq 'cleanup whitespace-action)
  2212. (whitespace-cleanup))
  2213. ((memq 'report-on-bogus whitespace-action)
  2214. (whitespace-report nil t))))
  2215. (defun whitespace-write-file-hook ()
  2216. "Action to be taken when buffer is written.
  2217. It should be added buffer-locally to `write-file-functions'."
  2218. (cond ((memq 'auto-cleanup whitespace-action)
  2219. (whitespace-cleanup))
  2220. ((memq 'abort-on-bogus whitespace-action)
  2221. (when (whitespace-report nil t)
  2222. (error "Abort write due to whitespace problems in %s"
  2223. (buffer-name)))))
  2224. nil) ; continue hook processing
  2225. (defun whitespace-warn-read-only (msg)
  2226. "Warn if buffer is read-only."
  2227. (when (memq 'warn-if-read-only whitespace-action)
  2228. (message "Can't %s: %s is read-only" msg (buffer-name))))
  2229. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2230. (defun whitespace-unload-function ()
  2231. "Unload the whitespace library."
  2232. (global-whitespace-mode -1)
  2233. ;; be sure all local whitespace mode is turned off
  2234. (save-current-buffer
  2235. (dolist (buf (buffer-list))
  2236. (set-buffer buf)
  2237. (whitespace-mode -1)))
  2238. nil) ; continue standard unloading
  2239. (provide 'whitespace)
  2240. (run-hooks 'whitespace-load-hook)
  2241. ;;; whitespace.el ends here