File: ruby-dialog.rb

package info (click to toggle)
kazehakase 0.4.2-1etch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 7,260 kB
  • ctags: 7,377
  • sloc: ansic: 62,697; cpp: 13,422; sh: 9,083; ruby: 1,588; makefile: 1,026; xml: 372
file content (492 lines) | stat: -rw-r--r-- 13,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
107
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
206
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#
#   Copyright (C) 2006 Eriko Sato
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

require "kz/ruby-completion"
require "kz/search-window"

module Kz
  class SandBox
    def initialize(kz)
      @kz = kz
      @binding = binding
    end

    def _binding
      @binding
    end

    def evaluate(statements, file=__FILE__, line=__LINE__)
      eval(statements, @binding, file, line)
    end
  end

  class RubyDialog
    HISTORY_PATH = File.join(Kz::CONFIG_DIR, "ruby-command-history")
    @@history ||= nil
    @@history_spins ||= []

    attr_reader :dialog
    def initialize(kz)
      @kz = kz
      @default_font_size = 14
      init_dialog
      @dialog.show_all
      @entry.grab_focus
      init_sandbox
    end

    def clear_history
      @@history.clear
      update_history_spins_range
      nil
    end

    private
    def _(str)
      Kz.gettext(str)
    end

    def init_sandbox
      @sandbox = SandBox.new(@kz)
      @sandbox.instance_variable_set("@dialog", self)
      @sandbox.evaluate("dialog = @dialog")
    end

    def init_dialog
      @dialog = Gtk::Dialog.new(_("Ruby dialog"))
      @dialog.set_size_request(400, 300)
      @dialog.signal_connect("destroy") do |widget, event|
        Kz.barrier do
          save_history
          @@history_spins.delete(@history_spin)
        end
        false
      end
      init_history
      init_search
      init_output_area
      init_input_area
      init_buttons
      init_redirector
    end

    def init_history
      @@history ||= load_history
    end

    def load_history
      history = []
      return history unless File.exist?(HISTORY_PATH)
      Kz.barrier do
        File.open(HISTORY_PATH) do |f|
          f.each do |line|
            line.strip!
            history << line unless line.empty?
          end
        end
      end
      history
    end

    def save_history
      Kz.barrier do
        File.open(HISTORY_PATH, "w") do |f|
          f.puts(@@history.join("\n"))
        end
      end
    end

    def init_search
      searcher = Object.new
      def searcher.regexp(text)
        /#{Regexp.quote(text)}/i
      end
      @search_window = SearchWindow.new(searcher)
      @search_window.window.set_transient_for(@dialog)
      entry = @search_window.entry
      update_widget_font(entry, nil, "monospace")
      direction = @search_window.direction
      entry.signal_connect("key_press_event") do |widget, event|
        handled = false
        if event.state.control_mask?
          handled = true
          case event.keyval
          when Gdk::Keyval::GDK_s
            search_history(true)
          when Gdk::Keyval::GDK_r
            search_history(false)
          when Gdk::Keyval::GDK_g
            stop_history_search
          else
            handled = false
          end
        end
        handled
      end
      entry.signal_connect("changed") do
        search_history_with_current_input
      end
      direction.signal_connect("toggled") do
        search_history_with_current_input(true)
      end
      entry.signal_connect("activate") do
        stop_history_search
        true
      end
    end

    def init_output_area
      init_text_view
    end

    def init_text_view
      @view = Gtk::TextView.new
      @view.wrap_mode = Gtk::TextTag::WRAP_WORD_CHAR
      @view.editable = false
      @buffer = @view.buffer
      init_mark
      init_tags
      @buffer.signal_connect_after("insert_text") do |widget, iter, text, len|
        start = @buffer.get_iter_at_offset(iter.offset - len)
        @buffer.apply_tag(@all_tag, start, iter)
        false
      end
      sw = add_scrolled_window(@view)
      @dialog.vbox.pack_start(sw, true, true, 0)
    end

    def init_input_area
      init_history_spin_button
      init_input_entry

      input_hbox = Gtk::HBox.new
      input_hbox.pack_start(@history_spin, false, true, 0)
      input_hbox.pack_start(@entry, true, true, 0)
      @dialog.vbox.pack_start(input_hbox, false, true, 0)
    end

    def init_history_spin_button
      adjustment ||= Gtk::Adjustment.new(@@history.size, 0,
                                         @@history.size, 1, 4, 0)
      @history_spin = Gtk::SpinButton.new(adjustment, 1, 0)
      @history_spin.signal_connect_after("value-changed") do |widget, type|
        @last_history_index ||= @@history.size
        @entry_last_text = @entry.text if @last_history_index == @@history.size
        @last_history_index = @history_spin.value.to_i
        update_input_entry
        false
      end
      update_widget_font(@history_spin)
      @@history_spins << @history_spin
    end

    def init_input_entry
      @entry = Gtk::Entry.new
      @entry_last_text = nil
      update_widget_font(@entry, nil, "monospace")
      setup_input_entry_ruby_completion
      @entry.signal_connect("key_press_event") do |widget, event|
        Kz.barrier do
          handle_input(event)
        end
      end
      @entry.signal_connect("activate") do |widget, event|
        Kz.barrier do
          catch(:exit) {activate_input}
        end
        false
      end
      @entry.signal_connect("changed") do |widget, event|
        Kz.barrier do
          update_input_entry_ruby_completion
        end
      end
      @entry
    end

    def update_input_entry
      index = @history_spin.value.to_i
      @entry.text = @@history[index] || @entry_last_text || ""
      @entry.position = -1
    end

    def setup_input_entry_ruby_completion
      @ruby_exp_completion = Gtk::EntryCompletion.new
      @ruby_exp_model = Gtk::ListStore.new(String)
      @ruby_exp_completion.model = @ruby_exp_model
      @ruby_exp_completion.text_column = 0
      @entry.completion = @ruby_exp_completion
    end

    def update_input_entry_ruby_completion
      return if @entry.text.strip.empty?
      result = Kz::RubyCompletion.complete(@entry.text, @sandbox._binding)
      @ruby_exp_model.clear
      result.each do |item|
        iter = @ruby_exp_model.append
        iter[0] = item
      end
    end

    def update_widget_font(widget, size=nil, family=nil)
      size ||= @default_font_size
      desc = widget.style.font_desc.copy
      desc.size = size * Pango::SCALE
      desc.family = family if family
      widget.modify_font(desc)
    end

    def init_mark
      @end_mark = @buffer.create_mark("end", @buffer.end_iter, true)
    end

    def init_tags
      result_tag_prop = {:foreground => "HotPink"}
      @result_tag = @buffer.create_tag("result", result_tag_prop)
      input_tag_prop = {:foreground => "Green"}
      @input_tag = @buffer.create_tag("input", input_tag_prop)
      output_tag_prop = {:foreground => "Blue"}
      @output_tag = @buffer.create_tag("output", output_tag_prop)
      all_tag_prop = {
        :family => "monospace",
        :size_points => @default_font_size
      }
      @all_tag = @buffer.create_tag("all", all_tag_prop)
    end

    def handle_input(event)
      handled = false
      case event.keyval
      when Gdk::Keyval::GDK_m
        @entry.activate if event.state.control_mask?
      when Gdk::Keyval::GDK_p
        handled = previous_history if event.state.control_mask?
      when Gdk::Keyval::GDK_Up
        handled = previous_history
        handled = true
      when Gdk::Keyval::GDK_n
        handled = next_history if event.state.control_mask?
      when Gdk::Keyval::GDK_Down
        handled = next_history
        handled = true
      when Gdk::Keyval::GDK_i
        @ruby_exp_completion.insert_prefix if event.state.control_mask?
      when Gdk::Keyval::GDK_s
        if event.state.control_mask?
          search_history(true)
          handled = true
        end
      when Gdk::Keyval::GDK_r
        if event.state.control_mask?
          search_history(false)
          handled = true
        end
      end
      handled
    end

    def activate_input
      text = @entry.text.strip
      if text.empty?
        @history_spin.value = @@history.size
      else
        eval_print(text)
        update_history(text)
      end
      @entry.text = ""
    end

    def update_history(text)
      if @@history.last != text
        @@history << text
        update_history_spins_range
      end
      @history_spin.value = @@history.size
    end

    def update_history_spins_range
      @@history_spins.each do |spin|
        spin.set_range(0, @@history.size)
      end
    end

    def previous_history
      handled = false
      if @history_spin.value > 0
        @history_spin.value -= 1
        handled = true
      end
      handled
    end

    def next_history
      handled = false
      if @history_spin.value < @@history.size
        @history_spin.value += 1
        handled = true
      end
      handled
    end

    def search_history(forward=false)
      unless @search_window.visible?
        @search_window.show
        adjust_search_window
      end

      if @search_window.forward? == forward
        search_history_with_current_input(true)
      else
        @search_window.forward = forward
      end
    end

    def stop_history_search
      @search_window.hide
      @search_window.entry.text = ""
    end

    def adjust_search_window
      Utils.move_to_bottom_left_outer(@entry, @search_window.window)
    end

    def search_history_with_current_input(search_next=false)
      Kz.barrier do
        return if @search_window.empty?
        change_to_matched_history(@search_window.regexp,
                                  @search_window.forward?,
                                  search_next)
      end
    end

    def change_to_matched_history(reg, forward, search_next=false)
      current_index = @history_spin.value.to_i
      indexes = []
      @@history.each_with_index do |text, i|
        indexes << i if reg =~ text
      end
      if @entry_last_text and reg =~ @entry_last_text
        indexes << @@history.size
      end
      target_index = nil
      indexes.each_with_index do |index, i|
        if index == current_index
          target_index = i
          target_index += (forward ? 1 : -1) if search_next
          break
        elsif index > current_index
          target_index = i + (forward ? 0 : -1)
          break
        end
      end
      target_index = indexes.size - 1 if target_index.nil? and !forward
      if target_index and target_index >= 0 and target_index < indexes.size
        @history_spin.value = indexes[target_index].to_f
        @entry.position = (reg =~ @entry.text || 0)
      end
    end

    def eval_print(text)
      @buffer.insert(@buffer.end_iter, ">> ")
      @buffer.insert(@buffer.end_iter, text, @input_tag)
      @buffer.insert(@buffer.end_iter, "\n")
      result = eval_text(text)
      result = utf8_environment {result.inspect}
      @buffer.insert(@buffer.end_iter, "=> ")
      @buffer.insert(@buffer.end_iter, result, @result_tag)
      @buffer.insert(@buffer.end_iter, "\n")
      @buffer.move_mark(@end_mark, @buffer.end_iter)
      @view.scroll_to_mark(@end_mark, 0, false, 0, 1)
    end

    def add_scrolled_window(widget)
      sw = Gtk::ScrolledWindow.new
      sw.border_width = 5
      sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
      sw.add(widget)
      sw
    end
    
    def init_buttons
      init_font_size_spin_button
      init_exit_button
    end

    def init_font_size_spin_button
      adjustment = Gtk::Adjustment.new(@default_font_size, 8, 72, 1, 4, 0)
      button = Gtk::SpinButton.new(adjustment, 1, 0)
      button.signal_connect("value-changed") do |widget, type|
        @all_tag.size_points = widget.value
        update_widget_font(@entry, widget.value)
        update_widget_font(@history_spin, widget.value)
        update_widget_font(@search_window.entry, widget.value)
        false
      end
      @dialog.action_area.add(button)
      @dialog.action_area.set_child_secondary(button, true)
    end
    
    def init_exit_button
      button = Gtk::Button.new(_("_Close"))
      button.signal_connect("clicked") do |widget, event|
        @dialog.destroy
      end
      @dialog.action_area.add(button)
    end

    def init_redirector
      @buffer_out = Object.new
      @buffer_out.instance_variable_set("@buffer", @buffer)
      @buffer_out.instance_variable_set("@output_tag", @output_tag)
      class << @buffer_out
        def write(str)
          @buffer.insert(@buffer.end_iter, str, @output_tag)
        end
      end
    end

    def eval_text(text)
      redirect do
        @sandbox.evaluate(text)
      end
    rescue SystemExit
      @dialog.destroy
      throw(:exit)
    rescue Exception
      $!
    end

    def redirect
      stdout = $stdout
      $stdout = @buffer_out
      utf8_environment do
        yield
      end
    ensure
      $stdout = stdout
    end

    def utf8_environment
      kcode = $KCODE
      $KCODE = "UTF8"
      yield
    ensure
      $KCODE = kcode if $KCODE == "UTF8"
    end
  end
end