1

I wrote a new major mode config-general-mode, which is derived from conf-mode. I like it a lot and want to have it as the default mode for all configuration files. Thus I tried:

(defalias 'conf-mode 'config-general-mode)

But this doesn't work, I get the error message:

Lisp nesting exceeds `max-lisp-eval-depth'

Of course I could (and probably will) just associate *.cfg and *.conf with my mode, but I am curious, if such an alias can be made possible somehow.

  • Does adding #'config-general-mode to conf-mode-hook also incur infinite recursion? (I imagine it would, based on the description by phils.) – Basil Jun 26 '17 at 22:24
1

The nature of a derived mode is that its parent is called before the body of the child mode is evaluated1.

Therefore if the child becomes its own parent (or grandparent, etc...), that causes infinite recursion.

You might attempt to hack your way around this, but I would firmly suggest that you just "don't do that".

1 For more details on how derived modes work, see "Derived modes, and mode hooks" in https://stackoverflow.com/a/19295380

| improve this answer | |
1

Here's how you can do what you intended, while avoiding the inf-loop:

(defalias 'old-conf-mode (symbol-function 'conf-mode))
(define-derived-mode conf-mode old-conf-mode ...)

Note that this is still pretty risky: e.g. it won't work right if conf-mode is marked for autoloading when the above code is executed, and it can lead to inf-loops if you re-execute that code a second time.

A safer alternative would be to use advice-add to modify conf-mode rather than defining a separate conf-general-mode. But it'd still have some quirks (e.g. running conf-mode-hook too early).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.