简体   繁体   English

Emacs中perl的语法高亮显示已被破坏,是否有修复?

[英]Syntax highlight for perl in Emacs is broken, is there a fix?

I'm a perl programmer and a new emacs user. 我是一个perl程序员和一个新的emacs用户。 I'm under Windows, using cperl-mode for editing perl. 我在Windows下,使用cperl-mode编辑perl。 Emacs version is 24.2.1. Emacs版本是24.2.1。 Here's a screenshot with some sample code: 这是一个包含一些示例代码的屏幕截图:


(source: breqwas.net ) (来源: breqwas.net

  1. Obvious bug: it highlights arrays, hashes and non-quoted literal values in ( a => "b" ) style lists inside comments 明显的错误:它突出了注释中的(a =>“b”)样式列表中的数组,哈希和非引用的文字值
  2. One more bug: it highligts the first word in a regular expression as an array because of non-default quote symbol 还有一个错误:由于非默认引号符号,它将正则表达式中的第一个单词作为数组高举
  3. Not a bug, but looks weird: it highlights arrays and hashes any time when they are used (even in comments, huh), but scalars are highlighted only when declared 这不是一个bug,但看起来很奇怪:它在使用时会突出显示数组和哈希值(即使在注释中,呵呵),但只有在声明时才会突出显示标量
  4. Not a bug, but looks weird: same story with functions: highlighted when declared, not when called 不是错误,但看起来很奇怪:与功能相同的故事:声明时突出显示,而不是在被调用时突出显示
  5. Not a bug, but looks weird: why do "print", "say" and "system" have different colors? 不是一个错误,但看起来很奇怪:为什么“打印”,“说”和“系统”有不同的颜色?

I could keep going, but I hope that explains the word "broken" pretty well. 我可以坚持下去,但我希望能很好地解释“破碎”这个词。

Then I learned the magic Cu Cx = key combination, which shows, among other stuff, the text properties for the text under the cursor, hoping to make a better coloring scheme of my own. 然后我学会了神奇的Cu Cx =键组合,其中显示了光标下文本的文本属性,希望能够制作出更好的自己的着色方案。 At this point it started making even less sense. 在这一点上,它开始变得更没意义了。

"print" has cperl-nonoverridable-face . “print”具有cperl-nonoverridable-face "die" has font-lock-keyword-face . “die”有font-lock-keyword-face Meanwhile, both are functions and can be (and often are) overrided. 同时,两者都是功能,可以(通常是)被覆盖。 "system" has font-lock-type-face . “system”有font-lock-type-face Why? 为什么? Function declarations have font-lock-function-name-face - that's OK, but so do the arguments of "use". 函数声明有font-lock-function-name-face - 这没关系,但“use”的参数也是如此。 Why? 为什么? Function calls and scalars don't have face property at all and can't be highlighted. 函数调用和标量根本没有face属性,无法突出显示。 Why? 为什么? Etc, Etc. Again, I could keep going. 等等,再次,我可以继续前进。

Is there a way to fix all that? 有办法解决所有这些问题吗? Is there some config where I can remap lexical units to some other font-faces, or some other way to make the syntax highlight less crazy? 是否有一些配置,我可以将词法单位重新映射到其他一些字体,或其他一些方法,使语法高亮不那么疯狂?

To avoid the "perl is unparsible" talk, here's a screenshot of the editor I'm migrating from, with the very same code: http://breqwas.net/dropbox/perlsyn_pn.png . 为了避免“perl is notarsarsible”的讨论,这里是我正在迁移的编辑器的截图,使用相同的代码: http//breqwas.net/dropbox/perlsyn_pn.png Looks much more reasonable. 看起来更合理。


Upd: By now (3 weeks after asking this question here) I did not find a cure. 更新:到现在为止 (在此问这个问题后3周)我没有找到治疗方法。 All perl programmers who use emacs I know just ignore these problems. 所有使用emacs的perl程序员我都知道忽略了这些问题。 No better perl highlighters for emacs seem to exist. 似乎不存在更好的用于emacs的perl荧光笔。 Reading cperl-mode docs provides some explanations (the choice of faces for various elements seems less crazy now), but does not provide any answers. 阅读cperl-mode docs提供了一些解释(现在各种元素的面孔选择似乎不那么疯狂),但没有提供任何答案。 I wrote an email to cperl-mode maintainer with these questions and some more digging I did, but got no reply. 我写了一封给cperl-mode maintainer的电子邮件,里面有这些问题,还有一些挖掘我做了,但没有回复。

Yep, you got that right: seems like emacs community was not able to make a good perl syntax highligting in 25 years that perl is around. 是的,你做对了:似乎emacs社区无法在25年内制作一个好的perl语法,perl就是这样。 That's a sad story. 那是一个悲伤的故事。

You might want to try perl-mode instead of cperl-mode . 您可能想尝试perl-mode而不是cperl-mode If you find missing features in it we might be able to port them from cperl-mode without too much trouble. 如果您发现其中缺少功能,我们可以将它们从cperl-mode而不会有太多麻烦。 perl-mode 's highlighting is less gaudy than cperl-mode 's but it does fix some of the problems you point out. perl-mode的突出显示不像cperl-mode那样华丽,但它确实解决了你指出的一些问题。

As for "highlighted when declared, not when called" this is because Emacs usually highlights in this way. 至于“声明时突出显示,而不是在被调用时”这是因为Emacs通常以这种方式突出显示。 I find there is zero benefit to highlighting variable and function uses. 我发现突出变量和函数用途没有任何好处。 I kept the highlighting of variable uses in perl-mode mostly because it was there before and I didn't want to deal with disgruntled users. 我在perl-mode一直强调变量用法,主要是因为它之前存在,我不想处理心怀不满的用户。 Also, in the case of Perl, it's probably easier to highlight variable declarations and uses identically as does perl-mode whereas cperl-mode has to do extra work to distinguish those two cases. 此外,在Perl的情况下,突出变量声明和使用与perl-mode相同可能更容易,而cperl-mode必须做额外的工作来区分这两种情况。

Another syntax highlighting snafu to watch out for with Emacs cperl-mode relates to POD comments used to comment out blocks of code. 使用Emacs cperl-mode强调要注意的另一种语法与用于注释掉代码块的POD注释有关。 Here's a screenshot to demo: 这是一个演示截图:

cperl-mode语法突出显示错误演示

It's VERY easy to miss commented out code that uses POD directives without blank line padding as in my 'not recommended' example above. 很容易错过注释掉使用POD指令而没有空行填充的代码,如上面的“不推荐”示例中所示。 It just does not hit your eye as a comment when viewing it in Emacs, especially if the commented block is large and spans multiple screen pages so that the '=cut' lines aren't even in view. 在Emacs中查看它时,它只是不会引起注意,特别是如果注释块很大并且跨越多个屏幕页面,那么“= cut”行甚至不在视图中。

It's worth noting that vim 's Perl mode correctly highlights the POD without requiring blank line padding per the POD spec, though I would caution vim users to avoid relying on that feature and use blank line padding and '=begin comment ...=end comment' directives as shown above and as recommended . 值得注意的是, vim的Perl模式正确地突出了POD而不需要每个POD规范的空白行填充,但我会提醒vim用户避免依赖该功能并使用空行填充和'= begin comment ... = end评论'指令如上所示和推荐

It would be nice if Perl had a simpler multi-line comment syntax a la '/* ... */' rather than the POD mechanism that is subject to abuse. 如果Perl有一个更简单的多行注释语法la'/ * ... * /'而不是可能被滥用的POD机制,那将是很好的。 Perhaps we'll get this in Perl 6 some day? 也许我们有一天会在Perl 6中得到这个?

Another way do multi-line comments in Emacs (for any language) is via the 'string-rectangle' built-in: Cx rt then type '# '. 另一种方法是在Emacs(对于任何语言)中通过内置的“字符串矩形”进行多行注释:Cx rt然后键入“#”。 To uncomment a block, either do it manually or write a simple macro. 要取消注释块,可以手动执行,也可以编写一个简单的宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM