ironsand
ironsand

Reputation: 15181

How to call enlarge-window function depends on current window in Emacs

I set keybinding like following.

(global-set-key [M-down] 'shrink-window)
(global-set-key [M-up] 'enlarge-window)

So with M-up I can enlarge-window and with M-down otherwise. But I want to enlarge window with M-up if current window is lower side. And if current window is upper side with M-up I want to call shrink-window function.

Likewise I want to shrink window with M-down if current window is lower side and enlarge if current window is upper side.

In other words, I want to indicate the direction of window separator.

How can I write the function?

Upvotes: 1

Views: 73

Answers (2)

abo-abo
abo-abo

Reputation: 20362

I was just finishing this code as the answer came up. Oh well.

(require 'windmove)
(global-set-key [M-up] 
                (lambda() (interactive)
                       (call-interactively
                        (if (windmove-find-other-window 'up)
                            'enlarge-window
                          'shrink-window))))

(global-set-key [M-down] 
                (lambda() (interactive)
                       (call-interactively
                        (if (not (windmove-find-other-window 'up))
                            'enlarge-window
                          'shrink-window))))

Upvotes: 2

juanleon
juanleon

Reputation: 9410

Taking advantage of windmove being part of emacs, this is what you can do:

(require 'windmove)
(defun move-separator-up (arg)
       (interactive "p")
       (if (< (cdr (windmove-other-window-loc 'up)) 0)
           (shrink-window arg)
         (enlarge-window arg)))


(defun move-separator-down (arg)
       (interactive "p")
       (if (< (cdr (windmove-other-window-loc 'up)) 0)
           (enlarge-window arg)
         (shrink-window arg)))

When you have 3 windows, enlarge-window and shrink-window rules might not do always what you want, so some further customization might be needed.

Upvotes: 1

Related Questions