Switch between frames by number or letter

I would like to create a function that offers me the numbered or letter options ( 1, 2, 3 or a, b, c ) of the available frames for switching, instead of manually entering a name. Aspell would be the closest example I can think of.

Can someone share an example of how this can be done? Lines 6 through 14 of the following function create a list of all available frame names on the fly. Additional functions related to frame switching can be found here.

 (defun switch-frame (frame-to) (interactive (list (read-string (format "From: (%s) => To: %s. Select: " ;; From: (frame-parameter nil 'name) ;; To: (mapcar (lambda (frame) "print frame" (reduce 'concat (mapcar (lambda (s) (format "%s" s)) (list "|" (frame-parameter frame 'name) "|" ) ) ) ) (frame-list) ) )))) ;; end of interactive statement (setq frame-from (frame-parameter nil 'name)) (let ((frames (frame-list))) (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)))))) ) (message "Switched -- From: \"%s\" To: \"%s\"." frame-from frame-to) ) 

EDIT (November 13, 2014): here is a revised function using ido-completing-read :

 (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))))))))) 
+4
source share
3 answers

INITIAL PROJECT (November 13, 2014): The first draft of the function for switching frames by selecting a number. The basic concept is implemented.

EDIT (November 15, 2014): Added custom coloring for frame number and frame name. Screenshot added.

 (defface frame-number-face '((t (:background "black" :foreground "red" ))) "Face for `frame-number-face`." :group 'frame-fn) (defface frame-name-face '((t ( :background "black" :foreground "ForestGreen"))) "Face for `frame-name-face`." :group 'frame-fn) (defun select-frame-number () "Select a frame by number -- a maximum of 9 frames are supported." (interactive) (let* ( choice chosen-frame (n 0) (frame-list (frame-list)) (total-frames (safe-length frame-list)) (frame-name-list (mapcar (lambda (frame) (cons frame (frame-parameter frame 'name))) frame-list)) (frame-name-list-sorted (sort frame-name-list #'(lambda (xy) (string< (cdr x) (cdr y))))) (frame-number-list (mapcar (lambda (frame) (setq n (1+ n)) (cons n (cdr frame))) frame-name-list-sorted)) (pretty-list (mapconcat 'identity (mapcar (lambda (x) (concat "[" (propertize (format "%s" (car x)) 'face 'frame-number-face) "] " (propertize (format "%s" (cdr x)) 'face 'frame-name-face))) frame-number-list) " | ")) ) (message "%s" pretty-list) (setq choice (read-char-exclusive)) (cond ((eq choice ?1) (setq choice 1)) ((eq choice ?2) (setq choice 2)) ((eq choice ?3) (setq choice 3)) ((eq choice ?4) (setq choice 4)) ((eq choice ?5) (setq choice 5)) ((eq choice ?6) (setq choice 6)) ((eq choice ?7) (setq choice 7)) ((eq choice ?8) (setq choice 8)) ((eq choice ?9) (setq choice 9)) (t (setq choice 10))) (setq chosen-frame (car (nth (1- choice) frame-name-list-sorted))) (when (> choice total-frames) (let* ( (debug-on-quit nil) (quit-message (format "You must select a number between 1 and %s." total-frames))) (signal 'quit `(,quit-message )))) (select-frame chosen-frame) (raise-frame chosen-frame) chosen-frame)) 

Example

+1
source

I see what you are trying to do. Here is how I solved this problem:

Part 1

Files that you use every day should be bookmarked. The reason is that you lose focus when you read a menu, even the short one you describe. After a while with bookmarks, it is like touch input: you select a buffer without thinking about it.

You can check this question to see my system. I have about 20 important files and buffers with bookmarks and available in two keystrokes, for example. μ k for keys.el and μ h for hooks.el . A nice bonus is that bookmark-bmenu-list shows it all, so I can

  • add / remove bookmarks easily
  • rename bookmarks (rename change binding)
  • it can be clicked (sometimes useful)

bookmark+ allows you to add bookmarks, so I got org-agenda on μ a and magit on μ m . And, of course, the dired bookmarks: the source is on μ s and the org files are on μ g .

Part 2

For files that cannot be bookmarked, I use:

 (ido-mode) (setq ido-enable-flex-matching t) (global-set-key "η" 'ido-switch-buffer) 

This is also fast: you need one keystroke of the ido-switch-buffer key and about 2-3 letters to find the desired buffer, and RET to select.

I also recently added this hack:

 (add-hook 'ido-setup-hook (lambda() (define-key ido-buffer-completion-map "η" 'ido-next-match))) 

With this, you can use the same key to call ido-switch-buffer and loop through.

Part 3

The actual function with the inscription with the inscription for a while was on my task list Now. I will post a message here when I start implementing it, or maybe just copy the solution from another answer :)

+2
source
  • This answer is described by Icicles icicle-select-frame command, which allows you to select frames by name using completion.

  • There is also the Icicles icicle-other-window-or-frame ( Cx o ) command, which combines the icicle-select-frame , other-frame and other-window commands. It allows you to select a window or frame by name or by order.

    • Without arg prefix or non-zero numeric arg prefix: If the selected frame has multiple windows, then this is another other-window . Otherwise, it is other-frame .

    • With a zero prefix arg (for example, C-0 ): If the selected frame has several windows, then this is icicle-select-window with windows in the frame as candidates. Otherwise (single-frame) this is icicle-select-frame .

    • With regular Cu : If the selected frame has several windows, then this is icicle-select-window with windows from all visible frames as candidates. Otherwise, it is icicle-select-frame .

+1
source

Source: https://habr.com/ru/post/1493834/


All Articles