wx.stc.StyledTextCtrl: HowTo suppress lexing for some lines?

I am using wxSTC with the Python Lexer for a Python shell (based on pycrust). I am trying to turn off Python styling for stdout, but am running into the problem described below:

  Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
  Type "help", "copyright", "credits" or "license" for more information.

First, just test writing to the shell - this works fine, but it's Python styled:

  >>> self = _Session.shell # get a reference to the wxSTC instance
  >>> assert isinstance(self, wx.stc.StyledTextCtrl)
  >>>
  >>> def write(text):
  ... self.AddText(text)
  ...
  >>> write("while not print")
  while not print # This is displayed in Python style, as expected
...
Now, I try to write plain text by setting the Lexer to NULL. This also works as I expect:

  >>> lexer = self.GetLexer() # preserve the existing one
  >>> self.SetLexer(wx.stc.STC_LEX_NULL)
  >>> write("while not print")
  while not print # This is displayed without styling, as expected
  >>>
  >>> self.SetLexer(lexer) # return the styling to Python

Now, try to include the lexer changes in a function:

  >>> def writeplain(text):
  ... lexer = self.GetLexer()
  ... self.SetLexer(wx.stc.STC_LEX_NULL)
  ... self.AddText(text)
  ... self.SetLexer(lexer)
  ...
  >>> writeplain("while not print")
  while not print # <<<< This is Python styled though I want it to be plain
  >>> wx.VERSION_STRING
  '2.5.3.1'
  >>>

I presume that after changing the Lexer I need to do something else to make the change effective. But what? Any ideas or pointers to code that does this, much appreciated

Michael

Michael Spencer wrote:

Now, try to include the lexer changes in a function:

>>> def writeplain(text):
... lexer = self.GetLexer()
... self.SetLexer(wx.stc.STC_LEX_NULL)
... self.AddText(text)
... self.SetLexer(lexer)
...
>>> writeplain("while not print")
while not print # <<<< This is Python styled though I want it to be plain

I presume that after changing the Lexer I need to do something else to make the change effective. But what? Any ideas or pointers to code that does this, much appreciated

Setting a lexer makes it apply to the whole buffer, not just the text being added after the lexer change. To do what you are wanting you'll probably have to handle setting the styles yourself rather than using one of the built-in lexers.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Michael Spencer wrote:

[How to get plain text in an STC buffer that has a Lexer]

Setting a lexer makes it apply to the whole buffer, not just the text being added after the lexer change. To do what you are wanting you'll probably have to handle setting the styles yourself rather than using one of the built-in lexers.

Thanks for the tip, Robin:

I got what I wanted (almost*) working with the following:

def writeOut(self, text, style = wx.stc.STC_STYLE_DEFAULT):
     """Replacement for stdout."""

     pos = self.GetCurrentPos()
     self.write(text)
     # See: http://www.yellowbrain.com/stc/styling.html#start
     self.StartStyling(pos, 0x1F)
     self.SetStyling(len(text)+5, style)

in other words, I realized that I didn't need to turn off the Python lexer, rather just set the style for the lines that are plain text to wx.stc.STC_STYLE_DEFAULT

*almost, because this applies the plain style to all but the last line of stdout. I suspect something to do with line ending characters, and the update of the stc position index.

Michael