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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2005-2018. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(emacs_SUITE).
%%-define(line_trace, 1).
-export([all/0, init_per_testcase/2, end_per_testcase/2]).
-export([bif_highlight/1,
load_interpreted/1, compile_and_load/1,
indent/1,
tests_interpreted/1, tests_compiled/1
]).
all() ->
[bif_highlight, load_interpreted, compile_and_load,
indent,
tests_interpreted, tests_compiled
].
init_per_testcase(Case, Config) ->
ErlangEl = filename:join([code:lib_dir(tools),"emacs","erlang.el"]),
case file:read_file_info(ErlangEl) of
{ok, _} ->
case Case =:= bif_highlight orelse emacs_version_ok(24.1) of
false -> {skip, "Old or no emacs found"};
_ -> [{el, ErlangEl}|Config]
end;
_ ->
{skip, "Could not find erlang.el"}
end.
end_per_testcase(_Case, _Config) ->
ok.
bif_highlight(Config) ->
ErlangEl = proplists:get_value(el,Config),
{ok, Bin} = file:read_file(ErlangEl),
%% All auto-imported bifs
IntBifs = lists:usort(
[F || {F,A} <- erlang:module_info(exports),
erl_internal:bif(F,A)]),
%% all bif which need erlang: prefix and are not operands
ExtBifs = lists:usort(
[F || {F,A} <- erlang:module_info(exports),
not erl_internal:bif(F,A) andalso
not is_atom(catch erl_internal:op_type(F,A))]),
check_bif_highlight(Bin, <<"erlang-int-bifs">>, IntBifs),
check_bif_highlight(Bin, <<"erlang-ext-bifs">>, ExtBifs).
check_bif_highlight(Bin, Tag, Compare) ->
[_H,IntMatch,_T] =
re:split(Bin,<<"defvar ",Tag/binary,
"[^(]*\\(([^)]*)">>,[]),
EmacsIntBifs = [list_to_atom(S) ||
S <- string:tokens(binary_to_list(IntMatch)," '\"\n")],
ct:log("Emacs ~p",[EmacsIntBifs]),
ct:log("Int ~p",[Compare]),
ct:log("Diff1 ~p",[Compare -- EmacsIntBifs]),
ct:log("Diff2 ~p",[EmacsIntBifs -- Compare]),
[] = Compare -- EmacsIntBifs,
[] = EmacsIntBifs -- Compare.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load_interpreted(_Config) ->
_ = emacs(["-l erlang.el -f erlang-mode"]),
ok.
compile_and_load(_Config) ->
Dir = emacs_dir(),
Files0 = filelib:wildcard("*.el", Dir),
Files = case emacs_version_ok(24.3) of
%% erldoc.el depends on cl-lib which was introduced in 24.3.
false -> Files0 -- ["erldoc.el"];
_ -> Files0
end,
Unforgiving =
case emacs_version_ok(24) of
Ver when Ver < 25 ->
"";
Ver when Ver < 26 ->
%% Workaround byte-compile-error-on-warn which seem broken in
%% Emacs 25.
"\"(advice-add #'display-warning :after "
"(lambda (_ f _ _) (error \"%s\" f)))\"";
_ ->
"\"(setq byte-compile-error-on-warn t)\""
end,
%% Add files here whenever they are cleaned of warnings.
NoWarn = ["erlang.el", "erlang-test.el", "erlang-edoc.el", "erlang-start.el", "erldoc.el"],
Compile = fun(File) ->
Pedantic = case lists:member(File, NoWarn) andalso Unforgiving /= "" of
true -> ["--eval ", Unforgiving, " "];
false -> " "
end,
emacs([Pedantic,
" -f batch-byte-compile ",filename:join(Dir, File)]),
true
end,
lists:foreach(Compile, Files),
emacs(["-l erlang.elc -f erlang-mode"]),
ok.
tests_interpreted(_Config) ->
case emacs_version_ok(25) of
false -> {skip, "Old or no emacs found"};
_ ->
emacs(["-l erlang.el ",
"-l erlang-test.el -f ert-run-tests-batch-and-exit"]),
ok
end.
tests_compiled(_Config) ->
case emacs_version_ok(25) of
false -> {skip, "Old or no emacs found"};
_ ->
emacs(["-l erlang.elc ",
"-l erlang-test.elc -f ert-run-tests-batch-and-exit"]),
ok
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
indent(Config) ->
Def = filename:dirname(code:which(?MODULE))
++ "/"
++ ?MODULE_STRING
++ "_data",
Dir = proplists:get_value(data_dir, Config, Def),
OrigFs = filelib:wildcard(Dir ++ "/*"),
io:format("Dir: ~s~nFs: ~p~n", [Dir, OrigFs]),
Fs = [{File, unindent(File)} || File <- OrigFs,
filename:extension(File) =:= ""],
Indent = fun(File) ->
emacs([
File, " ",
"--eval '(indent-region (point-min) (point-max) nil)' ",
"--eval '(save-buffer 0)'"
]),
ok
end,
[Indent(File) || {_, File} <- Fs],
Res = [diff(Orig, File) || {Orig, File} <- Fs],
[file:delete(File) || {ok, File} <- Res], %% Cleanup
[] = [Fail || {fail, Fail} <- Res],
ok.
unindent(Input) ->
Output = Input ++ ".erl",
{ok, Bin} = file:read_file(Input),
Lines0 = string:split(Bin, "\n", all),
Lines = [string:trim(Line, leading, [$\s,$\t]) || Line <- Lines0],
%% io:format("File: ~s lines: ~w~n", [Input, length(Lines0)]),
%% [io:format("~s~n", [L]) || L <- Lines],
ok = file:write_file(Output, lists:join("\n", Lines)),
Output.
diff(Orig, File) ->
case os:cmd(["diff ", Orig, " ", File]) of
"" -> {ok, File};
Diff ->
io:format("Fail: ~s vs ~s~n~s~n~n",[Orig, File, Diff]),
{fail, File}
end.
emacs_version_ok(AcceptVer) ->
case os:cmd("emacs --version | head -1") of
"GNU Emacs " ++ Ver ->
case string:to_float(Ver) of
{Vsn, _} when Vsn >= AcceptVer ->
Vsn;
_ ->
false
end;
Res ->
io:format("Emacs version fail~n~s~n~n",[Res]),
false
end.
emacs(EmacsCmds) when is_list(EmacsCmds) ->
Cmd = ["emacs ",
"--batch --quick ",
"--directory ", emacs_dir(), " ",
"--eval \"(require 'erlang-start)\" "
| EmacsCmds],
Res0 = os:cmd(Cmd ++ " ; echo $?"),
Rows = string:lexemes(Res0, ["\r\n", $\n]),
Res = lists:last(Rows),
Output = string:join(lists:droplast(Rows), "\n"),
io:format("Cmd ~s:~n => ~s ~ts~n", [Cmd, Res, Output]),
"0" = Res,
Output.
emacs_dir() ->
filename:join([code:lib_dir(tools), "emacs"]).
|