Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Whitespace rules |
| 3 | * |
| 4 | * Copyright (c) 2007 Junio C Hamano |
| 5 | */ |
| 6 | |
| 7 | #include "cache.h" |
| 8 | #include "attr.h" |
| 9 | |
| 10 | static struct whitespace_rule { |
| 11 | const char *rule_name; |
| 12 | unsigned rule_bits; |
Junio C Hamano | 727c371 | 2010-04-03 00:37:00 +0100 | [diff] [blame] | 13 | unsigned loosens_error:1, |
| 14 | exclude_default:1; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 15 | } whitespace_rule_names[] = { |
Junio C Hamano | a437900 | 2009-06-21 02:35:18 -0700 | [diff] [blame] | 16 | { "trailing-space", WS_TRAILING_SPACE, 0 }, |
| 17 | { "space-before-tab", WS_SPACE_BEFORE_TAB, 0 }, |
| 18 | { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB, 0 }, |
| 19 | { "cr-at-eol", WS_CR_AT_EOL, 1 }, |
Junio C Hamano | afd9db4 | 2009-09-15 03:28:08 -0700 | [diff] [blame] | 20 | { "blank-at-eol", WS_BLANK_AT_EOL, 0 }, |
| 21 | { "blank-at-eof", WS_BLANK_AT_EOF, 0 }, |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 22 | { "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 }, |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | unsigned parse_whitespace_rule(const char *string) |
| 26 | { |
| 27 | unsigned rule = WS_DEFAULT_RULE; |
| 28 | |
| 29 | while (string) { |
| 30 | int i; |
| 31 | size_t len; |
| 32 | const char *ep; |
| 33 | int negated = 0; |
| 34 | |
| 35 | string = string + strspn(string, ", \t\n\r"); |
| 36 | ep = strchr(string, ','); |
| 37 | if (!ep) |
| 38 | len = strlen(string); |
| 39 | else |
| 40 | len = ep - string; |
| 41 | |
| 42 | if (*string == '-') { |
| 43 | negated = 1; |
| 44 | string++; |
| 45 | len--; |
| 46 | } |
| 47 | if (!len) |
| 48 | break; |
| 49 | for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) { |
| 50 | if (strncmp(whitespace_rule_names[i].rule_name, |
| 51 | string, len)) |
| 52 | continue; |
| 53 | if (negated) |
| 54 | rule &= ~whitespace_rule_names[i].rule_bits; |
| 55 | else |
| 56 | rule |= whitespace_rule_names[i].rule_bits; |
| 57 | break; |
| 58 | } |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 59 | if (strncmp(string, "tabwidth=", 9) == 0) { |
| 60 | unsigned tabwidth = atoi(string + 9); |
| 61 | if (0 < tabwidth && tabwidth < 0100) { |
| 62 | rule &= ~WS_TAB_WIDTH_MASK; |
| 63 | rule |= tabwidth; |
| 64 | } |
| 65 | else |
| 66 | warning("tabwidth %.*s out of range", |
| 67 | (int)(len - 9), string + 9); |
| 68 | } |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 69 | string = ep; |
| 70 | } |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 71 | |
| 72 | if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB) |
| 73 | die("cannot enforce both tab-in-indent and indent-with-non-tab"); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 74 | return rule; |
| 75 | } |
| 76 | |
| 77 | static void setup_whitespace_attr_check(struct git_attr_check *check) |
| 78 | { |
| 79 | static struct git_attr *attr_whitespace; |
| 80 | |
| 81 | if (!attr_whitespace) |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 82 | attr_whitespace = git_attr("whitespace"); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 83 | check[0].attr = attr_whitespace; |
| 84 | } |
| 85 | |
| 86 | unsigned whitespace_rule(const char *pathname) |
| 87 | { |
| 88 | struct git_attr_check attr_whitespace_rule; |
| 89 | |
| 90 | setup_whitespace_attr_check(&attr_whitespace_rule); |
| 91 | if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) { |
| 92 | const char *value; |
| 93 | |
| 94 | value = attr_whitespace_rule.value; |
| 95 | if (ATTR_TRUE(value)) { |
| 96 | /* true (whitespace) */ |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 97 | unsigned all_rule = ws_tab_width(whitespace_rule_cfg); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 98 | int i; |
| 99 | for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) |
Junio C Hamano | 727c371 | 2010-04-03 00:37:00 +0100 | [diff] [blame] | 100 | if (!whitespace_rule_names[i].loosens_error && |
| 101 | !whitespace_rule_names[i].exclude_default) |
Junio C Hamano | a437900 | 2009-06-21 02:35:18 -0700 | [diff] [blame] | 102 | all_rule |= whitespace_rule_names[i].rule_bits; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 103 | return all_rule; |
| 104 | } else if (ATTR_FALSE(value)) { |
| 105 | /* false (-whitespace) */ |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 106 | return ws_tab_width(whitespace_rule_cfg); |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 107 | } else if (ATTR_UNSET(value)) { |
| 108 | /* reset to default (!whitespace) */ |
| 109 | return whitespace_rule_cfg; |
| 110 | } else { |
| 111 | /* string */ |
| 112 | return parse_whitespace_rule(value); |
| 113 | } |
| 114 | } else { |
| 115 | return whitespace_rule_cfg; |
| 116 | } |
| 117 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 118 | |
| 119 | /* The returned string should be freed by the caller. */ |
| 120 | char *whitespace_error_string(unsigned ws) |
| 121 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 122 | struct strbuf err = STRBUF_INIT; |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 123 | if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE) |
Wincent Colaiuta | 420f4f0 | 2007-12-14 12:23:43 +0100 | [diff] [blame] | 124 | strbuf_addstr(&err, "trailing whitespace"); |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 125 | else { |
| 126 | if (ws & WS_BLANK_AT_EOL) |
| 127 | strbuf_addstr(&err, "trailing whitespace"); |
| 128 | if (ws & WS_BLANK_AT_EOF) { |
| 129 | if (err.len) |
| 130 | strbuf_addstr(&err, ", "); |
| 131 | strbuf_addstr(&err, "new blank line at EOF"); |
| 132 | } |
| 133 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 134 | if (ws & WS_SPACE_BEFORE_TAB) { |
| 135 | if (err.len) |
| 136 | strbuf_addstr(&err, ", "); |
Wincent Colaiuta | 420f4f0 | 2007-12-14 12:23:43 +0100 | [diff] [blame] | 137 | strbuf_addstr(&err, "space before tab in indent"); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 138 | } |
| 139 | if (ws & WS_INDENT_WITH_NON_TAB) { |
| 140 | if (err.len) |
| 141 | strbuf_addstr(&err, ", "); |
Wincent Colaiuta | 420f4f0 | 2007-12-14 12:23:43 +0100 | [diff] [blame] | 142 | strbuf_addstr(&err, "indent with spaces"); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 143 | } |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 144 | if (ws & WS_TAB_IN_INDENT) { |
| 145 | if (err.len) |
| 146 | strbuf_addstr(&err, ", "); |
| 147 | strbuf_addstr(&err, "tab in indent"); |
| 148 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 149 | return strbuf_detach(&err, NULL); |
| 150 | } |
| 151 | |
| 152 | /* If stream is non-NULL, emits the line after checking. */ |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 153 | static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule, |
| 154 | FILE *stream, const char *set, |
| 155 | const char *reset, const char *ws) |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 156 | { |
| 157 | unsigned result = 0; |
J. Bruce Fields | 954ecd4 | 2007-12-16 11:31:39 -0500 | [diff] [blame] | 158 | int written = 0; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 159 | int trailing_whitespace = -1; |
| 160 | int trailing_newline = 0; |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 161 | int trailing_carriage_return = 0; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 162 | int i; |
| 163 | |
| 164 | /* Logic is simpler if we temporarily ignore the trailing newline. */ |
| 165 | if (len > 0 && line[len - 1] == '\n') { |
| 166 | trailing_newline = 1; |
| 167 | len--; |
| 168 | } |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 169 | if ((ws_rule & WS_CR_AT_EOL) && |
| 170 | len > 0 && line[len - 1] == '\r') { |
| 171 | trailing_carriage_return = 1; |
| 172 | len--; |
| 173 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 174 | |
| 175 | /* Check for trailing whitespace. */ |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 176 | if (ws_rule & WS_BLANK_AT_EOL) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 177 | for (i = len - 1; i >= 0; i--) { |
| 178 | if (isspace(line[i])) { |
| 179 | trailing_whitespace = i; |
Junio C Hamano | aeb84b0 | 2009-09-05 22:21:17 -0700 | [diff] [blame] | 180 | result |= WS_BLANK_AT_EOL; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 181 | } |
| 182 | else |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
Kevin Ballard | cfd1a98 | 2010-10-20 15:17:26 -0700 | [diff] [blame] | 187 | if (trailing_whitespace == -1) |
| 188 | trailing_whitespace = len; |
| 189 | |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 190 | /* Check indentation */ |
Kevin Ballard | cfd1a98 | 2010-10-20 15:17:26 -0700 | [diff] [blame] | 191 | for (i = 0; i < trailing_whitespace; i++) { |
J. Bruce Fields | 9afa2d4 | 2007-12-16 11:31:40 -0500 | [diff] [blame] | 192 | if (line[i] == ' ') |
J. Bruce Fields | 1020999 | 2007-12-16 11:31:38 -0500 | [diff] [blame] | 193 | continue; |
J. Bruce Fields | 1020999 | 2007-12-16 11:31:38 -0500 | [diff] [blame] | 194 | if (line[i] != '\t') |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 195 | break; |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 196 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) { |
J. Bruce Fields | 1020999 | 2007-12-16 11:31:38 -0500 | [diff] [blame] | 197 | result |= WS_SPACE_BEFORE_TAB; |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 198 | if (stream) { |
| 199 | fputs(ws, stream); |
| 200 | fwrite(line + written, i - written, 1, stream); |
| 201 | fputs(reset, stream); |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 202 | fwrite(line + i, 1, 1, stream); |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 203 | } |
Chris Webb | 3e3ec2a | 2010-04-03 00:37:08 +0100 | [diff] [blame] | 204 | } else if (ws_rule & WS_TAB_IN_INDENT) { |
| 205 | result |= WS_TAB_IN_INDENT; |
| 206 | if (stream) { |
| 207 | fwrite(line + written, i - written, 1, stream); |
| 208 | fputs(ws, stream); |
| 209 | fwrite(line + i, 1, 1, stream); |
| 210 | fputs(reset, stream); |
| 211 | } |
| 212 | } else if (stream) { |
| 213 | fwrite(line + written, i - written + 1, 1, stream); |
| 214 | } |
J. Bruce Fields | 9afa2d4 | 2007-12-16 11:31:40 -0500 | [diff] [blame] | 215 | written = i + 1; |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* Check for indent using non-tab. */ |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 219 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 220 | result |= WS_INDENT_WITH_NON_TAB; |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 221 | if (stream) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 222 | fputs(ws, stream); |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 223 | fwrite(line + written, i - written, 1, stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 224 | fputs(reset, stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 225 | } |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 226 | written = i; |
| 227 | } |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 228 | |
J. Bruce Fields | ffe5688 | 2007-12-16 11:31:41 -0500 | [diff] [blame] | 229 | if (stream) { |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 230 | /* |
| 231 | * Now the rest of the line starts at "written". |
| 232 | * The non-highlighted part ends at "trailing_whitespace". |
| 233 | */ |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 234 | |
| 235 | /* Emit non-highlighted (middle) segment. */ |
J. Bruce Fields | 954ecd4 | 2007-12-16 11:31:39 -0500 | [diff] [blame] | 236 | if (trailing_whitespace - written > 0) { |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 237 | fputs(set, stream); |
J. Bruce Fields | 954ecd4 | 2007-12-16 11:31:39 -0500 | [diff] [blame] | 238 | fwrite(line + written, |
| 239 | trailing_whitespace - written, 1, stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 240 | fputs(reset, stream); |
| 241 | } |
| 242 | |
| 243 | /* Highlight errors in trailing whitespace. */ |
| 244 | if (trailing_whitespace != len) { |
| 245 | fputs(ws, stream); |
| 246 | fwrite(line + trailing_whitespace, |
| 247 | len - trailing_whitespace, 1, stream); |
| 248 | fputs(reset, stream); |
| 249 | } |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 250 | if (trailing_carriage_return) |
| 251 | fputc('\r', stream); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 252 | if (trailing_newline) |
| 253 | fputc('\n', stream); |
| 254 | } |
| 255 | return result; |
| 256 | } |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 257 | |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 258 | void ws_check_emit(const char *line, int len, unsigned ws_rule, |
| 259 | FILE *stream, const char *set, |
| 260 | const char *reset, const char *ws) |
| 261 | { |
| 262 | (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws); |
| 263 | } |
| 264 | |
| 265 | unsigned ws_check(const char *line, int len, unsigned ws_rule) |
| 266 | { |
| 267 | return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL); |
| 268 | } |
| 269 | |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 270 | int ws_blank_line(const char *line, int len, unsigned ws_rule) |
| 271 | { |
| 272 | /* |
| 273 | * We _might_ want to treat CR differently from other |
| 274 | * whitespace characters when ws_rule has WS_CR_AT_EOL, but |
| 275 | * for now we just use this stupid definition. |
| 276 | */ |
| 277 | while (len-- > 0) { |
| 278 | if (!isspace(*line)) |
| 279 | return 0; |
| 280 | line++; |
| 281 | } |
| 282 | return 1; |
| 283 | } |
| 284 | |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 285 | /* Copy the line onto the end of the strbuf while fixing whitespaces */ |
| 286 | void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule, int *error_count) |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 287 | { |
| 288 | /* |
| 289 | * len is number of bytes to be copied from src, starting |
| 290 | * at src. Typically src[len-1] is '\n', unless this is |
| 291 | * the incomplete last line. |
| 292 | */ |
| 293 | int i; |
| 294 | int add_nl_to_tail = 0; |
| 295 | int add_cr_to_tail = 0; |
| 296 | int fixed = 0; |
| 297 | int last_tab_in_indent = -1; |
| 298 | int last_space_in_indent = -1; |
| 299 | int need_fix_leading_space = 0; |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * Strip trailing whitespace |
| 303 | */ |
Junio C Hamano | afd9db4 | 2009-09-15 03:28:08 -0700 | [diff] [blame] | 304 | if (ws_rule & WS_BLANK_AT_EOL) { |
Junio C Hamano | 422a82f | 2009-07-25 01:29:20 -0700 | [diff] [blame] | 305 | if (0 < len && src[len - 1] == '\n') { |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 306 | add_nl_to_tail = 1; |
| 307 | len--; |
Junio C Hamano | 422a82f | 2009-07-25 01:29:20 -0700 | [diff] [blame] | 308 | if (0 < len && src[len - 1] == '\r') { |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 309 | add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL); |
| 310 | len--; |
| 311 | } |
| 312 | } |
| 313 | if (0 < len && isspace(src[len - 1])) { |
| 314 | while (0 < len && isspace(src[len-1])) |
| 315 | len--; |
| 316 | fixed = 1; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Check leading whitespaces (indent) |
| 322 | */ |
| 323 | for (i = 0; i < len; i++) { |
| 324 | char ch = src[i]; |
| 325 | if (ch == '\t') { |
| 326 | last_tab_in_indent = i; |
| 327 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && |
| 328 | 0 <= last_space_in_indent) |
| 329 | need_fix_leading_space = 1; |
| 330 | } else if (ch == ' ') { |
| 331 | last_space_in_indent = i; |
| 332 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 333 | ws_tab_width(ws_rule) <= i - last_tab_in_indent) |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 334 | need_fix_leading_space = 1; |
| 335 | } else |
| 336 | break; |
| 337 | } |
| 338 | |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 339 | if (need_fix_leading_space) { |
| 340 | /* Process indent ourselves */ |
| 341 | int consecutive_spaces = 0; |
| 342 | int last = last_tab_in_indent + 1; |
| 343 | |
| 344 | if (ws_rule & WS_INDENT_WITH_NON_TAB) { |
| 345 | /* have "last" point at one past the indent */ |
| 346 | if (last_tab_in_indent < last_space_in_indent) |
| 347 | last = last_space_in_indent + 1; |
| 348 | else |
| 349 | last = last_tab_in_indent + 1; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * between src[0..last-1], strip the funny spaces, |
| 354 | * updating them to tab as needed. |
| 355 | */ |
| 356 | for (i = 0; i < last; i++) { |
| 357 | char ch = src[i]; |
| 358 | if (ch != ' ') { |
| 359 | consecutive_spaces = 0; |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 360 | strbuf_addch(dst, ch); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 361 | } else { |
| 362 | consecutive_spaces++; |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 363 | if (consecutive_spaces == ws_tab_width(ws_rule)) { |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 364 | strbuf_addch(dst, '\t'); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 365 | consecutive_spaces = 0; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | while (0 < consecutive_spaces--) |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 370 | strbuf_addch(dst, ' '); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 371 | len -= last; |
| 372 | src += last; |
| 373 | fixed = 1; |
Chris Webb | 4e35c51 | 2010-04-03 00:37:30 +0100 | [diff] [blame] | 374 | } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) { |
| 375 | /* Expand tabs into spaces */ |
Johannes Sixt | d35711a | 2010-11-30 09:22:04 +0100 | [diff] [blame] | 376 | int start = dst->len; |
Chris Webb | 4e35c51 | 2010-04-03 00:37:30 +0100 | [diff] [blame] | 377 | int last = last_tab_in_indent + 1; |
| 378 | for (i = 0; i < last; i++) { |
| 379 | if (src[i] == '\t') |
| 380 | do { |
| 381 | strbuf_addch(dst, ' '); |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 382 | } while ((dst->len - start) % ws_tab_width(ws_rule)); |
Chris Webb | 4e35c51 | 2010-04-03 00:37:30 +0100 | [diff] [blame] | 383 | else |
| 384 | strbuf_addch(dst, src[i]); |
| 385 | } |
| 386 | len -= last; |
| 387 | src += last; |
| 388 | fixed = 1; |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 391 | strbuf_add(dst, src, len); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 392 | if (add_cr_to_tail) |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 393 | strbuf_addch(dst, '\r'); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 394 | if (add_nl_to_tail) |
Chris Webb | d511bd3 | 2010-04-03 00:37:23 +0100 | [diff] [blame] | 395 | strbuf_addch(dst, '\n'); |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 396 | if (fixed && error_count) |
| 397 | (*error_count)++; |
Junio C Hamano | fe3403c | 2008-02-23 16:59:16 -0800 | [diff] [blame] | 398 | } |