20

Q: Why does lisp-interaction-mode exist, and are there any reasons to use it instead of emacs-lisp-mode?

The manual states that emacs-lisp-mode and lisp-interaction-mode are identical except that the latter binds C-j to eval-print-last-sexp. Beyond that, "all other commands in Lisp Interaction mode are the same as in Emacs Lisp mode." As far as I can tell, only the *scratch* buffer uses the latter mode.

It strikes me as odd that there is an entire mode that differs from another by only a single keybinding, so I presume I'm missing either some history or context.

So:

The motivation for this question is that, right now, I'm binding keys twice (in the two modes) so that my *scratch* buffer behaves like buffers visiting *.el files. If there's no practical reason to keep lisp-interaction-mode around, I'll just (setq initial-major-mode 'emacs-lisp-mode) and be done with it.

Dan
  • 32,584
  • 6
  • 98
  • 168

4 Answers4

15

A new derived mode is cheap: lisp-interaction-mode inherits from emacs-lisp-mode, its implementation is just a dozen lines of code or so. It differs from emacs-lisp-mode in just the following ways:

  • it has a different name;
  • it has a different keymap;
  • it has a different syntax table;
  • it has an additional hook.

On the other hand, it shares its abbrev table with emacs-lisp-mode.

Edit: as noted by @phils in his answer (which see), the keymaps of emacs-lisp-mode and lisp-interaction-mode share a common parent, lisp-mode-shared-map. There is therefore no reason to duplicate keybindings — just define them in lisp-mode-shared-map, and they will apply to both modes (and lisp-mode too, but that's probably fine).

Would there be any unexpected consequences to changing the *scratch* buffer's mode to emacs-lisp-mode?

The most obvious consequence would be that lisp-interaction-mode-hook would no longer be run in the *scratch* buffer.

jch
  • 5,680
  • 22
  • 39
  • 3
    It has an *additional* hook. `emacs-lisp-mode-hook` runs for `lisp-interaction-mode` because [that's how derived modes work](http://stackoverflow.com/questions/19280851/how-to-keep-dir-local-variables-when-switching-major-modes/19295380#19295380). It *does* have a different keymap, but both elisp modes share the same parent keymap (`lisp-mode-shared-map`). It does have a separate syntax table, but it's identical to that of its parent mode (because it defers to the parent for setting it). – phils Nov 23 '14 at 21:13
  • Darn, you're right. Hopefully correct now. – jch Nov 23 '14 at 21:50
14

Unless you hate the C-j behaviour (and I'm sure most elisp authors find it handy), just keep things the way they are.

Define your keys for lisp-mode-shared-map instead of duplicating them for the mode-specific keymaps.

All of lisp-mode-map, emacs-lisp-mode-map, and lisp-interaction-mode-map have lisp-mode-shared-map as their parent keymap.

phils
  • 48,657
  • 3
  • 76
  • 115
4

FWIW, I use emacs-lisp-mode in the *scratch* buffer myself. If I wish to evaluate something, I just do C-x C-e, with a C-u prefix when needed. I see no downside to this practice.

As to why the mode is there, it is just a few lines of lisp code in elisp-mode.el, and it's been there like forever, so removing it seems pointless.

Harald Hanche-Olsen
  • 2,401
  • 12
  • 15
  • I started doing this myself ages ago because I wanted `C-j` bound to `newline-and-indent`, but these days, as indentation happens more automatically, this is not a serious concern anymore. So if I hadn't already made this change long ago, I wouldn't bother with it now. – Harald Hanche-Olsen Nov 23 '14 at 15:59
  • Me too, for what it's worth - have done so for a long time. Or I use a throwaway `*.el` file buffer. – Drew Nov 23 '14 at 16:58
2

Besides the differences mentioned in the other answers, there is a small but major difference between 'lisp-interaction-mode' and 'emacs-lisp-mode', which is that in 'lisp-interaction-mode' lexical binding is activated by default (see the documention here).

So for the scratch buffer, using 'emacs-lisp-mode' does not automatically provide lexical binding (which may lead to some, 'unexpected' behavior). And, therefore, maybe (I don't see any reason why not), if you are not using auto-insert-mode, it might be wise to use 'lisp-interaction-mode' in .el buffers also (or to be 'extra' aware that when you just create a new .el buffer, lexical binding, by default, is not activated automatically).

The documentation you are linking to mentions that all other commands are identical (not all other 'aspects' of the modes). B.t.w. I do think that the difference regarding lexical binding should be mentioned (again) in that section of the docs also.

dalanicolai
  • 6,108
  • 7
  • 23