diff --git a/src/janetsh b/src/janetsh index 9d80852..3138788 100755 --- a/src/janetsh +++ b/src/janetsh @@ -10,6 +10,30 @@ (var *parens* false) (var *handleopts* true) + +# function should accept: +# - type of expression: +# :shell -- when is a regular command (without paranes) +# :janet -- when is a Janet expression +# - quoted expression to be executed +(var *pre-exec-hook* nil) + +# function should accept: +# - type of expression: +# :shell -- when is a regular command (without paranes) +# :janet -- when is a Janet expression +# - quoted expression to be executed +# - result returned by expression +(var *post-exec-hook* nil) + +# function should accept: +# - type of expression: +# :shell -- when is a regular command (without paranes) +# :janet -- when is a Janet expression +# - quoted expression to be executed +# - error thrown by expression +(var *post-crash-exec-hook* nil) + (def- user-env (fiber/getenv (fiber/current))) (defn- help-handler @@ -158,15 +182,32 @@ (empty? (parser/state p)) (peg/match implicit-checker-peg buf))) +(defmacro- hookit + [expresion_type & f] + (with-syms [$res] + ~(do + (if (function? *pre-exec-hook*) + (apply *pre-exec-hook* ,expresion_type [',f])) + (var ,$res + (try + (do ,;f) + ([e] + (if (function? *post-crash-exec-hook*) + (apply *post-crash-exec-hook* ,expresion_type ',f [e])) + (error e)))) + (if (function? *post-exec-hook*) + (apply *post-exec-hook* ,expresion_type ',f [,$res])) + ,$res))) + (defn- getchunk [buf p] (sh/update-all-jobs-status) (def prompt (try (*get-prompt* p) ([e] "$ "))) (when (getline prompt buf) - (when (want-implicit-parens buf p) - (let [line (string buf)] - (buffer/clear buf) - (buffer/format buf "(sh/$? %s)\n" line))) - buf)) + (let [line (string buf)] + (buffer/clear buf) + (if (want-implicit-parens line p) + (buffer/format buf "(hookit :shell (sh/$? %s))\n" line) + (buffer/format buf "(hookit :janet %s)\n" line))))) (setdyn :pretty-format "%.40p")