6 from Delegator
import Delegator
7 from configHandler
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
)
42 def setdelegate(self
, delegate
):
43 if self
.delegate
is not None:
44 self
.unbind("<<toggle-auto-coloring>>")
45 Delegator
.setdelegate(self
, delegate
)
46 if delegate
is not None:
48 self
.bind("<<toggle-auto-coloring>>", self
.toggle_colorize_event
)
49 self
.notify_range("1.0", "end")
51 def config_colors(self
):
52 for tag
, cnf
in self
.tagdefs
.items():
54 apply(self
.tag_configure
, (tag
,), cnf
)
57 def LoadTagDefs(self
):
58 theme
= idleConf
.GetOption('main','Theme','name')
60 "COMMENT": idleConf
.GetHighlight(theme
, "comment"),
61 "KEYWORD": idleConf
.GetHighlight(theme
, "keyword"),
62 "STRING": idleConf
.GetHighlight(theme
, "string"),
63 "DEFINITION": idleConf
.GetHighlight(theme
, "definition"),
64 "SYNC": {'background':None,'foreground':None},
65 "TODO": {'background':None,'foreground':None},
66 "BREAK": idleConf
.GetHighlight(theme
, "break"),
67 # The following is used by ReplaceDialog:
68 "hit": idleConf
.GetHighlight(theme
, "hit"),
71 if DEBUG
: print 'tagdefs',tagdefs
73 def insert(self
, index
, chars
, tags
=None):
74 index
= self
.index(index
)
75 self
.delegate
.insert(index
, chars
, tags
)
76 self
.notify_range(index
, index
+ "+%dc" % len(chars
))
78 def delete(self
, index1
, index2
=None):
79 index1
= self
.index(index1
)
80 self
.delegate
.delete(index1
, index2
)
81 self
.notify_range(index1
)
87 def notify_range(self
, index1
, index2
=None):
88 self
.tag_add("TODO", index1
, index2
)
90 if DEBUG
: print "colorizing already scheduled"
93 self
.stop_colorizing
= 1
94 if DEBUG
: print "stop colorizing"
95 if self
.allow_colorizing
:
96 if DEBUG
: print "schedule colorizing"
97 self
.after_id
= self
.after(1, self
.recolorize
)
99 close_when_done
= None # Window to be closed when done colorizing
101 def close(self
, close_when_done
=None):
103 after_id
= self
.after_id
105 if DEBUG
: print "cancel scheduled recolorizer"
106 self
.after_cancel(after_id
)
107 self
.allow_colorizing
= 0
108 self
.stop_colorizing
= 1
110 if not self
.colorizing
:
111 close_when_done
.destroy()
113 self
.close_when_done
= close_when_done
115 def toggle_colorize_event(self
, event
):
117 after_id
= self
.after_id
119 if DEBUG
: print "cancel scheduled recolorizer"
120 self
.after_cancel(after_id
)
121 if self
.allow_colorizing
and self
.colorizing
:
122 if DEBUG
: print "stop colorizing"
123 self
.stop_colorizing
= 1
124 self
.allow_colorizing
= not self
.allow_colorizing
125 if self
.allow_colorizing
and not self
.colorizing
:
126 self
.after_id
= self
.after(1, self
.recolorize
)
128 print "auto colorizing turned", self
.allow_colorizing
and "on" or "off"
131 def recolorize(self
):
133 if not self
.delegate
:
134 if DEBUG
: print "no delegate"
136 if not self
.allow_colorizing
:
137 if DEBUG
: print "auto colorizing is off"
140 if DEBUG
: print "already colorizing"
143 self
.stop_colorizing
= 0
145 if DEBUG
: print "colorizing..."
147 self
.recolorize_main()
149 if DEBUG
: print "%.3f seconds" % (t1
-t0
)
152 if self
.allow_colorizing
and self
.tag_nextrange("TODO", "1.0"):
153 if DEBUG
: print "reschedule colorizing"
154 self
.after_id
= self
.after(1, self
.recolorize
)
155 if self
.close_when_done
:
156 top
= self
.close_when_done
157 self
.close_when_done
= None
160 def recolorize_main(self
):
163 item
= self
.tag_nextrange("TODO", next
)
167 self
.tag_remove("SYNC", head
, tail
)
168 item
= self
.tag_prevrange("SYNC", head
)
180 next
= self
.index(mark
+ "+%d lines linestart" %
182 lines_to_get
= min(lines_to_get
* 2, 100)
183 ok
= "SYNC" in self
.tag_names(next
+ "-1c")
184 line
= self
.get(mark
, next
)
185 ##print head, "get", mark, next, "->", `line`
188 for tag
in self
.tagdefs
.keys():
189 self
.tag_remove(tag
, mark
, next
)
191 m
= self
.prog
.search(chars
)
193 for key
, value
in m
.groupdict().items():
199 if value
in ("def", "class"):
200 m1
= self
.idprog
.match(chars
, b
)
203 self
.tag_add("DEFINITION",
206 elif value
== "import":
207 # color all the "as" words on same line;
208 # cheap approximation to the truth
210 m1
= self
.asprog
.match(chars
, b
)
214 self
.tag_add("KEYWORD",
217 m
= self
.prog
.search(chars
, m
.end())
218 if "SYNC" in self
.tag_names(next
+ "-1c"):
224 # We're in an inconsistent state, and the call to
225 # update may tell us to stop. It may also change
226 # the correct value for "next" (since this is a
227 # line.col string, not a true mark). So leave a
228 # crumb telling the next invocation to resume here
229 # in case update tells us to leave.
230 self
.tag_add("TODO", next
)
232 if self
.stop_colorizing
:
233 if DEBUG
: print "colorizing stopped"
238 from Percolator
import Percolator
240 root
.wm_protocol("WM_DELETE_WINDOW", root
.quit
)
241 text
= Text(background
="white")
242 text
.pack(expand
=1, fill
="both")
249 if __name__
== "__main__":