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