Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How would you indent this?

by haj (Vicar)
on Mar 18, 2021 at 13:49 UTC ( [id://11129906]=note: print w/replies, xml ) Need Help??


in reply to How would you indent this?

I'd consider this indentation of CPerl to be a bug.

CPerl mode's M-x indent-region does more than just indent, it also tries to "fix" spacing. But in this case it turns out just horribly wrong. Here's an example to show the intention: If you type the following:

if($fee){fie()}else{foe($foo)}

...and then process this with M-x indent-region, then you get (depending on your style, I'm using PBP here):

if ($fee) { fie(); } else { foe($foo); }

Nifty, eh? A real life saver... but only if your space key is broken.

The customization variable responsible for this is cperl-indent-region-fix-constructs (on by default), which is out of the scope of the predefined indentation styles. The documentation for that variable shows what it should do, but not what it actually does. So, if you don't need CPerl mode to reformat your code like this, then I suggest you just turn off that variable as a workaround (and, if you like, write a bug report).

By the way: Emacs can help with aligning the keys as suggested by ikegami and GrandFather, unrelated to CPerl mode. It is hardly worth the effort for four lines, though: Mark the assignment as a region, and then

align-regexp<RET>name<RET> align-regexp<RET>class<RET>

This yields:

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'}, )

Replies are listed 'Best First'.
Re^2: How would you indent this?
by no longer just digit (Beadle) on Mar 19, 2021 at 03:29 UTC

    Generally speaking I don't indent code, so I use mark-whole-buffer and then indent-region. In Go code I actually use

    (add-hook 'before-save-hook #'gofmt-before-save)

    instead so the whole buffer is formatted for me.

    I haven't been able to do that with Perl because of numerous oddities in cperl-mode. Probably the most annoying one is that it turns

    };
    

    into

    }
    ;
    
    So, if you don't need CPerl mode to reformat your code like this, then I suggest you just turn off that variable as a workaround (and, if you like, write a bug report).

    Thanks. I have reported bugs in cperl-mode in the past but things don't seem to have improved, and in at least one case things have got much worse, the case where it immediately throws an error about unbalanced parentheses whenever one presses the m or s keys. That used to be annoying when it was just an error message, but with recent Emacs it also scrolls the window, so in cperl-mode each time I press "m" or "s" the Emacs window scrolls in a random direction. It is truly exhausting when reading a piece of code in one place and then writing in another, and then the Emacs suddenly scrolls due to typing my or sub

    What I'm trying to do is to set up my own version of cperl-mode with better defaults and fewer bugs. In the case of this hash reference case, I wasn't sure what sort of indentation to use, although I was pretty sure I was going to use something other than the default behaviour, so I thought I'd ask here what people thought. It's been very interesting to get feedback from everyone, so thank you to all participants.

    By the way: Emacs can help with aligning the keys as suggested by ikegami and GrandFather, unrelated to CPerl mode. It is hardly worth the effort for four lines, though: Mark the assignment as a region, and then

    align-regexp<RET>name<RET> align-regexp<RET>class<RET>

    Thank you, that is extremely useful to know about. This page has more.

      Probably the most annoying one is that it turns
      };
      into
      } ;
      Hm. I can't reproduce that one. Do you have a example snippet for that behavior?
      I have reported bugs in cperl-mode in the past but things don't seem to have improved,...

      Well, not yet visible for users of released versions of Emacs. About half a year ago I started to work through the open bugs of CPerl mode, and a dozen or so have been fixed.

      If you are running Emacs 26.1 or newer, I'd like to invite you to try the "hot" cperl-mode.el from the Emacs repository. There has been quite some activity recently.

      Actually, I am currently working on a patch for indenting issues in CPerl mode. That patch has grown to 600 lines by now and is supposed to cover bugs #11773, #42169, and by accident, also #8077. It is kinda annoying but also kinda nice that new test cases are popping up right now!

      ... and in at least one case things have got much worse, the case where it immediately throws an error about unbalanced parentheses whenever one presses the m or s keys.

      That should not happen and I can not reproduce it. The error message appears only after typing m and an opening delimiter where it is at least technically correct. I haven't experienced any scrolling, though. Do you have some customization for the echo area?

        Hm. I can't reproduce that one. Do you have a example snippet for that behavior?

        That might be just me. I used to write BEGIN: {}; with a semicolon after it, as you can see here, so it was doing something possibly excessive rather than actually wrong.

        ... and in at least one case things have got much worse, the case where it immediately throws an error about unbalanced parentheses whenever one presses the m or s keys. That should not happen and I can not reproduce it.

        I couldn't reproduce it very well under emacs -Q, since it happens sometimes and doesn't happen other times. Here is a screenshot of a case when it happened. But it is reproducibly throwing the error message if I type m/ or tr/ instead of just m. The scrolling happens as well, which is much more annoying than the error message. I remember talking about the annoying error message to Ilya in about 2007 or so on comp.lang.perl.misc.

      It's been a few days, but...

      That used to be annoying when it was just an error message, but with recent Emacs it also scrolls the window, so in cperl-mode each time I press "m" or "s" the Emacs window scrolls in a random direction.

      A recent Emacs bug report #47549 prompted me to revisit that issue, and eventually I've been able to reproduce the scrolling. I've submitted a patch, and the Emacs maintainers have merged it into the master branch.

      So, the current cperl-mode.el from the Emacs repository should no longer jump around.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129906]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-20 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found