r.vim 14.4 KB
Newer Older
1 2 3 4 5
" Vim syntax file
" Language:	      R (GNU S)
" Maintainer:	      Jakson Aquino <jalvesaq@gmail.com>
" Former Maintainers: Vaidotas Zemlys <zemlys@gmail.com>
" 		      Tom Payne <tom@tompayne.org>
KillTheMule's avatar
KillTheMule committed
6 7
" Contributor:        Johannes Ranke <jranke@uni-bremen.de>
" Homepage:           https://github.com/jalvesaq/R-Vim-runtime
Justin M. Keyes's avatar
Justin M. Keyes committed
8
" Last Change:	      Wed Aug 01, 2018  10:10PM
9
" Filenames:	      *.R *.r *.Rhistory *.Rt
10
"
Justin M. Keyes's avatar
Justin M. Keyes committed
11
" NOTE: The highlighting of R functions might be defined in
12
" runtime files created by a filetype plugin, if installed.
13 14
"
" CONFIGURATION:
James McCoy's avatar
James McCoy committed
15
"   Syntax folding can be turned on by
16 17 18
"
"      let r_syntax_folding = 1
"
James McCoy's avatar
James McCoy committed
19 20
"   ROxygen highlighting can be turned off by
"
Justin M. Keyes's avatar
Justin M. Keyes committed
21
"      let r_syntax_hl_roxygen = 0
James McCoy's avatar
James McCoy committed
22
"
23 24 25 26 27 28
" Some lines of code were borrowed from Zhuojun Chen.

if exists("b:current_syntax")
  finish
endif

Justin M. Keyes's avatar
Justin M. Keyes committed
29 30 31 32 33 34 35 36 37 38 39 40 41
if has("patch-7.4.1142")
  syn iskeyword @,48-57,_,.
else
  setlocal iskeyword=@,48-57,_,.
endif

" The variables g:r_hl_roxygen and g:r_syn_minlines were renamed on April 8, 2017.
if exists("g:r_hl_roxygen")
  let g:r_syntax_hl_roxygen = g:r_hl_roxygen
endif
if exists("g:r_syn_minlines")
  let g:r_syntax_minlines = g:r_syn_minlines
endif
42

James McCoy's avatar
James McCoy committed
43
if exists("g:r_syntax_folding") && g:r_syntax_folding
44 45
  setlocal foldmethod=syntax
endif
Justin M. Keyes's avatar
Justin M. Keyes committed
46 47

let g:r_syntax_hl_roxygen = get(g:, 'r_syntax_hl_roxygen', 1)
48 49 50 51 52

syn case match

" Comment
syn match rCommentTodo contained "\(BUG\|FIXME\|NOTE\|TODO\):"
Justin M. Keyes's avatar
Justin M. Keyes committed
53 54 55 56
syn match rTodoParen contained "\(BUG\|FIXME\|NOTE\|TODO\)\s*(.\{-})\s*:" contains=rTodoKeyw,rTodoInfo transparent
syn keyword rTodoKeyw BUG FIXME NOTE TODO contained
syn match rTodoInfo "(\zs.\{-}\ze)" contained
syn match rComment contains=@Spell,rCommentTodo,rTodoParen,rOBlock "#.*"
57 58

" Roxygen
Justin M. Keyes's avatar
Justin M. Keyes committed
59 60 61 62 63 64 65 66 67 68 69
if g:r_syntax_hl_roxygen
  " A roxygen block can start at the beginning of a file (first version) and
  " after a blank line (second version). It ends when a line that does not
  " contain a roxygen comment. In the following comments, any line containing
  " a roxygen comment marker (one or two hash signs # followed by a single
  " quote ' and preceded only by whitespace) is called a roxygen line. A
  " roxygen line containing only a roxygen comment marker, optionally followed
  " by whitespace is called an empty roxygen line.

  " First we match all roxygen blocks as containing only a title. In case an
  " empty roxygen line ending the title or a tag is found, this will be
Justin M. Keyes's avatar
Justin M. Keyes committed
70
  " overridden later by the definitions of rOBlock.
Justin M. Keyes's avatar
Justin M. Keyes committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  syn match rOTitleBlock "\%^\(\s*#\{1,2}' .*\n\)\{1,}" contains=rOCommentKey,rOTitleTag
  syn match rOTitleBlock "^\s*\n\(\s*#\{1,2}' .*\n\)\{1,}" contains=rOCommentKey,rOTitleTag

  " When a roxygen block has a title and additional content, the title
  " consists of one or more roxygen lines (as little as possible are matched),
  " followed either by an empty roxygen line
  syn region rOBlock start="\%^\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*$" end="^\s*\(#\{1,2}'\)\@!" contains=rOTitle,rOTag,rOExamples,@Spell keepend fold
  syn region rOBlock start="^\s*\n\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*$" end="^\s*\(#\{1,2}'\)\@!" contains=rOTitle,rOTag,rOExamples,@Spell keepend fold

  " or by a roxygen tag (we match everything starting with @ but not @@ which is used as escape sequence for a literal @).
  syn region rOBlock start="\%^\(\s*#\{1,2}' .*\n\)\{-}\s*#\{1,2}' @\(@\)\@!" end="^\s*\(#\{1,2}'\)\@!" contains=rOTitle,rOTag,rOExamples,@Spell keepend fold
  syn region rOBlock start="^\s*\n\(\s*#\{1,2}' .*\n\)\{-}\s*#\{1,2}' @\(@\)\@!" end="^\s*\(#\{1,2}'\)\@!" contains=rOTitle,rOTag,rOExamples,@Spell keepend fold

  " If a block contains an @rdname, @describeIn tag, it may have paragraph breaks, but does not have a title
  syn region rOBlockNoTitle start="\%^\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*\n\(\s*#\{1,2}'.*\n\)\{-}\s*#\{1,2}' @rdname" end="^\s*\(#\{1,2}'\)\@!" contains=rOTag,rOExamples,@Spell keepend fold
  syn region rOBlockNoTitle start="^\s*\n\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*\n\(\s*#\{1,2}'.*\n\)\{-}\s*#\{1,2}' @rdname" end="^\s*\(#\{1,2}'\)\@!" contains=rOTag,rOExamples,@Spell keepend fold
  syn region rOBlockNoTitle start="\%^\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*\n\(\s*#\{1,2}'.*\n\)\{-}\s*#\{1,2}' @describeIn" end="^\s*\(#\{1,2}'\)\@!" contains=rOTag,rOExamples,@Spell keepend fold
  syn region rOBlockNoTitle start="^\s*\n\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*\n\(\s*#\{1,2}'.*\n\)\{-}\s*#\{1,2}' @describeIn" end="^\s*\(#\{1,2}'\)\@!" contains=rOTag,rOExamples,@Spell keepend fold

  " A title as part of a block is always at the beginning of the block, i.e.
  " either at the start of a file or after a completely empty line.
  syn match rOTitle "\%^\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*$" contained contains=rOCommentKey,rOTitleTag
  syn match rOTitle "^\s*\n\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*$" contained contains=rOCommentKey,rOTitleTag
  syn match rOTitleTag contained "@title"

Justin M. Keyes's avatar
Justin M. Keyes committed
96
  syn match rOCommentKey "^\s*#\{1,2}'" contained
Justin M. Keyes's avatar
Justin M. Keyes committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  syn region rOExamples start="^#\{1,2}' @examples.*"rs=e+1,hs=e+1 end="^\(#\{1,2}' @.*\)\@=" end="^\(#\{1,2}'\)\@!" contained contains=rOTag fold

  " rOTag list generated from the lists in
  " https://github.com/klutometis/roxygen/R/rd.R and
  " https://github.com/klutometis/roxygen/R/namespace.R
  " using s/^    \([A-Za-z0-9]*\) = .*/  syn match rOTag contained "@\1"/
  " Plus we need the @include tag

  " rd.R
  syn match rOTag contained "@aliases"
  syn match rOTag contained "@author"
  syn match rOTag contained "@backref"
  syn match rOTag contained "@concept"
  syn match rOTag contained "@describeIn"
  syn match rOTag contained "@description"
  syn match rOTag contained "@details"
  syn match rOTag contained "@docType"
  syn match rOTag contained "@encoding"
  syn match rOTag contained "@evalRd"
  syn match rOTag contained "@example"
  syn match rOTag contained "@examples"
  syn match rOTag contained "@family"
  syn match rOTag contained "@field"
  syn match rOTag contained "@format"
  syn match rOTag contained "@inherit"
  syn match rOTag contained "@inheritParams"
  syn match rOTag contained "@inheritDotParams"
  syn match rOTag contained "@inheritSection"
  syn match rOTag contained "@keywords"
  syn match rOTag contained "@method"
  syn match rOTag contained "@name"
  syn match rOTag contained "@md"
  syn match rOTag contained "@noMd"
  syn match rOTag contained "@noRd"
  syn match rOTag contained "@note"
  syn match rOTag contained "@param"
  syn match rOTag contained "@rdname"
  syn match rOTag contained "@rawRd"
  syn match rOTag contained "@references"
  syn match rOTag contained "@return"
  syn match rOTag contained "@section"
  syn match rOTag contained "@seealso"
  syn match rOTag contained "@slot"
  syn match rOTag contained "@source"
  syn match rOTag contained "@template"
  syn match rOTag contained "@templateVar"
  syn match rOTag contained "@title"
  syn match rOTag contained "@usage"
  " namespace.R
  syn match rOTag contained "@export"
  syn match rOTag contained "@exportClass"
  syn match rOTag contained "@exportMethod"
  syn match rOTag contained "@exportPattern"
  syn match rOTag contained "@import"
  syn match rOTag contained "@importClassesFrom"
  syn match rOTag contained "@importFrom"
  syn match rOTag contained "@importMethodsFrom"
  syn match rOTag contained "@rawNamespace"
  syn match rOTag contained "@S3method"
  syn match rOTag contained "@useDynLib"
  " other
  syn match rOTag contained "@include"
James McCoy's avatar
James McCoy committed
159
endif
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197


if &filetype == "rhelp"
  " string enclosed in double quotes
  syn region rString contains=rSpecial,@Spell start=/"/ skip=/\\\\\|\\"/ end=/"/
  " string enclosed in single quotes
  syn region rString contains=rSpecial,@Spell start=/'/ skip=/\\\\\|\\'/ end=/'/
else
  " string enclosed in double quotes
  syn region rString contains=rSpecial,rStrError,@Spell start=/"/ skip=/\\\\\|\\"/ end=/"/
  " string enclosed in single quotes
  syn region rString contains=rSpecial,rStrError,@Spell start=/'/ skip=/\\\\\|\\'/ end=/'/
endif

syn match rStrError display contained "\\."


" New line, carriage return, tab, backspace, bell, feed, vertical tab, backslash
syn match rSpecial display contained "\\\(n\|r\|t\|b\|a\|f\|v\|'\|\"\)\|\\\\"

" Hexadecimal and Octal digits
syn match rSpecial display contained "\\\(x\x\{1,2}\|[0-8]\{1,3}\)"

" Unicode characters
syn match rSpecial display contained "\\u\x\{1,4}"
syn match rSpecial display contained "\\U\x\{1,8}"
syn match rSpecial display contained "\\u{\x\{1,4}}"
syn match rSpecial display contained "\\U{\x\{1,8}}"

" Statement
syn keyword rStatement   break next return
syn keyword rConditional if else
syn keyword rRepeat      for in repeat while

" Constant (not really)
syn keyword rConstant T F LETTERS letters month.abb month.name pi
syn keyword rConstant R.version.string

198
syn keyword rNumber   NA_integer_ NA_real_ NA_complex_ NA_character_
199 200 201 202

" Constants
syn keyword rConstant NULL
syn keyword rBoolean  FALSE TRUE
203
syn keyword rNumber   NA Inf NaN
204 205 206 207 208 209 210 211

" integer
syn match rInteger "\<\d\+L"
syn match rInteger "\<0x\([0-9]\|[a-f]\|[A-F]\)\+L"
syn match rInteger "\<\d\+[Ee]+\=\d\+L"

" number with no fractional part or exponent
syn match rNumber "\<\d\+\>"
212
" hexadecimal number
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
syn match rNumber "\<0x\([0-9]\|[a-f]\|[A-F]\)\+"

" floating point number with integer and fractional parts and optional exponent
syn match rFloat "\<\d\+\.\d*\([Ee][-+]\=\d\+\)\="
" floating point number with no integer part and optional exponent
syn match rFloat "\<\.\d\+\([Ee][-+]\=\d\+\)\="
" floating point number with no fractional part and optional exponent
syn match rFloat "\<\d\+[Ee][-+]\=\d\+"

" complex number
syn match rComplex "\<\d\+i"
syn match rComplex "\<\d\++\d\+i"
syn match rComplex "\<0x\([0-9]\|[a-f]\|[A-F]\)\+i"
syn match rComplex "\<\d\+\.\d*\([Ee][-+]\=\d\+\)\=i"
syn match rComplex "\<\.\d\+\([Ee][-+]\=\d\+\)\=i"
syn match rComplex "\<\d\+[Ee][-+]\=\d\+i"

230
syn match rAssign    '='
231 232 233 234 235 236 237 238 239 240
syn match rOperator    "&"
syn match rOperator    '-'
syn match rOperator    '\*'
syn match rOperator    '+'
if &filetype != "rmd" && &filetype != "rrst"
  syn match rOperator    "[|!<>^~/:]"
else
  syn match rOperator    "[|!<>^~`/:]"
endif
syn match rOperator    "%\{2}\|%\S\{-}%"
241 242
syn match rOperator '\([!><]\)\@<=='
syn match rOperator '=='
243 244 245 246 247 248 249
syn match rOpError  '\*\{3}'
syn match rOpError  '//'
syn match rOpError  '&&&'
syn match rOpError  '|||'
syn match rOpError  '<<'
syn match rOpError  '>>'

250 251
syn match rAssign "<\{1,2}-"
syn match rAssign "->\{1,2}"
252 253 254 255 256 257 258 259 260

" Special
syn match rDelimiter "[,;:]"

" Error
if exists("g:r_syntax_folding")
  syn region rRegion matchgroup=Delimiter start=/(/ matchgroup=Delimiter end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError fold
  syn region rRegion matchgroup=Delimiter start=/{/ matchgroup=Delimiter end=/}/ transparent contains=ALLBUT,rError,rBraceError,rParenError fold
  syn region rRegion matchgroup=Delimiter start=/\[/ matchgroup=Delimiter end=/]/ transparent contains=ALLBUT,rError,rCurlyError,rParenError fold
Justin M. Keyes's avatar
Justin M. Keyes committed
261
  syn region rSection matchgroup=Title start=/^#.*[-=#]\{4,}/ end=/^#.*[-=#]\{4,}/ms=s-2,me=s-1 transparent contains=ALL fold
262 263 264 265 266 267 268 269 270 271 272
else
  syn region rRegion matchgroup=Delimiter start=/(/ matchgroup=Delimiter end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError
  syn region rRegion matchgroup=Delimiter start=/{/ matchgroup=Delimiter end=/}/ transparent contains=ALLBUT,rError,rBraceError,rParenError
  syn region rRegion matchgroup=Delimiter start=/\[/ matchgroup=Delimiter end=/]/ transparent contains=ALLBUT,rError,rCurlyError,rParenError
endif

syn match rError      "[)\]}]"
syn match rBraceError "[)}]" contained
syn match rCurlyError "[)\]]" contained
syn match rParenError "[\]}]" contained

Justin M. Keyes's avatar
Justin M. Keyes committed
273 274 275 276 277 278 279 280
" Use Nvim-R to highlight functions dynamically if it is installed
if !exists("g:r_syntax_fun_pattern")
  let s:ff = split(substitute(globpath(&rtp, "R/functions.vim"), "functions.vim", "", "g"), "\n")
  if len(s:ff) > 0
    let g:r_syntax_fun_pattern = 0
  else
    let g:r_syntax_fun_pattern = 1
  endif
James McCoy's avatar
James McCoy committed
281
endif
Justin M. Keyes's avatar
Justin M. Keyes committed
282 283 284 285 286 287

" Only use Nvim-R to highlight functions if they should not be highlighted
" according to a generic pattern
if g:r_syntax_fun_pattern == 1
  syn match rFunction '[0-9a-zA-Z_\.]\+\s*\ze('
else
Justin M. Keyes's avatar
Justin M. Keyes committed
288 289
  " Nvim-R:
  runtime R/functions.vim
290
endif
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

syn match rDollar display contained "\$"
syn match rDollar display contained "@"

" List elements will not be highlighted as functions:
syn match rLstElmt "\$[a-zA-Z0-9\\._]*" contains=rDollar
syn match rLstElmt "@[a-zA-Z0-9\\._]*" contains=rDollar

" Functions that may add new objects
syn keyword rPreProc     library require attach detach source

if &filetype == "rhelp"
  syn match rHelpIdent '\\method'
  syn match rHelpIdent '\\S4method'
endif

" Type
308
syn keyword rType array category character complex double function integer list logical matrix numeric vector data.frame
309 310 311

" Name of object with spaces
if &filetype != "rmd" && &filetype != "rrst"
Justin M. Keyes's avatar
Justin M. Keyes committed
312
  syn region rNameWSpace start="`" end="`" contains=rSpaceFun
313 314 315
endif

if &filetype == "rhelp"
316 317
  syn match rhPreProc "^#ifdef.*"
  syn match rhPreProc "^#endif.*"
318 319 320
  syn match rhSection "\\dontrun\>"
endif

Justin M. Keyes's avatar
Justin M. Keyes committed
321 322
if exists("r_syntax_minlines")
  exe "syn sync minlines=" . r_syntax_minlines
323 324 325 326
else
  syn sync minlines=40
endif

327
" Define the default highlighting.
328
hi def link rAssign      Statement
329 330 331
hi def link rBoolean     Boolean
hi def link rBraceError  Error
hi def link rComment     Comment
Justin M. Keyes's avatar
Justin M. Keyes committed
332 333
hi def link rTodoParen   Comment
hi def link rTodoInfo    SpecialComment
334
hi def link rCommentTodo Todo
Justin M. Keyes's avatar
Justin M. Keyes committed
335
hi def link rTodoKeyw    Todo
336 337 338 339 340 341 342 343 344
hi def link rComplex     Number
hi def link rConditional Conditional
hi def link rConstant    Constant
hi def link rCurlyError  Error
hi def link rDelimiter   Delimiter
hi def link rDollar      SpecialChar
hi def link rError       Error
hi def link rFloat       Float
hi def link rFunction    Function
Justin M. Keyes's avatar
Justin M. Keyes committed
345
hi def link rSpaceFun    Function
346 347 348 349
hi def link rHelpIdent   Identifier
hi def link rhPreProc    PreProc
hi def link rhSection    PreCondit
hi def link rInteger     Number
350
hi def link rLstElmt     Normal
351 352 353 354 355 356 357 358 359 360 361 362
hi def link rNameWSpace  Normal
hi def link rNumber      Number
hi def link rOperator    Operator
hi def link rOpError     Error
hi def link rParenError  Error
hi def link rPreProc     PreProc
hi def link rRepeat      Repeat
hi def link rSpecial     SpecialChar
hi def link rStatement   Statement
hi def link rString      String
hi def link rStrError    Error
hi def link rType        Type
Justin M. Keyes's avatar
Justin M. Keyes committed
363 364 365 366 367 368
if g:r_syntax_hl_roxygen
  hi def link rOTitleTag   Operator
  hi def link rOTag        Operator
  hi def link rOTitleBlock Title
  hi def link rOBlock         Comment
  hi def link rOBlockNoTitle  Comment
James McCoy's avatar
James McCoy committed
369 370 371 372
  hi def link rOTitle      Title
  hi def link rOCommentKey Comment
  hi def link rOExamples   SpecialComment
endif
KillTheMule's avatar
KillTheMule committed
373

374 375 376
let b:current_syntax="r"

" vim: ts=8 sw=2