From c14732e95d0b6ee476ebe6d8856e8bc35e181f96 Mon Sep 17 00:00:00 2001 From: okan Date: Sat, 23 Jan 2021 17:55:18 +0300 Subject: [PATCH 1/3] New pluging 'stateful_switch' improves working on long text files in different split panes --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bcee2ac8..ca09e5b8 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Plugin | Description [`selectionhighlight`](plugins/selectionhighlight.lua?raw=1) | Highlights regions of code that match the current selection *([screenshot](https://user-images.githubusercontent.com/3920290/80710883-5f597c80-8ae7-11ea-97f0-76dfacc08439.png))* [`sort`](plugins/sort.lua?raw=1) | Sorts selected lines alphabetically [`spellcheck`](plugins/spellcheck.lua?raw=1) | Underlines misspelt words *([screenshot](https://user-images.githubusercontent.com/3920290/79923973-9caa7400-842e-11ea-85d4-7a196a91ca50.png))* *— note: on Windows a [`words.txt`](https://github.com/dwyl/english-words/blob/master/words.txt) dictionary file must be placed beside the exe* +[`stateful_switch`](plugins/stateful_switch.lua?raw=1) | Improved switching between split panes which are displaying the same text file [`tabnumbers`](plugins/tabnumbers.lua?raw=1) | Displays tab numbers from 1–9 next to their names *([screenshot](https://user-images.githubusercontent.com/16415678/101285362-007a8500-37e5-11eb-869b-c10eb9d9d902.png)) [`theme16`](https://github.com/monolifed/theme16)* | Theme manager with base16 themes [`titleize`](plugins/titleize.lua?raw=1) | Titleizes selected string (`hello world` => `Hello World`) From 937e1221a71ea48410600ef7578594ca91682040 Mon Sep 17 00:00:00 2001 From: okan Date: Sat, 23 Jan 2021 18:04:02 +0300 Subject: [PATCH 2/3] New pluging 'stateful_switch' improves working on long text files in different split panes --- plugins/stateful_switch.lua | 102 ++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 plugins/stateful_switch.lua diff --git a/plugins/stateful_switch.lua b/plugins/stateful_switch.lua new file mode 100644 index 00000000..756de201 --- /dev/null +++ b/plugins/stateful_switch.lua @@ -0,0 +1,102 @@ +local core = require "core" +local command = require "core.command" +local keymap = require "core.keymap" + + +-- Keeps the line and column number where cursor was while switching +-- between split panes. It is useful when working on a large +-- text file in multiple panes. + + +local function new_weak_table() + return setmetatable({ }, { __mode = "v" }) +end + + +local function dv() + return core.active_view +end + + +local view_states = { } + + +local function find_in_view_state(view_state, node) + for k, v in pairs(view_state) do + if v.node == node then + return k + end + end + + return -1 +end + + +local function save_view_state() + local node = core.root_view:get_active_node() + local idx = node:get_view_idx(dv()) + local line, col = dv().doc:get_selection(false) + + local view_state = view_states[idx] + if view_state == nil then + view_state = { } + end + + local ind = find_in_view_state(view_state, node) + + local state + if ind > -1 then + state = view_state[ind] + state.line = line + state.col = col + + view_state[ind] = nil + else + state = new_weak_table() + state.node = node + state.line = line + state.col = col + end + + table.insert(view_state, state) + view_states[idx] = view_state +end + + +local function restore_view_state() + local node = core.root_view:get_active_node() + local idx = node:get_view_idx(dv()) + + local view_state = view_states[idx] + if view_state then + local ind = find_in_view_state(view_state, node) + if ind > -1 then + local state = view_state[ind] + local line = state.line + local col = state.col + + dv().doc:set_selection(line, col) + dv():scroll_to_line(line, true) + end + end +end + + +for _, dir in ipairs { "left", "right", "up", "down" } do + command.add(nil, { + ["stateful-switch:switch-to-" .. dir] = function () + save_view_state() + command.perform("root:switch-to-" .. dir) + restore_view_state() + end + }) +end + + +keymap.add({ + ["alt+j"] = "stateful-switch:switch-to-left", + ["alt+l"] = "stateful-switch:switch-to-right", + ["alt+i"] = "stateful-switch:switch-to-up", + ["alt+k"] = "stateful-switch:switch-to-down" +}) + From 614b18368c9e767d7f082a67490b489f2569f274 Mon Sep 17 00:00:00 2001 From: okan Date: Tue, 16 Feb 2021 23:32:49 +0300 Subject: [PATCH 3/3] Code is refactored, simplified. Switching between tabs is also considered now. --- plugins/stateful_switch.lua | 66 ++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/plugins/stateful_switch.lua b/plugins/stateful_switch.lua index 756de201..16767ee0 100644 --- a/plugins/stateful_switch.lua +++ b/plugins/stateful_switch.lua @@ -21,9 +21,9 @@ end local view_states = { } -local function find_in_view_state(view_state, node) +local function find_in_view_state(view_state, node, idx) for k, v in pairs(view_state) do - if v.node == node then + if v.node == node and v.idx == idx then return k end end @@ -35,31 +35,25 @@ end local function save_view_state() local node = core.root_view:get_active_node() local idx = node:get_view_idx(dv()) + if dv().doc == nil then + return + end + local line, col = dv().doc:get_selection(false) - local view_state = view_states[idx] - if view_state == nil then - view_state = { } - end + local ind = find_in_view_state(view_states, node, idx) - local ind = find_in_view_state(view_state, node) + local state = new_weak_table() + state.node = node + state.idx = idx + state.line = line + state.col = col - local state if ind > -1 then - state = view_state[ind] - state.line = line - state.col = col - - view_state[ind] = nil - else - state = new_weak_table() - state.node = node - state.line = line - state.col = col + view_states[ind] = nil end - table.insert(view_state, state) - view_states[idx] = view_state + table.insert(view_states, state) end @@ -67,17 +61,14 @@ local function restore_view_state() local node = core.root_view:get_active_node() local idx = node:get_view_idx(dv()) - local view_state = view_states[idx] - if view_state then - local ind = find_in_view_state(view_state, node) - if ind > -1 then - local state = view_state[ind] - local line = state.line - local col = state.col + local ind = find_in_view_state(view_states, node, idx) + if ind > -1 then + local state = view_states[ind] + local line = state.line + local col = state.col - dv().doc:set_selection(line, col) - dv():scroll_to_line(line, true) - end + dv().doc:set_selection(line, col) + dv():scroll_to_line(line, true) end end @@ -93,10 +84,23 @@ for _, dir in ipairs { "left", "right", "up", "down" } do end +for _, dir in ipairs { "previous", "next" } do + command.add(nil, { + ["stateful-switch:switch-to-" .. dir .. "-tab"] = function () + save_view_state() + command.perform("root:switch-to-" .. dir .. "-tab") + restore_view_state() + end + }) +end + + keymap.add({ ["alt+j"] = "stateful-switch:switch-to-left", ["alt+l"] = "stateful-switch:switch-to-right", ["alt+i"] = "stateful-switch:switch-to-up", - ["alt+k"] = "stateful-switch:switch-to-down" + ["alt+k"] = "stateful-switch:switch-to-down", + ["ctrl+tab"] = "stateful-switch:switch-to-next-tab", + ["ctrl+shift+tab"] = "stateful-switch:switch-to-previous-tab" })