old-whitespace.el 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Rajesh Vaidheeswarran <rv@gnu.org>
  4. ;; Keywords: convenience
  5. ;; Obsolete-since: 23.1
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; URL: http://www.dsmit.com/lisp/
  19. ;;
  20. ;; The whitespace library is intended to find and help fix five different types
  21. ;; of whitespace problems that commonly exist in source code.
  22. ;;
  23. ;; 1. Leading space (empty lines at the top of a file).
  24. ;; 2. Trailing space (empty lines at the end of a file).
  25. ;; 3. Indentation space (8 or more spaces at beginning of line, that should be
  26. ;; replaced with TABS).
  27. ;; 4. Spaces followed by a TAB. (Almost always, we never want that).
  28. ;; 5. Spaces or TABS at the end of a line.
  29. ;;
  30. ;; Whitespace errors are reported in a buffer, and on the modeline.
  31. ;;
  32. ;; Modeline will show a W:<x>!<y> to denote a particular type of whitespace,
  33. ;; where `x' and `y' can be one (or more) of:
  34. ;;
  35. ;; e - End-of-Line whitespace.
  36. ;; i - Indentation whitespace.
  37. ;; l - Leading whitespace.
  38. ;; s - Space followed by Tab.
  39. ;; t - Trailing whitespace.
  40. ;;
  41. ;; If any of the whitespace checks is turned off, the modeline will display a
  42. ;; !<y>.
  43. ;;
  44. ;; (since (3) is the most controversial one, here is the rationale: Most
  45. ;; terminal drivers and printer drivers have TAB configured or even
  46. ;; hardcoded to be 8 spaces. (Some of them allow configuration, but almost
  47. ;; always they default to 8.)
  48. ;;
  49. ;; Changing `tab-width' to other than 8 and editing will cause your code to
  50. ;; look different from within Emacs, and say, if you cat it or more it, or
  51. ;; even print it.
  52. ;;
  53. ;; Almost all the popular programming modes let you define an offset (like
  54. ;; c-basic-offset or perl-indent-level) to configure the offset, so you
  55. ;; should never have to set your `tab-width' to be other than 8 in all
  56. ;; these modes. In fact, with an indent level of say, 4, 2 TABS will cause
  57. ;; Emacs to replace your 8 spaces with one \t (try it). If vi users in
  58. ;; your office complain, tell them to use vim, which distinguishes between
  59. ;; tabstop and shiftwidth (vi equivalent of our offsets), and also ask them
  60. ;; to set smarttab.)
  61. ;;
  62. ;; All the above have caused (and will cause) unwanted codeline integration and
  63. ;; merge problems.
  64. ;;
  65. ;; whitespace.el will complain if it detects whitespaces on opening a file, and
  66. ;; warn you on closing a file also (in case you had inserted any
  67. ;; whitespaces during the process of your editing).
  68. ;;
  69. ;; Exported functions:
  70. ;;
  71. ;; `whitespace-buffer' - To check the current buffer for whitespace problems.
  72. ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
  73. ;; `whitespace-region' - To check between point and mark for whitespace
  74. ;; problems.
  75. ;; `whitespace-cleanup-region' - To cleanup all whitespaces between point
  76. ;; and mark in the current buffer.
  77. ;;; Code:
  78. (defvar whitespace-version "3.5" "Version of the whitespace library.")
  79. (defvar whitespace-all-buffer-files nil
  80. "An associated list of buffers and files checked for whitespace cleanliness.
  81. This is to enable periodic checking of whitespace cleanliness in the files
  82. visited by the buffers.")
  83. (defvar whitespace-rescan-timer nil
  84. "Timer object used to rescan the files in buffers that have been modified.")
  85. ;; Tell Emacs about this new kind of minor mode
  86. (defvar whitespace-mode nil
  87. "Non-nil when Whitespace mode (a minor mode) is enabled.")
  88. (make-variable-buffer-local 'whitespace-mode)
  89. (defvar whitespace-mode-line nil
  90. "String to display in the mode line for Whitespace mode.")
  91. (make-variable-buffer-local 'whitespace-mode-line)
  92. (defvar whitespace-check-buffer-leading nil
  93. "Test leading whitespace for file in current buffer if t.")
  94. (make-variable-buffer-local 'whitespace-check-buffer-leading)
  95. ;;;###autoload(put 'whitespace-check-buffer-leading 'safe-local-variable 'booleanp)
  96. (defvar whitespace-check-buffer-trailing nil
  97. "Test trailing whitespace for file in current buffer if t.")
  98. (make-variable-buffer-local 'whitespace-check-buffer-trailing)
  99. ;;;###autoload(put 'whitespace-check-buffer-trailing 'safe-local-variable 'booleanp)
  100. (defvar whitespace-check-buffer-indent nil
  101. "Test indentation whitespace for file in current buffer if t.")
  102. (make-variable-buffer-local 'whitespace-check-buffer-indent)
  103. ;;;###autoload(put 'whitespace-check-buffer-indent 'safe-local-variable 'booleanp)
  104. (defvar whitespace-check-buffer-spacetab nil
  105. "Test Space-followed-by-TABS whitespace for file in current buffer if t.")
  106. (make-variable-buffer-local 'whitespace-check-buffer-spacetab)
  107. ;;;###autoload(put 'whitespace-check-buffer-spacetab 'safe-local-variable 'booleanp)
  108. (defvar whitespace-check-buffer-ateol nil
  109. "Test end-of-line whitespace for file in current buffer if t.")
  110. (make-variable-buffer-local 'whitespace-check-buffer-ateol)
  111. ;;;###autoload(put 'whitespace-check-buffer-ateol 'safe-local-variable 'booleanp)
  112. (defvar whitespace-highlighted-space nil
  113. "The variable to store the extent to highlight.")
  114. (make-variable-buffer-local 'whitespace-highlighted-space)
  115. (defalias 'whitespace-make-overlay
  116. (if (featurep 'xemacs) 'make-extent 'make-overlay))
  117. (defalias 'whitespace-overlay-put
  118. (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
  119. (defalias 'whitespace-delete-overlay
  120. (if (featurep 'xemacs) 'delete-extent 'delete-overlay))
  121. (defalias 'whitespace-overlay-start
  122. (if (featurep 'xemacs) 'extent-start 'overlay-start))
  123. (defalias 'whitespace-overlay-end
  124. (if (featurep 'xemacs) 'extent-end 'overlay-end))
  125. (defalias 'whitespace-mode-line-update
  126. (if (featurep 'xemacs) 'redraw-modeline 'force-mode-line-update))
  127. (defgroup whitespace nil
  128. "Check for and fix five different types of whitespaces in source code."
  129. :version "21.1"
  130. :link '(emacs-commentary-link "whitespace.el")
  131. ;; Since XEmacs doesn't have a 'convenience group, use the next best group
  132. ;; which is 'editing?
  133. :group (if (featurep 'xemacs) 'editing 'convenience))
  134. (defcustom whitespace-check-leading-whitespace t
  135. "Flag to check leading whitespace. This is the global for the system.
  136. It can be overridden by setting a buffer local variable
  137. `whitespace-check-buffer-leading'."
  138. :type 'boolean
  139. :group 'whitespace)
  140. (defcustom whitespace-check-trailing-whitespace t
  141. "Flag to check trailing whitespace. This is the global for the system.
  142. It can be overridden by setting a buffer local variable
  143. `whitespace-check-buffer-trailing'."
  144. :type 'boolean
  145. :group 'whitespace)
  146. (defcustom whitespace-check-spacetab-whitespace t
  147. "Flag to check space followed by a TAB. This is the global for the system.
  148. It can be overridden by setting a buffer local variable
  149. `whitespace-check-buffer-spacetab'."
  150. :type 'boolean
  151. :group 'whitespace)
  152. (defcustom whitespace-spacetab-regexp "[ ]+\t"
  153. "Regexp to match one or more spaces followed by a TAB."
  154. :type 'regexp
  155. :group 'whitespace)
  156. (defcustom whitespace-check-indent-whitespace indent-tabs-mode
  157. "Flag to check indentation whitespace. This is the global for the system.
  158. It can be overridden by setting a buffer local variable
  159. `whitespace-check-buffer-indent'."
  160. :type 'boolean
  161. :group 'whitespace)
  162. (defcustom whitespace-indent-regexp "^\t*\\( \\)+"
  163. "Regexp to match multiples of eight spaces near line beginnings.
  164. The default value ignores leading TABs."
  165. :type 'regexp
  166. :group 'whitespace)
  167. (defcustom whitespace-check-ateol-whitespace t
  168. "Flag to check end-of-line whitespace. This is the global for the system.
  169. It can be overridden by setting a buffer local variable
  170. `whitespace-check-buffer-ateol'."
  171. :type 'boolean
  172. :group 'whitespace)
  173. (defcustom whitespace-ateol-regexp "[ \t]+$"
  174. "Regexp to match one or more TABs or spaces at line ends."
  175. :type 'regexp
  176. :group 'whitespace)
  177. (defcustom whitespace-errbuf "*Whitespace Errors*"
  178. "The name of the buffer where whitespace related messages will be logged."
  179. :type 'string
  180. :group 'whitespace)
  181. (defcustom whitespace-clean-msg "clean."
  182. "If non-nil, this message will be displayed after a whitespace check
  183. determines a file to be clean."
  184. :type 'string
  185. :group 'whitespace)
  186. (defcustom whitespace-abort-on-error nil
  187. "While writing a file, abort if the file is unclean.
  188. If `whitespace-auto-cleanup' is set, that takes precedence over
  189. this variable."
  190. :type 'boolean
  191. :group 'whitespace)
  192. (defcustom whitespace-auto-cleanup nil
  193. "Cleanup a buffer automatically on finding it whitespace unclean."
  194. :type 'boolean
  195. :group 'whitespace)
  196. (defcustom whitespace-silent nil
  197. "All whitespace errors will be shown only in the modeline when t.
  198. Note that setting this may cause all whitespaces introduced in a file to go
  199. unnoticed when the buffer is killed, unless the user visits the `*Whitespace
  200. Errors*' buffer before opening (or closing) another file."
  201. :type 'boolean
  202. :group 'whitespace)
  203. (defcustom whitespace-modes '(ada-mode asm-mode autoconf-mode awk-mode
  204. c-mode c++-mode cc-mode
  205. change-log-mode cperl-mode
  206. electric-nroff-mode emacs-lisp-mode
  207. f90-mode fortran-mode html-mode
  208. html3-mode java-mode jde-mode
  209. ksh-mode latex-mode LaTeX-mode
  210. lisp-mode m4-mode makefile-mode
  211. modula-2-mode nroff-mode objc-mode
  212. pascal-mode perl-mode prolog-mode
  213. python-mode scheme-mode sgml-mode
  214. sh-mode shell-script-mode simula-mode
  215. tcl-mode tex-mode texinfo-mode
  216. vrml-mode xml-mode)
  217. "Major modes in which we turn on whitespace checking.
  218. These are mostly programming and documentation modes. But you may add other
  219. modes that you want whitespaces checked in by adding something like the
  220. following to your `.emacs':
  221. \(setq whitespace-modes (cons 'my-mode (cons 'my-other-mode
  222. whitespace-modes))\)
  223. Or, alternately, you can use the Emacs `customize' command to set this."
  224. :type '(repeat symbol)
  225. :group 'whitespace)
  226. (defcustom whitespace-rescan-timer-time 600
  227. "Period in seconds to rescan modified buffers for whitespace creep.
  228. This is the period after which the timer will fire causing
  229. `whitespace-rescan-files-in-buffers' to check for whitespace creep in
  230. modified buffers.
  231. To disable timer scans, set this to zero."
  232. :type 'integer
  233. :group 'whitespace)
  234. (defcustom whitespace-display-in-modeline t
  235. "Display whitespace errors on the modeline."
  236. :type 'boolean
  237. :group 'whitespace)
  238. (defcustom whitespace-display-spaces-in-color t
  239. "Display the bogus whitespaces by coloring them with the face
  240. `whitespace-highlight'."
  241. :type 'boolean
  242. :group 'whitespace)
  243. (defgroup whitespace-faces nil
  244. "Faces used in whitespace."
  245. :prefix "whitespace-"
  246. :group 'whitespace
  247. :group 'faces)
  248. (defface whitespace-highlight '((((class color) (background light))
  249. (:background "green1"))
  250. (((class color) (background dark))
  251. (:background "sea green"))
  252. (((class grayscale mono)
  253. (background light))
  254. (:background "black"))
  255. (((class grayscale mono)
  256. (background dark))
  257. (:background "white")))
  258. "Face used for highlighting the bogus whitespaces that exist in the buffer."
  259. :group 'whitespace-faces)
  260. (define-obsolete-face-alias 'whitespace-highlight-face
  261. 'whitespace-highlight "22.1")
  262. (if (not (assoc 'whitespace-mode minor-mode-alist))
  263. (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
  264. minor-mode-alist)))
  265. (set-default 'whitespace-check-buffer-leading
  266. whitespace-check-leading-whitespace)
  267. (set-default 'whitespace-check-buffer-trailing
  268. whitespace-check-trailing-whitespace)
  269. (set-default 'whitespace-check-buffer-indent
  270. whitespace-check-indent-whitespace)
  271. (set-default 'whitespace-check-buffer-spacetab
  272. whitespace-check-spacetab-whitespace)
  273. (set-default 'whitespace-check-buffer-ateol
  274. whitespace-check-ateol-whitespace)
  275. (defun whitespace-check-whitespace-mode (&optional arg)
  276. "Test and set the whitespace-mode in qualifying buffers."
  277. (if (null whitespace-mode)
  278. (setq whitespace-mode
  279. (if (or arg (member major-mode whitespace-modes))
  280. t
  281. nil))))
  282. ;;;###autoload
  283. (defun whitespace-toggle-leading-check ()
  284. "Toggle the check for leading space in the local buffer."
  285. (interactive)
  286. (let ((current-val whitespace-check-buffer-leading))
  287. (setq whitespace-check-buffer-leading (not current-val))
  288. (message "Will%s check for leading space in buffer."
  289. (if whitespace-check-buffer-leading "" " not"))
  290. (if whitespace-check-buffer-leading (whitespace-buffer-leading))))
  291. ;;;###autoload
  292. (defun whitespace-toggle-trailing-check ()
  293. "Toggle the check for trailing space in the local buffer."
  294. (interactive)
  295. (let ((current-val whitespace-check-buffer-trailing))
  296. (setq whitespace-check-buffer-trailing (not current-val))
  297. (message "Will%s check for trailing space in buffer."
  298. (if whitespace-check-buffer-trailing "" " not"))
  299. (if whitespace-check-buffer-trailing (whitespace-buffer-trailing))))
  300. ;;;###autoload
  301. (defun whitespace-toggle-indent-check ()
  302. "Toggle the check for indentation space in the local buffer."
  303. (interactive)
  304. (let ((current-val whitespace-check-buffer-indent))
  305. (setq whitespace-check-buffer-indent (not current-val))
  306. (message "Will%s check for indentation space in buffer."
  307. (if whitespace-check-buffer-indent "" " not"))
  308. (if whitespace-check-buffer-indent
  309. (whitespace-buffer-search whitespace-indent-regexp))))
  310. ;;;###autoload
  311. (defun whitespace-toggle-spacetab-check ()
  312. "Toggle the check for space-followed-by-TABs in the local buffer."
  313. (interactive)
  314. (let ((current-val whitespace-check-buffer-spacetab))
  315. (setq whitespace-check-buffer-spacetab (not current-val))
  316. (message "Will%s check for space-followed-by-TABs in buffer."
  317. (if whitespace-check-buffer-spacetab "" " not"))
  318. (if whitespace-check-buffer-spacetab
  319. (whitespace-buffer-search whitespace-spacetab-regexp))))
  320. ;;;###autoload
  321. (defun whitespace-toggle-ateol-check ()
  322. "Toggle the check for end-of-line space in the local buffer."
  323. (interactive)
  324. (let ((current-val whitespace-check-buffer-ateol))
  325. (setq whitespace-check-buffer-ateol (not current-val))
  326. (message "Will%s check for end-of-line space in buffer."
  327. (if whitespace-check-buffer-ateol "" " not"))
  328. (if whitespace-check-buffer-ateol
  329. (whitespace-buffer-search whitespace-ateol-regexp))))
  330. ;;;###autoload
  331. (defun whitespace-buffer (&optional quiet)
  332. "Find five different types of white spaces in buffer.
  333. These are:
  334. 1. Leading space \(empty lines at the top of a file\).
  335. 2. Trailing space \(empty lines at the end of a file\).
  336. 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
  337. 4. Spaces followed by a TAB. \(Almost always, we never want that\).
  338. 5. Spaces or TABS at the end of a line.
  339. Check for whitespace only if this buffer really contains a non-empty file
  340. and:
  341. 1. the major mode is one of the whitespace-modes, or
  342. 2. `whitespace-buffer' was explicitly called with a prefix argument."
  343. (interactive)
  344. (let ((whitespace-error nil))
  345. (whitespace-check-whitespace-mode current-prefix-arg)
  346. (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
  347. (progn
  348. (whitespace-check-buffer-list (buffer-name) buffer-file-name)
  349. (whitespace-tickle-timer)
  350. (overlay-recenter (point-max))
  351. (remove-overlays nil nil 'face 'whitespace-highlight)
  352. (if whitespace-auto-cleanup
  353. (if buffer-read-only
  354. (if (not quiet)
  355. (message "Can't cleanup: %s is read-only" (buffer-name)))
  356. (whitespace-cleanup-internal))
  357. (let ((whitespace-leading (if whitespace-check-buffer-leading
  358. (whitespace-buffer-leading)
  359. nil))
  360. (whitespace-trailing (if whitespace-check-buffer-trailing
  361. (whitespace-buffer-trailing)
  362. nil))
  363. (whitespace-indent (if whitespace-check-buffer-indent
  364. (whitespace-buffer-search
  365. whitespace-indent-regexp)
  366. nil))
  367. (whitespace-spacetab (if whitespace-check-buffer-spacetab
  368. (whitespace-buffer-search
  369. whitespace-spacetab-regexp)
  370. nil))
  371. (whitespace-ateol (if whitespace-check-buffer-ateol
  372. (whitespace-buffer-search
  373. whitespace-ateol-regexp)
  374. nil))
  375. (whitespace-errmsg nil)
  376. (whitespace-filename buffer-file-name)
  377. (whitespace-this-modeline ""))
  378. ;; Now let's complain if we found any of the above.
  379. (setq whitespace-error (or whitespace-leading whitespace-indent
  380. whitespace-spacetab whitespace-ateol
  381. whitespace-trailing))
  382. (if whitespace-error
  383. (progn
  384. (setq whitespace-errmsg
  385. (concat whitespace-filename " contains:\n"
  386. (if whitespace-leading
  387. "Leading whitespace\n")
  388. (if whitespace-indent
  389. (concat "Indentation whitespace"
  390. whitespace-indent "\n"))
  391. (if whitespace-spacetab
  392. (concat "Space followed by Tab"
  393. whitespace-spacetab "\n"))
  394. (if whitespace-ateol
  395. (concat "End-of-line whitespace"
  396. whitespace-ateol "\n"))
  397. (if whitespace-trailing
  398. "Trailing whitespace\n")
  399. "\ntype `M-x whitespace-cleanup' to "
  400. "cleanup the file."))
  401. (setq whitespace-this-modeline
  402. (concat (if whitespace-ateol "e")
  403. (if whitespace-indent "i")
  404. (if whitespace-leading "l")
  405. (if whitespace-spacetab "s")
  406. (if whitespace-trailing "t")))))
  407. (whitespace-update-modeline whitespace-this-modeline)
  408. (if (get-buffer whitespace-errbuf)
  409. (kill-buffer whitespace-errbuf))
  410. (with-current-buffer (get-buffer-create whitespace-errbuf)
  411. (if whitespace-errmsg
  412. (progn
  413. (insert whitespace-errmsg)
  414. (if (not (or quiet whitespace-silent))
  415. (display-buffer (current-buffer) t))
  416. (if (not quiet)
  417. (message "Whitespaces: [%s%s] in %s"
  418. whitespace-this-modeline
  419. (let ((whitespace-unchecked
  420. (whitespace-unchecked-whitespaces)))
  421. (if whitespace-unchecked
  422. (concat "!" whitespace-unchecked)
  423. ""))
  424. whitespace-filename)))
  425. (if (and (not quiet) (not (equal whitespace-clean-msg "")))
  426. (message "%s %s" whitespace-filename
  427. whitespace-clean-msg))))))))
  428. whitespace-error))
  429. ;;;###autoload
  430. (defun whitespace-region (s e)
  431. "Check the region for whitespace errors."
  432. (interactive "r")
  433. (save-excursion
  434. (save-restriction
  435. (narrow-to-region s e)
  436. (whitespace-buffer))))
  437. ;;;###autoload
  438. (defun whitespace-cleanup ()
  439. "Cleanup the five different kinds of whitespace problems.
  440. It normally applies to the whole buffer, but in Transient Mark mode
  441. when the mark is active it applies to the region.
  442. See `whitespace-buffer' docstring for a summary of the problems."
  443. (interactive)
  444. (if (and transient-mark-mode mark-active)
  445. (whitespace-cleanup-region (region-beginning) (region-end))
  446. (whitespace-cleanup-internal)))
  447. (defun whitespace-cleanup-internal (&optional region-only)
  448. ;; If this buffer really contains a file, then run, else quit.
  449. (whitespace-check-whitespace-mode current-prefix-arg)
  450. (if (and buffer-file-name whitespace-mode)
  451. (let ((whitespace-any nil)
  452. (whitespace-tabwidth 8)
  453. (whitespace-tabwidth-saved tab-width))
  454. ;; since all printable TABS should be 8, irrespective of how
  455. ;; they are displayed.
  456. (setq tab-width whitespace-tabwidth)
  457. (if (and whitespace-check-buffer-leading
  458. (whitespace-buffer-leading))
  459. (progn
  460. (whitespace-buffer-leading-cleanup)
  461. (setq whitespace-any t)))
  462. (if (and whitespace-check-buffer-trailing
  463. (whitespace-buffer-trailing))
  464. (progn
  465. (whitespace-buffer-trailing-cleanup)
  466. (setq whitespace-any t)))
  467. (if (and whitespace-check-buffer-indent
  468. (whitespace-buffer-search whitespace-indent-regexp))
  469. (progn
  470. (whitespace-indent-cleanup)
  471. (setq whitespace-any t)))
  472. (if (and whitespace-check-buffer-spacetab
  473. (whitespace-buffer-search whitespace-spacetab-regexp))
  474. (progn
  475. (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
  476. (setq whitespace-any t)))
  477. (if (and whitespace-check-buffer-ateol
  478. (whitespace-buffer-search whitespace-ateol-regexp))
  479. (progn
  480. (whitespace-buffer-cleanup whitespace-ateol-regexp "")
  481. (setq whitespace-any t)))
  482. ;; Call this recursively till everything is taken care of
  483. (if whitespace-any
  484. (whitespace-cleanup-internal region-only)
  485. ;; if we are done, talk to the user
  486. (progn
  487. (unless whitespace-silent
  488. (if region-only
  489. (message "The region is now clean")
  490. (message "%s is now clean" buffer-file-name)))
  491. (whitespace-update-modeline)))
  492. (setq tab-width whitespace-tabwidth-saved))))
  493. ;;;###autoload
  494. (defun whitespace-cleanup-region (s e)
  495. "Whitespace cleanup on the region."
  496. (interactive "r")
  497. (save-excursion
  498. (save-restriction
  499. (narrow-to-region s e)
  500. (whitespace-cleanup-internal t))
  501. (whitespace-buffer t)))
  502. (defun whitespace-buffer-leading ()
  503. "Return t if the current buffer has leading newline characters.
  504. If highlighting is enabled, highlight these characters."
  505. (save-excursion
  506. (goto-char (point-min))
  507. (skip-chars-forward "\n")
  508. (unless (bobp)
  509. (whitespace-highlight-the-space (point-min) (point))
  510. t)))
  511. (defun whitespace-buffer-leading-cleanup ()
  512. "Remove any leading newline characters from current buffer."
  513. (save-excursion
  514. (goto-char (point-min))
  515. (skip-chars-forward "\n")
  516. (delete-region (point-min) (point))))
  517. (defun whitespace-buffer-trailing ()
  518. "Return t if the current buffer has extra trailing newline characters.
  519. If highlighting is enabled, highlight these characters."
  520. (save-excursion
  521. (goto-char (point-max))
  522. (skip-chars-backward "\n")
  523. (forward-line)
  524. (unless (eobp)
  525. (whitespace-highlight-the-space (point) (point-max))
  526. t)))
  527. (defun whitespace-buffer-trailing-cleanup ()
  528. "Remove extra trailing newline characters from current buffer."
  529. (save-excursion
  530. (goto-char (point-max))
  531. (skip-chars-backward "\n")
  532. (unless (eobp)
  533. (forward-line)
  534. (delete-region (point) (point-max)))))
  535. (defun whitespace-buffer-search (regexp)
  536. "Search for any given whitespace REGEXP."
  537. (with-local-quit
  538. (let (whitespace-retval)
  539. (save-excursion
  540. (goto-char (point-min))
  541. (while (re-search-forward regexp nil t)
  542. (whitespace-highlight-the-space (match-beginning 0) (match-end 0))
  543. (push (match-beginning 0) whitespace-retval)))
  544. (when whitespace-retval
  545. (format " %s" (nreverse whitespace-retval))))))
  546. (defun whitespace-buffer-cleanup (regexp newregexp)
  547. "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
  548. (save-excursion
  549. (goto-char (point-min))
  550. (while (re-search-forward regexp nil t)
  551. (replace-match newregexp))))
  552. (defun whitespace-indent-cleanup ()
  553. "Search for 8/more spaces at the start of a line and replace it with tabs."
  554. (save-excursion
  555. (goto-char (point-min))
  556. (while (re-search-forward whitespace-indent-regexp nil t)
  557. (let ((column (current-column))
  558. (indent-tabs-mode t))
  559. (delete-region (match-beginning 0) (point))
  560. (indent-to column)))))
  561. (defun whitespace-unchecked-whitespaces ()
  562. "Return the list of whitespaces whose testing has been suppressed."
  563. (let ((unchecked-spaces
  564. (concat (if (not whitespace-check-buffer-ateol) "e")
  565. (if (not whitespace-check-buffer-indent) "i")
  566. (if (not whitespace-check-buffer-leading) "l")
  567. (if (not whitespace-check-buffer-spacetab) "s")
  568. (if (not whitespace-check-buffer-trailing) "t"))))
  569. (if (not (equal unchecked-spaces ""))
  570. unchecked-spaces
  571. nil)))
  572. (defun whitespace-update-modeline (&optional whitespace-err)
  573. "Update modeline with whitespace errors.
  574. Also with whitespaces whose testing has been turned off."
  575. (if whitespace-display-in-modeline
  576. (progn
  577. (setq whitespace-mode-line nil)
  578. ;; Whitespace errors
  579. (if (and whitespace-err (not (equal whitespace-err "")))
  580. (setq whitespace-mode-line whitespace-err))
  581. ;; Whitespace suppressed errors
  582. (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
  583. (if whitespace-unchecked
  584. (setq whitespace-mode-line
  585. (concat whitespace-mode-line "!" whitespace-unchecked))))
  586. ;; Add the whitespace modeline prefix
  587. (setq whitespace-mode-line (if whitespace-mode-line
  588. (concat " W:" whitespace-mode-line)
  589. nil))
  590. (whitespace-mode-line-update))))
  591. (defun whitespace-highlight-the-space (b e)
  592. "Highlight the current line, unhighlighting a previously jumped to line."
  593. (if whitespace-display-spaces-in-color
  594. (let ((ol (whitespace-make-overlay b e)))
  595. (whitespace-overlay-put ol 'face 'whitespace-highlight))))
  596. (defun whitespace-unhighlight-the-space()
  597. "Unhighlight the currently highlight line."
  598. (if (and whitespace-display-spaces-in-color whitespace-highlighted-space)
  599. (progn
  600. (mapc 'whitespace-delete-overlay whitespace-highlighted-space)
  601. (setq whitespace-highlighted-space nil))))
  602. (defun whitespace-check-buffer-list (buf-name buf-file)
  603. "Add a buffer and its file to the whitespace monitor list.
  604. The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
  605. periodically for whitespace."
  606. (if (and whitespace-mode (not (member (list buf-file buf-name)
  607. whitespace-all-buffer-files)))
  608. (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
  609. (defun whitespace-tickle-timer ()
  610. "Tickle timer to periodically to scan qualifying files for whitespace creep.
  611. If timer is not set, then set it to scan the files in
  612. `whitespace-all-buffer-files' periodically (defined by
  613. `whitespace-rescan-timer-time') for whitespace creep."
  614. (if (and whitespace-rescan-timer-time
  615. (/= whitespace-rescan-timer-time 0)
  616. (not whitespace-rescan-timer))
  617. (setq whitespace-rescan-timer
  618. (add-timeout whitespace-rescan-timer-time
  619. 'whitespace-rescan-files-in-buffers nil
  620. whitespace-rescan-timer-time))))
  621. (defun whitespace-rescan-files-in-buffers (&optional arg)
  622. "Check monitored files for whitespace creep since last scan."
  623. (let ((whitespace-all-my-files whitespace-all-buffer-files)
  624. buffile bufname thiselt buf)
  625. (if (not whitespace-all-my-files)
  626. (progn
  627. (disable-timeout whitespace-rescan-timer)
  628. (setq whitespace-rescan-timer nil))
  629. (while whitespace-all-my-files
  630. (setq thiselt (car whitespace-all-my-files))
  631. (setq whitespace-all-my-files (cdr whitespace-all-my-files))
  632. (setq buffile (car thiselt))
  633. (setq bufname (cadr thiselt))
  634. (setq buf (get-buffer bufname))
  635. (if (buffer-live-p buf)
  636. (with-current-buffer bufname
  637. ;;(message "buffer %s live" bufname)
  638. (if whitespace-mode
  639. (progn
  640. ;;(message "checking for whitespace in %s" bufname)
  641. (if whitespace-auto-cleanup
  642. (progn
  643. ;;(message "cleaning up whitespace in %s" bufname)
  644. (whitespace-cleanup-internal))
  645. (progn
  646. ;;(message "whitespace-buffer %s." (buffer-name))
  647. (whitespace-buffer t))))
  648. ;;(message "Removing %s from refresh list" bufname)
  649. (whitespace-refresh-rescan-list buffile bufname)))
  650. ;;(message "Removing %s from refresh list" bufname)
  651. (whitespace-refresh-rescan-list buffile bufname))))))
  652. (defun whitespace-refresh-rescan-list (buffile bufname)
  653. "Refresh the list of files to be rescanned for whitespace creep."
  654. (if whitespace-all-buffer-files
  655. (setq whitespace-all-buffer-files
  656. (delete (list buffile bufname) whitespace-all-buffer-files))
  657. (when whitespace-rescan-timer
  658. (disable-timeout whitespace-rescan-timer)
  659. (setq whitespace-rescan-timer nil))))
  660. ;;;###autoload
  661. (defalias 'global-whitespace-mode 'whitespace-global-mode)
  662. ;;;###autoload
  663. (define-minor-mode whitespace-global-mode
  664. "Toggle using Whitespace mode in new buffers.
  665. With ARG, turn the mode on if ARG is positive, otherwise turn it off.
  666. When this mode is active, `whitespace-buffer' is added to
  667. `find-file-hook' and `kill-buffer-hook'."
  668. :global t
  669. :group 'whitespace
  670. (if whitespace-global-mode
  671. (progn
  672. (add-hook 'find-file-hook 'whitespace-buffer)
  673. (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
  674. (add-hook 'kill-buffer-hook 'whitespace-buffer))
  675. (remove-hook 'find-file-hook 'whitespace-buffer)
  676. (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
  677. (remove-hook 'kill-buffer-hook 'whitespace-buffer)))
  678. ;;;###autoload
  679. (defun whitespace-write-file-hook ()
  680. "Hook function to be called on the buffer when whitespace check is enabled.
  681. This is meant to be added buffer-locally to `write-file-functions'."
  682. (let ((werr nil))
  683. (if whitespace-auto-cleanup
  684. (whitespace-cleanup-internal)
  685. (setq werr (whitespace-buffer)))
  686. (if (and whitespace-abort-on-error werr)
  687. (error "Abort write due to whitespaces in %s"
  688. buffer-file-name)))
  689. nil)
  690. (defun whitespace-unload-function ()
  691. "Unload the whitespace library."
  692. (if (unintern "whitespace-unload-hook" obarray)
  693. ;; if whitespace-unload-hook is defined, let's get rid of it
  694. ;; and recursively call `unload-feature'
  695. (progn (unload-feature 'whitespace) t)
  696. ;; this only happens in the recursive call
  697. (whitespace-global-mode -1)
  698. (save-current-buffer
  699. (dolist (buf (buffer-list))
  700. (set-buffer buf)
  701. (remove-hook 'write-file-functions 'whitespace-write-file-hook t)))
  702. ;; continue standard unloading
  703. nil))
  704. (defun whitespace-unload-hook ()
  705. (remove-hook 'find-file-hook 'whitespace-buffer)
  706. (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
  707. (remove-hook 'kill-buffer-hook 'whitespace-buffer))
  708. (add-hook 'whitespace-unload-hook 'whitespace-unload-hook)
  709. (provide 'whitespace)
  710. ;;; whitespace.el ends here