Module: VER::Methods::Clipboard

Included in:
VER::Methods
Defined in:
lib/ver/methods/clipboard.rb

Instance Method Summary collapse

Instance Method Details

#copy(text) ⇒ Object



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

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

#copy_array(text) ⇒ Object



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

def copy_array(text)
  array = text.to_ary
  string = array.map(&:to_tcl).join(' ')
  clipboard_set(string, type: 'ARRAY')

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

#copy_fallback(text) ⇒ Object



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

def copy_fallback(text)
  clipboard_set(text)

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

#copy_left_wordObject



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

def copy_left_word
  copy get('insert', 'insert wordstart')
end

#copy_lineObject



4
5
6
# File 'lib/ver/methods/clipboard.rb', line 4

def copy_line
  copy get('insert linestart', 'insert lineend + 1 chars')
end

#copy_message(lines, chars) ⇒ Object



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

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]
  message msg
end

#copy_right_wordObject



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

def copy_right_word
  copy get('insert', 'insert wordend')
end

#copy_string(text) ⇒ Object



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

def copy_string(text)
  clipboard_set(text = text.to_str, type: 'STRING')

  copy_message text.count("\n"), text.size
end

#pasteObject

FIXME: nasty hack or neccesary?



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ver/methods/clipboard.rb', line 77

def paste
  text = clipboard_get
  paste_continous text.to_s

rescue RuntimeError => ex
  if ex.message =~ /form "STRING" not defined/
    array = clipboard_get('ARRAY')
    paste_tk_array array.to_a
  else
    Kernel.raise ex
  end
end

#paste_continous(text) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ver/methods/clipboard.rb', line 54

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

#paste_tk_array(chunks) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/ver/methods/clipboard.rb', line 67

def paste_tk_array(chunks)
  insert_y, insert_x = index(:insert).split

  chunks.each_with_index do |chunk, idx|
    y = insert_y + idx
    insert "#{y}.#{insert_x}", chunk
  end
end