From 0803e5ca0a9417e08622fc6d779682aba13666a0 Mon Sep 17 00:00:00 2001
From: Thomas Jensen <tsjensen@users.noreply.github.com>
Date: Sat, 22 Jul 2006 19:19:26 +0000
Subject: [PATCH] Applied patch by Christoph Dreyer to support unexpansion of
 leading tabs Added ability to retain existing tabs

---
 src/generate.c | 51 ++++++++++++++++++++++++++++++++++++++++----------
 src/remove.c   | 47 +++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 81 insertions(+), 17 deletions(-)

diff --git a/src/generate.c b/src/generate.c
index a2f1ed7..3d8fcc8 100644
--- a/src/generate.c
+++ b/src/generate.c
@@ -3,7 +3,7 @@
  *  Project Main:     boxes.c
  *  Date created:     June 23, 1999 (Wednesday, 20:10h)
  *  Author:           Copyright (C) 1999 Thomas Jensen <boxes@thomasjensen.com>
- *  Version:          $Id: generate.c,v 1.9 1999-08-31 08:37:01-07 tsjensen Exp tsjensen $
+ *  Version:          $Id: generate.c,v 1.10 2006/07/12 05:42:28 tsjensen Exp tsjensen $
  *  Language:         ANSI C
  *  World Wide Web:   http://boxes.thomasjensen.com/
  *  Purpose:          Box generation, i.e. the drawing of boxes
@@ -24,6 +24,9 @@
  *  Revision History:
  *
  *    $Log: generate.c,v $
+ *    Revision 1.10  2006/07/12 05:42:28  tsjensen
+ *    Updated email and web addresses in comment header
+ *
  *    Revision 1.9  1999-08-31 08:37:01-07  tsjensen
  *    Applied Joe Zbiciak's patches to remove all snprintf()s and variants
  *    Replaced snprintf() calls with calls to concat_strings() in the process
@@ -73,7 +76,7 @@
 
 
 static const char rcsid_generate_c[] =
-    "$Id: generate.c,v 1.9 1999-08-31 08:37:01-07 tsjensen Exp tsjensen $";
+    "$Id: generate.c,v 1.10 2006/07/12 05:42:28 tsjensen Exp tsjensen $";
 
 
 
@@ -861,6 +864,7 @@ int output_box (const sentry_t *thebox)
     size_t nol = thebox[BRIG].height;    /* number of output lines */
     char   trailspc[LINE_MAX+1];
     char  *indentspc;
+    int    indentspclen;
     size_t vfill, vfill1, vfill2;        /* empty lines/columns in box */
     size_t hfill;
     char  *hfill1, *hfill2;              /* space before/after text */
@@ -872,6 +876,8 @@ int output_box (const sentry_t *thebox)
     size_t skip_start;                   /* lines to skip for box top */
     size_t skip_end;                     /* lines to skip for box bottom */
     size_t skip_left;                    /* true if left box part is to be skipped */
+    int    ntabs, nspcs;                 /* needed for unexpand of tabs */
+    char  *restored_indent;
 
     #ifdef DEBUG
         fprintf (stderr, "Padding used: left %d, top %d, right %d, bottom %d\n",
@@ -882,14 +888,31 @@ int output_box (const sentry_t *thebox)
     /*
      *  Create string of spaces for indentation
      */
+    indentspc = NULL;
+    ntabs = nspcs = indentspclen = 0;
     if (opt.design->indentmode == 'b') {
-        indentspc = (char *) malloc (input.indent+1);
+        if (opt.tabexp == 'u') {
+            ntabs = input.indent / opt.tabstop;
+            nspcs = input.indent % opt.tabstop;
+            indentspclen = ntabs + nspcs;
+        }
+        else {
+            indentspclen = input.indent;
+        }
+        indentspc = (char *) malloc (indentspclen + 1);
         if (indentspc == NULL) {
             perror (PROJECT);
             return 1;
         }
-        memset (indentspc, (int)' ', input.indent);
-        indentspc[input.indent] = '\0';
+
+        if (opt.tabexp == 'u') {
+            memset (indentspc, (int)'\t', ntabs);
+            memset (indentspc + ntabs, (int)' ', nspcs);
+        }
+        else {
+            memset (indentspc, (int)' ', indentspclen);
+        }
+        indentspc[indentspclen] = '\0';
     }
     else {
         indentspc = (char *) strdup ("");
@@ -1004,7 +1027,8 @@ int output_box (const sentry_t *thebox)
     for (j=skip_start; j<nol-skip_end; ++j) {
 
         if (j < thebox[BTOP].height) {   /* box top */
-            concat_strings (obuf, LINE_MAX+1, 4, indentspc,
+            restored_indent = tabbify_indent (0, indentspc, indentspclen);
+            concat_strings (obuf, LINE_MAX+1, 4, restored_indent,
                     skip_left?"":thebox[BLEF].chars[j], thebox[BTOP].chars[j],
                     thebox[BRIG].chars[j]);
         }
@@ -1012,7 +1036,8 @@ int output_box (const sentry_t *thebox)
         else if (vfill1) {               /* top vfill */
             r = thebox[BTOP].width;
             trailspc[r] = '\0';
-            concat_strings (obuf, LINE_MAX+1, 4, indentspc,
+            restored_indent = tabbify_indent (0, indentspc, indentspclen);
+            concat_strings (obuf, LINE_MAX+1, 4, restored_indent,
                     skip_left?"":thebox[BLEF].chars[j], trailspc,
                     thebox[BRIG].chars[j]);
             trailspc[r] = ' ';
@@ -1031,7 +1056,8 @@ int output_box (const sentry_t *thebox)
                     return rc;
                 r = input.maxline - input.lines[ti].len;
                 trailspc[r] = '\0';
-                concat_strings (obuf, LINE_MAX+1, 7, indentspc,
+                restored_indent = tabbify_indent (ti, indentspc, indentspclen);
+                concat_strings (obuf, LINE_MAX+1, 7, restored_indent,
                         skip_left?"":thebox[BLEF].chars[j], hfill1,
                         ti >= 0? input.lines[ti].text : "", hfill2,
                         trailspc, thebox[BRIG].chars[j]);
@@ -1039,7 +1065,8 @@ int output_box (const sentry_t *thebox)
             else {                       /* bottom vfill */
                 r = thebox[BTOP].width;
                 trailspc[r] = '\0';
-                concat_strings (obuf, LINE_MAX+1, 4, indentspc,
+                restored_indent = tabbify_indent (input.anz_lines - 1, indentspc, indentspclen);
+                concat_strings (obuf, LINE_MAX+1, 4, restored_indent,
                         skip_left?"":thebox[BLEF].chars[j], trailspc,
                         thebox[BRIG].chars[j]);
             }
@@ -1047,7 +1074,8 @@ int output_box (const sentry_t *thebox)
         }
 
         else {                           /* box bottom */
-            concat_strings (obuf, LINE_MAX+1, 4, indentspc,
+            restored_indent = tabbify_indent (input.anz_lines - 1, indentspc, indentspclen);
+            concat_strings (obuf, LINE_MAX+1, 4, restored_indent,
                     skip_left?"":thebox[BLEF].chars[j],
                     thebox[BBOT].chars[j-(nol-thebox[BBOT].height)],
                     thebox[BRIG].chars[j]);
@@ -1062,6 +1090,9 @@ int output_box (const sentry_t *thebox)
         else {
             btrim (obuf, &obuf_len);
         }
+        if (opt.tabexp == 'k') {
+            BFREE (restored_indent);
+        }
 
         fprintf (opt.outfile, "%s\n", obuf);
     }
diff --git a/src/remove.c b/src/remove.c
index a921b48..8998c90 100644
--- a/src/remove.c
+++ b/src/remove.c
@@ -3,7 +3,7 @@
  *  Project Main:     boxes.c
  *  Date created:     June 23, 1999 (Wednesday, 20:59h)
  *  Author:           Copyright (C) 1999 Thomas Jensen <boxes@thomasjensen.com>
- *  Version:          $Id: remove.c,v 1.6 1999-11-08 02:51:41-08 tsjensen Exp tsjensen $
+ *  Version:          $Id: remove.c,v 1.7 2006/07/12 05:28:58 tsjensen Exp tsjensen $
  *  Language:         ANSI C
  *  World Wide Web:   http://boxes.thomasjensen.com/
  *  Purpose:          Box removal, i.e. the deletion of boxes
@@ -24,6 +24,10 @@
  *  Revision History:
  *
  *    $Log: remove.c,v $
+ *    Revision 1.7  2006/07/12 05:28:58  tsjensen
+ *    Updated email and web addresses in comment header
+ *    Added trim_only flag to output_input() function, used for box mending
+ *
  *    Revision 1.6  1999-11-08 02:51:41-08  tsjensen
  *    Bugfix: For non-empty left box sides, spaces belonging to "empty" shape
  *    lines were not properly removed in some cases
@@ -61,7 +65,7 @@
 #include "remove.h"
 
 static const char rcsid_remove_c[] =
-    "$Id: remove.c,v 1.6 1999-11-08 02:51:41-08 tsjensen Exp tsjensen $";
+    "$Id: remove.c,v 1.7 2006/07/12 05:28:58 tsjensen Exp tsjensen $";
 
 
 
@@ -1048,17 +1052,46 @@ void output_input (const int trim_only)
  */
 {
     size_t j;
+    size_t indent;
+    char  *indentspc;
+    int    ntabs, nspcs;
 
     #ifdef DEBUG
         fprintf (stderr, "output_input() - enter (trim_only=%d)\n", trim_only);
     #endif
-    for (j=0; j<input.anz_lines; ++j) {
-        if (input.lines[j].text) {
-            btrim (input.lines[j].text, &(input.lines[j].len));
-            if (!trim_only) {
-                printf ("%s\n", input.lines[j].text);
+    for (j=0; j<input.anz_lines; ++j)
+    {
+        if (input.lines[j].text == NULL)
+            continue;
+        btrim (input.lines[j].text, &(input.lines[j].len));
+        if (trim_only)
+            continue;
+        
+        indentspc = NULL;
+        if (opt.tabexp == 'u') {
+            indent = strspn (input.lines[j].text, " ");
+            ntabs = indent / opt.tabstop;
+            nspcs = indent % opt.tabstop;
+            indentspc = (char *) malloc (ntabs + nspcs + 1);
+            if (indentspc == NULL) {
+                perror (PROJECT);
+                return;
             }
+            memset (indentspc, (int)'\t', ntabs);
+            memset (indentspc+ntabs, (int)' ', nspcs);
+            indentspc[ntabs+nspcs] = '\0';
         }
+        else if (opt.tabexp == 'k') {
+            indentspc = tabbify_indent (j, NULL, input.indent);
+            indent = input.indent;
+        }
+        else {
+            indentspc = (char *) strdup ("");
+            indent = 0;
+        }
+        
+        printf ("%s%s\n", indentspc, input.lines[j].text + indent);
+        BFREE (indentspc);
     }
 }