roadrider
roadrider

Reputation: 349

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).

If I include the following line in my .emacs

(defalias 'perl-mode 'cperl-mode)

then CPerl mode will be loaded when a Perl source file is opened.

However, the following line, which seems like ti should work, results in Perl mode being loaded instead:

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

There's no error message - it just loads Perl mode instead of CPerl mode.

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.

The statement I'm using in my .emacs to set the indenting style as a hook to CPerl mode is

(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.

Upvotes: 3

Views: 609

Answers (2)

Ivan Andrus
Ivan Andrus

Reputation: 5301

You need to use (cperl-set-style "C++") instead of (cperl-set-style 'C++). If you look at the variable cperl-style-alist (e.g. with C-hv) then you will see that the car's consist of strings rather than symbols. 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.

Upvotes: 2

phils
phils

Reputation: 73344

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

Do your perl scripts start with #!/usr/bin/perl ?

Second, if there is no file variable specifying a major mode, Emacs checks whether the file's contents begin with `#!'. 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). Therefore, Emacs tries to use the interpreter name to choose a mode. For instance, a file that begins with `#!/usr/bin/perl' is opened in Perl mode. The variable `interpreter-mode-alist' specifies the correspondence between interpreter program names and major modes.

The default is perl-mode of course:

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))

Upvotes: 2

Related Questions