From ba2cdf8d40e149bb2f0ff84ddd527d8c4904a110 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 16 Dec 2014 13:38:17 -0800 Subject: Smarter decision about setting up expandtab. Previously we assumed hard tabs upon finding just one hard tab in the file. --- autotab.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'autotab.c') diff --git a/autotab.c b/autotab.c index 5c38446..9d709f2 100644 --- a/autotab.c +++ b/autotab.c @@ -4,8 +4,8 @@ * it as a Vim command to set up the tabstop, shiftwidth and expandtab * parameters. * - * Copyright 2012 - * Kaz Kylheku + * Copyright 2014 + * Kaz Kylheku * Vancouver, Canada * * To use this, compile to an executable called "autotab". @@ -710,6 +710,40 @@ static int compute_alignment_histogram(long (*array)[128], line_t *lines) } #endif +int determine_expandtab(line_t *lines_in, int tabsize, int shiftwidth) +{ + line_t *lines = tab_munge(lines_in, tabsize); + int indented = 0, tabbed = 0; + + for (; lines; lines = lines->next) { + char *str = lines->str; + long ind = strspn(str, ANYSP); + + /* Count indented lines which require at least one tab, + * and count how many of these include a tab in that + * indentation. + */ + if (ind % shiftwidth == 0 && ind >= tabsize) { + char *tab = strpbrk(str, INTAB LETAB); + + indented++; + if (!tab) + continue; + if (tab - str > ind) + continue; + tabbed++; + } + } + + free_lines(lines); + + /* If 25% or fewer of the indented lines which should + * have tabs actually have tabs, then let's turn + * on expandtab mode. + */ + return (tabbed <= indented / 4) ? 1 : 0; +} + int main(int argc, char **argv) { line_t *lines; @@ -731,6 +765,9 @@ int main(int argc, char **argv) if ((shiftwidth = determine_shiftwidth(lines, tabsize, 0)) == 0) goto out; + if (!expandtabs) + expandtabs = determine_expandtab(lines, tabsize, shiftwidth); + out_default: printf("tabstop=%d shiftwidth=%d %sexpandtab\n", tabsize, shiftwidth, expandtabs ? "" : "no"); -- cgit v1.2.3