2017-05-16 13:35:43 +02:00
|
|
|
decl -docstring "remove backups once they've been restored" \
|
|
|
|
bool autorestore_purge_restored true
|
2015-07-30 17:43:57 +02:00
|
|
|
|
2015-08-04 15:56:35 +02:00
|
|
|
## Insert the content of the backup file into the current buffer, if a suitable one is found
|
|
|
|
def autorestore-restore-buffer -docstring "Restore the backup for the current file if it exists" %{
|
|
|
|
%sh{
|
2017-02-24 12:26:27 +01:00
|
|
|
buffer_basename="${kak_buffile##*/}"
|
2016-10-07 09:57:45 +02:00
|
|
|
buffer_dirname=$(dirname "${kak_buffile}")
|
2015-08-05 16:30:21 +02:00
|
|
|
|
2015-07-30 17:43:57 +02:00
|
|
|
## Find the name of the latest backup created for the buffer that was open
|
2017-03-01 12:55:29 +01:00
|
|
|
backup_path=$(ls -1t ."${kak_bufname}".kak.* 2>/dev/null | head -n 1)
|
2016-10-06 23:34:22 +02:00
|
|
|
|
|
|
|
if [ -z "${backup_path}" ]; then
|
2016-04-23 07:47:01 +02:00
|
|
|
exit
|
2015-08-07 10:18:12 +02:00
|
|
|
fi
|
2015-07-30 17:43:57 +02:00
|
|
|
|
2016-04-23 07:47:01 +02:00
|
|
|
printf %s\\n "
|
2016-10-06 23:34:22 +02:00
|
|
|
## Replace the content of the buffer with the content of the backup file
|
|
|
|
exec -draft %{ %d!cat<space>${backup_path}<ret>d }
|
2015-07-30 17:43:57 +02:00
|
|
|
|
2016-10-06 23:34:22 +02:00
|
|
|
## 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
|
2016-10-07 09:57:45 +02:00
|
|
|
hook -group autorestore buffer BufWritePost '${kak_buffile}' %{
|
2015-07-30 17:43:57 +02:00
|
|
|
nop %sh{
|
2016-04-20 14:25:38 +02:00
|
|
|
if [ \"\${kak_opt_autorestore_purge_restored}\" = true ]; then
|
2017-03-01 12:55:29 +01:00
|
|
|
ls -1 '${buffer_dirname}'/.'${buffer_basename}'.kak.* 2>/dev/null | while read -r f; do
|
2017-02-24 12:26:27 +01:00
|
|
|
rm -f \"\${f}\"
|
|
|
|
done
|
2015-07-30 17:43:57 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## Remove all the backups that have been created for the current buffer
|
2015-08-04 15:56:35 +02:00
|
|
|
def autorestore-purge-backups -docstring "Remove all the backups of the current buffer" %{
|
2015-07-30 17:43:57 +02:00
|
|
|
nop %sh{
|
2017-02-24 12:26:27 +01:00
|
|
|
if [ ! -f "${kak_buffile}" ]; then
|
|
|
|
exit
|
|
|
|
fi
|
2015-07-30 17:43:57 +02:00
|
|
|
|
2017-02-24 12:26:27 +01:00
|
|
|
buffer_basename="${kak_bufname##*/}"
|
2016-10-07 09:57:45 +02:00
|
|
|
buffer_dirname=$(dirname "${kak_bufname}")
|
2015-08-05 16:30:21 +02:00
|
|
|
|
2017-03-01 12:55:29 +01:00
|
|
|
ls -1 "${buffer_dirname}"/."${buffer_basename}".kak.* 2>/dev/null | while read -r f; do
|
2017-02-24 12:26:27 +01:00
|
|
|
rm -f "${f}"
|
|
|
|
done
|
2015-07-30 17:43:57 +02:00
|
|
|
}
|
2015-08-11 08:25:58 +02:00
|
|
|
echo -color Information 'Backup files removed'
|
2015-07-30 17:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
## If for some reason, backup files need to be ignored
|
2015-08-04 15:56:35 +02:00
|
|
|
def autorestore-disable -docstring "Disable automatic backup recovering" %{
|
2017-01-04 01:07:45 +01:00
|
|
|
remove-hooks global autorestore
|
2015-07-30 17:43:57 +02:00
|
|
|
}
|
|
|
|
|
2017-02-24 12:26:27 +01:00
|
|
|
hook -group autorestore global BufCreate .* %{ autorestore-restore-buffer }
|