Add feature patch for -tpg/-ntpg.
authorTom Lane <[email protected]>
Tue, 20 Jun 2017 19:27:29 +0000 (15:27 -0400)
committerTom Lane <[email protected]>
Tue, 20 Jun 2017 19:27:29 +0000 (15:27 -0400)
This switch enables using Postgres' rules for using tabs vs spaces
in indents, to match the output of our old "entab" program.
The FreeBSD maintainer wants no part of this, so we'll have to carry
this as a forked patch.  (The alternative is to go over to use-tabs-
whenever-possible rules, but that would result in circa 10K lines of
invisible whitespace diffs in the PG sources, which seems like lots
more pain than is justified.)

args.c
indent.1
indent.c
indent_globs.h
io.c

diff --git a/args.c b/args.c
index 73d8121c8d2faf2328ee73805c6c841d58471344..f79de754d2793c07a4c6583143d35fe69a94bb95 100644 (file)
--- a/args.c
+++ b/args.c
@@ -154,6 +154,7 @@ struct pro {
     {"nsac", PRO_BOOL, false, OFF, &space_after_cast},
     {"nsc", PRO_BOOL, true, OFF, &star_comment_cont},
     {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines},
+    {"ntpg", PRO_BOOL, false, OFF, &postgres_tab_rules},
     {"nut", PRO_BOOL, true, OFF, &use_tabs},
     {"nv", PRO_BOOL, false, OFF, &verbose},
     {"pcs", PRO_BOOL, false, ON, &proc_calls_space},
@@ -163,6 +164,7 @@ struct pro {
     {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines},
     {"st", PRO_SPECIAL, 0, STDIN, 0},
     {"ta", PRO_BOOL, false, ON, &auto_typedefs},
+    {"tpg", PRO_BOOL, false, ON, &postgres_tab_rules},
     {"ts", PRO_INT, 8, 0, &tabsize},
     {"ut", PRO_BOOL, true, ON, &use_tabs},
     {"v", PRO_BOOL, false, ON, &verbose},
index 0a6ef066307f08ec06ae2258a25f8a932de96456..131abfe70bec43d333b6eeafd49fd55c5f806179 100644 (file)
--- a/indent.1
+++ b/indent.1
@@ -87,6 +87,7 @@
 .Op Fl \&st
 .Op Fl \&ta
 .Op Fl T Ns Ar typename
+.Op Fl tpg | Fl ntpg
 .Op Fl ts Ns Ar n
 .Op Fl U Ns Ar file
 .Op Fl ut | Fl nut
@@ -472,6 +473,14 @@ language and
 cannot find all
 instances of
 .Ic typedef .
+.It Fl tpg , ntpg
+If
+.Fl tpg
+is specified, follow Postgres rules about when to use spaces versus
+tabs for indentation, that is, use a space instead of a tab if the
+tab would move only one column and no tab will follow it.
+Default:
+.Fl ntpg .
 .It Fl ts Ns Ar n
 Assumed distance between tab stops.
 The default is 8.
index d01d72216ad7b7caa8ac089571297b0145059dcd..e6560af2a63da1c228a10ffdc20cbaac628baf2d 100644 (file)
--- a/indent.c
+++ b/indent.c
@@ -1251,7 +1251,9 @@ indent_declaration(int cur_dec_ind, int tabs_to_var)
 
        CHECK_SIZE_CODE(cur_dec_ind / tabsize);
        while ((tpos = tabsize * (1 + pos / tabsize)) <= cur_dec_ind) {
-           *e_code++ = '\t';
+           *e_code++ = (!postgres_tab_rules ||
+                        tpos != pos + 1 ||
+                        cur_dec_ind >= tpos + tabsize) ? '\t' : ' ';
            pos = tpos;
        }
     }
index ade80e51e422ba3e17f5a839174ae0a777b09a5d..d018af133c0a6ff4fcf57f37d7ad1bc316ca07e5 100644 (file)
@@ -220,6 +220,7 @@ int     use_tabs;                   /* set true to use tabs for spacing,
 int        auto_typedefs;              /* set true to recognize identifiers
                                         * ending in "_t" like typedefs */
 int        space_after_cast;           /* "b = (int) a" vs "b = (int)a" */
+int        postgres_tab_rules;         /* use Postgres tab-vs-space rules */
 int        tabsize;                    /* the size of a tab */
 int        else_endif_com_ind;         /* the column in which comments to
                                         * the right of #else and #endif
diff --git a/io.c b/io.c
index 0792df08d5434123e2532d58200552391255befb..df110947ffd0debd30b4a29055578af7fe5b82bf 100644 (file)
--- a/io.c
+++ b/io.c
@@ -401,7 +401,9 @@ pad_output(int current, int target)
        int tcur;
 
        while ((tcur = tabsize * (1 + (curr - 1) / tabsize) + 1) <= target) {
-           putc('\t', output);
+           putc((!postgres_tab_rules ||
+                 tcur != curr + 1 ||
+                 target >= tcur + tabsize) ? '\t' : ' ', output);
            curr = tcur;
        }
     }