local mobdebug = require"mobdebug"
local function DebugCoro(arg)
if type(arg)~="boolean" then return ZBS.debugcoro else ZBS.debugcoro = arg end
end
local function DebuggerIsAttached(arg)
if type(arg)~="boolean" then return ZBS.attached else ZBS.attached = arg; ZBS.on = arg end
end
local function MessagePopup(msg,title,flags,delay) --private
local s = far.SaveScreen()
far.Message(msg,title or "","",flags)
mf.sleep(delay or 500); far.RestoreScreen(s)
end
Macro { description="ZBS: toggle debugging of coroutines (macros will be reloaded)";
area="Editor"; filemask="*.lua"; key="CtrlF5";
action=function()
local msg = ([[Current status: %senabled]]):format(DebugCoro() and "" or "not ")
local ans = far.Message(msg,"Debugging of coroutines","Enable;&Disable","l")
if ans~=-1 then
DebugCoro(ans==1 and true or false)
if far.MacroLoadAll() then MessagePopup("Macros reloaded","Debugger") end
end
end;
}
Macro { description="ZBS: attach debugger";
area="Editor"; filemask="*.lua"; key="F5";
action=function()
local coroinfo = DebugCoro() and "" or "\n[Debugging of coroutines not enabled]"
if DebuggerIsAttached() then
MessagePopup("Current status: attached"..coroinfo,"Debugger","",1000)
elseif mobdebug.start() then
DebuggerIsAttached(true)
MessagePopup("Attaching..."..coroinfo,"Debugger")
os.exit = function()
mobdebug.done(); os.exit = ZBS.osexit
DebuggerIsAttached(false);
MessagePopup("Detaching...","Debugger")
end
else
MessagePopup("Server is not available","Debugger","w")
end
end;
}
Macro { description="ZBS: detach debugger";
area="Editor"; filemask="*.lua"; key="ShiftF5";
action=function()
if DebuggerIsAttached() then
os.exit()
else
MessagePopup("Not attached","Debugger","w")
end
end;
}
Macro { description="ZBS: debug hook toggle";
area="Common"; key="AltF5"; priority=60;
condition=DebuggerIsAttached;
action=function()
ZBS.on = not ZBS.on
local action = ZBS.on and "on" or "off"
mobdebug[action]()
MessagePopup("Turning "..action.."...","Debugger")
end;
}
local function norm (name) --!!depends on projectroot
return name:lower():gsub(ZBS.projectroot,""):gsub("\\","/")
end
local function BreakHere(d)
local info = debug.getinfo(2+(d or 0))
local name,line = norm(info.source:match"@(.+)"),info.currentline
mobdebug.setbreakpoint(name,line);
mf.postmacro(mobdebug.removebreakpoint,name,line)
end
Macro { description="ZBS: break execution at next executed line of code";
area="Common"; key="ShiftF9"; priority=60;
condition=DebuggerIsAttached;
action=function() mobdebug.on(); BreakHere() end;
}
local bp = ZBS.breakpoints
Macro { description="ZBS: toggle breakpoint";
area="Editor";filemask="*.lua"; key="F9";
action=function()
local file,line = norm(Editor.FileName),Editor.CurLine
local action = (bp[file] and bp[file][line]) and "remove" or "set"
bp[file] = bp[file] or {}
bp[file][line] = action=="set"
mobdebug[action.."breakpoint"](file,line)
MessagePopup("Breakpoint "..action,"Debugger")
end;
}