#compdef kdesrc-build kde-builder --run

local exec_cmd i

# Scan command line for --exec argument and its value.
# I cound't find any easier built-in way of doing this.
for (( i = 1; i <= ${#words}; i++ )); do
  case $words[$i] in
    -e=*|--exec=*)
      exec_cmd="${${words[$i]}#*=}"
      break
      ;;
    -e|--exec)
      (( i += 1 ))
      exec_cmd="${words[$i]}"
      break
      ;;
  esac
done

_kdesrc_run_precommand() {
  local module

  # trick zsh into thinking that we are now completing opitons for $exec_cmd (if any)
  if [[ -n "$exec_cmd" ]]; then
    module=${words[1]}
    words[1]=$exec_cmd
    line=${line:/$module/$exec_cmd}
  fi

  _normal
}

_arguments -s \
  '(-e --exec)'{-e=,--exec=}'[Specify program of the module. Default to module name.]:::_command_names -e' \
  '(-f --fork)'{-f,--fork}'[Launch the program in a new session]' \
  '(-)1: :_kde-builder_modules' \
  '*:: :_kdesrc_run_precommand'
