我可以在 emacs 中自定义三元运算符的缩进吗? cperl 模式?
在 emacs cperl 模式中,三元运算符没有得到特殊处理。如果将它们分成多行,cperl-mode 只是缩进每行,就像缩进任何连续语句一样,如下所示:
$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-mode 版本 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';
}
请注意,在任何大括号之外,我基本上得到了我想要的缩进。但在大括号内,三元运算符缩进得很严重。有解决办法吗?
In emacs cperl-mode, ternary operators are not treated specially. If you break them over multiple lines, cperl-mode simply indents each line the same way it indents any continued statement, like this:
$result = ($foo == $bar) ? 'result1' :
($foo == $baz) ? 'result2' :
($foo == $qux) ? 'result3' :
($foo == $quux) ? 'result4' :
'fail_result';
This is not very readable. Is there some way that I can convince cperl-mode indent like this?
$result = ($foo == $bar) ? 'result1' :
($foo == $baz) ? 'result2' :
($foo == $qux) ? 'result3' :
($foo == $quux) ? 'result4' :
'fail_result';
By the way, code example from this question.
EDIT
There seems to be a bug in cperl-mode's indentation of ternary operators. Take the following example, which was indented using Emacs 23.1.1, cperl-mode version 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';
}
Notice that outside any braces, I basically get the indentation I want. But inside braces, the ternary operator is indented badly. Is there a fix for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用什么版本的 cperl-mode 和 Emacs?在 GNU Emacs 23.1,
cperl-version
5.23 中,没有初始化文件,我得到:如果我希望它们在第一个文件下排列,我会添加一组额外的括号:
我很漂亮确保实现您请求的缩进(将
fail_result
与'result'
字符串对齐)需要对 cperl-mode 的缩进引擎进行一些重要的更改。不过,欢迎您尝试一下。 :-)What version of cperl-mode and Emacs are you using? In GNU Emacs 23.1,
cperl-version
5.23, with no init file, I get:If I want them to line up under the first, I'd add an extra set of parens:
I'm pretty sure that achieving your requested indentation (with
fail_result
lining up with the'result'
strings) would require some non-trivial changes to cperl-mode's indentation engine. You're welcome to try it, though. :-)我不知道 Cperl 模式下的自动缩进,但
M-1 MS-| perltidy
(如果您安装了 Perl::Tidy )将很好地整理标记区域(包括三元语句)。默认情况下,它看起来与您的示例不完全一样,但我相信您可以自定义它以在 .perltidyrc 中执行您想要的操作。顺便说一句,我自己并没有弄清楚这一点 - 我在某个地方读过它 - 我以为是 PBP,但我刚刚检查过 &似乎并非如此,但无论如何我一直在使用它&发现它非常有用。
编辑:它位于 emacs wiki 中的 cperl 页面
I don't know about auto-indentation in Cperl-mode but
M-1 M-S-| perltidy
(if you have Perl::Tidy installed) will tidy a marked region (including ternary statements) nicely. By default it won't look precisely like your example but I believe you can customise it to do what you want in its .perltidyrc.I didn't figure this out myself btw - I read it somewhere - I thought PBP but I've just checked & it doesn't seem to be that, but anyway I use it all the time & find it very useful.
Edit: It was on the cperl page in the emacs wiki