From 73bda963a5b4afa33b911beea9a8d4d39bf0b4d9 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/commands/doc.lua | 12 ++++++------ data/core/init.lua | 33 ++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index ffaf029b..8165b426 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -47,7 +47,7 @@ local function save(filename) core.log("Saved \"%s\"", saved_filename) end --- returns the size of the original indent, and the indent +-- returns the size of the original indent, and the indent -- in your config format, rounded either up or down local function get_line_indent(line, rnd_up) local _, e = line:find("^[ \t]+") @@ -58,18 +58,18 @@ local function get_line_indent(line, rnd_up) else local indent = e and line:sub(1, e):gsub("\t", soft_tab) or "" local number = #indent / #soft_tab - return e, indent:sub(1, + return e, indent:sub(1, (rnd_up and math.ceil(number) or math.floor(number))*#soft_tab) end end -- un/indents text; behaviour varies based on selection and un/indent. --- * if there's a selection, it will stay static around the +-- * if there's a selection, it will stay static around the -- text for both indenting and unindenting. --- * if you are in the beginning whitespace of a line, and are indenting, the +-- * if you are in the beginning whitespace of a line, and are indenting, the -- cursor will insert the exactly appropriate amount of spaces, and jump the -- cursor to the beginning of first non whitespace characters --- * if you are not in the beginning whitespace of a line, and you indent, it +-- * if you are not in the beginning whitespace of a line, and you indent, it -- inserts the appropriate whitespace, as if you typed them normally. -- * if you are unindenting, the cursor will jump to the start of the line, -- and remove the appropriate amount of spaces (or a tab). @@ -83,7 +83,7 @@ local function indent_text(unindent) for line = line1, line2 do local e, rnded = get_line_indent(doc().lines[line], unindent) doc():remove(line, 1, line, (e or 0) + 1) - doc():insert(line, 1, + doc():insert(line, 1, unindent and rnded:sub(1, #rnded - #text) or rnded .. text) end l1d, l2d = #doc().lines[line1] - l1d, #doc().lines[line2] - l2d 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