From 0d126021d0e064cfa390ba12f82007fdef74e6a3 Mon Sep 17 00:00:00 2001 From: Syed Daanish Date: Mon, 31 Aug 2026 10:13:14 +0100 Subject: [PATCH] Seperate cd and pwd semantics to mimic shells --- src/internal/functions/extended.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/internal/functions/extended.cc b/src/internal/functions/extended.cc index b61a054..b78cf6d 100644 --- a/src/internal/functions/extended.cc +++ b/src/internal/functions/extended.cc @@ -14,13 +14,15 @@ void Function::register_extented(BEd &ctx) { .default_address = "", .accept_zero = false, .pre_text_mode = nullptr, - .handle = [](BEd &ctx, const buffer::Address &, vase::Shard *, const Argument &arg_, std::vector *) { + .handle = [](BEd &, const buffer::Address &, vase::Shard *, const Argument &arg_, std::vector *) { auto path = std::get(arg_); const auto first = path.find_first_not_of(" \t"); const auto last = path.find_last_not_of(" \t"); path = path.substr(first, last - first + 1); + if (path.empty()) + path = getenv("HOME"); if (path == "~") - path = std::getenv("HOME"); + path = getenv("HOME"); if (path.starts_with("~/")) { const char *home = getenv("HOME"); if (home) @@ -29,6 +31,20 @@ void Function::register_extented(BEd &ctx) { if (first != std::string::npos) if (chdir(path.c_str()) == -1) throw ed_error("Can't change directory."); + }, + } + ); + ctx.functions.insert( + "pwd", + Function{ + .address_kind = Function::AddressKind::None, + .argument_kind = Function::ArgumentKind::None, + .input_mode = Function::InputMode::None, + .desc = "Print directory.", + .default_address = "", + .accept_zero = false, + .pre_text_mode = nullptr, + .handle = [](BEd &ctx, const buffer::Address &, vase::Shard *, const Argument &, std::vector *) { char cwd[PATH_MAX]; if (!getcwd(cwd, sizeof(cwd))) throw ed_error("Directory changed, but couldn't determine current directory.");