whitespace.el 90 KB

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