-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfxirb.rb
218 lines (178 loc) · 3.9 KB
/
fxirb.rb
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
#! /usr/bin/env ruby
require 'fox16'
require 'guirb'
require 'singleton'
require 'fxkeys'
include Fox
class FXEvent
def ctrl?
(self.state & CONTROLMASK) != 0
end
def shift?
(self.state & SHIFTMASK) != 0
end
end
class FXIrb < FXText
include Responder
attr_accessor :multiline
attr_accessor :irb
def initialize(p, tgt, sel, opts)
FXMAPFUNC(SEL_KEYRELEASE, 0, "onKeyRelease")
FXMAPFUNC(SEL_KEYPRESS, 0, "onKeyPress")
FXMAPFUNC(SEL_LEFTBUTTONPRESS, 0, "onLeftBtnPress")
FXMAPFUNC(SEL_MIDDLEBUTTONPRESS, 0, "onMiddleBtnPress")
FXMAPFUNC(SEL_LEFTBUTTONRELEASE, 0, "onLeftBtnRelease")
super
setFont(FXFont.new(FXApp.instance, "courier", 9))
@anchor = 0
@exit_proc = lambda {}
end
def create
super
setFocus
end
def on_irb_exit(&block)
@exit_proc = block
end
def handle_irb_exit
instance_eval(&@exit_proc)
end
private
def onLeftBtnPress(sender,sel,event)
@store_anchor = @anchor
setFocus
super
end
def onLeftBtnRelease(sender,sel,event)
super
@anchor = @store_anchor
setCursorPos(getLength)
end
def onMiddleBtnPress(sender,sel,event)
pos = getPosAt(event.win_x,event.win_y)
if pos >= @anchor
super
end
end
def auto_dedent
str = get_frame
@anchor -= 2
clear_frame
appendText(str)
setCursorPos(getLength)
end
def history(dir)
if (s = @irb.history(dir))
clear_frame
write(s)
end
end
def quit_irb
clear_frame
appendText("exit")
new_line_entered
handle_irb_exit
end
def get_frame
extractText(@anchor, getLength-@anchor)
end
def invalid_pos?
getCursorPos < @anchor
end
def can_move_left?
getCursorPos > @anchor
end
def move_to_start_of_frame
setCursorPos(@anchor)
end
def move_to_end_of_frame
setCursorPos(getLength)
end
def move_to_start_of_line
if multiline
cur = getCursorPos
pos = lineStart(cur)
pos = @anchor if pos < @anchor
else
pos = @anchor
end
setCursorPos(pos)
end
def move_to_end_of_line
if multiline
cur = getCursorPos
pos = lineEnd(cur)
else
pos = getLength
end
setCursorPos(pos)
end
def get_from_start_of_line
extractText(@anchor, getCursorPos-@anchor)
end
def get_to_end_of_line
extractText(getCursorPos, getLength - getCursorPos)
end
def clear_frame
removeText(@anchor, getLength-@anchor)
end
def delete_from_start_of_line
str = get_to_end_of_line
clear_frame
appendText(str)
setCursorPos(@anchor)
end
def delete_to_end_of_line
str = get_from_start_of_line
clear_frame
appendText(str)
setCursorPos(getLength)
end
def empty_frame?
get_frame == ""
end
def indented?
extractText(@anchor-2, 2) == " "
end
def new_line_entered
process_commandline(get_frame)
end
def process_commandline(cmd)
@irb.process_commandline(cmd)
end
public
def send_command(cmd)
setCursorPos(getLength)
makePositionVisible(getLength) unless isPosVisible(getLength)
cmd += "\n"
appendText(cmd)
process_commandline(cmd)
end
def write(obj)
str = obj.to_s
appendText(str)
setCursorPos(getLength)
makePositionVisible(getLength) unless isPosVisible(getLength)
return str.length
end
def print(obj)
write(obj)
@anchor = getCursorPos
end
end
# Stand alone run
if __FILE__ == $0
application = FXApp.new("FXIrb", "ruby")
application.threadsEnabled = true
Thread.abort_on_exception = true
window = FXMainWindow.new(application, "FXIrb",
nil, nil, DECOR_ALL, 0, 0, 580, 500)
editor = FXIrb.new(window, nil, 0,
LAYOUT_FILL_X|LAYOUT_FILL_Y|TEXT_WORDWRAP|TEXT_SHOWACTIVE)
application.create
window.show(PLACEMENT_SCREEN)
editor.on_irb_exit {exit}
irb = IrbRunner.new(editor)
editor.irb = irb
application.run
end