Subject: Re: bug#12507: [debbugs-tracker] Processed: severity 12507 wishlist

From: Thierry Volpiatto <[email-masked]>
Date: Thu, 27 Sep 2012 23:16:12 +0200

"Drew Adams" <[email-masked]> writes:

>> >>   ;; Don't use write-file; we don't want this buffer to visit it.
>> >
>> > After write-file, the buffer is marked as visiting that file, which
>> > affects the behavior of C-x C-f and a lot more (e.g. asks the user
>> > for confirmation if the file was modified by some other process, ...).
>>
>> What about improving write-region to use backup when needed?
>> Possibly writing a new write-region-something function that handle
>> backup, or a write-file-noselect function.
>
> +1
>
> And please let us know how best to accomplish that (in the doc perhaps, but also
> in this thread).
>
> It's not clear to me how to make a backup copy of a file without visiting that
> file in some buffer, however temporarily.
>
> For example, I can imagine this as a way to append the region to a file and back
> it up:
>
>  (write-region (point-min) (point-max) FILE 'append)
>  (with-current-buffer (find-file-noselect FILE) (backup-buffer))
>
> But IIUC `find-file-noselect' visits the buffer (and so "asks the user for
> confirmation if the file was modified by some other process").  So that's
> apparently not the way to go.  What is?
>
> Leaving the question of visiting aside for the moment, what about
> `backup-buffer' here?  Should it be `save-buffer' instead, so that the modes of
> FILE get updated properly?  Should it be just `basic-save-buffer-1' instead of
> `save-buffer'?
>
> And should any such code take what Juri mentioned wrt vc into account?  If so,
> how?
>
> It's not clear to me how best to handle this
> write-stuff-to-a-file-and-back-it-up-when-appropriate, but I (and perhaps
> others) would like to learn.  I haven't found the answer by looking at the
> manuals or perusing the source code.  Can you help?
Will try to look better to this tomorrow if I find time, just for
`bookmark-write-file', what about something like this?

diff --git a/lisp/bookmark.el b/lisp/bookmark.el
--- a/lisp/bookmark.el
+++ b/lisp/bookmark.el
@@ -1355,7 +1355,7 @@
 (defun bookmark-write-file (file)
   "Write `bookmark-alist' to FILE."
   (bookmark-maybe-message "Saving bookmarks to file %s..." file)
-  (with-current-buffer (get-buffer-create " *Bookmarks*")
+  (with-current-buffer (find-file-noselect file)
     (goto-char (point-min))
     (delete-region (point-min) (point-max))
     (let ((print-length nil)
@@ -1374,7 +1374,7 @@
               ((eq 'nospecial bookmark-version-control) version-control)
               (t t))))
         (condition-case nil
-            (write-region (point-min) (point-max) file)
+            (save-buffer)
           (file-error (message "Can't write %s" file)))
         (kill-buffer (current-buffer))
         (bookmark-maybe-message

-- 
  Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997