1 ;;; windresize.el --- resize windows interactively
3 ;; Copyright (C) 2011 Free Software Foundation, Inc.
5 ;; Filename: windresize.el
7 ;; Author: Bastien <bzg AT altern DOT org>
8 ;; Maintainer: Bastien <bzg AT altern DOT org>
10 ;; 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 let's you edit the window configuration interactively just
31 ;; by using the keyboard. Quickstart: M-x windresize
35 ;; This was largely inspired by Hirose Yuuji and Bob Wiener original
36 ;; `resize-window' as posted on the emacs-devel mailing list by Juanma.
37 ;; This was also inspired by Lennart Borgman's bw-interactive.el (now
38 ;; winsize.el). See related discussions on the emacs-devel mailing
39 ;; list. Special thanks to Drew Adams, Juri Linkov, Stefan Monnier and
40 ;; JunJie Nan for useful suggestions.
42 ;; Also check http://www.emacswiki.org/cgi-bin/wiki/WindowResize for
43 ;; general hints on window resizing.
45 ;; Put this file into your load-path and the following into your
46 ;; ~/.emacs: (require 'windresize)
50 ;; - better help window
51 ;; - register key sequences as macros
52 ;; - maybe add numbers to window configurations in the ring
56 (require 'ring) ; for storing window configuration
57 (require 'windmove) ; for selecting adjacent window
61 (defconst windresize-version "0.6"
62 "The version number of the file windresize.el.")
64 (defcustom windresize-move-borders t
65 "Default method for resizing windows.
66 \\<windresize-map>Non-nil means that windresize will move borders.
67 For example, \\[windresize-left] will move the first movable border to the
68 left, trying to move the right border then the left border. \\[windresize-up]
69 will move the first movable border up, trying to move the bottom border then
72 Nil means that it will shrink or enlarge the window instead.
73 \\[windresize-down] and \\[windresize-up] will shrink and enlarge the window
74 vertically. \\[windresize-left] and \\[windresize-right] will shrink and
75 enlarge the window horizontally."
79 (defcustom windresize-default-increment 1
80 "The default number of lines for resizing windows."
84 (defcustom windresize-verbose 2
85 "Integer that say how verbose Windresize should be.
86 The higher the number, the more feedback Windresize will give.
87 A value of 0 will prevent any message to be displayed.
88 A value of 1 will display errors only.
89 A value of 2 will display errors and messages."
93 (defcustom windresize-ring-size 10
94 "The size of the ring for storing window configurations."
98 (defcustom windresize-windmove-relative-to-point 0
99 "Nil means select adjacent window relatively to the point position.
100 Non-nil means select adjacent window relatively to the window
101 edges. See the docstring of `windmove-up' for details."
105 (defcustom windresize-modifiers '((meta shift) meta
106 (control meta) control)
107 "A list of modifiers for arrow keys commands.
108 Each element can be a modifier or a list of modifiers.
110 The first modifier is for selecting windows with `windmove'.
111 The second modifier is for moving the up/left border instead of
112 the bottom/right border when there are two movable borders.
113 The third modifier is to move borders and keep the width/height
115 The fourth modifier is to move boder or resize window while
116 temporarily negating the increment value.
118 Make sure the four elements of this list are distinct to avoid
119 conflicts between keybindings."
122 (choice :tag "Modifier for selecting the adjacent windows"
123 (symbol :tag "Single modifier")
124 (repeat :tag "Multiple modifiers"
125 (symbol :tag "Modifier")))
126 (choice :tag "Modifier for moving the left/up border instead of the right/bottom border"
127 (symbol :tag "Single modifier")
128 (repeat :tag "Multiple modifiers"
129 (symbol :tag "Modifier")))
130 (choice :tag "Modifier for moving borders with fixed width/height"
131 (symbol :tag "Single modifier")
132 (repeat :tag "Multiple modifiers"
133 (symbol :tag "Modifier")))
134 (choice :tag "Modifier for negating increment temporarily"
135 (symbol :tag "Single modifier")
136 (repeat :tag "Multiple modifiers"
137 (symbol :tag "Modifier")))))
139 ;;; Variables and keymap:
141 (defvar windresize-msg '("" . 0))
142 (defvar windresize-buffer nil)
143 (defvar windresize-increment nil)
144 (defvar windresize-resizing nil)
145 (defvar windresize-configuration-ring nil)
146 (defvar windresize-window-configuration-0 nil)
147 (defvar windresize-overriding-terminal-local-map-0 nil)
148 (defvar windresize-overriding-menu-flag-0 nil)
150 (defvar windresize-map
151 (let ((map (make-sparse-keymap)))
152 (define-key map [remap self-insert-command] 'windresize-other-char)
153 (define-key map (kbd "M-x") 'windresize-other-char)
154 (define-key map (kbd "C-h") 'windresize-other-char)
155 ;; move borders outwards or shrink/enlarge
156 (define-key map [left] 'windresize-left)
157 (define-key map [right] 'windresize-right)
158 (define-key map [up] 'windresize-up)
159 (define-key map [down] 'windresize-down)
160 ;; Use windmove to select adjacent window. The default keybindings
161 ;; in `windresize-modifiers' should match those of windmove
162 (let ((mod (nth 0 windresize-modifiers)))
163 (if (symbolp mod) (setq mod (list mod)))
164 (define-key map (vector (append mod '(left))) 'windresize-select-left)
165 (define-key map (vector (append mod '(right))) 'windresize-select-right)
166 (define-key map (vector (append mod '(up))) 'windresize-select-up)
167 (define-key map (vector (append mod '(down))) 'windresize-select-down))
168 ;; Move the up/left border instead of bottom/right when there are
169 ;; two movable borders
170 (let ((mod (nth 1 windresize-modifiers)))
171 (if (symbolp mod) (setq mod (list mod)))
172 (define-key map (vector (append mod '(left))) 'windresize-left-force-left)
173 (define-key map (vector (append mod '(right))) 'windresize-right-force-left)
174 (define-key map (vector (append mod '(up))) 'windresize-up-force-up)
175 (define-key map (vector (append mod '(down))) 'windresize-down-force-up))
176 ;; Move borders with fixed width/height
177 (let ((mod (nth 2 windresize-modifiers)))
178 (if (symbolp mod) (setq mod (list mod)))
179 (define-key map (vector (append mod '(left))) 'windresize-left-fixed)
180 (define-key map (vector (append mod '(right))) 'windresize-right-fixed)
181 (define-key map (vector (append mod '(up))) 'windresize-up-fixed)
182 (define-key map (vector (append mod '(down))) 'windresize-down-fixed))
183 ;; Negate increment temporarily
184 (let ((mod (nth 3 windresize-modifiers)))
185 (if (symbolp mod) (setq mod (list mod)))
186 (define-key map (vector (append mod '(left))) 'windresize-left-minus)
187 (define-key map (vector (append mod '(right))) 'windresize-right-minus)
188 (define-key map (vector (append mod '(up))) 'windresize-up-minus)
189 (define-key map (vector (append mod '(down))) 'windresize-down-minus))
191 (define-key map "~" 'windresize-negate-increment)
192 (define-key map "+" 'windresize-increase-increment)
193 (define-key map "-" 'windresize-decrease-increment)
195 (define-key map "i" 'windresize-set-increment)
197 (define-key map " " 'windresize-toggle-method)
198 (define-key map "s" 'windresize-save-window-configuration)
199 (define-key map "r" 'windresize-restore-window-configuration)
200 ;; shorcut keys for manipulating windows
201 (define-key map "0" 'delete-window)
202 (define-key map "o" 'windresize-other-window)
203 (define-key map "n" 'windresize-next-window)
204 (define-key map "p" 'windresize-previous-window)
205 (define-key map "/" 'windresize-bottom-right)
206 (define-key map "\M-/" 'windresize-up-left)
207 (define-key map (kbd "\\") 'windresize-up-right)
208 (define-key map (kbd "M-\\") 'windresize-bottom-left)
209 (define-key map "1" 'windresize-delete-other-windows)
210 (define-key map "2" 'windresize-split-window-vertically)
211 (define-key map "3" 'windresize-split-window-horizontally)
212 (define-key map "=" 'windresize-balance-windows)
213 (define-key map "\C-xo" 'windresize-other-window)
214 (define-key map "\C-x0" 'windresize-delete-window)
215 (define-key map "\C-x1" 'windresize-delete-other-windows)
216 (define-key map "\C-x2" 'windresize-split-window-vertically)
217 (define-key map "\C-x3" 'windresize-split-window-horizontally)
218 (define-key map "\C-x+" 'windresize-balance-windows)
219 (define-key map (kbd "C-a")
220 (lambda() (interactive) (move-beginning-of-line 1)))
221 (define-key map (kbd "C-e")
222 (lambda() (interactive) (move-end-of-line 1)))
223 (define-key map "=" 'windresize-balance-windows)
224 (define-key map [mouse-1] 'mouse-set-point)
225 ;; help, save and exit
226 (define-key map (kbd "RET") 'windresize-exit)
227 (define-key map "x" 'windresize-exit)
228 (define-key map "\C-c\C-c" 'windresize-exit)
229 (define-key map "?" 'windresize-help)
230 (define-key map "q" 'windresize-cancel-and-quit)
231 (define-key map "c" 'windresize-cancel-and-quit)
232 (define-key map (kbd "C-g") 'windresize-cancel-and-quit)
234 "Keymap for `windresize'.")
236 (defun windresize-other-char ()
237 "Show a message instead of processing `self-insert-command'."
239 (let* ((key (if current-prefix-arg
240 (substring (this-command-keys)
241 universal-argument-num-events)
242 (this-command-keys))))
243 (cond ((vectorp key) (ding))
245 ;; send warning for only no warning for complex keys and
248 (cons (format "[`%s' not bound]" key) 1))))))
252 (defun windresize-other-window ()
253 "Select other window."
256 (setq windresize-msg (cons "[No other window]" 1))
258 (setq windresize-msg (cons (format "Now in %s" (buffer-name)) 2))))
260 (defalias 'windresize-next-window 'windresize-other-window)
262 (defun windresize-previous-window ()
263 "Select the previous window."
266 (setq windresize-msg (cons "[No previous window]" 1))
268 (setq windresize-msg (cons (format "Now in %s" (buffer-name)) 2))))
270 (defun windresize-delete-window ()
274 (setq windresize-msg (cons "[Can't delete sole window]" 1))
276 (setq windresize-msg (cons "Window deleted" 2))))
278 (defun windresize-delete-other-windows ()
279 "Delete other windows."
282 (setq windresize-msg (cons "[No other window]" 1))
283 (delete-other-windows)
284 (setq windresize-msg (cons "Windows deleted" 2))))
286 (defun windresize-split-window-horizontally ()
287 "Split window horizontally."
289 (split-window-horizontally)
290 (setq windresize-msg (cons "Window horizontally split" 2)))
292 (defun windresize-split-window-vertically ()
293 "Split window vertically."
295 (split-window-vertically)
296 (setq windresize-msg (cons "Window vertically split" 2)))
298 (defun windresize-balance-windows ()
302 (setq windresize-msg (cons "Windows balanced" 2)))
307 (defun windresize (&optional increment)
308 "Resize windows interactively.
309 INCREMENT is the number of lines by which borders should move.
311 By default, the method for resizing is by moving the borders.
312 The left/right key will move the only movable vertical border to
313 the left/right and the up/down key will move the only horizontal
314 movable border up/down. If there are two movable borders, the
315 right and the bottom border will have priority over the left and
316 upper border. You can reverse this priority by using \\[windresize-left-force-left],
317 \\[windresize-right-force-left], etc.
319 Resizing can also be done by increasing/decreasing the window
320 width and height. The up and down arrow keys will enlarge or
321 shrink the window vertically and the right and left arrow keys
322 will enlarge or shrink the window horizontally.
324 You can toggle the method with \\[windresize-toggle-method].
326 You can set the number of line by which a border should move by
327 calling \\[windresize-set-increment] with a numeric prefix.
328 You can temporarily negate the number of lines by which the
329 windows are resized by using \\[windresize-left-minus], \\[windresize-right-minus], etc.
330 If you want to permanently negate this increment value,
331 use `\\[windresize-negate-increment]' instead.
333 You can also save window configurations with `\\[windresize-save-window-configuration]' in a ring,
334 and restore them with `\\[windresize-restore-window-configuration]'.
336 `\\[windresize-cancel-and-quit]' will quit `windresize' and cancel any change. `\\[windresize-exit]'
337 will set the new window configuration and exit.
341 (if windresize-resizing
343 ;; FIXME shall we exit we calling again `windresize'?
344 ;; (progn (windresize-message '("[Already resizing]" . 0))
346 (setq windresize-overriding-terminal-local-map-0
347 overriding-terminal-local-map)
348 (setq windresize-overriding-menu-flag-0
349 overriding-local-map-menu-flag)
350 (setq windresize-window-configuration-0
351 (current-window-configuration))
352 ;; set increment, window configuration ring, initial buffer
353 (setq windresize-increment windresize-default-increment)
354 (setq windresize-configuration-ring
355 (make-ring windresize-ring-size))
356 (ring-insert windresize-configuration-ring
357 (current-window-configuration))
358 (setq windresize-buffer (current-buffer))
359 ;; set overriding map and pre/post-command hooks
360 (setq overriding-terminal-local-map windresize-map)
361 (setq overriding-local-map-menu-flag t)
362 (windresize-add-command-hooks)
363 ;; set the initial message
366 (setq windresize-msg (cons "Split window with [23]" 2))
367 (setq windresize-msg (cons "" 0))))
368 (setq windresize-resizing t)
369 (windresize-message)))
371 (defun windresize-message (&optional msg)
372 "Display a message at the bottom of the screen.
373 If MSG is nil, use `windresize-msg' instead."
374 (let* ((msg0 (or msg windresize-msg))
377 (method (if windresize-move-borders
378 "move borders " "resize window")))
379 (cond ((< msg-l 2) ; information
380 (add-text-properties 0 (length msg-t) '(face bold) msg-t))
381 ((< msg-l 3) ; warnings
382 (add-text-properties 0 (length msg-t) '(face shadow) msg-t)))
383 (add-text-properties 0 (length method) '(face bold) method)
384 (message "Use arrow keys to %s by %d %s RET:set ?:help %s"
385 method windresize-increment
386 (if (not (equal (abs windresize-increment) 1))
388 (if (<= (cdr windresize-msg) windresize-verbose)
391 (defun windresize-add-command-hooks ()
392 "Add hooks to commands when entering `windresize'."
393 (add-hook 'pre-command-hook 'windresize-pre-command)
394 (add-hook 'post-command-hook 'windresize-post-command))
396 (defun windresize-remove-command-hooks ()
397 "Remove hooks to commands when exiting `windresize'."
398 (remove-hook 'pre-command-hook 'windresize-pre-command)
399 (remove-hook 'post-command-hook 'windresize-post-command))
401 (defun windresize-pre-command ()
402 "Pre-command in `windresize'."
403 (setq windresize-msg (cons "" 0)))
405 (defun windresize-post-command ()
406 "Post-command in `windresize'."
407 (windresize-message))
409 (defun windresize-toggle-method ()
410 "Toggle resizing method."
412 (setq windresize-move-borders
413 (not windresize-move-borders))
417 (if (not windresize-move-borders)
418 "resize window" "move borders")) 2)))
420 ;;; Use windmove to select the adjacent window:
422 (defun windresize-select-down (&optional arg)
423 "Select the window below the current one.
424 If ARG is nil or zero, select the window relatively to the point
425 position. If ARG is positive, select relatively to the left edge
426 and select relatively to the right edge otherwise."
430 (or arg windresize-windmove-relative-to-point))
431 (error (setq windresize-msg
432 (cons "[Can't select window below this one]" 1)))))
434 (defun windresize-select-up (&optional arg)
435 "Select the window above the current one.
436 If ARG is nil or zero, select the window relatively to the point
437 position. If ARG is positive, select relatively to the left edge
438 and select relatively to the right edge otherwise."
442 (or arg windresize-windmove-relative-to-point))
443 (error (setq windresize-msg
444 (cons "[Can't select window above this one]" 1)))))
446 (defun windresize-select-left (&optional arg)
447 "Select the window to the left of the current one.
448 If ARG is nil or zero, select the window relatively to the point
449 position. If ARG is positive, select relatively to the top edge
450 and select relatively to the bottom edge otherwise."
454 (or arg windresize-windmove-relative-to-point))
455 (error (setq windresize-msg
456 (cons "[Can't select window left this one]" 1)))))
458 (defun windresize-select-right (&optional arg)
459 "Select the window to the right of the current one.
460 If ARG is nil or zero, select the window relatively to the point
461 position. If ARG is positive, select relatively to the top edge
462 and select relatively to the bottom edge otherwise."
466 (or arg windresize-windmove-relative-to-point))
467 (error (setq windresize-msg
468 (cons "[Can't select window right this one]" 1)))))
470 ;;; Increase/decrease/set the increment value:
472 (defun windresize-set-increment (&optional n)
473 "Set the increment value to N."
475 (setq windresize-increment n)
476 (setq windresize-msg (cons "Increment set" 2)))
478 (defun windresize-negate-increment (&optional silent)
479 "Negate the increment value.
480 If SILENT, dont output a message."
482 (setq windresize-increment (- windresize-increment))
483 (unless silent (setq windresize-msg (cons "Negated increment" 2))))
485 (defun windresize-increase-increment (&optional silent)
486 "Increase the increment.
487 If SILENT is non-nil, don't output a message."
489 (let ((i windresize-increment))
490 (if (eq i -1) (setq i (- i)) (setq i (1+ i)))
491 (setq windresize-increment i))
492 (unless silent (setq windresize-msg (cons "Increased increment" 2))))
494 (defun windresize-decrease-increment (&optional silent)
495 "Decrease the increment.
496 If SILENT is non-nil, don't output a message."
498 (let ((i windresize-increment))
499 (if (eq i 1) (setq i (- 1)) (setq i (1- i)))
500 (setq windresize-increment i))
501 (unless silent (setq windresize-msg (cons "Decreased increment" 2))))
503 ;;; Window configuration ring:
505 (defun windresize-save-window-configuration ()
506 "Save the current window configuration in the ring."
508 (if (equal (ring-ref windresize-configuration-ring 0)
509 (current-window-configuration))
511 (cons "[Same window configuration: not saved]" 1))
512 (ring-insert windresize-configuration-ring
513 (current-window-configuration))
515 (cons "Configuration saved -- use `r' to restore" 2))))
517 (defun windresize-restore-window-configuration ()
518 "Restore the previous window configuration in the ring."
520 (let ((wcf (ring-remove windresize-configuration-ring 0)))
521 (set-window-configuration wcf)
522 (ring-insert-at-beginning windresize-configuration-ring wcf))
523 (setq windresize-msg (cons "Previous configuration restored" 2)))
525 ;;; Commands for arrow keys:
527 (defun windresize-left (&optional n left-border fixed-width)
528 "Main function for handling left commands.
529 N is the number of lines by which moving borders.
530 In the move-border method, move the right border to the left.
531 If LEFT-BORDER is non-nil, move the left border to the left.
532 In the resize-window method, shrink the window horizontally.
534 If FIXED-WIDTH is non-nil and both left and right borders are
535 movable, move the window to the left and preserve its width."
537 (let* ((left-w (windmove-find-other-window 'left))
538 (right-w (windmove-find-other-window 'right))
539 (i (if n (prefix-numeric-value n) windresize-increment))
540 (shrink-ok (> (- (window-width) i) window-min-width))
541 (w (selected-window)))
542 (if (not windresize-move-borders)
545 (cons "[Can't shrink window horizontally]" 1))
547 (if shrink-ok (shrink-window-horizontally i)
549 (error (setq windresize-msg
550 (cons "[Can't shrink window horizontally]" 1)))))
551 (cond ((equal (frame-width) (window-width))
552 (setq windresize-msg (cons "No vertical split" 2)))
553 ((and left-w right-w)
555 (progn (windmove-left windresize-windmove-relative-to-point)
556 (adjust-window-trailing-edge (selected-window) (- i) t)
558 (if fixed-width (windresize-left)))
560 (progn (adjust-window-trailing-edge w (- i) t)
561 (if fixed-width (windresize-left nil t)))
562 (error (setq windresize-msg
563 (cons "[Can't move right border left]" 1))))))
566 (adjust-window-trailing-edge left-w (- i) t)
567 (error (setq windresize-msg (cons "[Can't move left border left]" 1)))))
568 (right-w (windresize-left-inwards))
569 (t (setq windresize-msg (cons "[Can't move border]" 1)))))))
571 (defun windresize-right (&optional n left-border fixed-width)
572 "Main function for handling right commands.
573 N is the number of lines by which moving borders.
574 In the move-border method, move the right border to the right.
575 If LEFT-BORDER is non-nil, move the left border to the right.
576 In the resize-window method, enlarge the window horizontally.
578 If FIXED-WIDTH is non-nil and both left and right borders are
579 movable, move the window to the right and preserve its width."
581 (let ((right-w (windmove-find-other-window 'right))
582 (left-w (windmove-find-other-window 'left))
583 (i (if n (prefix-numeric-value n) windresize-increment))
584 (wcf (current-window-configuration))
585 (w (selected-window)))
586 (if (not windresize-move-borders)
587 (progn (ignore-errors (enlarge-window-horizontally i))
588 (if (equal wcf (current-window-configuration))
590 (cons "[Can't enlarge window horizontally]" 1))))
591 (cond ((equal (frame-width) (window-width))
592 (setq windresize-msg (cons "No vertical split" 2)))
593 ((and right-w left-w left-border)
594 (progn (windmove-left windresize-windmove-relative-to-point)
595 (adjust-window-trailing-edge left-w i t)
597 (if fixed-width (windresize-right))))
600 (adjust-window-trailing-edge w i t)
601 (error (setq windresize-msg
602 (cons "[Can't move right border right]" 1)))))
603 (left-w (windresize-right-inwards))
604 (t (setq windresize-msg (cons "[Can't move border]" 1)))))))
606 (defun windresize-up (&optional n upper-border fixed-height)
607 "Main function for handling up commands.
608 N is the number of lines by which moving borders.
609 In the move-border method, move the bottom border upwards.
610 If UPPER-BORDER is non-nil, move the upper border upwards.
611 In the resize-window method, enlarge the window vertically.
613 If FIXED-HEIGHT is non-nil and both the upper and lower borders
614 are movable, move the window up and preserve its height."
616 (let ((up-w (windmove-find-other-window 'up))
617 (down-w (windmove-find-other-window 'down))
618 (i (if n (prefix-numeric-value n) windresize-increment))
619 (wcf (current-window-configuration))
620 (w (selected-window)))
621 (if (not windresize-move-borders)
622 (progn (ignore-errors (enlarge-window i))
623 (if (equal wcf (current-window-configuration))
625 (cons "[Can't enlarge window vertically]" 1))))
626 (cond ((equal (frame-height) (1+ (window-height)))
627 (setq windresize-msg (cons "No horizontal split" 2)))
628 ((and up-w down-w (not (window-minibuffer-p down-w)))
630 (progn (windmove-up windresize-windmove-relative-to-point)
631 (adjust-window-trailing-edge (selected-window) (- i) nil)
633 (if fixed-height (windresize-up)))
635 (adjust-window-trailing-edge w (- i) nil)
638 (cons "[Can't move bottom border up]" 1))))))
639 (up-w (condition-case nil
640 (adjust-window-trailing-edge up-w (- i) nil)
641 (error (setq windresize-msg
642 (cons "[Can't move upper border up]" 1)))))
643 ((and down-w (not (window-minibuffer-p down-w)))
644 (windresize-up-inwards))
645 (t (setq windresize-msg (cons "[Can't move border]" 1)))))))
647 (defun windresize-down (&optional n upper-border fixed-height)
648 "Main function for handling down commands.
649 N is the number of lines by which moving borders.
650 In the move-border method, move the bottom border down.
651 If UPPER-BORDER is non-nil, move the upper border down.
652 In the resize-window method, shrink the window vertically.
654 If FIXED-HEIGHT is non-nil and both the upper and lower borders
655 are movable, move the window down and preserve its height."
657 (let* ((down-w (windmove-find-other-window 'down))
658 (up-w (windmove-find-other-window 'up))
659 (i (if n (prefix-numeric-value n) windresize-increment))
660 (shrink-ok (> (- (window-width) i) window-min-width))
661 (wcf (current-window-configuration))
662 (w (selected-window)))
663 (if (not windresize-move-borders)
664 (if (or (and (window-minibuffer-p down-w) (not up-w))
665 (< (- (window-height) i) window-min-height))
666 (setq windresize-msg (cons "[Can't shrink window vertically]" 1))
667 (if shrink-ok (shrink-window i)
668 (setq windresize-msg (cons "[Can't shrink window vertically]" 1))))
669 (cond ((equal (frame-height) (1+ (window-height)))
670 (setq windresize-msg (cons "No horizontal split" 2)))
671 ((and up-w down-w (not (window-minibuffer-p down-w))
673 (progn (windmove-up windresize-windmove-relative-to-point)
674 (adjust-window-trailing-edge (selected-window) i nil)
676 (if fixed-height (windresize-down))))
677 ((and down-w (not (window-minibuffer-p down-w)))
679 (adjust-window-trailing-edge w i nil)
680 (error (setq windresize-msg (cons "[Can't move bottom border down]" 1)))))
681 (up-w (windresize-down-inwards))
682 (t (setq windresize-msg (cons "[Can't move border]" 1)))))))
684 ;;; Moving the opposite border inwards:
686 (defun windresize-left-inwards (&optional n)
687 "Move the right border left by N lines."
689 (let ((i (if n (prefix-numeric-value n) windresize-increment)))
691 (adjust-window-trailing-edge (selected-window) (- i) t)
692 (error (setq windresize-msg
693 (cons "[Can't move right border to the left]" 1))))))
695 (defun windresize-right-inwards (&optional n)
696 "Move the left border right by N lines."
698 (let ((i (if n (prefix-numeric-value n) windresize-increment))
699 (left-w (windmove-find-other-window 'left)))
701 (if left-w (adjust-window-trailing-edge left-w i t) (error t))
702 (error (setq windresize-msg
703 (cons "[Can't move left border right]" 1))))))
705 (defun windresize-up-inwards (&optional n)
706 "Move the bottom border up by N lines."
708 (let ((i (if n (prefix-numeric-value n) windresize-increment))
709 (down-w (windmove-find-other-window 'down)))
711 (progn (if (window-minibuffer-p down-w)
713 (cons "[Can't move bottom border up]" 1)))
714 (adjust-window-trailing-edge
715 (selected-window) (- i) nil))
716 (error (setq windresize-msg
717 (cons "[Can't move bottom border up]" 1))))))
719 (defun windresize-down-inwards (&optional n)
720 "Move the upper border down by N lines."
722 (let ((i (if n (prefix-numeric-value n) windresize-increment))
723 (wcf (current-window-configuration))
724 (up-w (windmove-find-other-window 'up)))
726 (if up-w (adjust-window-trailing-edge up-w i nil)
728 (error (setq windresize-msg
729 (cons "[Can't move upper border down]" 1))))))
731 ;;; Arrow keys temoporarily negating the increment value:
733 (defun windresize-down-minus ()
734 "Same as `windresize-left' but negate `windresize-increment'."
736 (let ((i windresize-increment))
737 (windresize-decrease-increment t)
739 (windresize-increase-increment t)))
741 (defun windresize-right-minus ()
742 "Same as `windresize-left' but negate `windresize-increment'."
744 (let ((i windresize-increment))
745 (windresize-decrease-increment t)
747 (windresize-increase-increment t)))
749 (defun windresize-up-minus ()
750 "Same as `windresize-left' but negate `windresize-increment'."
752 (let ((i windresize-increment))
753 (windresize-decrease-increment t)
755 (windresize-increase-increment t)))
757 (defun windresize-left-minus ()
758 "Same as `windresize-left' but negate `windresize-increment'."
760 (let ((i windresize-increment))
761 (windresize-decrease-increment t)
763 (windresize-increase-increment t)))
765 ;;; Let's left/up borders have priority over right/bottom borders:
767 (defun windresize-left-force-left (&optional n)
768 "If two movable borders, move the left border.
769 N is the number of lines by which moving borders."
771 (let ((i (if n (prefix-numeric-value n)
772 windresize-increment)))
773 (windresize-left i t)))
775 (defun windresize-right-force-left (&optional n)
776 "If two movable borders, move the left border.
777 N is the number of lines by which moving borders."
779 (let ((i (if n (prefix-numeric-value n)
780 windresize-increment)))
781 (windresize-right i t)))
783 (defun windresize-up-force-up (n)
784 "If two movable borders, move the upper border.
785 N is the number of lines by which moving borders."
787 (let ((i (if n (prefix-numeric-value n)
788 windresize-increment)))
789 (windresize-up i t)))
791 (defun windresize-down-force-up (n)
792 "If two movable borders, move the upper border.
793 N is the number of lines by which moving borders."
795 (let ((i (if n (prefix-numeric-value n)
796 windresize-increment)))
797 (windresize-down i t)))
799 ;;; Move the whole window, with fixed width/height:
801 (defun windresize-left-fixed ()
802 "Move the window left, keeping its width constant."
804 (windresize-left nil t t))
806 (defun windresize-right-fixed ()
807 "Move the window right, keeping its width constant."
809 (windresize-right nil t t))
811 (defun windresize-up-fixed ()
812 "Move the window up, keeping its height constant."
814 (windresize-up nil t t))
816 (defun windresize-down-fixed ()
817 "Move the window down, keeping its height constant."
819 (windresize-down nil t t))
823 (defun windresize-bottom-right ()
824 "Call `windresize-right' and `windresize-down' successively.
825 In move-borders method, move the bottom-right edge of the window
826 outwards. In resize-window method, enlarge the window
827 horizontally and shrink it vertically."
832 (defun windresize-up-left ()
833 "Call `windresize-left' and `windresize-up' successively.
834 In move-borders method, move the upper-left edge of the window
835 outwards. In resize-window method, shrink the window
836 horizontally and enlarge it vertically."
838 (windresize-left nil t)
839 (windresize-up nil t))
841 (defun windresize-up-right ()
842 "Call `windresize-right' and `windresize-up' successively.
843 In move-borders method, move the upper-right edge of the window
844 outwards. In resize-window method, enlarge the window both
845 horizontally and horizontally."
848 (windresize-up nil t))
850 (defun windresize-bottom-left ()
851 "Call `windresize-left' and `windresize-up' successively.
852 In move-borders method, move the bottom-left edge of the window
853 outwards. In resize-window method, shrink the window both
854 horizontally and vertically."
856 (windresize-left nil t)
859 ;;; Cancel, exit and help:
861 (defun windresize-cancel-and-quit ()
862 "Cancel window resizing and quit `windresize'."
864 (if (eq major-mode 'help-mode)
866 (setq windresize-msg '("Help quit" . 2)))
867 (switch-to-buffer windresize-buffer)
868 (set-window-configuration windresize-window-configuration-0)
869 (setq overriding-local-map-menu-flag
870 windresize-overriding-terminal-local-map-0)
871 (setq overriding-terminal-local-map
872 windresize-overriding-menu-flag-0)
873 (message "Window resizing quit (not saved)")
874 (windresize-remove-command-hooks)
875 (setq windresize-resizing nil)))
877 (defun windresize-exit ()
878 "Keep this window configuration and exit `windresize'."
880 (setq overriding-local-map-menu-flag
881 windresize-overriding-terminal-local-map-0)
882 (setq overriding-terminal-local-map
883 windresize-overriding-menu-flag-0)
884 (message "Window configuration set")
885 (windresize-remove-command-hooks)
886 (setq windresize-resizing nil))
888 (defun windresize-help ()
889 "Display a help window for `windresize'."
891 (let ((pop-up-frames nil) ; otherwise we exit the loop
892 (temp-buffer-show-hook
894 (fit-window-to-buffer)
895 (shrink-window-if-larger-than-buffer)
896 (goto-char (point-min))
898 (while (re-search-forward
899 "^[ M][^\n:]+:\\|[0123~=oq]:\\|RET:" nil t)
900 (add-text-properties (match-beginning 0)
901 (match-end 0) '(face bold))))))
903 "Use the arrow keys to move a border into the arrow direction.
904 Right and bottom borders have priority over left and up borders.
905 Press SPC to toggle between moving borders and resizing windows,
906 where arrow keys mean shrink/enlarge.
908 Here is a list of default keybindings:
910 arrows: move border or resize windows =: balance windows
911 M-S-arrows: select adjacent window o: other-window
912 C-M-arrows: move window with fixed width/height 0: delete current window
913 C-arrows: temporarilly negate INCREMENT ~: negate INCREMENT
914 M-arrows: move with priority to left/up 1: delete other windows
915 i: set INCREMENT (to numeric prefix) 2: split window vertically
916 +/-: increase/decrease INCREMENT 3: split window horizontally
917 s: save window configuration q: cancel and quit
918 r: restore window configuration ?: show this help window
919 SPC: toggle method: move border, resize RET: set and exit
921 /: move right-bottom edge outwards or left-upper edge inwards
922 M-/: move left-upper edge outwards or right-bottom edge inwards
923 \\: move right-upper edge outwards or left-bottom edge inwards
924 M-\\: move left-bottom edge outwards or right-upper edge inwards
926 See the docstring of `windresize' for detailed description."))
927 (with-output-to-temp-buffer "*Help*"
930 (provide 'windresize)
932 ;;; windresize.el ends here