Setup ed the posix line editor.
This commit is contained in:
@@ -4,6 +4,84 @@
|
||||
#include "pch.h"
|
||||
|
||||
namespace crib::commands::ed {
|
||||
struct ed_error : std::runtime_error {
|
||||
ed_error(const char *msg) : std::runtime_error(msg) {}
|
||||
};
|
||||
|
||||
struct Command {
|
||||
enum struct Type : uint8_t {
|
||||
Invalid,
|
||||
Quit,
|
||||
ForceQuit,
|
||||
PromptToggle,
|
||||
HelpToggle,
|
||||
Help,
|
||||
Print,
|
||||
Number,
|
||||
Append,
|
||||
Change,
|
||||
Delete,
|
||||
Write,
|
||||
Dump,
|
||||
None
|
||||
} type;
|
||||
|
||||
struct Address {
|
||||
enum struct Type : uint8_t {
|
||||
None,
|
||||
Current,
|
||||
Last,
|
||||
Number,
|
||||
Mark,
|
||||
SearchForward,
|
||||
SearchBackward
|
||||
} type;
|
||||
int64_t number = 0;
|
||||
std::string regex = "";
|
||||
int64_t offset = 0;
|
||||
char mark = '\0';
|
||||
};
|
||||
|
||||
Address start{};
|
||||
Address end{};
|
||||
static constexpr uint8_t SEMICOLON = 0b01;
|
||||
static constexpr uint8_t RANGE = 0b10;
|
||||
uint8_t address_flags = 0;
|
||||
char suffix = '\0';
|
||||
std::string argument;
|
||||
};
|
||||
|
||||
struct Ed {
|
||||
crib::internal::vase::Vase vase;
|
||||
uint64_t line = 0;
|
||||
bool modified = false;
|
||||
bool quitting = false;
|
||||
uint64_t marks[26]{0};
|
||||
std::string last_regex = "";
|
||||
bool help_mode = false;
|
||||
bool suppress_mode = false;
|
||||
bool prompt_mode = false;
|
||||
std::string prompt = "*";
|
||||
std::string last_message = "";
|
||||
|
||||
Ed(std::filesystem::path file, bool suppress_mode, std::string prompt_)
|
||||
: vase(file, "/tmp"), suppress_mode(suppress_mode), prompt(prompt_) {
|
||||
if (prompt == "")
|
||||
prompt = "*";
|
||||
else
|
||||
prompt_mode = true;
|
||||
line = vase.lines();
|
||||
std::cout << vase.length() << std::endl;
|
||||
}
|
||||
|
||||
void parse_address(std::string_view cmd, uint64_t &i, Command::Address &addr);
|
||||
void resolve_address(Command::Address addr, uint64_t *out_line);
|
||||
Command parse(std::string cmd, bool eof);
|
||||
void append(std::string text, uint64_t line);
|
||||
void remove(uint64_t start_line, uint64_t end_line);
|
||||
bool handle(std::string cmd, bool eof);
|
||||
};
|
||||
|
||||
std::string summary();
|
||||
void help();
|
||||
void run(std::vector<std::string>);
|
||||
|
||||
@@ -24,8 +24,8 @@ struct Iterator {
|
||||
Shard *root;
|
||||
std::string line;
|
||||
|
||||
Iterator(Shard *root, uint64_t line_num)
|
||||
: root(root), it(root, line_num, Direction::Forward) {
|
||||
Iterator(Shard *root, uint64_t line_num, Direction dir)
|
||||
: root(root), it(root, line_num, dir) {
|
||||
Shard::retain(root);
|
||||
}
|
||||
|
||||
@@ -63,9 +63,8 @@ struct Iterator {
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string &next() {
|
||||
it.next(&line);
|
||||
return line;
|
||||
bool next() {
|
||||
return it.next(&line);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -25,7 +25,7 @@ struct Shard {
|
||||
static void retain(Shard *n);
|
||||
static void release(Shard *n);
|
||||
|
||||
static Shard *from_file(std::filesystem::path path, OriginalBuffer *b);
|
||||
static Shard *from_file(std::filesystem::path path, OriginalBuffer *b, bool posix_ending);
|
||||
static std::vector<Shard *> from_swap(std::filesystem::path path, OriginalBuffer *b);
|
||||
|
||||
static std::pair<Shard *, Shard *> split(Shard *n, uint64_t offset);
|
||||
@@ -34,6 +34,7 @@ struct Shard {
|
||||
static Shard *merge_leaves(Shard *a, Shard *b);
|
||||
static Shard *append(Shard *root, Shard *leaf);
|
||||
static Shard *build(Shard **pieces, uint64_t lo, uint64_t hi);
|
||||
static void dump(Shard *node, int depth = 0);
|
||||
};
|
||||
|
||||
struct Branch : Shard {
|
||||
@@ -62,54 +63,4 @@ struct Petal : Shard {
|
||||
source(source), pos(pos) {};
|
||||
};
|
||||
|
||||
extern inline void dump_shard(Shard *node, int depth = 0) {
|
||||
if (!node) {
|
||||
std::cout << std::string(depth * 2, ' ') << "<null>\n";
|
||||
return;
|
||||
}
|
||||
|
||||
std::string indent(depth * 2, ' ');
|
||||
|
||||
std::cout << indent
|
||||
<< "Shard@" << node
|
||||
<< " kind=";
|
||||
|
||||
switch (node->kind) {
|
||||
case Shard::Kind::Branch:
|
||||
std::cout << "Branch";
|
||||
break;
|
||||
case Shard::Kind::Petal:
|
||||
std::cout << "Petal";
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout
|
||||
<< " height=" << unsigned(node->height)
|
||||
<< " length=" << node->length
|
||||
<< " lines=" << node->lines
|
||||
<< " refs=" << node->refs.load()
|
||||
<< "\n";
|
||||
|
||||
if (node->kind == Shard::Kind::Branch) {
|
||||
auto *branch = static_cast<Branch *>(node);
|
||||
|
||||
std::cout << indent << " left:\n";
|
||||
dump_shard(branch->left, depth + 2);
|
||||
|
||||
std::cout << indent << " right:\n";
|
||||
dump_shard(branch->right, depth + 2);
|
||||
} else {
|
||||
auto *petal = static_cast<Petal *>(node);
|
||||
|
||||
std::cout
|
||||
<< indent << " source=" << petal->source
|
||||
<< " pos=" << petal->pos
|
||||
<< " length=" << petal->length
|
||||
<< " lines=" << petal->lines
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
if (!depth)
|
||||
std::cout << "\n\n";
|
||||
}
|
||||
} // namespace crib::internal::vase
|
||||
|
||||
@@ -43,6 +43,15 @@ struct Vase {
|
||||
OriginalBuffer *original;
|
||||
AppendBuffer *append;
|
||||
Shard *root;
|
||||
|
||||
#ifdef _WIN32
|
||||
bool posix_ending = false;
|
||||
bool using_crlf = true;
|
||||
#else
|
||||
bool posix_ending = true;
|
||||
bool using_crlf = false;
|
||||
#endif
|
||||
|
||||
std::filesystem::path path;
|
||||
std::filesystem::path swapdir;
|
||||
|
||||
@@ -50,15 +59,18 @@ struct Vase {
|
||||
~Vase();
|
||||
|
||||
uint64_t length();
|
||||
uint64_t lines();
|
||||
std::string to_string();
|
||||
std::string to_string(Range range);
|
||||
|
||||
Iterator iterate(uint64_t line);
|
||||
Iterator iterate(uint64_t line, Direction dir);
|
||||
|
||||
void insert(Point *point, char key);
|
||||
void insert(Point *point, std::string_view str);
|
||||
void insert(Point *point, const char *data, uint64_t len);
|
||||
void erase(Point *point, uint64_t amount, Direction dir);
|
||||
void erase(Range range);
|
||||
void replace(Range range, std::string_view str);
|
||||
void replace(Range range, const char *data, uint64_t len);
|
||||
|
||||
void move_clusters(Point *point, uint64_t amount, Direction dir);
|
||||
|
||||
Reference in New Issue
Block a user