From 4e93eabbacba2743d324e7a53ae3f3e1b5b0eaca Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Mon, 31 May 2021 09:41:37 +0200 Subject: [PATCH] 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. --- data/core/init.lua | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index a7ae2a2..36cc1d0 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) + doc_save(self) + 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