3

So I've created a major mode for a custom lisp by deriving from the standard Lisp Mode. However, when emacs enters it, it automatically activates slime-mode as well, which overrides most of my bindings. As far as I can tell, this happens because SLIME registers some hooks with Lisp Mode and my mode triggers them as well, but I'm not sure. Is there a way to avoid this?

user3026691
  • 497
  • 2
  • 11

3 Answers3

3

If you're using define-derived-mode then the body and mode hook for your parent mode are going to run.

Refer to https://stackoverflow.com/a/19295380 for details.

If you're enabling slime-mode in lisp-mode-hook, and your new mode is deriving from lisp-mode, then the simplest thing would be to disable slime-mode again in the mode hook for your derived mode.


Edit: Actually I believe you could prevent the mode hooks for the ancestor modes from running by manipulating delayed-mode-hooks in the body of your mode.

(You can't prevent the bodies of the ancestor modes from running.)

I recommend that you don't do this, though. I think if you find yourself wanting to mess with the derived mode mechanisms (especially if you're planning to share the code), then you shouldn't be using a derived mode at all.

You should probably take a cue from the implementations of lisp-mode vs emacs-lisp-mode. Rather than one being derived from the other, they are distinct modes (each derived from prog-mode). Their (independent) keymaps have a shared parent keymap, however, meaning that a lot of keybindings do the same things.

I suggest using that code as a template for creating a new lisp-ish major mode.

Community
  • 1
  • 1
phils
  • 71,335
  • 11
  • 153
  • 198
  • Well, that solves one specific instance of the problem that I happened to run into, but somebody else may have different Lisp Mode hooks that would break something again... Is there a more general solution to prevent this, other than duplicating Lisp Mode instead of deriving from it? – user3026691 Mar 28 '16 at 21:07
  • No, the whole point of derived modes is that they build on top of one another, and all the things which happen for `parent-mode` also happen for `child-mode`. You certainly shouldn't be looking for ways to stop *other people's* `lisp-mode` functionality from working in this scenario. – phils Mar 28 '16 at 21:53
  • If you macroexpand your call to `derived-minor-mode`, you'll see the code which is actually generated. You could always use that as a basis for modification, if you still think that deriving from `lisp-mode` makes partial sense. – phils Mar 28 '16 at 21:56
  • Frankly, I don't see why I shouldn't stop other people's lisp-mode functionality from working in a mode for a completely different language that just happens to use S-expressions. It being derived from lisp-mode is meant to be an implementation detail... However, since there seems to be no clean workaround, I'll just do as you suggest. – user3026691 Mar 28 '16 at 23:20
1

The right way is to inherit from a parent of lisp-mode. Ideally, there should be a parent lispish-mode used by all Lisp-like major modes, but there's currently no such thing, so you'll have to use prog-mode and then manually setup/copy the things you want from lisp-mode.

I'd welcome a patch which adds a lispish-mode (even better if it comes with a better name), but so far everytime I looked at it I ended up finding that there's too much variation between Lispish modes for there to be much to be shared.

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • The question though comes up not only for lisp-mode, e.g. [this one](https://emacs.stackexchange.com/questions/32414/prevent-hooks-from-parent-mode-in-derived-mode) is for text mode; and I locally stumbled upon it because of vala-mode derived from csharp-mode. I wonder if correct solution would rather be adding a function `add-mode-hook` that essentially gonna work the same as `add-hook`, except there would be additional parameter for the hook to not get triggered for derived modes. – Hi-Angel Apr 14 '19 at 07:57
0

I can see two ways to go about attacking the problem

  1. Change the hook function that is added to lisp-mode-hook so that it tests (eq major-mode 'lisp-mode) and is hence not executed in derived modes.

  2. disable lisp-mode-hook while running your parent:

    (defun my-tamed-lisp-mode ()
      (let ((lisp-mode-hook nil))
        (lisp-mode)))
    
    (define-derived-mode my-custom-lisp-mode my-tamed-lisp-mode "CustomLisp"
      "Major mode for my custom Lisp."
      ...)
    
Stefan
  • 27,908
  • 4
  • 53
  • 82