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)
before
afterfor my$scalar(1..10){print"$scalar"."txt";}
NB: this is work progress but it's by far faster than perltidyfor my $scalar (1..10) { print"$scalar"."txt"; }
(defun my-indent-block () "reindent surroundig expression" (interactive) (save-excursion (backward-up-list) (cperl-indent-exp) ) ) (defadvice cperl-electric-semi (after my-electric-indent-context) "indent whole context surrounding block/context" (my-indent-block) ) (add-hook 'cperl-mode-hook (lambda () (local-set-key (kbd "M-C-q") 'my-indent-block ) ) )
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.
COMMENTS ?
*) there is align-current for regions, but it's reg-ex based fails with nested structures
my $a = { a => 1, b => { c => 3, } };
but with the semantic information of cperl's parsing it's possible to distinguish between the different levels of =>.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: [emacs] RFC live formating / tidying while I type
by zakame (Pilgrim) on Sep 14, 2016 at 06:22 UTC | |
by LanX (Saint) on Sep 14, 2016 at 10:40 UTC | |
by zakame (Pilgrim) on Sep 14, 2016 at 10:53 UTC | |
by LanX (Saint) on Sep 14, 2016 at 14:10 UTC |