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

Creation of a poly-mode for a T4-like language #268

Closed
mcraveiro opened this issue May 29, 2020 · 3 comments
Closed

Creation of a poly-mode for a T4-like language #268

mcraveiro opened this issue May 29, 2020 · 3 comments

Comments

@mcraveiro
Copy link

mcraveiro commented May 29, 2020

Hi poly-mode developers,

Thanks for an extremely useful mode. I have setup a simple derived mode that almost does all I need. My objective is to define a mode for a file format very similar to Microsoft's T4, text-templating language [1]. The main exception is where they use C# as the host language, I use C++. I have defined the mode as follows:

(require 'polymode)

(define-hostmode poly-stitch-hostmode :mode 'c++-mode)

(define-innermode poly-stitch-directive-innermode
  :mode 'conf-unix-mode
  :head-matcher "<#@"
  :tail-matcher "#>"
  :head-mode 'host
  :tail-mode 'host)

(define-innermode poly-stitch-expansion-block-innermode
  :mode 'conf-unix-mode
  :head-matcher "<#\$"
  :tail-matcher "#>"
  :head-mode 'host
  :tail-mode 'host)

(define-innermode poly-stitch-expression-control-block-innermode
  :mode 'c++-mode
  :head-matcher "<#="
  :tail-matcher "#>"
  :head-mode 'host
  :tail-mode 'host)

(define-polymode poly-stitch-mode
  :hostmode 'poly-stitch-hostmode
  :innermodes '(poly-stitch-directive-innermode
                poly-stitch-expansion-block-innermode
                poly-stitch-expression-control-block-innermode))

;; (add-to-list 'auto-mode-alist '("\\.stitch" . stitch-mode))

The main problem I have at present is that when I open a file with this mode, I don't see either the host or the inner mode highlighted with a different background as I see on your examples (please see screenshot below of what I see). They all have the same background, making it hard to distinguish. Ideally, I'd like them to be visually quite distinct, as the examples are.

emacs-stitch-mode

A second, but slightly less important problem, is that I am not matching correctly lines starting with:

<#$

This is probably some basic regex error, so I'm playing around with regexes. At any rate, any pointers as to what I am doing wrong would be greatly appreciated.

Many thanks for your time.

[1] https://docs.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2019

@mcraveiro mcraveiro changed the title Creation of a poly-mode for a t4-like language Creation of a poly-mode for a T4-like language May 29, 2020
@mcraveiro
Copy link
Author

mcraveiro commented May 29, 2020

I checked the values of the following variables:

poly-lock-allow-fontification
poly-lock-allow-background-adjustment

And they are both t. I suspect I am not marking the entries as a "span", somehow... Or perhaps the theme doom-dark+ does not play well with this logic...

@mcraveiro
Copy link
Author

Actually, I tried a light theme (doom-solarized-light) and it partially answered my question; it does seem its having some kind of effect. However, not all "blocks" are marked as dark as I'd expect.

emacs-stitch-light-theme

@mcraveiro
Copy link
Author

mcraveiro commented May 29, 2020

OK I'm not entirely sure but after changing themes a few times I managed to get what I was looking for :-) The final mode is as follows:

(require 'polymode)

(define-hostmode poly-stitch-hostmode :mode 'fundamental-mode)

(define-innermode poly-stitch-directive-innermode
  :mode 'fundamental-mode
  :head-matcher "<#@"
  :tail-matcher "#>"
  :head-mode 'host
  :tail-mode 'host)

(define-innermode poly-stitch-expansion-block-innermode
  :mode 'fundamental-mode
  :head-matcher "<#\\$"
  :tail-matcher "#>"
  :head-mode 'host
  :tail-mode 'host)

(define-innermode poly-stitch-big-block-innermode
  :mode 'c++-mode
  :head-matcher "<#\\+"
  :tail-matcher "#>"
  :head-mode 'host
  :tail-mode 'host)

(define-innermode poly-stitch-expression-control-block-innermode
  :mode 'c++-mode
  :head-matcher "<#="
  :tail-matcher "#>"
  :head-mode 'host
  :tail-mode 'host)

(define-polymode poly-stitch-mode
  :hostmode 'poly-stitch-hostmode
  :innermodes '(poly-stitch-directive-innermode
                poly-stitch-expansion-block-innermode
                poly-stitch-big-block-innermode
                poly-stitch-expression-control-block-innermode))

And it looks like so:

emacs-stitch-fixed

Ideally I'd like them to be even more visually distinctive, but this is good enough for now. 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

1 participant