-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeditor.py
executable file
·595 lines (500 loc) · 19.6 KB
/
peditor.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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
import wx
from pbuffer import Buffer
from wx.py import crust
from wx.py import dispatcher
import keyword
import pnotebook
import pstc
import pframe
from wx.py.shell import Shell
from wx.py import version
import pdb
class PEditorFrame(pframe.PFrame):
def __init__(self, parent=None, id=-1, title='PEditor',
pos=wx.DefaultPosition, size=(800, 600),
style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE,
filename=None):
pframe.PFrame.__init__(self, parent, id, title, pos, size, style)
self.buffers = {}
self.buffer = None # Current buffer.
self.editor = None
self._defaultText = title + ' - my Python editor.'
#self.psb.SetFieldsCount(2)
self._statusText = self._defaultText
self.SetStatusText(self._statusText)
self.Bind(wx.EVT_IDLE, self.OnIdle)
self.notebook = None
if self.notebook:
dispatcher.connect(receiver=self._editorChange,
signal='EditorChange', sender=self.notebook)
self._setup()
if filename:
self.bufferCreate(filename)
def _setup(self):
"""Setup prior to first buffer creation.
Called automatically by base class during init."""
self.notebook = pnotebook.PNotebook(parent=self)
intro = 'Py %s' % version.VERSION
import imp
module = imp.new_module('__main__')
import __builtin__
module.__dict__['__builtins__'] = __builtin__
namespace = module.__dict__.copy()
self.crust = crust.Crust(parent=self.notebook, intro=intro, locals=namespace)
self.shell = self.crust.shell
# Override the filling so that status messages go to the status bar.
self.crust.filling.tree.setStatusText = self.SetStatusText
# Override the shell so that status messages go to the status bar.
self.shell.setStatusText = self.SetStatusText
# Fix a problem with the sash shrinking to nothing.
self.crust.filling.SetSashPosition(200)
self.notebook.AddPage(page=self.crust, text='*Shell*', select=True)
self.setEditor(self.crust.editor)
self.crust.editor.SetFocus()
# buffer = Buffer()
# panel = wx.Panel(parent=self.notebook, id=-1)
# panel.Bind(wx.EVT_ERASE_BACKGROUND, lambda x: x)
# editor = Editor(parent=panel)
# panel.editor = editor
# sizer = wx.BoxSizer(wx.VERTICAL)
# sizer.Add(editor.window, 1, wx.EXPAND)
# panel.SetSizer(sizer)
# panel.SetAutoLayout(True)
# sizer.Layout()
# buffer.addEditor(editor)
# buffer.open(None)
# self.setEditor(editor)
# self.notebook.AddPage(page=panel, text=self.buffer.name, select=True)
# self.editor.setFocus()
self.bufferCreate()
def _editorChange(self, editor):
self.setEditor(editor)
def _updateStatus(self):
"""Show current status information."""
if self.editor and hasattr(self.editor, 'getStatus'):
status = self.editor.getStatus()
text = 'File: %s | Line: %d | Column: %d' % status
else:
text = self._defaultText
if text != self._statusText:
self.psb.SetStatusText(text,1)
self._statusText = text
def _updateTabText(self):
"""Show current buffer information on notebook tab."""
## suffix = ' **'
## notebook = self.notebook
## selection = notebook.GetSelection()
## if selection == -1:
## return
## text = notebook.GetPageText(selection)
## window = notebook.GetPage(selection)
## if window.editor and window.editor.buffer.hasChanged():
## if text.endswith(suffix):
## pass
## else:
## notebook.SetPageText(selection, text + suffix)
## else:
## if text.endswith(suffix):
## notebook.SetPageText(selection, text[:len(suffix)])
def _updateTitle(self):
"""Show current title information."""
title = self.GetTitle()
if self.bufferHasChanged():
if title.startswith('* '):
pass
else:
self.SetTitle('* ' + title)
else:
if title.startswith('* '):
self.SetTitle(title[2:])
def bufferCreate(self, filename=None):
buffer = Buffer()
panel = wx.Panel(parent=self.notebook, id=-1)
panel.Bind(wx.EVT_ERASE_BACKGROUND, lambda x: x)
editor = Editor(parent=panel)
panel.editor = editor
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(editor.window, 1, wx.EXPAND)
panel.SetSizer(sizer)
panel.SetAutoLayout(True)
sizer.Layout()
buffer.addEditor(editor)
buffer.open(filename)
self.setEditor(editor)
self.notebook.AddPage(page=panel, text=self.buffer.name, select=True)
self.editor.setFocus()
def hasBuffer(self):
if self.buffers != {}:
return True
else:
return False
def bufferClose(self):
if self.bufferHasChanged():
cancel = self.bufferSuggestSave()
if cancel:
return cancel
self.bufferDestroy()
cancel = False
return cancel
def bufferHasChanged(self):
if self.buffer:
return self.buffer.hasChanged()
else:
return False
def bufferSave(self):
if self.buffer.doc.filepath:
self.buffer.save()
cancel = False
else:
cancel = self.bufferSaveAs()
return cancel
def bufferSaveAs(self):
if self.bufferHasChanged() and self.buffer.doc.filepath:
cancel = self.bufferSuggestSave()
if cancel:
return cancel
filedir = ''
if self.buffer and self.buffer.doc.filedir:
filedir = self.buffer.doc.filedir
result = saveSingle(directory=filedir)
if result.path:
self.buffer.saveAs(result.path)
cancel = False
else:
cancel = True
return cancel
def bufferSuggestSave(self):
"""Suggest saving changes. Return True if user selected Cancel."""
result = messageDialog(parent=None,
message='%s has changed.\n'
'Would you like to save it first'
'?' % self.buffer.name,
title='Save current file?')
if result.positive:
cancel = self.bufferSave()
else:
cancel = result.text == 'Cancel'
return cancel
# def updateNamespace(self):
# """Update the buffer namespace for autocompletion and calltips."""
# if self.buffer.updateNamespace():
# self.SetStatusText('Namespace updated')
# else:
# self.SetStatusText('Error executing, unable to update namespace')
def bufferDestroy(self):
selection = self.notebook.GetSelection()
## print "Destroy Selection:", selection
#if selection > 0: # Don't destroy the PyCrust tab.
if self.buffer:
del self.buffers[self.buffer.id]
self.buffer = None # Do this before DeletePage().
self.notebook.DeletePage(selection)
def bufferNew(self):
self.bufferCreate()
cancel = False
return cancel
def bufferOpen(self):
filedir = ''
if self.buffer and self.buffer.doc.filedir:
filedir = self.buffer.doc.filedir
result = openMultiple(directory=filedir)
for path in result.paths:
if self.bufferHasChanged():
self.bufferCreate(path)
elif "NewFile" in self.buffer.name:
self.buffer.open(path)
else :
self.bufferCreate(path)
cancel = False
return cancel
def shellOpen(self):
"""Open a python interpretor"""
# intro = 'Py %s' % version.VERSION
# import imp
# module = imp.new_module('__main__')
# import __builtin__
# module.__dict__['__builtins__'] = __builtin__
# namespace = module.__dict__.copy()
self.crust = crust.Crust(parent=self.notebook)#, intro=intro, locals=namespace)
self.shell = self.crust.shell
# Override the filling so that status messages go to the status bar.
self.crust.filling.tree.setStatusText = self.SetStatusText
# Override the shell so that status messages go to the status bar.
self.shell.setStatusText = self.SetStatusText
# Fix a problem with the sash shrinking to nothing.
self.crust.filling.SetSashPosition(200)
self.notebook.AddPage(page=self.crust, text='*Shell*', select=True)
self.setEditor(self.crust.editor)
self.crust.editor.SetFocus()
def setEditor(self, editor):
self.editor = editor
self.buffer = self.editor.buffer
self.buffers[self.buffer.id] = self.buffer
def OnAbout(self, event):
title = 'About PEditor'
text = 'Simple Python Editor.'
dialog = wx.MessageDialog(self, text, title,
wx.OK | wx.ICON_INFORMATION)
dialog.ShowModal()
dialog.Destroy()
def OnClose(self, event):
for buffer in self.buffers.values():
self.buffer = buffer
if buffer.hasChanged():
cancel = self.bufferSuggestSave()
if cancel and event.CanVeto():
event.Veto()
return
self.Destroy()
def OnIdle(self, event):
self._updateStatus()
if hasattr(self, 'notebook'):
self._updateTabText()
self._updateTitle()
event.Skip()
class Editor:
"""Editor having an PSTC."""
def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=wx.CLIP_CHILDREN | wx.SUNKEN_BORDER |wx.TE_MULTILINE):
self.window = PSTC(self, parent, id, pos, size, style)
self.id = self.window.GetId()
self.buffer = None
# Assign handlers for keyboard events.
self.window.Bind(wx.EVT_CHAR, self.OnChar)
self.window.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
def _setBuffer(self, buffer, text):
self.buffer = buffer
self.autoCompleteKeys = buffer.interp.getAutoCompleteKeys()
self.clearAll()
self.setText(text)
self.emptyUndoBuffer()
self.setSavePoint()
def destroy(self):
self.window.Destroy()
def clearAll(self):
self.window.ClearAll()
def emptyUndoBuffer(self):
self.window.EmptyUndoBuffer()
def getStatus(self):
"""Return (filepath, line, column) status tuple."""
if self.window:
pos = self.window.GetCurrentPos()
line = self.window.LineFromPosition(pos) + 1
col = self.window.GetColumn(pos)
if self.buffer:
name = self.buffer.doc.filepath or self.buffer.name
else:
name = ''
status = (name, line, col)
return status
else:
return ('', 0, 0)
def getText(self):
return self.window.GetText()
def hasChanged(self):
"""Return True if contents have changed."""
return self.window.GetModify()
def setFocus(self):
self.window.SetFocus()
def setSavePoint(self):
self.window.SetSavePoint()
def setText(self, text):
self.window.SetText(text)
def OnChar(self, event):
"""Keypress event handler."""
key = event.GetKeyCode()
self.promptPosEnd = 0
if key in self.autoCompleteKeys:
# Usually the dot (period) key activates auto completion.
if self.window.AutoCompActive():
self.window.AutoCompCancel()
self.window.ReplaceSelection('')
self.window.AddText(chr(key))
#text, pos = self.window.GetCurLine()
#text = text[:pos]
currpos = self.window.GetCurrentPos()
stoppos = self.promptPosEnd
command = self.window.GetTextRange(stoppos,currpos) + chr(key)
if self.window.autoComplete:
self.autoCompleteShow(command)
elif key == ord('('):
# The left paren activates a call tip and cancels an
# active auto completion.
if self.window.AutoCompActive():
self.window.AutoCompCancel()
self.window.ReplaceSelection('')
self.window.AddText('(')
text, pos = self.window.GetCurLine()
text = text[:pos]
self.autoCallTipShow(text)
else:
# Allow the normal event handling to take place.
event.Skip()
def OnKeyDown(self, event):
"""Key down event handler."""
key = event.GetKeyCode()
# If the auto-complete window is up let it do its thing.
if self.window.AutoCompActive():
event.Skip()
return
controlDown = event.ControlDown()
altDown = event.AltDown()
shiftDown = event.ShiftDown()
# Let Ctrl-Alt-* get handled normally.
if controlDown and altDown:
event.Skip()
# Increase font size.
elif controlDown and key in (ord(']'),):
dispatcher.send(signal='FontIncrease')
# Decrease font size.
elif controlDown and key in (ord('['),):
dispatcher.send(signal='FontDecrease')
# Default font size.
elif controlDown and key in (ord('='),):
dispatcher.send(signal='FontDefault')
elif shiftDown and key in [wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER]:
self.window.OnShowCompHistory()
else:
event.Skip()
def autoCompleteShow(self, command):
"""Display auto-completion popup list."""
#list = self.buffer.interp.getAutoCompleteList(command,
# includeMagic=self.window.autoCompleteIncludeMagic,
# includeSingle=self.window.autoCompleteIncludeSingle,
# includeDouble=self.window.autoCompleteIncludeDouble)
list = keyword.kwlist
if list:
options = ' '.join(list)
offset = 0
self.window.AutoCompShow(offset, options)
def autoCallTipShow(self, command):
"""Display argument spec and docstring in a popup window."""
if self.window.CallTipActive():
self.window.CallTipCancel()
(name, argspec, tip) = self.buffer.interp.getCallTip(command)
if tip:
dispatcher.send(signal='Shell.calltip', sender=self, calltip=tip)
if not self.window.autoCallTip:
return
startpos = self.window.GetCurrentPos()
if argspec:
self.window.AddText(argspec + ')')
endpos = self.window.GetCurrentPos()
self.window.SetSelection(startpos, endpos)
if tip:
tippos = startpos - (len(name) + 1)
fallback = startpos - self.GetColumn(startpos)
# In case there isn't enough room, only go back to the
# fallback.
tippos = max(tippos, fallback)
self.CallTipShow(tippos, tip)
class PSTC(pstc.PSTC):
"""PSTC based on StyledTextCtrl."""
def __init__(self, editor, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=wx.CLIP_CHILDREN | wx.SUNKEN_BORDER):
pstc.PSTC.__init__(self, parent, id, pos, size, style)
self.editor = editor
class DialogResults:
def __init__(self, returned):
"""Create wrapper for results returned by dialog."""
self.returned = returned
self.positive = returned in (wx.ID_OK, wx.ID_YES)
self.text = self._asString()
def __repr__(self):
return str(self.__dict__)
def _asString(self):
returned = self.returned
if returned == wx.ID_OK:
return "Ok"
elif returned == wx.ID_CANCEL:
return "Cancel"
elif returned == wx.ID_YES:
return "Yes"
elif returned == wx.ID_NO:
return "No"
def fileDialog(parent=None, title='Open', directory='', filename='',
wildcard='All Files (*.*)|*.*',
style=wx.OPEN | wx.MULTIPLE):
dialog = wx.FileDialog(parent, title, directory, filename,
wildcard, style)
result = DialogResults(dialog.ShowModal())
if result.positive:
result.paths = dialog.GetPaths()
else:
result.paths = []
dialog.Destroy()
return result
def openSingle(parent=None, title='Open', directory='', filename='',
wildcard='All Files (*.*)|*.*', style=wx.OPEN):
dialog = wx.FileDialog(parent, title, directory, filename,
wildcard, style)
result = DialogResults(dialog.ShowModal())
if result.positive:
result.path = dialog.GetPath()
else:
result.path = None
dialog.Destroy()
return result
def openMultiple(parent=None, title='Open', directory='', filename='',
wildcard='All Files (*.*)|*.*',
style=wx.OPEN | wx.MULTIPLE):
return fileDialog(parent, title, directory, filename, wildcard, style)
def saveSingle(parent=None, title='Save', directory='', filename='',
wildcard='All Files (*.*)|*.*',
style=wx.SAVE | wx.OVERWRITE_PROMPT):
dialog = wx.FileDialog(parent, title, directory, filename,
wildcard, style)
result = DialogResults(dialog.ShowModal())
if result.positive:
result.path = dialog.GetPath()
else:
result.path = None
dialog.Destroy()
return result
#def directory(parent=None, message='Choose a directory', path='', style=0,
# pos=wx.DefaultPosition, size=wx.DefaultSize):
# """Dir dialog wrapper function."""
# dialog = wx.DirDialog(parent, message, path, style, pos, size)
# result = DialogResults(dialog.ShowModal())
# if result.positive:
# result.path = dialog.GetPath()
# else:
# result.path = None
# dialog.Destroy()
# return result
def messageDialog(parent=None, message='', title='Message box',
style=wx.YES_NO | wx.CANCEL | wx.CENTRE | wx.ICON_QUESTION,
pos=wx.DefaultPosition):
dialog = wx.MessageDialog(parent, message, title, style, pos)
result = DialogResults(dialog.ShowModal())
dialog.Destroy()
return result
class CommandHis(wx.TextCtrl):
"""Text control containing all commands for session."""
def __init__(self, parent=None, id=-1,ShellClassName='Shell'):
style = (wx.TE_MULTILINE | wx.TE_READONLY |
wx.TE_RICH2 | wx.TE_DONTWRAP)
wx.TextCtrl.__init__(self, parent, id, style=style)
dispatcher.connect(receiver=self.addHistory,
signal="PSTC"+".addHistory")
dispatcher.connect(receiver=self.clearHistory,
signal="PSTC"+".clearHistory")
dispatcher.connect(receiver=self.loadHistory,
signal="PSTC"+".loadHistory")
df = self.GetFont()
font = wx.Font(df.GetPointSize(), wx.TELETYPE, wx.NORMAL, wx.NORMAL)
self.SetFont(font)
def loadHistory(self, history):
# preload the existing history, if any
hist = history[:]
hist.reverse()
self.SetValue('\n'.join(hist) + '\n')
self.SetInsertionPointEnd()
def addHistory(self, command):
if command:
self.SetInsertionPointEnd()
self.AppendText(command + '\n')
def clearHistory(self):
self.SetValue("")