@@ -3,6 +3,10 @@ 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+
7+ --- @alias OrgCalendarOnRenderDayOpts { line : number , from : number , to : number , buf : number , namespace : number }
8+ --- @alias OrgCalendarOnRenderDay fun ( day : OrgDate , opts : OrgCalendarOnRenderDayOpts )
9+
610--- @class OrgCalendar
711--- @field win number
812--- @field buf number
@@ -11,6 +15,7 @@ local namespace = vim.api.nvim_create_namespace('org_calendar')
1115--- @field date OrgDate
1216--- @field month OrgDate
1317--- @field title ? string
18+ --- @field on_day ? OrgCalendarOnRenderDay
1419
1520local Calendar = {
1621 win = nil ,
@@ -22,12 +27,13 @@ Calendar.__index = Calendar
2227
2328vim .cmd ([[ hi default OrgCalendarToday gui=reverse cterm=reverse]] )
2429
25- --- @param data { date ?: OrgDate , clearable ?: boolean , title ?: string }
30+ --- @param data { date ?: OrgDate , clearable ?: boolean , title ?: string , on_day ?: OrgCalendarOnRenderDay }
2631function Calendar .new (data )
2732 data = data or {}
2833 local this = setmetatable ({}, Calendar )
2934 this .clearable = data .clearable
3035 this .title = data .title
36+ this .on_day = data .on_day
3137 if data .date then
3238 this .date = data .date
3339 this .month = this .date :set ({ day = 1 })
@@ -186,22 +192,47 @@ function Calendar:render()
186192 vim .api .nvim_buf_add_highlight (self .buf , namespace , ' Comment' , # content - 2 , 0 , - 1 )
187193 vim .api .nvim_buf_add_highlight (self .buf , namespace , ' Comment' , # content - 1 , 0 , - 1 )
188194
189- -- highlight the cell of the current day
190- local today = Date .today ()
191- local is_today_month = today :is_same (self .month , ' month' )
192- if is_today_month then
193- local day_formatted = today :format (' %d' )
194- for i , line in ipairs (content ) do
195- local from , to = line :find (' %s' .. day_formatted .. ' %s' )
195+ for i , line in ipairs (content ) do
196+ local from = 0
197+ local to , num
198+
199+ while true do
200+ from , to , num = line :find (' %s(%d%d?)%s' , from + 1 )
201+ if from == nil then
202+ break
203+ end
196204 if from and to then
197- vim .api .nvim_buf_add_highlight (self .buf , namespace , ' OrgCalendarToday' , i - 1 , from - 1 , to )
205+ local date = self .month :set ({ day = num })
206+ self :on_render_day (date , {
207+ from = from ,
208+ to = to ,
209+ line = i ,
210+ })
198211 end
199212 end
200213 end
201214
202215 vim .api .nvim_set_option_value (' modifiable' , false , { buf = self .buf })
203216end
204217
218+ --- @param day OrgDate
219+ --- @param opts { from : number , to : number , line : number }
220+ function Calendar :on_render_day (day , opts )
221+ local is_today = day :is_today ()
222+ if is_today then
223+ vim .api .nvim_buf_add_highlight (self .buf , namespace , ' OrgCalendarToday' , opts .line - 1 , opts .from - 1 , opts .to )
224+ end
225+ if self .on_day then
226+ self .on_day (
227+ day ,
228+ vim .tbl_extend (' force' , opts , {
229+ buf = self .buf ,
230+ namespace = namespace ,
231+ })
232+ )
233+ end
234+ end
235+
205236function Calendar :forward ()
206237 self .month = self .month :add ({ month = vim .v .count1 })
207238 self :render ()
@@ -303,8 +334,7 @@ function Calendar:get_selected_date()
303334 if line < 3 or not char :match (' %d' ) then
304335 return utils .echo_warning (' Please select valid day number.' , nil , false )
305336 end
306- day = tonumber (day )
307- return self .month :set ({ day = day })
337+ return self .month :set ({ day = tonumber (day ) })
308338end
309339
310340function Calendar :select ()
0 commit comments