aboutsummaryrefslogtreecommitdiffhomepage
path: root/make_mercurial_completions.fish
blob: e885d00ea8e958926bd2a5888ff5767289cc88cb (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env fish

#
# This file produces command specific completions for either hg or darcs
#

function cap
	set res (echo $argv |cut -c 1|tr a-z A-Z)(echo $argv |cut -c 2-)
	echo $res
end

function esc
	echo $argv | sed -e "s/'/\\\'/g"
end


#
# This function formats a list of completion information into a set of fish completions
# 
# The first argument is the condition string, which will be copied to
# the resulting commandline verbatim
#
# Remaining arguments are tab separated lists of completion
# information. Each list contains four elements, the short switch, the
# long switch, the argument and the description.
#

function complete_from_list

	set condition $argv[1]
	set -e argv[1]

	for j in $argv
		set exploded (echo $j|tr \t \n)
		set short $exploded[1]
		set long $exploded[2]
		set arg $exploded[3]
		set desc (cap (esc $exploded[4]))

		set str 

		switch $short
			case '-?'
				set str $str -s (printf "%s\n" $short|cut -c 2)
		end

		switch $long
			case '--?*'
				set str $str -l (printf "%s\n" $long|cut -c 3-)
		end

		switch $arg
			case '=DIRECTORY' ' dir'
				set str $str -x -a "'(__fish_complete_directories (commandline -ct))'"

			case '=COMMAND'
				set str $str -x -a "'(__fish_complete_command)'"

			case '=USERNAME' ' <user>'
				set str $str -x -a "'(__fish_complete_users)'"

			case '=FILENAME' '=FILE' ' <file>'
				set str $str -r 

			case ' arg'
				set str $str -x

			case ' (*):'
				 set str $str -x -a \'(echo $arg| sed -e "s/ (\(.*\)):/\1/" |tr '/' ' ')\'

			case '?*'
				set str $str -x
				echo "Don't know how to handle arguments of type '$arg'" >&2
		end

		switch $desc
			case '?*'
				set str $str --description \'$desc\'
		end

		echo complete -c $cmd $condition $str 

	end

end


set cmd $argv[1]; or exit 1

echo '
#
# Completions for the '$cmd' command
# This file was autogenerated by the file make_mercurial_completions.fish
# which is shipped with the fish source code
#

#
# Completions from commandline
#
'
set -e argv[1]

while count $argv >/dev/null
	  echo $argv[1]
	  set -e argv[1]
end


eval "function cmd; $cmd \$argv; end"

set -l cmd_str

switch $cmd
	case svn

		function list_subcommand 
			set cmd1 '\([^ ]*\)'
			set cmd2 '\([^,)]*\)'
			set cmdn '\(, \([^,)]*\)\|\)'
			set svn_re '^   *'$cmd1'\( ('$cmd2$cmdn$cmdn')\|\).*$'
			cmd help|sed -ne 's/'$svn_re'/\1\n\3\n\5\n\7/p'| grep .
		end

		function list_subcommand_help
			set short_exp '\(-.\|\)'
			set long_exp '\(--[^ =,]*\)'
			set arg_exp '\(\|[= ][^ ][^ ]*\)'
			set desc_exp '\([\t ]*:[\t ]*\|\)\([^ ].*[^.]\)'
			set re "^ *$short_exp  *$long_exp$arg_exp  *$desc_exp\(\|\\.\)\$"
			cmd help $argv | sed -n -e 's/'$re'/\1\t\2\t\3\t\5/p'
		end


		for i in (list_subcommand)
			set desc (cmd help $i|head -n 1|sed -e 's/[^:]*: *\(.*[^.]\)\(\|\\.\)$/\1/')
			set desc (esc $desc)
			set cmd_str $cmd_str "-a $i --description '$desc'"
		end

	case cvs

		function list_subcommand 
		 	cmd --help-commands 2>| sed -n -e 's/^  *\([^ ][^ ]*\) .*$/\1/p'
		end

		set short_exp '\(-.\)'
		set arg_exp '\(\| [^ \t][^ \t]*\)'
		set desc_exp '\([\t ]*:[\t ]*\|\)\([^ ].*\)'
		set re '^[ \t]*'$short_exp$arg_exp'[ \t]*'$desc_exp'$'

		function list_subcommand_help
#'s/^[ \t]*\(-.\)[ \t]\([^- \t][^ \t]*\)*[ \t]*\([^-].*\)$/\1\t\2\t\3/p'

			cmd -H $argv 2>| sed -n -e 's/'$re'/\1\t\t\2\t\4/p' 
		end

		echo '
#
# Global switches
#
'

		complete_from_list "-n '__fish_use_subcommand'" (cmd --help-options 2>| sed -n -e 's/'$re'/\1\t\t\2\t\4/p')

		set cmd_str_internal (cmd --help-commands 2>| sed -n -e 's/^  *\([^ ][^ ]*\)[\t ]*\([^ ].*\)$/\1\t\2/p')
		for i in $cmd_str_internal
			set exploded (echo $i|tr \t \n)
			set cmd_str $cmd_str "-a $exploded[1] --description '"(esc $exploded[2])"'"		 
		end

	case '*'

		function list_subcommand 
		 	cmd help | sed -n -e 's/^  *\([^ ][^ ]*\) .*$/\1/p'
		end

		function list_subcommand_help
			set short_exp '\(-.\|\)\( [^ -][^ ]*\|\)'
			set long_exp '\(--[^ =,]*\)'
			set arg_exp '\(\|[= ][^ ][^ ]*\)'
			set desc_exp '\([\t ]*:[\t ]*\|\)\([^ ].*[^.]\)'
			set re "^ *$short_exp  *$long_exp$arg_exp  *$desc_exp\(\|\\.\)\$"

			cmd help $argv | sed -n -e 's/'$re'/\1\t\3\t\4\t\6/p'
		end

		set cmd_str (cmd help | sed -n -e 's/^  *\([^ ][^ ]*\)[\t ]*\([^ ].*[^.]\)\(\|\\.\)$/-a \1 --description \'\2\'/p')

end

echo '
#
# subcommands
#
'

printf "complete -c $cmd -n '__fish_use_subcommand' -x %s\n" $cmd_str

for i in (list_subcommand)

	echo '

#
# Completions for the \''$i'\' subcommand
#
'

	complete_from_list "-n 'contains \\'$i\\' (commandline -poc)'" (list_subcommand_help $i)
end

echo \n\n