From 7a230fa76a5a0f5ada7b6afd2c42b2f99a549569 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Sat, 21 Jan 2012 22:31:47 +0100
Subject: src/calendar.c: Fix range check in calendar_move()

We added count prefix support to motion commands in commit
59e006e56d9f893506af56a4ca114fe53b537e49 but obviously forgot to check
whether the range checks in calendar_move() still work correctly.

Refactor out range checks and replace them by a single check that is
performed *after* the new date is computed, but before we assign the new
value to the actual selected date. This ensures we won't have to bother
about these again when changing/adding functionality in/to
calendar_move(), while keeping performance (date_change() is pretty
cheap and range violations are corner cases anyway).

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/calendar.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

(limited to 'src')

diff --git a/src/calendar.c b/src/calendar.c
index 8fea79d..c369ef0 100644
--- a/src/calendar.c
+++ b/src/calendar.c
@@ -681,27 +681,15 @@ calendar_move (enum move move, int count)
   switch (move)
     {
     case UP:
-      if ((slctd_day.dd <= 7) && (slctd_day.mm == 1)
-          && (slctd_day.yyyy == 1902))
-        return;
       ret = date_change (&t, 0, -count * WEEKINDAYS);
       break;
     case DOWN:
-      if ((slctd_day.dd > days[slctd_day.mm - 1] - 7)
-          && (slctd_day.mm == 12) && (slctd_day.yyyy == 2037))
-        return;
       ret = date_change (&t, 0, count * WEEKINDAYS);
       break;
     case LEFT:
-      if ((slctd_day.dd == 1) && (slctd_day.mm == 1)
-          && (slctd_day.yyyy == 1902))
-        return;
       ret = date_change (&t, 0, -count);
       break;
     case RIGHT:
-      if ((slctd_day.dd == 31) && (slctd_day.mm == 12)
-          && (slctd_day.yyyy == 2037))
-        return;
       ret = date_change (&t, 0, count);
       break;
     case WEEK_START:
@@ -728,8 +716,22 @@ calendar_move (enum move move, int count)
       ret = 1;
       /* NOTREACHED */
     }
+
   if (ret == 0)
     {
+      if (t.tm_year < 2)
+        {
+          t.tm_mday = 1;
+          t.tm_mon = 0;
+          t.tm_year = 2;
+        }
+      else if (t.tm_year > 137)
+        {
+          t.tm_mday = 31;
+          t.tm_mon = 11;
+          t.tm_year = 137;
+        }
+
       slctd_day.dd = t.tm_mday;
       slctd_day.mm = t.tm_mon + 1;
       slctd_day.yyyy = t.tm_year + 1900;
-- 
cgit v1.2.3-70-g09d2