blob: 11337f76ec887954b017d996911763bb2553a97f (
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
|
local core = require "core"
local config = require "core.config"
local Doc = require "core.doc"
local DocView = require "core.docview"
local command = require "core.command"
-- this is used to detect the wait time
local last_keypress = os.time()
-- this exists so that we don't end up with multiple copies of the loop running at once
local looping = false
local on_text_change = Doc.on_text_change
-- the approximate amount of time, in seconds, that it takes to trigger an autosave
config.autosave_timeout = 1
local function loop_for_save()
if not looping then
looping = true
while looping do
if os.difftime(os.time(), last_keypress) >= config.autosave_timeout then
command.perform "doc:save"
-- stop loop
looping = false
end
-- dividing by five is completely arbitrary but it seemed to work well so idc
coroutine.yield(config.autosave_timeout)
end
end
end
local function updatepress()
-- set last keypress time to now
last_keypress = os.time()
-- put loop in coroutine so it doesn't lag out this script
core.add_thread(loop_for_save)
end
function Doc:on_text_change(type)
-- check if file is saved
if core.active_view:get_name() ~= "unsaved*" then
updatepress()
end
return on_text_change(type)
end
|