blob: e188192994133dfc9b7678e22e327ebeca439f84 [file] [log] [blame]
David Scherer7aced172000-08-15 01:13:23 +00001import time
David Scherer7aced172000-08-15 01:13:23 +00002import re
3import keyword
Georg Brandl1a3284e2007-12-02 09:40:06 +00004import builtins
Georg Brandl14fc4272008-05-17 18:39:55 +00005from tkinter import *
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +00006from idlelib.Delegator import Delegator
7from idlelib.configHandler import idleConf
David Scherer7aced172000-08-15 01:13:23 +00008
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +00009DEBUG = False
David Scherer7aced172000-08-15 01:13:23 +000010
Thomas Wouters0e3f5912006-08-11 14:57:12 +000011def any(name, alternates):
12 "Return a named group pattern matching list of alternates."
13 return "(?P<%s>" % name + "|".join(alternates) + ")"
David Scherer7aced172000-08-15 01:13:23 +000014
15def make_pat():
16 kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b"
Georg Brandl1a3284e2007-12-02 09:40:06 +000017 builtinlist = [str(name) for name in dir(builtins)
Terry Jan Reedydc224f82012-01-16 03:20:27 -050018 if not name.startswith('_') and \
19 name not in keyword.kwlist]
Alex Martelli01c77c62006-08-24 02:58:11 +000020 # self.file = open("file") :
Thomas Wouters0e3f5912006-08-11 14:57:12 +000021 # 1st 'file' colorized normal, 2nd as builtin, 3rd as string
22 builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b"
David Scherer7aced172000-08-15 01:13:23 +000023 comment = any("COMMENT", [r"#[^\n]*"])
Ned Deily2f0ad742011-12-07 01:08:35 -080024 sqstring = r"(\b[rRbB])?'[^'\\\n]*(\\.[^'\\\n]*)*'?"
25 dqstring = r'(\b[rRbB])?"[^"\\\n]*(\\.[^"\\\n]*)*"?'
26 sq3string = r"(\b[rRbB])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
27 dq3string = r'(\b[rRbB])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
David Scherer7aced172000-08-15 01:13:23 +000028 string = any("STRING", [sq3string, dq3string, sqstring, dqstring])
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +000029 return kw + "|" + builtin + "|" + comment + "|" + string +\
30 "|" + any("SYNC", [r"\n"])
David Scherer7aced172000-08-15 01:13:23 +000031
32prog = re.compile(make_pat(), re.S)
33idprog = re.compile(r"\s+(\w+)", re.S)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000034asprog = re.compile(r".*?\b(as)\b")
David Scherer7aced172000-08-15 01:13:23 +000035
36class ColorDelegator(Delegator):
37
38 def __init__(self):
39 Delegator.__init__(self)
40 self.prog = prog
41 self.idprog = idprog
Kurt B. Kaiser6df4bf22001-07-13 00:04:24 +000042 self.asprog = asprog
Steven M. Gavab77d3432002-03-02 07:16:21 +000043 self.LoadTagDefs()
David Scherer7aced172000-08-15 01:13:23 +000044
45 def setdelegate(self, delegate):
46 if self.delegate is not None:
47 self.unbind("<<toggle-auto-coloring>>")
48 Delegator.setdelegate(self, delegate)
49 if delegate is not None:
50 self.config_colors()
51 self.bind("<<toggle-auto-coloring>>", self.toggle_colorize_event)
52 self.notify_range("1.0", "end")
53
54 def config_colors(self):
55 for tag, cnf in self.tagdefs.items():
56 if cnf:
Raymond Hettinger931237e2003-07-09 18:48:24 +000057 self.tag_configure(tag, **cnf)
David Scherer7aced172000-08-15 01:13:23 +000058 self.tag_raise('sel')
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000059
Steven M. Gavab77d3432002-03-02 07:16:21 +000060 def LoadTagDefs(self):
61 theme = idleConf.GetOption('main','Theme','name')
62 self.tagdefs = {
63 "COMMENT": idleConf.GetHighlight(theme, "comment"),
64 "KEYWORD": idleConf.GetHighlight(theme, "keyword"),
Kurt B. Kaiser73360a32004-03-08 18:15:31 +000065 "BUILTIN": idleConf.GetHighlight(theme, "builtin"),
Steven M. Gavab77d3432002-03-02 07:16:21 +000066 "STRING": idleConf.GetHighlight(theme, "string"),
67 "DEFINITION": idleConf.GetHighlight(theme, "definition"),
68 "SYNC": {'background':None,'foreground':None},
69 "TODO": {'background':None,'foreground':None},
70 "BREAK": idleConf.GetHighlight(theme, "break"),
Kurt B. Kaiser92b5ca32002-12-17 21:16:12 +000071 "ERROR": idleConf.GetHighlight(theme, "error"),
Steven M. Gavab77d3432002-03-02 07:16:21 +000072 # The following is used by ReplaceDialog:
73 "hit": idleConf.GetHighlight(theme, "hit"),
74 }
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000075
Guido van Rossumbe19ed72007-02-09 05:37:30 +000076 if DEBUG: print('tagdefs',self.tagdefs)
David Scherer7aced172000-08-15 01:13:23 +000077
78 def insert(self, index, chars, tags=None):
79 index = self.index(index)
80 self.delegate.insert(index, chars, tags)
81 self.notify_range(index, index + "+%dc" % len(chars))
82
83 def delete(self, index1, index2=None):
84 index1 = self.index(index1)
85 self.delegate.delete(index1, index2)
86 self.notify_range(index1)
87
88 after_id = None
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +000089 allow_colorizing = True
90 colorizing = False
David Scherer7aced172000-08-15 01:13:23 +000091
92 def notify_range(self, index1, index2=None):
93 self.tag_add("TODO", index1, index2)
94 if self.after_id:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000095 if DEBUG: print("colorizing already scheduled")
David Scherer7aced172000-08-15 01:13:23 +000096 return
97 if self.colorizing:
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +000098 self.stop_colorizing = True
Guido van Rossumbe19ed72007-02-09 05:37:30 +000099 if DEBUG: print("stop colorizing")
David Scherer7aced172000-08-15 01:13:23 +0000100 if self.allow_colorizing:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000101 if DEBUG: print("schedule colorizing")
David Scherer7aced172000-08-15 01:13:23 +0000102 self.after_id = self.after(1, self.recolorize)
103
104 close_when_done = None # Window to be closed when done colorizing
105
106 def close(self, close_when_done=None):
107 if self.after_id:
108 after_id = self.after_id
109 self.after_id = None
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000110 if DEBUG: print("cancel scheduled recolorizer")
David Scherer7aced172000-08-15 01:13:23 +0000111 self.after_cancel(after_id)
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +0000112 self.allow_colorizing = False
113 self.stop_colorizing = True
David Scherer7aced172000-08-15 01:13:23 +0000114 if close_when_done:
115 if not self.colorizing:
116 close_when_done.destroy()
117 else:
118 self.close_when_done = close_when_done
119
120 def toggle_colorize_event(self, event):
121 if self.after_id:
122 after_id = self.after_id
123 self.after_id = None
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000124 if DEBUG: print("cancel scheduled recolorizer")
David Scherer7aced172000-08-15 01:13:23 +0000125 self.after_cancel(after_id)
126 if self.allow_colorizing and self.colorizing:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000127 if DEBUG: print("stop colorizing")
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +0000128 self.stop_colorizing = True
David Scherer7aced172000-08-15 01:13:23 +0000129 self.allow_colorizing = not self.allow_colorizing
130 if self.allow_colorizing and not self.colorizing:
131 self.after_id = self.after(1, self.recolorize)
Kurt B. Kaiser6df4bf22001-07-13 00:04:24 +0000132 if DEBUG:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000133 print("auto colorizing turned",\
134 self.allow_colorizing and "on" or "off")
David Scherer7aced172000-08-15 01:13:23 +0000135 return "break"
136
137 def recolorize(self):
138 self.after_id = None
139 if not self.delegate:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000140 if DEBUG: print("no delegate")
David Scherer7aced172000-08-15 01:13:23 +0000141 return
142 if not self.allow_colorizing:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000143 if DEBUG: print("auto colorizing is off")
David Scherer7aced172000-08-15 01:13:23 +0000144 return
145 if self.colorizing:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000146 if DEBUG: print("already colorizing")
David Scherer7aced172000-08-15 01:13:23 +0000147 return
148 try:
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +0000149 self.stop_colorizing = False
150 self.colorizing = True
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000151 if DEBUG: print("colorizing...")
David Scherer7aced172000-08-15 01:13:23 +0000152 t0 = time.clock()
153 self.recolorize_main()
154 t1 = time.clock()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000155 if DEBUG: print("%.3f seconds" % (t1-t0))
David Scherer7aced172000-08-15 01:13:23 +0000156 finally:
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +0000157 self.colorizing = False
David Scherer7aced172000-08-15 01:13:23 +0000158 if self.allow_colorizing and self.tag_nextrange("TODO", "1.0"):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000159 if DEBUG: print("reschedule colorizing")
David Scherer7aced172000-08-15 01:13:23 +0000160 self.after_id = self.after(1, self.recolorize)
161 if self.close_when_done:
162 top = self.close_when_done
163 self.close_when_done = None
164 top.destroy()
165
166 def recolorize_main(self):
167 next = "1.0"
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +0000168 while True:
David Scherer7aced172000-08-15 01:13:23 +0000169 item = self.tag_nextrange("TODO", next)
170 if not item:
171 break
172 head, tail = item
173 self.tag_remove("SYNC", head, tail)
174 item = self.tag_prevrange("SYNC", head)
175 if item:
176 head = item[1]
177 else:
178 head = "1.0"
179
180 chars = ""
181 next = head
182 lines_to_get = 1
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +0000183 ok = False
David Scherer7aced172000-08-15 01:13:23 +0000184 while not ok:
185 mark = next
186 next = self.index(mark + "+%d lines linestart" %
187 lines_to_get)
188 lines_to_get = min(lines_to_get * 2, 100)
189 ok = "SYNC" in self.tag_names(next + "-1c")
190 line = self.get(mark, next)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000191 ##print head, "get", mark, next, "->", repr(line)
David Scherer7aced172000-08-15 01:13:23 +0000192 if not line:
193 return
Kurt B. Kaisere0712772007-08-23 05:25:55 +0000194 for tag in self.tagdefs:
David Scherer7aced172000-08-15 01:13:23 +0000195 self.tag_remove(tag, mark, next)
196 chars = chars + line
197 m = self.prog.search(chars)
198 while m:
199 for key, value in m.groupdict().items():
200 if value:
201 a, b = m.span(key)
202 self.tag_add(key,
203 head + "+%dc" % a,
204 head + "+%dc" % b)
205 if value in ("def", "class"):
206 m1 = self.idprog.match(chars, b)
207 if m1:
208 a, b = m1.span(1)
209 self.tag_add("DEFINITION",
210 head + "+%dc" % a,
211 head + "+%dc" % b)
Kurt B. Kaiser6df4bf22001-07-13 00:04:24 +0000212 elif value == "import":
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000213 # color all the "as" words on same line, except
214 # if in a comment; cheap approximation to the
215 # truth
216 if '#' in chars:
217 endpos = chars.index('#')
218 else:
219 endpos = len(chars)
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +0000220 while True:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221 m1 = self.asprog.match(chars, b, endpos)
Kurt B. Kaiser6df4bf22001-07-13 00:04:24 +0000222 if not m1:
223 break
224 a, b = m1.span(1)
225 self.tag_add("KEYWORD",
226 head + "+%dc" % a,
227 head + "+%dc" % b)
David Scherer7aced172000-08-15 01:13:23 +0000228 m = self.prog.search(chars, m.end())
229 if "SYNC" in self.tag_names(next + "-1c"):
230 head = next
231 chars = ""
232 else:
Kurt B. Kaiser0bc3d982004-03-15 04:26:37 +0000233 ok = False
David Scherer7aced172000-08-15 01:13:23 +0000234 if not ok:
235 # We're in an inconsistent state, and the call to
236 # update may tell us to stop. It may also change
237 # the correct value for "next" (since this is a
238 # line.col string, not a true mark). So leave a
239 # crumb telling the next invocation to resume here
240 # in case update tells us to leave.
241 self.tag_add("TODO", next)
242 self.update()
243 if self.stop_colorizing:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000244 if DEBUG: print("colorizing stopped")
David Scherer7aced172000-08-15 01:13:23 +0000245 return
246
Kurt B. Kaiserdf506ea2005-06-12 04:33:30 +0000247 def removecolors(self):
Kurt B. Kaisere0712772007-08-23 05:25:55 +0000248 for tag in self.tagdefs:
Kurt B. Kaiserdf506ea2005-06-12 04:33:30 +0000249 self.tag_remove(tag, "1.0", "end")
David Scherer7aced172000-08-15 01:13:23 +0000250
251def main():
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +0000252 from idlelib.Percolator import Percolator
David Scherer7aced172000-08-15 01:13:23 +0000253 root = Tk()
254 root.wm_protocol("WM_DELETE_WINDOW", root.quit)
255 text = Text(background="white")
256 text.pack(expand=1, fill="both")
257 text.focus_set()
258 p = Percolator(text)
259 d = ColorDelegator()
260 p.insertfilter(d)
261 root.mainloop()
262
263if __name__ == "__main__":
264 main()