if [[ $- =~ i ]] && [[ -z "$ZELLIJ" ]] && [[ -n "$SSH_TTY" ]]; then
  zellij attach --create ssh
fi

function ga {
  if [[ -z $1 ]]; then
    git add -p
  else
    git add $@
  fi
}

function gbfm {
  if [[ ! -z $3 ]]; then
    start=$3
  elif [[ $(git rev-parse --abbrev-ref HEAD) == "HEAD" ]]; then
    start=HEAD
  elif git remote get-url upstream >/dev/null 2>&1; then
    start=upstream
  elif git remote get-url origin >/dev/null 2>&1; then
    start=origin
  else
    echo "Unknown start point"
    return 1
  fi

  git switch --no-track $1 $2 $start
}

function gfc {
  git clone $@ || return 1
  cd ./*(/om[1]) || return 1
  default_branch=$(git branch --show-current)
  git checkout origin || return 1
  git branch --delete $default_branch || return 1
}

function gps {
  branch=$(git rev-parse --abbrev-ref HEAD) || return 1

  if git remote get-url fork >/dev/null 2>&1; then
    remote=fork
  elif git remote get-url origin >/dev/null 2>&1; then
    remote=origin
  elif [[ -z $1 ]]; then
    remote=$1
  else
    echo "No remote specified"
    return 1
  fi

  if [[ $branch != "HEAD" ]]; then
    git push --set-upstream $remote $branch
  else
    echo "Not on a branch"
    return 1
  fi
}

# https://github.com/nix-community/nix-direnv#shell-integration
function nixify {
  if [ ! -e ./.envrc ]; then
    echo "use nix" > .envrc
    direnv allow
  fi
  if [[ ! -e shell.nix ]]; then
    cat > shell.nix <<'EOF'
with import <nixpkgs> {};
mkShell {
  nativeBuildInputs = [
    bashInteractive
  ];
}
EOF
    ${EDITOR:-vim} shell.nix
  fi
}

function flakify {
  if [ ! -e flake.nix ]; then
    nix flake new -t github:nix-community/nix-direnv .
  elif [ ! -e .envrc ]; then
    echo "use flake" > .envrc
    direnv allow
  fi
  ${EDITOR:-vim} flake.nix
}

function ranger-cd {
  tempfile=$(mktemp)
  ranger --choosedir="$tempfile" "${@:-$(pwd)}" < $TTY
  test -f "$tempfile" &&
  if [[ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]]; then
      cd -- "$(cat "$tempfile")"
  fi
  rm -f -- "$tempfile"
}

function carry-ranger {
  ranger < $TTY
  zle push-line
  zle accept-line
}

function carry-ranger-cd {
  ranger-cd
  zle push-line
  zle accept-line
}

function peco_select_history {
  local tac

  if (( $+commands[tac] )); then
    tac="tac"
  else
    tac="tail -r"
  fi

  BUFFER=$(fc -l -n 1 | eval $tac | peco --query "$LBUFFER")
  zle end-of-line
  zle push-line
  zle accept-line
}

zle -N peco_select_history
bindkey '^R' peco_select_history
bindkey -r '^S'

autoload -z edit-command-line
zle -N edit-command-line

zle -N carry-ranger
zle -N carry-ranger-cd

bindkey '^E^E' edit-command-line
bindkey '^Er' carry-ranger
bindkey '^Ec' carry-ranger-cd

bindkey -r '^[OA'
bindkey -r '^[OB'

# Allow using `#`, `~` and `^` without escape
unsetopt EXTENDED_GLOB

unalias gcfS 2>/dev/null || true
unalias gcF 2>/dev/null || true
unalias gcFS 2>/dev/null || true
unalias gfc 2>/dev/null || true
unalias gr 2>/dev/null || true
