6 from Delegator
import Delegator
7 from IdleConf
import idleconf
9 #$ event <<toggle-auto-coloring>>
10 #$ win <Control-slash>
11 #$ unix <Control-slash>
17 return "(?P<%s>" % name
+ string
.join(list, "|") + ")"
20 kw
= r
"\b" + any("KEYWORD", keyword
.kwlist
) + r
"\b"
21 comment
= any("COMMENT", [r
"#[^\n]*"])
22 sqstring
= r
"(\b[rR])?'[^'\\\n]*(\\.[^'\\\n]*)*'?"
23 dqstring
= r
'(\b[rR])?"[^"\\\n]*(\\.[^"\\\n]*)*"?'
24 sq3string
= r
"(\b[rR])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
25 dq3string
= r
'(\b[rR])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
26 string
= any("STRING", [sq3string
, dq3string
, sqstring
, dqstring
])
27 return kw
+ "|" + comment
+ "|" + string
+ "|" + any("SYNC", [r
"\n"])
29 prog
= re
.compile(make_pat(), re
.S
)
30 idprog
= re
.compile(r
"\s+(\w+)", re
.S
)
32 class ColorDelegator(Delegator
):
35 Delegator
.__init
__(self
)
39 def setdelegate(self
, delegate
):
40 if self
.delegate
is not None:
41 self
.unbind("<<toggle-auto-coloring>>")
42 Delegator
.setdelegate(self
, delegate
)
43 if delegate
is not None:
45 self
.bind("<<toggle-auto-coloring>>", self
.toggle_colorize_event
)
46 self
.notify_range("1.0", "end")
48 def config_colors(self
):
49 for tag
, cnf
in self
.tagdefs
.items():
51 apply(self
.tag_configure
, (tag
,), cnf
)
54 cconf
= idleconf
.getsection('Colors')
57 "COMMENT": cconf
.getcolor("comment"),
58 "KEYWORD": cconf
.getcolor("keyword"),
59 "STRING": cconf
.getcolor("string"),
60 "DEFINITION": cconf
.getcolor("definition"),
61 "SYNC": cconf
.getcolor("sync"),
62 "TODO": cconf
.getcolor("todo"),
63 "BREAK": cconf
.getcolor("break"),
64 # The following is used by ReplaceDialog:
65 "hit": cconf
.getcolor("hit"),
68 def insert(self
, index
, chars
, tags
=None):
69 index
= self
.index(index
)
70 self
.delegate
.insert(index
, chars
, tags
)
71 self
.notify_range(index
, index
+ "+%dc" % len(chars
))
73 def delete(self
, index1
, index2
=None):
74 index1
= self
.index(index1
)
75 self
.delegate
.delete(index1
, index2
)
76 self
.notify_range(index1
)
82 def notify_range(self
, index1
, index2
=None):
83 self
.tag_add("TODO", index1
, index2
)
85 if __debug__
: print "colorizing already scheduled"
88 self
.stop_colorizing
= 1
89 if __debug__
: print "stop colorizing"
90 if self
.allow_colorizing
:
91 if __debug__
: print "schedule colorizing"
92 self
.after_id
= self
.after(1, self
.recolorize
)
94 close_when_done
= None # Window to be closed when done colorizing
96 def close(self
, close_when_done
=None):
98 after_id
= self
.after_id
100 if __debug__
: print "cancel scheduled recolorizer"
101 self
.after_cancel(after_id
)
102 self
.allow_colorizing
= 0
103 self
.stop_colorizing
= 1
105 if not self
.colorizing
:
106 close_when_done
.destroy()
108 self
.close_when_done
= close_when_done
110 def toggle_colorize_event(self
, event
):
112 after_id
= self
.after_id
114 if __debug__
: print "cancel scheduled recolorizer"
115 self
.after_cancel(after_id
)
116 if self
.allow_colorizing
and self
.colorizing
:
117 if __debug__
: print "stop colorizing"
118 self
.stop_colorizing
= 1
119 self
.allow_colorizing
= not self
.allow_colorizing
120 if self
.allow_colorizing
and not self
.colorizing
:
121 self
.after_id
= self
.after(1, self
.recolorize
)
123 print "auto colorizing turned", self
.allow_colorizing
and "on" or "off"
126 def recolorize(self
):
128 if not self
.delegate
:
129 if __debug__
: print "no delegate"
131 if not self
.allow_colorizing
:
132 if __debug__
: print "auto colorizing is off"
135 if __debug__
: print "already colorizing"
138 self
.stop_colorizing
= 0
140 if __debug__
: print "colorizing..."
142 self
.recolorize_main()
144 if __debug__
: print "%.3f seconds" % (t1
-t0
)
147 if self
.allow_colorizing
and self
.tag_nextrange("TODO", "1.0"):
148 if __debug__
: print "reschedule colorizing"
149 self
.after_id
= self
.after(1, self
.recolorize
)
150 if self
.close_when_done
:
151 top
= self
.close_when_done
152 self
.close_when_done
= None
155 def recolorize_main(self
):
158 item
= self
.tag_nextrange("TODO", next
)
162 self
.tag_remove("SYNC", head
, tail
)
163 item
= self
.tag_prevrange("SYNC", head
)
175 next
= self
.index(mark
+ "+%d lines linestart" %
177 lines_to_get
= min(lines_to_get
* 2, 100)
178 ok
= "SYNC" in self
.tag_names(next
+ "-1c")
179 line
= self
.get(mark
, next
)
180 ##print head, "get", mark, next, "->", `line`
183 for tag
in self
.tagdefs
.keys():
184 self
.tag_remove(tag
, mark
, next
)
186 m
= self
.prog
.search(chars
)
188 for key
, value
in m
.groupdict().items():
194 if value
in ("def", "class"):
195 m1
= self
.idprog
.match(chars
, b
)
198 self
.tag_add("DEFINITION",
201 m
= self
.prog
.search(chars
, m
.end())
202 if "SYNC" in self
.tag_names(next
+ "-1c"):
208 # We're in an inconsistent state, and the call to
209 # update may tell us to stop. It may also change
210 # the correct value for "next" (since this is a
211 # line.col string, not a true mark). So leave a
212 # crumb telling the next invocation to resume here
213 # in case update tells us to leave.
214 self
.tag_add("TODO", next
)
216 if self
.stop_colorizing
:
217 if __debug__
: print "colorizing stopped"
222 from Percolator
import Percolator
224 root
.wm_protocol("WM_DELETE_WINDOW", root
.quit
)
225 text
= Text(background
="white")
226 text
.pack(expand
=1, fill
="both")
233 if __name__
== "__main__":