PyFrame Guide to  wxPython

Copyright and License information  Home

__  A  B  C  D  E  F  G  H  I  L  M  P  R  S  T  U  V  W 

wxStyledTextCtrl - Lexing

Summary:

Support for syntax coloration and possibly folding is provided by a lexer. The wxPython distribution of the Scintilla editing component includes support for a number of languages. Support for a particular language is set by using the SetLexer method. If you wanted to have your code perform the lexing (unlikely...) you could set the lexer to wxSTC_LEX_CONTAINER; but this is beyond the scope of this guide. If you did use wxSTC_LEX_CONTAINER, you would get STYLE_NEEDED event, see EVT_STC_STYLENEEDED.

You can configure some operational aspects of some of the lexers with SetProperty.


Colourise(start,end)

Manually request the lexer to style a range. start and end are integer character positions in the Document. Returns None. Not really useful in wxPython (?) as the lexer should do this automatically. However, this is the method you'd use if handling a STYLE_NEEDED event as discussed in the Summary section of this page.

top

GetLexer()

Returns an integer object with the lexer selection value.

top

SetKeyWords(keywordSet,keyWords)

If you want to colorize language keywords, this method is for you. This saucy little number takes an integer parameter keywordSet and a space-delimited set of keywords in the string parameter keyWords. Impudently returns the omnipresent None.

Some languages require even more fershlugginer complexity. Deceptively simple HTML, for example, can have embedded languages like VBScript and Javascript. For HTML, use keywordSet 0 for HTML, 1 for Javascript, and 2 for VBScript support. Otherwise always set this to 0.

Examples

Python

stcInstance.SetKeyWords(0,string.join(python_keywords.kwlist))

HTML with optional Zope DTML support

 
htmlKeywords = (
        "a abbr acronym address applet area b base basefont bdo big blockquote"
        " body br button caption center cite code col colgroup dd del dfn dir"
        " div dl dt em fieldset font form frame frameset h1 h2 h3 h4 h5 h6"
        " head hr html i iframe img input ins isindex kbd label legend li link"
        " map menu meta noframes noscript object ol optgroup option p param"
        " pre q s samp script select small span strike strong style sub sup"
        " table tbody td textarea tfoot th thead title tr tt u ul var xml"
        " xmlns abbr accept-charset accept accesskey action align alink alt"
        " archive axis background bgcolor border cellpadding cellspacing char"
        " charoff charset checked cite class classid clear codebase codetype"
        " color cols colspan compact content coords data datafld dataformatas"
        " datapagesize datasrc datetime declare defer dir disabled enctype"
        " event face for frame frameborder headers height href hreflang hspace"
        " http-equiv id ismap label lang language leftmargin link longdesc"
        " marginwidth marginheight maxlength media method multiple name nohref"
        " noresize noshade nowrap object onblur onchange onclick ondblclick"
        " onfocus onkeydown onkeypress onkeyup onload onmousedown onmousemove"
        " onmouseover onmouseout onmouseup onreset onselect onsubmit onunload"
        " profile prompt readonly rel rev rows rowspan rules scheme scope"
        " selected shape size span src standby start style summary tabindex"
        " target text title topmargin type usemap valign value valuetype"
        " version vlink vspace width text password checkbox radio submit reset"
        " file hidden image public !doctype")

dtmlKeywords = (
        "dtml-var dtml-if dtml-unless dtml-in dtml-with dtml-let dtml-call"
        "dtml-raise dtml-try dtml-comment dtml-tree")
         
 
keywords = htmlKeywords
if self.submode and 1:  #add DTML keywords
	keywords = htmlKeywords + ' ' + dtmlKeywords
stcInstance.SetKeyWords(0,keywords)
top

SetLexer(lexer)

Set the lexer support for a particular language. The integer parameter lexer should be chosen from the following list. Note that the list indicates whether or not folding is supported and any properties (SetProperty) used by the lexer. The method returns None.

ValueFolding?properties
wxSTC_LEX_ADAnono
wxSTC_LEX_ASPnosee wxSTC_LEX_HTML
wxSTC_LEX_AUTOMATICN.A.N.A.
wxSTC_LEX_AVEno?"fold" (not implemented?)
wxSTC_LEX_BAANyes"styling.within.preprocessor"
"fold.comment"
"fold.compact"
wxSTC_LEX_BATCHnono
wxSTC_LEX_BULLANTno?"fold" (not implemented?)
wxSTC_LEX_CONFnono
wxSTC_LEX_CONTAINERN.A.N.A.
wxSTC_LEX_CPPyes"styling.within.preprocessor"
"fold.comment"
"fold.preprocessor"
"fold.compact"
wxSTC_LEX_DIFFnono
wxSTC_LEX_EIFFELyesno
wxSTC_LEX_EIFFELKWyesno
wxSTC_LEX_ERRORLISTnono
wxSTC_LEX_HTMLno"asp.default.language" (javascript)
"fold.html"
"fold"
"fold.compact"
wxSTC_LEX_LATEXnono
wxSTC_LEX_LISPyesno
wxSTC_LEX_LUAyes"fold.compact"
wxSTC_LEX_MAKEFILEnono
wxSTC_LEX_MATLAByesno
wxSTC_LEX_NNCRONTABnono
wxSTC_LEX_NULLN.A.Null language just handles the protocol but does nothing. Use for plain text.
wxSTC_LEX_PASCALyes"fold.comment"
"fold.preprocessor"
"fold.compact"
wxSTC_LEX_PERLyesno
wxSTC_LEX_PHPnosee wxSTC_LEX_HTML
wxSTC_LEX_PROPERTIESnono
wxSTC_LEX_PYTHONyes"fold.comment.python"
"fold.quotes.python"
"tab.timmy.whinge.level"
wxSTC_LEX_RUBYyes"tab.timmy.whinge.level"
wxSTC_LEX_SCRIPTOLUnknownAlthough defined, support doesn't exist.
wxSTC_LEX_SQLyes"fold"
wxSTC_LEX_TCLyessee wxSTC_LEX_HTML
wxSTC_LEX_VByesno
wxSTC_LEX_VBSCRIPTyesno
wxSTC_LEX_XCODEUnknownAlthough defined, support doesn't exist. It's for for DevelopMentor's Gen product.
wxSTC_LEX_XMLnosee wxSTC_LEX_HTML

Note: "tab.timmy.whinge.level" is a setting that determines how to indicate bad indentation.

When a lexer specifies its language as wxSTC_LEX_AUTOMATIC it receives a value assigned in sequence from wxSTC_LEX_AUTOMATIC+1 (wxSTC_LEX_AUTOMATIC = 1000). This is ordinarily not useful in a wxPython application.

wxSTC_LEX_NULL is useful (believe it or not) when you're editing plain text, although one supposes you could not set this one and the STC would still work just fine.

top

SetLexerLanguage()

Set the lexer language using a string rather than one of the integers shown in SetLexer. Returns None.

The strings that can be used are: ada, ave, baan, bullant, conf, cpp, tcl, nncrontab, eiffel, eiffelkw, hypertext (this is for HTML), xml, asp, php, lisp, lua, matlab, batch, diff, props, makefile, errorlist, latex, pascal, perl, python, ruby, sql, vb, and vbscript.

top

SetProperty(key,value)

This method is used to set a property for a lexer to a certain value. This method returns None. Both the key and value parameters are string objects. For example, if you were using the Python lexer, you might want to twiddle the "tab.timmy.whinge.level" (whatever that means!) setting like this:

stcInstance.SetProperty("tab.timmy.whinge.level", "1")

See SetLexer for supported properties.

top