#!/bin/sh
#
# vim: ft=sh:sw=2:ts=2:sts=2:fdm=marker:fdl=0:noml:

v() {
  vim "$@"
}
# complete -F _filedir_xspec v

vv() {
  # shellcheck disable=SC2016
  vim +'edit $MYVIMRC' "$@"
}
# complete -F _filedir_xspec vv


vf() {
  # shellcheck disable=SC2312
  if [ "$#" -gt 0 ]; then
    vim "$("$@")"
  else
    printf '%s\n' 'Usage: vf cmd' '' 'Use {cmd} output as filenames' \
      'Brittle: {cmd} output will be word-split'
  fi
}
# complete -A command vf

vff() {
  # shellcheck disable=SC2312
  if [ $# = 1 ]; then
    vim "$(rg --files | grep "$1")"
  else
    printf '%s\n' "Usage: vff [filename] find {filename} to edit"
  fi
}

# vc() {
#   if [ $# -gt 0 ]; then
#     if pushd "$1" >/dev/null ; then
#       # ${@[0]} = $0 = name
#       # ${@[1]} = $1 = directory
#       # ${@:2}  = remaining args
#       vim "${@:2}"
#       # this must work because pushd succeeded
#       popd >/dev/null || return
#     fi
#   else
#     printf '%s\n' "$(basename "$0") dir [args] - execute vim in {dir}"
#   fi
# }
# complete -A directory vc

vs() {
  if [ "$#" -gt 0 ]; then
    session="$1"
    shift
    vim "$@" -S "${session}"
  else
    vim -S
  fi
}
# complete -G '*.vim' vs

# start vim with its last cursor position
lvim() {
  vim +'normal '"'"'0' "$@"
}

vh() {
  vim +"help $*" +only
}

# vgrep() {
#   pattern="${1////\\/}"
#   for f in "${@:2}" ; do
#     if [ -f "${f}" ]; then
#       ex -N -i NONE -n -- "${f}" <<<"argdo global/${pattern}/print" \
#         | sed "s/^/${f////\\/}:/"
#     # else
#     #   printf '%s\n' "${FUNCNAME[0]}: $f: Is a directory" >&2
#     fi
#   done
# }
#
# vw() {
#   # shellcheck disable=SC2312
#   if [ "$(type -t "$@")" != file ]; then
#     printf '%s\n' 'Usage: vw cmd' '' 'edit file defining {cmd}'
#   else
#     vim "$(command -v "$@")"
#   fi
# }
# complete -A command vw

# ============================================================================

# vww() {
#   { vim ="$*" } always {
#     if (( TRY_BLOCK_ERROR == 1 )) ; then
#     printf '%s\n' 'Usage: vw cmd' '' 'edit file defining {cmd}'
#     fi
#   }
# }

vn() {
  vim +'Ewiki notepad' "$@"
}

batchy() {
  vim +BatchyHere "$@"
}

vimnone() {
  vim -N -u NONE -U NONE "$@"
}

vimnorc() {
  vim -N -u NORC -U NONE "$@"
}

vimdefaults() {
  vim -u DEFAULTS -U NONE "$@"
}

vimclean() {
  vim --clean "$@"
}

vt() {
  for file; do
    # shellcheck disable=SC2312
    printf '\e]51;%s\07' "$(jq -cn --arg f "${file}" '["drop", $f]')"
  done
}
