David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | import time |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 2 | import re |
| 3 | import keyword |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 4 | import builtins |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 5 | from tkinter import * |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 6 | from idlelib.Delegator import Delegator |
| 7 | from idlelib.configHandler import idleConf |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 8 | |
Kurt B. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 9 | DEBUG = False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11 | def any(name, alternates): |
| 12 | "Return a named group pattern matching list of alternates." |
| 13 | return "(?P<%s>" % name + "|".join(alternates) + ")" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 14 | |
| 15 | def make_pat(): |
| 16 | kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b" |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 17 | builtinlist = [str(name) for name in dir(builtins) |
Terry Jan Reedy | dc224f8 | 2012-01-16 03:20:27 -0500 | [diff] [blame] | 18 | if not name.startswith('_') and \ |
| 19 | name not in keyword.kwlist] |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 20 | # self.file = open("file") : |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 21 | # 1st 'file' colorized normal, 2nd as builtin, 3rd as string |
| 22 | builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 23 | comment = any("COMMENT", [r"#[^\n]*"]) |
Ned Deily | 2f0ad74 | 2011-12-07 01:08:35 -0800 | [diff] [blame] | 24 | sqstring = r"(\b[rRbB])?'[^'\\\n]*(\\.[^'\\\n]*)*'?" |
| 25 | dqstring = r'(\b[rRbB])?"[^"\\\n]*(\\.[^"\\\n]*)*"?' |
| 26 | sq3string = r"(\b[rRbB])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?" |
| 27 | dq3string = r'(\b[rRbB])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 28 | string = any("STRING", [sq3string, dq3string, sqstring, dqstring]) |
Kurt B. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 29 | return kw + "|" + builtin + "|" + comment + "|" + string +\ |
| 30 | "|" + any("SYNC", [r"\n"]) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 31 | |
| 32 | prog = re.compile(make_pat(), re.S) |
| 33 | idprog = re.compile(r"\s+(\w+)", re.S) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 34 | asprog = re.compile(r".*?\b(as)\b") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 35 | |
| 36 | class ColorDelegator(Delegator): |
| 37 | |
| 38 | def __init__(self): |
| 39 | Delegator.__init__(self) |
| 40 | self.prog = prog |
| 41 | self.idprog = idprog |
Kurt B. Kaiser | 6df4bf2 | 2001-07-13 00:04:24 +0000 | [diff] [blame] | 42 | self.asprog = asprog |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 43 | self.LoadTagDefs() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 44 | |
| 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 Hettinger | 931237e | 2003-07-09 18:48:24 +0000 | [diff] [blame] | 57 | self.tag_configure(tag, **cnf) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 58 | self.tag_raise('sel') |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 59 | |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 60 | 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. Kaiser | 73360a3 | 2004-03-08 18:15:31 +0000 | [diff] [blame] | 65 | "BUILTIN": idleConf.GetHighlight(theme, "builtin"), |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 66 | "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. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 71 | "ERROR": idleConf.GetHighlight(theme, "error"), |
Steven M. Gava | b77d343 | 2002-03-02 07:16:21 +0000 | [diff] [blame] | 72 | # The following is used by ReplaceDialog: |
| 73 | "hit": idleConf.GetHighlight(theme, "hit"), |
| 74 | } |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 76 | if DEBUG: print('tagdefs',self.tagdefs) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 77 | |
| 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. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 89 | allow_colorizing = True |
| 90 | colorizing = False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 91 | |
| 92 | def notify_range(self, index1, index2=None): |
| 93 | self.tag_add("TODO", index1, index2) |
| 94 | if self.after_id: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 95 | if DEBUG: print("colorizing already scheduled") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 96 | return |
| 97 | if self.colorizing: |
Kurt B. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 98 | self.stop_colorizing = True |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 99 | if DEBUG: print("stop colorizing") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 100 | if self.allow_colorizing: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 101 | if DEBUG: print("schedule colorizing") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 102 | 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 Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 110 | if DEBUG: print("cancel scheduled recolorizer") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 111 | self.after_cancel(after_id) |
Kurt B. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 112 | self.allow_colorizing = False |
| 113 | self.stop_colorizing = True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 114 | 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 Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 124 | if DEBUG: print("cancel scheduled recolorizer") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 125 | self.after_cancel(after_id) |
| 126 | if self.allow_colorizing and self.colorizing: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 127 | if DEBUG: print("stop colorizing") |
Kurt B. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 128 | self.stop_colorizing = True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 129 | 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. Kaiser | 6df4bf2 | 2001-07-13 00:04:24 +0000 | [diff] [blame] | 132 | if DEBUG: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 133 | print("auto colorizing turned",\ |
| 134 | self.allow_colorizing and "on" or "off") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 135 | return "break" |
| 136 | |
| 137 | def recolorize(self): |
| 138 | self.after_id = None |
| 139 | if not self.delegate: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 140 | if DEBUG: print("no delegate") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 141 | return |
| 142 | if not self.allow_colorizing: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 143 | if DEBUG: print("auto colorizing is off") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 144 | return |
| 145 | if self.colorizing: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 146 | if DEBUG: print("already colorizing") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 147 | return |
| 148 | try: |
Kurt B. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 149 | self.stop_colorizing = False |
| 150 | self.colorizing = True |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 151 | if DEBUG: print("colorizing...") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 152 | t0 = time.clock() |
| 153 | self.recolorize_main() |
| 154 | t1 = time.clock() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 155 | if DEBUG: print("%.3f seconds" % (t1-t0)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 156 | finally: |
Kurt B. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 157 | self.colorizing = False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 158 | if self.allow_colorizing and self.tag_nextrange("TODO", "1.0"): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 159 | if DEBUG: print("reschedule colorizing") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 160 | 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. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 168 | while True: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 169 | 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. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 183 | ok = False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 184 | 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örwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 191 | ##print head, "get", mark, next, "->", repr(line) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 192 | if not line: |
| 193 | return |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 194 | for tag in self.tagdefs: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 195 | 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. Kaiser | 6df4bf2 | 2001-07-13 00:04:24 +0000 | [diff] [blame] | 212 | elif value == "import": |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 213 | # 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. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 220 | while True: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 221 | m1 = self.asprog.match(chars, b, endpos) |
Kurt B. Kaiser | 6df4bf2 | 2001-07-13 00:04:24 +0000 | [diff] [blame] | 222 | 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 Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 228 | 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. Kaiser | 0bc3d98 | 2004-03-15 04:26:37 +0000 | [diff] [blame] | 233 | ok = False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 234 | 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 Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 244 | if DEBUG: print("colorizing stopped") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 245 | return |
| 246 | |
Kurt B. Kaiser | df506ea | 2005-06-12 04:33:30 +0000 | [diff] [blame] | 247 | def removecolors(self): |
Kurt B. Kaiser | e071277 | 2007-08-23 05:25:55 +0000 | [diff] [blame] | 248 | for tag in self.tagdefs: |
Kurt B. Kaiser | df506ea | 2005-06-12 04:33:30 +0000 | [diff] [blame] | 249 | self.tag_remove(tag, "1.0", "end") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 250 | |
| 251 | def main(): |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 252 | from idlelib.Percolator import Percolator |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 253 | 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 | |
| 263 | if __name__ == "__main__": |
| 264 | main() |