Parent directory

clipboard.zsh

4154 bytes
  1# System clipboard integration
  2#
  3# This file has support for doing system clipboard copy and paste operations
  4# from the command line in a generic cross-platform fashion.
  5#
  6# This is uses essentially the same heuristic as neovim, with the additional
  7# special support for Cygwin.
  8# See: https://github.com/neovim/neovim/blob/e682d799fa3cf2e80a02d00c6ea874599d58f0e7/runtime/autoload/provider/clipboard.vim#L55-L121
  9#
 10# - pbcopy, pbpaste (macOS)
 11# - cygwin (Windows running Cygwin)
 12# - wl-copy, wl-paste (if $WAYLAND_DISPLAY is set)
 13# - xsel (if $DISPLAY is set)
 14# - xclip (if $DISPLAY is set)
 15# - lemonade (for SSH) https://github.com/pocke/lemonade
 16# - doitclient (for SSH) http://www.chiark.greenend.org.uk/~sgtatham/doit/
 17# - win32yank (Windows)
 18# - tmux (if $TMUX is set)
 19#
 20# Defines two functions, clipcopy and clippaste, based on the detected platform.
 21##
 22#
 23# clipcopy - Copy data to clipboard
 24#
 25# Usage:
 26#
 27#  <command> | clipcopy    - copies stdin to clipboard
 28#
 29#  clipcopy <file>         - copies a file's contents to clipboard
 30#
 31##
 32#
 33# clippaste - "Paste" data from clipboard to stdout
 34#
 35# Usage:
 36#
 37#   clippaste   - writes clipboard's contents to stdout
 38#
 39#   clippaste | <command>    - pastes contents and pipes it to another process
 40#
 41#   clippaste > <file>      - paste contents to a file
 42#
 43# Examples:
 44#
 45#   # Pipe to another process
 46#   clippaste | grep foo
 47#
 48#   # Paste to a file
 49#   clippaste > file.txt
 50#
 51function detect-clipboard() {
 52  emulate -L zsh
 53
 54  if [[ "${OSTYPE}" == darwin* ]] && (( ${+commands[pbcopy]} )) && (( ${+commands[pbpaste]} )); then
 55    function clipcopy() { cat "${1:-/dev/stdin}" | pbcopy; }
 56    function clippaste() { pbpaste; }
 57  elif [[ "${OSTYPE}" == (cygwin|msys)* ]]; then
 58    function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; }
 59    function clippaste() { cat /dev/clipboard; }
 60  elif (( $+commands[clip.exe] )) && (( $+commands[powershell.exe] )); then
 61    function clipcopy() { cat "${1:-/dev/stdin}" | clip.exe; }
 62    function clippaste() { powershell.exe -noprofile -command Get-Clipboard; }
 63  elif [ -n "${WAYLAND_DISPLAY:-}" ] && (( ${+commands[wl-copy]} )) && (( ${+commands[wl-paste]} )); then
 64    function clipcopy() { cat "${1:-/dev/stdin}" | wl-copy &>/dev/null &|; }
 65    function clippaste() { wl-paste --no-newline; }
 66  elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xsel]} )); then
 67    function clipcopy() { cat "${1:-/dev/stdin}" | xsel --clipboard --input; }
 68    function clippaste() { xsel --clipboard --output; }
 69  elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xclip]} )); then
 70    function clipcopy() { cat "${1:-/dev/stdin}" | xclip -selection clipboard -in &>/dev/null &|; }
 71    function clippaste() { xclip -out -selection clipboard; }
 72  elif (( ${+commands[lemonade]} )); then
 73    function clipcopy() { cat "${1:-/dev/stdin}" | lemonade copy; }
 74    function clippaste() { lemonade paste; }
 75  elif (( ${+commands[doitclient]} )); then
 76    function clipcopy() { cat "${1:-/dev/stdin}" | doitclient wclip; }
 77    function clippaste() { doitclient wclip -r; }
 78  elif (( ${+commands[win32yank]} )); then
 79    function clipcopy() { cat "${1:-/dev/stdin}" | win32yank -i; }
 80    function clippaste() { win32yank -o; }
 81  elif [[ $OSTYPE == linux-android* ]] && (( $+commands[termux-clipboard-set] )); then
 82    function clipcopy() { cat "${1:-/dev/stdin}" | termux-clipboard-set; }
 83    function clippaste() { termux-clipboard-get; }
 84  elif [ -n "${TMUX:-}" ] && (( ${+commands[tmux]} )); then
 85    function clipcopy() { tmux load-buffer -w "${1:--}"; }
 86    function clippaste() { tmux save-buffer -; }
 87  else
 88    function _retry_clipboard_detection_or_fail() {
 89      local clipcmd="${1}"; shift
 90      if detect-clipboard; then
 91        "${clipcmd}" "$@"
 92      else
 93        print "${clipcmd}: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
 94        return 1
 95      fi
 96    }
 97    function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; }
 98    function clippaste() { _retry_clipboard_detection_or_fail clippaste "$@"; }
 99    return 1
100  fi
101}
102
103function clipcopy clippaste {
104  unfunction clipcopy clippaste
105  detect-clipboard || true # let one retry
106  "$0" "$@"
107}