From fa2a65d6c14862d071de5088caeb3e73ccfe11b3 Mon Sep 17 00:00:00 2001
From: Michael Ochmann <miko007@me.com>
Date: Fri, 10 Mar 2017 10:19:00 +0100
Subject: [PATCH] added save on exit, when buffer has changed

---
 src/Editor.cpp     |  4 ++++
 src/Editor.hpp     |  5 ++++-
 src/modes/Edit.cpp |  3 ++-
 src/modes/Exit.cpp | 34 ++++++++++++++++++++++++++++++++++
 src/modes/Exit.hpp | 18 ++++++++++++++++++
 5 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 src/modes/Exit.cpp
 create mode 100644 src/modes/Exit.hpp

diff --git a/src/Editor.cpp b/src/Editor.cpp
index fe9c50c..03864a7 100644
--- a/src/Editor.cpp
+++ b/src/Editor.cpp
@@ -14,6 +14,7 @@ namespace groove {
 		this->modes.emplace(Mode::EDIT, std::make_unique<modes::Edit>(*this));
 		this->modes.emplace(Mode::QUIT, std::make_unique<modes::Quit>(*this));
 		this->modes.emplace(Mode::SAVE, std::make_unique<modes::Save>(*this));
+		this->modes.emplace(Mode::EXIT, std::make_unique<modes::Exit>(*this));
 
 		this->lineMode = this->config.get<std::string>("linenumbers") == "relative" ? LineMode::RELATIVE :
 				this->config.get<std::string>("linenumbers") == "none" ? LineMode::NONE : LineMode::NUMBERS;
@@ -40,8 +41,11 @@ namespace groove {
 				std::getline(file, line);
 				this->buffer->insert(line);
 			}
+			this->buffer->changed = false;
+
 			return true;
 		}
+		this->buffer->changed = false;
 
 		return false;
 	}
diff --git a/src/Editor.hpp b/src/Editor.hpp
index 22fcb13..9460cef 100644
--- a/src/Editor.hpp
+++ b/src/Editor.hpp
@@ -10,6 +10,7 @@
 #include <modes/Save.hpp>
 #include <History.hpp>
 #include <config/ConfigParser.hpp>
+#include <modes/Exit.hpp>
 
 namespace groove {
 
@@ -17,7 +18,8 @@ namespace groove {
 		INSERT,
 		EDIT,
 		SAVE,
-		QUIT
+		QUIT,
+		EXIT
 	};
 
 	enum LineMode {
@@ -33,6 +35,7 @@ namespace groove {
 		friend class modes::Edit;
 		friend class modes::Quit;
 		friend class modes::Save;
+		friend class modes::Exit;
 		friend class History;
 	private:
 		long x, y;
diff --git a/src/modes/Edit.cpp b/src/modes/Edit.cpp
index e7b37ec..86047ee 100644
--- a/src/modes/Edit.cpp
+++ b/src/modes/Edit.cpp
@@ -10,7 +10,8 @@ namespace groove {
 				return;
 			switch (c) {
 				case 'q':
-					this->editor.mode_ = groove::Mode::QUIT;
+					this->editor.mode_ = groove::Mode::EXIT;
+					this->editor.input(' ');
 					break;
 				case 'i':
 					this->editor.mode_ = groove::Mode::INSERT;
diff --git a/src/modes/Exit.cpp b/src/modes/Exit.cpp
new file mode 100644
index 0000000..8bb3669
--- /dev/null
+++ b/src/modes/Exit.cpp
@@ -0,0 +1,34 @@
+#include <modes/Exit.hpp>
+#include <Editor.hpp>
+
+
+namespace groove {
+	namespace modes {
+
+		void Exit::input(int c) {
+			if (!this->editor.buffer->changed) {
+				this->editor.mode_ = groove::Mode::QUIT;
+
+				return;
+			}
+
+			this->status_ = "Save file '" + this->editor.filename + "'? [Y/N]:";
+			switch (c) {
+				case 27:
+					this->status_ = "";
+					this->editor.mode_ = groove::Mode::EDIT;
+					this->editor.input(' ');
+					break;
+				case 'n':
+					this->editor.mode_ = groove::Mode::QUIT;
+					break;
+				case 'y':
+					if (!this->editor.save())
+						std::cerr << "Could not write to file: '" << this->editor.filename << "'\n";
+					this->editor.mode_ = groove::Mode::QUIT;
+					break;
+			}
+		}
+
+	}
+}
\ No newline at end of file
diff --git a/src/modes/Exit.hpp b/src/modes/Exit.hpp
new file mode 100644
index 0000000..db17a15
--- /dev/null
+++ b/src/modes/Exit.hpp
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <modes/Mode.hpp>
+
+namespace groove {
+	namespace modes {
+
+		class Exit : public Mode {
+		public:
+			using Mode::Mode;
+			std::string status() {
+				return this->status_;
+			}
+			void input(int c);
+		};
+
+	}
+}
\ No newline at end of file