2
#This file is part of Xdo.
3
#Copyright © 2009, 2010 Marvin Gülker
4
# Initia in potestate nostra sunt, de eventu fortuna iudicat.
6
# Modified by Gerry Boland <gerry dot boland at canonical dot com>
7
require File.join(File.dirname(__FILE__), '_xdo')
11
#A module for interaction with the X clipboard. Please note, that the X clipboard
12
#consists of three parts: The PRIMARY clipboard, the CLIPBOARD clipboard, and
13
#the SECONDARY clipboard. The clipboard you access normally via [CTRL]+[C]
14
#or by right-clicking and selecting "copy", is usually the CLIPBOARD clipboard (but that
15
#depends on the application you use). The three main methods of this module (#read, #write
16
#and #clear) take a list symbols of the clipboards to interact with. If you don't want to
17
#pass in the symbols, use the predefined read_xy, write_xy and clear_xy methods. They cannot
18
#access more than one clipboard at a time.
19
#The symbols for the clipboards are:
21
#[SECONDARY] :secondary
22
#[CLIPBOARD] :clipboard
23
#You cannot store complex objects like images via this interface, only strings. However,
24
#you could translate an image into a string (packed pixels maybe?) and put that on the
25
#clipboard -- for your own application this may be fine, but it won't magically allow
26
#a user to paste that image into a graphics program.
28
#The +xsel+ program used by this module is quite outdated. As far as I can see, it's
29
#last update happened in 2002 and since I do not believe that software exists that
30
#won't break over a period of 8 years without a single modification while updating systems I'm about to
31
#switch to a newer one. +xclip+ is likely, but that one got it's last update in early
38
# :singleton-method: read_primary
39
#Returns the contents of the PRIMARY clipboard.
40
#See #read for an explanation.
43
# :singleton-method: read_clipboard
44
#Returns the contents of the CLIPBOARD clipboard.
45
#See #read for an explanation.
48
# :singleton-method: read_secondary
49
#Returns the contents of the SECONDARY clipboard.
50
#See #read for an explanation.
53
# :singleton-method: write_primary
54
#Writes to the PRIMARY clipboard.
55
#See #write for an explanation.
58
# :singleton-method: write_clipboard
59
#Writes to the CLIPBOARD clipboard.
60
#See #write for an explanation.
63
# :singleton-method: write_secondary
64
#Writes to the SECONDARY clipboard.
65
#See #write for an explanation.
68
# :singleton-method: clear_primary
69
#Clears the PRIMARY clipboard.
70
#See #clear for an explanation.
73
# :singleton-method: clear_clipboard
74
#Clears the CLIPBOARD clipboard.
75
#See #clear for an explanation.
78
# :singleton-method: clear_secondary
79
#Clears the SECONDARY clipboard.
80
#See #clear for an explanation.
82
#Reads text from a X clipboard.
84
#[<tt>*from</tt>] (<tt>:clipboard</tt>, <tt>:primary</tt>, <tt>:secondary</tt>) Specifies from which clipboards you want to read (in 70% of all cases you want to read from <tt>:clipboard</tt>).
87
# {:clip_sym => "clipboard_content"}
88
#If you didn't pass any arguments to #read, the hash will contain keys for
89
#all clipboard, i.e. for <tt>:clipboard</tt>, <tt>:primary</tt> and <tt>:secondary</tt>.
90
#If you did, only those symbols will be included you passed. See
91
#the _Example_ section for an example of this.
93
# XDo::Clipboard.read #| {:clipboard => "...", :primary => "...", :secondary => "..."}
94
# XDo::Clipboard.read(:primary) #| {:primary => "..."}
95
# XDo::Clipboard.read(:clipboard, :secondary) #| {clipboard => "...", :secondary => "..."}
97
#You could also use one of the read_* methods for convenience.
99
if from.first.kind_of? Hash
100
warn("#{caller.first}: Deprecation warning: Use symbols as a rest argument now!")
101
from = from.first.keys
103
from.concat([:clipboard, :primary, :secondary]) if from.empty?
106
hsh[:primary] = `#{XSEL}` if from.include? :primary
107
hsh[:clipboard] = `#{XSEL} -b` if from.include? :clipboard
108
hsh[:secondary] = `#{XSEL} -s` if from.include? :secondary
113
#Writes text to a X clipboard.
115
#[<tt>*to</tt>] (<tt>:clipboard</tt>) Specifies to what clipboards you want to wrote to.
119
# XDo::Clipboard.write("I love Ruby") #You can now paste this via [CTRL] + [V]
120
# XDo::Clipboard.write("I love Ruby", :primary) #You can now paste this via a middle-mouse-button click
121
# XDo::Clipboard.write("I love Ruby", :clipboard, :primary) #Both of the above
123
#You could also use one of the write_* methods for convenience.
125
if to.first.kind_of? Hash
126
warn("#{caller.first}: Deprecation warning: Use symbols as a rest argument now!")
129
to << :clipboard if to.empty?
131
IO.popen("xsel -i", "w"){|io| io.write(text)} if to.include? :primary
132
IO.popen("xsel -b -i", "w"){|io| io.write(text)} if to.include? :clipboard
133
IO.popen("xsel -s -i", "w"){|io| io.write(text)} if to.include? :secondary
137
#Appends text to a X clipboard.
139
#[+text+] The text to append.
140
#[<tt>*to</tt>] (<tt>:clipboard</tt>) The clipboards to which you want to append.
144
# XDo::Clipboard.write("I love ")
145
# XDo::Clipboard.append("Ruby")
146
# puts XDo::Clipboard.read(:clipboard)[:clipboard] #=> I love Ruby
148
# XDo::Clipboard.write("I love", :primary)
149
# XDo::Clipboard.append("Ruby", :primary, :clipboard)
150
# #If you now paste via [CTRL] + [V], you'll get 'Ruby'. If you
151
# #paste via the middle mouse button, you'll get 'I love Ruby'
152
# #(Assuming you didn't execute the first block of code, of course).
153
def append(text, *to)
154
if to.first.kind_of? Hash
155
warn("#{caller.first}: Deprecation warning: Use symbols as a rest argument now!")
158
to << :clipboard if to.empty?
160
IO.popen("xsel -a -i", "w"){|io| io.write(text)} if to.include? :primary
161
IO.popen("xsel -b -a -i", "w"){|io| io.write(text)} if to.include? :clipboard
162
IO.popen("xsel -s -a -i", "w"){|io| io.write(text)} if to.include? :secondary
165
#Clears the specified clipboards.
167
#[<tt>*clips</tt>] (<tt>:primary</tt>, <tt>:clipboard</tt>, <tt>:secondary</tt>) The clipboards you want to clear.
171
# XDo::Clipboard.write("I love Ruby")
172
# XDo::Clipboard.clear
173
# #Nothing can be pasted anymore
175
# XDo::Clipboard.write("I love Ruby", :clipboard, :primary)
176
# XDo::Clipboard.clear(:primary)
177
# #You can still paste via [CTRL] + [V], but not with the middle mouse button
179
if clips.first.kind_of? Hash
180
warn("#{caller.first}: Deprecation warning: Use symbols as a rest argument now!")
181
clips = clips.first.keys
183
clips.concat([:primary, :clipboard, :secondary]) if clips.empty?
185
`#{XSEL} -c` if clips.include? :primary
186
`#{XSEL} -b -c` if clips.include? :clipboard
187
`#{XSEL} -s -c` if clips.include? :secondary
191
[:primary, :clipboard, :secondary].each do |sym|
193
define_method(:"read_#{sym}") do
197
define_method(:"write_#{sym}") do |text|
201
define_method(:"clear_#{sym}") do