Skip to content

Commit b28a976

Browse files
Merge pull request #209 from nathom/master
2 parents 9e3f190 + a02b89f commit b28a976

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

lua/orgmode/objects/calendar.lua

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ function Calendar.open()
5555
vim.api.nvim_buf_set_var(Calendar.buf, 'indent_blankline_enabled', false)
5656
vim.api.nvim_buf_set_option(Calendar.buf, 'bufhidden', 'wipe')
5757

58+
utils.buf_keymap(Calendar.buf, 'n', 'j', '<cmd>lua require("orgmode.objects.calendar").cursor_down()<cr>')
59+
utils.buf_keymap(Calendar.buf, 'n', 'k', '<cmd>lua require("orgmode.objects.calendar").cursor_up()<cr>')
60+
utils.buf_keymap(Calendar.buf, 'n', 'h', '<cmd>lua require("orgmode.objects.calendar").cursor_left()<cr>')
61+
utils.buf_keymap(Calendar.buf, 'n', 'l', '<cmd>lua require("orgmode.objects.calendar").cursor_right()<cr>')
5862
utils.buf_keymap(Calendar.buf, 'n', '>', '<cmd>lua require("orgmode.objects.calendar").forward()<CR>')
5963
utils.buf_keymap(Calendar.buf, 'n', '<', '<cmd>lua require("orgmode.objects.calendar").backward()<CR>')
6064
utils.buf_keymap(Calendar.buf, 'n', '<CR>', '<cmd>lua require("orgmode.objects.calendar").select()<CR>')
@@ -134,6 +138,76 @@ function Calendar.backward()
134138
vim.fn.search([[\d\d]], 'b')
135139
end
136140

141+
function Calendar.cursor_right()
142+
for i = 1, vim.v.count1 do
143+
local line, col = vim.fn.line('.'), vim.fn.col('.')
144+
local curr_line = vim.fn.getline('.')
145+
local offset = curr_line:sub(col + 1, #curr_line):find('%d%d')
146+
if offset ~= nil then
147+
vim.fn.cursor(line, col + offset)
148+
end
149+
end
150+
end
151+
152+
function Calendar.cursor_left()
153+
for i = 1, vim.v.count1 do
154+
local line, col = vim.fn.line('.'), vim.fn.col('.')
155+
local curr_line = vim.fn.getline('.')
156+
local _, offset = curr_line:sub(1, col - 1):find('.*%d%d')
157+
if offset ~= nil then
158+
vim.fn.cursor(line, offset)
159+
end
160+
end
161+
end
162+
163+
function Calendar.cursor_up()
164+
for i = 1, vim.v.count1 do
165+
local line, col = vim.fn.line('.'), vim.fn.col('.')
166+
if line > 9 then
167+
vim.fn.cursor(line - 1, col)
168+
return
169+
end
170+
171+
local prev_line = vim.fn.getline(line - 1)
172+
local first_num = prev_line:find('%d%d')
173+
if first_num == nil then
174+
return
175+
end
176+
177+
local move_to
178+
if first_num > col then
179+
move_to = first_num
180+
else
181+
move_to = col
182+
end
183+
vim.fn.cursor(line - 1, move_to)
184+
end
185+
end
186+
187+
function Calendar.cursor_down()
188+
for i = 1, vim.v.count1 do
189+
local line, col = vim.fn.line('.'), vim.fn.col('.')
190+
if line <= 1 then
191+
vim.fn.cursor(line + 1, col)
192+
return
193+
end
194+
195+
local next_line = vim.fn.getline(line + 1)
196+
local _, last_num = next_line:find('.*%d%d')
197+
if last_num == nil then
198+
return
199+
end
200+
201+
local move_to
202+
if last_num < col then
203+
move_to = last_num
204+
else
205+
move_to = col
206+
end
207+
vim.fn.cursor(line + 1, move_to)
208+
end
209+
end
210+
137211
function Calendar.reset()
138212
local today = Calendar.month:set_todays_date()
139213
Calendar.month = today:set({ day = 1 })

0 commit comments

Comments
 (0)