require("utility.misc") function table.filter(list, predicate) local filtered_list = {} for _, item in ipairs(list) do if predicate(item) then table.insert(filtered_list, item) end end return filtered_list end function set_terminal_height(height) if not vim.api.nvim_win_is_valid(vim.g.term_win) then return end vim.api.nvim_win_set_height(vim.g.term_win, height) end function _G.check_back_space() local col = vim.api.nvim_win_get_cursor(0)[2] if col == 0 then return true end return vim.api.nvim_get_current_line():sub(col, col):find("%s") and true end function _G.show_documentation() if vim.fn.index({"vim", "help"}, vim.bo.filetype) >= 0 then vim.cmd("help " .. vim.fn.expand("")) elseif vim.fn["coc#rpc#ready()"] then vim.fn.CocActionAsync("doHover") else vim.cmd(vim.go.keywordprg .. " " .. vim.fn.expand("")) end end function _G.toggle_terminal() if vim.fn.win_gotoid(vim.g.term_win) == 1 then vim.cmd("quit!") else local terminal_buffer = vim.api.nvim_create_buf(false, false) vim.api.nvim_open_win( terminal_buffer, true, { split = "below", vertical = true } ) vim.api.nvim_win_set_height(vim.fn.win_getid(), vim.g.terminal_height) vim.api.nvim_win_set_option( vim.fn.win_getid(), "winhighlight", "Normal:TerminalWindow" ) vim.fn.termopen(vim.env.SHELL, {detach=0}) vim.g.term_buf = terminal_buffer vim.o.number = false vim.o.relativenumber = false vim.o.signcolumn = "no" vim.cmd("startinsert!") vim.g.term_win = vim.fn.win_getid() end end function _G.get_current_file() return vim.fn.expand("%") end function _G.remove_hidden_windowless_buffers() local all_buffers = vim.fn.getbufinfo() local buffers = table.filter(all_buffers, function (item) local windows = item.windows return #windows == 0 and item.hidden end) for _, buffer in ipairs(buffers) do vim.api.nvim_buf_delete(buffer.bufnr, { force = true }) end end function _G.close_current_buffer() local curr_win_id = vim.fn.win_getid() if curr_win_id == vim.g.term_win then return end local current_buf_nr = vim.fn.bufnr() local all_buffers = vim.fn.getbufinfo() local file_buffers = table.filter( all_buffers, function(buf_info) return buf_info.loaded == 1 and buf_info.listed == 1 and buf_info.bufnr ~= current_buf_nr and not string.find(buf_info.name, "term://") end ) if #file_buffers == 0 then vim.cmd("new") else vim.cmd("buffer " .. file_buffers[1].bufnr) end vim.api.nvim_buf_delete(current_buf_nr, { force = true }) set_terminal_height(vim.g.terminal_height) end