From b39db791f91788eb5bee667f684f1e3503a9a33e Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Thu, 17 Jun 2021 10:13:04 +0200 Subject: [PATCH 1/5] Do not duplicate RootView method in contextmenu The method RootView:on_mouse_pressed was copied in the contextmenu plugin with a small modification to intercept the mouse clicks of the active view. This approach is problematic because a relatively large portion of code is duplicated. We introduced a function named RootView.on_view_mouse_pressed to let plugins like contextmenu intercepts mouse clicks in the active area without duplicating the function RootView:on_mouse_pressed. --- data/core/rootview.lua | 10 +++++++++- data/plugins/contextmenu.lua | 37 +++++++----------------------------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/data/core/rootview.lua b/data/core/rootview.lua index 1fd5135d..aa9a40cb 100644 --- a/data/core/rootview.lua +++ b/data/core/rootview.lua @@ -738,6 +738,12 @@ function RootView:close_all_docviews() end +-- Function to intercept mouse pressed events on the active view. +-- Do nothing by default. +function RootView.on_view_mouse_pressed(button, x, y, clicks) +end + + function RootView:on_mouse_pressed(button, x, y, clicks) local div = self.root_node:get_divider_overlapping_point(x, y) if div then @@ -759,7 +765,9 @@ function RootView:on_mouse_pressed(button, x, y, clicks) end else core.set_active_view(node.active_view) - node.active_view:on_mouse_pressed(button, x, y, clicks) + if not self.on_view_mouse_pressed(button, x, y, clicks) then + node.active_view:on_mouse_pressed(button, x, y, clicks) + end end end diff --git a/data/plugins/contextmenu.lua b/data/plugins/contextmenu.lua index 6320c5fc..4de46080 100644 --- a/data/plugins/contextmenu.lua +++ b/data/plugins/contextmenu.lua @@ -219,43 +219,20 @@ end local menu = ContextMenu() -local root_view_on_mouse_pressed = RootView.on_mouse_pressed -local root_view_on_mouse_moved = RootView.on_mouse_moved +local on_view_mouse_pressed = RootView.on_view_mouse_pressed +local on_mouse_moved = RootView.on_mouse_moved local root_view_update = RootView.update local root_view_draw = RootView.draw function RootView:on_mouse_moved(...) if menu:on_mouse_moved(...) then return end - root_view_on_mouse_moved(self, ...) + on_mouse_moved(self, ...) end --- this function is mostly copied from lite-xl's source -function RootView:on_mouse_pressed(button, x,y, clicks) - local div = self.root_node:get_divider_overlapping_point(x, y) - if div then - self.dragged_divider = div - return - end - local node = self.root_node:get_child_overlapping_point(x, y) - if node.hovered_scroll_button > 0 then - node:scroll_tabs(node.hovered_scroll_button) - return - end - local idx = node:get_tab_overlapping_point(x, y) - if idx then - if button == "middle" or node.hovered_close == idx then - node:close_view(self.root_node, node.views[idx]) - else - self.dragged_node = { node, idx or #node.views } - node:set_active_view(node.views[idx]) - end - else - core.set_active_view(node.active_view) - -- send to context menu first - if not menu:on_mouse_pressed(button, x, y, clicks) then - node.active_view:on_mouse_pressed(button, x, y, clicks) - end - end +function RootView.on_view_mouse_pressed(button, x, y, clicks) + -- We give the priority to the menu to process mouse pressed events. + local handled = menu:on_mouse_pressed(button, x, y, clicks) + return handled or on_view_mouse_pressed(button, x, y, clicks) end function RootView:update(...) From 4c9083398a6faa08568f2010a196fceb2dfee582 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Thu, 17 Jun 2021 18:23:30 +0200 Subject: [PATCH 2/5] Fix error in dirname computation in TreeView In TreeView:on_mouse_pressed() we need to find the directory a relative filename belongs to from its absolute filename. The code was using string.find to locate the relative filename within the absolute path but in some very specific cases we can find a pattern which is not the right-most one leading to a wrong directory name. Fix the error by adding a loop to make sure we find the right-most match. The standard Lua library has not a string.rfind to make a reverse search. Close #275 --- data/plugins/treeview.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 8615343a..5c2af3c4 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -222,7 +222,14 @@ function TreeView:on_mouse_pressed(button, x, y, clicks) else if core.project_files_limit and not hovered_item.expanded then local filename, abs_filename = hovered_item.filename, hovered_item.abs_filename - local index = string.find(abs_filename, filename, 1, true) + local index = 0 + -- The loop below is used to find the first match starting from the end + -- in case there are multiple matches. + while index and index + #filename < #abs_filename do + index = string.find(abs_filename, filename, index + 1, true) + end + -- we assume here index is not nil because the abs_filename must contain the + -- relative filename local dirname = string.sub(abs_filename, 1, index - 2) core.scan_project_folder(dirname, filename) self:invalidate_cache(dirname) From 1ad4289e769486388db531a7fd55cc02ee40ed8a Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Thu, 17 Jun 2021 19:07:32 +0200 Subject: [PATCH 3/5] Do not try to update topdir folder in treeview If the directory expanded is a project's top directory do not attempt to update its content. Fix again issue #275 --- data/core/init.lua | 10 ++++++++++ data/plugins/treeview.lua | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index 7adb316b..e571e6b4 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -195,6 +195,16 @@ local function project_scan_thread() end +function core.is_project_folder(dirname) + for _, dir in ipairs(core.project_directories) do + if dir.name == dirname then + return true + end + end + return false +end + + function core.scan_project_folder(dirname, filename) for _, dir in ipairs(core.project_directories) do if dir.name == dirname then diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 5c2af3c4..8214bda4 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -231,8 +231,10 @@ function TreeView:on_mouse_pressed(button, x, y, clicks) -- we assume here index is not nil because the abs_filename must contain the -- relative filename local dirname = string.sub(abs_filename, 1, index - 2) - core.scan_project_folder(dirname, filename) - self:invalidate_cache(dirname) + if core.is_project_folder(dirname) then + core.scan_project_folder(dirname, filename) + self:invalidate_cache(dirname) + end end hovered_item.expanded = not hovered_item.expanded end From bdc37f1f6cba10ce3a643e68a19556136866f751 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 17 Jun 2021 16:26:27 -0400 Subject: [PATCH 4/5] Added in remove file function. (#272) * Added in remove file function. * Changed namespace of rename and remove (now delete). --- data/core/commands/doc.lua | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 21866892..c8c27509 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -238,7 +238,7 @@ local commands = { ["doc:lower-case"] = function() doc():replace(string.lower) end, - + ["doc:go-to-line"] = function() local dv = dv() @@ -297,7 +297,7 @@ local commands = { end end, - ["doc:rename"] = function() + ["file:rename"] = function() local old_filename = doc().filename if not old_filename then core.error("Cannot rename unsaved doc") @@ -312,6 +312,21 @@ local commands = { end end, common.path_suggest) end, + + + ["file:delete"] = function() + local filename = doc().abs_filename + if not filename then + core.error("Cannot remove unsaved doc") + return + end + for i,docview in ipairs(core.get_views_referencing_doc(doc())) do + local node = core.root_view.root_node:get_node_for_view(docview) + node:close_view(core.root_view, docview) + end + os.remove(filename) + core.log("Removed \"%s\"", filename) + end } From 5151e36981abf247d2d45258c5facc53d8f7f4d2 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 17 Jun 2021 17:15:08 -0400 Subject: [PATCH 5/5] Added in the ability to customize the config directory used with the environment variable XDG_CONFIG_HOME. (#271) --- data/core/start.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/core/start.lua b/data/core/start.lua index 513fda22..3ef806cc 100644 --- a/data/core/start.lua +++ b/data/core/start.lua @@ -12,7 +12,7 @@ else local prefix = EXEDIR:match("^(.+)[/\\]bin$") DATADIR = prefix and (prefix .. '/share/lite-xl') or (EXEDIR .. '/data') end -USERDIR = HOME and (HOME .. '/.config/lite-xl') or (EXEDIR .. '/user') +USERDIR = os.getenv("XDG_CONFIG_HOME") or (HOME and (HOME .. '/.config/lite-xl') or (EXEDIR .. '/user')) package.path = DATADIR .. '/?.lua;' .. package.path package.path = DATADIR .. '/?/init.lua;' .. package.path