1. Polish tabbing code.

2. Restore use of set_indentation_params(), was dead code since
   Autoindent.py was merged into EditorWindow.py.
3. Make usetabs, indentwidth, tabwidth, context_use_ps1 instance vars
   and set in EditorWindow.__init__()
4. In PyShell.__init__() set usetabs, indentwidth and context_use_ps1
   explicitly (config() is eliminated).
5. Add Tabnanny check when Module is Run/F5, not just when Checked.
6. Discourage using an indent width other than 8 when using tabs to
   indent Python code.

M EditorWindow.py
M NEWS.txt
M PyShell.py
M ScriptBinding.py
This commit is contained in:
Kurt B. Kaiser 2005-01-19 00:22:59 +00:00
parent 75eabd2944
commit 6af4498602
4 changed files with 71 additions and 62 deletions

View File

@ -165,6 +165,38 @@ class EditorWindow(object):
text.pack(side=TOP, fill=BOTH, expand=1) text.pack(side=TOP, fill=BOTH, expand=1)
text.focus_set() text.focus_set()
# usetabs true -> literal tab characters are used by indent and
# dedent cmds, possibly mixed with spaces if
# indentwidth is not a multiple of tabwidth,
# which will cause Tabnanny to nag!
# false -> tab characters are converted to spaces by indent
# and dedent cmds, and ditto TAB keystrokes
self.usetabs = False
# indentwidth is the number of characters per logical indent level.
# Recommended Python default indent is four spaces.
self.indentwidth = 4
# tabwidth is the display width of a literal tab character.
# CAUTION: telling Tk to use anything other than its default
# tab setting causes it to use an entirely different tabbing algorithm,
# treating tab stops as fixed distances from the left margin.
# Nobody expects this, so for now tabwidth should never be changed.
self.tabwidth = 8 # for IDLE use, must remain 8 until Tk is fixed.
# indentwidth should be 8 when usetabs is True.
# If context_use_ps1 is true, parsing searches back for a ps1 line;
# else searches for a popular (if, def, ...) Python stmt.
self.context_use_ps1 = False
# When searching backwards for a reliable place to begin parsing,
# first start num_context_lines[0] lines back, then
# num_context_lines[1] lines back if that didn't work, and so on.
# The last value should be huge (larger than the # of lines in a
# conceivable file).
# Making the initial values larger slows things down more often.
self.num_context_lines = 50, 500, 5000000
self.per = per = self.Percolator(text) self.per = per = self.Percolator(text)
if self.ispythonsource(filename): if self.ispythonsource(filename):
self.color = color = self.ColorDelegator() self.color = color = self.ColorDelegator()
@ -196,6 +228,8 @@ class EditorWindow(object):
io.set_filename(filename) io.set_filename(filename)
self.saved_change_hook() self.saved_change_hook()
self.set_indentation_params(self.ispythonsource(filename))
self.load_extensions() self.load_extensions()
menu = self.menudict.get('windows') menu = self.menudict.get('windows')
@ -214,10 +248,6 @@ class EditorWindow(object):
self.askinteger = tkSimpleDialog.askinteger self.askinteger = tkSimpleDialog.askinteger
self.showerror = tkMessageBox.showerror self.showerror = tkMessageBox.showerror
if self.extensions.has_key('AutoIndent'):
self.extensions['AutoIndent'].set_indentation_params(
self.ispythonsource(filename))
def new_callback(self, event): def new_callback(self, event):
dirname, basename = self.io.defaultfilename() dirname, basename = self.io.defaultfilename()
self.flist.new(dirname) self.flist.new(dirname)
@ -881,62 +911,19 @@ class EditorWindow(object):
"n" * newtabwidth) "n" * newtabwidth)
text.configure(tabs=pixels) text.configure(tabs=pixels)
### begin autoindent code ###
# usetabs true -> literal tab characters are used by indent and
# dedent cmds, possibly mixed with spaces if
# indentwidth is not a multiple of tabwidth
# false -> tab characters are converted to spaces by indent
# and dedent cmds, and ditto TAB keystrokes
# indentwidth is the number of characters per logical indent level.
# tabwidth is the display width of a literal tab character.
# CAUTION: telling Tk to use anything other than its default
# tab setting causes it to use an entirely different tabbing algorithm,
# treating tab stops as fixed distances from the left margin.
# Nobody expects this, so for now tabwidth should never be changed.
usetabs = 0
indentwidth = 4
tabwidth = 8 # for IDLE use, must remain 8 until Tk is fixed
# If context_use_ps1 is true, parsing searches back for a ps1 line;
# else searches for a popular (if, def, ...) Python stmt.
context_use_ps1 = 0
# When searching backwards for a reliable place to begin parsing,
# first start num_context_lines[0] lines back, then
# num_context_lines[1] lines back if that didn't work, and so on.
# The last value should be huge (larger than the # of lines in a
# conceivable file).
# Making the initial values larger slows things down more often.
num_context_lines = 50, 500, 5000000
def config(self, **options):
for key, value in options.items():
if key == 'usetabs':
self.usetabs = value
elif key == 'indentwidth':
self.indentwidth = value
elif key == 'tabwidth':
self.tabwidth = value
elif key == 'context_use_ps1':
self.context_use_ps1 = value
else:
raise KeyError, "bad option name: %r" % (key,)
# If ispythonsource and guess are true, guess a good value for # If ispythonsource and guess are true, guess a good value for
# indentwidth based on file content (if possible), and if # indentwidth based on file content (if possible), and if
# indentwidth != tabwidth set usetabs false. # indentwidth != tabwidth set usetabs false.
# In any case, adjust the Text widget's view of what a tab # In any case, adjust the Text widget's view of what a tab
# character means. # character means.
def set_indentation_params(self, ispythonsource, guess=1): def set_indentation_params(self, ispythonsource, guess=True):
if guess and ispythonsource: if guess and ispythonsource:
i = self.guess_indent() i = self.guess_indent()
if 2 <= i <= 8: if 2 <= i <= 8:
self.indentwidth = i self.indentwidth = i
if self.indentwidth != self.tabwidth: if self.indentwidth != self.tabwidth:
self.usetabs = 0 self.usetabs = False
self.set_tabwidth(self.tabwidth) self.set_tabwidth(self.tabwidth)
def smart_backspace_event(self, event): def smart_backspace_event(self, event):
@ -988,8 +975,9 @@ class EditorWindow(object):
# if intraline selection: # if intraline selection:
# delete it # delete it
# elif multiline selection: # elif multiline selection:
# do indent-region & return # do indent-region
# indent one level # else:
# indent one level
text = self.text text = self.text
first, last = self.get_selection_indices() first, last = self.get_selection_indices()
text.undo_block_start() text.undo_block_start()
@ -1005,6 +993,7 @@ class EditorWindow(object):
# only whitespace to the left # only whitespace to the left
self.reindent_to(effective + self.indentwidth) self.reindent_to(effective + self.indentwidth)
else: else:
# tab to the next 'stop' within or to right of line's text:
if self.usetabs: if self.usetabs:
pad = '\t' pad = '\t'
else: else:
@ -1178,28 +1167,34 @@ class EditorWindow(object):
def toggle_tabs_event(self, event): def toggle_tabs_event(self, event):
if self.askyesno( if self.askyesno(
"Toggle tabs", "Toggle tabs",
"Turn tabs " + ("on", "off")[self.usetabs] + "?", "Turn tabs " + ("on", "off")[self.usetabs] +
"?\nIndent width " +
("will be", "remains at")[self.usetabs] + " 8.",
parent=self.text): parent=self.text):
self.usetabs = not self.usetabs self.usetabs = not self.usetabs
# Try to prevent mixed tabs/spaces.
# User must reset indent width manually after using tabs
# if he insists on getting into trouble.
self.indentwidth = 8
return "break" return "break"
# XXX this isn't bound to anything -- see class tabwidth comments # XXX this isn't bound to anything -- see tabwidth comments
def change_tabwidth_event(self, event): ## def change_tabwidth_event(self, event):
new = self._asktabwidth() ## new = self._asktabwidth()
if new != self.tabwidth: ## if new != self.tabwidth:
self.tabwidth = new ## self.tabwidth = new
self.set_indentation_params(0, guess=0) ## self.set_indentation_params(0, guess=0)
return "break" ## return "break"
def change_indentwidth_event(self, event): def change_indentwidth_event(self, event):
new = self.askinteger( new = self.askinteger(
"Indent width", "Indent width",
"New indent width (2-16)", "New indent width (2-16)\n(Always use 8 when using tabs)",
parent=self.text, parent=self.text,
initialvalue=self.indentwidth, initialvalue=self.indentwidth,
minvalue=2, minvalue=2,
maxvalue=16) maxvalue=16)
if new and new != self.indentwidth: if new and new != self.indentwidth and not self.usetabs:
self.indentwidth = new self.indentwidth = new
return "break" return "break"

View File

@ -3,6 +3,14 @@ What's New in IDLE 1.2a0?
*Release date: XX-XXX-2005* *Release date: XX-XXX-2005*
- Discourage using an indent width other than 8 when using tabs to indent
Python code.
- Restore use of EditorWindow.set_indentation_params(), was dead code since
Autoindent was merged into EditorWindow.
- Add Tabnanny check before Run/F5, not just when Checking module.
- If an extension can't be loaded, print warning and skip it instead of - If an extension can't be loaded, print warning and skip it instead of
erroring out. erroring out.

View File

@ -795,7 +795,11 @@ class PyShell(OutputWindow):
import __builtin__ import __builtin__
__builtin__.quit = __builtin__.exit = "To exit, type Ctrl-D." __builtin__.quit = __builtin__.exit = "To exit, type Ctrl-D."
# #
self.config(usetabs=1, indentwidth=8, context_use_ps1=1) ## self.config(usetabs=1, indentwidth=8, context_use_ps1=1)
self.usetabs = True
# indentwidth must be 8 when using tabs. See note in EditorWindow:
self.indentwidth = 8
self.context_use_ps1 = True
# #
text = self.text text = self.text
text.configure(wrap="char") text.configure(wrap="char")

View File

@ -138,6 +138,8 @@ class ScriptBinding:
filename = self.getfilename() filename = self.getfilename()
if not filename: if not filename:
return return
if not self.tabnanny(filename):
return
code = self.checksyntax(filename) code = self.checksyntax(filename)
if not code: if not code:
return return