erlang.vim (18655B)
1 " Vim syntax file 2 " Language: Erlang (http://www.erlang.org) 3 " Maintainer: Csaba Hoch <csaba.hoch@gmail.com> 4 " Contributor: Adam Rutkowski <hq@mtod.org> 5 " Johannes Christ <jc@jchri.st> 6 " Last Update: 2026-Jun-14 7 " License: Vim license 8 " URL: https://github.com/vim-erlang/vim-erlang-runtime 9 10 " Acknowledgements: This script was originally created by Kresimir Marzic [1]. 11 " The script was then revamped by Csaba Hoch [2]. During the revamp, the new 12 " highlighting style and some code was taken from the Erlang syntax script 13 " that is part of vimerl [3], created by Oscar Hellström [4] and improved by 14 " Ricardo Catalinas Jiménez [5]. 15 16 " [1]: Kreąimir Marľić (Kresimir Marzic) <kmarzic@fly.srk.fer.hr> 17 " [2]: Csaba Hoch <csaba.hoch@gmail.com> 18 " [3]: https://github.com/jimenezrick/vimerl 19 " [4]: Oscar Hellström <oscar@oscarh.net> (http://oscar.hellstrom.st) 20 " [5]: Ricardo Catalinas Jiménez <jimenezrick@gmail.com> 21 22 " Customization: 23 " 24 " To use the old highlighting style, add this to your .vimrc: 25 " 26 " let g:erlang_old_style_highlight = 1 27 28 " quit when a syntax file was already loaded 29 if exists("b:current_syntax") 30 finish 31 endif 32 33 if !exists('g:main_syntax') 34 " This is an Erlang source file, and this is the main execution of 35 " syntax/erlang.vim. 36 let g:main_syntax = 'erlang' 37 elseif g:main_syntax == 'erlang' 38 " This is an Erlang source file, and this is an inner execution of 39 " syntax/erlang.vim. For example: 40 " 41 " 1. The main execution of syntax/erlang.vim included syntax/markdown.vim 42 " because "g:erlang_use_markdown_for_docs == 1". 43 " 44 " 2. syntax/markdown.vim included syntax/erlang.vim because 45 " "g:markdown_fenced_languages == ['erlang']". This is the inner 46 " execution of syntax/erlang.vim. 47 " 48 " To avoid infinite recursion with Markdown and Erlang including each other, 49 " and to avoid the inner syntax/erlang.vim execution messing up the 50 " variables of the outer erlang.vim execution, we finish executing the inner 51 " erlang.vim. 52 " 53 " In the inner execution, we already have the Erlang syntax items included, 54 " so the highlighting of Erlang within Markdown within Erlang will be 55 " acceptable. It won't highlight Markdown inside Erlang inside Markdown 56 " inside Erlang. 57 finish 58 endif 59 60 let s:cpo_save = &cpo 61 set cpo&vim 62 63 " "g:erlang_old_style_highlight": Whether to use old style highlighting. 64 " 65 " * "g:erlang_old_style_highlight == 0" (default): Use new style 66 " highlighting. 67 " 68 " * "g:erlang_old_style_highlight == 1": Use old style highlighting. 69 let s:old_style = (exists("g:erlang_old_style_highlight") && 70 \g:erlang_old_style_highlight == 1) 71 72 " "g:erlang_use_markdown_for_docs": Whether to use Markdown highlighting in 73 " docstrings. 74 " 75 " * "g:erlang_use_markdown_for_docs == 1": Enable Markdown highlighting in 76 " docstrings. 77 " 78 " * "g:erlang_use_markdown_for_docs == 0" (default): Disable Markdown 79 " highlighting in docstrings. 80 " 81 " If "g:main_syntax" is not 'erlang', this is not an Erlang source file but 82 " for example a Markdown file, and syntax/markdown.vim is including 83 " syntax/erlang.vim. To avoid infinite recursion with Markdown and Erlang 84 " including each other, we disable sourcing syntax/markdown.vim in this case. 85 if exists("g:erlang_use_markdown_for_docs") && g:main_syntax == 'erlang' 86 let s:use_markdown = g:erlang_use_markdown_for_docs 87 else 88 let s:use_markdown = 0 89 endif 90 91 " "g:erlang_docstring_default_highlight": How to highlight the text inside 92 " docstrings (except the text which is highlighted by Markdown). 93 " 94 " If "g:erlang_use_markdown_for_docs == 1": 95 " 96 " * "g:erlang_docstring_default_highlight == 'Comment'" (default): the plugin 97 " highlights the plain text inside Markdown as Markdown normally does, 98 " with comment highlighting to regular text in the docstring. 99 " 100 " * If you set g:erlang_docstring_default_highlight to the name of highlight 101 " group, for example "String", the plugin highlights the plain text inside 102 " Markdown with the specified highlight group. See ":highlight" for the 103 " available groups. You may also set it to an empty string to disable any 104 " specific highlighting. 105 " 106 " If "g:erlang_use_markdown_for_docs == 0": 107 " 108 " * "g:erlang_docstring_default_highlight == 'Comment'" (default): the plugin 109 " does not highlight the contents of the docstring as markdown, but 110 " continues to display them in the style of comments. 111 " 112 " * If you set g:erlang_docstring_default_highlight to the name of highlight 113 " group, for example "String", the plugin highlights the plain text inside 114 " Markdown with the specified highlight group. See ":highlight" for the 115 " available groups. You may also set it to an empty string to disable any 116 " specific highlighting. 117 " 118 " Configuration examples: 119 " 120 " " Highlight docstrings as Markdown. 121 " let g:erlang_use_markdown_for_docs = 1 122 " let g:erlang_docstring_default_highlight = 'Comment' 123 " 124 " " 1. Highlight Markdown elements in docstrings as Markdown. 125 " " 2. Highlight the plain text in docstrings as String. 126 " let g:erlang_use_markdown_for_docs = 1 127 " let g:erlang_docstring_default_highlight = 'String' 128 " 129 " " Highlight docstrings as strings. 130 " let g:erlang_use_markdown_for_docs = 0 131 " let g:erlang_docstring_default_highlight = 'String' 132 " 133 " " Highlight docstrings as comments (default). 134 " let g:erlang_use_markdown_for_docs = 0 135 " let g:erlang_docstring_default_highlight = 'Comment' 136 if exists("g:erlang_docstring_default_highlight") 137 let s:docstring_default_highlight = g:erlang_docstring_default_highlight 138 else 139 let s:docstring_default_highlight = 'Comment' 140 endif 141 142 " Case sensitive 143 syn case match 144 145 setlocal iskeyword+=$,@-@ 146 147 " Comments 148 syn match erlangComment '%.*$' contains=erlangCommentAnnotation,erlangTodo 149 syn match erlangCommentAnnotation ' \@<=@\%(clear\|docfile\|end\|headerfile\|todo\|TODO\|type\|author\|copyright\|doc\|reference\|see\|since\|title\|version\|deprecated\|hidden\|param\|private\|equiv\|spec\|throws\)' contained 150 syn match erlangCommentAnnotation /`[^']*'/ contained 151 syn keyword erlangTodo TODO FIXME XXX contained 152 153 " Numbers (minimum base is 2, maximum is 36.) 154 syn match erlangNumberInteger '\<\d\+\>' 155 syn match erlangNumberInteger '\<\%([2-9]\|[12]\d\|3[0-6]\)\+#[[:alnum:]]\+\>' 156 syn match erlangNumberFloat '\<\d\+\.\d\+\%([eE][+-]\=\d\+\)\=\>' 157 158 " Strings, atoms, characters 159 syn region erlangString start=/"/ end=/"/ contains=erlangStringModifier 160 syn region erlangStringTripleQuoted matchgroup=String start=/"""/ end=/\%(^\s*\)\@<="""/ keepend 161 162 " Documentation 163 syn region erlangDocString start=/^-\%(module\)\=doc\s*\~\="/ end=/"\.$/ contains=@erlangDocStringCluster keepend 164 syn region erlangDocString start=/^-\%(module\)\=doc\s*<<"/ end=/">>\.$/ contains=@erlangDocStringCluster keepend 165 syn region erlangDocString start=/^-\%(module\)\=doc\s*\~\="""/ end=/\%(^\s*\)\@<="""\.$/ contains=@erlangDocStringCluster keepend 166 syn region erlangDocString start=/^-\%(module\)\=doc\s*<<"""/ end=/\%(^\s*\)\@<=""">>\.$/ contains=@erlangDocStringCluster keepend 167 syn cluster erlangDocStringCluster contains=erlangInnerDocAttribute,erlangDocStringDelimiter 168 syn region erlangDocStringDelimiter matchgroup=erlangString start=/"/ end=/"/ contains=@erlangDocStringContained contained 169 syn region erlangDocStringDelimiter matchgroup=erlangString start=/"""/ end=/"""/ contains=@erlangDocStringContained contained 170 171 if s:use_markdown 172 syn cluster erlangDocStringContained contains=@markdown 173 endif 174 175 syn region erlangQuotedAtom start=/'/ end=/'/ contains=erlangQuotedAtomModifier 176 syn match erlangStringModifier '\\\%(\o\{1,3}\|x\x\x\|x{\x\+}\|\^.\|.\)\|\~\%([ni~]\|\%(-\=\d\+\|\*\)\=\.\=\%(\*\|\d\+\)\=\%(\..\)\=[tl]*[cfegswpWPBX#bx+]\)' contained 177 syn match erlangQuotedAtomModifier '\\\%(\o\{1,3}\|x\x\x\|x{\x\+}\|\^.\|.\)' contained 178 syn match erlangModifier '\$\%([^\\]\|\\\%(\o\{1,3}\|x\x\x\|x{\x\+}\|\^.\|.\)\)' 179 180 " Operators, separators 181 syn match erlangOperator '=' 182 syn match erlangOperator '+' 183 syn match erlangOperator '-' 184 syn match erlangOperator '*' 185 syn match erlangOperator '/' 186 syn match erlangOperator '<' 187 syn match erlangOperator '>' 188 syn match erlangOperator '!' 189 syn match erlangOperator '=<' 190 syn match erlangOperator '>=' 191 syn match erlangOperator '=>' 192 syn match erlangOperator '==' 193 syn match erlangOperator '=:=' 194 syn match erlangOperator '/=' 195 syn match erlangOperator '=/=' 196 syn match erlangOperator ':=' 197 syn match erlangOperator '?=' 198 syn match erlangOperator '++' 199 syn match erlangOperator '--' 200 syn match erlangOperator '<-' 201 202 " erlangEqualsBinary makes the syntax reflect how Erlang parses the following: 203 " 204 " A=<<<"binary">> 205 " Valid, same as: A =< <<"binary">> 206 " A==<<"binary">> 207 " Valid, same as: A == <<"binary">> 208 " A=<<"binary">> 209 " Invalid, parsed as: A =< <"binary">>. This is invalid because there are no 210 " "<<" before the binary, only "<". 211 syn match erlangEqualsBinary '=<<\%(<\)\@!' 212 213 syn keyword erlangOperator div rem or xor bor bxor bsl bsr and band not bnot andalso orelse 214 syn match erlangBracket '{\|}\|\[\|]\||\|||' 215 syn match erlangPipe '|' 216 syn match erlangRightArrow '->' 217 218 " Atoms, function calls (order is important) 219 syn match erlangAtom '\<\l[[:alnum:]_@]*' contains=erlangBoolean 220 syn keyword erlangBoolean true false contained 221 syn match erlangLocalFuncCall '\<\a[[:alnum:]_@]*\>\%(\%(\s\|\n\|%.*\n\)*(\)\@=' contains=erlangBIF 222 syn match erlangLocalFuncRef '\<\a[[:alnum:]_@]*\>\%(\%(\s\|\n\|%.*\n\)*/\)\@=' 223 syn match erlangGlobalFuncCall '\<\%(\a[[:alnum:]_@]*\%(\s\|\n\|%.*\n\)*\.\%(\s\|\n\|%.*\n\)*\)*\a[[:alnum:]_@]*\%(\s\|\n\|%.*\n\)*:\%(\s\|\n\|%.*\n\)*\a[[:alnum:]_@]*\>\%(\%(\s\|\n\|%.*\n\)*(\)\@=' contains=erlangComment,erlangVariable 224 syn match erlangGlobalFuncRef '\<\%(\a[[:alnum:]_@]*\%(\s\|\n\|%.*\n\)*\.\%(\s\|\n\|%.*\n\)*\)*\a[[:alnum:]_@]*\%(\s\|\n\|%.*\n\)*:\%(\s\|\n\|%.*\n\)*\a[[:alnum:]_@]*\>\%(\%(\s\|\n\|%.*\n\)*/\)\@=' contains=erlangComment,erlangVariable 225 226 " Variables, macros, records, maps 227 syn match erlangVariable '\<[A-Z][[:alnum:]_@]*' 228 syn match erlangAnonymousVariable '\<_[[:alnum:]_@]*' 229 syn match erlangMacro '??\=[[:alnum:]_@]\+' 230 syn match erlangMacro '\%(-define(\)\@<=[[:alnum:]_@]\+' 231 syn region erlangQuotedMacro start=/??\=\s*'/ end=/'/ contains=erlangQuotedAtomModifier 232 syn match erlangMap '#' 233 syn match erlangRecord '#\s*\l[[:alnum:]_@]*' 234 syn region erlangQuotedRecord start=/#\s*'/ end=/'/ contains=erlangQuotedAtomModifier 235 236 " Shebang (this line has to be after the ErlangMap) 237 syn match erlangShebang '^#!.*' 238 239 " Bitstrings 240 syn match erlangBitType '\%#=1\%(\/\%(\s\|\n\|%.*\n\)*\)\@<=\<\%(integer\|float\|binary\|bytes\|bitstring\|bits\|binary\|utf8\|utf16\|utf32\|signed\|unsigned\|big\|little\|native\|unit\)\%(\%(\s\|\n\|%.*\n\)*-\%(\s\|\n\|%.*\n\)*\<\%(integer\|float\|binary\|bytes\|bitstring\|bits\|binary\|utf8\|utf16\|utf32\|signed\|unsigned\|big\|little\|native\|unit\)\)*' contains=erlangComment 241 242 " Constants and Directives 243 syn match erlangUnknownAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\l[[:alnum:]_@]*' contains=erlangComment 244 syn match erlangAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\%(behaviou\=r\|compile\|dialyzer\|export\|export_type\|file\|import\|module\|author\|copyright\|vsn\|on_load\|optional_callbacks\|feature\|mode\)\>' contains=erlangComment 245 syn match erlangDocAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\%(moduledoc\|doc\)\>' contains=erlangComment,erlangDocString 246 syn match erlangInnerDocAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\%(moduledoc\|doc\)\>' contained 247 syn match erlangInclude '^\s*-\%(\s\|\n\|%.*\n\)*\%(include\|include_lib\)\>' contains=erlangComment 248 syn match erlangRecordDef '^\s*-\%(\s\|\n\|%.*\n\)*record\>' contains=erlangComment 249 syn match erlangDefine '^\s*-\%(\s\|\n\|%.*\n\)*\%(define\|undef\)\>' contains=erlangComment 250 syn match erlangPreCondit '^\s*-\%(\s\|\n\|%.*\n\)*\%(ifdef\|ifndef\|else\|if\|elif\|endif\)\>' contains=erlangComment 251 syn match erlangType '^\s*-\%(\s\|\n\|%.*\n\)*\%(spec\|type\|opaque\|nominal\|callback\)\>' contains=erlangComment 252 253 " Keywords 254 syn keyword erlangKeyword after begin case catch cond end fun if let of else 255 syn keyword erlangKeyword receive when try maybe 256 257 " Build-in-functions (BIFs) 258 syn keyword erlangBIF abs alive apply atom_to_binary atom_to_list contained 259 syn keyword erlangBIF binary_part binary_to_atom contained 260 syn keyword erlangBIF binary_to_existing_atom binary_to_float contained 261 syn keyword erlangBIF binary_to_integer bitstring_to_list contained 262 syn keyword erlangBIF binary_to_list binary_to_term bit_size contained 263 syn keyword erlangBIF byte_size check_old_code check_process_code contained 264 syn keyword erlangBIF concat_binary date delete_module demonitor contained 265 syn keyword erlangBIF disconnect_node element erase error exit contained 266 syn keyword erlangBIF float float_to_binary float_to_list contained 267 syn keyword erlangBIF garbage_collect get get_keys group_leader contained 268 syn keyword erlangBIF halt hd integer_to_binary integer_to_list contained 269 syn keyword erlangBIF iolist_to_binary iolist_size is_alive contained 270 syn keyword erlangBIF is_atom is_binary is_bitstring is_boolean contained 271 syn keyword erlangBIF is_float is_function is_integer is_list is_map is_map_key contained 272 syn keyword erlangBIF is_number is_pid is_port is_process_alive contained 273 syn keyword erlangBIF is_record is_reference is_tuple length link contained 274 syn keyword erlangBIF list_to_atom list_to_binary contained 275 syn keyword erlangBIF list_to_bitstring list_to_existing_atom contained 276 syn keyword erlangBIF list_to_float list_to_integer list_to_pid contained 277 syn keyword erlangBIF list_to_tuple load_module make_ref map_size max contained 278 syn keyword erlangBIF min module_loaded monitor monitor_node node contained 279 syn keyword erlangBIF nodes now open_port pid_to_list port_close contained 280 syn keyword erlangBIF port_command port_connect pre_loaded contained 281 syn keyword erlangBIF process_flag process_flag process_info contained 282 syn keyword erlangBIF process purge_module put register registered contained 283 syn keyword erlangBIF round self setelement size spawn spawn_link contained 284 syn keyword erlangBIF spawn_monitor spawn_opt split_binary contained 285 syn keyword erlangBIF statistics term_to_binary throw time tl contained 286 syn keyword erlangBIF trunc tuple_size tuple_to_list unlink contained 287 syn keyword erlangBIF unregister whereis contained 288 289 " Sync at the beginning of functions: if this is not used, multiline string 290 " are not always recognized, and the indentation script cannot use the 291 " "searchpair" (because it would not always skip strings and comments when 292 " looking for keywords and opening parens/brackets). 293 syn sync match erlangSync grouphere NONE "^[a-z]\s*(" 294 let b:erlang_syntax_synced = 1 295 296 " Define the default highlighting. See ":help group-name" for the groups and 297 " their colors. 298 299 if s:use_markdown 300 " Add markdown syntax elements for docstrings (actually, for all 301 " triple-quoted strings). 302 unlet! b:current_syntax 303 304 syn include @markdown syntax/markdown.vim 305 let b:current_syntax = "erlang" 306 307 " markdown-erlang.vim includes html.vim, which includes css.vim, which adds 308 " the dash character (-) to the list of syntax keywords, which causes 309 " `-VarName` not to be highlighted as a variable in the Erlang code. 310 " 311 " Here we override that. 312 syntax iskeyword @,48-57,192-255,$,_ 313 endif 314 315 " Comments 316 hi def link erlangComment Comment 317 hi def link erlangCommentAnnotation Special 318 hi def link erlangTodo Todo 319 hi def link erlangShebang Comment 320 321 " Numbers 322 hi def link erlangNumberInteger Number 323 hi def link erlangNumberFloat Float 324 325 " Strings, atoms, characters 326 hi def link erlangString String 327 hi def link erlangStringTripleQuoted String 328 329 " Triple quoted strings 330 if s:docstring_default_highlight != '' 331 execute 'hi def link erlangDocStringDelimiter '. s:docstring_default_highlight 332 endif 333 334 if s:old_style 335 hi def link erlangQuotedAtom Type 336 else 337 hi def link erlangQuotedAtom String 338 endif 339 340 hi def link erlangStringModifier Special 341 hi def link erlangQuotedAtomModifier Special 342 hi def link erlangModifier Special 343 344 " Operators, separators 345 hi def link erlangOperator Operator 346 hi def link erlangEqualsBinary ErrorMsg 347 hi def link erlangRightArrow Operator 348 if s:old_style 349 hi def link erlangBracket Normal 350 hi def link erlangPipe Normal 351 else 352 hi def link erlangBracket Delimiter 353 hi def link erlangPipe Delimiter 354 endif 355 356 " Atoms, functions, variables, macros 357 if s:old_style 358 hi def link erlangAtom Normal 359 hi def link erlangLocalFuncCall Normal 360 hi def link erlangLocalFuncRef Normal 361 hi def link erlangGlobalFuncCall Function 362 hi def link erlangGlobalFuncRef Function 363 hi def link erlangVariable Normal 364 hi def link erlangAnonymousVariable erlangVariable 365 hi def link erlangMacro Normal 366 hi def link erlangQuotedMacro Normal 367 hi def link erlangRecord Normal 368 hi def link erlangQuotedRecord Normal 369 hi def link erlangMap Normal 370 else 371 hi def link erlangAtom String 372 hi def link erlangLocalFuncCall Normal 373 hi def link erlangLocalFuncRef Normal 374 hi def link erlangGlobalFuncCall Normal 375 hi def link erlangGlobalFuncRef Normal 376 hi def link erlangVariable Identifier 377 hi def link erlangAnonymousVariable erlangVariable 378 hi def link erlangMacro Macro 379 hi def link erlangQuotedMacro Macro 380 hi def link erlangRecord Structure 381 hi def link erlangQuotedRecord Structure 382 hi def link erlangMap Structure 383 endif 384 385 " Bitstrings 386 if !s:old_style 387 hi def link erlangBitType Type 388 endif 389 390 " Constants and Directives 391 if s:old_style 392 hi def link erlangAttribute Type 393 hi def link erlangMacroDef Type 394 hi def link erlangUnknownAttribute Normal 395 hi def link erlangInclude Type 396 hi def link erlangRecordDef Type 397 hi def link erlangDefine Type 398 hi def link erlangPreCondit Type 399 hi def link erlangType Type 400 else 401 hi def link erlangAttribute Keyword 402 hi def link erlangDocAttribute Keyword 403 hi def link erlangInnerDocAttribute Keyword 404 hi def link erlangMacroDef Macro 405 hi def link erlangUnknownAttribute Normal 406 hi def link erlangInclude Include 407 hi def link erlangRecordDef Keyword 408 hi def link erlangDefine Define 409 hi def link erlangPreCondit PreCondit 410 hi def link erlangType Type 411 endif 412 413 " Keywords 414 hi def link erlangKeyword Keyword 415 416 " Build-in-functions (BIFs) 417 hi def link erlangBIF Function 418 419 if s:old_style 420 hi def link erlangBoolean Statement 421 hi def link erlangExtra Statement 422 hi def link erlangSignal Statement 423 else 424 hi def link erlangBoolean Boolean 425 hi def link erlangExtra Statement 426 hi def link erlangSignal Statement 427 endif 428 429 let b:current_syntax = "erlang" 430 431 if g:main_syntax ==# 'erlang' 432 unlet g:main_syntax 433 endif 434 435 let &cpo = s:cpo_save 436 unlet s:cpo_save 437 438 " vim: sw=2 et