|There's more than one way to do things|
[emacs] RFC live formating / tidying while I typeby LanX (Cardinal)
|on Sep 08, 2016 at 19:32 UTC||Need Help??|
I always wanted to have live formatting as I type Perl code.
The usual advice to pipe a text selection through perltidy is just too slow, and furthermore you might want to have only a visible snippet formatted to keep control, and tidy can fail here (emacs is mostly very tolerant)
The following code called with M-x my-indent-block is re-indenting the surrounding block you are in. The devadvice binds it to the cperls electric-semi feature which automatically does a return and and indentation of the following line (see M-x customize-group RET cperl-autoinsert-details for details)
NB: this is work progress but it's by far faster than perltidy
cperl-indent-exp is normally bound to M-C-q , but is only useful after you've put the cursor in front of the block to highlight (here the for ), that's why I remapped it to work from inside the block.
M-q should be the far better choice ( I hate 3+ finger combinations). In cperl-mode it's bound to cperl-fill-paragraph which wraps text to 80 colums ONLY in POD, here-docs or comments, but isn't useful in code! ( well as long as you are not trying to obfuscate )
But now I'm not sure how to combine the old cperl-heuristic to distinguish between code and text with this new features, without patching cperl.
Nice to have for my-indent-block
*) there is align-current for regions, but it's reg-ex based fails with nested structures
but with the semantic information of cperl's parsing it's possible to distinguish between the different levels of =>.