DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Python Syntax Coloring For Series60

08.01.2005
| 6004 views |
  • submit to reddit
        The new Text() control allows rich text which can be
used for syntax coloring
(screenshot)
http://photos22.flickr.com/30187174_8598b800ec_o.jpg
from appuifw import *
from pyfontify import fontify
import os, e32

src = ur"""
__version__ = "0.4"
import string, re, keyword  
commentPat = "#.*"  
# Build up a regular expression
pat = "q[^\q\n]*(\\\\[\000-\377][^\q\n]*)*q"
"""

color = { 'keyword': 0x0000ff,
          'string': 0xff00ff,
          'comment': 0x008000,
          'function': 0x008080,
          'class': 0x008080 }

t = Text()
pos = 0
for tag, start, end, sl in fontify(src):
    t.color = 0
    t.add(src[pos:start])
    t.color = color[tag]
    t.add(src[start:end])
    pos = end
t.color = 0
t.add(src[pos:])

app.body = t
e32.ao_sleep(10)
You first need to install the pyfontify module from here
http://linux.duke.edu/~mstenner/free-docs/diveintopython-3.9-1/py/pyfontify.py