contrib / completion / git-completion.tcshon commit Merge branch 'jc/maint-pull-docfix' into maint (01a5774)
   1#!tcsh
   2#
   3# tcsh completion support for core Git.
   4#
   5# Copyright (C) 2012 Marc Khouzam <marc.khouzam@gmail.com>
   6# Distributed under the GNU General Public License, version 2.0.
   7#
   8# When sourced, this script will generate a new script that uses
   9# the git-completion.bash script provided by core Git.  This new
  10# script can be used by tcsh to perform git completion.
  11# The current script also issues the necessary tcsh 'complete'
  12# commands.
  13#
  14# To use this completion script:
  15#
  16#    0) You need tcsh 6.16.00 or newer.
  17#    1) Copy both this file and the bash completion script to ${HOME}.
  18#       You _must_ use the name ${HOME}/.git-completion.bash for the
  19#       bash script.
  20#       (e.g. ~/.git-completion.tcsh and ~/.git-completion.bash).
  21#    2) Add the following line to your .tcshrc/.cshrc:
  22#        source ~/.git-completion.tcsh
  23#    3) For completion similar to bash, it is recommended to also
  24#       add the following line to your .tcshrc/.cshrc:
  25#        set autolist=ambiguous
  26#       It will tell tcsh to list the possible completion choices.
  27
  28set __git_tcsh_completion_version = `\echo ${tcsh} | \sed 's/\./ /g'`
  29if ( ${__git_tcsh_completion_version[1]} < 6 || \
  30     ( ${__git_tcsh_completion_version[1]} == 6 && \
  31       ${__git_tcsh_completion_version[2]} < 16 ) ) then
  32        echo "git-completion.tcsh: Your version of tcsh is too old, you need version 6.16.00 or newer.  Git completion will not work."
  33        exit
  34endif
  35unset __git_tcsh_completion_version
  36
  37set __git_tcsh_completion_original_script = ${HOME}/.git-completion.bash
  38set __git_tcsh_completion_script = ${HOME}/.git-completion.tcsh.bash
  39
  40# Check that the user put the script in the right place
  41if ( ! -e ${__git_tcsh_completion_original_script} ) then
  42        echo "git-completion.tcsh: Cannot find: ${__git_tcsh_completion_original_script}.  Git completion will not work."
  43        exit
  44endif
  45
  46cat << EOF > ${__git_tcsh_completion_script}
  47#!bash
  48#
  49# This script is GENERATED and will be overwritten automatically.
  50# Do not modify it directly.  Instead, modify git-completion.tcsh
  51# and source it again.
  52
  53source ${__git_tcsh_completion_original_script}
  54
  55# Remove the colon as a completion separator because tcsh cannot handle it
  56COMP_WORDBREAKS=\${COMP_WORDBREAKS//:}
  57
  58# For file completion, tcsh needs the '/' to be appended to directories.
  59# By default, the bash script does not do that.
  60# We can achieve this by using the below compatibility
  61# method of the git-completion.bash script.
  62__git_index_file_list_filter ()
  63{
  64        __git_index_file_list_filter_compat
  65}
  66
  67# Set COMP_WORDS in a way that can be handled by the bash script.
  68COMP_WORDS=(\$2)
  69
  70# The cursor is at the end of parameter #1.
  71# We must check for a space as the last character which will
  72# tell us that the previous word is complete and the cursor
  73# is on the next word.
  74if [ "\${2: -1}" == " " ]; then
  75        # The last character is a space, so our location is at the end
  76        # of the command-line array
  77        COMP_CWORD=\${#COMP_WORDS[@]}
  78else
  79        # The last character is not a space, so our location is on the
  80        # last word of the command-line array, so we must decrement the
  81        # count by 1
  82        COMP_CWORD=\$((\${#COMP_WORDS[@]}-1))
  83fi
  84
  85# Call _git() or _gitk() of the bash script, based on the first argument
  86_\${1}
  87
  88IFS=\$'\n'
  89if [ \${#COMPREPLY[*]} -eq 0 ]; then
  90        # No completions suggested.  In this case, we want tcsh to perform
  91        # standard file completion.  However, there does not seem to be way
  92        # to tell tcsh to do that.  To help the user, we try to simulate
  93        # file completion directly in this script.
  94        #
  95        # Known issues:
  96        #     - Possible completions are shown with their directory prefix.
  97        #     - Completions containing shell variables are not handled.
  98        #     - Completions with ~ as the first character are not handled.
  99
 100        # No file completion should be done unless we are completing beyond
 101        # the git sub-command.  An improvement on the bash completion :)
 102        if [ \${COMP_CWORD} -gt 1 ]; then
 103                TO_COMPLETE="\${COMP_WORDS[\${COMP_CWORD}]}"
 104
 105                # We don't support ~ expansion: too tricky.
 106                if [ "\${TO_COMPLETE:0:1}" != "~" ]; then
 107                        # Use ls so as to add the '/' at the end of directories.
 108                        COMPREPLY=(\`ls -dp \${TO_COMPLETE}* 2> /dev/null\`)
 109                fi
 110        fi
 111fi
 112
 113# tcsh does not automatically remove duplicates, so we do it ourselves
 114echo "\${COMPREPLY[*]}" | sort | uniq
 115
 116# If there is a single completion and it is a directory, we output it
 117# a second time to trick tcsh into not adding a space after it.
 118if [ \${#COMPREPLY[*]} -eq 1 ] && [ "\${COMPREPLY[0]: -1}" == "/" ]; then
 119        echo "\${COMPREPLY[*]}"
 120fi
 121
 122EOF
 123
 124# Don't need this variable anymore, so don't pollute the users environment
 125unset __git_tcsh_completion_original_script
 126
 127complete git  'p,*,`bash ${__git_tcsh_completion_script} git "${COMMAND_LINE}"`,'
 128complete gitk 'p,*,`bash ${__git_tcsh_completion_script} gitk "${COMMAND_LINE}"`,'