@@ -3,6 +3,10 @@ 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
+
7
+ --- @alias OrgCalendarOnRenderDayOpts { line : number , from : number , to : number , buf : number , namespace : number }
8
+ --- @alias OrgCalendarOnRenderDay fun ( day : OrgDate , opts : OrgCalendarOnRenderDayOpts )
9
+
6
10
--- @class OrgCalendar
7
11
--- @field win number
8
12
--- @field buf number
@@ -11,6 +15,7 @@ local namespace = vim.api.nvim_create_namespace('org_calendar')
11
15
--- @field date OrgDate
12
16
--- @field month OrgDate
13
17
--- @field title ? string
18
+ --- @field on_day ? OrgCalendarOnRenderDay
14
19
15
20
local Calendar = {
16
21
win = nil ,
@@ -22,12 +27,13 @@ Calendar.__index = Calendar
22
27
23
28
vim .cmd ([[ hi default OrgCalendarToday gui=reverse cterm=reverse]] )
24
29
25
- --- @param data { date ?: OrgDate , clearable ?: boolean , title ?: string }
30
+ --- @param data { date ?: OrgDate , clearable ?: boolean , title ?: string , on_day ?: OrgCalendarOnRenderDay }
26
31
function Calendar .new (data )
27
32
data = data or {}
28
33
local this = setmetatable ({}, Calendar )
29
34
this .clearable = data .clearable
30
35
this .title = data .title
36
+ this .on_day = data .on_day
31
37
if data .date then
32
38
this .date = data .date
33
39
this .month = this .date :set ({ day = 1 })
@@ -186,22 +192,47 @@ function Calendar:render()
186
192
vim .api .nvim_buf_add_highlight (self .buf , namespace , ' Comment' , # content - 2 , 0 , - 1 )
187
193
vim .api .nvim_buf_add_highlight (self .buf , namespace , ' Comment' , # content - 1 , 0 , - 1 )
188
194
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
196
204
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
+ })
198
211
end
199
212
end
200
213
end
201
214
202
215
vim .api .nvim_set_option_value (' modifiable' , false , { buf = self .buf })
203
216
end
204
217
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
+
205
236
function Calendar :forward ()
206
237
self .month = self .month :add ({ month = vim .v .count1 })
207
238
self :render ()
@@ -303,8 +334,7 @@ function Calendar:get_selected_date()
303
334
if line < 3 or not char :match (' %d' ) then
304
335
return utils .echo_warning (' Please select valid day number.' , nil , false )
305
336
end
306
- day = tonumber (day )
307
- return self .month :set ({ day = day })
337
+ return self .month :set ({ day = tonumber (day ) })
308
338
end
309
339
310
340
function Calendar :select ()
0 commit comments