summaryrefslogtreecommitdiff
path: root/ws.c
diff options
context:
space:
mode:
Diffstat (limited to 'ws.c')
-rw-r--r--ws.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/ws.c b/ws.c
index 7302f8f..9fb9b14 100644
--- a/ws.c
+++ b/ws.c
@@ -56,6 +56,16 @@ unsigned parse_whitespace_rule(const char *string)
rule |= whitespace_rule_names[i].rule_bits;
break;
}
+ if (strncmp(string, "tabwidth=", 9) == 0) {
+ unsigned tabwidth = atoi(string + 9);
+ if (0 < tabwidth && tabwidth < 0100) {
+ rule &= ~WS_TAB_WIDTH_MASK;
+ rule |= tabwidth;
+ }
+ else
+ warning("tabwidth %.*s out of range",
+ (int)(len - 9), string + 9);
+ }
string = ep;
}
@@ -84,7 +94,7 @@ unsigned whitespace_rule(const char *pathname)
value = attr_whitespace_rule.value;
if (ATTR_TRUE(value)) {
/* true (whitespace) */
- unsigned all_rule = 0;
+ unsigned all_rule = ws_tab_width(whitespace_rule_cfg);
int i;
for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
if (!whitespace_rule_names[i].loosens_error &&
@@ -93,7 +103,7 @@ unsigned whitespace_rule(const char *pathname)
return all_rule;
} else if (ATTR_FALSE(value)) {
/* false (-whitespace) */
- return 0;
+ return ws_tab_width(whitespace_rule_cfg);
} else if (ATTR_UNSET(value)) {
/* reset to default (!whitespace) */
return whitespace_rule_cfg;
@@ -206,7 +216,7 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
}
/* Check for indent using non-tab. */
- if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) {
+ if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) {
result |= WS_INDENT_WITH_NON_TAB;
if (stream) {
fputs(ws, stream);
@@ -320,7 +330,7 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
} else if (ch == ' ') {
last_space_in_indent = i;
if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
- 8 <= i - last_tab_in_indent)
+ ws_tab_width(ws_rule) <= i - last_tab_in_indent)
need_fix_leading_space = 1;
} else
break;
@@ -350,7 +360,7 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
strbuf_addch(dst, ch);
} else {
consecutive_spaces++;
- if (consecutive_spaces == 8) {
+ if (consecutive_spaces == ws_tab_width(ws_rule)) {
strbuf_addch(dst, '\t');
consecutive_spaces = 0;
}
@@ -363,12 +373,13 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
fixed = 1;
} else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
/* Expand tabs into spaces */
+ int start = dst->len;
int last = last_tab_in_indent + 1;
for (i = 0; i < last; i++) {
if (src[i] == '\t')
do {
strbuf_addch(dst, ' ');
- } while (dst->len % 8);
+ } while ((dst->len - start) % ws_tab_width(ws_rule));
else
strbuf_addch(dst, src[i]);
}