magic-enter.plugin.zsh 1.23 KB
Newer Older
1
2
3
# Default commands
: ${MAGIC_ENTER_GIT_COMMAND:="git status -u ."} # run when in a git repository
: ${MAGIC_ENTER_OTHER_COMMAND:="ls -lh ."}      # run anywhere else
Shadab Zafar's avatar
Shadab Zafar committed
4

5
6
7
8
9
10
11
12
13
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"
Shadab Zafar's avatar
Shadab Zafar committed
14
  else
15
    BUFFER="$MAGIC_ENTER_OTHER_COMMAND"
Shadab Zafar's avatar
Shadab Zafar committed
16
17
  fi
}
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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
37

38
zle -N accept-line _magic-enter_accept-line