<commit_before>#  cd to the project root
#-----------------------------------------------
cdrt() {
  if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
    cd `git rev-parse --show-toplevel`
  fi
}


#  Make directory and enter to it
#-----------------------------------------------
mkd() {
  mkdir -p "$@" && cd "$@"
}


#  Peco insert path
#-----------------------------------------------
peco_insert_path() {
  local cmd

  if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
    cmd='git ls-files .'
  else
    cmd='find .'
  fi

  local filepath="$(eval "$cmd" | peco --rcfile=$HOME/.pecorc)"

  if [ "$LBUFFER" = "" ] && [ $(wc -l <<< "$filepath") -eq 1 ]; then
    if [ -d "$filepath" ]; then
      BUFFER="cd $filepath"
    elif [ -f "$filepath" ]; then
      BUFFER="$EDITOR $filepath"
    fi
  else
    filepath=$(tr "\n" ' ' <<< "$filepath")
    BUFFER="$LBUFFER$filepath"
  fi

  CURSOR=$#BUFFER
}

zle -N peco_insert_path
bindkey '^o^p' peco_insert_path


#  Peco insert modified files
#-----------------------------------------------
peco_modified_file() {
  local filepath="$(git status -s | cut -b 4- | peco --rcfile=$HOME/.pecorc | tr "\n" ' ')"

  local rbuf="$RBUFFER"
  BUFFER="$LBUFFER$filepath"
  CURSOR=$#BUFFER
  BUFFER="$BUFFER$rbuf"
}

zle -N peco_modified_file
bindkey '^o^m' peco_modified_file


#  Peco insert branch
#-----------------------------------------------
peco_insert_branch() {
  local branch="$(git branch --color=never | cut -c 3- | peco --rcfile=$HOME/.pecorc | tr "\n" ' ')"

  local rbuf="$RBUFFER"
  BUFFER="$LBUFFER$branch"
  CURSOR=$#BUFFER
  BUFFER="$BUFFER$rbuf"
}

zle -N peco_insert_branch
bindkey '^o^b' peco_insert_branch


#  Peco insert commit id
#-----------------------------------------------
peco_insert_commit() {
  local commit="$(git log --oneline --color=never | peco --rcfile=$HOME/.pecorc | cut -c -7 | tr "\n" ' ')"

  local rbuf="$RBUFFER"
  BUFFER="$LBUFFER$commit"
  CURSOR=$#BUFFER
  BUFFER="$BUFFER$rbuf"
}

zle -N peco_insert_commit
bindkey '^o^l' peco_insert_commit


#  Peco insert issue
#-----------------------------------------------
peco_insert_issue() {
  local issue="$(git github ls-issue | peco --rcfile=$HOME/.pecorc | tr "\n" ' ')"

  if [ "${LBUFFER[$CURSOR]}" = '#' ]; then
    issue=$(echo "$issue" | cut -d ' ' -f 1 | cut -c 2-)
  fi

  local rbuf="$RBUFFER"
  BUFFER="$LBUFFER$issue"
  CURSOR=$#BUFFER
  BUFFER="$BUFFER$rbuf"
}

zle -N peco_insert_issue
bindkey '^o^i' peco_insert_issue


#  Peco history
#-----------------------------------------------
peco_history() {
  BUFFER=$(\history -n 1 | tail -r | peco --query "$LBUFFER")
  CURSOR=$#BUFFER
}

zle -N peco_history
bindkey '^r' peco_history<commit_msg>Refactoring peco<commit_after>#=== Helper
#==============================================================================================
_register_keycommand() {
  zle -N $2
  bindkey "$1" $2
}


#=== CD
#==============================================================================================
#  Project root
#-----------------------------------------------
cdrt() {
  if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
    cd `git rev-parse --show-toplevel`
  fi
}


#  Make directory and enter to it
#-----------------------------------------------
mkd() {
  mkdir -p "$@" && cd "$@"
}


#=== Peco
#==============================================================================================
_peco_insert() {
  local rbuf="$RBUFFER"
  BUFFER="$LBUFFER$(cat)"
  CURSOR=$#BUFFER
  BUFFER="$BUFFER$rbuf"
}
_peco_replace() {
  BUFFER="$(cat)"
  CURSOR=$#BUFFER
}
_peco_select() {
  local tx="$(cat)"

  if [ "$tx" != '' ]; then
    peco --rcfile=$HOME/.pecorc --query "$1" <<< "$tx"
  fi
}
_peco_select_multi() {
  cat | _peco_select "$1" | tr "\n" ' '
}


#  Insert path
#-----------------------------------------------
peco_insert_path() {
  local cmd

  if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
    cmd='git ls-files .'
  else
    cmd='find .'
  fi

  eval "$cmd" \
    | _peco_select \
    | {
      file="$(cat)"

      if [ "$LBUFFER" = "" ] && [ $(wc -l <<< "$file") -eq 1 ]; then
        if [ -d "$file" ]; then
          echo -n "cd $file"
        elif [ -f "$file" ]; then
          echo -n "$EDITOR $file"
        fi
      else
        echo -n "$(tr "\n" ' ' <<< "$file")"
      fi
    } \
    | _peco_insert
}

_register_keycommand '^o^p' peco_insert_path


#  Insert modified files
#-----------------------------------------------
peco_modified_file() {
  git status -s \
    | cut -b 4- \
    | _peco_select_multi \
    | _peco_insert
}

_register_keycommand '^o^m' peco_modified_file


#  Insert branch
#-----------------------------------------------
peco_insert_branch() {
  git branch --color=never \
    | cut -c 3- \
    | _peco_select_multi \
    | _peco_insert
}

_register_keycommand '^o^b' peco_insert_branch


#  Insert commit id
#-----------------------------------------------
peco_insert_commit() {
  git log --oneline --color=never \
    | _peco_select_multi \
    | _peco_insert
}

_register_keycommand '^o^l' peco_insert_commit


#  Insert issue
#-----------------------------------------------
peco_insert_issue() {
  git github ls-issue \
    | _peco_select_multi \
    | {
      if [ "${LBUFFER[$CURSOR]}" = '#' ]; then
        cat | cut -d ' ' -f 1 | cut -c 2-
      else
        cat
      fi
    } \
    | _peco_insert
}

_register_keycommand '^o^i' peco_insert_issue


#  Iistory
#-----------------------------------------------
peco_history() {
  \history -n 1 \
    | tail -r \
    | _peco_select "$LBUFFER" \
    | _peco_replace
}

_register_keycommand '^r' peco_history
{'fraction_non_alphanumeric': 0.326163610719323, 'fraction_numerical': 0.004583921015514809, 'mean_word_length': 1.8608169440242057, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '65865', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1880, 'n_words': 421}