bug-indent
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 07/11] Add an option to use spaces for aligment when using tabs


From: Tim Hentenaar
Subject: [PATCH 07/11] Add an option to use spaces for aligment when using tabs
Date: Mon, 15 Jun 2015 21:52:58 +0200

-as (or --align-with-spaces):
    When using tabs for indentation, tab only until the requested
    indentation level, then pad with spaces the rest of the way.
---
 ChangeLog                               |   5 ++
 doc/indent.texi                         | 112 +++++++++++++++++++++++++++++++-
 regression/TEST                         |   4 +-
 regression/input/align-with-spaces.c    |   7 ++
 regression/standard/align-with-spaces.c |   9 +++
 src/args.c                              |   4 ++
 src/indent.h                            |   1 +
 src/output.c                            |  29 ++++++---
 8 files changed, 160 insertions(+), 11 deletions(-)
 create mode 100644 regression/input/align-with-spaces.c
 create mode 100644 regression/standard/align-with-spaces.c

diff --git a/ChangeLog b/ChangeLog
index 1f797ac..c6cf846 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2015-06-14 Tim Hentenaar <address@hidden>
+       * Added an option (-as/--align-with-spaces) which if indenting with
+         tabs will ident to the current indentation level, and then pad
+         with spces up till the target_column.
+
 2015-06-13 Tim Hentenaar <address@hidden>
        * lexi.c: Fix a compilation error related to the presence of
          the __gnu_inlne__ attribute on the functions in gperf.c/gperf-cc.c
diff --git a/doc/indent.texi b/doc/indent.texi
index c7f7f61..9ca295d 100644
--- a/doc/indent.texi
+++ b/doc/indent.texi
@@ -1214,10 +1214,49 @@ int one(void)
 @end group
 @end example
 
address@hidden -ut
address@hidden --use-tabs
address@hidden -nut
address@hidden --no-tabs
 @node Indentation, Breaking long lines, Declarations, Indent Program
 @comment  node-name,  next,  previous,  up
 @section Indentation
 
+The most basic, and most controversial issues with regard to code
+formatting is precisely how indentation should be acoomplished.
+Fortunately, @command{indent} supports several different styles of
+identation.  The default is to use tabs for indentation, which is
+specified by the @option{-ut} option. Assuming the default tab size of
+8, the code would look like this:
+
address@hidden
address@hidden
+int a(int b)
address@hidden
+        return b;
+|------|
+ 1 tab
address@hidden
address@hidden group
address@hidden example
+
address@hidden
+For those that prefer spaces to tabs, @option{indent} provides the
address@hidden option. The same code would look like this:
+
address@hidden -nut
address@hidden --no-tabs
address@hidden
address@hidden
+int a(int b)
address@hidden
+        return b;
+|------|
+8 spaces
address@hidden
address@hidden group
address@hidden example
+
 @kindex address@hidden
 @kindex address@hidden
 @kindex address@hidden
@@ -1226,7 +1265,7 @@ int one(void)
 @kindex --continue-at-parentheses
 @kindex -nlp
 @kindex --dont-line-up-parentheses
-One issue in the formatting of code is how far each line should be
+Another issue in the formatting of code is how far each line should be
 indented from the left margin.  When the beginning of a statement such
 as @code{if} or @code{for} is encountered, the indentation level is
 increased by the value specified by the @option{-i} option.  For example,
@@ -1291,6 +1330,70 @@ input and output character streams.  These intervals are 
by default 8
 columns wide, but (as of version 1.2) may be changed by the @option{-ts}
 option.  Tabs are treated as the equivalent number of spaces.
 
address@hidden -as
address@hidden --align-with-spaces
+By default, @command{indent} will use tabs to indent as far as possible,
+and then pad with spaces until the desired position is reached. However,
+with the @option{-as} option, spaces will be used for alignment beyond
+the current indentation level. By default, assuming @option{-lp} is
+enabled, the code would be indented like so (@samp{t} represents tabs,
address@hidden represents spaces):
+
address@hidden
address@hidden
+unsigned long really_long_proc_name(unsigned long x, unsigned long y,
+                                    int a)
+|------||-------||------||-------|__
+   t        t       t       t     ss
address@hidden
+        p1 = first_procedure (second_procedure (p2, p3),
+                              third_procedure (p4, p5));
+|------||------||------|_____
+   t       t       t    sssss
address@hidden
address@hidden group
address@hidden example
+
address@hidden
+This is fine, if you assume that whoever is reading the code will honor
+your assumption of 8-space tabs. If the reader was using 4-space tabs,
+it would look like this:
+
address@hidden
address@hidden
+unsigned long really_long_proc_name(unsigned long x, unsigned long y,
+                      int a)
+|---||---||---||---|__
+  t    t    t    t  ss
address@hidden
+        p1 = first_procedure (second_procedure (p2, p3),
+                     third_procedure (p4, p5));
+|---||---||---|______
+  t    t    t  ssssss
address@hidden
address@hidden group
address@hidden example
+
address@hidden
+The @option{-as} option fixes this so that the code will appear consistent
+regardless of what tab size the user users to read the code. This looks
+like:
+
address@hidden
address@hidden
+unsigned long really_long_proc_name(unsigned long x, unsigned long y,
+                                    int a)
+____________________________________
+ssssssssssssssssssssssssssssssssssss
address@hidden
+        p1 = first_procedure (second_procedure (p2, p3),
+                              third_procedure (p4, p5));
+|------|______________________
+   t    ssssssssssssssssssssss
address@hidden
address@hidden group
address@hidden example
+
 @kindex address@hidden
 @kindex address@hidden
 @kindex -nip
@@ -1676,6 +1779,11 @@ short option.  It is followed by a cross key 
alphabetized by long option.
 @c The cross references are all on lines by themselves because this
 @c looks better in both printed output and Info.  4 Feb 1992  --rjc
 @table @samp
address@hidden -as
address@hidden --align-with-spaces
+If using tabs for indentation, use spaces for address@hidden
address@hidden
+
 @item -bad
 @itemx --blank-lines-after-declarations
 Force blank lines after the address@hidden
@@ -2178,6 +2286,7 @@ the corresponding short option.
 \def\leaderfill{\leaders\hbox to 1em{\hss.\hss}\hfill}
 
 {\tt
+\line{ --align-with-spacess \leaderfill               -as\ }
 \line{ --blank-lines-after-commas \leaderfill         -bc\ \ }  
 \line{ --blank-lines-after-declarations \leaderfill   -bad\ } 
 \line{ --blank-lines-after-procedures \leaderfill     -bap\ }  
@@ -2270,6 +2379,7 @@ the corresponding short option.
 
 @ifinfo
 @example
+--align-with-spaces                             -as
 --blank-lines-after-commas                      -bc             
 --blank-lines-after-declarations                -bad            
 --blank-lines-after-procedures                  -bap            
diff --git a/regression/TEST b/regression/TEST
index fa312dc..6b310ac 100755
--- a/regression/TEST
+++ b/regression/TEST
@@ -67,7 +67,8 @@ SPECIALS="typedef-bug.c bug-hp.c bug-di.c newlined-parms.c 
indent.pro.c \
         comment-break.c cdb.c bbreak.c bad-break.c pre.c pre_lps.c pre_nlps.c 
dont-line-up-parentheses.c \
         bad-comment.c unknown-type.c unknown-type-npsl.c bug-npsl.c bug-psl.c 
do-cdw.c \
         label.c goto-1.c goto-2.c line-count.c decl_block.c pointer-pal.c 
else-comment-2-br.c else-comment-2-bl.c \
-        else-comment-2-br-ce.c preserve-newline-after-right-brace.c 
gettext-strings.c single-line-conditionals.c"
+        else-comment-2-br-ce.c preserve-newline-after-right-brace.c 
gettext-strings.c single-line-conditionals.c \
+        align-with-spaces.c"
 
 ARGS="-bad"
 $INDENT -npro $ARGS input/bad-break.c -o output/bad-break.c
@@ -175,6 +176,7 @@ $INDENT -npro -br -ce 
input/preserve-newline-after-right-brace.c -o output/prese
 
 ARGS=""
 $INDENT -npro -slc input/single-line-conditionals.c -o 
output/single-line-conditionals.c
+$INDENT -npro -br -ut -i8 -l70 -ts8 -as input/align-with-spaces.c -o 
output/align-with-spaces.c
 
 ARGS="-kr -cp0 -l132 -lps -br -psl"
 $INDENT -npro $ARGS input/const.c -o output/const.c
diff --git a/regression/input/align-with-spaces.c 
b/regression/input/align-with-spaces.c
new file mode 100644
index 0000000..5ab9ffc
--- /dev/null
+++ b/regression/input/align-with-spaces.c
@@ -0,0 +1,7 @@
+static void really_long_function_decl(const char *string_here, int 
integer_here, unsigned long xxx)
+{
+       if (errno == ENOMEM) {
+               ERROR("unable to allocate memory for a new xxx "
+                         "because %s", strerror(errno));
+       }
+}
diff --git a/regression/standard/align-with-spaces.c 
b/regression/standard/align-with-spaces.c
new file mode 100644
index 0000000..65b8753
--- /dev/null
+++ b/regression/standard/align-with-spaces.c
@@ -0,0 +1,9 @@
+static void
+really_long_function_decl (const char *string_here, int integer_here,
+                           unsigned long xxx)
+{
+       if (errno == ENOMEM) {
+               ERROR ("unable to allocate memory for a new xxx "
+                      "because %s", strerror (errno));
+       }
+}
diff --git a/src/args.c b/src/args.c
index 2a7dbe4..c739f9f 100644
--- a/src/args.c
+++ b/src/args.c
@@ -199,6 +199,7 @@ static int exp_v    = 0;
 static int exp_version = 0;
 static int exp_par  = 0;
 static int exp_slc  = 0;
+static int exp_as   = 0;
 
 /**
  * The following structure is controlled by command line parameters and
@@ -364,6 +365,7 @@ const pro_ty pro[] =
     {"pal",     PRO_BOOL,                            true,      OFF, 
&settings.pointer_align_right,              &exp_par},
     {"par",     PRO_BOOL,                            true,       ON, 
&settings.pointer_align_right,              &exp_par},
     {"slc",     PRO_BOOL,                            false,      ON, 
&settings.allow_single_line_conditionals,   &exp_slc},
+    {"as",      PRO_BOOL,                            false,      ON, 
&settings.align_with_spaces,                &exp_as},
 
     /* Signify end of structure.  */
     {0,         PRO_IGN,                                0, ONOFF_NA, 0,        
                                  0}
@@ -490,6 +492,7 @@ const pro_ty pro[] =
     {"pal",     PRO_BOOL,                            true,      OFF, 
&settings.pointer_align_right,              &exp_par},
     {"par",     PRO_BOOL,                            true,       ON, 
&settings.pointer_align_right,              &exp_par},
     {"slc",     PRO_BOOL,                            false,      ON, 
&settings.allow_single_line_conditionals,   &exp_slc},
+    {"as",      PRO_BOOL,                            false,      ON, 
&settings.align_with_spaces,                &exp_as},
 
     /* Signify end of structure.  */
     {0,         PRO_IGN,                                0, ONOFF_NA, 0,        
                                  0}
@@ -626,6 +629,7 @@ const long_option_conversion_ty option_conversions[] =
     {"pointer-align-right",                         "par"},
     {"pointer-align-left",                          "pal"},
     {"single-line-conditionals",                    "slc"},
+    {"align-with-spaces",                           "as"},
 
     /* Signify end of structure.  */
     {0,                                             0},
diff --git a/src/indent.h b/src/indent.h
index 4b535a5..8179658 100644
--- a/src/indent.h
+++ b/src/indent.h
@@ -328,6 +328,7 @@ typedef struct user_options_st
     int pointer_align_right; /*!< true: "char *a", false: "char* a" */
     int gettext_strings;     /*!< true: _("...") is a string, false: it's a 
function */ 
     int allow_single_line_conditionals; /*!< Don't indent the body of an 
unbraced if, else, etc. */
+    int align_with_spaces; /*!< Align with spaces if indenting with tabs. */
 } user_options_ty;
 
 extern user_options_ty settings;
diff --git a/src/output.c b/src/output.c
index bedadf2..c3b5fee 100644
--- a/src/output.c
+++ b/src/output.c
@@ -1,8 +1,9 @@
 /** \file
+ * Copyright (c) 2015 Tim Hentenaar. All Rights Reserved.<br>
  * Copyright (c) 2002 D.Ingamells
  * Copyright (c) 1999, 2000 Carlo Wood.  All rights reserved. <br>
  * Copyright (c) 1994, 1996, 1997 Joseph Arceneaux.  All rights reserved. <br>
- * Copyright (c) 1992, 2002, 2008 Free Software Foundation, Inc.  All rights 
reserved. <br>
+ * Copyright (c) 1992, 2002, 2008, 2015 Free Software Foundation, Inc.  All 
rights reserved. <br>
  *
  * Copyright (c) 1980 The Regents of the University of California. <br>
  * Copyright (c) 1976 Board of Trustees of the University of Illinois. All 
rights reserved.
@@ -592,31 +593,41 @@ static void set_next_buf_break (
  */
 
 static int pad_output(
-    int currentColumn,
+    int current_column,
     int target_column)
 {
-    if (currentColumn < target_column)
+    int offset = 0;
+    int align_target = target_column;
+
+    if (current_column < target_column)
     {
         if (settings.use_tabs && (settings.tabsize > 1))
         {
-            int offset = settings.tabsize - (currentColumn - 1) % 
settings.tabsize;
+            if (settings.align_with_spaces)
+            {
+                if (align_target >= parser_state_tos->ind_level)
+                       align_target = parser_state_tos->ind_level;
+                   offset = (align_target - current_column + 1) / 
settings.tabsize;
+                   align_target = current_column + (offset * settings.tabsize);
+            }
 
-            while (currentColumn + offset <= target_column)
+            offset = settings.tabsize - (current_column - 1) % 
settings.tabsize;
+            while (current_column + offset <= align_target)
             {
                 putc(TAB, output);
-                currentColumn += offset;
+                current_column += offset;
                 offset = settings.tabsize;
             }
         }
 
-        while (currentColumn < target_column)
+        while (current_column < target_column)
         {
             putc(' ', output);
-            currentColumn++;
+            current_column++;
         }
     }
 
-    return currentColumn;
+    return current_column;
 }
 
 /**
-- 
2.3.6




reply via email to

[Prev in Thread] Current Thread [Next in Thread]