Subject: bug#25365: 25.1; Coding system for bookmarks and desktop

From: Dmitri Paduchikh <[email-masked]>
Date: Mon, 09 Jan 2017 22:07:49 +0500

Eli Zaretskii <[email-masked]> wrote:

[...]

>> Also setting coding system through coding-system-for-write will make
>> non-encodable characters replaced by spaces.

EZ> This option exists for users who really know what they are doing; the
EZ> default utf-8-emacs encoding is safe for any and all characters Emacs
EZ> supports.

It may be hard to know what is happening if it is designed to happen
automatically. So, if you insist on supporting user choice here, please
consider the following patch. It adds the check of workability of the chosen
codec unless it is given directly by user in which case the usual procedure
applies, or the codec is omnipotent. In the case of encodability problems it
will fallback to utf-8-emacs.

I hope it is trivial enough as I did not sign papers.

diff --git a/lisp/bookmark.el b/lisp/bookmark.el
index e18362bca9..87638cdf1e 100644
--- a/lisp/bookmark.el
+++ b/lisp/bookmark.el
@@ -1432,13 +1432,21 @@ for a file, defaulting to the file defined by variable
           ;; arbitrary Lisp objects being stored in bookmark records,
           ;; and some users create objects containing circularities.
           (print-circle t))
-      (bookmark-insert-file-format-version-stamp coding-system-for-write)
       (insert "(")
       ;; Rather than a single call to `pp' we make one per bookmark.
       ;; Apparently `pp' has a poor algorithmic complexity, so this
       ;; scales a lot better.  bug#4485.
       (dolist (i bookmark-alist) (pp i (current-buffer)))
       (insert ")")
+      ;; Check that coding system is safe unless it is given directly by
+      ;; user or is omnipotent. Silently fallback to utf-8-emacs in a case.
+      (unless (or coding-system-require-warning ; coding is given by user
+                  (memq 'emacs (coding-system-charset-list coding-system-for-write)))
+        (setq coding-system-for-write
+              (select-safe-coding-system (point-min) (point-max)
+                                         (list coding-system-for-write 'utf-8-emacs) t)))
+      (goto-char (point-min))
+      (bookmark-insert-file-format-version-stamp coding-system-for-write)
       (let ((version-control
              (cond
               ((null bookmark-version-control) nil)

With best regards
Dmitri Paduchikh