我可以自定义emacs';中三元运算符的缩进吗;cperl模式?

我可以自定义emacs';中三元运算符的缩进吗;cperl模式?,perl,emacs,indentation,ternary-operator,cperl-mode,Perl,Emacs,Indentation,Ternary Operator,Cperl Mode,在emacs cperl模式中,三元运算符不作特殊处理。如果将它们拆分为多行,cperl模式将以缩进任何连续语句的相同方式缩进每行,如下所示: $result = ($foo == $bar) ? 'result1' : ($foo == $baz) ? 'result2' : ($foo == $qux) ? 'result3' : ($foo == $quux) ? 'result4' : 'fail_r

在emacs cperl模式中,三元运算符不作特殊处理。如果将它们拆分为多行,cperl模式将以缩进任何连续语句的相同方式缩进每行,如下所示:

$result = ($foo == $bar)  ? 'result1' :
    ($foo == $baz)  ? 'result2' :
        ($foo == $qux)  ? 'result3' :
            ($foo == $quux) ? 'result4' : 
                'fail_result';
这不是很可读。有什么方法可以让我像这样说服cperl模式缩进吗

$result = ($foo == $bar)  ? 'result1' :
          ($foo == $baz)  ? 'result2' :
          ($foo == $qux)  ? 'result3' :
          ($foo == $quux) ? 'result4' : 
                            'fail_result';
顺便说一下,代码示例来自

编辑 cperl模式对三元运算符的缩进似乎有一个缺陷。以使用Emacs 23.1.1、cperl模式版本5.23缩进的以下示例为例:

my $result = ($foo == $bar)  ? 'result1' :
  ($foo == $baz)  ? 'result2' :
  ($foo == $qux)  ? 'result3' :
  ($foo == $quux) ? 'result4' :
  'fail_result';

{
  my $result = ($foo == $bar)  ? 'result1' :
    ($foo == $baz)  ? 'result2' :
      ($foo == $qux)  ? 'result3' :
        ($foo == $quux) ? 'result4' :
          'fail_result';
}

注意,在任何大括号之外,我基本上得到了我想要的缩进。但在大括号内,三元运算符缩进严重。有解决方法吗?

我不知道Cperl模式下的自动缩进,但是
M-1 M-S-| perltidy
(如果您已安装)将很好地整理标记区域(包括三元语句)。默认情况下,它看起来并不完全像您的示例,但我相信您可以对其进行定制,以在其.perltidyrc中实现您想要的功能

顺便说一句,我自己并没有弄明白这一点——我在某个地方读过它——我以为是PBP,但我刚刚检查过&似乎不是那样,但无论如何我一直在使用它&发现它非常有用


编辑:它位于

您使用的是什么版本的cperl模式和Emacs?在GNU Emacs 23.1中,
cperl版本
5.23,没有初始化文件,我得到:

$result = ($foo == $bar)  ? 'result1' :
  ($foo == $baz)  ? 'result2' :
  ($foo == $qux)  ? 'result3' :
  ($foo == $quux) ? 'result4' :
  fail_result;
如果我想让他们排在第一排,我会添加一组额外的排列:

$result = (($foo == $bar)  ? 'result1' :
           ($foo == $baz)  ? 'result2' :
           ($foo == $qux)  ? 'result3' :
           ($foo == $quux) ? 'result4' :
           fail_result);

我很确定,要实现您请求的缩进(使用
fail\u result
'result'
字符串对齐),需要对cperl模式的缩进引擎进行一些非常重要的更改。不过,欢迎您尝试一下。:-)

+1问得好,我自己也一直在想这个问题。说得清楚一点,我希望能够使用emacs的内置缩进功能来实现这一点,这样使用TAB做正确的事情。但是如果做不到这一点,其他的解决方案是受欢迎的。我想我在cperl模式的缩进中发现了一个bug。我会用细节编辑我的问题。等一下,一组额外的家长不会强制列表上下文吗@Ryan Thompson:不。要获取列表上下文,您必须将paren放在
$result
周围,而不是指定给
$result
的表达式周围。我意识到赋值将发生在标量上下文中,但是额外的一对paren仍然可能最终创建一个单元素列表,从而将数字1分配给
$result
@Ryan Thompson:No。赋值运算符右侧的paren不会创建列表上下文<代码>$x=((func))完全等同于
$x=func。要获取列表上下文,请将参数放在赋值的左侧:
($x)=func在列表上下文中调用
func