from Delegator
import Delegator
from configHandler
import idleConf
return "(?P<%s>" % name
+ "|".join(list) + ")"
kw
= r
"\b" + any("KEYWORD", keyword
.kwlist
) + r
"\b"
builtinlist
= [str(name
) for name
in dir(__builtin__
)
if not name
.startswith('_')]
# self.file = file("file") :
# 1st 'file' colorized normal, 2nd as builtin, 3rd as comment
builtin
= r
"([^.'\"\\]\b|^
)" + any("BUILTIN
", builtinlist) + r"\b"
comment = any("COMMENT
", [r"#[^\n]*"])
sqstring
= r
"(\b[rR])?'[^'\\\n]*(\\.[^'\\\n]*)*'?"
dqstring
= r
'(\b[rR])?"[^"\\\n]*(\\.[^"\\\n]*)*"?'
sq3string
= r
"(\b[rR])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
dq3string
= r
'(\b[rR])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
string
= any("STRING", [sq3string
, dq3string
, sqstring
, dqstring
])
return kw
+ "|" + builtin
+ "|" + comment
+ "|" + string
+\
"|" + any("SYNC", [r
"\n"])
prog
= re
.compile(make_pat(), re
.S
)
idprog
= re
.compile(r
"\s+(\w+)", re
.S
)
asprog
= re
.compile(r
".*?\b(as)\b", re
.S
)
class ColorDelegator(Delegator
):
def setdelegate(self
, delegate
):
if self
.delegate
is not None:
self
.unbind("<<toggle-auto-coloring>>")
Delegator
.setdelegate(self
, delegate
)
self
.bind("<<toggle-auto-coloring>>", self
.toggle_colorize_event
)
self
.notify_range("1.0", "end")
for tag
, cnf
in self
.tagdefs
.items():
self
.tag_configure(tag
, **cnf
)
theme
= idleConf
.GetOption('main','Theme','name')
"COMMENT": idleConf
.GetHighlight(theme
, "comment"),
"KEYWORD": idleConf
.GetHighlight(theme
, "keyword"),
"BUILTIN": idleConf
.GetHighlight(theme
, "builtin"),
"STRING": idleConf
.GetHighlight(theme
, "string"),
"DEFINITION": idleConf
.GetHighlight(theme
, "definition"),
"SYNC": {'background':None,'foreground':None},
"TODO": {'background':None,'foreground':None},
"BREAK": idleConf
.GetHighlight(theme
, "break"),
"ERROR": idleConf
.GetHighlight(theme
, "error"),
# The following is used by ReplaceDialog:
"hit": idleConf
.GetHighlight(theme
, "hit"),
if DEBUG
: print 'tagdefs',self
.tagdefs
def insert(self
, index
, chars
, tags
=None):
index
= self
.index(index
)
self
.delegate
.insert(index
, chars
, tags
)
self
.notify_range(index
, index
+ "+%dc" % len(chars
))
def delete(self
, index1
, index2
=None):
index1
= self
.index(index1
)
self
.delegate
.delete(index1
, index2
)
self
.notify_range(index1
)
def notify_range(self
, index1
, index2
=None):
self
.tag_add("TODO", index1
, index2
)
if DEBUG
: print "colorizing already scheduled"
self
.stop_colorizing
= True
if DEBUG
: print "stop colorizing"
if self
.allow_colorizing
:
if DEBUG
: print "schedule colorizing"
self
.after_id
= self
.after(1, self
.recolorize
)
close_when_done
= None # Window to be closed when done colorizing
def close(self
, close_when_done
=None):
if DEBUG
: print "cancel scheduled recolorizer"
self
.after_cancel(after_id
)
self
.allow_colorizing
= False
self
.stop_colorizing
= True
close_when_done
.destroy()
self
.close_when_done
= close_when_done
def toggle_colorize_event(self
, event
):
if DEBUG
: print "cancel scheduled recolorizer"
self
.after_cancel(after_id
)
if self
.allow_colorizing
and self
.colorizing
:
if DEBUG
: print "stop colorizing"
self
.stop_colorizing
= True
self
.allow_colorizing
= not self
.allow_colorizing
if self
.allow_colorizing
and not self
.colorizing
:
self
.after_id
= self
.after(1, self
.recolorize
)
print "auto colorizing turned",\
self
.allow_colorizing
and "on" or "off"
if DEBUG
: print "no delegate"
if not self
.allow_colorizing
:
if DEBUG
: print "auto colorizing is off"
if DEBUG
: print "already colorizing"
self
.stop_colorizing
= False
if DEBUG
: print "colorizing..."
if DEBUG
: print "%.3f seconds" % (t1
-t0
)
if self
.allow_colorizing
and self
.tag_nextrange("TODO", "1.0"):
if DEBUG
: print "reschedule colorizing"
self
.after_id
= self
.after(1, self
.recolorize
)
top
= self
.close_when_done
self
.close_when_done
= None
def recolorize_main(self
):
item
= self
.tag_nextrange("TODO", next
)
self
.tag_remove("SYNC", head
, tail
)
item
= self
.tag_prevrange("SYNC", head
)
next
= self
.index(mark
+ "+%d lines linestart" %
lines_to_get
= min(lines_to_get
* 2, 100)
ok
= "SYNC" in self
.tag_names(next
+ "-1c")
line
= self
.get(mark
, next
)
##print head, "get", mark, next, "->", repr(line)
for tag
in self
.tagdefs
.keys():
self
.tag_remove(tag
, mark
, next
)
m
= self
.prog
.search(chars
)
for key
, value
in m
.groupdict().items():
if value
in ("def", "class"):
m1
= self
.idprog
.match(chars
, b
)
self
.tag_add("DEFINITION",
# color all the "as" words on same line;
# cheap approximation to the truth
m1
= self
.asprog
.match(chars
, b
)
m
= self
.prog
.search(chars
, m
.end())
if "SYNC" in self
.tag_names(next
+ "-1c"):
# We're in an inconsistent state, and the call to
# update may tell us to stop. It may also change
# the correct value for "next" (since this is a
# line.col string, not a true mark). So leave a
# crumb telling the next invocation to resume here
# in case update tells us to leave.
self
.tag_add("TODO", next
)
if DEBUG
: print "colorizing stopped"
from Percolator
import Percolator
root
.wm_protocol("WM_DELETE_WINDOW", root
.quit
)
text
= Text(background
="white")
text
.pack(expand
=1, fill
="both")
if __name__
== "__main__":