Complete session names
[zsh.git] / .zsh / functions / _tm
1 #compdef tm
2 #autoload
3
4 # Copyright (c) 2013 Joerg Jaspert
5 # Author: Joerg Jaspert <joerg@debian.org>
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # .
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # .
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 zmodload zsh/mapfile
29 local expl state curcontext="$curcontext"
30 _tm_done=0
31 _tm_sess=0
32
33 function _tm_get_sessions {
34 typeset -ag _tm_sessions
35 if out=$(tmux list-sessions -F "#{session_name}" 2>/dev/null); then
36 for sess in ${=out}; do
37 _tm_sessions+="${sess}:Existing session ${sess}"
38 done
39 fi
40 _tm_done=1
41 _tm_sess=$#_tm_sessions
42
43 local TMDIR=${TMDIR:-"${HOME}/.tmux.d"}
44 for file in ${TMDIR}/*; do
45 _tm_sessions+=${file##*/}:${(f)${(f)mapfile[$file]}[1]}
46 done
47 }
48
49 function _tm_action {
50 typeset -a actions
51 if [[ $_tm_sess -ne $(tmux list-sessions|wc -l) ]]; then
52 _tm_get_sessions
53 fi
54 ((_tm_done)) || _tm_get_sessions
55 actions=(
56 'ls:List running sessions'
57 's:Open ssh session to a single host'
58 'ms:Open ssh session to multiple hosts'
59 '-l:List running sessions'
60 '-s:Open ssh session to a single host'
61 '-m:Open ssh session to multiple hosts'
62 '-e:use existing session'
63 ${_tm_sessions[@]}
64 )
65 _describe action actions
66 }
67
68 function _tm_arguments() {
69 case ${words[1]} in
70 s)
71 _arguments '::hostname:_hosts'
72 ;;
73 ms)
74 _arguments -C '*:hostnames:->hosts'
75 ;;
76 *)
77 echo "lala"
78 ;;
79 esac
80 }
81
82 function _tm() {
83 _arguments -s\
84 '-n[Open new multisession even if same exists]' \
85 '-c+[Setup session according to TMDIR file]:_tm_sessions: ' \
86 ':action:_tm_action' \
87 ':session:->session' \
88 '*::arguments:_tm_arguments'
89
90 case $state in
91 hosts)
92 _description hosts expl "hosts"
93 _wanted hosts expl hostname _hosts && return
94 ;;
95 session)
96 _tm_get_sessions
97 ;;
98 esac
99 }
100
101 _tm "$@"