whitespace.el 87 KB

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