Search code examples
perlemacscperl-mode

How do I keep braces from moving in Emacs cperl mode?


I'm using GNU Emacs 22.2.1 and cperl 5.23.

I have perl code like this:

sub foo
{
    if($x)
    {
      print "x";
    }
    else
    {
      print "y";
    }
}

I'd like to reindent the code to a 2-space indent. But when I run cperl-indent-region on this code, I get:

sub foo
  {
    if ($x) {
      print "x";
    } else {
      print "y";
    }
  }
  1. How can I keep the outer brace at the left margin / column 0?
  2. How can I prevent the opening brace for the if and else from moving up to the previous line?

Solution

  • I believe the customization you're looking for is:

    (setq cperl-extra-newline-before-brace t
          cperl-brace-offset              -2
          cperl-merge-trailing-else        nil)
    

    You can customize cperl mode with M-x customize-group <ENTER> cperl <ENTER>. The indentation variables are in the Cperl Indentation Details subgroup.