From cf995a4cf4ef0dc9e72939e0e2af3853694c20d1 Mon Sep 17 00:00:00 2001 From: jgmdev Date: Mon, 7 Jun 2021 01:36:12 -0400 Subject: Added navigate plugin. --- plugins/navigate.lua | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 plugins/navigate.lua 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 -- cgit v1.2.3 From 2e96190a9767ef4be8827b4b3e0d2cc3d3f8062e Mon Sep 17 00:00:00 2001 From: jgmdev Date: Mon, 7 Jun 2021 01:36:26 -0400 Subject: Updated readme. --- README.md | 1 + 1 file changed, 1 insertion(+) 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~~ -- cgit v1.2.3