Loading ...
Sorry, an error occurred while loading the content.

1110RE: [jasspa] Auto-indentation for Python

Expand Messages
  • Phillips, Steven
    May 20, 2004
    • 0 Attachment
      I'm kind of in agreement here, by using the 'indent' mode pressing enter will start a new line with the same indent as the previous line then you can either press 'tab' to indent more or 'S-tab' (shift-tab) to reduce the indent. Creating an indent rule would suggest that a 'restyle-buffer' should work but this clearly cannot work as the meaning of the program is determined by the indentation and therefore cannot be automatically worked out (otherwise we'd have monkeys writing programs :)

      The one thing that I think could be useful is doing a semi-automatic indentation when the enter key is pressed. In this situation it could do some vaguely useful things, i.e. if the current line ends in a ':' then create a new line with an extra indent, if the current line is blank (just has white spaces) then remove all white spaces on the current line and create a new empty line with no indentation - and I think that is as much as I would dare do considering how important the indentation is. This would best be implemented as a simple macro bound to the return key in python buffers.

      Sakari - How does this sound? If this is okay I will create said macro.

      Steve

      > -----Original Message-----
      > From: first last [mailto:prelude_2_murder@...]
      > Sent: 20 May 2004 09:34
      > To: jasspa@yahoogroups.com
      > Subject: Re: [jasspa] Auto-indentation for Python
      >
      >
      > From what you are saying (and for what I know of Python, I
      > haven't used
      > it for a long, long time) the probable best way to do what you want is
      > to map a key to decrease the indent index (or whatever you call the
      > variable indent uses to keep track of how many levels of indentation
      > has to use for a line).
      >
      > In C-like languages you can have automatic indentation because {} take
      > care of telling ME where to start/end the block, in Python you have to
      > tell the program where you want the indentation, as it is
      > meaningful to
      > the language and it cannot second guess (apart from : starting a
      > block).
      >
      > Correct me if I am wrong but ME just goes to the current indent level
      > when you press enter, so if we have a key (shift + backspace?) that
      > changes the indentation level it should work exactly as expected.
      >
      > Hope this message is helpful...
      >
      > Gabriel
      >
      > --- Sakari Tanhua <stanhua@...> wrote:
      > ---------------------------------
      > Hi Steven,
      >
      > > I would have expected the following to help/work
      > >
      > > indent .hilight.python o "^$" -4
      > >
      > > Which in theory should make a blank line reduce the
      > indentation by 4.
      >
      > I first thought of something like that, too, but the problem is (in
      > addition
      > to the fact that it doesn't work :) ) that it wouldn't allow empty
      > lines in
      > the code between other statements belonging to the same block.
      >
      > > if (type(result) == type('') or type(value) == type('')):
      > > ok = result == value
      > > else:
      > > ok = abs(result - value) <= fuzz
      > > if not ok:
      > > print '!!\t!!\t!! should be', value, 'diff', abs(result -
      > value)
      > >
      > > Is this indented correctly?
      >
      > Yes
      >
      > > If so why is the second 'if' not part of the 'else:'?
      >
      > Python seems a bit weird language at first because the
      > indentation is a
      > critical part of the code. So the second 'if' is not part of
      > the 'else'
      > simply because it's not indented at the 'else' block level!
      >
      > if (type(result) == type('') or type(value) == type('')):
      > ok = result == value
      > else:
      > ok = abs(result - value) <= fuzz
      >
      > if not ok:
      > print '!!\t!!\t!! should be', value, 'diff', abs(result -
      > value)
      >
      > Now it would belong to the else block, so it's now a totally different
      > program, and I only added some whitespace to it. This is Python. :)
      >
      > This is also the root of all evil (well, at least from indent's point
      > of
      > view), as it's impossible for a program to predict how a given line
      > should
      > be indented. But the good thing is that auto indentation for Python is
      > really simple to implement:
      >
      > 1. If the previous line ends with a colon, the second line should be
      > indented one level more.
      >
      > 2. Otherwise, always maintain the previous indentation level,
      > regardless
      > whether or not the previous line contains anything else than
      > whitespaces.
      >
      > 3. The "b" and "c" type indentations (indent(2) terminology) should
      > behave
      > like in C.
      >
      > I guess that'd pretty much do the trick. It should also be noted that
      > the
      > indentation level in Python doesn't necessarily have to be exactly 4
      > spaces.
      > It can be anything, even \t's, but it must be uniform in one
      > block. The
      > style guide favors 4 spaces.
      >
      > After all, maybe it's not wise to try to modify ME's indentation code
      > to
      > support this, but write some macros to change the behavior of
      > backspace
      > and
      > enter?
      >
      > Sakari
      >
      >
      > ______________________________________________________________
      > ____________
      >
      > This is an unmoderated list. JASSPA is not responsible for the content
      > of
      > any material posted to this list.
      >
      > To unsubscribe, send a mail message to
      >
      > mailto:jasspa-unsubscribe@yahoogroups.com
      >
      > or visit http://groups.yahoo.com/group/jasspa and
      > modify your account settings manually.
      >
      >
      >
      >
      >
      > Yahoo! Groups Sponsor ADVERTISEMENT
      >
      >
      > ---------------------------------
      > Yahoo! Groups Links
      >
      > To visit your group on the web, go to:
      > http://groups.yahoo.com/group/jasspa/
      >
      > To unsubscribe from this group, send an email to:
      > jasspa-unsubscribe@yahoogroups.com
      >
      > Your use of Yahoo! Groups is subject to the Yahoo! Terms
      > of Service.
      >
      >
      >
      >
      >
      >
      > ____________________________________________________________
      > Yahoo! Messenger - Communicate instantly..."Ping"
      > your friends today! Download Messenger Now
      > http://uk.messenger.yahoo.com/download/index.html
      >
      >
      > ------------------------ Yahoo! Groups Sponsor
      > ---------------------~-->
      > Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
      > Now with Pop-Up Blocker. Get it for free!
      > http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/89EolB/TM
      > --------------------------------------------------------------
      > -------~->
      >
      > ______________________________________________________________
      > ____________
      >
      > This is an unmoderated list. JASSPA is not responsible for
      > the content of
      > any material posted to this list.
      >
      > To unsubscribe, send a mail message to
      >
      > mailto:jasspa-unsubscribe@yahoogroups.com
      >
      > or visit http://groups.yahoo.com/group/jasspa and
      > modify your account settings manually.
      >
      >
      >
      > Yahoo! Groups Links
      >
      >
      >
      >
      >
      >
    • Show all 6 messages in this topic