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