neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

erlang.vim (17827B)


      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:  2025-Nov-12
      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 erlangEqualsBinary '=<<\%(<\)\@!'
    183 syn keyword erlangOperator div rem or xor bor bxor bsl bsr and band not bnot andalso orelse
    184 syn match erlangBracket    '{\|}\|\[\|]\||\|||'
    185 syn match erlangPipe       '|'
    186 syn match erlangRightArrow '->'
    187 
    188 " Atoms, function calls (order is important)
    189 syn match erlangAtom           '\<\l[[:alnum:]_@]*' contains=erlangBoolean
    190 syn keyword erlangBoolean      true false contained
    191 syn match erlangLocalFuncCall  '\<\a[[:alnum:]_@]*\>\%(\%(\s\|\n\|%.*\n\)*(\)\@=' contains=erlangBIF
    192 syn match erlangLocalFuncRef   '\<\a[[:alnum:]_@]*\>\%(\%(\s\|\n\|%.*\n\)*/\)\@='
    193 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
    194 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
    195 
    196 " Variables, macros, records, maps
    197 syn match erlangVariable '\<[A-Z][[:alnum:]_@]*'
    198 syn match erlangAnonymousVariable '\<_[[:alnum:]_@]*'
    199 syn match erlangMacro    '??\=[[:alnum:]_@]\+'
    200 syn match erlangMacro    '\%(-define(\)\@<=[[:alnum:]_@]\+'
    201 syn region erlangQuotedMacro         start=/??\=\s*'/ end=/'/ contains=erlangQuotedAtomModifier
    202 syn match erlangMap      '#'
    203 syn match erlangRecord   '#\s*\l[[:alnum:]_@]*'
    204 syn region erlangQuotedRecord        start=/#\s*'/ end=/'/ contains=erlangQuotedAtomModifier
    205 
    206 " Shebang (this line has to be after the ErlangMap)
    207 syn match erlangShebang  '^#!.*'
    208 
    209 " Bitstrings
    210 syn match erlangBitType '\%(\/\%(\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
    211 
    212 " Constants and Directives
    213 syn match erlangUnknownAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\l[[:alnum:]_@]*' contains=erlangComment
    214 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
    215 syn match erlangDocAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\%(moduledoc\|doc\)\>' contains=erlangComment,erlangDocString
    216 syn match erlangInnerDocAttribute '^\s*-\%(\s\|\n\|%.*\n\)*\%(moduledoc\|doc\)\>' contained
    217 syn match erlangInclude   '^\s*-\%(\s\|\n\|%.*\n\)*\%(include\|include_lib\)\>' contains=erlangComment
    218 syn match erlangRecordDef '^\s*-\%(\s\|\n\|%.*\n\)*record\>' contains=erlangComment
    219 syn match erlangDefine    '^\s*-\%(\s\|\n\|%.*\n\)*\%(define\|undef\)\>' contains=erlangComment
    220 syn match erlangPreCondit '^\s*-\%(\s\|\n\|%.*\n\)*\%(ifdef\|ifndef\|else\|if\|elif\|endif\)\>' contains=erlangComment
    221 syn match erlangType      '^\s*-\%(\s\|\n\|%.*\n\)*\%(spec\|type\|opaque\|nominal\|callback\)\>' contains=erlangComment
    222 
    223 " Keywords
    224 syn keyword erlangKeyword after begin case catch cond end fun if let of else
    225 syn keyword erlangKeyword receive when try maybe
    226 
    227 " Build-in-functions (BIFs)
    228 syn keyword erlangBIF abs alive apply atom_to_binary atom_to_list contained
    229 syn keyword erlangBIF binary_part binary_to_atom contained
    230 syn keyword erlangBIF binary_to_existing_atom binary_to_float contained
    231 syn keyword erlangBIF binary_to_integer bitstring_to_list contained
    232 syn keyword erlangBIF binary_to_list binary_to_term bit_size contained
    233 syn keyword erlangBIF byte_size check_old_code check_process_code contained
    234 syn keyword erlangBIF concat_binary date delete_module demonitor contained
    235 syn keyword erlangBIF disconnect_node element erase error exit contained
    236 syn keyword erlangBIF float float_to_binary float_to_list contained
    237 syn keyword erlangBIF garbage_collect get get_keys group_leader contained
    238 syn keyword erlangBIF halt hd integer_to_binary integer_to_list contained
    239 syn keyword erlangBIF iolist_to_binary iolist_size is_alive contained
    240 syn keyword erlangBIF is_atom is_binary is_bitstring is_boolean contained
    241 syn keyword erlangBIF is_float is_function is_integer is_list is_map is_map_key contained
    242 syn keyword erlangBIF is_number is_pid is_port is_process_alive contained
    243 syn keyword erlangBIF is_record is_reference is_tuple length link contained
    244 syn keyword erlangBIF list_to_atom list_to_binary contained
    245 syn keyword erlangBIF list_to_bitstring list_to_existing_atom contained
    246 syn keyword erlangBIF list_to_float list_to_integer list_to_pid contained
    247 syn keyword erlangBIF list_to_tuple load_module make_ref map_size max contained
    248 syn keyword erlangBIF min module_loaded monitor monitor_node node contained
    249 syn keyword erlangBIF nodes now open_port pid_to_list port_close contained
    250 syn keyword erlangBIF port_command port_connect pre_loaded contained
    251 syn keyword erlangBIF process_flag process_flag process_info contained
    252 syn keyword erlangBIF process purge_module put register registered contained
    253 syn keyword erlangBIF round self setelement size spawn spawn_link contained
    254 syn keyword erlangBIF spawn_monitor spawn_opt split_binary contained
    255 syn keyword erlangBIF statistics term_to_binary throw time tl contained
    256 syn keyword erlangBIF trunc tuple_size tuple_to_list unlink contained
    257 syn keyword erlangBIF unregister whereis contained
    258 
    259 " Sync at the beginning of functions: if this is not used, multiline string
    260 " are not always recognized, and the indentation script cannot use the
    261 " "searchpair" (because it would not always skip strings and comments when
    262 " looking for keywords and opening parens/brackets).
    263 syn sync match erlangSync grouphere NONE "^[a-z]\s*("
    264 let b:erlang_syntax_synced = 1
    265 
    266 " Define the default highlighting. See ":help group-name" for the groups and
    267 " their colors.
    268 
    269 if s:use_markdown
    270  " Add markdown syntax elements for docstrings (actually, for all
    271  " triple-quoted strings).
    272  unlet! b:current_syntax
    273 
    274  syn include @markdown syntax/markdown.vim
    275  let b:current_syntax = "erlang"
    276 
    277  " markdown-erlang.vim includes html.vim, which includes css.vim, which adds
    278  " the dash character (-) to the list of syntax keywords, which causes
    279  " `-VarName` not to be highlighted as a variable in the Erlang code.
    280  "
    281  " Here we override that.
    282  syntax iskeyword @,48-57,192-255,$,_
    283 endif
    284 
    285 " Comments
    286 hi def link erlangComment Comment
    287 hi def link erlangCommentAnnotation Special
    288 hi def link erlangTodo Todo
    289 hi def link erlangShebang Comment
    290 
    291 " Numbers
    292 hi def link erlangNumberInteger Number
    293 hi def link erlangNumberFloat Float
    294 
    295 " Strings, atoms, characters
    296 hi def link erlangString String
    297 hi def link erlangStringTripleQuoted String
    298 
    299 " Triple quoted strings
    300 if s:docstring_default_highlight != ''
    301  execute 'hi def link erlangDocStringDelimiter '. s:docstring_default_highlight
    302 endif
    303 
    304 if s:old_style
    305 hi def link erlangQuotedAtom Type
    306 else
    307 hi def link erlangQuotedAtom String
    308 endif
    309 
    310 hi def link erlangStringModifier Special
    311 hi def link erlangQuotedAtomModifier Special
    312 hi def link erlangModifier Special
    313 
    314 " Operators, separators
    315 hi def link erlangOperator Operator
    316 hi def link erlangEqualsBinary ErrorMsg
    317 hi def link erlangRightArrow Operator
    318 if s:old_style
    319 hi def link erlangBracket Normal
    320 hi def link erlangPipe Normal
    321 else
    322 hi def link erlangBracket Delimiter
    323 hi def link erlangPipe Delimiter
    324 endif
    325 
    326 " Atoms, functions, variables, macros
    327 if s:old_style
    328 hi def link erlangAtom Normal
    329 hi def link erlangLocalFuncCall Normal
    330 hi def link erlangLocalFuncRef Normal
    331 hi def link erlangGlobalFuncCall Function
    332 hi def link erlangGlobalFuncRef Function
    333 hi def link erlangVariable Normal
    334 hi def link erlangAnonymousVariable erlangVariable
    335 hi def link erlangMacro Normal
    336 hi def link erlangQuotedMacro Normal
    337 hi def link erlangRecord Normal
    338 hi def link erlangQuotedRecord Normal
    339 hi def link erlangMap Normal
    340 else
    341 hi def link erlangAtom String
    342 hi def link erlangLocalFuncCall Normal
    343 hi def link erlangLocalFuncRef Normal
    344 hi def link erlangGlobalFuncCall Normal
    345 hi def link erlangGlobalFuncRef Normal
    346 hi def link erlangVariable Identifier
    347 hi def link erlangAnonymousVariable erlangVariable
    348 hi def link erlangMacro Macro
    349 hi def link erlangQuotedMacro Macro
    350 hi def link erlangRecord Structure
    351 hi def link erlangQuotedRecord Structure
    352 hi def link erlangMap Structure
    353 endif
    354 
    355 " Bitstrings
    356 if !s:old_style
    357 hi def link erlangBitType Type
    358 endif
    359 
    360 " Constants and Directives
    361 if s:old_style
    362 hi def link erlangAttribute Type
    363 hi def link erlangMacroDef Type
    364 hi def link erlangUnknownAttribute Normal
    365 hi def link erlangInclude Type
    366 hi def link erlangRecordDef Type
    367 hi def link erlangDefine Type
    368 hi def link erlangPreCondit Type
    369 hi def link erlangType Type
    370 else
    371 hi def link erlangAttribute Keyword
    372 hi def link erlangDocAttribute Keyword
    373 hi def link erlangInnerDocAttribute Keyword
    374 hi def link erlangMacroDef Macro
    375 hi def link erlangUnknownAttribute Normal
    376 hi def link erlangInclude Include
    377 hi def link erlangRecordDef Keyword
    378 hi def link erlangDefine Define
    379 hi def link erlangPreCondit PreCondit
    380 hi def link erlangType Type
    381 endif
    382 
    383 " Keywords
    384 hi def link erlangKeyword Keyword
    385 
    386 " Build-in-functions (BIFs)
    387 hi def link erlangBIF Function
    388 
    389 if s:old_style
    390 hi def link erlangBoolean Statement
    391 hi def link erlangExtra Statement
    392 hi def link erlangSignal Statement
    393 else
    394 hi def link erlangBoolean Boolean
    395 hi def link erlangExtra Statement
    396 hi def link erlangSignal Statement
    397 endif
    398 
    399 let b:current_syntax = "erlang"
    400 
    401 if g:main_syntax ==# 'erlang'
    402  unlet g:main_syntax
    403 endif
    404 
    405 let &cpo = s:cpo_save
    406 unlet s:cpo_save
    407 
    408 " vim: sw=2 et