aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--plugins/tab_switcher.lua60
2 files changed, 61 insertions, 0 deletions
diff --git a/README.md b/README.md
index b1e33b7..3291515 100644
--- a/README.md
+++ b/README.md
@@ -172,6 +172,7 @@ asterisk.*
| [`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* |
| [`statusclock`](plugins/statusclock.lua?raw=1) | Displays the current date and time in the corner of the status view |
+| [`tab_switcher`](plugins/tab_switcher.lua?raw=1) | Switch between open tabs by searching by name |
| [`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)) |
| [`texcompile`](plugins/texcompile.lua?raw=1) | Compile Tex files into PDF |
| [`theme16`](https://github.com/monolifed/theme16)\* | Theme manager with base16 themes |
diff --git a/plugins/tab_switcher.lua b/plugins/tab_switcher.lua
new file mode 100644
index 0000000..3d55eed
--- /dev/null
+++ b/plugins/tab_switcher.lua
@@ -0,0 +1,60 @@
+-- mod-version:3
+local core = require "core"
+local command = require "core.command"
+local keymap = require "core.keymap"
+local common = require "core.common"
+local DocView = require "core.docview"
+
+local tab_switcher = {}
+function tab_switcher.get_tab_list(base_node)
+ local raw_list = base_node:get_children()
+ local list = {}
+ local mt = {
+ -- fuzzy_match uses tostring to get the text to compare
+ __tostring = function(i) return i.text end
+ }
+ for _,v in pairs(raw_list) do
+ if v:is(DocView) then
+ table.insert(list, setmetatable({
+ text = v:get_name(),
+ view = v
+ }, mt))
+ end
+ end
+ return list
+end
+
+local function ask_selection(label, items)
+ if #items == 0 then
+ core.warn("No tabs available")
+ return
+ end
+ core.command_view:enter(label, {
+ submit = function(_, item)
+ local n = core.root_view.root_node:get_node_for_view(item.view)
+ if n then n:set_active_view(item.view) end
+ end,
+ suggest = function(text)
+ return common.fuzzy_match(items, text, true)
+ end,
+ validate = function(_, item)
+ return item
+ end
+ })
+end
+
+command.add(nil,{
+ ["tab-switcher:tab-list"] = function()
+ ask_selection("Switch to tab", tab_switcher.get_tab_list(core.root_view.root_node))
+ end,
+ ["tab-switcher:tab-list-current-split"] = function()
+ ask_selection("Switch to tab in current split", tab_switcher.get_tab_list(core.root_view:get_active_node()))
+ end
+})
+
+keymap.add({
+ ["alt+p"] = "tab-switcher:tab-list",
+ ["alt+shift+p"] = "tab-switcher:tab-list-current-split"
+})
+
+return tab_switcher