浏览代码

Merge pull request #4254 from apjanke/copyfile-portability

Cross-platform clipboard clipcopy() and clippaste()
Robby Russell 9 年之前
父节点
当前提交
dc06e96e9c
共有 4 个文件被更改,包括 100 次插入10 次删除
  1. 86 0
      lib/clipboard.zsh
  2. 5 5
      plugins/coffee/coffee.plugin.zsh
  3. 4 2
      plugins/copydir/copydir.plugin.zsh
  4. 5 3
      plugins/copyfile/copyfile.plugin.zsh

+ 86 - 0
lib/clipboard.zsh

@@ -0,0 +1,86 @@
+# System clipboard integration
+#
+# 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.
+
+# clipcopy - Copy data to clipboard
+#
+# Usage:
+#
+#  <command> | clipcopy    - copies stdin to clipboard
+#
+#  clipcopy <file>         - copies a file's contents to clipboard
+#
+function clipcopy() {
+  emulate -L zsh
+  local file=$1
+  if [[ $OSTYPE == darwin* ]]; then
+    if [[ -z $file ]]; then
+      pbcopy
+    else
+      cat $file | pbcopy
+    fi
+  elif [[ $OSTYPE == cygwin* ]]; then
+    if [[ -z $file ]]; then
+      cat > /dev/clipboard
+    else
+      cat $file > /dev/clipboard
+    fi
+  else
+    if which xclip &>/dev/null; then
+      if [[ -z $file ]]; then
+        xclip -in -selection clipboard
+      else
+        xclip -in -selection clipboard $file
+      fi
+    elif which xsel &>/dev/null; then
+      if [[ -z $file ]]; then
+        xsel --clipboard --input 
+      else
+        cat "$file" | xsel --clipboard --input
+      fi
+    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:
+#
+#   clippaste   - writes clipboard's contents to stdout
+#
+#   clippaste | <command>    - pastes contents and pipes it to another process
+#
+#   clippaste > <file>      - paste contents to a file
+#
+# Examples:
+#
+#   # Pipe to another process
+#   clippaste | grep foo
+#
+#   # Paste to a file
+#   clippaste > file.txt
+function clippaste() {
+  emulate -L zsh
+  if [[ $OSTYPE == darwin* ]]; then
+    pbpaste
+  elif [[ $OSTYPE == cygwin* ]]; then
+    cat /dev/clipboard
+  else
+    if which xclip &>/dev/null; then
+      xclip -out -selection clipboard
+    elif which xsel &>/dev/null; then
+      xsel --clipboard --output
+    else
+      print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
+      return 1
+    fi
+  fi
+}

+ 5 - 5
plugins/coffee/coffee.plugin.zsh

@@ -6,11 +6,11 @@ cf () {
 }
 # compile & copy to clipboard
 cfc () {
-  cf "$1" | pbcopy
+  cf "$1" | clipcopy
 }
 
-# compile from pasteboard & print
-alias cfp='coffeeMe "$(pbpaste)"'
+# compile from clipboard & print
+alias cfp='coffeeMe "$(clippaste)"'
 
-# compile from pasteboard and copy to clipboard
-alias cfpc='cfp | pbcopy'
+# compile from clipboard and copy to clipboard
+alias cfpc='cfp | clipcopy'

+ 4 - 2
plugins/copydir/copydir.plugin.zsh

@@ -1,3 +1,5 @@
+# Copies the pathname of the current directory to the system or X Windows clipboard
 function copydir {
-  pwd | tr -d "\r\n" | pbcopy
-}
+  emulate -L zsh
+  print -n $PWD | clipcopy
+}

+ 5 - 3
plugins/copyfile/copyfile.plugin.zsh

@@ -1,5 +1,7 @@
+# Copies the contents of a given file to the system or X Windows clipboard
+#
+# copyfile <file>
 function copyfile {
-  [[ "$#" != 1 ]] && return 1
-  local file_to_copy=$1
-  cat $file_to_copy | pbcopy
+  emulate -L zsh
+  clipcopy $1
 }