I need to be able to create frames and to refer to them programmatically.

How can one create a new Emacs frame with a given name, so that he can refer to it later on?

I know how to create new frames, either interactively or programmatically: https://www.gnu.org/software/emacs/manual/html_node/emacs/Creating-Frames.html

It is possible to display a frame title in different ways: http://www.emacswiki.org/emacs/FrameTitle but I could not find how to set and refer to a frame name.

A code pointer or a precise search engine search would suffice.

Thank you in advance.

  • @lawlist Thank you for your reference to set-frame-name, it helps clarify the issue. With that and all the help I came up with a proper function that does what I need. I listed it as an answer, if that is all right. – gsl Sep 14 '14 at 6:49

Sounds like you want to get a frame, given its name. Use function get-a-frame, in library frame-fns.el to do that.

And if you want to get the frame name from a frame, use function get-frame-name in the same library.

(See also library frame-cmds.el.)

  • Thank you for the answer. And thank you for all the very nice libraries, truly inspiring. – gsl Sep 13 '14 at 15:41
  • Both links are 404. – Chris Stryczynski Jun 24 '18 at 11:22
  • 1
    @ChrisStryczynski: Thx. Corrected (Emacs wiki changed from using http to using https. – Drew Jun 24 '18 at 15:49

See select-frame-set-input-focus at http://www.gnu.org/software/emacs/manual/html_node/elisp/Input-Focus.html. For example, try

(select-frame-set-input-focus (second (frame-list)))
  • Thank you, it works. Emacs assign an internal code when creating a new frame. In my case, the secon one was #<frame *scratch* 0x112ec2440>. So, (frame-list) was returning (#<frame *scratch* 0x112ec2440> #<frame *scratch* 0x105013b98>). Then, your code snippet will allow me to switch to the second frame in that list. I guess one would use the last element of that list? Would that be the most recent frame? – gsl Sep 13 '14 at 8:14

Thank you to all for the links and the code. I have learned a lot!

After studying those pages, it seems that Emacs does not allow to assign a custom ID to a new frame.

So, in order to uniquely reference a newly created frame, one needs to catch the ID assigned by Emacs at creation time.

I can now better express what I was looking for with a function like this:

(defun my/frame-create (&optional title)
  "Create a new frame returning its internal id,
   optionally setting TITLE as title"
  (interactive "sNew frame name: ")
    (setq my/frame-id (make-frame-command))
    (set-frame-name title))

This function creates a new frame, and returns its ID, so that that frame can be called at any time.

I would be grateful for any comments to write this or similar function with proper elisp idiom.

Thank you once again to all for the advice.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.