diff --git a/src/examples/pebblisp.pbl b/src/examples/pebblisp.pbl index 026c36d..0f54b0d 100644 --- a/src/examples/pebblisp.pbl +++ b/src/examples/pebblisp.pbl @@ -10,8 +10,12 @@ (def bold "") (def reset "") +(def esc (ch 27)) + (def nl (ch 10)) +(def first (fn (list) (at 0 list))) + (def up "..") (def ~ (env "HOME")) @@ -28,10 +32,23 @@ (def clock (fn (ti) (cat (hour ti) ":" (zero ti.minute) ":" (zero ti.sec)))) +(struct Alias (name value)) +(def aliases ( + (Alias "ls" "ls --color") +)) + +(def preprocess (fn (text) ( + (def matches (fil (fn (a) (= text a.name)) aliases)) + (def match (first matches)) + (if (iserr match) text match.value) +))) + (def prompt (fn (a) ( (def ti (time)) + (def d (cwd)) (cat nl - bold red "[sage] " blue (clock) " " reset cyan (cwd) nl + esc "]0; " d (ch 7) + bold red "[sage] " blue (clock) " " reset cyan d nl bold green "pebblisp ~> " reset) ))) diff --git a/src/pebblisp.c b/src/pebblisp.c index 175309d..7317b02 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -613,6 +613,15 @@ char* getPrompt(struct Environment* env) return ret; } +char* preprocess(char* buf, struct Environment* env) +{ + Object lambda = fetchFromEnvironment("preprocess", env); + Object buffer = nullTerminated(buf); + Object s = listEvalLambda(&lambda, &buffer, 2, env); + size_t length; + return stringObj(&s, &length); +} + void repl(struct Environment* env) { char* buf; @@ -623,12 +632,19 @@ void repl(struct Environment* env) free(buf); break; } + buf = preprocess(buf, env); if (buf[0] == '\0') { free(buf); continue; } add_history(buf); - if ((buf[0] == 'c' && buf[1] == 'd') || (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0'))) { + if ((buf[0] == 'c' && buf[1] == 'd')) { + char* oldBuf = buf; + buf = malloc(sizeof(char) * strlen(buf + 6)); + sprintf(buf, "(cd \"%s\")", oldBuf + 3); + free(oldBuf); + } + if ((buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0'))) { char* oldBuf = buf; buf = malloc(sizeof(char) * strlen(buf + 3)); sprintf(buf, "(%s)", oldBuf); @@ -707,6 +723,8 @@ int main(int argc, const char* argv[]) Object o = parseEval("(def prompt \"pebblisp::> \")", &env); cleanObject(&o); + o = parseEval("(def preprocess (fn (text) (text)))", &env); + cleanObject(&o); const char* const home = getenv("HOME"); char config[strlen(home) + 15];