summaryrefslogtreecommitdiff
path: root/runtime/syntax/python.vim
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/syntax/python.vim')
-rw-r--r--runtime/syntax/python.vim342
1 files changed, 228 insertions, 114 deletions
diff --git a/runtime/syntax/python.vim b/runtime/syntax/python.vim
index 5288d9bdc..d59074321 100644
--- a/runtime/syntax/python.vim
+++ b/runtime/syntax/python.vim
@@ -1,143 +1,252 @@
" Vim syntax file
" Language: Python
" Maintainer: Neil Schemenauer <nas@python.ca>
-" Updated: 2006-10-15
-" Added Python 2.4 features 2006 May 4 (Dmitry Vasiliev)
+" Last Change: 2009-10-13
+" Credits: Zvezdan Petkovic <zpetkovic@acm.org>
+" Neil Schemenauer <nas@python.ca>
+" Dmitry Vasiliev
"
-" Options to control Python syntax highlighting:
+" This version is a major rewrite by Zvezdan Petkovic.
"
-" For highlighted numbers:
+" - introduced highlighting of doctests
+" - updated keywords, built-ins, and exceptions
+" - corrected regular expressions for
"
-" let python_highlight_numbers = 1
+" * functions
+" * decorators
+" * strings
+" * escapes
+" * numbers
+" * space error
"
-" For highlighted builtin functions:
+" - corrected synchronization
+" - more highlighting is ON by default, except
+" - space error highlighting is OFF by default
"
-" let python_highlight_builtins = 1
+" Optional highlighting can be controlled using these variables.
"
-" For highlighted standard exceptions:
+" let python_no_builtin_highlight = 1
+" let python_no_doctest_code_highlight = 1
+" let python_no_doctest_highlight = 1
+" let python_no_exception_highlight = 1
+" let python_no_number_highlight = 1
+" let python_space_error_highlight = 1
"
-" let python_highlight_exceptions = 1
+" All the options above can be switched on together.
"
-" Highlight erroneous whitespace:
-"
-" let python_highlight_space_errors = 1
-"
-" If you want all possible Python highlighting (the same as setting the
-" preceding options):
-"
-" let python_highlight_all = 1
+" let python_highlight_all = 1
"
-" For version 5.x: Clear all syntax items
-" For version 6.x: Quit when a syntax file was already loaded
+" For version 5.x: Clear all syntax items.
+" For version 6.x: Quit when a syntax file was already loaded.
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
-
-syn keyword pythonStatement break continue del
-syn keyword pythonStatement except exec finally
-syn keyword pythonStatement pass print raise
-syn keyword pythonStatement return try with
-syn keyword pythonStatement global assert
-syn keyword pythonStatement lambda yield
-syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite
-syn match pythonFunction "[a-zA-Z_][a-zA-Z0-9_]*" contained
+" Keep Python keywords in alphabetical order inside groups for easy
+" comparison with the table in the 'Python Language Reference'
+" http://docs.python.org/reference/lexical_analysis.html#keywords.
+" Groups are in the order presented in NAMING CONVENTIONS in syntax.txt.
+" Exceptions come last at the end of each group (class and def below).
+"
+" Keywords 'with' and 'as' are new in Python 2.6
+" (use 'from __future__ import with_statement' in Python 2.5).
+"
+" Some compromises had to be made to support both Python 3.0 and 2.6.
+" We include Python 3.0 features, but when a definition is duplicated,
+" the last definition takes precedence.
+"
+" - 'False', 'None', and 'True' are keywords in Python 3.0 but they are
+" built-ins in 2.6 and will be highlighted as built-ins below.
+" - 'exec' is a built-in in Python 3.0 and will be highlighted as
+" built-in below.
+" - 'nonlocal' is a keyword in Python 3.0 and will be highlighted.
+" - 'print' is a built-in in Python 3.0 and will be highlighted as
+" built-in below (use 'from __future__ import print_function' in 2.6)
+"
+syn keyword pythonStatement False, None, True
+syn keyword pythonStatement as assert break continue del exec global
+syn keyword pythonStatement lambda nonlocal pass print return with yield
+syn keyword pythonStatement class def nextgroup=pythonFunction skipwhite
+syn keyword pythonConditional elif else if
syn keyword pythonRepeat for while
-syn keyword pythonConditional if elif else
syn keyword pythonOperator and in is not or
-" AS will be a keyword in Python 3
-syn keyword pythonPreCondit import from as
-syn match pythonComment "#.*$" contains=pythonTodo,@Spell
-syn keyword pythonTodo TODO FIXME XXX contained
+syn keyword pythonException except finally raise try
+syn keyword pythonInclude from import
" Decorators (new in Python 2.4)
syn match pythonDecorator "@" display nextgroup=pythonFunction skipwhite
+" The zero-length non-grouping match before the function name is
+" extremely important in pythonFunction. Without it, everything is
+" interpreted as a function inside the contained environment of
+" doctests.
+" A dot must be allowed because of @MyClass.myfunc decorators.
+syn match pythonFunction
+ \ "\%(\%(def\s\|class\s\|@\)\s*\)\@<=\h\%(\w\|\.\)*" contained
-" strings
-syn region pythonString matchgroup=Normal start=+[uU]\='+ end=+'+ skip=+\\\\\|\\'+ contains=pythonEscape,@Spell
-syn region pythonString matchgroup=Normal start=+[uU]\="+ end=+"+ skip=+\\\\\|\\"+ contains=pythonEscape,@Spell
-syn region pythonString matchgroup=Normal start=+[uU]\="""+ end=+"""+ contains=pythonEscape,@Spell
-syn region pythonString matchgroup=Normal start=+[uU]\='''+ end=+'''+ contains=pythonEscape,@Spell
-syn region pythonRawString matchgroup=Normal start=+[uU]\=[rR]'+ end=+'+ skip=+\\\\\|\\'+ contains=@Spell
-syn region pythonRawString matchgroup=Normal start=+[uU]\=[rR]"+ end=+"+ skip=+\\\\\|\\"+ contains=@Spell
-syn region pythonRawString matchgroup=Normal start=+[uU]\=[rR]"""+ end=+"""+ contains=@Spell
-syn region pythonRawString matchgroup=Normal start=+[uU]\=[rR]'''+ end=+'''+ contains=@Spell
-syn match pythonEscape +\\[abfnrtv'"\\]+ contained
-syn match pythonEscape "\\\o\{1,3}" contained
-syn match pythonEscape "\\x\x\{2}" contained
-syn match pythonEscape "\(\\u\x\{4}\|\\U\x\{8}\)" contained
-syn match pythonEscape "\\$"
+syn match pythonComment "#.*$" contains=pythonTodo,@Spell
+syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained
+
+" Triple-quoted strings can contain doctests.
+syn region pythonString
+ \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
+ \ contains=pythonEscape,@Spell
+syn region pythonString
+ \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
+ \ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
+syn region pythonRawString
+ \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
+ \ contains=@Spell
+syn region pythonRawString
+ \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
+ \ contains=pythonSpaceError,pythonDoctest,@Spell
+
+syn match pythonEscape +\\[abfnrtv'"\\]+ contained
+syn match pythonEscape "\\\o\{1,3}" contained
+syn match pythonEscape "\\x\x\{2}" contained
+syn match pythonEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
+" Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
+syn match pythonEscape "\\N{\a\+\%(\s\a\+\)*}" contained
+syn match pythonEscape "\\$"
if exists("python_highlight_all")
- let python_highlight_numbers = 1
- let python_highlight_builtins = 1
- let python_highlight_exceptions = 1
- let python_highlight_space_errors = 1
+ if exists("python_no_builtin_highlight")
+ unlet python_no_builtin_highlight
+ endif
+ if exists("python_no_doctest_code_highlight")
+ unlet python_no_doctest_code_highlight
+ endif
+ if exists("python_no_doctest_highlight")
+ unlet python_no_doctest_highlight
+ endif
+ if exists("python_no_exception_highlight")
+ unlet python_no_exception_highlight
+ endif
+ if exists("python_no_number_highlight")
+ unlet python_no_number_highlight
+ endif
+ let python_space_error_highlight = 1
endif
-if exists("python_highlight_numbers")
+" It is very important to understand all details before changing the
+" regular expressions below or their order.
+" The word boundaries are *not* the floating-point number boundaries
+" because of a possible leading or trailing decimal point.
+" The expressions below ensure that all valid number literals are
+" highlighted, and invalid number literals are not. For example,
+"
+" - a decimal point in '4.' at the end of a line is highlighted,
+" - a second dot in 1.0.0 is not highlighted,
+" - 08 is not highlighted,
+" - 08e0 or 08j are highlighted,
+"
+" and so on, as specified in the 'Python Language Reference'.
+" http://docs.python.org/reference/lexical_analysis.html#numeric-literals
+if !exists("python_no_number_highlight")
" numbers (including longs and complex)
- syn match pythonNumber "\<0x\x\+[Ll]\=\>"
- syn match pythonNumber "\<\d\+[LljJ]\=\>"
- syn match pythonNumber "\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"
- syn match pythonNumber "\<\d\+\.\([eE][+-]\=\d\+\)\=[jJ]\=\>"
- syn match pythonNumber "\<\d\+\.\d\+\([eE][+-]\=\d\+\)\=[jJ]\=\>"
+ syn match pythonNumber "\<0[oO]\=\o\+[Ll]\=\>"
+ syn match pythonNumber "\<0[xX]\x\+[Ll]\=\>"
+ syn match pythonNumber "\<0[bB][01]\+[Ll]\=\>"
+ syn match pythonNumber "\<\%([1-9]\d*\|0\)[Ll]\=\>"
+ syn match pythonNumber "\<\d\+[jJ]\>"
+ syn match pythonNumber "\<\d\+[eE][+-]\=\d\+[jJ]\=\>"
+ syn match pythonNumber
+ \ "\<\d\+\.\%([eE][+-]\=\d\+\)\=[jJ]\=\%(\W\|$\)\@="
+ syn match pythonNumber
+ \ "\%(^\|\W\)\@<=\d*\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>"
endif
-if exists("python_highlight_builtins")
- " builtin functions, types and objects, not really part of the syntax
- syn keyword pythonBuiltin True False bool enumerate set frozenset help
- syn keyword pythonBuiltin reversed sorted sum
- syn keyword pythonBuiltin Ellipsis None NotImplemented __import__ abs
- syn keyword pythonBuiltin apply buffer callable chr classmethod cmp
- syn keyword pythonBuiltin coerce compile complex delattr dict dir divmod
- syn keyword pythonBuiltin eval execfile file filter float getattr globals
- syn keyword pythonBuiltin hasattr hash hex id input int intern isinstance
- syn keyword pythonBuiltin issubclass iter len list locals long map max
- syn keyword pythonBuiltin min object oct open ord pow property range
- syn keyword pythonBuiltin raw_input reduce reload repr round setattr
- syn keyword pythonBuiltin slice staticmethod str super tuple type unichr
- syn keyword pythonBuiltin unicode vars xrange zip
+" Group the built-ins in the order in the 'Python Library Reference' for
+" easier comparison.
+" http://docs.python.org/library/constants.html
+" http://docs.python.org/library/functions.html
+" http://docs.python.org/library/functions.html#non-essential-built-in-functions
+" Python built-in functions are in alphabetical order.
+if !exists("python_no_builtin_highlight")
+ " built-in constants
+ " 'False', 'True', and 'None' are also reserved words in Python 3.0
+ syn keyword pythonBuiltin False True None
+ syn keyword pythonBuiltin NotImplemented Ellipsis __debug__
+ " built-in functions
+ syn keyword pythonBuiltin abs all any bin bool chr classmethod
+ syn keyword pythonBuiltin compile complex delattr dict dir divmod
+ syn keyword pythonBuiltin enumerate eval filter float format
+ syn keyword pythonBuiltin frozenset getattr globals hasattr hash
+ syn keyword pythonBuiltin help hex id input int isinstance
+ syn keyword pythonBuiltin issubclass iter len list locals map max
+ syn keyword pythonBuiltin min next object oct open ord pow print
+ syn keyword pythonBuiltin property range repr reversed round set
+ syn keyword pythonBuiltin setattr slice sorted staticmethod str
+ syn keyword pythonBuiltin sum super tuple type vars zip __import__
+ " Python 2.6 only
+ syn keyword pythonBuiltin basestring callable cmp execfile file
+ syn keyword pythonBuiltin long raw_input reduce reload unichr
+ syn keyword pythonBuiltin unicode xrange
+ " Python 3.0 only
+ syn keyword pythonBuiltin ascii bytearray bytes exec memoryview
+ " non-essential built-in functions; Python 2.6 only
+ syn keyword pythonBuiltin apply buffer coerce intern
endif
-if exists("python_highlight_exceptions")
- " builtin exceptions and warnings
- syn keyword pythonException ArithmeticError AssertionError AttributeError
- syn keyword pythonException DeprecationWarning EOFError EnvironmentError
- syn keyword pythonException Exception FloatingPointError IOError
- syn keyword pythonException ImportError IndentationError IndexError
- syn keyword pythonException KeyError KeyboardInterrupt LookupError
- syn keyword pythonException MemoryError NameError NotImplementedError
- syn keyword pythonException OSError OverflowError OverflowWarning
- syn keyword pythonException ReferenceError RuntimeError RuntimeWarning
- syn keyword pythonException StandardError StopIteration SyntaxError
- syn keyword pythonException SyntaxWarning SystemError SystemExit TabError
- syn keyword pythonException TypeError UnboundLocalError UnicodeError
- syn keyword pythonException UnicodeEncodeError UnicodeDecodeError
- syn keyword pythonException UnicodeTranslateError
- syn keyword pythonException UserWarning ValueError Warning WindowsError
- syn keyword pythonException ZeroDivisionError
+" From the 'Python Library Reference' class hierarchy at the bottom.
+" http://docs.python.org/library/exceptions.html
+if !exists("python_no_exception_highlight")
+ " builtin base exceptions (only used as base classes for other exceptions)
+ syn keyword pythonExceptions BaseException Exception
+ syn keyword pythonExceptions ArithmeticError EnvironmentError
+ syn keyword pythonExceptions LookupError
+ " builtin base exception removed in Python 3.0
+ syn keyword pythonExceptions StandardError
+ " builtin exceptions (actually raised)
+ syn keyword pythonExceptions AssertionError AttributeError BufferError
+ syn keyword pythonExceptions EOFError FloatingPointError GeneratorExit
+ syn keyword pythonExceptions IOError ImportError IndentationError
+ syn keyword pythonExceptions IndexError KeyError KeyboardInterrupt
+ syn keyword pythonExceptions MemoryError NameError NotImplementedError
+ syn keyword pythonExceptions OSError OverflowError ReferenceError
+ syn keyword pythonExceptions RuntimeError StopIteration SyntaxError
+ syn keyword pythonExceptions SystemError SystemExit TabError TypeError
+ syn keyword pythonExceptions UnboundLocalError UnicodeError
+ syn keyword pythonExceptions UnicodeDecodeError UnicodeEncodeError
+ syn keyword pythonExceptions UnicodeTranslateError ValueError VMSError
+ syn keyword pythonExceptions WindowsError ZeroDivisionError
+ " builtin warnings
+ syn keyword pythonExceptions BytesWarning DeprecationWarning FutureWarning
+ syn keyword pythonExceptions ImportWarning PendingDeprecationWarning
+ syn keyword pythonExceptions RuntimeWarning SyntaxWarning UnicodeWarning
+ syn keyword pythonExceptions UserWarning Warning
endif
-if exists("python_highlight_space_errors")
+if exists("python_space_error_highlight")
" trailing whitespace
- syn match pythonSpaceError display excludenl "\S\s\+$"ms=s+1
+ syn match pythonSpaceError display excludenl "\s\+$"
" mixed tabs and spaces
- syn match pythonSpaceError display " \+\t"
- syn match pythonSpaceError display "\t\+ "
+ syn match pythonSpaceError display " \+\t"
+ syn match pythonSpaceError display "\t\+ "
endif
-" 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. If
-" you have a fast machine you can try uncommenting the "sync minlines"
-" and commenting out the rest.
-syn sync match pythonSync grouphere NONE "):$"
-syn sync maxlines=200
-"syn sync minlines=2000
+" Do not spell doctests inside strings.
+" Notice that the end of a string, either ''', or """, will end the contained
+" doctest too. Thus, we do *not* need to have it as an end pattern.
+if !exists("python_no_doctest_highlight")
+ if !exists("python_no_doctest_code_higlight")
+ syn region pythonDoctest
+ \ start="^\s*>>>\s" end="^\s*$"
+ \ contained contains=ALLBUT,pythonDoctest,@Spell
+ syn region pythonDoctestValue
+ \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$"
+ \ contained
+ else
+ syn region pythonDoctest
+ \ start="^\s*>>>" end="^\s*$"
+ \ contained contains=@NoSpell
+ endif
+endif
+
+" Sync at the beginning of class, function, or method definition.
+syn sync match pythonSync grouphere NONE "^\s*\%(def\|class\)\s\+\h\w*\s*("
if version >= 508 || !exists("did_python_syn_inits")
if version <= 508
@@ -147,35 +256,40 @@ if version >= 508 || !exists("did_python_syn_inits")
command -nargs=+ HiLink hi def link <args>
endif
- " The default methods for highlighting. Can be overridden later
+ " The default highlight links. Can be overridden later.
HiLink pythonStatement Statement
- HiLink pythonFunction Function
HiLink pythonConditional Conditional
HiLink pythonRepeat Repeat
- HiLink pythonString String
- HiLink pythonRawString String
- HiLink pythonEscape Special
HiLink pythonOperator Operator
- HiLink pythonPreCondit PreCondit
+ HiLink pythonException Exception
+ HiLink pythonInclude Include
+ HiLink pythonDecorator Define
+ HiLink pythonFunction Function
HiLink pythonComment Comment
HiLink pythonTodo Todo
- HiLink pythonDecorator Define
- if exists("python_highlight_numbers")
- HiLink pythonNumber Number
+ HiLink pythonString String
+ HiLink pythonRawString String
+ HiLink pythonEscape Special
+ if !exists("python_no_number_highlight")
+ HiLink pythonNumber Number
endif
- if exists("python_highlight_builtins")
+ if !exists("python_no_builtin_highlight")
HiLink pythonBuiltin Function
endif
- if exists("python_highlight_exceptions")
- HiLink pythonException Exception
+ if !exists("python_no_exception_highlight")
+ HiLink pythonExceptions Structure
endif
- if exists("python_highlight_space_errors")
+ if exists("python_space_error_highlight")
HiLink pythonSpaceError Error
endif
+ if !exists("python_no_doctest_highlight")
+ HiLink pythonDoctest Special
+ HiLink pythonDoctestValue Define
+ endif
delcommand HiLink
endif
let b:current_syntax = "python"
-" vim: ts=8
+" vim:set sw=2 sts=2 ts=8 noet: