aboutsummaryrefslogtreecommitdiff
path: root/data/plugins/autoreload.lua
blob: 9978092eec57964415fc6103a5555f21c0faa6bc (plain)
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
-- mod-version:2 -- lite-xl 2.0
local core = require "core"
local config = require "core.config"
local Doc = require "core.doc"

local times = setmetatable({}, { __mode = "k" })

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)
  doc:insert(1, 1, text:gsub("\r", ""):gsub("\n$", ""))
  doc:set_selection(table.unpack(sel))

  update_time(doc)
  doc:clean()
  core.log_quiet("Auto-reloaded doc \"%s\"", doc.filename)
end

local on_modify = core.on_dirmonitor_modify

core.on_dirmonitor_modify = function(dir, filepath)
  local abs_filename = dir.name .. PATHSEP .. filepath
  for _, doc in ipairs(core.docs) do
    local info = system.get_file_info(doc.filename or "")
    if doc.abs_filename == abs_filename and info and times[doc] ~= info.modified then
      reload_doc(doc)
      break
    end
  end
  on_modify(dir, filepath)
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