aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--plugins/autosaveonfocuslost.lua31
2 files changed, 32 insertions, 0 deletions
diff --git a/README.md b/README.md
index 62a3582..8027c04 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,7 @@ Plugin | Description
-------|-----------------------------------------
[`autoinsert`](plugins/autoinsert.lua?raw=1) | Automatically inserts closing brackets and quotes. Also allows selected text to be wrapped with brackets or quotes.
*[`autosave`](plugins/autosave.lua?raw=1)* | Automatically saves files when they are changed
+*[`autosaveonfocuslost`](plugins/autosaveonfocuslost.lua?raw=1)* | Automatically saves files that were changed when the main window loses focus by switching to another application
[`autowrap`](plugins/autowrap.lua?raw=1) | Automatically hardwraps lines when typing
*[`bigclock`](plugins/bigclock.lua?raw=1)* | Shows the current time and date in a view with large text *([screenshot](https://user-images.githubusercontent.com/3920290/82752891-3318df00-9db9-11ea-803f-261d80d5cf53.png))*
[`black`](https://git.sr.ht/~tmpod/black-lite)* | Integrates the [black](https://github.com/psf/black) Python formatter with lite
diff --git a/plugins/autosaveonfocuslost.lua b/plugins/autosaveonfocuslost.lua
new file mode 100644
index 0000000..bc193a8
--- /dev/null
+++ b/plugins/autosaveonfocuslost.lua
@@ -0,0 +1,31 @@
+-- mod-version:1 -- lite-xl 1.16
+local core = require "core"
+local CommandView = require "core.commandview"
+local DocView = require "core.docview"
+local RootView = require "core.rootview"
+
+local on_focus_lost = RootView.on_focus_lost
+
+local function save_node(node)
+ if node.type == "leaf" then
+ local i = 1
+ while i <= #node.views do
+ local view = node.views[i]
+ if view:is(DocView) and not view:is(CommandView) and
+ view.doc.filename and view.doc:is_dirty() then
+ core.log("Saving doc \"%s\"", view.doc.filename)
+ view.doc:save()
+ end
+ i = i + 1
+ end
+ else
+ if node.a then save_node(node.a) end
+ if node.b then save_node(node.b) end
+ end
+end
+
+function RootView:on_focus_lost(...)
+ save_node(core.root_view.root_node)
+
+ return on_focus_lost(...)
+end