aboutsummaryrefslogtreecommitdiff
path: root/data/core/init.lua
diff options
context:
space:
mode:
authorFrancesco Abbate <francesco.bbt@gmail.com>2021-05-31 09:41:37 +0200
committerFrancesco Abbate <francesco.bbt@gmail.com>2021-06-17 23:31:28 +0200
commit73bda963a5b4afa33b911beea9a8d4d39bf0b4d9 (patch)
treef0212a44b625b804e73aa4ddbf2c834b142ec99f /data/core/init.lua
parent6d044224c1ad8f629a7baaa40a912f58d30c9896 (diff)
downloadlite-xl-73bda963a5b4afa33b911beea9a8d4d39bf0b4d9.tar.gz
lite-xl-73bda963a5b4afa33b911beea9a8d4d39bf0b4d9.zip
Deprecate core.add_save_hook to override Doc:save
In order to stay simple and closer to the lite's design principles we deprecate the core.add_save_hook function and the related mechanism. Instead we now directly override the Doc:save() method. The method is already overrided from core.init to add the automatic reloading of style when user's module is saved. The cleanup is related to the discussion in issue #229.
Diffstat (limited to 'data/core/init.lua')
-rw-r--r--data/core/init.lua33
1 files changed, 22 insertions, 11 deletions
diff --git a/data/core/init.lua b/data/core/init.lua
index a7ae2a26..cc4b46d8 100644
--- a/data/core/init.lua
+++ b/data/core/init.lua
@@ -411,6 +411,20 @@ local function whitespace_replacements()
end
+local function reload_on_user_module_save()
+ -- auto-realod style when user's module is saved by overriding Doc:Save()
+ local doc_save = Doc.save
+ local user_filename = system.absolute_path(USERDIR .. PATHSEP .. "init.lua")
+ function Doc:save(filename, abs_filename)
+ doc_save(self, filename, abs_filename)
+ if self.abs_filename == user_filename then
+ core.reload_module("core.style")
+ core.load_user_directory()
+ end
+ end
+end
+
+
function core.init()
command = require "core.command"
keymap = require "core.keymap"
@@ -553,6 +567,8 @@ function core.init()
if item.text == "Exit" then os.exit(1) end
end)
end
+
+ reload_on_user_module_save()
end
@@ -612,13 +628,19 @@ do
end
+-- DEPRECATED function
core.doc_save_hooks = {}
function core.add_save_hook(fn)
+ core.error("The function core.add_save_hook is deprecated." ..
+ " Modules should now directly override the Doc:save function.")
core.doc_save_hooks[#core.doc_save_hooks + 1] = fn
end
+-- DEPRECATED function
function core.on_doc_save(filename)
+ -- for backward compatibility in modules. Hooks are deprecated, the function Doc:save
+ -- should be directly overidded.
for _, hook in ipairs(core.doc_save_hooks) do
hook(filename)
end
@@ -1065,15 +1087,4 @@ function core.on_error(err)
end
-core.add_save_hook(function(filename)
- local doc = core.active_view.doc
- local user_filename = system.absolute_path(USERDIR .. PATHSEP .. "init.lua")
- if doc and doc:is(Doc) and doc.abs_filename == user_filename then
- core.reload_module("core.style")
- core.load_user_directory()
- end
-end)
-
-
-
return core