fix(magic-enter): fix various bugs in the plugin (#9466)

This commit is contained in:
Marc Cornellà 2020-11-25 20:14:28 +01:00 committed by GitHub
parent 7a76afd61e
commit d88887195f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 21 deletions

View File

@ -1,27 +1,38 @@
# Bind quick stuff to enter! # Default commands
# : ${MAGIC_ENTER_GIT_COMMAND:="git status -u ."} # run when in a git repository
# Pressing enter in a git directory runs `git status` : ${MAGIC_ENTER_OTHER_COMMAND:="ls -lh ."} # run anywhere else
# in other directories `ls`
magic-enter () {
# If commands are not already set, use the defaults
[ -z "$MAGIC_ENTER_GIT_COMMAND" ] && MAGIC_ENTER_GIT_COMMAND="git status -u ."
[ -z "$MAGIC_ENTER_OTHER_COMMAND" ] && MAGIC_ENTER_OTHER_COMMAND="ls -lh ."
if [[ -z $BUFFER ]]; then magic-enter() {
echo "" # Only run MAGIC_ENTER commands when in PS1 and command line is empty
if git rev-parse --is-inside-work-tree &>/dev/null; then # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#User_002dDefined-Widgets
eval "$MAGIC_ENTER_GIT_COMMAND" if [[ -n "$BUFFER" || "$CONTEXT" != start ]]; then
else return
eval "$MAGIC_ENTER_OTHER_COMMAND"
fi fi
zle redisplay
if command git rev-parse --is-inside-work-tree &>/dev/null; then
BUFFER="$MAGIC_ENTER_GIT_COMMAND"
else else
zle accept-line BUFFER="$MAGIC_ENTER_OTHER_COMMAND"
fi fi
} }
zle -N magic-enter # Wrapper for the accept-line zle widget (run when pressing Enter)
bindkey -M emacs "^M" magic-enter # If the wrapper already exists don't redefine it
bindkey -M vicmd "^M" magic-enter (( ! ${+functions[_magic-enter_accept-line]} )) || return 0
bindkey -M viins "^M" magic-enter
case "$widgets[accept-line]" in
# Override the current accept-line widget, calling the old one
user:*) zle -N _magic-enter_orig_accept-line "${widgets[accept-line]#user:}"
function _magic-enter_accept-line() {
magic-enter
zle _magic-enter_orig_accept-line -- "$@"
} ;;
# If no user widget defined, call the original accept-line widget
builtin) function _magic-enter_accept-line() {
magic-enter
zle .accept-line
} ;;
esac
zle -N accept-line _magic-enter_accept-line