Skip to content

Commit da9a0cf

Browse files
authored
Calendar next/prev supports count prefix (#438)
1 parent e4b3baa commit da9a0cf

File tree

1 file changed

+53
-34
lines changed

1 file changed

+53
-34
lines changed

lua/orgmode/objects/calendar.lua

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,20 @@ function Calendar.new(data)
3232
return Calendar
3333
end
3434

35+
local width = 36
36+
local height = 10
37+
local x_offset = 1 -- one border cell
38+
local y_offset = 2 -- one border cell and one padding cell
39+
3540
function Calendar.open()
3641
local opts = {
3742
relative = 'editor',
38-
width = 36,
39-
height = Calendar.clearable and 11 or 10,
43+
width = width,
44+
height = Calendar.clearable and height + 1 or height,
4045
style = 'minimal',
4146
border = 'single',
42-
row = vim.o.lines / 2 - 4,
43-
col = vim.o.columns / 2 - 20,
47+
row = vim.o.lines / 2 - (y_offset + height) / 2,
48+
col = vim.o.columns / 2 - (x_offset + width) / 2,
4449
}
4550

4651
Calendar.buf = vim.api.nvim_create_buf(false, true)
@@ -85,7 +90,7 @@ function Calendar.open()
8590
if Calendar.date then
8691
search_day = Calendar.date:format('%d')
8792
end
88-
vim.fn.cursor({ 2, 0 })
93+
vim.fn.cursor(2, 0)
8994
vim.fn.search(search_day, 'W')
9095
return Promise.new(function(resolve)
9196
Calendar.callback = resolve
@@ -94,55 +99,69 @@ end
9499

95100
function Calendar.render()
96101
vim.api.nvim_buf_set_option(Calendar.buf, 'modifiable', true)
102+
103+
local cal_rows = { {}, {}, {}, {}, {}, {} } -- the calendar rows
97104
local start_from_sunday = config.calendar_week_start_day == 0
105+
local weekday_row = { 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' }
98106

99-
local first_row = { 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' }
100107
if start_from_sunday then
101-
first_row = { 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' }
108+
weekday_row = { 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' }
102109
end
103-
local content = { {}, {}, {}, {}, {}, {} }
110+
111+
-- construct title (Month YYYY)
112+
local title = Calendar.month:format('%B %Y')
113+
title = string.rep(' ', math.floor((width - title:len()) / 2)) .. title
114+
115+
-- insert whitespace before first day of month
104116
local start_weekday = Calendar.month:get_isoweekday()
105117
if start_from_sunday then
106118
start_weekday = Calendar.month:get_weekday()
107119
end
108120
while start_weekday > 1 do
109-
table.insert(content[1], ' ')
121+
table.insert(cal_rows[1], ' ')
110122
start_weekday = start_weekday - 1
111123
end
112-
local today = Date.today()
113-
local is_today_month = today:is_same(Calendar.month, 'month')
124+
125+
-- insert dates into cal_rows
114126
local dates = Calendar.month:get_range_until(Calendar.month:end_of('month'))
115-
local month = Calendar.month:format('%B %Y')
116-
month = string.rep(' ', math.floor((36 - month:len()) / 2)) .. month
117-
local start_row = 1
127+
local current_row = 1
118128
for _, date in ipairs(dates) do
119-
table.insert(content[start_row], date:format('%d'))
120-
if #content[start_row] % 7 == 0 then
121-
start_row = start_row + 1
129+
table.insert(cal_rows[current_row], date:format('%d'))
130+
if #cal_rows[current_row] % 7 == 0 then
131+
current_row = current_row + 1
122132
end
123133
end
124-
local value = vim.tbl_map(function(item)
134+
135+
-- add spacing between the calendar cells
136+
local content = vim.tbl_map(function(item)
125137
return ' ' .. table.concat(item, ' ') .. ' '
126-
end, content)
127-
first_row = ' ' .. table.concat(first_row, ' ')
128-
table.insert(value, 1, first_row)
129-
table.insert(value, 1, month)
130-
table.insert(value, ' [<] - prev month [>] - next month')
131-
table.insert(value, ' [.] - today [Enter] - select day')
138+
end, cal_rows)
139+
weekday_row = ' ' .. table.concat(weekday_row, ' ')
140+
141+
-- put it all together
142+
table.insert(content, 1, weekday_row)
143+
table.insert(content, 1, title)
144+
-- TODO: redundant, since it's static data
145+
table.insert(content, ' [<] - prev month [>] - next month')
146+
table.insert(content, ' [.] - today [Enter] - select day')
132147
if Calendar.clearable then
133-
table.insert(value, ' [r] Clear date')
148+
table.insert(content, ' [r] Clear date')
134149
end
135150

136-
vim.api.nvim_buf_set_lines(Calendar.buf, 0, -1, true, value)
151+
vim.api.nvim_buf_set_lines(Calendar.buf, 0, -1, true, content)
137152
vim.api.nvim_buf_clear_namespace(Calendar.buf, Calendar.namespace, 0, -1)
138153
if Calendar.clearable then
139-
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'Comment', #value - 3, 0, -1)
154+
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'Comment', #content - 3, 0, -1)
140155
end
141-
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'Comment', #value - 2, 0, -1)
142-
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'Comment', #value - 1, 0, -1)
156+
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'Comment', #content - 2, 0, -1)
157+
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'Comment', #content - 1, 0, -1)
158+
159+
-- highlight the cell of the current day
160+
local today = Date.today()
161+
local is_today_month = today:is_same(Calendar.month, 'month')
143162
if is_today_month then
144163
local day_formatted = today:format('%d')
145-
for i, line in ipairs(value) do
164+
for i, line in ipairs(content) do
146165
local from, to = line:find('%s' .. day_formatted .. '%s')
147166
if from and to then
148167
vim.api.nvim_buf_add_highlight(Calendar.buf, Calendar.namespace, 'OrgCalendarToday', i - 1, from - 1, to)
@@ -154,14 +173,14 @@ function Calendar.render()
154173
end
155174

156175
function Calendar.forward()
157-
Calendar.month = Calendar.month:add({ month = 1 })
176+
Calendar.month = Calendar.month:add({ month = vim.v.count1 })
158177
Calendar.render()
159-
vim.fn.cursor({ 2, 0 })
178+
vim.fn.cursor(2, 0)
160179
vim.fn.search('01')
161180
end
162181

163182
function Calendar.backward()
164-
Calendar.month = Calendar.month:subtract({ month = 1 })
183+
Calendar.month = Calendar.month:subtract({ month = vim.v.count1 })
165184
Calendar.render()
166185
vim.fn.cursor('$', 0)
167186
vim.fn.search([[\d\d]], 'b')
@@ -173,7 +192,7 @@ function Calendar.cursor_right()
173192
local curr_line = vim.fn.getline('.')
174193
local offset = curr_line:sub(col + 1, #curr_line):find('%d%d')
175194
if offset ~= nil then
176-
vim.fn.cursor({ line, col + offset })
195+
vim.fn.cursor(line, col + offset)
177196
end
178197
end
179198
end

0 commit comments

Comments
 (0)