From 9536a4c2b46e6851db41bab2806c7bdeee8edbb8 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Wed, 15 May 2024 19:47:21 -0500 Subject: Add kak and more --- config/kak/smarttab.kak | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 config/kak/smarttab.kak (limited to 'config/kak/smarttab.kak') diff --git a/config/kak/smarttab.kak b/config/kak/smarttab.kak new file mode 100644 index 0000000..53b53b6 --- /dev/null +++ b/config/kak/smarttab.kak @@ -0,0 +1,119 @@ +# ╭─────────────╥──────────────────────╮ +# │ Author: ║ File: │ +# │ Andrey Orst ║ smarttab.kak │ +# ╞═════════════╩══════════════════════╡ +# │ Extends tab handling by adding │ +# │ three different commands for │ +# │ each mode. │ +# ╞════════════════════════════════════╡ +# │ Rest of .dotfiles: │ +# │ GitHub.com/andreyorst/smarttab.kak │ +# ╰────────────────────────────────────╯ + +define-command -docstring "noexpandtab: use tab character to indent and align" \ +noexpandtab %{ require-module smarttab; noexpandtab-impl } + +define-command -docstring "expandtab: use space character to indent and align" \ +expandtab %{ require-module smarttab; expandtab-impl } + +define-command -docstring "smarttab: use tab character for indentation and space character for alignment" \ +smarttab %{ require-module smarttab; smarttab-impl } + +# note: there is no space-indent, tab-align setting; this is assumed to be equivalent to noexpandtab +define-command -docstring "autoconfigtab: use tab or space character to indent and align based upon existing settings (e.g. via editorconfig)" \ +autoconfigtab %{ require-module smarttab; autoconfigtab-impl } + +provide-module smarttab %§ + +# Options +# ‾‾‾‾‾‾‾ + +declare-option -docstring "amount of spaces that should be treated as single tab character when deleting spaces" \ +int softtabstop 0 + +declare-option -docstring "displays current tab handling mode" \ +str smarttab_mode '' + +declare-option -docstring 'what text to display in ''%opt{smarttab_mode}'' when expandtab mode is on' \ +str smarttab_expandtab_mode_name 'expandtab' + +declare-option -docstring 'what text to display in ''%opt{smarttab_mode}'' when expandtab mode is on' \ +str smarttab_noexpandtab_mode_name 'noexpandtab' + +declare-option -docstring 'what text to display in ''%opt{smarttab_mode}'' when expandtab mode is on' \ +str smarttab_smarttab_mode_name 'smarttab' + +declare-option -hidden int oldindentwidth %opt{indentwidth} + +# Commands +# ‾‾‾‾‾‾‾‾ + +define-command -hidden noexpandtab-impl %{ + set-option buffer smarttab_mode %opt{smarttab_noexpandtab_mode_name} + remove-hooks buffer smarttab-mode + smarttab-set + set-option buffer indentwidth 0 + set-option buffer aligntab true + hook -group smarttab-mode buffer InsertDelete ' ' %{ try %sh{ + if [ $kak_opt_softtabstop -gt 1 ]; then + printf "%s\n" 'execute-keys -draft "^\h+.\zI"' + fi + } catch %{ try %{ + execute-keys -itersel -draft "h%opt{softtabstop}2s\h+\zd" + }}} +} + +define-command -hidden expandtab-impl %{ + set-option buffer smarttab_mode %opt{smarttab_expandtab_mode_name} + remove-hooks buffer smarttab-mode + smarttab-set + set-option buffer aligntab false + hook -group smarttab-mode buffer InsertChar '\t' %{ execute-keys -draft "h%opt{indentwidth}@" } + hook -group smarttab-mode buffer InsertDelete ' ' %{ try %sh{ + if [ $kak_opt_softtabstop -gt 1 ]; then + printf "%s\n" 'execute-keys -draft -itersel "^\h+.\zI"' + fi + } catch %{ try %{ + execute-keys -itersel -draft "h%opt{softtabstop}2s\h+\zd" + }}} +} + +define-command -hidden smarttab-impl %{ + set-option buffer smarttab_mode %opt{smarttab_smarttab_mode_name} + remove-hooks buffer smarttab-mode + smarttab-set + set-option buffer indentwidth 0 + set-option buffer aligntab false + hook -group smarttab-mode buffer InsertChar '\t' %{ try %{ + execute-keys -draft "^\h*.\z" + } catch %{ + execute-keys -draft "h@" + }} + hook -group smarttab-mode buffer InsertDelete ' ' %{ try %sh{ + if [ $kak_opt_softtabstop -gt 1 ]; then + printf "%s\n" 'execute-keys -draft "^\h+.\zI"' + fi + } catch %{ try %{ + execute-keys -itersel -draft "h%opt{softtabstop}2s\h+\zd" + }}} +} + +define-command -hidden autoconfigtab-impl %{ evaluate-commands %sh{ + if [ $kak_opt_aligntab = true ]; then + echo "noexpandtab" + elif [ $kak_opt_indentwidth -eq 0 ]; then + echo "smarttab" + else + echo "expandtab" + fi +}} + +define-command -hidden smarttab-set %{ evaluate-commands %sh{ + if [ $kak_opt_indentwidth -eq 0 ]; then + printf "%s\n" "set-option buffer indentwidth $kak_opt_oldindentwidth" + else + printf "%s\n" "set-option buffer oldindentwidth $kak_opt_indentwidth" + fi +}} + +§ -- cgit v1.2.3