summaryrefslogtreecommitdiff
path: root/appl/cmd/fmt.b
blob: 337f9fd2fc788975c18b25ccde2a8d29652ff83f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
implement Fmt;

#
#	Copyright © 2002 Lucent Technologies Inc.
#	based on the Plan 9 command; subject to the Lucent Public License 1.02
#	this Vita Nuova variant uses Limbo channels and processes to avoid accumulating words
#

#
#  block up paragraphs, possibly with indentation
#

include "sys.m";
	sys: Sys;

include "draw.m";

include "bufio.m";
	bufio: Bufio;
	Iobuf: import bufio;

include "arg.m";

Fmt: module
{
	init: fn(nil: ref Draw->Context, nil: list of string);
};

extraindent := 0;	# how many spaces to indent all lines
indent := 0;	# current value of indent, before extra indent
length := 70;	# how many columns per output line
join := 1;	# can lines be joined?
maxtab := 8;
bout: ref Iobuf;

Word: adt {
	text:	string;
	indent:	int;
	bol:	int;
};

init(nil: ref Draw->Context, args: list of string)
{
	sys = load Sys Sys->PATH;
	bufio = load Bufio Bufio->PATH;
	arg := load Arg Arg->PATH;

	arg->init(args);
	arg->setusage("fmt [-j] [-i indent] [-l length] [file...]");
	while((c := arg->opt()) != 0) 
		case(c){
		'i' =>
			extraindent = int arg->earg();
		'j' =>
			join = 0;
		'w' or 'l' =>
			length = int arg->earg();
		* =>
			arg->usage();
		}
	args = arg->argv();
	if(length <= extraindent){
		sys->fprint(sys->fildes(2), "fmt: line length<=indentation\n");
		raise "fail:length";
	}
	arg = nil;

	err := "";
	bout = bufio->fopen(sys->fildes(1), Bufio->OWRITE);
	if(args == nil){
		bin := bufio->fopen(sys->fildes(0), Bufio->OREAD);
		fmt(bin);
	}else
		for(; args != nil; args = tl args){
			bin := bufio->open(hd args, Bufio->OREAD);
			if(bin == nil){
				sys->fprint(sys->fildes(2), "fmt: can't open %s: %r\n", hd args);
				err = "open";
			}else{
				fmt(bin);
				if(tl args != nil)
					bout.putc('\n');
			}
		}
	bout.flush();
	if(err != nil)
		raise "fail:"+err;
}

fmt(f: ref Iobuf)
{
	words := chan of ref Word;
	spawn parser(f, words);
	printwords(words);
}

parser(f: ref Iobuf, words: chan of ref Word)
{
	while((s := f.gets('\n')) != nil){
		if(s[len s-1] == '\n')
			s = s[0:len s-1];
		parseline(s, words);
	}
	words <-= nil;
}

parseline(line: string, words: chan of ref Word)
{
	ind: int;
	(line, ind) = indentof(line);
	indent = ind;
	bol := 1;
	for(i:=0; i < len line;){
		# find next word
		if(line[i] == ' ' || line[i] == '\t'){
			i++;
			continue;
		}
		# where does this word end?
		for(l:=i; l < len line; l++)
			if(line[l]==' ' || line[l]=='\t')
				break;
		words <-= ref Word(line[i:l], indent, bol);
		bol = 0;
		i = l;
	}
	if(bol)
		words <-= ref Word("", -1, bol);
}

indentof(line: string): (string, int)
{
	ind := 0;
	for(i:=0; i < len line; i++)
		case line[i] {
		' ' =>
			ind++;
		'\t' =>
			ind += maxtab;
			ind -= ind%maxtab;
		* =>
			return (line, ind);
		}
	# plain white space doesn't change the indent
	return (line, indent);
}
	
printwords(words: chan of ref Word)
{
	# one output line per loop
	nw := <-words;
	while((w := nw) != nil){
		# if it's a blank line, print it
		if(w.indent == -1){
			bout.putc('\n');
			nw = <-words;
			continue;
		}
		# emit leading indent
		col := extraindent+w.indent;
		printindent(col);
		# emit words until overflow; always emit at least one word
		for(n:=0;; n++){
			bout.puts(w.text);
			col += len w.text;
			if((nw = <-words) == nil)
				break;	# out of words
			if(nw.indent != w.indent)
				break;	# indent change
			nsp := nspaceafter(w.text);
			if(col+nsp+len nw.text > extraindent+length)
				break;	# fold line
			if(!join && nw.bol)
				break;
			for(j:=0; j<nsp; j++)
				bout.putc(' ');	# emit space; another word will follow
			col += nsp;
			w = nw;
		}
		bout.putc('\n');
	}
}

printindent(w: int)
{
	while(w >= maxtab){
		bout.putc('\t');
		w -= maxtab;
	}
	while(--w >= 0)
		bout.putc(' ');
}

# give extra space if word ends with punctuation
nspaceafter(s: string): int
{
	if(len s < 2)
		return 1;
	if(len s < 4 && s[0] >= 'A' && s[0] <= 'Z')
		return 1;	# assume it's a title, not full stop
	if((c := s[len s-1]) == '.' || c == '!' || c == '?')
		return 2;
	return 1;
}