dgutov / mmm-mode

New official home for mmm-mode, fixed for Emacs >= 23
http://mmm-mode.sourceforge.net/
GNU General Public License v2.0
333 stars 32 forks source link

A clarification on how mmm-mode works #94

Closed some-mthfka closed 5 years ago

some-mthfka commented 5 years ago

I have skimmed the info file, but something there seems missing as to the basic working principles.

According to the info file:

The way MMM Mode works is as follows. Each buffer has a dominant or default major mode, which is chosen as major modes normally are: the user can set it interactively, or it can be chosen automatically with `auto-mode-alist' (@pxref{Choosing Modes, , , emacs, The Emacs Manual}). Within the file, MMM Mode creates submode regions within which other major modes are in effect. While the point is in a submode region, the following changes occur: <...> local keymap <...> modeline <...> major-mode-menu <...> some local variables <...> syntax table and indentation <...> font-lock <...> background color

The submode regions are represented internally by Emacs Lisp objects known as /overlays/.

A lot of the functionality of MMM Mode---that which makes the major mode appear to change---is implemented by saving and restoring the values of local variables, or pseudo-variables.

What I don't understand is (1) where the modes of the submode region run (if ever) (2) and when they are turned on. Are necessary modes just allowed to run at the right time for the whole buffer? But then, how are they limited in their effect to just the necessary region? Narrowing? Or is it that only some very specific parts of a submode are applied (the list of things quoted above)? Do I understand correctly that something like syntax checking for a subregion can't currently be done using mmm-mode?

A clarification could help and this is currently discussed here (if you feel like it, please, feel free to respond there as well).

Thanks!

some-mthfka commented 5 years ago

Nevermind, the answer was just right around the corner

https://github.com/polymode/polymode/issues/187

dgutov commented 5 years ago

where the modes of the submode region run (if ever)

In a temporary, empty buffer, one per submode.

and when they are turned on

When the major mode function runs? After that mmm-mode can be turned on, and it performs all initialization.

Are necessary modes just allowed to run at the right time for the whole buffer?

I guess so (not sure about the exact meaning of your wording).

But then, how are they limited in their effect to just the necessary region? Narrowing?

Mostly narrowing, yes. And also variables like font-lock-dont-widen. And defining mmm-aware syntax-propertize-function, font-lock-fontify-region-function, etc.

Or is it that only some very specific parts of a submode are applied (the list of things quoted above)?

You could say that. The exact lists of variables that are saved per-subregion (or per-submode) are pre-defined. See mmm-save-local-variables in mmm-vars.el.

Do I understand correctly that something like syntax checking for a subregion can't currently be done using mmm-mode?

I would say it can be implemented, and it's not exactly rocket science. New flymake's flymake-diagnostic-functions API can be used similarly to how mmm-fontify-region works: collect all chunks, put them in a temporary buffer in the right submode, and run the diagnostic function for the relevant major mode in that buffer (if it needs a file on disk, guess it'll have to be saved first).

some-mthfka commented 5 years ago

Alright, my picture is now as follows:

When the cursor jumps to a submode region:

On any user interaction:

When the region has to have it's own buffer (such as for syntax checking if it requires a saved file):

Is that right? If so, what are the other uses for a temporary empty buffer?

dgutov commented 5 years ago

Keymap/modeline/... is changed

...because mmm-update-submode-region is called. Because it's on post-command-hook.

On any user interaction: The region is temporarily narrowed to (w/ those other changes applied as you listed, like font-lock-dont-widen)

No. The user doesn't see any narrowing. Narrowing is only done temporarily during the runtime of every major facility (font-lock, indentation, ...).

Is that right?

It should work, yes. We don't have any code doing exactly that, yet.

If so, what are the other uses for a temporary empty buffer?

After the major mode and minor mode functions run, we collect and remember the local variable values they had set in that buffer.

some-mthfka commented 5 years ago

Got it, thanks!