Parent directory

magic-enter.plugin.zsh

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