diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index d7e6be2f..17a0a83d 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -294,7 +294,7 @@ local commands = { ["doc:lower-case"] = function() doc():replace(string.lower) end, - + ["doc:go-to-line"] = function() local dv = dv() @@ -353,7 +353,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") @@ -370,6 +370,21 @@ local commands = { return common.home_encode_list(common.path_suggest(common.home_expand(text))) end) 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 } diff --git a/data/core/init.lua b/data/core/init.lua index 24dccdd1..ce95a2c3 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -189,6 +189,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/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/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 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(...) diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 8615343a..8214bda4 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -222,10 +222,19 @@ 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) + 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