diff options
| author | Nicholas Ochiel <nochiel@users.noreply.github.com> | 2017-09-28 04:06:08 +0300 |
|---|---|---|
| committer | Nicholas Ochiel <nochiel@users.noreply.github.com> | 2017-10-12 15:33:21 +0300 |
| commit | 040a096f36790aeef8b08868e3716b2771e84a91 (patch) | |
| tree | dded7d51a78cb5550faf060ccd7289a3fcb59ab6 | |
| parent | 89f016d871c9ccc516e244e8bc594defe3678daf (diff) | |
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.
| -rw-r--r-- | rc/extra/autorestore.kak | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/rc/extra/autorestore.kak b/rc/extra/autorestore.kak index af4e8c07..0db6d3b8 100644 --- a/rc/extra/autorestore.kak +++ b/rc/extra/autorestore.kak @@ -7,20 +7,29 @@ def autorestore-restore-buffer -docstring "Restore the backup for the current fi buffer_basename="${kak_buffile##*/}" buffer_dirname=$(dirname "${kak_buffile}") - ## Find the name of the latest backup created for the buffer that was open - ## NB. kak doesn't automatically cd to current buffer_dirname. - buffer_backup_glob="${buffer_dirname}/.${buffer_basename}.kak.*" - backup_path=$(ls -1t ${buffer_backup_glob} 2>/dev/null | head -n 1) + if [ -f "${kak_buffile}" ]; then + newer=$(find "${buffer_dirname}"/".${buffer_basename}.kak."* -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1) - if [ -z "${backup_path}" ]; then - exit + 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 fi printf %s\\n " ## 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 ## 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 hook -group autorestore buffer BufWritePost '${kak_buffile}' %{ nop %sh{ - if [ \"\${kak_opt_autorestore_purge_restored}\" = true ]; then - ls -1 '${buffer_dirname}'/.'${buffer_basename}'.kak.* 2>/dev/null | while read -r f; do - rm -f \"\${f}\" - done + if [ \"\${kak_opt_autorestore_purge_restored}\" = true ]; + then + rm -f \"${buffer_dirname}/.${buffer_basename}.kak.\"* 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 def autorestore-purge-backups -docstring "Remove all the backups of the current buffer" %{ - nop %sh{ - if [ ! -f "${kak_buffile}" ]; then - exit - fi + %sh{ + [ ! -f "${kak_buffile}" ] && exit buffer_basename="${kak_bufname##*/}" buffer_dirname=$(dirname "${kak_bufname}") - ls -1 "${buffer_dirname}"/."${buffer_basename}".kak.* 2>/dev/null | while read -r f; do - rm -f "${f}" - done + rm -f "${buffer_dirname}/.${buffer_basename}.kak."* + + 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 |
