Why don't these prog-mode commands work in emacs-lisp-mode?

I have a couple of lines in my Emacs setup:

;; swap defaults (define-key prog-mode-map (kbd "RET") 'newline-and-indent) (define-key prog-mode-map (kbd "Cj") 'newline) 

This works as expected in several other programming modes that I tried. But in Emacs mode, Lisp RET is still tied to newline , and Cj is still tied to newline-and-indent . I still observed this confusing behavior even after I moved the key binding code to the very beginning of my Emacs initialization. If I create separate binding operators for Emacs Lisp mode, I have no problem.

 ;; swap defaults for most programming modes (define-key prog-mode-map (kbd "RET") 'newline-and-indent) (define-key prog-mode-map (kbd "Cj") 'newline) ;; swap defaults in Emacs Lisp mode too (define-key emacs-lisp-mode-map (kbd "RET") 'newline-and-indent) (define-key emacs-lisp-mode-map (kbd "Cj") 'newline) 

Why is this? If that matters, I'm using Emacs 24.3 for OS X 10.8.3.

PS I recently learned about electric-indent-mode , which probably performs something very similar to these key bindings. However, the mystery is still standing.

+4
source share
1 answer

Take a look at the definition of emacs-lisp-mode-map in lisp-modes.el :

 (defvar emacs-lisp-mode-map (let ((map (make-sparse-keymap "Emacs-Lisp")) (menu-map (make-sparse-keymap "Emacs-Lisp")) (lint-map (make-sparse-keymap)) (prof-map (make-sparse-keymap)) (tracing-map (make-sparse-keymap))) (set-keymap-parent map lisp-mode-shared-map) … map)) 

The key is to call set-keymap-parent . Although Emacs Lisp mode is inherited from Prog mode, its layout is not inherited from prog-mode-map , but from another keyboard layout defined in lisp-modes.el :

 (defvar lisp-mode-shared-map (let ((map (make-sparse-keymap))) (define-key map "\e\Cq" 'indent-sexp) (define-key map "\177" 'backward-delete-char-untabify) map) "Keymap for commands shared by all sorts of Lisp modes.") 

This map also does not inherit from prog-mode-map , so the bindings in prog-mode-map really have no effect in Emacs Lisp Mode.

This may be a bug in Emacs.

Update: I wrote to the mailing list .

Update 2: Corresponding Error Report

Update 3: Bug fixed. In the current snapshot creation, your key bindings should work as expected. To work with earlier versions of Emacs, you can use the following snippet in init.el :

 (unless (keymap-parent lisp-mode-shared-map) (set-keymap-parent lisp-mode-shared-map prog-mode-map)) 

Now lisp-mode-shared-map inherits from prog-mode-map , effectively replicating bug fixes.

+9
source

Source: https://habr.com/ru/post/1483231/


All Articles