|
| 1 | +local F = require("core.functions") |
| 2 | + |
| 3 | +-- Module for managing buffers, these are used for navigation |
| 4 | + |
| 5 | +local M = {} |
| 6 | + |
| 7 | +-- Buffer table structure: |
| 8 | +-- { |
| 9 | +-- "Group" = { "buffer1", "buffer2", active_index = 1 } |
| 10 | +-- } |
| 11 | +M.buffers = {} |
| 12 | + |
| 13 | +M.buffer_default_group = "Other" |
| 14 | + |
| 15 | +M.buffer_matcher = { |
| 16 | + -- match = truthy, function() -> thruthy, or string matching buffer filename |
| 17 | + -- group = string, function() -> string |
| 18 | + { match = F.find_project_root, group = F.find_project_root }, |
| 19 | + { match = true, group = "Other" }, -- default |
| 20 | +} |
| 21 | + |
| 22 | +-------------------------------------------- |
| 23 | + |
| 24 | +local get_buffer_name = function() |
| 25 | + local buf = vim.api.nvim_get_current_buf() |
| 26 | + local buffer_name = vim.api.nvim_buf_get_name(buf) |
| 27 | + if not buffer_name or buffer_name == "" then return nil end |
| 28 | + return buffer_name |
| 29 | +end |
| 30 | + |
| 31 | +M.add_buffer = function() |
| 32 | + local buffer_name = get_buffer_name() |
| 33 | + if not buffer_name then return end |
| 34 | + |
| 35 | + -- Evaluate group |
| 36 | + local eval_group = function(config_group) |
| 37 | + if not config_group then return M.buffer_default_group end |
| 38 | + if type(config_group) == "function" then return config_group() end |
| 39 | + return config_group |
| 40 | + end |
| 41 | + |
| 42 | + -- Add if buffer does not exist yet |
| 43 | + local add = function(config_group) |
| 44 | + local group = eval_group(config_group) |
| 45 | + if not M.buffers[group] then M.buffers[group] = {} end |
| 46 | + for i, buffer in ipairs(M.buffers[group]) do |
| 47 | + if buffer == buffer_name then |
| 48 | + M.buffers[group]["active_index"] = i |
| 49 | + return group, i |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + table.insert(M.buffers[group], buffer_name) |
| 54 | + local count = #M.buffers[group] |
| 55 | + M.buffers[group]["active_index"] = count |
| 56 | + |
| 57 | + return group, count |
| 58 | + end |
| 59 | + |
| 60 | + -- Walk matcher configuration |
| 61 | + local matcher = M.buffer_matcher or {} |
| 62 | + if matcher and type(matcher) == "function" then |
| 63 | + matcher = matcher() |
| 64 | + end |
| 65 | + if #matcher == 0 then add(M.buffer_default_group) end |
| 66 | + for _, config in ipairs(M.buffer_matcher) do |
| 67 | + if type(config.match) == "function" and config.match() then |
| 68 | + return add(config.group) |
| 69 | + end |
| 70 | + |
| 71 | + -- Match path's filename |
| 72 | + if type(config.match) == "string" and config.match == buffer_name:match("([^/]+)$") then |
| 73 | + return add(config.group) |
| 74 | + end |
| 75 | + |
| 76 | + if type(config.match) == "boolean" and config.match then |
| 77 | + return add(config.group) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + return add(M.buffer_default_group) |
| 82 | +end |
| 83 | + |
| 84 | +local get_group_from_buffer = function(buffer_name) |
| 85 | + for group, buffers in pairs(M.buffers) do |
| 86 | + for i, buffer in ipairs(buffers) do |
| 87 | + if buffer == buffer_name then return group, i end |
| 88 | + end |
| 89 | + end |
| 90 | + return nil |
| 91 | +end |
| 92 | + |
| 93 | +local buffer_action_in_active_group = function(action, direction) |
| 94 | + local buffer_name = get_buffer_name() |
| 95 | + if not buffer_name then return end |
| 96 | + |
| 97 | + local group, index = get_group_from_buffer(buffer_name) |
| 98 | + if not group then group, index = M.add_buffer() end |
| 99 | + |
| 100 | + local buffers = M.buffers[group] |
| 101 | + if #buffers < 2 then return end |
| 102 | + |
| 103 | + -- Determine the other index |
| 104 | + local other_index = index + direction |
| 105 | + if index == 1 and direction == -1 then |
| 106 | + other_index = #buffers |
| 107 | + elseif index == #buffers and direction == 1 then |
| 108 | + other_index = 1 |
| 109 | + end |
| 110 | + |
| 111 | + if action == "move" then |
| 112 | + for _, buffer in ipairs(vim.api.nvim_list_bufs()) do |
| 113 | + -- Remove inactive buffers |
| 114 | + if not vim.api.nvim_buf_is_loaded(buffer) then |
| 115 | + for i, group_buffer in ipairs(buffers) do |
| 116 | + if buffer == group_buffer then |
| 117 | + table.remove(buffers, i) |
| 118 | + if i > 1 and i <= other_index then |
| 119 | + other_index = other_index - 1 |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + goto continue |
| 125 | + end |
| 126 | + |
| 127 | + -- Make buffer active |
| 128 | + if vim.api.nvim_buf_get_name(buffer) == buffers[other_index] then |
| 129 | + vim.api.nvim_set_current_buf(buffer) |
| 130 | + buffers.active_index = other_index |
| 131 | + break |
| 132 | + end |
| 133 | + |
| 134 | + ::continue:: |
| 135 | + end |
| 136 | + elseif action == "swap" then |
| 137 | + local tmp = buffers[other_index] |
| 138 | + buffers[other_index] = buffers[index] |
| 139 | + buffers[index] = tmp |
| 140 | + end |
| 141 | + |
| 142 | + M.buffers[group] = buffers |
| 143 | +end |
| 144 | + |
| 145 | +M.buffer_move_left = function() |
| 146 | + buffer_action_in_active_group("move", -1) |
| 147 | +end |
| 148 | + |
| 149 | +M.buffer_move_right = function() |
| 150 | + buffer_action_in_active_group("move", 1) |
| 151 | +end |
| 152 | + |
| 153 | +M.buffer_swap_left = function() |
| 154 | + buffer_action_in_active_group("swap", -1) |
| 155 | +end |
| 156 | + |
| 157 | +M.buffer_swap_right = function() |
| 158 | + buffer_action_in_active_group("swap", 1) |
| 159 | +end |
| 160 | + |
| 161 | +-------------------------------------------- |
| 162 | +-- Group move |
| 163 | + |
| 164 | +local buffer_group_move = function(direction) |
| 165 | + local buffer_name = get_buffer_name() |
| 166 | + if not buffer_name then return end |
| 167 | + |
| 168 | + -- TODO: How to get groups deterministically? |
| 169 | + |
| 170 | + -- Get active group from the current buffer |
| 171 | + |
| 172 | + -- Check groups bounds |
| 173 | + |
| 174 | + -- Change active group |
| 175 | +end |
| 176 | + |
| 177 | +M.buffer_group_move_up = function() |
| 178 | + buffer_group_move(-1) |
| 179 | +end |
| 180 | + |
| 181 | +M.buffer_group_move_down = function() |
| 182 | + buffer_group_move(1) |
| 183 | +end |
| 184 | + |
| 185 | +-- TODO: functions that can |
| 186 | +-- v Navigate buffer left |
| 187 | +-- v Navigate buffer right |
| 188 | +-- - Navigate group up (use active_index when swapping) |
| 189 | +-- - Navigate group down |
| 190 | +-- v Move buffer left |
| 191 | +-- v Move buffer right |
| 192 | +-- - Remove buffer from currently active group (decrease active_index) |
| 193 | + |
| 194 | +return M |
| 195 | + |
| 196 | + |
| 197 | +-- (cond |
| 198 | +-- ((string-equal "*" (substring (buffer-name) 0 1)) "Emacs") |
| 199 | +-- ((or (memq major-mode '(magit-process-mode |
| 200 | +-- magit-status-mode |
| 201 | +-- magit-diff-mode |
| 202 | +-- magit-log-mode |
| 203 | +-- magit-file-mode |
| 204 | +-- magit-blob-mode |
| 205 | +-- magit-blame-mode)) |
| 206 | +-- (string= (buffer-name) "COMMIT_EDITMSG")) "Magit") |
| 207 | +-- ((project-current) (dot/project-project-name)) |
| 208 | +-- ((memq major-mode '(org-mode |
| 209 | +-- emacs-lisp-mode)) "Org Mode") |
| 210 | +-- ((derived-mode-p 'dired-mode) "Dired") |
| 211 | +-- ((derived-mode-p 'prog-mode |
| 212 | +-- 'text-mode) "Editing") |
| 213 | +-- (t "Other"))))) |
0 commit comments