My purpose is to get information which would explain why the region under cursor has that particular color. Yes, there are several very similar questions, but still my question is different.
I can get required information "manually". For example, in python file the color of a string is red. Ok, I navigate to the string and enter following commands (text after ->
is a result and my comment):
:lua=vim.fn.synID(vim.fn.line("."), vim.fn.col("."), 0) -> 550, this is id of syntax
:lua=vim.fn.synIDattr(550, 'name') -> "pythonString"
(I use neovim, if you use vim you can call these functions using approach described here: How to know which highlighting group is used for the background of a word?)
Ok, so the name of syntax region is "pythonString". Now I can find out the highlight group associated with this syntax group:
:highlight pythonString -> "pythonString xxx links to String"
:highlight String -> "String xxx links to Constant"
:highlight Constant -> "Constant xxx ctermfg=217"
and here it is, color 217 is the color used to display the string.
Now I want to automate last steps. I want to call some function from my script which would find out that syntax #550 ("pythonString") is linked to highlight group "String".
I try to use synIDtrans
for this purpose:
:lua=vim.fn.synIDtrans(550) -> 73
:lua=vim.fn.synIDattr(73, 'name') -> "Constant"
Here is the problem: vim.fn.synIDtrans
reports that "pythonString" is linked to highlight group "Constant", not to "String".
I guess this is by design. Help for synIDtrans
says:
Highlight links given with ":highlight link" are followed.
Is there any workaround for this behavior? I want my script to report the full chain of links: "pythonString" -> "String" -> "Constant".
NOTES:
- I guess numeric ids of the highlight groups may be different on your installation.
- Highlight region id returned by
synIDtrans
depends on current colorscheme (!!!). If in your colorscheme the "String" is not linked to anything, the method would return id of "String" highlight group as expected.