[hackers] [sbase] Implement -i flag for expand || Tuukka Kataja

From: <git_AT_suckless.org>
Date: Mon, 09 Jun 2014 18:00:36 +0200

commit 612e09af7e9ff1ffca64652b6561871aec3db5de
Author: Tuukka Kataja <stuge_AT_xor.fi>
Date: Mon Jun 9 16:54:45 2014 +0100

    Implement -i flag for expand

diff --git a/expand.1 b/expand.1
index 98c66ab..6ae819b 100644
--- a/expand.1
+++ b/expand.1
_AT_@ -13,6 +13,9 @@ are preserved into the output and decrement the column count for tab
 calculations.
 .SH OPTIONS
 .TP
+.BI \-i
+Only change tabs to spaces at the start of lines.
+.TP
 .BI \-t " n"
 Expand tabs to
 .I n
diff --git a/expand.c b/expand.c
index 2bcc668..61e67ee 100644
--- a/expand.c
+++ b/expand.c
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <wchar.h>
_AT_@ -11,6 +12,8 @@ typedef struct {
 
 static int expand(Fdescr *f, int tabstop);
 
+static bool iflag = false;
+
 static void
 usage(void)
 {
_AT_@ -26,7 +29,8 @@ main(int argc, char *argv[])
 
         ARGBEGIN {
         case 'i':
- eprintf("not implemented
");
+ iflag = true;
+ break;
         case 't':
                 tabstop = estrtol(EARGF(usage()), 0);
                 break;
_AT_@ -77,6 +81,7 @@ expand(Fdescr *dsc, int tabstop)
 {
         int col = 0;
         wint_t c;
+ bool bol = true;
 
         for (;;) {
                 c = in(dsc);
_AT_@ -85,22 +90,31 @@ expand(Fdescr *dsc, int tabstop)
 
                 switch (c) {
                 case ' ':
- do {
- col++;
- out(' ');
- } while (col & (tabstop - 1));
+ if (bol || !iflag) {
+ do {
+ col++;
+ out(' ');
+ } while (col & (tabstop - 1));
+ } else {
+ out(' ');
+ col += tabstop - col % tabstop;
+ }
                         break;
                 case '':
                         if (col)
                                 col--;
+ bol = false;
                         out(c);
                         break;
                 case '
':
                         col = 0;
+ bol = true;
                         out(c);
                         break;
                 default:
                         col++;
+ if (c != ' ')
+ bol = false;
                         out(c);
                         break;
                 }
Received on Mon Jun 09 2014 - 18:00:36 CEST

This archive was generated by hypermail 2.3.0 : Mon Jun 09 2014 - 18:12:14 CEST