From 48fb8e350153b980f98da9af738f199b49b2c58d Mon Sep 17 00:00:00 2001 From: Syed Daanish Date: Thu, 13 Aug 2026 16:38:01 +0100 Subject: [PATCH] Add a few more ed commands. --- src/bed.cc | 5 ----- src/bed/handle.cc | 2 ++ src/internal/address/handle.cc | 6 +++++- src/internal/commands/commands.cc | 35 +++++++++++++++++++++++++++++-- src/main.cc | 2 +- 5 files changed, 41 insertions(+), 9 deletions(-) delete mode 100644 src/bed.cc diff --git a/src/bed.cc b/src/bed.cc deleted file mode 100644 index 786bcf8..0000000 --- a/src/bed.cc +++ /dev/null @@ -1,5 +0,0 @@ -#include "bed.h" - -namespace bed { -void run(std::vector) {} -} // namespace bed diff --git a/src/bed/handle.cc b/src/bed/handle.cc index 3764b3d..36f5f67 100644 --- a/src/bed/handle.cc +++ b/src/bed/handle.cc @@ -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()); diff --git a/src/internal/address/handle.cc b/src/internal/address/handle.cc index 8492e0d..bf607bd 100644 --- a/src/internal/address/handle.cc +++ b/src/internal/address/handle.cc @@ -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(curr.base)) { if (std::holds_alternative(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}; } } diff --git a/src/internal/commands/commands.cc b/src/internal/commands/commands.cc index b929e85..9bae688 100644 --- a/src/internal/commands/commands.cc +++ b/src/internal/commands/commands.cc @@ -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, 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 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]); } } ); diff --git a/src/main.cc b/src/main.cc index 0bd5f68..0f1f2d6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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 (...) {