mirror of
https://github.com/mawww/kakoune.git
synced 2024-12-26 13:02:01 +03:00
ec16969609
Automatic reparsing of %sh{...}, while convenient in many cases, can be surprising as well, and can lead to security problems: 'echo %sh{ printf "foo\necho bar" }' runs 'echo foo', then 'echo bar'. we make this danger explicit, and we fix the 'nop %sh{...}' pattern. To reparse %sh{...} strings, they can be passed to evaluate-commands, which has been fixed to work in every cases where %sh{...} reparsing was used..
72 lines
2.8 KiB
Plaintext
72 lines
2.8 KiB
Plaintext
declare-option -docstring "remove backups once they've been restored" \
|
|
bool autorestore_purge_restored true
|
|
|
|
## Insert the content of the backup file into the current buffer, if a suitable one is found
|
|
define-command autorestore-restore-buffer -docstring "Restore the backup for the current file if it exists" %{
|
|
evaluate-commands %sh{
|
|
buffer_basename="${kak_buffile##*/}"
|
|
buffer_dirname=$(dirname "${kak_buffile}")
|
|
|
|
if [ -f "${kak_buffile}" ]; then
|
|
newer=$(find "${buffer_dirname}"/".${buffer_basename}.kak."* -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1)
|
|
|
|
older=$(find "${buffer_dirname}"/".${buffer_basename}.kak."* \! -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1)
|
|
else
|
|
# New buffers that were never written to disk.
|
|
newer=$(ls -1t "${buffer_dirname}"/".${buffer_basename}.kak."* 2>/dev/null | head -n 1)
|
|
fi
|
|
|
|
if [ -z "${newer}" ]; then
|
|
if [ -n "${older}" ]; then
|
|
printf %s\\n "
|
|
echo -debug Old backup file(s) found: will not restore ${older} .
|
|
"
|
|
fi
|
|
exit
|
|
fi
|
|
|
|
printf %s\\n "
|
|
## Replace the content of the buffer with the content of the backup file
|
|
echo -debug Restoring file: ${newer}
|
|
|
|
execute-keys -draft %{ %d!cat<space>\"${newer}\"<ret>d }
|
|
|
|
## If the backup file has to be removed, issue the command once
|
|
## the current buffer has been saved
|
|
## If the autorestore_purge_restored option has been unset right after the
|
|
## buffer was restored, do not remove the backup
|
|
hook -group autorestore buffer BufWritePost '${kak_buffile}' %{
|
|
nop %sh{
|
|
if [ \"\${kak_opt_autorestore_purge_restored}\" = true ];
|
|
then
|
|
rm -f \"${buffer_dirname}/.${buffer_basename}.kak.\"*
|
|
fi
|
|
}
|
|
}
|
|
"
|
|
}
|
|
}
|
|
|
|
## Remove all the backups that have been created for the current buffer
|
|
define-command autorestore-purge-backups -docstring "Remove all the backups of the current buffer" %{
|
|
evaluate-commands %sh{
|
|
[ ! -f "${kak_buffile}" ] && exit
|
|
|
|
buffer_basename="${kak_bufname##*/}"
|
|
buffer_dirname=$(dirname "${kak_bufname}")
|
|
|
|
rm -f "${buffer_dirname}/.${buffer_basename}.kak."*
|
|
|
|
printf %s\\n "
|
|
echo -markup {Information}Backup files removed.
|
|
"
|
|
}
|
|
}
|
|
|
|
## If for some reason, backup files need to be ignored
|
|
define-command autorestore-disable -docstring "Disable automatic backup recovering" %{
|
|
remove-hooks global autorestore
|
|
}
|
|
|
|
hook -group autorestore global BufCreate .* %{ autorestore-restore-buffer }
|