diff --git a/src/buffer.cc b/src/buffer.cc index 044a8f1e..cd014574 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -17,43 +17,47 @@ namespace Kakoune { -struct ParsedLines { BufferLines lines; bool bom, crlf; }; +struct ParsedLines +{ + BufferLines lines; + ByteOrderMark bom = ByteOrderMark::None; + EolFormat eolformat = EolFormat::Lf; +}; static ParsedLines parse_lines(StringView data) { - bool bom = false, crlf = false; + ParsedLines res; const char* pos = data.begin(); if (data.substr(0, 3_byte) == "\xEF\xBB\xBF") { - bom = true; + res.bom = ByteOrderMark::Utf8; pos = data.begin() + 3; } - BufferLines lines; while (pos < data.end()) { const char* line_end = pos; while (line_end < data.end() and *line_end != '\r' and *line_end != '\n') ++line_end; - lines.emplace_back(StringData::create({pos, line_end}, '\n')); + res.lines.emplace_back(StringData::create({pos, line_end}, '\n')); if (line_end+1 != data.end() and *line_end == '\r' and *(line_end+1) == '\n') { - crlf = true; + res.eolformat = EolFormat::Crlf; pos = line_end + 2; } else pos = line_end + 1; } - return { std::move(lines), bom, crlf }; + return res; } static void apply_options(OptionManager& options, const ParsedLines& parsed_lines) { - options.get_local_option("eolformat").set(parsed_lines.crlf ? "crlf" : "lf"); - options.get_local_option("BOM").set(parsed_lines.bom ? "utf-8" : "no"); + options.get_local_option("eolformat").set(parsed_lines.eolformat); + options.get_local_option("BOM").set(parsed_lines.bom); } Buffer::Buffer(String name, Flags flags, StringView data, diff --git a/src/buffer.hh b/src/buffer.hh index b91a7ed1..60820487 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -12,6 +12,34 @@ namespace Kakoune { +enum class EolFormat +{ + Lf, + Crlf +}; + +constexpr Array, 2> enum_desc(EolFormat) +{ + return { { + { EolFormat::Lf, "lf" }, + { EolFormat::Crlf, "crlf" }, + } }; +} + +enum class ByteOrderMark +{ + None, + Utf8 +}; + +constexpr Array, 2> enum_desc(ByteOrderMark) +{ + return { { + { ByteOrderMark::None, "none" }, + { ByteOrderMark::Utf8, "utf8" }, + } }; +} + class Buffer; constexpr timespec InvalidTime = { -1, -1 }; diff --git a/src/file.cc b/src/file.cc index 73f50ca7..bc9c1cf9 100644 --- a/src/file.cc +++ b/src/file.cc @@ -215,14 +215,14 @@ void write(int fd, StringView data) void write_buffer_to_fd(Buffer& buffer, int fd) { - const String& eolformat = buffer.options()["eolformat"].get(); + auto eolformat = buffer.options()["eolformat"].get(); StringView eoldata; - if (eolformat == "crlf") + if (eolformat == EolFormat::Crlf) eoldata = "\r\n"; else eoldata = "\n"; - if (buffer.options()["BOM"].get() == "utf-8") + if (buffer.options()["BOM"].get() == ByteOrderMark::Utf8) ::write(fd, "\xEF\xBB\xBF", 3); for (LineCount i = 0; i < buffer.line_count(); ++i) diff --git a/src/main.cc b/src/main.cc index 91870c12..340f7cbd 100644 --- a/src/main.cc +++ b/src/main.cc @@ -188,9 +188,9 @@ void register_options() reg.declare_option("scrolloff", "number of lines and columns to keep visible main cursor when scrolling", CharCoord{0,0}); - reg.declare_option("eolformat", "end of line format: 'crlf' or 'lf'", "lf"_str); - reg.declare_option("BOM", "insert a byte order mark when writing buffer", - "no"_str); + reg.declare_option("eolformat", "end of line format: crlf or lf", EolFormat::Lf); + reg.declare_option("BOM", "insert a byte order mark when writing buffer (none or utf8)", + ByteOrderMark::None); reg.declare_option("complete_prefix", "complete up to common prefix in tab completion", true);