Merge remote-tracking branch 'nochiel/nochiel-fix-autorestore'

This commit is contained in:
Maxime Coste 2017-10-17 10:30:24 +08:00
commit 209113aa1c

View File

@ -7,20 +7,29 @@ def autorestore-restore-buffer -docstring "Restore the backup for the current fi
buffer_basename="${kak_buffile##*/}"
buffer_dirname=$(dirname "${kak_buffile}")
## Find the name of the latest backup created for the buffer that was open
## NB. kak doesn't automatically cd to current buffer_dirname.
buffer_backup_glob="${buffer_dirname}/.${buffer_basename}.kak.*"
backup_path=$(ls -1t ${buffer_backup_glob} 2>/dev/null | head -n 1)
if [ -f "${kak_buffile}" ]; then
newer=$(find "${buffer_dirname}"/".${buffer_basename}.kak."* -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1)
if [ -z "${backup_path}" ]; then
exit
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: ${backup_path}\"
echo -debug Restoring file: ${newer}
exec -draft %{ %d!cat<space>${backup_path}<ret>d }
exec -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
@ -28,10 +37,9 @@ def autorestore-restore-buffer -docstring "Restore the backup for the current fi
## 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
ls -1 '${buffer_dirname}'/.'${buffer_basename}'.kak.* 2>/dev/null | while read -r f; do
rm -f \"\${f}\"
done
if [ \"\${kak_opt_autorestore_purge_restored}\" = true ];
then
rm -f \"${buffer_dirname}/.${buffer_basename}.kak.\"*
fi
}
}
@ -41,19 +49,18 @@ def autorestore-restore-buffer -docstring "Restore the backup for the current fi
## Remove all the backups that have been created for the current buffer
def autorestore-purge-backups -docstring "Remove all the backups of the current buffer" %{
nop %sh{
if [ ! -f "${kak_buffile}" ]; then
exit
fi
%sh{
[ ! -f "${kak_buffile}" ] && exit
buffer_basename="${kak_bufname##*/}"
buffer_dirname=$(dirname "${kak_bufname}")
ls -1 "${buffer_dirname}"/."${buffer_basename}".kak.* 2>/dev/null | while read -r f; do
rm -f "${f}"
done
rm -f "${buffer_dirname}/.${buffer_basename}.kak."*
printf %s\\n "
echo -markup {Information}Backup files removed.
"
}
echo -markup '{Information}Backup files removed'
}
## If for some reason, backup files need to be ignored