Class: Clipboard

Inherits:
Object show all
Defined in:
lib/xiki/clipboard.rb

Overview

Provides copy and paste functionality

Constant Summary collapse

@@hash =

Stores things user copies

{}
@@hash_by_first_letter =
{}

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



86
87
88
# File 'lib/xiki/clipboard.rb', line 86

def self.[] key
  self.get key.to_s
end

.[]=(key, to) ⇒ Object



90
91
92
# File 'lib/xiki/clipboard.rb', line 90

def self.[]= key, to
  self.set key.to_s, to
end

.as_clipboardObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/xiki/clipboard.rb', line 284

def self.as_clipboard
  prefix = Keys.prefix :clear=>true
  if prefix == 0
    l, r = View.paragraph :bounds=>true
    Effects.blink :left=>l, :right=>r
    cursor = View.cursor
    View.cursor = l
    Location.as_spot('clipboard')
    Clipboard["0"] = View.txt(l, r)
    View.cursor = cursor
    return
  end

  if prefix == :-
    l, r = View.range
    Effects.blink :left=>l, :right=>r
    Clipboard["0"] = View.selection.gsub(/^ *\|.?/, '')
    return
  end

  Location.as_spot('clipboard')

  # If numeric prefix, get next n lines and put in clipboard
  if prefix.is_a?(Fixnum)
    l, r = Line.left, Line.left(prefix + 1)
    Effects.blink :left=>l, :right=>r
    Clipboard["0"] = View.txt(l, r)

    View.set_mark(r)
    return
  end

  Clipboard.copy("0")
  Clipboard.save_by_first_letter View.selection   # Store for retrieval with enter_yank
end

.as_line(many = nil) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/xiki/clipboard.rb', line 258

def self.as_line many=nil
  prefix = Keys.prefix :clear=>true

  return FileTree.copy_path if prefix == :u

  many ||= prefix || 1
  Move.to_axis
  left = Line.left
  right = Line.left(many+1)
  line = View.txt(left, right)
  Clipboard.set("0", line)
  Effects.blink :left=>left, :right=>right
  $el.set_mark(right)
  Clipboard.save_by_first_letter line   # Store for retrieval with enter_yank
end

.as_objectObject



246
247
248
249
250
# File 'lib/xiki/clipboard.rb', line 246

def self.as_object
  set("0", $el.thing_at_point(:symbol))
  left, right = $el.bounds_of_thing_at_point(:symbol).to_a
  Effects.blink(:left=>left, :right=>right)
end

.as_thingObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/xiki/clipboard.rb', line 209

def self.as_thing

  orig = Location.new

  # If at end of space, grab as tree
  if Line.indent.length == View.column
    left = Line.left
    return
  end

  # If on blank spaces, copy them
  if $el.buffer_substring($el.point-1, $el.point+1) =~ /[ \n] /
    $el.skip_chars_forward " "
    right = $el.point
    $el.skip_chars_backward " "
    left = $el.point
  else
    $el.skip_chars_forward " "
    left, right = $el.bounds_of_thing_at_point(:sexp).to_a
  end

  if Keys.prefix_u?
    left += 1
    right -= 1
  end
  Effects.blink(:left=>left, :right=>right)

  txt = View.txt(left, right)
  Clipboard.set "0", txt
  View.to right
  #     View.mark left   # What did this do?
  Clipboard.save_by_first_letter txt

  orig.go

end

.copy(loc = nil, txt = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xiki/clipboard.rb', line 25

def self.copy loc=nil, txt=nil
  # Use string if user types it quickly
  if ! loc
    View.flash "Enter variable name:", :times=>1
    loc = Keys.input(:chars=>1, :prompt=>"Enter one char (variable name to store this as): ") || "0"
  end

  unless txt
    left, right = View.range
    Effects.blink :left=>left, :right=>right
    txt = $el.buffer_substring($el.region_beginning, $el.region_end)
  end
  self.set(loc, txt, Keys.prefix)
end

.copy_everythingObject



252
253
254
255
256
# File 'lib/xiki/clipboard.rb', line 252

def self.copy_everything
  Effects.blink :what=>:all
  Clipboard.set("0", $el.buffer_string)
  $el.set_mark($el.point_max)
end

.copy_paragraph(options = {}) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/xiki/clipboard.rb', line 170

def self.copy_paragraph options={}
  prefix = Keys.prefix

  if prefix == :u or options[:rest]   # If U prefix, get rest of paragraph
    left, right = View.paragraph(:bounds => true, :start_here => true)
  else
    if prefix   # If numeric prefix
      self.as_line
      return
    end
    # If no prefix, get whole paragraph
    left, right = View.paragraph(:bounds => true)
  end

  if options[:just_return]
    return [View.txt(left, right), left, right]
  end
  $el.goto_char left
  $el.set_mark right
  Effects.blink(:left => left, :right => right)
  Clipboard.copy("0")
end

.cut(loc = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xiki/clipboard.rb', line 40

def self.cut loc=nil
  loc = loc.to_s
  prefix = Keys.prefix :clear=>true   # If numeric prefix, reset region

  if prefix == 0
    l, r = View.paragraph :bounds=>true
    View.cursor = l
    View.mark = r
  elsif prefix.is_a?(Fixnum)
    Line.to_left
    View.mark = Line.left 1+prefix
  end

  self.copy loc
  $el.delete_region($el.region_beginning, $el.region_end)

  Location.as_spot('killed')
end

.diff_1_and_2Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/xiki/clipboard.rb', line 193

def self.diff_1_and_2
  # Compare clipboard 1 with 2
  # Unquote if several spaces and |
  View.to_buffer "1", :clear => true
  one = Clipboard["1"]
  one.gsub!(/^ +\|/, '') if one =~ /\A   +\|/
  $el.insert Clipboard["1"]

  View.to_buffer "2", :clear => true
  one = Clipboard["2"]
  one.gsub!(/^ +\|/, '') if one =~ /\A   +\|/
  $el.insert Clipboard["2"]

  $el.ediff_buffers "1", "2"
end

.displayObject



94
95
96
97
98
# File 'lib/xiki/clipboard.rb', line 94

def self.display
  @@hash.each do |k, v|
    insert [k, v].to_s
  end
end

.do_as_camel_caseObject



146
147
148
149
150
151
152
# File 'lib/xiki/clipboard.rb', line 146

def self.do_as_camel_case
  Keys.prefix_times.times do
    word = Line.symbol(:delete => true)
    $el.insert TextUtil.camel_case(word)
    Move.forward
  end
end

.do_as_lower_caseObject



162
163
164
165
166
167
168
# File 'lib/xiki/clipboard.rb', line 162

def self.do_as_lower_case
  Keys.prefix_times.times do
    word = Line.symbol(:delete => true)
    $el.insert word.downcase
    Move.forward
  end
end

.do_as_snake_caseObject



138
139
140
141
142
143
144
# File 'lib/xiki/clipboard.rb', line 138

def self.do_as_snake_case
  Keys.prefix_times.times do
    word = Line.symbol(:delete => true)
    $el.insert TextUtil.snake_case(word)
    Move.forward
  end
end

.do_as_upper_caseObject



154
155
156
157
158
159
160
# File 'lib/xiki/clipboard.rb', line 154

def self.do_as_upper_case
  Keys.prefix_times.times do
    word = Line.symbol(:delete => true)
    $el.insert word.upcase
    Move.forward
  end
end

.enter_replacementObject



274
275
276
277
278
279
280
281
282
# File 'lib/xiki/clipboard.rb', line 274

def self.enter_replacement
  # If on whitespace, move to off of it
  $el.skip_chars_forward " "

  orig = $el.point
  Move.to_other_bracket
  View.delete orig, $el.point
  View.insert Clipboard['0']
end

.enter_yankObject



326
327
328
329
330
331
# File 'lib/xiki/clipboard.rb', line 326

def self.enter_yank
  ch = Keys.input :chars=>1
  value = @@hash_by_first_letter[ch]
  return unless value
  View.insert value
end

.get(key = '0', options = {}) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/xiki/clipboard.rb', line 78

def self.get key='0', options={}
  val = @@hash[key.to_s]
  if options[:add_linebreak]
    val = "#{val}\n" unless val[/\n$/]
  end
  val
end

.hashObject



100
101
102
# File 'lib/xiki/clipboard.rb', line 100

def self.hash
  @@hash
end

.hash_by_first_letterObject



104
105
106
# File 'lib/xiki/clipboard.rb', line 104

def self.hash_by_first_letter
  @@hash_by_first_letter
end

.listObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/xiki/clipboard.rb', line 108

def self.list
  $el.switch_to_buffer "*clipboard*"
  $el.erase_buffer
  Notes.mode

  Clipboard.hash.sort.each do |a, b|
    $el.insert "| #{a}\n#{b}\n\n"
  end
  $el.beginning_of_buffer
end

.logObject



17
18
19
20
21
22
23
# File 'lib/xiki/clipboard.rb', line 17

def self.log
  result = ""
  @@hash_by_first_letter.keys.sort.each do |k|
    result << "| #{@@hash_by_first_letter[k]}\n"
  end
  result.empty? ? "- Nothing was copied yet!" : result
end


11
12
13
14
15
# File 'lib/xiki/clipboard.rb', line 11

def self.menu
  "
  - .log/
  "
end

.paste(loc = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/xiki/clipboard.rb', line 59

def self.paste loc=nil
  # Use string if user types it quickly
  loc ||= Keys.input(:chars=>1, :prompt => "Enter one char: ") || 0

  $el.set_mark_command nil

  loc = loc.to_s
  txt = @@hash[loc] || @@hash_by_first_letter[loc]   # If nothing, try to grab by first letter

  # If nothing, try to grab from what's been searched
  txt ||= Search.searches.find{|o| o =~ /^#{loc}/i}

  return View.message("Nothing to search for matching '#{loc}'.", :beep=>1) if txt.nil?

  ($el.elvar.current_prefix_arg || 1).times do   # Get from corresponding register
    View << txt
  end
end

.save_by_first_letter(txt) ⇒ Object



320
321
322
323
324
# File 'lib/xiki/clipboard.rb', line 320

def self.save_by_first_letter txt
  key = txt[/[a-z]/i]
  return unless key
  @@hash_by_first_letter[key.downcase] = txt
end

.set(loc, str, append = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/xiki/clipboard.rb', line 119

def self.set loc, str, append=nil
  loc = loc.to_s
  # Save in corresponding register (or append if prefix)
  if append
    @@hash[loc] += str
  else
    # Store as path
    @@hash["/"] = $el.expand_file_name( $el.buffer_file_name ? $el.buffer_file_name : $el.elvar.default_directory )
    if $el.buffer_file_name
      # Store as tree snippet
      @@hash["="] = FileTree.snippet :txt=>str
      @@hash["."] = "#{$el.file_name_nondirectory($el.buffer_file_name)}"
      @@hash["\\"] = "#{$el.elvar.default_directory}\n  #{$el.file_name_nondirectory($el.buffer_file_name)}"
    end
    @@hash[loc] = str
    $el.x_select_text str if loc == "0"  # If 0, store in OS clipboard
  end
end