Buffer: replace Buffer::Type with Buffer::Flags

This commit is contained in:
Maxime Coste 2012-11-20 19:47:56 +01:00
parent 0b14f387d4
commit 8bbfbc8c72
7 changed files with 56 additions and 26 deletions

View File

@ -12,9 +12,9 @@
namespace Kakoune namespace Kakoune
{ {
Buffer::Buffer(String name, Type type, Buffer::Buffer(String name, Flags flags,
String initial_content) String initial_content)
: m_name(std::move(name)), m_type(type), : m_name(std::move(name)), m_flags(flags),
m_history(1), m_history_cursor(m_history.begin()), m_history(1), m_history_cursor(m_history.begin()),
m_last_save_undo_index(0), m_last_save_undo_index(0),
m_timestamp(0), m_timestamp(0),
@ -29,9 +29,9 @@ Buffer::Buffer(String name, Type type,
Editor editor_for_hooks(*this); Editor editor_for_hooks(*this);
Context context(editor_for_hooks); Context context(editor_for_hooks);
if (type == Type::NewFile) if (flags & Flags::File and flags & Flags::New)
m_hook_manager.run_hook("BufNew", m_name, context); m_hook_manager.run_hook("BufNew", m_name, context);
else if (type == Type::File) else
m_hook_manager.run_hook("BufOpen", m_name, context); m_hook_manager.run_hook("BufOpen", m_name, context);
m_hook_manager.run_hook("BufCreate", m_name, context); m_hook_manager.run_hook("BufCreate", m_name, context);

View File

@ -95,20 +95,22 @@ public:
class Buffer : public SafeCountable class Buffer : public SafeCountable
{ {
public: public:
enum class Type enum class Flags
{ {
File, None = 0,
NewFile, File = 1,
Scratch New = 2,
Fifo = 4
}; };
Buffer(String name, Type type, String initial_content = "\n"); Buffer(String name, Flags flags, String initial_content = "\n");
Buffer(const Buffer&) = delete; Buffer(const Buffer&) = delete;
Buffer(Buffer&&) = delete; Buffer(Buffer&&) = delete;
Buffer& operator= (const Buffer&) = delete; Buffer& operator= (const Buffer&) = delete;
~Buffer(); ~Buffer();
Type type() const { return m_type; } Flags flags() const { return m_flags; }
Flags& flags() { return m_flags; }
void insert(BufferIterator pos, String content); void insert(BufferIterator pos, String content);
void erase(BufferIterator begin, BufferIterator end); void erase(BufferIterator begin, BufferIterator end);
@ -207,7 +209,7 @@ private:
void do_erase(const BufferIterator& begin, const BufferIterator& end); void do_erase(const BufferIterator& begin, const BufferIterator& end);
String m_name; String m_name;
const Type m_type; Flags m_flags;
struct Modification; struct Modification;
typedef std::vector<Modification> UndoGroup; typedef std::vector<Modification> UndoGroup;
@ -232,6 +234,34 @@ private:
HookManager m_hook_manager; HookManager m_hook_manager;
}; };
constexpr Buffer::Flags operator|(Buffer::Flags lhs, Buffer::Flags rhs)
{
return (Buffer::Flags)((int) lhs | (int) rhs);
}
inline Buffer::Flags& operator|=(Buffer::Flags& lhs, Buffer::Flags rhs)
{
(int&) lhs |= (int) rhs;
return lhs;
}
constexpr bool operator&(Buffer::Flags lhs, Buffer::Flags rhs)
{
return ((int) lhs & (int) rhs) != 0;
}
inline Buffer::Flags& operator&=(Buffer::Flags& lhs, Buffer::Flags rhs)
{
(int&) lhs &= (int) rhs;
return lhs;
}
constexpr Buffer::Flags operator~(Buffer::Flags lhs)
{
return (Buffer::Flags)(~(int)lhs);
}
} }
#include "buffer_iterator.inl.hh" #include "buffer_iterator.inl.hh"

View File

@ -221,7 +221,7 @@ Buffer* open_or_create(const String& filename, Context& context)
if (not buffer) if (not buffer)
{ {
context.print_status("new file " + filename); context.print_status("new file " + filename);
buffer = new Buffer(filename, Buffer::Type::NewFile); buffer = new Buffer(filename, Buffer::Flags::File | Buffer::Flags::New);
} }
return buffer; return buffer;
} }
@ -231,7 +231,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
int fd = open(filename.c_str(), O_RDONLY); int fd = open(filename.c_str(), O_RDONLY);
if (fd < 0) if (fd < 0)
throw runtime_error("unable to open " + filename); throw runtime_error("unable to open " + filename);
Buffer* buffer = new Buffer(name, Buffer::Type::Scratch); Buffer* buffer = new Buffer(name, Buffer::Flags::Fifo);
buffer->hook_manager().add_hook( buffer->hook_manager().add_hook(
"BufClose", [=](const String&, const Context&) "BufClose", [=](const String&, const Context&)
@ -274,7 +274,7 @@ void edit(const CommandParameters& params, Context& context)
if (not buffer) if (not buffer)
{ {
if (parser.has_option("scratch")) if (parser.has_option("scratch"))
buffer = new Buffer(name, Buffer::Type::Scratch); buffer = new Buffer(name, Buffer::Flags::None);
else if (parser.has_option("fifo")) else if (parser.has_option("fifo"))
buffer = open_fifo(name, parser.option_value("fifo"), context); buffer = open_fifo(name, parser.option_value("fifo"), context);
else else
@ -311,8 +311,8 @@ void write_buffer(const CommandParameters& params, Context& context)
Buffer& buffer = context.buffer(); Buffer& buffer = context.buffer();
if (params.empty() and buffer.type() == Buffer::Type::Scratch) if (params.empty() and !(buffer.flags() & Buffer::Flags::File))
throw runtime_error("cannot write scratch buffer without a filename"); throw runtime_error("cannot write a non file buffer without a filename");
String filename = params.empty() ? buffer.name() String filename = params.empty() ? buffer.name()
: parse_filename(params[0]); : parse_filename(params[0]);
@ -328,7 +328,7 @@ void write_all_buffers(const CommandParameters& params, Context& context)
for (auto& buffer : BufferManager::instance()) for (auto& buffer : BufferManager::instance())
{ {
if (buffer->type() != Buffer::Type::Scratch and buffer->is_modified()) if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified())
{ {
write_buffer_to_file(*buffer, buffer->name()); write_buffer_to_file(*buffer, buffer->name());
buffer->notify_saved(); buffer->notify_saved();
@ -347,7 +347,7 @@ void quit(const CommandParameters& params, Context& context)
std::vector<String> names; std::vector<String> names;
for (auto& buffer : BufferManager::instance()) for (auto& buffer : BufferManager::instance())
{ {
if (buffer->type() != Buffer::Type::Scratch and buffer->is_modified()) if (buffer->flags() != Buffer::Flags::File and buffer->is_modified())
names.push_back(buffer->name()); names.push_back(buffer->name());
} }
if (not names.empty()) if (not names.empty())
@ -410,7 +410,7 @@ void delete_buffer(const CommandParameters& params, Context& context)
if (not buffer) if (not buffer)
throw runtime_error("buffer " + buffer_name + " does not exists"); throw runtime_error("buffer " + buffer_name + " does not exists");
} }
if (buffer->type()!= Buffer::Type::Scratch and buffer->is_modified()) if (buffer->flags() & Buffer::Flags::File and buffer->is_modified())
throw runtime_error("buffer " + buffer->name() + " is modified"); throw runtime_error("buffer " + buffer->name() + " is modified");
if (manager.count() == 1) if (manager.count() == 1)

View File

@ -13,7 +13,7 @@ static Buffer& get_or_create_debug_buffer()
Buffer* buffer = BufferManager::instance().get_buffer(debug_buffer_name); Buffer* buffer = BufferManager::instance().get_buffer(debug_buffer_name);
if (not buffer) if (not buffer)
buffer = new Buffer(debug_buffer_name, Buffer::Type::Scratch, ""); buffer = new Buffer(debug_buffer_name, Buffer::Flags::None, "");
assert(buffer); assert(buffer);
return *buffer; return *buffer;

View File

@ -85,7 +85,7 @@ Buffer* create_buffer_from_file(const String& filename)
if (Buffer* buffer = BufferManager::instance().get_buffer(filename)) if (Buffer* buffer = BufferManager::instance().get_buffer(filename))
delete buffer; delete buffer;
Buffer* buffer = new Buffer(filename, Buffer::Type::File, ""); Buffer* buffer = new Buffer(filename, Buffer::Flags::File, "");
String content; String content;
char buf[256]; char buf[256];

View File

@ -608,11 +608,11 @@ void create_local_client(const String& file)
if (not buffer) if (not buffer)
{ {
ui->print_status("new file " + file, -1); ui->print_status("new file " + file, -1);
buffer = new Buffer(file, Buffer::Type::NewFile); buffer = new Buffer(file, Buffer::Flags::New | Buffer::Flags::File);
} }
} }
else else
buffer = new Buffer("*scratch*", Buffer::Type::Scratch); buffer = new Buffer("*scratch*", Buffer::Flags::None);
ClientManager::instance().create_client( ClientManager::instance().create_client(
std::unique_ptr<UserInterface>{ui}, *buffer, 0); std::unique_ptr<UserInterface>{ui}, *buffer, 0);

View File

@ -7,7 +7,7 @@ using namespace Kakoune;
void test_buffer() void test_buffer()
{ {
Buffer buffer("test", Buffer::Type::Scratch, "allo ?\nmais que fais la police\n hein ?\n youpi\n"); Buffer buffer("test", Buffer::Flags::None, "allo ?\nmais que fais la police\n hein ?\n youpi\n");
assert(buffer.line_count() == 4); assert(buffer.line_count() == 4);
BufferIterator i = buffer.begin(); BufferIterator i = buffer.begin();
@ -40,7 +40,7 @@ void test_buffer()
void test_editor() void test_editor()
{ {
Buffer buffer("test", Buffer::Type::Scratch, "test\n\nyoupi\n"); Buffer buffer("test", Buffer::Flags::None, "test\n\nyoupi\n");
Editor editor(buffer); Editor editor(buffer);
using namespace std::placeholders; using namespace std::placeholders;
@ -56,7 +56,7 @@ void test_editor()
void test_incremental_inserter() void test_incremental_inserter()
{ {
Buffer buffer("test", Buffer::Type::Scratch, "test\n\nyoupi\nmatin\n"); Buffer buffer("test", Buffer::Flags::None, "test\n\nyoupi\nmatin\n");
Editor editor(buffer); Editor editor(buffer);
editor.select(buffer.begin()); editor.select(buffer.begin());