Add all basic ed functions

- except global and history ones
This commit is contained in:
2026-08-31 08:14:12 +01:00
parent 010f916d35
commit a996beb4aa
15 changed files with 732 additions and 362 deletions
+3 -2
View File
@@ -23,14 +23,15 @@ struct BEd {
internal::vase::AppendStorage append{"/tmp"};
bool help_mode = false;
std::string last_help = "";
bool prompt_mode = true;
std::function<std::string(BEd &)> prompt = nullptr;
bool suppress_mode = false;
bool temporary_current = false;
std::string last_help = "";
std::string last_regex = "";
std::string last_symbol = "";
std::string last_replacement = "";
std::string last_shell = "";
std::unordered_map<std::string, internal::buffer::Buffer *> buffers;
@@ -44,7 +45,7 @@ struct BEd {
internal::buffer::Buffer &buffer(const std::string &);
internal::buffer::Line &current();
internal::buffer::Range &prev();
void mark(char, internal::buffer::Line);
void mark(uint8_t, internal::buffer::Line);
void handle(std::string_view cmd, bool eof);
void run();
+3 -3
View File
@@ -13,10 +13,9 @@ struct ClipBuffer : Buffer {
bool waste() override;
uint64_t lines() override;
uint64_t bytes() override;
void load(BEd &ctx) override;
void load(BEd &ctx, vase::Shard *text) override;
void load(BEd &ctx, const char *cmd) override;
void load(BEd &ctx, std::filesystem::path path) override;
void set_filename(std::filesystem::path path) override;
std::filesystem::path filename() override;
vase::Shard *copy(uint64_t start_line, uint64_t end_line) override;
void substitute(
BEd &ctx, uint64_t start_line, uint64_t end_line,
@@ -27,6 +26,7 @@ struct ClipBuffer : Buffer {
void append(BEd &ctx, vase::Shard *text, uint64_t line) override;
void print(BEd &ctx, uint64_t start_line, uint64_t end_line) override;
void number_print(BEd &ctx, uint64_t start_line, uint64_t end_line) override;
void list_print(BEd &ctx, uint64_t start_line, uint64_t end_line) override;
uint64_t next_closing(uint64_t start) override;
uint64_t prev_closing(uint64_t start) override;
uint64_t find_next(std::string_view pattern, uint64_t start) override;
+61 -3
View File
@@ -32,10 +32,9 @@ struct Buffer {
virtual bool waste() = 0;
virtual uint64_t lines() = 0;
virtual uint64_t bytes() = 0;
virtual void set_filename(std::filesystem::path path) = 0;
virtual std::filesystem::path filename() = 0;
virtual void load(BEd &ctx, vase::Shard *text) = 0;
virtual void load(BEd &ctx, const char *cmd) = 0;
virtual void load(BEd &ctx, std::filesystem::path path) = 0;
virtual void load(BEd &ctx) = 0;
virtual vase::Shard *copy(uint64_t start_line, uint64_t end_line) = 0;
virtual void substitute(
BEd &ctx, uint64_t start_line, uint64_t end_line,
@@ -46,6 +45,7 @@ struct Buffer {
virtual void append(BEd &ctx, vase::Shard *text, uint64_t line) = 0;
virtual void print(BEd &ctx, uint64_t start_line, uint64_t end_line) = 0;
virtual void number_print(BEd &ctx, uint64_t start_line, uint64_t end_line) = 0;
virtual void list_print(BEd &ctx, uint64_t start_line, uint64_t end_line) = 0;
virtual uint64_t find_next(std::string_view pattern, uint64_t start) = 0;
virtual uint64_t find_prev(std::string_view pattern, uint64_t start) = 0;
virtual uint64_t next_closing(uint64_t start) = 0;
@@ -85,4 +85,62 @@ using Address = std::variant<
std::string,
buffer::Range,
buffer::Line>;
inline static std::string list_string(std::string_view s) {
uint32_t width = 80;
winsize ws{};
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col != 0)
width = ws.ws_col;
std::string out;
out.reserve(s.size());
const uint32_t max_width = width > 1 ? width - 1 : 1;
uint32_t column = 0;
auto append = [&](std::string_view text) {
if (column + text.size() > max_width) {
out += "\\\n";
column = 0;
}
out += text;
column += text.size();
};
for (unsigned char c : s) {
switch (c) {
case '\\':
append("\\\\");
break;
case '$':
append("\\$");
break;
case '\a':
append("\\a");
break;
case '\b':
append("\\b");
break;
case '\f':
append("\\f");
break;
case '\r':
append("\\r");
break;
case '\t':
append("\\t");
break;
case '\v':
append("\\v");
break;
default:
if (!std::isprint(c)) {
char buf[5];
std::snprintf(buf, sizeof(buf), "\\%03o", c);
append(buf);
} else {
append(std::string_view((const char *)&c, 1));
}
break;
}
}
out += '$';
return out;
}
} // namespace bed::internal::buffer
+3 -3
View File
@@ -17,10 +17,9 @@ struct GenericBuffer : Buffer {
bool waste() override;
uint64_t lines() override;
uint64_t bytes() override;
void load(BEd &ctx) override;
void load(BEd &ctx, vase::Shard *text) override;
void load(BEd &ctx, const char *cmd) override;
void load(BEd &ctx, std::filesystem::path path) override;
void set_filename(std::filesystem::path path) override;
std::filesystem::path filename() override;
vase::Shard *copy(uint64_t start_line, uint64_t end_line) override;
void substitute(
BEd &ctx, uint64_t start_line, uint64_t end_line,
@@ -31,6 +30,7 @@ struct GenericBuffer : Buffer {
void append(BEd &ctx, vase::Shard *text, uint64_t line) override;
void print(BEd &ctx, uint64_t start_line, uint64_t end_line) override;
void number_print(BEd &ctx, uint64_t start_line, uint64_t end_line) override;
void list_print(BEd &ctx, uint64_t start_line, uint64_t end_line) override;
uint64_t next_closing(uint64_t start) override;
uint64_t prev_closing(uint64_t start) override;
uint64_t find_next(std::string_view pattern, uint64_t start) override;
+1
View File
@@ -74,6 +74,7 @@ struct IO {
KeyEvent read_key();
void write(const char *, uint64_t);
void write(std::string_view);
void run_pty(const std::string &);
std::deque<char> input_queue;
+2
View File
@@ -50,6 +50,8 @@ Shard *insert(AppendStorage *ap, Shard *root, Shard *text, uint64_t line);
Shard *erase(Shard *root, uint64_t start, uint64_t end);
Shard *join(Shard *root, uint64_t start, uint64_t end);
Shard *copy(Shard *root, uint64_t start, uint64_t end);
void write_file(std::filesystem::path path, Shard *text);
void write_command(const char *cmd, Shard *text);
uint64_t find_next(Shard *root, std::string_view pattern, uint64_t start);
uint64_t find_prev(Shard *root, std::string_view pattern, uint64_t start);
+1
View File
@@ -35,6 +35,7 @@ extern "C" {
#include <mutex>
#include <optional>
#include <poll.h>
#include <pty.h>
#include <queue>
#include <set>
#include <shared_mutex>