2014-07-07 15:48:09 +02:00
|
|
|
# http://python.org
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global BufCreate .*[.](py) %{
|
2017-11-03 08:34:41 +01:00
|
|
|
set-option buffer filetype python
|
2014-07-07 15:48:09 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 05:54:19 +02:00
|
|
|
# Initialization
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global WinSetOption filetype=python %{
|
2019-03-13 06:24:33 +01:00
|
|
|
require-module python
|
2019-04-10 05:54:19 +02:00
|
|
|
|
|
|
|
set-option window static_words %opt{python_static_words}
|
|
|
|
|
2020-05-19 09:16:05 +02:00
|
|
|
hook window InsertChar \n -group python-insert python-insert-on-new-line
|
2019-04-10 05:54:19 +02:00
|
|
|
hook window InsertChar \n -group python-indent python-indent-on-new-line
|
|
|
|
# cleanup trailing whitespaces on current line insert end
|
2022-03-16 23:20:07 +01:00
|
|
|
hook window ModeChange pop:insert:.* -group python-trim-indent %{ try %{ execute-keys -draft <semicolon> x s ^\h+$ <ret> d } }
|
2019-04-10 05:54:19 +02:00
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window python-.+ }
|
|
|
|
}
|
|
|
|
|
|
|
|
hook -group python-highlight global WinSetOption filetype=python %{
|
|
|
|
add-highlighter window/python ref python
|
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/python }
|
2019-03-13 06:24:33 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 04:45:54 +02:00
|
|
|
provide-module python %§
|
2019-03-13 06:24:33 +01:00
|
|
|
|
2016-03-14 13:08:51 +01:00
|
|
|
# Highlighters & Completion
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
2014-07-07 15:48:09 +02:00
|
|
|
|
2018-06-28 13:08:58 +02:00
|
|
|
add-highlighter shared/python regions
|
|
|
|
add-highlighter shared/python/code default-region group
|
2020-11-20 15:18:15 +01:00
|
|
|
add-highlighter shared/python/docstring region -match-capture ^\h*("""|''') (?<!\\)(?:\\\\)*("""|''') regions
|
2022-11-30 11:15:49 +01:00
|
|
|
|
2018-06-23 07:38:46 +02:00
|
|
|
add-highlighter shared/python/documentation region '##' '$' fill documentation
|
2018-07-02 12:59:12 +02:00
|
|
|
add-highlighter shared/python/comment region '#' '$' fill comment
|
2014-07-07 15:48:09 +02:00
|
|
|
|
2022-11-30 11:15:49 +01:00
|
|
|
# String interpolation
|
|
|
|
add-highlighter shared/python/f_triple_string region -match-capture [fF]("""|''') (?<!\\)(?:\\\\)*("""|''') group
|
|
|
|
add-highlighter shared/python/f_triple_string/ fill string
|
|
|
|
add-highlighter shared/python/f_triple_string/ regex \{.*?\} 0:value
|
|
|
|
|
|
|
|
add-highlighter shared/python/f_double_string region '[fF]"' (?<!\\)(\\\\)*" group
|
|
|
|
add-highlighter shared/python/f_double_string/ fill string
|
|
|
|
add-highlighter shared/python/f_double_string/ regex \{.*?\} 0:value
|
|
|
|
|
|
|
|
add-highlighter shared/python/f_single_string region "[fF]'" (?<!\\)(\\\\)*' group
|
|
|
|
add-highlighter shared/python/f_single_string/ fill string
|
|
|
|
add-highlighter shared/python/f_single_string/ regex \{.*?\} 0:value
|
|
|
|
|
|
|
|
|
|
|
|
# Regular string
|
|
|
|
add-highlighter shared/python/triple_string region -match-capture ("""|''') (?<!\\)(?:\\\\)*("""|''') fill string
|
|
|
|
add-highlighter shared/python/double_string region '"' (?<!\\)(\\\\)*" fill string
|
|
|
|
add-highlighter shared/python/single_string region "'" (?<!\\)(\\\\)*' fill string
|
|
|
|
|
2017-11-14 10:42:57 +01:00
|
|
|
# Integer formats
|
2018-06-28 13:08:58 +02:00
|
|
|
add-highlighter shared/python/code/ regex '(?i)\b0b[01]+l?\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '(?i)\b0x[\da-f]+l?\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '(?i)\b0o?[0-7]+l?\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '(?i)\b([1-9]\d*|0)l?\b' 0:value
|
2017-11-14 10:42:57 +01:00
|
|
|
# Float formats
|
2018-06-28 13:08:58 +02:00
|
|
|
add-highlighter shared/python/code/ regex '\b\d+[eE][+-]?\d+\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '(\b\d+)?\.\d+\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '\b\d+\.' 0:value
|
2017-11-14 10:42:57 +01:00
|
|
|
# Imaginary formats
|
2018-06-28 13:08:58 +02:00
|
|
|
add-highlighter shared/python/code/ regex '\b\d+\+\d+[jJ]\b' 0:value
|
2017-11-14 10:42:57 +01:00
|
|
|
|
2020-11-23 07:31:56 +01:00
|
|
|
add-highlighter shared/python/docstring/ default-region fill documentation
|
2020-11-22 22:45:59 +01:00
|
|
|
add-highlighter shared/python/docstring/ region '(>>>|\.\.\.) \K' (?=''')|(?=""") ref python
|
2014-07-07 15:48:09 +02:00
|
|
|
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2016-03-14 13:08:51 +01:00
|
|
|
# Grammar
|
2019-04-10 05:54:19 +02:00
|
|
|
values="True False None self inf"
|
|
|
|
meta="import from"
|
2017-11-18 09:56:47 +01:00
|
|
|
|
2017-11-14 10:42:57 +01:00
|
|
|
# attributes and methods list based on https://docs.python.org/3/reference/datamodel.html
|
2019-04-10 05:54:19 +02:00
|
|
|
attributes="__annotations__ __closure__ __code__ __defaults__ __dict__ __doc__
|
|
|
|
__globals__ __kwdefaults__ __module__ __name__ __qualname__"
|
|
|
|
methods="__abs__ __add__ __aenter__ __aexit__ __aiter__ __and__ __anext__
|
|
|
|
__await__ __bool__ __bytes__ __call__ __complex__ __contains__
|
|
|
|
__del__ __delattr__ __delete__ __delitem__ __dir__ __divmod__
|
|
|
|
__enter__ __eq__ __exit__ __float__ __floordiv__ __format__
|
|
|
|
__ge__ __get__ __getattr__ __getattribute__ __getitem__
|
|
|
|
__gt__ __hash__ __iadd__ __iand__ __ifloordiv__ __ilshift__
|
|
|
|
__imatmul__ __imod__ __imul__ __index__ __init__
|
|
|
|
__init_subclass__ __int__ __invert__ __ior__ __ipow__
|
|
|
|
__irshift__ __isub__ __iter__ __itruediv__ __ixor__ __le__
|
|
|
|
__len__ __length_hint__ __lshift__ __lt__ __matmul__
|
|
|
|
__missing__ __mod__ __mul__ __ne__ __neg__ __new__ __or__
|
|
|
|
__pos__ __pow__ __radd__ __rand__ __rdivmod__ __repr__
|
|
|
|
__reversed__ __rfloordiv__ __rlshift__ __rmatmul__ __rmod__
|
|
|
|
__rmul__ __ror__ __round__ __rpow__ __rrshift__ __rshift__
|
|
|
|
__rsub__ __rtruediv__ __rxor__ __set__ __setattr__
|
|
|
|
__setitem__ __set_name__ __slots__ __str__ __sub__
|
|
|
|
__truediv__ __xor__"
|
2017-11-18 09:56:47 +01:00
|
|
|
|
|
|
|
# built-in exceptions https://docs.python.org/3/library/exceptions.html
|
2019-04-10 05:54:19 +02:00
|
|
|
exceptions="ArithmeticError AssertionError AttributeError BaseException BlockingIOError
|
|
|
|
BrokenPipeError BufferError BytesWarning ChildProcessError
|
|
|
|
ConnectionAbortedError ConnectionError ConnectionRefusedError
|
|
|
|
ConnectionResetError DeprecationWarning EOFError Exception
|
|
|
|
FileExistsError FileNotFoundError FloatingPointError FutureWarning
|
|
|
|
GeneratorExit ImportError ImportWarning IndentationError
|
|
|
|
IndexError InterruptedError IsADirectoryError KeyboardInterrupt
|
|
|
|
KeyError LookupError MemoryError ModuleNotFoundError NameError
|
|
|
|
NotADirectoryError NotImplementedError OSError OverflowError
|
|
|
|
PendingDeprecationWarning PermissionError ProcessLookupError
|
|
|
|
RecursionError ReferenceError ResourceWarning RuntimeError
|
|
|
|
RuntimeWarning StopAsyncIteration StopIteration SyntaxError
|
|
|
|
SyntaxWarning SystemError SystemExit TabError TimeoutError TypeError
|
|
|
|
UnboundLocalError UnicodeDecodeError UnicodeEncodeError UnicodeError
|
|
|
|
UnicodeTranslateError UnicodeWarning UserWarning ValueError Warning
|
|
|
|
ZeroDivisionError"
|
2017-11-18 09:56:47 +01:00
|
|
|
|
2016-03-14 13:08:51 +01:00
|
|
|
# Keyword list is collected using `keyword.kwlist` from `keyword`
|
2019-04-10 05:54:19 +02:00
|
|
|
keywords="and as assert async await break class continue def del elif else except exec
|
|
|
|
finally for global if in is lambda nonlocal not or pass print
|
|
|
|
raise return try while with yield"
|
|
|
|
|
2021-10-24 13:56:18 +02:00
|
|
|
# Collected from `keyword.softkwlist`
|
|
|
|
soft_keywords="_ case match"
|
|
|
|
|
2019-04-10 05:54:19 +02:00
|
|
|
types="bool buffer bytearray bytes complex dict file float frozenset int
|
|
|
|
list long memoryview object set str tuple unicode xrange"
|
2017-11-18 09:56:47 +01:00
|
|
|
|
2019-04-10 05:54:19 +02:00
|
|
|
functions="abs all any ascii bin breakpoint callable chr classmethod compile complex
|
|
|
|
delattr dict dir divmod enumerate eval exec filter
|
|
|
|
format frozenset getattr globals hasattr hash help
|
|
|
|
hex id __import__ input isinstance issubclass iter
|
|
|
|
len locals map max memoryview min next oct open ord
|
|
|
|
pow print property range repr reversed round
|
|
|
|
setattr slice sorted staticmethod sum super type vars zip"
|
2017-11-18 09:56:47 +01:00
|
|
|
|
2019-04-10 05:54:19 +02:00
|
|
|
join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
|
2014-07-07 15:48:09 +02:00
|
|
|
|
2016-03-14 13:08:51 +01:00
|
|
|
# Add the language's grammar to the static completion list
|
2019-04-10 05:54:19 +02:00
|
|
|
printf %s\\n "declare-option str-list python_static_words $(join "${values} ${meta} ${attributes} ${methods} ${exceptions} ${keywords} ${types} ${functions}" ' ')"
|
2014-07-07 15:48:09 +02:00
|
|
|
|
2016-03-14 13:08:51 +01:00
|
|
|
# Highlight keywords
|
2016-03-17 14:30:14 +01:00
|
|
|
printf %s "
|
2019-04-10 05:54:19 +02:00
|
|
|
add-highlighter shared/python/code/ regex '\b($(join "${values}" '|'))\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '\b($(join "${meta}" '|'))\b' 0:meta
|
2020-05-30 16:47:32 +02:00
|
|
|
add-highlighter shared/python/code/ regex '\b($(join "${attributes}" '|'))\b' 0:attribute
|
2019-04-10 05:54:19 +02:00
|
|
|
add-highlighter shared/python/code/ regex '\bdef\s+($(join "${methods}" '|'))\b' 1:function
|
|
|
|
add-highlighter shared/python/code/ regex '\b($(join "${exceptions}" '|'))\b' 0:function
|
2021-10-24 13:56:18 +02:00
|
|
|
add-highlighter shared/python/code/ regex '\b($(join "${keywords} ${soft_keywords}" '|'))\b' 0:keyword
|
2019-04-10 05:54:19 +02:00
|
|
|
add-highlighter shared/python/code/ regex '\b($(join "${functions}" '|'))\b\(' 1:builtin
|
|
|
|
add-highlighter shared/python/code/ regex '\b($(join "${types}" '|'))\b' 0:type
|
2019-07-26 12:01:58 +02:00
|
|
|
add-highlighter shared/python/code/ regex '^\h*(@[\w_.]+))' 1:attribute
|
2016-11-11 14:07:45 +01:00
|
|
|
"
|
2014-07-07 15:48:09 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 08:11:43 +02:00
|
|
|
add-highlighter shared/python/code/ regex (?<=[\w\s\d\)\]'"_])(<=|<<|>>|>=|<>?|>|!=|==|\||\^|&|\+|-|\*\*?|//?|%|~) 0:operator
|
2020-08-13 08:12:35 +02:00
|
|
|
add-highlighter shared/python/code/ regex (?<=[\w\s\d'"_])((?<![=<>!]):?=(?![=])|[+*-]=) 0:builtin
|
2019-10-20 20:21:08 +02:00
|
|
|
add-highlighter shared/python/code/ regex ^\h*(?:from|import)\h+(\S+) 1:module
|
2018-01-07 23:35:54 +01:00
|
|
|
|
2016-03-14 13:08:51 +01:00
|
|
|
# Commands
|
|
|
|
# ‾‾‾‾‾‾‾‾
|
|
|
|
|
2023-04-02 14:09:57 +02:00
|
|
|
define-command -hidden python-insert-on-new-line %{ evaluate-commands -itersel -draft %{
|
|
|
|
execute-keys <semicolon>
|
|
|
|
try %{
|
|
|
|
evaluate-commands -draft -save-regs '/"' %{
|
|
|
|
# copy the commenting prefix
|
|
|
|
execute-keys -save-regs '' k x1s^\h*(#+\h*)<ret> y
|
|
|
|
try %{
|
|
|
|
# if the previous comment isn't empty, create a new one
|
|
|
|
execute-keys x<a-K>^\h*#+\h*$<ret> jxs^\h*<ret>P
|
|
|
|
} catch %{
|
|
|
|
# if there is no text in the previous comment, remove it completely
|
|
|
|
execute-keys d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# trim trailing whitespace on the previous line
|
|
|
|
try %{ execute-keys -draft k x s\h+$<ret> d }
|
2020-05-19 09:16:05 +02:00
|
|
|
}
|
2023-04-02 14:09:57 +02:00
|
|
|
} }
|
2021-04-17 18:18:43 +02:00
|
|
|
|
2020-07-30 04:45:54 +02:00
|
|
|
define-command -hidden python-indent-on-new-line %<
|
|
|
|
evaluate-commands -draft -itersel %<
|
2014-07-07 15:48:09 +02:00
|
|
|
# preserve previous line indent
|
2019-10-22 11:02:06 +02:00
|
|
|
try %{ execute-keys -draft <semicolon> K <a-&> }
|
2016-05-05 14:59:06 +02:00
|
|
|
# cleanup trailing whitespaces from previous line
|
2022-03-16 23:20:07 +01:00
|
|
|
try %{ execute-keys -draft k x s \h+$ <ret> d }
|
2016-10-13 21:10:41 +02:00
|
|
|
# indent after line ending with :
|
2022-04-15 00:14:17 +02:00
|
|
|
try %{ execute-keys -draft , k x <a-k> :$ <ret> <a-K> ^\h*# <ret> j <a-gt> }
|
2020-07-30 04:31:34 +02:00
|
|
|
# deindent closing brace/bracket when after cursor (for arrays and dictionaries)
|
2022-03-16 23:20:07 +01:00
|
|
|
try %< execute-keys -draft x <a-k> ^\h*[}\]] <ret> gh / [}\]] <ret> m <a-S> 1<a-&> >
|
2020-07-30 04:45:54 +02:00
|
|
|
>
|
|
|
|
>
|
2014-07-07 15:48:09 +02:00
|
|
|
|
2020-07-30 04:45:54 +02:00
|
|
|
§
|