Sotanaht
Sotanaht

Reputation: 177

How can I open a new frame which always displays the active buffer of its parent frame in emacs?

I want to have a shortcut to create a new frame in emacs which will always display the active buffer of the frame that created it. This way the frame could be moved to another workspace to keep as sort of a small preview of another emacs frame.

The following is my attempt at a function that will get the new frame to always show the parent frame's buffer. get-active-buffer is pseudo-code, but aside from that the rest should do the trick.

(defun display-active-buffer (parent-frame)
  (while t
    (let ((active-buffer (get-current-buffer parent-frame)))
      (cond (/= active-buffer (current-buffer))
        (switch-to-buffer-other-frame active-buffer)))))

Really I mostly just want to know what sort of function there is like get-current-buffer, which would act like current-buffer, except it would take a frame as an argument, instead of just using the current frame. I also would need to figure out some way to pass the name of the parent-frame to this function. I'm also not sure how smart it is to be using an infinite while loop for this though. If anybody knows of a better way, please tell.

I feel like this could be a really useful feature to add to emacs, and so if I figure all of this out I'll be sure to edit this post to add my finished code for anybody to use.

Upvotes: 1

Views: 358

Answers (1)

re5et
re5et

Reputation: 4275

I have no idea why you would want to do this, but I think this does what you described:

(defun make-frame-synced-to-current-frame-current-buffer ()
  (interactive)
  (let ((parent-frame (selected-frame))
        (synced-frame (make-frame)))
    (add-hook
     'window-configuration-change-hook
     `(lambda ()
        (if (eq (selected-frame) ,parent-frame)
            (save-excursion
              (select-frame ,synced-frame)
              (switch-to-buffer (window-buffer
                                 (frame-selected-window
                                  ,parent-frame)))
              (select-frame ,parent-frame)))))))

Upvotes: 3

Related Questions