codekingpro/portable-devtools
114k
1" Vim syntax file2" Language: Python 23" Maintainer: Zvezdan Petkovic <zpetkovic@acm.org>4" Last Change: 2016 Oct 295" 2025 Jul 14 by Vim project: highlight unicode strings6" 2025 Jul 15 by Vim project: highlight b-strings7" Credits: Neil Schemenauer <nas@python.ca>8" Dmitry Vasiliev9" Rob B10"11" This version is a major rewrite by Zvezdan Petkovic.12"13" - introduced highlighting of doctests14" - updated keywords, built-ins, and exceptions15" - corrected regular expressions for16"17" * functions18" * decorators19" * strings20" * escapes21" * numbers22" * space error23"24" - corrected synchronization25" - more highlighting is ON by default, except26" - space error highlighting is OFF by default27"28" Optional highlighting can be controlled using these variables.29"30" let python_no_builtin_highlight = 131" let python_no_doctest_code_highlight = 132" let python_no_doctest_highlight = 133" let python_no_exception_highlight = 134" let python_no_number_highlight = 135" let python_space_error_highlight = 136"37" All the options above can be switched on together.38"39" let python_highlight_all = 140"41""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""42" NOTE: This file is a copy of the last commit of runtime/syntax/python.vim43" 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 code45" (Python 2 and 3 compatible) better.46""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""47 48" quit when a syntax file was already loaded.49if exists("b:current_syntax")50 finish51endif52 53" We need nocompatible mode in order to continue lines with backslashes.54" Original setting will be restored.55let s:cpo_save = &cpo56set cpo&vim57 58if exists("python_no_doctest_highlight")59 let python_no_doctest_code_highlight = 160endif61 62if exists("python_highlight_all")63 if exists("python_no_builtin_highlight")64 unlet python_no_builtin_highlight65 endif66 if exists("python_no_doctest_code_highlight")67 unlet python_no_doctest_code_highlight68 endif69 if exists("python_no_doctest_highlight")70 unlet python_no_doctest_highlight71 endif72 if exists("python_no_exception_highlight")73 unlet python_no_exception_highlight74 endif75 if exists("python_no_number_highlight")76 unlet python_no_number_highlight77 endif78 let python_space_error_highlight = 179endif80 81" Keep Python keywords in alphabetical order inside groups for easy82" 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.689" (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 are96" 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 as98" 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 as101" 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 True105syn keyword pythonStatement as assert break continue del exec global106syn keyword pythonStatement lambda nonlocal pass print return with yield107syn keyword pythonStatement class def nextgroup=pythonFunction skipwhite108syn keyword pythonConditional elif else if109syn keyword pythonRepeat for while110syn keyword pythonOperator and in is not or111syn keyword pythonException except finally raise try112syn keyword pythonInclude from import113syn keyword pythonAsync async await114 115" Decorators (new in Python 2.4)116" A dot must be allowed because of @MyClass.myfunc decorators.117syn match pythonDecorator "@" display contained118syn match pythonDecoratorName "@\s*\h\%(\w\|\.\)*" display contains=pythonDecorator119 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 the122" symbol from highlighting when used in that context.123" Single line multiplication.124syn match pythonMatrixMultiply125 \ "\%(\w\|[])]\)\s*@"126 \ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue127 \ transparent128" Multiplication continued on the next line after backslash.129syn match pythonMatrixMultiply130 \ "[^\\]\\\s*\n\%(\s*\.\.\.\s\)\=\s\+@"131 \ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue132 \ transparent133" Multiplication in a parenthesized expression over multiple lines with @ at134" the start of each continued line; very similar to decorators and complex.135syn match pythonMatrixMultiply136 \ "^\s*\%(\%(>>>\|\.\.\.\)\s\+\)\=\zs\%(\h\|\%(\h\|[[(]\).\{-}\%(\w\|[])]\)\)\s*\n\%(\s*\.\.\.\s\)\=\s\+@\%(.\{-}\n\%(\s*\.\.\.\s\)\=\s\+@\)*"137 \ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue138 \ transparent139 140syn match pythonFunction "\h\w*" display contained141 142syn match pythonComment "#.*$" contains=pythonTodo,@Spell143syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained144 145" Triple-quoted strings can contain doctests.146syn region pythonString matchgroup=pythonQuotes147 \ start=+[bB]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"148 \ contains=pythonEscape,@Spell149syn region pythonString matchgroup=pythonTripleQuotes150 \ start=+[bB]\=\z('''\|"""\)+ end="\z1" keepend151 \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell152syn region pythonRawString matchgroup=pythonQuotes153 \ start=+[bB]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"154 \ contains=@Spell155syn region pythonRawString matchgroup=pythonTripleQuotes156 \ start=+[bB]\=[rR]\z('''\|"""\)+ end="\z1" keepend157 \ contains=pythonSpaceError,pythonDoctest,@Spell158 159" Unicode strings160syn region pythonString161 \ matchgroup=pythonQuotes162 \ start=+[uU]\z(['"]\)+163 \ end="\z1"164 \ skip="\\\\\|\\\z1"165 \ contains=pythonEscape,pythonUnicodeEscape,@Spell166syn region pythonString167 \ matchgroup=pythonTripleQuotes168 \ start=+[uU]\z('''\|"""\)+169 \ end="\z1"170 \ keepend171 \ contains=pythonEscape,pythonUnicodeEscape,pythonSpaceError,pythonDoctest,@Spell172 173" Raw Unicode strings recognize Unicode escape sequences174" https://docs.python.org/2.7/reference/lexical_analysis.html#string-literals175syn region pythonRawString176 \ matchgroup=pythonQuotes177 \ start=+[uU][rR]\z(['"]\)+178 \ end="\z1"179 \ skip="\\\\\|\\\z1"180 \ contains=pythonUnicodeEscape,@Spell181syn region pythonRawString182 \ matchgroup=pythonTripleQuotes183 \ start=+[uU][rR]\z('''\|"""\)+184 \ end="\z1"185 \ keepend186 \ contains=pythonUnicodeEscape,pythonSpaceError,pythonDoctest,@Spell187 188syn match pythonEscape +\\[abfnrtv'"\\]+ contained189syn match pythonEscape "\\\o\{1,3}" contained190syn match pythonEscape "\\x\x\{2}" contained191syn match pythonUnicodeEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained192" Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/193syn match pythonUnicodeEscape "\\N{\a\+\%(\s\a\+\)*}" contained194syn match pythonEscape "\\$"195 196" It is very important to understand all details before changing the197" regular expressions below or their order.198" The word boundaries are *not* the floating-point number boundaries199" because of a possible leading or trailing decimal point.200" The expressions below ensure that all valid number literals are201" 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-literals210" https://docs.python.org/3/reference/lexical_analysis.html#numeric-literals211if !exists("python_no_number_highlight")212 " numbers (including longs and complex)213 syn match pythonNumber "\<0[oO]\=\o\+[Ll]\=\>"214 syn match pythonNumber "\<0[xX]\x\+[Ll]\=\>"215 syn match pythonNumber "\<0[bB][01]\+[Ll]\=\>"216 syn match pythonNumber "\<\%([1-9]\d*\|0\)[Ll]\=\>"217 syn match pythonNumber "\<\d\+[jJ]\>"218 syn match pythonNumber "\<\d\+[eE][+-]\=\d\+[jJ]\=\>"219 syn match pythonNumber220 \ "\<\d\+\.\%([eE][+-]\=\d\+\)\=[jJ]\=\%(\W\|$\)\@="221 syn match pythonNumber222 \ "\%(^\|\W\)\zs\d*\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>"223endif224 225" Group the built-ins in the order in the 'Python Library Reference' for226" easier comparison.227" https://docs.python.org/2/library/constants.html228" https://docs.python.org/3/library/constants.html229" http://docs.python.org/2/library/functions.html230" http://docs.python.org/3/library/functions.html231" http://docs.python.org/2/library/functions.html#non-essential-built-in-functions232" http://docs.python.org/3/library/functions.html#non-essential-built-in-functions233" Python built-in functions are in alphabetical order.234if !exists("python_no_builtin_highlight")235 " built-in constants236 " 'False', 'True', and 'None' are also reserved words in Python 3237 syn keyword pythonBuiltin False True None238 syn keyword pythonBuiltin NotImplemented Ellipsis __debug__239 " built-in functions240 syn keyword pythonBuiltin abs all any bin bool bytearray callable chr241 syn keyword pythonBuiltin classmethod compile complex delattr dict dir242 syn keyword pythonBuiltin divmod enumerate eval filter float format243 syn keyword pythonBuiltin frozenset getattr globals hasattr hash244 syn keyword pythonBuiltin help hex id input int isinstance245 syn keyword pythonBuiltin issubclass iter len list locals map max246 syn keyword pythonBuiltin memoryview min next object oct open ord pow247 syn keyword pythonBuiltin print property range repr reversed round set248 syn keyword pythonBuiltin setattr slice sorted staticmethod str249 syn keyword pythonBuiltin sum super tuple type vars zip __import__250 " Python 2 only251 syn keyword pythonBuiltin basestring cmp execfile file252 syn keyword pythonBuiltin long raw_input reduce reload unichr253 syn keyword pythonBuiltin unicode xrange254 " Python 3 only255 syn keyword pythonBuiltin ascii bytes exec256 " non-essential built-in functions; Python 2 only257 syn keyword pythonBuiltin apply buffer coerce intern258 " avoid highlighting attributes as builtins259 syn match pythonAttribute /\.\h\w*/hs=s+1260 \ contains=ALLBUT,pythonBuiltin,pythonFunction,pythonAsync261 \ transparent262endif263 264" From the 'Python Library Reference' class hierarchy at the bottom.265" http://docs.python.org/2/library/exceptions.html266" http://docs.python.org/3/library/exceptions.html267if !exists("python_no_exception_highlight")268 " builtin base exceptions (used mostly as base classes for other exceptions)269 syn keyword pythonExceptions BaseException Exception270 syn keyword pythonExceptions ArithmeticError BufferError271 syn keyword pythonExceptions LookupError272 " builtin base exceptions removed in Python 3273 syn keyword pythonExceptions EnvironmentError StandardError274 " builtin exceptions (actually raised)275 syn keyword pythonExceptions AssertionError AttributeError276 syn keyword pythonExceptions EOFError FloatingPointError GeneratorExit277 syn keyword pythonExceptions ImportError IndentationError278 syn keyword pythonExceptions IndexError KeyError KeyboardInterrupt279 syn keyword pythonExceptions MemoryError NameError NotImplementedError280 syn keyword pythonExceptions OSError OverflowError ReferenceError281 syn keyword pythonExceptions RuntimeError StopIteration SyntaxError282 syn keyword pythonExceptions SystemError SystemExit TabError TypeError283 syn keyword pythonExceptions UnboundLocalError UnicodeError284 syn keyword pythonExceptions UnicodeDecodeError UnicodeEncodeError285 syn keyword pythonExceptions UnicodeTranslateError ValueError286 syn keyword pythonExceptions ZeroDivisionError287 " builtin OS exceptions in Python 3288 syn keyword pythonExceptions BlockingIOError BrokenPipeError289 syn keyword pythonExceptions ChildProcessError ConnectionAbortedError290 syn keyword pythonExceptions ConnectionError ConnectionRefusedError291 syn keyword pythonExceptions ConnectionResetError FileExistsError292 syn keyword pythonExceptions FileNotFoundError InterruptedError293 syn keyword pythonExceptions IsADirectoryError NotADirectoryError294 syn keyword pythonExceptions PermissionError ProcessLookupError295 syn keyword pythonExceptions RecursionError StopAsyncIteration296 syn keyword pythonExceptions TimeoutError297 " builtin exceptions deprecated/removed in Python 3298 syn keyword pythonExceptions IOError VMSError WindowsError299 " builtin warnings300 syn keyword pythonExceptions BytesWarning DeprecationWarning FutureWarning301 syn keyword pythonExceptions ImportWarning PendingDeprecationWarning302 syn keyword pythonExceptions RuntimeWarning SyntaxWarning UnicodeWarning303 syn keyword pythonExceptions UserWarning Warning304 " builtin warnings in Python 3305 syn keyword pythonExceptions ResourceWarning306endif307 308if exists("python_space_error_highlight")309 " trailing whitespace310 syn match pythonSpaceError display excludenl "\s\+$"311 " mixed tabs and spaces312 syn match pythonSpaceError display " \+\t"313 syn match pythonSpaceError display "\t\+ "314endif315 316" Do not spell doctests inside strings.317" Notice that the end of a string, either ''', or """, will end the contained318" doctest too. Thus, we do *not* need to have it as an end pattern.319if !exists("python_no_doctest_highlight")320 if !exists("python_no_doctest_code_highlight")321 syn region pythonDoctest322 \ start="^\s*>>>\s" end="^\s*$"323 \ contained contains=ALLBUT,pythonDoctest,pythonFunction,@Spell324 syn region pythonDoctestValue325 \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$"326 \ contained327 else328 syn region pythonDoctest329 \ start="^\s*>>>" end="^\s*$"330 \ contained contains=@NoSpell331 endif332endif333 334" Sync at the beginning of class, function, or method definition.335syn sync match pythonSync grouphere NONE "^\%(def\|class\)\s\+\h\w*\s*[(:]"336 337" The default highlight links. Can be overridden later.338hi def link pythonStatement Statement339hi def link pythonConditional Conditional340hi def link pythonRepeat Repeat341hi def link pythonOperator Operator342hi def link pythonException Exception343hi def link pythonInclude Include344hi def link pythonAsync Statement345hi def link pythonDecorator Define346hi def link pythonDecoratorName Function347hi def link pythonFunction Function348hi def link pythonComment Comment349hi def link pythonTodo Todo350hi def link pythonString String351hi def link pythonRawString String352hi def link pythonQuotes String353hi def link pythonTripleQuotes pythonQuotes354hi def link pythonEscape Special355hi def link pythonUnicodeEscape pythonEscape356if !exists("python_no_number_highlight")357 hi def link pythonNumber Number358endif359if !exists("python_no_builtin_highlight")360 hi def link pythonBuiltin Function361endif362if !exists("python_no_exception_highlight")363 hi def link pythonExceptions Structure364endif365if exists("python_space_error_highlight")366 hi def link pythonSpaceError Error367endif368if !exists("python_no_doctest_highlight")369 hi def link pythonDoctest Special370 hi def link pythonDoctestValue Define371endif372 373let b:current_syntax = "python"374 375let &cpo = s:cpo_save376unlet s:cpo_save377 378" vim:set sw=2 sts=2 ts=8 noet:379 