feat(cli): add bin/doomscript
Meant as a simple elisp interpreter with Doom's CLI framework preloaded. Can be used as a shebang line: #!/usr/bin/env doomscript (princ "hello world!") This isn't used for bin/doom because it requires doomscript be in your $PATH, and any attempt to resolve its location in bin/doom's shebang line would reduce its portability. Neither of these should be an issue for the type of user who'd find this useful.
This commit is contained in:
parent
149306ef01
commit
e5b7edcd8d
1 changed files with 61 additions and 0 deletions
61
bin/doomscript
Executable file
61
bin/doomscript
Executable file
|
@ -0,0 +1,61 @@
|
|||
#!/usr/bin/env sh
|
||||
# This is a shebang interpreter for launching emacs lisp scripts with Doom's CLI
|
||||
# framework preloaded and all the metadata it needs initialized. Use it like so:
|
||||
#
|
||||
# #!/usr/bin/env doomscript
|
||||
# (print! "Hello world!")
|
||||
#
|
||||
# For this to work, this file must be in your $PATH.
|
||||
#
|
||||
# export PATH="$HOME/.emacs.d/bin:$PATH"
|
||||
#
|
||||
# This can also be exploited to evaluate arbitrary elisp against Doom's CLI
|
||||
# environment.
|
||||
#
|
||||
# This isn't used for bin/doom because of the $PATH requirement (and using
|
||||
# $BASH_SOURCE to locate it would reduce its POSIX compliance). This shouldn't
|
||||
# be an issue for folks writing their own CLIs, however.
|
||||
|
||||
set -e
|
||||
case "$EMACS" in
|
||||
*term*) EMACS=emacs ;;
|
||||
*) EMACS="${EMACS:-emacs}" ;;
|
||||
esac
|
||||
|
||||
TMPDIR=${TMPDIR:-`$EMACS -Q --batch --eval '(princ (temporary-file-directory))' 2>/dev/null`}
|
||||
if [ -z "$TMPDIR" ]; then
|
||||
>&2 echo "Error: failed to run Emacs with command '$EMACS'"
|
||||
>&2 echo
|
||||
>&2 echo "Are you sure Emacs is installed and in your \$PATH?"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$EMACSDIR" ]; then
|
||||
EMACSDIR="${XDG_CONFIG_HOME:-$HOME/.config}/emacs"
|
||||
[ -d "$EMACSDIR" ] || EMACSDIR="$HOME/.emacs.d"
|
||||
if [ ! -d "$EMACSDIR" ]; then
|
||||
>&2 echo "Failed to find \$EMACSDIR in ~/.config/emacs or ~/.emacs.d"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
export __DOOMPID="${__DOOMPID:-$$}"
|
||||
export __DOOMSTEP="$((__DOOMSTEP+1))"
|
||||
export __DOOMGEOM="${__DOOMGEOM:-`tput cols lines 2>/dev/null`}"
|
||||
export __DOOMGPIPE=${__DOOMGPIPE:-$__DOOMPIPE}
|
||||
export __DOOMPIPE=; [ -t 0 ] || __DOOMPIPE+=0; [ -t 1 ] || __DOOMPIPE+=1
|
||||
|
||||
tmpfile="$TMPDIR/doomscript.${__DOOMPID}"
|
||||
|
||||
target="$1"
|
||||
shift
|
||||
$EMACS -Q --batch \
|
||||
--load "$EMACSDIR/core/core-cli" \
|
||||
--load "$target" \
|
||||
-- "$@" || exit=$?
|
||||
# Execute exit-script, if requested (to simulate execve)
|
||||
if [ "${exit:-0}" -eq 254 ]; then
|
||||
sh "${tmpdir}/doom.${__DOOMPID}.${__DOOMSTEP}.sh" "$0" "$@" && true
|
||||
exit="$?"
|
||||
fi
|
||||
exit $exit
|
Loading…
Add table
Add a link
Reference in a new issue