-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-tcsh-completion.bash
executable file
·156 lines (135 loc) · 5.78 KB
/
setup-tcsh-completion.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash -i
#
# Copyright (C) 2017 Marc Khouzam <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# When executed, this script will generate a file with all the
# necessary tcsh complete commands to delegate completion
# to the existing bash completions scripts.
#
# Allow for debug printouts when running the script by hand
if [ "$1" == "-d" ] || [ "$1" == "--debug" ]; then
debug=true
shift
fi
root_path=$(cd `dirname $0` && pwd)
setup_script=${root_path}/`basename $0`
common_functions="${root_path}/common-functions.bash"
alias=completion-refresh
completion_scripts_path="/usr/share/bash-completion/completions"
bash_completion_script="/usr/share/bash-completion/bash_completion"
if [[ $(uname) == "Darwin" ]]; then
completion_scripts_path="/usr/local/etc/bash_completion.d"
bash_completion_script="/usr/local/etc/bash_completion"
fi
extra_scripts="${root_path}/extra-scripts.txt"
completion_file="${HOME}/.tcsh-completion.tcsh"
# Check that tcsh is modern enough for completion. We need version 6.16 or higher.
tcsh_version=(`tcsh --version | awk '{print $2}' | \sed 's/\./ /g'`)
if [[ ${tcsh_version[0]} -lt 6 || \
( ${tcsh_version[0]} -eq 6 && \
${tcsh_version[1]} -lt 16 ) ]]; then
unset tcsh_version
echo "ERROR: Your version of tcsh is too old, you need version 6.16.00 or newer. Enhanced tcsh-completion will not work."
exit
fi
# Echo the tcsh 'complete' command corresponding
# to the script passed as a parameter.
# Parameters:
# 1: The full path of the bash completion script
# for which this method will generate a tcsh
# 'complete' command.
_generate_tcsh_complete_command ()
{
toolCompletionScript=$1
# Remove any existing complete commands
complete -r
# Source the script to generate the complete command(s) we will parse
# Note that for some of the scripts we may handle to properly be sourced,
# we need to run the bash shell in interactive mode; this explains why
# we must use the '-i' flag at the top of the file.
# For example, this is necessary for the 'apropos' completion script
if [ "${debug}" == "true" ]; then
source ${toolCompletionScript}
else
source ${toolCompletionScript} &> /dev/null
fi
# Read each complete command generated as long as uses the -F format
complete | \egrep -e '-F' | while read completionCommand
do
# Remove everything up to the last space to find the command name
# e.g. complete -o bashdefault -o default -o nospace -F git_wrapgitk_main gitk
# becomes
# gitk
commandName=${completionCommand##* }
# Check if this command is one that can be sped up
speedUp="-s"
if [[ `grep ${commandName} ./speed.txt` != "" ]]; then
speedUp="-S"
fi
# Remove everything up to and including "-F "
# e.g. complete -o bashdefault -o default -o nospace -F git_wrapgitk_main gitk
# becomes
# git_wrapgitk_main gitk
tmp=${completionCommand##*-F }
# Remove everyting after the first space
# e.g. git_wrapgitk_main gitk
# becomes
# git_wrapgitk_main
# Note that we cannot simply use the output in $tmp to
# express both strings we are looking for as there could
# be other parameters included in $tmp
commandFunction=${tmp%% *}
echo complete ${commandName} \'p,\*,\`bash\ ${root_path}/tcsh-completion.bash\ ${speedUp}\ ${commandFunction}\ ${toolCompletionScript}\ \"\$\{COMMAND_LINE\}\"\`,\'
done
}
if [ -e ${common_functions} ]; then
source ${common_functions}
fi
\rm -f "${completion_file}"
# Go over each bash completion script and generate a corresponding 'complete' command
for script_path in ${completion_scripts_path}/*; do
_generate_tcsh_complete_command "${script_path}" >> "${completion_file}"
done
# Don't include those more basic completions until the tcsh handling is more robust
# _generate_tcsh_complete_command ${bash_completion_script} >> "${completion_file}"
# Handle any extra scripts specified by the user.
# First create the file if it is not there to help the user.
if [ ! -e "${extra_scripts}" ]; then
echo "# You can add the full path, one per line, of any" >> "${extra_scripts}"
echo "# bash completions script that you want tcsh-completion to use." >> "${extra_scripts}"
fi
# Ignore lines starting with # and lines with only spaces in them
for script_path in `cat "${extra_scripts}" | \egrep -ve '^#|^\s*$' `; do
# Replace a path starting with ~<user>/ or ~/ with the home directory of the user
# If we don't, then the function won't find the script in question
script_path="${script_path/#\~*([^\/])/$HOME}"
_generate_tcsh_complete_command "${script_path}" >> "${completion_file}"
done
# Add an alias to allow the user to easily refresh the completion script.
# This alias will be used when the user installs a new tool and wants to
# setup its completion
echo "alias ${alias} '${setup_script} && source ${completion_file}'" >> "${completion_file}"
echo
echo =\> 1.
echo =\> If not added already, add a line to source ${completion_file}
echo =\> in your .tcshrc or .cshrc file. Also note that you can add other completions scripts in
echo =\> the ${extra_scripts} file.
echo =\>
echo =\> 2.
echo =\> After installing a new tool, you can refresh the completions using the alias ${alias}
echo =\>
echo =\> 3.
echo =\> NOTE: For completions to work in the current shell you must source ${completion_file}
echo