Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I change paredit keybindings

I'm trying to use just a few functions from paredit, without loading all the keybindings. Looking at paredit.el, the only keymap I found was paredit-mode-map, so I tried this.

(setq paredit-mode-map (make-sparse-keymap))
(define-key paredit-mode-map (kbd "<C-M-left>") 'paredit-backward)

It didn't change the keybinding (As checked with C-h k), but the variable paredit-mode-map was changed.

I also tried

(eval-after-load "paredit"
  '(progn
     (setq paredit-mode-map (make-sparse-keymap))
     (define-key paredit-mode-map (kbd "<C-M-left>") 'paredit-backward)))

and then turning paredit on and off, with the same result.

Previously, making changes to a keymap directly has always worked for me. What is going on here?

Edit:

I succeeded in changing the keymap by doing this:

; Remove old paredit bindings
(defun take-from-list (condp list)
  "Returns elements in list satisfying condp"
  (delq nil
    (mapcar (lambda (x) (and (funcall condp x) x)) list)))
(setq minor-mode-map-alist 
      (take-from-list 
        (lambda (x) (not (eq (car x) 'paredit-mode))) 
        minor-mode-map-alist))

; Create new paredit-mode-map
(setq paredit-mode-map (make-sparse-keymap))
(define-key paredit-mode-map (kbd "<C-kp-enter>") 'paredit-backward)

; Add the new paredit-mode-map to minor-mode-map-alist
(setq minor-mode-map-alist (append
                (list (append (list 'paredit-mode) paredit-mode-map))
                minor-mode-map-alist))

So it seems minor-mode-map-alist is a the variable used for lookup. I'm sure there are more elegant ways to change the keybindings, but I wanted to understand more of how keybindings work in emacs.

like image 341
snowape Avatar asked May 17 '13 09:05

snowape


1 Answers

Paredit uses a different way of defining the keymap. Whereas most minor modes define the keymap in the variable definition, Paredit calls paredit-define-keys on top-level, and thus forcibly initializes the keymap.

In other words, you cannot prevent Paredit from setting up its bindings. You need to remove all keybindings in the keymap with (define-key paredit-mode-map … nil) to get rid of these.

Edit: You cannot “reset” keymaps by assigning a new keymap to the variable. (setq paredit-mode-map …) will change the variable paredit-mode-map, it will not change the actual keymap being used by Paredit mode.

The binding of this variable is only evaluated once at definition time, i.e. during the evaluation of define-minor-mode. This macro internally calls add-minor-mode, and passes to this function the current value of the keymap variable. All future use of the mode refers to this keymap only. The keymap variable is never again evaluated by the minor mode, thus changing its binding has no effect whatsoever.

If you want to change the keymap, you have to re-bind the variable before define-minor-mode is evaluated, i.e. before the corresponding library is loaded. Changing it in an eval-after-load form is hence completely useless.

Normally, changing the keymap variable before the library is loaded works well, because most modes define the keymap within the body of defvar. defvar however will not change the value of a variable if it already has a value. Thus, if the variable already has a keymap, it won't be touched.

However, as I said, Paredit doesn't respect this pattern, and instead forcibly add its bindings to the keymap. Thus changing it is pointless, because Paredit will add its bindings anyway.

As I said, you have to manually clear the existing keymap by un-defining every single of its keys.

TL;DR: Use Smartparens, really! It covers all of Paredit, it's flexible, it's powerful, it's extensible, in short it's just good. And it lets you choose whatever keybindings you want.

like image 67
lunaryorn Avatar answered Oct 09 '22 16:10

lunaryorn