diff --git a/README.md b/README.md index e86d9c9a..5e30d885 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Plugin | Description [`linecopypaste`](plugins/linecopypaste.lua?raw=1) | Copy, cut and paste the current line when nothing is selected [`lineguide`](plugins/lineguide.lua?raw=1) | Displays a line-guide at the line limit offset *([screenshot](https://user-images.githubusercontent.com/3920290/81476159-2cf70000-9208-11ea-928b-9dae3884c477.png))* [`linter`](https://github.com/drmargarido/linters)* | Linters for multiple languages +[`listkeybindings`](plugins/listkeybindings.lua?raw=1) | Dump all defined key bindings to a new document [`macmodkeys`](plugins/macmodkeys.lua?raw=1) | Remaps mac modkeys `command/option` to `ctrl/alt` [`markers`](plugins/markers.lua?raw=1) | Add markers to docs and jump between them quickly *([screenshot](https://user-images.githubusercontent.com/3920290/82252149-5faaa200-9946-11ea-9199-bea2efb7ee23.png))* [`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))* diff --git a/plugins/listkeybindings.lua b/plugins/listkeybindings.lua new file mode 100644 index 00000000..3601b9fa --- /dev/null +++ b/plugins/listkeybindings.lua @@ -0,0 +1,90 @@ +--[[ + listkeybindings.lua + creates a new document and lists all key-bindings + version: 20200627_152637 + originally by SwissalpS + + show-key-binding will list key first then binding + show-binding-key will list binding first then key + + If sort plugin is installed, the list will also be sorted. +--]] +local core = require "core" +local command = require "core.command" +local keymap = require "core.keymap" + +local function pasteAndSort(sOut) + core.root_view:open_doc(core.open_doc()) + core.active_view.doc:text_input(sOut) + if command.map['sort:sort'] then + command.perform('doc:select-all') + command.perform('sort:sort') + command.perform('doc:select-none') + end +end + +local function gatherLists() + + local lKeys = {} + local lBindings = {} + local iLongestKey = 0 + local iLongestBinding = 0 + local iIndex = 0 + + for k, v in pairs(keymap.map) do + if iLongestKey < #k then iLongestKey = #k end + for _, x in ipairs(v) do + iIndex = iIndex + 1 + if iLongestBinding < #x then iLongestBinding = #x end + lKeys[iIndex] = k + lBindings[iIndex] = x + end + end + + return lKeys, lBindings, iLongestKey, iLongestBinding + +end -- gatherLists + +local function listBindingsKey() + + local sOut = '' + local lKeys, lBindings, iLongestKey, iLongestBinding = gatherLists() + + -- format pretty output + local sSpace, k, v + local iLongest = iLongestBinding + 1 + for i = 1, #lKeys, 1 do + k = lBindings[i] + v = lKeys[i] + sSpace = string.rep(' ', iLongest - #k) + sOut = sOut .. k .. sSpace .. v .. '\n' + end + + pasteAndSort(sOut) + +end + +local function listKeyBindings() + + local sOut = '' + local lKeys, lBindings, iLongestKey, iLongestBinding = gatherLists() + + -- format pretty output + local sSpace, k, v + local iLongest = iLongestKey + 1 + for i = 1, #lKeys, 1 do + v = lBindings[i] + k = lKeys[i] + sSpace = string.rep(' ', iLongest - #k) + sOut = sOut .. k .. sSpace .. v .. '\n' + end + + pasteAndSort(sOut) + +end + +command.add("core.docview", { + ["listkeybindings:show-key-binding"] = listKeyBindings, + ["listkeybindings:show-binding-key"] = listBindingsKey, +}) +