65642dfcffa1bd63470125c17233cfdbb62a6f79

Author
Marc Cornellà <marc.cornella@live.com>
Committer
GitHub <noreply@github.com>
Date

Message

lib: support additional clipboard types (#7996)

* 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.

* 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.

* 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)

* 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.

* clipboard: Fix bad expansion of exit-code test

Diff

This diff is truncated to protect this page.

  1diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh
  2index 5bba11d1642eb59851c3c3e60399ba8e42a00925..6102f33246fc115410b9b86a05a15ff9edccb4ee 100644
  3--- a/lib/clipboard.zsh
  4+++ b/lib/clipboard.zsh
  5@@ -3,10 +3,23 @@
  6 # This file has support for doing system clipboard copy and paste operations
  7 # from the command line in a generic cross-platform fashion.
  8 #
  9-# On OS X and Windows, the main system clipboard or "pasteboard" is used. On other
 10-# Unix-like OSes, this considers the X Windows CLIPBOARD selection to be the
 11-# "system clipboard", and the X Windows `xclip` command must be installed.
 12-
 13+# This is uses essentially the same heuristic as neovim, with the additional
 14+# special support for Cygwin.
 15+# See: https://github.com/neovim/neovim/blob/e682d799fa3cf2e80a02d00c6ea874599d58f0e7/runtime/autoload/provider/clipboard.vim#L55-L121
 16+#
 17+# - pbcopy, pbpaste (macOS)
 18+# - cygwin (Windows running Cygwin)
 19+# - wl-copy, wl-paste (if $WAYLAND_DISPLAY is set)
 20+# - xclip (if $DISPLAY is set)
 21+# - xsel (if $DISPLAY is set)
 22+# - lemonade (for SSH) https://github.com/pocke/lemonade
 23+# - doitclient (for SSH) http://www.chiark.greenend.org.uk/~sgtatham/doit/
 24+# - win32yank (Windows)
 25+# - tmux (if $TMUX is set)
 26+#
 27+# Defines two functions, clipcopy and clippaste, based on the detected platform.
 28+##
 29+#
 30 # clipcopy - Copy data to clipboard
 31 #
 32 # Usage:
 33@@ -15,41 +28,8 @@
 34 #
 35 #  clipcopy <file>         - copies a file's contents to clipboard
 36 #
 37-function clipcopy() {
 38-  emulate -L zsh
 39-  local file=$1
 40-  if [[ $OSTYPE == darwin* ]]; then
 41-    if [[ -z $file ]]; then
 42-      pbcopy
 43-    else
 44-      cat $file | pbcopy
 45-    fi
 46-  elif [[ $OSTYPE == (cygwin|msys)* ]]; then
 47-    if [[ -z $file ]]; then
 48-      cat > /dev/clipboard
 49-    else
 50-      cat $file > /dev/clipboard
 51-    fi
 52-  else
 53-    if (( $+commands[xclip] )); then
 54-      if [[ -z $file ]]; then
 55-        xclip -in -selection clipboard
 56-      else
 57-        xclip -in -selection clipboard $file
 58-      fi
 59-    elif (( $+commands[xsel] )); then
 60-      if [[ -z $file ]]; then
 61-        xsel --clipboard --input 
 62-      else
 63-        cat "$file" | xsel --clipboard --input
 64-      fi
 65-    else
 66-      print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
 67-      return 1
 68-    fi
 69-  fi
 70-}
 71-
 72+##
 73+#
 74 # clippaste - "Paste" data from clipboard to stdout
 75 #
 76 # Usage:
 77@@ -67,20 +47,55 @@ function clipcopy() {
 78 #
 79 #   # Paste to a file
 80 #   clippaste > file.txt
 81-function clippaste() {
 82+#
 83+function detect-clipboard() {
 84   emulate -L zsh
 85-  if [[ $OSTYPE == darwin* ]]; then
 86-    pbpaste
 87-  elif [[ $OSTYPE == (cygwin|msys)* ]]; then
 88-    cat /dev/clipboard
 89+
 90+  if [[ "${OSTYPE}" == darwin* ]] && (( ${+commands[pbcopy]} )) && (( ${+commands[pbpaste]} )); then
 91+    function clipcopy() { pbcopy < "${1:-/dev/stdin}"; }
 92+    function clippaste() { pbpaste; }
 93+  elif [[ "${OSTYPE}" == (cygwin|msys)* ]]; then
 94+    function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; }
 95+    function clippaste() { cat /dev/clipboard; }
 96+  elif [ -n "${WAYLAND_DISPLAY:-}" ] && (( ${+commands[wl-copy]} )) && (( ${+commands[wl-paste]} )); then
 97+    function clipcopy() { wl-copy < "${1:-/dev/stdin}"; }
 98+    function clippaste() { wl-paste; }
 99+  elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xclip]} )); then
100+    function clipcopy() { xclip -in -selection clipboard < "${1:-/dev/stdin}"; }
101+    function clippaste() { xclip -out -selection clipboard; }
102+  elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xsel]} )); then
103+    function clipcopy() { xsel --clipboard --input < "${1:-/dev/stdin}"; }
104+    function clippaste() { xsel --clipboard --output; }