Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Infinite Nesting #113

Open
charJe opened this issue Aug 22, 2020 · 9 comments
Open

Feature Request: Infinite Nesting #113

charJe opened this issue Aug 22, 2020 · 9 comments

Comments

@charJe
Copy link

charJe commented Aug 22, 2020

Sorry for all the edits, I accidentally pressed enter while typing the title.

This is similar to issue #102, but instead of going two levels, I need to go any number of levels deep.

Here is the sample code:
Screenshot from 2020-08-21 23-00-45
As you can see the html where is cursor is is lisp-mode, while it should be html-mode.

Here is is as text:

(write-html 
   <html>
     <head>
       <link rel="stylesheet" href="/site.css" />
       <title>,(progn title) - New Poll</title>
     </head>
     <body>
       <header>
         <h1 id="title">,(progn title)</h1>
       </header>
       <form method="POST" action="/new-poll">
         ,@(when (null names)
             (list <input-name index="0" name="" />
                   <input-name index="1" name="" />
                   <input-name index="2" name="" />))
         ,@(mapcar (lambda (name i)
                     <input-name index=i name=name />)
                   names (enumerate names))
         ,(when (string= action "add")
            <input-name index=(length names) name="" />)
         ,(when errors <ul class="error"> ,@errors </ul>)
            <button name="action" type="submit" value="add">Add</button>
            <button name="action" type="submit" value="submit">Submit</button>
       </form>
       <hr />
       <footer>
       </footer>
     </body>
   </html>)

Here is my MMM config:

(require 'sgml-mode)
(require 'lisp-mode)
(require 'mmm-mode)

;; add preferences for relevant modes
(mmm-add-to-major-mode-preferences 'lisp 'lisp-mode t)
(mmm-add-to-major-mode-preferences 'html 'html-mode t)

(defun html-back-matcher ()
  (forward-char -2)
  (with-syntax-table html-mode-syntax-table
    (sgml-skip-tag-forward 1))
  0)

(defun lisp-back-matcher ()
  (forward-char -1)
  (with-syntax-table lisp-mode-syntax-table
    (forward-sexp))
  0)

(mmm-add-classes
 '((mmm-lisp
    :submode lisp
    :face mmm-declaration-submode-face
    :front ",(\\|,@\\|=("
    :include-front t
    :back "."
    :back-offset (lisp-back-matcher))))
(mmm-add-mode-ext-class 'lisp-mode nil 'mmm-lisp)

(mmm-add-classes
 '((mmm-html
    :submode html
    :face mmm-declaration-submode-face
    :front "\\(?1:<\\)[^=[:space:]]"
    :front-match 1
    :include-front t
    :back "."
    :back-offset (html-back-matcher))))
(mmm-add-mode-ext-class 'lisp-mode nil 'mmm-html)

Basically, what I would like is for the mmm-modes to be applied recursively until there isn't another one found.

@charJe charJe changed the title Feature Feature Request Aug 22, 2020
@charJe charJe changed the title Feature Request Feature Request: Infinite Nesting Aug 22, 2020
@dgutov
Copy link
Owner

dgutov commented Aug 24, 2020

Hi!

This is indeed not something currently supported.

If you have spare time to dig in and implement this, I'd review a PR. The performance hit would have to be minimal, or this would have to be preffed out by default, of course.

@charJe
Copy link
Author

charJe commented Aug 24, 2020

Do you have any idea where in the source I would start looking to implement it?

@dgutov
Copy link
Owner

dgutov commented Aug 24, 2020

mmm-apply-classes might be the place to start.

@charJe
Copy link
Author

charJe commented Aug 26, 2020

Thanks for the tip. I have made some progress: commit. But I'm stuck on some stubborn parenthesis; I thought I could use some help.
Screenshot from 2020-08-26 00-36-14
See how the parenthesis under the cursor is in html-mode while it should be in lisp-mode? I've tried debugging html-back-matcher and lisp-back-matcher, but they appear to be stopping in the correct locations. I have also tried changing html-back-matcher and lisp-back-matcher to return -1, but it makes no difference.

@dgutov
Copy link
Owner

dgutov commented Aug 26, 2020

Is it parsed as part of the delimiters? As you can see in mmm-make-region, delimiters are by default left in "primary" mode.

@charJe
Copy link
Author

charJe commented Aug 26, 2020

Upon further inspection, I have determined that the mode regions are being applied correctly. The red parenthesis are due to rainbow-delimiters-mode not using widen. So think this issue is solved. Should I just make a pull request with that commit?

@dgutov
Copy link
Owner

dgutov commented Aug 26, 2020

I'm happy it's working for you, but always running the loop 10 times will result in a definite slowdown.

I'd prefer to see some smarter solution. At least one that checks whether the last iteration changed something.

BTW, have you ever tried https://github.com/polymode/polymode/? AFAIK it has a couple more advanced features. It's possible that it supports this feature out-of-the-box.

@charJe
Copy link
Author

charJe commented Aug 26, 2020

I actually did try polymode, but it is more for markup, and the documentation is more sparse than mmm. Mmm seems to be more for code and is already working better than polymode ever did for this same purpose.

About my commit, it only runs 10 times as a maximum. Line 109 will exit the loop if no more sub-modes have been applied.

@dgutov
Copy link
Owner

dgutov commented Aug 26, 2020

I actually did try polymode, but it is more for markup, and the documentation is more sparse than mmm. Mmm seems to be more for code and is already working better than polymode ever did for this same purpose.

I wonder if it is because the documentation is lacking, or if it really lacks in features. If you had done a comparison, I'd be curious to examine it.

About my commit, it only runs 10 times as a maximum. Line 109 will exit the loop if no more sub-modes have been applied.

Ah, I see. You can open a PR, and we'll continue the discussion from there.

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

No branches or pull requests

2 participants