[Python-Dev] Fun with whitespace

Kurt B. Kaiser kbk at shore.net
Wed Jul 21 06:59:26 CEST 2004


Tim Peters <tim.peters at gmail.com> writes:

> [Kurt B. Kaiser]
>> I'm thinking that it might be a good idea to modify IDLE to use
>> four-space indenting only, and remove the Tab options from the
>> configuration dialog.
>
> The conceptual disconnect here comes from the IDLE shell window, which
> still defaults to using hard tabs.  This appears to be a consequence
> of setting (the conceptual) sys.PS2 to an empty string but (the
> conceptual) sys.PS1 to a 4-character string, so that continued lines
> "don't look right" in the shell unless hard tabs are used.  I dislike
> this so much that I avoid using the shell window most days -- I'd
> rather it looked "just like" a command-line shell.

The thing that bothers me about the command-line shell is the first
indent is four characters in from the "def", and subsequent indents
are eight.

IDLE lines up code the same way, except without the leading '...'.
First an apparent four character indent, then subsequent eight
character indents, using tabs.

I remember that you previously remarked that it would be useful to be
able to cut/paste from the shell to an Edit window.  I agree.  Right
now it's a disaster because of the '>>> ", the strange indentation,
and the tabs, which causes tabnanny nag.  Then the unfortunate user
gets to play with 'untabify region', and if he doesn't watch out
(i.e. convert the eight character tabs to four spaces) a real snarl
results.

What I'd like is for the shell to look the same as an Edit window.
This would imply replacing the '>>>' with a prompt column on the left
which was not part of the text region.  I haven't investigated that,
but it would seem to involve a Tkinter/Tk modification.  There are
other editors which take this approach, which also allows the
cut/paste and the use of spaces in the Shell.  But it may not be
possible without modifying Tk, which is probably not desirable.

Alternatively, PS2 could be four spaces (or do you all prefer the
dots?)  and indents could be four spaces.  It seems that cut/paste
would work if the reindent first magically replaced '^>>> ' (and
'^...') with ''.

>> Right now, every time a program is Run/F5, IDLE runs a version of
>> tabnanny on it and then runs a syntax check.  We could add a
>> reindent.py check after tabnanny (if tabnanny fails, the user is notified
>> to fix things first).  Indents would be normalized at 4 spaces, and if
>> an error was detected, a dialog would warn the user that the file
>> would be changed.  The user could confirm (auto fix) or cancel and fix
>> things manually.
>
> I know Guido wants IDLE to whine whenever it's about to save a code
> file, but that dialog is a PITA.  

I'm happy to report that there is now an autosave option.  It's set
off by default, but I think it works great.  With the undo feature,
there really isn't any reason not to use it.  Code is saved and run
in a fresh environment with a single F5.

> I sure don't want two of 'em!  I vote for a silent revolution, where
> you add a secret "don't whine at me about any changes you make when
> I hit F5 -- just do it and leave me alone" option that only I know
> about <wink>.

If reindent can't damage user code, that would be OK by me.

I'd say also get rid of 'untabify' and let the reindent handle it.

>> I've tried to use IDLE to convert tab-indented code to space-indented,
>> but it's never worked well for me.  If you set the wrong number of
>> spaces/tab the code gets scrambled.  reindent.py seems to work fine.
>> Are there situations in which it can fail, assuming tabnanny is happy?
>
> None are known.  All of Python has been run through it now <wink>, and
> all of Zope3 was run through it many months ago w/o problems.  If you
> *don't* check the syntax first, it can be baffling, because
> tokenize.py can be baffling.  For example, if you have an extra left
> paren on some line, tokenize is likely to produce an incomprehensible
> exception at the end of the file, claiming that it hit EOF in an
> unterminated string.

OK, so the sequence would be: tabnanny, syntax check, reindent.

>> The implementation would continue to create the .bak file.  Alternatively,
>> maybe the undo capability could be extended to cover the reindent.  Or
>> both.
>>
>> Is it time for IDLE to enforce four-space indentation?  Any opinions?
>
> 4-space is all I ever use -- except in the shell window.  So have to
> leave objections to someone insane ...

:-)   Do people have issues with a 4-space indented Shell?

-- 
KBK


More information about the Python-Dev mailing list