1313import Tkinter
1414from configHandler import idleConf
1515from PyShell import PyShell
16- from string import whitespace
1716import re
1817
1918BLOCKOPENERS = dict ([(x , None ) for x in ("class" , "def" , "elif" , "else" ,
2019 "except" , "finally" , "for" , "if" ,
2120 "try" , "while" )])
2221INFINITY = 1 << 30
23- UPDATEINTERVAL = 100 #ms
24- FONTUPDATEINTERVAL = 1000 #ms
22+ UPDATEINTERVAL = 100 # millisec
23+ FONTUPDATEINTERVAL = 1000 # millisec
2524
2625getspacesfirstword = lambda s , c = re .compile (r"^(\s*)(\w*)" ): c .match (s ).groups ()
2726
2827class CodeContext :
29- menudefs = []
28+ menudefs = [('options' , [('!Code Conte_xt' , '<<toggle-code-context>>' )])]
29+
3030 numlines = idleConf .GetOption ("extensions" , "CodeContext" ,
3131 "numlines" , type = "int" , default = 3 )
3232 bgcolor = idleConf .GetOption ("extensions" , "CodeContext" ,
3333 "bgcolor" , type = "str" , default = "LightGray" )
3434 fgcolor = idleConf .GetOption ("extensions" , "CodeContext" ,
3535 "fgcolor" , type = "str" , default = "Black" )
36+ default_on = idleConf .GetOption ("extensions" , "CodeContext" ,
37+ "default_on" , type = "int" , default = 0 )
3638 def __init__ (self , editwin ):
3739 if isinstance (editwin , PyShell ):
3840 return
3941 self .editwin = editwin
4042 self .text = editwin .text
4143 self .textfont = self .text ["font" ]
42- self .label = Tkinter .Label (self .editwin .top ,
43- text = "\n " * (self .numlines - 1 ),
44- anchor = "w" , justify = "left" ,
45- font = self .textfont ,
46- bg = self .bgcolor , fg = self .fgcolor ,
47- relief = "sunken" ,
48- width = 1 , # Don't request more than we get
49- )
50- self .label .pack (side = "top" , fill = "x" , expand = 0 ,
51- after = self .editwin .status_bar )
44+ self .label = None
5245 # Dummy line, which starts the "block" of the whole document:
5346 self .info = list (self .interesting_lines (1 ))
5447 self .lastfirstline = 1
48+ if self .default_on :
49+ self .toggle_code_context_event ()
50+ self .editwin .setvar ('<<toggle-code-context>>' , True )
5551 # Start two update cycles, one for context lines, one for font changes.
5652 self .text .after (UPDATEINTERVAL , self .timer_event )
5753 self .text .after (FONTUPDATEINTERVAL , self .font_timer_event )
5854
55+ def toggle_code_context_event (self , event = None ):
56+ if not self .label :
57+ self .label = Tkinter .Label (self .editwin .top ,
58+ text = "\n " * (self .numlines - 1 ),
59+ anchor = "w" , justify = "left" ,
60+ font = self .textfont ,
61+ bg = self .bgcolor , fg = self .fgcolor ,
62+ relief = "sunken" ,
63+ width = 1 , # Don't request more than we get
64+ )
65+ self .label .pack (side = "top" , fill = "x" , expand = 0 ,
66+ after = self .editwin .status_bar )
67+ else :
68+ self .label .destroy ()
69+ self .label = None
70+
5971 def get_line_info (self , linenum ):
6072 """Get the line indent value, text, and any block start keyword
6173
@@ -107,7 +119,6 @@ def update_label(self):
107119 del self .info [- 1 ]
108120 if self .info [- 1 ][0 ] == line_index :
109121 break
110- # Add the block starting line info to tmpstack
111122 tmpstack .append ((line_index , text ))
112123 while tmpstack :
113124 self .info .append (tmpstack .pop ())
@@ -116,12 +127,13 @@ def update_label(self):
116127 self .label ["text" ] = '\n ' .join (lines )
117128
118129 def timer_event (self ):
119- self .update_label ()
130+ if self .label :
131+ self .update_label ()
120132 self .text .after (UPDATEINTERVAL , self .timer_event )
121133
122134 def font_timer_event (self ):
123135 newtextfont = self .text ["font" ]
124- if newtextfont != self .textfont :
136+ if self . label and newtextfont != self .textfont :
125137 self .textfont = newtextfont
126138 self .label ["font" ] = self .textfont
127139 self .text .after (FONTUPDATEINTERVAL , self .font_timer_event )
0 commit comments