summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2011-09-08 14:30:36 +0000
committerMaxime Coste <frrrwww@gmail.com>2011-09-08 14:30:36 +0000
commit76b7c60afbe93f619d944293e2c0997473dde2af (patch)
treea0e58a9f499f9e07342b0e1641cc1b90ded92895 /src
parent60e673acba9f5e06d5a7520465b446b701e78e99 (diff)
Buffer is now responsible for window creation
Diffstat (limited to 'src')
-rw-r--r--src/buffer.cc10
-rw-r--r--src/buffer.hh2
-rw-r--r--src/main.cc11
-rw-r--r--src/window.cc1
-rw-r--r--src/window.hh8
5 files changed, 16 insertions, 16 deletions
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<Window>(new Window(*this)));
- m_windows.push_front(std::unique_ptr<Window>(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 <unordered_map>
#include <cassert>
@@ -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("<scratch>");
- 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<Selection (const BufferIterator&)> 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;