2020-09-04 12:20:50 +03:00
|
|
|
declare-option -docstring %{
|
|
|
|
Remove backups once they've been restored
|
|
|
|
|
|
|
|
See `:doc autorestore` for details.
|
|
|
|
} \
|
2017-05-16 14:35:43 +03:00
|
|
|
bool autorestore_purge_restored true
|
2015-07-30 18:43:57 +03:00
|
|
|
|
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
|
2020-09-04 12:20:50 +03:00
|
|
|
define-command autorestore-restore-buffer \
|
|
|
|
-docstring %{
|
|
|
|
Restore the backup for the current file if it exists
|
|
|
|
|
|
|
|
See `:doc autorestore` for details.
|
|
|
|
} \
|
|
|
|
%{
|
2018-05-07 00:29:52 +03:00
|
|
|
evaluate-commands %sh{
|
2017-02-24 14:26:27 +03:00
|
|
|
buffer_basename="${kak_buffile##*/}"
|
2016-10-07 10:57:45 +03:00
|
|
|
buffer_dirname=$(dirname "${kak_buffile}")
|
2015-08-05 17:30:21 +03:00
|
|
|
|
2017-09-28 04:06:08 +03:00
|
|
|
if [ -f "${kak_buffile}" ]; then
|
|
|
|
newer=$(find "${buffer_dirname}"/".${buffer_basename}.kak."* -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1)
|
|
|
|
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)
|
2020-08-23 19:55:50 +03:00
|
|
|
older=""
|
2017-09-28 04:06:08 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${newer}" ]; then
|
|
|
|
if [ -n "${older}" ]; then
|
|
|
|
printf %s\\n "
|
|
|
|
echo -debug Old backup file(s) found: will not restore ${older} .
|
|
|
|
"
|
|
|
|
fi
|
2019-03-14 00:00:59 +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
|
2017-09-28 04:06:08 +03:00
|
|
|
echo -debug Restoring file: ${newer}
|
2017-09-26 23:52:11 +03:00
|
|
|
|
2022-04-24 20:56:32 +03:00
|
|
|
execute-keys -draft %{%d!cat<space>\"${newer}\"<ret>jd}
|
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{
|
2017-09-28 04:06:08 +03:00
|
|
|
if [ \"\${kak_opt_autorestore_purge_restored}\" = true ];
|
|
|
|
then
|
2019-03-14 00:00:59 +03:00
|
|
|
rm -f \"${buffer_dirname}/.${buffer_basename}.kak.\"*
|
2015-07-30 18:43:57 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## Remove all the backups that have been created for the current buffer
|
2020-09-04 12:20:50 +03:00
|
|
|
define-command autorestore-purge-backups \
|
|
|
|
-docstring %{
|
|
|
|
Remove all the backups of the current buffer
|
|
|
|
|
|
|
|
See `:doc autorestore` for details.
|
|
|
|
} \
|
|
|
|
%{
|
2018-05-07 00:29:52 +03:00
|
|
|
evaluate-commands %sh{
|
2017-09-28 04:06:08 +03:00
|
|
|
[ ! -f "${kak_buffile}" ] && exit
|
2015-07-30 18:43:57 +03:00
|
|
|
|
2017-02-24 14:26:27 +03:00
|
|
|
buffer_basename="${kak_bufname##*/}"
|
2016-10-07 10:57:45 +03:00
|
|
|
buffer_dirname=$(dirname "${kak_bufname}")
|
2015-08-05 17:30:21 +03:00
|
|
|
|
2017-09-28 04:06:08 +03:00
|
|
|
rm -f "${buffer_dirname}/.${buffer_basename}.kak."*
|
|
|
|
|
|
|
|
printf %s\\n "
|
|
|
|
echo -markup {Information}Backup files removed.
|
|
|
|
"
|
2015-07-30 18:43:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## If for some reason, backup files need to be ignored
|
2020-09-04 12:20:50 +03:00
|
|
|
define-command autorestore-disable \
|
|
|
|
-docstring %{
|
|
|
|
Disable automatic backup recovering
|
|
|
|
|
|
|
|
See `:doc autorestore` for details.
|
|
|
|
} \
|
|
|
|
%{
|
2017-01-04 03:07:45 +03:00
|
|
|
remove-hooks global autorestore
|
2015-07-30 18:43:57 +03:00
|
|
|
}
|
|
|
|
|
2017-02-24 14:26:27 +03:00
|
|
|
hook -group autorestore global BufCreate .* %{ autorestore-restore-buffer }
|