dotfiles/.config/kak/autoload/file-utils.kak

76 lines
2.1 KiB
Text

define-command -docstring %{
move-file <name>: move the file on the filesystem and the open buffer.
} -params 1 move-file %{
nop %sh{
old="${kak_bufname}"
echo "rename-buffer $1 ; write" >$kak_command_fifo
rm "$old"
}
}
alias global mv move-file
define-command -docstring %{
rename-file <name>: rename the file on filesystem and buffer,
keeping the same directory.
} -params 1 rename-file %{
nop %sh{
dir="${kak_buffile%/*}"
# base="${kak_buffile##/*}"
new="${dir}/$1"
echo "rename-buffer $new ; write" >$kak_command_fifo
rm "$kak_buffile"
}
}
alias global rf rename-file
define-command -docstring %{
remove-file [<switches>] [<name>]: remove the file from the filesystem
and close the buffer. Omit <name> to remove the current file.
Switches:
-f remove even if buffer has unsaved changes
} -params 0..2 remove-file %{
evaluate-commands %sh{
if [ "$1" = "-f" ]; then
force="true"
shift
fi
if [ -z "$1" ]; then
targ="$kak_buffile"
targ_bname="$kak_bufname"
else
targ="$(realpath ${1})"
targ_bname="$1"
fi
if [ "$kak_modified" = "true" ]; then
if [ "$force" != "true" ]; then
echo "fail 'Refusing to remove modified buffer without -f switch.'"
else
echo "try %{ delete-buffer! $targ_bname }" >$kak_command_fifo
rm "$targ"
fi
else
echo "try %{ delete-buffer $targ_bname }" >$kak_command_fifo
rm "$targ"
fi
}
}
define-command -docstring %{
mkdir [<args>]: create directory
If no arguments, parents of current file are ensured.
Else, all args passed to mkdir.
} mkdir -params 0.. %{
evaluate-commands %sh{
if [ "$#" -eq 0 ]; then
# ensure current file parent
mkdir -p "$(dirname -- "$kak_buffile")"
else
# pass to mkdir
mkdir $@
fi
}
}
alias global rm remove-file
complete-command remove-file file