Do not restore old backup files. This check is necessary for cases where

- the file was edited with a different editor/program or
- kak didn't restore a backup or
- if old backups weren't purged or
- if autorestore wasn't loaded (e.g. `kak -n`) after backups were generated.
This commit is contained in:
Nicholas Ochiel 2017-09-28 04:06:08 +03:00
parent 89f016d871
commit 040a096f36

View File

@ -7,20 +7,29 @@ def autorestore-restore-buffer -docstring "Restore the backup for the current fi
buffer_basename="${kak_buffile##*/}" buffer_basename="${kak_buffile##*/}"
buffer_dirname=$(dirname "${kak_buffile}") buffer_dirname=$(dirname "${kak_buffile}")
## Find the name of the latest backup created for the buffer that was open if [ -f "${kak_buffile}" ]; then
## NB. kak doesn't automatically cd to current buffer_dirname. newer=$(find "${buffer_dirname}"/".${buffer_basename}.kak."* -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1)
buffer_backup_glob="${buffer_dirname}/.${buffer_basename}.kak.*"
backup_path=$(ls -1t ${buffer_backup_glob} 2>/dev/null | head -n 1)
if [ -z "${backup_path}" ]; then 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 exit
fi fi
printf %s\\n " printf %s\\n "
## Replace the content of the buffer with the content of the backup file ## 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 ## If the backup file has to be removed, issue the command once
## the current buffer has been saved ## 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 ## buffer was restored, do not remove the backup
hook -group autorestore buffer BufWritePost '${kak_buffile}' %{ hook -group autorestore buffer BufWritePost '${kak_buffile}' %{
nop %sh{ nop %sh{
if [ \"\${kak_opt_autorestore_purge_restored}\" = true ]; then if [ \"\${kak_opt_autorestore_purge_restored}\" = true ];
ls -1 '${buffer_dirname}'/.'${buffer_basename}'.kak.* 2>/dev/null | while read -r f; do then
rm -f \"\${f}\" rm -f \"${buffer_dirname}/.${buffer_basename}.kak.\"*
done
fi 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 ## 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" %{ def autorestore-purge-backups -docstring "Remove all the backups of the current buffer" %{
nop %sh{ %sh{
if [ ! -f "${kak_buffile}" ]; then [ ! -f "${kak_buffile}" ] && exit
exit
fi
buffer_basename="${kak_bufname##*/}" buffer_basename="${kak_bufname##*/}"
buffer_dirname=$(dirname "${kak_bufname}") buffer_dirname=$(dirname "${kak_bufname}")
ls -1 "${buffer_dirname}"/."${buffer_basename}".kak.* 2>/dev/null | while read -r f; do rm -f "${buffer_dirname}/.${buffer_basename}.kak."*
rm -f "${f}"
done 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 ## If for some reason, backup files need to be ignored