Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: How would you indent this?

by no longer just digit (Beadle)
on Mar 19, 2021 at 03:29 UTC ( [id://11129942]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^3: How would you indent this?
by haj (Vicar) on Mar 19, 2021 at 07:59 UTC
    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.

        I used to write BEGIN: {}; with a semicolon after it

        In this case, the behavior of CPerl mode is understandable. One of the more difficult things when parsing Perl is to distinguish in which cases you need a semicolon after a closing brace and in which cases you don't. In your example, the semicolon is not required, so it is actually a statement of its own. I'd say CPerl mode is, to some extent, entitled to move this statement on a separate line, if only as a hint that you might revisit your code.

        By the way: Your BEGIN: is actually a label. If you want a BEGIN block, get rid of the colon.

        it happens sometimes and doesn't happen other times. Here is a screenshot...

        I can explain that, too. The message is technically correct. Read closely: End of ‘m} ... }’. The closing brace in the next line is interpreted as the starting delimiter of the match. That's weird, but it is actually valid to write m}foo}. The two numbers in the message are the character positions of the closing brace and the following newline, so M-g c 462 will take you to the closing brace. This is, in fact, the same situation as when you type m/ without a closing delimiter.

        Without that strange scrolling (which I still fail to reproduce), the message is harmless and will disappear as soon as the closing delimiter is entered. Since the message is also an indicator that syntax highlighting and indentation will not work correctly until the closing delimiter is found, I'd hesitate to just suppress it.

Re^3: How would you indent this?
by haj (Vicar) on Apr 04, 2021 at 23:43 UTC

    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://11129942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found