views:

453

answers:

3

The key bindings I've defined in my .emacs file aren't working. Here's the file:

;init modes
(menu-bar-mode 0)
(tool-bar-mode 0)
(cua-mode)
(column-number-mode)
(fset 'perl-mode 'cperl-mode)
(cperl-set-style PerlStyle)

;keymappings
(global-set-key [f12] 'save-buffer)
(global-set-key [S-f12] 'write-file)
(global-set-key [f7] 'ispell)
(global-set-key [up] 'scroll-one-line-up)
(global-set-key [down] 'scroll-one-line-down)

;functions
(defun scroll-one-line-up (&optional arg)
  (interactive "p")
  (scroll-up (or arg 1)))
(defun scroll-one-line-down (&optional arg)
  (interactive "p")
  (scroll-down (or arg 1)))

I know Emacs parses the file since everything else seems to work. It's just that the keys are not being bound.

How can I make it work?

A: 

It is hard to say what your problem might be without more information, like is it all your keybindings or just one or two that do not work. I will hazard a guess that it is the last two ([up] and [down]). In those cases the on-line documentation below seems to indicate that you might be shadowing the global definitions with local ones defined by the mode.

global-set-key is an interactive compiled Lisp function in `subr.el'.

(global-set-key key command)

Give key a global binding as command. command is the command definition to use; usually it is a symbol naming an interactively-callable function. key is a key sequence; noninteractively, it is a string or vector of characters or event types, and non-ASCII characters with codes above 127 (such as ISO Latin-1) can be included if you use a vector.

Note that if key has a local binding in the current buffer, that local binding will continue to shadow any global binding that you make with this function.

pajato0
I don't think that the local mapping is the problem since C-h k <F12>, for example, returns "<F12> is undefined". If by chance it is, however, what do I do about it?
gvkv
You would have to change the binding in the local keymap using a hook variable. Here's an example that I use with java-mode:(defun java-setup () (setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92) indent-tabs-mode nil tab-width 4 fill-column 96 c-comment-start-regexp "\\(@\\|/\\(/\\|[*][*]?\\)\\)"))(add-hook 'java-mode-hook 'java-setup)In your case you would use something like:(defun mysetup () (define-key local-map [f12] 'func))(add-hook 'your-mode-hook 'mysetup)
pajato0
That was a bad idea. I'll redo my comment as another answer to make it clearer.
pajato0
+6  A: 

You have an error in your .emacs at line:

(cperl-set-style PerlStyle)

It should be written as:

(cperl-set-style 'PerlStyle)

Since it raises an error that stops parsing .emacs at that point, your key bindings won't be evaluated.

Török Gábor
Thanks Török, although the problem wasn't quite that. The cperl-set-style function only exists if cperl-mode has been run. So that was the line that is giving me a parsing error. Do you happen to know a good reference on how to run functions conditionally with respect to the mode?
gvkv
@gvkv: You're looking for mode hooks. See http://www.emacswiki.org/emacs/ModeHooks for more info. Something like this should do the trick: (add-hook 'cperl-mode-hook (lambda () (cperl-set-style 'PerlStyle)))
Boojum
Cool. Thanks Boojum.
gvkv
Also, `emacs --debug-init` would help diagnose this.
Joe Casadonte
@Joe: I just tried this and it's been helpful. Thanks
gvkv
+1  A: 

To follow up to my previous answer, you would have to change the binding in the local keymap using a hook variable. Here's an example that I use with java-mode:

(defun java-setup ()
   (setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92)
         indent-tabs-mode nil
         tab-width 4
         fill-column 96
         c-comment-start-regexp "\\(@\\|/\\(/\\|[*][*]?\\)\\)"))

 (add-hook 'java-mode-hook 'java-setup)

In your case you would use something like:

 (defun mysetup ()
    (define-key local-map [f12] 'func))

 (add-hook 'your-mode-hook 'mysetup)

Also, fwiw, I do the following to define my global keys:

(defun function-key-help ()
  (interactive)
  (switch-to-buffer "*Help*")
  (erase-buffer)
  (insert-file (expand-file-name "~/lib/fkeys.help"))
  (message "Type C-x b <nl> to remove help window."))

(define-key global-map [f12] 'function-key-help)

And it works perfectly in my Emacs 23 setup.

pajato0
Thanks for the quickie tutorial. I'm going to try this tonight.
gvkv