annotate lisp/whitespace.el @ 25582:e8428725fec3

Initial revision
author Dave Love <fx@gnu.org>
date Tue, 07 Sep 1999 11:28:47 +0000
parents
children c0e27d3ce645
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25582
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
1 ;;; whitespace.el --- Warn about and clean bogus whitespaces in the file.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
2
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1999 Free Software Foundation, Inc.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
4
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
5 ;; Author: Rajesh Vaidheeswarran <rv@dsmit.com>
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
6 ;; Keywords: convenience
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
7
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
9
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
13 ;; any later version.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
14
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
19
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
23 ;; Boston, MA 02111-1307, USA.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
24
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
25 ;;; Commentary:
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
26
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
27 ;; Whitespace.el URL: http://www.dsmit.com/lisp/
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
28
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
29 ;; Exported functions:
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
30
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
31 ;; `whitespace-buffer' - To check the current buffer for whitespace problems.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
32 ;; `whitespace-cleanup' - To cleanup all whitespaces in the current buffer.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
33
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
34 ;;; Code:
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
35
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
36 ;; add a hook to find-file-hooks and kill-buffer-hook
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
37 (add-hook 'find-file-hooks 'whitespace-buffer)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
38 (add-hook 'kill-buffer-hook 'whitespace-buffer)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
39
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
40 (defvar whitespace-version "1.9" "Version of the whitespace library.")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
41 (defvar whitespace-indent-regexp (concat "^\\( *\\) " " ")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
42 "Any 8 or more spaces that can be replaced with a TAB")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
43 (defvar whitespace-spacetab-regexp " \t" "A TAB followed by a space")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
44 (defvar whitespace-ateol-regexp "[ \t]$" "A TAB or a space at the EOL")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
45 (defvar whitespace-errbuf "*Whitespace Errors*"
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
46 "The buffer where the errors will appear")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
47
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
48 ;; Find out what type of emacs we are running in.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
49 (defvar whitespace-running-emacs (if (string-match "XEmacs\\|Lucid"
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
50 emacs-version) nil t)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
51 "If the current Emacs is not XEmacs, then, this is t.")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
52
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
53 ;; For users of Emacs 19.x, defgroup and defcustom are not defined.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
54
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
55 (if (< (string-to-int emacs-version) 20)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
56 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
57 (defmacro defgroup (sym memb doc &rest args)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
58 t)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
59 (defmacro defcustom (sym val doc &rest args)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
60 `(defvar ,sym ,val ,doc))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
61
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
62 (defgroup whitespace nil
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
63 "Check for five different types of whitespaces in source code.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
64
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
65 1. Leading space \(empty lines at the top of a file\).
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
66 2. Trailing space \(empty lines at the end of a file\).
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
67 3. Indentation space \(8 or more spaces at beginning of line, that should be
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
68 replaced with TABS\).
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
69 4. Spaces followed by a TAB. \(Almost always, we never want that\).
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
70 5. Spaces or TABS at the end of a line.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
71
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
72 Whitespace errors are reported in a buffer, and on the modeline.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
73
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
74 Modeline will show a W:<x> to denote a particular type of whitespace, where
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
75 `x' can be one \(or more\) of:
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
76
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
77 e - End-of-Line whitespace.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
78 i - Indentation whitespace.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
79 l - Leading whitespace.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
80 s - Space followed by Tab.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
81 t - Trailing whitespace.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
82
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
83 "
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
84 ;; Since XEmacs doesn't have a 'convenience group, use the next best group
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
85 ;; which is 'editing?
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
86 :group (if whitespace-running-emacs 'convenience 'editing))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
87
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
88 (defcustom whitespace-auto-cleanup nil
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
89 "Setting this will cleanup a buffer automatically on finding it whitespace
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
90 unclean.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
91
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
92 Use the emacs `customize' command to set this.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
93 "
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
94 :type 'boolean
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
95 :group 'whitespace)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
96
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
97 (defcustom whitespace-silent nil
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
98 "Setting this to t will cause the whitespace error buffer not to pop
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
99 up. All whitespace errors will be shown only in the modeline.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
100
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
101 Note that setting this may cause all whitespaces introduced in a file to go
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
102 unnoticed when the buffer is killed, unless the user visits the `*Whitespace
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
103 Errors*' buffer before opening \(or closing\) another file.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
104
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
105 Use the emacs `customize' command to set this.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
106 "
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
107 :type 'boolean
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
108 :group 'whitespace)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
109
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
110 (defcustom whitespace-modes '(ada-mode asm-mode autoconf-mode awk-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
111 c-mode c++-mode cc-mode cperl-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
112 electric-nroff-mode emacs-lisp-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
113 f90-mode fortran-mode html-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
114 html3-mode java-mode jde-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
115 ksh-mode latex-mode LaTeX-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
116 lisp-mode m4-mode makefile-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
117 modula-2-mode nroff-mode objc-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
118 pascal-mode perl-mode prolog-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
119 python-mode scheme-mode sgml-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
120 sh-mode shell-script-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
121 simula-mode tcl-mode tex-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
122 texinfo-mode vrml-mode xml-mode)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
123
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
124 "Modes that we check whitespace in. These are mostly programming and
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
125 documentation modes. But you may add other modes that you want whitespaces
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
126 checked in by adding something like the following to your `.emacs':
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
127
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
128 \(setq whitespace-modes \(cons 'my-mode \(cons 'my-other-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
129 whitespace-modes\)\)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
130
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
131 Or, alternately, you can use the Emacs `customize' command to set this.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
132 "
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
133 :group 'whitespace)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
134
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
135 (if whitespace-running-emacs (require 'timer))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
136
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
137 (defvar whitespace-all-buffer-files nil
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
138 "An associated list of all buffers
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
139 and theirs files checked for whitespace cleanliness. This is to enable
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
140 periodic checking of whitespace cleanliness in the files visited by the
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
141 buffers.")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
142
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
143 (defvar whitespace-rescan-timer nil
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
144 "Timer object that will be set to
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
145 rescan the files in Emacs buffers that have been modified.")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
146
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
147 (defcustom whitespace-rescan-timer-time 60
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
148 "seconds after which
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
149 `whitespace-rescan-files-in-buffers' will check for modified files in Emacs
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
150 buffers."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
151 :type 'integer
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
152 :group 'whitespace)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
153
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
154
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
155 ;; Tell Emacs about this new kind of minor mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
156 (make-variable-buffer-local 'whitespace-mode)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
157 (put 'whitespace-mode 'permanent-local nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
158 (set-default 'whitespace-mode nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
159
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
160 (make-variable-buffer-local 'whitespace-mode-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
161 (put 'whitespace-mode-line 'permanent-local nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
162 (set-default 'whitespace-mode-line nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
163
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
164 (if (not (assoc 'whitespace-mode minor-mode-alist))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
165 (setq minor-mode-alist (cons '(whitespace-mode whitespace-mode-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
166 minor-mode-alist)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
167
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
168 (defun whitespace-check-whitespace-mode (&optional arg)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
169 (if (null whitespace-mode)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
170 (setq whitespace-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
171 (if (or arg (member major-mode whitespace-modes))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
172 t
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
173 nil))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
174
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
175 (defun whitespace-buffer (&optional quiet)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
176 "Find five different types of white spaces in buffer:
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
177
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
178 1. Leading space \(empty lines at the top of a file\).
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
179 2. Trailing space \(empty lines at the end of a file\).
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
180 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
181 4. Spaces followed by a TAB. \(Almost always, we never want that\).
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
182 5. Spaces or TABS at the end of a line.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
183
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
184 Check for whitespace only if this buffer really contains a non-empty file
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
185 and:
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
186 1. the major mode is one of the whitespace-modes, or
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
187 2. `whitespace-buffer' was explicitly called with a prefix argument.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
188 "
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
189 (interactive)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
190 (whitespace-check-whitespace-mode current-prefix-arg)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
191 (if (and buffer-file-name (> (buffer-size) 0) whitespace-mode)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
192 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
193 (whitespace-check-buffer-list (buffer-name) buffer-file-name)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
194 (whitespace-tickle-timer)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
195 (if whitespace-auto-cleanup
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
196 (if (and (not quiet) buffer-read-only)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
197 (message "Can't Cleanup: %s is read-only." (buffer-name))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
198 (whitespace-cleanup))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
199 (let ((whitespace-leading (whitespace-buffer-leading))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
200 (whitespace-trailing (whitespace-buffer-trailing))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
201 (whitespace-indent (whitespace-buffer-search
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
202 whitespace-indent-regexp))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
203 (whitespace-spacetab (whitespace-buffer-search
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
204 whitespace-spacetab-regexp))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
205 (whitespace-ateol (whitespace-buffer-search
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
206 whitespace-ateol-regexp))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
207 (whitespace-errmsg nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
208 (whitespace-error nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
209 (whitespace-filename buffer-file-name)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
210 (whitespace-this-modeline ""))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
211
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
212 ;; Now let's complain if we found any of the above.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
213 (setq whitespace-error (or whitespace-leading whitespace-indent
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
214 whitespace-spacetab whitespace-ateol
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
215 whitespace-trailing))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
216
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
217 (if whitespace-error
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
218 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
219 (setq whitespace-errmsg
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
220 (concat whitespace-filename " contains:\n"
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
221 (if whitespace-leading "Leading whitespace\n")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
222 (if whitespace-indent
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
223 (concat "Indentation whitespace"
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
224 whitespace-indent "\n"))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
225 (if whitespace-spacetab
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
226 (concat "Space followed by Tab"
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
227 whitespace-spacetab "\n"))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
228 (if whitespace-ateol
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
229 (concat "End-of-line whitespace"
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
230 whitespace-ateol "\n"))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
231 (if whitespace-trailing
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
232 "Trailing whitespace.\n")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
233 "\ntype "
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
234 "`whitespace-cleanup' to cleanup the file."))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
235 (setq whitespace-this-modeline
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
236 (concat (if whitespace-ateol "e")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
237 (if whitespace-indent "i")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
238 (if whitespace-leading "l")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
239 (if whitespace-spacetab "s")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
240 (if whitespace-trailing "t")))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
241 (setq whitespace-mode-line
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
242 (concat " W:" whitespace-this-modeline))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
243 (whitespace-force-mode-line-update)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
244 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
245 (get-buffer-create whitespace-errbuf)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
246 (kill-buffer whitespace-errbuf)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
247 (get-buffer-create whitespace-errbuf)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
248 (set-buffer whitespace-errbuf)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
249 (if whitespace-errmsg
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
250 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
251 (insert whitespace-errmsg)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
252 (if (not (and quiet whitespace-silent))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
253 (display-buffer whitespace-errbuf t))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
254 (if (not quiet)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
255 (message "Whitespaces: [%s] in %s"
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
256 whitespace-this-modeline
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
257 whitespace-filename)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
258 (if (not quiet)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
259 (message "%s clean" whitespace-filename)))))))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
260
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
261 (defun whitespace-region (s e)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
262 "To check a region specified by point and mark for whitespace errors."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
263 (interactive "r")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
264 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
265 (save-restriction
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
266 (narrow-to-region s e)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
267 (whitespace-buffer))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
268
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
269 (defun whitespace-cleanup ()
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
270 "To cleanup the five different kinds of whitespace problems that
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
271 are defined in \\[whitespace-buffer]"
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
272 (interactive)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
273 ;; If this buffer really contains a file, then run, else quit.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
274 (whitespace-check-whitespace-mode current-prefix-arg)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
275 (if (and buffer-file-name whitespace-mode)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
276 (let ((whitespace-any nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
277 (whitespace-tabwith 8)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
278 (whitespace-tabwith-saved tab-width))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
279
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
280 ;; since all printable TABS should be 8, irrespective of how
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
281 ;; they are displayed.
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
282 (setq tab-width whitespace-tabwith)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
283
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
284 (if (whitespace-buffer-leading)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
285 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
286 (whitespace-buffer-leading-cleanup)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
287 (setq whitespace-any t)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
288
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
289 (if (whitespace-buffer-trailing)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
290 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
291 (whitespace-buffer-trailing-cleanup)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
292 (setq whitespace-any t)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
293
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
294 (if (whitespace-buffer-search whitespace-indent-regexp)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
295 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
296 (whitespace-indent-cleanup)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
297 (setq whitespace-any t)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
298
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
299 (if (whitespace-buffer-search whitespace-spacetab-regexp)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
300 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
301 (whitespace-buffer-cleanup whitespace-spacetab-regexp "\t")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
302 (setq whitespace-any t)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
303
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
304 (if (whitespace-buffer-search whitespace-ateol-regexp)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
305 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
306 (whitespace-buffer-cleanup whitespace-ateol-regexp "")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
307 (setq whitespace-any t)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
308
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
309 ;; Call this recursively till everything is taken care of
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
310 (if whitespace-any (whitespace-cleanup)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
311 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
312 (message "%s clean" buffer-file-name)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
313 (setq whitespace-mode-line nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
314 (whitespace-force-mode-line-update)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
315 (setq tab-width whitespace-tabwith-saved))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
316
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
317 (defun whitespace-cleanup-region (s e)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
318 "To do a whitespace cleanup on a region specified by point and mark."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
319 (interactive "r")
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
320 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
321 (save-restriction
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
322 (narrow-to-region s e)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
323 (whitespace-cleanup))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
324 (whitespace-buffer t)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
325
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
326 (defun whitespace-buffer-leading ()
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
327 "Check to see if there are any empty lines at the top of the file."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
328 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
329 (let ((pmin nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
330 (pmax nil))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
331 (goto-char (point-min))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
332 (beginning-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
333 (setq pmin (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
334 (end-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
335 (setq pmax (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
336 (if (equal pmin pmax)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
337 t
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
338 nil))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
339
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
340 (defun whitespace-buffer-leading-cleanup ()
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
341 "To remove any empty lines at the top of the file."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
342 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
343 (let ((pmin nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
344 (pmax nil))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
345 (goto-char (point-min))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
346 (beginning-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
347 (setq pmin (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
348 (end-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
349 (setq pmax (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
350 (if (equal pmin pmax)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
351 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
352 (kill-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
353 (whitespace-buffer-leading-cleanup))))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
354
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
355 (defun whitespace-buffer-trailing ()
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
356 "Check to see if are is more than one empty line at the bottom."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
357 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
358 (let ((pmin nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
359 (pmax nil))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
360 (goto-char (point-max))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
361 (beginning-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
362 (setq pmin (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
363 (end-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
364 (setq pmax (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
365 (if (equal pmin pmax)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
366 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
367 (goto-char (- (point) 1))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
368 (beginning-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
369 (setq pmin (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
370 (end-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
371 (setq pmax (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
372 (if (equal pmin pmax)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
373 t
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
374 nil))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
375 nil))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
376
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
377 (defun whitespace-buffer-trailing-cleanup ()
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
378 "Delete all the empty lines at the bottom."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
379 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
380 (let ((pmin nil)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
381 (pmax nil))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
382 (goto-char (point-max))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
383 (beginning-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
384 (setq pmin (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
385 (end-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
386 (setq pmax (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
387 (if (equal pmin pmax)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
388 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
389 (goto-char (1- pmin))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
390 (beginning-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
391 (setq pmin (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
392 (end-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
393 (setq pmax (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
394 (if (equal pmin pmax)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
395 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
396 (goto-char (1- (point-max)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
397 (beginning-of-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
398 (kill-line)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
399 (whitespace-buffer-trailing-cleanup))))))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
400
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
401 (defun whitespace-buffer-search (regexp)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
402 "Search for any given whitespace REGEXP."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
403 (let ((whitespace-retval ""))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
404 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
405 (goto-char (point-min))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
406 (while (re-search-forward regexp nil t)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
407 (setq whitespace-retval (format "%s %s " whitespace-retval
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
408 (match-beginning 0))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
409 (if (equal "" whitespace-retval)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
410 nil
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
411 whitespace-retval))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
412
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
413 (defun whitespace-buffer-cleanup (regexp newregexp)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
414 "Search for any given whitespace REGEXP and replace it with the NEWREGEXP."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
415 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
416 (goto-char (point-min))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
417 (while (re-search-forward regexp nil t)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
418 (replace-match newregexp))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
419
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
420 (defun whitespace-indent-cleanup ()
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
421 "Search for any 8 or more whitespaces at the beginning of a line and
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
422 replace it with tabs."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
423 (interactive)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
424 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
425 (goto-char (point-min))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
426 (while (re-search-forward whitespace-indent-regexp nil t)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
427 (let ((column (current-column))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
428 (indent-tabs-mode t))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
429 (delete-region (match-beginning 0) (point))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
430 (indent-to column)))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
431
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
432 ;; Force mode line updation for different Emacs versions
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
433 (defun whitespace-force-mode-line-update ()
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
434 "To Force the mode line update for different flavors of Emacs."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
435 (if whitespace-running-emacs
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
436 (force-mode-line-update) ; Emacs
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
437 (redraw-modeline))) ; XEmacs
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
438
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
439 (defun whitespace-check-buffer-list (buf-name buf-file)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
440 (if (and whitespace-mode (not (member (list buf-file buf-name)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
441 whitespace-all-buffer-files)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
442 (add-to-list 'whitespace-all-buffer-files (list buf-file buf-name))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
443
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
444 (defun whitespace-tickle-timer ()
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
445 (if (not whitespace-rescan-timer)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
446 (setq whitespace-rescan-timer
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
447 (if whitespace-running-emacs
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
448 (run-at-time nil whitespace-rescan-timer-time
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
449 'whitespace-rescan-files-in-buffers)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
450 (add-timeout whitespace-rescan-timer-time
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
451 'whitespace-rescan-files-in-buffers nil
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
452 whitespace-rescan-timer-time)))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
453
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
454 (defun whitespace-rescan-files-in-buffers (&optional arg)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
455 "Check to see if all the files that are whitespace clean are
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
456 actually clean still, if in buffers, or need rescaning."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
457 (let ((whitespace-all-my-files whitespace-all-buffer-files)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
458 buffile bufname thiselt buf)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
459 (if (not whitespace-all-my-files)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
460 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
461 (if whitespace-running-emacs
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
462 (cancel-timer whitespace-rescan-timer)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
463 (disable-timeout whitespace-rescan-timer))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
464 (setq whitespace-rescan-timer nil))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
465 (while whitespace-all-my-files
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
466 (setq thiselt (car whitespace-all-my-files))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
467 (setq whitespace-all-my-files (cdr whitespace-all-my-files))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
468 (setq buffile (car thiselt))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
469 (setq bufname (cadr thiselt))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
470 (setq buf (get-buffer bufname))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
471 (if (buffer-live-p buf)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
472 (save-excursion
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
473 ;;(message "buffer %s live" bufname)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
474 (set-buffer bufname)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
475 (if whitespace-mode
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
476 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
477 ;;(message "checking for whitespace in %s" bufname)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
478 (if whitespace-auto-cleanup
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
479 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
480 ;;(message "cleaning up whitespace in %s" bufname)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
481 (whitespace-cleanup))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
482 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
483 ;;(message "whitespace-buffer %s." (buffer-name))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
484 (whitespace-buffer t))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
485 ;;(message "Removing %s from refresh list" bufname)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
486 (whitespace-refresh-rescan-list buffile bufname)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
487 ;;(message "Removing %s from refresh list" bufname)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
488 (whitespace-refresh-rescan-list buffile bufname))))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
489
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
490 (defun whitespace-refresh-rescan-list (buffile bufname)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
491 "Refresh the list of files to be rescaned."
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
492 (if whitespace-all-buffer-files
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
493 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
494 (setq whitespace-all-buffer-files
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
495 (delete (list buffile bufname) whitespace-all-buffer-files)))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
496 (progn
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
497 (if (and whitespace-running-emacs (timerp whitespace-rescan-timer))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
498 (cancel-timer whitespace-rescan-timer))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
499 (if (and (not whitespace-running-emacs) whitespace-rescan-timer)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
500 (disable-timeout whitespace-rescan-timer))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
501 (if whitespace-rescan-timer
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
502 (setq whitespace-rescan-timer nil)))))
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
503
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
504 (provide 'whitespace)
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
505
e8428725fec3 Initial revision
Dave Love <fx@gnu.org>
parents:
diff changeset
506 ;;; whitespace.el ends here