Skip to content

Commit

Permalink
feat: allow disabling focus for individual buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy authored and cryptomilk committed Oct 5, 2023
1 parent 40fee16 commit 1a945b2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ vim.highlight.link('UnfocusedWindow', 'VisualNOS', true)

## Disabling Focus

Focus can be disabled by setting a variable for just one window
(`vim.w.focus_disable = true`) or globally (`vim.g.focus_disable = true`).
Focus can be disabled by setting a variable for just one window,
(`vim.w.focus_disable = true`), just one buffer (`vim.b.focus_disable = true`), or globally (`vim.g.focus_disable = true`).

If you want to disable Focus for certain buffer or file types you can do
this by setting up autocommands (`:help autocmd`) in your configuration.
Expand Down Expand Up @@ -346,9 +346,9 @@ vim.api.nvim_create_autocmd('FileType', {
group = augroup,
callback = function(_)
if vim.tbl_contains(ignore_filetypes, vim.bo.filetype) then
vim.w.focus_disable = true
vim.b.focus_disable = true
else
vim.w.focus_disable = false
vim.b.focus_disable = false
end
end,
desc = 'Disable focus autoresize for FileType',
Expand Down
16 changes: 8 additions & 8 deletions doc/focus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ default settings:
relativenumber = false, -- Display relative line numbers in the focussed window only
hybridnumber = false, -- Display hybrid line numbers in the focussed window only
absolutenumber_unfocussed = false, -- Preserve absolute numbers in the unfocussed windows

cursorline = true, -- Display a cursorline in the focussed window only
cursorcolumn = false, -- Display cursorcolumn in the focussed window only
colorcolumn = {
Expand Down Expand Up @@ -283,7 +283,7 @@ buffer**
-- Enable auto highlighting for focussed/unfocussed windows
-- Default: false
require("focus").setup({ ui = { winhighlight = true } })

-- By default, the highlight groups are setup as such:
-- hi default link FocusedWindow VertSplit
-- hi default link UnfocusedWindow Normal
Expand All @@ -307,10 +307,10 @@ Here is an example:
>lua
local ignore_filetypes = { 'neo-tree' }
local ignore_buftypes = { 'nofile', 'prompt', 'popup' }

local augroup =
vim.api.nvim_create_augroup('FocusDisable', { clear = true })

vim.api.nvim_create_autocmd('WinEnter', {
group = augroup,
callback = function(_)
Expand All @@ -323,14 +323,14 @@ Here is an example:
end,
desc = 'Disable focus autoresize for BufType',
})

vim.api.nvim_create_autocmd('FileType', {
group = augroup,
callback = function(_)
if vim.tbl_contains(ignore_filetypes, vim.bo.filetype) then
vim.w.focus_disable = true
vim.b.focus_disable = true
else
vim.w.focus_disable = false
vim.b.focus_disable = false
end
end,
desc = 'Disable focus autoresize for FileType',
Expand Down Expand Up @@ -462,7 +462,7 @@ LEVERAGE HJKL TO MOVE OR CREATE YOUR SPLITS DIRECTIONALLY ~
require('focus').split_command(direction)
end, { desc = string.format('Create or move to split (%s)', direction) })
end

-- Use `<Leader>h` to split the screen to the left, same as command FocusSplitLeft etc
focusmap('h')
focusmap('j')
Expand Down
15 changes: 15 additions & 0 deletions lua/focus/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ function Focus.focus_toggle_window()
Focus.resize()
end

function Focus.focus_disable_buffer()
vim.b.focus_disable = true
Focus.resize()
end

function Focus.focus_enable_buffer()
vim.b.focus_disable = false
Focus.resize()
end

function Focus.focus_toggle_buffer()
vim.b.focus_disable = not vim.b.focus_disable
Focus.resize()
end

H.default_config = Focus.config

H.setup_config = function(config)
Expand Down
18 changes: 18 additions & 0 deletions lua/focus/modules/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ M.commands = {
end,
{ nargs = 0 },
},
FocusDisableBuffer = {
function()
require('focus').focus_disable_buffer()
end,
{ nargs = 0 },
},
FocusEnableBuffer = {
function()
require('focus').focus_enable_buffer()
end,
{ nargs = 0 },
},
FocusToggleBuffer = {
function()
require('focus').focus_toggle_buffer()
end,
{ nargs = 0 },
},
FocusEqualise = {
function()
require('focus').focus_equalise()
Expand Down
4 changes: 3 additions & 1 deletion lua/focus/modules/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ function M.remove_from_set(set, item)
end

M.is_disabled = function()
return vim.g.focus_disable == true or vim.w.focus_disable == true
return vim.g.focus_disable == true
or vim.w.focus_disable == true
or vim.b.focus_disable == true
end

return M

0 comments on commit 1a945b2

Please sign in to comment.