_make_boundary(): Fix for SF bug #745478, broken boundary calculation
[python/dscho.git] / Tools / idle / ColorDelegator.py
blob059108f65c91743cbc3eff4f02aee24d60705413
1 import time
2 import re
3 import keyword
4 from Tkinter import *
5 from Delegator import Delegator
6 from IdleConf import idleconf
8 #$ event <<toggle-auto-coloring>>
9 #$ win <Control-slash>
10 #$ unix <Control-slash>
12 DEBUG = 0
15 def any(name, list):
16 return "(?P<%s>" % name + "|".join(list) + ")"
18 def make_pat():
19 kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b"
20 comment = any("COMMENT", [r"#[^\n]*"])
21 sqstring = r"(\b[rR])?'[^'\\\n]*(\\.[^'\\\n]*)*'?"
22 dqstring = r'(\b[rR])?"[^"\\\n]*(\\.[^"\\\n]*)*"?'
23 sq3string = r"(\b[rR])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
24 dq3string = r'(\b[rR])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
25 string = any("STRING", [sq3string, dq3string, sqstring, dqstring])
26 return kw + "|" + comment + "|" + string + "|" + any("SYNC", [r"\n"])
28 prog = re.compile(make_pat(), re.S)
29 idprog = re.compile(r"\s+(\w+)", re.S)
30 asprog = re.compile(r".*?\b(as)\b", re.S)
32 class ColorDelegator(Delegator):
34 def __init__(self):
35 Delegator.__init__(self)
36 self.prog = prog
37 self.idprog = idprog
38 self.asprog = asprog
40 def setdelegate(self, delegate):
41 if self.delegate is not None:
42 self.unbind("<<toggle-auto-coloring>>")
43 Delegator.setdelegate(self, delegate)
44 if delegate is not None:
45 self.config_colors()
46 self.bind("<<toggle-auto-coloring>>", self.toggle_colorize_event)
47 self.notify_range("1.0", "end")
49 def config_colors(self):
50 for tag, cnf in self.tagdefs.items():
51 if cnf:
52 apply(self.tag_configure, (tag,), cnf)
53 self.tag_raise('sel')
55 cconf = idleconf.getsection('Colors')
57 tagdefs = {
58 "COMMENT": cconf.getcolor("comment"),
59 "KEYWORD": cconf.getcolor("keyword"),
60 "STRING": cconf.getcolor("string"),
61 "DEFINITION": cconf.getcolor("definition"),
62 "SYNC": cconf.getcolor("sync"),
63 "TODO": cconf.getcolor("todo"),
64 "BREAK": cconf.getcolor("break"),
65 # The following is used by ReplaceDialog:
66 "hit": cconf.getcolor("hit"),
69 def insert(self, index, chars, tags=None):
70 index = self.index(index)
71 self.delegate.insert(index, chars, tags)
72 self.notify_range(index, index + "+%dc" % len(chars))
74 def delete(self, index1, index2=None):
75 index1 = self.index(index1)
76 self.delegate.delete(index1, index2)
77 self.notify_range(index1)
79 after_id = None
80 allow_colorizing = 1
81 colorizing = 0
83 def notify_range(self, index1, index2=None):
84 self.tag_add("TODO", index1, index2)
85 if self.after_id:
86 if DEBUG: print "colorizing already scheduled"
87 return
88 if self.colorizing:
89 self.stop_colorizing = 1
90 if DEBUG: print "stop colorizing"
91 if self.allow_colorizing:
92 if DEBUG: print "schedule colorizing"
93 self.after_id = self.after(1, self.recolorize)
95 close_when_done = None # Window to be closed when done colorizing
97 def close(self, close_when_done=None):
98 if self.after_id:
99 after_id = self.after_id
100 self.after_id = None
101 if DEBUG: print "cancel scheduled recolorizer"
102 self.after_cancel(after_id)
103 self.allow_colorizing = 0
104 self.stop_colorizing = 1
105 if close_when_done:
106 if not self.colorizing:
107 close_when_done.destroy()
108 else:
109 self.close_when_done = close_when_done
111 def toggle_colorize_event(self, event):
112 if self.after_id:
113 after_id = self.after_id
114 self.after_id = None
115 if DEBUG: print "cancel scheduled recolorizer"
116 self.after_cancel(after_id)
117 if self.allow_colorizing and self.colorizing:
118 if DEBUG: print "stop colorizing"
119 self.stop_colorizing = 1
120 self.allow_colorizing = not self.allow_colorizing
121 if self.allow_colorizing and not self.colorizing:
122 self.after_id = self.after(1, self.recolorize)
123 if DEBUG:
124 print "auto colorizing turned", self.allow_colorizing and "on" or "off"
125 return "break"
127 def recolorize(self):
128 self.after_id = None
129 if not self.delegate:
130 if DEBUG: print "no delegate"
131 return
132 if not self.allow_colorizing:
133 if DEBUG: print "auto colorizing is off"
134 return
135 if self.colorizing:
136 if DEBUG: print "already colorizing"
137 return
138 try:
139 self.stop_colorizing = 0
140 self.colorizing = 1
141 if DEBUG: print "colorizing..."
142 t0 = time.clock()
143 self.recolorize_main()
144 t1 = time.clock()
145 if DEBUG: print "%.3f seconds" % (t1-t0)
146 finally:
147 self.colorizing = 0
148 if self.allow_colorizing and self.tag_nextrange("TODO", "1.0"):
149 if DEBUG: print "reschedule colorizing"
150 self.after_id = self.after(1, self.recolorize)
151 if self.close_when_done:
152 top = self.close_when_done
153 self.close_when_done = None
154 top.destroy()
156 def recolorize_main(self):
157 next = "1.0"
158 while 1:
159 item = self.tag_nextrange("TODO", next)
160 if not item:
161 break
162 head, tail = item
163 self.tag_remove("SYNC", head, tail)
164 item = self.tag_prevrange("SYNC", head)
165 if item:
166 head = item[1]
167 else:
168 head = "1.0"
170 chars = ""
171 next = head
172 lines_to_get = 1
173 ok = 0
174 while not ok:
175 mark = next
176 next = self.index(mark + "+%d lines linestart" %
177 lines_to_get)
178 lines_to_get = min(lines_to_get * 2, 100)
179 ok = "SYNC" in self.tag_names(next + "-1c")
180 line = self.get(mark, next)
181 ##print head, "get", mark, next, "->", `line`
182 if not line:
183 return
184 for tag in self.tagdefs.keys():
185 self.tag_remove(tag, mark, next)
186 chars = chars + line
187 m = self.prog.search(chars)
188 while m:
189 for key, value in m.groupdict().items():
190 if value:
191 a, b = m.span(key)
192 self.tag_add(key,
193 head + "+%dc" % a,
194 head + "+%dc" % b)
195 if value in ("def", "class"):
196 m1 = self.idprog.match(chars, b)
197 if m1:
198 a, b = m1.span(1)
199 self.tag_add("DEFINITION",
200 head + "+%dc" % a,
201 head + "+%dc" % b)
202 elif value == "import":
203 # color all the "as" words on same line;
204 # cheap approximation to the truth
205 while 1:
206 m1 = self.asprog.match(chars, b)
207 if not m1:
208 break
209 a, b = m1.span(1)
210 self.tag_add("KEYWORD",
211 head + "+%dc" % a,
212 head + "+%dc" % b)
213 m = self.prog.search(chars, m.end())
214 if "SYNC" in self.tag_names(next + "-1c"):
215 head = next
216 chars = ""
217 else:
218 ok = 0
219 if not ok:
220 # We're in an inconsistent state, and the call to
221 # update may tell us to stop. It may also change
222 # the correct value for "next" (since this is a
223 # line.col string, not a true mark). So leave a
224 # crumb telling the next invocation to resume here
225 # in case update tells us to leave.
226 self.tag_add("TODO", next)
227 self.update()
228 if self.stop_colorizing:
229 if DEBUG: print "colorizing stopped"
230 return
233 def main():
234 from Percolator import Percolator
235 root = Tk()
236 root.wm_protocol("WM_DELETE_WINDOW", root.quit)
237 text = Text(background="white")
238 text.pack(expand=1, fill="both")
239 text.focus_set()
240 p = Percolator(text)
241 d = ColorDelegator()
242 p.insertfilter(d)
243 root.mainloop()
245 if __name__ == "__main__":
246 main()