.bash_completion
author Oleksandr Gavenko <gavenkoa@gmail.com>
Mon, 01 Jan 2024 20:53:49 +0200
changeset 1039 78cdb4a057e3
parent 1038 79c56b8fb8e0
permissions -rw-r--r--
Create symlink from ~/.bash_profile for login shell to activate my ~/.bashrc.

# -*- mode: shell-script -*-

[[ "$OSTYPE" = cygwin ]] && shopt -s completion_strip_exe

# user commands see only users
complete -u su passwd write chfn groups slay w sux

# bg completes with stopped jobs
complete -A stopped -P '"%' -S '"' bg

# other job commands
complete -j -P '"%' -S '"' fg jobs disown

# readonly and unset complete with shell variables
complete -v readonly unset

# set completes with set options
complete -A setopt set

# shopt completes with shopt options
complete -A shopt shopt

# helptopics
complete -A helptopic help

# unalias completes with aliases
complete -a unalias

# bind completes with readline bindings (make this more intelligent)
complete -A binding bind

# type and which complete on commands
complete -c command type which

# builtin completes on builtins
complete -b builtin

_pathsearch() {
  local prev cur=${COMP_WORDS[COMP_CWORD]}
  [[ $COMP_CWORD > 0 ]] && prev=${COMP_WORDS[COMP_CWORD-1]}
  if [[ $prev = -d ]]; then
    COMPREPLY=()
    return 0
  fi
  case "$cur" in
    -*) COMPREPLY=( $(compgen -W "-d --delimiter= -h --help" -- $cur ) ) ;;
    *) COMPREPLY=() ;;
  esac
  return 0
}
complete -F _pathsearch -o nospace pathsearch

_mycd() {
  local cur
  local IFS=$'\n'
  cur=${COMP_WORDS[COMP_CWORD]}
  if [[ -z "${CDPATH:-}" ]] || [[ $cur == ?(.)?(.)/* ]] || [[ $cur == '~'/* ]]; then
    compopt -o dirnames; COMPREPLY=()
    return 0
  fi
  local i j k
  for i in ${CDPATH//:/$'\n'}; do
    for j in $( compgen -d -- $i/$cur ); do
      COMPREPLY+=(${j#$i/}/)
    done
  done
  return 0
}
complete -F _mycd -o nospace cd

_man() {
  local p
  local cur=${COMP_WORDS[COMP_CWORD]}
  if [[ $cur = /* || $cur = ./* ]]; then
    COMPREPLY=( $(compgen -f -- $cur) )
  fi
  local IFS=':
'
  for p in /usr/share/man $MANPATH; do
    [[ -n $p ]] || continue
    p=( $p/man*/* )
    p=( ${p[@]##*/} )
    p=( ${p[@]%.gz} )
    p=( ${p[@]%.*} )
    p=( $(compgen -W '${p[@]}' -- $cur ) )
    COMPREPLY=( ${COMPREPLY[@]} ${p[@]} )
  done
}
complete -F _man man

_make() {
  local i mk
  local dir=.
  local cur=${COMP_WORDS[COMP_CWORD]}
  if [[ $cur = -* ]]; then
    COMPREPLY=( $(compgen -W "-C -f" -- "$cur") )
    return 0
  fi
  [[ $COMP_CWORD > 0 ]] && prev=${COMP_WORDS[COMP_CWORD-1]}
  for (( i=1; i < ${#COMP_WORDS[@]}-1; i++ )); do
    case ${COMP_WORDS[i]} in
      -f) mk=${COMP_WORDS[i+1]} ;;
      -C) dir=${COMP_WORDS[i+1]} ;;
    esac
  done
  if [[ $prev = -C ]]; then
    COMPREPLY=( $(compgen -d -- "$cur") )
    return 0
  fi
  if [[ $prev = -f ]]; then
    COMPREPLY=( $(cd $dir >/dev/null 2>&1; compgen -f -- "$cur") )
    return 0
  fi
  mk=$dir/$mk
  [[ -f $mk ]] || mk=$(eval echo $dir/[Mm]akefile)
  [[ -f $mk ]] || return 1
  COMPREPLY=( $(compgen -W "$(sed -n -e '/^[[:alnum:]_-]*:/{s=^\([^:]*\):.*=\1=;p;}' $mk)" -- $cur) )
  return 0
}
complete -F _make make gmake pmake

_cygcheck() {
  local opt
  local cur=${COMP_WORDS[COMP_CWORD]}
  case $COMP_CWORD in
    1)
      case $cur in
        -*)
          COMPREPLY=( $(compgen -W "-v --verbose -h --help -V --version -c --check-setup -s --sysinfo
            -k --keycheck -f --find-package -l --list-package -p --package-query" -- "$cur") )
          return 0 ;;
        *)
          COMPREPLY=( $(compgen -c -- "$cur") )
          return 0 ;;
      esac
      ;;
    2)
      opt=${COMP_WORDS[1]}
      case $opt in
        -c|--check-setup|-l|--list-package)
          local pkgs=( /etc/setup/*.lst.gz )
          pkgs=( ${pkgs[@]##*/} )
          pkgs=( ${pkgs[@]%.lst.gz} )
          COMPREPLY=( $(compgen -W '${pkgs[@]}' -- $cur ) )
          return 0;;
      esac
      return 0;;
  esac
  return 0
}
complete -F _cygcheck cygcheck.exe cygcheck

_lighty() {
  local cur=${COMP_WORDS[COMP_CWORD]}
  local mods=( /etc/lighttpd/conf-available/*.conf )
  mods=( ${mods[@]##*/} )
  mods=( ${mods[@]#??-} )
  mods=( ${mods[@]%.conf} )

  COMPREPLY=( $(compgen -W '${mods[@]}' -- $cur) )
  return 0
}

complete -F _lighty lighty-disable-mod
complete -F _lighty lighty-enable-mod
complete -F _lighty lighttpd-disable-mod
complete -F _lighty lighttpd-enable-mod