質問

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.

役に立ちましたか?

解決

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.

他のヒント

Read lunaryorn's answer first. This is just a clarification.

what exactly is broken with your code

(setq paredit-mode-map (make-sparse-keymap))

This will not work with any already loaded mode. paredit is not special.

Paredit's disrespect of defvar does mean that it's very difficult to unbind all keys as you wish.

Why don't you just create your own minor mode? All that Paredit Mode does is provide key bindings, so if you clobber its keymap it does nothing for you. The paredit commands are available whether you use Paredit Mode or not. (Nobody `forces' key bindings on you!)

(defvar snowape-mode-map (make-sparse-keymap))
(define-minor-mode snowape-mode
  "Minor mode for snowape's favorite pareditoid key bindings.
\\<snowape-mode-map>"
  :lighter " Snowape")
(define-key snowape-mode-map (kbd "C-M-<left>") 'paredit-backward)
...

Alternatively, you could just use local-set-key in your favorite mode hooks:

(add-hook 'lisp-mode-hook
  (defun lisp-mode-snowape-setup ()
    (local-set-key (kbd "C-M-<left>") 'paredit-backward)))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top