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



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

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

.file_contents(filename) ⇒ Object



6
7
8
9
10
11
# File 'lib/ver/methods/insert.rb', line 6

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

.indented_newline(text) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ver/methods/insert.rb', line 64

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

.newline(text) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ver/methods/insert.rb', line 21

def newline(text)
  if text.options.autoindent
    indented_newline(text)
  else
    text.insert(:insert, "\n")
  end
end

.newline_above(text) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ver/methods/insert.rb', line 50

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ver/methods/insert.rb', line 29

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

      indent = line[/^\s*/]
      text.mark_set(:insert, 'insert lineend')
      record.insert(:insert, "\n#{indent}")
    else
      text.mark_set(: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(text, replacement = text.event.unicode) ⇒ Object



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

def replace_char(text, replacement = text.event.unicode)
  Undo.record text do |record|
    record.delete(:insert, 'insert + 1 chars')
    common_string(text, replacement, record)
  end
  text.mark_set(:insert, 'insert - 1 chars')
  text.minor_mode(:replace_char, :control)
end

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



91
92
93
94
95
96
# File 'lib/ver/methods/insert.rb', line 91

def replace_string(text, replacement = text.event.unicode)
  Undo.record text do |record|
    record.delete(:insert, 'insert + 1 chars')
    common_string(text, replacement, record)
  end
end

.selection(text) ⇒ Object



13
14
15
# File 'lib/ver/methods/insert.rb', line 13

def selection(text)
  text.insert(:insert, Tk::Selection.get)
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.



87
88
89
# File 'lib/ver/methods/insert.rb', line 87

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

.tab(text) ⇒ Object



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

def tab(text)
  text.insert(:insert, "\t")
end