Skip to content

Decouple utop.el from tuareg-mode#511

Open
bbatsov wants to merge 7 commits intoocaml-community:masterfrom
bbatsov:decouple-from-tuareg
Open

Decouple utop.el from tuareg-mode#511
bbatsov wants to merge 7 commits intoocaml-community:masterfrom
bbatsov:decouple-from-tuareg

Conversation

@bbatsov
Copy link
Contributor

@bbatsov bbatsov commented Mar 6, 2026

I'm the author of neocaml (a new OCaml major mode for Emacs) and I wanted to make sure it plays nicely with utop.el. Currently utop.el has a hard dependency on tuareg, which makes it difficult for other OCaml major modes to integrate with it.

This PR decouples utop.el from tuareg by:

  • Replacing the hard (require 'tuareg) with lazy loading — tuareg is only loaded when its functions are actually needed (i.e., when the user is in tuareg-mode)
  • Introducing utop-mode-compat-alist — an extensible alist that maps major modes to their phrase navigation functions, replacing the old hard-coded positional dispatch
  • Walking the derived-mode-parent chain in the compat lookup, so modes derived from a known mode work automatically
  • Updating the README to document the new registration mechanism

With this change any OCaml major mode can register itself with a simple:

(add-to-list 'utop-mode-compat-alist
             '(my-ocaml-mode . (my-next-phrase . my-discover-phrase)))

Existing tuareg/caml/typerex/reason users are unaffected — their modes are pre-registered in the alist and tuareg is loaded lazily on first use.

bbatsov added 4 commits March 6, 2026 13:45
tuareg is now loaded on demand only when its functions are actually
needed (i.e., when the user is in tuareg-mode). This removes the
mandatory (require 'tuareg) and the tuareg entry from Package-Requires,
allowing utop.el to work without tuareg installed.
Introduce `utop-mode-compat-alist` that maps major modes to their
phrase navigation functions. This replaces the old positional-index
dispatch and makes it easy for any OCaml major mode (e.g. neocaml)
to register itself without modifying utop.el.
Walk the derived-mode-parent chain when looking up phrase functions,
so modes that derive from a known mode (e.g. a mode derived from
tuareg-mode) work automatically without explicit registration.
Document `utop-mode-compat-alist` as the way to add support for
new major modes, mention derived mode support, and list neocaml
alongside the other supported modes.
@bbatsov bbatsov requested a review from rgrinberg as a code owner March 6, 2026 13:05
README.md Outdated
sensitive completion, colors, and more.

It integrates with the Tuareg, caml, ReasonML and typerex modes in Emacs.
It integrates with the Tuareg, caml, neocaml, ReasonML and typerex modes in Emacs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read the code correctly, the integration with neocaml is done on the neocaml side, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly. I could have added it here, but I thought it's probably better to just provide it in neocaml. IMO that's the best for every major mode, but I didn't want to make changes that might break someone's setup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might then be slightly clearer to update the phrasing to

The following Emacs modes are explicitly supported: Tuareg, caml, neocaml, ReasonML and typerex .

to not give the impression that there is explicit integration for neocaml inside utop.

(Because I believe that this important for a community maintained project to be very explicit in term of the state of the code.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I'll think about some updated wording for this.

bbatsov added 3 commits March 6, 2026 15:38
Plist entries like `:next-phrase` and `:discover-phrase` are more
readable and easier to extend with new properties in the future
than positional cons pairs.
Don't list neocaml alongside the built-in modes — instead mention it
as an example of a third-party mode that integrates via the new
utop-mode-compat-alist mechanism.
@bbatsov
Copy link
Contributor Author

bbatsov commented Mar 6, 2026

I've addressed all feedback. There are a few other unrelated improvements I have in mind, but I'll wait for this PR to get merged first before continuing with additional changes.

@Octachron
Copy link
Member

I am planning to merge in the beginning of next week to give time for eventual reactions.

@bbatsov
Copy link
Contributor Author

bbatsov commented Mar 6, 2026

Thanks for letting me know. No rush!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants