#compdef gdb

local cur="$words[CURRENT]" prev w list ret=1 expl

[[ "$PREFIX" = --* ]] &&
    _arguments -S \
		  '--args[Arguments after executable-file are passed to inferior]' \
		  '--batch[Exit after processing options.]' \
		  '-b[Set serial port baud rate used for remote debugging.]' \
		  '--cd=[Change current directory to DIR.]:directory:_files -/' \
		  '--command=[Execute GDB commands from FILE.]:file:_files' \
		  '--core=[Analyze the core dump COREFILE.]:core file:_files' \
		  '--dbx[DBX compatibility mode.]' \
		  '--directory=[Search for source files in DIR.]:directory:_files -/' \
		  '--epoch[Output information used by epoch emacs-GDB interface.]' \
		  '--exec=[Use EXECFILE as the executable.]:executable:_files -g \*\(-\*\)' \
		  '--fullname[Output information used by emacs-GDB interface.]' \
		  '--help[Print this message.]' \
		  '--interpreter=[Select a specific interpreter / user interface]:interp: ' \
		  '--mapped[Use mapped symbol files if supported on this system.]' \
		  '(--noasync)--async[Enable asynchronous version of CLI]' \
		  '(--async)--noasync[Disable asynchronous version of CLI]' \
		  '--nw[Do not use a window interface.]' \
		  '--nx[Do not read .gdbinit file.]' \
		  '--pid=[Attach to running process PID.]:pid: ' \
		  '--quiet[Do not print version number on startup.]' \
		  '--readnow[Fully read symbol files on first access.]' \
		  '--se=[Use FILE as symbol file and executable file.]:file:_files' \
		  '--symbols=[Read symbols from SYMFILE.]:core file:_files' \
		  '--tty=[Use TTY for input/output by the program being debugged.]:terminal device:compadd /dev/tty\*' \
		  '--tui[Use a terminal user interface.]' \
		  '--version[Print version information and then exit.]' \
		  '--write[Set writing into executable and core files.]' \
		  '-w[Use a window interface.]' \
		  '--xdb[XDB compatibility mode.]' \
		  && return 0

if compset -P '-(cd|directory)='; then
  _files -/
elif compset -P '-tty='; then
  _wanted devices expl 'terminal device' compadd - /dev/tty*
elif compset -P '-(exec|se)='; then
  _description files expl executable
  _files "$expl[@]" -g '*(-*)'
elif compset -P '-(symbols|core|command)='; then
  _files
elif [[ "$PREFIX" = -* ]]; then
  _tags options
  while _tags; do
    while _next_label options expl option; do
      compadd "$expl[@]" -QS '' - -symbols\= -exec\= -se\= -core\= -command\= \
                                  -directory\= -cd\= -tty\= && ret=0
      compadd "$expl[@]"        - -help -h -s -e -c -x -d -nx -n -quiet -q \
	    		          -batch -fullname -f -b && ret=0
    done
    (( ret )) || return 0
  done
else
  prev="$words[CURRENT-1]"

  case "$prev" in
  (-d)     _files -/ && return 0 ;;
  (-[csx]) _files && return 0 ;;
  (-e)     _description files expl executable
           _files "$expl[@]" -g '*(-*)' && return 0 ;;
  (-b)     _wanted -V values expl 'baud rate' \
               compadd 0 50 75 110 134 150 200 300 600 1200 1800 2400 4800 \
		       9600 19200 38400 57600 115200 230400 && return 0 ;;
  esac

  w=( "${(@)words[2,-1]}" )
  while [[ "$w[1]" = -* ]]; do
    [[ "$w[1]" = -[decsxb] ]] && shift 1 w
    shift 1 w
  done

  if [[ $#w -gt 1 ]]; then
    _alternative "files:: _files -g '*core(-.)'" "processes:: _pids -m ${w[1]:t}"
  else
    _description files expl executable
    _files "$expl[@]" -g '*(-*)'
  fi
fi
