From 711d5dea20679298192b25a5bbfe4fd70d9012d6 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Thu, 1 Sep 2011 16:09:18 +0200
Subject: Add configuration option to run the GC on exit

If "auto_gc" is enabled, the garbage collector for note files will be
run on every exit. As this is an experimental feature and may cause data
loss, this is disabled by default.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/calcurse.c |  2 ++
 src/calcurse.h |  1 +
 src/custom.c   | 46 +++++++++++++++++++++++++++++++---------------
 src/io.c       |  5 +++++
 src/vars.c     |  1 +
 5 files changed, 40 insertions(+), 15 deletions(-)

(limited to 'src')

diff --git a/src/calcurse.c b/src/calcurse.c
index ab55254..7dae864 100644
--- a/src/calcurse.c
+++ b/src/calcurse.c
@@ -537,6 +537,8 @@ main (int argc, char **argv)
         case KEY_GENERIC_QUIT:
           if (conf.auto_save)
             io_save_cal (&conf, IO_SAVE_DISPLAY_BAR);
+          if (conf.auto_gc)
+            note_gc ();
 
           if (conf.confirm_quit)
             {
diff --git a/src/calcurse.h b/src/calcurse.h
index 956010d..48325e8 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -219,6 +219,7 @@
 /* General configuration variables. */
 struct conf {
   unsigned   auto_save;
+  unsigned   auto_gc;
   unsigned   periodic_save;
   unsigned   confirm_quit;
   unsigned   confirm_delete;
diff --git a/src/custom.c b/src/custom.c
index b2679d2..78a3a6a 100644
--- a/src/custom.c
+++ b/src/custom.c
@@ -44,6 +44,7 @@
 /* Available configuration variables. */
 enum conf_var {
   CUSTOM_CONF_AUTOSAVE,
+  CUSTOM_CONF_AUTOGC,
   CUSTOM_CONF_PERIODICSAVE,
   CUSTOM_CONF_CONFIRMQUIT,
   CUSTOM_CONF_CONFIRMDELETE,
@@ -75,6 +76,7 @@ struct conf_varname {
 static struct conf_varname conf_varmap[] =
 {
   { CUSTOM_CONF_AUTOSAVE, "auto_save" },
+  { CUSTOM_CONF_AUTOGC, "auto_gc" },
   { CUSTOM_CONF_PERIODICSAVE, "periodic_save" },
   { CUSTOM_CONF_CONFIRMQUIT, "confirm_quit" },
   { CUSTOM_CONF_CONFIRMDELETE, "confirm_delete" },
@@ -291,6 +293,9 @@ custom_set_conf (struct conf *conf, enum conf_var var, char *val)
     case CUSTOM_CONF_AUTOSAVE:
       return conf_parse_bool (&conf->auto_save, val);
       break;
+    case CUSTOM_CONF_AUTOGC:
+      return conf_parse_bool (&conf->auto_gc, val);
+      break;
     case CUSTOM_CONF_PERIODICSAVE:
       return conf_parse_unsigned (&conf->periodic_save, val);
       break;
@@ -1017,6 +1022,7 @@ print_general_options (WINDOW *win, struct conf *conf)
 {
   enum {
     AUTO_SAVE,
+    AUTO_GC,
     PERIODIC_SAVE,
     CONFIRM_QUIT,
     CONFIRM_DELETE,
@@ -1032,6 +1038,7 @@ print_general_options (WINDOW *win, struct conf *conf)
   int y;
   char *opt[NB_OPTIONS] = {
     _("auto_save = "),
+    _("auto_gc = "),
     _("periodic_save = "),
     _("confirm_quit = "),
     _("confirm_delete = "),
@@ -1049,7 +1056,13 @@ print_general_options (WINDOW *win, struct conf *conf)
   mvwprintw (win, y + 1, XPOS,
              _("(if set to YES, automatic save is done when quitting)"));
   y += YOFF;
-  mvwprintw (win, y, XPOS, "[2] %s      ", opt[PERIODIC_SAVE]);
+  mvwprintw (win, y, XPOS, "[2] %s      ", opt[AUTO_GC]);
+  print_bool_option_incolor (win, conf->auto_gc, y,
+                             XPOS + 4 + strlen (opt[AUTO_GC]));
+  mvwprintw (win, y + 1, XPOS,
+             _("(run the garbage collector when quitting)"));
+  y += YOFF;
+  mvwprintw (win, y, XPOS, "[3] %s      ", opt[PERIODIC_SAVE]);
   custom_apply_attr (win, ATTR_HIGHEST);
   mvwprintw (win, y, XPOS + 4 + strlen (opt[PERIODIC_SAVE]), "%d",
              conf->periodic_save);
@@ -1058,41 +1071,41 @@ print_general_options (WINDOW *win, struct conf *conf)
              _("(if not null, automatically save data every 'periodic_save' "
                "minutes)"));
   y += YOFF;
-  mvwprintw (win, y, XPOS, "[3] %s      ", opt[CONFIRM_QUIT]);
+  mvwprintw (win, y, XPOS, "[4] %s      ", opt[CONFIRM_QUIT]);
   print_bool_option_incolor (win, conf->confirm_quit, y,
                              XPOS + 4 + strlen (opt[CONFIRM_QUIT]));
   mvwprintw (win, y + 1, XPOS,
              _("(if set to YES, confirmation is required before quitting)"));
   y += YOFF;
-  mvwprintw (win, y, XPOS, "[4] %s      ", opt[CONFIRM_DELETE]);
+  mvwprintw (win, y, XPOS, "[5] %s      ", opt[CONFIRM_DELETE]);
   print_bool_option_incolor (win, conf->confirm_delete, y,
                              XPOS + 4 + strlen (opt[CONFIRM_DELETE]));
   mvwprintw (win, y + 1, XPOS,
              _("(if set to YES, confirmation is required "
                "before deleting an event)"));
   y += YOFF;
-  mvwprintw (win, y, XPOS, "[5] %s      ", opt[SKIP_SYSTEM_DIAGS]);
+  mvwprintw (win, y, XPOS, "[6] %s      ", opt[SKIP_SYSTEM_DIAGS]);
   print_bool_option_incolor (win, conf->skip_system_dialogs, y,
                              XPOS + 4 + strlen (opt[SKIP_SYSTEM_DIAGS]));
   mvwprintw (win, y + 1, XPOS,
              _("(if set to YES, messages about loaded "
                "and saved data will not be displayed)"));
   y += YOFF;
-  mvwprintw (win, y, XPOS, "[6] %s      ", opt[SKIP_PROGRESS_BAR]);
+  mvwprintw (win, y, XPOS, "[7] %s      ", opt[SKIP_PROGRESS_BAR]);
   print_bool_option_incolor (win, conf->skip_progress_bar, y,
                             XPOS + 4 + strlen (opt[SKIP_PROGRESS_BAR]));
   mvwprintw (win, y + 1, XPOS,
              _("(if set to YES, progress bar will not be displayed "
                 "when saving data)"));
   y += YOFF;
-  mvwprintw (win, y, XPOS, "[7] %s      ", opt[WEEK_BEGINS_MONDAY]);
+  mvwprintw (win, y, XPOS, "[8] %s      ", opt[WEEK_BEGINS_MONDAY]);
   print_bool_option_incolor (win, calendar_week_begins_on_monday (), y,
                              XPOS + 4 + strlen (opt[WEEK_BEGINS_MONDAY]));
   mvwprintw (win, y + 1, XPOS,
              _("(if set to YES, monday is the first day of the week, "
                "else it is sunday)"));
   y += YOFF;
-  mvwprintw (win, y, XPOS, "[8] %s      ", opt[OUTPUT_DATE_FMT]);
+  mvwprintw (win, y, XPOS, "[9] %s      ", opt[OUTPUT_DATE_FMT]);
   custom_apply_attr (win, ATTR_HIGHEST);
   mvwprintw (win, y, XPOS + 4 + strlen (opt[OUTPUT_DATE_FMT]), "%s",
              conf->output_datefmt);
@@ -1100,7 +1113,7 @@ print_general_options (WINDOW *win, struct conf *conf)
   mvwprintw (win, y + 1, XPOS,
              _("(Format of the date to be displayed in non-interactive mode)"));
   y += YOFF;
-  mvwprintw (win, y, XPOS, "[9] %s      ", opt[INPUT_DATE_FMT]);
+  mvwprintw (win, y, XPOS, "[0] %s      ", opt[INPUT_DATE_FMT]);
   custom_apply_attr (win, ATTR_HIGHEST);
   mvwprintw (win, y, XPOS + 4 + strlen (opt[INPUT_DATE_FMT]), "%d",
              conf->input_datefmt);
@@ -1171,6 +1184,9 @@ custom_general_config (struct conf *conf)
           conf->auto_save = !conf->auto_save;
           break;
         case '2':
+          conf->auto_gc = !conf->auto_gc;
+          break;
+        case '3':
           status_mesg (periodic_save_str, "");
           if (updatestring (win[STA].p, &buf, 0, 1) == 0)
             {
@@ -1184,22 +1200,22 @@ custom_general_config (struct conf *conf)
             }
           status_mesg (number_str, keys);
           break;
-        case '3':
+        case '4':
           conf->confirm_quit = !conf->confirm_quit;
           break;
-        case '4':
+        case '5':
           conf->confirm_delete = !conf->confirm_delete;
           break;
-        case '5':
+        case '6':
           conf->skip_system_dialogs = !conf->skip_system_dialogs;
           break;
-        case '6':
+        case '7':
           conf->skip_progress_bar = !conf->skip_progress_bar;
           break;
-        case '7':
+        case '8':
           calendar_change_first_day_of_week ();
           break;
-        case '8':
+        case '9':
           status_mesg (output_datefmt_str, "");
           (void)strncpy (buf, conf->output_datefmt,
                          strlen (conf->output_datefmt) + 1);
@@ -1209,7 +1225,7 @@ custom_general_config (struct conf *conf)
             }
           status_mesg (number_str, keys);
           break;
-        case '9':
+        case '0':
           status_mesg (input_datefmt_str, "");
           if (updatestring (win[STA].p, &buf, 0, 1) == 0)
             {
diff --git a/src/io.c b/src/io.c
index c71934a..c3a6a61 100644
--- a/src/io.c
+++ b/src/io.c
@@ -867,6 +867,11 @@ io_save_conf (struct conf *conf)
   (void)fprintf (fp, "auto_save=");
   (void)fprintf (fp, "%s\n", (conf->auto_save) ? "yes" : "no");
 
+  (void)fprintf (fp, "\n# If this option is set to yes, "
+                 "the GC is run automatically when quitting\n");
+  (void)fprintf (fp, "auto_gc=");
+  (void)fprintf (fp, "%s\n", (conf->auto_gc) ? "yes" : "no");
+
   (void)fprintf (fp, "\n# If not null, perform automatic saves every "
                  "'periodic_save' minutes\n");
   (void)fprintf (fp, "periodic_save=");
diff --git a/src/vars.c b/src/vars.c
index ef2cd73..1e2453f 100644
--- a/src/vars.c
+++ b/src/vars.c
@@ -125,6 +125,7 @@ vars_init (struct conf *conf)
   conf->confirm_quit = 1;
   conf->confirm_delete = 1;
   conf->auto_save = 1;
+  conf->auto_gc = 0;
   conf->periodic_save = 0;
   conf->skip_system_dialogs = 0;
   conf->skip_progress_bar = 0;
-- 
cgit v1.2.3-70-g09d2