Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions src/janetsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down