magic-enter.plugin.zsh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Default commands
  2. : ${MAGIC_ENTER_GIT_COMMAND:="git status -u ."} # run when in a git repository
  3. : ${MAGIC_ENTER_OTHER_COMMAND:="ls -lh ."} # run anywhere else
  4. magic-enter() {
  5. # Only run MAGIC_ENTER commands when in PS1 and command line is empty
  6. # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#User_002dDefined-Widgets
  7. if [[ -n "$BUFFER" || "$CONTEXT" != start ]]; then
  8. return
  9. fi
  10. if command git rev-parse --is-inside-work-tree &>/dev/null; then
  11. BUFFER="$MAGIC_ENTER_GIT_COMMAND"
  12. else
  13. BUFFER="$MAGIC_ENTER_OTHER_COMMAND"
  14. fi
  15. }
  16. # Wrapper for the accept-line zle widget (run when pressing Enter)
  17. # If the wrapper already exists don't redefine it
  18. (( ! ${+functions[_magic-enter_accept-line]} )) || return 0
  19. case "$widgets[accept-line]" in
  20. # Override the current accept-line widget, calling the old one
  21. user:*) zle -N _magic-enter_orig_accept-line "${widgets[accept-line]#user:}"
  22. function _magic-enter_accept-line() {
  23. magic-enter
  24. zle _magic-enter_orig_accept-line -- "$@"
  25. } ;;
  26. # If no user widget defined, call the original accept-line widget
  27. builtin) function _magic-enter_accept-line() {
  28. magic-enter
  29. zle .accept-line
  30. } ;;
  31. esac
  32. zle -N accept-line _magic-enter_accept-line