summaryrefslogtreecommitdiffstats
path: root/autotab.c
diff options
context:
space:
mode:
Diffstat (limited to 'autotab.c')
-rw-r--r--autotab.c41
1 files changed, 39 insertions, 2 deletions
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 <kaz@kylheku.com>
+ * Copyright 2014
+ * Kaz Kylheku <kkylheku@gmail.com>
* 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");