#!/usr/bin/env sh

set -o errexit

# based on edit-command-line widget

tmpfile=${TMPPREFIX:-/tmp/zsh}ecls$$

print -R - "${PREBUFFER-}${BUFFER-}" >"${tmpfile-}"

# added emacs mode specification
print "# Local Variables:\n# mode: shell-script\n# End:" >>"${tmpfile-}"

"${VISUAL:-${EDITOR:-vi}}" "${tmpfile-}"
exec </dev/tty

# added deletion of comment lines
# shellcheck disable=SC2312
print -Rz - "$(sed '/^#/d' <"${tmpfile-}")"

command rm -f "${tmpfile-}"
zle send-break
