summaryrefslogtreecommitdiffstats
path: root/autotab.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2014-12-16 21:38:17 (GMT)
committerKaz Kylheku <kaz@kylheku.com>2014-12-17 23:37:22 (GMT)
commitba2cdf8d40e149bb2f0ff84ddd527d8c4904a110 (patch)
tree7001fdcdba87dc353e1f5278d1bb48b0f3c677e6 /autotab.c
parentb4880eac8e3e43785cb94ad74a6045904f4dcd02 (diff)
downloadc-snippets-ba2cdf8d40e149bb2f0ff84ddd527d8c4904a110.zip
c-snippets-ba2cdf8d40e149bb2f0ff84ddd527d8c4904a110.tar.gz
c-snippets-ba2cdf8d40e149bb2f0ff84ddd527d8c4904a110.tar.bz2
Smarter decision about setting up expandtab.
Previously we assumed hard tabs upon finding just one hard tab in the file.
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");