From 23f91caf4ec03f200861351cc0bc693fed7a7ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Fl=C3=BCgge?= Date: Sun, 14 Jan 2024 00:46:19 +0100 Subject: [PATCH] feat: round minutes on adjusting hours --- lua/orgmode/config/defaults.lua | 3 ++- lua/orgmode/objects/calendar.lua | 38 +++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lua/orgmode/config/defaults.lua b/lua/orgmode/config/defaults.lua index 7db469029..a73afab1b 100644 --- a/lua/orgmode/config/defaults.lua +++ b/lua/orgmode/config/defaults.lua @@ -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, diff --git a/lua/orgmode/objects/calendar.lua b/lua/orgmode/objects/calendar.lua index 523bf3dbc..7c54a419f 100644 --- a/lua/orgmode/objects/calendar.lua +++ b/lua/orgmode/objects/calendar.lua @@ -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 @@ -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 }) @@ -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 })