1. Add an Options menu entry: Code Context
2. Add a <<toggle-code-context>> envent to the [CodeContext] section of
config-extensions.def and also a default-on variable, set to 0.
3. Update the help file to include Code Context.
M CodeContext.py
M config-extensions.def
M help.txt
diff --git a/Lib/idlelib/CodeContext.py b/Lib/idlelib/CodeContext.py
index 32dec82..d8f9237 100644
--- a/Lib/idlelib/CodeContext.py
+++ b/Lib/idlelib/CodeContext.py
@@ -13,49 +13,61 @@
import Tkinter
from configHandler import idleConf
from PyShell import PyShell
-from string import whitespace
import re
BLOCKOPENERS = dict([(x, None) for x in ("class", "def", "elif", "else",
"except", "finally", "for", "if",
"try", "while")])
INFINITY = 1 << 30
-UPDATEINTERVAL = 100 #ms
-FONTUPDATEINTERVAL = 1000 #ms
+UPDATEINTERVAL = 100 # millisec
+FONTUPDATEINTERVAL = 1000 # millisec
getspacesfirstword = lambda s, c=re.compile(r"^(\s*)(\w*)"): c.match(s).groups()
class CodeContext:
- menudefs = []
+ menudefs = [('options', [('!Code Conte_xt', '<<toggle-code-context>>')])]
+
numlines = idleConf.GetOption("extensions", "CodeContext",
"numlines", type="int", default=3)
bgcolor = idleConf.GetOption("extensions", "CodeContext",
"bgcolor", type="str", default="LightGray")
fgcolor = idleConf.GetOption("extensions", "CodeContext",
"fgcolor", type="str", default="Black")
+ default_on = idleConf.GetOption("extensions", "CodeContext",
+ "default_on", type="int", default=0)
def __init__(self, editwin):
if isinstance(editwin, PyShell):
return
self.editwin = editwin
self.text = editwin.text
self.textfont = self.text["font"]
- self.label = Tkinter.Label(self.editwin.top,
- text="\n" * (self.numlines - 1),
- anchor="w", justify="left",
- font=self.textfont,
- bg=self.bgcolor, fg=self.fgcolor,
- relief="sunken",
- width=1, # Don't request more than we get
- )
- self.label.pack(side="top", fill="x", expand=0,
- after=self.editwin.status_bar)
+ self.label = None
# Dummy line, which starts the "block" of the whole document:
self.info = list(self.interesting_lines(1))
self.lastfirstline = 1
+ if self.default_on:
+ self.toggle_code_context_event()
+ self.editwin.setvar('<<toggle-code-context>>', True)
# Start two update cycles, one for context lines, one for font changes.
self.text.after(UPDATEINTERVAL, self.timer_event)
self.text.after(FONTUPDATEINTERVAL, self.font_timer_event)
+ def toggle_code_context_event(self, event=None):
+ if not self.label:
+ self.label = Tkinter.Label(self.editwin.top,
+ text="\n" * (self.numlines - 1),
+ anchor="w", justify="left",
+ font=self.textfont,
+ bg=self.bgcolor, fg=self.fgcolor,
+ relief="sunken",
+ width=1, # Don't request more than we get
+ )
+ self.label.pack(side="top", fill="x", expand=0,
+ after=self.editwin.status_bar)
+ else:
+ self.label.destroy()
+ self.label = None
+
def get_line_info(self, linenum):
"""Get the line indent value, text, and any block start keyword
@@ -107,7 +119,6 @@
del self.info[-1]
if self.info[-1][0] == line_index:
break
- # Add the block starting line info to tmpstack
tmpstack.append((line_index, text))
while tmpstack:
self.info.append(tmpstack.pop())
@@ -116,12 +127,13 @@
self.label["text"] = '\n'.join(lines)
def timer_event(self):
- self.update_label()
+ if self.label:
+ self.update_label()
self.text.after(UPDATEINTERVAL, self.timer_event)
def font_timer_event(self):
newtextfont = self.text["font"]
- if newtextfont != self.textfont:
+ if self.label and newtextfont != self.textfont:
self.textfont = newtextfont
self.label["font"] = self.textfont
self.text.after(FONTUPDATEINTERVAL, self.font_timer_event)
diff --git a/Lib/idlelib/config-extensions.def b/Lib/idlelib/config-extensions.def
index 00162a0..39f9098 100644
--- a/Lib/idlelib/config-extensions.def
+++ b/Lib/idlelib/config-extensions.def
@@ -3,13 +3,14 @@
#
# Each extension must have at least one section, named after the extension
# module. This section must contain an 'enable' item (=1 to enable the
-# extension, =0 to disable it) and also contain any other general
-# configuration items for the extension. Each extension may also define up to
-# two optional sections named ExtensionName_bindings and
-# ExtensionName_cfgBindings. If present, ExtensionName_bindings defines virtual
-# event bindings for the extension that are not sensibly re-configurable. If
-# present, ExtensionName_cfgBindings defines virtual event bindings for the
-# extension that may be sensibly re-configured.
+# extension, =0 to disable it) and also contain any other general configuration
+# items for the extension. Each extension must define at least one section
+# named ExtensionName_bindings or ExtensionName_cfgBindings. If present,
+# ExtensionName_bindings defines virtual event bindings for the extension that
+# are not user re-configurable. If present, ExtensionName_cfgBindings
+# defines virtual event bindings for the extension that may be sensibly
+# re-configured. If there are no keybindings for a menus' virtual events,
+# include lines like <<toggle-code-context>>= (See [CodeContext], below.)
# Currently it is necessary to manually modify this file to change extension
# key bindings and default values. To customize, create
@@ -65,5 +66,8 @@
[CodeContext]
enable=1
numlines=3
+default_on=0
bgcolor=LightGray
fgcolor=Black
+[CodeContext_bindings]
+toggle-code-context=
diff --git a/Lib/idlelib/help.txt b/Lib/idlelib/help.txt
index 9af8e0d..e91e8c6 100644
--- a/Lib/idlelib/help.txt
+++ b/Lib/idlelib/help.txt
@@ -88,9 +88,9 @@
Startup Preferences may be set, and Additional Help
Souces can be specified.
---
- Revert to Default Settings -- Restore original settings. Not
- currently implemented - simply delete
- your .idlerc file.
+ Code Context -- Open a pane at the top of the edit window which
+ shows the block context of the section of code
+ which is scrolling off the top or the window.
Windows Menu: