Skip to content

Commit

Permalink
feat: round minutes on adjusting hours
Browse files Browse the repository at this point in the history
  • Loading branch information
seflue committed May 25, 2024
1 parent bf47d29 commit 5345e65
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lua/orgmode/config/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ local DefaultConfig = {
org_startup_indented = false,
org_indent_mode_turns_off_org_adapt_indentation = true,
org_indent_mode_turns_on_hiding_stars = true,
org_time_stamp_rounding_min_big = 15,
org_time_picker_min_big = 15,
org_time_picker_round_min_with_hours = true,
org_time_stamp_rounding_minutes = 5,
org_blank_before_new_entry = {
heading = true,
Expand Down
66 changes: 48 additions & 18 deletions lua/orgmode/objects/calendar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local namespace = vim.api.nvim_create_namespace('org_calendar')
---@alias OrgCalendarOnRenderDay fun(day: OrgDate, opts: OrgCalendarOnRenderDayOpts)

local SelState = { DAY = 0, HOUR = 1, MIN_BIG = 2, MIN_SMALL = 3 }
local big_minute_step = config.org_time_stamp_rounding_min_big
local big_minute_step = config.org_time_picker_min_big
local small_minute_step = config.org_time_stamp_rounding_minutes

---@class OrgCalendar
Expand Down Expand Up @@ -313,15 +313,15 @@ function Calendar:forward()
self:render()
vim.fn.cursor(2, 1)
vim.fn.search('01')
Calendar.render()
self:render()
end

function Calendar:backward()
self.month = self.month:subtract({ month = vim.v.count1 })
self:render()
vim.fn.cursor(vim.fn.line('$'), 0)
vim.fn.search([[\d\d]], 'b')
Calendar.render()
self:render()
end

function Calendar:cursor_right()
Expand All @@ -343,8 +343,8 @@ function Calendar:cursor_right()
vim.fn.cursor(line, col + offset)
end
end
Calendar.date = Calendar.get_selected_date()
Calendar.render()
self.date = self:get_selected_date()
self:render()
end

function Calendar:cursor_left()
Expand All @@ -366,31 +366,51 @@ function Calendar:cursor_left()
vim.fn.cursor(line, offset)
end
end
Calendar.date = Calendar.get_selected_date()
Calendar.render()
self.date = self:get_selected_date()
self:render()
end

---@param direction string
---@param step_size number
---@param current number
---@param count number
local function step(direction, step_size, current, count)
local function step_minute(direction, step_size, current, count)
local sign = direction == 'up' and -1 or 1
local residual = current % step_size
local factor = (residual == 0 or direction == 'up') and count or count - 1
return factor * step_size + sign * residual
end

--- Controls, how the hours are adjusted. The rounding the minutes can be disabled
--- by the user, so adjusting the hours just moves the time 1 our back or forth
---@param direction string
---@param current OrgDate
---@param count number
---@return table
local function step_hour(direction, current, count)
if not config.org_time_picker_round_min_with_hours or current.min % big_minute_step == 0 then
return { hour = count, min = 0 }
end

-- if adjusting the mins would land on a full hour, we don't step a full hour,
-- otherwise we do and round the minutes
local sign = direction == 'up' and 1 or -1
local min = step_minute(direction, big_minute_step, current.min, 1)
local min_new = current.min + sign * min
local hour = min_new % 60 ~= 0 and count or count - 1
return { hour = hour, min = min }
end

function Calendar:cursor_up()
if self.select_state ~= SelState.DAY then
-- to avoid unexpectedly changing the day we cache it ...
local day = self.date.day
if self.select_state == SelState.HOUR then
self.date = self.date:add({ hour = vim.v.count1 })
self.date = self.date:add(step_hour('up', self.date, vim.v.count1))
elseif self.select_state == SelState.MIN_BIG then
self.date = self.date:add({ min = step('up', big_minute_step, self.date.min, vim.v.count1) })
self.date = self.date:add({ min = step_minute('up', big_minute_step, self.date.min, vim.v.count1) })
elseif self.select_state == SelState.MIN_SMALL then
self.date = self.date:add({ min = step('up', small_minute_step, self.date.min, vim.v.count1) })
self.date = self.date:add({ min = step_minute('up', small_minute_step, self.date.min, vim.v.count1) })
end
-- and restore the cached day after adjusting the time
self.date = self.date:set({ day = day })
Expand Down Expand Up @@ -419,20 +439,30 @@ function Calendar:cursor_up()
end
vim.fn.cursor(line - 1, move_to)
end
Calendar.date = Calendar.get_selected_date()
Calendar.render()
self.date = self:get_selected_date()
self:render()
end

function Calendar:cursor_down()
if self.select_state ~= SelState.DAY then
-- to avoid unexpectedly changing the day we cache it ...
--<<<<<<< HEAD
-- local day = self.date.day
-- if self.select_state == SelState.HOUR then
-- self.date = self.date:subtract({ hour = 1 * vim.v.count1 })
-- elseif self.select_state == SelState.MIN_BIG then
-- self.date = self.date:subtract({ min = step('down', big_minute_step, self.date.min, vim.v.count1) })
-- elseif self.select_state == SelState.MIN_SMALL then
-- self.date = self.date:subtract({ min = step('down', small_minute_step, self.date.min, vim.v.count1) })
--=======
local day = self.date.day
if self.select_state == SelState.HOUR then
self.date = self.date:subtract({ hour = 1 * vim.v.count1 })
self.date = self.date:subtract(step_hour('down', self.date, vim.v.count1))
elseif self.select_state == SelState.MIN_BIG then
self.date = self.date:subtract({ min = step('down', big_minute_step, self.date.min, vim.v.count1) })
self.date = self.date:subtract({ min = step_minute('down', big_minute_step, self.date.min, vim.v.count1) })
elseif self.select_state == SelState.MIN_SMALL then
self.date = self.date:subtract({ min = step('down', small_minute_step, self.date.min, vim.v.count1) })
self.date = self.date:subtract({ min = step_minute('down', small_minute_step, self.date.min, vim.v.count1) })
-->>>>>>> 8d7fcfd (feat: round minutes on adjusting hours)
end
-- and restore the cached day after adjusting the time
self.date = self.date:set({ day = day })
Expand Down Expand Up @@ -460,8 +490,8 @@ function Calendar:cursor_down()
end
vim.fn.cursor(line + 1, move_to)
end
Calendar.date = Calendar.get_selected_date()
Calendar.render()
self.date = self:get_selected_date()
self:render()
end

function Calendar:reset()
Expand Down

0 comments on commit 5345e65

Please sign in to comment.