Compare commits

..
4 Commits
Author SHA1 Message Date
syedm c0f9c6a234 Make r fucntion allow zero. 2026-08-31 10:16:25 +01:00
syedm 4b0f3b8fb7 Fix first == npos bug. 2026-08-31 10:15:07 +01:00
syedm 24914028c0 Fix incorrect error message. 2026-08-31 10:13:46 +01:00
syedm 0d126021d0 Seperate cd and pwd semantics to mimic shells 2026-08-31 10:13:14 +01:00
2 changed files with 26 additions and 8 deletions
+25 -7
View File
@@ -14,24 +14,42 @@ void Function::register_extented(BEd &ctx) {
.default_address = "", .default_address = "",
.accept_zero = false, .accept_zero = false,
.pre_text_mode = nullptr, .pre_text_mode = nullptr,
.handle = [](BEd &ctx, const buffer::Address &, vase::Shard *, const Argument &arg_, std::vector<buffer::Line> *) { .handle = [](BEd &, const buffer::Address &, vase::Shard *, const Argument &arg_, std::vector<buffer::Line> *) {
auto path = std::get<std::string>(arg_); auto path = std::get<std::string>(arg_);
const auto first = path.find_first_not_of(" \t"); const auto first = path.find_first_not_of(" \t");
const auto last = path.find_last_not_of(" \t"); const auto last = path.find_last_not_of(" \t");
path = path.substr(first, last - first + 1); if (first == std::string::npos)
path.clear();
else
path = path.substr(first, last - first + 1);
if (path.empty())
path = getenv("HOME");
if (path == "~") if (path == "~")
path = std::getenv("HOME"); path = getenv("HOME");
if (path.starts_with("~/")) { if (path.starts_with("~/")) {
const char *home = getenv("HOME"); const char *home = getenv("HOME");
if (home) if (home)
path = std::string(home) + path.substr(1); path = std::string(home) + path.substr(1);
} }
if (first != std::string::npos) if (chdir(path.c_str()) == -1)
if (chdir(path.c_str()) == -1) throw ed_error("Can't change directory.");
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<buffer::Line> *) {
char cwd[PATH_MAX]; char cwd[PATH_MAX];
if (!getcwd(cwd, sizeof(cwd))) if (!getcwd(cwd, sizeof(cwd)))
throw ed_error("Directory changed, but couldn't determine current directory."); throw ed_error("Can't determine current directory.");
ctx.io.write_line(cwd); ctx.io.write_line(cwd);
}, },
} }
+1 -1
View File
@@ -433,7 +433,7 @@ void Function::register_posix(BEd &ctx) {
.input_mode = Function::InputMode::None, .input_mode = Function::InputMode::None,
.desc = "Read from file into buffer.", .desc = "Read from file into buffer.",
.default_address = "$", .default_address = "$",
.accept_zero = false, .accept_zero = true,
.pre_text_mode = nullptr, .pre_text_mode = nullptr,
.handle = [](BEd &ctx, const buffer::Address &addr_, vase::Shard *, const Argument &arg, std::vector<buffer::Line> *) { .handle = [](BEd &ctx, const buffer::Address &addr_, vase::Shard *, const Argument &arg, std::vector<buffer::Line> *) {
auto &addr = std::get<buffer::Line>(addr_); auto &addr = std::get<buffer::Line>(addr_);