-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzsh-hardcopy
81 lines (69 loc) · 2.48 KB
/
zsh-hardcopy
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
# Sections of, binding changed
# http://git.grml.org/?p=grml-etc-core.git : 78d890c776564875326d0ab41e8ed80ee1c6c0ab
# NON-FREEEEEEEEEEEEEEEEEEEEEEE
# utility functions
# this function checks if a command exists and returns either true
# or false. This avoids using 'which' and 'whence', which will
# avoid problems with aliases for which on certain weird systems. :-)
# Usage: check_com [-c|-g] word
# -c only checks for external commands
# -g does the usual tests and also checks for global aliases
check_com() {
emulate -L zsh
local -i comonly gatoo
if [[ $1 == '-c' ]] ; then
(( comonly = 1 ))
shift
elif [[ $1 == '-g' ]] ; then
(( gatoo = 1 ))
else
(( comonly = 0 ))
(( gatoo = 0 ))
fi
if (( ${#argv} != 1 )) ; then
printf 'usage: check_com [-c] <command>\n' >&2
return 1
fi
if (( comonly > 0 )) ; then
[[ -n ${commands[$1]} ]] && return 0
return 1
fi
if [[ -n ${commands[$1]} ]] \
|| [[ -n ${functions[$1]} ]] \
|| [[ -n ${aliases[$1]} ]] \
|| [[ -n ${reswords[(r)$1]} ]] ; then
return 0
fi
if (( gatoo > 0 )) && [[ -n ${galiases[$1]} ]] ; then
return 0
fi
return 1
}
## complete word from currently visible Screen or Tmux buffer.
if check_com -c screen || check_com -c tmux; then
_complete_screen_display() {
[[ "$TERM" != "screen" ]] && return 1
local TMPFILE=$(mktemp)
local -U -a _screen_display_wordlist
trap "rm -f $TMPFILE" EXIT
# fill array with contents from screen hardcopy
if ((${+TMUX})); then
#works, but crashes tmux below version 1.4
#luckily tmux -V option to ask for version, was also added in 1.4
tmux -V &>/dev/null || return
tmux -q capture-pane \; save-buffer -b 0 $TMPFILE \; delete-buffer -b 0
else
screen -X hardcopy $TMPFILE
# screen sucks, it dumps in latin1, apparently always. so recode it
# to system charset
check_com recode && recode latin1 $TMPFILE
fi
_screen_display_wordlist=( ${(QQ)$(<$TMPFILE)} )
# remove PREFIX to be completed from that array
_screen_display_wordlist[${_screen_display_wordlist[(i)$PREFIX]}]=""
compadd -a _screen_display_wordlist
}
#k# complete word from currently visible GNU screen buffer
bindkey -r "^xs"
compdef -k _complete_screen_display complete-word '^xs'
fi