aboutsummaryrefslogtreecommitdiffhomepage
path: root/init/fish_complete.fish.in
blob: ac8b405667c4371233fcbd9354535cdcd2d66cc7 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Main file for fish command completions. This file contains various
# common helper functions for the command completions. All actual
# completions are located in the completions subdirectory.
#
# @configure_input@

#
# Don't need completions in non-interactive mode
#

if not status --is-interactive
	exit
end

set -g fish_complete_path @SYSCONFDIR@/fish.d/completions ~/.fish.d/completions

# Knowing the location of the whatis database speeds up command
# description lookup.

for i in /var/cache/man/{whatis,windex} /usr{,/local}{,/share}/man/{whatis,windex}
	if test -f $i
		set -g __fish_whatis_path $i
		break
	end
end

#
# Convenience functions
#
# The naming heuristic is that __fish_complete_* prints completions
# and descriptions, while __fish_print_* only prints the completion,
# without the description
#

#
# Find files that complete $argv[1], has the suffix $argv[2], and
# output them as completions with description $argv[3]
#

function __fish_complete_suffix -d "Complete using files"

	set -- comp $argv[1]
	set -- suff $argv[2]
	set -- desc $argv[3]

	set -- base (echo $comp |sed -e 's/\.[a-zA-Z0-9]*$//')
	eval "set -- files $base*$suff"

	if test $files[1]
		printf "%s\t$desc\n" $files
	end

	#
	# Also do directory completion, since there might be files
	# with the correct suffix in a subdirectory
	#

	__fish_complete_directory $comp

end

#
# Find directories that complete $argv[1], output them as completions
# with description $argv[2] if defined, otherwise use 'Directory'
#

function __fish_complete_directory -d "Complete using directories"

	set -- comp $argv[1]
	set -- desc (_ Directory)

	if test (count $argv) -gt 1
		set desc $argv[2]
	end

	eval "set -- dirs "$comp"*/"

	if test $dirs[1]
		printf "%s\t$desc\n" $dirs
	end

end

function __fish_complete_users -d "Print a list of local users, with the real user name as a description"
	cat /etc/passwd | sed -e "s/^\([^:]*\):[^:]*:[^:]*:[^:]*:\([^:]*\):.*/\1\t\2/"
end

function __fish_complete_groups -d "Print a list of local groups, with group members as the description"
	cat /etc/group | sed -e "s/^\([^:]*\):[^:]*:[^:]*:\(.*\)/\1\tMembers: \2/"
end

function __fish_complete_pids -d "Print a list of process identifiers along with brief descriptions"
	# This may be a bit slower, but it's nice - having the tty displayed is really handy
	ps --no-heading -o pid,comm,tty --ppid %self -N | sed -r 's/ *([0-9]+) +([^ ].*[^ ]|[^ ]) +([^ ]+)$/\1\t\2 [\3]/' ^/dev/null

	# If the above is too slow, this is faster but a little less useful
	#	pgrep -l -v -P %self | sed 's/ /\t/'
end

function __fish_complete_command -d "Complete using all available commands"
	printf "%s\n" (commandline -ct)(complete -C (commandline -ct))
end

function __fish_complete_subcommand  -d "Complete subcommand"
	set -l res ""
	set -l had_cmd 0
	set -l cmd (commandline -cop) (commandline -ct)
	set -l skip_next 1

	for i in $cmd

		if test "$skip_next" = 1
			set skip_next 0
			continue
		end

		if test "$had_cmd" = 1
			set res "$res $i"
		else

			if contains -- $i $argv
				set skip_next 1
				continue
			end

			switch $i
				case '-*'
					 
				case '*'
					set had_cmd 1
					set res $i
			end
		end
	end
	
	printf "%s\n" (commandline -ct)(complete -C $res)

end



function __fish_print_hostnames -d "Print a list of known hostnames"

	# Print all hosts from /etc/hosts
	if test -f /etc/hosts
		sed </etc/hosts -e 's/[0-9.]*\( \|\t\)*\(.*\)/\2/'|sed -e 's/\#.*//'|tr \t \n |grep -v '^$'
	end
	# Print nfs servers from /etc/fstab
	if test -f /etc/fstab
		grep </etc/fstab "^\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\|[a-zA-Z.]*\):"|cut -d : -f 1
	end

	# Print hosts with known ssh keys
	cat ~/.ssh/known_hosts{,2} ^/dev/null|cut -d ' ' -f 1| cut -d , -f 1
end

function __fish_print_interfaces -d "Print a list of known network interfaces"
	netstat -i -n -a | awk 'NR>2'|awk '{print $1}'
end

function __fish_print_addresses -d "Print a list of known network addresses"
	/sbin/ifconfig |grep 'inet addr'|cut -d : -f 2|cut -d ' ' -f 1
end

function __fish_print_users -d "Print a list of local users"
	cat /etc/passwd | cut -d : -f 1
end


function __fish_contains_opt -d "Checks if a specific option has been given in the current commandline"
	set -l next_short 

	set -l short_opt
	set -l long_opt 

	for i in $argv
		if test $next_short 
			set next_short 
			set -- short_opt $short_opt $i
		else
			switch $i
				case -s
					set next_short 1
				case '-*'
					echo __fish_contains_opt: Unknown option $i
					return 1

				case '**'
					set -- long_opt $long_opt $i
			end
		end
	end

	for i in $short_opt

		if test -z $i
			continue
		end

		if commandline -cpo | grep -- "^-"$i"\|^-[^-]*"$i >/dev/null
			return 0
		end
		
		if commandline -ct | grep -- "^-"$i"\|^-[^-]*"$i >/dev/null
			return 0
		end
	end

	for i in $long_opt
		if test -z $i
			continue
		end

		if contains -- --$i (commandline -cpo)
			return 0
		end
	end

	return 1
end

#
# Completions for the shell and it's builtin commands and functions
#

set -l __fish_help_desc (_ "Display help and exit")
for i in (builtin -n|grep -vE '(while|for|if|function|switch)' )

	complete -c $i -s h -l help -d $__fish_help_desc
end



function __fish_print_packages

	# apt-cache is much, much faster than rpm, and can do this in real
    # time. We use it if available.

	switch (commandline -tc)
		case '-**'
			return
	end

	#Get the word 'Package' in the current language
	set -l package (_ Package)

	if which apt-cache >/dev/null ^/dev/null
		# Apply the following filters to output of apt-cache:
		# 1) Remove package names with parentesis in them, since these seem to not correspond to actual packages as reported by rpm
		# 2) Remove package names that are .so files, since these seem to not correspond to actual packages as reported by rpm
		# 3) Remove path information such as /usr/bin/, as rpm packages do not have paths

		apt-cache --no-generate pkgnames (commandline -tc)|grep -v \( |grep -v '\.so\(\.[0-9]\)*$'|sed -e 's/\/.*\///'|sed -e 's/$/\t'$package'/'
		return
	end

	# Rpm is too slow for this job, so we set it up to do completions
    # as a background job and cache the results.

	if which rpm >/dev/null ^/dev/null

		# If the cache is less than five minutes old, we do not recalculate it

		set cache_file /tmp/.rpm-cache.$USER
			if test -f $cache_file 
			cat $cache_file
			set age (echo (date +%s) - (stat -c '%Y' $cache_file) | bc)
			set max_age 250
			if test $age -lt $max_age
				return
			end
		end

		# Remove package version information from output and pipe into cache file
		rpm -qa >$cache_file |sed -e 's/-[^-]*-[^-]*$//' | sed -e 's/$/\t'$package'/' &
	end

	# This completes the package name from the portage tree. 
	# True for installing new packages. Function for printing 
	# installed on the system packages is in completions/emerge.fish
	if which emerge >/dev/null ^/dev/null
		emerge -s \^(commandline -tc) |grep "^*" |cut -d\  -f3 |cut -d/ -f2
		return
	end

end


function __fish_append -d "Internal completion function for appending string to the commandline"
	set separator $argv[1]
	set -e argv[1]
	set str (commandline -tc| sed -ne "s/\(.*$separator\)[^$separator]*/\1/p"|sed -e "s/--.*=//")
	printf "%s\n" "$str"$argv "$str"(printf "%s\n" $argv|sed -e "s/\(\t\|\$\)/,\1/") 
end

function __fish_gnu_complete -d "Wrapper for the complete builtin. Skips the long completions on non-GNU systems"
	set is_gnu 0

	# Check if we are using a gnu system
	for i in (seq (count $argv))
		switch  $argv[$i]
			
			case -g --is-gnu
				set -e argv[$i]
				set is_gnu 1
				break
		end
	end

	# Remove long option if not on a gnu system
	if test $is_gnu = 0
		for i in (seq (count $argv))
			if test $argv[$i] = -l
				set -e argv[$i]
				set -e argv[$i]
				break
			end
		end
	end
	
	complete $argv

end

function __fish_is_first_token -d 'Test if no non-switch argument has been specified yet'
	set -- cmd (commandline -poc)
	set -e -- cmd[1]
	for i in $cmd
		switch $i
			case '-*'

			case '*'
				return 1;
		end
	end
	return 0
end

function __fish_no_arguments -d "Internal fish function"
	set -l cmd (commandline -poc) (commandline -tc)
	set -e cmd[1]
	for i in $cmd
		switch $i
			case '-*'
				 
			case '*'
				 return 1
		end
	end
	return 0
end

#
# Completions for SysV startup scripts
#

complete -x -p "/etc/init.d/*" -a start\t(_ 'Start service')
complete -x -p "/etc/init.d/*" -a stop\t(_ 'Stop service')
complete -x -p "/etc/init.d/*" -a status\t(_ 'Print service status')
complete -x -p "/etc/init.d/*" -a restart\t(_ 'Stop and then start service')
complete -x -p "/etc/init.d/*" -a reload\t(_ 'Reload service configuration')