Module: VER::Methods::Control

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

Instance Method Summary collapse

Instance Method Details

#buffer_switchObject



171
172
173
174
175
# File 'lib/ver/methods/control.rb', line 171

def buffer_switch
  View::List::Buffer.new self do |file|
    view.find_or_create(file) if File.exists?(file)
  end
end

#clean_line(index) ⇒ Object



243
244
245
246
247
248
249
# File 'lib/ver/methods/control.rb', line 243

def clean_line(index)
  index = index(index)
  from, to = index.linestart, index.lineend
  line = get(from, to)
  bare = line.rstrip
  replace(from, to, bare) if bare.empty?
end

#eval_bufferObject



165
166
167
168
169
# File 'lib/ver/methods/control.rb', line 165

def eval_buffer
  result = eval(value, TOPLEVEL_BINDING)
rescue Exception => exception
  VER.error(exception)
end

#file_open_fuzzyObject



201
202
203
204
205
# File 'lib/ver/methods/control.rb', line 201

def file_open_fuzzy
  View::List::FuzzyFileFinder.new self do |path|
    view.find_or_create(path)
  end
end

#file_open_popupObject



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ver/methods/control.rb', line 188

def file_open_popup
  filetypes = [
    ['ALL Files',  '*'    ],
    ['Text Files', '*.txt'],
  ]

  fpath = Tk.get_open_file(filetypes: filetypes)

  return unless fpath

  view.find_or_create(fpath)
end

#gsub(regexp, with) ⇒ Object

Substitute over all lines of the buffer



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

def gsub(regexp, with)
  total = 0
  index('1.0').upto(index('end')) do |index|
    lineend = index.lineend
    line = get(index, lineend)

    if line.gsub!(regexp, with)
      replace(index, lineend, line)
      total += 1
    end
  end

  message "Performed gsub on #{total} lines"
end

#indent_lineObject



227
228
229
# File 'lib/ver/methods/control.rb', line 227

def indent_line
  insert('insert linestart', '  ')
end

#join_linesObject



207
208
209
210
211
212
213
# File 'lib/ver/methods/control.rb', line 207

def join_lines
  start_of_next_line = search(/\S/, 'insert lineend').first
  replace('insert lineend', start_of_next_line, ' ')
rescue RuntimeError => exception
  return if exception.message =~ /Index "\d+\.\d+" before "insert lineend" in the text/
  Kernel.raise exception
end

#line_evaluateObject



140
141
142
143
144
145
# File 'lib/ver/methods/control.rb', line 140

def line_evaluate
  text = get('insert linestart', 'insert lineend')
  stdout_capture_evaluate(text) do |res,out|
    insert("insert lineend", "\n%s%p" % [out, res] )
  end
end

#open_consoleObject



83
84
85
# File 'lib/ver/methods/control.rb', line 83

def open_console
  View::Console.new(self)
end

#open_grep_listObject



44
45
46
47
48
# File 'lib/ver/methods/control.rb', line 44

def open_grep_list
  View::List::Grep.new self do |file, line|
    view.find_or_create(file, line)
  end
end

#open_method_listObject



50
51
52
53
54
# File 'lib/ver/methods/control.rb', line 50

def open_method_list
  View::List::Methods.new self do |file, line|
    view.find_or_create(file, line)
  end
end

#open_terminalObject



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

def open_terminal
  require 'ver/view/term'
  View::Terminal.new(self)
end

#redoObject



267
268
269
270
271
272
# File 'lib/ver/methods/control.rb', line 267

def redo
  edit_redo
  touch!
rescue RuntimeError => ex
  status.value = ex.message
end

#replace_charObject



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

def replace_char
  status_ask 'Replace with: ', take: 1 do |char|
    if char.size == 1
      replace('insert', 'insert + 1 chars', char)
      backward_char
      "replaced #{char.size} chars"
    else
      status.message 'replace aborted'
    end
  end
end

#smart_evaluateObject



131
132
133
134
135
136
137
138
# File 'lib/ver/methods/control.rb', line 131

def smart_evaluate
  if sel = tag_ranges(:sel)
    from, to = sel.first
    return selection_evaluate if from && to
  end

  line_evaluate
end

#start_control_modeObject



255
256
257
258
# File 'lib/ver/methods/control.rb', line 255

def start_control_mode
  clean_line(:insert)
  self.mode = :control
end

#start_insert_modeObject



251
252
253
# File 'lib/ver/methods/control.rb', line 251

def start_insert_mode
  self.mode = :insert
end

#status_evaluateObject



121
122
123
124
125
126
127
128
129
# File 'lib/ver/methods/control.rb', line 121

def status_evaluate
  status_ask 'Eval expression: ' do |term|
    begin
      eval(term)
    rescue Exception => ex
      ex
    end
  end
end

#status_exObject

TODO: make this better?



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ver/methods/control.rb', line 57

def status_ex
  completion = method(:status_ex_filter)

  View::List::Ex.new self, completion do |command, propose|
    begin
      result = propose ? send(command, propose) : eval(command)
      status.message "%s # => %p" % [command, result]
    rescue Exception => ex
      status.error "%s # => %p" % [command, ex]
    end
  end
end

#status_ex_filter(input) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ver/methods/control.rb', line 70

def status_ex_filter(input)
  input = input.to_s.split.last
  return [] if !input || input.empty?

  begin
    regexp = Regexp.new(input)
  rescue ArgumentError, RegexpError, SyntaxError
    regexp = Regexp.new(Regexp.escape(input))
  end

  self.methods.grep(regexp).sort_by{|sym| sym =~ regexp }
end

#status_theme_selectObject



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

def status_theme_select
  return unless @syntax

  status_ask 'Theme name: ' do |name|
    load_theme(name) || "No theme called #{name} found"
  end
end

#stdout_capture_evaluate(code) ⇒ Object



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

def stdout_capture_evaluate(code)
  begin
    old_stdout = $stdout.dup
    rd, wr = IO.pipe
    $stdout.reopen(wr)
    result = eval(code)
    $stdout.reopen old_stdout; wr.close
    stdout = rd.read

    yield(result, stdout)
  rescue Exception => exception
    yield(exception, '')
  ensure
    wr.closed? || $stdout.reopen(old_stdout) && wr.close
    rd.closed? || rd.close
  end
end

#sub(regexp, with) ⇒ Object

Substitute on current line



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

def sub(regexp, with)
  linestart = index('insert linestart')
  lineend = linestart.lineend
  line = get(linestart, lineend)

  if line.sub!(regexp, with)
    replace(linestart, lineend, line)
  end
end

#syntax_switchObject



115
116
117
118
119
# File 'lib/ver/methods/control.rb', line 115

def syntax_switch
  return unless @syntax

  View::List::Syntax.new(self){|name| load_syntax(name) }
end

#tags_at(index = :insert) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/ver/methods/control.rb', line 4

def tags_at(index = :insert)
  index = index(index)
  tags = tag_names(index)
  message tags.inspect

  require 'ver/tooltip'

  tooltip = Tk::Tooltip.new(tags.inspect)
  tooltip.show_on(self)

  Tk::After.ms(5000){ tooltip.destroy }
end

#theme_switchObject



109
110
111
112
113
# File 'lib/ver/methods/control.rb', line 109

def theme_switch
  return unless @syntax

  View::List::Theme.new(self){|name| load_theme(name) }
end

#undoObject



260
261
262
263
264
265
# File 'lib/ver/methods/control.rb', line 260

def undo
  edit_undo
  touch!
rescue RuntimeError => ex
  status.value = ex.message
end

#unindent_lineObject



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/ver/methods/control.rb', line 231

def unindent_line
 line = get('insert linestart', 'insert lineend')

 return unless line =~ /^(\s\s?)/

 replace(
   'insert linestart',
   "insert linestart + #{$1.size} chars",
   ''
 )
end

#window_switch(count = nil) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/ver/methods/control.rb', line 177

def window_switch(count = nil)
  if count
    # p count: count
  else
    View::List::Window.new self do |view|
      view.push_top
      view.focus
    end
  end
end

#wrap_lineObject



92
93
94
95
96
97
98
99
# File 'lib/ver/methods/control.rb', line 92

def wrap_line
  text = get('insert linestart', 'insert lineend')
  textwidth = options[:textwidth]
  lines = wrap_lines_of(text, textwidth).join("\n")
  lines.rstrip!

  replace('insert linestart', 'insert lineend', lines)
end