简体   繁体   中英

Derive from Lisp Mode, ignoring its hooks?

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?

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.

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.

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." ...) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM