Module: VER::Methods::Insert

Included in:
VER::Methods, Shortcuts
Defined in:
lib/ver/methods/insert.rb

Instance Method Summary collapse

Instance Method Details

#auto_indent_lineObject



173
174
175
176
177
# File 'lib/ver/methods/insert.rb', line 173

def auto_indent_line
  if @syntax
    syntax_indent_line
  end
end

#fallback_insert_indented_newlineObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ver/methods/insert.rb', line 138

def fallback_insert_indented_newline
  line1 = get('insert linestart', 'insert lineend')
  indentation1 = line1[/^\s+/] || ''
  insert :insert, "\n"

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

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

  clean_line('insert - 1 line')
end

#indent_after(index, indent = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ver/methods/insert.rb', line 82

def indent_after(index, indent = nil)
  settings = indent_settings.values_at(:increase, :decrease, :indent_next, :unindented)

  return unless settings.any?
  increase, decrease, indent_next, unindented = settings

  index = index(index)
  indent ||= (index.y > 1 ? indent_after(index.prev) : 0)

  linestart, lineend  = index.linestart, index.lineend
  line = get(linestart, lineend).strip

  if increase && decrease && line =~ increase && line =~ decrease
    indent -= 1
    indent += 1
  elsif decrease && line =~ decrease
    indent -= 1
  elsif increase && line =~ increase
    indent += 1
  elsif line =~ /^\s*$/
  else
  end

  return indent
end

#indent_fix_at(index, indent) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ver/methods/insert.rb', line 108

def indent_fix_at(index, indent)
  settings = indent_settings.values_at(:increase, :decrease, :indent_next, :unindented)

  return unless settings.any?
  increase, decrease, indent_next, unindented = settings

  index = index(index)

  linestart, lineend  = index.linestart, index.lineend
  line = get(linestart, lineend).strip

  if increase && decrease && line =~ increase && line =~ decrease
    indent -= 1
    replace(linestart, lineend, ('  ' * indent) << line)
    indent += 1
  elsif decrease && line =~ decrease
    replace(linestart, lineend, ('  ' * indent) << line)
    indent -= 1
  elsif increase && line =~ increase
    replace(linestart, lineend, ('  ' * indent) << line)
    indent += 1
  elsif line =~ /^\s*$/
    replace(linestart, lineend, ('  ' * indent) << line)
  else
    replace(linestart, lineend, ('  ' * indent) << line)
  end

  return indent
end

#indent_of(index) ⇒ Object



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

def indent_of(index)
  index = index(index)
  return 0 if index.y < 1

  line = get(index.linestart, index.lineend)

  if line =~ /^\s*$/
    return indent_of(index.prev)
  else
    indent_size = line[/^\s*/].size
    return 0 if indent_size == 0
    return indent_size / 2
  end
end

#indent_settingsObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ver/methods/insert.rb', line 179

def indent_settings
  return {} unless @syntax
  name = @syntax.name
  file = VER.find_in_loadpath("preferences/#{name}.json")
  pref = JSON.load(File.read(file))

  indent_settings = {}

  pref.each do |key, value|
    settings = value['settings']
    indent_settings[:increase]    ||= settings['increaseIndentPattern']
    indent_settings[:decrease]    ||= settings['decreaseIndentPattern']
    indent_settings[:indent_next] ||= settings['indentNextLinePattern']
    indent_settings[:unindented]  ||= settings['unIndentedLinePattern']
  end


  [:increase, :decrease, :indent_next, :unindented].each do |key|
    if value = indent_settings[key]
      indent_settings[key] = Regexp.new(value)
    else
      indent_settings.delete(key)
    end
  end

  return indent_settings
end

#insert_auto_indented_newlineObject



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

def insert_auto_indented_newline
  insert 'insert lineend', "\n"
  indent_fix_at('insert', indent_fix('insert - 1 line', indent_after('insert - 2 line')))
rescue Errno::ENOENT, TypeError
  fallback_insert_indented_newline
end

#insert_file_contents(filename) ⇒ Object



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

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

#insert_indented_newlineObject



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

def insert_indented_newline
  if VER.options.autoindent
    fallback_insert_indented_newline
  else
    insert_newline
  end
end

#insert_indented_newline_aboveObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ver/methods/insert.rb', line 35

def insert_indented_newline_above
  if index(:insert).y > 1
    previous_line
    insert_indented_newline_below
  else
    insert('insert linestart', "\n")
    mark_set(:insert, 'insert - 1 line')
  end

  clean_line('insert + 1 line')
  start_insert_mode
end

#insert_indented_newline_belowObject



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

def insert_indented_newline_below
  if VER.options.autoindent
    line = get('insert linestart', 'insert lineend')

    indent = line.empty? ? "" : (line[/^\s+/] || '')
    mark_set :insert, 'insert lineend'
    insert :insert, "\n#{indent}"
  else
    mark_set :insert, 'insert lineend'
    insert :insert, "\n"
  end

  clean_line('insert - 1 line')
  start_insert_mode
end

#insert_newlineObject



48
49
50
# File 'lib/ver/methods/insert.rb', line 48

def insert_newline
  insert :insert, "\n"
end

#insert_selectionObject



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

def insert_selection
  insert :insert, Tk::Selection.get
end

#insert_string(string) ⇒ Object

Most of the input will be in US-ASCII, but an encoding can be set per view 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.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ver/methods/insert.rb', line 158

def insert_string(string)
  return if string.empty?

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

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

#insert_tabObject



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

def insert_tab
  insert :insert, "\t"
end

#syntax_indent_fileObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ver/methods/insert.rb', line 207

def syntax_indent_file
  settings = indent_settings.values_at(:increase, :decrease, :indent_next, :unindented)

  return unless settings.any?
  increase, decrease, indent_next, unindented = settings

  empty_line = /^\s*$/
  indent = 0

  index('1.0').upto(index('end')) do |pos|
    pos_lineend = pos.lineend
    line = get(pos, pos_lineend).strip

    if increase && decrease && line =~ increase && line =~ decrease
      indent -= 1
      replace(pos, pos_lineend, ('  ' * indent) << line)
      indent += 1
    elsif decrease && line =~ decrease
      indent -= 1
      replace(pos, pos_lineend, ('  ' * indent) << line)
    elsif increase && line =~ increase
      replace(pos, pos_lineend, ('  ' * indent) << line)
      indent += 1
    elsif line =~ empty_line
    else
      replace(pos, pos_lineend, ('  ' * indent) << line)
    end
  end
end