Skip to content

Commit ea069d0

Browse files
committed
independent window highlighting
1 parent 023b2b1 commit ea069d0

2 files changed

Lines changed: 48 additions & 26 deletions

File tree

lua/winbender/highlight.lua

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,6 @@ local function adjust_color_cterm(color_num, factor)
9797
return color_num
9898
end
9999

100-
local highlight_groups = {
101-
"ColorColumn",
102-
"CursorLine",
103-
"CursorLineNr",
104-
"EndOfBuffer",
105-
"FoldColumn",
106-
"LineNr",
107-
"NonText",
108-
"Normal",
109-
"SignColumn",
110-
}
111-
112100
local function adjust_highlight_group(group_name, factor)
113101
local hl = compat.nvim_get_hl(0, { name = group_name })
114102
if not hl.bg then
@@ -126,27 +114,61 @@ local function adjust_highlight_group(group_name, factor)
126114
}
127115
end
128116

117+
local function get_local_highlight_groups(winid)
118+
local hl_dict = {
119+
ColorColumn = "ColorColumn",
120+
CursorLine = "CursorLine",
121+
CursorLineNr = "CursorLineNr",
122+
EndOfBuffer = "EndOfBuffer",
123+
FoldColumn = "FoldColumn",
124+
LineNr = "LineNr",
125+
NonText = "NonText",
126+
Normal = "Normal",
127+
SignColumn = "SignColumn",
128+
}
129+
130+
local pairs = vim.split(vim.wo[winid].winhighlight, ",")
131+
for _, pair in ipairs(pairs) do
132+
local parts = vim.split(pair, ":")
133+
if hl_dict[parts[1]] then
134+
hl_dict[parts[1]] = parts[2]
135+
end
136+
end
137+
return hl_dict
138+
end
139+
140+
local function concat_winhighlight_dict(hl_dict)
141+
local winhighlight = {}
142+
for hl_from, hl_to in pairs(hl_dict) do
143+
table.insert(winhighlight, hl_from .. ":" .. hl_to)
144+
end
145+
return table.concat(winhighlight, ",")
146+
end
147+
148+
local function register_adjusted_hl_group(hl_group)
149+
if not vim.fn.hlexists("WinBender" .. hl_group) then
150+
else
151+
local adjusted_hl = adjust_highlight_group(hl_group, highlight_factor)
152+
if adjusted_hl then
153+
vim.api.nvim_set_hl(0, "WinBender" .. hl_group, adjusted_hl)
154+
end
155+
end
156+
return "WinBender" .. hl_group
157+
end
158+
129159
local function update_window_highlight(winid)
130160
local silent = true
131161
if not state.validate_window(winid, silent) then
132162
return
133163
end
134164

135165
local is_focused = (winid == core.get_current_window())
136-
137166
if is_focused then
138-
local highlight_mappings = {}
139-
140-
for _, group_name in ipairs(highlight_groups) do
141-
local adjusted_hl = adjust_highlight_group(group_name, highlight_factor)
142-
if adjusted_hl then
143-
local focused_group_name = "WinBenderFocused" .. group_name
144-
vim.api.nvim_set_hl(0, focused_group_name, adjusted_hl)
145-
table.insert(highlight_mappings, group_name .. ":" .. focused_group_name)
146-
end
167+
local hl_groups = get_local_highlight_groups(winid)
168+
for hl_from, hl_to in pairs(hl_groups) do
169+
hl_groups[hl_from] = register_adjusted_hl_group(hl_to)
147170
end
148-
149-
vim.wo[winid].winhighlight = table.concat(highlight_mappings, ",")
171+
vim.wo[winid].winhighlight = concat_winhighlight_dict(hl_groups)
150172
else
151173
vim.wo[winid].winhighlight = state.get_highlight(winid)
152174
end
@@ -198,7 +220,7 @@ function M.disable()
198220
vim.wo[winid].winhighlight = original_hl
199221
end
200222
end
201-
state.clear_all_highlights()
223+
state.clear_stored_highlights()
202224
end
203225

204226
return M

lua/winbender/state.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function M.get_all_highlights()
4848
return win_highlights
4949
end
5050

51-
function M.clear_all_highlights()
51+
function M.clear_stored_highlights()
5252
win_highlights = {}
5353
end
5454

0 commit comments

Comments
 (0)