If frame named “xyz” exists, then switch to that frame

瘦欲@ 提交于 2019-12-29 07:16:09

问题


Could someone please give me hand with a function that detects whether a frame named "xyz" exists, and if so, then switch to that frame. I'm using frame-cmds to give each frame a user-defined name:  http://www.emacswiki.org/emacs/frame-cmds.el

I would imagine it is similar to a buffer, but I'm not finding anything on Google. Here is the buffer function:

(defun buffer-exists (bufname)
    (not (eq nil (get-buffer bufname))))

(defun lawlist-switch-to-buffer-xyz ()
(interactive)
    (if (buffer-exists "xyz")
        (switch-to-buffer "xyz") ))

Here is a semi-related post:  https://superuser.com/questions/358037/emacsclient-create-a-frame-if-a-frame-does-not-exist


EDIT (September 15, 2014):  Modified the function ido-switch-frame to make frame-to a let-bound variable, and removed the message. Removed previous edits as the functions get-a-frame and get-frame-name written by Drew Adams are sufficient when used in conjunction with select-frame-set-input-focus -- see his answer below.

(defun ido-switch-frame ()
(interactive)
  (when (not (minibufferp))
    (let* (
        (frames (frame-list))
        (frame-to (ido-completing-read "Select Frame:  "
          (mapcar (lambda (frame) (frame-parameter frame 'name)) frames))))
      (catch 'break
        (while frames
          (let ((frame (car frames)))
            (if (equal (frame-parameter frame 'name) frame-to)
              (throw 'break (select-frame-set-input-focus frame))
              (setq frames (cdr frames)))))))))

回答1:


There may be more elegant solutions but this gets the job done:

(defun switch-to-frame (frame-name)
  (interactive "sFrame name:")
  (let ((frames (frame-list)))
    (catch 'break
      (while frames
        (let ((frame (car frames)))
          (if (equal (frame-parameter frame 'name) frame-name)
              (throw 'break (select-frame-set-input-focus frame))
            (setq frames (cdr frames))))))))



回答2:


  1. Wrt your request for "a function that detects whether a frame named "xyz" exists": You already have that, since you say you are using frame-cmds.el, which requires frame-fns.el --- Function get-a-frame does just that.

  2. Icicles provides multi-command icicle-select-frame, which lets you choose frames by name using completion.



来源:https://stackoverflow.com/questions/17823448/if-frame-named-xyz-exists-then-switch-to-that-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!