Add clipcopy() and clippaste() generic cross-platform CLI clipboard functions.
Change copydir, copyfile, and coffee plugins to use them, instead of the Mac-only `pbcopy` command.
Diff
1diff --git a/lib/clipboard.zsh b/lib/clipboard.zsh
2new file mode 100644
3index 0000000000000000000000000000000000000000..24b7380f71bac57bd044a41c3639e143944e098a
4--- /dev/null
5+++ b/lib/clipboard.zsh
6@@ -0,0 +1,67 @@
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+ which xclip &>/dev/null
41+ if [[ $? != 0 ]]; then
42+ print "clipcopy: Platform $OSTYPE not supported or xclip not installed" >&2
43+ return 1
44+ fi
45+ if [[ -z $file ]]; then
46+ xclip -in -selection clipboard
47+ else
48+ xclip -in -selection clipboard $file
49+ fi
50+ fi
51+}
52+
53+# clippaste - "Paste" data from clipboard to stdout
54+#
55+# Usage:
56+#
57+# clippaste - writes clipboard's contents to stdout
58+#
59+function clippaste() {
60+ emulate -L zsh
61+ if [[ $OSTYPE == darwin* ]]; then
62+ pbpaste
63+ elif [[ $OSTYPE == cygwin* ]]; then
64+ cat /dev/clipboard
65+ else
66+ which xclip &>/dev/null
67+ if [[ $? != 0 ]]; then
68+ print "clipcopy: Platform $OSTYPE not supported or xclip not installed" >&2
69+ return 1
70+ fi
71+ xclip -out -selection clipboard
72+ fi
73+}
74diff --git a/plugins/coffee/coffee.plugin.zsh b/plugins/coffee/coffee.plugin.zsh
75index 1a7bedd87fe9426f18c00e9f99bbc269a194406d..c7b486a6b27465f08fcecb6e91961039d9c38390 100644
76--- a/plugins/coffee/coffee.plugin.zsh
77+++ b/plugins/coffee/coffee.plugin.zsh
78@@ -6,11 +6,11 @@ cf () {
79 }
80 # compile & copy to clipboard
81 cfc () {
82- cf $1 | pbcopy
83+ cf $1 | clipcopy
84 }
85 86-# compile from pasteboard & print
87-alias cfp='coffeeMe "$(pbpaste)"'
88+# compile from clipboard & print
89+alias cfp='coffeeMe "$(clippaste)"'
90 91-# compile from pasteboard and copy to clipboard
92-alias cfpc='cfp | pbcopy'
93+# compile from clipboard and copy to clipboard
94+alias cfpc='cfp | clipcopy'
95diff --git a/plugins/copydir/copydir.plugin.zsh b/plugins/copydir/copydir.plugin.zsh
96index 37bb5e086dc3e9e38b6ebcd435fffcc492f4c1e2..4b918e815edc3d187902d61e95ac3c151feb6ca7 100644
97--- a/plugins/copydir/copydir.plugin.zsh
98+++ b/plugins/copydir/copydir.plugin.zsh
99@@ -1,3 +1,5 @@
100+# Copies the pathname of the current directory to the system or X Windows clipboard
101 function copydir {
102- pwd | tr -d "\r\n" | pbcopy
103+ emulate -L zsh
104+ print -n $PWD | clipcopy
105 }
106diff --git a/plugins/copyfile/copyfile.plugin.zsh b/plugins/copyfile/copyfile.plugin.zsh
107index 944a903c644689d26e5918ea67a4a55f94736eda..f4eca5acf82c6b20a5aabaeed81add2117e636c3 100644
108--- a/plugins/copyfile/copyfile.plugin.zsh
109+++ b/plugins/copyfile/copyfile.plugin.zsh
110@@ -1,5 +1,7 @@
111+# Copies the contents of a given file to the system or X Windows clipboard
112+#
113+# copyfile <file>
114 function copyfile {
115- [[ "$#" != 1 ]] && return 1
116- local file_to_copy=$1
117- cat $file_to_copy | pbcopy
118+ emulate -L zsh
119+ clipcopy $1
120 }