in reply to Re^4: prettyfy hashes (emacs)
in thread prettyfy hashes
This is self contained dual code!
Place the cursor behind the last lisp parenthesis and type C-x C-e and the fat commas will be aligned.
$a = { bla => 1, trax => [ #comment bla2 => { #comment sauh1 => 1666, 3 => 2, }, ], bla => 1, _periods => 2 }; my $h_periodsxxxxxxxxx = { Mercury => { orbital => 0.24, rotation => 58.64 }, Venus => { orbal => 0.62, rotation => -243.02 }, Earth => { orbital => 1.00, rotation => 1.00 }, Mars => { orbital => 1.88, rotation => 1.03 }, Jupiter => { orbital => 11.86, rotation => 0.41 }, Saturn => { orbital => 29.46, rotation => 0.43 }, Uranus => { orbital => 84.01, rotation => -0.72 }, Neptune => { orbital => 164.8, rotation => 0.67 }, }; __END__ (lisp-mode) (defun end-perl () "Return position of last Perl character in Buffer" (save-excursion (beginning-of-buffer) (search-forward "\n__END__") (point) ) ) (progn ;; activate Perl mode (cperl-mode) ;; reset example code (replace-regexp "\\s-+" " " nil 0 (end-perl)) ;; indent perl code (indent-region 0 (end-perl)) ;; align fat commas (align 0 ; BEG (end-perl) ; END ;; SEPARATE on starting ]} or ending {[ "\\(^\\s-*$\\|^\\s-*[]}]\\|[[{]\\s-*\\(#\\|$\\)\\)" ;; RULE '( (perl-hash-pair (regexp . "\\(\\s-*\\)=>\\(\\s-*\\)") (group . (1)) (modes . align-perl-modes) (tab-stop . nil) (repeat . t) (justify . t) ) ) ) ;; reactivate lisp mode for experimenting (lisp-mode) )
The result is already almost as good as perltidy just way faster.
It's so fast that it could be bound to each return or semicolon key event to align the current statement
$a = { bla => 1, trax => [ #comment bla2 => { #comment sauh1 => 1666, 3 => 2, }, ], bla => 1, _periods => 2 }; my $h_periodsxxxxxxxxx = { Mercury => { orbital => 0.24, rotation => 58.64 }, Venus => { orbal => 0.62, rotation => -243.02 }, Earth => { orbital => 1.00, rotation => 1.00 }, Mars => { orbital => 1.88, rotation => 1.03 }, Jupiter => { orbital => 11.86, rotation => 0.41 }, Saturn => { orbital => 29.46, rotation => 0.43 }, Uranus => { orbital => 84.01, rotation => -0.72 }, Neptune => { orbital => 164.8, rotation => 0.67 }, };
you can play around with the lisp code to improve the result.
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
FootballPerl is like chess, only without the dice
|
|---|