2021-08-17 15:46:21 +02:00
|
|
|
-- mod-version:2 -- lite-xl 2.0
|
2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local config = require "core.config"
|
|
|
|
local Doc = require "core.doc"
|
|
|
|
|
|
|
|
local times = setmetatable({}, { __mode = "k" })
|
|
|
|
|
2021-07-14 22:57:37 +02:00
|
|
|
local autoreload_scan_rate = 5
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
local function update_time(doc)
|
|
|
|
local info = system.get_file_info(doc.filename)
|
|
|
|
times[doc] = info.modified
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function reload_doc(doc)
|
|
|
|
local fp = io.open(doc.filename, "r")
|
|
|
|
local text = fp:read("*a")
|
|
|
|
fp:close()
|
|
|
|
|
|
|
|
local sel = { doc:get_selection() }
|
|
|
|
doc:remove(1, 1, math.huge, math.huge)
|
2020-05-21 10:57:26 +02:00
|
|
|
doc:insert(1, 1, text:gsub("\r", ""):gsub("\n$", ""))
|
2019-12-28 12:16:32 +01:00
|
|
|
doc:set_selection(table.unpack(sel))
|
|
|
|
|
|
|
|
update_time(doc)
|
|
|
|
doc:clean()
|
2020-04-25 14:26:55 +02:00
|
|
|
core.log_quiet("Auto-reloaded doc \"%s\"", doc.filename)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
core.add_thread(function()
|
|
|
|
while true do
|
|
|
|
-- check all doc modified times
|
|
|
|
for _, doc in ipairs(core.docs) do
|
|
|
|
local info = system.get_file_info(doc.filename or "")
|
|
|
|
if info and times[doc] ~= info.modified then
|
|
|
|
reload_doc(doc)
|
|
|
|
end
|
|
|
|
coroutine.yield()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- wait for next scan
|
2021-07-14 22:57:37 +02:00
|
|
|
coroutine.yield(autoreload_scan_rate)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
-- patch `Doc.save|load` to store modified time
|
|
|
|
local load = Doc.load
|
|
|
|
local save = Doc.save
|
|
|
|
|
|
|
|
Doc.load = function(self, ...)
|
|
|
|
local res = load(self, ...)
|
|
|
|
update_time(self)
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
Doc.save = function(self, ...)
|
|
|
|
local res = save(self, ...)
|
|
|
|
update_time(self)
|
|
|
|
return res
|
|
|
|
end
|