@@ -3,6 +3,8 @@ local utils = require('orgmode.utils')
3
3
local Promise = require (' orgmode.utils.promise' )
4
4
local config = require (' orgmode.config' )
5
5
local namespace = vim .api .nvim_create_namespace (' org_calendar' )
6
+ local colors = require (' orgmode.colors' )
7
+ local Range = require (' orgmode.files.elements.range' )
6
8
7
9
--- @alias OrgCalendarOnRenderDayOpts { line : number , from : number , to : number , buf : number , namespace : number }
8
10
--- @alias OrgCalendarOnRenderDay fun ( day : OrgDate , opts : OrgCalendarOnRenderDayOpts )
@@ -15,7 +17,6 @@ local small_minute_step = config.calendar.min_small_step or config.org_time_stam
15
17
--- @field win number ?
16
18
--- @field buf number ?
17
19
--- @field callback fun ( date : OrgDate | nil , cleared ?: boolean )
18
- --- @field namespace function
19
20
--- @field date OrgDate ?
20
21
--- @field title ? string
21
22
--- @field on_day ? OrgCalendarOnRenderDay
@@ -239,17 +240,27 @@ function Calendar:render()
239
240
vim .api .nvim_buf_set_lines (self .buf , 0 , - 1 , true , content )
240
241
vim .api .nvim_buf_clear_namespace (self .buf , namespace , 0 , - 1 )
241
242
if self .clearable then
242
- vim .api .nvim_buf_add_highlight (self .buf , namespace , ' Comment' , # content - 3 , 0 , - 1 )
243
+ local range = Range :new ({
244
+ start_line = # content - 2 ,
245
+ start_col = 0 ,
246
+ end_line = # content - 2 ,
247
+ end_col = 1 ,
248
+ })
249
+ colors .highlight ({
250
+ range = range ,
251
+ hlgroup = ' Comment' ,
252
+ }, self .buf )
253
+ self :_apply_hl (' Comment' , # content - 3 , 0 , - 1 )
243
254
end
244
255
245
256
if not self :has_time () then
246
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' Comment' , 8 , 0 , - 1 )
257
+ self : _apply_hl ( ' Comment' , 8 , 0 , - 1 )
247
258
end
248
259
249
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' Comment' , # content - 4 , 0 , - 1 )
250
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' Comment' , # content - 3 , 0 , - 1 )
251
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' Comment' , # content - 2 , 0 , - 1 )
252
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' Comment' , # content - 1 , 0 , - 1 )
260
+ self : _apply_hl ( ' Comment' , # content - 4 , 0 , - 1 )
261
+ self : _apply_hl ( ' Comment' , # content - 3 , 0 , - 1 )
262
+ self : _apply_hl ( ' Comment' , # content - 2 , 0 , - 1 )
263
+ self : _apply_hl ( ' Comment' , # content - 1 , 0 , - 1 )
253
264
254
265
for i , line in ipairs (content ) do
255
266
local from = 0
@@ -274,14 +285,28 @@ function Calendar:render()
274
285
vim .api .nvim_set_option_value (' modifiable' , false , { buf = self .buf })
275
286
end
276
287
288
+ function Calendar :_apply_hl (hl_group , start_line , start_col , end_col )
289
+ local range = Range :new ({
290
+ start_line = start_line + 1 ,
291
+ start_col = start_col + 1 ,
292
+ end_line = start_line + 1 ,
293
+ end_col = end_col > 0 and end_col + 1 or end_col ,
294
+ })
295
+ colors .highlight ({
296
+ namespace = namespace ,
297
+ range = range ,
298
+ hlgroup = hl_group ,
299
+ }, self .buf )
300
+ end
301
+
277
302
--- @param day OrgDate
278
303
--- @param opts { from : number , to : number , line : number }
279
304
function Calendar :on_render_day (day , opts )
280
305
if day :is_today () then
281
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' OrgCalendarToday' , opts .line - 1 , opts .from - 1 , opts .to )
306
+ self : _apply_hl ( ' OrgCalendarToday' , opts .line - 1 , opts .from - 1 , opts .to )
282
307
end
283
308
if day :is_same_day (self .date ) then
284
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' OrgCalendarSelected' , opts .line - 1 , opts .from - 1 , opts .to )
309
+ self : _apply_hl ( ' OrgCalendarSelected' , opts .line - 1 , opts .from - 1 , opts .to )
285
310
end
286
311
if self .on_day then
287
312
self .on_day (
@@ -324,12 +349,12 @@ function Calendar:rerender_time()
324
349
else
325
350
vim .api .nvim_buf_set_lines (self .buf , 13 , 14 , true , { ' [d] - select day [T] - clear time' })
326
351
end
327
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' Normal' , 8 , 0 , - 1 )
328
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' Comment' , 13 , 0 , - 1 )
352
+ self : _apply_hl ( ' Normal' , 8 , 0 , - 1 )
353
+ self : _apply_hl ( ' Comment' , 13 , 0 , - 1 )
329
354
else
330
355
vim .api .nvim_buf_set_lines (self .buf , 13 , 14 , true , { ' [t] - enter time' })
331
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' Comment' , 8 , 0 , - 1 )
332
- vim . api . nvim_buf_add_highlight ( self . buf , namespace , ' Comment' , 13 , 0 , - 1 )
356
+ self : _apply_hl ( ' Comment' , 8 , 0 , - 1 )
357
+ self : _apply_hl ( ' Comment' , 13 , 0 , - 1 )
333
358
end
334
359
vim .api .nvim_set_option_value (' modifiable' , false , { buf = self .buf })
335
360
end
0 commit comments