#!/bin/sh - # cutbuf - print/save/rotate the contents of X11 cut buffers # Steve Kinzler, kinzler@cs.indiana.edu, Oct 98/Oct 04/Aug 08/Jan 10/Nov 10 # http://www.cs.indiana.edu/~kinzler/home.html#x11 z=; op=; N=0 case "$1" in -z) z="$1"; shift;; esac case "$1" in -[oOipP]|-io) op="$1"; shift;; -[awdus]) op="$1"; shift; N=8;; -f) op="$1"; shift;; --) shift;; -*) op=bad;; esac case "$op" in '') case "$#,$1" in 0,|1,[0-9]*) op=-o; test ! -t 0 -a -t 1 && op=-i;; *) op=-f;; esac esac case "$op,$1" in -f,*|*,) ;; *,[0-9]*) N="$1"; shift;; *) op=bad;; esac case "$op" in bad) cat <&2; exit 1;; cutbuf [ -z ] [ -o | -O | -i | -p | -P | -a | -io | -d | -u | -s ] [ # ] cutbuf [ -z ] [ -f ] [ file ... ] -o output the given cut buffer # (default) -O ditto but if empty then output the primary selection -i input stdin to the given cut buffer # (default if piped to) -p input primary selection to the given cut buffer # -P output primary selection -a input stdin lines to cut buffers 0 thru # (max, default 8) -w input stdin words to cut buffers 0 thru # (max, default 8) -io pass through stdin to stdout while saving to given cut buffer -d rotate the first # (dflt 8) cut buffers down/plus one position -u rotate the first # (dflt 8) cut buffers up/minus one position -s swap cut buffers 0 and 1 using the given cut buffer # (dflt 8) -f input any given files to cut buffers 0 thru the number of files -z do not input to a cut buffer if the input is empty The default cut buffer is 0. Final newlines are not saved. Flag ordering is significant. -a, -w and -f will start at \$CUTBUF_BASE instead of 0 if defined. EOF esac case "$op" in -o) (xcb -V) > /dev/null 2>&1 && exec xcb -p "$N" xprop -root -notype "CUT_BUFFER$N" | perl -0e 's/\n//g; s/^[^=]*=\s*"?//; s/"$//; s/\\\\/\001/g; s/\\t/\t/g; s/\\n/\n/g; s/\\"/"/g; s/\001/\\/g; print';; -O) case `"$0" -o "$N"` in '') exec "$0" -P;; *) exec "$0" -o "$N";; esac;; -i) case "$z" in ?*) in="`perl -0pe chomp`"; test -z "$in" && exit esac case "$z" in ?*) echo "$in";; *) perl -0pe chomp;; esac | xcb -s "$N";; -p) #xcb -S "$N";; # this doesn't work on alembic RHEL5.4 despite # ForwardX11Trusted yes; it may be an X server bug "$0" -P | "$0" $z -i "$N";; -P) # perl KLUDGE to try to fix multi-byte high chars in xsel output xsel -l /dev/null | perl -pe 's/\303([\200-\277])/chr(ord($1) + 0100)/ge; s/\302([\200-\277])/$1/g';; -a) case "$CUTBUF_BASE" in [0-9]|[1-9][0-9]) n=$CUTBUF_BASE; N=`expr "$N" + $CUTBUF_BASE`;; *) n=0;; esac while read line do test "$n" -gt "$N" && break test -n "$z" -a -z "$line" || echo "$line" | tr -d '\012' | xcb -s "$n" n=`expr "$n" + 1` done;; -w) perl -pe 's/^\s+//; s/\s+$//; s/\s+/\n/g; s/.$/$&\n/' | "$0" $z -a "$N";; -io) "$0" -i $z "$N"; exec "$0" -o "$N";; -d) exec xcb -n "$N" -r 1;; -u) exec xcb -n "$N" -r -1;; -s) xcb -p 0 | xcb -s "$N" xcb -p 1 | xcb -s 0 xcb -p "$N" | xcb -s 1 xcb -s "$N" < /dev/null;; -f) case "$CUTBUF_BASE" in [0-9]|[1-9][0-9]) n=$CUTBUF_BASE;; *) n=0;; esac for file do test -n "$z" -a ! -s "$file" || xcb -s "$n" < "$file" n=`expr "$n" + 1` done;; esac