z_mirror/dotfiles
git clone https://git.raiguard.me/z_mirror/dotfiles
3683920
trunk
1# Implementation reference: 2# https://github.com/helix-editor/helix/blob/master/helix-core/src/indent.rs 3define-command set_current_buffer_indent_style -params 1 %{ 4 set-option buffer indentwidth %arg{1} 5} 6alias global set-indent-style set_current_buffer_indent_style 7 8define-command detect_current_buffer_indent_style %{ 9 detect_indent_style %val{bufname} 10} 11alias global detect-indent-style detect_current_buffer_indent_style 12 13define-command show_current_buffer_indent_style %{ 14 echo -markup "{Information}indentwidth=%opt{indentwidth}" 15} 16 17alias global show-indent-style show_current_buffer_indent_style 18 19define-command detect_indent_style -params 1 %{ 20 analyze_indent_style %arg{1} 21 try_infer_indent_style %arg{1} %val{selections} 22 delete-buffer 23} 24 25define-command analyze_indent_style -params 1 %{ 26 evaluate-commands -buffer %arg{1} -verbatim try %{ 27 set-register h 28 execute-keys '1000gGgx<a-s><a-K>^\h*$<ret>' 29 evaluate-commands -draft -itersel -verbatim try %{ 30 execute-keys 's^\t+<ret>' 31 set-register h %reg{h} 1 %val{selection_length} 32 } catch %{ 33 execute-keys 's^ +<ret>' 34 set-register h %reg{h} 0 %val{selection_length} 35 } catch %{ 36 set-register h %reg{h} 0 0 37 } 38 } 39 edit -scratch 40 try %{ 41 execute-keys '"h<a-R>a<ret><esc><a-_>s[01]\n\d+<ret><a-j>x<a-,>yp1,O0 0<esc>%s([01]+ \d+\n){2}<ret><a-k>\A1|\A0 0<ret>hk<a-i>nyjA-<c-r>"<esc>xy%<a-R>ghWd%|bc<ret><a-s>Px<a-k>\A[01] [1-9]\d*<ret>y%<a-R><a-_>|sed "s/^1 [0-9]\\+/1 0/"<ret><a-s>h<a-i>nLy%<a-R><a-_>|sort | uniq -c | sort -n -r<ret>s\d+<ret>' 42 } catch %{ 43 execute-keys '%c-1 -1<esc>%s-1<ret>' 44 } 45} 46 47define-command try_infer_indent_style -params 3.. %{ 48 evaluate-commands %sh{ 49 if [ "$3" -eq 0 -o "$3" -eq 2 -o "$3" -eq 4 -o "$3" -eq 6 -o "$3" -eq 8 ] && [ "$2" -gt 0 ] && echo "${4:-0} / $2 < 0.66" | bc -l | grep -q 1; then 50 printf 'set-option "buffer=%%arg{1}" indentwidth %d' "$3" 51 fi 52 } 53} 54 55complete-command detect_indent_style buffer 56complete-command analyze_indent_style buffer 57complete-command try_infer_indent_style buffer