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
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
declare-option -docstring "amount of lines that will be checked at the beginning and the end of the buffer" \
|
2017-05-16 14:35:43 +03:00
|
|
|
int modelines 5
|
2016-01-26 10:55:23 +03:00
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command -hidden modeline-parse-impl %{
|
2018-05-07 00:29:52 +03:00
|
|
|
evaluate-commands %sh{
|
2020-09-16 11:37:01 +03:00
|
|
|
kakquote() { printf "%s" "$*" | sed "s/'/''/g; 1s/^/'/; \$s/\$/'/"; }
|
|
|
|
|
2016-01-26 10:55:23 +03:00
|
|
|
# Translate a vim option into the corresponding kakoune one
|
2017-02-26 10:09:57 +03:00
|
|
|
translate_opt_vim() {
|
2016-01-26 10:55:23 +03:00
|
|
|
readonly key="$1"
|
|
|
|
readonly value="$2"
|
2017-02-26 10:09:57 +03:00
|
|
|
tr=""
|
2016-01-26 10:55:23 +03:00
|
|
|
|
|
|
|
case "${key}" in
|
2020-09-16 11:37:01 +03:00
|
|
|
so|scrolloff) tr=$(kakquote scrolloff "${value},${kak_opt_scrolloff##*,}");;
|
|
|
|
siso|sidescrolloff) tr=$(kakquote scrolloff "${kak_opt_scrolloff%%,*},${value}");;
|
|
|
|
ts|tabstop) tr=$(kakquote tabstop "${value}");;
|
|
|
|
sw|shiftwidth) tr=$(kakquote indentwidth "${value}");;
|
|
|
|
tw|textwidth) tr=$(kakquote autowrap_column "${value}");;
|
2016-09-07 14:37:57 +03:00
|
|
|
ff|fileformat)
|
2016-01-26 10:55:23 +03:00
|
|
|
case "${value}" in
|
|
|
|
unix) tr="eolformat lf";;
|
|
|
|
dos) tr="eolformat crlf";;
|
2020-09-16 11:58:47 +03:00
|
|
|
*) printf 'echo -debug %s' "$(kakquote "Unsupported file format: ${value}")" \
|
|
|
|
| kak -p "${kak_session}";;
|
2016-01-26 10:55:23 +03:00
|
|
|
esac
|
|
|
|
;;
|
2020-09-16 11:37:01 +03:00
|
|
|
ft|filetype) tr=$(kakquote filetype "{value}");;
|
2016-01-26 10:55:23 +03:00
|
|
|
bomb) tr="BOM utf8";;
|
|
|
|
nobomb) tr="BOM none";;
|
2020-09-16 11:37:01 +03:00
|
|
|
spelllang|spl) tr=$(kakquote spell_lang "{value%%,*}");;
|
2020-09-16 11:58:47 +03:00
|
|
|
*) printf 'echo -debug %s' "$(kakquote "Unsupported vim variable: ${key}")" \
|
|
|
|
| kak -p "${kak_session}";;
|
2016-01-26 10:55:23 +03:00
|
|
|
esac
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
[ -n "${tr}" ] && printf %s\\n "set-option buffer ${tr}"
|
2016-01-26 10:55:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Pass a few whitelisted options to kakoune directly
|
2017-02-26 10:09:57 +03:00
|
|
|
translate_opt_kakoune() {
|
2016-01-26 10:55:23 +03:00
|
|
|
readonly key="$1"
|
|
|
|
readonly value="$2"
|
|
|
|
|
2017-02-26 10:09:57 +03:00
|
|
|
case "${key}" in
|
2019-11-08 19:56:36 +03:00
|
|
|
scrolloff|tabstop|indentwidth|autowrap_column|eolformat|filetype|BOM|spell_lang);;
|
2020-09-16 11:58:47 +03:00
|
|
|
*) printf 'echo -debug %s' "$(kakquote "Unsupported kakoune variable: ${key}")" \
|
|
|
|
| kak -p "${kak_session}"
|
2017-02-26 10:09:57 +03:00
|
|
|
return;;
|
|
|
|
esac
|
2016-01-26 10:55:23 +03:00
|
|
|
|
2020-09-16 11:37:01 +03:00
|
|
|
printf 'set-option buffer %s' "$(kakquote "${key}" "${value}")"
|
2016-01-26 10:55:23 +03:00
|
|
|
}
|
|
|
|
|
2017-02-26 10:09:57 +03:00
|
|
|
case "${kak_selection}" in
|
|
|
|
*vi:*|*vim:*) type_selection="vim";;
|
|
|
|
*kak:*|*kakoune:*) type_selection="kakoune";;
|
2020-09-16 11:58:47 +03:00
|
|
|
*) printf 'echo -debug Unsupported modeline format' \
|
|
|
|
| kak -p "${kak_session}"; exit 1 ;;
|
2017-02-26 10:09:57 +03:00
|
|
|
esac
|
|
|
|
|
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
|
2017-02-26 10:09:57 +03:00
|
|
|
printf %s "${kak_selection}" | sed \
|
2016-09-07 14:37:57 +03:00
|
|
|
-e 's/^[^:]\{1,\}://' \
|
|
|
|
-e 's/[ \t]*set\{0,1\}[ \t]//' \
|
|
|
|
-e 's/:[^a-zA-Z0-9_=-]*$//' \
|
2017-02-26 10:09:57 +03:00
|
|
|
-e 's/:/ /g' \
|
|
|
|
| tr ' ' '\n' \
|
|
|
|
| while read -r option; do
|
2016-01-26 10:55:23 +03:00
|
|
|
name_option="${option%%=*}"
|
|
|
|
value_option="${option#*=}"
|
|
|
|
|
2017-02-26 10:09:57 +03:00
|
|
|
[ -z "${option}" ] && continue
|
|
|
|
|
2016-01-26 10:55:23 +03:00
|
|
|
case "${type_selection}" in
|
|
|
|
vim) tr=$(translate_opt_vim "${name_option}" "${value_option}");;
|
|
|
|
kakoune) tr=$(translate_opt_kakoune "${name_option}" "${value_option}");;
|
2020-08-23 19:55:50 +03:00
|
|
|
*) tr="";;
|
2016-01-26 10:55:23 +03:00
|
|
|
esac
|
|
|
|
|
2016-04-23 08:47:01 +03:00
|
|
|
[ -n "${tr}" ] && printf %s\\n "${tr}"
|
2016-01-26 10:55:23 +03:00
|
|
|
done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-19 16:08:06 +03:00
|
|
|
# Add the following function to a hook on BufOpenFile 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
|
2017-10-15 05:22:38 +03:00
|
|
|
# ref. options.txt (in vim `:help options`) : 2 forms of modelines:
|
|
|
|
# [text]{white}{vi:|vim:|ex:}[white]{options}
|
|
|
|
# [text]{white}{vi:|vim:|Vim:|ex:}[white]se[t] {options}:[text]
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command modeline-parse -docstring "Read and interpret vi-format modelines at the beginning/end of the buffer" %{
|
2017-11-03 11:09:45 +03:00
|
|
|
try %{ evaluate-commands -draft %{
|
2020-09-16 10:55:36 +03:00
|
|
|
execute-keys <percent> "s(?S)\A(.+\n){,%opt{modelines}}|(.+\n){,%opt{modelines}}\z<ret>" \
|
2017-10-21 21:00:56 +03:00
|
|
|
s^\S*?\s+?(vim?|kak(oune)?):\s?[^\n]+<ret> <a-x>
|
2017-11-03 11:09:45 +03:00
|
|
|
evaluate-commands -draft -itersel modeline-parse-impl
|
2016-02-11 00:26:56 +03:00
|
|
|
} }
|
2016-01-26 10:55:23 +03:00
|
|
|
}
|