aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <adamdharrison@gmail.com>2021-12-15 16:53:52 -0500
committerGitHub <noreply@github.com>2021-12-15 16:53:52 -0500
commitd79c5435506937f447f48238d5a748fd92d1b45d (patch)
treeaeb913543ed12cec9fbbfaf5770e9d1181eeba25
parent22022a9751ee0b059049b8e16b8b2b15db3b7727 (diff)
parent72d30b00d6012d4ea432a5f680eac37040475279 (diff)
downloadlite-xl-plugins-d79c5435506937f447f48238d5a748fd92d1b45d.tar.gz
lite-xl-plugins-d79c5435506937f447f48238d5a748fd92d1b45d.zip
Merge pull request #106 from Jipok/ephemeraldocviews
Rewrite ephemeral tabs plugin
-rw-r--r--README.md2
-rw-r--r--plugins/ephemeral_tabs.lua77
-rw-r--r--plugins/ephemeraldocviews.lua45
3 files changed, 78 insertions, 46 deletions
diff --git a/README.md b/README.md
index 8c4a59f..2e9f0c0 100644
--- a/README.md
+++ b/README.md
@@ -33,7 +33,7 @@ Plugin | Description
[`eofnewline`](https://github.com/bokunodev/lite_modules/blob/master/plugins/eofnewline-xl.lua?raw=1) | Make sure the file ends with one blank line.
[`eval`](plugins/eval.lua?raw=1) | Replaces selected Lua code with its evaluated result
[`exec`](plugins/exec.lua?raw=1) | Runs selected text through shell command and replaces with result
-[`ephemeraldocviews`](plugins/ephemeraldocviews.lua?raw=1) | Preview tabs. Opening a doc will replace the contents of the preview tab. Marks tabs as non-preview on any change.
+[`ephemeral_tabs`](plugins/ephemeral_tabs.lua?raw=1) | Preview tabs. Opening a doc will replace the contents of the preview tab. Marks tabs as non-preview on any change or tab double clicking.
*[`extend_selection_line`](plugins/extend_selection_line.lua?raw=1)* | When a selection crosses multiple lines, it is drawn to the end of the screen *([screenshot](https://user-images.githubusercontent.com/2798487/140995616-89a20b55-5917-4df8-8a7c-d7c53732fa8b.png))*
[`fallbackfonts`](https://github.com/takase1121/lite-fallback-fonts)* | Adds support for fallback fonts *([gif](https://raw.githubusercontent.com/takase1121/lite-fallback-fonts/master/assets/Iw18fI57J0.gif))*
[`fontconfig`](plugins/fontconfig.lua?raw=1) | Allows users to load fonts with [fontconfig](https://www.freedesktop.org/software/fontconfig/fontconfig-user.html).
diff --git a/plugins/ephemeral_tabs.lua b/plugins/ephemeral_tabs.lua
new file mode 100644
index 0000000..dea4261
--- /dev/null
+++ b/plugins/ephemeral_tabs.lua
@@ -0,0 +1,77 @@
+-- mod-version:2 -- lite-xl 2.0
+local core = require "core"
+local command = require "core.command"
+local RootView = require "core.rootview"
+local DocView = require "core.docview"
+local Doc = require "core.doc"
+local TreeView = require "plugins.treeview"
+
+local RootView_open_doc = RootView.open_doc
+function RootView:open_doc(doc)
+ local docview = RootView_open_doc(self, doc)
+ -- The absence of the ephemeral flag means that before this moment in this
+ -- node this document was not exists
+ if docview.ephemeral == nil then
+ local node = self:get_active_node_default()
+ -- We assume that ephemeral tab is always the last one
+ -- But user can drag and drop tabs so full check is needed
+ for i, v in ipairs(node.views) do
+ if v.ephemeral then
+ node:close_view(self.root_node, v)
+ end
+ end
+ docview.ephemeral = true
+ end
+ return docview
+end
+
+local Doc_get_name = DocView.get_name
+function DocView:get_name()
+ return self.doc and self.ephemeral and ("~ " .. Doc_get_name(self) .. " ~")
+ or Doc_get_name(self)
+end
+
+-- Any change to the document makes the tab normal
+local Doc_on_text_change = Doc.on_text_change
+function Doc:on_text_change(type)
+ core.active_view.ephemeral = false
+ Doc_on_text_change(self, type)
+end
+
+-- Double clicking in the TreeView makes the tab normal
+local TreeView_on_mouse_pressed = TreeView.on_mouse_pressed
+function TreeView:on_mouse_pressed(button, x, y, clicks)
+ local result = TreeView_on_mouse_pressed(self, button, x, y, clicks)
+ if (clicks > 1) and (core.active_view.doc ~= nil) then
+ core.active_view.ephemeral = false
+ end
+ return result
+end
+
+-- Double clicking on a tab makes it normal
+local RootView_on_mouse_pressed = RootView.on_mouse_pressed
+function RootView:on_mouse_pressed(button, x, y, clicks)
+ local result = RootView_on_mouse_pressed(self, button, x, y, clicks)
+ if clicks > 1 then
+ local node = self.root_node:get_child_overlapping_point(x, y)
+ local idx = node:get_tab_overlapping_point(x, y)
+ if idx then
+ node.views[idx].ephemeral = false
+ end
+ end
+ return result
+end
+
+-- Dragging a tab makes it normal
+local RootView_on_mouse_released = RootView.on_mouse_released
+function RootView:on_mouse_released(button, x, y, ...)
+ if self.dragged_node then
+ if button == "left" then
+ if self.dragged_node.dragging then
+ local view = self.dragged_node.node.views[self.dragged_node.idx]
+ view.ephemeral = false
+ end
+ end
+ end
+ return RootView_on_mouse_released(self, button, x, y, ...)
+end
diff --git a/plugins/ephemeraldocviews.lua b/plugins/ephemeraldocviews.lua
deleted file mode 100644
index cb9f53d..0000000
--- a/plugins/ephemeraldocviews.lua
+++ /dev/null
@@ -1,45 +0,0 @@
--- mod-version:2 -- lite-xl 2.0
-local core = require "core"
-local command = require "core.command"
-local RootView = require "core.rootview"
-local DocView = require "core.docview"
-local Doc = require "core.doc"
-
-local open_doc = RootView.open_doc
-function RootView:open_doc(doc)
- local node = self:get_active_node_default()
- local ephemeral, existing_ephemeral = node.views, nil
- for i, view in ipairs(node.views) do
- if view.doc == doc then
- ephemeral = false
- end
- if view.doc and view.doc.ephemeral then
- existing_ephemeral = view
- end
- end
- if ephemeral and existing_ephemeral then
- node:close_view(self.root_node, existing_ephemeral)
- end
- local view = open_doc(self, doc)
- if ephemeral then
- view.doc.ephemeral = #node.views > 1
- end
- return view
-end
-
-local get_name = DocView.get_name
-function DocView:get_name()
- return self.doc and self.doc.ephemeral and ("-- " .. get_name(self) .. " --") or get_name(self)
-end
-
-local doc_insert = Doc.insert
-function Doc:insert(...)
- doc_insert(self, ...)
- self.ephemeral = false
-end
-
-local doc_remove = Doc.remove
-function Doc:remove(...)
- doc_remove(self, ...)
- self.ephemeral = false
-end