]> code.delx.au - gnu-emacs-elpa/blob - packages/windresize/windresize.el
Add windresize.el to GNU ELPA.
[gnu-emacs-elpa] / packages / windresize / windresize.el
1 ;;; windresize.el --- resize windows interactively
2 ;;
3 ;; Copyright (C) 2011 Free Software Foundation, Inc.
4 ;;
5 ;; Filename: windresize.el
6 ;; Version: 0.6d
7 ;; Author: Bastien <bzg AT altern DOT org>
8 ;; Maintainer: Bastien <bzg AT altern DOT org>
9 ;; Keywords: window
10 ;; Description: Set window configuration with keystrokes
11 ;;
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)
15 ;; any later version.
16 ;;
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.
21 ;;
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.
25 ;;
26 ;; This is not part of GNU Emacs.
27 ;;
28 ;;; Commentary:
29 ;;
30 ;; This mode let's you edit the window configuration interactively just
31 ;; by using the keyboard. Quickstart: M-x windresize
32 ;;
33 ;;; History:
34 ;;
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.
41 ;;
42 ;; Also check http://www.emacswiki.org/cgi-bin/wiki/WindowResize for
43 ;; general hints on window resizing.
44 ;;
45 ;; Put this file into your load-path and the following into your
46 ;; ~/.emacs: (require 'windresize)
47 ;;
48 ;;; Todo:
49 ;;
50 ;; - better help window
51 ;; - register key sequences as macros
52 ;; - maybe add numbers to window configurations in the ring
53 ;;
54 ;;; Code:
55
56 (require 'ring) ; for storing window configuration
57 (require 'windmove) ; for selecting adjacent window
58
59 ;;; User variables:
60
61 (defconst windresize-version "0.6"
62 "The version number of the file windresize.el.")
63
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
70 the upper border.
71
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."
76 :type 'boolean
77 :group 'convenience)
78
79 (defcustom windresize-default-increment 1
80 "The default number of lines for resizing windows."
81 :type 'integer
82 :group 'convenience)
83
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."
90 :type 'integer
91 :group 'convenience)
92
93 (defcustom windresize-ring-size 10
94 "The size of the ring for storing window configurations."
95 :type 'integer
96 :group 'convenience)
97
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."
102 :group 'convenience
103 :type 'integer)
104
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.
109
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
114 size fixed.
115 The fourth modifier is to move boder or resize window while
116 temporarily negating the increment value.
117
118 Make sure the four elements of this list are distinct to avoid
119 conflicts between keybindings."
120 :group 'convenience
121 :type '(list
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")))))
138
139 ;;; Variables and keymap:
140
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)
149
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))
190 ;; Set the increment
191 (define-key map "~" 'windresize-negate-increment)
192 (define-key map "+" 'windresize-increase-increment)
193 (define-key map "-" 'windresize-decrease-increment)
194 ;; FIXME
195 (define-key map "i" 'windresize-set-increment)
196 ;; other keys
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)
233 map)
234 "Keymap for `windresize'.")
235
236 (defun windresize-other-char ()
237 "Show a message instead of processing `self-insert-command'."
238 (interactive)
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))
244 ((stringp key)
245 ;; send warning for only no warning for complex keys and
246 ;; mouse events
247 (setq windresize-msg
248 (cons (format "[`%s' not bound]" key) 1))))))
249
250 ;;; Aliases:
251
252 (defun windresize-other-window ()
253 "Select other window."
254 (interactive)
255 (if (one-window-p)
256 (setq windresize-msg (cons "[No other window]" 1))
257 (other-window 1)
258 (setq windresize-msg (cons (format "Now in %s" (buffer-name)) 2))))
259
260 (defalias 'windresize-next-window 'windresize-other-window)
261
262 (defun windresize-previous-window ()
263 "Select the previous window."
264 (interactive)
265 (if (one-window-p)
266 (setq windresize-msg (cons "[No previous window]" 1))
267 (other-window -1)
268 (setq windresize-msg (cons (format "Now in %s" (buffer-name)) 2))))
269
270 (defun windresize-delete-window ()
271 "Delete window."
272 (interactive)
273 (if (one-window-p)
274 (setq windresize-msg (cons "[Can't delete sole window]" 1))
275 (other-window 1)
276 (setq windresize-msg (cons "Window deleted" 2))))
277
278 (defun windresize-delete-other-windows ()
279 "Delete other windows."
280 (interactive)
281 (if (one-window-p)
282 (setq windresize-msg (cons "[No other window]" 1))
283 (delete-other-windows)
284 (setq windresize-msg (cons "Windows deleted" 2))))
285
286 (defun windresize-split-window-horizontally ()
287 "Split window horizontally."
288 (interactive)
289 (split-window-horizontally)
290 (setq windresize-msg (cons "Window horizontally split" 2)))
291
292 (defun windresize-split-window-vertically ()
293 "Split window vertically."
294 (interactive)
295 (split-window-vertically)
296 (setq windresize-msg (cons "Window vertically split" 2)))
297
298 (defun windresize-balance-windows ()
299 "Balance windows."
300 (interactive)
301 (balance-windows)
302 (setq windresize-msg (cons "Windows balanced" 2)))
303
304 ;;; Windresize:
305
306 ;;;###autoload
307 (defun windresize (&optional increment)
308 "Resize windows interactively.
309 INCREMENT is the number of lines by which borders should move.
310
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.
318
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.
323
324 You can toggle the method with \\[windresize-toggle-method].
325
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.
332
333 You can also save window configurations with `\\[windresize-save-window-configuration]' in a ring,
334 and restore them with `\\[windresize-restore-window-configuration]'.
335
336 `\\[windresize-cancel-and-quit]' will quit `windresize' and cancel any change. `\\[windresize-exit]'
337 will set the new window configuration and exit.
338
339 \\{windresize-map}"
340 (interactive "P")
341 (if windresize-resizing
342 (windresize-exit)
343 ;; FIXME shall we exit we calling again `windresize'?
344 ;; (progn (windresize-message '("[Already resizing]" . 0))
345 ;; (sit-for 2))
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
364 (setq windresize-msg
365 (if (one-window-p)
366 (setq windresize-msg (cons "Split window with [23]" 2))
367 (setq windresize-msg (cons "" 0))))
368 (setq windresize-resizing t)
369 (windresize-message)))
370
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))
375 (msg-l (cdr msg0))
376 (msg-t (car msg0))
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))
387 "lines" "line ")
388 (if (<= (cdr windresize-msg) windresize-verbose)
389 msg-t ""))))
390
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))
395
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))
400
401 (defun windresize-pre-command ()
402 "Pre-command in `windresize'."
403 (setq windresize-msg (cons "" 0)))
404
405 (defun windresize-post-command ()
406 "Post-command in `windresize'."
407 (windresize-message))
408
409 (defun windresize-toggle-method ()
410 "Toggle resizing method."
411 (interactive)
412 (setq windresize-move-borders
413 (not windresize-move-borders))
414 (setq windresize-msg
415 (cons (format
416 "Method: %s"
417 (if (not windresize-move-borders)
418 "resize window" "move borders")) 2)))
419
420 ;;; Use windmove to select the adjacent window:
421
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."
427 (interactive "P")
428 (condition-case nil
429 (windmove-down
430 (or arg windresize-windmove-relative-to-point))
431 (error (setq windresize-msg
432 (cons "[Can't select window below this one]" 1)))))
433
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."
439 (interactive "P")
440 (condition-case nil
441 (windmove-up
442 (or arg windresize-windmove-relative-to-point))
443 (error (setq windresize-msg
444 (cons "[Can't select window above this one]" 1)))))
445
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."
451 (interactive "P")
452 (condition-case nil
453 (windmove-left
454 (or arg windresize-windmove-relative-to-point))
455 (error (setq windresize-msg
456 (cons "[Can't select window left this one]" 1)))))
457
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."
463 (interactive "P")
464 (condition-case nil
465 (windmove-right
466 (or arg windresize-windmove-relative-to-point))
467 (error (setq windresize-msg
468 (cons "[Can't select window right this one]" 1)))))
469
470 ;;; Increase/decrease/set the increment value:
471
472 (defun windresize-set-increment (&optional n)
473 "Set the increment value to N."
474 (interactive "p")
475 (setq windresize-increment n)
476 (setq windresize-msg (cons "Increment set" 2)))
477
478 (defun windresize-negate-increment (&optional silent)
479 "Negate the increment value.
480 If SILENT, dont output a message."
481 (interactive)
482 (setq windresize-increment (- windresize-increment))
483 (unless silent (setq windresize-msg (cons "Negated increment" 2))))
484
485 (defun windresize-increase-increment (&optional silent)
486 "Increase the increment.
487 If SILENT is non-nil, don't output a message."
488 (interactive)
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))))
493
494 (defun windresize-decrease-increment (&optional silent)
495 "Decrease the increment.
496 If SILENT is non-nil, don't output a message."
497 (interactive)
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))))
502
503 ;;; Window configuration ring:
504
505 (defun windresize-save-window-configuration ()
506 "Save the current window configuration in the ring."
507 (interactive)
508 (if (equal (ring-ref windresize-configuration-ring 0)
509 (current-window-configuration))
510 (setq windresize-msg
511 (cons "[Same window configuration: not saved]" 1))
512 (ring-insert windresize-configuration-ring
513 (current-window-configuration))
514 (setq windresize-msg
515 (cons "Configuration saved -- use `r' to restore" 2))))
516
517 (defun windresize-restore-window-configuration ()
518 "Restore the previous window configuration in the ring."
519 (interactive)
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)))
524
525 ;;; Commands for arrow keys:
526
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.
533
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."
536 (interactive "P")
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)
543 (if (not shrink-ok)
544 (setq windresize-msg
545 (cons "[Can't shrink window horizontally]" 1))
546 (condition-case nil
547 (if shrink-ok (shrink-window-horizontally i)
548 (error t))
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)
554 (if left-border
555 (progn (windmove-left windresize-windmove-relative-to-point)
556 (adjust-window-trailing-edge (selected-window) (- i) t)
557 (select-window w)
558 (if fixed-width (windresize-left)))
559 (condition-case nil
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))))))
564 (left-w
565 (condition-case nil
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)))))))
570
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.
577
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."
580 (interactive "P")
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))
589 (setq windresize-msg
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)
596 (select-window w)
597 (if fixed-width (windresize-right))))
598 (right-w
599 (condition-case nil
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)))))))
605
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.
612
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."
615 (interactive "P")
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))
624 (setq windresize-msg
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)))
629 (if upper-border
630 (progn (windmove-up windresize-windmove-relative-to-point)
631 (adjust-window-trailing-edge (selected-window) (- i) nil)
632 (select-window w)
633 (if fixed-height (windresize-up)))
634 (condition-case nil
635 (adjust-window-trailing-edge w (- i) nil)
636 (error
637 (setq windresize-msg
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)))))))
646
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.
653
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."
656 (interactive "P")
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))
672 upper-border)
673 (progn (windmove-up windresize-windmove-relative-to-point)
674 (adjust-window-trailing-edge (selected-window) i nil)
675 (select-window w)
676 (if fixed-height (windresize-down))))
677 ((and down-w (not (window-minibuffer-p down-w)))
678 (condition-case nil
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)))))))
683
684 ;;; Moving the opposite border inwards:
685
686 (defun windresize-left-inwards (&optional n)
687 "Move the right border left by N lines."
688 (interactive "P")
689 (let ((i (if n (prefix-numeric-value n) windresize-increment)))
690 (condition-case nil
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))))))
694
695 (defun windresize-right-inwards (&optional n)
696 "Move the left border right by N lines."
697 (interactive "P")
698 (let ((i (if n (prefix-numeric-value n) windresize-increment))
699 (left-w (windmove-find-other-window 'left)))
700 (condition-case nil
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))))))
704
705 (defun windresize-up-inwards (&optional n)
706 "Move the bottom border up by N lines."
707 (interactive "P")
708 (let ((i (if n (prefix-numeric-value n) windresize-increment))
709 (down-w (windmove-find-other-window 'down)))
710 (condition-case nil
711 (progn (if (window-minibuffer-p down-w)
712 (setq windresize-msg
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))))))
718
719 (defun windresize-down-inwards (&optional n)
720 "Move the upper border down by N lines."
721 (interactive "P")
722 (let ((i (if n (prefix-numeric-value n) windresize-increment))
723 (wcf (current-window-configuration))
724 (up-w (windmove-find-other-window 'up)))
725 (condition-case nil
726 (if up-w (adjust-window-trailing-edge up-w i nil)
727 (error t))
728 (error (setq windresize-msg
729 (cons "[Can't move upper border down]" 1))))))
730
731 ;;; Arrow keys temoporarily negating the increment value:
732
733 (defun windresize-down-minus ()
734 "Same as `windresize-left' but negate `windresize-increment'."
735 (interactive)
736 (let ((i windresize-increment))
737 (windresize-decrease-increment t)
738 (windresize-down)
739 (windresize-increase-increment t)))
740
741 (defun windresize-right-minus ()
742 "Same as `windresize-left' but negate `windresize-increment'."
743 (interactive)
744 (let ((i windresize-increment))
745 (windresize-decrease-increment t)
746 (windresize-right)
747 (windresize-increase-increment t)))
748
749 (defun windresize-up-minus ()
750 "Same as `windresize-left' but negate `windresize-increment'."
751 (interactive)
752 (let ((i windresize-increment))
753 (windresize-decrease-increment t)
754 (windresize-up)
755 (windresize-increase-increment t)))
756
757 (defun windresize-left-minus ()
758 "Same as `windresize-left' but negate `windresize-increment'."
759 (interactive)
760 (let ((i windresize-increment))
761 (windresize-decrease-increment t)
762 (windresize-left)
763 (windresize-increase-increment t)))
764
765 ;;; Let's left/up borders have priority over right/bottom borders:
766
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."
770 (interactive "P")
771 (let ((i (if n (prefix-numeric-value n)
772 windresize-increment)))
773 (windresize-left i t)))
774
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."
778 (interactive "P")
779 (let ((i (if n (prefix-numeric-value n)
780 windresize-increment)))
781 (windresize-right i t)))
782
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."
786 (interactive "P")
787 (let ((i (if n (prefix-numeric-value n)
788 windresize-increment)))
789 (windresize-up i t)))
790
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."
794 (interactive)
795 (let ((i (if n (prefix-numeric-value n)
796 windresize-increment)))
797 (windresize-down i t)))
798
799 ;;; Move the whole window, with fixed width/height:
800
801 (defun windresize-left-fixed ()
802 "Move the window left, keeping its width constant."
803 (interactive)
804 (windresize-left nil t t))
805
806 (defun windresize-right-fixed ()
807 "Move the window right, keeping its width constant."
808 (interactive)
809 (windresize-right nil t t))
810
811 (defun windresize-up-fixed ()
812 "Move the window up, keeping its height constant."
813 (interactive)
814 (windresize-up nil t t))
815
816 (defun windresize-down-fixed ()
817 "Move the window down, keeping its height constant."
818 (interactive)
819 (windresize-down nil t t))
820
821 ;;; Move edges:
822
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."
828 (interactive)
829 (windresize-right)
830 (windresize-down))
831
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."
837 (interactive)
838 (windresize-left nil t)
839 (windresize-up nil t))
840
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."
846 (interactive)
847 (windresize-right)
848 (windresize-up nil t))
849
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."
855 (interactive)
856 (windresize-left nil t)
857 (windresize-down))
858
859 ;;; Cancel, exit and help:
860
861 (defun windresize-cancel-and-quit ()
862 "Cancel window resizing and quit `windresize'."
863 (interactive)
864 (if (eq major-mode 'help-mode)
865 (progn (View-quit)
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)))
876
877 (defun windresize-exit ()
878 "Keep this window configuration and exit `windresize'."
879 (interactive)
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))
887
888 (defun windresize-help ()
889 "Display a help window for `windresize'."
890 (interactive)
891 (let ((pop-up-frames nil) ; otherwise we exit the loop
892 (temp-buffer-show-hook
893 '(lambda ()
894 (fit-window-to-buffer)
895 (shrink-window-if-larger-than-buffer)
896 (goto-char (point-min))
897 (save-excursion
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))))))
902 (help
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.
907
908 Here is a list of default keybindings:
909
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
920
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
925
926 See the docstring of `windresize' for detailed description."))
927 (with-output-to-temp-buffer "*Help*"
928 (princ help))))
929
930 (provide 'windresize)
931
932 ;;; windresize.el ends here