4

How do I get the highlighting group of a text when the group is defined by the command :spell?

For example when a word is misspelled I want to get the highlighting group SpellBad.

We have a question on this site about how to get the highlighting group of a text in a buffer, the answer recommends to use the following command:

:echo synIDattr(synID(line("."), col("."), 1), "name")

and I also have another command using synstack:

:synstack(line('.'), col('.'))

However both of these commands work pretty well for all of the highlighting groups others than the ones defined by :spell (i.e. SpellBad, SpellCap, SpellRare and SpellLocal).

The only reference of this bug that I can find is this SO comment. Am I missing something or does anyone have a workaround?

6
  • 3
    synstack also doesn't report search highlights or ones created with :match or matchadd*()
    – Mass
    Commented Jul 7, 2020 at 2:49
  • 2
    correct, synstack only uses the defined highlighting groups. matches or highlight searches, or sign highlights or cursorline or spelling highlights cannot be detected that way Commented Jul 7, 2020 at 6:43
  • 1
    Thank you Mass and Christian for these additional information, but then it means we have no way to detect these highlights? Hasn't it been an issue to someone else before?
    – statox
    Commented Jul 7, 2020 at 8:06
  • 1
    You can use expand('<cword>')->spellbadword() to detect whether the word under the cursor is wrongly spelled, and if so, which type of spelling error it is (bad, rare, local, caps). But that doesn't tell you whether the word under the cursor is highlighted, because it depends on the syntax plugin which can specify where spell checking should be done (see :h spell-syntax).
    – user938271
    Commented Jul 7, 2020 at 8:30
  • As a workaround, you could use term_start() to open the file in another Vim instance in a terminal buffer, then you could use term_dumpwrite() to dump the terminal screen in another file. Finally, you would have to try to parse the screen dump to check whether the first character of the word is followed by some color code corresponding to one of the spelling error highlight group... For a start, see :h terminal-dumptest, and this Vim test.
    – user938271
    Commented Jul 7, 2020 at 8:30

1 Answer 1

1

I've already discussed a similar problem.

Catch the “search hit BOTTOM, continuing at TOP” when cycle through the badly spelled words

Briefly said, there is not way to get the hi-group for this type of text, because there is always another hi-group for the same text, for example String. You can create a workaround using ]s and [s to discover if under the cursor is a badly spelled word. I tried... it's become extremely slow on big documents. If you want to search for badly spelled words on demand, maybe it will work faster. You decide.

6
  • I could be missing something but I don't understand how your linked question is related to mine: I don't see where you discuss how to get the highlighting group of a misspelled word (I'm not interested in using [s several time to find a misspelled word, I know that the word is under my cursor and I want to get its highlight). Also I'm not sure about your "there is always another hi-group for the same text" explanation: if you create a buffer without filetype and write a word in it, I think the word will not have any highlighting. So if it's misspelled it should have only SpellBad
    – statox
    Commented Jul 7, 2020 at 8:15
  • "I don't see where you discuss how to get the highlighting group of a misspelled word" read the comments below Luc Hermitte's answer. "if you create a buffer without filetype and write a word in it, I think the word will not have any highlighting". I tried it now again, also I tried it half year ago and this not seems to be true for me. I just see a blank output...
    – Max_Payne
    Commented Jul 7, 2020 at 9:22
  • About the highlighting I'm sorry if I am missing something in the 4 comments that I've read but the only mention of highlighting is Luc saying that he got acceptable performances getting text with a "Comment" highlight group, I still don't understand how it relates to my issue. About the second part we see the same thing: the function to get the highlight group returns nothing even when the word is not misspelled so "there is always another hi-group for the same text, for example String" is not true otherwise the function would return "String", right?
    – statox
    Commented Jul 7, 2020 at 10:45
  • All I'm trying to explain to you, that Vim behaves differently as you expect it would. When you move using ]S (like "]s" but only stop at bad words) Vim skips tons of words and jumps directly to a misspelled world; this means that you derivate it just like when you do math and approach the solution from another angle...
    – Max_Payne
    Commented Jul 7, 2020 at 11:13
  • 1
    a small addition: the function to get the highlight group returns nothing when the word is not misspelled because it uses the hi-names defined by your theme, or hi-names defined by Bram Moolenaar hehe )). Let's derivate it: run Vim without any config: vim -u NONE and write something to the buffer. Now let's change the default hi-name color, because it belongs to Normal hi-name: hi! Normal ctermfg=darkblue. I see it darkblue, so yes there is always another hi-group for the text. The function to get the highlight is just an API to Vim core. Do not trust them too much.
    – Max_Payne
    Commented Jul 7, 2020 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.