]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/magic-enter/magic-enter.plugin.zsh
55b893535ca3895ea91de3687acaf17dd1b650a8
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / magic-enter / magic-enter.plugin.zsh
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
5 magic-enter() {
6   # Only run MAGIC_ENTER commands when in PS1 and command line is empty
7   # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#User_002dDefined-Widgets
8   if [[ -n "$BUFFER" || "$CONTEXT" != start ]]; then
9     return
10   fi
11
12   if command git rev-parse --is-inside-work-tree &>/dev/null; then
13     BUFFER="$MAGIC_ENTER_GIT_COMMAND"
14   else
15     BUFFER="$MAGIC_ENTER_OTHER_COMMAND"
16   fi
17 }
18
19 # Wrapper for the accept-line zle widget (run when pressing Enter)
20
21 # If the wrapper already exists don't redefine it
22 (( ! ${+functions[_magic-enter_accept-line]} )) || return 0
23
24 case "$widgets[accept-line]" in
25   # Override the current accept-line widget, calling the old one
26   user:*) zle -N _magic-enter_orig_accept-line "${widgets[accept-line]#user:}"
27     function _magic-enter_accept-line() {
28       magic-enter
29       zle _magic-enter_orig_accept-line -- "$@"
30     } ;;
31   # If no user widget defined, call the original accept-line widget
32   builtin) function _magic-enter_accept-line() {
33       magic-enter
34       zle .accept-line
35     } ;;
36 esac
37
38 zle -N accept-line _magic-enter_accept-line