diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2020-12-16 22:13:45 -0700 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2020-12-16 22:13:45 -0700 |
commit | fb45741fc1dbd40dd2be72bc35a28c6ee8f3f7a5 (patch) | |
tree | dd7746c9910755dfeb5bf28bda68e28b47d5771f /plugins/magic-enter | |
parent | 3aaa0bc62ece494dd2b6e47a191de79e562156f9 (diff) | |
parent | b28665aebb4c1b07a57890eb59551bc51d0acf37 (diff) | |
download | zsh-fb45741fc1dbd40dd2be72bc35a28c6ee8f3f7a5.tar.gz zsh-fb45741fc1dbd40dd2be72bc35a28c6ee8f3f7a5.tar.bz2 zsh-fb45741fc1dbd40dd2be72bc35a28c6ee8f3f7a5.zip |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'plugins/magic-enter')
-rw-r--r-- | plugins/magic-enter/magic-enter.plugin.zsh | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/plugins/magic-enter/magic-enter.plugin.zsh b/plugins/magic-enter/magic-enter.plugin.zsh index 2d4d578b6..55b893535 100644 --- a/plugins/magic-enter/magic-enter.plugin.zsh +++ b/plugins/magic-enter/magic-enter.plugin.zsh @@ -1,27 +1,38 @@ -# Bind quick stuff to enter! -# -# Pressing enter in a git directory runs `git status` -# 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 ." +# Default commands +: ${MAGIC_ENTER_GIT_COMMAND:="git status -u ."} # run when in a git repository +: ${MAGIC_ENTER_OTHER_COMMAND:="ls -lh ."} # run anywhere else - if [[ -z $BUFFER ]]; then - echo "" - if git rev-parse --is-inside-work-tree &>/dev/null; then - eval "$MAGIC_ENTER_GIT_COMMAND" - else - eval "$MAGIC_ENTER_OTHER_COMMAND" - fi - zle redisplay +magic-enter() { + # Only run MAGIC_ENTER commands when in PS1 and command line is empty + # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#User_002dDefined-Widgets + if [[ -n "$BUFFER" || "$CONTEXT" != start ]]; then + return + fi + + if command git rev-parse --is-inside-work-tree &>/dev/null; then + BUFFER="$MAGIC_ENTER_GIT_COMMAND" else - zle accept-line + BUFFER="$MAGIC_ENTER_OTHER_COMMAND" fi } -zle -N magic-enter +# Wrapper for the accept-line zle widget (run when pressing Enter) + +# If the wrapper already exists don't redefine it +(( ! ${+functions[_magic-enter_accept-line]} )) || return 0 + +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 -bindkey -M emacs "^M" magic-enter -bindkey -M vicmd "^M" magic-enter -bindkey -M viins "^M" magic-enter +zle -N accept-line _magic-enter_accept-line |