98 lines
		
	
	
	
		
			2.4 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable file
		
	
	
	
	
			
		
		
	
	
			98 lines
		
	
	
	
		
			2.4 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable file
		
	
	
	
	
| #!/bin/zsh
 | |
| #dep:xclip
 | |
| 
 | |
| # install this program with the name xc and symlink it as xs.
 | |
| # xc will use the clipboard selection, while xs while use the primary selection
 | |
| read -r -d '' HELPTEXT <<HELP
 | |
| Usage: xc [COMMAND]
 | |
| 	   xc fromxs|toxs
 | |
| 	   xs [COMMAND]
 | |
| 	   xs fromxc|toxc
 | |
| 
 | |
| The used X selection is determined by the name the program is called as:
 | |
| 	xc: clipboard (Ctrl-C / Ctrl-V)
 | |
| 	xs: primary selection (middle mouse button)
 | |
| For showboth/readboth, the action is done for both selections.
 | |
| 
 | |
| Commands:
 | |
| 	show/showboth:  show contents of clipboard
 | |
| 	                (default if no command given)
 | |
| 	read/readboth:  store input to clipboard
 | |
| 	                (default if stdin is not a terminal)
 | |
| 	sed:            modify contents using a sed expression
 | |
| 	pipe:           modify contents by piping through a command
 | |
| 	edit:           edit clipboard contents using \$EDITOR
 | |
| 
 | |
| 	fromxs/toxs:    xc only: copy contents from or to primary selection
 | |
| 	fromxc/toxc:    xs only: copy contents from or to clipboard
 | |
| HELP
 | |
| 
 | |
| 
 | |
| source ${$(realpath "$0"):h:h}/lib/common.zsh
 | |
| 
 | |
| function get_primary() { xclip -o -selection primary }
 | |
| function get_clipboard() { xclip -o -selection clipboard }
 | |
| function set_primary() { xclip -i -selection primary }
 | |
| function set_clipboard() { xclip -i -selection clipboard }
 | |
| function show_both() {
 | |
| 	printf "\e[1;94mPrimary\e[0m\n"
 | |
| 	get_primary
 | |
| 	echo
 | |
| 	printf "\e[1;94mClipboard\e[0m\n"
 | |
| 	get_clipboard
 | |
| }
 | |
| 
 | |
| function read_both() {
 | |
| 	depend pee
 | |
| 	pee "xclip -i -selection primary" "xclip -i -selection clipboard"
 | |
| }
 | |
| 
 | |
| function common() {
 | |
| 	cmd=$1
 | |
| 	getc=$2
 | |
| 	setc=$3
 | |
| 	shift 3
 | |
| 	case "$cmd" in
 | |
| 		"read") $setc;;
 | |
| 		"clear") echo -n | $setc;;
 | |
| 		"readboth") read_both ;;
 | |
| 		"showboth") show_both ;;
 | |
| 		"sed")
 | |
| 			res=$($getc | sed -e "$@")
 | |
| 			if [[ $? == 0 ]]; then
 | |
| 				echo $res | $setc; $getc
 | |
| 			fi
 | |
| 			;;
 | |
| 		"pipe") $getc | "$@" | $setc; $getc;;
 | |
| 		"edit") depend vipe; $getc | vipe | $setc; $getc;;
 | |
| 		"") if [[ -t 0 ]]; then $getc; else $setc; fi;;
 | |
| 		*)
 | |
| 			if [[ -f "$cmd" ]]; then
 | |
| 				$setc < $cmd
 | |
| 			else
 | |
| 				echo $HELPTEXT
 | |
| 			fi
 | |
| 			;;
 | |
| 		esac
 | |
| }
 | |
| 
 | |
| cmd=$1
 | |
| [[ -n $1 ]] && shift
 | |
| 
 | |
| case "${0:t}" in
 | |
| 	xc)
 | |
| 		case "$cmd" in
 | |
| 			"fromxs"|"fromXS") get_primary | set_clipboard ;;
 | |
| 			"toxs"|"toXS") get_clipboard | set_primary ;;
 | |
| 			*) common "$cmd" get_clipboard set_clipboard "$@";;
 | |
| 		esac
 | |
| 		;;
 | |
| 	xs)
 | |
| 		case "$cmd" in
 | |
| 			"fromxc"|"fromXC") get_clipboard | set_primary ;;
 | |
| 			"toxc"|"toXC") get_primary | set_clipboard ;;
 | |
| 			*) common "$cmd" get_primary set_primary "$@";;
 | |
| 		esac
 | |
| 		;;
 | |
| esac
 | |
| 
 | 
