Skip to content

Instantly share code, notes, and snippets.

@JJK96
Last active January 31, 2020 08:25
Show Gist options
  • Save JJK96/5775319f052afbaa13e0f5aab0b41f5b to your computer and use it in GitHub Desktop.
Save JJK96/5775319f052afbaa13e0f5aab0b41f5b to your computer and use it in GitHub Desktop.
Kakoune detect indent
set-option global tabstop 4
set-option global indentwidth 4
declare-option -hidden str detect_indent_script %sh{ echo "${kak_source%/*}/index.js" }
define-command detect-indent %{
evaluate-commands %sh{
node "$kak_opt_detect_indent_script" "$kak_buffile"
}
}
hook global BufCreate .+ detect-indent
const fs = require('fs')
const detectIndent = require('detect-indent')
console.error(process.argv) // debugging, won't be taken by kakoune
const file = fs.readFileSync(process.argv[2], 'utf8')
const indent = detectIndent(file).indent
let spaces = 0
if (indent[0] === ' ') {
spaces = indent.length
}
if (spaces) {
console.error(spaces + ' spaces!')
console.log(`
set-option buffer tabstop ${spaces}
set-option buffer indentwidth ${spaces}
set-option buffer aligntab false
hook buffer InsertChar '\t' %{ execute-keys -draft "h${spaces}@" }
`)
} else {
console.error('tabs!')
console.log(`
set-option buffer indentwidth 0
set-option buffer aligntab true
`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment