diff options
author | Andrea Canciani <ranma42@gmail.com> | 2011-08-06 08:31:46 +0200 |
---|---|---|
committer | Andrea Canciani <ranma42@gmail.com> | 2011-08-06 08:31:46 +0200 |
commit | 3ce65b423b71ce6ede64c9cc7f6f602697a151e8 (patch) | |
tree | 74b693e13c6c026e2d6ff4fc930c81fa3f731194 | |
parent | c856d534398ebf2bf9113380f887a3cf136ec040 (diff) |
sed: Add script to enforce whitespace guidelines
Add a script which cleans up trailing whitespace and normalizes tabs
and spaces in code lines.
-rw-r--r-- | cleanup-whitespace.sed | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/cleanup-whitespace.sed b/cleanup-whitespace.sed new file mode 100644 index 0000000..cfbf003 --- /dev/null +++ b/cleanup-whitespace.sed @@ -0,0 +1,53 @@ +# only modify new lines +/^+/ { + +# remove diff-style + line prefix +s/^+// + +# remove trailing whitespace +s/[[:space:]]*$// + +# replace 8 spaces at the beginning of a line with a single tab +s/^ / / + +# remove 1-7 spaces at the beginning of a line if they are followed by +# a tab (which would hide them in the resulting layout) +s/^ \{1,7\} / / + +:spacestotab +# replace 8 tab-aligned spaces with a single tab +s/ / / +t spacestotab + +:removespaces +# remove 1-7 tab-aligned spaces if they are followed by a tab (which +# would hide them in the resulting layout) +s/ \{1,7\} / / +t removespaces + +:tabtospaces +# replace non-leading tabs with spaces (except for trailing tabs in +# macros) to make reindentation easier +s/^\( *\([^ ]\{8\}\)*[^ ]\{8\}\) \(.*[^\\]\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{1\}\) \(.*[^\\]\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{2\}\) \(.*[^\\]\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{3\}\) \(.*[^\\]\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{4\}\) \(.*[^\\]\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{5\}\) \(.*[^\\]\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{6\}\) \(.*[^\\]\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{7\}\) \(.*[^\\]\)$/\1 \3/ + +s/^\( *\([^ ]\{8\}\)*[^ ]\{8\}\) \(.*[^ ].*\\\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{1\}\) \(.*[^ ].*\\\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{2\}\) \(.*[^ ].*\\\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{3\}\) \(.*[^ ].*\\\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{4\}\) \(.*[^ ].*\\\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{5\}\) \(.*[^ ].*\\\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{6\}\) \(.*[^ ].*\\\)$/\1 \3/ +s/^\( *\([^ ]\{8\}\)*[^ ]\{7\}\) \(.*[^ ].*\\\)$/\1 \3/ + +t tabtospaces + +# restore diff-style + line prefix +s/^/+/ +} |