@@ -3,6 +3,8 @@ local utils = require('orgmode.utils')
33local Promise = require (' orgmode.utils.promise' )
44local config = require (' orgmode.config' )
55local namespace = vim .api .nvim_create_namespace (' org_calendar' )
6+ local colors = require (' orgmode.colors' )
7+ local Range = require (' orgmode.files.elements.range' )
68
79--- @alias OrgCalendarOnRenderDayOpts { line : number , from : number , to : number , buf : number , namespace : number }
810--- @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
1517--- @field win number ?
1618--- @field buf number ?
1719--- @field callback fun ( date : OrgDate | nil , cleared ?: boolean )
18- --- @field namespace function
1920--- @field date OrgDate ?
2021--- @field title ? string
2122--- @field on_day ? OrgCalendarOnRenderDay
@@ -239,17 +240,27 @@ function Calendar:render()
239240 vim .api .nvim_buf_set_lines (self .buf , 0 , - 1 , true , content )
240241 vim .api .nvim_buf_clear_namespace (self .buf , namespace , 0 , - 1 )
241242 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 )
243254 end
244255
245256 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 )
247258 end
248259
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 )
253264
254265 for i , line in ipairs (content ) do
255266 local from = 0
@@ -274,14 +285,28 @@ function Calendar:render()
274285 vim .api .nvim_set_option_value (' modifiable' , false , { buf = self .buf })
275286end
276287
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+
277302--- @param day OrgDate
278303--- @param opts { from : number , to : number , line : number }
279304function Calendar :on_render_day (day , opts )
280305 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 )
282307 end
283308 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 )
285310 end
286311 if self .on_day then
287312 self .on_day (
@@ -324,12 +349,12 @@ function Calendar:rerender_time()
324349 else
325350 vim .api .nvim_buf_set_lines (self .buf , 13 , 14 , true , { ' [d] - select day [T] - clear time' })
326351 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 )
329354 else
330355 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 )
333358 end
334359 vim .api .nvim_set_option_value (' modifiable' , false , { buf = self .buf })
335360end
0 commit comments