5

Why does clone-buffer not work on file buffers.

I can make it work by the following advice:

(advice-add 'clone-buffer :around
  (lambda (oldfun &rest args)
     "Allow cloning file buffers."
     (interactive "bName of new cloned buffer: ")
     (let (buffer-file-name)
       (apply oldfun args))))

The clone is a non-file buffer. One can modify it and save it into a new file with save-buffer.

Everything is working quite fine. So why does the original command clone-buffer test whether the current buffer is a file buffer and disallow cloning in that case?


My current best guess for the motivation of the restriction: The change from a file buffer to a non-file buffer is quite hard.

Examples:

  • Version control like vc-git does not work anymore.
  • compile-command compiles the original buffer file and not the clone
  • 1
    Maybe to provide a layer of protection against users over-writing the original file with the modified clone? It wouldn't be difficult (for me at least) to get confused and choose the same file name as the original to save the clone to. The alternative workflow, starting with writing the original to a new file name, is arguably safer in this respect, since you establish the correct file-buffer association before making any changes to either file. – Tyler Jan 23 at 18:18
  • 3
    You might get an answer for such a design question here. But I'd probably ask it at emacs-devel@gnu.org if I were you. An answer here will likely either be just a guess, unless someone finds an explicit reference to this somewhere. – Drew Jan 23 at 18:46
  • 1
    Interesting question! It looks like you can use clone-buffer on a file-visiting buffer created with clone-indirect-buffer. Does cloning from an indirect buffer fix the issues with vc-git and compile-command? – Melioratus Jan 23 at 19:39
  • 1
    @Tyler In my use-case I clone the buffer and heavily modify the text for analysis purposes. I don't intent to write the clone back to a file.If I know that I want to save the clone to a file I would rather use write-buffer and find-file the old one when I need it again. Alternatively one could insert the buffer-string into a new file buffer if it is clear that one wants to keep the original buffer. – Tobias Jan 24 at 3:18
  • 1
    @Melioratus Nice observation. It indicates that clone-indirect-buffer is also faulty. It keeps vc mode despite it is not working there (buffer-file-name is nil). So it seems vc is not really the reason for restricting clone-buffer to non-file buffers. – Tobias Jan 24 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.