How can I configure so that I can define multiple actions in cperl-mode-hook in .emacs?

141 Views Asked by At

Here is an excerpt of my .emacs

CASE 1: With the config below, perl-completion mode works perfectly.

;;-------------------------------------------------------
;; -*-- CPerl mode
;;-------------------------------------------------------

(defalias 'perl-mode 'cperl-mode)
(add-to-list 'auto-mode-alist '("\\.[apP][Llm]$" . cperl-mode))
(add-to-list 'auto-mode-alist '("\\.al$" . cperl-mode))
(add-to-list 'auto-mode-alist '("\\.t$" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))

(defun my-cperl-hook-func()
  (add-to-list 'load-path "~/.emacs.d/elpa/perl-completion-20090527.2336")
  (require 'perl-completion)
  (perl-completion-mode t)
  ;; (make-local-variable 'compile-command)
  ;;  (setq compile-command
  ;;        (concat "perl " (buffer-file-name)))
  ;;  (cperl-define-key "\C-c\C-c" 'compile)
  )

(add-hook 'cperl-mode-hook 'my-cperl-hook-func)

CASE 2: With the config below, C-c C-c in cperl-mode will successfully launch perl compilation.

;;-------------------------------------------------------
;; -*-- CPerl mode
;;-------------------------------------------------------
(defalias 'perl-mode 'cperl-mode)

(add-to-list 'auto-mode-alist '("\\.[apP][Llm]$" . cperl-mode))
(add-to-list 'auto-mode-alist '("\\.al$" . cperl-mode))
(add-to-list 'auto-mode-alist '("\\.t$" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))

(defun my-cperl-hook-func()
  ;; (add-to-list 'load-path "~/.emacs.d/elpa/perl-completion-20090527.2336")
  ;; (require 'perl-completion)
  ;; (perl-completion-mode t)
  (make-local-variable 'compile-command)
  (setq compile-command
        (concat "perl " (buffer-file-name)))
  (cperl-define-key "\C-c\C-c" 'compile)
  )

(add-hook 'cperl-mode-hook 'my-cperl-hook-func)

CASE 3: However, with the code below to have both of perl-completion and C-c C-c to launch perl compilattion enabled by uncommenting all of lines in hook function (my-cperl-hook-func), it just ends up with that perl-completion would work fine while C-c C-c won't work at all (emacs says C-c C-c is undefined.)

How can I make both of actions valid in cperl-mode-hook?

;;-------------------------------------------------------
;; -*-- CPerl mode
;;-------------------------------------------------------
(defalias 'perl-mode 'cperl-mode)

(add-to-list 'auto-mode-alist '("\\.[apP][Llm]$" . cperl-mode))
(add-to-list 'auto-mode-alist '("\\.al$" . cperl-mode))
(add-to-list 'auto-mode-alist '("\\.t$" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))

(defun my-cperl-hook-func()
  (add-to-list 'load-path "~/.emacs.d/elpa/perl-completion-20090527.2336")
  (require 'perl-completion)
  (perl-completion-mode t)    
  (make-local-variable 'compile-command)
   (setq compile-command
         (concat "perl " (buffer-file-name)))
   (cperl-define-key "\C-c\C-c" 'compile)
  )

(add-hook 'cperl-mode-hook 'my-cperl-hook-func)
1

There are 1 best solutions below

1
On BEST ANSWER

First off, if you are using elpa and the package system to install perl-completion, it will automatically add it to the load path, so the add-to-list load-path is unnecessary. Unfortunately the package does not include an autoload for perl-completion-mode, so the (require 'perl-completion) is necessary.

After examining the perl-completion library at http://www.emacswiki.org/emacs/perl-completion.el, it appears it uses C-c C-c as a prefix. Unfortunately, by using it as a prefix, it is clobbering the ability to bind C-c C-c for compile in perl-mode.

I used the following to test this, and to bind compile to C-c C-c c.

(require 'package)
(add-to-list 'package-archives
  '("melpa" . "http://melpa.milkbox.net/packages/") t)

(package-initialize)

(package-refresh-contents)
(package-install 'anything)
(package-install 'perl-completion)

;;-------------------------------------------------------
;; -*-- CPerl mode
;;-------------------------------------------------------

(defalias 'perl-mode 'cperl-mode)

(add-to-list 'auto-mode-alist '("\\.[apP][Llm]$" . cperl-mode))
(add-to-list 'auto-mode-alist '("\\.al$" . cperl-mode))
(add-to-list 'auto-mode-alist '("\\.t$" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))

(defun my-cperl-hook-func()
  (require 'perl-completion)
  (perl-completion-mode t)
  (make-local-variable 'compile-command)
   (setq compile-command
         (concat "perl " (buffer-file-name)))
   (cperl-define-key "\C-c\C-cc" 'compile)
  )

(add-hook 'cperl-mode-hook 'my-cperl-hook-func)

Saved as cperl-init.el, and run with no other packages using emacs -q -l cperl-init.el.

If you want to use C-c C-c for compile I think you have to unbind it's use in perl-completion. It looks like cperl itself has some compatibility with mode-compile, but not sure if it adds any bindings.