in reply to Re^4: How would you indent this?
in thread How would you indent this?

Problem in this thread is that the OP is effectively asking multiple questions:
  1. how should this be best written
  2. how should my editor do it
  3. what's the bug in my editor
  1. They way I write it of course!
  2. The way I would write it if I were taking appropriate care
  3. Is it that it is emacs?

The are many very bad indentation and white space usage styles in use. Aside from a desire for consistency and style informed by "that is the way we always do it", I don't think many people think very much about their code writing style. My own style decisions, which I have thought about, are driven by laziness. I use white space, to the extent reasonably possible, consistently with the conventional usage in (English) written prose. That way I already have a parsing tool (my brain) that knows how to break stuff up without needing different settings packages for prose and code.

Other style decisions are made based on intelligibility, such as keeping lines short with the important stuff toward the front, minimizing nesting, keeping functions reasonably short, and so on. Look at pretty much any of my code on PerlMonks to see how that looks. Note though that my block delimiting brace usage for Perl is K&R which I avoid in other languages (I prefer Whitesmiths).

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^6: How would you indent this?
by LanX (Saint) on Mar 18, 2021 at 20:48 UTC
    > Is it that it is emacs?

    The OP is using a function which is not standard indenting, hitting TAB is.°

    This function offers plenty of interactive real-time benefits, without needing to wait 2 sec till a Perl::Tidy background process returned.

    Komodo has nothing like this to offer.

    And contrary to you I know both editors pretty well.

    So please cut the FUD out.

    > The way I would write it if I were taking appropriate care

    That's BS, no software can decide what the best choice is, it can only assist at best.

    See AnoMonks demo here: Re: How would you indent this? Which identation style do you prefer?

    The editor shouldn't interfere automatically in the formatting after the author has chosen a representation.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    °) or activating auto-indent, which seems to be default now.

      > Is it that it is emacs?
      The OP is using a function which is not standard indenting, hitting TAB is.°

      That's an oversimplification.

      1. <TAB> is equivalent to M-x cperl-indent-line if (as per default) tab-always-indent is set to a true value. It indents the current line.
      2. M-x cperl-indent-exp indents the current expression and reformats if (as per default) cperl-indent-region-fix-constructs is set to 1.
      3. M-x cperl-indent-region indents the current region and reformats if (as per default) cperl-indent-region-fix-constructs is set to 1.
      4. <RET> is, per default, equivalent to M-x newline which, also per default, indents the current and the new (empty) line.

      None of these functions are more standard than the others, and with the current settings there's rarely the need to use <TAB> at all.

      °) or activating auto-indent, which seems to be default now.

      That's electric-indent-mode, which isn't actually overdocumented right now. One of the consequences of this mode is that the PBP recommendation to remap <RET> to M-x newline-and-indent is outdated and should no longer be used. Also, in Python buffers, where indentation can't be inferred from braces and such, Electric Indent mode is switched off.

        > <RET> ... also per default, indents the current and the new (empty) line.

        do you happen to know since which version?

        It would be nice if the GNU folks published the emacs info with a version number.

        > That's electric-indent-mode, which isn't actually overdocumented right now

        I welcome freeing up TAB for completion, but this makes my setups even more complicated if they are supposed to work with different emacs versions.

        Which is why I'm still stuck on 24 btw :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      I agree that the "editor shouldn't interfere automatically". One of the worst editors I ever worked with was the UCSD Pascal system which forced its own idea of indentation live while editing, including disallowing multi-line comments. I run perltidy occasionally to clean up after things like a big code refactor, and I pretty much always use it to clean up code from PerlMonks questions.

      The impressive thing about perltidy is that I have tuned it to produce almost exactly what I write most of the time. Some of the modern C++ pretty printer type tools are pretty good too. Modern Visual Studio can do a pretty good job and the C++ Code Style Formatter provided by Eclipse is very configurable aside from some weird omissions (why does it insist on a space before a : in a constructor declerator list?). So yes, pretty printing tools can get extremely close to making the same code style decisions I do, partly because I have thought hard about my style choices and understand what I want to achieve, so results are consistent and fairly easy to codify.

      I apologize for the perceived attack on emacs. Never fear, I treat vi equally. :-D

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        Perl::Tidy is an awesome tool. Unfortunately configuring it not so much.

        > I apologize for the perceived attack on emacs. Never fear, I treat vi equally. :-D

        Cheap attacks are cheap to get. That's the economics of FUD.

        Actually I like Komodo, it has indeed one feature I want to port back to emacs.

        Sadly it might not survive the next 5 years after Mozilla ended XUL and Activestate saw the need to Open Source it.

        Good luck!

        PS: No worries, I treat Pythonistas equally! :-D

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      Quoth Rolf:
      The OP is using a function which is not standard indenting, hitting TAB is.°

      I quite categorically am using the standard indenting of cperl-mode. Copy the original example into a file called, say "test.pl", then edit with emacs -Q test.pl, and take the following sequence of instructions:

      M-X cperl-mode C-SPC M-> M-C-\
      gives me (copy-pasting from Emacs window):
      my @headings = ( { key => 'k', name => 'Kanji', class => 'kanji'}, { key => 'skip', name => 'SKIP', class => 'skip-code'}, { key => 'co', name => 'Suggestion', class => 'skip-code'}, { key => 'disc', name => 'Discussion'}, );

      The version of Emacs I am using is as follows:

      GNU Emacs 27.1 (build 1, i386-portbld-freebsd12.1, GTK+ Version 3.24.2 +4, cairo version 1.16.0) Copyright (C) 2020 Free Software Foundation, Inc.

      The cperl-mode is the default supplied with the above.

        Hi

        M-C-\ is supposed to run indent-region resp. cperl-indent-region right?

        (Running C-h k will show the function bound to a key-combi)

        Guess what, I can't even activate this key-combination on my laptop with Windows + German keyboard with remapped CTRL.° (I'm too lazy to figure out why)

        I'm using emacs since university, it's only a few years I'm even actively using indent-region (and only since I remapped a closing curly } to run it automatically on the Perl block)

        Now if you take 100 average emacs hackers, how many will know indent-region ? I bet all will know TAB .

        indent-region is not even in the menus of emacs, cperl-mode is adding it's own version when activated.

        That's why I couldn't replicate your problem at first.

        Now from the docs of indent-region (all demos with 'emacs27 -Q')

        C-h f indent-region ... 3) Indent each line via ‘indent-according-to-mode’. ...

        from here on you can click on the links and navigate back with l (last)

        (indent-according-to-mode) Indent line in proper way for current major mode. *Normally*, this is done by calling the function specified by the variable ‘indent-line-function’.

        indent-line-function is a variable defined in ‘indent.el’. ... Setting this function is all you need to make TAB indent appropriately +. ...

        as you can see the default behavior is supposed to be that of TAB in a single line.

        Cperl-mode extended regional indentation with line-breaks for {blocks}

        And that feature is biting you with nested hashes.

        > The cperl-mode is the default supplied with the above.

        you are contradicting yourself since your recipe says M-x cperl-mode

        Emacs default is still perl-mode

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        °) And here we are at the very heart why many vimmers hate emacs - byzantine hotkeys. Which gets even worse with non US keyboards. A problem shared by Perl's sigils.