1 ;;; windresize.el --- Resize windows interactively
3 ;; Copyright (C) 2011 Free Software Foundation, Inc.
5 ;; Filename: windresize.el
6 ;; Author: Bastien <bzg AT altern DOT org>
7 ;; Maintainer: Bastien <bzg AT altern DOT org>
9 ;; Description: Set window configuration with keystrokes
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, write to the Free Software
24 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This is not part of GNU Emacs.
30 ;; This mode lets you edit the window configuration interactively just
31 ;; by using the keyboard.
33 ;; To use it, type M-x windresize; this enters put Emacs in a state
34 ;; where the up/down and left/right arrow keys resize the window
35 ;; dimensions. To return Emacs to its ordinary state, type RET.
37 ;; See the docstring of `windresize' for a detailed description of the
38 ;; other commands that are available while windresize is active.
42 ;; This was largely inspired by Hirose Yuuji and Bob Wiener original
43 ;; `resize-window' as posted on the emacs-devel mailing list by Juanma.
44 ;; This was also inspired by Lennart Borgman's bw-interactive.el (now
45 ;; winsize.el). See related discussions on the emacs-devel mailing
46 ;; list. Special thanks to Drew Adams, Juri Linkov, Stefan Monnier and
47 ;; JunJie Nan for useful suggestions.
49 ;; Also check http://www.emacswiki.org/cgi-bin/wiki/WindowResize for
50 ;; general hints on window resizing.
52 ;; Put this file into your load-path and the following into your
53 ;; ~/.emacs: (require 'windresize)
57 ;; - better help window
58 ;; - register key sequences as macros
59 ;; - maybe add numbers to window configurations in the ring
63 (require 'ring) ; for storing window configuration
64 (require 'windmove) ; for selecting adjacent window
68 (defconst windresize-version "0.6"
69 "The version number of the file windresize.el.")
71 (defcustom windresize-move-borders t
72 "Default method for resizing windows.
73 \\<windresize-map>Non-nil means that windresize will move borders.
74 For example, \\[windresize-left] will move the first movable border to the
75 left, trying to move the right border then the left border. \\[windresize-up]
76 will move the first movable border up, trying to move the bottom border then
79 Nil means that it will shrink or enlarge the window instead.
80 \\[windresize-down] and \\[windresize-up] will shrink and enlarge the window
81 vertically. \\[windresize-left] and \\[windresize-right] will shrink and
82 enlarge the window horizontally."
86 (defcustom windresize-default-increment 1
87 "The default number of lines for resizing windows."
91 (defcustom windresize-verbose 2
92 "Integer that say how verbose Windresize should be.
93 The higher the number, the more feedback Windresize will give.
94 A value of 0 will prevent any message to be displayed.
95 A value of 1 will display errors only.
96 A value of 2 will display errors and messages."
100 (defcustom windresize-ring-size 10
101 "The size of the ring for storing window configurations."
105 (defcustom windresize-windmove-relative-to-point 0
106 "Nil means select adjacent window relatively to the point position.
107 Non-nil means select adjacent window relatively to the window
108 edges. See the docstring of `windmove-up' for details."
112 (defcustom windresize-modifiers '((meta shift) meta
113 (control meta) control)
114 "A list of modifiers for arrow keys commands.
115 Each element can be a modifier or a list of modifiers.
117 The first modifier is for selecting windows with `windmove'.
118 The second modifier is for moving the up/left border instead of
119 the bottom/right border when there are two movable borders.
120 The third modifier is to move borders and keep the width/height
122 The fourth modifier is to move boder or resize window while
123 temporarily negating the increment value.
125 Make sure the four elements of this list are distinct to avoid
126 conflicts between keybindings."
129 (choice :tag "Modifier for selecting the adjacent windows"
130 (symbol :tag "Single modifier")
131 (repeat :tag "Multiple modifiers"
132 (symbol :tag "Modifier")))
133 (choice :tag "Modifier for moving the left/up border instead of the right/bottom border"
134 (symbol :tag "Single modifier")
135 (repeat :tag "Multiple modifiers"
136 (symbol :tag "Modifier")))
137 (choice :tag "Modifier for moving borders with fixed width/height"
138 (symbol :tag "Single modifier")
139 (repeat :tag "Multiple modifiers"
140 (symbol :tag "Modifier")))
141 (choice :tag "Modifier for negating increment temporarily"
142 (symbol :tag "Single modifier")
143 (repeat :tag "Multiple modifiers"
144 (symbol :tag "Modifier")))))
146 ;;; Variables and keymap:
148 (defvar windresize-msg '("" . 0))
149 (defvar windresize-buffer nil)
150 (defvar windresize-increment nil)
151 (defvar windresize-resizing nil)
152 (defvar windresize-configuration-ring nil)
153 (defvar windresize-window-configuration-0 nil)
154 (defvar windresize-overriding-terminal-local-map-0 nil)
155 (defvar windresize-overriding-menu-flag-0 nil)
157 (defvar windresize-map
158 (let ((map (make-sparse-keymap)))
159 (define-key map [remap self-insert-command] 'windresize-other-char)
160 (define-key map (kbd "M-x") 'windresize-other-char)
161 (define-key map (kbd "C-h") 'windresize-other-char)
162 ;; move borders outwards or shrink/enlarge
163 (define-key map [left] 'windresize-left)
164 (define-key map [right] 'windresize-right)
165 (define-key map [up] 'windresize-up)
166 (define-key map [down] 'windresize-down)
167 ;; Use windmove to select adjacent window. The default keybindings
168 ;; in `windresize-modifiers' should match those of windmove
169 (let ((mod (nth 0 windresize-modifiers)))
170 (if (symbolp mod) (setq mod (list mod)))
171 (define-key map (vector (append mod '(left))) 'windresize-select-left)
172 (define-key map (vector (append mod '(right))) 'windresize-select-right)
173 (define-key map (vector (append mod '(up))) 'windresize-select-up)
174 (define-key map (vector (append mod '(down))) 'windresize-select-down))
175 ;; Move the up/left border instead of bottom/right when there are
176 ;; two movable borders
177 (let ((mod (nth 1 windresize-modifiers)))
178 (if (symbolp mod) (setq mod (list mod)))
179 (define-key map (vector (append mod '(left))) 'windresize-left-force-left)
180 (define-key map (vector (append mod '(right))) 'windresize-right-force-left)
181 (define-key map (vector (append mod '(up))) 'windresize-up-force-up)
182 (define-key map (vector (append mod '(down))) 'windresize-down-force-up))
183 ;; Move borders with fixed width/height
184 (let ((mod (nth 2 windresize-modifiers)))
185 (if (symbolp mod) (setq mod (list mod)))
186 (define-key map (vector (append mod '(left))) 'windresize-left-fixed)
187 (define-key map (vector (append mod '(right))) 'windresize-right-fixed)
188 (define-key map (vector (append mod '(up))) 'windresize-up-fixed)
189 (define-key map (vector (append mod '(down))) 'windresize-down-fixed))
190 ;; Negate increment temporarily
191 (let ((mod (nth 3 windresize-modifiers)))
192 (if (symbolp mod) (setq mod (list mod)))
193 (define-key map (vector (append mod '(left))) 'windresize-left-minus)
194 (define-key map (vector (append mod '(right))) 'windresize-right-minus)
195 (define-key map (vector (append mod '(up))) 'windresize-up-minus)
196 (define-key map (vector (append mod '(down))) 'windresize-down-minus))
198 (define-key map "~" 'windresize-negate-increment)
199 (define-key map "+" 'windresize-increase-increment)
200 (define-key map "-" 'windresize-decrease-increment)
202 (define-key map "i" 'windresize-set-increment)
204 (define-key map " " 'windresize-toggle-method)
205 (define-key map "s" 'windresize-save-window-configuration)
206 (define-key map "r" 'windresize-restore-window-configuration)
207 ;; shorcut keys for manipulating windows
208 (define-key map "0" 'delete-window)
209 (define-key map "o" 'windresize-other-window)
210 (define-key map "n" 'windresize-next-window)
211 (define-key map "p" 'windresize-previous-window)
212 (define-key map "/" 'windresize-bottom-right)
213 (define-key map "\M-/" 'windresize-up-left)
214 (define-key map (kbd "\\") 'windresize-up-right)
215 (define-key map (kbd "M-\\") 'windresize-bottom-left)
216 (define-key map "1" 'windresize-delete-other-windows)
217 (define-key map "2" 'windresize-split-window-vertically)
218 (define-key map "3" 'windresize-split-window-horizontally)
219 (define-key map "=" 'windresize-balance-windows)
220 (define-key map "\C-xo" 'windresize-other-window)
221 (define-key map "\C-x0" 'windresize-delete-window)
222 (define-key map "\C-x1" 'windresize-delete-other-windows)
223 (define-key map "\C-x2" 'windresize-split-window-vertically)
224 (define-key map "\C-x3" 'windresize-split-window-horizontally)
225 (define-key map "\C-x+" 'windresize-balance-windows)
226 (define-key map (kbd "C-a")
227 (lambda() (interactive) (move-beginning-of-line 1)))
228 (define-key map (kbd "C-e")
229 (lambda() (interactive) (move-end-of-line 1)))
230 (define-key map "=" 'windresize-balance-windows)
231 (define-key map [mouse-1] 'mouse-set-point)
232 ;; help, save and exit
233 (define-key map (kbd "RET") 'windresize-exit)
234 (define-key map "x" 'windresize-exit)
235 (define-key map "\C-c\C-c" 'windresize-exit)
236 (define-key map "?" 'windresize-help)
237 (define-key map "q" 'windresize-cancel-and-quit)
238 (define-key map "c" 'windresize-cancel-and-quit)
239 (define-key map (kbd "C-g") 'windresize-cancel-and-quit)
241 "Keymap for `windresize'.")
243 (defun windresize-other-char ()
244 "Show a message instead of processing `self-insert-command'."
246 (let* ((key (if current-prefix-arg
247 (substring (this-command-keys)
248 universal-argument-num-events)
249 (this-command-keys))))
250 (cond ((vectorp key) (ding))
252 ;; send warning for only no warning for complex keys and
255 (cons (format "[`%s' not bound]" key) 1))))))
259 (defun windresize-other-window ()
260 "Select other window."
263 (setq windresize-msg (cons "[No other window]" 1))
265 (setq windresize-msg (cons (format "Now in %s" (buffer-name)) 2))))
267 (defalias 'windresize-next-window 'windresize-other-window)
269 (defun windresize-previous-window ()
270 "Select the previous window."
273 (setq windresize-msg (cons "[No previous window]" 1))
275 (setq windresize-msg (cons (format "Now in %s" (buffer-name)) 2))))
277 (defun windresize-delete-window ()
281 (setq windresize-msg (cons "[Can't delete sole window]" 1))
283 (setq windresize-msg (cons "Window deleted" 2))))
285 (defun windresize-delete-other-windows ()
286 "Delete other windows."
289 (setq windresize-msg (cons "[No other window]" 1))
290 (delete-other-windows)
291 (setq windresize-msg (cons "Windows deleted" 2))))
293 (defun windresize-split-window-horizontally ()
294 "Split window horizontally."
296 (split-window-horizontally)
297 (setq windresize-msg (cons "Window horizontally split" 2)))
299 (defun windresize-split-window-vertically ()
300 "Split window vertically."
302 (split-window-vertically)
303 (setq windresize-msg (cons "Window vertically split" 2)))
305 (defun windresize-balance-windows ()
309 (setq windresize-msg (cons "Windows balanced" 2)))
314 (defun windresize (&optional increment)
315 "Resize windows interactively.
316 INCREMENT is the number of lines by which borders should move.
318 By default, the method for resizing is by moving the borders.
319 The left/right key will move the only movable vertical border to
320 the left/right and the up/down key will move the only horizontal
321 movable border up/down. If there are two movable borders, the
322 right and the bottom border will have priority over the left and
323 upper border. You can reverse this priority by using \\[windresize-left-force-left],
324 \\[windresize-right-force-left], etc.
326 Resizing can also be done by increasing/decreasing the window
327 width and height. The up and down arrow keys will enlarge or
328 shrink the window vertically and the right and left arrow keys
329 will enlarge or shrink the window horizontally.
331 You can toggle the method with \\[windresize-toggle-method].
333 You can set the number of line by which a border should move by
334 calling \\[windresize-set-increment] with a numeric prefix.
335 You can temporarily negate the number of lines by which the
336 windows are resized by using \\[windresize-left-minus], \\[windresize-right-minus], etc.
337 If you want to permanently negate this increment value,
338 use `\\[windresize-negate-increment]' instead.
340 You can also save window configurations with `\\[windresize-save-window-configuration]' in a ring,
341 and restore them with `\\[windresize-restore-window-configuration]'.
343 `\\[windresize-cancel-and-quit]' will quit `windresize' and cancel any change. `\\[windresize-exit]'
344 will set the new window configuration and exit.
348 (if windresize-resizing
350 ;; FIXME shall we exit we calling again `windresize'?
351 ;; (progn (windresize-message '("[Already resizing]" . 0))
353 (setq windresize-overriding-terminal-local-map-0
354 overriding-terminal-local-map)
355 (setq windresize-overriding-menu-flag-0
356 overriding-local-map-menu-flag)
357 (setq windresize-window-configuration-0
358 (current-window-configuration))
359 ;; set increment, window configuration ring, initial buffer
360 (setq windresize-increment windresize-default-increment)
361 (setq windresize-configuration-ring
362 (make-ring windresize-ring-size))
363 (ring-insert windresize-configuration-ring
364 (current-window-configuration))
365 (setq windresize-buffer (current-buffer))
366 ;; set overriding map and pre/post-command hooks
367 (setq overriding-terminal-local-map windresize-map)
368 (setq overriding-local-map-menu-flag t)
369 (windresize-add-command-hooks)
370 ;; set the initial message
373 (setq windresize-msg (cons "Split window with [23]" 2))
374 (setq windresize-msg (cons "" 0))))
375 (setq windresize-resizing t)
376 (windresize-message)))
378 (defun windresize-message (&optional msg)
379 "Display a message at the bottom of the screen.
380 If MSG is nil, use `windresize-msg' instead."
381 (let* ((msg0 (or msg windresize-msg))
384 (method (if windresize-move-borders
385 "move borders " "resize window")))
386 (cond ((< msg-l 2) ; information
387 (add-text-properties 0 (length msg-t) '(face bold) msg-t))
388 ((< msg-l 3) ; warnings
389 (add-text-properties 0 (length msg-t) '(face shadow) msg-t)))
390 (add-text-properties 0 (length method) '(face bold) method)
391 (message "Use arrow keys to %s by %d %s RET:set ?:help %s"
392 method windresize-increment
393 (if (not (equal (abs windresize-increment) 1))
395 (if (<= (cdr windresize-msg) windresize-verbose)
398 (defun windresize-add-command-hooks ()
399 "Add hooks to commands when entering `windresize'."
400 (add-hook 'pre-command-hook 'windresize-pre-command)
401 (add-hook 'post-command-hook 'windresize-post-command))
403 (defun windresize-remove-command-hooks ()
404 "Remove hooks to commands when exiting `windresize'."
405 (remove-hook 'pre-command-hook 'windresize-pre-command)
406 (remove-hook 'post-command-hook 'windresize-post-command))
408 (defun windresize-pre-command ()
409 "Pre-command in `windresize'."
410 (setq windresize-msg (cons "" 0)))
412 (defun windresize-post-command ()
413 "Post-command in `windresize'."
414 (windresize-message))
416 (defun windresize-toggle-method ()
417 "Toggle resizing method."
419 (setq windresize-move-borders
420 (not windresize-move-borders))
424 (if (not windresize-move-borders)
425 "resize window" "move borders")) 2)))
427 ;;; Use windmove to select the adjacent window:
429 (defun windresize-select-down (&optional arg)
430 "Select the window below the current one.
431 If ARG is nil or zero, select the window relatively to the point
432 position. If ARG is positive, select relatively to the left edge
433 and select relatively to the right edge otherwise."
437 (or arg windresize-windmove-relative-to-point))
438 (error (setq windresize-msg
439 (cons "[Can't select window below this one]" 1)))))
441 (defun windresize-select-up (&optional arg)
442 "Select the window above the current one.
443 If ARG is nil or zero, select the window relatively to the point
444 position. If ARG is positive, select relatively to the left edge
445 and select relatively to the right edge otherwise."
449 (or arg windresize-windmove-relative-to-point))
450 (error (setq windresize-msg
451 (cons "[Can't select window above this one]" 1)))))
453 (defun windresize-select-left (&optional arg)
454 "Select the window to the left of the current one.
455 If ARG is nil or zero, select the window relatively to the point
456 position. If ARG is positive, select relatively to the top edge
457 and select relatively to the bottom edge otherwise."
461 (or arg windresize-windmove-relative-to-point))
462 (error (setq windresize-msg
463 (cons "[Can't select window left this one]" 1)))))
465 (defun windresize-select-right (&optional arg)
466 "Select the window to the right of the current one.
467 If ARG is nil or zero, select the window relatively to the point
468 position. If ARG is positive, select relatively to the top edge
469 and select relatively to the bottom edge otherwise."
473 (or arg windresize-windmove-relative-to-point))
474 (error (setq windresize-msg
475 (cons "[Can't select window right this one]" 1)))))
477 ;;; Increase/decrease/set the increment value:
479 (defun windresize-set-increment (&optional n)
480 "Set the increment value to N."
482 (setq windresize-increment n)
483 (setq windresize-msg (cons "Increment set" 2)))
485 (defun windresize-negate-increment (&optional silent)
486 "Negate the increment value.
487 If SILENT, dont output a message."
489 (setq windresize-increment (- windresize-increment))
490 (unless silent (setq windresize-msg (cons "Negated increment" 2))))
492 (defun windresize-increase-increment (&optional silent)
493 "Increase the increment.
494 If SILENT is non-nil, don't output a message."
496 (let ((i windresize-increment))
497 (if (eq i -1) (setq i (- i)) (setq i (1+ i)))
498 (setq windresize-increment i))
499 (unless silent (setq windresize-msg (cons "Increased increment" 2))))
501 (defun windresize-decrease-increment (&optional silent)
502 "Decrease the increment.
503 If SILENT is non-nil, don't output a message."
505 (let ((i windresize-increment))
506 (if (eq i 1) (setq i (- 1)) (setq i (1- i)))
507 (setq windresize-increment i))
508 (unless silent (setq windresize-msg (cons "Decreased increment" 2))))
510 ;;; Window configuration ring:
512 (defun windresize-save-window-configuration ()
513 "Save the current window configuration in the ring."
515 (if (equal (ring-ref windresize-configuration-ring 0)
516 (current-window-configuration))
518 (cons "[Same window configuration: not saved]" 1))
519 (ring-insert windresize-configuration-ring
520 (current-window-configuration))
522 (cons "Configuration saved -- use `r' to restore" 2))))
524 (defun windresize-restore-window-configuration ()
525 "Restore the previous window configuration in the ring."
527 (let ((wcf (ring-remove windresize-configuration-ring 0)))
528 (set-window-configuration wcf)
529 (ring-insert-at-beginning windresize-configuration-ring wcf))
530 (setq windresize-msg (cons "Previous configuration restored" 2)))
532 ;;; Commands for arrow keys:
534 (defun windresize-left (&optional n left-border fixed-width)
535 "Main function for handling left commands.
536 N is the number of lines by which moving borders.
537 In the move-border method, move the right border to the left.
538 If LEFT-BORDER is non-nil, move the left border to the left.
539 In the resize-window method, shrink the window horizontally.
541 If FIXED-WIDTH is non-nil and both left and right borders are
542 movable, move the window to the left and preserve its width."
544 (let* ((left-w (windmove-find-other-window 'left))
545 (right-w (windmove-find-other-window 'right))
546 (i (if n (prefix-numeric-value n) windresize-increment))
547 (shrink-ok (> (- (window-width) i) window-min-width))
548 (w (selected-window)))
549 (if (not windresize-move-borders)
552 (cons "[Can't shrink window horizontally]" 1))
554 (if shrink-ok (shrink-window-horizontally i)
556 (error (setq windresize-msg
557 (cons "[Can't shrink window horizontally]" 1)))))
558 (cond ((equal (frame-width) (window-width))
559 (setq windresize-msg (cons "No vertical split" 2)))
560 ((and left-w right-w)
562 (progn (windmove-left windresize-windmove-relative-to-point)
563 (adjust-window-trailing-edge (selected-window) (- i) t)
565 (if fixed-width (windresize-left)))
567 (progn (adjust-window-trailing-edge w (- i) t)
568 (if fixed-width (windresize-left nil t)))
569 (error (setq windresize-msg
570 (cons "[Can't move right border left]" 1))))))
573 (adjust-window-trailing-edge left-w (- i) t)
574 (error (setq windresize-msg (cons "[Can't move left border left]" 1)))))
575 (right-w (windresize-left-inwards))
576 (t (setq windresize-msg (cons "[Can't move border]" 1)))))))
578 (defun windresize-right (&optional n left-border fixed-width)
579 "Main function for handling right commands.
580 N is the number of lines by which moving borders.
581 In the move-border method, move the right border to the right.
582 If LEFT-BORDER is non-nil, move the left border to the right.
583 In the resize-window method, enlarge the window horizontally.
585 If FIXED-WIDTH is non-nil and both left and right borders are
586 movable, move the window to the right and preserve its width."
588 (let ((right-w (windmove-find-other-window 'right))
589 (left-w (windmove-find-other-window 'left))
590 (i (if n (prefix-numeric-value n) windresize-increment))
591 (wcf (current-window-configuration))
592 (w (selected-window)))
593 (if (not windresize-move-borders)
594 (progn (ignore-errors (enlarge-window-horizontally i))
595 (if (equal wcf (current-window-configuration))
597 (cons "[Can't enlarge window horizontally]" 1))))
598 (cond ((equal (frame-width) (window-width))
599 (setq windresize-msg (cons "No vertical split" 2)))
600 ((and right-w left-w left-border)
601 (progn (windmove-left windresize-windmove-relative-to-point)
602 (adjust-window-trailing-edge left-w i t)
604 (if fixed-width (windresize-right))))
607 (adjust-window-trailing-edge w i t)
608 (error (setq windresize-msg
609 (cons "[Can't move right border right]" 1)))))
610 (left-w (windresize-right-inwards))
611 (t (setq windresize-msg (cons "[Can't move border]" 1)))))))
613 (defun windresize-up (&optional n upper-border fixed-height)
614 "Main function for handling up commands.
615 N is the number of lines by which moving borders.
616 In the move-border method, move the bottom border upwards.
617 If UPPER-BORDER is non-nil, move the upper border upwards.
618 In the resize-window method, enlarge the window vertically.
620 If FIXED-HEIGHT is non-nil and both the upper and lower borders
621 are movable, move the window up and preserve its height."
623 (let ((up-w (windmove-find-other-window 'up))
624 (down-w (windmove-find-other-window 'down))
625 (i (if n (prefix-numeric-value n) windresize-increment))
626 (wcf (current-window-configuration))
627 (w (selected-window)))
628 (if (not windresize-move-borders)
629 (progn (ignore-errors (enlarge-window i))
630 (if (equal wcf (current-window-configuration))
632 (cons "[Can't enlarge window vertically]" 1))))
633 (cond ((equal (frame-height) (1+ (window-height)))
634 (setq windresize-msg (cons "No horizontal split" 2)))
635 ((and up-w down-w (not (window-minibuffer-p down-w)))
637 (progn (windmove-up windresize-windmove-relative-to-point)
638 (adjust-window-trailing-edge (selected-window) (- i) nil)
640 (if fixed-height (windresize-up)))
642 (adjust-window-trailing-edge w (- i) nil)
645 (cons "[Can't move bottom border up]" 1))))))
646 (up-w (condition-case nil
647 (adjust-window-trailing-edge up-w (- i) nil)
648 (error (setq windresize-msg
649 (cons "[Can't move upper border up]" 1)))))
650 ((and down-w (not (window-minibuffer-p down-w)))
651 (windresize-up-inwards))
652 (t (setq windresize-msg (cons "[Can't move border]" 1)))))))
654 (defun windresize-down (&optional n upper-border fixed-height)
655 "Main function for handling down commands.
656 N is the number of lines by which moving borders.
657 In the move-border method, move the bottom border down.
658 If UPPER-BORDER is non-nil, move the upper border down.
659 In the resize-window method, shrink the window vertically.
661 If FIXED-HEIGHT is non-nil and both the upper and lower borders
662 are movable, move the window down and preserve its height."
664 (let* ((down-w (windmove-find-other-window 'down))
665 (up-w (windmove-find-other-window 'up))
666 (i (if n (prefix-numeric-value n) windresize-increment))
667 (shrink-ok (> (- (window-width) i) window-min-width))
668 (wcf (current-window-configuration))
669 (w (selected-window)))
670 (if (not windresize-move-borders)
671 (if (or (and (window-minibuffer-p down-w) (not up-w))
672 (< (- (window-height) i) window-min-height))
673 (setq windresize-msg (cons "[Can't shrink window vertically]" 1))
674 (if shrink-ok (shrink-window i)
675 (setq windresize-msg (cons "[Can't shrink window vertically]" 1))))
676 (cond ((equal (frame-height) (1+ (window-height)))
677 (setq windresize-msg (cons "No horizontal split" 2)))
678 ((and up-w down-w (not (window-minibuffer-p down-w))
680 (progn (windmove-up windresize-windmove-relative-to-point)
681 (adjust-window-trailing-edge (selected-window) i nil)
683 (if fixed-height (windresize-down))))
684 ((and down-w (not (window-minibuffer-p down-w)))
686 (adjust-window-trailing-edge w i nil)
687 (error (setq windresize-msg (cons "[Can't move bottom border down]" 1)))))
688 (up-w (windresize-down-inwards))
689 (t (setq windresize-msg (cons "[Can't move border]" 1)))))))
691 ;;; Moving the opposite border inwards:
693 (defun windresize-left-inwards (&optional n)
694 "Move the right border left by N lines."
696 (let ((i (if n (prefix-numeric-value n) windresize-increment)))
698 (adjust-window-trailing-edge (selected-window) (- i) t)
699 (error (setq windresize-msg
700 (cons "[Can't move right border to the left]" 1))))))
702 (defun windresize-right-inwards (&optional n)
703 "Move the left border right by N lines."
705 (let ((i (if n (prefix-numeric-value n) windresize-increment))
706 (left-w (windmove-find-other-window 'left)))
708 (if left-w (adjust-window-trailing-edge left-w i t) (error t))
709 (error (setq windresize-msg
710 (cons "[Can't move left border right]" 1))))))
712 (defun windresize-up-inwards (&optional n)
713 "Move the bottom border up by N lines."
715 (let ((i (if n (prefix-numeric-value n) windresize-increment))
716 (down-w (windmove-find-other-window 'down)))
718 (progn (if (window-minibuffer-p down-w)
720 (cons "[Can't move bottom border up]" 1)))
721 (adjust-window-trailing-edge
722 (selected-window) (- i) nil))
723 (error (setq windresize-msg
724 (cons "[Can't move bottom border up]" 1))))))
726 (defun windresize-down-inwards (&optional n)
727 "Move the upper border down by N lines."
729 (let ((i (if n (prefix-numeric-value n) windresize-increment))
730 (wcf (current-window-configuration))
731 (up-w (windmove-find-other-window 'up)))
733 (if up-w (adjust-window-trailing-edge up-w i nil)
735 (error (setq windresize-msg
736 (cons "[Can't move upper border down]" 1))))))
738 ;;; Arrow keys temoporarily negating the increment value:
740 (defun windresize-down-minus ()
741 "Same as `windresize-left' but negate `windresize-increment'."
743 (let ((i windresize-increment))
744 (windresize-decrease-increment t)
746 (windresize-increase-increment t)))
748 (defun windresize-right-minus ()
749 "Same as `windresize-left' but negate `windresize-increment'."
751 (let ((i windresize-increment))
752 (windresize-decrease-increment t)
754 (windresize-increase-increment t)))
756 (defun windresize-up-minus ()
757 "Same as `windresize-left' but negate `windresize-increment'."
759 (let ((i windresize-increment))
760 (windresize-decrease-increment t)
762 (windresize-increase-increment t)))
764 (defun windresize-left-minus ()
765 "Same as `windresize-left' but negate `windresize-increment'."
767 (let ((i windresize-increment))
768 (windresize-decrease-increment t)
770 (windresize-increase-increment t)))
772 ;;; Let's left/up borders have priority over right/bottom borders:
774 (defun windresize-left-force-left (&optional n)
775 "If two movable borders, move the left border.
776 N is the number of lines by which moving borders."
778 (let ((i (if n (prefix-numeric-value n)
779 windresize-increment)))
780 (windresize-left i t)))
782 (defun windresize-right-force-left (&optional n)
783 "If two movable borders, move the left border.
784 N is the number of lines by which moving borders."
786 (let ((i (if n (prefix-numeric-value n)
787 windresize-increment)))
788 (windresize-right i t)))
790 (defun windresize-up-force-up (n)
791 "If two movable borders, move the upper border.
792 N is the number of lines by which moving borders."
794 (let ((i (if n (prefix-numeric-value n)
795 windresize-increment)))
796 (windresize-up i t)))
798 (defun windresize-down-force-up (n)
799 "If two movable borders, move the upper border.
800 N is the number of lines by which moving borders."
802 (let ((i (if n (prefix-numeric-value n)
803 windresize-increment)))
804 (windresize-down i t)))
806 ;;; Move the whole window, with fixed width/height:
808 (defun windresize-left-fixed ()
809 "Move the window left, keeping its width constant."
811 (windresize-left nil t t))
813 (defun windresize-right-fixed ()
814 "Move the window right, keeping its width constant."
816 (windresize-right nil t t))
818 (defun windresize-up-fixed ()
819 "Move the window up, keeping its height constant."
821 (windresize-up nil t t))
823 (defun windresize-down-fixed ()
824 "Move the window down, keeping its height constant."
826 (windresize-down nil t t))
830 (defun windresize-bottom-right ()
831 "Call `windresize-right' and `windresize-down' successively.
832 In move-borders method, move the bottom-right edge of the window
833 outwards. In resize-window method, enlarge the window
834 horizontally and shrink it vertically."
839 (defun windresize-up-left ()
840 "Call `windresize-left' and `windresize-up' successively.
841 In move-borders method, move the upper-left edge of the window
842 outwards. In resize-window method, shrink the window
843 horizontally and enlarge it vertically."
845 (windresize-left nil t)
846 (windresize-up nil t))
848 (defun windresize-up-right ()
849 "Call `windresize-right' and `windresize-up' successively.
850 In move-borders method, move the upper-right edge of the window
851 outwards. In resize-window method, enlarge the window both
852 horizontally and horizontally."
855 (windresize-up nil t))
857 (defun windresize-bottom-left ()
858 "Call `windresize-left' and `windresize-up' successively.
859 In move-borders method, move the bottom-left edge of the window
860 outwards. In resize-window method, shrink the window both
861 horizontally and vertically."
863 (windresize-left nil t)
866 ;;; Cancel, exit and help:
868 (defun windresize-cancel-and-quit ()
869 "Cancel window resizing and quit `windresize'."
871 (if (eq major-mode 'help-mode)
873 (setq windresize-msg '("Help quit" . 2)))
874 (switch-to-buffer windresize-buffer)
875 (set-window-configuration windresize-window-configuration-0)
876 (setq overriding-local-map-menu-flag
877 windresize-overriding-terminal-local-map-0)
878 (setq overriding-terminal-local-map
879 windresize-overriding-menu-flag-0)
880 (message "Window resizing quit (not saved)")
881 (windresize-remove-command-hooks)
882 (setq windresize-resizing nil)))
884 (defun windresize-exit ()
885 "Keep this window configuration and exit `windresize'."
887 (setq overriding-local-map-menu-flag
888 windresize-overriding-terminal-local-map-0)
889 (setq overriding-terminal-local-map
890 windresize-overriding-menu-flag-0)
891 (message "Window configuration set")
892 (windresize-remove-command-hooks)
893 (setq windresize-resizing nil))
895 (defun windresize-help ()
896 "Display a help window for `windresize'."
898 (let ((pop-up-frames nil) ; otherwise we exit the loop
899 (temp-buffer-show-hook
901 (fit-window-to-buffer)
902 (shrink-window-if-larger-than-buffer)
903 (goto-char (point-min))
905 (while (re-search-forward
906 "^[ M][^\n:]+:\\|[0123~=oq]:\\|RET:" nil t)
907 (add-text-properties (match-beginning 0)
908 (match-end 0) '(face bold))))))
910 "Use the arrow keys to move a border into the arrow direction.
911 Right and bottom borders have priority over left and up borders.
912 Press SPC to toggle between moving borders and resizing windows,
913 where arrow keys mean shrink/enlarge.
915 Here is a list of default keybindings:
917 arrows: move border or resize windows =: balance windows
918 M-S-arrows: select adjacent window o: other-window
919 C-M-arrows: move window with fixed width/height 0: delete current window
920 C-arrows: temporarilly negate INCREMENT ~: negate INCREMENT
921 M-arrows: move with priority to left/up 1: delete other windows
922 i: set INCREMENT (to numeric prefix) 2: split window vertically
923 +/-: increase/decrease INCREMENT 3: split window horizontally
924 s: save window configuration q: cancel and quit
925 r: restore window configuration ?: show this help window
926 SPC: toggle method: move border, resize RET: set and exit
928 /: move right-bottom edge outwards or left-upper edge inwards
929 M-/: move left-upper edge outwards or right-bottom edge inwards
930 \\: move right-upper edge outwards or left-bottom edge inwards
931 M-\\: move left-bottom edge outwards or right-upper edge inwards
933 See the docstring of `windresize' for detailed description."))
934 (with-output-to-temp-buffer "*Help*"
937 (provide 'windresize)
939 ;;; windresize.el ends here