Module: VER::Methods::Clipboard

Defined in:
lib/ver/methods/clipboard.rb

Class Method Summary collapse

Class Method Details

.clipboard_get(text, type = 'STRING') ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/ver/methods/clipboard.rb', line 92

def clipboard_get(text, type = 'STRING')
  Tk::Clipboard.get(text, type)
rescue RuntimeError => ex
  if ex.message =~ /form "#{type}" not defined/
    yield if block_given?
  else
    VER.error(ex)
  end
end

.copy(text, content) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/ver/methods/clipboard.rb', line 31

def copy(text, content)
  if content.respond_to?(:to_str)
    copy_string(content.to_str)
  elsif content.respond_to?(:to_ary)
    copy_array(content.to_ary)
  else
    copy_fallback(content)
  end
end

.copy_array(content) ⇒ Object



47
48
49
50
51
52
# File 'lib/ver/methods/clipboard.rb', line 47

def copy_array(content)
  marshal = [Marshal.dump(content)].pack('m')
  Tk::Clipboard.set(marshal, type: 'ARRAY')

  copy_message content.size, content.reduce(0){|s,v| s + v.size }
end

.copy_fallback(content) ⇒ Object



54
55
56
57
58
# File 'lib/ver/methods/clipboard.rb', line 54

def copy_fallback(content)
  Tk::Clipboard.set(content)

  VER.message "Copied unkown entity of class %p" % [content.class]
end

.copy_line(text) ⇒ Object



6
7
8
9
# File 'lib/ver/methods/clipboard.rb', line 6

def copy_line(text)
  content = text.get('insert linestart', 'insert lineend + 1 chars')
  copy(text, content)
end

.copy_message(lines, chars) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/ver/methods/clipboard.rb', line 60

def copy_message(lines, chars)
  lines_desc = lines == 1 ? 'line' : 'lines'
  chars_desc = chars == 1 ? 'character' : 'characters'

  msg = "copied %d %s of %d %s" % [lines, lines_desc, chars, chars_desc]
  VER.message(msg)
end

.copy_motion(text, motion, count = 1) ⇒ Object



11
12
13
14
# File 'lib/ver/methods/clipboard.rb', line 11

def copy_motion(text, motion, count = 1)
  movement = Move.virtual(text, motion, count)
  copy(text, text.get(*movement))
end

.copy_string(content) ⇒ Object



41
42
43
44
45
# File 'lib/ver/methods/clipboard.rb', line 41

def copy_string(content)
  Tk::Clipboard.set(content, type: 'STRING')

  copy_message(content.count("\n") + 1, content.size)
end

.paste(text) ⇒ Object

FIXME: nasty hack or neccesary?



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

def paste(text)
  content = clipboard_get(text, 'STRING'){
    array = clipboard_get(text, 'ARRAY')
    return paste_array(text, array) if array
  }

  paste_continous(text, content.to_s) if content
end

.paste_above(text) ⇒ Object



26
27
28
29
# File 'lib/ver/methods/clipboard.rb', line 26

def paste_above(text)
  text.mark_set(:insert, 'insert - 1 line lineend')
  paste(text)
end

.paste_array(text, marshal_array) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/ver/methods/clipboard.rb', line 81

def paste_array(text, marshal_array)
  array = Marshal.load(marshal_array.unpack('m').first)
  insert_y, insert_x = text.index(:insert).split

  Undo.record text do |record|
    array.each_with_index do |line, index|
      record.insert("#{insert_y + index}.#{insert_x}", line)
    end
  end
end

.paste_continous(text, content) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ver/methods/clipboard.rb', line 68

def paste_continous(text, content)
  if content =~ /\A([^\n]*)\n\Z/
    text.mark_set :insert, 'insert lineend'
    text.insert :insert, "\n#{$1}"
  elsif content =~ /\n/
    text.mark_set :insert, 'insert lineend'
    text.insert :insert, "\n"
    content.each_line{|line| text.insert(:insert, line) }
  else
    text.insert :insert, content
  end
end