2015-07-30 17:43:57 +02:00
|
|
|
## If set to true, backups will be removed as soon as they have been restored
|
|
|
|
decl bool autorestore_purge_restored true
|
|
|
|
|
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{
|
2015-07-30 17:43:57 +02:00
|
|
|
buffer_basename="${kak_bufname##*/}"
|
|
|
|
buffer_dirname=$(dirname "${kak_bufname}")
|
|
|
|
|
2015-08-05 16:30:21 +02:00
|
|
|
test ! -f "${kak_bufname}" && exit
|
|
|
|
|
2015-07-30 17:43:57 +02:00
|
|
|
## Find the name of the latest backup created for the buffer that was open
|
2015-08-04 15:56:35 +02:00
|
|
|
## The backup file has to have been last modified more recently than the file we are editing
|
|
|
|
latest_backup_path=$(find "${buffer_dirname}" -maxdepth 1 -type f -readable -newer "${kak_bufname}" -name "\.${buffer_basename}\.kak\.*" -printf '%A@/%p\n' 2>/dev/null \
|
|
|
|
| sort -n -t. -k1 | sed -nr 's/^[^\/]+\///;$p')
|
2015-08-07 10:18:12 +02:00
|
|
|
if [ -z "${latest_backup_path}" ]; then
|
2015-08-04 15:56:35 +02:00
|
|
|
echo "eval -draft %{ autorestore-purge-backups }";
|
|
|
|
exit;
|
2015-08-07 10:18:12 +02:00
|
|
|
fi
|
2015-07-30 17:43:57 +02:00
|
|
|
|
|
|
|
## Replace the content of the buffer with the content of the backup file
|
|
|
|
echo "
|
|
|
|
exec -draft %{ %d!cat<space>${latest_backup_path}<ret>d }
|
2015-08-11 08:25:58 +02:00
|
|
|
echo -color Information 'Backup restored'
|
2015-07-30 17:43:57 +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
|
|
|
|
echo "
|
|
|
|
hook -group autorestore global BufWritePost (.+/)?${kak_bufname} %{
|
|
|
|
nop %sh{
|
2016-04-20 14:25:38 +02:00
|
|
|
if [ \"\${kak_opt_autorestore_purge_restored}\" = true ]; then
|
2015-07-30 17:43:57 +02:00
|
|
|
rm -f '${latest_backup_path}'
|
|
|
|
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{
|
|
|
|
buffer_basename="${kak_bufname##*/}"
|
|
|
|
buffer_dirname=$(dirname "${kak_bufname}")
|
|
|
|
|
2015-08-05 16:30:21 +02:00
|
|
|
test ! -f "${kak_bufname}" && exit
|
|
|
|
|
2015-08-05 14:09:27 +02:00
|
|
|
find "${buffer_dirname}" -maxdepth 1 -type f -readable -name "\.${buffer_basename}\.kak\.*" -delete 2>/dev/null
|
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" %{
|
2015-07-30 17:43:57 +02:00
|
|
|
rmhooks global autorestore
|
|
|
|
}
|
|
|
|
|
2015-08-04 15:56:35 +02:00
|
|
|
hook -group autorestore global BufCreate .* %{ autorestore-restore-buffer }
|