summaryrefslogtreecommitdiff
path: root/rc/detection
diff options
context:
space:
mode:
authorAlex Leferry 2 <alexherbo2@gmail.com>2019-03-18 19:56:34 +0100
committerAlex Leferry 2 <alexherbo2@gmail.com>2019-03-21 01:06:16 +0100
commitc0dccdd90dd615cf663d95fd94fbdbdf2a88b165 (patch)
treecb48fb1b7fb74e6e3b98a62f6e2768686bb75c98 /rc/detection
parentf87e844244d5ee81e9c1ceb04c354726002ae760 (diff)
Add categories in rc/
Closes #2783
Diffstat (limited to 'rc/detection')
-rw-r--r--rc/detection/editorconfig.kak57
-rw-r--r--rc/detection/file.kak17
-rw-r--r--rc/detection/modeline.kak102
3 files changed, 176 insertions, 0 deletions
diff --git a/rc/detection/editorconfig.kak b/rc/detection/editorconfig.kak
new file mode 100644
index 00000000..9421e033
--- /dev/null
+++ b/rc/detection/editorconfig.kak
@@ -0,0 +1,57 @@
+# http://editorconfig.org/#file-format-details
+# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
+
+# Detection
+# ‾‾‾‾‾‾‾‾‾
+
+hook global BufCreate .*[.](editorconfig) %{
+ set-option buffer filetype ini
+ set-option buffer static_words indent_style indent_size tab_width \
+ end_of_line charset insert_final_newline trim_trailing_whitespace root \
+ latin1 utf-8 utf-8-bom utf-16be utf-16le lf cr crlf unset space tab
+}
+
+declare-option -hidden bool editorconfig_trim_trailing_whitespace false
+
+define-command editorconfig-load -params ..1 -docstring "editorconfig-load [file]: set formatting behavior according to editorconfig" %{
+ remove-hooks buffer editorconfig-hooks
+ evaluate-commands %sh{
+ command -v editorconfig >/dev/null 2>&1 || { echo 'echo -markup "{Error}editorconfig could not be found"'; exit 1; }
+ editorconfig "${1:-$kak_buffile}" | awk -F= -- '
+ /indent_style=/ { indent_style = $2 }
+ /indent_size=/ { indent_size = $2 == "tab" ? 4 : $2 }
+ /tab_width=/ { tab_width = $2 }
+ /end_of_line=/ { end_of_line = $2 }
+ /charset=/ { charset = $2 }
+ /trim_trailing_whitespace=/ { trim_trailing_whitespace = $2 }
+
+ END {
+ if (indent_style == "tab") {
+ print "set-option buffer indentwidth 0"
+ print "set-option buffer aligntab true"
+ }
+ if (indent_style == "space") {
+ print "set-option buffer indentwidth " (indent_size == "tab" ? 4 : indent_size)
+ print "set-option buffer aligntab false"
+ }
+ if (indent_size || tab_width) {
+ print "set-option buffer tabstop " (tab_width ? tab_width : indent_size)
+ }
+ if (end_of_line == "lf" || end_of_line == "crlf") {
+ print "set-option buffer eolformat " end_of_line
+ }
+ if (charset == "utf-8-bom") {
+ print "set-option buffer BOM utf8"
+ }
+ if (trim_trailing_whitespace == "true") {
+ print "set-option buffer editorconfig_trim_trailing_whitespace true"
+ }
+ }
+ '
+ }
+ hook buffer BufWritePre %val{buffile} -group editorconfig-hooks %{ evaluate-commands %sh{
+ if [ ${kak_opt_editorconfig_trim_trailing_whitespace} = "true" ]; then
+ printf %s\\n "try %{ execute-keys -draft %{ %s\h+$<ret>d } }"
+ fi
+ } }
+}
diff --git a/rc/detection/file.kak b/rc/detection/file.kak
new file mode 100644
index 00000000..073e9ce0
--- /dev/null
+++ b/rc/detection/file.kak
@@ -0,0 +1,17 @@
+hook global BufOpenFile .* %{ evaluate-commands %sh{
+ if [ -z "${kak_opt_filetype}" ]; then
+ mime=$(file -b --mime-type "${kak_buffile}")
+ case "${mime}" in
+ application/*+xml) filetype="xml" ;;
+ image/*+xml) filetype="xml" ;; #SVG
+ message/rfc822) filetype="mail" ;;
+ text/x-shellscript) filetype="sh" ;;
+ text/x-*) filetype="${mime#text/x-}" ;;
+ text/*) filetype="${mime#text/}" ;;
+ application/*) filetype="${mime#application/}" ;;
+ esac
+ if [ -n "${filetype}" ]; then
+ printf "set-option buffer filetype '%s'\n" "${filetype}"
+ fi
+ fi
+} }
diff --git a/rc/detection/modeline.kak b/rc/detection/modeline.kak
new file mode 100644
index 00000000..ece6d97a
--- /dev/null
+++ b/rc/detection/modeline.kak
@@ -0,0 +1,102 @@
+##
+## modeline.kak by lenormf
+##
+
+## 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
+
+declare-option -docstring "amount of lines that will be checked at the beginning and the end of the buffer" \
+ int modelines 5
+
+define-command -hidden modeline-parse-impl %{
+ evaluate-commands %sh{
+ # Translate a vim option into the corresponding kakoune one
+ translate_opt_vim() {
+ readonly key="$1"
+ readonly value="$2"
+ tr=""
+
+ case "${key}" in
+ 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)
+ case "${value}" in
+ unix) tr="eolformat lf";;
+ dos) tr="eolformat crlf";;
+ *) printf %s\\n "echo -debug 'Unsupported file format: ${value}'";;
+ esac
+ ;;
+ ft|filetype) tr="filetype ${value}";;
+ bomb) tr="BOM utf8";;
+ nobomb) tr="BOM none";;
+ *) printf %s\\n "echo -debug 'Unsupported vim variable: ${key}'";;
+ esac
+
+ [ -n "${tr}" ] && printf %s\\n "set-option buffer ${tr}"
+ }
+
+ # Pass a few whitelisted options to kakoune directly
+ translate_opt_kakoune() {
+ readonly key="$1"
+ readonly value="$2"
+
+ case "${key}" in
+ scrolloff|tabstop|indentwidth|autowrap_column|eolformat|filetype|BOM);;
+ *) printf %s\\n "echo -debug 'Unsupported kakoune variable: ${key}'"
+ return;;
+ esac
+
+ printf %s\\n "set-option buffer ${key} ${value}"
+ }
+
+ case "${kak_selection}" in
+ *vi:*|*vim:*) type_selection="vim";;
+ *kak:*|*kakoune:*) type_selection="kakoune";;
+ *) echo "echo -debug Unsupported modeline format";;
+ esac
+ [ -n "${type_selection}" ] || exit 1
+
+ # 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
+ printf %s "${kak_selection}" | sed \
+ -e 's/^[^:]\{1,\}://' \
+ -e 's/[ \t]*set\{0,1\}[ \t]//' \
+ -e 's/:[^a-zA-Z0-9_=-]*$//' \
+ -e 's/:/ /g' \
+ | tr ' ' '\n' \
+ | while read -r option; do
+ name_option="${option%%=*}"
+ value_option="${option#*=}"
+
+ [ -z "${option}" ] && continue
+
+ case "${type_selection}" in
+ vim) tr=$(translate_opt_vim "${name_option}" "${value_option}");;
+ kakoune) tr=$(translate_opt_kakoune "${name_option}" "${value_option}");;
+ esac
+
+ [ -n "${tr}" ] && printf %s\\n "${tr}"
+ done
+ }
+}
+
+# Add the following function to a hook on BufOpenFile to automatically parse modelines
+# Select the first and last `modelines` lines in the buffer, only keep modelines
+# 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]
+define-command modeline-parse -docstring "Read and interpret vi-format modelines at the beginning/end of the buffer" %{
+ try %{ evaluate-commands -draft %{
+ execute-keys \%s\A|.\z<ret> %opt{modelines}k <a-x> %opt{modelines}X \
+ s^\S*?\s+?(vim?|kak(oune)?):\s?[^\n]+<ret> <a-x>
+ evaluate-commands -draft -itersel modeline-parse-impl
+ } }
+}