diff --git a/include/internal/vase/shard.h b/include/internal/vase/shard.h index 9b4ec65..7c297eb 100644 --- a/include/internal/vase/shard.h +++ b/include/internal/vase/shard.h @@ -24,9 +24,9 @@ struct Shard { static void retain(Shard *n); static void release(Shard *n); - static Shard *from_file(const std::filesystem::path &path, bool posix_ending); - static Shard *from_string(const char *data, uint64_t len, bool posix_ending); - static Shard *from_command(const char *cmd, bool posix_ending); + static Shard *from_file(const std::filesystem::path &path); + static Shard *from_string(const char *data, uint64_t len); + static Shard *from_command(const char *cmd); static std::pair split(Shard *n, uint64_t offset); static Shard *concat(Shard *a, Shard *b); diff --git a/src/internal/buffer/special/clip.cc b/src/internal/buffer/special/clip.cc index e52ed68..670be23 100644 --- a/src/internal/buffer/special/clip.cc +++ b/src/internal/buffer/special/clip.cc @@ -11,14 +11,14 @@ bool ClipBuffer::waste() { void ClipBuffer::saved_hook() {} uint64_t ClipBuffer::lines() { - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); uint64_t lines = s ? s->lines + 1 : 0; vase::Shard::release(s); return lines; } uint64_t ClipBuffer::bytes() { - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); uint64_t length = s ? s->length + 1 : 0; vase::Shard::release(s); return length; @@ -57,7 +57,7 @@ void ClipBuffer::append(BEd &ctx, vase::Shard *text, uint64_t line) { ctx.prev().buffername = name; ctx.prev().start = line + 1; ctx.prev().end = line + (text ? text->lines + 1 : 0); - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); s = vase::insert(&ctx.append, s, text, line); clip_write(s); vase::Shard::release(s); @@ -65,7 +65,7 @@ void ClipBuffer::append(BEd &ctx, vase::Shard *text, uint64_t line) { } void ClipBuffer::remove(BEd &ctx, uint64_t start_line, uint64_t end_line) { - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); s = vase::erase(s, start_line, end_line); clip_write(s); ctx.prev().buffername = name; @@ -85,7 +85,7 @@ void ClipBuffer::replace(BEd &ctx, vase::Shard *text, uint64_t start_line, uint6 ctx.prev().end = start_line + text->lines; uint64_t new_count = text->lines + 1; uint64_t old_count = end_line - start_line + 1; - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); s = vase::replace(s, text, start_line, end_line); clip_write(s); vase::Shard::release(s); @@ -99,7 +99,7 @@ void ClipBuffer::replace(BEd &ctx, vase::Shard *text, uint64_t start_line, uint6 } void ClipBuffer::join(BEd &ctx, uint64_t start_line, uint64_t end_line) { - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); s = vase::join(s, start_line, end_line); clip_write(s); vase::Shard::release(s); @@ -116,7 +116,7 @@ void ClipBuffer::substitute( ctx.prev().buffername = name; ctx.prev().start = start_line; ctx.prev().end = end_line; - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); s = vase::substitute( &ctx.append, s, @@ -138,21 +138,21 @@ void ClipBuffer::substitute( } vase::Shard *ClipBuffer::copy(uint64_t start_line, uint64_t end_line) { - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); vase::Shard *o = vase::copy(s, start_line, end_line); vase::Shard::release(s); return o; } uint64_t ClipBuffer::find_next(std::string_view pattern, uint64_t start) { - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); uint64_t line = vase::find_next(s, pattern, start); vase::Shard::release(s); return line; } uint64_t ClipBuffer::find_prev(std::string_view pattern, uint64_t start) { - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); uint64_t line = vase::find_prev(s, pattern, start); vase::Shard::release(s); return line; @@ -176,7 +176,7 @@ void ClipBuffer::print(BEd &ctx, uint64_t start_line, uint64_t end_line) { ctx.prev().buffername = name; ctx.prev().start = start_line; ctx.prev().end = end_line; - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); vase::Iterator it(s, start_line - 1, Direction::Forward); while (it.next() && start_line++ <= end_line) ctx.io.write_line(it.line); @@ -190,7 +190,7 @@ void ClipBuffer::number_print(BEd &ctx, uint64_t start_line, uint64_t end_line) uint8_t width = 1; for (uint64_t n = end_line; n >= 10; n /= 10) ++width; - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); vase::Iterator it(s, start_line - 1, Direction::Forward); while (it.next() && start_line <= end_line) ctx.io.write_line(std::format("{:>{}}\t{}", start_line++, width, it.line)); @@ -201,7 +201,7 @@ void ClipBuffer::list_print(BEd &ctx, uint64_t start_line, uint64_t end_line) { ctx.prev().buffername = name; ctx.prev().start = start_line; ctx.prev().end = end_line; - auto s = vase::Shard::from_command("xclip -selection clipboard -o", true); + auto s = vase::Shard::from_command("xclip -selection clipboard -o"); vase::Iterator it(s, start_line - 1, Direction::Forward); while (it.next() && start_line++ <= end_line) ctx.io.write_line(list_string(it.line)); diff --git a/src/internal/functions/extended.cc b/src/internal/functions/extended.cc index 4dab381..36bdfdd 100644 --- a/src/internal/functions/extended.cc +++ b/src/internal/functions/extended.cc @@ -197,7 +197,7 @@ void Function::register_extented(BEd &ctx) { ctx.io.write("=> ", 3); auto parser = syntax::MiniParser( *ctx.languages["ruby"], - vase::Shard::from_string(line.data(), line.size(), true), + vase::Shard::from_string(line.data(), line.size()), nullptr ); const auto &tokens = parser.lines[0].second; diff --git a/src/internal/functions/posix.cc b/src/internal/functions/posix.cc index 6fe964c..90d0648 100644 --- a/src/internal/functions/posix.cc +++ b/src/internal/functions/posix.cc @@ -144,17 +144,17 @@ void Function::register_posix(BEd &ctx) { vase::Shard *s = nullptr; if (std::holds_alternative(arg)) { auto path = std::get(arg); - s = vase::Shard::from_file(path, true); + s = vase::Shard::from_file(path); buf.set_filename(path); } else if (std::holds_alternative(arg)) { auto cmd = std::get(arg).cmd; ctx.escape_command(cmd, buf.filename().string()); - s = vase::Shard::from_command(cmd.c_str(), true); + s = vase::Shard::from_command(cmd.c_str()); } else { auto path = buf.filename(); if (path.empty()) throw ed_error("Need filename."); - s = vase::Shard::from_file(path, true); + s = vase::Shard::from_file(path); }; try { buf.load(ctx, s); @@ -190,17 +190,17 @@ void Function::register_posix(BEd &ctx) { vase::Shard *s = nullptr; if (std::holds_alternative(arg)) { auto path = std::get(arg); - s = vase::Shard::from_file(path, true); + s = vase::Shard::from_file(path); buf.set_filename(path); } else if (std::holds_alternative(arg)) { auto cmd = std::get(arg).cmd; ctx.escape_command(cmd, buf.filename().string()); - s = vase::Shard::from_command(cmd.c_str(), true); + s = vase::Shard::from_command(cmd.c_str()); } else { auto path = buf.filename(); if (path.empty()) throw ed_error("Need filename."); - s = vase::Shard::from_file(path, true); + s = vase::Shard::from_file(path); }; try { buf.load(ctx, s); @@ -588,18 +588,18 @@ void Function::register_posix(BEd &ctx) { vase::Shard *s = nullptr; if (std::holds_alternative(arg)) { auto path = std::get(arg); - s = vase::Shard::from_file(path, true); + s = vase::Shard::from_file(path); if (buf.filename().empty()) buf.set_filename(path); } else if (std::holds_alternative(arg)) { auto cmd = std::get(arg).cmd; ctx.escape_command(cmd, buf.filename().string()); - s = vase::Shard::from_command(cmd.c_str(), true); + s = vase::Shard::from_command(cmd.c_str()); } else { auto path = buf.filename(); if (path.empty()) throw ed_error("Need filename."); - s = vase::Shard::from_file(path, true); + s = vase::Shard::from_file(path); }; try { buf.append(ctx, s, addr.number); diff --git a/src/internal/vase/shard.cc b/src/internal/vase/shard.cc index 25a1357..5aa2224 100644 --- a/src/internal/vase/shard.cc +++ b/src/internal/vase/shard.cc @@ -202,7 +202,7 @@ Shard *Shard::build(Shard **pieces, uint64_t lo, uint64_t hi) { return node; } -Shard *Shard::from_command(const char *cmd, bool posix_ending) { +Shard *Shard::from_command(const char *cmd) { auto o = new OriginalStorage("/tmp"); int dest_fd = o->fd; if (dest_fd == -1) { @@ -278,23 +278,21 @@ Shard *Shard::from_command(const char *cmd, bool posix_ending) { io::IO::enable_raw(); return nullptr; } - if (posix_ending) { - if (ending[1] == '\n') { - Petal *last = (Petal *)pieces.back(); - last->lines--; - last->length--; - if (last->length == 0) { - Shard::release(last); - pieces.pop_back(); - last = nullptr; - if (!pieces.empty()) - last = (Petal *)pieces.back(); - else - return nullptr; - } - if (last && ending[0] == '\r') - last->length--; + if (ending[1] == '\n') { + Petal *last = (Petal *)pieces.back(); + last->lines--; + last->length--; + if (last->length == 0) { + Shard::release(last); + pieces.pop_back(); + last = nullptr; + if (!pieces.empty()) + last = (Petal *)pieces.back(); + else + return nullptr; } + if (last && ending[0] == '\r') + last->length--; } o->initialize(); io::IO::enable_raw(); @@ -303,7 +301,7 @@ Shard *Shard::from_command(const char *cmd, bool posix_ending) { return build(pieces.data(), 0, pieces.size()); } -Shard *Shard::from_file(const std::filesystem::path &path, bool posix_ending) { +Shard *Shard::from_file(const std::filesystem::path &path) { auto o = new OriginalStorage("/tmp"); int dest_fd = o->fd; if (dest_fd == -1) { @@ -313,10 +311,10 @@ Shard *Shard::from_file(const std::filesystem::path &path, bool posix_ending) { int src_fd = open(path.c_str(), O_RDONLY); if (src_fd == -1) { delete o; - return nullptr; + throw ed_error("Couldn't open file."); } uint64_t total = std::filesystem::file_size(path); - if (posix_ending && total > 0) { + if (total > 0) { char last; if (pread(src_fd, &last, 1, (off_t)(total - 1)) != 1) { delete o; @@ -381,7 +379,7 @@ Shard *Shard::from_file(const std::filesystem::path &path, bool posix_ending) { return build(pieces.data(), 0, pieces.size()); } -Shard *Shard::from_string(const char *data, uint64_t len, bool posix_ending) { +Shard *Shard::from_string(const char *data, uint64_t len) { auto o = new OriginalStorage("/tmp"); int dest_fd = o->fd; if (dest_fd == -1 || data == nullptr) { @@ -389,7 +387,7 @@ Shard *Shard::from_string(const char *data, uint64_t len, bool posix_ending) { return nullptr; } uint64_t total = len; - if (posix_ending && total > 0) { + if (total > 0) { if (data[total - 1] == '\n') { total--; if (total > 0 && data[total - 1] == '\r')