Skip to content

Instantly share code, notes, and snippets.

@rhizoome
Last active July 12, 2019 19:30
Show Gist options
  • Save rhizoome/806aa0a0b16989763a876f285ee859c1 to your computer and use it in GitHub Desktop.
Save rhizoome/806aa0a0b16989763a876f285ee859c1 to your computer and use it in GitHub Desktop.
viel kak
#!/usr/bin/env python3
import os
from subprocess import Popen, PIPE
proc = Popen(
["xsel", "-o", "-b"], stdout=PIPE
)
linesep = bytes(os.linesep, encoding="UTF-8")
stdout, _ = proc.communicate()
lines = stdout.split(linesep)
blen = 0
out = []
if lines:
for line in lines:
blen = max(blen, len(line))
for line in lines:
flen = blen - len(line)
out.append(line + (b" " * flen))
proc = Popen(
["xsel", "-i", "-b"], stdin=PIPE
)
proc.communicate(linesep.join(out))
def -hidden find-apply-impl -params 4 %{
eval -buffer %arg{1} %{
try %{
# go to the target line and select it (except for \n)
exec "%arg{2}g<a-x>H"
# check for noop, and abort if it's one
reg / %arg{3}
exec <a-K><ret>
# replace
reg '"' %arg{4}
exec R
reg s "%reg{s}o"
write
} catch %{
reg i "%reg{i}o"
}
}
}
def -hidden find-apply-force-impl -params 4 %{
try %{
find-apply-impl %arg{@}
} catch %{
# the buffer wasn't open: try editing it
# if this fails there is nothing we can do
eval -no-hooks -draft "edit -existing %arg{1}"
find-apply-impl %arg{@}
eval -no-hooks -buffer %arg{1} "delete-buffer"
}
}
def find-apply-changes -params ..1 -docstring "
find-apply-changes [-force]: apply changes specified in the current buffer to their respective file
If -force is specified, changes will also be applied to files that do not currently have a buffer
" %{
eval -no-hooks -save-regs 'csif' %{
reg s ""
reg i ""
reg f ""
reg c %sh{ [ "$1" = "-force" ] && printf find-apply-force-impl || printf find-apply-impl }
eval -save-regs '/"' -draft %{
# select all lines that match the *find* pattern
exec '%3s^([^\n]+):(\d+):([^\n]*)$<ret>'
eval -itersel %{
try %{
exec -save-regs '' <a-*>
%reg{c} %reg{1} %reg{2} "%reg{/}" %reg{3}
} catch %{
reg f "%reg{f}o"
}
}
}
echo -markup %sh{
printf "{Information}"
s=${#kak_main_reg_s}
[ $s -ne 1 ] && p=s
printf "%i change%s applied" "$s" "$p"
i=${#kak_main_reg_i}
[ $i -gt 0 ] && printf ", %i ignored" "$i"
f=${#kak_main_reg_f}
[ $f -gt 0 ] && printf ", %i failed" "$f"
}
}
}
#!/usr/bin/env python3
import shlex
import sys
import os
sys.stdout.write(os.linesep.join(shlex.split(sys.stdin.read())[2:]))
# Default format settings
set-option global tabstop 4
hook global WinCreate .* autowrap-enable
hook global WinCreate .* %{ add-highlighter window/* number-lines }
hook global BufOpenFile .* editorconfig-load
hook global BufNewFile .* editorconfig-load
# Plugins
source %sh{ echo "$HOME/jlf/kak_plugs/plug.kak/rc/plug.kak" }
set-option global plug_install_dir %sh{ echo "$HOME/jlf/kak_plugs" }
plug "andreyorst/plug.kak" noload
plug "andreyorst/smarttab.kak" config %{
set-option global softtabstop 4
hook global BufCreate .* expandtab
hook global WinSetOption filetype=(makefile|gas) noexpandtab
}
plug "ul/kak-lsp" do %{
mv "$HOME/jlf/kak_plugs/kak-lsp" "$HOME/.local/share"
ln -s "../../.local/share/kak-lsp/" "$HOME/jlf/kak_plugs/kak-lsp"
cd "$HOME/jlf/kak_plugs/kak-lsp"
cargo build --release --locked
cargo install --force --path .
} config %{
lsp-auto-hover-enable
hook global KakEnd .* lsp-stop
set-option global lsp_server_configuration pyls.plugins.pydocstyle.enabled=true
}
# Clipboard
map global user y -docstring "yank to clipboard" '<a-|>xsel -i -b<ret>'
map global user p -docstring "paste from clipboard (After)" '<a-!>xsel -o -b<ret>'
map global user P -docstring "paste from clipboard (Before)" '!xsel -o -b<ret>'
map global user R -docstring "replace from clipboard" '|xsel -o -b<ret>'
# Escape using alt-j
map -docstring "escape" global insert <A-j> <esc>
map -docstring "escape" global prompt <A-j> <esc>
map -docstring "escape" global menu <A-j> <esc>
map -docstring "escape" global view <A-j> <esc>
map -docstring "escape" global goto <A-j> <esc>
map -docstring "escape" global object <A-j> <esc>
map -docstring "escape" global user <A-j> <esc>
map -docstring "join line" global normal <C-r> <A-j>
map -docstring "escape" global normal <A-j> <esc>
# Mappings
map -docstring "search next word" global normal <C-e> <A-i>w*n
map -docstring "grep word under cursor" global user g '<A-i>w:grep %val{selection}<ret>'
map -docstring "wrap text" global user w '|fmt -w $kak_opt_autowrap_column<ret>'
# Finders
define-command -docstring "edit file from recursive search" file-edit -params 1 -shell-script-candidates %{ find -type f | grep -v '/\.git/' } %{ edit %arg{1} }
define-command -docstring "edit file from current git-worktree" git-edit -params 1 -shell-script-candidates %{ git ls-files } %{ edit %arg{1} }
# Commands
define-command -docstring "split horizontal" split-horizontal %{tmux-terminal-horizontal kak -c %val{session}}
alias global sh split-horizontal
define-command -docstring "split vertical" split-vertical %{tmux-terminal-vertical kak -c %val{session}}
alias global sv split-vertical
define-command -docstring "fill block with trailing spaces" fill-space %{ %sh{ fillspace } }
define-command -docstring "execute history command" -params 1 -shell-script-candidates %{
cat "$HOME/.kak_history" | kakfilterhist
} execute-history %{
evaluate-commands %sh{
echo "$1"
}
}
alias global eh execute-history
# Spelling
define-command -docstring "close spelling-file" -params 1 -shell-script-candidates %{
for b in ${kak_buflist}; do
if expr "$b" : '\*s:.*\*' > /dev/null; then
echo $b | sed 's/^\*s:\(.*\).*\*/\1/'
fi
done
} close-spell-file %{
evaluate-commands %sh{
echo buffer "*s:$1*"
echo delete-buffer
}
}
define-command -docstring "close all dictionaries" close-spell %{
close-spell-file %sh{
for b in ${kak_buflist}; do
if expr "$b" : '\*s:.*\*' > /dev/null; then
echo $b | sed 's/^\*s:\(.*\).*\*/\1/'
fi
done
}
}
define-command -docstring "load a spelling-file for completion" -params 1 open-spell-file %{
evaluate-commands %sh{
if ! expr "${kak_buflist}" : ".*$1.*" > /dev/null; then
cur_buf="${kak_bufname}"
echo edit "${HOME}/jlf/kakd/$1"
echo rename-buffer "*s:$1*"
echo buffer "$cur_buf"
fi
}
}
define-command -docstring "load de_CH_frami spelling" spell-de-ch-frami %{ open-spell-file de_CH_frami }
alias global splde spell-de-ch-frami
define-command -docstring "load en_GB spelling" spell-en-gb %{ open-spell-file en_GB }
alias global splen spell-en-gb
# Colors
colorscheme solarized-dark-termcolors
# Hooks
hook global KakEnd .* %{ echo -to-file %sh{ echo "$HOME/.kak_history" } -quoting kakoune reg : %reg{:} }
hook global KakBegin .* %{ try %{ evaluate-commands %sh{ cat "$HOME/.kak_history" | kaktrunchist } } }
hook global WinSetOption filetype=python %{ evaluate-commands %sh{
if command -v kak-lsp > /dev/null; then
echo hook buffer BufWritePre '.*' lsp-formatting-sync
echo lsp-enable-window
else
if command -v flake8 > /dev/null; then
echo lint-enable
echo set-option buffer lintcmd "'flake8 --filename=* --format=\"%(path)s:%(row)d:%(col)d: error: %(text)s\"'"
echo hook buffer BufWritePost '.*' lint
fi
if command -v black > /dev/null; then
echo set-option buffer formatcmd "'black -'"
echo hook buffer BufWritePre '.*' format
fi
echo jedi-enable-autocomplete
fi
}}
hook global InsertCompletionShow .* %{
try %{
exec -draft 'h<a-K>\h<ret>'
map window insert <s-tab> <c-p>
map window insert <tab> <c-n>
}
}
hook global InsertCompletionHide .* %{
unmap window insert <tab> <c-n>
unmap window insert <s-tab> <c-p>
}
# External commands
evaluate-commands %sh{
if command -v ag > /dev/null; then
echo "set-option global grepcmd 'ag --'"
else
echo "set-option global grepcmd 'grep -RHn --'"
fi
}
# Help
define-command -docstring "show custom help" show-custom-help %{
info -title "Help" %{n:<shift>hjkl select characters
n:xX select lines n:C extend selection down
n:<A-i>(w) select inner (word) n:% select all
n:d delete under cursor n:s split selection
n:<A-d> delete without yank n:t select next character
n:<A-.>. repeat last select + cmd n:t<char> extend to next <char>
n:<C-r> join lines n:<A-i>w*n search next word
n:` swap case n:<C-e> search next word
i:#g goto line number g:<ret> grep jump to
c:eh execute-history i:<A-i> insert snippet
n:<C>+p paste block use tmux and fill-space to create block
Files: ../$project.sess, setup.cfg (pycodestyle, pydocstyle)
}}
map global normal ( -docstring "show custom help" ':show-custom-help<ret>'
# Snippets
map global insert <A-i> -docstring "Execute word before cursor" '<esc>h<A-i>w"xd:snippet-<C-r>x<ret>'
define-command -docstring "set_trace snippet" snippet-ipdb %{ exec "i__import__('ipdb').set_trace()" }
define-command -docstring "sset_trace snippet" snippet-sipdb %{ exec "i__import__('ipdb').sset_trace()" }
# Load kakd
evaluate-commands %sh{
autoload_directory() {
find -L "$1" -type f -name '*\.kak' \
| sed 's/.*/try %{ source "&" } catch %{ echo -debug Autoload: could not load "&" }/'
}
if [ -d "${HOME}/jlf/kakd" ]; then
autoload_directory "${HOME}/jlf/kakd"
fi
}
# Load session
evaluate-commands %sh{
project=$(basename ${PWD})
if [ -f "../${project}.kak" ]; then
echo source "../${project}.kak"
fi
}
#!/usr/bin/env python3
import shlex
import sys
hist = shlex.split(sys.stdin.read())
for h in hist[:2000]:
sys.stdout.write("'%s' " % h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment