Ensure the CWD precedes PROFILE_HOME in new CDPATH.
[profile.git] / .profile.d / completion.bashrc
1 # Old bash doesn't have completion support.
2 if ! builtin complete 2>&1 | grep 'not a shell builtin' >/dev/null; then
3
4 # Helper!
5 COMPLETION_DIR_SSH=${PROFILE_HOME:-~}/.ssh/hosts
6 COMPLETION_DIR_TELNET=${PROFILE_HOME:-~}/.telnet/hosts
7 COMPLETION_DIR_RDP=${PROFILE_HOME:-~}/.rdp/hosts
8 COMPLETION_DIR_PING="$COMPLETION_DIR_SSH $COMPLETION_DIR_TELNET $COMPLETION_DIR_RDP"
9
10 function _generic_completion() {
11   COMPLETION_DIR=${1+"$@"}
12   COMPREPLY=()
13   cur=${COMP_WORDS[COMP_CWORD]}
14
15   HOSTS="$(find $COMPLETION_DIR -type f -perm -100 2>/dev/null | sed 's@.*/@@')"
16
17   COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
18   return 0
19 }
20
21 # Process completion for killall.
22 function _process() {
23   cur=${COMP_WORDS[COMP_CWORD]}
24
25   PROCESSES="$(ps agx --no-heading | awk '{ print $5 }' | sed 's@.*/@@' | sort | uniq)"
26
27   COMPREPLY=($(compgen -W "$PROCESSES" -- "$cur"))
28   return 0
29 }
30
31 # Host completion for SSH known hosts.
32 function _known_hosts() {
33   cur=${COMP_WORDS[COMP_CWORD]}
34
35   HOSTS=$(sed 's/[      ].*//;s/,/\n/' ~/.ssh/known_hosts)
36
37   COMPREPLY=($(compgen -W "$HOSTS" -- "$cur"))
38   return 0
39 }
40
41 # Process completion for kill, strace etc.
42 function _pid() {
43   cur=${COMP_WORDS[COMP_CWORD]}
44
45   PROCESSES="$(find /proc -name '[0-9]*' -maxdepth 1 2>/dev/null | sed 's@.*/@@')"
46
47   COMPREPLY=($(compgen -W "$PROCESSES" -- "$cur"))
48   return 0
49 }
50
51 # Complete ssh with hostnames from the Window Maker menu.
52 function _ssh() {
53   _generic_completion $COMPLETION_DIR_SSH
54   return 0
55 }
56
57 # Complete telnet with hostnames from the Window Maker menu.
58 function _telnet() {
59   _generic_completion $COMPLETION_DIR_TELNET
60   return 0
61 }
62
63 # Complete rdesktop with hostnames from the Window Maker menu.
64 function _rdp() {
65   _generic_completion $COMPLETION_DIR_RDP
66   return 0
67 }
68
69 # Ping hosts from any of the above lists.
70 function _ping() {
71   _generic_completion $COMPLETION_DIR_PING
72 }
73
74 # Hacked up make rule.
75 function _make() {
76   COMPREPLY=()
77   cur=${COMP_WORDS[COMP_CWORD]}
78
79   RULES="all check clean dep depend install reinstall setup uninstall"
80
81   COMPREPLY=($(compgen -W "$RULES" -- "$cur"))
82   return 0
83 }
84
85 # Look for modules.
86 function _modprobe() {
87   COMPREPLY=()
88   cur=${COMP_WORDS[COMP_CWORD]}
89
90   ver=$KERNEL
91   MODULES="$(find /lib/modules/$ver -name \*.ko | sed -n 's@^.*/\([^/]*\).ko$@\1@p')"
92
93   COMPREPLY=($(compgen -W "$MODULES" -- "$cur"))
94   return 0
95 }
96
97 # Look for loaded modules.
98 function _rmmod() {
99   COMPREPLY=()
100   cur=${COMP_WORDS[COMP_CWORD]}
101
102   MODULES="$(lsmod | sed '1d;s/ .*$//')"
103
104   COMPREPLY=($(compgen -W "$MODULES" -- "$cur"))
105   return 0
106 }
107
108 # Find tags with GNU GLOBAL.
109 function _global() {
110   COMPREPLY=()
111   cur=${COMP_WORDS[COMP_CWORD]}
112
113   COMPREPLY=($(global -c "$cur"))
114   return 0
115 }
116
117 complete -F _ssh rollout
118 complete -F _ssh ssh
119 complete -F _ssh sshterm
120 complete -F _ping telnet
121 complete -F _telnet telnetterm
122 complete -F _rdp rdesktop
123 complete -F _ping ping
124 complete -F _ping traceroute
125 complete -F _ping dnsip
126 complete -F _make make
127 complete -F _process killall
128 complete -F _pid kill
129 complete -F _pid strace
130 complete -F _known_hosts kill_known_host
131 complete -F _known_hosts knh
132 complete -F _modprobe modprobe
133 complete -F _rmmod rmmod
134 complete -F _global global
135
136 fi