|
| 1 | +local M = {} |
| 2 | + |
| 3 | +M.contexts = {} |
| 4 | +M.active = {} |
| 5 | +M.current_floor = nil |
| 6 | + |
| 7 | +-- ========================================================= |
| 8 | +-- UTIL |
| 9 | +-- ========================================================= |
| 10 | +local function tbl_contains(tbl, val) |
| 11 | + for _, v in ipairs(tbl) do |
| 12 | + if v == val then |
| 13 | + return true |
| 14 | + end |
| 15 | + end |
| 16 | + return false |
| 17 | +end |
| 18 | + |
| 19 | +-- ========================================================= |
| 20 | +-- CONTEXT MANAGEMENT |
| 21 | +-- ========================================================= |
| 22 | +function M.add_context(name, ctx) |
| 23 | + M.contexts[name] = ctx |
| 24 | + local group = vim.api.nvim_create_augroup("Elevator", { clear = false }) |
| 25 | + |
| 26 | + for _, event in ipairs(ctx.events or {}) do |
| 27 | + vim.api.nvim_create_autocmd(event, { |
| 28 | + group = group, |
| 29 | + callback = function() |
| 30 | + M.check_context(name, ctx) |
| 31 | + end, |
| 32 | + }) |
| 33 | + end |
| 34 | +end |
| 35 | + |
| 36 | +function M.remove_context(name) |
| 37 | + if M.contexts[name] then |
| 38 | + M.contexts[name] = nil |
| 39 | + M.active[name] = nil |
| 40 | + if M.current_floor == name then |
| 41 | + M.current_floor = nil |
| 42 | + M.resolve_floor() |
| 43 | + end |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +-- ========================================================= |
| 48 | +-- FLOOR RESOLUTION |
| 49 | +-- ========================================================= |
| 50 | +function M.check_context(name, context) |
| 51 | + local is_active = context.match and context.match() or false |
| 52 | + |
| 53 | + -- Activate keymaps if the context matches and is not already active |
| 54 | + if is_active and not context._active then |
| 55 | + for mode, map in pairs(context.mappings or {}) do |
| 56 | + for lhs, rhs in pairs(map) do |
| 57 | + vim.keymap.set( |
| 58 | + mode, |
| 59 | + lhs, |
| 60 | + rhs, |
| 61 | + { noremap = true, silent = true } |
| 62 | + ) |
| 63 | + end |
| 64 | + end |
| 65 | + context._active = true |
| 66 | + M.active[name] = context |
| 67 | + -- update current_floor if higher priority |
| 68 | + if |
| 69 | + not M.current_floor |
| 70 | + or context.priority |
| 71 | + > (M.contexts[M.current_floor] and M.contexts[M.current_floor].priority or 0) |
| 72 | + then |
| 73 | + M.current_floor = name |
| 74 | + end |
| 75 | + |
| 76 | + -- Deactivate keymaps if the context no longer matches but was active |
| 77 | + elseif context._active and not is_active then |
| 78 | + for mode, map in pairs(context.mappings or {}) do |
| 79 | + for lhs, _ in pairs(map) do |
| 80 | + vim.keymap.del(mode, lhs) |
| 81 | + end |
| 82 | + end |
| 83 | + context._active = false |
| 84 | + M.active[name] = nil |
| 85 | + |
| 86 | + -- recalc current_floor if needed |
| 87 | + M.current_floor = nil |
| 88 | + for floor_name, ctx in pairs(M.active) do |
| 89 | + if |
| 90 | + not M.current_floor |
| 91 | + or ctx.priority > M.active[M.current_floor].priority |
| 92 | + then |
| 93 | + M.current_floor = floor_name |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +function M.resolve_floor() |
| 100 | + local top, top_prio = nil, -math.huge |
| 101 | + for name, _ in pairs(M.active) do |
| 102 | + local ctx = M.contexts[name] |
| 103 | + if ctx.priority > top_prio then |
| 104 | + top, top_prio = name, ctx.priority |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + if top ~= M.current_floor then |
| 109 | + M.swap_mappings(M.current_floor, top) |
| 110 | + M.current_floor = top |
| 111 | + end |
| 112 | +end |
| 113 | + |
| 114 | +-- ========================================================= |
| 115 | +-- MAPPINGS |
| 116 | +-- ========================================================= |
| 117 | +function M.swap_mappings(old, new) |
| 118 | + -- restore old |
| 119 | + if old then |
| 120 | + local ctx = M.contexts[old] |
| 121 | + if ctx and ctx.mappings then |
| 122 | + for mode, maps in pairs(ctx.mappings) do |
| 123 | + for lhs, _ in pairs(maps) do |
| 124 | + pcall(vim.keymap.del, mode, lhs, { buffer = 0 }) |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + -- apply new |
| 131 | + if new then |
| 132 | + local ctx = M.contexts[new] |
| 133 | + if ctx and ctx.mappings then |
| 134 | + for mode, maps in pairs(ctx.mappings) do |
| 135 | + for lhs, rhs in pairs(maps) do |
| 136 | + vim.keymap.set(mode, lhs, rhs, { buffer = 0 }) |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | +end |
| 142 | + |
| 143 | +-- ========================================================= |
| 144 | +-- PUBLIC API |
| 145 | +-- ========================================================= |
| 146 | +function M.setup(opts) |
| 147 | + opts = opts or {} |
| 148 | + M.contexts = {} |
| 149 | + |
| 150 | + -- clear previous autocmds |
| 151 | + vim.api.nvim_create_augroup("Elevator", { clear = true }) |
| 152 | + |
| 153 | + for name, ctx in pairs(opts.contexts or {}) do |
| 154 | + M.add_context(name, ctx) |
| 155 | + end |
| 156 | + |
| 157 | + -- user commands |
| 158 | + vim.api.nvim_create_user_command("ElevatorAddContext", function(args) |
| 159 | + local ctx = load("return " .. args.args)() |
| 160 | + M.add_context(ctx.name, ctx) |
| 161 | + end, { nargs = 1 }) |
| 162 | + |
| 163 | + vim.api.nvim_create_user_command("ElevatorRemoveContext", function(args) |
| 164 | + M.remove_context(args.args) |
| 165 | + end, { nargs = 1 }) |
| 166 | + |
| 167 | + vim.api.nvim_create_user_command("ElevatorFloors", function() |
| 168 | + print("Current floor: " .. (M.current_floor or "none")) |
| 169 | + print("Active contexts:") |
| 170 | + for name, _ in pairs(M.active) do |
| 171 | + print(" - " .. name) |
| 172 | + end |
| 173 | + end, {}) |
| 174 | +end |
| 175 | + |
| 176 | +function M.clear() |
| 177 | + M.contexts = {} |
| 178 | + M.active = {} |
| 179 | + M.current_floor = nil |
| 180 | +end |
| 181 | + |
| 182 | +-- ========================================================= |
| 183 | +-- STATUSLINE HELPER |
| 184 | +-- ========================================================= |
| 185 | +function M.statusline() |
| 186 | + return M.current_floor and ("[Elevator:" .. M.current_floor .. "]") or "" |
| 187 | +end |
| 188 | + |
| 189 | +return M |
0 commit comments