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 Feb 17, 2024
1 parent 3f49a20 commit 23f91ca
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lua/orgmode/config/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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
38 changes: 30 additions & 8 deletions lua/orgmode/objects/calendar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local Promise = require('orgmode.utils.promise')
local config = require('orgmode.config')
--
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 @@ -288,23 +288,43 @@ end
---@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 Date
---@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 Calendar.select_state ~= SelState.DAY then
-- to avoid unexpectedly changing the day we cache it ...
local day = Calendar.date.day
if Calendar.select_state == SelState.HOUR then
Calendar.date = Calendar.date:add({ hour = vim.v.count1 })
Calendar.date = Calendar.date:add(step_hour('up', Calendar.date, vim.v.count1))
elseif Calendar.select_state == SelState.MIN_BIG then
Calendar.date = Calendar.date:add({ min = step('up', big_minute_step, Calendar.date.min, vim.v.count1) })
Calendar.date = Calendar.date:add({ min = step_minute('up', big_minute_step, Calendar.date.min, vim.v.count1) })
elseif Calendar.select_state == SelState.MIN_SMALL then
Calendar.date = Calendar.date:add({ min = step('up', small_minute_step, Calendar.date.min, vim.v.count1) })
Calendar.date = Calendar.date:add({ min = step_minute('up', small_minute_step, Calendar.date.min, vim.v.count1) })
end
-- and restore the cached day after adjusting the time
Calendar.date = Calendar.date:set({ day = day })
Expand Down Expand Up @@ -341,11 +361,13 @@ function Calendar.cursor_down()
-- to avoid unexpectedly changing the day we cache it ...
local day = Calendar.date.day
if Calendar.select_state == SelState.HOUR then
Calendar.date = Calendar.date:subtract({ hour = 1 * vim.v.count1 })
Calendar.date = Calendar.date:subtract(step_hour('down', Calendar.date, vim.v.count1))
elseif Calendar.select_state == SelState.MIN_BIG then
Calendar.date = Calendar.date:subtract({ min = step('down', big_minute_step, Calendar.date.min, vim.v.count1) })
Calendar.date =
Calendar.date:subtract({ min = step_minute('down', big_minute_step, Calendar.date.min, vim.v.count1) })
elseif Calendar.select_state == SelState.MIN_SMALL then
Calendar.date = Calendar.date:subtract({ min = step('down', small_minute_step, Calendar.date.min, vim.v.count1) })
Calendar.date =
Calendar.date:subtract({ min = step_minute('down', small_minute_step, Calendar.date.min, vim.v.count1) })
end
-- and restore the cached day after adjusting the time
Calendar.date = Calendar.date:set({ day = day })
Expand Down

0 comments on commit 23f91ca

Please sign in to comment.