Skip to content

Commit

Permalink
feat: toggle resizing on a per window basis. get toggled windows. sim…
Browse files Browse the repository at this point in the history
…plified resizer and autocommand logic
  • Loading branch information
beauwilliams committed Jul 24, 2022
1 parent 9a7592a commit 6ff7636
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

👌 Maximises current split/window automatically when cursor moves based on golden ratio

⚙️ Set Focus auto-cursorline/signcolumn/linenums & active/inactive win-highlight + disable
⚙️ Set Focus auto-cursorline/signcolumn/linenums & active/inactive win-highlight + disable

🙌 Compatible with NvimTree, NerdTree, CHADTree, Fern, Telescope, Snap, FZF, Diffview.nvim & QuickFix

Expand Down Expand Up @@ -76,6 +76,10 @@ lua require("focus").setup()
| `:FocusToggle` | Toggle focus on and off again. |
| `:FocusSplitNicely` | Split a window based on the golden ratio rule |
| `:FocusSplitCycle` | If there are no splits, create one and move to it, else cycle focussed split. `:FocusSplitCycle reverse` for counterclockwise |
| `:FocusDisableWindow` | Disable resizing of the current window (winnr). |
| `:FocusEnableWindow` | Enable resizing of the current window (winnr). |
| `:FocusToggleWindow` | Toggle focus on and off again on a per window basis. |
| `:FocusGetDisabledWindows` | Pretty prints the list of disabled window ID's along with the current window ID. |
| `:FocusSplitLeft` | Move to existing or create a new split to the left of your current window + open file or custom command |
| `:FocusSplitDown` | Move to existing or create a new split to the bottom of your current window + open file or custom command |
| `:FocusSplitUp` | Move to existing or create a new split to the top of your current window + open file or custom command |
Expand Down
40 changes: 40 additions & 0 deletions lua/focus/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local autocmd = require('focus.modules.autocmd')
local resizer = require('focus.modules.resizer')
local split = require('focus.modules.split')
local functions = require('focus.modules.functions')
local utils = require('focus.modules.utils')

local M = {}

Expand Down Expand Up @@ -87,4 +88,43 @@ function M.focus_max_or_equal()
functions.focus_max_or_equal()
end

function M.focus_disable_window()
table.insert(M.excluded_windows, vim.api.nvim_get_current_win())
end

function M.focus_enable_window()
for k, v in pairs(M.excluded_windows) do
if v == vim.api.nvim_get_current_win() then
table.remove(M.excluded_windows, k)
end
end
end


function M.focus_toggle_window()
for _, v in pairs(M.excluded_windows) do
if v == vim.api.nvim_get_current_win() then
M.focus_enable_window()
return
end
end
M.focus_disable_window()
end


function M.focus_get_disabled_windows()
print('------------------')
print('█▀ ▄▀▄ ▄▀▀ █ █ ▄▀▀')
print('█▀ ▀▄▀ ▀▄▄ ▀▄█ ▄██')
print('------------------')
print('Disabled Windows')
for _, v in pairs(M.excluded_windows) do
print('- ' .. v)
end
print('-------------------')
print('Current Window')
print('- ' .. vim.api.nvim_get_current_win())
print('-------------------')
end

return M
7 changes: 6 additions & 1 deletion lua/focus/modules/autocmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ function M.setup(config)
-- This is an upstream vim issue because there is no way to specify filetype etc when creating a new buffer
-- You can only create a blank buffer, and then set the variables after it was created
-- Which means focus will initially read it as blank buffer and resize. This is an issue for many other plugins that read ft too.
{ 'WinEnter,BufEnter', '*', 'lua vim.schedule(function() require"focus".resize(); vim.cmd([[doautocmd WinScrolled]]) end)' },
{
'BufEnter',
'*',
'doautocmd WinScrolled | lua require"focus".resize()',
-- 'lua vim.schedule(function() require"focus".resize(); vim.cmd([[doautocmd WinScrolled]]) end)',
},
{ 'WinEnter,BufEnter', 'NvimTree_*', 'lua require"focus".resize()' },
}
end
Expand Down
4 changes: 4 additions & 0 deletions lua/focus/modules/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ M.setup = function()
command! -nargs=0 FocusDisable lua require('focus').focus_disable()
command! -nargs=0 FocusEnable lua require('focus').focus_enable()
command! -nargs=0 FocusToggle lua require('focus').focus_toggle()
command! -nargs=0 FocusDisableWindow lua require('focus').focus_disable_window()
command! -nargs=0 FocusEnableWindow lua require('focus').focus_enable_window()
command! -nargs=0 FocusToggleWindow lua require('focus').focus_toggle_window()
command! -nargs=0 FocusGetDisabledWindows lua require('focus').focus_get_disabled_windows()
command! -nargs=0 FocusEqualise lua require('focus').focus_equalise()
command! -nargs=0 FocusMaximise lua require('focus').focus_maximise()
command! -nargs=0 FocusMaxOrEqual lua require('focus').focus_max_or_equal()
Expand Down
1 change: 1 addition & 0 deletions lua/focus/modules/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local defaults = {
bufnew = false,
compatible_filetrees = { 'nvimtree', 'nerdtree', 'chadtree', 'fern' },
excluded_filetypes = {},
excluded_windows = {},
excluded_buftypes = { 'nofile', 'prompt', 'popup' },
forced_filetypes = { 'dap-repl' },
}
Expand Down
4 changes: 3 additions & 1 deletion lua/focus/modules/resizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function M.split_resizer(config) --> Only resize normal buffers, set qf to 10 al
local excluded_ft_set = utils.to_set(utils.to_lower(config.excluded_filetypes))
local excluded_bt_set = utils.to_set(utils.to_lower(config.excluded_buftypes))
local forced_ft_set = utils.to_set(utils.to_lower(config.forced_filetypes))
local excluded_windows_set = utils.to_set(config.excluded_windows)
local winnr = vim.api.nvim_get_current_win()

if ft == 'diffviewfiles' or ft == 'spectre_panel' then
vim.schedule(function()
Expand All @@ -45,7 +47,7 @@ function M.split_resizer(config) --> Only resize normal buffers, set qf to 10 al
elseif filetrees_set[ft] or ft == 'nvimtree' then
vim.o.winminwidth = config.minwidth or 0
vim.o.winwidth = config.treewidth
elseif (excluded_bt_set[bt] or excluded_ft_set[ft]) and not forced_ft_set[ft] then
elseif (excluded_bt_set[bt] or excluded_ft_set[ft]) or excluded_windows_set[winnr] and not forced_ft_set[ft] then
vim.o.winminheight = 0
vim.o.winheight = 1
vim.o.winminwidth = 0
Expand Down
10 changes: 10 additions & 0 deletions lua/focus/modules/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ function M.to_set(list)
return set
end

function M.add_to_set(set, item)
set[item] = true
return set
end

function M.remove_from_set(set, item)
set[item] = nil
return set
end

-- SPLITS A STRING BY SPACE FOR : COMMAND PARSING
function M.split(s, delimiter)
local result = {}
Expand Down

0 comments on commit 6ff7636

Please sign in to comment.