Module: VER::Methods::Insert

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

Class Method Summary collapse

Class Method Details

.common_string(text, string, record = text) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ver/methods/insert.rb', line 119

def common_string(text, string, record = text)
  return if string.empty?

  if !string.frozen? && string.encoding == Encoding::ASCII_8BIT
    begin
      string.encode!(text.encoding)
    rescue Encoding::UndefinedConversionError
      string.force_encoding(string.encoding)
    end
  end

  # puts "Insert %p in mode %p" % [string, keymap.mode]
  record.insert(:insert, string)
end

.enter_replace(buffer, old_mode, new_mode) ⇒ Object



81
82
83
84
85
# File 'lib/ver/methods/insert.rb', line 81

def enter_replace(buffer, old_mode, new_mode)
  # store count argument for later repetition
  buffer.store(self, :replace_count, buffer.prefix_count(0))
  buffer.store(self, :replace_chars, '')
end

.file_contents(filename) ⇒ Object



14
15
16
17
18
19
# File 'lib/ver/methods/insert.rb', line 14

def file_contents(filename)
  content = read_file(filename)
  insert :insert, content
rescue Errno::ENOENT => ex
  VER.error(ex)
end

.indented_newline(text) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ver/methods/insert.rb', line 54

def indented_newline(text)
  Undo.record text do |record|
    line1 = text.get('insert linestart', 'insert lineend')
    indentation1 = line1[/^\s+/] || ''
    record.insert(:insert, "\n")

    line2 = text.get('insert linestart', 'insert lineend')
    indentation2 = line2[/^\s+/] || ''

    record.replace(
      'insert linestart',
      "insert linestart + #{indentation2.size} chars",
      indentation1
    )

    Control.clean_line(text, 'insert - 1 line', record)
  end
end

.leave_replace(buffer, old_mode, new_mode) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ver/methods/insert.rb', line 87

def leave_replace(buffer, old_mode, new_mode)
  # repeat replacment
  count = buffer.store(self, :replace_count)
  chars = buffer.store(self, :replace_chars).dup

  buffer.undo_record do |record|
    count.times do
      common_string(buffer, chars, record)
    end
  end
end

.literal(buffer) ⇒ Object

Insert characters literally, or enter decimal byte value (3 digits). This means we try to get up to 3 digits, but possibly don’t get any.

This code is less than elegant, but so is the behaviour we try to achieve.

(none) decimal 3 255 o or O octal 3 377 (255) x or X hexadecimal 2 ff (255) u hexadecimal 4 ffff (65535) U hexadecimal 8 7fffffff (2147483647)



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ver/methods/insert.rb', line 145

def literal(buffer)
  reader = ->(string = ''){
    buffer.major_mode.read(1) do |event|
      if unicode = event.unicode
        string += unicode # copy
        buffer.message string.inspect

        case result = literal_handle(buffer, string)
        when nil
          reader.call(string)
        when String
          literal_insert(buffer, result)
        end
      else
        return # Unverrichteter Dinge
      end
    end
  }

  reader.call
end

.literal_handle(buffer, string) ⇒ Object

returning nil means read next char returning a String means you’re done and want the result inserted. returning anything else means you’re giving up.



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

def literal_handle(buffer, string)
  case string
  when /^\d{,3}$/
    return if string.size < 3
    [string.to_i].pack('U')
  when /^o([0-7]{,3})$/i
    return if $1.size < 3
    [Integer("0#$1")].pack('U')
  when /^x(\h{,2})$/i
    return if $1.size < 2
    [Integer("0x#$1")].pack('U')
  when /^u(\h{,4})$/
    return if $1.size < 4
    [Integer("0x#$1")].pack('U')
  when /^U(\h{,8})$/
    return if $1.size < 8
    [Integer("0x#$1")].pack('U')
  end
end

.literal_insert(buffer, char) ⇒ Object



190
191
192
# File 'lib/ver/methods/insert.rb', line 190

def literal_insert(buffer, char)
  buffer.at_insert.insert(char)
end

.newline(buffer) ⇒ Object



11
# File 'lib/ver/methods/insert.rb', line 11

def newline(buffer) buffer.insert_newline end

.newline_above(text) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ver/methods/insert.rb', line 40

def newline_above(text)
  Undo.record text do |record|
    if text.index(:insert).y > 1
      Move.prev_line(text)
      newline_below(text)
    else
      record.insert('insert linestart', "\n")
      text.mark_set(:insert, 'insert - 1 line')
      Control.clean_line(text, 'insert - 1 line', record)
      text.minor_mode(:control, :insert)
    end
  end
end

.newline_below(text) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ver/methods/insert.rb', line 21

def newline_below(text)
  Undo.record text do |record|
    if text.options.autoindent
      line = text.get('insert linestart', 'insert lineend')

      indent = line[/^\s*/]
      text.insert = 'insert lineend'
      record.insert(:insert, "\n#{indent}")
    else
      text.insert = 'insert lineend'
      record.insert(:insert, "\n")
    end

    Control.clean_line(text, 'insert - 1 line', record)
  end

  text.minor_mode(:control, :insert)
end

.replace_char(buffer, replacement = buffer.event.unicode, count = buffer.prefix_count) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ver/methods/insert.rb', line 107

def replace_char(buffer, replacement = buffer.event.unicode, count = buffer.prefix_count)
  buffer.undo_record do |record|
    count.times do
      record.delete(:insert, 'insert + 1 chars')
      common_string(buffer, replacement, record)
    end
  end
  buffer.skip_prefix_count_once = replacement =~ /^\d+$/
  buffer.mark_set(:insert, 'insert - 1 chars')
  buffer.minor_mode(:replace_char, :control)
end

.replace_string(buffer, replacement = buffer.event.unicode) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/ver/methods/insert.rb', line 99

def replace_string(buffer, replacement = buffer.event.unicode)
  buffer.undo_record do |record|
    record.delete(:insert, 'insert + 1 chars')
    buffer.store(self, :replace_chars) << replacement
    common_string(buffer, replacement, record)
  end
end

.selection(buffer) ⇒ Object



12
# File 'lib/ver/methods/insert.rb', line 12

def selection(buffer) buffer.insert_selection end

.string(text) ⇒ Object

Most of the input will be in US-ASCII, but an encoding can be set per buffer for the input. For just about all purposes, UTF-8 should be what you want to input, and it’s what Tk can handle best.



77
78
79
# File 'lib/ver/methods/insert.rb', line 77

def string(text)
  common_string(text, text.event.unicode)
end

.tab(buffer) ⇒ Object



10
# File 'lib/ver/methods/insert.rb', line 10

def tab(buffer)     buffer.insert_tab end