Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

emacs - If frame named "xyz" exists, then switch to that frame

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)))))))))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))))))))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...