Function: whitespace-cleanup-region
whitespace-cleanup-region is an autoloaded, interactive and
byte-compiled function defined in whitespace.el.gz.
Signature
(whitespace-cleanup-region START END)
Documentation
Cleanup some blank problems at region.
The problems cleaned up are:
1. tab-width or more SPACEs at beginning of line.
If whitespace-style includes the value indentation:
replace tab-width or more SPACEs at beginning of line by TABs,
if indent-tabs-mode(var)/indent-tabs-mode(fun) is non-nil; otherwise, replace TABs by
SPACEs.
If whitespace-style includes the value indentation::tab,
replace tab-width or more SPACEs at beginning of line by TABs.
If whitespace-style includes the value indentation::space,
replace TABs by SPACEs.
2. SPACEs before TAB.
If whitespace-style includes the value space-before-tab:
replace SPACEs by TABs, if indent-tabs-mode(var)/indent-tabs-mode(fun) is non-nil;
otherwise, replace TABs by SPACEs.
If whitespace-style includes the value
space-before-tab::tab, replace SPACEs by TABs.
If whitespace-style includes the value
space-before-tab::space, replace TABs by SPACEs.
3. SPACEs or TABs at end of line.
If whitespace-style includes the value trailing, remove
all SPACEs or TABs at end of line.
4. tab-width or more SPACEs after TAB.
If whitespace-style includes the value space-after-tab:
replace SPACEs by TABs, if indent-tabs-mode(var)/indent-tabs-mode(fun) is non-nil;
otherwise, replace TABs by SPACEs.
If whitespace-style includes the value
space-after-tab::tab, replace SPACEs by TABs.
If whitespace-style includes the value
space-after-tab::space, replace TABs by SPACEs.
5. missing newline at end of file.
If whitespace-style includes the value missing-newline-at-eof,
and the cleanup region includes the end of file, add a final newline
if it is not there already.
See whitespace-style, indent-tabs-mode(var)/indent-tabs-mode(fun) and tab-width for
documentation.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/whitespace.el.gz
;;;###autoload
(defun whitespace-cleanup-region (start end)
"Cleanup some blank problems at region.
The problems cleaned up are:
1. `tab-width' or more SPACEs at beginning of line.
If `whitespace-style' includes the value `indentation':
replace `tab-width' or more SPACEs at beginning of line by TABs,
if `indent-tabs-mode' is non-nil; otherwise, replace TABs by
SPACEs.
If `whitespace-style' includes the value `indentation::tab',
replace `tab-width' or more SPACEs at beginning of line by TABs.
If `whitespace-style' includes the value `indentation::space',
replace TABs by SPACEs.
2. SPACEs before TAB.
If `whitespace-style' includes the value `space-before-tab':
replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
otherwise, replace TABs by SPACEs.
If `whitespace-style' includes the value
`space-before-tab::tab', replace SPACEs by TABs.
If `whitespace-style' includes the value
`space-before-tab::space', replace TABs by SPACEs.
3. SPACEs or TABs at end of line.
If `whitespace-style' includes the value `trailing', remove
all SPACEs or TABs at end of line.
4. `tab-width' or more SPACEs after TAB.
If `whitespace-style' includes the value `space-after-tab':
replace SPACEs by TABs, if `indent-tabs-mode' is non-nil;
otherwise, replace TABs by SPACEs.
If `whitespace-style' includes the value
`space-after-tab::tab', replace SPACEs by TABs.
If `whitespace-style' includes the value
`space-after-tab::space', replace TABs by SPACEs.
5. missing newline at end of file.
If `whitespace-style' includes the value `missing-newline-at-eof',
and the cleanup region includes the end of file, add a final newline
if it is not there already.
See `whitespace-style', `indent-tabs-mode' and `tab-width' for
documentation."
(interactive "@r")
(if buffer-read-only
;; read-only buffer
(whitespace-warn-read-only "cleanup region")
;; non-read-only buffer
(let ((rstart (min start end))
(rend (copy-marker (max start end)))
overwrite-mode ; enforce no overwrite
tmp)
(save-excursion
;; PROBLEM 1: `tab-width' or more SPACEs at bol
(cond
;; ACTION: replace `tab-width' or more SPACEs at bol by TABs, if
;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
;; by SPACEs.
((memq 'indentation whitespace-style)
(let ((regexp (whitespace-indentation-regexp)))
(goto-char rstart)
(while (re-search-forward regexp rend t)
(setq tmp (current-indentation))
(goto-char (match-beginning 0))
(delete-horizontal-space)
(unless (eolp)
(indent-to tmp)))))
;; ACTION: replace `tab-width' or more SPACEs at bol by TABs.
((memq 'indentation::tab whitespace-style)
(whitespace-replace-action
'tabify rstart rend
(whitespace-indentation-regexp 'tab) 0))
;; ACTION: replace TABs by SPACEs.
((memq 'indentation::space whitespace-style)
(whitespace-replace-action
'untabify rstart rend
(whitespace-indentation-regexp 'space) 0)))
;; PROBLEM 3: SPACEs or TABs at eol
;; ACTION: remove all SPACEs or TABs at eol
(when (memq 'trailing whitespace-style)
(whitespace-replace-action
'delete-region rstart rend
whitespace-trailing-regexp 1))
;; PROBLEM 4: `tab-width' or more SPACEs after TAB
(cond
;; ACTION: replace `tab-width' or more SPACEs by TABs, if
;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
;; by SPACEs.
((memq 'space-after-tab whitespace-style)
(whitespace-replace-action
(if indent-tabs-mode 'tabify 'untabify)
rstart rend (whitespace-space-after-tab-regexp) 1))
;; ACTION: replace `tab-width' or more SPACEs by TABs.
((memq 'space-after-tab::tab whitespace-style)
(whitespace-replace-action
'tabify rstart rend
(whitespace-space-after-tab-regexp 'tab) 1))
;; ACTION: replace TABs by SPACEs.
((memq 'space-after-tab::space whitespace-style)
(whitespace-replace-action
'untabify rstart rend
(whitespace-space-after-tab-regexp 'space) 1)))
;; PROBLEM 2: SPACEs before TAB
(cond
;; ACTION: replace SPACEs before TAB by TABs, if
;; `indent-tabs-mode' is non-nil; otherwise, replace TABs
;; by SPACEs.
((memq 'space-before-tab whitespace-style)
(whitespace-replace-action
(if indent-tabs-mode 'tabify 'untabify)
rstart rend whitespace-space-before-tab-regexp
(if indent-tabs-mode 0 2)))
;; ACTION: replace SPACEs before TAB by TABs.
((memq 'space-before-tab::tab whitespace-style)
(whitespace-replace-action
'tabify rstart rend
whitespace-space-before-tab-regexp 0))
;; ACTION: replace TABs by SPACEs.
((memq 'space-before-tab::space whitespace-style)
(whitespace-replace-action
'untabify rstart rend
whitespace-space-before-tab-regexp 2)))
;; PROBLEM 5: missing newline at end of file
(and (memq 'missing-newline-at-eof whitespace-style)
(> (point-max) (point-min))
(= (point-max) (without-restriction (point-max)))
(/= (char-before (point-max)) ?\n)
(not (and (eq selective-display t)
(= (char-before (point-max)) ?\r)))
(goto-char (point-max))
(ignore-errors (insert "\n"))))
(set-marker rend nil)))) ; point marker to nowhere