old-whitespace.el 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. ;;; whitespace.el --- warn about and clean bogus whitespaces in the file
  2. ;; Copyright (C) 1999-2015 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 mode line.
  31. ;;
  32. ;; Mode line 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 mode line 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 mode line 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. (defface whitespace-highlight '((((class color) (background light))
  244. (:background "green1"))
  245. (((class color) (background dark))
  246. (:background "sea green"))
  247. (((class grayscale mono)
  248. (background light))
  249. (:background "black"))
  250. (((class grayscale mono)
  251. (background dark))
  252. (:background "white")))
  253. "Face used for highlighting the bogus whitespaces that exist in the buffer."
  254. :group 'whitespace)
  255. (define-obsolete-face-alias 'whitespace-highlight-face
  256. 'whitespace-highlight "22.1")
  257. (if (not (assoc 'whitespace-mode minor-mode-alist))
  258. (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
  259. minor-mode-alist)))
  260. (set-default 'whitespace-check-buffer-leading
  261. whitespace-check-leading-whitespace)
  262. (set-default 'whitespace-check-buffer-trailing
  263. whitespace-check-trailing-whitespace)
  264. (set-default 'whitespace-check-buffer-indent
  265. whitespace-check-indent-whitespace)
  266. (set-default 'whitespace-check-buffer-spacetab
  267. whitespace-check-spacetab-whitespace)
  268. (set-default 'whitespace-check-buffer-ateol
  269. whitespace-check-ateol-whitespace)
  270. (defun whitespace-check-whitespace-mode (&optional arg)
  271. "Test and set the whitespace-mode in qualifying buffers."
  272. (if (null whitespace-mode)
  273. (setq whitespace-mode
  274. (if (or arg (member major-mode whitespace-modes))
  275. t
  276. nil))))
  277. ;;;###autoload
  278. (defun whitespace-toggle-leading-check ()
  279. "Toggle the check for leading space in the local buffer."
  280. (interactive)
  281. (let ((current-val whitespace-check-buffer-leading))
  282. (setq whitespace-check-buffer-leading (not current-val))
  283. (message "Will%s check for leading space in buffer."
  284. (if whitespace-check-buffer-leading "" " not"))
  285. (if whitespace-check-buffer-leading (whitespace-buffer-leading))))
  286. ;;;###autoload
  287. (defun whitespace-toggle-trailing-check ()
  288. "Toggle the check for trailing space in the local buffer."
  289. (interactive)
  290. (let ((current-val whitespace-check-buffer-trailing))
  291. (setq whitespace-check-buffer-trailing (not current-val))
  292. (message "Will%s check for trailing space in buffer."
  293. (if whitespace-check-buffer-trailing "" " not"))
  294. (if whitespace-check-buffer-trailing (whitespace-buffer-trailing))))
  295. ;;;###autoload
  296. (defun whitespace-toggle-indent-check ()
  297. "Toggle the check for indentation space in the local buffer."
  298. (interactive)
  299. (let ((current-val whitespace-check-buffer-indent))
  300. (setq whitespace-check-buffer-indent (not current-val))
  301. (message "Will%s check for indentation space in buffer."
  302. (if whitespace-check-buffer-indent "" " not"))
  303. (if whitespace-check-buffer-indent
  304. (whitespace-buffer-search whitespace-indent-regexp))))
  305. ;;;###autoload
  306. (defun whitespace-toggle-spacetab-check ()
  307. "Toggle the check for space-followed-by-TABs in the local buffer."
  308. (interactive)
  309. (let ((current-val whitespace-check-buffer-spacetab))
  310. (setq whitespace-check-buffer-spacetab (not current-val))
  311. (message "Will%s check for space-followed-by-TABs in buffer."
  312. (if whitespace-check-buffer-spacetab "" " not"))
  313. (if whitespace-check-buffer-spacetab
  314. (whitespace-buffer-search whitespace-spacetab-regexp))))
  315. ;;;###autoload
  316. (defun whitespace-toggle-ateol-check ()
  317. "Toggle the check for end-of-line space in the local buffer."
  318. (interactive)
  319. (let ((current-val whitespace-check-buffer-ateol))
  320. (setq whitespace-check-buffer-ateol (not current-val))
  321. (message "Will%s check for end-of-line space in buffer."
  322. (if whitespace-check-buffer-ateol "" " not"))
  323. (if whitespace-check-buffer-ateol
  324. (whitespace-buffer-search whitespace-ateol-regexp))))
  325. ;;;###autoload
  326. (defun whitespace-buffer (&optional quiet)
  327. "Find five different types of white spaces in buffer.
  328. These are:
  329. 1. Leading space \(empty lines at the top of a file\).
  330. 2. Trailing space \(empty lines at the end of a file\).
  331. 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
  332. 4. Spaces followed by a TAB. \(Almost always, we never want that\).
  333. 5. Spaces or TABS at the end of a line.
  334. Check for whitespace only if this buffer really contains a non-empty file
  335. and:
  336. 1. the major mode is one of the whitespace-modes, or
  337. 2. `whitespace-buffer' was explicitly called with a prefix argument."
  338. (interactive)
  339. (let ((whitespace-error nil))
  340. (whitespace-check-whitespace-mode current-prefix-arg)
  341. (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
  342. (progn
  343. (whitespace-check-buffer-list (buffer-name) buffer-file-name)
  344. (whitespace-tickle-timer)
  345. (overlay-recenter (point-max))
  346. (remove-overlays nil nil 'face 'whitespace-highlight)
  347. (if whitespace-auto-cleanup
  348. (if buffer-read-only
  349. (if (not quiet)
  350. (message "Can't cleanup: %s is read-only" (buffer-name)))
  351. (whitespace-cleanup-internal))
  352. (let ((whitespace-leading (if whitespace-check-buffer-leading
  353. (whitespace-buffer-leading)
  354. nil))
  355. (whitespace-trailing (if whitespace-check-buffer-trailing
  356. (whitespace-buffer-trailing)
  357. nil))
  358. (whitespace-indent (if whitespace-check-buffer-indent
  359. (whitespace-buffer-search
  360. whitespace-indent-regexp)
  361. nil))
  362. (whitespace-spacetab (if whitespace-check-buffer-spacetab
  363. (whitespace-buffer-search
  364. whitespace-spacetab-regexp)
  365. nil))
  366. (whitespace-ateol (if whitespace-check-buffer-ateol
  367. (whitespace-buffer-search
  368. whitespace-ateol-regexp)
  369. nil))
  370. (whitespace-errmsg nil)
  371. (whitespace-filename buffer-file-name)
  372. (whitespace-this-modeline ""))
  373. ;; Now let's complain if we found any of the above.
  374. (setq whitespace-error (or whitespace-leading whitespace-indent
  375. whitespace-spacetab whitespace-ateol
  376. whitespace-trailing))
  377. (if whitespace-error
  378. (progn
  379. (setq whitespace-errmsg
  380. (concat whitespace-filename " contains:\n"
  381. (if whitespace-leading
  382. "Leading whitespace\n")
  383. (if whitespace-indent
  384. (concat "Indentation whitespace"
  385. whitespace-indent "\n"))
  386. (if whitespace-spacetab
  387. (concat "Space followed by Tab"
  388. whitespace-spacetab "\n"))
  389. (if whitespace-ateol
  390. (concat "End-of-line whitespace"
  391. whitespace-ateol "\n"))
  392. (if whitespace-trailing
  393. "Trailing whitespace\n")
  394. "\ntype `M-x whitespace-cleanup' to "
  395. "cleanup the file."))
  396. (setq whitespace-this-modeline
  397. (concat (if whitespace-ateol "e")
  398. (if whitespace-indent "i")
  399. (if whitespace-leading "l")
  400. (if whitespace-spacetab "s")
  401. (if whitespace-trailing "t")))))
  402. (whitespace-update-modeline whitespace-this-modeline)
  403. (if (get-buffer whitespace-errbuf)
  404. (kill-buffer whitespace-errbuf))
  405. (with-current-buffer (get-buffer-create whitespace-errbuf)
  406. (if whitespace-errmsg
  407. (progn
  408. (insert whitespace-errmsg)
  409. (if (not (or quiet whitespace-silent))
  410. (display-buffer (current-buffer) t))
  411. (if (not quiet)
  412. (message "Whitespaces: [%s%s] in %s"
  413. whitespace-this-modeline
  414. (let ((whitespace-unchecked
  415. (whitespace-unchecked-whitespaces)))
  416. (if whitespace-unchecked
  417. (concat "!" whitespace-unchecked)
  418. ""))
  419. whitespace-filename)))
  420. (if (and (not quiet) (not (equal whitespace-clean-msg "")))
  421. (message "%s %s" whitespace-filename
  422. whitespace-clean-msg))))))))
  423. whitespace-error))
  424. ;;;###autoload
  425. (defun whitespace-region (s e)
  426. "Check the region for whitespace errors."
  427. (interactive "r")
  428. (save-excursion
  429. (save-restriction
  430. (narrow-to-region s e)
  431. (whitespace-buffer))))
  432. ;;;###autoload
  433. (defun whitespace-cleanup ()
  434. "Cleanup the five different kinds of whitespace problems.
  435. It normally applies to the whole buffer, but in Transient Mark mode
  436. when the mark is active it applies to the region.
  437. See `whitespace-buffer' docstring for a summary of the problems."
  438. (interactive)
  439. (if (and transient-mark-mode mark-active)
  440. (whitespace-cleanup-region (region-beginning) (region-end))
  441. (whitespace-cleanup-internal)))
  442. (defun whitespace-cleanup-internal (&optional region-only)
  443. ;; If this buffer really contains a file, then run, else quit.
  444. (whitespace-check-whitespace-mode current-prefix-arg)
  445. (if (and buffer-file-name whitespace-mode)
  446. (let ((whitespace-any nil)
  447. (whitespace-tabwidth 8)
  448. (whitespace-tabwidth-saved tab-width))
  449. ;; since all printable TABS should be 8, irrespective of how
  450. ;; they are displayed.
  451. (setq tab-width whitespace-tabwidth)
  452. (if (and whitespace-check-buffer-leading
  453. (whitespace-buffer-leading))
  454. (progn
  455. (whitespace-buffer-leading-cleanup)
  456. (setq whitespace-any t)))
  457. (if (and whitespace-check-buffer-trailing
  458. (whitespace-buffer-trailing))
  459. (progn
  460. (whitespace-buffer-trailing-cleanup)
  461. (setq whitespace-any t)))
  462. (if (and whitespace-check-buffer-indent
  463. (whitespace-buffer-search whitespace-indent-regexp))
  464. (progn
  465. (whitespace-indent-cleanup)
  466. (setq whitespace-any t)))
  467. (if (and whitespace-check-buffer-spacetab
  468. (whitespace-buffer-search whitespace-spacetab-regexp))
  469. (progn
  470. (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
  471. (setq whitespace-any t)))
  472. (if (and whitespace-check-buffer-ateol
  473. (whitespace-buffer-search whitespace-ateol-regexp))
  474. (progn
  475. (whitespace-buffer-cleanup whitespace-ateol-regexp "")
  476. (setq whitespace-any t)))
  477. ;; Call this recursively till everything is taken care of
  478. (if whitespace-any
  479. (whitespace-cleanup-internal region-only)
  480. ;; if we are done, talk to the user
  481. (progn
  482. (unless whitespace-silent
  483. (if region-only
  484. (message "The region is now clean")
  485. (message "%s is now clean" buffer-file-name)))
  486. (whitespace-update-modeline)))
  487. (setq tab-width whitespace-tabwidth-saved))))
  488. ;;;###autoload
  489. (defun whitespace-cleanup-region (s e)
  490. "Whitespace cleanup on the region."
  491. (interactive "r")
  492. (save-excursion
  493. (save-restriction
  494. (narrow-to-region s e)
  495. (whitespace-cleanup-internal t))
  496. (whitespace-buffer t)))
  497. (defun whitespace-buffer-leading ()
  498. "Return t if the current buffer has leading newline characters.
  499. If highlighting is enabled, highlight these characters."
  500. (save-excursion
  501. (goto-char (point-min))
  502. (skip-chars-forward "\n")
  503. (unless (bobp)
  504. (whitespace-highlight-the-space (point-min) (point))
  505. t)))
  506. (defun whitespace-buffer-leading-cleanup ()
  507. "Remove any leading newline characters from current buffer."
  508. (save-excursion
  509. (goto-char (point-min))
  510. (skip-chars-forward "\n")
  511. (delete-region (point-min) (point))))
  512. (defun whitespace-buffer-trailing ()
  513. "Return t if the current buffer has extra trailing newline characters.
  514. If highlighting is enabled, highlight these characters."
  515. (save-excursion
  516. (goto-char (point-max))
  517. (skip-chars-backward "\n")
  518. (forward-line)
  519. (unless (eobp)
  520. (whitespace-highlight-the-space (point) (point-max))
  521. t)))
  522. (defun whitespace-buffer-trailing-cleanup ()
  523. "Remove extra trailing newline characters from current buffer."
  524. (save-excursion
  525. (goto-char (point-max))
  526. (skip-chars-backward "\n")
  527. (unless (eobp)
  528. (forward-line)
  529. (delete-region (point) (point-max)))))
  530. (defun whitespace-buffer-search (regexp)
  531. "Search for any given whitespace REGEXP."
  532. (with-local-quit
  533. (let (whitespace-retval)
  534. (save-excursion
  535. (goto-char (point-min))
  536. (while (re-search-forward regexp nil t)
  537. (whitespace-highlight-the-space (match-beginning 0) (match-end 0))
  538. (push (match-beginning 0) whitespace-retval)))
  539. (when whitespace-retval
  540. (format " %s" (nreverse whitespace-retval))))))
  541. (defun whitespace-buffer-cleanup (regexp newregexp)
  542. "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
  543. (save-excursion
  544. (goto-char (point-min))
  545. (while (re-search-forward regexp nil t)
  546. (replace-match newregexp))))
  547. (defun whitespace-indent-cleanup ()
  548. "Search for 8/more spaces at the start of a line and replace it with tabs."
  549. (save-excursion
  550. (goto-char (point-min))
  551. (while (re-search-forward whitespace-indent-regexp nil t)
  552. (let ((column (current-column))
  553. (indent-tabs-mode t))
  554. (delete-region (match-beginning 0) (point))
  555. (indent-to column)))))
  556. (defun whitespace-unchecked-whitespaces ()
  557. "Return the list of whitespaces whose testing has been suppressed."
  558. (let ((unchecked-spaces
  559. (concat (if (not whitespace-check-buffer-ateol) "e")
  560. (if (not whitespace-check-buffer-indent) "i")
  561. (if (not whitespace-check-buffer-leading) "l")
  562. (if (not whitespace-check-buffer-spacetab) "s")
  563. (if (not whitespace-check-buffer-trailing) "t"))))
  564. (if (not (equal unchecked-spaces ""))
  565. unchecked-spaces
  566. nil)))
  567. (defun whitespace-update-modeline (&optional whitespace-err)
  568. "Update mode line with whitespace errors.
  569. Also with whitespaces whose testing has been turned off."
  570. (if whitespace-display-in-modeline
  571. (progn
  572. (setq whitespace-mode-line nil)
  573. ;; Whitespace errors
  574. (if (and whitespace-err (not (equal whitespace-err "")))
  575. (setq whitespace-mode-line whitespace-err))
  576. ;; Whitespace suppressed errors
  577. (let ((whitespace-unchecked (whitespace-unchecked-whitespaces)))
  578. (if whitespace-unchecked
  579. (setq whitespace-mode-line
  580. (concat whitespace-mode-line "!" whitespace-unchecked))))
  581. ;; Add the whitespace modeline prefix
  582. (setq whitespace-mode-line (if whitespace-mode-line
  583. (concat " W:" whitespace-mode-line)
  584. nil))
  585. (whitespace-mode-line-update))))
  586. (defun whitespace-highlight-the-space (b e)
  587. "Highlight the current line, unhighlighting a previously jumped to line."
  588. (if whitespace-display-spaces-in-color
  589. (let ((ol (whitespace-make-overlay b e)))
  590. (whitespace-overlay-put ol 'face 'whitespace-highlight))))
  591. (defun whitespace-unhighlight-the-space()
  592. "Unhighlight the currently highlight line."
  593. (if (and whitespace-display-spaces-in-color whitespace-highlighted-space)
  594. (progn
  595. (mapc 'whitespace-delete-overlay whitespace-highlighted-space)
  596. (setq whitespace-highlighted-space nil))))
  597. (defun whitespace-check-buffer-list (buf-name buf-file)
  598. "Add a buffer and its file to the whitespace monitor list.
  599. The buffer named BUF-NAME and its associated file BUF-FILE are now monitored
  600. periodically for whitespace."
  601. (if (and whitespace-mode (not (member (list buf-file buf-name)
  602. whitespace-all-buffer-files)))
  603. (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
  604. (defun whitespace-tickle-timer ()
  605. "Tickle timer to periodically to scan qualifying files for whitespace creep.
  606. If timer is not set, then set it to scan the files in
  607. `whitespace-all-buffer-files' periodically (defined by
  608. `whitespace-rescan-timer-time') for whitespace creep."
  609. (if (and whitespace-rescan-timer-time
  610. (/= whitespace-rescan-timer-time 0)
  611. (not whitespace-rescan-timer))
  612. (setq whitespace-rescan-timer
  613. (add-timeout whitespace-rescan-timer-time
  614. 'whitespace-rescan-files-in-buffers nil
  615. whitespace-rescan-timer-time))))
  616. (defun whitespace-rescan-files-in-buffers (&optional arg)
  617. "Check monitored files for whitespace creep since last scan."
  618. (let ((whitespace-all-my-files whitespace-all-buffer-files)
  619. buffile bufname thiselt buf)
  620. (if (not whitespace-all-my-files)
  621. (progn
  622. (disable-timeout whitespace-rescan-timer)
  623. (setq whitespace-rescan-timer nil))
  624. (while whitespace-all-my-files
  625. (setq thiselt (car whitespace-all-my-files))
  626. (setq whitespace-all-my-files (cdr whitespace-all-my-files))
  627. (setq buffile (car thiselt))
  628. (setq bufname (cadr thiselt))
  629. (setq buf (get-buffer bufname))
  630. (if (buffer-live-p buf)
  631. (with-current-buffer bufname
  632. ;;(message "buffer %s live" bufname)
  633. (if whitespace-mode
  634. (progn
  635. ;;(message "checking for whitespace in %s" bufname)
  636. (if whitespace-auto-cleanup
  637. (progn
  638. ;;(message "cleaning up whitespace in %s" bufname)
  639. (whitespace-cleanup-internal))
  640. (progn
  641. ;;(message "whitespace-buffer %s." (buffer-name))
  642. (whitespace-buffer t))))
  643. ;;(message "Removing %s from refresh list" bufname)
  644. (whitespace-refresh-rescan-list buffile bufname)))
  645. ;;(message "Removing %s from refresh list" bufname)
  646. (whitespace-refresh-rescan-list buffile bufname))))))
  647. (defun whitespace-refresh-rescan-list (buffile bufname)
  648. "Refresh the list of files to be rescanned for whitespace creep."
  649. (if whitespace-all-buffer-files
  650. (setq whitespace-all-buffer-files
  651. (delete (list buffile bufname) whitespace-all-buffer-files))
  652. (when whitespace-rescan-timer
  653. (disable-timeout whitespace-rescan-timer)
  654. (setq whitespace-rescan-timer nil))))
  655. ;;;###autoload
  656. (defalias 'global-whitespace-mode 'whitespace-global-mode)
  657. ;;;###autoload
  658. (define-minor-mode whitespace-global-mode
  659. "Toggle using Whitespace mode in new buffers.
  660. With ARG, turn the mode on if ARG is positive, otherwise turn it off.
  661. When this mode is active, `whitespace-buffer' is added to
  662. `find-file-hook' and `kill-buffer-hook'."
  663. :global t
  664. :group 'whitespace
  665. (if whitespace-global-mode
  666. (progn
  667. (add-hook 'find-file-hook 'whitespace-buffer)
  668. (add-hook 'write-file-functions 'whitespace-write-file-hook nil t)
  669. (add-hook 'kill-buffer-hook 'whitespace-buffer))
  670. (remove-hook 'find-file-hook 'whitespace-buffer)
  671. (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
  672. (remove-hook 'kill-buffer-hook 'whitespace-buffer)))
  673. ;;;###autoload
  674. (defun whitespace-write-file-hook ()
  675. "Hook function to be called on the buffer when whitespace check is enabled.
  676. This is meant to be added buffer-locally to `write-file-functions'."
  677. (let ((werr nil))
  678. (if whitespace-auto-cleanup
  679. (whitespace-cleanup-internal)
  680. (setq werr (whitespace-buffer)))
  681. (if (and whitespace-abort-on-error werr)
  682. (error "Abort write due to whitespaces in %s"
  683. buffer-file-name)))
  684. nil)
  685. (defun whitespace-unload-function ()
  686. "Unload the whitespace library."
  687. (if (unintern "whitespace-unload-hook" obarray)
  688. ;; if whitespace-unload-hook is defined, let's get rid of it
  689. ;; and recursively call `unload-feature'
  690. (progn (unload-feature 'whitespace) t)
  691. ;; this only happens in the recursive call
  692. (whitespace-global-mode -1)
  693. (save-current-buffer
  694. (dolist (buf (buffer-list))
  695. (set-buffer buf)
  696. (remove-hook 'write-file-functions 'whitespace-write-file-hook t)))
  697. ;; continue standard unloading
  698. nil))
  699. (defun whitespace-unload-hook ()
  700. (remove-hook 'find-file-hook 'whitespace-buffer)
  701. (remove-hook 'write-file-functions 'whitespace-write-file-hook t)
  702. (remove-hook 'kill-buffer-hook 'whitespace-buffer))
  703. (add-hook 'whitespace-unload-hook 'whitespace-unload-hook)
  704. (provide 'whitespace)
  705. ;;; whitespace.el ends here