dc06e96e9cf52354ac76e9795cc85d528b2d5018

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #4254 from apjanke/copyfile-portability

Cross-platform clipboard clipcopy() and clippaste()

Diff

  1diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..b663800a4d09bb9918d5f33d3188c81d58ea8882
  4--- /dev/null
  5+++ b/lib/clipboard.zsh
  6@@ -0,0 +1,86 @@
  7+# System clipboard integration
  8+#
  9+# This file has support for doing system clipboard copy and paste operations
 10+# from the command line in a generic cross-platform fashion.
 11+#
 12+# On OS X and Windows, the main system clipboard or "pasteboard" is used. On other
 13+# Unix-like OSes, this considers the X Windows CLIPBOARD selection to be the
 14+# "system clipboard", and the X Windows `xclip` command must be installed.
 15+
 16+# clipcopy - Copy data to clipboard
 17+#
 18+# Usage:
 19+#
 20+#  <command> | clipcopy    - copies stdin to clipboard
 21+#
 22+#  clipcopy <file>         - copies a file's contents to clipboard
 23+#
 24+function clipcopy() {
 25+  emulate -L zsh
 26+  local file=$1
 27+  if [[ $OSTYPE == darwin* ]]; then
 28+    if [[ -z $file ]]; then
 29+      pbcopy
 30+    else
 31+      cat $file | pbcopy
 32+    fi
 33+  elif [[ $OSTYPE == cygwin* ]]; then
 34+    if [[ -z $file ]]; then
 35+      cat > /dev/clipboard
 36+    else
 37+      cat $file > /dev/clipboard
 38+    fi
 39+  else
 40+    if which xclip &>/dev/null; then
 41+      if [[ -z $file ]]; then
 42+        xclip -in -selection clipboard
 43+      else
 44+        xclip -in -selection clipboard $file
 45+      fi
 46+    elif which xsel &>/dev/null; then
 47+      if [[ -z $file ]]; then
 48+        xsel --clipboard --input 
 49+      else
 50+        cat "$file" | xsel --clipboard --input
 51+      fi
 52+    else
 53+      print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
 54+      return 1
 55+    fi
 56+  fi
 57+}
 58+
 59+# clippaste - "Paste" data from clipboard to stdout
 60+#
 61+# Usage:
 62+#
 63+#   clippaste   - writes clipboard's contents to stdout
 64+#
 65+#   clippaste | <command>    - pastes contents and pipes it to another process
 66+#
 67+#   clippaste > <file>      - paste contents to a file
 68+#
 69+# Examples:
 70+#
 71+#   # Pipe to another process
 72+#   clippaste | grep foo
 73+#
 74+#   # Paste to a file
 75+#   clippaste > file.txt
 76+function clippaste() {
 77+  emulate -L zsh
 78+  if [[ $OSTYPE == darwin* ]]; then
 79+    pbpaste
 80+  elif [[ $OSTYPE == cygwin* ]]; then
 81+    cat /dev/clipboard
 82+  else
 83+    if which xclip &>/dev/null; then
 84+      xclip -out -selection clipboard
 85+    elif which xsel &>/dev/null; then
 86+      xsel --clipboard --output
 87+    else
 88+      print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
 89+      return 1
 90+    fi
 91+  fi
 92+}
 93diff --git a/plugins/coffee/coffee.plugin.zsh b/plugins/coffee/coffee.plugin.zsh
 94index 4e98e022869ea8b968ab4f3598f904bb51ff27d8..77cb663f7b62f1272e51e881f703f5f7c259f666 100644
 95--- a/plugins/coffee/coffee.plugin.zsh
 96+++ b/plugins/coffee/coffee.plugin.zsh
 97@@ -6,11 +6,11 @@ cf () {
 98 }
 99 # compile & copy to clipboard
100 cfc () {
101-  cf "$1" | pbcopy
102+  cf "$1" | clipcopy
103 }
104 
105-# compile from pasteboard & print
106-alias cfp='coffeeMe "$(pbpaste)"'
107+# compile from clipboard & print
108+alias cfp='coffeeMe "$(clippaste)"'
109 
110-# compile from pasteboard and copy to clipboard
111-alias cfpc='cfp | pbcopy'
112+# compile from clipboard and copy to clipboard
113+alias cfpc='cfp | clipcopy'
114diff --git a/plugins/copydir/copydir.plugin.zsh b/plugins/copydir/copydir.plugin.zsh
115index 37bb5e086dc3e9e38b6ebcd435fffcc492f4c1e2..c451062406a0a3fab62728e673c94f6cab5b396c 100644
116--- a/plugins/copydir/copydir.plugin.zsh
117+++ b/plugins/copydir/copydir.plugin.zsh
118@@ -1,3 +1,5 @@
119+# Copies the pathname of the current directory to the system or X Windows clipboard
120 function copydir {
121-  pwd | tr -d "\r\n" | pbcopy
122-}
123+  emulate -L zsh
124+  print -n $PWD | clipcopy
125+}
126diff --git a/plugins/copyfile/copyfile.plugin.zsh b/plugins/copyfile/copyfile.plugin.zsh
127index 944a903c644689d26e5918ea67a4a55f94736eda..f4eca5acf82c6b20a5aabaeed81add2117e636c3 100644
128--- a/plugins/copyfile/copyfile.plugin.zsh
129+++ b/plugins/copyfile/copyfile.plugin.zsh
130@@ -1,5 +1,7 @@
131+# Copies the contents of a given file to the system or X Windows clipboard
132+#
133+# copyfile <file>
134 function copyfile {
135-  [[ "$#" != 1 ]] && return 1
136-  local file_to_copy=$1
137-  cat $file_to_copy | pbcopy
138+  emulate -L zsh
139+  clipcopy $1
140 }