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
)
31 asprog
= re
.compile(r
".*?\b(as)\b", re
.S
)
33 class ColorDelegator(Delegator
):
36 Delegator
.__init
__(self
)
41 def setdelegate(self
, delegate
):
42 if self
.delegate
is not None:
43 self
.unbind("<<toggle-auto-coloring>>")
44 Delegator
.setdelegate(self
, delegate
)
45 if delegate
is not None:
47 self
.bind("<<toggle-auto-coloring>>", self
.toggle_colorize_event
)
48 self
.notify_range("1.0", "end")
50 def config_colors(self
):
51 for tag
, cnf
in self
.tagdefs
.items():
53 apply(self
.tag_configure
, (tag
,), cnf
)
56 cconf
= idleconf
.getsection('Colors')
59 "COMMENT": cconf
.getcolor("comment"),
60 "KEYWORD": cconf
.getcolor("keyword"),
61 "STRING": cconf
.getcolor("string"),
62 "DEFINITION": cconf
.getcolor("definition"),
63 "SYNC": cconf
.getcolor("sync"),
64 "TODO": cconf
.getcolor("todo"),
65 "BREAK": cconf
.getcolor("break"),
66 # The following is used by ReplaceDialog:
67 "hit": cconf
.getcolor("hit"),
70 def insert(self
, index
, chars
, tags
=None):
71 index
= self
.index(index
)
72 self
.delegate
.insert(index
, chars
, tags
)
73 self
.notify_range(index
, index
+ "+%dc" % len(chars
))
75 def delete(self
, index1
, index2
=None):
76 index1
= self
.index(index1
)
77 self
.delegate
.delete(index1
, index2
)
78 self
.notify_range(index1
)
84 def notify_range(self
, index1
, index2
=None):
85 self
.tag_add("TODO", index1
, index2
)
87 if DEBUG
: print "colorizing already scheduled"
90 self
.stop_colorizing
= 1
91 if DEBUG
: print "stop colorizing"
92 if self
.allow_colorizing
:
93 if DEBUG
: print "schedule colorizing"
94 self
.after_id
= self
.after(1, self
.recolorize
)
96 close_when_done
= None # Window to be closed when done colorizing
98 def close(self
, close_when_done
=None):
100 after_id
= self
.after_id
102 if DEBUG
: print "cancel scheduled recolorizer"
103 self
.after_cancel(after_id
)
104 self
.allow_colorizing
= 0
105 self
.stop_colorizing
= 1
107 if not self
.colorizing
:
108 close_when_done
.destroy()
110 self
.close_when_done
= close_when_done
112 def toggle_colorize_event(self
, event
):
114 after_id
= self
.after_id
116 if DEBUG
: print "cancel scheduled recolorizer"
117 self
.after_cancel(after_id
)
118 if self
.allow_colorizing
and self
.colorizing
:
119 if DEBUG
: print "stop colorizing"
120 self
.stop_colorizing
= 1
121 self
.allow_colorizing
= not self
.allow_colorizing
122 if self
.allow_colorizing
and not self
.colorizing
:
123 self
.after_id
= self
.after(1, self
.recolorize
)
125 print "auto colorizing turned", self
.allow_colorizing
and "on" or "off"
128 def recolorize(self
):
130 if not self
.delegate
:
131 if DEBUG
: print "no delegate"
133 if not self
.allow_colorizing
:
134 if DEBUG
: print "auto colorizing is off"
137 if DEBUG
: print "already colorizing"
140 self
.stop_colorizing
= 0
142 if DEBUG
: print "colorizing..."
144 self
.recolorize_main()
146 if DEBUG
: print "%.3f seconds" % (t1
-t0
)
149 if self
.allow_colorizing
and self
.tag_nextrange("TODO", "1.0"):
150 if DEBUG
: print "reschedule colorizing"
151 self
.after_id
= self
.after(1, self
.recolorize
)
152 if self
.close_when_done
:
153 top
= self
.close_when_done
154 self
.close_when_done
= None
157 def recolorize_main(self
):
160 item
= self
.tag_nextrange("TODO", next
)
164 self
.tag_remove("SYNC", head
, tail
)
165 item
= self
.tag_prevrange("SYNC", head
)
177 next
= self
.index(mark
+ "+%d lines linestart" %
179 lines_to_get
= min(lines_to_get
* 2, 100)
180 ok
= "SYNC" in self
.tag_names(next
+ "-1c")
181 line
= self
.get(mark
, next
)
182 ##print head, "get", mark, next, "->", `line`
185 for tag
in self
.tagdefs
.keys():
186 self
.tag_remove(tag
, mark
, next
)
188 m
= self
.prog
.search(chars
)
190 for key
, value
in m
.groupdict().items():
196 if value
in ("def", "class"):
197 m1
= self
.idprog
.match(chars
, b
)
200 self
.tag_add("DEFINITION",
203 elif value
== "import":
204 # color all the "as" words on same line;
205 # cheap approximation to the truth
207 m1
= self
.asprog
.match(chars
, b
)
211 self
.tag_add("KEYWORD",
214 m
= self
.prog
.search(chars
, m
.end())
215 if "SYNC" in self
.tag_names(next
+ "-1c"):
221 # We're in an inconsistent state, and the call to
222 # update may tell us to stop. It may also change
223 # the correct value for "next" (since this is a
224 # line.col string, not a true mark). So leave a
225 # crumb telling the next invocation to resume here
226 # in case update tells us to leave.
227 self
.tag_add("TODO", next
)
229 if self
.stop_colorizing
:
230 if DEBUG
: print "colorizing stopped"
235 from Percolator
import Percolator
237 root
.wm_protocol("WM_DELETE_WINDOW", root
.quit
)
238 text
= Text(background
="white")
239 text
.pack(expand
=1, fill
="both")
246 if __name__
== "__main__":