Remove #Spell from a syntax cluster - vim

I'm using the vim-polyglot plugin. In the JavaScript filetype plugin file, it configures Vim to run spell checking on strings. After glancing through the plugin's implementation, I can see that does this for the following syntax clusters:
jsString
jsTemplateString
jsObjectKeyString
jsObjectStringKey
Here's an example of how the cluster is defined:
syntax region jsString start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1+ end=+$+ contains=jsSpecial,#Spell extend
I've tried adding the lines below to ~/.config/nvim/init.vim as well as ~/.config.nvim/ftplugins/javascript.vim, but neither seems to remove the spelling:
syntax cluster jsString remove=#Spell
syntax cluster jsTemplateString remove=#Spell
syntax cluster jsObjectKeyString remove=#Spell
syntax cluster jsObjectStringKey remove=#Spell
How can I remove spelling from select syntax highlighting groups provided by vim-polyglot?

These are not really syntax clusters, but simply syntax items.
While #Spell and #NoSpell are syntax clusters, what matters with those is where they are contained and not really items they contain, so we can't really use syntax cluster to modify those and enable or spelling for existing syntax items...
So your best bet here is really to redefine the syntax items, removing the #Spell part of them.
You can redefine only those 4 items, but there aren't really good ways to just modify part of the command (simply removing the #Spell part), you end up having to set them from scratch again, which means you have to copy their definition from the original file and then make the modifications.
To add to an existing syntax file, see :help mysyntaxfile-add.
In short, you should create a ~/.vim/after/syntax/javascript.vim with contents:
syntax clear jsString
syntax clear jsTemplateString
syntax clear jsObjectKeyString
syntax clear jsObjectStringKey
syntax region jsString start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1+ end=+$+ contains=jsSpecial extend
syntax region jsTemplateString start=+`+ skip=+\\`+ end=+`+ contains=jsTemplateExpression,jsSpecial extend
syntax region jsObjectKeyString contained start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1\|$+ contains=jsSpecial skipwhite skipempty nextgroup=jsObjectValue
syntax region jsObjectStringKey contained start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1\|$+ contains=jsSpecial extend skipwhite skipempty nextgroup=jsFuncArgs,jsObjectValue
These commands will first clear the syntax items and then define them again, but this time without including the contains=#Spell part, so they'll not enable spell checking for those rules.

Related

Vim recipe for a minor syntax highlighting extension

I want to leave my system's Python syntax highlighting mostly intact, but I have a specific pattern I'd like to highlight for an idiom I use a lot. How can I add additional highlighting instructions on top of the existing highlighting done by vim?
(Apologies if this has already been asked. All the vim syntax highlighting questions I found seemed to involve writing a new syntax highlighting from scratch.)
Put your additional :syntax commands into ~/.vim/after/syntax/python.vim, and they will be automatically executed after the original syntax script.
It's easy to highlight stuff that so far isn't parsed at all.
For elements already parsed / hightlighted, you need to find out by which syntax group (e.g. pythonFunction), and add a containedin=pythonFunction clause to your :syntax commands. Without that, the original matching will obscure yours. To find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.
Introducing highlighting across (larger) elements that have multiple existing syntax groups is difficult, as your match will obscure the original ones, and that may break the entire parsing. You need to carefully examine the existing nested element structure, and try to fit in yours, again via contains= and containedin= clauses. Depending on the actual situation, that can be difficult.
For the actual syntax definitions, see the help starting at :h :syn-keyword. Basically, there are simple keyword definitions, regular expression matches, and regions defined by start and end patterns.

How to get vim spell check to mark bad words inside of Latex math equations?

Spell check in vim is nice enough to not highlight things inside of a math environment when writing LaTeX. This is convenient, but I make some very common typos like "theat" instead of "theta" that would be nice to catch automatically. Is there a way to get the words in the explicit list of 'bad words' to be highlighted regardless of their context?
Note that I still don't want to check whether everything is good within a math environment, just that it is not explicitly bad.
To enable spell checking for a syntax group, you'd have to add contains=#Spell, e.g. for the texStatement group, that would be:
:syn match texStatement "\\\a\+" contains=#Spell
But with that, you'd still have to add all "good" statements to your spell file. If you just want to highlight certain "bad" words, you can define a contained match:
:syn match texBadStatement "theat" containedin=texStatement
And then link to the error or bad spell highlighting:
:hi link texBadStatement SpellBad
Put those commands into ~/.vim/after/syntax/tex.vim to make them persistent.
I'm not a Latex specialist. If the texStatement group is wrong, you need to find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.

vim syntax scripts "sourcing" another, but only for matching lines

I'm writing a vim syntax script and I want to be able to make lines matching a certain pattern, say, '^>', "source" or imitate the markdown syntax highlighting.
Is there a way to do this at the syntax script level? Do I need to just copy and paste it in manually and make the proper adjustments? Does this require a modeline on the actual file?
Thanks!
Have a look at :help :syn-include. It allows you to import an existing syntax (like e.g. markdown) into a syntax cluster in your own syntax, and then you can assign syntax regions (if I understand you correctly, that would be a region starting with /^>/ and ending at the end of the line /$/) to it.
Note that success isn't guaranteed; you need some collaboration from the included syntax. (For example, if the markdown syntax anchors its patterns at ^, but now it's included behind the > prefix, it won't match any more.) In the worst case, you have to modify the included syntax or copy it completely into your own syntax.

Adding syntax highlighting for latex plugin in vim

I am using a package called outlines for LaTeX. It adds commands such as \1 \2 \3 etc.
They are not highlighted by default in vim. So, I created a file called tex.vim in my .vimrc/syntax folder, and put this in the file:
:syn match outline /\\[1-9]/
hi link outline Label
This works only at the top level, not within a block. In other words, it works before my \begin{document}, but not between \begin{document} and \end{document}.
This is pretty much useless. How can I get vim to recognize the syntax, regardless of where it appears in the document?
You need to find the syntax group or cluster defined by the Tex syntax, and use contained containedin=..., but in your case, there is already a syntax group for statements, it's just that it doesn't include numbers. Therefore, you can just piggyback on the existing group and only add matching for numbers:
:syn match texStatement /\\\d/

Vim syntax highlighting: overlapping regex

I try to add some syntax highlighting for javascript to vim, but I keep running into one problem: when characters are already highlighted, they seem to be completely ignored by all other regular expressions.
For example, I tried to add syntax highlighting for the argument list of a function. While creating the right rexex I disabled the syntax highlighting for the function keyword, such that it was easier to see what my regex did. I ended up with the following (working) regex:
syn match javaScriptArguments "[(=\:\s,]function.\{-}(\zs.\{-}\ze)"
However, as soon as I enabled the highlighting for the function keyword again, this line doesn't work anymore. It seems that vim simply excludes everything which is already highlighted, and thus it won't find any matches for the regex above, even though it won't result in characters being highlighted twice.
How can I solve/work around this problem?
Syntax definitions must be contained for them to match inside other syntax items. Find all the gruesome details at :help syn-contains.
In your case, you're relying on a look-before of the "function" keyword via \zs. In my experience, that's bound to cause problems, but may turn out to be unneccessary once you use contained. In general, it is difficult to extend an existing syntax definition without modifying the original script (which I suppose is what you're intending to do). Have a look at :help :syn-containedin and :help :syn-nextgroup.

Resources