[Python-mode] comment to signal de-indentation?

Andreas Eisele eisele at coli.uni-sb.de
Thu Feb 19 10:53:43 EST 2004


Dear Python-Mode maintainers,

I am not sure this is the best way to promote an idea
that is bugging me since a while, but here it goes:

Whereas I generally like Python's idea of significant
whitespace very much, there is one particular problem
that regularly comes up in connection with editing
source files in emacs, especially re-indenting code
that has been moved.

I often move pieces of code between files and functions
(e.g. during refactoring), and quite often, the moved
codes need a new level of indentation.  In all
programming languages I have used so far, except for
Python, I can walk through the moved code from top to
bottom, press tab in each line, trust the respective
emacs mode, and voila, everything fine.  I am sure
there are other ways to achieve the same effect, but as
my approach is simple and works almost everywhere, I do
not really want to give it up.

Now in Python, the problem appears in each line where
indendation is reduced, although it could have stayed
the same.  As there is no syntactical evidence for
de-indentation, emacs indents to the maximal possible
amount, and I have to manually reduce indendation
again, which means I have to recognize the logical
structure of my code and make sure it stays the same.
No big deal to do occasionally, but if there is a lot
of code, and I am already tired, this starts getting
inconvenient and risky...

One could solve this problem quite easily by
introducing a piece of notation that makes clear that
indendation should be reduced.  As I do not want to
mess with the language (which is very nice), I would
prefer a special kind of comment that is meaningful to
the editor, and would have a comparable effect to the
presence of a pass, break, or return statement.

I would e.g. want to write

        if someCondition:
           statement1
           statement2
           #-

and after that, I would expect the editor to go on in
the next line on the level of the "if", i.e.

        statement3

In a way, the #- would be a counterpart to the final
colon; you could think of them as opening vs. closing
the indented block. (Not sure #- is the ultimate
choice, but it would make for a good start).

Now my questions:

Has this or have similar proposals ever been made and
discussed?  Is there perhaps existing functionality
within python-mode.el which does almost what I need
which I just missed to find or understand?  Is it just
me who wants a simple and (cross-lingual) standard
approach for re-indenting moved code?

Lots of questions, but perhaps you can give one short
answer to all of them?

Thanks a lot for your consideration, and best regards,

Andreas Eisele


PS.: Before writing this, I had tried to modify
py-block-closing-keywords-re, but it did not work out
as easily as I had hoped.  Perhaps I should just try a
bit harder...

-- 
Dr. Andreas Eisele          Computational Linguistics           
University of Saarland          eisele at coli.uni-sb.de
Stuhlsatzenhausweg 3            tel: +49-681-302-5285
D-66123 Saarbrücken             fax: +49-681-302-5338





More information about the Python-mode mailing list