├── CHANGES.txt ├── LICENSE ├── README.rst ├── TODO.txt ├── folding-ideas ├── python.vim.1.13 └── python.vim.1.14 ├── syntax.txt ├── syntax └── python.vim └── test.py /CHANGES.txt: -------------------------------------------------------------------------------- 1 | Revision 3.6.0 (2015-11-XX): 2 | 3 | - Fix 'async def' highlighting. Patch by Joongi Kim 4 | - Add dummy 'pythonInclude' group to avoid crashing standard pyrex.vim. 5 | Patch by Antony Lee 6 | 7 | Revision 3.5.0 (2015-06-10): 8 | 9 | - Add support for 'async ...' and 'await' keywords introduced in 10 | Python 3.5. Patch by Ihor Gorobets 11 | 12 | Revision 3.3.7 (2014-12-27): 13 | 14 | - Add support for Python 3 non-ASCII decorator names. 15 | Patch by Victor Salgado 16 | 17 | Revision 3.3.6 (2013-11-18): 18 | 19 | - Highlight 'yield from' statement introduced in Python 3.3. Reported by 20 | Elizabeth Myers. 21 | 22 | Revision 3.3.5 (2013-08-31): 23 | 24 | - Highlight 'import', 'from' and 'as' as include statements. 25 | Patch by David Briscoe 26 | - Added new option 'python_highlight_file_headers_as_comments' (disabled by 27 | default) to highlight shebang and coding file headers as comments. 28 | Proposed by David Briscoe. 29 | 30 | Revision 3.3.4 (2013-08-11): 31 | 32 | - Highlight True and False as booleans. Patch by Yuri Habrusiev. 33 | 34 | Revision 3.3.3 (2013-06-02): 35 | 36 | - More lightweight syntax reloading. Patch by Will Gray. 37 | 38 | Revision 3.3.2 (2013-06-01): 39 | 40 | - Fixed behaviour of b:python_version_2 variable. Reported by Will Gray. 41 | 42 | Revision 3.3.1 (2013-05-12): 43 | 44 | - The script was moved to its own repository at 45 | https://github.com/hdima/python-syntax 46 | 47 | Revision 3.3.0 (2013-03-10): 48 | 49 | - Merge Python 2 and Python 3 script versions into the single python.vim 50 | script 51 | 52 | Revision 3.0.7 (2012-02-11): 53 | 54 | - Updated email and URL 55 | 56 | Revision 2.6.7 (2012-02-11): 57 | 58 | - Updated email and URL 59 | 60 | Revision 3.0.6 (2010-11-14): 61 | 62 | - Fixed highlighting for str.format syntax. Patch by Anton Butanaev. 63 | 64 | Revision 3.0.5 (2010-11-11): 65 | 66 | - Fixed highlighting for bytes. Patch by Anton Butanaev. 67 | - Fixed highlighting for numbers. 68 | 69 | Revision 3.0.4 (2010-11-09): 70 | 71 | - Fixed highlighting for raw bytes literals. Patch by Anton Butanaev. 72 | 73 | Revision 3.0.3 (2010-04-09): 74 | 75 | - Applied patch by Andrea Riciputi with new configuration options 76 | "python_highlight_builtin_objs" and "python_highlight_builtin_funcs" 77 | 78 | Revision 3.0.2 (2009-07-24): 79 | 80 | - Applied patch by Caleb Adamantine which fixes highlighting for decorators 81 | 82 | Revision 3.0.1 (2009-05-03): 83 | 84 | - Fixed compatibility with pyrex.vim 85 | 86 | Revision 3.0.0 (2008-12-07): 87 | 88 | - Added support for non-ASCII identifiers; 89 | - Added support for new text strings and binary data (bytes); 90 | - Updated support for numeric literals; 91 | - Updated support for str.format; 92 | - Added new builtins introduced in Python 2.6: "ascii", "exec", 93 | "memoryview", "print"; 94 | - Added new keyword "nonlocal"; 95 | - Removed exception "StandardError"; 96 | - Removed builtins: "apply", "basestring", "buffer", "callable", "coerce", 97 | "execfile", "file", "help", "intern", "long", "raw_input", "reduce", 98 | "reload", "unichr", "unicode", "xrange"; 99 | 100 | Revision 2.6.6 (2010-04-09): 101 | 102 | - Applied patch by Andrea Riciputi with new configuration options 103 | "python_highlight_builtin_objs" and "python_highlight_builtin_funcs" 104 | 105 | Revision 2.6.5 (2009-07-24): 106 | 107 | - Applied patch by Caleb Adamantine which fixes highlighting for decorators 108 | 109 | Revision 2.6.4 (2009-05-03): 110 | 111 | - Fixed compatibility with pyrex.vim 112 | 113 | Revision 2.6.3 (2008-09-29): 114 | 115 | - Return back trailing 'L' support for numbers. Actually it was changed for 116 | future Python 3.0 syntax but in wrong file; 117 | 118 | Revision 2.6.2 (2008-09-22): 119 | 120 | - Added "VMSError" exception; 121 | - Added support for b"..." syntax; 122 | - Added support for str.format brace escaping; 123 | 124 | Revision 2.6.1 (2008-09-21): 125 | 126 | - Added new builtins and exceptions introduced in Python 2.6: "bin", 127 | "bytearray", "bytes", "format", "next", "BufferError", "BytesWarning"; 128 | - Added builtin "__debug__"; 129 | - Added global variables: "__doc__", "__file__", "__name__", "__package__"; 130 | - Removed "OverflowWarning" (removed in Python 2.5); 131 | - Added option "python_print_as_function" for highlight "print" as a 132 | function; 133 | - Added support for new integer literal syntax "0o" and "0b"; 134 | - Added support for string.Template syntax controlled by 135 | "python_highlight_string_templates" option; 136 | - Added support for str.format syntax controlled by 137 | "python_highlight_string_format" option; 138 | - Removed highlighting for "--" and "++" because it's a valid Python 139 | expressions which can be interpreted as "a + +b"; 140 | 141 | Revision 2.5.6 (2007-02-04): 142 | 143 | - Applied patch by Pedro Algarvio to enable spell checking only for 144 | the right spots (strings and comments); 145 | 146 | Revision 2.5.5 (2006-09-26): 147 | 148 | - added new warnings (ImportWarning, UnicodeWarning) 149 | introduced in Python 2.5; 150 | 151 | Revision 2.5.4 (2006-05-11): 152 | 153 | - added highlighting for erroneous operators: &&, ||, ++, --, === 154 | (inspired by http://www.vim.org/tips/tip.php?tip_id=969, thanks 155 | Jeroen Ruigrok van der Werven for the link); 156 | - added highlighting for new 'with' statement and 'BaseException', 157 | 'GeneratorExit' exceptions introduced in Python 2.5; 158 | - added highlighting for 'OverflowWarning' exception which had been 159 | forgotten; 160 | - returned more robust recognition for function names; 161 | 162 | Revision 2.5.3: 163 | 164 | - fixed %-formatting highlighting for raw unicode strings; 165 | 166 | Revision 2.5.2: 167 | 168 | - slightly simplified option handling; 169 | - fixed regexp for indentation errors; 170 | - fixed highlighting for backslashed symbols inside strings; 171 | - added highlighting for trailing-space errors (triggered by new 172 | option: python_highlight_space_errors); 173 | - added highlighting for variable name errors; 174 | - added highlighting for hex number errors; 175 | 176 | Revision 2.5.1 (2005-03-13): 177 | 178 | - added new builtins 'all' and 'any' (Python 2.5a0) 179 | 180 | Revision 2.4.2 (2004-08-05): 181 | 182 | - added highlighting for new @decorator syntax introduced in python 2.4a2 183 | 184 | Revision 2.4.1 (2004-03-17): 185 | 186 | - new versioning scheme (based on python version numbers); 187 | - added highlighting for new types/builtins introduced in python 2.4 188 | (set, frozenset, reversed, sorted); 189 | - new option added: python_slow_sync (set this for slow but more 190 | robust syntax synchronization); 191 | - added highlighting for doctests; 192 | 193 | Revision 1.19: 194 | 195 | - new option added: python_highlight_indent_errors; 196 | - python_highlight_all now not override previously set options, 197 | for example code: 198 | let python_highlight_indent_errors = 0 199 | let python_highlight_all = 1 200 | set all highlight options except indentation errors highlighting option; 201 | 202 | Revision 1.17: 203 | 204 | - changed header, "Based on..." string added; 205 | 206 | Revision 1.16: 207 | 208 | - added basestring builtin; 209 | 210 | Revision 1.15 (first public revision). 211 | 212 | The changes since the original (vim6.1) python.vim are: 213 | 214 | - changed string highlighting; 215 | - enhanced special symbols highlighting inside strings; 216 | - enhanced constant numbers highlighting; 217 | - added optional highlighting for %-formatting inside strings; 218 | - added highlighting for error conditions (wrong symbols in source file, 219 | mixing spaces and tabs, wrong number values, 220 | wrong %-formatting inside strings); 221 | - added highlighting for magic comments: source code encoding 222 | and #! (executable) strings; 223 | - added highlighting for new exceptions and builtins introduced 224 | in python 2.3; 225 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2002-2014 Dmitry Vasiliev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Python syntax highlighting script for Vim 2 | ========================================= 3 | 4 | .. contents:: 5 | 6 | About 7 | ----- 8 | 9 | Enhanced version of the original Python syntax highlighting script. Based on 10 | ``python.vim`` from Vim 6.1 distribution by Neil Schemenauer (nas at python dot 11 | ca). Check also `python.vim page on vim.org 12 | `_. 13 | 14 | Please use the following channels for reporting bugs, offering suggestions or 15 | feedback: 16 | 17 | - python.vim issue tracker: https://github.com/hdima/python-syntax/issues 18 | - Email: Dmitry Vasiliev (dima at hlabs.org) 19 | - Send a message or follow me for updates on Twitter: `@hdima 20 | `__ 21 | 22 | Features 23 | -------- 24 | 25 | Changes from the original ``python.vim`` are: 26 | 27 | - Added support for Python 3.5/3.6+ syntax highlighting, including f-string 28 | literals and type annotations. 29 | - Added ``:Python2Syntax`` and ``:Python3Syntax`` commands which allow to 30 | switch between Python 2 and Python 3 syntaxes respectively without 31 | reloads/restarts 32 | - Updated strings highlighting 33 | - Enhanced special symbols highlighting inside strings 34 | - Enhanced highlighting of numeric constants 35 | - Added optional highlighting for %-formatting inside strings 36 | - Added highlighting for magic comments: source code encoding and #! 37 | (executable) strings 38 | - Added highlighting for new exceptions and builtins 39 | - Added highlighting for doctests 40 | - Added highlighting for new ``@decorator`` syntax introduced in Python 2.4a2 41 | - Added highlighting for the following errors: 42 | 43 | - invalid symbols in source file 44 | - mixing spaces and tabs 45 | - invalid numeric constants 46 | - invalid %-formatting inside strings 47 | - invalid variable names 48 | - trailing spaces (triggered by the ``python_highlight_space_errors`` option) 49 | 50 | Some of these features was later backported into the original ``python.vim``. 51 | 52 | How to install 53 | -------------- 54 | 55 | The easiest installation method is to place `syntax/python.vim 56 | `_ script 57 | into your ``~/.vim/syntax/`` directory. 58 | 59 | You can also use `Pathogen `_ or `Vundle 60 | `_ plugin managers in which case you can 61 | install the whole `python.vim repository 62 | `_ into the corresponding plugins 63 | directory. 64 | 65 | Script options 66 | -------------- 67 | 68 | There are two commands to enable or disable an option: 69 | 70 | ``:let OPTION_NAME = 1`` 71 | Enable option 72 | ``:let OPTION_NAME = 0`` 73 | Disable option 74 | 75 | For example to enable all syntax highlighting features you can place the 76 | following command in your ``~/.vimrc`` script:: 77 | 78 | let python_highlight_all = 1 79 | 80 | Option and commands to select Python version 81 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 82 | 83 | ``python_version_2`` 84 | Enable highlighting for Python 2 (Python 3 highlighting is enabled by 85 | default). Also can be set as a local to buffer ``b:python_version_2`` 86 | variable. 87 | 88 | The following local to buffer commands can be used to switch between two 89 | highlighting modes: 90 | 91 | ``:Python2Syntax`` 92 | Switch to Python 2 highlighting mode 93 | ``:Python3Syntax`` 94 | Switch to Python 3 highlighting mode 95 | 96 | Options used by the script 97 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ 98 | 99 | ``python_highlight_builtins`` 100 | Highlight builtin functions and objects 101 | ``python_highlight_builtin_objs`` 102 | Highlight builtin objects only 103 | ``python_highlight_builtin_funcs`` 104 | Highlight builtin functions only 105 | ``python_highlight_type_annotations`` 106 | Highlight Python 3.5+ type annotations 107 | ``python_highlight_exceptions`` 108 | Highlight standard exceptions 109 | ``python_highlight_string_formatting`` 110 | Highlight ``%`` string formatting 111 | ``python_highlight_string_format`` 112 | Highlight syntax of ``str.format`` syntax 113 | ``python_highlight_string_templates`` 114 | Highlight syntax of ``string.Template`` 115 | ``python_highlight_indent_errors`` 116 | Highlight indentation errors 117 | ``python_highlight_space_errors`` 118 | Highlight trailing spaces 119 | ``python_highlight_doctests`` 120 | Highlight doc-tests 121 | ``python_print_as_function`` 122 | Highlight ``print`` statement as function for Python 2 123 | ``python_highlight_file_headers_as_comments`` 124 | Highlight shebang and coding headers as comments 125 | ``python_highlight_all`` 126 | Enable all the options above. *NOTE: This option don't override any 127 | previously set options* 128 | ``python_slow_sync`` 129 | Can be set to 0 for slow machines 130 | 131 | Contributors 132 | ------------ 133 | 134 | List of the contributors in alphabetical order: 135 | 136 | - `Andrea Riciputi `_ 137 | - Anton Butanaev 138 | - `Antony Lee `_ 139 | - Caleb Adamantine 140 | - `David Briscoe `_ 141 | - `Elizabeth Myers `_ 142 | - `Ihor Gorobets `_ 143 | - `Jeroen Ruigrok van der Werven `_ 144 | - `John Eikenberry `_ 145 | - `Joongi Kim `_ 146 | - `Marc Weber `_ 147 | - `Pedro Algarvio `_ 148 | - `Victor Salgado `_ 149 | - `Will Gray `_ 150 | - `Yuri Habrusiev `_ 151 | -------------------------------------------------------------------------------- /TODO.txt: -------------------------------------------------------------------------------- 1 | Now 2 | === 3 | 4 | - It seems python.vim doesn't highlight special characters inside strings by 5 | default but only when reloaded? Or maybe only when set to Python 2 6 | by default? 7 | 8 | - Add support for slice syntax: 9 | http://img155.imageshack.us/img155/7767/screenshotgs.png 10 | 11 | - When we check spelling we don't need to check the whole strings only the 12 | content. For example we don't need to check 'u' for spelling in strings 13 | like this: u"Some text"; 14 | 15 | Later 16 | ===== 17 | 18 | - Need more accurate way to handle indentation errors. For example 19 | mixing spaces and tabs may be used for pretty formatting; 20 | 21 | - Need more checks for errors like: absent brackets, absent quotes, 22 | back slash at the end of strings; 23 | 24 | - pythonError tips from 25 | http://blog.sontek.net/2008/05/11/python-with-a-modular-ide-vim/ (reported by 26 | Giuliani Deon Sanches) 27 | -------------------------------------------------------------------------------- /folding-ideas/python.vim.1.13: -------------------------------------------------------------------------------- 1 | " Vim syntax file 2 | " Language: Python 3 | " Maintainer: Neil Schemenauer 4 | " Updated: $Date: 2003/01/12 14:17:34 $ 5 | " Updated by: Dmitry Vasiliev 6 | " Filenames: *.py 7 | " $Revision: 1.13 $ 8 | " 9 | " Options: 10 | " For folded functions and classes: 11 | " 12 | " let python_folding = 1 13 | " 14 | " For highlighted builtin functions: 15 | " 16 | " let python_highlight_builtins = 1 17 | " 18 | " For highlighted standard exceptions: 19 | " 20 | " let python_highlight_exceptions = 1 21 | " 22 | " For highlighted string formatting: 23 | " 24 | " let python_highlight_string_formatting = 1 25 | " 26 | " If you want all possible Python highlighting: 27 | " 28 | " let python_highlight_all = 1 29 | " 30 | " TODO: Check more errors? 31 | 32 | " For version 5.x: Clear all syntax items 33 | " For version 6.x: Quit when a syntax file was already loaded 34 | if version < 600 35 | syntax clear 36 | elseif exists("b:current_syntax") 37 | finish 38 | endif 39 | 40 | if exists("python_highlight_all") 41 | let python_folding = 1 42 | let python_highlight_builtins = 1 43 | let python_highlight_exceptions = 1 44 | let python_highlight_string_formatting = 1 45 | endif 46 | 47 | " Keywords 48 | syn keyword pythonStatement break continue del 49 | syn keyword pythonStatement exec return 50 | syn keyword pythonStatement pass print raise 51 | syn keyword pythonStatement global assert 52 | syn keyword pythonStatement lambda yield 53 | if exists("python_folding") && has("folding") 54 | syn match pythonStatement "\<\(def\|class\)\>" display nextgroup=pythonFunction skipwhite 55 | else 56 | syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite 57 | endif 58 | syn match pythonFunction "\h\w*" display contained 59 | syn keyword pythonRepeat for while 60 | syn keyword pythonConditional if elif else 61 | syn keyword pythonImport import from as 62 | syn keyword pythonException try except finally 63 | syn keyword pythonOperator and in is not or 64 | 65 | " Comments 66 | syn match pythonComment "#.*$" display contains=pythonTodo 67 | syn keyword pythonTodo TODO FIXME XXX contained 68 | 69 | " Erroneous characters that cannont be in a python program 70 | syn match pythonError "[@$?]" display 71 | " Mixing spaces and tabs is bad 72 | syn match pythonIndentError "^\s*\(\t \| \t\)\s*" display 73 | 74 | " Strings 75 | syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonEscape,pythonEscapeError 76 | syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonEscape,pythonEscapeError 77 | syn region pythonString start=+"""+ end=+"""+ contains=pythonEscape,pythonEscapeError 78 | syn region pythonString start=+'''+ end=+'''+ contains=pythonEscape,pythonEscapeError 79 | 80 | syn match pythonEscape +\\[abfnrtv'"\\]+ display contained 81 | syn match pythonEscapeError +\\[^abfnrtv'"\\]+ display contained 82 | syn match pythonEscape "\\\o\o\=\o\=" display contained 83 | syn match pythonEscapeError "\\\o\{,2}[89]" display contained 84 | syn match pythonEscape "\\x\x\{2}" display contained 85 | syn match pythonEscapeError "\\x\x\=\X" display contained 86 | syn match pythonEscape "\\$" 87 | 88 | " Unicode strings 89 | syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError 90 | syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError 91 | syn region pythonUniString start=+[uU]"""+ end=+"""+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError 92 | syn region pythonUniString start=+[uU]'''+ end=+'''+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError 93 | 94 | syn match pythonUniEscape "\\u\x\{4}" display contained 95 | syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained 96 | syn match pythonUniEscape "\\U\x\{8}" display contained 97 | syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained 98 | syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained 99 | syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained 100 | 101 | " Raw strings 102 | syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonRawEscape 103 | syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonRawEscape 104 | syn region pythonRawString start=+[rR]"""+ end=+"""+ 105 | syn region pythonRawString start=+[rR]'''+ end=+'''+ 106 | 107 | syn match pythonRawEscape +\\['"]+ display transparent contained 108 | 109 | " Unicode raw strings 110 | syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError 111 | syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError 112 | syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ contains=pythonUniRawEscape,pythonUniRawEscapeError 113 | syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ contains=pythonUniRawEscape,pythonUniRawEscapeError 114 | 115 | syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained 116 | syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained 117 | 118 | if exists("python_highlight_string_formatting") 119 | " String formatting 120 | syn match pythonStrFormat "%\(([^)]\+)\)\=[-#0 +]\=\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString 121 | syn match pythonStrFormat "%[-#0 +]\=\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString 122 | endif 123 | 124 | " Numbers (ints, longs, floats, complex) 125 | syn match pythonNumber "\<0[xX]\x\+[lL]\=\>" display 126 | syn match pythonNumber "\<\d\+[lLjJ]\=\>" display 127 | syn match pythonFloat "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display 128 | syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display 129 | syn match pythonFloat "\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display 130 | syn match pythonOctalError "\<0\o*[89]\d*[lLjJ]\=\>" display 131 | 132 | if exists("python_highlight_builtins") 133 | " Builtin functions, types and objects, not really part of the syntax 134 | syn keyword pythonBuiltinObj True False Ellipsis None NotImplemented 135 | syn keyword pythonBuiltinFunc bool __import__ abs 136 | syn keyword pythonBuiltinFunc apply buffer callable chr classmethod cmp 137 | syn keyword pythonBuiltinFunc coerce compile complex delattr dict dir divmod 138 | syn keyword pythonBuiltinFunc eval execfile file filter float getattr globals 139 | syn keyword pythonBuiltinFunc hasattr hash hex id input int intern isinstance 140 | syn keyword pythonBuiltinFunc issubclass iter len list locals long map max 141 | syn keyword pythonBuiltinFunc min object oct open ord pow property range 142 | syn keyword pythonBuiltinFunc raw_input reduce reload repr round setattr 143 | syn keyword pythonBuiltinFunc slice staticmethod str super tuple type unichr 144 | syn keyword pythonBuiltinFunc unicode vars xrange zip 145 | endif 146 | 147 | if exists("python_highlight_exceptions") 148 | " Builtin exceptions and warnings 149 | syn keyword pythonExClass ArithmeticError AssertionError AttributeError 150 | syn keyword pythonExClass DeprecationWarning EOFError EnvironmentError 151 | syn keyword pythonExClass Exception FloatingPointError IOError 152 | syn keyword pythonExClass ImportError IndentiationError IndexError 153 | syn keyword pythonExClass KeyError KeyboardInterrupt LookupError 154 | syn keyword pythonExClass MemoryError NameError NotImplementedError 155 | syn keyword pythonExClass OSError OverflowError OverflowWarning 156 | syn keyword pythonExClass ReferenceError RuntimeError RuntimeWarning 157 | syn keyword pythonExClass StandardError StopIteration SyntaxError 158 | syn keyword pythonExClass SyntaxWarning SystemError SystemExit TabError 159 | syn keyword pythonExClass TypeError UnboundLocalError UnicodeError 160 | syn keyword pythonExClass UserWarning ValueError Warning WindowsError 161 | syn keyword pythonExClass ZeroDivisionError 162 | endif 163 | 164 | syn sync clear 165 | if exists("python_folding") && has("folding") 166 | syn sync fromstart 167 | 168 | "syn match pythonFold "^\(\s*\)\(class\|def\)\s.*\(\(\n\s*\)*\n\1\s\+\S.*\)\+" transparent fold 169 | syn region pythonFold start="^\z(\s*\)\(class\|def\)\s" skip="^\z1\s\+\S" end="^\s*\S"me=s-1 transparent fold 170 | syn region pythonFold start="{" end="}" transparent fold 171 | syn region pythonFold start="\[" end="\]" transparent fold 172 | else 173 | " This is fast but code inside triple quoted strings screws it up. It 174 | " is impossible to fix because the only way to know if you are inside a 175 | " triple quoted string is to start from the beginning of the file. If 176 | " you have a fast machine you can try uncommenting the "sync minlines" 177 | " and commenting out the rest. 178 | syn sync match pythonSync grouphere NONE "):$" 179 | syn sync maxlines=200 180 | "syn sync minlines=2000 181 | endif 182 | 183 | if version >= 508 || !exists("did_python_syn_inits") 184 | if version <= 508 185 | let did_python_syn_inits = 1 186 | command -nargs=+ HiLink hi link 187 | else 188 | command -nargs=+ HiLink hi def link 189 | endif 190 | 191 | HiLink pythonStatement Statement 192 | HiLink pythonImport Statement 193 | HiLink pythonFunction Function 194 | HiLink pythonConditional Conditional 195 | HiLink pythonRepeat Repeat 196 | HiLink pythonException Exception 197 | HiLink pythonOperator Operator 198 | 199 | HiLink pythonComment Comment 200 | HiLink pythonTodo Todo 201 | 202 | HiLink pythonError Error 203 | HiLink pythonIndentError Error 204 | 205 | HiLink pythonString String 206 | HiLink pythonUniString String 207 | HiLink pythonRawString String 208 | HiLink pythonUniRawString String 209 | 210 | HiLink pythonEscape Special 211 | HiLink pythonEscapeError Error 212 | HiLink pythonUniEscape Special 213 | HiLink pythonUniEscapeError Error 214 | HiLink pythonUniRawEscape Special 215 | HiLink pythonUniRawEscapeError Error 216 | 217 | if exists("python_highlight_string_formatting") 218 | HiLink pythonStrFormat Special 219 | endif 220 | 221 | HiLink pythonNumber Number 222 | HiLink pythonFloat Float 223 | HiLink pythonOctalError Error 224 | 225 | if exists("python_highlight_builtins") 226 | HiLink pythonBuiltinObj Structure 227 | HiLink pythonBuiltinFunc Function 228 | endif 229 | 230 | if exists("python_highlight_exceptions") 231 | HiLink pythonExClass Structure 232 | endif 233 | 234 | delcommand HiLink 235 | endif 236 | 237 | let b:current_syntax = "python" 238 | -------------------------------------------------------------------------------- /folding-ideas/python.vim.1.14: -------------------------------------------------------------------------------- 1 | " Vim syntax file 2 | " Language: Python 3 | " Maintainer: Neil Schemenauer 4 | " Updated: $Date: 2003/01/12 15:35:02 $ 5 | " Updated by: Dmitry Vasiliev 6 | " Filenames: *.py 7 | " $Revision: 1.14 $ 8 | " 9 | " Options: 10 | " For folded functions and classes: 11 | " 12 | " let python_folding = 1 13 | " 14 | " For highlighted builtin functions: 15 | " 16 | " let python_highlight_builtins = 1 17 | " 18 | " For highlighted standard exceptions: 19 | " 20 | " let python_highlight_exceptions = 1 21 | " 22 | " For highlighted string formatting: 23 | " 24 | " let python_highlight_string_formatting = 1 25 | " 26 | " If you want all possible Python highlighting: 27 | " 28 | " let python_highlight_all = 1 29 | " 30 | " TODO: Check more errors? 31 | 32 | " For version 5.x: Clear all syntax items 33 | " For version 6.x: Quit when a syntax file was already loaded 34 | if version < 600 35 | syntax clear 36 | elseif exists("b:current_syntax") 37 | finish 38 | endif 39 | 40 | if exists("python_highlight_all") 41 | let python_folding = 1 42 | let python_highlight_builtins = 1 43 | let python_highlight_exceptions = 1 44 | let python_highlight_string_formatting = 1 45 | endif 46 | 47 | " Keywords 48 | syn keyword pythonStatement break continue del 49 | syn keyword pythonStatement exec return 50 | syn keyword pythonStatement pass print raise 51 | syn keyword pythonStatement global assert 52 | syn keyword pythonStatement lambda yield 53 | if exists("python_folding") && has("folding") 54 | syn match pythonStatement "\<\(def\|class\)\>" display nextgroup=pythonFunction skipwhite 55 | else 56 | syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite 57 | endif 58 | syn match pythonFunction "\h\w*" display contained 59 | syn keyword pythonRepeat for while 60 | syn keyword pythonConditional if elif else 61 | syn keyword pythonImport import from as 62 | syn keyword pythonException try except finally 63 | syn keyword pythonOperator and in is not or 64 | 65 | " Comments 66 | syn match pythonComment "#.*$" display contains=pythonTodo 67 | syn keyword pythonTodo TODO FIXME XXX contained 68 | 69 | " Erroneous characters that cannont be in a python program 70 | syn match pythonError "[@$?]" display 71 | " Mixing spaces and tabs is bad 72 | syn match pythonError "^\s*\(\t \| \t\)\s*" display 73 | 74 | " Strings 75 | syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonEscape,pythonEscapeError 76 | syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonEscape,pythonEscapeError 77 | syn region pythonString start=+"""+ end=+"""+ contains=pythonEscape,pythonEscapeError 78 | syn region pythonString start=+'''+ end=+'''+ contains=pythonEscape,pythonEscapeError 79 | 80 | syn match pythonEscape +\\[abfnrtv'"\\]+ display contained 81 | syn match pythonEscapeError +\\[^abfnrtv'"\\]+ display contained 82 | syn match pythonEscape "\\\o\o\=\o\=" display contained 83 | syn match pythonEscapeError "\\\o\{,2}[89]" display contained 84 | syn match pythonEscape "\\x\x\{2}" display contained 85 | syn match pythonEscapeError "\\x\x\=\X" display contained 86 | syn match pythonEscape "\\$" 87 | 88 | " Unicode strings 89 | syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError 90 | syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError 91 | syn region pythonUniString start=+[uU]"""+ end=+"""+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError 92 | syn region pythonUniString start=+[uU]'''+ end=+'''+ contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError 93 | 94 | syn match pythonUniEscape "\\u\x\{4}" display contained 95 | syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained 96 | syn match pythonUniEscape "\\U\x\{8}" display contained 97 | syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained 98 | syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained 99 | syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained 100 | 101 | " Raw strings 102 | syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonRawEscape 103 | syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonRawEscape 104 | syn region pythonRawString start=+[rR]"""+ end=+"""+ 105 | syn region pythonRawString start=+[rR]'''+ end=+'''+ 106 | 107 | syn match pythonRawEscape +\\['"]+ display transparent contained 108 | 109 | " Unicode raw strings 110 | syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError 111 | syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError 112 | syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ contains=pythonUniRawEscape,pythonUniRawEscapeError 113 | syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ contains=pythonUniRawEscape,pythonUniRawEscapeError 114 | 115 | syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained 116 | syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained 117 | 118 | if exists("python_highlight_string_formatting") 119 | " String formatting 120 | syn match pythonStrFormat "%\(([^)]\+)\)\=[-#0 +]\=\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString 121 | syn match pythonStrFormat "%[-#0 +]\=\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonRawString 122 | endif 123 | 124 | " Numbers (ints, longs, floats, complex) 125 | syn match pythonNumber "\<0[xX]\x\+[lL]\=\>" display 126 | syn match pythonNumber "\<\d\+[lLjJ]\=\>" display 127 | syn match pythonFloat "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display 128 | syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display 129 | syn match pythonFloat "\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display 130 | syn match pythonOctalError "\<0\o*[89]\d*[lLjJ]\=\>" display 131 | 132 | if exists("python_highlight_builtins") 133 | " Builtin functions, types and objects, not really part of the syntax 134 | syn keyword pythonBuiltinObj True False Ellipsis None NotImplemented 135 | syn keyword pythonBuiltinFunc bool __import__ abs 136 | syn keyword pythonBuiltinFunc apply buffer callable chr classmethod cmp 137 | syn keyword pythonBuiltinFunc coerce compile complex delattr dict dir divmod 138 | syn keyword pythonBuiltinFunc eval execfile file filter float getattr globals 139 | syn keyword pythonBuiltinFunc hasattr hash hex id input int intern isinstance 140 | syn keyword pythonBuiltinFunc issubclass iter len list locals long map max 141 | syn keyword pythonBuiltinFunc min object oct open ord pow property range 142 | syn keyword pythonBuiltinFunc raw_input reduce reload repr round setattr 143 | syn keyword pythonBuiltinFunc slice staticmethod str super tuple type unichr 144 | syn keyword pythonBuiltinFunc unicode vars xrange zip 145 | endif 146 | 147 | if exists("python_highlight_exceptions") 148 | " Builtin exceptions and warnings 149 | syn keyword pythonExClass ArithmeticError AssertionError AttributeError 150 | syn keyword pythonExClass DeprecationWarning EOFError EnvironmentError 151 | syn keyword pythonExClass Exception FloatingPointError IOError 152 | syn keyword pythonExClass ImportError IndentiationError IndexError 153 | syn keyword pythonExClass KeyError KeyboardInterrupt LookupError 154 | syn keyword pythonExClass MemoryError NameError NotImplementedError 155 | syn keyword pythonExClass OSError OverflowError OverflowWarning 156 | syn keyword pythonExClass ReferenceError RuntimeError RuntimeWarning 157 | syn keyword pythonExClass StandardError StopIteration SyntaxError 158 | syn keyword pythonExClass SyntaxWarning SystemError SystemExit TabError 159 | syn keyword pythonExClass TypeError UnboundLocalError UnicodeError 160 | syn keyword pythonExClass UserWarning ValueError Warning WindowsError 161 | syn keyword pythonExClass ZeroDivisionError 162 | endif 163 | 164 | syn sync clear 165 | if exists("python_folding") && has("folding") 166 | syn sync fromstart 167 | 168 | "syn match pythonFold "^\(\s*\)\(class\|def\)\s.*\(\(\n\s*\)*\n\1\s\+\S.*\)\+" transparent fold 169 | syn region pythonFold start="^\z(\s*\)\(class\|def\)\s" skip="\(\s*\n\)\+\z1\s\+\(\S\|\%$\)" end="\(\s*\n\)\+\s*\(\S\|\%$\)"me=s-1 transparent fold 170 | syn region pythonFold start="{" end="}" transparent fold 171 | syn region pythonFold start="\[" end="\]" transparent fold 172 | else 173 | " This is fast but code inside triple quoted strings screws it up. It 174 | " is impossible to fix because the only way to know if you are inside a 175 | " triple quoted string is to start from the beginning of the file. If 176 | " you have a fast machine you can try uncommenting the "sync minlines" 177 | " and commenting out the rest. 178 | syn sync match pythonSync grouphere NONE "):$" 179 | syn sync maxlines=200 180 | "syn sync minlines=2000 181 | endif 182 | 183 | if version >= 508 || !exists("did_python_syn_inits") 184 | if version <= 508 185 | let did_python_syn_inits = 1 186 | command -nargs=+ HiLink hi link 187 | else 188 | command -nargs=+ HiLink hi def link 189 | endif 190 | 191 | HiLink pythonStatement Statement 192 | HiLink pythonImport Statement 193 | HiLink pythonFunction Function 194 | HiLink pythonConditional Conditional 195 | HiLink pythonRepeat Repeat 196 | HiLink pythonException Exception 197 | HiLink pythonOperator Operator 198 | 199 | HiLink pythonComment Comment 200 | HiLink pythonTodo Todo 201 | 202 | HiLink pythonError Error 203 | 204 | HiLink pythonString String 205 | HiLink pythonUniString String 206 | HiLink pythonRawString String 207 | HiLink pythonUniRawString String 208 | 209 | HiLink pythonEscape Special 210 | HiLink pythonEscapeError Error 211 | HiLink pythonUniEscape Special 212 | HiLink pythonUniEscapeError Error 213 | HiLink pythonUniRawEscape Special 214 | HiLink pythonUniRawEscapeError Error 215 | 216 | if exists("python_highlight_string_formatting") 217 | HiLink pythonStrFormat Special 218 | endif 219 | 220 | HiLink pythonNumber Number 221 | HiLink pythonFloat Float 222 | HiLink pythonOctalError Error 223 | 224 | if exists("python_highlight_builtins") 225 | HiLink pythonBuiltinObj Structure 226 | HiLink pythonBuiltinFunc Function 227 | endif 228 | 229 | if exists("python_highlight_exceptions") 230 | HiLink pythonExClass Structure 231 | endif 232 | 233 | delcommand HiLink 234 | endif 235 | 236 | let b:current_syntax = "python" 237 | -------------------------------------------------------------------------------- /syntax.txt: -------------------------------------------------------------------------------- 1 | PYTHON *python.vim* *ft-python-syntax* 2 | 3 | There are two commands to enable or disable an option for python.vim 4 | 5 | Enable option: > 6 | :let option_name = 1 7 | < 8 | Disable option: > 9 | :let option_name = 0 10 | < 11 | For example to enable all syntax highlighting features you can place the 12 | following command in your `~/.vimrc` script: > 13 | 14 | let python_highlight_all = 1 15 | < 16 | Option and commands to select Python version: > 17 | 18 | :let python_version_2 = 1 19 | < 20 | Enable highlighting for Python 2 (Python 3 highlighting is enabled by 21 | default). Also can be set as a local to buffer `b:python_version_2` 22 | variable. 23 | 24 | The following local to buffer commands can be used to switch between two 25 | highlighting modes: 26 | 27 | Switch to Python 2 highlighting mode > 28 | :Python2Syntax 29 | < 30 | Switch to Python 3 highlighting mode > 31 | :Python3Syntax 32 | < 33 | Options used by the script 34 | 35 | Highlight builtin functions and objects > 36 | :let python_highlight_builtins = 1 37 | < Highlight builtin objects only > 38 | :let python_highlight_builtin_objs = 1 39 | < Highlight builtin functions only > 40 | :let python_highlight_builtin_funcs = 1 41 | < Highlight standard exceptions > 42 | :let python_highlight_exceptions = 1 43 | < Highlight `%` string formatting > 44 | :let python_highlight_string_formatting = 1 45 | < Highlight syntax of `str.format` syntax > 46 | :let python_highlight_string_format = 1 47 | < Highlight syntax of `string.Template` > 48 | :let python_highlight_string_templates = 1 49 | < Highlight indentation errors > 50 | :let python_highlight_indent_errors = 1 51 | < Highlight trailing spaces > 52 | :let python_highlight_space_errors = 1 53 | < Highlight doc-tests > 54 | :let python_highlight_doctests = 1 55 | < Highlight `print` statement as function for Python 2 > 56 | :let python_print_as_function = 1 57 | < Highlight shebang and coding headers as comments > 58 | :let python_highlight_file_headers_as_comments = 1 59 | < Enable all the options above. NOTE: This option don't override any 60 | previously set options > 61 | :let python_highlight_all = 1 62 | < Can be set to 0 for slow machines > 63 | :let python_slow_sync = 1 64 | < 65 | vim:tw=78:sw=4:ts=8:ft=help:norl: 66 | -------------------------------------------------------------------------------- /syntax/python.vim: -------------------------------------------------------------------------------- 1 | " Vim syntax file 2 | " Language: Python 3 | " Current Maintainer: Dmitry Vasiliev 4 | " Previous Maintainer: Neil Schemenauer 5 | " URL: https://github.com/hdima/python-syntax 6 | " Last Change: 2015-11-01 7 | " Filenames: *.py 8 | " Version: 3.6.0 9 | " 10 | " Based on python.vim (from Vim 6.1 distribution) 11 | " by Neil Schemenauer 12 | " 13 | " Please use the following channels for reporting bugs, offering suggestions or 14 | " feedback: 15 | 16 | " - python.vim issue tracker: https://github.com/hdima/python-syntax/issues 17 | " - Email: Dmitry Vasiliev (dima at hlabs.org) 18 | " - Send a message or follow me for updates on Twitter: `@hdima 19 | " `__ 20 | " 21 | " Contributors 22 | " ============ 23 | " 24 | " List of the contributors in alphabetical order: 25 | " 26 | " Andrea Riciputi 27 | " Anton Butanaev 28 | " Antony Lee 29 | " Caleb Adamantine 30 | " David Briscoe 31 | " Elizabeth Myers 32 | " Ihor Gorobets 33 | " Jeroen Ruigrok van der Werven 34 | " John Eikenberry 35 | " Joongi Kim 36 | " Marc Weber 37 | " Pedro Algarvio 38 | " Victor Salgado 39 | " Will Gray 40 | " Yuri Habrusiev 41 | " 42 | " Options 43 | " ======= 44 | " 45 | " :let OPTION_NAME = 1 Enable option 46 | " :let OPTION_NAME = 0 Disable option 47 | " 48 | " 49 | " Option to select Python version 50 | " ------------------------------- 51 | " 52 | " python_version_2 Enable highlighting for Python 2 53 | " (Python 3 highlighting is enabled 54 | " by default). Can also be set as 55 | " a buffer (b:python_version_2) 56 | " variable. 57 | " 58 | " You can also use the following local to buffer commands to switch 59 | " between two highlighting modes: 60 | " 61 | " :Python2Syntax Switch to Python 2 highlighting 62 | " mode 63 | " :Python3Syntax Switch to Python 3 highlighting 64 | " mode 65 | " 66 | " Option names used by the script 67 | " ------------------------------- 68 | " 69 | " python_highlight_builtins Highlight builtin functions and 70 | " objects 71 | " python_highlight_builtin_objs Highlight builtin objects only 72 | " python_highlight_builtin_funcs Highlight builtin functions only 73 | " python_highlight_exceptions Highlight standard exceptions 74 | " python_highlight_string_formatting Highlight % string formatting 75 | " python_highlight_string_format Highlight str.format syntax 76 | " python_highlight_string_templates Highlight string.Template syntax 77 | " python_highlight_indent_errors Highlight indentation errors 78 | " python_highlight_space_errors Highlight trailing spaces 79 | " python_highlight_doctests Highlight doc-tests 80 | " python_print_as_function Highlight 'print' statement as 81 | " function for Python 2 82 | " python_highlight_file_headers_as_comments 83 | " Highlight shebang and coding 84 | " headers as comments 85 | " 86 | " python_highlight_all Enable all the options above 87 | " NOTE: This option don't override 88 | " any previously set options 89 | " 90 | " python_slow_sync Can be set to 0 for slow machines 91 | " 92 | 93 | " For version 5.x: Clear all syntax items 94 | " For versions greater than 6.x: Quit when a syntax file was already loaded 95 | if version < 600 96 | syntax clear 97 | elseif exists("b:current_syntax") 98 | finish 99 | endif 100 | 101 | " 102 | " Commands 103 | " 104 | command! -buffer Python2Syntax let b:python_version_2 = 1 | let &syntax=&syntax 105 | command! -buffer Python3Syntax let b:python_version_2 = 0 | let &syntax=&syntax 106 | 107 | " Enable option if it's not defined 108 | function! s:EnableByDefault(name) 109 | if !exists(a:name) 110 | let {a:name} = 1 111 | endif 112 | endfunction 113 | 114 | " Check if option is enabled 115 | function! s:Enabled(name) 116 | return exists(a:name) && {a:name} 117 | endfunction 118 | 119 | " Is it Python 2 syntax? 120 | function! s:Python2Syntax() 121 | if exists("b:python_version_2") 122 | return b:python_version_2 123 | endif 124 | return s:Enabled("g:python_version_2") 125 | endfunction 126 | 127 | " 128 | " Default options 129 | " 130 | 131 | call s:EnableByDefault("g:python_slow_sync") 132 | 133 | if s:Enabled("g:python_highlight_all") 134 | call s:EnableByDefault("g:python_highlight_builtins") 135 | if s:Enabled("g:python_highlight_builtins") 136 | call s:EnableByDefault("g:python_highlight_builtin_objs") 137 | call s:EnableByDefault("g:python_highlight_builtin_funcs") 138 | endif 139 | call s:EnableByDefault("g:python_highlight_type_annotations") 140 | call s:EnableByDefault("g:python_highlight_exceptions") 141 | call s:EnableByDefault("g:python_highlight_string_formatting") 142 | call s:EnableByDefault("g:python_highlight_string_format") 143 | call s:EnableByDefault("g:python_highlight_string_templates") 144 | call s:EnableByDefault("g:python_highlight_indent_errors") 145 | call s:EnableByDefault("g:python_highlight_space_errors") 146 | call s:EnableByDefault("g:python_highlight_doctests") 147 | call s:EnableByDefault("g:python_print_as_function") 148 | endif 149 | 150 | " 151 | " Keywords 152 | " 153 | 154 | syn keyword pythonStatement break continue del 155 | if s:Python2Syntax() 156 | syn keyword pythonStatement exec 157 | endif 158 | syn keyword pythonStatement return 159 | syn keyword pythonStatement pass raise 160 | syn keyword pythonStatement global assert 161 | syn keyword pythonStatement with 162 | syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite 163 | syn keyword pythonRepeat for while 164 | syn keyword pythonConditional if elif else 165 | " The standard pyrex.vim unconditionally removes the pythonInclude group, so 166 | " we provide a dummy group here to avoid crashing pyrex.vim. 167 | syn keyword pythonInclude import 168 | syn keyword pythonImport import 169 | syn match pythonIdentifier "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" nextgroup=pythonFuncArgs,pythonTypeAnno display 170 | syn keyword pythonException try except finally 171 | syn keyword pythonOperator and in is not or 172 | syn match pythonLambdaExpr "\\s*['"]\?\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\)*['"]\?@ display contains=pythonType,pythonString nextgroup=pythonTypeArgs 183 | syn region pythonTypeArgs matchgroup=pythonTypeArgs start='\[' end='\]' display contained contains=pythonType,pythonString,pythonTypeArgs 184 | syn keyword pythonType Any AnyStr Callable ClassVar Tuple Union Optional Type TypeVar None contained 185 | syn keyword pythonType AbstractSet MutableSet Mapping MutableMapping Sequence MutableSequence ByteString Deque List contained 186 | syn keyword pythonType Set FrozenSet MappingView KeysView ItemsView ValuesView Awaitable Coroutine AsyncIterable contained 187 | syn keyword pythonType AsyncIterator ContextManager Dict DefaultDict Generator AsyncGenerator Text NamedTuple contained 188 | syn keyword pythonType Iterable Iterator Reversible SupportsInt SupportsFloat SupportsAbs SupportsRound Container contained 189 | syn keyword pythonType Hashable Sized Collection contained 190 | endif 191 | endif 192 | 193 | syn match pythonStatement "\" display 194 | syn match pythonImport "\" display 195 | 196 | if s:Python2Syntax() 197 | if !s:Enabled("g:python_print_as_function") 198 | syn keyword pythonStatement print 199 | endif 200 | syn keyword pythonImport as 201 | syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" display contained 202 | else 203 | syn keyword pythonStatement as nonlocal 204 | syn match pythonStatement "\" display 205 | syn keyword pythonBuiltinObj None True False 206 | syn match pythonFunction "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" nextgroup=pythonFuncArgs display contained 207 | syn keyword pythonStatement await async 208 | syn match pythonStatement "\" display nextgroup=pythonFunction skipwhite 209 | syn match pythonStatement "\" display 210 | syn match pythonStatement "\" display 211 | endif 212 | 213 | syn cluster pythonTypeExpression contains=pythonTypeAnno,pythonTypeUnion,pythonTypeArgs 214 | 215 | syn cluster pythonExpression contains= 216 | \ pythonFuncArgs, 217 | \ pythonDictSetExpr, 218 | \ pythonListSliceExpr, 219 | \ pythonLambdaExpr, 220 | \ pythonStatement, 221 | \ pythonRepeat, 222 | \ pythonConditional, 223 | \ pythonComment, 224 | \ pythonOperator, 225 | \ pythonNumber, 226 | \ pythonNumberError, 227 | \ pythonFloat, 228 | \ pythonHexNumber, 229 | \ pythonOctNumber, 230 | \ pythonBytes, 231 | \ pythonString, 232 | \ pythonRawString, 233 | \ pythonUniString, 234 | \ pythonUniRawString, 235 | \ pythonFString, 236 | \ pythonExClass, 237 | \ pythonBuiltinObj, 238 | \ pythonBuiltinFunc 239 | 240 | syn cluster pythonFExpression contains= 241 | \ pythonStatement, 242 | \ pythonDictSetExpr, 243 | \ pythonListSliceExpr, 244 | \ pythonLambdaExpr, 245 | \ pythonRepeat, 246 | \ pythonConditional, 247 | \ pythonOperator, 248 | \ pythonNumber, 249 | \ pythonHexNumber, 250 | \ pythonOctNumber, 251 | \ pythonBinNumber, 252 | \ pythonFloat, 253 | \ pythonString, 254 | \ pythonBytes, 255 | \ pythonBuiltinObj, 256 | \ pythonBuiltinFunc 257 | 258 | " 259 | " Decorators (new in Python 2.4) 260 | " 261 | 262 | syn match pythonDecorator "^\s*\zs@" display nextgroup=pythonDottedName skipwhite 263 | if s:Python2Syntax() 264 | syn match pythonDottedName "[a-zA-Z_][a-zA-Z0-9_]*\%(\.[a-zA-Z_][a-zA-Z0-9_]*\)*" display contained 265 | else 266 | syn match pythonDottedName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\)*" display contained 267 | endif 268 | syn match pythonDot "\." display containedin=pythonDottedName 269 | 270 | " 271 | " Comments 272 | " 273 | 274 | syn match pythonComment "#.*$" display contains=pythonTodo,@Spell 275 | if !s:Enabled("g:python_highlight_file_headers_as_comments") 276 | syn match pythonRun "\%^#!.*$" 277 | syn match pythonCoding "\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$" 278 | endif 279 | syn keyword pythonTodo TODO FIXME XXX contained 280 | 281 | " 282 | " Errors 283 | " 284 | 285 | syn match pythonError "\<\d\+\D\+\>" display 286 | syn match pythonError "[$?]" display 287 | syn match pythonError "[&|]\{2,}" display 288 | syn match pythonError "[=]\{3,}" display 289 | 290 | " Mixing spaces and tabs also may be used for pretty formatting multiline 291 | " statements 292 | if s:Enabled("g:python_highlight_indent_errors") 293 | syn match pythonIndentError "^\s*\%( \t\|\t \)\s*\S"me=e-1 display 294 | endif 295 | 296 | " Trailing space errors 297 | if s:Enabled("g:python_highlight_space_errors") 298 | syn match pythonSpaceError "\s\+$" display 299 | endif 300 | 301 | " 302 | " Strings 303 | " 304 | 305 | if s:Python2Syntax() 306 | " Python 2 strings 307 | syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell 308 | syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell 309 | syn region pythonString start=+[bB]\="""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell 310 | syn region pythonString start=+[bB]\='''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell 311 | else 312 | " Python 3 byte strings 313 | syn region pythonBytes start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell 314 | syn region pythonBytes start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell 315 | syn region pythonBytes start=+[bB]"""+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell 316 | syn region pythonBytes start=+[bB]'''+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell 317 | 318 | syn match pythonBytesError ".\+" display contained 319 | syn match pythonBytesContent "[\u0000-\u00ff]\+" display contained contains=pythonBytesEscape,pythonBytesEscapeError 320 | endif 321 | 322 | syn match pythonBytesEscape +\\[abfnrtv'"\\]+ display contained 323 | syn match pythonBytesEscape "\\\o\o\=\o\=" display contained 324 | syn match pythonBytesEscapeError "\\\o\{,2}[89]" display contained 325 | syn match pythonBytesEscape "\\x\x\{2}" display contained 326 | syn match pythonBytesEscapeError "\\x\x\=\X" display contained 327 | syn match pythonBytesEscape "\\$" 328 | 329 | syn match pythonUniEscape "\\u\x\{4}" display contained 330 | syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained 331 | syn match pythonUniEscape "\\U\x\{8}" display contained 332 | syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained 333 | syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained 334 | syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained 335 | 336 | if s:Python2Syntax() 337 | " Python 2 Unicode strings 338 | syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell 339 | syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell 340 | syn region pythonUniString start=+[uU]"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell 341 | syn region pythonUniString start=+[uU]'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell 342 | else 343 | " Python 3 strings 344 | syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell 345 | syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell 346 | syn region pythonString start=+"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell 347 | syn region pythonString start=+'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell 348 | 349 | syn region pythonFString start=+[fF]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell 350 | syn region pythonFString start=+[fF]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell 351 | syn region pythonFString start=+[fF]"""+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell 352 | syn region pythonFString start=+[fF]'''+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell 353 | endif 354 | 355 | if s:Python2Syntax() 356 | " Python 2 Unicode raw strings 357 | syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell 358 | syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell 359 | syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell 360 | syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell 361 | 362 | syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained 363 | syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained 364 | endif 365 | 366 | " Python 2/3 raw strings 367 | if s:Python2Syntax() 368 | syn region pythonRawString start=+[bB]\=[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell 369 | syn region pythonRawString start=+[bB]\=[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell 370 | syn region pythonRawString start=+[bB]\=[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell 371 | syn region pythonRawString start=+[bB]\=[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell 372 | else 373 | syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell 374 | syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell 375 | syn region pythonRawString start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell 376 | syn region pythonRawString start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell 377 | 378 | syn region pythonRawBytes start=+[bB][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell 379 | syn region pythonRawBytes start=+[bB][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell 380 | syn region pythonRawBytes start=+[bB][rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell 381 | syn region pythonRawBytes start=+[bB][rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell 382 | endif 383 | 384 | syn match pythonRawEscape +\\['"]+ display transparent contained 385 | 386 | if s:Enabled("g:python_highlight_string_formatting") 387 | " % operator string formatting 388 | if s:Python2Syntax() 389 | syn match pythonStrFormatting "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString 390 | syn match pythonStrFormatting "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString 391 | else 392 | syn match pythonStrFormatting "%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString 393 | syn match pythonStrFormatting "%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonRawString 394 | endif 395 | endif 396 | 397 | if s:Enabled("g:python_highlight_string_format") 398 | " str.format syntax 399 | if s:Python2Syntax() 400 | syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString 401 | syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString 402 | else 403 | syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString 404 | syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonRawString 405 | syn region pythonStrInterpRegion matchgroup=pythonStrInterpRegion start="{" end="}" extend contained containedin=pythonFString contains=@pythonFExpression 406 | endif 407 | endif 408 | 409 | if s:Enabled("g:python_highlight_string_templates") 410 | " string.Template format 411 | if s:Python2Syntax() 412 | syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString 413 | syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString 414 | syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString 415 | else 416 | syn match pythonStrTemplate "\$\$" contained containedin=pythonString,pythonRawString 417 | syn match pythonStrTemplate "\${[a-zA-Z_][a-zA-Z0-9_]*}" contained containedin=pythonString,pythonRawString 418 | syn match pythonStrTemplate "\$[a-zA-Z_][a-zA-Z0-9_]*" contained containedin=pythonString,pythonRawString 419 | endif 420 | endif 421 | 422 | if s:Enabled("g:python_highlight_doctests") 423 | " DocTests 424 | syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained 425 | syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained 426 | endif 427 | 428 | " 429 | " Numbers (ints, longs, floats, complex) 430 | " 431 | 432 | if s:Python2Syntax() 433 | syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>" display 434 | syn match pythonOctError "\<0[oO]\=\o*\D\+\d*[lL]\=\>" display 435 | syn match pythonBinError "\<0[bB][01]*\D\+\d*[lL]\=\>" display 436 | 437 | syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display 438 | syn match pythonOctNumber "\<0[oO]\o\+[lL]\=\>" display 439 | syn match pythonBinNumber "\<0[bB][01]\+[lL]\=\>" display 440 | 441 | syn match pythonNumberError "\<\d\+\D[lL]\=\>" display 442 | syn match pythonNumber "\<\d[lL]\=\>" display 443 | syn match pythonNumber "\<[0-9]\d\+[lL]\=\>" display 444 | syn match pythonNumber "\<\d\+[lLjJ]\>" display 445 | 446 | syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*[lL]\=\>" display 447 | syn match pythonBinError "\<0[bB][01]*[2-9]\d*[lL]\=\>" display 448 | 449 | syn match pythonFloat "\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>" display 450 | syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display 451 | syn match pythonFloat "\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=" display 452 | else 453 | syn match pythonHexError "\<0[xX]\x*[g-zG-Z]\x*\>" display 454 | syn match pythonOctError "\<0[oO]\=\o*\D\+\d*\>" display 455 | syn match pythonBinError "\<0[bB][01]*\D\+\d*\>" display 456 | 457 | syn match pythonHexNumber "\<0[xX][_0-9a-fA-F]*\x\>" display 458 | syn match pythonOctNumber "\<0[oO][_0-7]*\o\>" display 459 | syn match pythonBinNumber "\<0[bB][_01]*[01]\>" display 460 | 461 | syn match pythonNumberError "\<\d[_0-9]*\D\>" display 462 | syn match pythonNumberError "\<0[_0-9]\+\>" display 463 | syn match pythonNumberError "\<\d[_0-9]*_\>" display 464 | syn match pythonNumber "\<\d\>" display 465 | syn match pythonNumber "\<[1-9][_0-9]*\d\>" display 466 | syn match pythonNumber "\<\d[jJ]\>" display 467 | syn match pythonNumber "\<[1-9][_0-9]*\d[jJ]\>" display 468 | 469 | syn match pythonOctError "\<0[oO]\=\o*[8-9]\d*\>" display 470 | syn match pythonBinError "\<0[bB][01]*[2-9]\d*\>" display 471 | 472 | syn match pythonFloat "\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=\>" display 473 | syn match pythonFloat "\<\d\%([_0-9]*\d\)\=[eE][+-]\=\d\%([_0-9]*\d\)\=[jJ]\=\>" display 474 | syn match pythonFloat "\<\d\%([_0-9]*\d\)\=\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=" display 475 | endif 476 | 477 | " 478 | " Builtin objects and types 479 | " 480 | 481 | if s:Enabled("g:python_highlight_builtin_objs") 482 | if s:Python2Syntax() 483 | syn keyword pythonBuiltinObj None False True 484 | endif 485 | syn keyword pythonBuiltinObj Ellipsis NotImplemented self cls 486 | syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__ 487 | endif 488 | 489 | " 490 | " Builtin functions 491 | " 492 | 493 | if s:Enabled("g:python_highlight_builtin_funcs") 494 | if s:Python2Syntax() 495 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 496 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 497 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 498 | if s:Enabled("g:python_print_as_function") 499 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 500 | endif 501 | else 502 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 505 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 506 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 507 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 508 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 509 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 510 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 511 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 512 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 513 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 514 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 515 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 516 | syn match pythonBuiltinFunc '\v(\.)@\ze\(' nextgroup=pythonFuncArgs 517 | endif 518 | 519 | " 520 | " Builtin exceptions and warnings 521 | " 522 | 523 | if s:Enabled("g:python_highlight_exceptions") 524 | if s:Python2Syntax() 525 | syn keyword pythonExClass StandardError nextgroup=pythonFuncArgs 526 | else 527 | syn keyword pythonExClass BlockingIOError ChildProcessError nextgroup=pythonFuncArgs 528 | syn keyword pythonExClass ConnectionError BrokenPipeError nextgroup=pythonFuncArgs 529 | syn keyword pythonExClass ConnectionAbortedError ConnectionRefusedError nextgroup=pythonFuncArgs 530 | syn keyword pythonExClass ConnectionResetError FileExistsError nextgroup=pythonFuncArgs 531 | syn keyword pythonExClass FileNotFoundError InterruptedError nextgroup=pythonFuncArgs 532 | syn keyword pythonExClass IsADirectoryError NotADirectoryError nextgroup=pythonFuncArgs 533 | syn keyword pythonExClass PermissionError ProcessLookupError TimeoutError nextgroup=pythonFuncArgs 534 | 535 | syn keyword pythonExClass ResourceWarning nextgroup=pythonFuncArgs 536 | endif 537 | syn keyword pythonExClass BaseException nextgroup=pythonFuncArgs 538 | syn keyword pythonExClass Exception ArithmeticError nextgroup=pythonFuncArgs 539 | syn keyword pythonExClass LookupError EnvironmentError nextgroup=pythonFuncArgs 540 | 541 | syn keyword pythonExClass AssertionError AttributeError BufferError EOFError nextgroup=pythonFuncArgs 542 | syn keyword pythonExClass FloatingPointError GeneratorExit IOError nextgroup=pythonFuncArgs 543 | syn keyword pythonExClass ImportError IndexError KeyError nextgroup=pythonFuncArgs 544 | syn keyword pythonExClass KeyboardInterrupt MemoryError NameError nextgroup=pythonFuncArgs 545 | syn keyword pythonExClass NotImplementedError OSError OverflowError nextgroup=pythonFuncArgs 546 | syn keyword pythonExClass ReferenceError RuntimeError StopIteration nextgroup=pythonFuncArgs 547 | syn keyword pythonExClass SyntaxError IndentationError TabError nextgroup=pythonFuncArgs 548 | syn keyword pythonExClass SystemError SystemExit TypeError nextgroup=pythonFuncArgs 549 | syn keyword pythonExClass UnboundLocalError UnicodeError nextgroup=pythonFuncArgs 550 | syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError nextgroup=pythonFuncArgs 551 | syn keyword pythonExClass UnicodeTranslateError ValueError VMSError nextgroup=pythonFuncArgs 552 | syn keyword pythonExClass WindowsError ZeroDivisionError nextgroup=pythonFuncArgs 553 | 554 | syn keyword pythonExClass Warning UserWarning BytesWarning DeprecationWarning nextgroup=pythonFuncArgs 555 | syn keyword pythonExClass PendingDepricationWarning SyntaxWarning nextgroup=pythonFuncArgs 556 | syn keyword pythonExClass RuntimeWarning FutureWarning nextgroup=pythonFuncArgs 557 | syn keyword pythonExClass ImportWarning UnicodeWarning nextgroup=pythonFuncArgs 558 | endif 559 | 560 | if s:Enabled("g:python_slow_sync") 561 | syn sync minlines=2000 562 | else 563 | " This is fast but code inside triple quoted strings screws it up. It 564 | " is impossible to fix because the only way to know if you are inside a 565 | " triple quoted string is to start from the beginning of the file. 566 | syn sync match pythonSync grouphere NONE "):$" 567 | syn sync maxlines=200 568 | endif 569 | 570 | if version >= 508 || !exists("did_python_syn_inits") 571 | if version <= 508 572 | let did_python_syn_inits = 1 573 | command -nargs=+ HiLink hi link 574 | else 575 | command -nargs=+ HiLink hi def link 576 | endif 577 | 578 | HiLink pythonStatement Statement 579 | HiLink pythonLambdaExpr Statement 580 | HiLink pythonImport Include 581 | HiLink pythonFunction Function 582 | HiLink pythonConditional Conditional 583 | HiLink pythonRepeat Repeat 584 | HiLink pythonException Exception 585 | HiLink pythonOperator Operator 586 | 587 | HiLink pythonDecorator Define 588 | HiLink pythonDottedName Function 589 | HiLink pythonDot Normal 590 | 591 | HiLink pythonComment Comment 592 | if !s:Enabled("g:python_highlight_file_headers_as_comments") 593 | HiLink pythonCoding Special 594 | HiLink pythonRun Special 595 | endif 596 | HiLink pythonTodo Todo 597 | 598 | HiLink pythonError Error 599 | HiLink pythonIndentError Error 600 | HiLink pythonSpaceError Error 601 | 602 | HiLink pythonString String 603 | HiLink pythonRawString String 604 | 605 | HiLink pythonUniEscape Special 606 | HiLink pythonUniEscapeError Error 607 | 608 | if s:Python2Syntax() 609 | HiLink pythonUniString String 610 | HiLink pythonUniRawString String 611 | HiLink pythonUniRawEscape Special 612 | HiLink pythonUniRawEscapeError Error 613 | else 614 | HiLink pythonBytes String 615 | HiLink pythonRawBytes String 616 | HiLink pythonBytesContent String 617 | HiLink pythonBytesError Error 618 | HiLink pythonBytesEscape Special 619 | HiLink pythonBytesEscapeError Error 620 | HiLink pythonFString String 621 | HiLink pythonStrInterpRegion Special 622 | endif 623 | 624 | HiLink pythonStrFormatting Special 625 | HiLink pythonStrFormat Special 626 | HiLink pythonStrTemplate Special 627 | 628 | HiLink pythonDocTest Special 629 | HiLink pythonDocTest2 Special 630 | 631 | HiLink pythonNumber Number 632 | HiLink pythonHexNumber Number 633 | HiLink pythonOctNumber Number 634 | HiLink pythonBinNumber Number 635 | HiLink pythonFloat Float 636 | HiLink pythonNumberError Error 637 | HiLink pythonOctError Error 638 | HiLink pythonHexError Error 639 | HiLink pythonBinError Error 640 | 641 | HiLink pythonBuiltinObj Structure 642 | HiLink pythonBuiltinFunc Function 643 | 644 | HiLink pythonExClass Structure 645 | 646 | HiLink pythonTypeAnno Optional 647 | HiLink pythonTypeUnion Optional 648 | HiLink pythonTypeAnnoReturn Optional 649 | HiLink pythonTypeArgs Optional 650 | HiLink pythonType Special 651 | 652 | delcommand HiLink 653 | 654 | " default style for custom highlight group (may be overriden in colors) 655 | hi Optional gui=italic cterm=italic 656 | endif 657 | 658 | let b:current_syntax = "python" 659 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # Above the run-comment and file encoding comment. 4 | 5 | # Comments. 6 | 7 | # TODO FIXME XXX 8 | 9 | # Keywords. 10 | 11 | with break continue del exec return pass print raise global assert yield 12 | for while if elif else import from as try except finally and in is not or 13 | 14 | lambda: a + 1 15 | lambda x, y: x + y 16 | 17 | yield from 18 | 19 | def functionname 20 | class Classname 21 | def функция 22 | class Класс 23 | 24 | await 25 | async def Test 26 | async with 27 | async for 28 | 29 | # Type annotations 30 | 31 | def myfunc(a: str, something_other, 32 | b: Callable[[str, str], int], 33 | c: mypkg.MyType) -> 'runtime_resolved_type': 34 | myval: float 35 | mygood: Optional[int, Any] = b('wow', 'oops') 36 | myarr: Sequence[int] = origarr[aa:bb] + (lambda: x)() 37 | mykey = a 38 | wow = { 39 | mykey: this_should_not_be_type_anno[Any], 40 | 'b': some_data, 41 | } 42 | call_with_dict(a={ 43 | 'a': asdf, 44 | 'b': 'zxcb', 45 | mykey: this_should_not_be_type_anno[Any], 46 | }, b=mydata['a']) 47 | vanilla_lambda = lambda x, y: myval + 1.0 48 | call_with_lambda(lambda x, y: myval + 1.0) 49 | call_with_slice(mydata[range_start:range_end]) 50 | 51 | 52 | # Builtin objects. 53 | 54 | True False Ellipsis None NotImplemented 55 | 56 | # Builtin function and types. 57 | 58 | __import__() abs() all() any() apply() basestring() bool() buffer() callable() chr() classmethod() 59 | cmp() coerce() compile() complex() delattr() dict() dir() divmod() enumerate() eval() execfile() file() 60 | filter() float() frozenset() getattr() globals() hasattr() hash() help() hex() id() input() int() 61 | intern() isinstance() issubclass() iter() len() list() locals() long() map() max() min() object() oct() 62 | open() ord() pow() property() range() raw_input() reduce() reload() repr() reversed() round() set() 63 | setattr() slice() sorted() staticmethod() str() sum() super() tuple() type() unichr() unicode() vars() 64 | xrange() zip() 65 | 66 | when_we_dont_call = a.float 67 | float = when_we_dont_call 68 | 69 | when_we_call = float(x) 70 | when_we_call = min(a, b) 71 | 72 | # Builtin exceptions and warnings. 73 | 74 | BaseException Exception StandardError ArithmeticError LookupError 75 | EnvironmentError 76 | 77 | AssertionError AttributeError EOFError FloatingPointError GeneratorExit IOError 78 | ImportError IndexError KeyError KeyboardInterrupt MemoryError NameError 79 | NotImplementedError OSError OverflowError ReferenceError RuntimeError 80 | StopIteration SyntaxError IndentationError TabError SystemError SystemExit 81 | TypeError UnboundLocalError UnicodeError UnicodeEncodeError UnicodeDecodeError 82 | UnicodeTranslateError ValueError WindowsError ZeroDivisionError 83 | 84 | Warning UserWarning DeprecationWarning PendingDepricationWarning SyntaxWarning 85 | RuntimeWarning FutureWarning ImportWarning UnicodeWarning 86 | 87 | # Decorators. 88 | 89 | @ decoratorname 90 | @ object.__init__(arg1, arg2) 91 | @ декоратор 92 | @ декоратор.décorateur 93 | 94 | # Numbers 95 | 96 | 0 1 2 9 10 0x1f .3 12.34 0j 124j 34.2E-3 0b10 0o77 1023434 0x0 97 | 1_1 1_1.2_2 1_2j 0x_1f 0x1_f 34_56e-3 34_56e+3_1 0o7_7 98 | 99 | # Erroneous numbers 100 | 101 | 077 100L 0xfffffffL 0L 08 0xk 0x 0b102 0o78 0o123LaB 102 | 0_ 0_1 0_x1f 0x1f_ 0_b77 0b77_ .2_ 1_j 103 | 104 | # Strings 105 | 106 | " test " ' test ' 107 | """ 108 | test 109 | """ 110 | ''' 111 | test 112 | ''' 113 | 114 | " \a\b\c\"\'\n\r \x34\077 \08 \xag" 115 | r" \" \' " 116 | 117 | "testтест" 118 | 119 | b"test" 120 | 121 | b"test\r\n\xffff" 122 | 123 | b"тестtest" 124 | 125 | br"test" 126 | 127 | br"\a\b\n\r" 128 | 129 | # Formattings 130 | 131 | " %f " 132 | b" %f " 133 | 134 | "{0.name!r:b} {0[n]} {name!s: } {{test}} {{}} {} {.__len__:s}" 135 | b"{0.name!r:b} {0[n]} {name!s: } {{test}} {{}} {} {.__len__:s}" 136 | 137 | "${test} ${test ${test}aname $$$ $test+nope" 138 | b"${test} ${test ${test}aname $$$ $test+nope" 139 | 140 | f"{var}...{arr[123]} normal {var['{'] // 0xff} \"xzcb\" 'xzcb' {var['}'] + 1} text" 141 | f"{expr1 if True or False else expr2} wow {','.join(c.lower() for c in 'asdf')}" 142 | f"hello {expr:.2f} yes {(lambda: 0b1)():#03x} lol {var!r}" 143 | 144 | # Doctests. 145 | 146 | """ 147 | Test: 148 | >>> a = 5 149 | >>> a 150 | 5 151 | 152 | Test 153 | """ 154 | 155 | ''' 156 | Test: 157 | >>> a = 5 158 | >>> a 159 | 5 160 | 161 | Test 162 | ''' 163 | 164 | # Erroneous symbols or bad variable names. 165 | 166 | $ ? 6xav 167 | 168 | && || === 169 | 170 | # Indentation errors. 171 | 172 | break 173 | 174 | # Trailing space errors. 175 | 176 | 177 | break 178 | """ 179 | 180 | test 181 | """ 182 | --------------------------------------------------------------------------------