Discussion:
SetKeyword Problem On wxStyledTextCtrl/Scintilla
a***@agip.it
2004-12-14 08:35:44 UTC
Permalink
Hello NG,

I have already posted this message on the wxPython NG, and after a
discussion they suggested me to post my message on the Scintilla NG. I am
using wxPython and a styled text control, with a LUA style keyword set (it
is very similar to what I need).
For what I understood till now, it seems to me that wxStyledTextCtrl (and
so Scintilla) does NOT recognize keywords that starts with characters like
$, %, @, # and so on. I was going crazy. If someone of you uses
Python+wxPython, I leave at the end 2 python scripts that are exactly the
same EXCEPT for the keyword set: the first one contains keywords WITHOUT
the character "$", the second one contains keywords that start with the
character "$". Just press the button at right to see if the keywords are
highlighted or not (they should be RED BOLD when you press the button).
Notice that in the second application there is no highlight in the
keyword... it is a big problem, because I need keywords that start with
$...

Any help is HIGHLY HIGHLY appreciated. Sorry for the long post.

Andrea.


# CODE APP 1 BEGIN #

import wx
import wx.stc as stc

#
============================================================================

# PLATFORM INDEPENDENCY DECLARATIONS
#
============================================================================


if wx.Platform == '__WXMSW__':
faces = { 'times': 'Times New Roman',
'mono' : 'Courier New',
'helv' : 'Verdana',
'other': 'Comic Sans MS',
'size' : 9,
'size2': 8,
}
else:
faces = { 'times': 'Times',
'mono' : 'Courier',
'helv' : 'Helvetica',
'other': 'new century schoolbook',
'size' : 12,
'size2': 10,
}


class ECLIPSE_STC(stc.StyledTextCtrl):
def __init__(self, parent, ID):

stc.StyledTextCtrl.__init__(self, parent, ID,
style = wx.NO_FULL_REPAINT_ON_RESIZE)

keywordsmie = ["THESE","KEYWORDS","WORK","WELL"]

self.SetLexer(stc.STC_LEX_LUA)
self.SetKeyWords(0, " ".join(keywordsmie))

self.StyleClearAll()

# Global default styles for all languages
self.StyleSetSpec(stc.STC_STYLE_DEFAULT,
"face:%(helv)s,size:%(size)d" %
faces)

# ECLIPSE styles
# Default
self.StyleSetSpec(stc.STC_LUA_DEFAULT,
"fore:
#000000,face:%(times)s,size:%(size)d" % faces)

# Keyword
self.StyleSetSpec(stc.STC_LUA_WORD,
"fore:
#0000FF,bold,underline,size:%(size)d" % faces)



class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title, size=(600, 350))

panel = wx.Panel(self,-1)

ed = ECLIPSE_STC(panel, -1)
ed.SetPosition(wx.Point(10,10))
ed.SetSize(wx.Size(400,300))

mystr = "Press This To\nSee The Second\nSet Of Keywords"
b = wx.Button(panel, -1, mystr, pos=(450,100), size=(100,100))

self.Bind(wx.EVT_BUTTON, self.PressedButton, b)

mystr = "THESE\nKEYWORDS\nWORK\nWELL\n\n"
ed.SetText(mystr)
ed.Colourise(0, -1)

self.ed = ed

def PressedButton(self, event):

keywords2 = ["WHY","THEY","DONOT","UPDATE"]
self.ed.SetKeyWords(1, " ".join(keywords2))
# Keyword 2
self.ed.StyleSetSpec(stc.STC_LUA_WORD2,
"fore:#FF0000,bold,size:%(size)d" %
faces)

mystr
= "THESE\nKEYWORDS\nWORK\nWELL\n\n\nWHY\nTHEY\nDONOT\nUPDATE\n"
self.ed.SetText(mystr)
self.ed.Colourise(0, -1)


app = wx.PySimpleApp()
frame = MyFrame(None, -1, "wxSTC Test")
frame.Show()
app.MainLoop()

# CODE APP 1 END #


#############################################################


# CODE APP 2 BEGIN #

import wx
import wx.stc as stc

#
============================================================================

# PLATFORM INDEPENDENCY DECLARATIONS
#
============================================================================


if wx.Platform == '__WXMSW__':
faces = { 'times': 'Times New Roman',
'mono' : 'Courier New',
'helv' : 'Verdana',
'other': 'Comic Sans MS',
'size' : 9,
'size2': 8,
}
else:
faces = { 'times': 'Times',
'mono' : 'Courier',
'helv' : 'Helvetica',
'other': 'new century schoolbook',
'size' : 12,
'size2': 10,
}


class ECLIPSE_STC(stc.StyledTextCtrl):
def __init__(self, parent, ID):

stc.StyledTextCtrl.__init__(self, parent, ID,
style = wx.NO_FULL_REPAINT_ON_RESIZE)

keywordsmie = ["THESE","KEYWORDS","WORK","WELL"]

self.SetLexer(stc.STC_LEX_LUA)
self.SetKeyWords(0, " ".join(keywordsmie))

self.StyleClearAll()

# Global default styles for all languages
self.StyleSetSpec(stc.STC_STYLE_DEFAULT,
"face:%(helv)s,size:%(size)d" %
faces)

# ECLIPSE styles
# Default
self.StyleSetSpec(stc.STC_LUA_DEFAULT,
"fore:
#000000,face:%(times)s,size:%(size)d" % faces)

# Keyword
self.StyleSetSpec(stc.STC_LUA_WORD,
"fore:
#0000FF,bold,underline,size:%(size)d" % faces)



class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title, size=(600, 350))

panel = wx.Panel(self,-1)

ed = ECLIPSE_STC(panel, -1)
ed.SetPosition(wx.Point(10,10))
ed.SetSize(wx.Size(400,300))

mystr = "Press This To\nSee The Second\nSet Of Keywords"
b = wx.Button(panel, -1, mystr, pos=(450,100), size=(100,100))

self.Bind(wx.EVT_BUTTON, self.PressedButton, b)

mystr = "THESE\nKEYWORDS\nWORK\nWELL\n\n"
ed.SetText(mystr)
ed.Colourise(0, -1)

self.ed = ed

def PressedButton(self, event):

keywords2 = ["$WHY","$THEY","$DONOT","$UPDATE"]
self.ed.SetKeyWords(1, " ".join(keywords2))
# Keyword 2
self.ed.StyleSetSpec(stc.STC_LUA_WORD2,
"fore:#FF0000,bold,size:%(size)d" %
faces)

mystr
= "THESE\nKEYWORDS\nWORK\nWELL\n\n\n$WHY\n$THEY\n$DONOT\n$UPDATE\n"
self.ed.SetText(mystr)
self.ed.Colourise(0, -1)


app = wx.PySimpleApp()
frame = MyFrame(None, -1, "wxSTC Test")
frame.Show()
app.MainLoop()


_______________________________________________
Andrea Gavana
Reservoir Engineer
MOGI ? Reservoir Characterization and Modeling  Dept.
ENI S.p.A. ? Exploration & Production Division
Via Emilia, 1 ? 20097 San Donato Milanese (MI) ? Italy
Phone: +39 02 520 62972
Fax:     +39 02 520 61824
E-mail: ***@agip.it
Updated Internet Site: http://xoomer.virgilio.it/infinity77/
____________________________________________________

Eni S.p.A.
Sede legale in Roma,
Piazzale Enrico Mattei 1, 00144 Roma
Tel. centralino: +39 06598.21
www.eni.it
Capitale sociale € 4.002.934.326 i.v.
Registro Imprese di Roma,
Codice Fiscale 00484960588
Part. IVA 00905811006
R.E.A. Roma n. 756453


-------------------------------------------------------------------------------------------------------------------------------------------
Message for the recipient only, if received in error, please notify the
sender and read http://www.eni.it/disclaimer/
Iago Rubio
2004-12-14 14:35:30 UTC
Permalink
Post by a***@agip.it
Hello NG,
I have already posted this message on the wxPython NG, and after a
discussion they suggested me to post my message on the Scintilla NG. I am
using wxPython and a styled text control, with a LUA style keyword set (it
is very similar to what I need).
For what I understood till now, it seems to me that wxStyledTextCtrl (and
so Scintilla) does NOT recognize keywords that starts with characters like
I don't know a word about WxWidget neither about Lua, but reading the
LexLua.cxx file it seems that some of those characters are taken as
special ones by the lexer.
http://cvs.sourceforge.net/viewcvs.py/scintilla/scintilla/src/LexLua.cxx?view=markup

% is taken as a Lua operator (I suposse the fractional module operator?)
$ is taken as a Lua preproccessor.
# is taken as a single line comment.

With this issues, the lexing will not work if you use %foo, $bar or
#blah in your Lua code.
Post by a***@agip.it
Python+wxPython, I leave at the end 2 python scripts that are exactly the
same EXCEPT for the keyword set: the first one contains keywords WITHOUT
the character "$", the second one contains keywords that start with the
character "$".
Being python, Why you use Lua styling ?
Post by a***@agip.it
Just press the button at right to see if the keywords are
highlighted or not (they should be RED BOLD when you press the button).
Notice that in the second application there is no highlight in the
keyword... it is a big problem, because I need keywords that start with
$...
There's a message to Scintilla to tell the control what are the word
chars - the characters valid into a word.

SCI_SETWORDCHARS(<unused>, const char *chars)
http://scintilla.sourceforge.net/ScintillaDoc.html#SCI_SETWORDCHARS

I don't know how it maps to wxScintilla or wxPython, but there should be
a binding for this somewere.

If you find this binding, you can try with:
SCI_SETWORDCHARS(0,
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$");

I don't know if it will work, but you can take a try.
Post by a***@agip.it
Any help is HIGHLY HIGHLY appreciated. Sorry for the long post.
Hope this helps.
--
Iago Rubio
- Home page * http://www.iagorubio.com
- Last project * http://cssed.sourceforge.net
- GPG Keyserv * pgp.rediris.es id=0x909BD4DD
a***@agip.it
2004-12-14 15:49:56 UTC
Permalink
Hello Iago,

thanks for your answer. I have anyway a couple of points that are not
Post by Iago Rubio
I don't know a word about WxWidget neither about Lua, but reading the
LexLua.cxx file it seems that some of those characters are taken as
special ones by the lexer.
http://cvs.sourceforge.net/viewcvs.py/scintilla/scintilla/src/LexLua.cxx?view=markup
Post by Iago Rubio
% is taken as a Lua operator (I suposse the fractional module operator?)
$ is taken as a Lua preproccessor.
# is taken as a single line comment.
With this issues, the lexing will not work if you use %foo, $bar or
#blah in your Lua code.
Everything you said is correct, but I also tried with characters like "@",
"£", and it does not work anyway.
Post by Iago Rubio
Being python, Why you use Lua styling ?
Because LUA style is very similar to the style I need, that has nothing to
share with Python. I only build an editor in Python+wxPython, and that
editor needs to have a lexer that is very similar to the LUA one.
Post by Iago Rubio
There's a message to Scintilla to tell the control what are the word
chars - the characters valid into a word.
SCI_SETWORDCHARS(<unused>, const char *chars)
http://scintilla.sourceforge.net/ScintillaDoc.html#SCI_SETWORDCHARS
There is a binding in wxPython, called SetWordChars... I didn't see it
before, the wxStyledTextCtrl is quite complex (as Scintilla is). However,
also by using the instructions:

1) SetWordChars("
_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$")
2) SetWordChars("
***@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
3) SetWordChars("
***@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$")

Nothing changed... my keywords are not recognized.

Thank you a lot for your suggestions.

Andrea.

Loading...