2015-08-18 10:53:45 +08:00
|
|
|
# System clipboard integration
|
|
|
|
#
|
|
|
|
# This file has support for doing system clipboard copy and paste operations
|
|
|
|
# from the command line in a generic cross-platform fashion.
|
|
|
|
#
|
|
|
|
# On OS X and Windows, the main system clipboard or "pasteboard" is used. On other
|
|
|
|
# Unix-like OSes, this considers the X Windows CLIPBOARD selection to be the
|
|
|
|
# "system clipboard", and the X Windows `xclip` command must be installed.
|
|
|
|
|
|
|
|
# clipcopy - Copy data to clipboard
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# <command> | clipcopy - copies stdin to clipboard
|
|
|
|
#
|
|
|
|
# clipcopy <file> - copies a file's contents to clipboard
|
|
|
|
#
|
2019-07-13 05:16:01 +08:00
|
|
|
#
|
|
|
|
##
|
|
|
|
#
|
2015-08-18 10:53:45 +08:00
|
|
|
# clippaste - "Paste" data from clipboard to stdout
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# clippaste - writes clipboard's contents to stdout
|
|
|
|
#
|
2015-10-04 15:42:24 +08:00
|
|
|
# clippaste | <command> - pastes contents and pipes it to another process
|
|
|
|
#
|
|
|
|
# clippaste > <file> - paste contents to a file
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# # Pipe to another process
|
|
|
|
# clippaste | grep foo
|
|
|
|
#
|
|
|
|
# # Paste to a file
|
|
|
|
# clippaste > file.txt
|
2019-07-13 05:16:01 +08:00
|
|
|
#
|
|
|
|
function detect-clipboard() {
|
2015-08-18 10:53:45 +08:00
|
|
|
emulate -L zsh
|
2019-07-13 05:16:01 +08:00
|
|
|
|
2015-08-18 10:53:45 +08:00
|
|
|
if [[ $OSTYPE == darwin* ]]; then
|
2019-07-13 05:16:01 +08:00
|
|
|
function clipcopy() { pbcopy < "${1:-/dev/stdin}"; }
|
|
|
|
function clippaste() { pbpaste; }
|
2015-08-18 10:53:45 +08:00
|
|
|
elif [[ $OSTYPE == cygwin* ]]; then
|
2019-07-13 05:16:01 +08:00
|
|
|
function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; }
|
|
|
|
function clippaste() { cat /dev/clipboard; }
|
|
|
|
elif (( $+commands[xclip] )); then
|
|
|
|
function clipcopy() { xclip -in -selection clipboard < "${1:-/dev/stdin}"; }
|
|
|
|
function clippaste() { xclip -out -selection clipboard; }
|
|
|
|
elif (( $+commands[xsel] )); then
|
|
|
|
function clipcopy() { xsel --clipboard --input < "${1:-/dev/stdin}"; }
|
|
|
|
function clippaste() { xsel --clipboard --output; }
|
2015-08-18 10:53:45 +08:00
|
|
|
else
|
2019-07-13 05:16:01 +08:00
|
|
|
function _retry_clipboard_detection_or_fail() {
|
|
|
|
local clipcmd="${1}"; shift
|
|
|
|
if detect-clipboard; then
|
|
|
|
"${clipcmd}" "$@"
|
|
|
|
else
|
|
|
|
print "${clipcmd}: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; }
|
|
|
|
function cilppaste() { _retry_clipboard_detection_or_fail clippaste "$@"; }
|
|
|
|
return 1
|
2015-08-18 10:53:45 +08:00
|
|
|
fi
|
2015-10-04 15:42:24 +08:00
|
|
|
}
|
2019-07-13 05:16:01 +08:00
|
|
|
|
|
|
|
# Detect at startup. A non-zero exit here indicates that the dummy clipboards were set,
|
|
|
|
# which is not really an error. If the user calls them, they will attempt to redetect
|
|
|
|
# (for example, perhaps the user has now installed xclip) and then either print an error
|
|
|
|
# or proceed successfully.
|
|
|
|
detect-clipboard || true
|