As a feature I use all the time in R (to paste into excel and send to someone, play with a graph, etc.), I've created a start for copying objects to the clipboard. As of now the below code is Windows only (I did just set up a Linux vmbox, but I haven't had a chance to play with it much yet). Any who'd like to contribute for Linux/Mac compatibility, that'd be great. As of now, it uses the show() function result of an object to copy to the clipboard.
I thought about a package, but as it's a single function, I thought I'd just upload this to let others hack on it. Eventually this probably belongs as a method to write() or dlmwrite() and a global clipboard stream object could be specified. Just an idea.
Cheers.
function clipboard(x)
#determine number of bytes needed to allocate
obj = bytestring(string(x))
bytes = length(obj)
if(ccall( (:OpenClipboard, "user32"), stdcall, Bool, (Ptr{Void},), C_NULL) )
#Empty the clipboard
ccall( (:EmptyClipboard, "user32"), stdcall, Bool, () )
#Allocate space on the system for copying
ptr = ccall( (:GlobalAlloc, "kernel32"), stdcall,
Ptr{Void}, (Uint16,Int32), 2,bytes+1)
ptr = ccall( (:GlobalLock, "kernel32"), stdcall,
Ptr{Void}, (Ptr{Void},), ptr)
#Copy data to allocated space
r = ccall(:memcpy, Ptr{Void}, (Ptr{Void},Ptr{Uint8},Int32), ptr, obj, length(obj) + 1 )
ccall( (:GlobalUnlock, "kernel32"), stdcall,
Void, (Ptr{Void},), ptr)
#Set clipboard data type
ptr = ccall( (:SetClipboardData, "user32"), stdcall,
Ptr{Void}, (Uint32, Ptr{Void}), 1, ptr)
ccall( (:CloseClipboard, "user32"), stdcall,
Void, (), )
else
error("Could not copy object to clipboard")
end
end