1 """The PyCrust Shell is an interactive text control in which a user types in
2 commands to be sent to the interpreter. This particular shell is based on
3 wxPython's wxStyledTextCtrl. The latest files are always available at the
4 SourceForge project page at http://sourceforge.net/projects/pycrust/."""
6 __author__
= "Patrick K. O'Brien <pobrien@orbtech.com>"
8 __date__
= "July 1, 2001"
9 __version__
= "$Revision$"[11:-2]
11 from wxPython
.wx
import *
12 from wxPython
.stc
import *
16 from pseudo
import PseudoFileIn
17 from pseudo
import PseudoFileOut
18 from pseudo
import PseudoFileErr
19 from version
import VERSION
22 if wxPlatform
== '__WXMSW__':
23 faces
= { 'times' : 'Times New Roman',
24 'mono' : 'Courier New',
25 'helv' : 'Lucida Console',
26 'lucida' : 'Lucida Console',
27 'other' : 'Comic Sans MS',
32 # Versions of wxPython prior to 2.3.2 had a sizing bug on Win platform.
33 # The font was 2 points too large. So we need to reduce the font size.
34 if ((wxMAJOR_VERSION
, wxMINOR_VERSION
) == (2, 3) and wxRELEASE_NUMBER
< 2) \
35 or (wxMAJOR_VERSION
<= 2 and wxMINOR_VERSION
<= 2):
39 faces
= { 'times' : 'Times',
42 'other' : 'new century schoolbook',
50 """Simplified interface to all shell-related functionality.
52 This is a semi-transparent facade, in that all attributes of other are
53 still accessible, even though only some are visible to the user."""
55 name
= 'PyCrust Shell Interface'
56 revision
= __version__
58 def __init__(self
, other
):
59 """Create a ShellFacade instance."""
71 for method
in methods
:
72 self
.__dict
__[method
] = getattr(other
, method
)
75 d
['help'] = 'There is no help available, yet.'
78 def __getattr__(self
, name
):
79 if hasattr(self
.other
, name
):
80 return getattr(self
.other
, name
)
82 raise AttributeError, name
84 def __setattr__(self
, name
, value
):
85 if self
.__dict
__.has_key(name
):
86 self
.__dict
__[name
] = value
87 elif hasattr(self
.other
, name
):
88 return setattr(self
.other
, name
, value
)
90 raise AttributeError, name
92 def _getAttributeNames(self
):
93 """Return list of magic attributes to extend introspection."""
94 list = ['autoCallTip',
96 'autoCompleteCaseInsensitive',
97 'autoCompleteIncludeDouble',
98 'autoCompleteIncludeMagic',
99 'autoCompleteIncludeSingle',
105 class Shell(wxStyledTextCtrl
):
106 """PyCrust Shell based on wxStyledTextCtrl."""
108 name
= 'PyCrust Shell'
109 revision
= __version__
111 def __init__(self
, parent
, id=-1, pos
=wxDefaultPosition
, \
112 size
=wxDefaultSize
, style
=wxCLIP_CHILDREN
, introText
='', \
113 locals=None, InterpClass
=None, *args
, **kwds
):
114 """Create a PyCrust Shell instance."""
115 wxStyledTextCtrl
.__init
__(self
, parent
, id, pos
, size
, style
)
116 # Grab these so they can be restored by self.redirect* methods.
117 self
.stdin
= sys
.stdin
118 self
.stdout
= sys
.stdout
119 self
.stderr
= sys
.stderr
120 # Add the current working directory "." to the search path.
121 sys
.path
.insert(0, os
.curdir
)
122 # Import a default interpreter class if one isn't provided.
123 if InterpClass
== None:
124 from interpreter
import Interpreter
126 Interpreter
= InterpClass
127 # Create default locals so we have something interesting.
128 shellLocals
= {'__name__': 'PyCrust-Shell',
129 '__doc__': 'PyCrust-Shell, The PyCrust Python Shell.',
130 '__version__': VERSION
,
132 # Add the dictionary that was passed in.
134 shellLocals
.update(locals)
135 self
.interp
= Interpreter(locals=shellLocals
, \
136 rawin
=self
.readRaw
, \
137 stdin
=PseudoFileIn(self
.readIn
), \
138 stdout
=PseudoFileOut(self
.writeOut
), \
139 stderr
=PseudoFileErr(self
.writeErr
), \
141 # Keep track of the most recent prompt starting and ending positions.
142 self
.promptPos
= [0, 0]
143 # Keep track of the most recent non-continuation prompt.
144 self
.prompt1Pos
= [0, 0]
145 # Keep track of multi-line commands.
147 # Create the command history. Commands are added into the front of
148 # the list (ie. at index 0) as they are entered. self.historyIndex
149 # is the current position in the history; it gets incremented as you
150 # retrieve the previous command, decremented as you retrieve the
151 # next, and reset when you hit Enter. self.historyIndex == -1 means
152 # you're on the current command, not in the history.
154 self
.historyIndex
= -1
155 # Assign handlers for keyboard events.
156 EVT_KEY_DOWN(self
, self
.OnKeyDown
)
157 EVT_CHAR(self
, self
.OnChar
)
158 # Configure various defaults and user preferences.
160 # Display the introductory banner information.
161 try: self
.showIntro(introText
)
163 # Assign some pseudo keywords to the interpreter's namespace.
164 try: self
.setBuiltinKeywords()
166 # Add 'shell' to the interpreter's local namespace.
167 try: self
.setLocalShell()
169 # Do this last so the user has complete control over their
170 # environment. They can override anything they want.
171 try: self
.execStartupScript(self
.interp
.startupScript
)
178 """Configure shell based on user preferences."""
179 self
.SetMarginType(1, wxSTC_MARGIN_NUMBER
)
180 self
.SetMarginWidth(1, 40)
182 self
.SetLexer(wxSTC_LEX_PYTHON
)
183 self
.SetKeyWords(0, ' '.join(keyword
.kwlist
))
185 self
.setStyles(faces
)
186 self
.SetViewWhiteSpace(0)
189 # Do we want to automatically pop up command completion options?
190 self
.autoComplete
= 1
191 self
.autoCompleteIncludeMagic
= 1
192 self
.autoCompleteIncludeSingle
= 1
193 self
.autoCompleteIncludeDouble
= 1
194 self
.autoCompleteCaseInsensitive
= 1
195 self
.AutoCompSetIgnoreCase(self
.autoCompleteCaseInsensitive
)
196 # De we want to automatically pop up command argument help?
198 self
.CallTipSetBackground(wxColour(255, 255, 232))
200 def showIntro(self
, text
=''):
201 """Display introductory text in the shell."""
203 if not text
.endswith(os
.linesep
): text
+= os
.linesep
206 self
.write(self
.interp
.introText
)
207 except AttributeError:
210 def setBuiltinKeywords(self
):
211 """Create pseudo keywords as part of builtins.
213 This is a rather clever hack that sets "close", "exit" and "quit"
214 to a PseudoKeyword object so that we can make them do what we want.
215 In this case what we want is to call our self.quit() method.
216 The user can type "close", "exit" or "quit" without the final parens.
218 ## POB: This is having some weird side-effects so I'm taking it out.
219 ## import __builtin__
220 ## from pseudo import PseudoKeyword
221 ## __builtin__.close = __builtin__.exit = __builtin__.quit = \
222 ## PseudoKeyword(self.quit)
224 from pseudo
import PseudoKeyword
225 __builtin__
.close
= __builtin__
.exit
= __builtin__
.quit
= \
226 'Click on the close button to leave the application.'
229 """Quit the application."""
231 # XXX Good enough for now but later we want to send a close event.
233 # In the close event handler we can make sure they want to quit.
234 # Other applications, like PythonCard, may choose to hide rather than
235 # quit so we should just post the event and let the surrounding app
236 # decide what it wants to do.
237 self
.write('Click on the close button to leave the application.')
239 def setLocalShell(self
):
240 """Add 'shell' to locals as reference to ShellFacade instance."""
241 self
.interp
.locals['shell'] = ShellFacade(other
=self
)
243 def execStartupScript(self
, startupScript
):
244 """Execute the user's PYTHONSTARTUP script if they have one."""
245 if startupScript
and os
.path
.isfile(startupScript
):
246 startupText
= 'Startup script executed: ' + startupScript
247 self
.push('print %s;execfile(%s)' % \
248 (`startupText`
, `startupScript`
))
252 def setStyles(self
, faces
):
253 """Configure font size, typeface and color for lexer."""
256 self
.StyleSetSpec(wxSTC_STYLE_DEFAULT
, "face:%(mono)s,size:%(size)d" % faces
)
261 self
.StyleSetSpec(wxSTC_STYLE_LINENUMBER
, "back:#C0C0C0,face:%(mono)s,size:%(lnsize)d" % faces
)
262 self
.StyleSetSpec(wxSTC_STYLE_CONTROLCHAR
, "face:%(mono)s" % faces
)
263 self
.StyleSetSpec(wxSTC_STYLE_BRACELIGHT
, "fore:#0000FF,back:#FFFF88")
264 self
.StyleSetSpec(wxSTC_STYLE_BRACEBAD
, "fore:#FF0000,back:#FFFF88")
267 self
.StyleSetSpec(wxSTC_P_DEFAULT
, "face:%(mono)s" % faces
)
268 self
.StyleSetSpec(wxSTC_P_COMMENTLINE
, "fore:#007F00,face:%(mono)s" % faces
)
269 self
.StyleSetSpec(wxSTC_P_NUMBER
, "")
270 self
.StyleSetSpec(wxSTC_P_STRING
, "fore:#7F007F,face:%(mono)s" % faces
)
271 self
.StyleSetSpec(wxSTC_P_CHARACTER
, "fore:#7F007F,face:%(mono)s" % faces
)
272 self
.StyleSetSpec(wxSTC_P_WORD
, "fore:#00007F,bold")
273 self
.StyleSetSpec(wxSTC_P_TRIPLE
, "fore:#7F0000")
274 self
.StyleSetSpec(wxSTC_P_TRIPLEDOUBLE
, "fore:#000033,back:#FFFFE8")
275 self
.StyleSetSpec(wxSTC_P_CLASSNAME
, "fore:#0000FF,bold")
276 self
.StyleSetSpec(wxSTC_P_DEFNAME
, "fore:#007F7F,bold")
277 self
.StyleSetSpec(wxSTC_P_OPERATOR
, "")
278 self
.StyleSetSpec(wxSTC_P_IDENTIFIER
, "")
279 self
.StyleSetSpec(wxSTC_P_COMMENTBLOCK
, "fore:#7F7F7F")
280 self
.StyleSetSpec(wxSTC_P_STRINGEOL
, "fore:#000000,face:%(mono)s,back:#E0C0E0,eolfilled" % faces
)
282 def OnChar(self
, event
):
283 """Keypress event handler.
285 Prevents modification of previously submitted commands/responses."""
286 if not self
.CanEdit():
288 key
= event
.KeyCode()
289 currpos
= self
.GetCurrentPos()
290 stoppos
= self
.promptPos
[1]
292 # The dot or period key activates auto completion.
293 # Get the command between the prompt and the cursor.
294 # Add a dot to the end of the command.
295 command
= self
.GetTextRange(stoppos
, currpos
) + '.'
297 if self
.autoComplete
: self
.autoCompleteShow(command
)
298 elif key
== ord('('):
299 # The left paren activates a call tip and cancels
300 # an active auto completion.
301 if self
.AutoCompActive(): self
.AutoCompCancel()
302 # Get the command between the prompt and the cursor.
303 # Add the '(' to the end of the command.
304 command
= self
.GetTextRange(stoppos
, currpos
) + '('
306 if self
.autoCallTip
: self
.autoCallTipShow(command
)
307 # Hack to keep characters from entering when Alt or Control are down.
308 elif event
.ControlDown() or event
.AltDown():
311 # Allow the normal event handling to take place.
314 def OnKeyDown(self
, event
):
315 """Key down event handler.
317 Prevents modification of previously submitted commands/responses."""
318 key
= event
.KeyCode()
319 currpos
= self
.GetCurrentPos()
320 stoppos
= self
.promptPos
[1]
321 # Return is used to submit a command to the interpreter.
322 if key
== WXK_RETURN
:
323 if self
.AutoCompActive(): self
.AutoCompCancel()
324 if self
.CallTipActive
: self
.CallTipCancel()
326 # If the auto-complete window is up let it do its thing.
327 elif self
.AutoCompActive():
329 # Retrieve the previous command from the history buffer.
330 elif (event
.ControlDown() and key
== WXK_UP
) \
331 or (event
.AltDown() and key
in (ord('P'), ord('p'))):
332 self
.OnHistoryRetrieve(step
=+1)
333 # Retrieve the next command from the history buffer.
334 elif (event
.ControlDown() and key
== WXK_DOWN
) \
335 or (event
.AltDown() and key
in (ord('N'), ord('n'))):
336 self
.OnHistoryRetrieve(step
=-1)
337 # Search up the history for the text in front of the cursor.
339 self
.OnHistorySearch()
340 # Home needs to be aware of the prompt.
341 elif key
== WXK_HOME
:
342 if currpos
>= stoppos
:
343 if event
.ShiftDown():
344 # Select text from current position to end of prompt.
345 self
.SetSelection(self
.GetCurrentPos(), stoppos
)
347 self
.SetCurrentPos(stoppos
)
348 self
.SetAnchor(stoppos
)
351 # Basic navigation keys should work anywhere.
352 elif key
in (WXK_END
, WXK_LEFT
, WXK_RIGHT
, WXK_UP
, WXK_DOWN
, \
353 WXK_PRIOR
, WXK_NEXT
):
355 # Don't backspace over the latest prompt.
356 elif key
== WXK_BACK
:
357 if currpos
> self
.prompt1Pos
[1]:
359 # Only allow these keys after the latest prompt.
360 elif key
in (WXK_TAB
, WXK_DELETE
):
363 # Don't toggle between insert mode and overwrite mode.
364 elif key
== WXK_INSERT
:
369 def OnHistoryRetrieve(self
, step
):
370 """Retrieve the previous/next command from the history buffer."""
371 if not self
.CanEdit():
373 startpos
= self
.GetCurrentPos()
374 newindex
= self
.historyIndex
+ step
375 if not (-1 <= newindex
< len(self
.history
)):
377 self
.historyIndex
= newindex
379 self
.ReplaceSelection('')
381 self
.ReplaceSelection('')
382 command
= self
.history
[self
.historyIndex
]
383 command
= command
.replace('\n', os
.linesep
+ sys
.ps2
)
384 self
.ReplaceSelection(command
)
385 endpos
= self
.GetCurrentPos()
386 self
.SetSelection(endpos
, startpos
)
388 def OnHistorySearch(self
):
389 """Search up the history buffer for the text in front of the cursor."""
390 if not self
.CanEdit():
392 startpos
= self
.GetCurrentPos()
393 # The text up to the cursor is what we search for.
394 numCharsAfterCursor
= self
.GetTextLength() - startpos
395 searchText
= self
.getCommand(rstrip
=0)
396 if numCharsAfterCursor
> 0:
397 searchText
= searchText
[:-numCharsAfterCursor
]
400 # Search upwards from the current history position and loop back
401 # to the beginning if we don't find anything.
402 if (self
.historyIndex
<= -1) \
403 or (self
.historyIndex
>= len(self
.history
)-2):
404 searchOrder
= range(len(self
.history
))
406 searchOrder
= range(self
.historyIndex
+1, len(self
.history
)) + \
407 range(self
.historyIndex
)
408 for i
in searchOrder
:
409 command
= self
.history
[i
]
410 if command
[:len(searchText
)] == searchText
:
411 # Replace the current selection with the one we've found.
412 self
.ReplaceSelection(command
[len(searchText
):])
413 endpos
= self
.GetCurrentPos()
414 self
.SetSelection(endpos
, startpos
)
415 # We've now warped into middle of the history.
416 self
.historyIndex
= i
419 def setStatusText(self
, text
):
420 """Display status information."""
422 # This method will most likely be replaced by the enclosing app
423 # to do something more interesting, like write to a status bar.
426 def processLine(self
):
427 """Process the line of text at which the user hit Enter."""
429 # The user hit ENTER and we need to decide what to do. They could be
430 # sitting on any line in the shell.
432 thepos
= self
.GetCurrentPos()
433 endpos
= self
.GetTextLength()
434 # If they hit RETURN at the very bottom, execute the command.
437 if self
.getCommand():
438 command
= self
.GetTextRange(self
.prompt1Pos
[1], endpos
)
440 # This is a hack, now that we allow editing of previous
441 # lines, which throws off our promptPos values.
442 newend
= endpos
- len(self
.getCommand(rstrip
=0))
443 command
= self
.GetTextRange(self
.prompt1Pos
[1], newend
)
444 command
= command
.replace(os
.linesep
+ sys
.ps2
, '\n')
446 # Or replace the current command with the other command.
447 elif thepos
< self
.prompt1Pos
[0]:
448 theline
= self
.GetCurrentLine()
449 command
= self
.getCommand(rstrip
=0)
450 # If the new line contains a command (even an invalid one).
452 command
= self
.getMultilineCommand()
453 self
.SetCurrentPos(endpos
)
454 startpos
= self
.prompt1Pos
[1]
455 self
.SetSelection(startpos
, endpos
)
456 self
.ReplaceSelection('')
459 # Otherwise, put the cursor back where we started.
461 self
.SetCurrentPos(thepos
)
462 self
.SetAnchor(thepos
)
463 # Or add a new line to the current single or multi-line command.
464 elif thepos
> self
.prompt1Pos
[1]:
465 self
.write(os
.linesep
)
469 def getMultilineCommand(self
, rstrip
=1):
470 """Extract a multi-line command from the editor.
472 The command may not necessarily be valid Python syntax."""
473 # XXX Need to extract real prompts here. Need to keep track of the
474 # prompt every time a command is issued.
479 # This is a total hack job, but it works.
480 text
= self
.GetCurLine()[0]
481 line
= self
.GetCurrentLine()
482 while text
[:ps2size
] == ps2
and line
> 0:
485 text
= self
.GetCurLine()[0]
486 if text
[:ps1size
] == ps1
:
487 line
= self
.GetCurrentLine()
489 startpos
= self
.GetCurrentPos() + ps1size
492 while self
.GetCurLine()[0][:ps2size
] == ps2
:
495 stoppos
= self
.GetCurrentPos()
496 command
= self
.GetTextRange(startpos
, stoppos
)
497 command
= command
.replace(os
.linesep
+ sys
.ps2
, '\n')
498 command
= command
.rstrip()
499 command
= command
.replace('\n', os
.linesep
+ sys
.ps2
)
503 command
= command
.rstrip()
506 def getCommand(self
, text
=None, rstrip
=1):
507 """Extract a command from text which may include a shell prompt.
509 The command may not necessarily be valid Python syntax."""
511 text
= self
.GetCurLine()[0]
512 # XXX Need to extract real prompts here. Need to keep track of the
513 # prompt every time a command is issued.
518 # Strip the prompt off the front of text leaving just the command.
519 if text
[:ps1size
] == ps1
:
520 command
= text
[ps1size
:]
521 elif text
[:ps2size
] == ps2
:
522 command
= text
[ps2size
:]
526 command
= command
.rstrip()
529 def push(self
, command
):
530 """Send command to the interpreter for execution."""
531 self
.write(os
.linesep
)
532 self
.more
= self
.interp
.push(command
)
534 self
.addHistory(command
.rstrip())
537 def addHistory(self
, command
):
538 """Add command to the command history."""
539 # Reset the history position.
540 self
.historyIndex
= -1
541 # Insert this command into the history, unless it's a blank
542 # line or the same as the last command.
544 and (len(self
.history
) == 0 or command
!= self
.history
[0]):
545 self
.history
.insert(0, command
)
547 def write(self
, text
):
548 """Display text in the shell.
550 Replace line endings with OS-specific endings."""
551 text
= self
.fixLineEndings(text
)
553 self
.EnsureCaretVisible()
555 def fixLineEndings(self
, text
):
556 """Return text with line endings replaced by OS-specific endings."""
557 lines
= text
.split('\r\n')
558 for l
in range(len(lines
)):
559 chunks
= lines
[l
].split('\r')
560 for c
in range(len(chunks
)):
561 chunks
[c
] = os
.linesep
.join(chunks
[c
].split('\n'))
562 lines
[l
] = os
.linesep
.join(chunks
)
563 text
= os
.linesep
.join(lines
)
567 """Display appropriate prompt for the context, either ps1 or ps2.
569 If this is a continuation line, autoindent as necessary."""
571 prompt
= str(sys
.ps2
)
573 prompt
= str(sys
.ps1
)
574 pos
= self
.GetCurLine()[1]
575 if pos
> 0: self
.write(os
.linesep
)
576 self
.promptPos
[0] = self
.GetCurrentPos()
577 if not self
.more
: self
.prompt1Pos
[0] = self
.GetCurrentPos()
579 self
.promptPos
[1] = self
.GetCurrentPos()
581 self
.prompt1Pos
[1] = self
.GetCurrentPos()
582 # Keep the undo feature from undoing previous responses.
583 self
.EmptyUndoBuffer()
584 # XXX Add some autoindent magic here if more.
586 self
.write(' '*4) # Temporary hack indentation.
587 self
.EnsureCaretVisible()
588 self
.ScrollToColumn(0)
591 """Replacement for stdin."""
592 prompt
= 'Please enter your response:'
593 dialog
= wxTextEntryDialog(None, prompt
, \
594 'Input Dialog (Standard)', '')
596 if dialog
.ShowModal() == wxID_OK
:
597 text
= dialog
.GetValue()
598 self
.write(text
+ os
.linesep
)
604 def readRaw(self
, prompt
='Please enter your response:'):
605 """Replacement for raw_input."""
606 dialog
= wxTextEntryDialog(None, prompt
, \
607 'Input Dialog (Raw)', '')
609 if dialog
.ShowModal() == wxID_OK
:
610 text
= dialog
.GetValue()
616 def ask(self
, prompt
='Please enter your response:'):
617 """Get response from the user."""
618 return raw_input(prompt
=prompt
)
621 """Halt execution pending a response from the user."""
622 self
.ask('Press enter to continue:')
625 """Delete all text from the shell."""
628 def run(self
, command
, prompt
=1, verbose
=1):
629 """Execute command within the shell as if it was typed in directly.
630 >>> shell.run('print "this"')
635 # Go to the very bottom of the text.
636 endpos
= self
.GetTextLength()
637 self
.SetCurrentPos(endpos
)
638 command
= command
.rstrip()
639 if prompt
: self
.prompt()
640 if verbose
: self
.write(command
)
643 def runfile(self
, filename
):
644 """Execute all commands in file as if they were typed into the shell."""
645 file = open(filename
)
648 for command
in file.readlines():
649 if command
[:6] == 'shell.': # Run shell methods silently.
650 self
.run(command
, prompt
=0, verbose
=0)
652 self
.run(command
, prompt
=0, verbose
=1)
656 def autoCompleteShow(self
, command
):
657 """Display auto-completion popup list."""
658 list = self
.interp
.getAutoCompleteList(command
, \
659 includeMagic
=self
.autoCompleteIncludeMagic
, \
660 includeSingle
=self
.autoCompleteIncludeSingle
, \
661 includeDouble
=self
.autoCompleteIncludeDouble
)
663 options
= ' '.join(list)
665 self
.AutoCompShow(offset
, options
)
667 def autoCallTipShow(self
, command
):
668 """Display argument spec and docstring in a popup bubble thingie."""
669 if self
.CallTipActive
: self
.CallTipCancel()
670 tip
= self
.interp
.getCallTip(command
)
672 offset
= self
.GetCurrentPos()
673 self
.CallTipShow(offset
, tip
)
675 def writeOut(self
, text
):
676 """Replacement for stdout."""
679 def writeErr(self
, text
):
680 """Replacement for stderr."""
683 def redirectStdin(self
, redirect
=1):
684 """If redirect is true then sys.stdin will come from the shell."""
686 sys
.stdin
= PseudoFileIn(self
.readIn
)
688 sys
.stdin
= self
.stdin
690 def redirectStdout(self
, redirect
=1):
691 """If redirect is true then sys.stdout will go to the shell."""
693 sys
.stdout
= PseudoFileOut(self
.writeOut
)
695 sys
.stdout
= self
.stdout
697 def redirectStderr(self
, redirect
=1):
698 """If redirect is true then sys.stderr will go to the shell."""
700 sys
.stderr
= PseudoFileErr(self
.writeErr
)
702 sys
.stderr
= self
.stderr
705 """Return true if text is selected and can be cut."""
706 if self
.GetSelectionStart() != self
.GetSelectionEnd() \
707 and self
.GetSelectionStart() >= self
.prompt1Pos
[1] \
708 and self
.GetSelectionEnd() >= self
.prompt1Pos
[1]:
714 """Return true if text is selected and can be copied."""
715 return self
.GetSelectionStart() != self
.GetSelectionEnd()
718 """Return true if a paste should succeed."""
719 if self
.CanEdit() and wxStyledTextCtrl
.CanPaste(self
):
725 """Return true if editing should succeed."""
726 return self
.GetCurrentPos() >= self
.prompt1Pos
[1]
729 """Remove selection and place it on the clipboard."""
730 if self
.CanCut() and self
.CanCopy():
731 if self
.AutoCompActive(): self
.AutoCompCancel()
732 if self
.CallTipActive
: self
.CallTipCancel()
734 self
.ReplaceSelection('')
737 """Copy selection and place it on the clipboard."""
739 command
= self
.GetSelectedText()
740 command
= command
.replace(os
.linesep
+ sys
.ps2
, os
.linesep
)
741 data
= wxTextDataObject(command
)
742 if wxTheClipboard
.Open():
743 wxTheClipboard
.SetData(data
)
744 wxTheClipboard
.Close()
747 """Replace selection with clipboard contents."""
749 if wxTheClipboard
.Open():
750 if wxTheClipboard
.IsSupported(wxDataFormat(wxDF_TEXT
)):
751 data
= wxTextDataObject()
752 if wxTheClipboard
.GetData(data
):
753 command
= data
.GetText()
754 command
= self
.fixLineEndings(command
)
755 command
= command
.replace(os
.linesep
+ sys
.ps2
, '\n')
756 command
= command
.replace(os
.linesep
, '\n')
757 command
= command
.replace('\n', os
.linesep
+ sys
.ps2
)
758 self
.ReplaceSelection('')
760 wxTheClipboard
.Close()
763 wxID_SELECTALL
= NewId() # This *should* be defined by wxPython.
764 ID_AUTOCOMP
= NewId()
765 ID_AUTOCOMP_SHOW
= NewId()
766 ID_AUTOCOMP_INCLUDE_MAGIC
= NewId()
767 ID_AUTOCOMP_INCLUDE_SINGLE
= NewId()
768 ID_AUTOCOMP_INCLUDE_DOUBLE
= NewId()
769 ID_CALLTIPS
= NewId()
770 ID_CALLTIPS_SHOW
= NewId()
774 """Mixin class to add standard menu items."""
776 def createMenus(self
):
777 m
= self
.fileMenu
= wxMenu()
779 m
.Append(wxID_EXIT
, 'E&xit', 'Exit PyCrust')
781 m
= self
.editMenu
= wxMenu()
782 m
.Append(wxID_UNDO
, '&Undo \tCtrl+Z', 'Undo the last action')
783 m
.Append(wxID_REDO
, '&Redo \tCtrl+Y', 'Redo the last undone action')
785 m
.Append(wxID_CUT
, 'Cu&t \tCtrl+X', 'Cut the selection')
786 m
.Append(wxID_COPY
, '&Copy \tCtrl+C', 'Copy the selection')
787 m
.Append(wxID_PASTE
, '&Paste \tCtrl+V', 'Paste')
789 m
.Append(wxID_CLEAR
, 'Cle&ar', 'Delete the selection')
790 m
.Append(wxID_SELECTALL
, 'Select A&ll', 'Select all text')
792 m
= self
.autocompMenu
= wxMenu()
793 m
.Append(ID_AUTOCOMP_SHOW
, 'Show Auto Completion', \
794 'Show auto completion during dot syntax', \
796 m
.Append(ID_AUTOCOMP_INCLUDE_MAGIC
, 'Include Magic Attributes', \
797 'Include attributes visible to __getattr__ and __setattr__', \
799 m
.Append(ID_AUTOCOMP_INCLUDE_SINGLE
, 'Include Single Underscores', \
800 'Include attibutes prefixed by a single underscore', \
802 m
.Append(ID_AUTOCOMP_INCLUDE_DOUBLE
, 'Include Double Underscores', \
803 'Include attibutes prefixed by a double underscore', \
806 m
= self
.calltipsMenu
= wxMenu()
807 m
.Append(ID_CALLTIPS_SHOW
, 'Show Call Tips', \
808 'Show call tips with argument specifications', checkable
=1)
810 m
= self
.optionsMenu
= wxMenu()
811 m
.AppendMenu(ID_AUTOCOMP
, '&Auto Completion', self
.autocompMenu
, \
812 'Auto Completion Options')
813 m
.AppendMenu(ID_CALLTIPS
, '&Call Tips', self
.calltipsMenu
, \
816 m
= self
.helpMenu
= wxMenu()
818 m
.Append(wxID_ABOUT
, '&About...', 'About PyCrust')
820 b
= self
.menuBar
= wxMenuBar()
821 b
.Append(self
.fileMenu
, '&File')
822 b
.Append(self
.editMenu
, '&Edit')
823 b
.Append(self
.optionsMenu
, '&Options')
824 b
.Append(self
.helpMenu
, '&Help')
827 EVT_MENU(self
, wxID_EXIT
, self
.OnExit
)
828 EVT_MENU(self
, wxID_UNDO
, self
.OnUndo
)
829 EVT_MENU(self
, wxID_REDO
, self
.OnRedo
)
830 EVT_MENU(self
, wxID_CUT
, self
.OnCut
)
831 EVT_MENU(self
, wxID_COPY
, self
.OnCopy
)
832 EVT_MENU(self
, wxID_PASTE
, self
.OnPaste
)
833 EVT_MENU(self
, wxID_CLEAR
, self
.OnClear
)
834 EVT_MENU(self
, wxID_SELECTALL
, self
.OnSelectAll
)
835 EVT_MENU(self
, wxID_ABOUT
, self
.OnAbout
)
836 EVT_MENU(self
, ID_AUTOCOMP_SHOW
, \
837 self
.OnAutoCompleteShow
)
838 EVT_MENU(self
, ID_AUTOCOMP_INCLUDE_MAGIC
, \
839 self
.OnAutoCompleteIncludeMagic
)
840 EVT_MENU(self
, ID_AUTOCOMP_INCLUDE_SINGLE
, \
841 self
.OnAutoCompleteIncludeSingle
)
842 EVT_MENU(self
, ID_AUTOCOMP_INCLUDE_DOUBLE
, \
843 self
.OnAutoCompleteIncludeDouble
)
844 EVT_MENU(self
, ID_CALLTIPS_SHOW
, \
847 EVT_UPDATE_UI(self
, wxID_UNDO
, self
.OnUpdateMenu
)
848 EVT_UPDATE_UI(self
, wxID_REDO
, self
.OnUpdateMenu
)
849 EVT_UPDATE_UI(self
, wxID_CUT
, self
.OnUpdateMenu
)
850 EVT_UPDATE_UI(self
, wxID_COPY
, self
.OnUpdateMenu
)
851 EVT_UPDATE_UI(self
, wxID_PASTE
, self
.OnUpdateMenu
)
852 EVT_UPDATE_UI(self
, wxID_CLEAR
, self
.OnUpdateMenu
)
853 EVT_UPDATE_UI(self
, ID_AUTOCOMP_SHOW
, self
.OnUpdateMenu
)
854 EVT_UPDATE_UI(self
, ID_AUTOCOMP_INCLUDE_MAGIC
, self
.OnUpdateMenu
)
855 EVT_UPDATE_UI(self
, ID_AUTOCOMP_INCLUDE_SINGLE
, self
.OnUpdateMenu
)
856 EVT_UPDATE_UI(self
, ID_AUTOCOMP_INCLUDE_DOUBLE
, self
.OnUpdateMenu
)
857 EVT_UPDATE_UI(self
, ID_CALLTIPS_SHOW
, self
.OnUpdateMenu
)
859 def OnExit(self
, event
):
862 def OnUndo(self
, event
):
865 def OnRedo(self
, event
):
868 def OnCut(self
, event
):
871 def OnCopy(self
, event
):
874 def OnPaste(self
, event
):
877 def OnClear(self
, event
):
880 def OnSelectAll(self
, event
):
881 self
.shell
.SelectAll()
883 def OnAbout(self
, event
):
884 """Display an About PyCrust window."""
886 title
= 'About PyCrust'
887 text
= 'PyCrust %s\n\n' % VERSION
+ \
888 'Yet another Python shell, only flakier.\n\n' + \
889 'Half-baked by Patrick K. O\'Brien,\n' + \
890 'the other half is still in the oven.\n\n' + \
891 'Shell Revision: %s\n' % self
.shell
.revision
+ \
892 'Interpreter Revision: %s\n\n' % self
.shell
.interp
.revision
+ \
893 'Python Version: %s\n' % sys
.version
.split()[0] + \
894 'wxPython Version: %s\n' % wx
.__version
__ + \
895 'Platform: %s\n' % sys
.platform
896 dialog
= wxMessageDialog(self
, text
, title
, wxOK | wxICON_INFORMATION
)
900 def OnAutoCompleteShow(self
, event
):
901 self
.shell
.autoComplete
= event
.IsChecked()
903 def OnAutoCompleteIncludeMagic(self
, event
):
904 self
.shell
.autoCompleteIncludeMagic
= event
.IsChecked()
906 def OnAutoCompleteIncludeSingle(self
, event
):
907 self
.shell
.autoCompleteIncludeSingle
= event
.IsChecked()
909 def OnAutoCompleteIncludeDouble(self
, event
):
910 self
.shell
.autoCompleteIncludeDouble
= event
.IsChecked()
912 def OnCallTipsShow(self
, event
):
913 self
.shell
.autoCallTip
= event
.IsChecked()
915 def OnUpdateMenu(self
, event
):
916 """Update menu items based on current status."""
919 event
.Enable(self
.shell
.CanUndo())
920 elif id == wxID_REDO
:
921 event
.Enable(self
.shell
.CanRedo())
923 event
.Enable(self
.shell
.CanCut())
924 elif id == wxID_COPY
:
925 event
.Enable(self
.shell
.CanCopy())
926 elif id == wxID_PASTE
:
927 event
.Enable(self
.shell
.CanPaste())
928 elif id == wxID_CLEAR
:
929 event
.Enable(self
.shell
.CanCut())
930 elif id == ID_AUTOCOMP_SHOW
:
931 event
.Check(self
.shell
.autoComplete
)
932 elif id == ID_AUTOCOMP_INCLUDE_MAGIC
:
933 event
.Check(self
.shell
.autoCompleteIncludeMagic
)
934 elif id == ID_AUTOCOMP_INCLUDE_SINGLE
:
935 event
.Check(self
.shell
.autoCompleteIncludeSingle
)
936 elif id == ID_AUTOCOMP_INCLUDE_DOUBLE
:
937 event
.Check(self
.shell
.autoCompleteIncludeDouble
)
938 elif id == ID_CALLTIPS_SHOW
:
939 event
.Check(self
.shell
.autoCallTip
)
942 class ShellFrame(wxFrame
, ShellMenu
):
943 """Frame containing the PyCrust shell component."""
945 name
= 'PyCrust Shell Frame'
946 revision
= __version__
948 def __init__(self
, parent
=None, id=-1, title
='PyShell', \
949 pos
=wxDefaultPosition
, size
=wxDefaultSize
, \
950 style
=wxDEFAULT_FRAME_STYLE
, locals=None, \
951 InterpClass
=None, *args
, **kwds
):
952 """Create a PyCrust ShellFrame instance."""
953 wxFrame
.__init
__(self
, parent
, id, title
, pos
, size
, style
)
954 intro
= 'Welcome To PyCrust %s - The Flakiest Python Shell' % VERSION
955 self
.CreateStatusBar()
956 self
.SetStatusText(intro
)
957 if wxPlatform
== '__WXMSW__':
958 icon
= wxIcon('PyCrust.ico', wxBITMAP_TYPE_ICO
)
960 self
.shell
= Shell(parent
=self
, id=-1, introText
=intro
, \
961 locals=locals, InterpClass
=InterpClass
, \
963 # Override the shell so that status messages go to the status bar.
964 self
.shell
.setStatusText
= self
.SetStatusText