#compdef cmake
# ------------------------------------------------------------------------------
# Copyright (c) 2016 Github zsh-users - http://github.com/zsh-users
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the zsh-users nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------
# Description
# -----------
#
#  Completion script for CMake (http://www.cmake.org).
#
# -------------------------------------------------------------------------
# Authors
# -------
#
#  * Scott M. Kroll <skroll@gmail.com> (initial version)
#  * Paul Seyfert <pseyfert@mathphys.fsk.uni-heidelberg.de> (handling of --build)
#
# -------------------------------------------------------------------------
# Notes
# -----
#
#   * By default only C and C++ languages are supported for compiler flag
#     variables. To define your own list of languages:
#
#       cmake_langs=('C'   'C'
#                    'CXX' 'C++')
#       zstyle ':completion:*:cmake:*' languages $cmake_langs
#
# -------------------------------------------------------------------------

local context state line curcontext="$curcontext" cmake_args

local cmake_build_options;cmake_build_options=(
    '-C[Pre-load a script to populate the cache]:script:_files'
    '*-D-[Create a cmake cache entry]:property:_cmake_define_property'
    '-U[Remove matching entries from CMake cache]:globbing expression'
    '-G[Specify a makefile generator]:generator:_cmake_generators'
    '-T[Specify toolset name if supported by generator]:toolset name'
    '(-Wno-dev -Wdev)-Wno-dev[Suppress/Enable developer warnings]'
    '(-Wno-dev -Wdev)-Wdev[Suppress/Enable developer warnings]'
    '(-Wno-deprecated -Wdeprecated)-Wno-deprecated[Suppress/Enable deprecation warnings]'
    '(-Wno-deprecated -Wdeprecated)-Wdeprecated[Suppress/Enable deprecation warnings]'
    '(-Wno-error=dev -Werror=dev)-Wno-error=dev[Make developer warnings (not) errors]'
    '(-Wno-error=dev -Werror=dev)-Werror=dev[Make developer warnings (not) errors]'
    '(-Wno-error=deprecated -Werror=deprecated)-Werror=deprecated[Make deprecated macro and function warnings (not) errors]'
    '(-Wno-error=deprecated -Werror=deprecated)-Wno-error=deprecated[Make deprecated macro and function warnings (not) errors]'
    '--warn-uninitialized[Warn about uninitialized values.]'
    '--warn-unused-vars[Warn about unused variables.]'
    '--no-warn-unused-cli[Dont warn about command line options.]'
    '-i[Run in wizard mode]'
    '-L-[List cache variables]::_values "options" "[non-advanced cache variables]" "A[advanced cache variables]" "H[non-advanced cached variables with help]" "AH[advanced cache variables with help]"'
    '--trace[Put cmake in trace mode]'
    '--find-package[Run in pkg-config like mode.]'
    ':cmake project:_files -/'
)

# ------------------------
# _cmake_generator_options
# ------------------------
(( $+functions[_cmake_generator_options] )) ||
_cmake_generator_options() {
  if [ -f $1/Makefile ]
  then
    $_comps[make]
  elif [ -f $1/build.ninja ]
  then
    $_comps[ninja]
  fi
}

# --------------
# _cmake_targets
# --------------
(( $+functions[_cmake_targets] )) ||
_cmake_targets() {
  local -a targets
  if [ -f $1/Makefile ]
  then
    # `make help` doesn't work for Makefiles in general, but for cmake generated makefiles it does.
    i=1
    for target in $(make help | \grep -e "\.\.\." | sed "s/\.\.\. //" | sed "s/ (the default.*//") ; do
      targets[$i]=$target
      (( i = $i + 1 ))
    done
  elif [ -f $1/build.ninja ]
  then
    # `ninja help` doesn't seem to be the list of targets we're interested in
    i=1
    for target in $(ninja -C $1 -t targets all 2&>/dev/null | awk -F: '{print $1}') ; do
      targets[$i]="$target"
      (( i++ ))
    done
  fi
  _describe 'build targets' targets
}

_cmake_on_build() {
  local build_extras;build_extras=(
    '--[Native build tool options]'
    '--target[specify build target]'
    '--clean-first[build target clean first]'
    '--config[For multi-configuration tools]'
    '--use-stderr')
  local -a undescribed_build_extras
  i=1
  for be in $build_extras ; do
    undescribed_build_extras[$i]=$(echo $be | sed "s/\[.*//")
    (( i++ ))
  done
  inbuild=false
  nativemode=false
  for ((i = (($CURRENT - 1)); i > 1 ; i--)); do
    if [[ $words[$i] == --build ]] ; then
      inbuild=true
      buildat=$i
      (( difference = $CURRENT - $i ))
    elif [[ $words[$i] == -- ]] ; then
      nativemode=true
    fi
  done
  # check if build mode has been left
  outofbuild=false
  for ((i = (($CURRENT - 1)); i > (($buildat + 1)); i--)); do
    # don't check the word after --build (should be a directory)
    if [[ ${undescribed_build_extras[(r)$words[$i]]} == $words[$i] ]] ; then continue ; fi
    if [[ $words[(($i - 1))] == --target ]] ; then continue ; fi
    if [[ $words[(($i - 1))] == --config ]] ; then continue ; fi
    outofbuild=true
  done
  if [ "$nativemode" = true ] ; then
    _cmake_generator_options $words[(($buildat + 1))] && return 0
  fi
  if [ "$inbuild" = false ] ; then
    _arguments -C -s \
      - build_opts \
      "$cmake_build_options[@]" \
      - build_cmds \
      "$cmake_suggest_build[@]" && return 0
  elif [ $difference -eq 1 ] ; then
    # directly after --build comes the build directory
    _alternative ':current directory:(.)' 'directory::_directories' && return 0
  elif [[ $words[(($CURRENT - 1))] == --target ]] ; then
    # after --build <dir> --target, suggest targets
    _cmake_targets $words[(($buildat + 1))] && return 0
  elif [[ $words[(($CURRENT - 1))] == --config ]] ; then
    # after --build <dir> --config, no idea
    return 0
  elif [ "$outofbuild" = true ] ; then
    # after --build <dir> --<not a --build option>, suggest other cmake_build_options (like -Wno-dev)
    _arguments "$cmake_build_options[@]" && return 0
  else
    # after --build <dir>, suggest other cmake_build_options (like -Wno-dev) or --build options (like --clean-first)
    _arguments "$build_extras[@]" "$cmake_build_options[@]" && return 0
  fi
}

local cmake_help_actions;cmake_help_actions=(
    '(- 1)--help-command[Print help for a single command and exit]:command-name:_cmake_command_names'
    '(- 1)--help-command-list[List available listfile commands and exit]'
    '(- 1)--help-commands[Print help for all commands and exit]'
    '(- 1)--help-compatcommands[Print help for compatibility commands]'
    '(- 1)--help-module[Print help for compatibility commands]:module-name:_cmake_module_names'
    '(- 1)--help-module-list[Print help for a single module and exit]'
    '(- 1)--help-modules[Print help for all modules and exit]'
    '(- 1)--help-property[List available properties and exit]:property-name:_cmake_property_names'
    '(- 1)--help-property-list[List available properties and exit]'
    '(- 1)--help-properties[Print help for all properties and exit]'
    '(- 1)--help-variable[Print help for a single variable and exit]:variable-name:_cmake_variable_names'
    '(- 1)--help-variable-list[List documented variables and exit]'
    '(- 1)--help-variables[Print help for all variables and exit]'
    '(- 1)--copyright[Print the CMake copyright and exit]'
    '(- 1)'{--help,-help,-usage,-h,-H}'[Print usage information and exit]'
    '(- 1)--help-full[Print full help and exit]'
    '(- 1)--help-html[Print full help in HTML format]'
    '(- 1)--help-man[Print full help as a UNIX man page and exit]'
    '(- 1)'{--version,-version}'[Print full help as a UNIX man page and exit]'
)
_cmake_help() {
  _arguments -C -s - help "$cmake_help_actions[@]"
}

# -------------------
# _cmake_command_names
# -------------------
(( $+functions[_cmake_command_names] )) ||
_cmake_command_names() {
  local command_names; command_names=(${(f)"$($service --help-command-list 2> /dev/null)"})
  _values 'command name' ${command_names[@]:1} && return 0
}

# -----------------
# _cmake_list_names
# -----------------
(( $+functions[_cmake_list_names] )) ||
_cmake_list_names() {
  local command; command="$@[1]"
  local desc; desc="$@[2]"
  local list_names; list_names=(${(f)"$($service $command 2> /dev/null | sed -e 's/\[/\\\[/' -e 's/\]/\\\]/')"})

  _values ${desc} ${list_names[@]:1} && return 0
}

# ------------------
# _cmake_module_names
# ------------------
(( $+functions[_cmake_module_names] )) ||
_cmake_module_names() {
  _cmake_list_names '--help-module-list' 'module name' && return 0
}

# --------------------
# _cmake_property_names
# --------------------
(( $+functions[_cmake_property_names] )) ||
_cmake_property_names() {
  _cmake_list_names '--help-property-list' 'property name' && return 0
}

# ---------------------
# _cmake_variable_names
# ---------------------
(( $+functions[_cmake_variable_names] )) ||
_cmake_variable_names() {
  _cmake_list_names '--help-variable-list' 'variable name' && return 0
}

# ----------------------
# _cmake_define_property
# ----------------------
(( $+functions[_cmake_define_property] )) ||
_cmake_define_property() {
  if compset -P '*='; then
    _wanted property-values expl 'property value' _cmake_define_property_values ${${IPREFIX%=}#-D} && return 0
  else
    _wanted property-names expl 'property name' _cmake_define_property_names -qS= && return 0
  fi
}

# ----------------------------
# _cmake_define_property_names
# ----------------------------
(( $+functions[_cmake_define_property_names] )) ||
_cmake_define_property_names() {
  local alternatives; alternatives=(
    'common-property-names:common property name:_cmake_define_common_property_names -qS='
  )
  local -A cmake_langs
  zstyle -a ":completion:${curcontext}:" languages cmake_langs
  [[ $#cmake_langs -eq 0 ]] && cmake_langs=('C' 'C' 'CXX' 'C++')

  for cmake_lang in ${(k)cmake_langs}; do
    cmake_lang_desc="${cmake_langs[$cmake_lang]}"
    alternatives+=("${cmake_lang//:/-}-property-names:${cmake_lang_desc} language property name:_cmake_define_lang_property_names -qS= ${cmake_lang} ${cmake_lang_desc}")
  done

  _alternative "${alternatives[@]}"
}

# ---------------------------------
# _cmake_define_lang_property_names
# ---------------------------------
(( $+functions[_cmake_define_lang_property_names] )) ||
_cmake_define_lang_property_names() {
  local cmake_lang="$@[-2]" cmake_lang_desc="$@[-1]"
  local properties; properties=(
    "CMAKE_${cmake_lang}_COMPILER:${cmake_lang_desc} compiler"
    "CMAKE_${cmake_lang}_FLAGS:${cmake_lang_desc} compiler flags for all builds"
    "CMAKE_${cmake_lang}_FLAGS_DEBUG:${cmake_lang_desc} compiler flags for all Debug build"
    "CMAKE_${cmake_lang}_FLAGS_RELEASE:${cmake_lang_desc} compiler flags for all Relase build"
    "CMAKE_${cmake_lang}_FLAGS_MINSIZREL:${cmake_lang_desc} compiler flags for all MinSizRel build"
    "CMAKE_${cmake_lang}_FLAGS_RELWITHDEBINFO:${cmake_lang_desc} compiler flags for all RelWithDebInfo build"
  )

  _describe -t "${cmake_lang//:/-}-property-names" "${cmake_lang_desc} property name" properties $@[0,-3] && return 0
}

# -----------------------------------
# _cmake_define_common_property_names
# -----------------------------------
(( $+functions[_cmake_define_common_property_names] )) ||
_cmake_define_common_property_names() {
  local properties; properties=(
    'CMAKE_MODULE_PATH:Search path for cmake modules'
    'CMAKE_BUILD_TYPE:Specifies the build type for make based generators'
    'CMAKE_TOOLCHAIN_FILE:Absolute or relative path to a cmake script which sets up toolchain related variables'
    'CMAKE_COLOR_MAKEFILE:Enables/disables color output when using the Makefile generator'
    'CMAKE_INSTALL_PREFIX:Install directory used by install'
    'CMAKE_EXPORT_COMPILE_COMMANDS:Enable/disable output of compilation database during generation'
  )

  _describe -t 'common-property-names' 'common property name' properties $@
}

# ----------------------------
# _cmake_define_property_values
# ----------------------------
(( $+functions[_cmake_define_property_values] )) ||
_cmake_define_property_values() {
  local ret=1
  setopt localoptions extendedglob
  case $@[-1] in
    (CMAKE_BUILD_TYPE)     _wanted build-types expl 'build type' _cmake_build_types && ret=0;;
    (CMAKE_TOOLCHAIN_FILE) _wanted toolchain-files expl 'file' _cmake_toolchain_files && ret=0;;
    (CMAKE_COLOR_MAKEFILE) _wanted booleans expl 'boolean' _cmake_booleans && ret=0;;
    (CMAKE_INSTALL_PREFIX) _files -/ && ret=0;;
    (CMAKE_EXPORT_COMPILE_COMMANDS) _wanted booleans expl 'boolean' _cmake_booleans && ret=0;;
    (CMAKE_*_COMPILER)     _wanted compilers expl 'compiler' _cmake_compilers && ret=0;;
    (CMAKE_*_FLAGS(|_?*))  _message -e compiler-flags 'compiler flags' && ret=0;;
    (*)                    _files && ret=0;;
  esac

  return ret
}

# ------------------
# _cmake_build_types
# ------------------
(( $+functions[_cmake_build_types] )) ||
_cmake_build_types() {
  local build_types; build_types=(
    'Debug'
    'Release'
    'RelWithDebInfo'
    'MinSizeRel'
  )
  _values 'build type' ${build_types[@]}
}

# -----------------
# _cmake_generators
# -----------------
(( $+functions[_cmake_generators] )) ||
_cmake_generators() {
  local generators; generators=(
    'Unix Makefiles'
    'Ninja'
    'CodeBlocks - Ninja'
    'CodeBlocks - Unix Makefiles'
    'Eclipse CDT4 - Ninja'
    'Eclipse CDT4 - Unix Makefiles'
    'KDevelop3'
    'KDevelop3 - Unix Makefiles'
    'Sublime Text 2 - Ninja'
    'Sublime Text 2 - Unix Makefiles'
  )

  _describe -t generators 'generator' generators
}

# ----------------------
# _cmake_toolchain_files
# ----------------------
(( $+functions[_cmake_toolchain_files] )) ||
_cmake_toolchain_files() {
  _files -g '*\.cmake*'
}

# ---------------
# _cmake_booleans
# ---------------
(( $+functions[_cmake_booleans] )) ||
_cmake_booleans() {
  local booleans; booleans=(
    'YES'
    'NO'
  )
  _describe -t booleans 'boolean' booleans
}

# ---------------
# _cmake_compilers
# ---------------
(( $+functions[_cmake_compilers] )) ||
_cmake_compilers() {
  _command_names -e
}

local cmake_command_actions;cmake_command_actions=(
    '-E[CMake command mode]:*:command'
)
_cmake_command() {
  _arguments -C -s - command "$cmake_command_actions[@]"
}

local cmake_suggest_build;cmake_suggest_build=(
    '--build[build]'
)

if [ $CURRENT -eq 2 ] ; then
  _arguments -C -s \
    - help \
      "$cmake_help_actions[@]" \
    - command \
      "$cmake_command_actions[@]" \
    - build_opts \
      "$cmake_build_options[@]" \
    - build_cmds \
      "$cmake_suggest_build[@]" && return 0
elif [[ $words[2] = --help* ]] ; then
  _cmake_help
elif [[ $words[2] != -E ]] ; then
  _cmake_on_build
else
  _cmake_command
fi
