Specify encoding of the bookmark file

* lisp/bookmark.el (bookmark-insert-file-format-version-stamp):
Accept an argument CODING and include a 'coding:' cookie in the
bookmark file preamble.
(bookmark-upgrade-file-format-from-0): Call
'bookmark-insert-file-format-version-stamp' with the file buffer's
encoding, as detected when it was read.
(bookmark-file-coding-system): New variable.
(bookmark-load): Set bookmark-file-coding-system to the encoding
of the loaded file.
(bookmark-write-file): Bind coding-system-for-write to either the
user setting via "C-x RET c" or to the existing file encoding,
defaulting to 'utf-8-emacs'.  Update the value of
bookmark-file-coding-system.  (Bug#25365)
This commit is contained in:
Eli Zaretskii 2017-01-07 14:33:41 +02:00
parent 50fd04cd4b
commit e272032769

View file

@ -267,6 +267,8 @@ or the deprecated form (BOOKMARK-NAME PARAM-ALIST).
(defvar bookmarks-already-loaded nil
"Non-nil if and only if bookmarks have been loaded from `bookmark-default-file'.")
(defvar bookmark-file-coding-system nil
"The coding-system of the last loaded or saved bookmark file.")
;; more stuff added by db.
@ -689,7 +691,7 @@ This expects to be called from `point-min' in a bookmark file."
(let* ((old-list (bookmark-alist-from-buffer))
(new-list (bookmark-upgrade-version-0-alist old-list)))
(delete-region (point-min) (point-max))
(bookmark-insert-file-format-version-stamp)
(bookmark-insert-file-format-version-stamp buffer-file-coding-system)
(pp new-list (current-buffer))
(save-buffer))
(goto-char (point-min))
@ -726,11 +728,14 @@ This expects to be called from `point-min' in a bookmark file."
(error "Bookmark file format version strangeness")))))
(defun bookmark-insert-file-format-version-stamp ()
"Insert text indicating current version of bookmark file format."
(defun bookmark-insert-file-format-version-stamp (coding)
"Insert text indicating current version of bookmark file format.
CODING is the symbol of the coding-system in which the file is encoded."
(if (memq (coding-system-base coding) '(undecided prefer-utf-8))
(setq coding 'utf-8-emacs))
(insert
(format ";;;; Emacs Bookmark Format Version %d ;;;;\n"
bookmark-file-format-version))
(format ";;;; Emacs Bookmark Format Version %d ;;;; -*- coding: %S -*- \n"
bookmark-file-format-version (coding-system-base coding)))
(insert ";;; This format is meant to be slightly human-readable;\n"
";;; nevertheless, you probably don't want to edit it.\n"
";;; "
@ -1417,14 +1422,17 @@ for a file, defaulting to the file defined by variable
(with-current-buffer (get-buffer-create " *Bookmarks*")
(goto-char (point-min))
(delete-region (point-min) (point-max))
(let ((print-length nil)
(let ((coding-system-for-write
(or coding-system-for-write
bookmark-file-coding-system 'utf-8-emacs))
(print-length nil)
(print-level nil)
;; See bug #12503 for why we bind `print-circle'. Users
;; can define their own bookmark types, which can result in
;; arbitrary Lisp objects being stored in bookmark records,
;; and some users create objects containing circularities.
(print-circle t))
(bookmark-insert-file-format-version-stamp)
(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
@ -1440,6 +1448,7 @@ for a file, defaulting to the file defined by variable
(condition-case nil
(write-region (point-min) (point-max) file)
(file-error (message "Can't write %s" file)))
(setq bookmark-file-coding-system coding-system-for-write)
(kill-buffer (current-buffer))
(bookmark-maybe-message
"Saving bookmarks to file %s...done" file)))))
@ -1521,7 +1530,8 @@ unique numeric suffixes \"<2>\", \"<3>\", etc."
(expand-file-name bookmark-default-file))
file)
(setq bookmarks-already-loaded t))
(bookmark-bmenu-surreptitiously-rebuild-list))
(bookmark-bmenu-surreptitiously-rebuild-list)
(setq bookmark-file-coding-system buffer-file-coding-system))
(error "Invalid bookmark list in %s" file)))
(kill-buffer (current-buffer)))
(if (null no-msg)