2015-07-30 18:43:57 +03: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 16:56:35 +03: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{
|
2016-10-07 10:57:45 +03:00
|
|
|
test ! -f "${kak_buffile}" && exit
|
2015-07-30 18:43:57 +03:00
|
|
|
|
2016-10-07 10:57:45 +03:00
|
|
|
buffer_basename=$(basename "${kak_buffile}")
|
|
|
|
buffer_dirname=$(dirname "${kak_buffile}")
|
2015-08-05 17:30:21 +03:00
|
|
|
|
2015-07-30 18:43:57 +03:00
|
|
|
## Find the name of the latest backup created for the buffer that was open
|
2015-08-04 16:56:35 +03:00
|
|
|
## The backup file has to have been last modified more recently than the file we are editing
|
2016-10-07 00:34:22 +03:00
|
|
|
## Other backups are removed
|
2016-10-17 23:31:56 +03:00
|
|
|
backup_path=$(find "${buffer_dirname}" -maxdepth 1 -type f -name "\.${buffer_basename}\.kak\.*" \
|
|
|
|
\( \( -newer "${kak_buffile}" -exec ls -1t {} + \) -o \( -exec rm {} \; \) \) 2>/dev/null)
|
2016-10-07 00:34:22 +03:00
|
|
|
|
|
|
|
if [ -z "${backup_path}" ]; then
|
2016-04-23 08:47:01 +03:00
|
|
|
exit
|
2015-08-07 11:18:12 +03:00
|
|
|
fi
|
2015-07-30 18:43:57 +03:00
|
|
|
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "
|
2016-10-07 00:34:22 +03:00
|
|
|
## Replace the content of the buffer with the content of the backup file
|
|
|
|
exec -draft %{ %d!cat<space>${backup_path}<ret>d }
|
2015-08-11 09:25:58 +03:00
|
|
|
echo -color Information 'Backup restored'
|
2015-07-30 18:43:57 +03:00
|
|
|
|
2016-10-07 00:34:22 +03: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 10:57:45 +03:00
|
|
|
hook -group autorestore buffer BufWritePost '${kak_buffile}' %{
|
2015-07-30 18:43:57 +03:00
|
|
|
nop %sh{
|
2016-04-20 15:25:38 +03:00
|
|
|
if [ \"\${kak_opt_autorestore_purge_restored}\" = true ]; then
|
2016-10-07 00:34:22 +03:00
|
|
|
rm -f '${backup_path}'
|
2015-07-30 18:43:57 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## Remove all the backups that have been created for the current buffer
|
2015-08-04 16:56:35 +03:00
|
|
|
def autorestore-purge-backups -docstring "Remove all the backups of the current buffer" %{
|
2015-07-30 18:43:57 +03:00
|
|
|
nop %sh{
|
2016-10-07 10:57:45 +03:00
|
|
|
test ! -f "${kak_buffile}" && exit
|
2015-07-30 18:43:57 +03:00
|
|
|
|
2016-10-07 10:57:45 +03:00
|
|
|
buffer_basename=$(basename "${kak_bufname}")
|
|
|
|
buffer_dirname=$(dirname "${kak_bufname}")
|
2015-08-05 17:30:21 +03:00
|
|
|
|
2015-08-05 15:09:27 +03:00
|
|
|
find "${buffer_dirname}" -maxdepth 1 -type f -readable -name "\.${buffer_basename}\.kak\.*" -delete 2>/dev/null
|
2015-07-30 18:43:57 +03:00
|
|
|
}
|
2015-08-11 09:25:58 +03:00
|
|
|
echo -color Information 'Backup files removed'
|
2015-07-30 18:43:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
## If for some reason, backup files need to be ignored
|
2015-08-04 16:56:35 +03:00
|
|
|
def autorestore-disable -docstring "Disable automatic backup recovering" %{
|
2015-07-30 18:43:57 +03:00
|
|
|
rmhooks global autorestore
|
|
|
|
}
|
|
|
|
|
2016-10-07 10:57:45 +03:00
|
|
|
hook -group autorestore global BufOpen .* %{ autorestore-restore-buffer }
|