diff --git a/src/buffer.cc b/src/buffer.cc index 81ba07e6..1f9503b0 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -316,14 +316,12 @@ void Buffer::append_modification(Modification&& modification) m_current_undo_group.push_back(modification); } -struct window_already_registered {}; - -void Buffer::register_window(Window* window) +Window* Buffer::get_or_create_window() { - if (std::find(m_windows.begin(), m_windows.end(), window) != m_windows.end()) - throw window_already_registered(); + if (m_windows.empty()) + m_windows.push_front(std::unique_ptr(new Window(*this))); - m_windows.push_front(std::unique_ptr(window)); + return m_windows.front().get(); } void Buffer::delete_window(Window* window) diff --git a/src/buffer.hh b/src/buffer.hh index 28a87fc4..67b374e1 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -100,7 +100,7 @@ public: const BufferString& content() const { return m_content; } - void register_window(Window* window); + Window* get_or_create_window(); void delete_window(Window* window); private: diff --git a/src/main.cc b/src/main.cc index b39b8be4..b76effcf 100644 --- a/src/main.cc +++ b/src/main.cc @@ -4,6 +4,7 @@ #include "file.hh" #include "regex_selector.hh" #include "command_manager.hh" +#include "buffer_manager.hh" #include #include @@ -160,17 +161,17 @@ void edit(const CommandParameters& params) throw wrong_argument_count(); std::string filename = params[0]; + Buffer* buffer = NULL; try { - Buffer* buffer = create_buffer_from_file(filename); - if (buffer) - current_window = new Window(*buffer); + buffer = create_buffer_from_file(filename); } catch (file_not_found& what) { print_status("new file " + filename); - current_window = new Window(*new Buffer(filename)); + buffer = new Buffer(filename); } + current_window = buffer->get_or_create_window(); } void write_buffer(const CommandParameters& params) @@ -293,7 +294,7 @@ int main() try { auto buffer = new Buffer(""); - current_window = new Window(*buffer); + current_window = buffer->get_or_create_window(); draw_window(*current_window); int count = 0; diff --git a/src/window.cc b/src/window.cc index 2694aef8..926258b9 100644 --- a/src/window.cc +++ b/src/window.cc @@ -11,7 +11,6 @@ Window::Window(Buffer& buffer) m_cursor(0, 0), m_dimensions(0, 0) { - m_buffer.register_window(this); } void Window::erase() diff --git a/src/window.hh b/src/window.hh index 92c28301..4c8a3243 100644 --- a/src/window.hh +++ b/src/window.hh @@ -34,9 +34,6 @@ public: typedef BufferString String; typedef std::function Selector; - Window(Buffer& buffer); - Window(const Window&) = delete; - void erase(); void insert(const String& string); void append(const String& string); @@ -69,6 +66,11 @@ public: bool redo(); private: + friend class Buffer; + + Window(Buffer& buffer); + Window(const Window&) = delete; + void scroll_to_keep_cursor_visible_ifn(); Buffer& m_buffer;