简体   繁体   English

将cperl-mode与emacs中的Perl代码关联

[英]associating cperl-mode with Perl code in emacs

I am trying to associate CPerl mode with Perl source files in emacs (23.1.1 on CentOS 6). 我正在尝试将CPerl模式与emacs中的Perl源文件关联(在CentOS 6上为23.1.1)。

If I include the following line in my .emacs 如果我在.emacs中包含以下行

(defalias 'perl-mode 'cperl-mode)

then CPerl mode will be loaded when a Perl source file is opened. 那么当打开Perl源文件时,将加载CPerl模式。

However, the following line, which seems like ti should work, results in Perl mode being loaded instead: 但是,以下行(似乎应该是ti可以正常工作)导致装入Perl模式:

(add-to-list 'auto-mode-alist '("\\.p[lm]$" . cperl-mode))

There's no error message - it just loads Perl mode instead of CPerl mode. 没有错误消息-它只是加载Perl模式而不是CPerl模式。

The reason I'm asking is that I've had some issues using cperl-set-style (works from the emacs menu but not if I add it as a hook to the CPerl mode when it's been aliased to perl-mode) and I wanted to try loading CPerl mode directly. 我问的原因是我在使用cperl-set-style时遇到了一些问题(可从emacs菜单运行,但如果将其别名为perl-mode时将其添加为CPerl模式的钩子,则不会)想尝试直接加载CPerl模式。

The statement I'm using in my .emacs to set the indenting style as a hook to CPerl mode is 我在.emacs中使用的将缩进样式设置为CPerl模式的钩子的语句是

(eval-after-load "cperl-mode" 
    add-hook 'cperl-mode-hook (lambda() (cperl-set-style 'C++))))

This obviously has no effect if CPerl mode is not loaded (when I use the auto-mode-alist approach) and does not do the right thing (seems to use GNU indent style) when I load CPerl mode by aliasing it to Perl mode. 如果未加载CPerl模式(当我使用auto-mode-alist方法时)并且在通过将其别名为Perl模式加载CPerl模式时未做正确的事情(似乎使用GNU缩进样式),则这显然没有任何效果。

M-: (info "(emacs) Choosing Modes") RET M- :( (info "(emacs) Choosing Modes") RET

Do your perl scripts start with #!/usr/bin/perl ? 您的perl脚本是否以#!/usr/bin/perl开头?

Second, if there is no file variable specifying a major mode, Emacs checks whether the file's contents begin with `#!'. 其次,如果没有文件变量指定主要模式,Emacs将检查文件内容是否以“#!”开头。 If so, that indicates that the file can serve as an executable shell command, which works by running an interpreter named on the file's first line (the rest of the file is used as input to the interpreter). 如果是这样,则表明该文件可以用作可执行的Shell命令,该命令通过运行在文件的第一行上命名的解释器来工作(文件的其余部分用作解释器的输入)。 Therefore, Emacs tries to use the interpreter name to choose a mode. 因此,Emacs尝试使用解释器名称来选择一种模式。 For instance, a file that begins with `#!/usr/bin/perl' is opened in Perl mode. 例如,以Perl模式打开以'#!/ usr / bin / perl'开头的文件。 The variable `interpreter-mode-alist' specifies the correspondence between interpreter program names and major modes. 变量“ interpreter-mode-alist”指定解释器程序名称和主要模式之间的对应关系。

The default is perl-mode of course: 当然默认是perl模式:

ELISP> (assoc "perl" interpreter-mode-alist)
("perl" . perl-mode)

So you would simply use add-to-list again... 因此,您只需再次使用添加到列表...

(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))

You need to use (cperl-set-style "C++") instead of (cperl-set-style 'C++) . 您需要使用(cperl-set-style "C++")而不是(cperl-set-style 'C++) If you look at the variable cperl-style-alist (eg with Ch v ) then you will see that the car 's consist of strings rather than symbols. 如果您查看变量cperl-style-alist (例如Ch v ),则会看到car的组成部分是字符串而不是符号。 It seems unfortunate that your example failed silently rather than raising an error. 不幸的是,您的示例无声地失败了,而不是引发错误。 Most of the time I would want to know that I tried to choose a non-existant style, but there's probably a good reason for it to be the way it is. 大多数时候,我想知道我试图选择一种不存在的样式,但可能有充分的理由使它保持原样。

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

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