Vim errorformat: include part of expression in message line

With vim errorformat syntax, is there a way to use part of a message when filtering results?

As an example, some linker errors do not have anything explicit to distinguish them from line errors except for the error itself:

/path/to/foo.cpp:42: undefined reference to 'UnimplementedFunction' 

or

 /path/to/foo.cpp:43: multiple definition of 'MultiplyDefinedFunction' 

Using the error format:

 set efm=%f:%l:\ %m 

will catch and display both of them correctly, but it will falsely match many other cases (any line starting with "[line]: [number]:").

Or, explicitly indicating both of them:

 set efm= set efm+=%f:%l:\ undefined\ reference\ to\ %m set efm+=%f:%l:\ multiple\ definition\ of\ %m 

removes false positives, but the β€œmessage” becomes much less useful - the actual error is no longer included (just what is after it).


Is there anything in the syntax that I am missing to handle this situation?

Ideally, I would like to say something like:

 set efm+=%f:%l:\ %{StartMessage}undefined\ reference\ to\ %*\\S%{EndMessage} set efm+=%f:%l:\ %{StartMessage}multiple\ definition\ of\ %*\\S%{EndMessage} 

... where all the match between StartMessage and EndMessage is used as an error message.

+6
source share
2 answers

Errorformat can also use vim regex syntax (albeit rather inconveniently), which gives us a solution to the problem. We can use a non-capturing group and a zero-width statement to require these signal phrases without consuming them. This allows %m pick them up. As a simple regular expression syntax, this zero-width statement looks like this:

 \%(undefined reference\|multiple definition\)\@= 

But in order to use it in efm , we need to replace \ with %\ and % with %% , and for use in the string :set we need to avoid backslashes, spaces and a vertical bar, so we finally have:

 :set efm=%f:%l:\ %\\%%(undefined\ reference%\\\|multiple\ definitions%\\)%\\@=%m 

When this error file

 /path/to/foo.cpp:42: undefined reference to 'UnimplementedFunction' /path/to/foo.cpp:43: multiple definition of 'MultiplyDefinedFunction' notafile:123: just some other text 

Appears in :copen as:

 /path/to/foo.cpp|42| undefined reference to 'UnimplementedFunction' /path/to/foo.cpp|43| multiple definition of 'MultiplyDefinedFunction' || notafile:123: just some other text 
+2
source

I used sed to overwrite the output in such cases when I want to get arbitrary output that is not uniform in the quickfix window.

You can write make.sh that runs make (or whatever you do to create it) and trim things that don't interest you:

 make | sed '/undefined reference\|multiple definition/!d' 

(Deletes strings not containing 'undefined reference' or 'multiple definition')

If this is too inconvenient due to the number of lines of errors you care about, you can do the reverse and just kill things you don't need:

 make | sed 's/some garbage\|other useless message//' 

then :set makeprg=make.sh in vim

+1
source

Source: https://habr.com/ru/post/948360/


All Articles