whitespace.el 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670
  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 mode line).
  161. ;;
  162. ;; `global-whitespace-newline-mode'
  163. ;; Toggle NEWLINE global minor mode visualization ("NL" on mode line).
  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. '((default :weight normal)
  557. (((class color) (background dark)) :foreground "darkgray")
  558. (((class color) (min-colors 88) (background light)) :foreground "lightgray")
  559. ;; Displays with 16 colors use lightgray as background, so using a
  560. ;; lightgray foreground makes the newline mark invisible.
  561. (((class color) (background light)) :foreground "brown")
  562. (t :underline t))
  563. "Face used to visualize NEWLINE char mapping.
  564. See `whitespace-display-mappings'."
  565. :group 'whitespace)
  566. (defcustom whitespace-trailing 'whitespace-trailing
  567. "Symbol face used to visualize trailing blanks.
  568. Used when `whitespace-style' includes the value `trailing'."
  569. :type 'face
  570. :group 'whitespace)
  571. (defface whitespace-trailing ; 'trailing-whitespace
  572. '((default :weight bold)
  573. (((class mono)) :inverse-video t :underline t)
  574. (t :background "red1" :foreground "yellow"))
  575. "Face used to visualize trailing blanks."
  576. :group 'whitespace)
  577. (defcustom whitespace-line 'whitespace-line
  578. "Symbol face used to visualize \"long\" lines.
  579. See `whitespace-line-column'.
  580. Used when `whitespace-style' includes the value `line'."
  581. :type 'face
  582. :group 'whitespace)
  583. (defface whitespace-line
  584. '((((class mono)) :inverse-video t :weight bold :underline t)
  585. (t :background "gray20" :foreground "violet"))
  586. "Face used to visualize \"long\" lines.
  587. See `whitespace-line-column'."
  588. :group 'whitespace)
  589. (defcustom whitespace-space-before-tab 'whitespace-space-before-tab
  590. "Symbol face used to visualize SPACEs before TAB.
  591. Used when `whitespace-style' includes the value `space-before-tab'."
  592. :type 'face
  593. :group 'whitespace)
  594. (defface whitespace-space-before-tab
  595. '((((class mono)) :inverse-video t :weight bold :underline t)
  596. (t :background "DarkOrange" :foreground "firebrick"))
  597. "Face used to visualize SPACEs before TAB."
  598. :group 'whitespace)
  599. (defcustom whitespace-indentation 'whitespace-indentation
  600. "Symbol face used to visualize 8 or more SPACEs at beginning of line.
  601. Used when `whitespace-style' includes the value `indentation'."
  602. :type 'face
  603. :group 'whitespace)
  604. (defface whitespace-indentation
  605. '((((class mono)) :inverse-video t :weight bold :underline t)
  606. (t :background "yellow" :foreground "firebrick"))
  607. "Face used to visualize 8 or more SPACEs at beginning of line."
  608. :group 'whitespace)
  609. (defcustom whitespace-empty 'whitespace-empty
  610. "Symbol face used to visualize empty lines at beginning and/or end of buffer.
  611. Used when `whitespace-style' includes the value `empty'."
  612. :type 'face
  613. :group 'whitespace)
  614. (defface whitespace-empty
  615. '((((class mono)) :inverse-video t :weight bold :underline t)
  616. (t :background "yellow" :foreground "firebrick"))
  617. "Face used to visualize empty lines at beginning and/or end of buffer."
  618. :group 'whitespace)
  619. (defcustom whitespace-space-after-tab 'whitespace-space-after-tab
  620. "Symbol face used to visualize 8 or more SPACEs after TAB.
  621. Used when `whitespace-style' includes the value `space-after-tab'."
  622. :type 'face
  623. :group 'whitespace)
  624. (defface whitespace-space-after-tab
  625. '((((class mono)) :inverse-video t :weight bold :underline t)
  626. (t :background "yellow" :foreground "firebrick"))
  627. "Face used to visualize 8 or more SPACEs after TAB."
  628. :group 'whitespace)
  629. (defcustom whitespace-hspace-regexp
  630. "\\(\\(\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)"
  631. "Specify HARD SPACE characters regexp.
  632. If you're using `mule' package, there may be other characters besides:
  633. \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \"\\xF20\"
  634. that should be considered HARD SPACE.
  635. Here are some examples:
  636. \"\\\\(^\\xA0+\\\\)\" \
  637. visualize only leading HARD SPACEs.
  638. \"\\\\(\\xA0+$\\\\)\" \
  639. visualize only trailing HARD SPACEs.
  640. \"\\\\(^\\xA0+\\\\|\\xA0+$\\\\)\" \
  641. visualize leading and/or trailing HARD SPACEs.
  642. \"\\t\\\\(\\xA0+\\\\)\\t\" \
  643. visualize only HARD SPACEs between TABs.
  644. NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
  645. Use exactly one pair of enclosing \\\\( and \\\\).
  646. Used when `whitespace-style' includes `spaces'."
  647. :type '(regexp :tag "HARD SPACE Chars")
  648. :group 'whitespace)
  649. (defcustom whitespace-space-regexp "\\( +\\)"
  650. "Specify SPACE characters regexp.
  651. If you're using `mule' package, there may be other characters
  652. besides \" \" that should be considered SPACE.
  653. Here are some examples:
  654. \"\\\\(^ +\\\\)\" visualize only leading SPACEs.
  655. \"\\\\( +$\\\\)\" visualize only trailing SPACEs.
  656. \"\\\\(^ +\\\\| +$\\\\)\" \
  657. visualize leading and/or trailing SPACEs.
  658. \"\\t\\\\( +\\\\)\\t\" visualize only SPACEs between TABs.
  659. NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
  660. Use exactly one pair of enclosing \\\\( and \\\\).
  661. Used when `whitespace-style' includes `spaces'."
  662. :type '(regexp :tag "SPACE Chars")
  663. :group 'whitespace)
  664. (defcustom whitespace-tab-regexp "\\(\t+\\)"
  665. "Specify TAB characters regexp.
  666. If you're using `mule' package, there may be other characters
  667. besides \"\\t\" that should be considered TAB.
  668. Here are some examples:
  669. \"\\\\(^\\t+\\\\)\" visualize only leading TABs.
  670. \"\\\\(\\t+$\\\\)\" visualize only trailing TABs.
  671. \"\\\\(^\\t+\\\\|\\t+$\\\\)\" \
  672. visualize leading and/or trailing TABs.
  673. \" \\\\(\\t+\\\\) \" visualize only TABs between SPACEs.
  674. NOTE: Enclose always by \\\\( and \\\\) the elements to highlight.
  675. Use exactly one pair of enclosing \\\\( and \\\\).
  676. Used when `whitespace-style' includes `tabs'."
  677. :type '(regexp :tag "TAB Chars")
  678. :group 'whitespace)
  679. (defcustom whitespace-trailing-regexp
  680. "\\([\t \u00A0]+\\)$"
  681. "Specify trailing characters regexp.
  682. If you're using `mule' package, there may be other characters besides:
  683. \" \" \"\\t\" \"\\u00A0\"
  684. that should be considered blank.
  685. NOTE: Enclose always by \"\\\\(\" and \"\\\\)$\" the elements to highlight.
  686. Use exactly one pair of enclosing elements above.
  687. Used when `whitespace-style' includes `trailing'."
  688. :type '(regexp :tag "Trailing Chars")
  689. :group 'whitespace)
  690. (defcustom whitespace-space-before-tab-regexp "\\( +\\)\\(\t+\\)"
  691. "Specify SPACEs before TAB regexp.
  692. If you're using `mule' package, there may be other characters besides:
  693. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  694. \"\\xF20\"
  695. that should be considered blank.
  696. Used when `whitespace-style' includes `space-before-tab',
  697. `space-before-tab::tab' or `space-before-tab::space'."
  698. :type '(regexp :tag "SPACEs Before TAB")
  699. :group 'whitespace)
  700. (defcustom whitespace-indentation-regexp
  701. '("^\t*\\(\\( \\{%d\\}\\)+\\)[^\n\t]"
  702. . "^ *\\(\t+\\)[^\n]")
  703. "Specify regexp for 8 or more SPACEs at beginning of line.
  704. It is a cons where the cons car is used for SPACEs visualization
  705. and the cons cdr is used for TABs visualization.
  706. If you're using `mule' package, there may be other characters besides:
  707. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  708. \"\\xF20\"
  709. that should be considered blank.
  710. Used when `whitespace-style' includes `indentation',
  711. `indentation::tab' or `indentation::space'."
  712. :type '(cons (regexp :tag "Indentation SPACEs")
  713. (regexp :tag "Indentation TABs"))
  714. :group 'whitespace)
  715. (defcustom whitespace-empty-at-bob-regexp "^\\(\\([ \t]*\n\\)+\\)"
  716. "Specify regexp for empty lines at beginning of buffer.
  717. If you're using `mule' package, there may be other characters besides:
  718. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  719. \"\\xF20\"
  720. that should be considered blank.
  721. Used when `whitespace-style' includes `empty'."
  722. :type '(regexp :tag "Empty Lines At Beginning Of Buffer")
  723. :group 'whitespace)
  724. (defcustom whitespace-empty-at-eob-regexp "^\\([ \t\n]+\\)"
  725. "Specify regexp for empty lines at end of buffer.
  726. If you're using `mule' package, there may be other characters besides:
  727. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  728. \"\\xF20\"
  729. that should be considered blank.
  730. Used when `whitespace-style' includes `empty'."
  731. :type '(regexp :tag "Empty Lines At End Of Buffer")
  732. :group 'whitespace)
  733. (defcustom whitespace-space-after-tab-regexp
  734. '("\t+\\(\\( \\{%d\\}\\)+\\)"
  735. . "\\(\t+\\) +")
  736. "Specify regexp for 8 or more SPACEs after TAB.
  737. It is a cons where the cons car is used for SPACEs visualization
  738. and the cons cdr is used for TABs visualization.
  739. If you're using `mule' package, there may be other characters besides:
  740. \" \" \"\\t\" \"\\xA0\" \"\\x8A0\" \"\\x920\" \"\\xE20\" \
  741. \"\\xF20\"
  742. that should be considered blank.
  743. Used when `whitespace-style' includes `space-after-tab',
  744. `space-after-tab::tab' or `space-after-tab::space'."
  745. :type '(regexp :tag "SPACEs After TAB")
  746. :group 'whitespace)
  747. (defcustom whitespace-line-column 80
  748. "Specify column beyond which the line is highlighted.
  749. It must be an integer or nil. If nil, the `fill-column' variable value is
  750. used.
  751. Used when `whitespace-style' includes `lines' or `lines-tail'."
  752. :type '(choice :tag "Line Length Limit"
  753. (integer :tag "Line Length")
  754. (const :tag "Use fill-column" nil))
  755. :group 'whitespace)
  756. ;; Hacked from `visible-whitespace-mappings' in visws.el
  757. (defcustom whitespace-display-mappings
  758. '(
  759. (space-mark ?\ [?\u00B7] [?.]) ; space - centered dot
  760. (space-mark ?\xA0 [?\u00A4] [?_]) ; hard space - currency
  761. (space-mark ?\x8A0 [?\x8A4] [?_]) ; hard space - currency
  762. (space-mark ?\x920 [?\x924] [?_]) ; hard space - currency
  763. (space-mark ?\xE20 [?\xE24] [?_]) ; hard space - currency
  764. (space-mark ?\xF20 [?\xF24] [?_]) ; hard space - currency
  765. ;; NEWLINE is displayed using the face `whitespace-newline'
  766. (newline-mark ?\n [?$ ?\n]) ; eol - dollar sign
  767. ;; (newline-mark ?\n [?\u21B5 ?\n] [?$ ?\n]) ; eol - downwards arrow
  768. ;; (newline-mark ?\n [?\u00B6 ?\n] [?$ ?\n]) ; eol - pilcrow
  769. ;; (newline-mark ?\n [?\x8AF ?\n] [?$ ?\n]) ; eol - overscore
  770. ;; (newline-mark ?\n [?\x8AC ?\n] [?$ ?\n]) ; eol - negation
  771. ;; (newline-mark ?\n [?\x8B0 ?\n] [?$ ?\n]) ; eol - grade
  772. ;;
  773. ;; WARNING: the mapping below has a problem.
  774. ;; When a TAB occupies exactly one column, it will display the
  775. ;; character ?\xBB at that column followed by a TAB which goes to
  776. ;; the next TAB column.
  777. ;; If this is a problem for you, please, comment the line below.
  778. (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; tab - left quote mark
  779. )
  780. "Specify an alist of mappings for displaying characters.
  781. Each element has the following form:
  782. (KIND CHAR VECTOR...)
  783. Where:
  784. KIND is the kind of character.
  785. It can be one of the following symbols:
  786. tab-mark for TAB character
  787. space-mark for SPACE or HARD SPACE character
  788. newline-mark for NEWLINE character
  789. CHAR is the character to be mapped.
  790. VECTOR is a vector of characters to be displayed in place of CHAR.
  791. The first display vector that can be displayed is used;
  792. if no display vector for a mapping can be displayed, then
  793. that character is displayed unmodified.
  794. The NEWLINE character is displayed using the face given by
  795. `whitespace-newline' variable.
  796. Used when `whitespace-style' includes `tab-mark', `space-mark' or
  797. `newline-mark'."
  798. :type '(repeat
  799. (list :tag "Character Mapping"
  800. (choice :tag "Char Kind"
  801. (const :tag "Tab" tab-mark)
  802. (const :tag "Space" space-mark)
  803. (const :tag "Newline" newline-mark))
  804. (character :tag "Char")
  805. (repeat :inline t :tag "Vector List"
  806. (vector :tag ""
  807. (repeat :inline t
  808. :tag "Vector Characters"
  809. (character :tag "Char"))))))
  810. :group 'whitespace)
  811. (defcustom whitespace-global-modes t
  812. "Modes for which global `whitespace-mode' is automagically turned on.
  813. Global `whitespace-mode' is controlled by the command
  814. `global-whitespace-mode'.
  815. If nil, means no modes have `whitespace-mode' automatically
  816. turned on.
  817. If t, all modes that support `whitespace-mode' have it
  818. automatically turned on.
  819. Else it should be a list of `major-mode' symbol names for which
  820. `whitespace-mode' should be automatically turned on. The sense
  821. of the list is negated if it begins with `not'. For example:
  822. (c-mode c++-mode)
  823. means that `whitespace-mode' is turned on for buffers in C and
  824. C++ modes only."
  825. :type '(choice :tag "Global Modes"
  826. (const :tag "None" nil)
  827. (const :tag "All" t)
  828. (set :menu-tag "Mode Specific" :tag "Modes"
  829. :value (not)
  830. (const :tag "Except" not)
  831. (repeat :inline t
  832. (symbol :tag "Mode"))))
  833. :group 'whitespace)
  834. (defcustom whitespace-action nil
  835. "Specify which action is taken when a buffer is visited or written.
  836. It's a list containing some or all of the following values:
  837. nil no action is taken.
  838. cleanup cleanup any bogus whitespace always when local
  839. whitespace is turned on.
  840. See `whitespace-cleanup' and
  841. `whitespace-cleanup-region'.
  842. report-on-bogus report if there is any bogus whitespace always
  843. when local whitespace is turned on.
  844. auto-cleanup cleanup any bogus whitespace when buffer is
  845. written.
  846. See `whitespace-cleanup' and
  847. `whitespace-cleanup-region'.
  848. abort-on-bogus abort if there is any bogus whitespace and the
  849. buffer is written.
  850. warn-if-read-only give a warning if `cleanup' or `auto-cleanup'
  851. is included in `whitespace-action' and the
  852. buffer is read-only.
  853. Any other value is treated as nil."
  854. :type '(choice :tag "Actions"
  855. (const :tag "None" nil)
  856. (repeat :tag "Action List"
  857. (choice :tag "Action"
  858. (const :tag "Cleanup When On" cleanup)
  859. (const :tag "Report On Bogus" report-on-bogus)
  860. (const :tag "Auto Cleanup" auto-cleanup)
  861. (const :tag "Abort On Bogus" abort-on-bogus)
  862. (const :tag "Warn If Read-Only" warn-if-read-only))))
  863. :group 'whitespace)
  864. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  865. ;;;; User commands - Local mode
  866. ;;;###autoload
  867. (define-minor-mode whitespace-mode
  868. "Toggle whitespace visualization (Whitespace mode).
  869. With a prefix argument ARG, enable Whitespace mode if ARG is
  870. positive, and disable it otherwise. If called from Lisp, enable
  871. the mode if ARG is omitted or nil.
  872. See also `whitespace-style', `whitespace-newline' and
  873. `whitespace-display-mappings'."
  874. :lighter " ws"
  875. :init-value nil
  876. :global nil
  877. :group 'whitespace
  878. (cond
  879. (noninteractive ; running a batch job
  880. (setq whitespace-mode nil))
  881. (whitespace-mode ; whitespace-mode on
  882. (whitespace-turn-on)
  883. (whitespace-action-when-on))
  884. (t ; whitespace-mode off
  885. (whitespace-turn-off))))
  886. ;;;###autoload
  887. (define-minor-mode whitespace-newline-mode
  888. "Toggle newline visualization (Whitespace Newline mode).
  889. With a prefix argument ARG, enable Whitespace Newline mode if ARG
  890. is positive, and disable it otherwise. If called from Lisp,
  891. enable the mode if ARG is omitted or nil.
  892. Use `whitespace-newline-mode' only for NEWLINE visualization
  893. exclusively. For other visualizations, including NEWLINE
  894. visualization together with (HARD) SPACEs and/or TABs, please,
  895. use `whitespace-mode'.
  896. See also `whitespace-newline' and `whitespace-display-mappings'."
  897. :lighter " nl"
  898. :init-value nil
  899. :global nil
  900. :group 'whitespace
  901. (let ((whitespace-style '(face newline-mark newline)))
  902. (whitespace-mode (if whitespace-newline-mode
  903. 1 -1)))
  904. ;; sync states (running a batch job)
  905. (setq whitespace-newline-mode whitespace-mode))
  906. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  907. ;;;; User commands - Global mode
  908. ;;;###autoload
  909. (define-minor-mode global-whitespace-mode
  910. "Toggle whitespace visualization globally (Global Whitespace mode).
  911. With a prefix argument ARG, enable Global Whitespace mode if ARG
  912. is positive, and disable it otherwise. If called from Lisp,
  913. enable it if ARG is omitted or nil.
  914. See also `whitespace-style', `whitespace-newline' and
  915. `whitespace-display-mappings'."
  916. :lighter " WS"
  917. :init-value nil
  918. :global t
  919. :group 'whitespace
  920. (cond
  921. (noninteractive ; running a batch job
  922. (setq global-whitespace-mode nil))
  923. (global-whitespace-mode ; global-whitespace-mode on
  924. (save-current-buffer
  925. (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
  926. (add-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
  927. (dolist (buffer (buffer-list)) ; adjust all local mode
  928. (set-buffer buffer)
  929. (unless whitespace-mode
  930. (whitespace-turn-on-if-enabled)))))
  931. (t ; global-whitespace-mode off
  932. (save-current-buffer
  933. (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled)
  934. (remove-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled)
  935. (dolist (buffer (buffer-list)) ; adjust all local mode
  936. (set-buffer buffer)
  937. (unless whitespace-mode
  938. (whitespace-turn-off)))))))
  939. (defun whitespace-turn-on-if-enabled ()
  940. (when (cond
  941. ((eq whitespace-global-modes t))
  942. ((listp whitespace-global-modes)
  943. (if (eq (car-safe whitespace-global-modes) 'not)
  944. (not (memq major-mode (cdr whitespace-global-modes)))
  945. (memq major-mode whitespace-global-modes)))
  946. (t nil))
  947. (let (inhibit-quit)
  948. ;; Don't turn on whitespace mode if...
  949. (or
  950. ;; ...we don't have a display (we're running a batch job)
  951. noninteractive
  952. ;; ...or if the buffer is invisible (name starts with a space)
  953. (eq (aref (buffer-name) 0) ?\ )
  954. ;; ...or if the buffer is temporary (name starts with *)
  955. (and (eq (aref (buffer-name) 0) ?*)
  956. ;; except the scratch buffer.
  957. (not (string= (buffer-name) "*scratch*")))
  958. ;; Otherwise, turn on whitespace mode.
  959. (whitespace-turn-on)))))
  960. ;;;###autoload
  961. (define-minor-mode global-whitespace-newline-mode
  962. "Toggle global newline visualization (Global Whitespace Newline mode).
  963. With a prefix argument ARG, enable Global Whitespace Newline mode
  964. if ARG is positive, and disable it otherwise. If called from
  965. Lisp, enable it if ARG is omitted or nil.
  966. Use `global-whitespace-newline-mode' only for NEWLINE
  967. visualization exclusively. For other visualizations, including
  968. NEWLINE visualization together with (HARD) SPACEs and/or TABs,
  969. please use `global-whitespace-mode'.
  970. See also `whitespace-newline' and `whitespace-display-mappings'."
  971. :lighter " NL"
  972. :init-value nil
  973. :global t
  974. :group 'whitespace
  975. (let ((whitespace-style '(newline-mark newline)))
  976. (global-whitespace-mode (if global-whitespace-newline-mode
  977. 1 -1))
  978. ;; sync states (running a batch job)
  979. (setq global-whitespace-newline-mode global-whitespace-mode)))
  980. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  981. ;;;; User commands - Toggle
  982. (defconst whitespace-style-value-list
  983. '(face
  984. tabs
  985. spaces
  986. trailing
  987. lines
  988. lines-tail
  989. newline
  990. empty
  991. indentation
  992. indentation::tab
  993. indentation::space
  994. space-after-tab
  995. space-after-tab::tab
  996. space-after-tab::space
  997. space-before-tab
  998. space-before-tab::tab
  999. space-before-tab::space
  1000. help-newline ; value used by `whitespace-insert-option-mark'
  1001. tab-mark
  1002. space-mark
  1003. newline-mark
  1004. )
  1005. "List of valid `whitespace-style' values.")
  1006. (defconst whitespace-toggle-option-alist
  1007. '((?f . face)
  1008. (?t . tabs)
  1009. (?s . spaces)
  1010. (?r . trailing)
  1011. (?l . lines)
  1012. (?L . lines-tail)
  1013. (?n . newline)
  1014. (?e . empty)
  1015. (?\C-i . indentation)
  1016. (?I . indentation::tab)
  1017. (?i . indentation::space)
  1018. (?\C-a . space-after-tab)
  1019. (?A . space-after-tab::tab)
  1020. (?a . space-after-tab::space)
  1021. (?\C-b . space-before-tab)
  1022. (?B . space-before-tab::tab)
  1023. (?b . space-before-tab::space)
  1024. (?T . tab-mark)
  1025. (?S . space-mark)
  1026. (?N . newline-mark)
  1027. (?x . whitespace-style)
  1028. )
  1029. "Alist of toggle options.
  1030. Each element has the form:
  1031. (CHAR . SYMBOL)
  1032. Where:
  1033. CHAR is a char which the user will have to type.
  1034. SYMBOL is a valid symbol associated with CHAR.
  1035. See `whitespace-style-value-list'.")
  1036. (defvar whitespace-active-style nil
  1037. "Used to save locally `whitespace-style' value.")
  1038. (defvar whitespace-indent-tabs-mode indent-tabs-mode
  1039. "Used to save locally `indent-tabs-mode' value.")
  1040. (defvar whitespace-tab-width tab-width
  1041. "Used to save locally `tab-width' value.")
  1042. (defvar whitespace-point (point)
  1043. "Used to save locally current point value.
  1044. Used by `whitespace-trailing-regexp' function (which see).")
  1045. (defvar whitespace-font-lock-refontify nil
  1046. "Used to save locally the font-lock refontify state.
  1047. Used by `whitespace-post-command-hook' function (which see).")
  1048. (defvar whitespace-bob-marker nil
  1049. "Used to save locally the bob marker value.
  1050. Used by `whitespace-post-command-hook' function (which see).")
  1051. (defvar whitespace-eob-marker nil
  1052. "Used to save locally the eob marker value.
  1053. Used by `whitespace-post-command-hook' function (which see).")
  1054. (defvar whitespace-buffer-changed nil
  1055. "Used to indicate locally if buffer changed.
  1056. Used by `whitespace-post-command-hook' and `whitespace-buffer-changed'
  1057. functions (which see).")
  1058. ;;;###autoload
  1059. (defun whitespace-toggle-options (arg)
  1060. "Toggle local `whitespace-mode' options.
  1061. If local whitespace-mode is off, toggle the option given by ARG
  1062. and turn on local whitespace-mode.
  1063. If local whitespace-mode is on, toggle the option given by ARG
  1064. and restart local whitespace-mode.
  1065. Interactively, it reads one of the following chars:
  1066. CHAR MEANING
  1067. (VIA FACES)
  1068. f toggle face visualization
  1069. t toggle TAB visualization
  1070. s toggle SPACE and HARD SPACE visualization
  1071. r toggle trailing blanks visualization
  1072. l toggle \"long lines\" visualization
  1073. L toggle \"long lines\" tail visualization
  1074. n toggle NEWLINE visualization
  1075. e toggle empty line at bob and/or eob visualization
  1076. C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
  1077. I toggle indentation SPACEs visualization
  1078. i toggle indentation TABs visualization
  1079. C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
  1080. A toggle SPACEs after TAB: SPACEs visualization
  1081. a toggle SPACEs after TAB: TABs visualization
  1082. C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
  1083. B toggle SPACEs before TAB: SPACEs visualization
  1084. b toggle SPACEs before TAB: TABs visualization
  1085. (VIA DISPLAY TABLE)
  1086. T toggle TAB visualization
  1087. S toggle SPACEs before TAB visualization
  1088. N toggle NEWLINE visualization
  1089. x restore `whitespace-style' value
  1090. ? display brief help
  1091. Non-interactively, ARG should be a symbol or a list of symbols.
  1092. The valid symbols are:
  1093. face toggle face visualization
  1094. tabs toggle TAB visualization
  1095. spaces toggle SPACE and HARD SPACE visualization
  1096. trailing toggle trailing blanks visualization
  1097. lines toggle \"long lines\" visualization
  1098. lines-tail toggle \"long lines\" tail visualization
  1099. newline toggle NEWLINE visualization
  1100. empty toggle empty line at bob and/or eob visualization
  1101. indentation toggle indentation SPACEs visualization
  1102. indentation::tab toggle indentation SPACEs visualization
  1103. indentation::space toggle indentation TABs visualization
  1104. space-after-tab toggle SPACEs after TAB visualization
  1105. space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
  1106. space-after-tab::space toggle SPACEs after TAB: TABs visualization
  1107. space-before-tab toggle SPACEs before TAB visualization
  1108. space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
  1109. space-before-tab::space toggle SPACEs before TAB: TABs visualization
  1110. tab-mark toggle TAB visualization
  1111. space-mark toggle SPACEs before TAB visualization
  1112. newline-mark toggle NEWLINE visualization
  1113. whitespace-style restore `whitespace-style' value
  1114. See `whitespace-style' and `indent-tabs-mode' for documentation."
  1115. (interactive (whitespace-interactive-char t))
  1116. (let ((whitespace-style
  1117. (whitespace-toggle-list t arg whitespace-active-style)))
  1118. (whitespace-mode 0)
  1119. (whitespace-mode 1)))
  1120. (defvar whitespace-toggle-style nil
  1121. "Used to toggle the global `whitespace-style' value.")
  1122. ;;;###autoload
  1123. (defun global-whitespace-toggle-options (arg)
  1124. "Toggle global `whitespace-mode' options.
  1125. If global whitespace-mode is off, toggle the option given by ARG
  1126. and turn on global whitespace-mode.
  1127. If global whitespace-mode is on, toggle the option given by ARG
  1128. and restart global whitespace-mode.
  1129. Interactively, it accepts one of the following chars:
  1130. CHAR MEANING
  1131. (VIA FACES)
  1132. f toggle face visualization
  1133. t toggle TAB visualization
  1134. s toggle SPACE and HARD SPACE visualization
  1135. r toggle trailing blanks visualization
  1136. l toggle \"long lines\" visualization
  1137. L toggle \"long lines\" tail visualization
  1138. n toggle NEWLINE visualization
  1139. e toggle empty line at bob and/or eob visualization
  1140. C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
  1141. I toggle indentation SPACEs visualization
  1142. i toggle indentation TABs visualization
  1143. C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
  1144. A toggle SPACEs after TAB: SPACEs visualization
  1145. a toggle SPACEs after TAB: TABs visualization
  1146. C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
  1147. B toggle SPACEs before TAB: SPACEs visualization
  1148. b toggle SPACEs before TAB: TABs visualization
  1149. (VIA DISPLAY TABLE)
  1150. T toggle TAB visualization
  1151. S toggle SPACEs before TAB visualization
  1152. N toggle NEWLINE visualization
  1153. x restore `whitespace-style' value
  1154. ? display brief help
  1155. Non-interactively, ARG should be a symbol or a list of symbols.
  1156. The valid symbols are:
  1157. face toggle face visualization
  1158. tabs toggle TAB visualization
  1159. spaces toggle SPACE and HARD SPACE visualization
  1160. trailing toggle trailing blanks visualization
  1161. lines toggle \"long lines\" visualization
  1162. lines-tail toggle \"long lines\" tail visualization
  1163. newline toggle NEWLINE visualization
  1164. empty toggle empty line at bob and/or eob visualization
  1165. indentation toggle indentation SPACEs visualization
  1166. indentation::tab toggle indentation SPACEs visualization
  1167. indentation::space toggle indentation TABs visualization
  1168. space-after-tab toggle SPACEs after TAB visualization
  1169. space-after-tab::tab toggle SPACEs after TAB: SPACEs visualization
  1170. space-after-tab::space toggle SPACEs after TAB: TABs visualization
  1171. space-before-tab toggle SPACEs before TAB visualization
  1172. space-before-tab::tab toggle SPACEs before TAB: SPACEs visualization
  1173. space-before-tab::space toggle SPACEs before TAB: TABs visualization
  1174. tab-mark toggle TAB visualization
  1175. space-mark toggle SPACEs before TAB visualization
  1176. newline-mark toggle NEWLINE visualization
  1177. whitespace-style restore `whitespace-style' value
  1178. See `whitespace-style' and `indent-tabs-mode' for documentation."
  1179. (interactive (whitespace-interactive-char nil))
  1180. (let ((whitespace-style
  1181. (whitespace-toggle-list nil arg whitespace-toggle-style)))
  1182. (setq whitespace-toggle-style whitespace-style)
  1183. (global-whitespace-mode 0)
  1184. (global-whitespace-mode 1)))
  1185. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1186. ;;;; User commands - Cleanup
  1187. ;;;###autoload
  1188. (defun whitespace-cleanup ()
  1189. "Cleanup some blank problems in all buffer or at region.
  1190. It usually applies to the whole buffer, but in transient mark
  1191. mode when the mark is active, it applies to the region. It also
  1192. applies to the region when it is not in transient mark mode, the
  1193. mark is active and \\[universal-argument] was pressed just before
  1194. calling `whitespace-cleanup' interactively.
  1195. See also `whitespace-cleanup-region'.
  1196. The problems cleaned up are:
  1197. 1. empty lines at beginning of buffer.
  1198. 2. empty lines at end of buffer.
  1199. If `whitespace-style' includes the value `empty', remove all
  1200. empty lines at beginning and/or end of buffer.
  1201. 3. 8 or more SPACEs at beginning of line.
  1202. If `whitespace-style' includes the value `indentation':
  1203. replace 8 or more SPACEs at beginning of line by TABs, if
  1204. `indent-tabs-mode' is non-nil; otherwise, replace TABs by
  1205. SPACEs.
  1206. If `whitespace-style' includes the value `indentation::tab',
  1207. replace 8 or more SPACEs at beginning of line by TABs.
  1208. If `whitespace-style' includes the value `indentation::space',
  1209. replace TABs by SPACEs.
  1210. 4. SPACEs before TAB.
  1211. If `whitespace-style' includes the value `space-before-tab':
  1212. replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  1213. otherwise, replace TABs by SPACEs.
  1214. If `whitespace-style' includes the value
  1215. `space-before-tab::tab', replace SPACEs by TABs.
  1216. If `whitespace-style' includes the value
  1217. `space-before-tab::space', replace TABs by SPACEs.
  1218. 5. SPACEs or TABs at end of line.
  1219. If `whitespace-style' includes the value `trailing', remove
  1220. all SPACEs or TABs at end of line.
  1221. 6. 8 or more SPACEs after TAB.
  1222. If `whitespace-style' includes the value `space-after-tab':
  1223. replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  1224. otherwise, replace TABs by SPACEs.
  1225. If `whitespace-style' includes the value
  1226. `space-after-tab::tab', replace SPACEs by TABs.
  1227. If `whitespace-style' includes the value
  1228. `space-after-tab::space', replace TABs by SPACEs.
  1229. See `whitespace-style', `indent-tabs-mode' and `tab-width' for
  1230. documentation."
  1231. (interactive "@")
  1232. (cond
  1233. ;; read-only buffer
  1234. (buffer-read-only
  1235. (whitespace-warn-read-only "cleanup"))
  1236. ;; region active
  1237. ((and (or transient-mark-mode
  1238. current-prefix-arg)
  1239. mark-active)
  1240. ;; PROBLEMs 1 and 2 are not handled in region
  1241. ;; PROBLEM 3: 8 or more SPACEs at bol
  1242. ;; PROBLEM 4: SPACEs before TAB
  1243. ;; PROBLEM 5: SPACEs or TABs at eol
  1244. ;; PROBLEM 6: 8 or more SPACEs after TAB
  1245. (whitespace-cleanup-region (region-beginning) (region-end)))
  1246. ;; whole buffer
  1247. (t
  1248. (save-excursion
  1249. (save-match-data ;FIXME: Why?
  1250. ;; PROBLEM 1: empty lines at bob
  1251. ;; PROBLEM 2: empty lines at eob
  1252. ;; ACTION: remove all empty lines at bob and/or eob
  1253. (when (memq 'empty whitespace-style)
  1254. (let (overwrite-mode) ; enforce no overwrite
  1255. (goto-char (point-min))
  1256. (when (looking-at whitespace-empty-at-bob-regexp)
  1257. (delete-region (match-beginning 1) (match-end 1)))
  1258. (when (re-search-forward
  1259. (concat whitespace-empty-at-eob-regexp "\\'") nil t)
  1260. (delete-region (match-beginning 1) (match-end 1)))))))
  1261. ;; PROBLEM 3: 8 or more SPACEs at bol
  1262. ;; PROBLEM 4: SPACEs before TAB
  1263. ;; PROBLEM 5: SPACEs or TABs at eol
  1264. ;; PROBLEM 6: 8 or more SPACEs after TAB
  1265. (whitespace-cleanup-region (point-min) (point-max)))))
  1266. ;;;###autoload
  1267. (defun whitespace-cleanup-region (start end)
  1268. "Cleanup some blank problems at region.
  1269. The problems cleaned up are:
  1270. 1. 8 or more SPACEs at beginning of line.
  1271. If `whitespace-style' includes the value `indentation':
  1272. replace 8 or more SPACEs at beginning of line by TABs, if
  1273. `indent-tabs-mode' is non-nil; otherwise, replace TABs by
  1274. SPACEs.
  1275. If `whitespace-style' includes the value `indentation::tab',
  1276. replace 8 or more SPACEs at beginning of line by TABs.
  1277. If `whitespace-style' includes the value `indentation::space',
  1278. replace TABs by SPACEs.
  1279. 2. SPACEs before TAB.
  1280. If `whitespace-style' includes the value `space-before-tab':
  1281. replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  1282. otherwise, replace TABs by SPACEs.
  1283. If `whitespace-style' includes the value
  1284. `space-before-tab::tab', replace SPACEs by TABs.
  1285. If `whitespace-style' includes the value
  1286. `space-before-tab::space', replace TABs by SPACEs.
  1287. 3. SPACEs or TABs at end of line.
  1288. If `whitespace-style' includes the value `trailing', remove
  1289. all SPACEs or TABs at end of line.
  1290. 4. 8 or more SPACEs after TAB.
  1291. If `whitespace-style' includes the value `space-after-tab':
  1292. replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
  1293. otherwise, replace TABs by SPACEs.
  1294. If `whitespace-style' includes the value
  1295. `space-after-tab::tab', replace SPACEs by TABs.
  1296. If `whitespace-style' includes the value
  1297. `space-after-tab::space', replace TABs by SPACEs.
  1298. See `whitespace-style', `indent-tabs-mode' and `tab-width' for
  1299. documentation."
  1300. (interactive "@r")
  1301. (if buffer-read-only
  1302. ;; read-only buffer
  1303. (whitespace-warn-read-only "cleanup region")
  1304. ;; non-read-only buffer
  1305. (let ((rstart (min start end))
  1306. (rend (copy-marker (max start end)))
  1307. (indent-tabs-mode whitespace-indent-tabs-mode)
  1308. (tab-width whitespace-tab-width)
  1309. overwrite-mode ; enforce no overwrite
  1310. tmp)
  1311. (save-excursion
  1312. (save-match-data ;FIXME: Why?
  1313. ;; PROBLEM 1: 8 or more SPACEs at bol
  1314. (cond
  1315. ;; ACTION: replace 8 or more SPACEs at bol by TABs, if
  1316. ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
  1317. ;; by SPACEs.
  1318. ((memq 'indentation whitespace-style)
  1319. (let ((regexp (whitespace-indentation-regexp)))
  1320. (goto-char rstart)
  1321. (while (re-search-forward regexp rend t)
  1322. (setq tmp (current-indentation))
  1323. (goto-char (match-beginning 0))
  1324. (delete-horizontal-space)
  1325. (unless (eolp)
  1326. (indent-to tmp)))))
  1327. ;; ACTION: replace 8 or more SPACEs at bol by TABs.
  1328. ((memq 'indentation::tab whitespace-style)
  1329. (whitespace-replace-action
  1330. 'tabify rstart rend
  1331. (whitespace-indentation-regexp 'tab) 0))
  1332. ;; ACTION: replace TABs by SPACEs.
  1333. ((memq 'indentation::space whitespace-style)
  1334. (whitespace-replace-action
  1335. 'untabify rstart rend
  1336. (whitespace-indentation-regexp 'space) 0)))
  1337. ;; PROBLEM 3: SPACEs or TABs at eol
  1338. ;; ACTION: remove all SPACEs or TABs at eol
  1339. (when (memq 'trailing whitespace-style)
  1340. (whitespace-replace-action
  1341. 'delete-region rstart rend
  1342. whitespace-trailing-regexp 1))
  1343. ;; PROBLEM 4: 8 or more SPACEs after TAB
  1344. (cond
  1345. ;; ACTION: replace 8 or more SPACEs by TABs, if
  1346. ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
  1347. ;; by SPACEs.
  1348. ((memq 'space-after-tab whitespace-style)
  1349. (whitespace-replace-action
  1350. (if whitespace-indent-tabs-mode 'tabify 'untabify)
  1351. rstart rend (whitespace-space-after-tab-regexp) 1))
  1352. ;; ACTION: replace 8 or more SPACEs by TABs.
  1353. ((memq 'space-after-tab::tab whitespace-style)
  1354. (whitespace-replace-action
  1355. 'tabify rstart rend
  1356. (whitespace-space-after-tab-regexp 'tab) 1))
  1357. ;; ACTION: replace TABs by SPACEs.
  1358. ((memq 'space-after-tab::space whitespace-style)
  1359. (whitespace-replace-action
  1360. 'untabify rstart rend
  1361. (whitespace-space-after-tab-regexp 'space) 1)))
  1362. ;; PROBLEM 2: SPACEs before TAB
  1363. (cond
  1364. ;; ACTION: replace SPACEs before TAB by TABs, if
  1365. ;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
  1366. ;; by SPACEs.
  1367. ((memq 'space-before-tab whitespace-style)
  1368. (whitespace-replace-action
  1369. (if whitespace-indent-tabs-mode 'tabify 'untabify)
  1370. rstart rend whitespace-space-before-tab-regexp
  1371. (if whitespace-indent-tabs-mode 0 2)))
  1372. ;; ACTION: replace SPACEs before TAB by TABs.
  1373. ((memq 'space-before-tab::tab whitespace-style)
  1374. (whitespace-replace-action
  1375. 'tabify rstart rend
  1376. whitespace-space-before-tab-regexp 0))
  1377. ;; ACTION: replace TABs by SPACEs.
  1378. ((memq 'space-before-tab::space whitespace-style)
  1379. (whitespace-replace-action
  1380. 'untabify rstart rend
  1381. whitespace-space-before-tab-regexp 2)))))
  1382. (set-marker rend nil)))) ; point marker to nowhere
  1383. (defun whitespace-replace-action (action rstart rend regexp index)
  1384. "Do ACTION in the string matched by REGEXP between RSTART and REND.
  1385. INDEX is the level group matched by REGEXP and used by ACTION.
  1386. See also `tab-width'."
  1387. (goto-char rstart)
  1388. (while (re-search-forward regexp rend t)
  1389. (goto-char (match-end index))
  1390. (funcall action (match-beginning index) (match-end index))))
  1391. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1392. ;;;; User command - report
  1393. (defun whitespace-regexp (regexp &optional kind)
  1394. "Return REGEXP depending on `whitespace-indent-tabs-mode'."
  1395. (cond
  1396. ((or (eq kind 'tab)
  1397. whitespace-indent-tabs-mode)
  1398. (format (car regexp) whitespace-tab-width))
  1399. ((or (eq kind 'space)
  1400. (not whitespace-indent-tabs-mode))
  1401. (cdr regexp))))
  1402. (defun whitespace-indentation-regexp (&optional kind)
  1403. "Return the indentation regexp depending on `whitespace-indent-tabs-mode'."
  1404. (whitespace-regexp whitespace-indentation-regexp kind))
  1405. (defun whitespace-space-after-tab-regexp (&optional kind)
  1406. "Return the space-after-tab regexp depending on `whitespace-indent-tabs-mode'."
  1407. (whitespace-regexp whitespace-space-after-tab-regexp kind))
  1408. (defconst whitespace-report-list
  1409. (list
  1410. (cons 'empty whitespace-empty-at-bob-regexp)
  1411. (cons 'empty whitespace-empty-at-eob-regexp)
  1412. (cons 'trailing whitespace-trailing-regexp)
  1413. (cons 'indentation nil)
  1414. (cons 'indentation::tab nil)
  1415. (cons 'indentation::space nil)
  1416. (cons 'space-before-tab whitespace-space-before-tab-regexp)
  1417. (cons 'space-before-tab::tab whitespace-space-before-tab-regexp)
  1418. (cons 'space-before-tab::space whitespace-space-before-tab-regexp)
  1419. (cons 'space-after-tab nil)
  1420. (cons 'space-after-tab::tab nil)
  1421. (cons 'space-after-tab::space nil)
  1422. )
  1423. "List of whitespace bogus symbol and corresponding regexp.")
  1424. (defconst whitespace-report-text
  1425. '( ;; `indent-tabs-mode' has non-nil value
  1426. "\
  1427. Whitespace Report
  1428. Current Setting Whitespace Problem
  1429. empty [] [] empty lines at beginning of buffer
  1430. empty [] [] empty lines at end of buffer
  1431. trailing [] [] SPACEs or TABs at end of line
  1432. indentation [] [] 8 or more SPACEs at beginning of line
  1433. indentation::tab [] [] 8 or more SPACEs at beginning of line
  1434. indentation::space [] [] TABs at beginning of line
  1435. space-before-tab [] [] SPACEs before TAB
  1436. space-before-tab::tab [] [] SPACEs before TAB: SPACEs
  1437. space-before-tab::space [] [] SPACEs before TAB: TABs
  1438. space-after-tab [] [] 8 or more SPACEs after TAB
  1439. space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
  1440. space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
  1441. indent-tabs-mode =
  1442. tab-width = \n\n"
  1443. . ;; `indent-tabs-mode' has nil value
  1444. "\
  1445. Whitespace Report
  1446. Current Setting Whitespace Problem
  1447. empty [] [] empty lines at beginning of buffer
  1448. empty [] [] empty lines at end of buffer
  1449. trailing [] [] SPACEs or TABs at end of line
  1450. indentation [] [] TABs at beginning of line
  1451. indentation::tab [] [] 8 or more SPACEs at beginning of line
  1452. indentation::space [] [] TABs at beginning of line
  1453. space-before-tab [] [] SPACEs before TAB
  1454. space-before-tab::tab [] [] SPACEs before TAB: SPACEs
  1455. space-before-tab::space [] [] SPACEs before TAB: TABs
  1456. space-after-tab [] [] 8 or more SPACEs after TAB
  1457. space-after-tab::tab [] [] 8 or more SPACEs after TAB: SPACEs
  1458. space-after-tab::space [] [] 8 or more SPACEs after TAB: TABs
  1459. indent-tabs-mode =
  1460. tab-width = \n\n")
  1461. "Text for whitespace bogus report.
  1462. It is a cons of strings, where the car part is used when
  1463. `indent-tabs-mode' is non-nil, and the cdr part is used when
  1464. `indent-tabs-mode' is nil.")
  1465. (defconst whitespace-report-buffer-name "*Whitespace Report*"
  1466. "The buffer name for whitespace bogus report.")
  1467. ;;;###autoload
  1468. (defun whitespace-report (&optional force report-if-bogus)
  1469. "Report some whitespace problems in buffer.
  1470. Return nil if there is no whitespace problem; otherwise, return
  1471. non-nil.
  1472. If FORCE is non-nil or \\[universal-argument] was pressed just
  1473. before calling `whitespace-report' interactively, it forces
  1474. `whitespace-style' to have:
  1475. empty
  1476. trailing
  1477. indentation
  1478. space-before-tab
  1479. space-after-tab
  1480. If REPORT-IF-BOGUS is non-nil, it reports only when there are any
  1481. whitespace problems in buffer.
  1482. Report if some of the following whitespace problems exist:
  1483. * If `indent-tabs-mode' is non-nil:
  1484. empty 1. empty lines at beginning of buffer.
  1485. empty 2. empty lines at end of buffer.
  1486. trailing 3. SPACEs or TABs at end of line.
  1487. indentation 4. 8 or more SPACEs at beginning of line.
  1488. space-before-tab 5. SPACEs before TAB.
  1489. space-after-tab 6. 8 or more SPACEs after TAB.
  1490. * If `indent-tabs-mode' is nil:
  1491. empty 1. empty lines at beginning of buffer.
  1492. empty 2. empty lines at end of buffer.
  1493. trailing 3. SPACEs or TABs at end of line.
  1494. indentation 4. TABS at beginning of line.
  1495. space-before-tab 5. SPACEs before TAB.
  1496. space-after-tab 6. 8 or more SPACEs after TAB.
  1497. See `whitespace-style' for documentation.
  1498. See also `whitespace-cleanup' and `whitespace-cleanup-region' for
  1499. cleaning up these problems."
  1500. (interactive (list current-prefix-arg))
  1501. (whitespace-report-region (point-min) (point-max)
  1502. force report-if-bogus))
  1503. ;;;###autoload
  1504. (defun whitespace-report-region (start end &optional force report-if-bogus)
  1505. "Report some whitespace problems in a region.
  1506. Return nil if there is no whitespace problem; otherwise, return
  1507. non-nil.
  1508. If FORCE is non-nil or \\[universal-argument] was pressed just
  1509. before calling `whitespace-report-region' interactively, it
  1510. forces `whitespace-style' to have:
  1511. empty
  1512. indentation
  1513. space-before-tab
  1514. trailing
  1515. space-after-tab
  1516. If REPORT-IF-BOGUS is non-nil, it reports only when there are any
  1517. whitespace problems in buffer.
  1518. Report if some of the following whitespace problems exist:
  1519. * If `indent-tabs-mode' is non-nil:
  1520. empty 1. empty lines at beginning of buffer.
  1521. empty 2. empty lines at end of buffer.
  1522. trailing 3. SPACEs or TABs at end of line.
  1523. indentation 4. 8 or more SPACEs at beginning of line.
  1524. space-before-tab 5. SPACEs before TAB.
  1525. space-after-tab 6. 8 or more SPACEs after TAB.
  1526. * If `indent-tabs-mode' is nil:
  1527. empty 1. empty lines at beginning of buffer.
  1528. empty 2. empty lines at end of buffer.
  1529. trailing 3. SPACEs or TABs at end of line.
  1530. indentation 4. TABS at beginning of line.
  1531. space-before-tab 5. SPACEs before TAB.
  1532. space-after-tab 6. 8 or more SPACEs after TAB.
  1533. See `whitespace-style' for documentation.
  1534. See also `whitespace-cleanup' and `whitespace-cleanup-region' for
  1535. cleaning up these problems."
  1536. (interactive "r")
  1537. (setq force (or current-prefix-arg force))
  1538. (save-excursion
  1539. (save-match-data ;FIXME: Why?
  1540. (let* ((has-bogus nil)
  1541. (rstart (min start end))
  1542. (rend (max start end))
  1543. (bogus-list
  1544. (mapcar
  1545. #'(lambda (option)
  1546. (when force
  1547. (add-to-list 'whitespace-style (car option)))
  1548. (goto-char rstart)
  1549. (let ((regexp
  1550. (cond
  1551. ((eq (car option) 'indentation)
  1552. (whitespace-indentation-regexp))
  1553. ((eq (car option) 'indentation::tab)
  1554. (whitespace-indentation-regexp 'tab))
  1555. ((eq (car option) 'indentation::space)
  1556. (whitespace-indentation-regexp 'space))
  1557. ((eq (car option) 'space-after-tab)
  1558. (whitespace-space-after-tab-regexp))
  1559. ((eq (car option) 'space-after-tab::tab)
  1560. (whitespace-space-after-tab-regexp 'tab))
  1561. ((eq (car option) 'space-after-tab::space)
  1562. (whitespace-space-after-tab-regexp 'space))
  1563. (t
  1564. (cdr option)))))
  1565. (and (re-search-forward regexp rend t)
  1566. (setq has-bogus t))))
  1567. whitespace-report-list)))
  1568. (when (if report-if-bogus has-bogus t)
  1569. (whitespace-kill-buffer whitespace-report-buffer-name)
  1570. ;; `whitespace-indent-tabs-mode' is local to current buffer
  1571. ;; `whitespace-tab-width' is local to current buffer
  1572. (let ((ws-indent-tabs-mode whitespace-indent-tabs-mode)
  1573. (ws-tab-width whitespace-tab-width))
  1574. (with-current-buffer (get-buffer-create
  1575. whitespace-report-buffer-name)
  1576. (erase-buffer)
  1577. (insert (if ws-indent-tabs-mode
  1578. (car whitespace-report-text)
  1579. (cdr whitespace-report-text)))
  1580. (goto-char (point-min))
  1581. (forward-line 3)
  1582. (dolist (option whitespace-report-list)
  1583. (forward-line 1)
  1584. (whitespace-mark-x
  1585. 27 (memq (car option) whitespace-style))
  1586. (whitespace-mark-x 7 (car bogus-list))
  1587. (setq bogus-list (cdr bogus-list)))
  1588. (forward-line 1)
  1589. (whitespace-insert-value ws-indent-tabs-mode)
  1590. (whitespace-insert-value ws-tab-width)
  1591. (when has-bogus
  1592. (goto-char (point-max))
  1593. (insert " Type `M-x whitespace-cleanup'"
  1594. " to cleanup the buffer.\n\n"
  1595. " Type `M-x whitespace-cleanup-region'"
  1596. " to cleanup a region.\n\n"))
  1597. (whitespace-display-window (current-buffer)))))
  1598. has-bogus))))
  1599. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1600. ;;;; Internal functions
  1601. (defvar whitespace-font-lock-mode nil
  1602. "Used to remember whether a buffer had font lock mode on or not.")
  1603. (defvar whitespace-font-lock nil
  1604. "Used to remember whether a buffer initially had font lock on or not.")
  1605. (defvar whitespace-font-lock-keywords nil
  1606. "Used to save locally `font-lock-keywords' value.")
  1607. (defconst whitespace-help-text
  1608. "\
  1609. Whitespace Toggle Options | scroll up : SPC or > |
  1610. | scroll down: M-SPC or < |
  1611. FACES \\__________________________/
  1612. [] f - toggle face visualization
  1613. [] t - toggle TAB visualization
  1614. [] s - toggle SPACE and HARD SPACE visualization
  1615. [] r - toggle trailing blanks visualization
  1616. [] l - toggle \"long lines\" visualization
  1617. [] L - toggle \"long lines\" tail visualization
  1618. [] n - toggle NEWLINE visualization
  1619. [] e - toggle empty line at bob and/or eob visualization
  1620. [] C-i - toggle indentation SPACEs visualization (via `indent-tabs-mode')
  1621. [] I - toggle indentation SPACEs visualization
  1622. [] i - toggle indentation TABs visualization
  1623. [] C-a - toggle SPACEs after TAB visualization (via `indent-tabs-mode')
  1624. [] A - toggle SPACEs after TAB: SPACEs visualization
  1625. [] a - toggle SPACEs after TAB: TABs visualization
  1626. [] C-b - toggle SPACEs before TAB visualization (via `indent-tabs-mode')
  1627. [] B - toggle SPACEs before TAB: SPACEs visualization
  1628. [] b - toggle SPACEs before TAB: TABs visualization
  1629. DISPLAY TABLE
  1630. [] T - toggle TAB visualization
  1631. [] S - toggle SPACE and HARD SPACE visualization
  1632. [] N - toggle NEWLINE visualization
  1633. x - restore `whitespace-style' value
  1634. ? - display this text\n\n"
  1635. "Text for whitespace toggle options.")
  1636. (defconst whitespace-help-buffer-name "*Whitespace Toggle Options*"
  1637. "The buffer name for whitespace toggle options.")
  1638. (defun whitespace-insert-value (value)
  1639. "Insert VALUE at column 20 of next line."
  1640. (forward-line 1)
  1641. (move-to-column 20 t)
  1642. (insert (format "%s" value)))
  1643. (defun whitespace-mark-x (nchars condition)
  1644. "Insert the mark ('X' or ' ') after NCHARS depending on CONDITION."
  1645. (forward-char nchars)
  1646. (insert (if condition "X" " ")))
  1647. (defun whitespace-insert-option-mark (the-list the-value)
  1648. "Insert the option mark ('X' or ' ') in toggle options buffer."
  1649. (goto-char (point-min))
  1650. (forward-line 2)
  1651. (dolist (sym the-list)
  1652. (if (eq sym 'help-newline)
  1653. (forward-line 2)
  1654. (forward-line 1)
  1655. (whitespace-mark-x 2 (memq sym the-value)))))
  1656. (defun whitespace-help-on (style)
  1657. "Display the whitespace toggle options."
  1658. (unless (get-buffer whitespace-help-buffer-name)
  1659. (delete-other-windows)
  1660. (let ((buffer (get-buffer-create whitespace-help-buffer-name)))
  1661. (with-current-buffer buffer
  1662. (erase-buffer)
  1663. (insert whitespace-help-text)
  1664. (whitespace-insert-option-mark
  1665. whitespace-style-value-list style)
  1666. (whitespace-display-window buffer)))))
  1667. (defun whitespace-display-window (buffer)
  1668. "Display BUFFER in a new window."
  1669. (goto-char (point-min))
  1670. (set-buffer-modified-p nil)
  1671. (when (< (window-height) (* 2 window-min-height))
  1672. (kill-buffer buffer)
  1673. (error "Window height is too small; \
  1674. can't split window to display whitespace toggle options"))
  1675. (let ((win (split-window)))
  1676. (set-window-buffer win buffer)
  1677. (shrink-window-if-larger-than-buffer win)))
  1678. (defun whitespace-kill-buffer (buffer-name)
  1679. "Kill buffer BUFFER-NAME and windows related with it."
  1680. (let ((buffer (get-buffer buffer-name)))
  1681. (when buffer
  1682. (delete-windows-on buffer)
  1683. (kill-buffer buffer))))
  1684. (defun whitespace-help-off ()
  1685. "Remove the buffer and window of the whitespace toggle options."
  1686. (whitespace-kill-buffer whitespace-help-buffer-name))
  1687. (defun whitespace-help-scroll (&optional up)
  1688. "Scroll help window, if it exists.
  1689. If UP is non-nil, scroll up; otherwise, scroll down."
  1690. (condition-case nil
  1691. (let ((buffer (get-buffer whitespace-help-buffer-name)))
  1692. (if buffer
  1693. (with-selected-window (get-buffer-window buffer)
  1694. (if up
  1695. (scroll-up 3)
  1696. (scroll-down 3)))
  1697. (ding)))
  1698. ;; handler
  1699. ((error)
  1700. ;; just ignore error
  1701. )))
  1702. (defun whitespace-interactive-char (local-p)
  1703. "Interactive function to read a char and return a symbol.
  1704. If LOCAL-P is non-nil, it uses a local context; otherwise, it
  1705. uses a global context.
  1706. It accepts one of the following chars:
  1707. CHAR MEANING
  1708. (VIA FACES)
  1709. f toggle face visualization
  1710. t toggle TAB visualization
  1711. s toggle SPACE and HARD SPACE visualization
  1712. r toggle trailing blanks visualization
  1713. l toggle \"long lines\" visualization
  1714. L toggle \"long lines\" tail visualization
  1715. n toggle NEWLINE visualization
  1716. e toggle empty line at bob and/or eob visualization
  1717. C-i toggle indentation SPACEs visualization (via `indent-tabs-mode')
  1718. I toggle indentation SPACEs visualization
  1719. i toggle indentation TABs visualization
  1720. C-a toggle SPACEs after TAB visualization (via `indent-tabs-mode')
  1721. A toggle SPACEs after TAB: SPACEs visualization
  1722. a toggle SPACEs after TAB: TABs visualization
  1723. C-b toggle SPACEs before TAB visualization (via `indent-tabs-mode')
  1724. B toggle SPACEs before TAB: SPACEs visualization
  1725. b toggle SPACEs before TAB: TABs visualization
  1726. (VIA DISPLAY TABLE)
  1727. T toggle TAB visualization
  1728. S toggle SPACE and HARD SPACE visualization
  1729. N toggle NEWLINE visualization
  1730. x restore `whitespace-style' value
  1731. ? display brief help
  1732. See also `whitespace-toggle-option-alist'."
  1733. (let* ((is-off (not (if local-p
  1734. whitespace-mode
  1735. global-whitespace-mode)))
  1736. (style (cond (is-off whitespace-style) ; use default value
  1737. (local-p whitespace-active-style)
  1738. (t whitespace-toggle-style)))
  1739. (prompt
  1740. (format "Whitespace Toggle %s (type ? for further options)-"
  1741. (if local-p "Local" "Global")))
  1742. ch sym)
  1743. ;; read a valid option and get the corresponding symbol
  1744. (save-window-excursion
  1745. (condition-case data
  1746. (progn
  1747. (while
  1748. ;; while condition
  1749. (progn
  1750. (setq ch (read-char prompt))
  1751. (not
  1752. (setq sym
  1753. (cdr
  1754. (assq ch whitespace-toggle-option-alist)))))
  1755. ;; while body
  1756. (cond
  1757. ((eq ch ?\?) (whitespace-help-on style))
  1758. ((eq ch ?\ ) (whitespace-help-scroll t))
  1759. ((eq ch ?\M- ) (whitespace-help-scroll))
  1760. ((eq ch ?>) (whitespace-help-scroll t))
  1761. ((eq ch ?<) (whitespace-help-scroll))
  1762. (t (ding))))
  1763. (whitespace-help-off)
  1764. (message " ")) ; clean echo area
  1765. ;; handler
  1766. ((quit error)
  1767. (whitespace-help-off)
  1768. (error (error-message-string data)))))
  1769. (list sym))) ; return the appropriate symbol
  1770. (defun whitespace-toggle-list (local-p arg the-list)
  1771. "Toggle options in THE-LIST based on list ARG.
  1772. If LOCAL-P is non-nil, it uses a local context; otherwise, it
  1773. uses a global context.
  1774. ARG is a list of options to be toggled.
  1775. THE-LIST is a list of options. This list will be toggled and the
  1776. resultant list will be returned."
  1777. (unless (if local-p whitespace-mode global-whitespace-mode)
  1778. (setq the-list whitespace-style))
  1779. (setq the-list (copy-sequence the-list)) ; keep original list
  1780. (dolist (sym (if (listp arg) arg (list arg)))
  1781. (cond
  1782. ;; ignore help value
  1783. ((eq sym 'help-newline))
  1784. ;; restore default values
  1785. ((eq sym 'whitespace-style)
  1786. (setq the-list whitespace-style))
  1787. ;; toggle valid values
  1788. ((memq sym whitespace-style-value-list)
  1789. (setq the-list (if (memq sym the-list)
  1790. (delq sym the-list)
  1791. (cons sym the-list))))))
  1792. the-list)
  1793. (defvar whitespace-display-table nil
  1794. "Used to save a local display table.")
  1795. (defvar whitespace-display-table-was-local nil
  1796. "Used to remember whether a buffer initially had a local display table.")
  1797. (defun whitespace-turn-on ()
  1798. "Turn on whitespace visualization."
  1799. ;; prepare local hooks
  1800. (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
  1801. ;; create whitespace local buffer environment
  1802. (set (make-local-variable 'whitespace-font-lock-mode) nil)
  1803. (set (make-local-variable 'whitespace-font-lock) nil)
  1804. (set (make-local-variable 'whitespace-font-lock-keywords) nil)
  1805. (set (make-local-variable 'whitespace-display-table) nil)
  1806. (set (make-local-variable 'whitespace-display-table-was-local) nil)
  1807. (set (make-local-variable 'whitespace-active-style)
  1808. (if (listp whitespace-style)
  1809. whitespace-style
  1810. (list whitespace-style)))
  1811. (set (make-local-variable 'whitespace-indent-tabs-mode)
  1812. indent-tabs-mode)
  1813. (set (make-local-variable 'whitespace-tab-width)
  1814. tab-width)
  1815. ;; turn on whitespace
  1816. (when whitespace-active-style
  1817. (whitespace-color-on)
  1818. (whitespace-display-char-on)))
  1819. (defun whitespace-turn-off ()
  1820. "Turn off whitespace visualization."
  1821. (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
  1822. (when whitespace-active-style
  1823. (whitespace-color-off)
  1824. (whitespace-display-char-off)))
  1825. (defun whitespace-style-face-p ()
  1826. "Return t if there is some visualization via face."
  1827. (and (memq 'face whitespace-active-style)
  1828. (or (memq 'tabs whitespace-active-style)
  1829. (memq 'spaces whitespace-active-style)
  1830. (memq 'trailing whitespace-active-style)
  1831. (memq 'lines whitespace-active-style)
  1832. (memq 'lines-tail whitespace-active-style)
  1833. (memq 'newline whitespace-active-style)
  1834. (memq 'empty whitespace-active-style)
  1835. (memq 'indentation whitespace-active-style)
  1836. (memq 'indentation::tab whitespace-active-style)
  1837. (memq 'indentation::space whitespace-active-style)
  1838. (memq 'space-after-tab whitespace-active-style)
  1839. (memq 'space-after-tab::tab whitespace-active-style)
  1840. (memq 'space-after-tab::space whitespace-active-style)
  1841. (memq 'space-before-tab whitespace-active-style)
  1842. (memq 'space-before-tab::tab whitespace-active-style)
  1843. (memq 'space-before-tab::space whitespace-active-style))))
  1844. (defun whitespace-color-on ()
  1845. "Turn on color visualization."
  1846. (when (whitespace-style-face-p)
  1847. (unless whitespace-font-lock
  1848. (setq whitespace-font-lock t
  1849. whitespace-font-lock-keywords
  1850. (copy-sequence font-lock-keywords)))
  1851. ;; save current point and refontify when necessary
  1852. (set (make-local-variable 'whitespace-point)
  1853. (point))
  1854. (set (make-local-variable 'whitespace-font-lock-refontify)
  1855. 0)
  1856. (set (make-local-variable 'whitespace-bob-marker)
  1857. (point-min-marker))
  1858. (set (make-local-variable 'whitespace-eob-marker)
  1859. (point-max-marker))
  1860. (set (make-local-variable 'whitespace-buffer-changed)
  1861. nil)
  1862. (add-hook 'post-command-hook #'whitespace-post-command-hook nil t)
  1863. (add-hook 'before-change-functions #'whitespace-buffer-changed nil t)
  1864. ;; turn off font lock
  1865. (set (make-local-variable 'whitespace-font-lock-mode)
  1866. font-lock-mode)
  1867. (font-lock-mode 0)
  1868. ;; add whitespace-mode color into font lock
  1869. (when (memq 'spaces whitespace-active-style)
  1870. (font-lock-add-keywords
  1871. nil
  1872. (list
  1873. ;; Show SPACEs
  1874. (list whitespace-space-regexp 1 whitespace-space t)
  1875. ;; Show HARD SPACEs
  1876. (list whitespace-hspace-regexp 1 whitespace-hspace t))
  1877. t))
  1878. (when (memq 'tabs whitespace-active-style)
  1879. (font-lock-add-keywords
  1880. nil
  1881. (list
  1882. ;; Show TABs
  1883. (list whitespace-tab-regexp 1 whitespace-tab t))
  1884. t))
  1885. (when (memq 'trailing whitespace-active-style)
  1886. (font-lock-add-keywords
  1887. nil
  1888. (list
  1889. ;; Show trailing blanks
  1890. (list #'whitespace-trailing-regexp 1 whitespace-trailing t))
  1891. t))
  1892. (when (or (memq 'lines whitespace-active-style)
  1893. (memq 'lines-tail whitespace-active-style))
  1894. (font-lock-add-keywords
  1895. nil
  1896. (list
  1897. ;; Show "long" lines
  1898. (list
  1899. (let ((line-column (or whitespace-line-column fill-column)))
  1900. (format
  1901. "^\\([^\t\n]\\{%s\\}\\|[^\t\n]\\{0,%s\\}\t\\)\\{%d\\}%s\\(.+\\)$"
  1902. whitespace-tab-width
  1903. (1- whitespace-tab-width)
  1904. (/ line-column whitespace-tab-width)
  1905. (let ((rem (% line-column whitespace-tab-width)))
  1906. (if (zerop rem)
  1907. ""
  1908. (format ".\\{%d\\}" rem)))))
  1909. (if (memq 'lines whitespace-active-style)
  1910. 0 ; whole line
  1911. 2) ; line tail
  1912. whitespace-line t))
  1913. t))
  1914. (cond
  1915. ((memq 'space-before-tab whitespace-active-style)
  1916. (font-lock-add-keywords
  1917. nil
  1918. (list
  1919. ;; Show SPACEs before TAB (indent-tabs-mode)
  1920. (list whitespace-space-before-tab-regexp
  1921. (if whitespace-indent-tabs-mode 1 2)
  1922. whitespace-space-before-tab t))
  1923. t))
  1924. ((memq 'space-before-tab::tab whitespace-active-style)
  1925. (font-lock-add-keywords
  1926. nil
  1927. (list
  1928. ;; Show SPACEs before TAB (SPACEs)
  1929. (list whitespace-space-before-tab-regexp
  1930. 1 whitespace-space-before-tab t))
  1931. t))
  1932. ((memq 'space-before-tab::space whitespace-active-style)
  1933. (font-lock-add-keywords
  1934. nil
  1935. (list
  1936. ;; Show SPACEs before TAB (TABs)
  1937. (list whitespace-space-before-tab-regexp
  1938. 2 whitespace-space-before-tab t))
  1939. t)))
  1940. (cond
  1941. ((memq 'indentation whitespace-active-style)
  1942. (font-lock-add-keywords
  1943. nil
  1944. (list
  1945. ;; Show indentation SPACEs (indent-tabs-mode)
  1946. (list (whitespace-indentation-regexp)
  1947. 1 whitespace-indentation t))
  1948. t))
  1949. ((memq 'indentation::tab whitespace-active-style)
  1950. (font-lock-add-keywords
  1951. nil
  1952. (list
  1953. ;; Show indentation SPACEs (SPACEs)
  1954. (list (whitespace-indentation-regexp 'tab)
  1955. 1 whitespace-indentation t))
  1956. t))
  1957. ((memq 'indentation::space whitespace-active-style)
  1958. (font-lock-add-keywords
  1959. nil
  1960. (list
  1961. ;; Show indentation SPACEs (TABs)
  1962. (list (whitespace-indentation-regexp 'space)
  1963. 1 whitespace-indentation t))
  1964. t)))
  1965. (when (memq 'empty whitespace-active-style)
  1966. (font-lock-add-keywords
  1967. nil
  1968. (list
  1969. ;; Show empty lines at beginning of buffer
  1970. (list #'whitespace-empty-at-bob-regexp
  1971. 1 whitespace-empty t))
  1972. t)
  1973. (font-lock-add-keywords
  1974. nil
  1975. (list
  1976. ;; Show empty lines at end of buffer
  1977. (list #'whitespace-empty-at-eob-regexp
  1978. 1 whitespace-empty t))
  1979. t))
  1980. (cond
  1981. ((memq 'space-after-tab whitespace-active-style)
  1982. (font-lock-add-keywords
  1983. nil
  1984. (list
  1985. ;; Show SPACEs after TAB (indent-tabs-mode)
  1986. (list (whitespace-space-after-tab-regexp)
  1987. 1 whitespace-space-after-tab t))
  1988. t))
  1989. ((memq 'space-after-tab::tab whitespace-active-style)
  1990. (font-lock-add-keywords
  1991. nil
  1992. (list
  1993. ;; Show SPACEs after TAB (SPACEs)
  1994. (list (whitespace-space-after-tab-regexp 'tab)
  1995. 1 whitespace-space-after-tab t))
  1996. t))
  1997. ((memq 'space-after-tab::space whitespace-active-style)
  1998. (font-lock-add-keywords
  1999. nil
  2000. (list
  2001. ;; Show SPACEs after TAB (TABs)
  2002. (list (whitespace-space-after-tab-regexp 'space)
  2003. 1 whitespace-space-after-tab t))
  2004. t)))
  2005. ;; now turn on font lock and highlight blanks
  2006. (font-lock-mode 1)))
  2007. (defun whitespace-color-off ()
  2008. "Turn off color visualization."
  2009. ;; turn off font lock
  2010. (when (whitespace-style-face-p)
  2011. (font-lock-mode 0)
  2012. (remove-hook 'post-command-hook #'whitespace-post-command-hook t)
  2013. (remove-hook 'before-change-functions #'whitespace-buffer-changed t)
  2014. (when whitespace-font-lock
  2015. (setq whitespace-font-lock nil
  2016. font-lock-keywords whitespace-font-lock-keywords))
  2017. ;; restore original font lock state
  2018. (font-lock-mode whitespace-font-lock-mode)))
  2019. (defun whitespace-trailing-regexp (limit)
  2020. "Match trailing spaces which do not contain the point at end of line."
  2021. (let ((status t))
  2022. (while (if (re-search-forward whitespace-trailing-regexp limit t)
  2023. (= whitespace-point (match-end 1)) ;; loop if point at eol
  2024. (setq status nil))) ;; end of buffer
  2025. status))
  2026. (defun whitespace-empty-at-bob-regexp (limit)
  2027. "Match spaces at beginning of buffer which do not contain the point at \
  2028. beginning of buffer."
  2029. (let ((b (point))
  2030. r)
  2031. (cond
  2032. ;; at bob
  2033. ((= b 1)
  2034. (setq r (and (/= whitespace-point 1)
  2035. (looking-at whitespace-empty-at-bob-regexp)))
  2036. (set-marker whitespace-bob-marker (if r (match-end 1) b)))
  2037. ;; inside bob empty region
  2038. ((<= limit whitespace-bob-marker)
  2039. (setq r (looking-at whitespace-empty-at-bob-regexp))
  2040. (if r
  2041. (when (< (match-end 1) limit)
  2042. (set-marker whitespace-bob-marker (match-end 1)))
  2043. (set-marker whitespace-bob-marker b)))
  2044. ;; intersection with end of bob empty region
  2045. ((<= b whitespace-bob-marker)
  2046. (setq r (looking-at whitespace-empty-at-bob-regexp))
  2047. (set-marker whitespace-bob-marker (if r (match-end 1) b)))
  2048. ;; it is not inside bob empty region
  2049. (t
  2050. (setq r nil)))
  2051. ;; move to end of matching
  2052. (and r (goto-char (match-end 1)))
  2053. r))
  2054. (defsubst whitespace-looking-back (regexp limit)
  2055. (save-excursion
  2056. (when (/= 0 (skip-chars-backward " \t\n" limit))
  2057. (unless (bolp)
  2058. (forward-line 1))
  2059. (looking-at regexp))))
  2060. (defun whitespace-empty-at-eob-regexp (limit)
  2061. "Match spaces at end of buffer which do not contain the point at end of \
  2062. buffer."
  2063. (let ((b (point))
  2064. (e (1+ (buffer-size)))
  2065. r)
  2066. (cond
  2067. ;; at eob
  2068. ((= limit e)
  2069. (when (/= whitespace-point e)
  2070. (goto-char limit)
  2071. (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)))
  2072. (if r
  2073. (set-marker whitespace-eob-marker (match-beginning 1))
  2074. (set-marker whitespace-eob-marker limit)
  2075. (goto-char b))) ; return back to initial position
  2076. ;; inside eob empty region
  2077. ((>= b whitespace-eob-marker)
  2078. (goto-char limit)
  2079. (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
  2080. (if r
  2081. (when (> (match-beginning 1) b)
  2082. (set-marker whitespace-eob-marker (match-beginning 1)))
  2083. (set-marker whitespace-eob-marker limit)
  2084. (goto-char b))) ; return back to initial position
  2085. ;; intersection with beginning of eob empty region
  2086. ((>= limit whitespace-eob-marker)
  2087. (goto-char limit)
  2088. (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b))
  2089. (if r
  2090. (set-marker whitespace-eob-marker (match-beginning 1))
  2091. (set-marker whitespace-eob-marker limit)
  2092. (goto-char b))) ; return back to initial position
  2093. ;; it is not inside eob empty region
  2094. (t
  2095. (setq r nil)))
  2096. r))
  2097. (defun whitespace-buffer-changed (_beg _end)
  2098. "Set `whitespace-buffer-changed' variable to t."
  2099. (setq whitespace-buffer-changed t))
  2100. (defun whitespace-post-command-hook ()
  2101. "Save current point into `whitespace-point' variable.
  2102. Also refontify when necessary."
  2103. (setq whitespace-point (point)) ; current point position
  2104. (let ((refontify
  2105. (or
  2106. ;; it is at end of line ...
  2107. (and (eolp)
  2108. ;; ... with trailing SPACE or TAB
  2109. (or (= (preceding-char) ?\ )
  2110. (= (preceding-char) ?\t)))
  2111. ;; it is at beginning of buffer (bob)
  2112. (= whitespace-point 1)
  2113. ;; the buffer was modified and ...
  2114. (and whitespace-buffer-changed
  2115. (or
  2116. ;; ... or inside bob whitespace region
  2117. (<= whitespace-point whitespace-bob-marker)
  2118. ;; ... or at bob whitespace region border
  2119. (and (= whitespace-point (1+ whitespace-bob-marker))
  2120. (= (preceding-char) ?\n))))
  2121. ;; it is at end of buffer (eob)
  2122. (= whitespace-point (1+ (buffer-size)))
  2123. ;; the buffer was modified and ...
  2124. (and whitespace-buffer-changed
  2125. (or
  2126. ;; ... or inside eob whitespace region
  2127. (>= whitespace-point whitespace-eob-marker)
  2128. ;; ... or at eob whitespace region border
  2129. (and (= whitespace-point (1- whitespace-eob-marker))
  2130. (= (following-char) ?\n)))))))
  2131. (when (or refontify (> whitespace-font-lock-refontify 0))
  2132. (setq whitespace-buffer-changed nil)
  2133. ;; adjust refontify counter
  2134. (setq whitespace-font-lock-refontify
  2135. (if refontify
  2136. 1
  2137. (1- whitespace-font-lock-refontify)))
  2138. ;; refontify
  2139. (jit-lock-refontify))))
  2140. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2141. ;;;; Hacked from visws.el (Miles Bader <miles@gnu.org>)
  2142. (defun whitespace-style-mark-p ()
  2143. "Return t if there is some visualization via display table."
  2144. (or (memq 'tab-mark whitespace-active-style)
  2145. (memq 'space-mark whitespace-active-style)
  2146. (memq 'newline-mark whitespace-active-style)))
  2147. (defsubst whitespace-char-valid-p (char)
  2148. ;; This check should be improved!!!
  2149. (or (< char 256)
  2150. (characterp char)))
  2151. (defun whitespace-display-vector-p (vec)
  2152. "Return true if every character in vector VEC can be displayed."
  2153. (let ((i (length vec)))
  2154. (when (> i 0)
  2155. (while (and (>= (setq i (1- i)) 0)
  2156. (whitespace-char-valid-p (aref vec i))))
  2157. (< i 0))))
  2158. (defun whitespace-display-char-on ()
  2159. "Turn on character display mapping."
  2160. (when (and whitespace-display-mappings
  2161. (whitespace-style-mark-p))
  2162. (let (vecs vec)
  2163. ;; Remember whether a buffer has a local display table.
  2164. (unless whitespace-display-table-was-local
  2165. (setq whitespace-display-table-was-local t
  2166. whitespace-display-table
  2167. (copy-sequence buffer-display-table))
  2168. ;; Assure `buffer-display-table' is unique
  2169. ;; when two or more windows are visible.
  2170. (setq buffer-display-table
  2171. (copy-sequence buffer-display-table)))
  2172. (unless buffer-display-table
  2173. (setq buffer-display-table (make-display-table)))
  2174. (dolist (entry whitespace-display-mappings)
  2175. ;; check if it is to display this mark
  2176. (when (memq (car entry) whitespace-style)
  2177. ;; Get a displayable mapping.
  2178. (setq vecs (cddr entry))
  2179. (while (and vecs
  2180. (not (whitespace-display-vector-p (car vecs))))
  2181. (setq vecs (cdr vecs)))
  2182. ;; Display a valid mapping.
  2183. (when vecs
  2184. (setq vec (copy-sequence (car vecs)))
  2185. ;; NEWLINE char
  2186. (when (and (eq (cadr entry) ?\n)
  2187. (memq 'newline whitespace-active-style))
  2188. ;; Only insert face bits on NEWLINE char mapping to avoid
  2189. ;; obstruction of other faces like TABs and (HARD) SPACEs
  2190. ;; faces, font-lock faces, etc.
  2191. (dotimes (i (length vec))
  2192. (or (eq (aref vec i) ?\n)
  2193. (aset vec i
  2194. (make-glyph-code (aref vec i)
  2195. whitespace-newline)))))
  2196. ;; Display mapping
  2197. (aset buffer-display-table (cadr entry) vec)))))))
  2198. (defun whitespace-display-char-off ()
  2199. "Turn off character display mapping."
  2200. (and whitespace-display-mappings
  2201. (whitespace-style-mark-p)
  2202. whitespace-display-table-was-local
  2203. (setq whitespace-display-table-was-local nil
  2204. buffer-display-table whitespace-display-table)))
  2205. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2206. ;;;; Hook
  2207. (defun whitespace-action-when-on ()
  2208. "Action to be taken always when local whitespace is turned on."
  2209. (cond ((memq 'cleanup whitespace-action)
  2210. (whitespace-cleanup))
  2211. ((memq 'report-on-bogus whitespace-action)
  2212. (whitespace-report nil t))))
  2213. (defun whitespace-write-file-hook ()
  2214. "Action to be taken when buffer is written.
  2215. It should be added buffer-locally to `write-file-functions'."
  2216. (cond ((memq 'auto-cleanup whitespace-action)
  2217. (whitespace-cleanup))
  2218. ((memq 'abort-on-bogus whitespace-action)
  2219. (when (whitespace-report nil t)
  2220. (error "Abort write due to whitespace problems in %s"
  2221. (buffer-name)))))
  2222. nil) ; continue hook processing
  2223. (defun whitespace-warn-read-only (msg)
  2224. "Warn if buffer is read-only."
  2225. (when (memq 'warn-if-read-only whitespace-action)
  2226. (message "Can't %s: %s is read-only" msg (buffer-name))))
  2227. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2228. (defun whitespace-unload-function ()
  2229. "Unload the whitespace library."
  2230. (global-whitespace-mode -1)
  2231. ;; be sure all local whitespace mode is turned off
  2232. (save-current-buffer
  2233. (dolist (buf (buffer-list))
  2234. (set-buffer buf)
  2235. (whitespace-mode -1)))
  2236. nil) ; continue standard unloading
  2237. (provide 'whitespace)
  2238. (run-hooks 'whitespace-load-hook)
  2239. ;;; whitespace.el ends here