2016-01-26 10:55:23 +03:00
|
|
|
##
|
2016-02-11 12:46:59 +03:00
|
|
|
## modeline.kak by lenormf
|
2016-01-26 10:55:23 +03:00
|
|
|
##
|
|
|
|
|
|
|
|
## Currently supported modeline format: vim
|
|
|
|
## Also supports kakoune options with a 'kak' or 'kakoune' prefix
|
|
|
|
## Only a few options are supported, in order to prevent the
|
|
|
|
## buffers from poking around the configuration too much
|
|
|
|
|
2016-02-11 00:26:56 +03:00
|
|
|
# Amount of additional lines that will be checked at the beginning
|
|
|
|
# and the end of the buffer
|
2016-01-26 10:55:23 +03:00
|
|
|
decl int modelines 5
|
|
|
|
|
2017-01-13 03:56:30 +03:00
|
|
|
def -hidden modeline-parse %{
|
2016-01-26 10:55:23 +03:00
|
|
|
%sh{
|
|
|
|
# Translate a vim option into the corresponding kakoune one
|
|
|
|
function translate_opt_vim {
|
|
|
|
readonly key="$1"
|
|
|
|
readonly value="$2"
|
|
|
|
local tr=""
|
|
|
|
|
|
|
|
case "${key}" in
|
2016-09-07 14:37:57 +03:00
|
|
|
so|scrolloff) tr="scrolloff ${value},${kak_opt_scrolloff##*,}";;
|
|
|
|
siso|sidescrolloff) tr="scrolloff ${kak_opt_scrolloff%%,*},${value}";;
|
|
|
|
ts|tabstop) tr="tabstop ${value}";;
|
|
|
|
sw|shiftwidth) tr="indentwidth ${value}";;
|
|
|
|
tw|textwidth) tr="autowrap_column ${value}";;
|
|
|
|
ff|fileformat)
|
2016-01-26 10:55:23 +03:00
|
|
|
case "${value}" in
|
|
|
|
unix) tr="eolformat lf";;
|
|
|
|
dos) tr="eolformat crlf";;
|
2016-04-23 08:47:01 +03:00
|
|
|
*) printf %s\\n "echo -debug 'Unsupported file format: ${value}'";;
|
2016-01-26 10:55:23 +03:00
|
|
|
esac
|
|
|
|
;;
|
2016-09-07 14:37:57 +03:00
|
|
|
ft|filetype) tr="filetype ${value}";;
|
2016-01-26 10:55:23 +03:00
|
|
|
bomb) tr="BOM utf8";;
|
|
|
|
nobomb) tr="BOM none";;
|
2016-04-23 08:47:01 +03:00
|
|
|
*) printf %s\\n "echo -debug 'Unsupported vim variable: ${key}'";;
|
2016-01-26 10:55:23 +03:00
|
|
|
esac
|
|
|
|
|
2016-04-23 08:47:01 +03:00
|
|
|
[ -n "${tr}" ] && printf %s\\n "set buffer ${tr}"
|
2016-01-26 10:55:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Pass a few whitelisted options to kakoune directly
|
|
|
|
function translate_opt_kakoune {
|
|
|
|
readonly key="$1"
|
|
|
|
readonly value="$2"
|
|
|
|
readonly OPTS_ALLOWED=(
|
|
|
|
scrolloff
|
|
|
|
tabstop
|
|
|
|
indentwidth
|
|
|
|
autowrap_column
|
|
|
|
eolformat
|
|
|
|
filetype
|
|
|
|
BOM
|
|
|
|
)
|
|
|
|
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "${OPTS_ALLOWED[@]}" | grep -qw "${key}" || {
|
|
|
|
printf %s\\n "echo -debug 'Unsupported kakoune variable: ${key}'";
|
2016-01-26 10:55:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "set buffer ${key} ${value}"
|
2016-01-26 10:55:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# The following subshell will keep the actual options of the modeline, and strip:
|
|
|
|
# - the text that leads the first option, according to the official vim modeline format
|
|
|
|
# - the trailing text after the last option, and an optional ':' sign before it
|
|
|
|
# It will also convert the ':' seperators beween the option=value pairs
|
|
|
|
# More info: http://vimdoc.sourceforge.net/htmldoc/options.html#modeline
|
|
|
|
options=(
|
2016-09-07 14:37:57 +03:00
|
|
|
$(printf %s\\n "${kak_selection}" | sed \
|
|
|
|
-e 's/^[^:]\{1,\}://' \
|
|
|
|
-e 's/[ \t]*set\{0,1\}[ \t]//' \
|
|
|
|
-e 's/:[^a-zA-Z0-9_=-]*$//' \
|
|
|
|
-e 's/:/ /g')
|
2016-01-26 10:55:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
case "${kak_selection}" in
|
2016-09-07 14:37:57 +03:00
|
|
|
*vi:*|*vim:*) type_selection="vim";;
|
|
|
|
*kak:*|*kakoune:*) type_selection="kakoune";;
|
2016-04-23 08:47:01 +03:00
|
|
|
*) echo "echo -debug Unsupported modeline format";;
|
2016-01-26 10:55:23 +03:00
|
|
|
esac
|
|
|
|
[ -n "${type_selection}" ] || exit 1
|
|
|
|
|
|
|
|
for option in "${options[@]}"; do
|
|
|
|
name_option="${option%%=*}"
|
|
|
|
value_option="${option#*=}"
|
|
|
|
|
|
|
|
case "${type_selection}" in
|
|
|
|
vim) tr=$(translate_opt_vim "${name_option}" "${value_option}");;
|
|
|
|
kakoune) tr=$(translate_opt_kakoune "${name_option}" "${value_option}");;
|
|
|
|
esac
|
|
|
|
|
2016-04-23 08:47:01 +03:00
|
|
|
[ -n "${tr}" ] && printf %s\\n "${tr}"
|
2016-01-26 10:55:23 +03:00
|
|
|
done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 12:30:37 +03:00
|
|
|
# Add the following function to a hook on BufOpen to automatically parse modelines
|
2016-01-26 10:55:23 +03:00
|
|
|
# Select the first and last `modelines` lines in the buffer, only keep modelines
|
2016-10-11 10:03:41 +03:00
|
|
|
def modeline-parse -docstring "Read and interpret vi-format modelines at the beginning/end of the buffer" %{
|
2016-02-11 00:26:56 +03:00
|
|
|
try %{ eval -draft %{
|
|
|
|
exec \%s\`|.\'<ret> %opt{modelines}k <a-x> %opt{modelines}X \
|
|
|
|
s^[^\s]+?\s(vim?|kak(oune)?):\s?[^\n]+<ret>
|
2017-01-13 03:56:30 +03:00
|
|
|
eval -draft -itersel modeline-parse
|
2016-02-11 00:26:56 +03:00
|
|
|
} }
|
2016-01-26 10:55:23 +03:00
|
|
|
}
|