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

Interaction with nix-mode: font-lock broken #324

Open
ccarlile opened this issue Nov 2, 2022 · 4 comments
Open

Interaction with nix-mode: font-lock broken #324

ccarlile opened this issue Nov 2, 2022 · 4 comments

Comments

@ccarlile
Copy link

ccarlile commented Nov 2, 2022

I have my emacs configured as a nix expression. In order to have the elisp sections of the config rendered in emacs lisp mode, I created a polymode to handle the elisp blocks in multi-line nix strings beginning with the string '';;. This breaks fontification in all nix files - syntax highlighting doesn't work until I run e.g. font-lock-debug-fontify. When I disable the polymode, syntax highlighting works as expected. I am not an elisp wizard, so I'm not sure if this is an issue with nix-mode or polymode. I was able to reproduce the issue with a minimal config file and load with emacs -q -l minimal.elisp. The config file is below:

(use-package nix-mode
  :mode "\\.nix\\'")

(use-package polymode
  :config
  (define-hostmode poly-nix-hostmode
    :mode 'nix-mode)
  
  (define-innermode poly-nix-elisp-innermode
    :mode 'emacs-lisp-mode
    :head-matcher "\'\';;"
    :tail-matcher "\'\';"
    :head-mode 'host
    :tail-mode 'host)
  
  (define-polymode poly-nix-mode
    :hostmode 'poly-nix-hostmode
    :innermodes '(poly-nix-elisp-innermode))

  :hook (nix-mode . poly-nix-mode))

If this is an issue with nix-mode, I'm happy to close this and reopen an issue there, but I'm hoping someone can help me figure out where the problem is. Thanks for a great package!

@tylerjl
Copy link

tylerjl commented Aug 9, 2023

Just thought I'd add here that I'm doing something similar - although with a dynamic innermode - and experiencing similar issues. I find that I can often ad-hoc fix things by invoking font-lock with C-x x f to recolor everything, although that isn't ideal. My approach is to derive the inner mode with comments like this:

{
  script = /* sh */ ''
    echo "This is in shell-script-mode"
  '';
}

Here's my config:

(define-hostmode poly-nix-hostmode
  :mode 'nix-mode)
(define-auto-innermode poly-nix-dynamic-innermode
  :head-matcher "/[*] +[[:alpha:]]+ +[*]/ +''$"
  :tail-matcher "^[ \t]+''"
  :mode-matcher (cons "/[*] +\\([[:alpha:]]+\\) +[*]/" 1)
  :head-mode 'host
  :tail-mode 'host)
(define-polymode poly-nix-mode
  :hostmode 'poly-nix-hostmode
  :innermodes '(poly-nix-dynamic-innermode))

(add-to-list 'auto-mode-alist '("\\.nix" . poly-nix-mode))

@ccarlile
Copy link
Author

It seems like the issue is with registering the mode in the hook - if I remove the :hook stanza from the use-package config block and invoke poly-nix-mode as a command, then everything works as expected

@uqix
Copy link

uqix commented Dec 30, 2023

It seems like the issue is with registering the mode in the hook - if I remove the :hook stanza from the use-package config block and invoke poly-nix-mode as a command, then everything works as expected

Same problem in groovy hostmode with bash innermode.

And with :hook stanza, we can't disable polymode by M-x poly-nix-mode, while manually invoking poly-nix-mode works.

@uqix
Copy link

uqix commented Feb 24, 2024

Just thought I'd add here that I'm doing something similar - although with a dynamic innermode - and experiencing similar issues. I find that I can often ad-hoc fix things by invoking font-lock with C-x x f to recolor everything, although that isn't ideal. My approach is to derive the inner mode with comments like this:

{
  script = /* sh */ ''
    echo "This is in shell-script-mode"
  '';
}

Here's my config:

(define-hostmode poly-nix-hostmode
  :mode 'nix-mode)
(define-auto-innermode poly-nix-dynamic-innermode
  :head-matcher "/[*] +[[:alpha:]]+ +[*]/ +''$"
  :tail-matcher "^[ \t]+''"
  :mode-matcher (cons "/[*] +\\([[:alpha:]]+\\) +[*]/" 1)
  :head-mode 'host
  :tail-mode 'host)
(define-polymode poly-nix-mode
  :hostmode 'poly-nix-hostmode
  :innermodes '(poly-nix-dynamic-innermode))

(add-to-list 'auto-mode-alist '("\\.nix" . poly-nix-mode))

Replacing mode-hook with auto-mode-alist works, thanks.

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

3 participants