Skip to content

Commit

Permalink
[~] stabilised version
Browse files Browse the repository at this point in the history
  • Loading branch information
Pheon-Dev committed Sep 7, 2023
1 parent 5ef35ea commit 4d65723
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 114 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ require("buffalo").setup({
}
},
general_commands = {
cycle = false, -- cycle through the list
cycle = true, -- cycle through buffers or tabs
exit_menu = "x", -- similar to 'q' and '<esc>'
},
go_to = {
Expand Down
20 changes: 10 additions & 10 deletions lua/buffalo/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ function M.init_tabs()
local tab_name = api.get_tab_number(tab_id)
local tab_wins = api.get_wins()
-- if buffer is listed, then add to contents and marks
-- if tab_is_valid(tab_id, tab_name, tab_wins) then
table.insert(
M.tab_marks,
{
tab_name = tab_name,
tab_id = tab_id,
tab_wins = tab_wins,
}
)
-- end
if tab_is_valid(tab_id, tab_name) then
table.insert(
M.tab_marks,
{
tab_name = tab_name,
tab_id = tab_id,
tab_wins = tab_wins,
}
)
end
end
end

Expand Down
148 changes: 47 additions & 101 deletions lua/buffalo/ui.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
local Path = require("plenary.path")
local buffalo = require("buffalo")
local popup = require("plenary.popup")
local utils = require("buffalo.utils")
local log = require("buffalo.dev").log
local marks = require("buffalo").marks
local tab_marks = require("buffalo").tab_marks
local api = require("buffalo.api")

local M = {}

Buffalo_win_id = nil
Buffalo_bufh = nil
local initial_marks = {}
local initial_tab_marks = {}
local config = buffalo.get_config()
local Path = require("plenary.path")
local buffalo = require("buffalo")
local popup = require("plenary.popup")
local utils = require("buffalo.utils")
local log = require("buffalo.dev").log
local marks = require("buffalo").marks
local api = require("buffalo.api")

local M = {}

Buffalo_win_id = nil
Buffalo_bufh = nil
Buffalo_Tabs_win_id = nil
Buffalo_Tabs_bufh = nil

local initial_marks = {}
local config = buffalo.get_config()

local function close_menu(force_save)
force_save = force_save or false
Expand All @@ -24,6 +25,14 @@ local function close_menu(force_save)
Buffalo_bufh = nil
end

local function close_tabs_menu(force_save)
force_save = force_save or false

vim.api.nvim_win_close(Buffalo_Tabs_win_id, true)

Buffalo_Tabs_win_id = nil
Buffalo_Tabs_bufh = nil
end
local opts = { noremap = true }
local map = vim.keymap.set

Expand Down Expand Up @@ -74,17 +83,6 @@ local function can_be_deleted(bufname, bufnr)
)
end

local function can_tab_be_deleted(tabname, tabid)
return (
vim.api.nvim_tabpage_is_valid(tabid)
and (not string_starts(tabname, "term://"))
-- and (not vim.bo[tabid].modified)
and tabid ~= -1
)
end



local function is_buffer_in_marks(bufnr)
for _, mark in pairs(marks) do
if mark.buf_id == bufnr then
Expand All @@ -94,20 +92,8 @@ local function is_buffer_in_marks(bufnr)
return false
end

local function is_tab_in_tab_marks(tabid)
for _, mark in pairs(tab_marks) do
if mark.tab_id == tabid then
return true
end
end
return false
end



local function get_mark_by_name(name, specific_marks)
local ref_name = nil
local current_short_fns = {}
for _, mark in pairs(specific_marks) do
ref_name = mark.filename
if string_starts(mark.filename, "term://") then
Expand Down Expand Up @@ -144,28 +130,6 @@ local function update_buffers()
end
end

local function update_tabs()
-- Check deletions
for _, mark in pairs(initial_tab_marks) do
if not is_tab_in_tab_marks(mark.tab_id) then
if can_tab_be_deleted(mark.tab_name, mark.tab_id) then
vim.api.nvim_tabpage_del_var(mark.tab_id, {})
end
end
end

-- Check additions
for idx, mark in pairs(marks) do
local bufnr = mark.tab_name
-- Add buffer only if it does not already exist
if bufnr == -1 then
vim.cmd("badd " .. mark.tab_name)
marks[idx].tab_id = vim.fn.bufnr(mark.tab_name)
end
end
end


local function remove_mark(idx)
marks[idx] = nil
if idx < #marks then
Expand Down Expand Up @@ -221,7 +185,6 @@ function M.toggle_buf_menu()
local current_buf_line = 1
local line = 1
local modified_lines = {}
local current_short_fns = {}
for idx, mark in pairs(marks) do
-- Add buffer only if it does not already exist
if vim.fn.buflisted(mark.buf_id) ~= 1 then
Expand Down Expand Up @@ -331,31 +294,29 @@ end

function M.toggle_tab_menu()
log.trace("toggle_tab_menu()")
if Buffalo_win_id ~= nil and vim.api.nvim_win_is_valid(Buffalo_win_id) then
close_menu(true)
if Buffalo_Tabs_win_id ~= nil and vim.api.nvim_win_is_valid(Buffalo_Tabs_win_id) then
close_tabs_menu(true)
return
end
local tabid = api.get_current_tab()
local current_tab_id = api.get_tab_number(tabid)
local tabs = api.get_tabs()
local wins = api.get_wins()

local win_info = create_window("tabpages")
local contents = {}
initial_tab_marks = {}

Buffalo_win_id = win_info.win_id
Buffalo_tabh = win_info.bufnr
Buffalo_Tabs_win_id = win_info.win_id
Buffalo_Tabs_bufh = win_info.bufnr

update_tabs()

local current_tab_line = 1
local current_buf_line = 1
local modified_lines = {}


for idx = 1, #tabs do
local current_tab = api.get_tab_number(idx)
if current_tab == current_tab_id then
current_tab_line = idx
current_buf_line = idx
end
local twins = api.get_tab_wins(idx)
if current_tab == 0 then
Expand All @@ -364,38 +325,38 @@ function M.toggle_tab_menu()
contents[idx] = string.format(" 󰓩 Tab %s [%s window%s]", current_tab, #twins, #twins > 1 and "s" or "")
end

vim.api.nvim_set_option_value("number", true, { win = Buffalo_win_id })
vim.api.nvim_set_option_value("number", true, { win = Buffalo_Tabs_win_id })

vim.api.nvim_buf_set_name(Buffalo_tabh, "buffalo-tabs")
vim.api.nvim_buf_set_lines(Buffalo_tabh, 0, #contents, false, contents)
vim.api.nvim_buf_set_option(Buffalo_tabh, "filetype", "buffalo")
vim.api.nvim_buf_set_option(Buffalo_tabh, "buftype", "acwrite")
vim.api.nvim_buf_set_option(Buffalo_tabh, "bufhidden", "delete")
vim.cmd(string.format(":call cursor(%d, %d)", current_tab_line, 1))
vim.api.nvim_buf_set_name(Buffalo_Tabs_bufh, "buffalo-tabs")
vim.api.nvim_buf_set_lines(Buffalo_Tabs_bufh, 0, #contents, false, contents)
vim.api.nvim_buf_set_option(Buffalo_Tabs_bufh, "filetype", "buffalo")
vim.api.nvim_buf_set_option(Buffalo_Tabs_bufh, "buftype", "acwrite")
vim.api.nvim_buf_set_option(Buffalo_Tabs_bufh, "bufhidden", "delete")
vim.cmd(string.format(":call cursor(%d, %d)", current_buf_line, 1))
vim.api.nvim_buf_set_keymap(
Buffalo_tabh,
Buffalo_Tabs_bufh,
"n",
config.general_commands.exit_menu,
"<Cmd>lua require('buffalo.ui').toggle_tab_menu()<CR>",
{ silent = true }
)
vim.api.nvim_buf_set_keymap(
Buffalo_tabh,
Buffalo_Tabs_bufh,
"n",
"q",
"<Cmd>lua require('buffalo.ui').toggle_tab_menu()<CR>",
{ silent = true }
)
vim.api.nvim_buf_set_keymap(
Buffalo_tabh,
Buffalo_Tabs_bufh,
"n",
"<ESC>",
"<Cmd>lua require('buffalo.ui').toggle_tab_menu()<CR>",
{ silent = true }
)
for _, value in pairs(config.tab_commands) do
vim.api.nvim_buf_set_keymap(
Buffalo_tabh,
Buffalo_Tabs_bufh,
"n",
value.key,
"<Cmd>lua require('buffalo.ui').select_tab_menu_item('" .. value.command .. "')<CR>",
Expand All @@ -405,7 +366,7 @@ function M.toggle_tab_menu()
vim.cmd(
string.format(
"autocmd BufModifiedSet <buffer=%s> set nomodified",
Buffalo_tabh
Buffalo_Tabs_bufh
)
)
vim.cmd(
Expand All @@ -416,15 +377,15 @@ function M.toggle_tab_menu()
string.format(
"autocmd BufWriteCmd <buffer=%s>" ..
" lua require('buffalo.ui').on_menu_save()",
Buffalo_tabh
Buffalo_Tabs_bufh
)
)
local str = "1234567890"

for i = 1, #str do
local c = str:sub(i, i)
vim.api.nvim_buf_set_keymap(
Buffalo_tabh,
Buffalo_Tabs_bufh,
"n",
c,
string.format(
Expand All @@ -435,23 +396,11 @@ function M.toggle_tab_menu()
{}
)
end

for _, modified_line in pairs(modified_lines) do
vim.api.nvim_buf_add_highlight(
Buffalo_tabh,
-1,
"BuffaloModified",
modified_line - 1,
0,
-1
)
end
end

function M.select_tab_menu_item(command)
local idx = vim.fn.line(".")
close_menu(true)
update_buffers()
close_tabs_menu(true)
M.nav_tab(idx, command)
end

Expand Down Expand Up @@ -510,7 +459,6 @@ end

function M.nav_tab(id, command)
log.trace("nav_buf(): Navigating to", id)
update_tabs()

if command == nil or command == "tabnext" then
local tabid = api.get_tab_number(id)
Expand Down Expand Up @@ -556,13 +504,11 @@ end

function M.nav_tab_next()
log.trace("nav_tab_next()")
update_tabs()
vim.cmd("tabnext")
end

function M.nav_tab_prev()
log.trace("nav_tab_prev()")
update_tabs()
vim.cmd("tabprev")
end

Expand Down
7 changes: 5 additions & 2 deletions lua/buffalo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ function M.buffer_is_valid(buf_id, buf_name)
end

function M.tab_is_valid(tab_id, tab_name)
return 1 == vim.api.nvim_tabpage_is_valid(tab_id)
and tab_name ~= ""
-- return 1 == vim.api.nvim_tabpage_is_valid(tab_id)
-- and tab_name ~= ""
local valid = vim.api.nvim_tabpage_is_valid(tab_id)
if not valid then return -1 end
return tab_name ~= ""
end

-- tbl_deep_extend does not work the way you would think
Expand Down

0 comments on commit 4d65723

Please sign in to comment.