Module: VER::Methods::Clipboard

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

Class Method Summary collapse

Class Method Details

.copy(text, content) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ver/methods/clipboard.rb', line 190

def copy(text, content)
  if content.respond_to?(:to_str)
    VER::Clipboard.string = string = content.to_str
    copy_message(text, string.count("\n") + 1, string.size)
  elsif content.respond_to?(:to_ary)
    VER::Clipboard.marshal = array = content.to_ary
    copy_message(text, array.size, array.reduce(0){|s,v| s + v.size })
  else
    VER::Clipboard.dwim = content
    text.message "Copied unkown entity of class %p" % [content.class]
  end
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(text, lines, chars) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/ver/methods/clipboard.rb', line 203

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

  # FIXME: messages should be:
  # block of N lines yanked
  # N lines yanked
  # no message for a couple of chars

  msg = "copied %d %s of %d %s" % [lines, lines_desc, chars, chars_desc]
  text.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

.paste_after(buffer, count = buffer.prefix_count) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ver/methods/clipboard.rb', line 20

def paste_after(buffer, count = buffer.prefix_count)
  buffer.with_register do |register|
    case content = register.value
    when responding_to?(:to_str)
      paste_string_after(buffer, count, content.to_str)
    when responding_to?(:to_ary)
      paste_array_after(buffer, count, content.to_ary)
    else
      buffer.warn "Don't know how to paste: %p" % [content]
    end
  end
end

.paste_after_adjust(buffer, count = buffer.prefix_count) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ver/methods/clipboard.rb', line 46

def paste_after_adjust(buffer, count = buffer.prefix_count)
  buffer.with_register do |register|
    case content = register.value
    when responding_to?(:to_str)
      indent = buffer.at_insert.get('linestart', 'lineend')[/^[ \t]*/]
      string = content.to_str.gsub(/^\s*/, indent)
      paste_string_after(buffer, count, string)
    when responding_to?(:to_ary)
      paste_array_after(buffer, count, content.to_ary)
    else
      buffer.warn "Don't know how to paste: %p" % [content]
    end
  end
end

.paste_after_go_after(buffer, count = buffer.prefix_count) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ver/methods/clipboard.rb', line 33

def paste_after_go_after(buffer, count = buffer.prefix_count)
  buffer.with_register do |register|
    case content = register.value
    when responding_to?(:to_str)
      paste_string_after_go_after(buffer, count, content.to_str)
    when responding_to?(:to_ary)
      paste_array_after(buffer, count, content.to_ary)
    else
      buffer.warn "Don't know how to paste: %p" % [content]
    end
  end
end

.paste_array(text, array) ⇒ Object



229
230
231
232
233
234
235
236
237
# File 'lib/ver/methods/clipboard.rb', line 229

def paste_array(text, array)
  insert_y, insert_x = *text.index(:insert)

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

.paste_array_after(buffer, count, array) ⇒ Object

the vim and the ver, the vim and the ver, one’s a genius, the other’s insane.

I have a faint idea how vim knows the difference, it involves the y_type of a register struct, but we won’t go into that. It’s enough to know that vim can only handle block pasting when it was yanked from vim, we shall behave the same.



100
101
102
103
104
105
106
107
108
# File 'lib/ver/methods/clipboard.rb', line 100

def paste_array_after(buffer, count, array)
  insert_line, insert_char = *buffer.index(:insert)

  buffer.undo_record do |record|
    array.each_with_index do |line, index|
      record.insert("#{insert_line + index}.#{insert_char}", line)
    end
  end
end

.paste_array_before(buffer, count, array) ⇒ Object



187
188
# File 'lib/ver/methods/clipboard.rb', line 187

def paste_array_before(buffer, count, array)
end

.paste_before(buffer, count = buffer.prefix_count) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ver/methods/clipboard.rb', line 138

def paste_before(buffer, count = buffer.prefix_count)
  buffer.with_register do |register|
    case content = register.value
    when responding_to?(:to_str)
      paste_string_before(buffer, count, content.to_str)
    when responding_to?(:to_ary)
      paste_array_before(buffer, count, content.to_ary)
    else
      buffer.warn "Don't know how to paste: %p" % [content]
    end
  end
end

.paste_before_adjust(buffer, count = buffer.prefix_count) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ver/methods/clipboard.rb', line 123

def paste_before_adjust(buffer, count = buffer.prefix_count)
  buffer.with_register do |register|
    case content = register.value
    when responding_to?(:to_str)
      indent = buffer.at_insert.get('linestart', 'lineend')[/^[ \t]*/]
      string = content.to_str.gsub(/^\s*/, indent)
      paste_string_before(buffer, count, string)
    when responding_to?(:to_ary)
      paste_array_before(buffer, count, content.to_ary)
    else
      buffer.warn "Don't know how to paste: %p" % [content]
    end
  end
end

.paste_before_go_after(buffer, count = buffer.prefix_count) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ver/methods/clipboard.rb', line 110

def paste_before_go_after(buffer, count = buffer.prefix_count)
  buffer.with_register do |register|
    case content = register.value
    when responding_to?(:to_str)
      paste_string_before_go_after(buffer, count, content.to_str)
    when responding_to?(:to_ary)
      paste_array_before(buffer, count, content.to_ary)
    else
      buffer.warn "Don't know how to paste: %p" % [content]
    end
  end
end

.paste_continous(buffer, content) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ver/methods/clipboard.rb', line 216

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

.paste_string_after(buffer, count, string) ⇒ Object

on “foo”, it’s simple char selection, insert at insert mark. on “foonb”, it was a char selection, insert at insert mark. on “foonbarn”:

we assume it's linewise, insert newline at eol, then insert the string
starting at the new line.


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

def paste_string_after(buffer, count, string)
  buffer.undo_record do |record|
    if string =~ /\n\Z/ # ends with newline
      string = "\n#{string}".chomp * count # # put newline in front
      buffer.insert = buffer.at_insert.lineend
      record.insert(:insert, string)
      buffer.insert = buffer.at_insert.linestart
    else
      buffer.insert = buffer.at_insert + '1 displaychars'
      record.insert(:insert, string * count)
    end
  end
end

.paste_string_after_go_after(buffer, count, string) ⇒ Object



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

def paste_string_after_go_after(buffer, count, string)
  buffer.undo_record do |record|
    if string =~ /\n\Z/ # ends with newline
      string = "\n#{string}".chomp * count # # put newline in front
      buffer.insert = buffer.at_insert.lineend
      record.insert(:insert, string)
    else
      buffer.insert = buffer.at_insert + '1 displaychars'
      record.insert(:insert, string * count)
    end
  end
end

.paste_string_before(buffer, count, string) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ver/methods/clipboard.rb', line 151

def paste_string_before(buffer, count, string)
  buffer.undo_record do |record|
    if string =~ /\n\Z/ # ends with newline
      string = string.chomp # get rid of that
      count.times do
        # insert newline at sol, insert string just before that
        buffer.insert = buffer.at_insert.linestart
        record.insert(:insert, "\n")
        record.insert(buffer.at_insert - '1 lines', string)
        buffer.insert = (buffer.at_insert - '1 lines').linestart
      end
    else
      buffer.insert = buffer.at_insert - '1 displaychars'
      record.insert(:insert, string * count)
    end
  end
end

.paste_string_before_go_after(buffer, count, string) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ver/methods/clipboard.rb', line 169

def paste_string_before_go_after(buffer, count, string)
  buffer.undo_record do |record|
    if string =~ /\n\Z/ # ends with newline
      string = string.chomp # get rid of that
      count.times do
        # insert newline at sol, insert string just before that
        buffer.insert = buffer.at_insert.linestart
        record.insert(:insert, "\n")
        record.insert(buffer.at_insert - '1 lines', string)
        buffer.insert = (buffer.at_insert - '1 lines').linestart
      end
    else
      buffer.insert = buffer.at_insert - '1 displaychars'
      record.insert(:insert, string * count)
    end
  end
end

.responding_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ver/methods/clipboard.rb', line 16

def responding_to?(method)
  lambda{|object| object.respond_to?(method) }
end