modeline.kak (5036B)
1 ## 2 ## modeline.kak by lenormf 3 ## 4 5 ## Currently supported modeline format: vim 6 ## Also supports kakoune options with a 'kak' or 'kakoune' prefix 7 ## Only a few options are supported, in order to prevent the 8 ## buffers from poking around the configuration too much 9 10 declare-option -docstring "amount of lines that will be checked at the beginning and the end of the buffer" \ 11 int modelines 5 12 13 define-command -hidden modeline-parse-impl %{ 14 evaluate-commands %sh{ 15 kakquote() { printf "%s" "$*" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"; } 16 17 # Translate a vim option into the corresponding kakoune one 18 translate_opt_vim() { 19 local key="$1" 20 local value="$2" 21 22 case "${key}" in 23 so|scrolloff) 24 key="scrolloff"; 25 value="${value},${kak_opt_scrolloff##*,}";; 26 siso|sidescrolloff) 27 key="scrolloff"; 28 value="${kak_opt_scrolloff%%,*},${value}";; 29 ts|tabstop) key="tabstop";; 30 sw|shiftwidth) key="indentwidth";; 31 tw|textwidth) key="autowrap_column";; 32 ff|fileformat) 33 key="eolformat"; 34 case "${value}" in 35 unix) value="lf";; 36 dos) value="crlf";; 37 *) 38 printf '%s\n' "Unsupported file format: ${value}" >&2 39 return;; 40 esac 41 ;; 42 ft|filetype) key="filetype";; 43 bomb) 44 key="BOM"; 45 value="utf8";; 46 nobomb) 47 key="BOM"; 48 value="none";; 49 spelllang|spl) 50 key="spell_lang"; 51 value="${value%%,*}";; 52 *) 53 printf '%s\n' "Unsupported vim variable: ${key}" >&2 54 return;; 55 esac 56 57 printf 'set-option buffer %s %s\n' "${key}" "$(kakquote "${value}")" 58 } 59 60 # Pass a few whitelisted options to kakoune directly 61 translate_opt_kakoune() { 62 local readonly key="$1" 63 local readonly value="$2" 64 65 case "${key}" in 66 scrolloff|tabstop|indentwidth|autowrap_column|eolformat|filetype|BOM|spell_lang);; 67 *) printf 'echo -debug %s' "$(kakquote "Unsupported kakoune variable: ${key}")" \ 68 | kak -p "${kak_session}" 69 return;; 70 esac 71 72 printf 'set-option buffer %s %s\n' "${key}" "$(kakquote "${value}")" 73 } 74 75 case "${kak_selection}" in 76 *vi:*|*vim:*) type_selection="vim";; 77 *kak:*|*kakoune:*) type_selection="kakoune";; 78 *) 79 printf 'fail %s\n' "$(kakquote "Unsupported modeline format: ${kak_selection}")" 80 exit 1 ;; 81 esac 82 83 # The following subshell will keep the actual options of the modeline, and strip: 84 # - the text that leads the first option, according to the official vim modeline format 85 # - the trailing text after the last option, and an optional ':' sign before it 86 # It will also convert the ':' seperators beween the option=value pairs 87 # More info: http://vimdoc.sourceforge.net/htmldoc/options.html#modeline 88 printf %s "${kak_selection}" | sed \ 89 -e 's/^[^:]\{1,\}://' \ 90 -e 's/[ \t]*set\{0,1\}[ \t]\([^:]*\).*$/\1/' \ 91 -e 's/:[^a-zA-Z0-9_=-]*$//' \ 92 -e 's/:/ /g' \ 93 | tr ' ' '\n' \ 94 | while read -r option; do 95 name_option="${option%%=*}" 96 value_option="${option#*=}" 97 98 if [ -z "${option}" ]; then 99 continue 100 fi 101 102 case "${type_selection}" in 103 vim) translate_opt_vim "${name_option}" "${value_option}";; 104 kakoune) translate_opt_kakoune "${name_option}" "${value_option}";; 105 *) exit 1;; 106 esac 107 done 108 } 109 } 110 111 # Add the following function to a hook on BufOpenFile to automatically parse modelines 112 # Select the first and last `modelines` lines in the buffer, only keep modelines 113 # ref. options.txt (in vim `:help options`) : 2 forms of modelines: 114 # [text]{white}{vi:|vim:|ex:}[white]{options} 115 # [text]{white}{vi:|vim:|Vim:|ex:}[white]se[t] {options}:[text] 116 define-command modeline-parse -docstring "Read and interpret vi-format modelines at the beginning/end of the buffer" %{ 117 try %{ evaluate-commands -draft -save-regs ^ %{ 118 execute-keys -save-regs "" gk %opt{modelines} JK x Z 119 execute-keys gj %opt{modelines} KJ x <a-z> a 120 execute-keys s^\S*?\s+?\w+:\s?\N+<ret> x 121 evaluate-commands -draft -itersel modeline-parse-impl 122 } } 123 }