7 * block up paragraphs, possibly with indentation
10 int extraindent = 0; /* how many spaces to indent all lines */
11 int indent = 0; /* current value of indent, before extra indent */
12 int length = 70; /* how many columns per output line */
13 int join = 1; /* can lines be joined? */
19 typedef struct Word Word;
35 fprint(2, "usage: %s [-j] [-i indent] [-l length] [file...]\n", argv0);
40 main(int argc, char **argv)
47 extraindent = atoi(EARGF(usage()));
54 length = atoi(EARGF(usage()));
61 fprint(2, "%s: line length<=indentation\n", argv0);
66 if(s!=nil && atoi(s)>0)
69 Binit(&bout, 1, OWRITE);
71 Binit(&bin, 0, OREAD);
74 for(i=0; i<argc; i++){
75 f = open(argv[i], OREAD);
77 fprint(2, "%s: can't open %s: %r\n", argv0, argv[i]);
80 Binit(&bin, f, OREAD);
97 for(; *s != '\0'; s++)
110 /* plain white space doesn't change the indent */
115 newword(char *s, int n, int ind, int bol)
119 w = malloc(sizeof(Word) + n+1);
123 memmove(w->text, s, n);
125 w->length = utflen(w->text);
132 static Word *head, *tail;
141 line = Brdstr(&bin, '\n', 1);
145 indent = indentof(line);
147 while(*line == ' ' || *line == '\t')
151 return newword("", 0, -1, 1);
154 /* how long is this word? */
155 for(s=line++; *line != '\0'; line++)
156 if(*line==' ' || *line=='\t')
158 w = newword(s, line-s, indent, head==nil);
183 /* give extra space if word ends with period, etc. */
192 if(isupper(s[0]) && n < 4)
194 if(strchr(".!?", s[n-1]) != nil)
215 printindent(extraindent+col);
216 /* emit words until overflow; always emit at least one word */
218 Bprint(&bout, "%s", w->text);
224 if(w->indent != o->indent)
225 break; /* indent change */
226 nsp = nspaceafter(o->text);
227 if(col+nsp+w->length > length)
228 break; /* fold line */
232 Bputc(&bout, ' '); /* emit space; another word will follow */