alpha22jp / atomic-chrome

Edit text area on Chrome with Emacs using Atomic Chrome
392 stars 37 forks source link

Allow recovering from when remote editing session closed unexpectedly (e.g. by backups or saving a history) #75

Open meliache opened 10 months ago

meliache commented 10 months ago

So it happened a couple of times that my Firefox tab with some text input was accidentally closed and I was surprised that the associated atomic-chrome buffer closed as well. There was no backup as it's not a file-visiting buffer.

Is there a way so set up temporary backups or at least save a history? Today I added a history-saving function myself to atomic-chrome-edit-done-hook and with a defcustom this could be made opt in:

  (defvar atomic-chrome-history-dir
    (file-name-concat no-littering-var-directory "atomic-chrome" "history")
    "Directory where to save atomic chrome buffer contents.")

  (defun my-atomic-chrome-save-to-file ()
    "Save atomic chrome buffer contents to file."
    (let* ((timestamp (format-time-string "%F_%T"))
           (escaped-buffer-name (file-name-base (make-auto-save-file-name)))
           (atomic-chrome-save-file-name
            (file-name-concat atomic-chrome-history-dir
                              (format "%s_%s" escaped-buffer-name timestamp))))
      (make-directory atomic-chrome-history-dir 'parents)
      (write-region nil nil atomic-chrome-save-file-name nil nil nil 'mustbenew)
      (message "Wrote backup to %s" atomic-chrome-save-file-name)))

  (add-hook 'atomic-chrome-edit-done-hook #'my-atomic-chrome-save-to-file)

It's not a backup as nothing is saved during editing. For that it would be nice somehow utilize Emacs' built-in backup/auto-save functions, but for most of my use-cases that's good enough.

meliache commented 10 months ago

For the history-saving solution, another way compared to the approach above would be to save it in a list or alist and then add this list to desktop-locals-to-save, so that it persists across sessions if the uses uses desktop-save-mode.

eev2 commented 7 months ago

You might be interested in my branch where I implemented a temporary file association with the atomic buffer. The file can be retained on exit by a variable. https://github.com/eev2/atomic-chrome/blob/file-assoc/atomic-chrome.el