kakoune

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

editorconfig.kak (3502B)


      1 # http://editorconfig.org/#file-format-details
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](editorconfig) %{
      8     set-option buffer filetype ini
      9     set-option buffer static_words indent_style indent_size tab_width \
     10     end_of_line charset insert_final_newline trim_trailing_whitespace root \
     11     latin1 utf-8 utf-8-bom utf-16be utf-16le lf cr crlf unset space tab max_line_length
     12 }
     13 
     14 define-command editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set formatting behavior according to editorconfig" %{
     15     evaluate-commands %sh{
     16         command -v editorconfig >/dev/null 2>&1 || { echo "fail editorconfig could not be found"; exit 1; }
     17 
     18         file="${1:-$kak_buffile}"
     19         case $file in
     20             /*) # $kak_buffile is a full path that starts with a '/'
     21                 printf %s\\n "remove-hooks buffer editorconfig-hooks"
     22                 editorconfig "$file" | awk -v file="$file" -F= -- '
     23                     $1 == "indent_style"             { indent_style = $2 }
     24                     $1 == "indent_size"              { indent_size = $2 == "tab" ? 4 : $2 }
     25                     $1 == "tab_width"                { tab_width = $2 }
     26                     $1 == "end_of_line"              { end_of_line = $2 }
     27                     $1 == "insert_final_newline"     { insert_final_newline = $2 }
     28                     $1 == "charset"                  { charset = $2 }
     29                     $1 == "trim_trailing_whitespace" { trim_trailing_whitespace = $2 }
     30                     $1 == "max_line_length"          { max_line_length = $2 }
     31 
     32                     END {
     33                         if (indent_style == "tab") {
     34                             print "set-option buffer indentwidth 0"
     35                         }
     36                         if (indent_style == "space") {
     37                             print "set-option buffer indentwidth " indent_size
     38                         }
     39                         if (indent_size || tab_width) {
     40                             print "set-option buffer tabstop " (tab_width ? tab_width : indent_size)
     41                         }
     42                         if (end_of_line == "lf" || end_of_line == "crlf") {
     43                             print "set-option buffer eolformat " end_of_line
     44                         }
     45                         if (insert_final_newline == "true" || insert_final_newline == "false") {
     46                             print "set-option buffer finaleol " (insert_final_newline == "true" ? "present" : "missing")
     47                         }
     48                         if (charset == "utf-8-bom") {
     49                             print "set-option buffer BOM utf8"
     50                         }
     51                         if (trim_trailing_whitespace == "true") {
     52                             print "hook buffer BufWritePre \"" file "\" -group editorconfig-hooks %{ try %{ execute-keys -draft %{%s\\h+$<ret>d} } }"
     53                         }
     54                         if (max_line_length && max_line_length != "off") {
     55                             print "set window autowrap_column " max_line_length
     56                             print "autowrap-enable"
     57                             print "add-highlighter window/ column %sh{ echo $((" max_line_length "+1)) } default,bright-black"
     58                         }
     59                     }
     60                 ' ;;
     61         esac
     62     }
     63 }
     64 complete-command editorconfig-load file