Vim syntax highlight two specific words with a space (regex) - regex

I have created this file called "~/.vim/syntax/proc.vim" and have been populating regex expressions (I think that's what they call it). I'm having to write some test scripts in this test language developed back in the 90's called STOL for a spacecraft payload. I'm working in a secured environment so the only thing I have access to is vim.
STOL lets you write several different types of print statements and I would like some syntax highlighting difference between each type of message (error message, info message, etc).
My vim color profile called "~/.vim/color/molokai.vim" lets me link various regex expressions to a specific syntax class which will invoke a specific color. For example, to highlight keywords I specify two lines like...
" This is the regex where two keywords have been defined
syntax keyword procKeywords IF ELSE
" This is how I link the above regex to a molokai color class
" which is called Keyword
highlight link procKeywords Keyword
I would essentially like to do the same for some error and info messages that STOL defines like the following...
EVENT ERROR "This is a error message"
EVENT INFO "This is a info message"
How can I match on two specific words with a space between them? I need something like the following in my "~/.vim/syntax/proc.vim". The following is wrong but I'm just writing it out showing you what I'm thinking...
syntax match procInfo "EVENT INFO"
syntax match procError "EVENT ERROR"
highlight link procInfo ModeMsg
highlight link procError ErrorMsg

Your attempt already mostly works. However, :syntax match does not automatically enforce whole-keyword match like :syntax keyword, so you'd get false highlighting on e.g. MYEVENT INFORMATION, too. You can easily fix that by adding the keyword boundary atoms :help /\< to the regular expressions:
syntax match procInfo "\<EVENT INFO\>"
syntax match procError "\<EVENT ERROR\>"

Related

Regex-Match while ignoring a char from Searchword

I am using an Engineering Program which lets me Code formulas in order to filter out specific lines in a database. I am trying to look for a certain line in the database which contains e.g. "concrete" as a property.
In the Code I can use regular expressions.
The regex I was using so far looked like this:
".*(concrete).*";
so if the line in the database contains concrete, I will get the wanted result.
Now the Problem is: i would like to switch the word concrete with a variable, so that it Looks like this:
".*(#VARIABLE1).*";
(the Syntax with the # works in the program btw.)
the Problem is: if i set the variable as concrete, the program automatically switches it for 'concrete' . Obviously, the word concrete cant be found anymore, since the searchterm now contains the two ' Symbols in the beginning and i the end.
Is there a way to ignore those two characters using the Right regex?
what I want it to do is the following:
If a line in the database contains "25cm concrete in Grey"
I should get a match from the regex.
with the searchterm ".*(concrete).*"; it works, with the variable ".*(#VARIABLE1).*"; it doesnt.
EDIT:
the whole "Formula" in the program Looks like that:
if(Match(QTO(Typ:="Attribut{FloorsLayer_02_MaterialName}");".*(#V_QUALITY).*" ;"regex") ;QTO(Typ:="Attribut{Fläche}");0)
I want the if-condition to be true, when the match inside is true.
the whole QTO function is just the programs Syntax to use a certain Attribute into the match-function, the middle part is my Problem. I really don't know the programming language or anything,I'm new to this. hope it helps!
Thats more of a hack than a real solution and i'm not sure if it even works:
if you use the regex
.*(#VARIABLE1)?).*
and the string ?concrete(
this will result in a regex looking like this:
.*('?concrete(')?).*
which makes the additional characters optional.
This uses the following assumtption:
the string (#VARIABLE1) gets replaced by the ('<content of VARIABLE1>')

Scanning a language with non-delimited strings with nested tokens

I want to create a lexer/parser for a language that has non-delimited strings.
Which part of the language is a string is defined by the command preceding it.
For example it has statements that look like this:
pause 5
alert Hello world[CRLF] this contains 'pause' once (1)
Alert in this instance can end with any string, including keywords and numbers.
Further complicating things, the text can contain tags like [CRLF] that I want to separate too.
Ideally I'd want this to be broken up into:
[PAUSE][INT 5]
[ALERT][STR "Hello world"][CRLF][STR " this contains 'pause' once (1)"]
I'm currently using flex but from what I've gathered this kind of thing isn't possible with flex.
How can I achieve what I want here?
(Since one of your tags is "regex", I'll suggest a non-flex approach.)
From the example, it seems like you could just:
match each line against ^(\w+) (.+) to obtain command and arguments-text, and then
get individual arguments by splitting the arguments-text on (\[\w+\]) (assuming your regex library's split function can return both the splitter-strings and the split-strings).
It's possible your actual situation is more complex and something like flex makes more sense, but I'm not really seeing it so far.

Go validator.v2 gives error "unknown tag" for regexp

I have been trying to understand why some regular expressions give me an error "Unknown tag" while using the validator.v2 package in golang. It works for some regular expressions but does not work with some which have "{}" inside them, and when I use the validator.Validate() it gives me an error at runtime "unknown tag".
Here's the code:
type Company struct {
Name string `validate:"regexp=^[a-zA-Z .]{1,100}$"`
}
which gives me the following error at runtime:
Name: unknown tag
however this regex works perfectly fine
type Company struct {
Name string `validate:"regexp=^[a-zA-Z .]*$"`
}
I am using the braces because of length restrictions that I want to put on the string. There could be other ways to do it, but I feel the regex is the way to go and is easier to have it along with other rules right there in the expression.
The problem appears to be the , char in your first regex. You can see in the validator source code that the tag is split on ,. By UTSLing, I see no support for escaped commas in the tags; this is probably an oversight on the part of the project author. I suggest filing a bug/feature request.
The problem pointed out by #Flimzy is correct, but it's not a bug.
Probably it was fixed since then, so at the moment validator supports escape sequence \\ for such case, and you can do it like this:
type Company struct {
Name string `validate:"regexp=^[a-zA-Z .]{1\\,100}$"`
}

How to save the error output of gcc to file

When I compile my code I get a bunch of errors which I span through the screen and I can see where does the error start. How can I save the output of gcc to a file?
I tried tricks like
gcc > log.txt
or grepping the result but it didn't work. Searching google yields mostly result on explaining how to print to file with c++
GCC outputs errors to the standard error stream not to the standard output stream. You need to redirect standard error, instead of standard output. In bash:
gcc 2> log.txt
I personally found out that merely outputing the error to a file would not help. In fact the easiest thing that could help me was to avoid wrapping the error lines which are usually super long. So, I decided to use vim highlighting to see the errors better.
Without the highlighter (View Larger Image)
With the highlighter (View Larger Image)
.
And fortunately, there was a very easy way to set up a new syntax highlighting in VIM.
Follow these steps and you will be more productive working on heavily templated C++ codes:
Create a new VIM custom syntax highlighting rule set
you have to define the syntax highlighting rules. Put the following in a file called cerr.vim and keep it for example in $HOME/vim_syntax/cerr.vim
"Set line wrapping to off to see more error lines in one page
set nowrap
set showmatch
"I use stl and boost alot so it is good to remove the namespaces from the error file :)
silent! %s/st![enter image description here][2]d:://g
silent! %s/boost::fusion:://g
silent! %s/boost:://g
"Usually I am not interested in the file paths until I can locate the error so I tried to
"hide them
silent! %s/\/[^\.]*\// /g
"By default syntax highlighting for each line is limited to 3000 characters
"However, 3000 characters is not sufficient for lengthy C++ errors, so I change it to 20000
set synmaxcol=20000
"Now I define the keywords that I would like them to be highlighted
syn keyword cerrInfo instantiated
syn keyword cerrError error Error ERROR
syn keyword cerrWarning warning Warning WARNING
"-------------------------------------
"In this step I would like to distinguish the prefix in each line (which shows the file name) from the rest of the line
syn region cerrLine start=/^/ end=/\:/
syn region cerrSeparator start=/^\.+/ end=/\./ fold oneline
"I want to make templated type information less visible while debugging
"You have to remember that a type can have nested types. So I define three regions
syn region cerrTemplate1 matchgroup=xBracket1 start=/</ end=/>/ contains=cerrTemplate2 fold oneline
syn region cerrTemplate2 matchgroup=xBracket2 start=/</ end=/>/ contains=cerrTemplate3 fold contained oneline
syn region cerrTemplate3 start=/</ end=/>/ contains=cerrTemplate3 contained oneline fold oneline
"Now I would like to highlight whatever is in parenthesis with a different color so I make
"another region in here. This makes sure that function arguments can have different color
syn region cerrPar matchgroup=xBracket start=/(/ end=/)/ contains=cerrTemplate1 oneline fold
"GCC puts the real type information in brackets, let's group them separately
syn region cerrBracket start=/\[/ end=/\]/ contains=cerrTemplate1,cerrPar oneline
"Again GCC puts the error in these weird characters :) So I define a separate region here
syn region cerrCode start=/‘/ end=/’/ contains=cerrPar,cerrBracket,cerrTemplate1 oneline
"And finally I would like to color the line numbers differently
syn match cerrNum "[0-9]\+[:|,]"
"--------------------------------------------------------------------------
"Now the fun part is here, change the colors to match your terminal colors.
"I Use the following colors for my white background terminal.
"In the following we assign a color for each group that we defined earlier
"Comment is a default VIM color group
highlight link cerrInfo Comment
"We use custom coloring for the rest
highlight default cerrWarning ctermfg=red ctermbg=yellow
highlight default cerrError ctermfg=white ctermbg=red
highlight default cerrLine ctermfg=grey term=bold
highlight default cerrSeparator ctermfg=darkgrey
highlight default cerrTemplate1 ctermfg=grey term=bold
highlight default cerrTemplate2 ctermfg=grey term=bold
highlight default cerrTemplate3 ctermfg=grey
highlight default cerrCode cterm=bold ctermfg=darkgrey
highlight default cerrBracket ctermfg=darkgreen
highlight default xBracket1 ctermfg=darkgrey term=bold
highlight default xBracket2 ctermfg=darkgrey
highlight default cerrPar ctermfg=yellow
highlight default cerrNum ctermfg=red
Change your .vimrc file
Now, you have to tell vim to use your new highlighting for files with specific extension. In my case I would like to output my error files to error.ccerr, put the following in your .vimrc in your home folder:
au BufRead,BufNewFile *.cerr set filetype=myerror
au Syntax myerror source $HOME/vim_syntax/cerr.vim
What I say in the above is that when files with the extension .cerr are opened using VIM, they will be considered of type myerror. In the second line I am saying that VIM should use my syntax highlighting rule set which I defined in the previous step for all myerror files.
Send your error output to a .cerr file and open it with VIM
This step is the easiest, we send all errors and warning to error.cerr and if there is any error in the file we immediately open the .cerr file using VIM.
g++ failing.cc &> error.cerr || vim error.cerr

class & function names highlighting in Vim

I just recently set up my Vim environment from Textmate, after becoming addicted to its modal input.
However, syntax highlighting seems to be not so beautiful in Vim. I code in C++ and since the function call and class names can't be highlighted, the code is more difficult to read. I played with color scheme for a bit, but couldn't find any field that corresponded to "class name" or "function name".
In the picture below, notice how DroughtLayer:: and *.size() is not highlighted on the right in MacVim.
(source: ivzhao.com)
Any ideas how to solve this? It really annoys me as I am so much a visual-sensitive guy.
I had this very same problem when I started using vim. The solution is simple, you just have to edit the c syntax file used by vim, here's how to do it:
When you start editing a C or C++ file, vim reads the default c syntax file located in
$VIMRUNTIME/syntax/c.vim
(Where $VIMRUNTIME is where you have vim installed. You can find out it's default value by opening vim and using the command ":echo $VIMRUNTIME").
You can simply overwrite that file, or you can create your custom C syntax file (which will be loaded by vim instead of the default one) in this location:
$HOME/.vim/syntax/c.vim (for UNIX)
$HOME/vimfiles/syntax/c.vim (for PC or OS/2)
(I have never used a Mac so I don't know which one will work for you. You can find out more in the vim help, ":help vimfiles")
Now the fun part. Copy the default "$VIMRUNTIME/syntax/c.vim" file to your vimfiles directory ("$HOME/.vim/syntax/c.vim" for UNIX), and edit it by adding these lines:
" Highlight Class and Function names
syn match cCustomParen "(" contains=cParen,cCppParen
syn match cCustomFunc "\w\+\s*(" contains=cCustomParen
syn match cCustomScope "::"
syn match cCustomClass "\w\+\s*::" contains=cCustomScope
hi def link cCustomFunc Function
hi def link cCustomClass Function
That's it! Now functions and class names will be highlighted with the color defined in the "Function" highlight (":hi Function"). If you want to customize colors, you can change the last two lines above to something like this:
hi def cCustomFunc gui=bold guifg=yellowgreen
hi def cCustomClass gui=reverse guifg=#00FF00
or you can leave the C syntax file alone and define colors in your vimrc file (":help vimrc"):
hi cCustomFunc gui=bold guifg=yellowgreen
hi cCustomClass gui=reverse guifg=#00FF00
(Note the absence of the "def" keyword, go to ":help highlight-default" for details). For the available parameters to the ":hi" command see ":help :highlight".
You can find the complete c.vim file for Vim 7.2 on this link (Note: only use this if you have a non-modified Vim, version 7.2):
http://pastebin.com/f33aeab77
And the obligatory screenshot:
this is my first post here and i didn't know how to make an observation, the answer of Eduardo makes "(" and "{" look unmached and bugs syntax foldind, I changed it a little to fix this.
syn match cCustomParen "?=(" contains=cParen,cCppParen
syn match cCustomFunc "\w\+\s*(\#=" contains=cCustomParen
syn match cCustomScope "::"
syn match cCustomClass "\w\+\s*::" contains=cCustomScope
hi def cCustomFunc gui=bold guifg=yellowgreen
hi def link cCustomClass Function
Interestingly, the syntax highlighters in VIM don't support applying a syntax to identifiers or function names - at least not the syntax highlighters for C and C++. So, even if you do:
:hi Function guifg=red
or
:hi Identifier guifg=red
it doesn't give these a color. I just seems to be not much more than keywords and constants for these languages.
Here, someone has started extending the cpp syntax file to support method names. It's a start I guess.
http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition
The one solution is to use built ctags database. So create one with the ctags utility. Then set the 'tags' variable and put the following to the
~/.vim/after/syntax/c.vim
function! s:highlight()
let list = taglist('.*')
for item in list
let kind = item.kind
if kind == 'f' || kind == 'c'
let name = item.name
exec 'syntax keyword Identifier '.name
endif
endfor
endfunction
call s:highlight()
I must warn you that this can work very slow on the very big ctags database.
Also there is one solution on the vim.org but I didn't try this one. Let me know if it works for you.
EDIT: color_coded may be too heavy for you. try octol/vim-cpp-enhanced-highlight. It supports C++11/14 and integrates what #Eduardo answers.
Semantic based highlighter:
I would recommend jeaye/color_coded,
A vim plugin for libclang-based highlighting
So sorry that i'm new to stackoverflow which means I've not enough reputation to post images. Go see its effects if you wanna give it a shot. :)
Pros:
Easy installation
Semantic highlighting
Clighter mentioned as above, need vim compiled with python2.7.
However, color_coded is written in C++ and provides lua binding ->
C++.
Cons:
It delays unless you make some vim events to acitve it.
Customization is bit harder; you need to edit syntax/color_coded.vim
yourself. But customization has been placed on its roadmap.
Although it's still under development, it's increasingly gaining attention.
Sergey, changing the first line from
syn match cCustomParen "(" contains=cParen,cCppParen
to
syn match cCustomParen "(" contains=cParen contains=cCppParen
seems to fix it for me.
Try using this plugin http://www.vim.org/scripts/script.php?script_id=2646
Its does all ctags highlighting very efficiently for you
Use a plug-in for vim like Taglist or set up ctags or cscope integration with vim (here's a tutorial for the vim/cscope.)
I really recommend you the taghighlight plugin, click here for it's website.
The Clighter plugin can also be considered, which is a
plugin for c-family semantic source code highlighting, based on Clang
However, requires fairly recent versions and software: vim 7.4.330 +python2 and libclang.
To match C functions definitions only, this works for me:
syn match cCustomFuncDef display /\(\w\+\(\s\|*\)\+\)\#<=\w\+\s*(\#=/
hi def cCustomFuncDef ctermfg=lightblue