Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jplindstrom (594)

jplindstrom
  (email not shown publicly)

Journal of jplindstrom (594)

Monday January 07, 2008
11:31 AM

Emacs: reformat parameter lists

[ #35321 ]

So, this is a line-wrappingly long line.

my $link = API::Link->new({rel => 'meta:version', href => $uri->as_string, type => 'application/xml'}),

If you think that's not a long line, imagine a really long line with a lot of parameters for the sake of argument.

One param per line is much nicer in this case.

my $link = API::Link->new({
    rel => 'meta:version',
    href => $uri->as_string,
    type => 'application/xml',
}),

But it is a bit of a hassle to reformat by hand. Manual drudgery which easily makes me lose focus on why I just just wrote that method call. Now, why did I create that $link again?

Here is some nice elisp I wrote to reformat parameter lists either into a single line, into multiple lines, or toggle between the two.

So just put the cursor somewhere inside the parameter list and hit C-o m p (or whatever key binding strikes your fancy, but this one will work well with out-of-the-box PerlySense conventions) and it will reformat things into the other layout.

Once you have the nice multi-line layout, you can also align the parameters if that makes things look more sensible. Hit C-o m a et voila:

my $link = API::Link->new({
    rel  => 'meta:version',
    href => $uri->as_string,
    type => 'application/xml',
}),

Well, not that much difference with those parameter names, but I can't be arsed. You get the picture.

Enjoy!

(Now I'm just waiting for someone to tell me I reinvented the wheel. I just couldn't find any.)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Perl::Tidy is your friend! :)

    I don't manually format any code any more.

    • And you can make it do this, how?

      • Well Perl::Tidy turns this:

        my $blodrfrf= $c->model('Fdsfkjsdlfkajd')->find({ id => $id, otehrs => $sdfkjsd });

        into this:

        my $blodrfrf= $c->model('Fdsfkjsdlfkajd')
                ->find({ id => $id, otehrs => $sdfkjsd });

        instead of:

        my $blodrfrf= $c->model('Fdsfkjsdlfkajd')->find({
            id => $id,
            otehrs => $sdfkjsd,
        });

        I'd appreciate any config hints you have, I just started and am currently trying to set it up to match our current c

      • I normally hit Command-Shift-H :P

        My .perltidyrc doesn't cuddle the ( and {, } and ) but I think I recall an option that covers that.

        Here's what that code looks like for me:

        my $link = API::Link->new(
            {
                rel  => 'meta:version',
                href => $uri->as_string,
                type => 'application/xml'
            }
        );

  • cperl-mode handles most of this, but its implementation is sort of broken.  I guess I will be fixing that sometime soon.

    One nit on your elisp code... the style like this:

    <code>
    (foo
      (bar
        baz
      )
    )
    </code>

    Looks really weird.  All the close parens should be on the same line:

    <code>
    (foo
      (bar
        baz))
    </code>

    This is the "official" emacs lisp standard, and I think most other Lisps like the same sort of formatting.