# http://ruby-lang.org # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ # Detection # ‾‾‾‾‾‾‾‾‾ hook global BufCreate .*(([.](rb))|(irbrc)|(pryrc)|(Capfile|[.]cap)|(Gemfile)|(Guardfile)|(Rakefile|[.]rake)|(Thorfile|[.]thor)|(Vagrantfile)) %{ set-option buffer filetype ruby } # Highlighters # ‾‾‾‾‾‾‾‾‾‾‾‾ add-highlighter shared/ regions -default code ruby \ double_string '"' (? < \ division '[\w\)\]](/|(\h+/\h+))' '\w' '' # Help Kakoune to better detect /…/ literals # Regular expression flags are: i → ignore case, m → multi-lines, o → only interpolate #{} blocks once, x → extended mode (ignore white spaces) # Literals are: i → array of symbols, q → string, r → regular expression, s → symbol, w → array of words, x → capture shell result add-highlighter shared/ruby/double_string fill string add-highlighter shared/ruby/double_string regions regions interpolation \Q#{ \} \{ add-highlighter shared/ruby/double_string/regions/interpolation fill meta add-highlighter shared/ruby/single_string fill string add-highlighter shared/ruby/backtick fill meta add-highlighter shared/ruby/backtick regions regions interpolation \Q#{ \} \{ add-highlighter shared/ruby/backtick/regions/interpolation fill meta add-highlighter shared/ruby/regex fill meta add-highlighter shared/ruby/regex regions regions interpolation \Q#{ \} \{ add-highlighter shared/ruby/regex/regions/interpolation fill meta add-highlighter shared/ruby/comment fill comment add-highlighter shared/ruby/literal fill meta add-highlighter shared/ruby/code regex \b([A-Za-z]\w*:(?!:))|([$@][A-Za-z]\w*)|((? # remove trailing white spaces try %{ execute-keys -draft s \h + $ d } } } define-command -hidden ruby-indent-on-char %{ evaluate-commands -no-hooks -draft -itersel %{ # align middle and end structures to start try %{ execute-keys -draft ^ \h * (else|elsif) $ ^ \h * (if) s \A | \z \' } try %{ execute-keys -draft ^ \h * (when) $ ^ \h * (case) s \A | \z \' } try %{ execute-keys -draft ^ \h * (rescue) $ ^ \h * (begin) s \A | \z \' } try %{ execute-keys -draft ^ \h * (end) $ ^ \h * (begin|case|class|def|do|for|if|module|unless|until|while) s \A | \z \' } } } define-command -hidden ruby-indent-on-new-line %{ evaluate-commands -no-hooks -draft -itersel %{ # preserve previous line indent try %{ execute-keys -draft K } # filter previous line try %{ execute-keys -draft k : ruby-filter-around-selections } # indent after start structure try %{ execute-keys -draft k ^ \h * (begin|case|class|def|do|else|elsif|ensure|for|if|module|rescue|unless|until|when|while) \b j } } } define-command -hidden ruby-insert-on-new-line %{ evaluate-commands -no-hooks -draft -itersel %{ # copy _#_ comment prefix and following white spaces try %{ execute-keys -draft k s '^\h*\K#\h*' y gh j P } # wisely add end structure evaluate-commands -save-regs x %{ try %{ execute-keys -draft k s ^ \h + \" x y } catch %{ reg x '' } try %{ execute-keys -draft k ^ x (begin|case|class|def|do|for|if|module|unless|until|while) j i X K ^ x (begin|case|class|def|do|for|if|module|unless|until|while) . * \n x end $ j x y p j a end } } } } # Initialization # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ hook -group ruby-highlight global WinSetOption filetype=ruby %{ add-highlighter window ref ruby } hook global WinSetOption filetype=ruby %{ hook window InsertChar .* -group ruby-indent ruby-indent-on-char hook window InsertChar \n -group ruby-insert ruby-insert-on-new-line hook window InsertChar \n -group ruby-indent ruby-indent-on-new-line alias window alt ruby-alternative-file } hook -group ruby-highlight global WinSetOption filetype=(?!ruby).* %{ remove-highlighter window/ruby } hook global WinSetOption filetype=(?!ruby).* %{ remove-hooks window ruby-indent remove-hooks window ruby-insert unalias window alt ruby-alternative-file }