Override colors based on the surrounding syntax region

15 lượt xem
Chuyển tới thư đầu tiên chưa đọc

Matteo Landi

chưa đọc,
11:19:20 4 thg 11, 20214/11/21
đến vim...@googlegroups.com, ass...@hairstyle.local
Hi there,

In Common Lisp, you can put a #+NIL in front of an expression, to
prevent the compiler from processing it; for example, the following
defines a list of 3 elements:

(list 1 2 3)

The following defines a list of 2 elements (the `3` is swallowed):

(list 1 2 #+nil 3)

While this one...does nothing:

#+nil (list 1 2 3)

So I wanted to highlight all the `#+nil (...)` patterns as if they were
comments, and to do so I started with something as simle as:

syntax match lispExcludedForm /\v\#\+nil \([^)]+\)/
highlight link lispExcludedForm lispComment

I knew that this was not going to work in case of nested parentheses,
and after taking a look at the default syntax file for Lisp, I came up
with the following:

syntax region lispExcludedFormWithNil
\ matchgroup=lispExcludedContentStart
\ start="#+nil ("
\ skip="|.\{-}|"
\ matchgroup=lispExcludedContentStop
\ end=")"
\ contains=@lispBaseListCluster
highlight link lispExcludedContentStart lispComment
highlight link lispExcludedContentStop lispComment

With this I was able to deal with nested parentheses just fine; however,
if I had highlight commands defined for any of the elements contained
inside the `@lispBaseListCluster` cluster, Vim would still end up
coloring those elements while I want the whole region to look like a
`lispComment` instead -- and this is where I got stuck.

So, for the brave ones that made it this far:

- Can I define highlight commands that not only take specific elements
into account (i.e. `lispList`), but also their parents (i.e.
`lispExcludedFormWithNil > lispList`) -- this way I could link
`lispList` to `lispComment` only when contained inside
`lispExcludedFormWithNil`
- If not, should I have to create my own `@lispBaseListExcludedCluster`
cluster, and make sure no highlight command is defined for any of its
elements?
- In which case, can I programmatically iterate through all the elements
of a syntax cluster, and maybe create copies of it?

And last but not least: is what I am trying to achieve, even possible?
Am I on the right track?

Thanks in advance!

--
Matteo Landi
https://matteolandi.net
Trả lời tất cả
Trả lời tác giả
Chuyển tiếp
0 tin nhắn mới