r.vim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. " Vim syntax file
  2. " Language: R (GNU S)
  3. " Maintainer: Jakson Aquino <jalvesaq@gmail.com>
  4. " Former Maintainers: Vaidotas Zemlys <zemlys@gmail.com>
  5. " Tom Payne <tom@tompayne.org>
  6. " Contributor: Johannes Ranke <jranke@uni-bremen.de>
  7. " Homepage: https://github.com/jalvesaq/R-Vim-runtime
  8. " Last Change: Sun Mar 28, 2021 01:47PM
  9. " Filenames: *.R *.r *.Rhistory *.Rt
  10. "
  11. " NOTE: The highlighting of R functions might be defined in
  12. " runtime files created by a filetype plugin, if installed.
  13. "
  14. " CONFIGURATION:
  15. " Syntax folding can be turned on by
  16. "
  17. " let r_syntax_folding = 1
  18. "
  19. " ROxygen highlighting can be turned off by
  20. "
  21. " let r_syntax_hl_roxygen = 0
  22. "
  23. " Some lines of code were borrowed from Zhuojun Chen.
  24. if exists("b:current_syntax")
  25. finish
  26. endif
  27. if has("patch-7.4.1142")
  28. syn iskeyword @,48-57,_,.
  29. else
  30. setlocal iskeyword=@,48-57,_,.
  31. endif
  32. " The variables g:r_hl_roxygen and g:r_syn_minlines were renamed on April 8, 2017.
  33. if exists("g:r_hl_roxygen")
  34. let g:r_syntax_hl_roxygen = g:r_hl_roxygen
  35. endif
  36. if exists("g:r_syn_minlines")
  37. let g:r_syntax_minlines = g:r_syn_minlines
  38. endif
  39. if exists("g:r_syntax_folding") && g:r_syntax_folding
  40. setlocal foldmethod=syntax
  41. endif
  42. let g:r_syntax_hl_roxygen = get(g:, 'r_syntax_hl_roxygen', 1)
  43. syn case match
  44. " Comment
  45. syn match rCommentTodo contained "\(BUG\|FIXME\|NOTE\|TODO\):"
  46. syn match rTodoParen contained "\(BUG\|FIXME\|NOTE\|TODO\)\s*(.\{-})\s*:" contains=rTodoKeyw,rTodoInfo transparent
  47. syn keyword rTodoKeyw BUG FIXME NOTE TODO contained
  48. syn match rTodoInfo "(\zs.\{-}\ze)" contained
  49. syn match rComment contains=@Spell,rCommentTodo,rTodoParen "#.*"
  50. " Roxygen
  51. if g:r_syntax_hl_roxygen
  52. " A roxygen block can start at the beginning of a file (first version) and
  53. " after a blank line (second version). It ends when a line appears that does not
  54. " contain a roxygen comment. In the following comments, any line containing
  55. " a roxygen comment marker (one or two hash signs # followed by a single
  56. " quote ' and preceded only by whitespace) is called a roxygen line. A
  57. " roxygen line containing only a roxygen comment marker, optionally followed
  58. " by whitespace is called an empty roxygen line.
  59. " First we match all roxygen blocks as containing only a title. In case an
  60. " empty roxygen line ending the title or a tag is found, this will be
  61. " overridden later by the definitions of rOBlock.
  62. syn match rOTitleBlock "\%^\(\s*#\{1,2}' .*\n\)\{1,}" contains=rOCommentKey,rOTitleTag
  63. syn match rOTitleBlock "^\s*\n\(\s*#\{1,2}' .*\n\)\{1,}" contains=rOCommentKey,rOTitleTag
  64. " A title as part of a block is always at the beginning of the block, i.e.
  65. " either at the start of a file or after a completely empty line.
  66. syn match rOTitle "\%^\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*$" contained contains=rOCommentKey,rOTitleTag
  67. syn match rOTitle "^\s*\n\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*$" contained contains=rOCommentKey,rOTitleTag
  68. syn match rOTitleTag contained "@title"
  69. " When a roxygen block has a title and additional content, the title
  70. " consists of one or more roxygen lines (as little as possible are matched),
  71. " followed either by an empty roxygen line
  72. syn region rOBlock start="\%^\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*$" end="^\s*\(#\{1,2}'\)\@!" contains=rOTitle,rOTag,rOExamples,@Spell keepend fold
  73. 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
  74. " or by a roxygen tag (we match everything starting with @ but not @@ which is used as escape sequence for a literal @).
  75. syn region rOBlock start="\%^\(\s*#\{1,2}' .*\n\)\{-}\s*#\{1,2}' @\(@\)\@!" end="^\s*\(#\{1,2}'\)\@!" contains=rOTitle,rOTag,rOExamples,@Spell keepend fold
  76. syn region rOBlock start="^\s*\n\(\s*#\{1,2}' .*\n\)\{-}\s*#\{1,2}' @\(@\)\@!" end="^\s*\(#\{1,2}'\)\@!" contains=rOTitle,rOTag,rOExamples,@Spell keepend fold
  77. " If a block contains an @rdname, @describeIn tag, it may have paragraph breaks, but does not have a title
  78. 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
  79. 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
  80. 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
  81. 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
  82. syn match rOCommentKey "^\s*#\{1,2}'" contained
  83. syn region rOExamples start="^\s*#\{1,2}' @examples.*"rs=e+1,hs=e+1 end="^\(#\{1,2}' @.*\)\@=" end="^\(#\{1,2}'\)\@!" contained contains=rOTag fold
  84. " R6 classes may contain roxygen lines independent of roxygen blocks
  85. syn region rOR6Class start=/R6Class(/ end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError fold
  86. syn match rOR6Block "#\{1,2}'.*" contains=rOTag,rOExamples,@Spell containedin=rOR6Class contained
  87. syn match rOR6Block "^\s*#\{1,2}'.*" contains=rOTag,rOExamples,@Spell containedin=rOR6Class contained
  88. " rOTag list originally generated from the lists that were available in
  89. " https://github.com/klutometis/roxygen/R/rd.R and
  90. " https://github.com/klutometis/roxygen/R/namespace.R
  91. " using s/^ \([A-Za-z0-9]*\) = .*/ syn match rOTag contained "@\1"/
  92. " Plus we need the @include tag
  93. " rd.R
  94. syn match rOTag contained "@aliases"
  95. syn match rOTag contained "@author"
  96. syn match rOTag contained "@backref"
  97. syn match rOTag contained "@concept"
  98. syn match rOTag contained "@describeIn"
  99. syn match rOTag contained "@description"
  100. syn match rOTag contained "@details"
  101. syn match rOTag contained "@docType"
  102. syn match rOTag contained "@encoding"
  103. syn match rOTag contained "@evalRd"
  104. syn match rOTag contained "@example"
  105. syn match rOTag contained "@examples"
  106. syn match rOTag contained "@family"
  107. syn match rOTag contained "@field"
  108. syn match rOTag contained "@format"
  109. syn match rOTag contained "@inherit"
  110. syn match rOTag contained "@inheritParams"
  111. syn match rOTag contained "@inheritDotParams"
  112. syn match rOTag contained "@inheritSection"
  113. syn match rOTag contained "@keywords"
  114. syn match rOTag contained "@method"
  115. syn match rOTag contained "@name"
  116. syn match rOTag contained "@md"
  117. syn match rOTag contained "@noMd"
  118. syn match rOTag contained "@noRd"
  119. syn match rOTag contained "@note"
  120. syn match rOTag contained "@param"
  121. syn match rOTag contained "@rdname"
  122. syn match rOTag contained "@rawRd"
  123. syn match rOTag contained "@references"
  124. syn match rOTag contained "@return"
  125. syn match rOTag contained "@section"
  126. syn match rOTag contained "@seealso"
  127. syn match rOTag contained "@slot"
  128. syn match rOTag contained "@source"
  129. syn match rOTag contained "@template"
  130. syn match rOTag contained "@templateVar"
  131. syn match rOTag contained "@title"
  132. syn match rOTag contained "@usage"
  133. " namespace.R
  134. syn match rOTag contained "@export"
  135. syn match rOTag contained "@exportClass"
  136. syn match rOTag contained "@exportMethod"
  137. syn match rOTag contained "@exportPattern"
  138. syn match rOTag contained "@import"
  139. syn match rOTag contained "@importClassesFrom"
  140. syn match rOTag contained "@importFrom"
  141. syn match rOTag contained "@importMethodsFrom"
  142. syn match rOTag contained "@rawNamespace"
  143. syn match rOTag contained "@S3method"
  144. syn match rOTag contained "@useDynLib"
  145. " other
  146. syn match rOTag contained "@eval"
  147. syn match rOTag contained "@include"
  148. syn match rOTag contained "@includeRmd"
  149. syn match rOTag contained "@order"
  150. endif
  151. if &filetype == "rhelp"
  152. " string enclosed in double quotes
  153. syn region rString contains=rSpecial,@Spell start=/"/ skip=/\\\\\|\\"/ end=/"/
  154. " string enclosed in single quotes
  155. syn region rString contains=rSpecial,@Spell start=/'/ skip=/\\\\\|\\'/ end=/'/
  156. else
  157. " string enclosed in double quotes
  158. syn region rString contains=rSpecial,rStrError,@Spell start=/"/ skip=/\\\\\|\\"/ end=/"/
  159. " string enclosed in single quotes
  160. syn region rString contains=rSpecial,rStrError,@Spell start=/'/ skip=/\\\\\|\\'/ end=/'/
  161. endif
  162. syn match rStrError display contained "\\."
  163. " New line, carriage return, tab, backspace, bell, feed, vertical tab, backslash
  164. syn match rSpecial display contained "\\\(n\|r\|t\|b\|a\|f\|v\|'\|\"\)\|\\\\"
  165. " Hexadecimal and Octal digits
  166. syn match rSpecial display contained "\\\(x\x\{1,2}\|[0-8]\{1,3}\)"
  167. " Unicode characters
  168. syn match rSpecial display contained "\\u\x\{1,4}"
  169. syn match rSpecial display contained "\\U\x\{1,8}"
  170. syn match rSpecial display contained "\\u{\x\{1,4}}"
  171. syn match rSpecial display contained "\\U{\x\{1,8}}"
  172. " Raw string
  173. syn region rRawString matchgroup=rRawStrDelim start=/[rR]\z(['"]\)\z(-*\)(/ end=/)\z2\z1/ keepend
  174. syn region rRawString matchgroup=rRawStrDelim start=/[rR]\z(['"]\)\z(-*\){/ end=/}\z2\z1/ keepend
  175. syn region rRawString matchgroup=rRawStrDelim start=/[rR]\z(['"]\)\z(-*\)\[/ end=/\]\z2\z1/ keepend
  176. " Statement
  177. syn keyword rStatement break next return
  178. syn keyword rConditional if else
  179. syn keyword rRepeat for in repeat while
  180. " Constant (not really)
  181. syn keyword rConstant T F LETTERS letters month.abb month.name pi
  182. syn keyword rConstant R.version.string
  183. syn keyword rNumber NA_integer_ NA_real_ NA_complex_ NA_character_
  184. " Constants
  185. syn keyword rConstant NULL
  186. syn keyword rBoolean FALSE TRUE
  187. syn keyword rNumber NA Inf NaN
  188. " integer
  189. syn match rInteger "\<\d\+L"
  190. syn match rInteger "\<0x\([0-9]\|[a-f]\|[A-F]\)\+L"
  191. syn match rInteger "\<\d\+[Ee]+\=\d\+L"
  192. " number with no fractional part or exponent
  193. syn match rNumber "\<\d\+\>"
  194. " hexadecimal number
  195. syn match rNumber "\<0x\([0-9]\|[a-f]\|[A-F]\)\+"
  196. " floating point number with integer and fractional parts and optional exponent
  197. syn match rFloat "\<\d\+\.\d*\([Ee][-+]\=\d\+\)\="
  198. " floating point number with no integer part and optional exponent
  199. syn match rFloat "\<\.\d\+\([Ee][-+]\=\d\+\)\="
  200. " floating point number with no fractional part and optional exponent
  201. syn match rFloat "\<\d\+[Ee][-+]\=\d\+"
  202. " complex number
  203. syn match rComplex "\<\d\+i"
  204. syn match rComplex "\<\d\++\d\+i"
  205. syn match rComplex "\<0x\([0-9]\|[a-f]\|[A-F]\)\+i"
  206. syn match rComplex "\<\d\+\.\d*\([Ee][-+]\=\d\+\)\=i"
  207. syn match rComplex "\<\.\d\+\([Ee][-+]\=\d\+\)\=i"
  208. syn match rComplex "\<\d\+[Ee][-+]\=\d\+i"
  209. syn match rAssign '='
  210. syn match rOperator "&"
  211. syn match rOperator '-'
  212. syn match rOperator '\*'
  213. syn match rOperator '+'
  214. if &filetype != "rmd" && &filetype != "rrst"
  215. syn match rOperator "[|!<>^~/:]"
  216. else
  217. syn match rOperator "[|!<>^~`/:]"
  218. endif
  219. syn match rOperator "%\{2}\|%\S\{-}%"
  220. syn match rOperator '\([!><]\)\@<=='
  221. syn match rOperator '=='
  222. syn match rOpError '\*\{3}'
  223. syn match rOpError '//'
  224. syn match rOpError '&&&'
  225. syn match rOpError '|||'
  226. syn match rOpError '<<'
  227. syn match rOpError '>>'
  228. syn match rAssign "<\{1,2}-"
  229. syn match rAssign "->\{1,2}"
  230. " Special
  231. syn match rDelimiter "[,;:]"
  232. " Error
  233. if exists("g:r_syntax_folding")
  234. syn region rRegion matchgroup=Delimiter start=/(/ matchgroup=Delimiter end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError fold
  235. syn region rRegion matchgroup=Delimiter start=/{/ matchgroup=Delimiter end=/}/ transparent contains=ALLBUT,rError,rBraceError,rParenError fold
  236. syn region rRegion matchgroup=Delimiter start=/\[/ matchgroup=Delimiter end=/]/ transparent contains=ALLBUT,rError,rCurlyError,rParenError fold
  237. syn region rSection matchgroup=Title start=/^#.*[-=#]\{4,}/ end=/^#.*[-=#]\{4,}/ms=s-2,me=s-1 transparent contains=ALL fold
  238. else
  239. syn region rRegion matchgroup=Delimiter start=/(/ matchgroup=Delimiter end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError
  240. syn region rRegion matchgroup=Delimiter start=/{/ matchgroup=Delimiter end=/}/ transparent contains=ALLBUT,rError,rBraceError,rParenError
  241. syn region rRegion matchgroup=Delimiter start=/\[/ matchgroup=Delimiter end=/]/ transparent contains=ALLBUT,rError,rCurlyError,rParenError
  242. endif
  243. syn match rError "[)\]}]"
  244. syn match rBraceError "[)}]" contained
  245. syn match rCurlyError "[)\]]" contained
  246. syn match rParenError "[\]}]" contained
  247. " Use Nvim-R to highlight functions dynamically if it is installed
  248. if !exists("g:r_syntax_fun_pattern")
  249. let s:ff = split(substitute(globpath(&rtp, "R/functions.vim"), "functions.vim", "", "g"), "\n")
  250. if len(s:ff) > 0
  251. let g:r_syntax_fun_pattern = 0
  252. else
  253. let g:r_syntax_fun_pattern = 1
  254. endif
  255. endif
  256. " Only use Nvim-R to highlight functions if they should not be highlighted
  257. " according to a generic pattern
  258. if g:r_syntax_fun_pattern == 1
  259. syn match rFunction '[0-9a-zA-Z_\.]\+\s*\ze('
  260. else
  261. " Nvim-R:
  262. runtime R/functions.vim
  263. endif
  264. syn match rDollar display contained "\$"
  265. syn match rDollar display contained "@"
  266. " List elements will not be highlighted as functions:
  267. syn match rLstElmt "\$[a-zA-Z0-9\\._]*" contains=rDollar
  268. syn match rLstElmt "@[a-zA-Z0-9\\._]*" contains=rDollar
  269. " Functions that may add new objects
  270. syn keyword rPreProc library require attach detach source
  271. if &filetype == "rhelp"
  272. syn match rHelpIdent '\\method'
  273. syn match rHelpIdent '\\S4method'
  274. endif
  275. " Type
  276. syn keyword rType array category character complex double function integer list logical matrix numeric vector data.frame
  277. " Name of object with spaces
  278. if &filetype != "rmd" && &filetype != "rrst"
  279. syn region rNameWSpace start="`" end="`" contains=rSpaceFun
  280. endif
  281. if &filetype == "rhelp"
  282. syn match rhPreProc "^#ifdef.*"
  283. syn match rhPreProc "^#endif.*"
  284. syn match rhSection "\\dontrun\>"
  285. endif
  286. if exists("r_syntax_minlines")
  287. exe "syn sync minlines=" . r_syntax_minlines
  288. else
  289. syn sync minlines=40
  290. endif
  291. " Define the default highlighting.
  292. hi def link rAssign Statement
  293. hi def link rBoolean Boolean
  294. hi def link rBraceError Error
  295. hi def link rComment Comment
  296. hi def link rTodoParen Comment
  297. hi def link rTodoInfo SpecialComment
  298. hi def link rCommentTodo Todo
  299. hi def link rTodoKeyw Todo
  300. hi def link rComplex Number
  301. hi def link rConditional Conditional
  302. hi def link rConstant Constant
  303. hi def link rCurlyError Error
  304. hi def link rDelimiter Delimiter
  305. hi def link rDollar SpecialChar
  306. hi def link rError Error
  307. hi def link rFloat Float
  308. hi def link rFunction Function
  309. hi def link rSpaceFun Function
  310. hi def link rHelpIdent Identifier
  311. hi def link rhPreProc PreProc
  312. hi def link rhSection PreCondit
  313. hi def link rInteger Number
  314. hi def link rLstElmt Normal
  315. hi def link rNameWSpace Normal
  316. hi def link rNumber Number
  317. hi def link rOperator Operator
  318. hi def link rOpError Error
  319. hi def link rParenError Error
  320. hi def link rPreProc PreProc
  321. hi def link rRawString String
  322. hi def link rRawStrDelim Delimiter
  323. hi def link rRepeat Repeat
  324. hi def link rSpecial SpecialChar
  325. hi def link rStatement Statement
  326. hi def link rString String
  327. hi def link rStrError Error
  328. hi def link rType Type
  329. if g:r_syntax_hl_roxygen
  330. hi def link rOTitleTag Operator
  331. hi def link rOTag Operator
  332. hi def link rOTitleBlock Title
  333. hi def link rOBlock Comment
  334. hi def link rOBlockNoTitle Comment
  335. hi def link rOR6Block Comment
  336. hi def link rOTitle Title
  337. hi def link rOCommentKey Comment
  338. hi def link rOExamples SpecialComment
  339. endif
  340. let b:current_syntax="r"
  341. " vim: ts=8 sw=2