kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

autorestore.kak (3464B)


      1 declare-option -docstring %{
      2         Remove backups once they've been restored
      3 
      4         See `:doc autorestore` for details.
      5     } \
      6     bool autorestore_purge_restored true
      7 
      8 ## Insert the content of the backup file into the current buffer, if a suitable one is found
      9 define-command autorestore-restore-buffer \
     10     -docstring %{
     11         Restore the backup for the current file if it exists
     12 
     13         See `:doc autorestore` for details.
     14     } \
     15 %{
     16     evaluate-commands %sh{
     17         buffer_basename="${kak_buffile##*/}"
     18         buffer_dirname=$(dirname "${kak_buffile}")
     19         backup_prefix="${buffer_dirname}"/".${buffer_basename}.kak."
     20 
     21         if [ -f "${kak_buffile}" ]; then
     22             newer=$(find "${backup_prefix}"* -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1)
     23             older=$(find "${backup_prefix}"* \! -newer "${kak_buffile}" -exec ls -1t {} + 2>/dev/null | head -n 1)
     24         else
     25             # New buffers that were never written to disk.
     26             newer=$(ls -1t "${backup_prefix}"* 2>/dev/null | head -n 1)
     27             older=""
     28         fi
     29 
     30         if [ -z "${newer}" ]; then
     31             if [ -n "${older}" ]; then
     32                 printf "echo -debug 'Old backup file(s) found: will not restore %s.'" "$(printf %s "${older}" | sed s/\'/\'\'/g)"
     33             fi
     34             exit
     35         fi
     36 
     37         # ensure backup suffix only contains portable filename characters
     38         case "$(printf %s "${newer}" | cut -b ${#backup_prefix}-)" in
     39             *[!A-Za-z0-9_.-]*)
     40                 printf "echo -debug 'backup file suffix contains unexpected characters %s, ignored.'" "$(printf %s "${newer}" | sed s/\'/\'\'/g)"
     41                 exit
     42                 ;;
     43         esac
     44 
     45         printf %s\\n "
     46             ## Replace the content of the buffer with the content of the backup file
     47             echo -debug Restoring file: ${newer}
     48 
     49             execute-keys -draft %{%d!cat<space>\"${newer}\"<ret>jd}
     50 
     51             ## If the backup file has to be removed, issue the command once
     52             ## the current buffer has been saved
     53             ## If the autorestore_purge_restored option has been unset right after the
     54             ## buffer was restored, do not remove the backup
     55             hook -group autorestore buffer BufWritePost '${kak_buffile}' %{
     56                 nop %sh{
     57                     if [ \"\${kak_opt_autorestore_purge_restored}\" = true ];
     58                     then
     59                         rm -f \"${buffer_dirname}/.${buffer_basename}.kak.\"*
     60                     fi
     61                 }
     62             }
     63         "
     64     }
     65 }
     66 
     67 ## Remove all the backups that have been created for the current buffer
     68 define-command autorestore-purge-backups \
     69     -docstring %{
     70         Remove all the backups of the current buffer
     71 
     72         See `:doc autorestore` for details.
     73     } \
     74 %{
     75     evaluate-commands %sh{
     76         [ ! -f "${kak_buffile}" ] && exit
     77 
     78         buffer_basename="${kak_bufname##*/}"
     79         buffer_dirname=$(dirname "${kak_bufname}")
     80 
     81         rm -f "${buffer_dirname}/.${buffer_basename}.kak."*
     82 
     83         printf %s\\n "
     84             echo -markup {Information}Backup files removed.
     85             "
     86     }
     87 }
     88 
     89 ## If for some reason, backup files need to be ignored
     90 define-command autorestore-disable \
     91     -docstring %{
     92         Disable automatic backup recovering
     93 
     94         See `:doc autorestore` for details.
     95     } \
     96 %{
     97     remove-hooks global autorestore
     98 }
     99 
    100 hook -group autorestore global BufCreate .* %{ autorestore-restore-buffer }