Add a few more ed commands.

This commit is contained in:
2026-08-13 16:38:01 +01:00
parent cd13557e15
commit 48fb8e3501
5 changed files with 41 additions and 9 deletions
-5
View File
@@ -1,5 +0,0 @@
#include "bed.h"
namespace bed {
void run(std::vector<std::string>) {}
} // namespace bed
+2
View File
@@ -15,6 +15,7 @@ void BEd::handle(std::string_view cmd_, bool eof) {
while (i < cmd.size() && (cmd[i] == ' ' || cmd[i] == '\t'))
++i;
};
skip_space();
auto addresses = internal::address::Address::handle(*this, cmd, i);
if (i >= cmd.size()) {
if (!no_op.accept_zero)
@@ -51,6 +52,7 @@ void BEd::handle(std::string_view cmd_, bool eof) {
throw ed_error("Command error.");
break;
case Command::SuffixKind::Suffix:
skip_space();
if (i < cmd.size()) {
if (suffixes[cmd[i] - 'a'].has_value())
suffix = &(suffixes[cmd[i++] - 'a'].value());
+5 -1
View File
@@ -13,6 +13,7 @@ Address::Result Address::handle(BEd &ctx, std::string &cmd, uint64_t &i) {
prev.offset = 0;
curr.base = Number(ctx.active->prev_range.end);
curr.offset = 0;
i++;
} else {
curr = Address(cmd, i);
}
@@ -29,11 +30,12 @@ Address::Result Address::handle(BEd &ctx, std::string &cmd, uint64_t &i) {
ctx.active->jump(curr.resolve(ctx));
prev_given = true;
}
i++;
prev = std::move(curr);
} else {
if (std::holds_alternative<None>(curr.base)) {
if (std::holds_alternative<std::monostate>(prev.base))
return {};
return {{}, 0};
if (prev_given) {
curr = prev;
} else {
@@ -42,6 +44,8 @@ Address::Result Address::handle(BEd &ctx, std::string &cmd, uint64_t &i) {
}
return {{prev.resolve(ctx), curr.resolve(ctx)}, 2};
}
if (prev_given)
return {{prev.resolve(ctx), curr.resolve(ctx)}, 2};
return {{curr.resolve(ctx)}, 1};
}
}
+33 -2
View File
@@ -40,7 +40,7 @@ void Command::register_posix(BEd &ctx) {
for (auto &[name, buffer] : ctx.buffers)
if (buffer->modified)
throw ed_error("Buffer " + name + " modified.");
throw fatal_error("", 0);
throw fatal_error("Quitting", 0);
}
};
ctx.commands.insert(
@@ -66,7 +66,38 @@ void Command::register_posix(BEd &ctx) {
for (auto &[name, buffer] : ctx.buffers)
if (buffer->modified)
throw ed_error("Buffer " + name + " modified.");
throw fatal_error("", 0);
throw fatal_error("Quitting", 0);
}
}
);
ctx.commands.insert(
"Q",
Command{
.address_mode = Command::AddressMode::None,
.suffix = Command::SuffixKind::None,
.desc = "Force quitting.",
.accept_zero = false,
.handle = [](BEd &, std::span<const uint64_t>, std::string_view) {
throw fatal_error("Force Quitting", 0);
}
}
);
ctx.commands.insert(
"p",
Command{
.address_mode = Command::AddressMode::Range,
.suffix = Command::SuffixKind::Suffix,
.desc = "Print range (default .,.)",
.accept_zero = false,
.handle = [](BEd &ctx, std::span<const uint64_t> addresses, std::string_view) {
uint64_t start_line = ctx.active->line;
uint64_t end_line = ctx.active->line;
if (!addresses.size())
ctx.active->print(start_line, end_line);
else if (addresses.size() == 1)
ctx.active->print(addresses[0], addresses[0]);
else
ctx.active->print(addresses[0], addresses[1]);
}
}
);
+1 -1
View File
@@ -7,7 +7,7 @@ int main(int argc, char *argv[]) {
bed::BEd ed(args);
ed.run();
} catch (bed::fatal_error &e) {
if (std::string_view(e.what()) != "")
if (e.code)
std::cout << "Fatal error: " << e.what() << std::endl;
return e.code;
} catch (...) {