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

Re: [jasspa] Auto-indentation for Python

Expand Messages
  • Sakari Tanhua
    Hi Steven, ... 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
    Message 1 of 6 , May 19, 2004
    • 0 Attachment
      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
    • first last
      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
      Message 2 of 6 , May 20, 2004
      • 0 Attachment
        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
      • Phillips, Steven
        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
        Message 3 of 6 , 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
          >
          >
          >
          >
          >
          >
        • Sakari Tanhua
          Steven and Gabriel, ... But why does it lose the indentation level if the previous line contains only whitespaces? I often want to leave empty lines between
          Message 4 of 6 , May 21, 2004
          • 0 Attachment
            Steven and Gabriel,

            > 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.

            But why does it lose the indentation level if the previous line contains
            only whitespaces? I often want to leave empty lines between statements which
            are at the same indentation level, and after the empty line I certainly
            don't want to hit tab several times to get back to the same indentation
            level.

            I tried to find the code which implements the 'indent' mode but couldn't
            find it. I guess it's written in C but emode.def's MDINDEN was about the
            only thing I could find. Sure I grepped for MDINDEN but didn't find anything
            interesting. Is there already a way to pass parameters to modes to finetune
            their behavior? It'd be nice if the 'indent' mode could be configured to
            maintain the indentation even when the previous line contains only
            whitespaces.

            > Creating an indent rule would suggest that a 'restyle-buffer' should work

            Indeed, so no indent mode for Python then.

            > 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,

            Yes, that's good.

            > 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

            But I don't like this. IMHO it shouldn't remove whitespaces from the current
            line, and it definitely shouldn't lose the indentation on the next line.
            Assuming that's what you meant by "no indentation", i.e. that the next line
            wouldn't contain any whitespaces and the cursor would be at column 0.

            It'd be much better if it would simply retain the indentation level even
            when the previous line contains only whitespaces. That's exactly what GNU
            Emacs does and I never need to hit tab when coding with it, which is nice.
            Furthermore, to reduce the indentation by one level, there's no separate key
            combination for it (like S-tab) but instead I simply hit backspace and it
            eats one indentation level. (Assuming the cursor is right after the
            indentation, of course.)

            I actually wrote a quite primitive auto indentation because I wanted to
            learn the macro language. It's long enough that I don't want to include it
            in this mail, but it can be found here:

            http://www.hut.fi/~stanhua/mypython.emf

            It's probably a bit amateurishly coded because I don't know all the tricks
            of the language. For example, is there a better way to return values from a
            macro than to set #p? variables? If I understood correctly, !return can
            return only boolean values.

            Anyway, it seems to work pretty much the way I like, and though it still
            needs lots of improvements, it's already usable.

            Sakari
          Your message has been successfully submitted and would be delivered to recipients shortly.