From ea29a4c2d4df01b03ae800ccc0bc32c15100b7e1 Mon Sep 17 00:00:00 2001 From: Alex Leferry 2 Date: Mon, 7 Jul 2014 14:48:09 +0100 Subject: [PATCH] add rc/python.kak --- rc/python.kak | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 rc/python.kak diff --git a/rc/python.kak b/rc/python.kak new file mode 100644 index 00000000..a78e3b1e --- /dev/null +++ b/rc/python.kak @@ -0,0 +1,73 @@ +# http://python.org +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +# Detection +# ‾‾‾‾‾‾‾‾‾ + +hook global BufSetOption mimetype=text/x-python %{ + set buffer filetype python +} + +hook global BufCreate .*[.](py) %{ + set buffer filetype python +} + +# Highlighters +# ‾‾‾‾‾‾‾‾‾‾‾‾ + +addhl -group / multi_region -default code python \ + double_string '"""' '"""' '' \ + single_string "'''" "'''" '' \ + double_string '"' (? 0:value +addhl -group /python/code regex \<(import|from)\> 0:macro + +# Keyword list is collected using `keyword.kwlist` from `keyword` +addhl -group /python/code regex \<(and|as|assert|break|class|continue|def|del|elif|else|except|exec|finally|for|global|if|in|is|lambda|not|or|pass|print|raise|return|try|while|with|yield)\> 0:keyword + +# Commands +# ‾‾‾‾‾‾‾‾ + +def -hidden _python_filter_around_selections %{ + eval -draft -itersel %{ + exec + # remove trailing white spaces + try %{ exec -draft s \h+$ d } + } +} + +def -hidden _python_indent_on_new_line %{ + eval -draft -itersel %{ + # preserve previous line indent + try %{ exec -draft K } + # filter previous line + try %{ exec -draft k : _python_filter_around_selections } + # copy '#' comment prefix and following white spaces + try %{ exec -draft k x s ^\h*\K#\h* y j p } + # indent after : + try %{ exec -draft k x :$ j } + } +} + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +hook global WinSetOption filetype=python %{ + addhl ref python + + hook window InsertEnd .* -group python-hooks _python_filter_around_selections + hook window InsertChar \n -group python-indent _python_indent_on_new_line +} + +hook global WinSetOption filetype=(?!python).* %{ + rmhl python + rmhooks window python-indent + rmhooks window python-hooks +}