From 80df9542cbc38f287cfb17b1b5da68e282532708 Mon Sep 17 00:00:00 2001
From: pandora17 <theil.markus@googlemail.com>
Date: Tue, 21 Feb 2012 17:26:59 +0100
Subject: [PATCH] file change notification added

---
 src/buffer.c |   20 ++++++++++++++++++++
 src/extern.h |    2 ++
 src/file.c   |    5 +++++
 3 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/src/buffer.c b/src/buffer.c
index 73bbbf7..2de7c53 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -25,6 +25,9 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <errno.h>
 #include "dirname.h"
 
 #include "main.h"
@@ -45,6 +48,7 @@ struct Buffer
   size_t pt;         /* The point. */
   estr text;         /* The text. */
   size_t gap;        /* Size of gap after point. */
+  time_t mtime;      /* Time of last modification, to see if file changed outside zile */
 };
 
 #define FIELD(ty, field)                         \
@@ -355,6 +359,7 @@ set_buffer_names (Buffer * bp, const char *filename)
     filename = astr_cstr (astr_fmt ("%s/%s", astr_cstr (agetcwd ()), filename));
   set_buffer_filename (bp, filename);
 
+  update_buffer_mtime (bp);
   char *s = base_name (filename);
   char *name = xstrdup (s);
   /* Note: there can't be more than SIZE_MAX buffers. */
@@ -797,3 +802,18 @@ goto_offset (size_t o)
       thisflag |= FLAG_NEED_RESYNC;
     }
 }
+
+time_t
+get_buffer_mtime(Buffer *bp)
+{
+  return bp->mtime;  
+}
+
+void
+update_buffer_mtime(Buffer *bp)
+{ 
+  struct stat st;
+  const char *filename = get_buffer_filename(bp);
+  if (!(stat (filename, &st) == -1) && errno == ENOENT)
+    bp->mtime = st.st_mtime;
+}
diff --git a/src/extern.h b/src/extern.h
index 245798c..d82ce45 100644
--- a/src/extern.h
+++ b/src/extern.h
@@ -93,6 +93,8 @@ bool move_char (int dir);
 bool move_line (int n);
 size_t offset_to_line (Buffer *bp, size_t offset);
 void goto_offset (size_t o);
+time_t get_buffer_mtime (Buffer * bp);
+void update_buffer_mtime (Buffer * bp);
 
 /* completion.c ----------------------------------------------------------- */
 #define FIELD(ty, field)                                        \
diff --git a/src/file.c b/src/file.c
index db82d4b..da036bb 100644
--- a/src/file.c
+++ b/src/file.c
@@ -534,6 +534,7 @@ write_buffer (Buffer *bp, bool needname, bool confirm,
   bool ans = true;
   le * ok = leT;
   castr name;
+  struct stat st;
 
   if (!needname)
     name = astr_new_cstr (name0);
@@ -558,6 +559,9 @@ write_buffer (Buffer *bp, bool needname, bool confirm,
       if (ans != true)
         ok = leNIL;
     }
+  
+  if (!(stat (astr_cstr (name), &st) == -1 && errno == ENOENT) && get_buffer_mtime (bp) != st.st_mtime)
+    ans = minibuf_read_yn ("File %s has been changed outside zile; overwrite? (y or n) ", astr_cstr (name));
 
   if (ans == true)
     {
@@ -569,6 +573,7 @@ write_buffer (Buffer *bp, bool needname, bool confirm,
       set_buffer_nosave (bp, false);
       if (backup_and_write (bp, astr_cstr (name)))
         {
+          update_buffer_mtime(bp);
           minibuf_write ("Wrote %s", astr_cstr (name));
           set_buffer_modified (bp, false);
           undo_set_unchanged (get_buffer_last_undop (bp));
-- 
1.7.9.1

