Add all basic ed functions
- except global and history ones
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user