Module: VER::Methods::Control
- Included in:
- VER::Methods
- Defined in:
- lib/ver/methods/control.rb
Instance Method Summary collapse
- #buffer_switch ⇒ Object
- #clean_line(index) ⇒ Object
- #eval_buffer ⇒ Object
- #file_open_fuzzy ⇒ Object
- #file_open_popup ⇒ Object
-
#gsub(regexp, with) ⇒ Object
Substitute over all lines of the buffer.
- #indent_line ⇒ Object
- #join_lines ⇒ Object
- #line_evaluate ⇒ Object
- #open_console ⇒ Object
- #open_grep_list ⇒ Object
- #open_method_list ⇒ Object
- #open_terminal ⇒ Object
- #redo ⇒ Object
- #replace_char ⇒ Object
- #smart_evaluate ⇒ Object
- #start_control_mode ⇒ Object
- #start_insert_mode ⇒ Object
- #status_evaluate ⇒ Object
-
#status_ex ⇒ Object
TODO: make this better?.
- #status_ex_filter(input) ⇒ Object
- #status_theme_select ⇒ Object
- #stdout_capture_evaluate(code) ⇒ Object
-
#sub(regexp, with) ⇒ Object
Substitute on current line.
- #syntax_switch ⇒ Object
- #tags_at(index = :insert) ⇒ Object
- #theme_switch ⇒ Object
- #undo ⇒ Object
- #unindent_line ⇒ Object
- #window_switch(count = nil) ⇒ Object
- #wrap_line ⇒ Object
Instance Method Details
#buffer_switch ⇒ Object
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) = line.rstrip replace(from, to, ) if .empty? end |
#eval_buffer ⇒ Object
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_fuzzy ⇒ Object
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_popup ⇒ Object
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 "Performed gsub on #{total} lines" end |
#indent_line ⇒ Object
227 228 229 |
# File 'lib/ver/methods/control.rb', line 227 def indent_line insert('insert linestart', ' ') end |
#join_lines ⇒ Object
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. =~ /Index "\d+\.\d+" before "insert lineend" in the text/ Kernel.raise exception end |
#line_evaluate ⇒ Object
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_console ⇒ Object
83 84 85 |
# File 'lib/ver/methods/control.rb', line 83 def open_console View::Console.new(self) end |
#open_grep_list ⇒ Object
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_list ⇒ Object
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_terminal ⇒ Object
87 88 89 90 |
# File 'lib/ver/methods/control.rb', line 87 def open_terminal require 'ver/view/term' View::Terminal.new(self) end |
#redo ⇒ Object
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. end |
#replace_char ⇒ Object
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. 'replace aborted' end end end |
#smart_evaluate ⇒ Object
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_mode ⇒ Object
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_mode ⇒ Object
251 252 253 |
# File 'lib/ver/methods/control.rb', line 251 def start_insert_mode self.mode = :insert end |
#status_evaluate ⇒ Object
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_ex ⇒ Object
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. "%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_select ⇒ Object
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_switch ⇒ Object
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 (index = :insert) index = index(index) = tag_names(index) .inspect require 'ver/tooltip' tooltip = Tk::Tooltip.new(.inspect) tooltip.show_on(self) Tk::After.ms(5000){ tooltip.destroy } end |
#theme_switch ⇒ Object
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 |
#undo ⇒ Object
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. end |
#unindent_line ⇒ Object
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_line ⇒ Object
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 = [:textwidth] lines = wrap_lines_of(text, textwidth).join("\n") lines.rstrip! replace('insert linestart', 'insert lineend', lines) end |