Witam na mojej prywatnej stronie internetowej!
[If this is all Polish to you, click here: English]
Uwaga: z oczywistych powodów nie mogę zagwarantować swojej nieomylności, choć staram się o zgodność tego, co piszę, z Prawdą. Jest również oczywiste, że nie gwarantuję takiej zgodności w przypadku komentarzy. Umieszczenie linku do strony spoza niniejszego serwisu nie musi oznaczać, że podzielam poglądy autora tej strony, a jedynie, że uważam ją za wartościową z takich czy innych powodów.
Marcin ‘mbork’ Borkowski
At my day job, I use multiple Emacs frames a lot. I often work on two or sometimes even more projects in parallel, and the way I don’t get lost is that I keep every project in a separate frame. Moving between them with other-frame (C-x 5 0, which I also bound to M-<f6>) is sometimes slightly clunky – especially that from time to time I’d like to be able to see the list of all my frames and possibly select one of them. (Before you tell me that my window manager could do that, let me explain that I use terminal Emacs inside a virtual machine, so I only see one frame at a time.)
A bit surprisingly, Emacs does not seem to have similar support for frames as it has for buffers. Everybody knows about switch-to-buffer, bu did you know about select-frame-by-name? It is like its poor cousin – for example, it cannot be used to create new frames. Also, believe it or not, but frame names in Emacs are not unique!
Let’s fix the latter problem first. My first thought was to advise set-frame-name, but I think rewriting it is a better solution – it is a short function, and you can only put an advice before or after the function, not in the middle of it. (Technically, there is the :override advice combinator, but using it would mean rewriting the function anyway.)
(defun mbork-set-unique-frame-name (name)
"Set the name of the selected frame to NAME.
Do not allow to use a name of an existing frame. This function is based
on `set-frame-name'."
(interactive
(list (completing-read "Frame name: " (make-frame-names-alist))))
(when (string-empty-p name)
(user-error "Frame name must not be empty"))
(when (assoc name (make-frame-names-alist))
(user-error "Frame called %s already exists" name))
(modify-frame-parameters (selected-frame)
(list (cons 'name name))))
This function is probably far from ideal. If the user decides to invoke set-frame-name and deliberately set a frame’s name to the same as some other frame’s name, there’s no stopping them. Still, it’s good enough for me.
Let’s now write a function to switch to a frame with the given name, and if the user provides a string which is not a name of an existing frame, to create a new one with that name. Of course, I started with select-frame-by-name and modified it.
(defun mbork-select-or-create-frame-by-name (name)
"Select or create the frame whose name is NAME and raise it.
This function is based on `select-frame-by-name'."
(interactive
(let* ((frame-names-alist (make-frame-names-alist))
(input (completing-read
"Select Frame: "
frame-names-alist nil nil nil 'frame-name-history)))
(when (string-empty-p input)
(user-error "Frame name must not be empty"))
(list input)))
(select-frame-set-input-focus
(or (cdr (assoc name (make-frame-names-alist)))
(make-frame (list (cons 'name name))))))
And that’s it for today, see you next week!