-
Notifications
You must be signed in to change notification settings - Fork 0
/
entab
executable file
·51 lines (46 loc) · 1.46 KB
/
entab
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
#!/usr/bin/env escript
%% settabs -- set initial tab stops
%%
%% We can't generate a list/array because we don't have a MAXLINE.
%% settabs returns the TabSpace value instead.
settabs() ->
TabSpace = 4,
TabSpace.
%% tabpos -- return true if col is a tab stop
%%
%% Doesn't make sense in using MAXLINE since Erlang handle
%% the EOF and buffer size for us, unless we're using
%% io:get_chars/2 because the second parameter of the said
%% function is exactly a limiter.
tabpos(Col) ->
TabSpace = settabs(),
TabSpace - Col rem TabSpace.
%% entab -- convert runs of blanks into tabs
%%
%% BUGS
%% Is (possibly) not an exact inverse of detab.
%% It doesn't convert a single blank to a tab if it occurs
%% at a tab stop. This behavior was considered a bug in Pascal,
%% here it could be considered a bug to don't have such a bug.
entab(Input) ->
entab(Input, 0, []).
entab([], _, Acc) ->
ststd:putf("~p~n", [Acc]);
%% Blank/Whitespace = ASCII 32
entab([32|T], Blanks, Acc) ->
TabSize = settabs(),
case Blanks =:= (TabSize - 1) of
true -> entab(T, 0, lists:concat([Acc, [9]]));
false -> entab(T, Blanks+1, Acc)
end;
entab([H|T], Blanks, Acc) ->
Seq = lists:seq(1, Blanks),
BlanksList = lists:map(fun(_) -> 32 end, Seq),
entab(T, 0, lists:concat([Acc, BlanksList, [H]])).
main([]) ->
Input = ststd:getf(),
entab(Input);
main(Arg) ->
{ok, Bin} = file:read_file(Arg),
File = erlang:binary_to_file(Bin),
entab(File).