forked from miketang84/mp-5.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp_build.mpsl
156 lines (116 loc) · 3.36 KB
/
mp_build.mpsl
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
/*
Minimum Profit - A Text Editor
A simple IDE-like build system.
Angel Ortega <angel@triptico.com> et al.
This software is released into the public domain.
NO WARRANTY. See file LICENSE for details.
*/
mp.config.make_opts = '';
/** editor actions **/
mp.actions['build'] = sub {
local t, target;
if ((t = mp.build_get_targets()) == NULL)
mp.alert(L("No build targets found."));
else {
/* more than one target? ask user to pick one */
local r = mp.form(
[
{
label: L("Build target") + ':',
type: 'list',
list: t,
value: mp.makefile_target
}
]
);
if (r != NULL) {
mp.makefile_target = r[0];
target = t[mp.makefile_target];
}
if (target != NULL)
mp.long_op(mp.build_execute, target);
}
};
/** Default key bindings **/
mp.keycodes['f2'] = "build";
/** action descriptions **/
mp.actdesc['build'] = LL("Build project...");
/** code **/
sub mp.build_get_targets()
{
local r = [];
local m = 'make';
local f;
if (mp.config.make_opts)
m = m + ' ' + mp.config.make_opts;
if ((f = open('Makefile', 'r')) != NULL) {
r->push(m);
foreach (l, f) {
local t;
if (t = regex(l, '/^[A-Za-z0-9_\.-]+:/'))
push(r, m + ' ' + sregex(t, '/:/', ''));
}
close(f);
}
if ((f = mp.active.syntax.build_targets) != NULL) {
r = r + f->map(sub (e) { sprintf(e, mp.active.name); });
}
return r;
}
sub mp.build_execute(target)
{
local syntax = mp.active.syntax.name;
local log = mp.open('<build output>');
log.txt.lines = [];
log.txt.y = 0;
/* very very special cases: MPSL */
if (syntax == "MPSL" && (regex(target, "/^check/") || regex(target, "/^execute/"))) {
local op = target->split(" ");
local f = open(op[1]);
if (f) {
local res = '';
local code = eval(sub { compile(f, op[1]); });
f->close();
if (ERROR)
res = ERROR;
else {
if (op[0] == 'execute') {
res = eval(code);
if (ERROR)
res = ERROR;
else
res = dumper(res);
}
else
res = 'Syntax OK';
}
log->append(res);
ERROR = NULL;
}
else
log->append("Error opening " + op[1]);
}
else {
/* pipe through make */
local p;
if ((p = popen(target, 'r')) != NULL) {
foreach (l, p)
log->append(mp.chomp(l));
pclose(p);
}
else
mp.alert(sprintf(L("Error executing '%s'"), target));
}
log.txt.mod = 0;
log.read_only = 1;
log.syntax = mp.get_syntax('make_output');
log->move_eof();
log->move_bol();
/* ensure the log document has something */
if (count(log.txt.lines) == 0)
log.txt.lines = [''];
/* set the last search regex to match file:line strings,
so that calling seek-next and seek-prev moves there */
mp.last_search = '/^[a-z\.\_0-9\/-]+:[0-9]+:/m';
mp.redraw();
}