10

I wanted to look at different portions of a buffer in two different frames, as I have two monitors of different shapes that I can't easily span a single frame over. Some googling led me to use clone-indirect-buffer, which works nicely.

However, buffer-file-name for the new buffer is nil. How can I reliably determine the filename of the original buffer (that this one is a clone of)?

rneatherway
  • 483
  • 3
  • 11
  • 2
    What's the difference/advantage to cloning the buffer and simply visiting the buffer in both frames simultaneously? – Nathanael Farley Nov 05 '14 at 15:59
  • 6
    Clones respond to events like narrowing/widening, code folding, and outline folding independently of each other. If it were the same buffer in two windows, these sorts of events would be mirrored in each window. Clones are helpful for, eg, having different views of different parts of the buffer at the same time. – Dan Nov 05 '14 at 16:08
  • I'm not actually sure if you can bring up the buffer in two frames simultaneously. Of course you can in two windows in the same frame, but if I try to change to the buffer in another frame with `C-x b` then focus switches to the frame already showing the buffer. – rneatherway Nov 05 '14 at 17:10
  • 1
    @NathanaelFarley I use indirect buffers when I'm terminal sharing with my vim inclined colleges who can use the indirect buffer with something like evil-mode. – stsquad Nov 05 '14 at 17:53

2 Answers2

14

I keep the Emacs manual and Elisp manual on speed dial for questions like these.

From the Elisp manual on indirect buffers:

Function: buffer-base-buffer &optional buffer

This function returns the base buffer of buffer, which defaults to the current buffer. If buffer is not indirect, the value is nil. Otherwise, the value is another buffer, which is never an indirect buffer.

You probably want (buffer-file-name (buffer-base-buffer)) or something like that.

purple_arrows
  • 2,373
  • 10
  • 19
4

This is an indirect answer to your question -- I'm not sure how to figure out what the original buffer's filename is without doing the following. (EDIT: @purple_arrows's answer fills in the details: you can use buffer-base-buffer. Please accept that answer as most correct.)

The following piece of advice will associate the original buffer's filename with each of its indirect clones:

(defadvice clone-indirect-buffer (around associate-filename activate)
  "Associate the original filename with the cloned buffer."
  (let ((filename buffer-file-name))
    ad-do-it
    (setq buffer-file-name filename)))

Now, each of the clones has the same filename as the original buffer.

I use indirectly-cloned buffers a lot, and like to be able to, eg, save the associated file when I'm working with any of the buffers, rather than the original. This piece of advice was my solution to that desired workflow.

Dan
  • 32,584
  • 6
  • 98
  • 168