From b9c1969126bc772a4ec34b56cd0de9b6fc4e44f5 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Mon, 11 Apr 2011 15:12:50 +0200
Subject: Drop empty notes after editing.

Keeping empty notes doesn't make sense here. Also, there doesn't seem to
be a simple way to erase notes yet. This will make calcurse delete any
notes that are empty (meaning that they are either 0-byte files or
contain nothing but a newline character) when returning from the editor.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/calcurse.h |  1 +
 src/day.c      |  3 +++
 src/io.c       | 25 +++++++++++++++++++++++++
 src/todo.c     |  3 +++
 4 files changed, 32 insertions(+)

(limited to 'src')

diff --git a/src/calcurse.h b/src/calcurse.h
index 4b28039..0a306f0 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -696,6 +696,7 @@ void             io_stop_psave_thread (void);
 void             io_set_lock (void);
 unsigned         io_dump_pid (char *);
 unsigned         io_get_pid (char *);
+int              io_file_is_empty (char *);
 
 /* keys.c */
 void      keys_init (void);
diff --git a/src/day.c b/src/day.c
index 0dc888e..50f025b 100644
--- a/src/day.c
+++ b/src/day.c
@@ -1085,6 +1085,9 @@ day_edit_note (char *editor)
   (void)snprintf (fullname, BUFSIZ, "%s%s", path_notes, p->note);
   wins_launch_external (fullname, editor);
 
+  if (io_file_is_empty (fullname))
+    erase_note (&p->note, ERASE_FORCE);
+
   date = calendar_get_slctd_day_sec ();
   switch (p->type)
     {
diff --git a/src/io.c b/src/io.c
index 7619696..04ad99f 100644
--- a/src/io.c
+++ b/src/io.c
@@ -3040,3 +3040,28 @@ io_get_pid (char *file)
 
   return pid;
 }
+
+/*
+ * Check whether a file is empty.
+ */
+int
+io_file_is_empty (char *file)
+{
+  FILE *fp;
+
+  if (file && (fp = fopen (file, "r")))
+    {
+      if (fgetc (fp) == '\n' || feof (fp))
+        {
+          fclose (fp);
+          return 1;
+        }
+      else
+        {
+          fclose (fp);
+          return 0;
+        }
+    }
+
+  return 1;
+}
diff --git a/src/todo.c b/src/todo.c
index 4076510..125b128 100644
--- a/src/todo.c
+++ b/src/todo.c
@@ -503,6 +503,9 @@ todo_edit_note (char *editor)
     }
   (void)snprintf (fullname, BUFSIZ, "%s%s", path_notes, i->note);
   wins_launch_external (fullname, editor);
+
+  if (io_file_is_empty (fullname))
+    erase_note (&i->note, ERASE_FORCE);
 }
 
 /* View a note previously attached to a todo */
-- 
cgit v1.2.3-70-g09d2