Vim syntax error highlight - vim

I'm vim newbie, and I been using someone's vim configuration(I've cloned his .vim directory to my home directory).
It seems that his configuration has a bug about syntax checking. As you can see:
The bracket should not be highlighted, cause there is not error there.
So my question is, which part of the vim configuration should I check for this issue.
Thanks
UPDATE: 0
I think I found the line that cause this bug.
I have a extra.vim file in ~/.vim/syntax/c/extra.vim the file is looks like:
"========================================================
" Highlight All Function
"========================================================
syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>[^()]*)("me=e-2
syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>\s*("me=e-1
"========================================================
" Highlight All Math Operator
"========================================================
" C math operators
syn match cMathOperator display "[-+\*/%=]"
"" C pointer operators
syn match cPointerOperator display "->\|\."
"" C logical operators - boolean results
syn match cLogicalOperator display "[!<>]=\="
syn match cLogicalOperator display "=="
"" C bit operators
syn match cBinaryOperator display "\(&\||\|\^\|<<\|>>\)=\="
syn match cBinaryOperator display "\~"
syn match cBinaryOperatorError display "\~="
"" More C logical operators - highlight in preference to binary
syn match cLogicalOperator display "&&\|||"
syn match cLogicalOperatorError display "\(&&\|||\)="
" Math Operator
hi def link cMathOperator cOperator
hi def link cPointerOperator cOperator
hi def link cLogicalOperator cOperator
hi def link cBinaryOperator cOperator
hi def link cBinaryOperatorError cOperator
hi def link cLogicalOperator cOperator
hi def link cLogicalOperatorError cOperator
hi def link cFunction Function
hi def link cOperator Operator
" hi Operator guifg=LightGoldenrod
When I comment out the line below:
syn match cMathOperator display "[-+\*/%=]"
The issue is gone.
So how can I fix this, and why ?

Vim probably has not checked the entire file. It is a kind of optimization which sometimes fails.
Usually scrolling the file backwards and forwards a couple of screens solves the problem.
You can also force a analysis on the entire file:
:syn sync fromstart
For more information, check Vim FAQ 24.8: Vim syntax highlighting is broken. When I am editing a file, some parts of the file is not syntax highlighted or syntax highlighted incorrectly.

OK, This Bug is corrected with:
-"syn match cMathOperator display "[-+\*/%=]"
+syn match cMathOperator display "[-+/*/%=]"

Related

How to turn off a vim highlight group named //

The two characters that introduce comments, namely //, get highlighted in yellow. I don't want this. It happens in files of all types: C, js, html, pl.
When I consult the vim help, it says to do this
:so $VIMRUNTIME/syntax/hitest.vim
to see the highlight groups. (I can also do this with :hi).
I do see a // highlight group. The documentation reads to me as if I could do the following to turn the highlighting off
:hi // NONE
but it doesn't work for me. (Gives me Invalid character in group name.) Mac OS, vim version 8.0
Are you certain that the highlight group in question you're after is named //? I know that's not the name of the highlight group for JavaScript syntax.
You can look at the highlight group definitions in the vim syntax files on Github:
" Define the default highlighting.
" Only when an item doesn't have highlighting yet
hi def link javaScriptComment Comment
hi def link javaScriptLineComment Comment
hi def link javaScriptCommentTodo Todo
hi def link javaScriptSpecial Special
hi def link javaScriptStringS String
hi def link javaScriptStringD String
hi def link javaScriptStringT String
hi def link javaScriptCharacter Character
So :hi javaScriptComment NONE should unhighlight just JavaScript comments. And :hi Comment NONE would unhlighlight comments of all types.

Vim custom syntax, excluding previous match highlighting in a region

I'm writing a vim syntax highlighting script for a file that uses * to denote the start of a comment, except when surrounded by {}. i.e.
* This is a comment, bellow is math
{ x_variable * y_variable + 10.0 }
I would like to highlight only the brackets, and ignore the comment highlighting inside, while still maintaining highlighting for numbers.
So far I have:
syn match mathSym "[{}]"
syn region mathRegion start=+{+ send=+}+ contains=numberHi
syn match commentHi "\*.*$" display contains=#Spell
hi link commentHi Comment
hi link mathSym Statement
hi link mathRegion Normal
I'm not sure if this is the right way to do it. It seem to ignore the * as a comment, and provide number highlighting, but no highlighting for the brackets.
I tried
region mathRegion start=+{+ send=+}+ contains=numberHi, mathSym
but this ends up setting all highlighting in the file to Normal
Your question is missing the numberHi stuff, but this should do the trick:
syn region mathRegion matchgroup=mathSym start=+{+ end=+}+ contains=numberHi,mathSym
syn match commentHi "\*.*$" display contains=#Spell
hi link commentHi Comment
hi link mathSym Statement
Instead of a separate match for mathSym, you can use the matchgroup=mathSym to highlight the start and end.
Also, you don't need to link mathRegion to Normal; just use it for structure.
The x * y won't match inside mathRegion, because it isn't contained in here. I don't see any problem with it.
PS: Do you know the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin? It's a great help when writing syntaxes.

How can I add a "::" operator to my VIM syntax highlighting file?

I am using a language embedded inside of Scala that has a :: operator. I would like to modify my scala.vim syntax file to recognize this new operator.
This is what I've tried (the first line works fine, but I don't know how to add the :: operator to it):
syn match COperator "[&|~><!)(*#%#+/=?:;}{,.\^\-\[\]]"
syn match COperator "\v::"
syn match COperator "\v\:\:"
hi link COperator Special
Any advice?
An existing scalaOperator ":\{2,\}" was causing all sequences of : to match (if there are two or more series of :), overriding my own syn match COperator statements.
The solution to get :: to match as a COperator was to simply rename the scalaOperator that matches series of : to a COperator:
syn match COperator ":\{2,\}"
Summary: check for existing match rules that override your own match rules.

LaTeX section highlighting in vim

In LaTeX, a section looks like:
\section{Section Title}
I would like to highlight such sections, or section titles. I tried to put the following in ~/.vim/bundle/latexrc/after/syntax/tex.vim:
syn match texSectioning "\\section\>" skipwhite nextgroup=texSectioningTitle
syn region texSectioningTitle contained matchgroup=Delimiter start='{' end='}' contains=#texSectioningGroup
syn cluster texSectioningGroup contains=texMatcher,texComment,texDelimiter
(Note that this kind of syntax is not handled by the default tex.vim syntax file. It only defines "section zones", which are pretty much worthless for me.)
I then define the following in my color scheme:
hi texSectioning gui=bold guifg=red
And nothing happens; that is, section titles do not appear in red in my LaTeX code (even after I reloaded the file completely).
I am totally confused as to how vim's syntax work, and how to debug it.
Edit
Some more information: it sometimes works and sometimes not. Completely unpredictable. What could be the problem? Pathogen? Something else? I'm completely puzzled.
You have defined the new syntax items texSectioning, texSectioningTitle and texSectioningGroup, but you have not linked them to a highlighting group, so Vim doesn't know how to display them. Try adding these lines:
hi def link texSectioning Statement
hi def link texSectioningTitle String
hi def link texSectioningGroup Comment
The Statement, String and Comment colourings are defining by the colourscheme you are using. These are just examples: you can replace them with any group defined in the colourscheme file.
Here is the answer: the tex.vim divides the text in zones, in which the syntax must be explicitly allowed. The key element is that command:
syn cluster texChapterGroup contains=#texSectioningGroup
This says to vim that inside a texChapterGroup, the syntax cluster texSectioningGroup is allowed. The next thing to do is simply to define that cluster as usual.
Another detail is that the region texSectioningTitle must be contained, otherwise it will match arbitrary pairs of {} in LaTeX.
So a complete solution goes like this:
syn match texSectioningCommand '\\section\>' skipwhite nextgroup=texSectioningTitle contains=#texSectioningGroup
syn region texSectioningTitle start='{' end='}' contained
syn cluster texSectioningGroup contains=texSectioningCommand
syn cluster texChapterGroup contains=#texSectioningGroup
Edit Here is why the behaviour was apparently unpredictable: vim does not read the entire file to figure out the syntax. So in a big enough chapter, my section syntax would work because vim did not go far enough to see it was in a chapter zone.
Just to update the information to highlight section easily. Using containedin means that all the other syntax matches contains this new syntax match. Then just define the color you want.
syn match texSectioningCommand '\\section\>' containedin=ALLBUT,texComment
hi texSectioningCommand guifg=#ec5f67 ctermfg=203
Alternatively, a simple new syntax match could be added to the texFoldGroup in order to be evaluated inside the the block document.
syn match texSectioningCommand '\\section\>'
syn cluster texFoldGroup add=texSectioningCommand
hi texSectioningCommand guifg=#ec5f67 ctermfg=203

Vim syntax highlighting

Alright, this is probably a stupid question, but....
I've got a file of source code in a proprietary language. I want to edit said file with VIM, instead of their crummy editor. I'd like basic syntax highlighting of the language, but I don't want to spend a bunch of time rolling my own syntax file.
Therefore, does VIM have a basic source highlighting module? It doesn't need to be perfect, I just want it to cover simple things. Currently, my only choices are no syntax highlighting, pick a random language, or roll my own.
EDIT: Source code sample below
{
function letter do
gposition 0, 0
if gender = "M" do
if language = "SPA" OR state = "PR" do
%male spanish letter
gposition .26, .75
pdfimage "MALE SPANISH.pdf", 1, .93
setcolor truewhite
setfillmode 1
%whitebox
gposition 5.25, 1.25
rectangle 2.5, .5
Could this be the correct language?
http://www.iml.ece.mcgill.ca/~stephan/node/17
Rolling your own syntax highlighting is not difficult at all and it would take a few minutes.
For example, I wrote a DSL (called Konira) that uses Python for the most part, but it fails at highlighting my custom DSL statements. This is how the "extra" highlighting looks:
function! KoniraSyntax() abort
let b:current_syntax = 'konira'
syn match KoniraIt '\v^\s+it\s+'
syn match KoniraSkipIf '\v^\s+skip\s+if'
syn match KoniraDescribe '\v^describe\s+'
syn match KoniraRaises '\v^\s+raises\s+'
syn match KoniraBeforeAll '\v^\s+before\s+all'
syn match KoniraBeforeEach '\v^\s+before\s+each'
syn match KoniraAfterEach '\v^\s+after\s+each'
syn match KoniraAfterAll '\v^\s+after\s+all'
hi def link KoniraSkipIf Statement
hi def link KoniraIt Statement
hi def link KoniraDescribe Statement
hi def link KoniraRaises Identifier
hi def link KoniraBeforeAll Statement
hi def link KoniraBeforeEach Statement
hi def link KoniraAfterAll Statement
hi def link KoniraAfterEach Statement
endfunction
As you can see above, I set the current syntax, then I match via regular expression those
statements that I want, and the I apply the type of highlighting that I need on that match.
And you can call it as a regular function when you know (or if you able to detect) that you are editing such a source file:
call KoniraSyntax()

Resources