-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcompiler.lua
227 lines (198 loc) · 6.85 KB
/
mcompiler.lua
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
220
221
222
223
224
225
226
227
VERSION = "1.0.1"
--[[-CONFIG VARS---------------------------------------------------------------------]]--
--[[
"runc" this variable is responsible for running the program/script, you can use any interpreter/compilec, the file name
is added automatically examples:
runc = "go run"
runc = "python"
if you need to specify the file name in a fixed position use #FILE, examples:
runc = "nasm -felf64 #FILE && ld hello.o && ./a.out"
]]--
-- compiler/interpreter params, change this for custom param
runc = ""
--[[
"build" this variable is responsible for the flags or compilation arguments
examples:
comp = "go build -gcflags=-e"
]]--
-- compiler/interpreter params, change this for custom param
build = ""
--[[
"debug" use this variable for your favorite debugger
examples:
comp = "gdb -d $(go env GOROOT) foo"
]]--
debug =""
--[[
"folder": "yes" to compile from the current folder instead of a file
"no" to compile from the current file
default "no"
]]--
folder ="no"
--[[
"tide": "yes" if yo use Tmux based Ide (https://github.com/joselbr2099/t-ide)
"no" for execution/compilation in background (no tide) use only micro
default "no"
]]--
tide="yes"
--[[-END CONFIG VARS-----------------------------------------------------------------]]--
--[[-CONFIG OPTIONS------------------------------------------------------------------]]--
-- add custom options for commands:
AddOption("runc", runc)
AddOption("build", build)
AddOption("debug", debug)
AddOption("folder", folder)
--[[-CONFIG OPTIONS------------------------------------------------------------------]]--
--make commands
MakeCommand("runc", "mcompiler.run_command", 0)
MakeCommand("build", "mcompiler.build_command", 0)
MakeCommand("debug", "mcompiler.debug_command", 0)
--bindkeys
BindKey("F5", "mcompiler.run_command")
BindKey("F6", "mcompiler.build_command")
BindKey("F8", "mcompiler.debug_command")
BindKey("F9", "mcompiler.tree_command")
--function to run code
function run_command()
command("runc")
end
--function to compile code
function build_command()
command("build")
end
--function to debug code
function debug_command()
command("debug")
end
--function to open file manager
function tree_command()
HandleCommand("tree")
end
function command(arg)
if tide =="yes"
then
loadConf()
if GetOption(arg) == ""
then
help(arg)
else
print_term(GetOption(arg),arg)
end
else
if GetOption(arg) == ""
then
RunShellCommand("clear")
RunInteractiveShell(help(arg),true,true)
else
print_term(GetOption(arg),arg)
end
end
end
function print_term(cmd,type)
CurView():Save(false) -- save current open file
local ft = CurView().Buf:FileType() -- get file extension
local file = CurView().Buf.Path -- get file name
local dir = DirectoryName(file) -- get directory of file
--add file name in command position or a end of coomand
if string.match(cmd, "#FILE") then
cmd = cmd:gsub("#FILE", file)
else
cmd = cmd.." "..file
end
if(tide=="yes")
then
if GetOption("folder") == "no"
then
opt=file
running='"'.."cd "..dir.." && "..cmd..' 2>&1'..'"'
else
opt=dir
running='"'.."cd "..dir.." && "..cmd.." "..dir..' 2>&1'..'"'
end
os.execute("tmux send-keys -t 2 'SES=$(tmux display-message -p "..'"'.."#S"..'"'..")' ENTER")
os.execute("tmux send-keys -t 2 'sh ~/.config/t-ide/$SES/builder "..("%q"):format(cmd).." "..("%q"):format(opt).." "..("%q"):format(type).." "..running.."' ENTER")
else
if GetOption("folder") == "no"
then
f = io.popen("cd "..dir.." && "..cmd..' 2>&1 && echo " $?"') --execute cmd
opt=file
else
f = io.popen("cd "..dir.." && "..cmd.." "..dir..' 2>&1 && echo " $?"') --execute cmd
opt=dir
end
RunShellCommand("clear")
n = os.tmpname()
file=io.open(n,"w")
file:write("\n")
file:write("------------------Init-" .. type .. "------------------ ","\n")
file:write("COMMAND_USE: " .. tostring(cmd) .. " " .. tostring(opt),"\n")
file:write("\n")
for line in f:lines() do
if line == " 0"
then
file:write("\n")
file:write("ALL_OK_NO_ERRORS ","\n")
file:write("EXEC_CODE:" .. line .. " ","\n")
else
file:write(line,"\n")
end
end
file:write("------------------Finish-" .. type .. "---------------- ","\n")
file:close()
RunInteractiveShell("cat "..n.."",true,true)
os.remove(n)
f:close()
end
--os.execute("tmux send-keys -t 2 'PageDown'")
end
function help(cmd)
if tide=="yes"
then
os.execute("tmux send-keys -t 2 'Escape'")
os.execute("tmux run-shell -t 2 'echo ------------------Error-" .. cmd .. "------------------' ")
os.execute("tmux run-shell -t 2 'echo COMMAND: "..cmd.." is empty'")
os.execute("tmux run-shell -t 2 'echo please config the command in -build options- window '")
os.execute("tmux run-shell -t 2 'echo or view README from tide'")
os.execute("tmux run-shell -t 2 'echo '")
os.execute("tmux run-shell -t 2 'echo mcompiler v1.0.1'")
os.execute("tmux run-shell -t 2 'echo https://github.com/joselbr2099/mcompiler'")
os.execute("tmux run-shell -t 2 'echo ------------------Error-" .. cmd .. "------------------' ")
else
RunInteractiveShell("clear",false,false)
hlp="echo -e 'Mcompiler v1.0.1\n"..
"https://github.com/joselbr2099/mcompiler\n"..
"\n"..
"Error the variable -"..cmd.."- is not set\n"..
" Please use te following commands to set your variables:\n"..
" -runc,\n"..
" -build,\n"..
" -bug,\n"..
" open command mode in micro (ctrl+e) an hit:\n"..
"\n"..
" use the 'set' command to set the 'option' variable globally\n"..
" for all micro instances:\n"..
" >set <option>\n"..
"\n"..
" for current instace only\n"..
" >setlocal <option>\n"..
" \n"..
" for run only (example python):\n"..
" >set run python\n"..
"\n"..
" to build your code with custom arguments (example go):\n"..
' >set build "go build -gcflags=-e"\n'..
"\n"..
' remember use " " for arguments that have spaces\n'..
" to see the current value of the variable\n"..
"\n"..
" >show <var>\n"..
"\n' "
return hlp
end
end
function loadConf()
session=io.popen("tmux display-message -p '#S'")
profile=session:read('*l')
dofile(os.getenv('HOME').."/.config/t-ide/"..profile.."/"..profile..".lua")
session:close()
end