1

I am designing a syntax rule (for R code) that highlights if I do not start a function with a curly all by itself. For example,

   A <- function(x,
                 y){

would be match but I do not want to match

   A <- function(x,
                 y)
   {

I do NOT watch the regular expression to match occurrences of ){ later on in the file, so I want to exclude ^\s*{ from occurring within my match.

Right now I have an expression that works well if all of this is one one line:

/function\s*(.*)\s*{$/

and I am aware that in vim you can use \_. to do a multiline match, but I don't know how to do a multiline match the EXCLUDES some characters.

I also looked at using syntax region instead but I don't see a way to exclude regions that have ){ in them.

What do you think?

======================= A little more detail ====================

To clarify, I'm trying to prevent putting the curly bracket on the same line as the close paren in my function definition. Kerninghan and Ritchie always start functions (but not conditionals or loops) with a curly bracket on its own line. This practice is helpful for vim's built in movement (going from one function to another) and my custom folding code. I've historically placed the curly in both locations, but want the syntax highlighter to mark it red if I put the curly on the same line as the close paren in order to discipline me to do it K&R style. In vim (using the tip I got below) I can write

syntax match MyError /function\s*(\_[^{]*)\s*{/ contains=Function

And it will highlight cases like my first example above but not K&R-compliant code like my second example.

It's necessary to exclude { as a possible match because otherwise the \_.* will greedily match with something lower in the file not related to this function (like the beginning of an if statement).

Probably there are multiple ways of solving this problem, but what I needed was an expression to do the following:

"Match any number of characters including newlines but excluding {".

  • 1
    You answered my question perfectly, but I'll add a couple of examples for any future readers. – farnsy Nov 12 '18 at 18:38
0

I'm a bit unclear about what you're trying to achieve, but perhaps this is what you're looking for?

/function\s*(\_[^)]*)\s*{$/

This is the same as the regular expression you supplied, except I've replaced .* (match any number of any characters but not including newlines) with \_[^)]* (match any number of characters including newlines but excluding ) close parentheses).

  • This is exactly what I was looking for, an expression to match anything including newlines, except for a particular character (actually { but it doesn't matter). Thanks! – farnsy Nov 12 '18 at 18:37
0

I figured out a brute-force solution.

/function\s*([abcdefghijklmnopqrstuvwxyz ,=()!ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\n]*)\s*{$/

that allows only charachters you would normally find in a function definition in R.

If anyone has a more elegant solution, I'd love to have it.

  • What about something like [^\n] ? – D. Ben Knoble Nov 12 '18 at 0:34
  • What I originally wanted was something like [^{] but it needed to include newline characters, which I do want to be matched. Using \_. includes newlines but can't exclude things as far as I know. – farnsy Nov 12 '18 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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