vis: make O implementation independent of <Up> mapping
[vis.git] / vis-clipboard
blob8397ee22c1f038b58b71ae7617b9611d87daab65
1 #!/bin/sh
3 # Copyright (C) 2016 Richard Burke, ISC licensed
5 vc_fatal() {
6 echo "$@" >&2
7 exit 1
10 vc_usage() {
11 vc_fatal "`basename $0` [--selection sel] [--usable|--copy|--paste]"
14 vc_determine_command() {
15 if [ -n "$WAYLAND_DISPLAY" ]; then
16 for c in wl-copy wl-paste; do
17 if type "$c" >/dev/null 2>&1; then
18 echo "wlclipboard"
19 return 0
21 done
24 if [ -n "$DISPLAY" ]; then
25 for c in xclip xsel; do
26 if type "$c" >/dev/null 2>&1; then
27 echo "$c"
28 return 0
30 done
33 if [ "$sel" = "primary" ]; then
34 vc_fatal "clipboard primary selection is not supported on your platform"
37 if type pbcopy >/dev/null 2>&1; then
38 echo 'mac'
39 return 0
42 if [ -c /dev/clipboard ]; then
43 echo 'cygwin'
44 return 0
47 return 1
50 vc_usable() {
51 if vc_determine_command >/dev/null 2>&1; then
52 exit 0
55 exit 1
58 vc_copy() {
59 COPY_CMD="`vc_determine_command 2>/dev/null`"
61 if [ $? -ne 0 ] || [ -z "$COPY_CMD" ]; then
62 vc_fatal 'System clipboard not supported'
65 vc_${COPY_CMD}_copy
67 exit $?
70 vc_paste() {
71 PASTE_CMD="`vc_determine_command 2>/dev/null`"
73 if [ $? -ne 0 ] || [ -z "$PASTE_CMD" ]; then
74 vc_fatal 'System clipboard not supported'
77 vc_${PASTE_CMD}_paste
79 exit $?
82 vc_wlclipboard_copy() {
83 if [ "$sel" = "primary" ]; then
84 wl-copy --primary -t TEXT
85 else
86 wl-copy -t TEXT
90 vc_wlclipboard_paste() {
91 if [ "$sel" = "primary" ]; then
92 wl-paste --primary -t text
93 else
94 wl-paste -t text
98 vc_xsel_copy() {
99 xsel --"$sel" -bi
102 vc_xsel_paste() {
103 xsel --"$sel" -bo
106 vc_xclip_copy() {
107 xclip -selection "$sel" -i >/dev/null 2>&1
110 vc_xclip_paste() {
111 xclip -selection "$sel" -o
114 vc_mac_copy() {
115 pbcopy
118 vc_mac_paste() {
119 pbpaste
122 vc_cygwin_copy() {
123 cat >/dev/clipboard
126 vc_cygwin_paste() {
127 cat /dev/clipboard
130 while [ $# -gt 0 ]; do
131 case "$1" in
132 --usable) fn=vc_usable;;
133 --copy) fn=vc_copy;;
134 --paste) fn=vc_paste;;
135 --selection) shift; sel="$1";;
136 *) ;;
137 esac
138 shift
139 done
141 sel=${sel:-"clipboard"} $fn
143 vc_usage