-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebut.py
340 lines (291 loc) · 10 KB
/
debut.py
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
#! python3
"""deb utilus ver 1.0rc
"""
import getopt
import sys
import os
import wx
import mwx
from mwx.utilus import ignore
from mwx.controls import Clipboard
from mwx.py.filling import FillingTree
## Monkey patch for filling-tree to display only atoms.
try:
_FillingTree_filter_org = FillingTree.filter
def atomic(self, obj, key):
if not _FillingTree_filter_org(self, obj, key):
return False
try:
v = getattr(obj, key)
return not hasattr(v, '__name__')
except Exception:
pass
FillingTree.filter = atomic
except AttributeError:
pass
## --------------------------------
## Configuration of Shell / Editor
## --------------------------------
def init_stc_interface(self):
"""Customize the common keymaps.
"""
@self.define_key('f9')
def toggle_wrap_mode():
mode = ['no-wrap',
'word-wrap',
'char-wrap',
'whitespace-wrap'
]
self.WrapMode = (self.WrapMode + 1) % 4
self.post_message("\b {!r}".format(mode[self.WrapMode]))
@self.define_key('S-f9')
def toggle_eol_view():
self.ViewEOL = not self.ViewEOL
self.ViewWhiteSpace = not self.ViewWhiteSpace
self.define_key('C-x [', self.beginning_of_buffer)
self.define_key('C-x ]', self.end_of_buffer)
self.define_key('C-c C-c', self.goto_matched_paren)
self.define_key('C-x C-x', self.exchange_point_and_mark)
def init_buffer(self):
"""Customize the keymaps of the Buffer.
"""
## Buffer text control
init_stc_interface(self)
@self.define_key('enter')
def newline_and_indent():
n = self.py_electric_indent()
self.AddText(os.linesep + ' ' * n)
@self.define_key('C-enter')
def newline_and_indent_eol():
n = self.py_electric_indent()
self.goto_char(self.eol)
self.AddText(os.linesep + ' ' * n)
@self.define_key('C-S-enter')
@self.define_key('S-enter')
def open_line_and_indent():
n = self.py_current_indent()
self.goto_char(self.bol)
self.InsertText(self.bol, ' ' * n + os.linesep)
self.goto_char(self.cpos + n) # relative indentation position
@self.define_key('M-w')
def copy_region():
self.anchor = self.mark
self.Copy()
@self.define_key('C-w')
def kill_region():
self.anchor = self.mark
self.Cut()
@self.define_key('C-x C-insert')
def copy_line():
text, lp = self.CurLine
Clipboard.write("{}:{}:\n{}".format(self.filename, self.cline+1, text))
def init_editor(self):
"""Customize the keymaps of the Editor.
"""
self.define_key('C-x k', self.kill_all_buffers)
self.define_key('C-x C-k', self.kill_buffer)
self.define_key('C-x C-n', self.new_buffer)
self.define_key('C-x C-l', self.load_buffer)
self.define_key('C-x s', self.save_all_buffers)
self.define_key('C-x C-s', self.save_buffer)
self.define_key('C-x S-s', self.save_buffer_as)
self.define_key('C-x C-f', self.find_file)
@self.define_key('S-f5', load=True)
@self.define_key('f5')
def eval_buffer(evt, load=False):
if load:
self.load_buffer()
self.buffer.exec_region()
@self.define_key('C-S-f9')
def load_file():
text = self.buffer.SelectedText or self.buffer.expr_at_caret
filename = os.path.join(os.path.dirname(self.buffer.filename), text)
if self.load_file(filename):
self.post_message(f"\b {text!r}")
def init_shell(self):
"""Customize the keymaps of the Shell.
"""
init_stc_interface(self)
@self.define_key('S-enter') # cf. [C-RET] Shell.insertLineBreak
def open_line():
self.back_to_indentation()
p = self.cpos
self.insertLineBreak()
self.cpos = self.anchor = p
@self.define_key('M-enter')
@self.define_key('M-S-enter', clear=0) # insert command
def duplicate_command(clear=True):
cmd = self.getMultilineCommand()
if cmd:
self.mark = self.cpos
if clear:
self.clearCommand() # => move to the prompt end
self.write(cmd, -1)
@self.define_key('f1')
def help(evt):
text = self.SelectedText or self.expr_at_caret
try:
obj = self.eval(text)
self.help(obj)
except Exception:
evt.Skip()
@self.define_key('f2')
def load_target():
text = self.SelectedText or self.expr_at_caret
if not text:
## self.post_message("No target")
obj = self.target
else:
try:
obj = self.eval(text)
except Exception as e:
self.post_message(f"\b failed: {e!r}")
return
if self.parent.load(obj):
self.post_message(f"\b {text!r}")
else:
self.post_message(f"\b {text!r} was nowhere to be found.")
py_error_re = r' +File "(.*?)", line ([0-9]+)'
py_frame_re = r" +file '(.*?)', line ([0-9]+)"
py_where_re = r'> +([^*?"<>|\r\n]+?):([0-9]+)'
py_break_re = r'at ([^*?"<>|\r\n]+?):([0-9]+)'
py_grep_re = '|'.join((py_frame_re, py_where_re, py_break_re))
@self.define_key('f4', pattern=py_error_re)
@self.define_key('f10', pattern=py_grep_re)
def grep_forward(pattern):
for err in self.grep_forward(pattern):
target = ':'.join(filter(None, err.groups()))
if self.parent.load(target):
self.post_message(f"\b {target}")
break
@self.define_key('S-f4', pattern=py_error_re)
@self.define_key('S-f10', pattern=py_grep_re)
def grep_backward(pattern):
for err in self.grep_backward(pattern):
target = ':'.join(filter(None, err.groups()))
if self.parent.load(target):
self.post_message(f"\b {target}")
break
@self.define_key('S-f12')
def clear_shell():
self.clear()
@self.define_key('C-f12')
def clone_shell():
self.parent.clone_shell(self.target)
@self.define_key('M-f12')
def close_shell():
self.parent.delete_shell(self)
@self.define_key('C-f2')
def HL():
try:
highlight(self.eval(self.expr_at_caret))
except Exception:
pass
@ignore(UserWarning)
def stylus(self):
"""Stylize Nautilus window.
Note:
This function is executed once at startup.
"""
## Customize the keymaps of the ShellFrame.
self.define_key('C-x C-S-o', self.load_session)
self.define_key('C-x C-S-s', self.save_session)
@self.define_key('Xbutton1', p=-1)
@self.define_key('Xbutton2', p=+1)
@self.define_key('C-x p', p=-1)
@self.define_key('C-x n', p=+1)
def other_editor(p=1):
"""Move focus to other topmost notebook page."""
nb = self.FindFocus()
while isinstance(nb.Parent, wx.aui.AuiNotebook):
nb = nb.Parent
try:
if nb.PageCount > 1:
nb.Selection = (nb.Selection + p) % nb.PageCount
except AttributeError:
pass
@self.define_key('M-left', p=-1)
@self.define_key('M-right', p=+1)
def other_window(p=1):
"Move focus to other window"
pages = [x for x in self.get_all_pages() if x.IsShownOnScreen()]
wnd = self.FindFocus()
while wnd:
if wnd in pages:
j = (pages.index(wnd) + p) % len(pages)
obj = pages[j]
if isinstance(obj, wx.aui.AuiNotebook):
obj = obj.CurrentPage
obj.SetFocus()
break
wnd = wnd.Parent
@self.define_key('C-d')
@self.define_key('C-S-d', clear=0) # insert line
def duplicate_line(clear=True):
"""Duplicate an expression at the caret-line."""
buf = self.FindFocus()
if not isinstance(buf, wx.stc.StyledTextCtrl):
return
text = buf.SelectedText or buf.expr_at_caret or buf.topic_at_caret
if text:
shell = self.current_shell
buf.mark = buf.cpos
if clear:
shell.clearCommand() # move to the prompt end
shell.write(text, -1) # write at the end of command-line
shell.SetFocus()
## Customize keymaps.
for page in self.all_editors:
init_editor(page)
for buf in page.all_buffers:
init_buffer(buf)
self.handler.unbind('buffer_new')
self.handler.bind('buffer_new', init_buffer)
for page in self.all_shells:
init_shell(page)
self.handler.unbind('shell_new')
self.handler.bind('shell_new', init_shell)
@self.define_key('C-x f11', win=self.ghost)
@self.define_key('C-x S-f11', win=self.watcher)
def toggle_pane(win):
pane = self._mgr.GetPane(win)
if pane.IsDocked():
## toggle the pnae state to maximumized or not.
if self.console.IsShown():
self._mgr.MaximizePane(pane)
else:
self._mgr.RestoreMaximizedPane()
self._mgr.Update()
## --------------------------------
## Main program
## --------------------------------
quote_unqoute = """
Anything one man can imagine, other man can make real.
--- Jules Verne (1828--1905)
"""
def main(target=None, **kwargs):
app = wx.GetApp() or wx.App()
frame = mwx.deb(target, loop=0, # Don't enter loop.
introText=(__doc__ or '') + quote_unqoute,
**kwargs)
stylus(frame)
if 1:
## To skip debugging a specific module, add module:str to debugger.skip:set.
frame.debugger.skip -= {
mwx.FSM.__module__, # for debugging FSM
}
shell = dive(frame)
wx.CallAfter(shell.SetFocus)
if not app.GetMainLoop():
app.MainLoop()
if __name__ == "__main__":
session = ''
opts, args = getopt.gnu_getopt(sys.argv[1:], "s:")
for k, v in opts:
if k == "-s":
if not v.endswith(".debrc"):
v += ".debrc"
session = v.strip()
if session:
print(f"Starting session {session!r}")
main(debrc=session)