From d855547661ee4173bd01ab89ad18418d4dbf508a Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Fri, 12 Jul 2019 17:01:10 -0400 Subject: [PATCH 1/6] clipboard: Reduce unnecessary special-casing on stdin Ideally the parameter would just be removed-users could always just do "clipcopy < some-file". but removing the parameter would break backwards compatibility. In any case, this simplifies the logic considerably. --- lib/clipboard.zsh | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh index 2c93d1bb5..15ad6d916 100644 --- a/lib/clipboard.zsh +++ b/lib/clipboard.zsh @@ -17,32 +17,17 @@ # function clipcopy() { emulate -L zsh - local file=$1 + local file="${1:-/dev/stdin}" + if [[ $OSTYPE == darwin* ]]; then - if [[ -z $file ]]; then - pbcopy - else - cat $file | pbcopy - fi + pbcopy < "${file}" elif [[ $OSTYPE == cygwin* ]]; then - if [[ -z $file ]]; then - cat > /dev/clipboard - else - cat $file > /dev/clipboard - fi + cat "${file}" > /dev/clipboard else if (( $+commands[xclip] )); then - if [[ -z $file ]]; then - xclip -in -selection clipboard - else - xclip -in -selection clipboard $file - fi + xclip -in -selection clipboard < "${file}" elif (( $+commands[xsel] )); then - if [[ -z $file ]]; then - xsel --clipboard --input - else - cat "$file" | xsel --clipboard --input - fi + xsel --clipboard --input < "${file}" else print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2 return 1 From 956ca639bba5780dc6272e76b0507c67e93c098b Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Fri, 12 Jul 2019 17:16:01 -0400 Subject: [PATCH 2/6] clipboard: Avoid unnecessary re-detection each time Previously, OS detection would happen on each invocation. This makes it happen once (unless it fails, in which case it will try again on the next invocation). This has the additional benefit of localizing the platform-specific checks and commands, too, versus spreading them out in separate functions. --- lib/clipboard.zsh | 65 +++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh index 15ad6d916..512a5a248 100644 --- a/lib/clipboard.zsh +++ b/lib/clipboard.zsh @@ -15,26 +15,9 @@ # # clipcopy - copies a file's contents to clipboard # -function clipcopy() { - emulate -L zsh - local file="${1:-/dev/stdin}" - - if [[ $OSTYPE == darwin* ]]; then - pbcopy < "${file}" - elif [[ $OSTYPE == cygwin* ]]; then - cat "${file}" > /dev/clipboard - else - if (( $+commands[xclip] )); then - xclip -in -selection clipboard < "${file}" - elif (( $+commands[xsel] )); then - xsel --clipboard --input < "${file}" - else - print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2 - return 1 - fi - fi -} - +# +## +# # clippaste - "Paste" data from clipboard to stdout # # Usage: @@ -52,20 +35,40 @@ function clipcopy() { # # # Paste to a file # clippaste > file.txt -function clippaste() { +# +function detect-clipboard() { emulate -L zsh + if [[ $OSTYPE == darwin* ]]; then - pbpaste + function clipcopy() { pbcopy < "${1:-/dev/stdin}"; } + function clippaste() { pbpaste; } elif [[ $OSTYPE == cygwin* ]]; then - cat /dev/clipboard + 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; } else - if (( $+commands[xclip] )); then - xclip -out -selection clipboard - elif (( $+commands[xsel] )); then - xsel --clipboard --output - else - print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2 - return 1 - fi + 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 fi } + +# 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 From 01e934d6346a93acd6b4343de3256d5ba82c5961 Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Fri, 12 Jul 2019 17:39:16 -0400 Subject: [PATCH 3/6] clipboard: Add support for several more clipboards This implements essentially the same heuristic as neovim, with the additional (existing) special support for Cygwin. See: https://github.com/neovim/neovim/blob/e682d799fa3cf2e80a02d00c6ea874599d58f0e7/runtime/autoload/provider/clipboard.vim#L55-L121 - pbcopy, pbpaste (macOS) - cygwin (Windows running Cygwin) - wl-copy, wl-paste (if $WAYLAND_DISPLAY is set) - xclip (if $DISPLAY is set) - xsel (if $DISPLAY is set) - lemonade (for SSH) https://github.com/pocke/lemonade - doitclient (for SSH) http://www.chiark.greenend.org.uk/~sgtatham/doit/ - win32yank (Windows) - tmux (if $TMUX is set) --- lib/clipboard.zsh | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh index 512a5a248..17d8d1aaf 100644 --- a/lib/clipboard.zsh +++ b/lib/clipboard.zsh @@ -3,10 +3,23 @@ # 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. - +# This is uses essentially the same heuristic as neovim, with the additional +# special support for Cygwin. +# See: https://github.com/neovim/neovim/blob/e682d799fa3cf2e80a02d00c6ea874599d58f0e7/runtime/autoload/provider/clipboard.vim#L55-L121 +# +# - pbcopy, pbpaste (macOS) +# - cygwin (Windows running Cygwin) +# - wl-copy, wl-paste (if $WAYLAND_DISPLAY is set) +# - xclip (if $DISPLAY is set) +# - xsel (if $DISPLAY is set) +# - lemonade (for SSH) https://github.com/pocke/lemonade +# - doitclient (for SSH) http://www.chiark.greenend.org.uk/~sgtatham/doit/ +# - win32yank (Windows) +# - tmux (if $TMUX is set) +# +# Defines two functions, clipcopy and clippaste, based on the detected platform. +## +# # clipcopy - Copy data to clipboard # # Usage: @@ -15,7 +28,6 @@ # # clipcopy - copies a file's contents to clipboard # -# ## # # clippaste - "Paste" data from clipboard to stdout @@ -39,18 +51,33 @@ function detect-clipboard() { emulate -L zsh - if [[ $OSTYPE == darwin* ]]; then + if [[ "${OSTYPE}" == darwin* ]] && (( ${+commands[pbcopy]} )) && (( ${+commands[pbpaste]} )); then function clipcopy() { pbcopy < "${1:-/dev/stdin}"; } function clippaste() { pbpaste; } - elif [[ $OSTYPE == cygwin* ]]; then + elif [[ "${OSTYPE}" == cygwin* ]]; then function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; } function clippaste() { cat /dev/clipboard; } - elif (( $+commands[xclip] )); then + elif [ -n "${WAYLAND_DISPLAY:-}" ] && (( ${+commands[wl-copy]} )) && (( ${+commands[wl-paste]} )); then + function clipcopy() { wl-copy < "${1:-/dev/stdin}"; } + function clippaste() { wl-paste; } + elif [ -n "${DISPLAY:-}" ] && (( ${+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}"; } + elif [ -n "${DISPLAY:-}" ] && $(( ${+commands[xsel]} )); then + function clipcopy() { xsel --clipboard --input < "${1:-/dev/stdin}"; } function clippaste() { xsel --clipboard --output; } + elif (( ${+commands[lemonade]} )); then + function clipcopy() { lemonade copy < "${1:-/dev/stdin}"; } + function clippaste() { lemonade paste; } + elif (( ${+commands[doitclient]} )); then + function clipcopy() { doitclient wclip < "${1:-/dev/stdin}"; } + function clippaste() { doitclient wclip -r; } + elif (( ${+commands[win32yank]} )); then + function clipcopy() { win32yank -i < "${1:-/dev/stdin}"; } + function clippaste() { win32yank -o; } + elif [ -n "${TMUX:-}" ] && (( ${+commands[tmux]} )); then + function clipcopy() { tmux load-buffer "${1:-/dev/stdin}"; } + function clippaste() { tmux save-buffer -; } else function _retry_clipboard_detection_or_fail() { local clipcmd="${1}"; shift From d71d3d99050fa2b658f41602b830c971235b9202 Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Sun, 14 Jul 2019 12:52:47 -0400 Subject: [PATCH 4/6] clipboard: Fix "cilppaste" -> "clippaste" typo --- lib/clipboard.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh index 17d8d1aaf..671d33d8e 100644 --- a/lib/clipboard.zsh +++ b/lib/clipboard.zsh @@ -89,7 +89,7 @@ function detect-clipboard() { fi } function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; } - function cilppaste() { _retry_clipboard_detection_or_fail clippaste "$@"; } + function clippaste() { _retry_clipboard_detection_or_fail clippaste "$@"; } return 1 fi } From 841008c947d5fbe780111539804945c46dcdfaf4 Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Sun, 14 Jul 2019 12:53:27 -0400 Subject: [PATCH 5/6] clipboard: Fix tmux clipcopy after testing Tmux must have special handling for /dev/stdin since it's managing the terminal itself. This was tested with tmux-2.9a on macOS. --- lib/clipboard.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh index 671d33d8e..5001f4695 100644 --- a/lib/clipboard.zsh +++ b/lib/clipboard.zsh @@ -76,7 +76,7 @@ function detect-clipboard() { function clipcopy() { win32yank -i < "${1:-/dev/stdin}"; } function clippaste() { win32yank -o; } elif [ -n "${TMUX:-}" ] && (( ${+commands[tmux]} )); then - function clipcopy() { tmux load-buffer "${1:-/dev/stdin}"; } + function clipcopy() { tmux load-buffer "${1:--}"; } function clippaste() { tmux save-buffer -; } else function _retry_clipboard_detection_or_fail() { From d81cd753e0b3a845e8f3549da245dbad102a6e4c Mon Sep 17 00:00:00 2001 From: Robert Estelle Date: Sun, 14 Jul 2019 12:56:48 -0400 Subject: [PATCH 6/6] clipboard: Fix bad expansion of exit-code test --- lib/clipboard.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh index 5001f4695..333f58dd8 100644 --- a/lib/clipboard.zsh +++ b/lib/clipboard.zsh @@ -63,7 +63,7 @@ function detect-clipboard() { elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xclip]} )); then function clipcopy() { xclip -in -selection clipboard < "${1:-/dev/stdin}"; } function clippaste() { xclip -out -selection clipboard; } - elif [ -n "${DISPLAY:-}" ] && $(( ${+commands[xsel]} )); then + elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xsel]} )); then function clipcopy() { xsel --clipboard --input < "${1:-/dev/stdin}"; } function clippaste() { xsel --clipboard --output; } elif (( ${+commands[lemonade]} )); then