Emacs cperl indentation

jplindstrom on 2004-08-09T08:56:04

Having started using Emacs for Perl programming, I'm thoroughly confused by the indentation settings and how they work together. Two things still bother me.

First is the continued line indentation. Consider this code:

if($install) {
    $oB->fileExistsOrDie($prog);
    $oB->saySystemOrDie("Installing", 3,
                        "$prog install", "Errors during installation");
}
What I really want is for the continued line to be indented two tab stops. It should _not_ be aligned with the opening paren on the line above, because that changes with each method name length... This is what I want:
if($install) {
    $oB->fileExistsOrDie($prog);
    $oB->saySystemOrDie("Installing", 3,
            "$prog install", "Errors during installation");
}


The second weirdness seems related. I can understand that cperl gets confused, but I'd still would like it to work.

The problem is that I have a layout habit I find useful. I write methods like this:
sub systemOrDie { my $self = shift;
    my ($command, $dieMessage) = @_;
    ...
Having the $self on the same line as the sub name is a nice visual cue that this is a method. However, Emacs insists that if I do that, the proper indentation is:
sub systemOrDie { my $self = shift;
                  my ($command, $dieMessage) = @_;
                  ...
and that doesn't really do it for me.

Now, I think these two has something to do with cperl-continued-statement-offset and/or cperl-continued-brace-offset, but I can't seem to get it working.

Any hints?


my .emacs settings

slanning on 2004-08-09T09:21:35

First, make sure that you have the latest cperl-mode. A lot of distributions don't include the latest one. Your file cperl-mode.el should be at least version 4.32. Find it on CPAN (I think). That fixes some problems with indentation.

My indenting style is probably different than yours, but this from my ~/.emacs might at least give you some ideas:

(custom-set-variables
   '(cperl-label-offset 0)
   '(cperl-indent-level 4)
   '(cperl-continued-statement-offset 2)
   '(cperl-tab-always-indent t)
   '(indent-tabs-mode nil))
   ;; let hashes indent normally
   '(cperl-indent-parens-as-block t)
   '(cperl-close-paren-offset -4)

The only problem I have with that is that it indents every continued line by two from each previous line, rather than indenting all of them by two from the first line.

cperl-mode 5

Dom2 on 2004-08-09T09:26:12

First, make sure you've got cperl-mode 5.0. Then, have a look at the cperl-indent-parens-as-block setting. That'll fix the first one, but I don't know how to do the second.

-Dom

Re:cperl-mode 5

zatoichi on 2004-08-09T12:11:26

I tried 5.0 but it threw errors (XEmacs) and I had to back peddle to the version that shipped with XEmacs.

perltidy

perrin on 2004-08-09T14:40:36

You could sidestep the issue and use perltidy on your code. It tends to be smarter than cperl-mode on the whole, and is very configurable.