Fanael / persistent-scratch

Preserve the scratch buffer across Emacs sessions
132 stars 14 forks source link

Take care of `buffer-file-coding-system` when saving *scratches* #13

Closed PierreTechoueyres closed 6 years ago

PierreTechoueyres commented 6 years ago

This allow the timer to save scratch buffer when visiting a buffer with another encoding whithout asking the user for an encoding.

Fanael commented 6 years ago

I'm confused, can you explain the problem that your patch is solving?

PierreTechoueyres commented 6 years ago

If your scratch buffer contain special chars ( ex: λ - GREEK SMALL LETTER LAMDA) and you're working in another buffer with a coding system that can't handle it (ex: windows-1252) then the persistent-scratch--autosave-timer fires you're asked to choose another coding system (utf-8). May be it's because I'm on windows and that the default coding system isn't UTF-8. But the patch solve it.

Fanael commented 6 years ago

Can you test this patch, please?

diff --git a/persistent-scratch.el b/persistent-scratch.el
index f559ffe..04ff869 100644
--- a/persistent-scratch.el
+++ b/persistent-scratch.el
@@ -148,7 +148,8 @@ representing the time of the last `persistent-scratch-new-backup' call."
   (let* ((actual-file (or file persistent-scratch-save-file))
          (tmp-file (concat actual-file ".new")))
     (let ((str (persistent-scratch--save-state-to-string))
-          (old-umask (default-file-modes)))
+          (old-umask (default-file-modes))
+          (coding-system-for-write 'utf-8-unix))
       (set-default-file-modes #o600)
       (unwind-protect
           (write-region str nil tmp-file nil 0)
PierreTechoueyres commented 6 years ago

I'll do it. But I'm afraid it'll break the scratch buffer for the Windows users as the default encoding isn't utf-8. Anyway, I'll try it and tell you the result.

PierreTechoueyres commented 6 years ago

Alas, this doesn't work correctly. I've done the following test on a Window machine: 0) apply you patch 1) emacs -q 2) load persistent-scratch.el 3) write (message "Pierre Téchoueyres") which contains an accent 4) open an file with an encoding in utf-8 (or whatever isn't iso-8859-1) 5) M-x persistent-scratch-save 6) quit emacs 7) emacs -q 8) load persistent-scratch.el 9) M-x persistent-scratch-restore

I've got:

;; Pierre Téchoueyres
(message "-*-Pierre Téchoueyres-*-")
Fanael commented 6 years ago

Right, so probably should force UTF-8 when reading too. Can you try this?

diff --git a/persistent-scratch.el b/persistent-scratch.el
index f559ffe..dc2bbd8 100644
--- a/persistent-scratch.el
+++ b/persistent-scratch.el
@@ -148,7 +148,8 @@ representing the time of the last `persistent-scratch-new-backup' call."
   (let* ((actual-file (or file persistent-scratch-save-file))
          (tmp-file (concat actual-file ".new")))
     (let ((str (persistent-scratch--save-state-to-string))
-          (old-umask (default-file-modes)))
+          (old-umask (default-file-modes))
+          (coding-system-for-write 'utf-8-unix))
       (set-default-file-modes #o600)
       (unwind-protect
           (write-region str nil tmp-file nil 0)
@@ -184,7 +185,8 @@ same name as a saved buffer, the contents of that buffer will be overwritten."
   (let ((save-data
          (read
           (with-temp-buffer
-            (insert-file-contents (or file persistent-scratch-save-file))
+            (let ((coding-system-for-read 'utf-8-unix))
+              (insert-file-contents (or file persistent-scratch-save-file)))
             (buffer-string)))))
     (dolist (saved-buffer save-data)
       (with-current-buffer (get-buffer-create (aref saved-buffer 0))
PierreTechoueyres commented 6 years ago

Yes it works but it's somewhat the same thing as the proposed patch, except you've hard coded the encoding where I return and use the one defined in source buffer.

Fanael commented 6 years ago

Yeah, but there may be multiple buffers with different encodings, so it's not really possible to determine the state save file encoding from that.