diff --git a/README.md b/README.md index e215e22..ef85fa9 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,8 @@ If you need full functionality of any plugin, please use it directly with your p - [puppet](https://github.com/voxpupuli/vim-puppet) (syntax, indent, ftplugin) - [purescript](https://github.com/purescript-contrib/purescript-vim) (syntax, indent, ftplugin) - [python-compiler](https://github.com/aliev/vim-compiler-python) (compiler, autoload) -- [python](https://github.com/mitsuhiko/vim-python-combined) (syntax, indent) +- [python-ident](https://github.com/Vimjas/vim-python-pep8-indent) (indent) +- [python](https://github.com/vim-python/python-syntax) (syntax) - [qml](https://github.com/peterhoeg/vim-qml) (syntax, indent, ftplugin) - [r-lang](https://github.com/vim-scripts/R.vim) (syntax, ftplugin) - [racket](https://github.com/wlangstroth/vim-racket) (syntax, indent, autoload, ftplugin) diff --git a/build b/build index cd00ddc..2a1398a 100755 --- a/build +++ b/build @@ -181,8 +181,9 @@ PACKS=" pug:digitaltoad/vim-pug puppet:voxpupuli/vim-puppet purescript:purescript-contrib/purescript-vim - python:mitsuhiko/vim-python-combined + python:vim-python/python-syntax python-compiler:aliev/vim-compiler-python + python-ident:Vimjas/vim-python-pep8-indent qml:peterhoeg/vim-qml r-lang:vim-scripts/R.vim racket:wlangstroth/vim-racket diff --git a/config.vim b/config.vim index d37407f..09f5eb8 100644 --- a/config.vim +++ b/config.vim @@ -13,3 +13,5 @@ let g:filetype_euphoria = 'elixir' augroup filetypedetect autocmd BufNewFile,BufReadPost *.vb setlocal filetype=vbnet augroup END + +let g:python_highlight_all = 1 diff --git a/ftdetect/polyglot.vim b/ftdetect/polyglot.vim index 4ae4ec4..e5d4cc2 100644 --- a/ftdetect/polyglot.vim +++ b/ftdetect/polyglot.vim @@ -13,6 +13,8 @@ let g:filetype_euphoria = 'elixir' augroup filetypedetect autocmd BufNewFile,BufReadPost *.vb setlocal filetype=vbnet augroup END + +let g:python_highlight_all = 1 augroup filetypedetect " apiblueprint:sheerun/apiblueprint.vim autocmd BufReadPost,BufNewFile *.apib set filetype=apiblueprint @@ -780,7 +782,7 @@ au FileType purescript let &l:commentstring='{--%s--}' augroup END augroup filetypedetect -" python:mitsuhiko/vim-python-combined +" python:vim-python/python-syntax augroup END augroup filetypedetect @@ -793,6 +795,10 @@ augroup filetypedetect autocmd FileType python compiler python augroup END +augroup filetypedetect +" python-ident:Vimjas/vim-python-pep8-indent +augroup END + augroup filetypedetect " qml:peterhoeg/vim-qml autocmd BufRead,BufNewFile *.qml setfiletype qml diff --git a/indent/cython.vim b/indent/cython.vim new file mode 100644 index 0000000..c6777c0 --- /dev/null +++ b/indent/cython.vim @@ -0,0 +1,433 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'python-ident') == -1 + +" PEP8 compatible Python indent file +" Language: Python +" Maintainer: Daniel Hahler +" Prev Maintainer: Hynek Schlawack +" Prev Maintainer: Eric Mc Sween (address invalid) +" Original Author: David Bustos (address invalid) +" License: CC0 +" +" vim-python-pep8-indent - A nicer Python indentation style for vim. +" Written in 2004 by David Bustos +" Maintained from 2004-2005 by Eric Mc Sween +" Maintained from 2013 by Hynek Schlawack +" Maintained from 2017 by Daniel Hahler +" +" To the extent possible under law, the author(s) have dedicated all copyright +" and related and neighboring rights to this software to the public domain +" worldwide. This software is distributed without any warranty. +" You should have received a copy of the CC0 Public Domain Dedication along +" with this software. If not, see +" . + +" Only load this indent file when no other was loaded. +if exists('b:did_indent') + finish +endif +let b:did_indent = 1 + +setlocal nolisp +setlocal autoindent +setlocal indentexpr=GetPythonPEPIndent(v:lnum) +setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except + +if !exists('g:python_pep8_indent_multiline_string') + let g:python_pep8_indent_multiline_string = 0 +endif + +let s:block_rules = { + \ '^\s*elif\>': ['if', 'elif'], + \ '^\s*except\>': ['try', 'except'], + \ '^\s*finally\>': ['try', 'except', 'else'] + \ } +let s:block_rules_multiple = { + \ '^\s*else\>': ['if', 'elif', 'for', 'try', 'except'], + \ } +" Pairs to look for when searching for opening parenthesis. +" The value is the maximum offset in lines. +let s:paren_pairs = {'()': 50, '[]': 100, '{}': 1000} + +if &filetype ==# 'pyrex' || &filetype ==# 'cython' + let b:control_statement = '\v^\s*(class|def|if|while|with|for|except|cdef|cpdef)>' +else + let b:control_statement = '\v^\s*(class|def|if|while|with|for|except)>' +endif +let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>' + +" Skip strings and comments. Return 1 for chars to skip. +" jedi* refers to syntax definitions from jedi-vim for call signatures, which +" are inserted temporarily into the buffer. +let s:skip_special_chars = 'synIDattr(synID(line("."), col("."), 0), "name") ' . + \ '=~? "\\vstring|comment|jedi\\S"' + +let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' . + \ '=~? "\\vcomment|jedi\\S"' + +" Also ignore anything concealed. +" Wrapper around synconcealed for older Vim (7.3.429, used on Travis CI). +function! s:is_concealed(line, col) + let concealed = synconcealed(a:line, a:col) + return len(concealed) && concealed[0] +endfunction +if has('conceal') + let s:skip_special_chars .= '|| s:is_concealed(line("."), col("."))' +endif + + +let s:skip_search = 'synIDattr(synID(line("."), col("."), 0), "name") ' . + \ '=~? "comment"' + +" Use 'shiftwidth()' instead of '&sw'. +" (Since Vim patch 7.3.629, 'shiftwidth' can be set to 0 to follow 'tabstop'). +if exists('*shiftwidth') + function! s:sw() + return shiftwidth() + endfunction +else + function! s:sw() + return &shiftwidth + endfunction +endif + +function! s:pair_sort(x, y) + if a:x[0] == a:y[0] + return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1 + else + return a:x[0] > a:y[0] ? 1 : -1 + endif +endfunction + +" Find backwards the closest open parenthesis/bracket/brace. +function! s:find_opening_paren(...) + " optional arguments: line and column (defaults to 1) to search around + if a:0 > 0 + let view = winsaveview() + call cursor(a:1, a:0 > 1 ? a:2 : 1) + let ret = s:find_opening_paren() + call winrestview(view) + return ret + endif + + " Return if cursor is in a comment. + exe 'if' s:skip_search '| return [0, 0] | endif' + + let nearest = [0, 0] + for [p, maxoff] in items(s:paren_pairs) + let stopline = max([0, line('.') - maxoff, nearest[0]]) + let next = searchpairpos( + \ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip_special_chars, stopline) + if next[0] && (next[0] > nearest[0] || (next[0] == nearest[0] && next[1] > nearest[1])) + let nearest = next + endif + endfor + return nearest +endfunction + +" Find the start of a multi-line statement +function! s:find_start_of_multiline_statement(lnum) + let lnum = a:lnum + while lnum > 0 + if getline(lnum - 1) =~# '\\$' + let lnum = prevnonblank(lnum - 1) + else + let [paren_lnum, _] = s:find_opening_paren(lnum) + if paren_lnum < 1 + return lnum + else + let lnum = paren_lnum + endif + endif + endwhile +endfunction + +" Find possible indent(s) of the block starter that matches the current line. +function! s:find_start_of_block(lnum, types, multiple) + let r = [] + let types = copy(a:types) + let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>' + let lnum = a:lnum + let last_indent = indent(lnum) + 1 + while lnum > 0 && last_indent > 0 + let indent = indent(lnum) + if indent < last_indent + for type in types + let re = '\v^\s*'.type.'>' + if getline(lnum) =~# re + if !a:multiple + return [indent] + endif + if index(r, indent) == -1 + let r += [indent] + endif + " Remove any handled type, e.g. 'if'. + call remove(types, index(types, type)) + endif + endfor + let last_indent = indent(lnum) + endif + let lnum = prevnonblank(lnum - 1) + endwhile + return r +endfunction + +" Is "expr" true for every position in "lnum", beginning at "start"? +" (optionally up to a:1 / 4th argument) +function! s:match_expr_on_line(expr, lnum, start, ...) + let text = getline(a:lnum) + let end = a:0 ? a:1 : len(text) + if a:start > end + return 1 + endif + let save_pos = getpos('.') + let r = 1 + for i in range(a:start, end) + call cursor(a:lnum, i) + if !(eval(a:expr) || text[i-1] =~# '\s') + let r = 0 + break + endif + endfor + call setpos('.', save_pos) + return r +endfunction + +" Line up with open parenthesis/bracket/brace. +function! s:indent_like_opening_paren(lnum) + let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum) + if paren_lnum <= 0 + return -2 + endif + let text = getline(paren_lnum) + let base = indent(paren_lnum) + + let nothing_after_opening_paren = s:match_expr_on_line( + \ s:skip_after_opening_paren, paren_lnum, paren_col+1) + let starts_with_closing_paren = getline(a:lnum) =~# '^\s*[])}]' + + if nothing_after_opening_paren + if starts_with_closing_paren + let res = base + else + let res = base + s:sw() + endif + else + " Indent to match position of opening paren. + let res = paren_col + endif + + " If this line is the continuation of a control statement + " indent further to distinguish the continuation line + " from the next logical line. + if text =~# b:control_statement && res == base + s:sw() + return base + s:sw() * 2 + else + return res + endif +endfunction + +" Match indent of first block of this type. +function! s:indent_like_block(lnum) + let text = getline(a:lnum) + for [multiple, block_rules] in [ + \ [0, s:block_rules], + \ [1, s:block_rules_multiple]] + for [line_re, blocks] in items(block_rules) + if text !~# line_re + continue + endif + + let indents = s:find_start_of_block(a:lnum - 1, blocks, multiple) + if !len(indents) + return -1 + endif + if len(indents) == 1 + return indents[0] + endif + + " Multiple valid indents, e.g. for 'else' with both try and if. + let indent = indent(a:lnum) + if index(indents, indent) != -1 + " The indent is valid, keep it. + return indent + endif + " Fallback to the first/nearest one. + return indents[0] + endfor + endfor + return -2 +endfunction + +function! s:indent_like_previous_line(lnum) + let lnum = prevnonblank(a:lnum - 1) + + " No previous line, keep current indent. + if lnum < 1 + return -1 + endif + + let text = getline(lnum) + let start = s:find_start_of_multiline_statement(lnum) + let base = indent(start) + let current = indent(a:lnum) + + " Jump to last character in previous line. + call cursor(lnum, len(text)) + let ignore_last_char = eval(s:skip_special_chars) + + " Search for final colon that is not inside something to be ignored. + while 1 + let curpos = getpos('.')[2] + if curpos == 1 | break | endif + if eval(s:skip_special_chars) || text[curpos-1] =~# '\s' + normal! h + continue + elseif text[curpos-1] ==# ':' + return base + s:sw() + endif + break + endwhile + + if text =~# '\\$' && !ignore_last_char + " If this line is the continuation of a control statement + " indent further to distinguish the continuation line + " from the next logical line. + if getline(start) =~# b:control_statement + return base + s:sw() * 2 + endif + + " Nest (other) explicit continuations only one level deeper. + return base + s:sw() + endif + + let empty = getline(a:lnum) =~# '^\s*$' + + " Current and prev line are empty, next is not -> indent like next. + if empty && a:lnum > 1 && + \ (getline(a:lnum - 1) =~# '^\s*$') && + \ !(getline(a:lnum + 1) =~# '^\s*$') + return indent(a:lnum + 1) + endif + + " If the previous statement was a stop-execution statement or a pass + if getline(start) =~# s:stop_statement + " Remove one level of indentation if the user hasn't already dedented + if empty || current > base - s:sw() + return base - s:sw() + endif + " Otherwise, trust the user + return -1 + endif + + if !empty && s:is_dedented_already(current, base) + return -1 + endif + + " In all other cases, line up with the start of the previous statement. + return base +endfunction + +" If this line is dedented and the number of indent spaces is valid +" (multiple of the indentation size), trust the user. +function! s:is_dedented_already(current, base) + let dedent_size = a:current - a:base + return (dedent_size < 0 && a:current % s:sw() == 0) ? 1 : 0 +endfunction + +" Is the syntax at lnum (and optionally cnum) a python string? +function! s:is_python_string(lnum, ...) + let line = getline(a:lnum) + let linelen = len(line) + if linelen < 1 + let linelen = 1 + endif + let cols = a:0 ? type(a:1) != type([]) ? [a:1] : a:1 : range(1, linelen) + for cnum in cols + if match(map(synstack(a:lnum, cnum), + \ "synIDattr(v:val, 'name')"), 'python\S*String') == -1 + return 0 + end + endfor + return 1 +endfunction + +function! GetPythonPEPIndent(lnum) + " First line has indent 0 + if a:lnum == 1 + return 0 + endif + + let line = getline(a:lnum) + let prevline = getline(a:lnum-1) + + " Multilinestrings: continous, docstring or starting. + if s:is_python_string(a:lnum-1, len(prevline)) + \ && (s:is_python_string(a:lnum, 1) + \ || match(line, '^\%("""\|''''''\)') != -1) + + " Indent closing quotes as the line with the opening ones. + let match_quotes = match(line, '^\s*\zs\%("""\|''''''\)') + if match_quotes != -1 + " closing multiline string + let quotes = line[match_quotes:(match_quotes+2)] + let pairpos = searchpairpos(quotes, '', quotes, 'b') + if pairpos[0] != 0 + return indent(pairpos[0]) + else + " TODO: test to cover this! + endif + endif + + if s:is_python_string(a:lnum-1) + " Previous line is (completely) a string. + return indent(a:lnum-1) + endif + + if match(prevline, '^\s*\%("""\|''''''\)') != -1 + " docstring. + return indent(a:lnum-1) + endif + + let indent_multi = get(b:, 'python_pep8_indent_multiline_string', + \ get(g:, 'python_pep8_indent_multiline_string', 0)) + if match(prevline, '\v%("""|'''''')$') != -1 + " Opening multiline string, started in previous line. + if (&autoindent && indent(a:lnum) == indent(a:lnum-1)) + \ || match(line, '\v^\s+$') != -1 + " with empty line or to split up 'foo("""bar' into + " 'foo("""' and 'bar'. + if indent_multi == -2 + return indent(a:lnum-1) + s:sw() + endif + return indent_multi + endif + endif + + " Keep existing indent. + if match(line, '\v^\s*\S') != -1 + return -1 + endif + + if indent_multi != -2 + return indent_multi + endif + + return s:indent_like_opening_paren(a:lnum) + endif + + " Parens: If we can find an open parenthesis/bracket/brace, line up with it. + let indent = s:indent_like_opening_paren(a:lnum) + if indent >= -1 + return indent + endif + + " Blocks: Match indent of first block of this type. + let indent = s:indent_like_block(a:lnum) + if indent >= -1 + return indent + endif + + return s:indent_like_previous_line(a:lnum) +endfunction + +endif diff --git a/indent/python.vim b/indent/python.vim index 48b3446..c6777c0 100644 --- a/indent/python.vim +++ b/indent/python.vim @@ -1,217 +1,433 @@ -if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'python') == -1 +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'python-ident') == -1 " PEP8 compatible Python indent file +" Language: Python +" Maintainer: Daniel Hahler +" Prev Maintainer: Hynek Schlawack +" Prev Maintainer: Eric Mc Sween (address invalid) +" Original Author: David Bustos (address invalid) +" License: CC0 +" +" vim-python-pep8-indent - A nicer Python indentation style for vim. +" Written in 2004 by David Bustos +" Maintained from 2004-2005 by Eric Mc Sween +" Maintained from 2013 by Hynek Schlawack +" Maintained from 2017 by Daniel Hahler +" +" To the extent possible under law, the author(s) have dedicated all copyright +" and related and neighboring rights to this software to the public domain +" worldwide. This software is distributed without any warranty. +" You should have received a copy of the CC0 Public Domain Dedication along +" with this software. If not, see +" . + " Only load this indent file when no other was loaded. -if exists("b:did_indent") +if exists('b:did_indent') finish endif let b:did_indent = 1 -setlocal expandtab setlocal nolisp setlocal autoindent setlocal indentexpr=GetPythonPEPIndent(v:lnum) setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except -let s:maxoff = 50 +if !exists('g:python_pep8_indent_multiline_string') + let g:python_pep8_indent_multiline_string = 0 +endif + +let s:block_rules = { + \ '^\s*elif\>': ['if', 'elif'], + \ '^\s*except\>': ['try', 'except'], + \ '^\s*finally\>': ['try', 'except', 'else'] + \ } +let s:block_rules_multiple = { + \ '^\s*else\>': ['if', 'elif', 'for', 'try', 'except'], + \ } +" Pairs to look for when searching for opening parenthesis. +" The value is the maximum offset in lines. +let s:paren_pairs = {'()': 50, '[]': 100, '{}': 1000} + +if &filetype ==# 'pyrex' || &filetype ==# 'cython' + let b:control_statement = '\v^\s*(class|def|if|while|with|for|except|cdef|cpdef)>' +else + let b:control_statement = '\v^\s*(class|def|if|while|with|for|except)>' +endif +let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>' + +" Skip strings and comments. Return 1 for chars to skip. +" jedi* refers to syntax definitions from jedi-vim for call signatures, which +" are inserted temporarily into the buffer. +let s:skip_special_chars = 'synIDattr(synID(line("."), col("."), 0), "name") ' . + \ '=~? "\\vstring|comment|jedi\\S"' + +let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' . + \ '=~? "\\vcomment|jedi\\S"' + +" Also ignore anything concealed. +" Wrapper around synconcealed for older Vim (7.3.429, used on Travis CI). +function! s:is_concealed(line, col) + let concealed = synconcealed(a:line, a:col) + return len(concealed) && concealed[0] +endfunction +if has('conceal') + let s:skip_special_chars .= '|| s:is_concealed(line("."), col("."))' +endif + + +let s:skip_search = 'synIDattr(synID(line("."), col("."), 0), "name") ' . + \ '=~? "comment"' + +" Use 'shiftwidth()' instead of '&sw'. +" (Since Vim patch 7.3.629, 'shiftwidth' can be set to 0 to follow 'tabstop'). +if exists('*shiftwidth') + function! s:sw() + return shiftwidth() + endfunction +else + function! s:sw() + return &shiftwidth + endfunction +endif + +function! s:pair_sort(x, y) + if a:x[0] == a:y[0] + return a:x[1] == a:y[1] ? 0 : a:x[1] > a:y[1] ? 1 : -1 + else + return a:x[0] > a:y[0] ? 1 : -1 + endif +endfunction " Find backwards the closest open parenthesis/bracket/brace. -function! s:SearchParensPair() - let line = line('.') - let col = col('.') +function! s:find_opening_paren(...) + " optional arguments: line and column (defaults to 1) to search around + if a:0 > 0 + let view = winsaveview() + call cursor(a:1, a:0 > 1 ? a:2 : 1) + let ret = s:find_opening_paren() + call winrestview(view) + return ret + endif - " Skip strings and comments and don't look too far - let skip = "line('.') < " . (line - s:maxoff) . " ? dummy :" . - \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? ' . - \ '"string\\|comment"' + " Return if cursor is in a comment. + exe 'if' s:skip_search '| return [0, 0] | endif' - " Search for parentheses - call cursor(line, col) - let parlnum = searchpair('(', '', ')', 'bW', skip) - let parcol = col('.') - - " Search for brackets - call cursor(line, col) - let par2lnum = searchpair('\[', '', '\]', 'bW', skip) - let par2col = col('.') - - " Search for braces - call cursor(line, col) - let par3lnum = searchpair('{', '', '}', 'bW', skip) - let par3col = col('.') - - " Get the closest match - if par2lnum > parlnum || (par2lnum == parlnum && par2col > parcol) - let parlnum = par2lnum - let parcol = par2col - endif - if par3lnum > parlnum || (par3lnum == parlnum && par3col > parcol) - let parlnum = par3lnum - let parcol = par3col - endif - - " Put the cursor on the match - if parlnum > 0 - call cursor(parlnum, parcol) - endif - return parlnum + let nearest = [0, 0] + for [p, maxoff] in items(s:paren_pairs) + let stopline = max([0, line('.') - maxoff, nearest[0]]) + let next = searchpairpos( + \ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip_special_chars, stopline) + if next[0] && (next[0] > nearest[0] || (next[0] == nearest[0] && next[1] > nearest[1])) + let nearest = next + endif + endfor + return nearest endfunction " Find the start of a multi-line statement -function! s:StatementStart(lnum) - let lnum = a:lnum - while 1 - if getline(lnum - 1) =~ '\\$' - let lnum = lnum - 1 - else - call cursor(lnum, 1) - let maybe_lnum = s:SearchParensPair() - if maybe_lnum < 1 - return lnum - else - let lnum = maybe_lnum - endif - endif - endwhile +function! s:find_start_of_multiline_statement(lnum) + let lnum = a:lnum + while lnum > 0 + if getline(lnum - 1) =~# '\\$' + let lnum = prevnonblank(lnum - 1) + else + let [paren_lnum, _] = s:find_opening_paren(lnum) + if paren_lnum < 1 + return lnum + else + let lnum = paren_lnum + endif + endif + endwhile endfunction -" Find the block starter that matches the current line -function! s:BlockStarter(lnum, block_start_re) - let lnum = a:lnum - let maxindent = 10000 " whatever - while lnum > 1 - let lnum = prevnonblank(lnum - 1) - if indent(lnum) < maxindent - if getline(lnum) =~ a:block_start_re - return lnum - else - let maxindent = indent(lnum) - " It's not worth going further if we reached the top level - if maxindent == 0 - return -1 +" Find possible indent(s) of the block starter that matches the current line. +function! s:find_start_of_block(lnum, types, multiple) + let r = [] + let types = copy(a:types) + let re = '\V\^\s\*\('.join(a:types, '\|').'\)\>' + let lnum = a:lnum + let last_indent = indent(lnum) + 1 + while lnum > 0 && last_indent > 0 + let indent = indent(lnum) + if indent < last_indent + for type in types + let re = '\v^\s*'.type.'>' + if getline(lnum) =~# re + if !a:multiple + return [indent] + endif + if index(r, indent) == -1 + let r += [indent] + endif + " Remove any handled type, e.g. 'if'. + call remove(types, index(types, type)) + endif + endfor + let last_indent = indent(lnum) endif - endif + let lnum = prevnonblank(lnum - 1) + endwhile + return r +endfunction + +" Is "expr" true for every position in "lnum", beginning at "start"? +" (optionally up to a:1 / 4th argument) +function! s:match_expr_on_line(expr, lnum, start, ...) + let text = getline(a:lnum) + let end = a:0 ? a:1 : len(text) + if a:start > end + return 1 endif - endwhile - return -1 + let save_pos = getpos('.') + let r = 1 + for i in range(a:start, end) + call cursor(a:lnum, i) + if !(eval(a:expr) || text[i-1] =~# '\s') + let r = 0 + break + endif + endfor + call setpos('.', save_pos) + return r +endfunction + +" Line up with open parenthesis/bracket/brace. +function! s:indent_like_opening_paren(lnum) + let [paren_lnum, paren_col] = s:find_opening_paren(a:lnum) + if paren_lnum <= 0 + return -2 + endif + let text = getline(paren_lnum) + let base = indent(paren_lnum) + + let nothing_after_opening_paren = s:match_expr_on_line( + \ s:skip_after_opening_paren, paren_lnum, paren_col+1) + let starts_with_closing_paren = getline(a:lnum) =~# '^\s*[])}]' + + if nothing_after_opening_paren + if starts_with_closing_paren + let res = base + else + let res = base + s:sw() + endif + else + " Indent to match position of opening paren. + let res = paren_col + endif + + " If this line is the continuation of a control statement + " indent further to distinguish the continuation line + " from the next logical line. + if text =~# b:control_statement && res == base + s:sw() + return base + s:sw() * 2 + else + return res + endif +endfunction + +" Match indent of first block of this type. +function! s:indent_like_block(lnum) + let text = getline(a:lnum) + for [multiple, block_rules] in [ + \ [0, s:block_rules], + \ [1, s:block_rules_multiple]] + for [line_re, blocks] in items(block_rules) + if text !~# line_re + continue + endif + + let indents = s:find_start_of_block(a:lnum - 1, blocks, multiple) + if !len(indents) + return -1 + endif + if len(indents) == 1 + return indents[0] + endif + + " Multiple valid indents, e.g. for 'else' with both try and if. + let indent = indent(a:lnum) + if index(indents, indent) != -1 + " The indent is valid, keep it. + return indent + endif + " Fallback to the first/nearest one. + return indents[0] + endfor + endfor + return -2 +endfunction + +function! s:indent_like_previous_line(lnum) + let lnum = prevnonblank(a:lnum - 1) + + " No previous line, keep current indent. + if lnum < 1 + return -1 + endif + + let text = getline(lnum) + let start = s:find_start_of_multiline_statement(lnum) + let base = indent(start) + let current = indent(a:lnum) + + " Jump to last character in previous line. + call cursor(lnum, len(text)) + let ignore_last_char = eval(s:skip_special_chars) + + " Search for final colon that is not inside something to be ignored. + while 1 + let curpos = getpos('.')[2] + if curpos == 1 | break | endif + if eval(s:skip_special_chars) || text[curpos-1] =~# '\s' + normal! h + continue + elseif text[curpos-1] ==# ':' + return base + s:sw() + endif + break + endwhile + + if text =~# '\\$' && !ignore_last_char + " If this line is the continuation of a control statement + " indent further to distinguish the continuation line + " from the next logical line. + if getline(start) =~# b:control_statement + return base + s:sw() * 2 + endif + + " Nest (other) explicit continuations only one level deeper. + return base + s:sw() + endif + + let empty = getline(a:lnum) =~# '^\s*$' + + " Current and prev line are empty, next is not -> indent like next. + if empty && a:lnum > 1 && + \ (getline(a:lnum - 1) =~# '^\s*$') && + \ !(getline(a:lnum + 1) =~# '^\s*$') + return indent(a:lnum + 1) + endif + + " If the previous statement was a stop-execution statement or a pass + if getline(start) =~# s:stop_statement + " Remove one level of indentation if the user hasn't already dedented + if empty || current > base - s:sw() + return base - s:sw() + endif + " Otherwise, trust the user + return -1 + endif + + if !empty && s:is_dedented_already(current, base) + return -1 + endif + + " In all other cases, line up with the start of the previous statement. + return base +endfunction + +" If this line is dedented and the number of indent spaces is valid +" (multiple of the indentation size), trust the user. +function! s:is_dedented_already(current, base) + let dedent_size = a:current - a:base + return (dedent_size < 0 && a:current % s:sw() == 0) ? 1 : 0 +endfunction + +" Is the syntax at lnum (and optionally cnum) a python string? +function! s:is_python_string(lnum, ...) + let line = getline(a:lnum) + let linelen = len(line) + if linelen < 1 + let linelen = 1 + endif + let cols = a:0 ? type(a:1) != type([]) ? [a:1] : a:1 : range(1, linelen) + for cnum in cols + if match(map(synstack(a:lnum, cnum), + \ "synIDattr(v:val, 'name')"), 'python\S*String') == -1 + return 0 + end + endfor + return 1 endfunction function! GetPythonPEPIndent(lnum) - let scol = col('.') - - " First line has indent 0 - if a:lnum == 1 - return 0 - endif - - " If we can find an open parenthesis/bracket/brace, line up with it. - call cursor(a:lnum, 1) - let parlnum = s:SearchParensPair() - if parlnum > 0 - let parcol = col('.') - let matches = matchlist(getline(a:lnum), '^\(\s*\)[])}]') - if len(matches) == 0 - let closing_paren = 0 - let closing_paren_pos = 0 - else - let closing_paren = 1 - let closing_paren_pos = len(matches[1]) + " First line has indent 0 + if a:lnum == 1 + return 0 endif - if match(getline(parlnum), '[([{]\s*$', parcol - 1) != -1 - if closing_paren - return indent(parlnum) - else - return indent(parlnum) + &shiftwidth - endif - elseif a:lnum - 1 != parlnum - if closing_paren && closing_paren_pos > scol - return indent(parlnum) - else - let lastindent = match(getline(a:lnum - 1), '\S') - if lastindent != -1 && lastindent < parcol - return lastindent + + let line = getline(a:lnum) + let prevline = getline(a:lnum-1) + + " Multilinestrings: continous, docstring or starting. + if s:is_python_string(a:lnum-1, len(prevline)) + \ && (s:is_python_string(a:lnum, 1) + \ || match(line, '^\%("""\|''''''\)') != -1) + + " Indent closing quotes as the line with the opening ones. + let match_quotes = match(line, '^\s*\zs\%("""\|''''''\)') + if match_quotes != -1 + " closing multiline string + let quotes = line[match_quotes:(match_quotes+2)] + let pairpos = searchpairpos(quotes, '', quotes, 'b') + if pairpos[0] != 0 + return indent(pairpos[0]) + else + " TODO: test to cover this! + endif endif - endif + + if s:is_python_string(a:lnum-1) + " Previous line is (completely) a string. + return indent(a:lnum-1) + endif + + if match(prevline, '^\s*\%("""\|''''''\)') != -1 + " docstring. + return indent(a:lnum-1) + endif + + let indent_multi = get(b:, 'python_pep8_indent_multiline_string', + \ get(g:, 'python_pep8_indent_multiline_string', 0)) + if match(prevline, '\v%("""|'''''')$') != -1 + " Opening multiline string, started in previous line. + if (&autoindent && indent(a:lnum) == indent(a:lnum-1)) + \ || match(line, '\v^\s+$') != -1 + " with empty line or to split up 'foo("""bar' into + " 'foo("""' and 'bar'. + if indent_multi == -2 + return indent(a:lnum-1) + s:sw() + endif + return indent_multi + endif + endif + + " Keep existing indent. + if match(line, '\v^\s*\S') != -1 + return -1 + endif + + if indent_multi != -2 + return indent_multi + endif + + return s:indent_like_opening_paren(a:lnum) endif - " If we line up with an opening column there is a special case - " we want to handle: a docstring as argument. In that case we - " don't want to line up with the paren but with the statement - " imagine foo(doc=""" as example - echo getline(parlnum) - if match(getline(parlnum), '\("""\|' . "'''" . '\)\s*$') != -1 - return indent(parlnum) + " Parens: If we can find an open parenthesis/bracket/brace, line up with it. + let indent = s:indent_like_opening_paren(a:lnum) + if indent >= -1 + return indent endif - return parcol - endif - - " Examine this line - let thisline = getline(a:lnum) - let thisindent = indent(a:lnum) - - " If the line starts with 'elif' or 'else', line up with 'if' or 'elif' - if thisline =~ '^\s*\(elif\|else\)\>' - let bslnum = s:BlockStarter(a:lnum, '^\s*\(if\|elif\)\>') - if bslnum > 0 - return indent(bslnum) - else - return -1 + " Blocks: Match indent of first block of this type. + let indent = s:indent_like_block(a:lnum) + if indent >= -1 + return indent endif - endif - " If the line starts with 'except' or 'finally', line up with 'try' - " or 'except' - if thisline =~ '^\s*\(except\|finally\)\>' - let bslnum = s:BlockStarter(a:lnum, '^\s*\(try\|except\)\>') - if bslnum > 0 - return indent(bslnum) - else - return -1 - endif - endif - - " Examine previous line - let plnum = a:lnum - 1 - let pline = getline(plnum) - let sslnum = s:StatementStart(plnum) - - " If the previous line is blank, keep the same indentation - if pline =~ '^\s*$' - return -1 - endif - - " If this line is explicitly joined, try to find an indentation that looks - " good. - if pline =~ '\\$' - let compound_statement = '^\s*\(if\|while\|from\|import\|for\s.*\sin\|except\)\s*' - let maybe_indent = matchend(getline(sslnum), compound_statement) - if maybe_indent != -1 - return maybe_indent - else - return indent(sslnum) + &sw * 2 - endif - endif - - " If the previous line ended with a colon and is not a comment, indent - " relative to statement start. - if pline =~ '^[^#]*:\s*\(#.*\)\?$' - return indent(sslnum) + &sw - endif - - " If the previous line was a stop-execution statement or a pass - if getline(sslnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>' - " See if the user has already dedented - if indent(a:lnum) > indent(sslnum) - &sw - " If not, recommend one dedent - return indent(sslnum) - &sw - endif - " Otherwise, trust the user - return -1 - endif - - " In all other cases, line up with the start of the previous statement. - return indent(sslnum) + return s:indent_like_previous_line(a:lnum) endfunction endif diff --git a/syntax/python.vim b/syntax/python.vim index 55f20a3..e3ac9ca 100644 --- a/syntax/python.vim +++ b/syntax/python.vim @@ -1,326 +1,487 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'python') == -1 -" Vim syntax file -" -" Based on python.vim (from Vim 6.1 distribution) -" by Neil Schemenauer -" -" Notes Armin: -" -" This version of the syntax file works better for 2.x and 3.x without -" having to switch modes. -" -" Thanks: -" -" Jeroen Ruigrok van der Werven -" for the idea of highlighting for erroneous operators -" Pedro Algarvio -" for the patch to enable spell checking only for the right spots -" (strings and comments) - -" -" Options: -" -" For set option do: let OPTION_NAME = 1 -" For clear option do: let OPTION_NAME = 0 -" -" Option names: -" -" For highlight builtin functions: -" python_highlight_builtins -" -" For highlight standard exceptions: -" python_highlight_exceptions -" -" For highlight string formatting: -" python_highlight_string_formatting -" -" For highlight indentation errors: -" python_highlight_indent_errors -" -" For highlight trailing spaces: -" python_highlight_space_errors -" -" For highlight doc-tests: -" python_highlight_doctests -" -" If you want all possible Python highlighting: -" (This option not override previously set options) -" python_highlight_all -" -" For fast machines: -" python_slow_sync -" - " For version 5.x: Clear all syntax items -" For version 6.x: Quit when a syntax file was already loaded -if version < 600 +" For versions greater than 6.x: Quit when a syntax file was already loaded +if v:version < 600 syntax clear -elseif exists("b:current_syntax") +elseif exists('b:current_syntax') finish endif -if exists("python_highlight_all") && python_highlight_all != 0 - " Not override previously set options - if !exists("python_highlight_builtins") - let python_highlight_builtins = 1 +" +" Commands +" +command! -buffer Python2Syntax let b:python_version_2 = 1 | let &syntax=&syntax +command! -buffer Python3Syntax let b:python_version_2 = 0 | let &syntax=&syntax + +" Enable option if it's not defined +function! s:EnableByDefault(name) + if !exists(a:name) + let {a:name} = 1 endif - if !exists("python_highlight_exceptions") - let python_highlight_exceptions = 1 +endfunction + +" Check if option is enabled +function! s:Enabled(name) + return exists(a:name) && {a:name} +endfunction + +" Is it Python 2 syntax? +function! s:Python2Syntax() + if exists('b:python_version_2') + return b:python_version_2 endif - if !exists("python_highlight_string_formatting") - let python_highlight_string_formatting = 1 - endif - if !exists("python_highlight_indent_errors") - let python_highlight_indent_errors = 1 - endif - if !exists("python_highlight_space_errors") - let python_highlight_space_errors = 1 - endif - if !exists("python_highlight_doctests") - let python_highlight_doctests = 1 + return s:Enabled('g:python_version_2') +endfunction + +" +" Default options +" + +call s:EnableByDefault('g:python_slow_sync') +call s:EnableByDefault('g:python_highlight_builtin_funcs_kwarg') + +if s:Enabled('g:python_highlight_all') + call s:EnableByDefault('g:python_highlight_builtins') + if s:Enabled('g:python_highlight_builtins') + call s:EnableByDefault('g:python_highlight_builtin_objs') + call s:EnableByDefault('g:python_highlight_builtin_funcs') endif + call s:EnableByDefault('g:python_highlight_exceptions') + call s:EnableByDefault('g:python_highlight_string_formatting') + call s:EnableByDefault('g:python_highlight_string_format') + call s:EnableByDefault('g:python_highlight_string_templates') + call s:EnableByDefault('g:python_highlight_indent_errors') + call s:EnableByDefault('g:python_highlight_space_errors') + call s:EnableByDefault('g:python_highlight_doctests') + call s:EnableByDefault('g:python_print_as_function') + call s:EnableByDefault('g:python_highlight_class_vars') + call s:EnableByDefault('g:python_highlight_operators') endif +" " Keywords -syn keyword pythonStatement break continue del -syn keyword pythonStatement exec return -syn keyword pythonStatement pass raise -syn keyword pythonStatement global assert -syn keyword pythonStatement lambda yield -syn keyword pythonStatement async await -syn keyword pythonStatement with nonlocal True False None -syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite -syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" display contained -syn keyword pythonRepeat for while -syn keyword pythonConditional if elif else -syn keyword pythonImport import from as -syn keyword pythonException try except finally -syn keyword pythonOperator and in is not or +" -" Print keyword but only if not used as function -syn match pythonStatement "\\((\|,\|*=\)\@!" display +syn keyword pythonStatement break continue del return pass yield global assert lambda with +syn keyword pythonStatement raise nextgroup=pythonExClass skipwhite +syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite +if s:Enabled('g:python_highlight_class_vars') + syn keyword pythonClassVar self cls +endif +syn keyword pythonRepeat for while +syn keyword pythonConditional if elif else +syn keyword pythonException try except finally +" The standard pyrex.vim unconditionally removes the pythonInclude group, so +" we provide a dummy group here to avoid crashing pyrex.vim. +syn keyword pythonInclude import +syn keyword pythonImport import +syn match pythonRaiseFromStatement '\' +syn match pythonImport '^\s*\zsfrom\>' + +if s:Python2Syntax() + if !s:Enabled('g:python_print_as_function') + syn keyword pythonStatement print + endif + syn keyword pythonStatement exec + syn keyword pythonImport as + syn match pythonFunction '[a-zA-Z_][a-zA-Z0-9_]*' display contained +else + syn keyword pythonStatement as nonlocal + syn match pythonStatement '\v\.@' + syn match pythonFunction '\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*' display contained + syn match pythonStatement '\' nextgroup=pythonFunction skipwhite + syn match pythonStatement '\' + syn match pythonStatement '\' + syn cluster pythonExpression contains=pythonStatement,pythonRepeat,pythonConditional,pythonOperator,pythonNumber,pythonHexNumber,pythonOctNumber,pythonBinNumber,pythonFloat,pythonString,pythonBytes,pythonBoolean,pythonBuiltinObj,pythonBuiltinFunc +endif + + +" +" Operators +" +syn keyword pythonOperator and in is not or +if s:Enabled('g:python_highlight_operators') + syn match pythonOperator '\V=\|-\|+\|*\|@\|/\|%\|&\||\|^\|~\|<\|>\|!=' +endif +syn match pythonError '[$?]\|\([-+@%&|^~]\)\1\{1,}\|\([=*/<>]\)\2\{2,}\|\([+@/%&|^~<>]\)\3\@![-+*@/%&|^~<>]\|\*\*[*@/%&|^<>]\|=[*@/%&|^<>]\|-[+*@/%&|^~<]\|[]\+=\{2,}\|!\{2,}=\+' display + +" " Decorators (new in Python 2.4) -syn match pythonDecorator "@" display nextgroup=pythonFunction skipwhite +" +syn match pythonDecorator '^\s*\zs@' display nextgroup=pythonDottedName skipwhite +if s:Python2Syntax() + syn match pythonDottedName '[a-zA-Z_][a-zA-Z0-9_]*\%(\.[a-zA-Z_][a-zA-Z0-9_]*\)*' display contained +else + syn match pythonDottedName '\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\)*' display contained +endif +syn match pythonDot '\.' display containedin=pythonDottedName + +" " Comments -syn match pythonComment "#.*$" display contains=pythonTodo,@Spell -syn match pythonRun "\%^#!.*$" -syn match pythonCoding "\%^.*\(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$" -syn keyword pythonTodo TODO FIXME XXX contained +" +syn match pythonComment '#.*$' display contains=pythonTodo,@Spell +if !s:Enabled('g:python_highlight_file_headers_as_comments') + syn match pythonRun '\%^#!.*$' + syn match pythonCoding '\%^.*\%(\n.*\)\?#.*coding[:=]\s*[0-9A-Za-z-_.]\+.*$' +endif +syn keyword pythonTodo TODO FIXME XXX contained + +" " Errors -syn match pythonError "\<\d\+\D\+\>" display -syn match pythonError "[$?]" display -syn match pythonError "[-+&|]\{2,}" display -syn match pythonError "[=]\{3,}" display +" -" TODO: Mixing spaces and tabs also may be used for pretty formatting multiline -" statements. For now I don't know how to work around this. -if exists("python_highlight_indent_errors") && python_highlight_indent_errors != 0 - syn match pythonIndentError "^\s*\( \t\|\t \)\s*\S"me=e-1 display +syn match pythonError '\<\d\+[^0-9[:space:]]\+\>' display + +" Mixing spaces and tabs also may be used for pretty formatting multiline +" statements +if s:Enabled('g:python_highlight_indent_errors') + syn match pythonIndentError '^\s*\%( \t\|\t \)\s*\S'me=e-1 display endif " Trailing space errors -if exists("python_highlight_space_errors") && python_highlight_space_errors != 0 - syn match pythonSpaceError "\s\+$" display +if s:Enabled('g:python_highlight_space_errors') + syn match pythonSpaceError '\s\+$' display endif +" " Strings -syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell -syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonEscapeError,@Spell -syn region pythonString start=+"""+ end=+"""+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest2,pythonSpaceError,@Spell -syn region pythonString start=+'''+ end=+'''+ keepend contains=pythonEscape,pythonEscapeError,pythonDocTest,pythonSpaceError,@Spell +" -syn match pythonEscape +\\[abfnrtv'"\\]+ display contained -syn match pythonEscape "\\\o\o\=\o\=" display contained -syn match pythonEscapeError "\\\o\{,2}[89]" display contained -syn match pythonEscape "\\x\x\{2}" display contained -syn match pythonEscapeError "\\x\x\=\X" display contained -syn match pythonEscape "\\$" +if s:Python2Syntax() + " Python 2 strings + syn region pythonString start=+[bB]\='+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell + syn region pythonString start=+[bB]\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell + syn region pythonString start=+[bB]\="""+ skip=+\\"+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell + syn region pythonString start=+[bB]\='''+ skip=+\\'+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell +else + " Python 3 byte strings + syn region pythonBytes start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell + syn region pythonBytes start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesError,pythonBytesContent,@Spell + syn region pythonBytes start=+[bB]'''+ skip=+\\'+ end=+'''+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest,pythonSpaceError,@Spell + syn region pythonBytes start=+[bB]"""+ skip=+\\"+ end=+"""+ keepend contains=pythonBytesError,pythonBytesContent,pythonDocTest2,pythonSpaceError,@Spell -" Byte-Strings -syn region pythonBString start=+[bB]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBEscape,pythonBEscapeError,@Spell -syn region pythonBString start=+[bB]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBEscape,pythonBEscapeError,@Spell -syn region pythonBString start=+[bB]"""+ end=+"""+ keepend contains=pythonBEscape,pythonBEscapeError,pythonDocTest2,pythonSpaceError,@Spell -syn region pythonBString start=+[bB]'''+ end=+'''+ keepend contains=pythonBEscape,pythonBEscapeError,pythonDocTest,pythonSpaceError,@Spell - -syn match pythonBEscape +\\[abfnrtv'"\\]+ display contained -syn match pythonBEscape "\\\o\o\=\o\=" display contained -syn match pythonBEscapeError "\\\o\{,2}[89]" display contained -syn match pythonBEscape "\\x\x\{2}" display contained -syn match pythonBEscapeError "\\x\x\=\X" display contained -syn match pythonBEscape "\\$" - -" Unicode strings -syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell -syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,@Spell -syn region pythonUniString start=+[uU]"""+ end=+"""+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell -syn region pythonUniString start=+[uU]'''+ end=+'''+ keepend contains=pythonEscape,pythonUniEscape,pythonEscapeError,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell - -syn match pythonUniEscape "\\u\x\{4}" display contained -syn match pythonUniEscapeError "\\u\x\{,3}\X" display contained -syn match pythonUniEscape "\\U\x\{8}" display contained -syn match pythonUniEscapeError "\\U\x\{,7}\X" display contained -syn match pythonUniEscape "\\N{[A-Z ]\+}" display contained -syn match pythonUniEscapeError "\\N{[^A-Z ]\+}" display contained - -" Raw strings -syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell -syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell -syn region pythonRawString start=+[rR]"""+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell -syn region pythonRawString start=+[rR]'''+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell - -syn match pythonRawEscape +\\['"]+ display transparent contained - -" Unicode raw strings -syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell -syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell -syn region pythonUniRawString start=+[uU][rR]"""+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell -syn region pythonUniRawString start=+[uU][rR]'''+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell - -syn match pythonUniRawEscape "\([^\\]\(\\\\\)*\)\@<=\\u\x\{4}" display contained -syn match pythonUniRawEscapeError "\([^\\]\(\\\\\)*\)\@<=\\u\x\{,3}\X" display contained - -if exists("python_highlight_string_formatting") && python_highlight_string_formatting != 0 - " String formatting - syn match pythonStrFormat "%\(([^)]\+)\)\=[-#0 +]*\d*\(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonBString,pythonUniString,pythonRawString,pythonUniRawString - syn match pythonStrFormat "%[-#0 +]*\(\*\|\d\+\)\=\(\.\(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]" contained containedin=pythonString,pythonBString,pythonUniString,pythonRawString,pythonUniRawString + syn match pythonBytesError '.\+' display contained + syn match pythonBytesContent '[\u0000-\u00ff]\+' display contained contains=pythonBytesEscape,pythonBytesEscapeError endif -if exists("python_highlight_doctests") && python_highlight_doctests != 0 +syn match pythonBytesEscape +\\[abfnrtv'"\\]+ display contained +syn match pythonBytesEscape '\\\o\o\=\o\=' display contained +syn match pythonBytesEscapeError '\\\o\{,2}[89]' display contained +syn match pythonBytesEscape '\\x\x\{2}' display contained +syn match pythonBytesEscapeError '\\x\x\=\X' display contained +syn match pythonBytesEscape '\\$' + +syn match pythonUniEscape '\\u\x\{4}' display contained +syn match pythonUniEscapeError '\\u\x\{,3}\X' display contained +syn match pythonUniEscape '\\U\x\{8}' display contained +syn match pythonUniEscapeError '\\U\x\{,7}\X' display contained +syn match pythonUniEscape '\\N{[A-Z ]\+}' display contained +syn match pythonUniEscapeError '\\N{[^A-Z ]\+}' display contained + +if s:Python2Syntax() + " Python 2 Unicode strings + syn region pythonUniString start=+[uU]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell + syn region pythonUniString start=+[uU]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell + syn region pythonUniString start=+[uU]'''+ skip=+\\'+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell + syn region pythonUniString start=+[uU]"""+ skip=+\\"+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell +else + " Python 3 strings + syn region pythonString start=+'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell + syn region pythonString start=+"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell + syn region pythonString start=+'''+ skip=+\\'+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell + syn region pythonString start=+"""+ skip=+\\"+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell + + syn region pythonFString start=+[fF]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell + syn region pythonFString start=+[fF]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,@Spell + syn region pythonFString start=+[fF]'''+ skip=+\\'+ end=+'''+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest,pythonSpaceError,@Spell + syn region pythonFString start=+[fF]"""+ skip=+\\"+ end=+"""+ keepend contains=pythonBytesEscape,pythonBytesEscapeError,pythonUniEscape,pythonUniEscapeError,pythonDocTest2,pythonSpaceError,@Spell +endif + +if s:Python2Syntax() + " Python 2 Unicode raw strings + syn region pythonUniRawString start=+[uU][rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell + syn region pythonUniRawString start=+[uU][rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,pythonUniRawEscape,pythonUniRawEscapeError,@Spell + syn region pythonUniRawString start=+[uU][rR]'''+ skip=+\\'+ end=+'''+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest,pythonSpaceError,@Spell + syn region pythonUniRawString start=+[uU][rR]"""+ skip=+\\"+ end=+"""+ keepend contains=pythonUniRawEscape,pythonUniRawEscapeError,pythonDocTest2,pythonSpaceError,@Spell + + syn match pythonUniRawEscape '\%([^\\]\%(\\\\\)*\)\@<=\\u\x\{4}' display contained + syn match pythonUniRawEscapeError '\%([^\\]\%(\\\\\)*\)\@<=\\u\x\{,3}\X' display contained +endif + +" Python 2/3 raw strings +if s:Python2Syntax() + syn region pythonRawString start=+[bB]\=[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell + syn region pythonRawString start=+[bB]\=[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell + syn region pythonRawString start=+[bB]\=[rR]'''+ skip=+\\'+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell + syn region pythonRawString start=+[bB]\=[rR]"""+ skip=+\\"+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell +else + syn region pythonRawString start=+[rR]'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell + syn region pythonRawString start=+[rR]"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell + syn region pythonRawString start=+[rR]'''+ skip=+\\'+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell + syn region pythonRawString start=+[rR]"""+ skip=+\\"+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell + + syn region pythonRawFString start=+\%([fF][rR]\|[rR][fF]\)'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell + syn region pythonRawFString start=+\%([fF][rR]\|[rR][fF]\)"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell + syn region pythonRawFString start=+\%([fF][rR]\|[rR][fF]\)'''+ skip=+\\'+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell + syn region pythonRawFString start=+\%([fF][rR]\|[rR][fF]\)"""+ skip=+\\"+ end=+"""+ keepend contains=pythonDocTest,pythonSpaceError,@Spell + + syn region pythonRawBytes start=+\%([bB][rR]\|[rR][bB]\)'+ skip=+\\\\\|\\'\|\\$+ excludenl end=+'+ end=+$+ keepend contains=pythonRawEscape,@Spell + syn region pythonRawBytes start=+\%([bB][rR]\|[rR][bB]\)"+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"+ end=+$+ keepend contains=pythonRawEscape,@Spell + syn region pythonRawBytes start=+\%([bB][rR]\|[rR][bB]\)'''+ skip=+\\'+ end=+'''+ keepend contains=pythonDocTest,pythonSpaceError,@Spell + syn region pythonRawBytes start=+\%([bB][rR]\|[rR][bB]\)"""+ skip=+\\"+ end=+"""+ keepend contains=pythonDocTest2,pythonSpaceError,@Spell +endif + +syn match pythonRawEscape +\\['"]+ display contained + +if s:Enabled('g:python_highlight_string_formatting') + " % operator string formatting + if s:Python2Syntax() + syn match pythonStrFormatting '%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]' contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString,pythonBytesContent + syn match pythonStrFormatting '%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]' contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString,pythonBytesContent + else + syn match pythonStrFormatting '%\%(([^)]\+)\)\=[-#0 +]*\d*\%(\.\d\+\)\=[hlL]\=[diouxXeEfFgGcrs%]' contained containedin=pythonString,pythonRawString,pythonBytesContent + syn match pythonStrFormatting '%[-#0 +]*\%(\*\|\d\+\)\=\%(\.\%(\*\|\d\+\)\)\=[hlL]\=[diouxXeEfFgGcrs%]' contained containedin=pythonString,pythonRawString,pythonBytesContent + endif +endif + +if s:Enabled('g:python_highlight_string_format') + " str.format syntax + if s:Python2Syntax() + syn match pythonStrFormat '{{\|}}' contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString + 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 + else + 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 + syn region pythonStrInterpRegion start="{"he=e+1,rs=e+1 end="\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}"hs=s-1,re=s-1 extend contained containedin=pythonFString,pythonRawFString contains=pythonStrInterpRegion,@pythonExpression + syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString,pythonFString,pythonRawFString + endif +endif + +if s:Enabled('g:python_highlight_string_templates') + " string.Template format + if s:Python2Syntax() + syn match pythonStrTemplate '\$\$' contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString + syn match pythonStrTemplate '\${[a-zA-Z_][a-zA-Z0-9_]*}' contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString + syn match pythonStrTemplate '\$[a-zA-Z_][a-zA-Z0-9_]*' contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString + else + syn match pythonStrTemplate '\$\$' contained containedin=pythonString,pythonRawString + syn match pythonStrTemplate '\${[a-zA-Z_][a-zA-Z0-9_]*}' contained containedin=pythonString,pythonRawString + syn match pythonStrTemplate '\$[a-zA-Z_][a-zA-Z0-9_]*' contained containedin=pythonString,pythonRawString + endif +endif + +if s:Enabled('g:python_highlight_doctests') " DocTests - syn region pythonDocTest start="^\s*>>>" end=+'''+he=s-1 end="^\s*$" contained - syn region pythonDocTest2 start="^\s*>>>" end=+"""+he=s-1 end="^\s*$" contained + syn region pythonDocTest start='^\s*>>>' skip=+\\'+ end=+'''+he=s-1 end='^\s*$' contained + syn region pythonDocTest2 start='^\s*>>>' skip=+\\"+ end=+"""+he=s-1 end='^\s*$' contained endif +" " Numbers (ints, longs, floats, complex) -syn match pythonHexNumber "\<0[xX]\x\+[lL]\=\>" display -syn match pythonHexNumber "\<0[xX]\>" display -syn match pythonNumber "\<\d\+[lLjJ]\=\>" display -syn match pythonFloat "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>" display -syn match pythonFloat "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" display -syn match pythonFloat "\<\d\+\.\d*\([eE][+-]\=\d\+\)\=[jJ]\=" display +" -syn match pythonOctalError "\<0\o*[89]\d*[lL]\=\>" display -syn match pythonHexError "\<0[xX]\X\+[lL]\=\>" display +if s:Python2Syntax() + syn match pythonHexError '\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>' display + syn match pythonOctError '\<0[oO]\=\o*\D\+\d*[lL]\=\>' display + syn match pythonBinError '\<0[bB][01]*\D\+\d*[lL]\=\>' display -if exists("python_highlight_builtins") && python_highlight_builtins != 0 - " Builtin functions, types and objects - syn keyword pythonBuiltinObj Ellipsis NotImplemented + syn match pythonHexNumber '\<0[xX]\x\+[lL]\=\>' display + syn match pythonOctNumber '\<0[oO]\o\+[lL]\=\>' display + syn match pythonBinNumber '\<0[bB][01]\+[lL]\=\>' display - syn keyword pythonBuiltinFunc __import__ abs all any apply - syn keyword pythonBuiltinFunc basestring bool buffer bytearray bytes callable - syn keyword pythonBuiltinFunc chr classmethod cmp coerce compile complex - syn keyword pythonBuiltinFunc delattr dict dir divmod enumerate eval - syn keyword pythonBuiltinFunc execfile file filter float frozenset getattr - syn keyword pythonBuiltinfunc globals hasattr hash help hex id - syn keyword pythonBuiltinFunc input int intern isinstance - syn keyword pythonBuiltinFunc issubclass iter len list locals long map max - syn keyword pythonBuiltinFunc min object oct open ord pow property range - syn keyword pythonBuiltinFunc raw_input reduce reload repr - syn keyword pythonBuiltinFunc reversed round set setattr - syn keyword pythonBuiltinFunc slice sorted staticmethod str sum super tuple - syn keyword pythonBuiltinFunc type unichr unicode vars xrange zip + syn match pythonNumberError '\<\d\+\D[lL]\=\>' display + syn match pythonNumber '\<\d[lL]\=\>' display + syn match pythonNumber '\<[0-9]\d\+[lL]\=\>' display + syn match pythonNumber '\<\d\+[lLjJ]\>' display + + syn match pythonOctError '\<0[oO]\=\o*[8-9]\d*[lL]\=\>' display + syn match pythonBinError '\<0[bB][01]*[2-9]\d*[lL]\=\>' display + + syn match pythonFloat '\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>' display + syn match pythonFloat '\<\d\+[eE][+-]\=\d\+[jJ]\=\>' display + syn match pythonFloat '\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=' display +else + syn match pythonOctError '\<0[oO]\=\o*\D\+\d*\>' display + " pythonHexError comes after pythonOctError so that 0xffffl is pythonHexError + syn match pythonHexError '\<0[xX]\x*[g-zG-Z]\x*\>' display + syn match pythonBinError '\<0[bB][01]*\D\+\d*\>' display + + syn match pythonHexNumber '\<0[xX][_0-9a-fA-F]*\x\>' display + syn match pythonOctNumber '\<0[oO][_0-7]*\o\>' display + syn match pythonBinNumber '\<0[bB][_01]*[01]\>' display + + syn match pythonNumberError '\<\d[_0-9]*\D\>' display + syn match pythonNumberError '\<0[_0-9]\+\>' display + syn match pythonNumberError '\<0_x\S*\>' display + syn match pythonNumberError '\<0[bBxXoO][_0-9a-fA-F]*_\>' display + syn match pythonNumberError '\<\d[_0-9]*_\>' display + syn match pythonNumber '\<\d\>' display + syn match pythonNumber '\<[1-9][_0-9]*\d\>' display + syn match pythonNumber '\<\d[jJ]\>' display + syn match pythonNumber '\<[1-9][_0-9]*\d[jJ]\>' display + + syn match pythonOctError '\<0[oO]\=\o*[8-9]\d*\>' display + syn match pythonBinError '\<0[bB][01]*[2-9]\d*\>' display + + syn match pythonFloat '\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=\>' display + syn match pythonFloat '\<\d\%([_0-9]*\d\)\=[eE][+-]\=\d\%([_0-9]*\d\)\=[jJ]\=\>' display + syn match pythonFloat '\<\d\%([_0-9]*\d\)\=\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=' display endif -if exists("python_highlight_exceptions") && python_highlight_exceptions != 0 - " Builtin exceptions and warnings - syn keyword pythonExClass BaseException - syn keyword pythonExClass Exception StandardError ArithmeticError - syn keyword pythonExClass LookupError EnvironmentError +" +" Builtin objects and types +" - syn keyword pythonExClass AssertionError AttributeError EOFError - syn keyword pythonExClass FloatingPointError GeneratorExit IOError - syn keyword pythonExClass ImportError IndexError KeyError - syn keyword pythonExClass KeyboardInterrupt MemoryError NameError - syn keyword pythonExClass NotImplementedError OSError OverflowError - syn keyword pythonExClass ReferenceError RuntimeError StopIteration - syn keyword pythonExClass SyntaxError IndentationError TabError - syn keyword pythonExClass SystemError SystemExit TypeError - syn keyword pythonExClass UnboundLocalError UnicodeError - syn keyword pythonExClass UnicodeEncodeError UnicodeDecodeError - syn keyword pythonExClass UnicodeTranslateError ValueError - syn keyword pythonExClass WindowsError ZeroDivisionError - - syn keyword pythonExClass Warning UserWarning DeprecationWarning - syn keyword pythonExClass PendingDepricationWarning SyntaxWarning - syn keyword pythonExClass RuntimeWarning FutureWarning OverflowWarning - syn keyword pythonExClass ImportWarning UnicodeWarning +if s:Enabled('g:python_highlight_builtin_objs') + syn keyword pythonNone None + syn keyword pythonBoolean True False + syn keyword pythonBuiltinObj Ellipsis NotImplemented + syn match pythonBuiltinObj '\v\.@' + syn keyword pythonBuiltinObj __debug__ __doc__ __file__ __name__ __package__ + syn keyword pythonBuiltinObj __loader__ __spec__ __path__ __cached__ endif -if exists("python_slow_sync") && python_slow_sync != 0 +" +" Builtin functions +" + +if s:Enabled('g:python_highlight_builtin_funcs') + let s:funcs_re = '__import__|abs|all|any|bin|callable|chr|classmethod|compile|complex|delattr|dir|divmod|enumerate|eval|filter|format|getattr|globals|hasattr|hash|help|hex|id|input|isinstance|issubclass|iter|len|locals|map|max|memoryview|min|next|oct|open|ord|pow|property|range|repr|reversed|round|setattr|slice|sorted|staticmethod|sum|super|type|vars|zip' + + if s:Python2Syntax() + let s:funcs_re .= '|apply|basestring|buffer|cmp|coerce|execfile|file|intern|long|raw_input|reduce|reload|unichr|unicode|xrange' + if s:Enabled('g:python_print_as_function') + let s:funcs_re .= '|print' + endif + else + let s:funcs_re .= '|ascii|exec|print' + endif + + let s:funcs_re = 'syn match pythonBuiltinFunc ''\v\.@' + + if !s:Enabled('g:python_highlight_builtin_funcs_kwarg') + let s:funcs_re .= '\=@!' + endif + + execute s:funcs_re . '''' + unlet s:funcs_re +endif + +" +" Builtin exceptions and warnings +" + +if s:Enabled('g:python_highlight_exceptions') + let s:exs_re = 'BaseException|Exception|ArithmeticError|LookupError|EnvironmentError|AssertionError|AttributeError|BufferError|EOFError|FloatingPointError|GeneratorExit|IOError|ImportError|IndexError|KeyError|KeyboardInterrupt|MemoryError|NameError|NotImplementedError|OSError|OverflowError|ReferenceError|RuntimeError|StopIteration|SyntaxError|IndentationError|TabError|SystemError|SystemExit|TypeError|UnboundLocalError|UnicodeError|UnicodeEncodeError|UnicodeDecodeError|UnicodeTranslateError|ValueError|VMSError|WindowsError|ZeroDivisionError|Warning|UserWarning|BytesWarning|DeprecationWarning|PendingDepricationWarning|SyntaxWarning|RuntimeWarning|FutureWarning|ImportWarning|UnicodeWarning' + + if s:Python2Syntax() + let s:exs_re .= '|StandardError' + else + let s:exs_re .= '|BlockingIOError|ChildProcessError|ConnectionError|BrokenPipeError|ConnectionAbortedError|ConnectionRefusedError|ConnectionResetError|FileExistsError|FileNotFoundError|InterruptedError|IsADirectoryError|NotADirectoryError|PermissionError|ProcessLookupError|TimeoutError|StopAsyncIteration|ResourceWarning' + endif + + execute 'syn match pythonExClass ''\v\.@''' + unlet s:exs_re +endif + +if s:Enabled('g:python_slow_sync') syn sync minlines=2000 else " This is fast but code inside triple quoted strings screws it up. It " is impossible to fix because the only way to know if you are inside a " triple quoted string is to start from the beginning of the file. - syn sync match pythonSync grouphere NONE "):$" + syn sync match pythonSync grouphere NONE '):$' syn sync maxlines=200 endif -if version >= 508 || !exists("did_python_syn_inits") - if version <= 508 +if v:version >= 508 || !exists('did_python_syn_inits') + if v:version <= 508 let did_python_syn_inits = 1 command -nargs=+ HiLink hi link else command -nargs=+ HiLink hi def link endif - HiLink pythonStatement Statement - HiLink pythonImport Statement - HiLink pythonFunction Function - HiLink pythonConditional Conditional - HiLink pythonRepeat Repeat - HiLink pythonException Exception - HiLink pythonOperator Operator + HiLink pythonStatement Statement + HiLink pythonRaiseFromStatement Statement + HiLink pythonImport Include + HiLink pythonFunction Function + HiLink pythonConditional Conditional + HiLink pythonRepeat Repeat + HiLink pythonException Exception + HiLink pythonOperator Operator - HiLink pythonDecorator Define + HiLink pythonDecorator Define + HiLink pythonDottedName Function + HiLink pythonDot Normal - HiLink pythonComment Comment - HiLink pythonCoding Special - HiLink pythonRun Special - HiLink pythonTodo Todo + HiLink pythonComment Comment + if !s:Enabled('g:python_highlight_file_headers_as_comments') + HiLink pythonCoding Special + HiLink pythonRun Special + endif + HiLink pythonTodo Todo - HiLink pythonError Error - HiLink pythonIndentError Error - HiLink pythonSpaceError Error + HiLink pythonError Error + HiLink pythonIndentError Error + HiLink pythonSpaceError Error - HiLink pythonString String - HiLink pythonBString String - HiLink pythonUniString String - HiLink pythonRawString String - HiLink pythonUniRawString String + HiLink pythonString String + HiLink pythonRawString String + HiLink pythonRawEscape Special - HiLink pythonEscape Special - HiLink pythonBEscape Special - HiLink pythonEscapeError Error - HiLink pythonBEscapeError Error - HiLink pythonUniEscape Special - HiLink pythonUniEscapeError Error - HiLink pythonUniRawEscape Special - HiLink pythonUniRawEscapeError Error + HiLink pythonUniEscape Special + HiLink pythonUniEscapeError Error - HiLink pythonStrFormat Special + if s:Python2Syntax() + HiLink pythonUniString String + HiLink pythonUniRawString String + HiLink pythonUniRawEscape Special + HiLink pythonUniRawEscapeError Error + else + HiLink pythonBytes String + HiLink pythonRawBytes String + HiLink pythonBytesContent String + HiLink pythonBytesError Error + HiLink pythonBytesEscape Special + HiLink pythonBytesEscapeError Error + HiLink pythonFString String + HiLink pythonRawFString String + HiLink pythonStrInterpRegion Special + endif - HiLink pythonDocTest Special - HiLink pythonDocTest2 Special + HiLink pythonStrFormatting Special + HiLink pythonStrFormat Special + HiLink pythonStrTemplate Special - HiLink pythonNumber Number - HiLink pythonHexNumber Number - HiLink pythonFloat Float - HiLink pythonOctalError Error - HiLink pythonHexError Error + HiLink pythonDocTest Special + HiLink pythonDocTest2 Special - HiLink pythonBuiltinObj Structure - HiLink pythonBuiltinFunc Function + HiLink pythonNumber Number + HiLink pythonHexNumber Number + HiLink pythonOctNumber Number + HiLink pythonBinNumber Number + HiLink pythonFloat Float + HiLink pythonNumberError Error + HiLink pythonOctError Error + HiLink pythonHexError Error + HiLink pythonBinError Error - HiLink pythonExClass Structure + HiLink pythonBoolean Boolean + HiLink pythonNone Constant + + HiLink pythonBuiltinObj Structure + HiLink pythonBuiltinFunc Function + + HiLink pythonExClass Structure + HiLink pythonClassVar Identifier delcommand HiLink endif -let b:current_syntax = "python" +let b:current_syntax = 'python' endif