indent bugfix / added features

Chip Norkus wd at teleri.net
Fri Jun 11 02:53:22 GMT 2004


Hi all,

  I've been working on a major project, and decided to use indent to
normalize the code a bit.  In doing so I discovered a few deficencies in
the stock FreeBSD (5.2-CURRENT) indent and decided to fix them, I
thought these might be fairly common wishes (and one of them is a
bugfix) and have attached a patch which does the following:
* Adds an option to control tab use in output code (-ut and -nut)
  (This is in the GNU indent)
* Adds an option to control brace placement after function declaration
  (-fbs and -nfbs).  What this does, specifically, is allow you to have
  indent produce 'func(args..) {' instead of 'func(args..)\n{'.  The old
  behavior is the default.
* Fixes the indent bug when aligning variable names with an indentation
  level that is not a multiple of 8.  This works for both the 'tab use'
  and 'no tab use' varieties of code output.

  I don't know if anyone would be interested in committing the bugfix (I
believe it is correct) or added features, but I hope someone else finds
this useful.

-chip
--
personal: chip norkus; renaissance hacker;        wd at teleri.net
work:     systems engineer @ lunarpages, inc.;    chip at lunarpages.com
info:     finger wd at teleri.net for plan or keys;  http://telekinesis.org
-------------- next part --------------
diff -u /usr/src/usr.bin/indent/args.c ./args.c
--- /usr/src/usr.bin/indent/args.c	Tue Mar 23 13:33:16 2004
+++ ./args.c	Thu Jun 10 19:01:10 2004
@@ -111,6 +111,7 @@
     {"eei", PRO_BOOL, false, ON, &extra_expression_indent},
     {"ei", PRO_BOOL, true, ON, &ps.else_if},
     {"fbc", PRO_FONT, 0, 0, (int *) &blkcomf},
+    {"fbs", PRO_BOOL, true, ON, &function_brace_split},
     {"fbx", PRO_FONT, 0, 0, (int *) &boxcomf},
     {"fb", PRO_FONT, 0, 0, (int *) &bodyf},
     {"fc1", PRO_BOOL, true, ON, &format_col1_comments},
@@ -136,6 +137,7 @@
     {"ndj", PRO_BOOL, false, OFF, &ps.ljust_decl},
     {"neei", PRO_BOOL, false, OFF, &extra_expression_indent},
     {"nei", PRO_BOOL, true, OFF, &ps.else_if},
+    {"nfbs", PRO_BOOL, true, OFF, &function_brace_split},
     {"nfc1", PRO_BOOL, true, OFF, &format_col1_comments},
     {"nfcb", PRO_BOOL, true, OFF, &format_block_comments},
     {"nip", PRO_BOOL, true, OFF, &ps.indent_parameters},
@@ -146,6 +148,7 @@
     {"nps", PRO_BOOL, false, OFF, &pointer_as_binop},
     {"nsc", PRO_BOOL, true, OFF, &star_comment_cont},
     {"nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines},
+    {"nut", PRO_BOOL, true, OFF, &use_tabs},
     {"nv", PRO_BOOL, false, OFF, &verbose},
     {"pcs", PRO_BOOL, false, ON, &proc_calls_space},
     {"psl", PRO_BOOL, true, ON, &procnames_start_line},
@@ -154,6 +157,7 @@
     {"sob", PRO_BOOL, false, ON, &swallow_optional_blanklines},
     {"st", PRO_SPECIAL, 0, STDIN, 0},
     {"troff", PRO_BOOL, false, ON, &troff},
+    {"ut", PRO_BOOL, true, ON, &use_tabs},
     {"v", PRO_BOOL, false, ON, &verbose},
     /* whew! */
     {0, 0, 0, 0, 0}
diff -u /usr/src/usr.bin/indent/indent.c ./indent.c
--- /usr/src/usr.bin/indent/indent.c	Tue Feb 17 02:33:36 2004
+++ ./indent.c	Thu Jun 10 21:45:58 2004
@@ -91,7 +91,7 @@
     int         squest;		/* when this is positive, we have seen a ?
 				 * without the matching : in a <c>?<s>:<s>
 				 * construct */
-    int		use_tabs;	/* true if using tabs to indent to var name */
+    int		loc_use_tabs;	/* true if using tabs to indent to var name */
     const char *t_ptr;		/* used for copying tokens */
     int         type_code;	/* the type of token, returned by lexi */
 
@@ -765,8 +765,13 @@
 		}
 		else if (ps.in_parameter_declaration && !ps.in_or_st) {
 		    ps.i_l_follow = 0;
-		    dump_line();
-		    ps.want_blank = false;
+		    if (function_brace_split) {
+			/* dump the line prior to the brace ... */
+			dump_line();
+			ps.want_blank = false;
+		    } else
+			/* add a space between the decl and brace */
+			ps.want_blank = true;
 		}
 	    }
 	    if (ps.in_parameter_declaration)
@@ -914,11 +919,11 @@
 	    if (ps.ind_level == 0 || ps.dec_nest > 0) {
 		/* global variable or struct member in local variable */
 		dec_ind = ps.decl_indent > 0 ? ps.decl_indent : i;
-		use_tabs = ps.decl_indent > 0;
+		loc_use_tabs = (use_tabs ? ps.decl_indent > 0 : 0);
 	    } else {
 		/* local variable */
 		dec_ind = ps.local_decl_indent > 0 ? ps.local_decl_indent : i;
-		use_tabs = ps.local_decl_indent > 0;
+		loc_use_tabs = (use_tabs ? ps.local_decl_indent > 0 : 0);
 	    }
 	    goto copy_id;
 
@@ -936,17 +941,31 @@
 			    e_code += strlen(e_code);
 			} else {
 			    int pos, startpos;
+			    int loc_dec_ind;
 
+			    /* In order to get the tab math right for
+			     * indentations that are not multiples of 8 we
+			     * need to modify both startpos and dec_ind
+			     * (loc_dec_ind) here by eight minus the
+			     * remainder of the current starting column
+			     * divided by eight.  This seems to be a
+			     * properly working fix. */
 			    startpos = e_code - s_code;
+			    loc_dec_ind = dec_ind;
+			    if ((ps.ind_level * ps.ind_size) % 8 != 0) {
+				startpos += (ps.ind_level * ps.ind_size) % 8;
+				loc_dec_ind += (ps.ind_level * ps.ind_size) % 8;
+			    }
+                            
 			    pos = startpos;
-			    if (use_tabs) {
-				while ((pos & ~7) + 8 <= dec_ind) {
+			    if (loc_use_tabs) {
+				while ((pos & ~7) + 8 <= loc_dec_ind) {
 				    CHECK_SIZE_CODE;
 				    *e_code++ = '\t';
 				    pos = (pos & ~7) + 8;
 				}
 			    }
-			    while (pos < dec_ind) {
+			    while (pos < loc_dec_ind) {
 				CHECK_SIZE_CODE;
 				*e_code++ = ' ';
 				pos++;
diff -u /usr/src/usr.bin/indent/indent_globs.h ./indent_globs.h
--- /usr/src/usr.bin/indent/indent_globs.h	Tue Feb 17 02:33:36 2004
+++ ./indent_globs.h	Thu Jun 10 21:43:42 2004
@@ -199,6 +199,10 @@
 					 * indented an extra tab stop so that
 					 * they don't conflict with the code
 					 * that follows */
+int	    function_brace_split;	/* split function declaration and
+					 * brace onto separate lines? */
+int	    use_tabs;			/* Set true to use tabs for spacing,
+					 * false uses all spaces. */
 
 /* -troff font state information */
 
diff -u /usr/src/usr.bin/indent/io.c ./io.c
--- /usr/src/usr.bin/indent/io.c	Tue Feb 17 02:33:36 2004
+++ ./io.c	Thu Jun 10 21:44:28 2004
@@ -472,11 +472,13 @@
 	if (current >= target)
 	    return (current);	/* line is already long enough */
 	curr = current;
-	while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
-	    putc('\t', output);
-	    curr = tcur;
-	}
-	while (curr++ < target)
+        if (use_tabs) {
+            while ((tcur = ((curr - 1) & tabmask) + tabsize + 1) <= target) {
+                putc('\t', output);
+                curr = tcur;
+            }
+        }
+        while (curr++ < target)
 	    putc(' ', output);	/* pad with final blanks */
     }
     return (target);


More information about the freebsd-hackers mailing list