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
{
Buffer::Buffer(String name, Type type,
Buffer::Buffer(String name, Flags flags,
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_last_save_undo_index(0),
m_timestamp(0),
@ -29,9 +29,9 @@ Buffer::Buffer(String name, Type type,
Editor editor_for_hooks(*this);
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);
else if (type == Type::File)
else
m_hook_manager.run_hook("BufOpen", m_name, context);
m_hook_manager.run_hook("BufCreate", m_name, context);

View File

@ -95,20 +95,22 @@ public:
class Buffer : public SafeCountable
{
public:
enum class Type
enum class Flags
{
File,
NewFile,
Scratch
None = 0,
File = 1,
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(Buffer&&) = delete;
Buffer& operator= (const Buffer&) = delete;
~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 erase(BufferIterator begin, BufferIterator end);
@ -207,7 +209,7 @@ private:
void do_erase(const BufferIterator& begin, const BufferIterator& end);
String m_name;
const Type m_type;
Flags m_flags;
struct Modification;
typedef std::vector<Modification> UndoGroup;
@ -232,6 +234,34 @@ private:
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"

View File

@ -221,7 +221,7 @@ Buffer* open_or_create(const String& filename, Context& context)
if (not buffer)
{
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;
}
@ -231,7 +231,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
int fd = open(filename.c_str(), O_RDONLY);
if (fd < 0)
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(
"BufClose", [=](const String&, const Context&)
@ -274,7 +274,7 @@ void edit(const CommandParameters& params, Context& context)
if (not buffer)
{
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"))
buffer = open_fifo(name, parser.option_value("fifo"), context);
else
@ -311,8 +311,8 @@ void write_buffer(const CommandParameters& params, Context& context)
Buffer& buffer = context.buffer();
if (params.empty() and buffer.type() == Buffer::Type::Scratch)
throw runtime_error("cannot write scratch buffer without a filename");
if (params.empty() and !(buffer.flags() & Buffer::Flags::File))
throw runtime_error("cannot write a non file buffer without a filename");
String filename = params.empty() ? buffer.name()
: parse_filename(params[0]);
@ -328,7 +328,7 @@ void write_all_buffers(const CommandParameters& params, Context& context)
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());
buffer->notify_saved();
@ -347,7 +347,7 @@ void quit(const CommandParameters& params, Context& context)
std::vector<String> names;
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());
}
if (not names.empty())
@ -410,7 +410,7 @@ void delete_buffer(const CommandParameters& params, Context& context)
if (not buffer)
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");
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);
if (not buffer)
buffer = new Buffer(debug_buffer_name, Buffer::Type::Scratch, "");
buffer = new Buffer(debug_buffer_name, Buffer::Flags::None, "");
assert(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))
delete buffer;
Buffer* buffer = new Buffer(filename, Buffer::Type::File, "");
Buffer* buffer = new Buffer(filename, Buffer::Flags::File, "");
String content;
char buf[256];

View File

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

View File

@ -7,7 +7,7 @@ using namespace Kakoune;
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);
BufferIterator i = buffer.begin();
@ -40,7 +40,7 @@ void test_buffer()
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);
using namespace std::placeholders;
@ -56,7 +56,7 @@ void test_editor()
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.select(buffer.begin());