my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.1 KiB

3 years ago
  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. # - xclip (if $DISPLAY is set)
  14. # - xsel (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. #
  51. function detect-clipboard() {
  52. emulate -L zsh
  53. if [[ "${OSTYPE}" == darwin* ]] && (( ${+commands[pbcopy]} )) && (( ${+commands[pbpaste]} )); then
  54. function clipcopy() { pbcopy < "${1:-/dev/stdin}"; }
  55. function clippaste() { pbpaste; }
  56. elif [[ "${OSTYPE}" == (cygwin|msys)* ]]; then
  57. function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; }
  58. function clippaste() { cat /dev/clipboard; }
  59. elif [ -n "${WAYLAND_DISPLAY:-}" ] && (( ${+commands[wl-copy]} )) && (( ${+commands[wl-paste]} )); then
  60. function clipcopy() { wl-copy < "${1:-/dev/stdin}"; }
  61. function clippaste() { wl-paste; }
  62. elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xclip]} )); then
  63. function clipcopy() { xclip -in -selection clipboard < "${1:-/dev/stdin}"; }
  64. function clippaste() { xclip -out -selection clipboard; }
  65. elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xsel]} )); then
  66. function clipcopy() { xsel --clipboard --input < "${1:-/dev/stdin}"; }
  67. function clippaste() { xsel --clipboard --output; }
  68. elif (( ${+commands[lemonade]} )); then
  69. function clipcopy() { lemonade copy < "${1:-/dev/stdin}"; }
  70. function clippaste() { lemonade paste; }
  71. elif (( ${+commands[doitclient]} )); then
  72. function clipcopy() { doitclient wclip < "${1:-/dev/stdin}"; }
  73. function clippaste() { doitclient wclip -r; }
  74. elif (( ${+commands[win32yank]} )); then
  75. function clipcopy() { win32yank -i < "${1:-/dev/stdin}"; }
  76. function clippaste() { win32yank -o; }
  77. elif [[ $OSTYPE == linux-android* ]] && (( $+commands[termux-clipboard-set] )); then
  78. function clipcopy() { termux-clipboard-set < "${1:-/dev/stdin}"; }
  79. function clippaste() { termux-clipboard-get; }
  80. elif [ -n "${TMUX:-}" ] && (( ${+commands[tmux]} )); then
  81. function clipcopy() { tmux load-buffer "${1:--}"; }
  82. function clippaste() { tmux save-buffer -; }
  83. elif [[ $(uname -r) = *icrosoft* ]]; then
  84. function clipcopy() { clip.exe < "${1:-/dev/stdin}"; }
  85. function clippaste() { powershell.exe -noprofile -command Get-Clipboard; }
  86. else
  87. function _retry_clipboard_detection_or_fail() {
  88. local clipcmd="${1}"; shift
  89. if detect-clipboard; then
  90. "${clipcmd}" "$@"
  91. else
  92. print "${clipcmd}: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
  93. return 1
  94. fi
  95. }
  96. function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; }
  97. function clippaste() { _retry_clipboard_detection_or_fail clippaste "$@"; }
  98. return 1
  99. fi
  100. }
  101. # Detect at startup. A non-zero exit here indicates that the dummy clipboards were set,
  102. # which is not really an error. If the user calls them, they will attempt to redetect
  103. # (for example, perhaps the user has now installed xclip) and then either print an error
  104. # or proceed successfully.
  105. detect-clipboard || true