diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-06-15 14:29:30 -0400 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-06-15 14:29:30 -0400 |
commit | 0673f87892e799437f25b5ba304073a15f5143f7 (patch) | |
tree | 9d68e233c8cd6fd8b95c607a1e72a33a5f286925 | |
parent | 81e7f738144b8ed22e254d238c9b2187ad9dbfa8 (diff) |
added indentation mode
-rw-r--r-- | badroff.c | 34 |
1 files changed, 30 insertions, 4 deletions
@@ -271,12 +271,21 @@ static char* nomrg(char* txt){ join = NULL; return NULL; } +int indlevel = 0; +static char* indent(char* txt){ + int orig = indlevel; + if (isdigit(*txt)) sscanf(txt, "%d", &indlevel); else indlevel = 0; + // REPEATED + width += orig-indlevel; + return NULL; +} static char* cmds[] = // MUST be sorted alphabetically - {"CT", "EG", "FIL", "LD", "LS", "MRG", "NBSP", "NMRG", "V", "W"}; + {"CT", "EG", "FIL", "IND", "LD", "LS", "MRG", "NBSP", "NMRG", "V", + "W"}; static char* (*call[])(char* txt) = - {center, fingroup, fillline, leader, lineset, merge, nbrkspc, nomrg, - vert, setwidth}; + {center, fingroup, fillline, indent, leader, lineset, merge, + nbrkspc, nomrg, vert, setwidth}; static char* cmd(void){ char* dat = norm(); @@ -329,9 +338,23 @@ static void nbspclean(char* txt){ } } +static char* wsadd(char* txt, int lnct) { + int len = strlen(txt) + lnct*indlevel; + txt = realloc(txt, sizeof(char)*(len+1)); + char* end = txt + len; + char *nl, *last; nl = last = txt; + while ( (nl = memchr(nl, '\n', end-nl))[1] ) { + memmove(last+indlevel, last, nl-last); + memset(last, ' ', indlevel); + last = nl+1; + } + return txt; +} + char* wordset(char* txt){ char* orig = txt; // txt will be manipulated int ws = 0; // zero means do not cut + int ln = 0; for (int i=0; txt[i] != 0; i++){ // turn into a nested loop if (txt[i] == ' ' || txt[i] == '\n') ws = i; // nl's (multiline input) and spaces are proper linebreaks @@ -340,9 +363,11 @@ char* wordset(char* txt){ txt[0] = '\n'; ws = 0; i = 0; + ln++; } } + orig = wsadd(orig, ln); wsclean(orig); // remove trailing whitespace nbspclean(orig); @@ -401,7 +426,8 @@ int main(int argc, char** argv){ } char* out; while ( (out = line())[0] != '\0'){ - printf("%s",wordset(out)); + out = wordset(out); + printf("%s",out); free(out); } fclose(in); |