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

From: Thierry Volpiatto <[email-masked]>
Date: Fri, 28 Sep 2012 11:04:34 +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))
This is not efficient IMO, like the actual version of
`bookmark-write-file':

1) create a new buffer named "*Bookmarks*".
2) erase buffer
3) write data to it.
4) write contents of this buffer to FILE.
5) save this FILE.

instead:

1) open FILE buffer
2) erase buffer
3) write data to it
4) save 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?
What is the problem for this?
What if you open another emacs session, bookmark something in this
session, (don't save and don't quit session) switch to the initial
session, bookmark something there and save bookmarks?
It is good if it ask you for confirmation at some point, no? 

> 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'?
`save-buffer' do all the job (saving and backing up), so why writing to
buffer and then using `backup-buffer'

> And should any such code take what Juri mentioned wrt vc into account?  If so,
> how?
`save-buffer' handle that.

> 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?

I think that doing like in the patch I sent is not too bad:

(with-current-buffer (find-file-noselect file)
   write--data--here
   [...]
   (save-buffer))

No more is needed I think, but maybe I miss something?

Also, the use of `bookmark-version-control' is questionable, why
handling this file specially?
If following that, we should have a special variable for .emacs-custom.el,
desktop, history, etc... which is non--sense.

If this variable is removed, the global value of version-control will be
used and .emacs.bmk will be backed up like any other file,
like it does (or should do because it is broken with write-region) with
its default value 'nospecial.

Note that with the patch I sent, it seems a little bit faster to save,
but maybe I am wrong, need to verify. (we write one time the data and
save instead of writing two time and save)

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