aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancesco <francesco.bbt@gmail.com>2021-06-07 22:56:49 +0200
committerGitHub <noreply@github.com>2021-06-07 22:56:49 +0200
commitebcc96175fb8d2c91edf1a0c2f06179996f607f2 (patch)
treef66ab785705b6d9dc416e79955efb0e33a873202
parent762b96a1e68be83f0dbcf1d7a0f4ef2c82b15e87 (diff)
parent2e96190a9767ef4be8827b4b3e0d2cc3d3f8062e (diff)
downloadlite-xl-plugins-ebcc96175fb8d2c91edf1a0c2f06179996f607f2.tar.gz
lite-xl-plugins-ebcc96175fb8d2c91edf1a0c2f06179996f607f2.zip
Merge pull request #26 from jgmdev/navigate-plugin
Added navigate plugin
-rw-r--r--README.md1
-rw-r--r--plugins/navigate.lua147
2 files changed, 148 insertions, 0 deletions
diff --git a/README.md b/README.md
index 91c5c0b..4af61c0 100644
--- a/README.md
+++ b/README.md
@@ -94,6 +94,7 @@ Plugin | Description
[`memoryusage`](plugins/memoryusage.lua?raw=1) | Show memory usage in the status view
[`motiontrail`](plugins/motiontrail.lua?raw=1) | Adds a motion-trail to the caret *([screenshot](https://user-images.githubusercontent.com/3920290/83256814-085ccb00-a1ab-11ea-9e35-e6633cbed1a9.gif))*
[`nagbar`](https://github.com/takase1121/lite-nagbar)* | consistent and _beautiful_ confirmation dialogs for lite and lite-xl *([gif](https://raw.githubusercontent.com/takase1121/lite-nagbar/master/assets/preview.gif))*
+[`navigate`](plugins/navigate.lua?raw=1) | Allows moving back and forward between document positions, reducing the amount of scrolling
[`openfilelocation`](plugins/openfilelocation.lua?raw=1) | Opens the parent directory of the current file in the file manager
[`openselected`](plugins/openselected.lua?raw=1) | Opens the selected filename or url
~~[`projectmanager`](plugins/projectmanager.lua?raw=1)~~ | Integrated in lite-xl with improvements ~~Save projects and load/reload them quickly~~
diff --git a/plugins/navigate.lua b/plugins/navigate.lua
new file mode 100644
index 0000000..5d37e76
--- /dev/null
+++ b/plugins/navigate.lua
@@ -0,0 +1,147 @@
+-- mod-version:1 -- lite-xl 1.16
+
+local core = require "core"
+local common = require "core.common"
+local command = require "core.command"
+local config = require "core.config"
+local keymap = require "core.keymap"
+local DocView = require "core.docview"
+
+local navigate = {
+ list = {},
+ current = nil,
+ index = 0
+}
+
+--
+-- Private functions
+--
+local function get_active_view()
+ if getmetatable(core.active_view) == DocView then
+ return core.active_view
+ end
+ return nil
+end
+
+local function add(doc)
+ -- Make new navigation point last in list
+ if navigate.index > 0 and navigate.index < #navigate.list then
+ local list_len = #navigate.list
+ for index=navigate.index+1, list_len, 1 do
+ if navigate.list[index] then
+ table.remove(navigate.list, index)
+ end
+ end
+ end
+
+ local line, col = doc:get_selection()
+ table.insert(navigate.list, {
+ filename = doc.filename,
+ line = line,
+ col = col
+ })
+
+ navigate.current = navigate.list[#navigate.list]
+ navigate.index = #navigate.list
+end
+
+local function open_doc(doc)
+ core.root_view:open_doc(
+ core.open_doc(
+ common.home_expand(
+ doc.filename
+ )
+ )
+ )
+
+ local av_doc = get_active_view().doc
+ local line, col = av_doc:get_selection()
+ if doc.line ~= line or doc.col ~= col then
+ av_doc:set_selection(doc.line, doc.col, doc.line, doc.col)
+ end
+end
+
+--
+-- Public functions
+--
+function navigate.next()
+ if navigate.index < #navigate.list then
+ navigate.index = navigate.index + 1
+ navigate.current = navigate.list[navigate.index]
+ open_doc(navigate.current)
+ end
+end
+
+function navigate.prev()
+ if navigate.index > 1 then
+ navigate.index = navigate.index - 1
+ navigate.current = navigate.list[navigate.index]
+ open_doc(navigate.current)
+ end
+end
+
+--
+-- Thread
+--
+core.add_thread(function()
+ while true do
+ local av = get_active_view()
+ if av and av.doc and av.doc.filename then
+ local doc = av.doc
+ local line, col = doc:get_selection()
+ local current = navigate.current
+ if
+ not current
+ or
+ current.filename ~= doc.filename
+ or
+ current.line ~= line
+ then
+ add(doc)
+ else
+ current.col = col
+ end
+ end
+
+ if system.window_has_focus() then
+ coroutine.yield(0.5)
+ else
+ coroutine.yield(config.project_scan_rate)
+ end
+ end
+end)
+
+core.add_close_hook(function(doc)
+ local filename = doc.filename
+ local list = {table.unpack(navigate.list)}
+ for index, position in ipairs(list) do
+ if position.filename == filename then
+ if navigate.list[index] then
+ table.remove(navigate.list, index)
+ end
+ end
+ end
+end)
+
+--
+-- Commands
+--
+command.add("core.docview", {
+ ["navigate:previous"] = function()
+ navigate.prev()
+ end,
+
+ ["navigate:next"] = function()
+ navigate.next()
+ end,
+})
+
+--
+-- Default Keybindings
+--
+keymap.add {
+ ["alt+left"] = "navigate:previous",
+ ["alt+right"] = "navigate:next",
+}
+
+return navigate