#autoload

# based on edit-command-line widget

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

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

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

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

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

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

autoload -U edit-command-line-in-shell-mode
zle -N edit-command-line-in-shell-mode
bindkey '^X^e' edit-command-line-in-shell-mode
bindkey -M viins '^X^e' edit-command-line-in-shell-mode
bindkey -M vicmd '^X^e' edit-command-line-in-shell-mode
