@@ -46,7 +46,13 @@ local create_horizontal_line = function(title, pos, width, left_char, mid_char,
46
46
string.rep (mid_char , width - title_len - left_start ),
47
47
right_char
48
48
)
49
- return horizontal_line
49
+ local ranges
50
+ if title_len ~= 0 then
51
+ -- Need to calculate again due to multi-byte characters
52
+ local r_start = string.len (left_char ) + math.max (left_start , 0 ) * string.len (mid_char )
53
+ ranges = { { r_start , r_start + string.len (title ) } }
54
+ end
55
+ return horizontal_line , ranges
50
56
end
51
57
52
58
function Border ._create_lines (content_win_options , border_win_options )
@@ -59,6 +65,7 @@ function Border._create_lines(content_win_options, border_win_options)
59
65
local left_enabled = thickness .left == 1
60
66
61
67
local border_lines = {}
68
+ local ranges = {}
62
69
63
70
local topline = nil
64
71
@@ -85,14 +92,18 @@ function Border._create_lines(content_win_options, border_win_options)
85
92
then
86
93
for _ , title in ipairs (titles ) do
87
94
if string.find (title .pos , " N" ) then
88
- topline = create_horizontal_line (
95
+ local top_ranges
96
+ topline , top_ranges = create_horizontal_line (
89
97
title .text ,
90
98
title .pos ,
91
99
content_win_options .width ,
92
100
topleft ,
93
101
border_win_options .top or " " ,
94
102
topright
95
103
)
104
+ for _ , r in pairs (top_ranges ) do
105
+ table.insert (ranges , { 0 , r [1 ], r [2 ] })
106
+ end
96
107
break
97
108
end
98
109
end
@@ -126,14 +137,18 @@ function Border._create_lines(content_win_options, border_win_options)
126
137
local botright = (right_enabled and border_win_options .botright ) or " "
127
138
for _ , title in ipairs (titles ) do
128
139
if string.find (title .pos , " S" ) then
129
- botline = create_horizontal_line (
140
+ local bot_ranges
141
+ botline , bot_ranges = create_horizontal_line (
130
142
title .text ,
131
143
title .pos ,
132
144
content_win_options .width ,
133
145
botleft ,
134
146
border_win_options .bot or " " ,
135
147
botright
136
148
)
149
+ for _ , r in pairs (bot_ranges ) do
150
+ table.insert (ranges , { content_win_options .height + 1 , r [1 ], r [2 ] })
151
+ end
137
152
break
138
153
end
139
154
end
@@ -145,7 +160,15 @@ function Border._create_lines(content_win_options, border_win_options)
145
160
table.insert (border_lines , botline )
146
161
end
147
162
148
- return border_lines
163
+ return border_lines , ranges
164
+ end
165
+
166
+ local set_title_highlights = function (bufnr , ranges , hl )
167
+ if hl and ranges then
168
+ for _ , r in pairs (ranges ) do
169
+ vim .api .nvim_buf_add_highlight (bufnr , - 1 , hl , r [1 ], r [2 ], r [3 ])
170
+ end
171
+ end
149
172
end
150
173
151
174
function Border :change_title (new_title )
@@ -154,8 +177,10 @@ function Border:change_title(new_title)
154
177
end
155
178
156
179
self ._border_win_options .title = new_title
157
- self .contents = Border ._create_lines (self .content_win_options , self ._border_win_options )
180
+ self .contents , self . title_ranges = Border ._create_lines (self .content_win_options , self ._border_win_options )
158
181
vim .api .nvim_buf_set_lines (self .bufnr , 0 , - 1 , false , self .contents )
182
+
183
+ set_title_highlights (self .bufnr , self .title_ranges , self ._border_win_options .titlehighlight )
159
184
end
160
185
161
186
-- Updates characters for border lines, and returns nvim_win_config
@@ -192,7 +217,8 @@ function Border:__align_calc_config(content_win_options, border_win_options)
192
217
-- Ensure the relevant contests and border win_options are set
193
218
self ._border_win_options = border_win_options
194
219
self .content_win_options = content_win_options
195
- self .contents = Border ._create_lines (content_win_options , border_win_options )
220
+ -- Update border characters and title_ranges
221
+ self .contents , self .title_ranges = Border ._create_lines (content_win_options , border_win_options )
196
222
197
223
vim .api .nvim_buf_set_lines (self .bufnr , 0 , - 1 , false , self .contents )
198
224
@@ -225,6 +251,12 @@ function Border:new(content_bufnr, content_win_id, content_win_options, border_w
225
251
local nvim_win_config = Border .__align_calc_config (obj , content_win_options , border_win_options )
226
252
obj .win_id = vim .api .nvim_open_win (obj .bufnr , false , nvim_win_config )
227
253
254
+ if border_win_options .highlight then
255
+ vim .api .nvim_win_set_option (obj .win_id , " winhl" , border_win_options .highlight )
256
+ end
257
+
258
+ set_title_highlights (obj .bufnr , obj .title_ranges , obj ._border_win_options .titlehighlight )
259
+
228
260
vim .cmd (
229
261
string.format (
230
262
" autocmd BufDelete <buffer=%s> ++nested ++once :lua require('plenary.window').close_related_win(%s, %s)" ,
0 commit comments