diff --git a/rc/autorestore.kak b/rc/autorestore.kak index f505044a..16b54973 100644 --- a/rc/autorestore.kak +++ b/rc/autorestore.kak @@ -1,15 +1,20 @@ ## If set to true, backups will be removed as soon as they have been restored decl bool autorestore_purge_restored true -def -hidden _autorestore-restore-buffer %{ - nop %sh{ +## 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{ buffer_basename="${kak_bufname##*/}" buffer_dirname=$(dirname "${kak_bufname}") ## Find the name of the latest backup created for the buffer that was open - latest_backup_path=$(find "${buffer_dirname}" -maxdepth 1 -type f -readable -name "\.${buffer_basename}\.kak\.*" -printf '%A@/%p\n' 2>/dev/null \ - | sort -n -t. -k1 | sed -nr 's/^.+\///;$p') - test ! -z "${latest_backup_path}" || exit + ## 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') + test ! -z "${latest_backup_path}" || { + echo "eval -draft %{ autorestore-purge-backups }"; + exit; + } ## Replace the content of the buffer with the content of the backup file echo " @@ -24,7 +29,6 @@ def -hidden _autorestore-restore-buffer %{ echo " hook -group autorestore global BufWritePost (.+/)?${kak_bufname} %{ nop %sh{ - echo \"\${kak_opt_autorestore_purge_restored}\" > /tmp/out if [ \"\${kak_opt_autorestore_purge_restored,,}\" = yes \ -o \"\${kak_opt_autorestore_purge_restored,,}\" = true ]; then rm -f '${latest_backup_path}' @@ -36,18 +40,19 @@ def -hidden _autorestore-restore-buffer %{ } ## Remove all the backups that have been created for the current buffer -def autorestore-purge-backups %{ +def autorestore-purge-backups -docstring "Remove all the backups of the current buffer" %{ nop %sh{ buffer_basename="${kak_bufname##*/}" buffer_dirname=$(dirname "${kak_bufname}") - find "${buffer_dirname}" -type f -readable -name ".${buffer_basename}.kak.*" -delete 2>/dev/null + find "${buffer_dirname}" -type f -readable -name "\.${buffer_basename}\.kak\.*" -delete 2>/dev/null } + echo -color 'Information Backup files removed' } ## If for some reason, backup files need to be ignored -def autorestore-disable %{ +def autorestore-disable -docstring "Disable automatic backup recovering" %{ rmhooks global autorestore } -hook -group autorestore global BufCreate .* %{ _autorestore-restore-buffer } +hook -group autorestore global BufCreate .* %{ autorestore-restore-buffer }