robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
83818b885a
master
1" Vim syntax file 2" Language: Python 2 3" Maintainer: Zvezdan Petkovic <zpetkovic@acm.org> 4" Last Change: 2016 Oct 29 5" 2025 Jul 14 by Vim project: highlight unicode strings 6" 2025 Jul 15 by Vim project: highlight b-strings 7" Credits: Neil Schemenauer <nas@python.ca> 8" Dmitry Vasiliev 9" Rob B 10" 11" This version is a major rewrite by Zvezdan Petkovic. 12" 13" - introduced highlighting of doctests 14" - updated keywords, built-ins, and exceptions 15" - corrected regular expressions for 16" 17" * functions 18" * decorators 19" * strings 20" * escapes 21" * numbers 22" * space error 23" 24" - corrected synchronization 25" - more highlighting is ON by default, except 26" - space error highlighting is OFF by default 27" 28" Optional highlighting can be controlled using these variables. 29" 30" let python_no_builtin_highlight = 1 31" let python_no_doctest_code_highlight = 1 32" let python_no_doctest_highlight = 1 33" let python_no_exception_highlight = 1 34" let python_no_number_highlight = 1 35" let python_space_error_highlight = 1 36" 37" All the options above can be switched on together. 38" 39" let python_highlight_all = 1 40" 41"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 42" NOTE: This file is a copy of the last commit of runtime/syntax/python.vim 43" that still supported Python 2. There is support for Python 3, up to 3.5, 44" and it was kept in the file as is, because it supports the straddling code 45" (Python 2 and 3 compatible) better. 46"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 47 48" quit when a syntax file was already loaded. 49if exists ( "b:current_syntax" ) 50finish 51endif 52 53" We need nocompatible mode in order to continue lines with backslashes. 54" Original setting will be restored. 55let s: cpo_save = & cpo 56set cpo & vim 57 58if exists ( "python_no_doctest_highlight" ) 59let python_no_doctest_code_highlight = 1 60endif 61 62if exists ( "python_highlight_all" ) 63if exists ( "python_no_builtin_highlight" ) 64unlet python_no_builtin_highlight 65endif 66if exists ( "python_no_doctest_code_highlight" ) 67unlet python_no_doctest_code_highlight 68endif 69if exists ( "python_no_doctest_highlight" ) 70unlet python_no_doctest_highlight 71endif 72if exists ( "python_no_exception_highlight" ) 73unlet python_no_exception_highlight 74endif 75if exists ( "python_no_number_highlight" ) 76unlet python_no_number_highlight 77endif 78let python_space_error_highlight = 1 79endif 80 81" Keep Python keywords in alphabetical order inside groups for easy 82" comparison with the table in the 'Python Language Reference' 83" https://docs.python.org/2/reference/lexical_analysis.html#keywords, 84" https://docs.python.org/3/reference/lexical_analysis.html#keywords. 85" Groups are in the order presented in NAMING CONVENTIONS in syntax.txt. 86" Exceptions come last at the end of each group (class and def below). 87" 88" Keywords 'with' and 'as' are new in Python 2.6 89" (use 'from __future__ import with_statement' in Python 2.5). 90" 91" Some compromises had to be made to support both Python 3 and 2. 92" We include Python 3 features, but when a definition is duplicated, 93" the last definition takes precedence. 94" 95" - 'False', 'None', and 'True' are keywords in Python 3 but they are 96" built-ins in 2 and will be highlighted as built-ins below. 97" - 'exec' is a built-in in Python 3 and will be highlighted as 98" built-in below. 99" - 'nonlocal' is a keyword in Python 3 and will be highlighted. 100" - 'print' is a built-in in Python 3 and will be highlighted as 101" built-in below (use 'from __future__ import print_function' in 2) 102" - async and await were added in Python 3.5 and are soft keywords. 103" 104syn keyword pythonStatement False None True 105syn keyword pythonStatement as assert break continue del exec global 106syn keyword pythonStatement lambda nonlocal pass return with yield 107syn keyword pythonStatement class def nextgroup = pythonFunction skipwhite 108syn keyword pythonConditional elif else if 109syn keyword pythonRepeat for while 110syn keyword pythonOperator and in is not or 111syn keyword pythonException except finally raise try 112syn keyword pythonInclude from import 113syn keyword pythonAsync async await 114 115" Decorators (new in Python 2.4) 116" A dot must be allowed because of @MyClass.myfunc decorators. 117syn match pythonDecorator "@ "display contained 118syn match pythonDecoratorName "@\s * \h\%(\w\|\.\) * "display contains = pythonDecorator 119 120" Python 3.5 introduced the use of the same symbol for matrix multiplication: 121" https://www.python.org/dev/peps/pep-0465/. We now have to exclude the 122" symbol from highlighting when used in that context. 123" Single line multiplication. 124syn match pythonMatrixMultiply 125 \ "\%(\w\| [] )]\)\s * @ " 126 \contains = ALLBUT , pythonDecoratorName , pythonDecorator , pythonFunction , pythonDoctestValue 127 \transparent 128" Multiplication continued on the next line after backslash. 129syn match pythonMatrixMultiply 130 \ "[ ^\\ ] \\\s * \n\%(\s * \.\.\.\s\)\=\s\+@ " 131 \contains = ALLBUT , pythonDecoratorName , pythonDecorator , pythonFunction , pythonDoctestValue 132 \transparent 133" Multiplication in a parenthesized expression over multiple lines with @ at 134" the start of each continued line; very similar to decorators and complex. 135syn match pythonMatrixMultiply 136 \ "^\s * \%(\%(>>>\|\.\.\.\)\s\+\)\=\zs\%(\h\|\%(\h\| [ [( ] \).\{-}\%(\w\| [] )]\)\)\s * \n\%(\s * \.\.\.\s\)\=\s\+@\%(.\{-}\n\%(\s * \.\.\.\s\)\=\s\+@\) * " 137 \contains = ALLBUT , pythonDecoratorName , pythonDecorator , pythonFunction , pythonDoctestValue 138 \transparent 139 140syn match pythonFunction "\h\w * "display contained 141 142syn match pythonComment "#. * $ "contains = pythonTodo , @Spell 143syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained 144 145" Triple-quoted strings can contain doctests. 146syn region pythonString matchgroup = pythonQuotes 147 \start = +[ bB ] \=\z( [ '" ] \) +end = "\z1 " skip="\\\\\|\\\z1" 148\ contains =pythonEscape,@Spell 149syn region pythonString matchgroup = pythonTripleQuotes 150 \start = +[ bB ] \=\z('''\|"""\) +end = "\z1 "keepend 151 \contains = pythonEscape , pythonSpaceError , pythonDoctest , @Spell 152syn region pythonRawString matchgroup = pythonQuotes 153 \start = +[ bB ] \= [ rR ] \z( [ '" ] \) +end = "\z1 " skip="\\\\\|\\\z1" 154\ contains =@Spell 155syn region pythonRawString matchgroup = pythonTripleQuotes 156 \start = +[ bB ] \= [ rR ] \z('''\|"""\) +end = "\z1 "keepend 157 \contains = pythonSpaceError , pythonDoctest , @Spell 158 159" Unicode strings 160syn region pythonString 161 \matchgroup = pythonQuotes 162 \start = +[ uU ] \z( [ '" ] \) + 163 \end = "\z1 " 164 \ skip="\\\\\|\\\z1" 165\ contains =pythonEscape,pythonUnicodeEscape ,@Spell 166syn region pythonString 167 \matchgroup = pythonTripleQuotes 168 \start = +[ uU ] \z('''\|"""\) + 169 \end = "\z1 " 170 \keepend 171 \contains = pythonEscape , pythonUnicodeEscape , pythonSpaceError , pythonDoctest , @Spell 172 173" Raw Unicode strings recognize Unicode escape sequences 174" https://docs.python.org/2.7/reference/lexical_analysis.html#string-literals 175syn region pythonRawString 176 \matchgroup = pythonQuotes 177 \start = +[ uU ][ rR ] \z( [ '" ] \) + 178 \end = "\z1 " 179 \ skip="\\\\\|\\\z1" 180\ contains =pythonUnicodeEscape,@Spell 181syn region pythonRawString 182 \matchgroup = pythonTripleQuotes 183 \start = +[ uU ][ rR ] \z('''\|"""\) + 184 \end = "\z1 " 185 \keepend 186 \contains = pythonUnicodeEscape , pythonSpaceError , pythonDoctest , @Spell 187 188syn match pythonEscape +\\ [ abfnrtv'"\\ ] +contained 189syn match pythonEscape "\\\o\{1,3} "contained 190syn match pythonEscape "\\x\x\{2} "contained 191syn match pythonUnicodeEscape "\%(\\u\x\{4}\|\\U\x\{8}\) "contained 192" Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/ 193syn match pythonUnicodeEscape "\\N{\a\+\%(\s\a\+\) * } "contained 194syn match pythonEscape "\\$ " 195 196" It is very important to understand all details before changing the 197" regular expressions below or their order. 198" The word boundaries are *not* the floating-point number boundaries 199" because of a possible leading or trailing decimal point. 200" The expressions below ensure that all valid number literals are 201" highlighted, and invalid number literals are not. For example, 202" 203" - a decimal point in '4.' at the end of a line is highlighted, 204" - a second dot in 1.0.0 is not highlighted, 205" - 08 is not highlighted, 206" - 08e0 or 08j are highlighted, 207" 208" and so on, as specified in the 'Python Language Reference'. 209" https://docs.python.org/2/reference/lexical_analysis.html#numeric-literals 210" https://docs.python.org/3/reference/lexical_analysis.html#numeric-literals 211if ! exists ( "python_no_number_highlight" ) 212" numbers (including longs and complex) 213syn match pythonNumber "\<0 [ oO ] \=\o\+ [ Ll ] \=\> " 214syn match pythonNumber "\<0 [ xX ] \x\+ [ Ll ] \=\> " 215syn match pythonNumber "\<0 [ bB ][ 01 ] \+ [ Ll ] \=\> " 216syn match pythonNumber "\<\%( [ 1-9 ] \d * \|0\) [ Ll ] \=\> " 217syn match pythonNumber "\<\d\+ [ jJ ] \> " 218syn match pythonNumber "\<\d\+ [ eE ][ +- ] \=\d\+ [ jJ ] \=\> " 219syn match pythonNumber 220 \ "\<\d\+\.\%( [ eE ][ +- ] \=\d\+\)\= [ jJ ] \=\%(\W\|$\)\@= " 221syn match pythonNumber 222 \ "\%(^\|\W\)\zs\d * \.\d\+\%( [ eE ][ +- ] \=\d\+\)\= [ jJ ] \=\> " 223endif 224 225" Group the built-ins in the order in the 'Python Library Reference' for 226" easier comparison. 227" https://docs.python.org/2/library/constants.html 228" https://docs.python.org/3/library/constants.html 229" http://docs.python.org/2/library/functions.html 230" http://docs.python.org/3/library/functions.html 231" http://docs.python.org/2/library/functions.html#non-essential-built-in-functions 232" http://docs.python.org/3/library/functions.html#non-essential-built-in-functions 233" Python built-in functions are in alphabetical order. 234if ! exists ( "python_no_builtin_highlight" ) 235" built-in constants 236" 'False', 'True', and 'None' are also reserved words in Python 3 237syn keyword pythonBuiltin False True None 238syn keyword pythonBuiltin NotImplemented Ellipsis __debug__ 239" built-in functions 240syn keyword pythonBuiltin abs all any bin bool bytearray callable chr 241syn keyword pythonBuiltin classmethod compile complex delattr dict dir 242syn keyword pythonBuiltin divmod enumerate eval filter float format 243syn keyword pythonBuiltin frozenset getattr globals hasattr hash 244syn keyword pythonBuiltin help hex id input int isinstance 245syn keyword pythonBuiltin issubclass iter len list locals map max 246syn keyword pythonBuiltin memoryview min next object oct open ord pow 247syn keyword pythonBuiltin property range repr reversed round set 248syn keyword pythonBuiltin setattr slice sorted staticmethod str 249syn keyword pythonBuiltin sum super tuple type vars zip __import__ 250" Python 2 only 251syn keyword pythonBuiltin basestring cmp execfile file 252syn keyword pythonBuiltin long raw_input reduce reload unichr 253syn keyword pythonBuiltin unicode xrange 254" Python 3 only 255syn keyword pythonBuiltin ascii bytes exec 256" non-essential built-in functions; Python 2 only 257syn keyword pythonBuiltin apply buffer coerce intern 258" avoid highlighting attributes as builtins 259syn match pythonAttribute /\.\h\w * /hs= s+1 260 \contains = ALLBUT , pythonBuiltin , pythonFunction , pythonAsync 261 \transparent 262endif 263 264" From the 'Python Library Reference' class hierarchy at the bottom. 265" http://docs.python.org/2/library/exceptions.html 266" http://docs.python.org/3/library/exceptions.html 267if ! exists ( "python_no_exception_highlight" ) 268" builtin base exceptions (used mostly as base classes for other exceptions) 269syn keyword pythonExceptions BaseException Exception 270syn keyword pythonExceptions ArithmeticError BufferError 271syn keyword pythonExceptions LookupError 272" builtin base exceptions removed in Python 3 273syn keyword pythonExceptions EnvironmentError StandardError 274" builtin exceptions (actually raised) 275syn keyword pythonExceptions AssertionError AttributeError 276syn keyword pythonExceptions EOFError FloatingPointError GeneratorExit 277syn keyword pythonExceptions ImportError IndentationError 278syn keyword pythonExceptions IndexError KeyError KeyboardInterrupt 279syn keyword pythonExceptions MemoryError NameError NotImplementedError 280syn keyword pythonExceptions OSError OverflowError ReferenceError 281syn keyword pythonExceptions RuntimeError StopIteration SyntaxError 282syn keyword pythonExceptions SystemError SystemExit TabError TypeError 283syn keyword pythonExceptions UnboundLocalError UnicodeError 284syn keyword pythonExceptions UnicodeDecodeError UnicodeEncodeError 285syn keyword pythonExceptions UnicodeTranslateError ValueError 286syn keyword pythonExceptions ZeroDivisionError 287" builtin OS exceptions in Python 3 288syn keyword pythonExceptions BlockingIOError BrokenPipeError 289syn keyword pythonExceptions ChildProcessError ConnectionAbortedError 290syn keyword pythonExceptions ConnectionError ConnectionRefusedError 291syn keyword pythonExceptions ConnectionResetError FileExistsError 292syn keyword pythonExceptions FileNotFoundError InterruptedError 293syn keyword pythonExceptions IsADirectoryError NotADirectoryError 294syn keyword pythonExceptions PermissionError ProcessLookupError 295syn keyword pythonExceptions RecursionError StopAsyncIteration 296syn keyword pythonExceptions TimeoutError 297" builtin exceptions deprecated/removed in Python 3 298syn keyword pythonExceptions IOError VMSError WindowsError 299" builtin warnings 300syn keyword pythonExceptions BytesWarning DeprecationWarning FutureWarning 301syn keyword pythonExceptions ImportWarning PendingDeprecationWarning 302syn keyword pythonExceptions RuntimeWarning SyntaxWarning UnicodeWarning 303syn keyword pythonExceptions UserWarning Warning 304" builtin warnings in Python 3 305syn keyword pythonExceptions ResourceWarning 306endif 307 308if exists ( "python_space_error_highlight" ) 309" trailing whitespace 310syn match pythonSpaceError display excludenl "\s\+$ " 311" mixed tabs and spaces 312syn match pythonSpaceError display "\+\t " 313syn match pythonSpaceError display "\t\+ " 314endif 315 316" Do not spell doctests inside strings. 317" Notice that the end of a string, either ''', or """, will end the contained 318" doctest too. Thus, we do *not* need to have it as an end pattern. 319if ! exists ( "python_no_doctest_highlight" ) 320if ! exists ( "python_no_doctest_code_highlight" ) 321syn region pythonDoctest 322 \start = "^\s * >>>\s "end = "^\s * $ " 323 \contained contains = ALLBUT , pythonDoctest , pythonFunction , @Spell 324syn region pythonDoctestValue 325 \start = +^\s * \%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\+ +end = "$ " 326 \contained 327else 328syn region pythonDoctest 329 \start = "^\s * >>> "end = "^\s * $ " 330 \contained contains = @NoSpell 331endif 332endif 333 334" Sync at the beginning of class, function, or method definition. 335syn syncmatch pythonSync grouphereNONE "^\%(def\|class\)\s\+\h\w * \s * [ (: ] " 336 337" The default highlight links. Can be overridden later. 338hi def link pythonStatement Statement 339hi def link pythonConditional Conditional 340hi def link pythonRepeat Repeat 341hi def link pythonOperator Operator 342hi def link pythonException Exception 343hi def link pythonInclude Include 344hi def link pythonAsync Statement 345hi def link pythonDecorator Define 346hi def link pythonDecoratorName Function 347hi def link pythonFunction Function 348hi def link pythonComment Comment 349hi def link pythonTodo Todo 350hi def link pythonString String 351hi def link pythonRawString String 352hi def link pythonQuotes String 353hi def link pythonTripleQuotes pythonQuotes 354hi def link pythonEscape Special 355hi def link pythonUnicodeEscape pythonEscape 356if ! exists ( "python_no_number_highlight" ) 357hi def link pythonNumber Number 358endif 359if ! exists ( "python_no_builtin_highlight" ) 360hi def link pythonBuiltin Function 361endif 362if ! exists ( "python_no_exception_highlight" ) 363hi def link pythonExceptions Structure 364endif 365if exists ( "python_space_error_highlight" ) 366hi def link pythonSpaceError Error 367endif 368if ! exists ( "python_no_doctest_highlight" ) 369hi def link pythonDoctest Special 370hi def link pythonDoctestValue Define 371endif 372 373let b: current_syntax = "python" 374 375let & cpo = s: cpo_save 376unlet s: cpo_save 377 378" vim:set sw=2 sts=2 ts=8 noet: