-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclipbuffer
executable file
·127 lines (108 loc) · 2.67 KB
/
clipbuffer
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
#!/usr/bin/env bash
#
# Simple X selection manager.
#
# Depends on: autocutsel -fork -selection PRIMARY -buttonup
# Idea from: http://chneukirchen.org/dotfiles/bin/backclip
function Clipbuffer::Daemon {
declare -i i
i=-1
command -- mkdir -p -m 700 -- "$xsels"
printf %d "$$" >"$xsels/$$$DISPLAY.pid"
exec -- xprop -root -spy CUT_BUFFER0 |
while read; do
oi=$i
command -- xorg-selection-primary-out >"$xsels/.$oi"
command -- cmp -s -- "$xsels/.$oi" "$xsels/$oi" ||
command -- mv -- "$xsels/.$oi" "$xsels/$((i = ++i % 32))"
done
}
function Clipbuffer::Kill {
shopt -s nullglob
declare p
for p in "$xsels/"?*"$DISPLAY.pid"; do
p=${p##*/}
command -- pkill -TERM -P "${p%$DISPLAY.pid}"
done
if $opt_clear; then
command -- rm -fr -- "$xsels" 2>/dev/null
else
command -- rm -f -- "$xsels/"?*"$DISPLAY.pid" 2>/dev/null
fi
command -- xorg-selection-clear
}
function Clipbuffer::Clip {
declare clip menu
if command -- test-ctty; then
menu=fzf
else
menu=dmenu2
fi
shopt -s nullglob
shopt -s extglob
printf -v clip %s "$(
# printf '%s\n' "$xsels/"+([0-9]) |
# sort -n |
# xargs -I f sh -c "tr '[:space:]' ' ' < 'f'; echo" |
# nl -w1 -s ' ' |
# tac |
for f in "$xsels/"+([0-9]); do
printf '%d ' "${f##*/}"
command -- tr '[:space:]' ' ' <"$f"
printf '\n'
done |
exec -- sort -nr |
exec -- menu "$menu"
)"
[[ -n $clip ]] &&
if $opt_board; then
# xsel -l /dev/null --clear --clipboard;
command -- xorg-selection-clipboard-in <"$xsels/${clip%% *}"
else
# xsel -l /dev/null --clear --primary;
command -- xorg-selection-primary-in <"$xsels/${clip%% *}"
fi
}
declare -i \
OPTIND=1 \
OPTERR=1
declare \
opt= \
opt_board=false \
opt_clear=false \
opt_daemon=false \
opt_kill=false \
opt_restart=false \
xsels=${TMPDIR:?}/clipbuffer
while getopts :bcdkr opt; do
case $opt in
b)
opt_board=:
;;
c)
opt_clear=:
;;
d)
opt_daemon=:
;;
k)
opt_kill=:
;;
r)
opt_restart=:
;;
esac
done
if $opt_daemon; then
Clipbuffer::Daemon
elif $opt_kill; then
Clipbuffer::Kill
elif $opt_restart; then
[[ -n $DISPLAY ]] ||
return 1
Clipbuffer::Kill
exec -- daemonize "${BASH_SOURCE[0]}" -d
else
Clipbuffer::Clip
fi
# vim: set ft=zsh :