kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

kakrc.kak (6753B)


      1 # http://kakoune.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate (.*/)?(kakrc|.*\.kak) %{
      8     set-option buffer filetype kak
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=kak %~
     15     require-module kak
     16 
     17     set-option window static_words %opt{kak_static_words}
     18 
     19     hook window InsertChar \n -group kak-insert kak-insert-on-new-line
     20     hook window InsertChar \n -group kak-indent kak-indent-on-new-line
     21     hook window InsertChar [>)}\]] -group kak-indent kak-indent-on-closing-matching
     22     hook window InsertChar (?![[{(<>)}\]])[^\s\w] -group kak-indent kak-indent-on-closing-char
     23     # cleanup trailing whitespaces on current line insert end
     24     hook window ModeChange pop:insert:.* -group kak-trim-indent %{ try %{ execute-keys -draft <semicolon> x s ^\h+$ <ret> d } }
     25     set-option buffer extra_word_chars '_' '-'
     26 
     27     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window kak-.+ }
     28 ~
     29 
     30 hook -group kak-highlight global WinSetOption filetype=kak %{
     31     add-highlighter window/kakrc ref kakrc
     32     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/kakrc }
     33 }
     34 
     35 provide-module kak %§
     36 
     37 require-module sh
     38 
     39 # Highlighters & Completion
     40 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     41 
     42 add-highlighter shared/kakrc regions
     43 add-highlighter shared/kakrc/code default-region group
     44 add-highlighter shared/kakrc/comment region (^|\h)\K# $ fill comment
     45 add-highlighter shared/kakrc/double_string region -recurse %{(?<!")("")+(?!")} %{(^|\h)\K"} %{"(?!")} group
     46 add-highlighter shared/kakrc/single_string region -recurse %{(?<!')('')+(?!')} %{(^|\h)\K'} %{'(?!')} group
     47 add-highlighter shared/kakrc/shell1 region -recurse '\{' '(^|\h)\K%?%sh\{' '\}' ref sh
     48 add-highlighter shared/kakrc/shell2 region -recurse '\(' '(^|\h)\K%?%sh\(' '\)' ref sh
     49 add-highlighter shared/kakrc/shell3 region -recurse '\[' '(^|\h)\K%?%sh\[' '\]' ref sh
     50 add-highlighter shared/kakrc/shell4 region -recurse '<'  '(^|\h)\K%?%sh<'  '>'  ref sh
     51 add-highlighter shared/kakrc/shell5 region -recurse '\{' '(^|\h)\K-?shell-script(-completion|-candidates)?\h+%\{' '\}' ref sh
     52 add-highlighter shared/kakrc/shell6 region -recurse '\(' '(^|\h)\K-?shell-script(-completion|-candidates)?\h+%\(' '\)' ref sh
     53 add-highlighter shared/kakrc/shell7 region -recurse '\[' '(^|\h)\K-?shell-script(-completion|-candidates)?\h+%\[' '\]' ref sh
     54 add-highlighter shared/kakrc/shell8 region -recurse '<'  '(^|\h)\K-?shell-script(-completion|-candidates)?\h+%<'  '>'  ref sh
     55 
     56 evaluate-commands %sh{
     57     # Grammar
     58     keywords="add-highlighter alias arrange-buffers buffer buffer-next buffer-previous catch
     59               change-directory colorscheme debug declare-option declare-user-mode define-command complete-command
     60               delete-buffer delete-buffer! echo edit edit! enter-user-mode evaluate-commands execute-keys
     61               fail hook info kill kill! map nop on-key prompt provide-module quit quit!
     62               remove-highlighter remove-hooks rename-buffer rename-client rename-session require-module
     63               select set-face set-option set-register source trigger-user-hook try
     64               unalias unmap unset-face unset-option update-option
     65               write write! write-all write-all-quit write-quit write-quit!"
     66     attributes="global buffer window current
     67                 normal insert prompt goto view user object combine
     68                 number-lines show-matching show-whitespaces fill regex dynregex group flag-lines
     69                 ranges line column wrap ref regions region default-region replace-ranges"
     70     types="int bool str regex int-list str-list completions line-specs range-specs str-to-str-map"
     71     values="default black red green yellow blue magenta cyan white yes no false true"
     72 
     73     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
     74 
     75     # Add the language's grammar to the static completion list
     76     printf %s\\n "declare-option str-list kak_static_words $(join "${keywords} ${attributes} ${types} ${values}" ' ')'"
     77 
     78     # Highlight keywords (which are always surrounded by whitespace)
     79     printf '%s\n' "add-highlighter shared/kakrc/code/keywords regex (?:\s|\A)\K($(join "${keywords}" '|'))(?:(?=\s)|\z) 0:keyword
     80                    add-highlighter shared/kakrc/code/attributes regex (?:\s|\A)\K($(join "${attributes}" '|'))(?:(?=\s)|\z) 0:attribute
     81                    add-highlighter shared/kakrc/code/types regex (?:\s|\A)\K($(join "${types}" '|'))(?:(?=\s)|\z) 0:type
     82                    add-highlighter shared/kakrc/code/values regex (?:\s|\A)\K($(join "${values}" '|'))(?:(?=\s)|\z) 0:value"
     83 }
     84 
     85 add-highlighter shared/kakrc/code/colors regex \b(rgb:[0-9a-fA-F]{6}|rgba:[0-9a-fA-F]{8})\b 0:value
     86 add-highlighter shared/kakrc/code/numbers regex \b\d+\b 0:value
     87 
     88 add-highlighter shared/kakrc/double_string/fill fill string
     89 add-highlighter shared/kakrc/double_string/escape regex '""' 0:default+b
     90 add-highlighter shared/kakrc/single_string/fill fill string
     91 add-highlighter shared/kakrc/single_string/escape regex "''" 0:default+b
     92 
     93 add-highlighter shared/kak ref kakrc
     94 
     95 # Commands
     96 # ‾‾‾‾‾‾‾‾
     97 
     98 define-command -hidden kak-insert-on-new-line %~
     99     evaluate-commands -draft -itersel %=
    100         # copy '#' comment prefix and following white spaces
    101         try %{ execute-keys -draft k x s ^\h*#\h* <ret> y jgh P }
    102     =
    103 ~
    104 
    105 define-command -hidden kak-indent-on-new-line %~
    106     evaluate-commands -draft -itersel %=
    107         # preserve previous line indent
    108         try %{ execute-keys -draft <semicolon> K <a-&> }
    109         # cleanup trailing whitespaces from previous line
    110         try %{ execute-keys -draft k x s \h+$ <ret> d }
    111         # indent after line ending with %\w*[^\s\w]
    112         try %{ execute-keys -draft k x <a-k> \%\w*[^\s\w]$ <ret> j <a-gt> }
    113         # deindent closing brace when after cursor
    114         try %_ execute-keys -draft -itersel x <a-k> ^\h*([>)}\]]) <ret> gh / <c-r>1 <ret> m <a-S> 1<a-&> _
    115         # deindent closing char(s)
    116         try %{ execute-keys -draft -itersel x <a-k> ^\h*([^\s\w]) <ret> gh / <c-r>1 <ret> <a-?> <c-r>1 <ret> <a-T>% <a-k> \w*<c-r>1$ <ret> <a-S> 1<a-&> }
    117     =
    118 ~
    119 
    120 define-command -hidden kak-indent-on-closing-matching %~
    121     # align to opening matching brace when alone on a line
    122     try %= execute-keys -draft -itersel <a-h><a-k>^\h*\Q %val{hook_param} \E$<ret> mGi s \A|.\z<ret> 1<a-&> =
    123 ~
    124 
    125 define-command -hidden kak-indent-on-closing-char %{
    126     # align to opening matching character when alone on a line
    127     try %{ execute-keys -draft -itersel <a-h><a-k>^\h*\Q %val{hook_param} \E$<ret>gi<a-f> %val{hook_param} <a-T>%<a-k>\w*\Q %val{hook_param} \E$<ret> s \A|.\z<ret> gi 1<a-&> }
    128 }
    129 
    130 §