summaryrefslogtreecommitdiff
path: root/windresize.el
blob: 8757cd1dfc40122866eb403e5b2c91d53b6223d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
;;; windresize.el --- Resize windows interactively
;;
;; Copyright (C) 2011-2017  Free Software Foundation, Inc.
;;
;; Filename: windresize.el
;; Author: Bastien <bzg@gnu.org>
;; Maintainer: Bastien <bzg@gnu.org>
;; Keywords: window
;; Description: Set window configuration with keystrokes
;; Version: 0.1
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;
;; This is not part of GNU Emacs.

;;; Commentary:

;; This mode lets you edit the window configuration interactively just
;; by using the keyboard.
;;
;; To use it, type M-x windresize; this puts Emacs in a state
;; where the up/down and left/right arrow keys resize the window
;; dimensions.  To return Emacs to its ordinary state, type RET.
;;
;; See the docstring of `windresize' for a detailed description of the
;; other commands that are available while windresize is active.

;;; History:
;;
;; This was largely inspired by Hirose Yuuji and Bob Wiener original
;; `resize-window' as posted on the emacs-devel mailing list by Juanma.
;; This was also inspired by Lennart Borgman's bw-interactive.el (now
;; winsize.el). See related discussions on the emacs-devel mailing
;; list. Special thanks to Drew Adams, Juri Linkov, Stefan Monnier and
;; JunJie Nan for useful suggestions.
;;
;; Also check https://www.emacswiki.org/cgi-bin/wiki/WindowResize for
;; general hints on window resizing.
;;
;; Put this file into your load-path and the following into your
;;   ~/.emacs: (require 'windresize)
;;
;;; Todo:
;;
;; - better help window
;; - register key sequences as macros
;; - maybe add numbers to window configurations in the ring
;;
;;; Code:

(require 'ring)      ; for storing window configuration
(require 'windmove)  ; for selecting adjacent window

;;; User variables:

(defconst windresize-version "0.6"
  "The version number of the file windresize.el.")

(defcustom windresize-move-borders t
  "Default method for resizing windows.
\\<windresize-map>Non-nil means that windresize will move borders.
For example, \\[windresize-left] will move the first movable border to the
left, trying to move the right border then the left border.  \\[windresize-up]
will move the first movable border up, trying to move the bottom border then
the upper border.

Nil means that it will shrink or enlarge the window instead.
\\[windresize-down] and  \\[windresize-up] will shrink and enlarge the window
vertically.  \\[windresize-left] and \\[windresize-right] will shrink and
enlarge the window horizontally."
  :type 'boolean
  :group 'convenience)

(defcustom windresize-default-increment 1
  "The default number of lines for resizing windows."
  :type 'integer
  :group 'convenience)

(defcustom windresize-verbose 2
  "Integer that say how verbose Windresize should be.
The higher the number, the more feedback Windresize will give.
A value of 0 will prevent any message to be displayed.
A value of 1 will display errors only.
A value of 2 will display errors and messages."
  :type 'integer
  :group 'convenience)

(defcustom windresize-ring-size 10
  "The size of the ring for storing window configurations."
  :type 'integer
  :group 'convenience)

(defcustom windresize-windmove-relative-to-point 0
  "Nil means select adjacent window relatively to the point position.
Non-nil means select adjacent window relatively to the window
edges.  See the docstring of `windmove-up' for details."
  :group 'convenience
  :type 'integer)

(defcustom windresize-modifiers '((meta shift) meta
				  (control meta) control)
  "A list of modifiers for arrow keys commands.
Each element can be a modifier or a list of modifiers.

The first modifier is for selecting windows with `windmove'.
The second modifier is for moving the up/left border instead of
the bottom/right border when there are two movable borders.
The third modifier is to move borders and keep the width/height
size fixed.
The fourth modifier is to move boder or resize window while
temporarily negating the increment value.

Make sure the four elements of this list are distinct to avoid
conflicts between keybindings."
  :group 'convenience
  :type '(list
	  (choice :tag "Modifier for selecting the adjacent windows"
		  (symbol :tag "Single modifier")
		  (repeat :tag "Multiple modifiers"
			  (symbol :tag "Modifier")))
	  (choice :tag "Modifier for moving the left/up border instead of the right/bottom border"
		  (symbol :tag "Single modifier")
		  (repeat :tag "Multiple modifiers"
			  (symbol :tag "Modifier")))
	  (choice :tag "Modifier for moving borders with fixed width/height"
		  (symbol :tag "Single modifier")
		  (repeat :tag "Multiple modifiers"
			  (symbol :tag "Modifier")))
	  (choice :tag "Modifier for negating increment temporarily"
		  (symbol :tag "Single modifier")
		  (repeat :tag "Multiple modifiers"
			  (symbol :tag "Modifier")))))

;;; Variables and keymap:

(defvar windresize-msg '("" . 0))
(defvar windresize-buffer nil)
(defvar windresize-increment nil)
(defvar windresize-resizing nil)
(defvar windresize-configuration-ring nil)
(defvar windresize-window-configuration-0 nil)
(defvar windresize-overriding-terminal-local-map-0 nil)
(defvar windresize-overriding-menu-flag-0 nil)

(defvar windresize-map
  (let ((map (make-sparse-keymap)))
    (define-key map [remap self-insert-command] 'undefined)
    (define-key map (kbd "M-x") 'undefined)
    (define-key map (kbd "C-h") 'windresize-help)
    ;; move borders outwards or shrink/enlarge
    (define-key map [left] 'windresize-left)
    (define-key map [right] 'windresize-right)
    (define-key map [up] 'windresize-up)
    (define-key map [down] 'windresize-down)
    ;; Use windmove to select adjacent window. The default keybindings
    ;; in `windresize-modifiers' should match those of windmove
    (let ((mod (nth 0 windresize-modifiers)))
      (if (symbolp mod) (setq mod (list mod)))
      (define-key map (vector (append mod '(left))) 'windresize-select-left)
      (define-key map (vector (append mod '(right))) 'windresize-select-right)
      (define-key map (vector (append mod '(up))) 'windresize-select-up)
      (define-key map (vector (append mod '(down))) 'windresize-select-down))
    ;; Move the up/left border instead of bottom/right when there are
    ;; two movable borders
    (let ((mod (nth 1 windresize-modifiers)))
      (if (symbolp mod) (setq mod (list mod)))
      (define-key map (vector (append mod '(left))) 'windresize-left-force-left)
      (define-key map (vector (append mod '(right))) 'windresize-right-force-left)
      (define-key map (vector (append mod '(up))) 'windresize-up-force-up)
      (define-key map (vector (append mod '(down))) 'windresize-down-force-up))
    ;; Move borders with fixed width/height
    (let ((mod (nth 2 windresize-modifiers)))
      (if (symbolp mod) (setq mod (list mod)))
      (define-key map (vector (append mod '(left))) 'windresize-left-fixed)
      (define-key map (vector (append mod '(right))) 'windresize-right-fixed)
      (define-key map (vector (append mod '(up))) 'windresize-up-fixed)
      (define-key map (vector (append mod '(down))) 'windresize-down-fixed))
    ;; Negate increment temporarily
    (let ((mod (nth 3 windresize-modifiers)))
      (if (symbolp mod) (setq mod (list mod)))
      (define-key map (vector (append mod '(left))) 'windresize-left-minus)
      (define-key map (vector (append mod '(right))) 'windresize-right-minus)
      (define-key map (vector (append mod '(up))) 'windresize-up-minus)
      (define-key map (vector (append mod '(down))) 'windresize-down-minus))
    ;; Set the increment
    (define-key map "~" 'windresize-negate-increment)
    (define-key map "+" 'windresize-increase-increment)
    (define-key map "-" 'windresize-decrease-increment)
    ;; FIXME
    (define-key map "i" 'windresize-set-increment)
    ;; other keys
    (define-key map " " 'windresize-toggle-method)
    (define-key map "s" 'windresize-save-window-configuration)
    (define-key map "r" 'windresize-restore-window-configuration)
    ;; shortcut keys for manipulating windows
    (define-key map "0" 'delete-window)
    (define-key map "o" 'windresize-other-window)
    (define-key map "n" 'windresize-next-window)
    (define-key map "p" 'windresize-previous-window)
    (define-key map "/" 'windresize-bottom-right)
    (define-key map "\M-/" 'windresize-up-left)
    (define-key map (kbd "\\") 'windresize-up-right)
    (define-key map (kbd "M-\\") 'windresize-bottom-left)
    (define-key map "1" 'windresize-delete-other-windows)
    (define-key map "2" 'windresize-split-window-vertically)
    (define-key map "3" 'windresize-split-window-horizontally)
    (define-key map "=" 'windresize-balance-windows)
    (define-key map "\C-xo" 'windresize-other-window)
    (define-key map "\C-x0" 'windresize-delete-window)
    (define-key map "\C-x1" 'windresize-delete-other-windows)
    (define-key map "\C-x2" 'windresize-split-window-vertically)
    (define-key map "\C-x3" 'windresize-split-window-horizontally)
    (define-key map "\C-x+" 'windresize-balance-windows)
    (define-key map (kbd "C-a")
      (lambda() (interactive) (move-beginning-of-line 1)))
    (define-key map (kbd "C-e")
      (lambda() (interactive) (move-end-of-line 1)))
    (define-key map "=" 'windresize-balance-windows)
    (define-key map [mouse-1] 'mouse-set-point)
    ;; help, save and exit
    (define-key map (kbd "RET") 'windresize-exit)
    (define-key map "x" 'windresize-exit)
    (define-key map "\C-c\C-c" 'windresize-exit)
    (define-key map "?" 'windresize-help)
    (define-key map "q" 'windresize-cancel-and-quit)
    (define-key map "c" 'windresize-cancel-and-quit)
    (define-key map (kbd "C-g") 'windresize-cancel-and-quit)
    map)
  "Keymap for `windresize'.")

;;; Aliases:

(defun windresize-other-window ()
  "Select other window."
  (interactive)
  (if (one-window-p)
      (setq windresize-msg (cons "[No other window]" 1))
    (other-window 1)
    (setq windresize-msg (cons (format "Now in %s" (buffer-name)) 2))))

(defalias 'windresize-next-window 'windresize-other-window)

(defun windresize-previous-window ()
  "Select the previous window."
  (interactive)
  (if (one-window-p)
      (setq windresize-msg (cons "[No previous window]" 1))
    (other-window -1)
    (setq windresize-msg (cons (format "Now in %s" (buffer-name)) 2))))

(defun windresize-delete-window ()
  "Delete window."
  (interactive)
  (if (one-window-p)
      (setq windresize-msg (cons "[Can't delete sole window]" 1))
    (other-window 1)
    (setq windresize-msg (cons "Window deleted" 2))))

(defun windresize-delete-other-windows ()
  "Delete other windows."
  (interactive)
  (if (one-window-p)
      (setq windresize-msg (cons "[No other window]" 1))
    (delete-other-windows)
    (setq windresize-msg (cons "Windows deleted" 2))))

(defun windresize-split-window-horizontally ()
  "Split window horizontally."
  (interactive)
  (split-window-horizontally)
  (setq windresize-msg (cons "Window horizontally split" 2)))

(defun windresize-split-window-vertically ()
  "Split window vertically."
  (interactive)
  (split-window-vertically)
  (setq windresize-msg (cons "Window vertically split" 2)))

(defun windresize-balance-windows ()
  "Balance windows."
  ;; FIXME: How 'bout balance-windows-area?
  (interactive)
  (balance-windows)
  (setq windresize-msg (cons "Windows balanced" 2)))

;;; Windresize:

;;;###autoload
(defun windresize (&optional increment)
  "Resize windows interactively.
INCREMENT is the number of lines by which borders should move.

By default, the method for resizing is by moving the borders.
The left/right key will move the only movable vertical border to
the left/right and the up/down key will move the only horizontal
movable border up/down.  If there are two movable borders, the
right and the bottom border will have priority over the left and
upper border.  You can reverse this priority by using \\[windresize-left-force-left],
\\[windresize-right-force-left], etc.

Resizing can also be done by increasing/decreasing the window
width and height.  The up and down arrow keys will enlarge or
shrink the window vertically and the right and left arrow keys
will enlarge or shrink the window horizontally.

You can toggle the method with \\[windresize-toggle-method].

You can set the number of line by which a border should move by
calling \\[windresize-set-increment] with a numeric prefix.
You can temporarily negate the number of lines by which the
windows are resized by using \\[windresize-left-minus], \\[windresize-right-minus], etc.
If you want to permanently negate this increment value,
use `\\[windresize-negate-increment]' instead.

You can also save window configurations with `\\[windresize-save-window-configuration]' in a ring,
and restore them with `\\[windresize-restore-window-configuration]'.

`\\[windresize-cancel-and-quit]' will quit `windresize' and cancel any change.  `\\[windresize-exit]'
will set the new window configuration and exit.

\\{windresize-map}"
  (interactive "P")
  (if windresize-resizing
      (windresize-exit)
    ;; FIXME shall we exit when calling again `windresize'?
    ;;(progn (windresize-message '("[Already resizing]" . 0))
    ;;       (sit-for 2))
    (setq windresize-overriding-terminal-local-map-0
	  overriding-terminal-local-map)
    (setq windresize-overriding-menu-flag-0
	  overriding-local-map-menu-flag)
    (setq windresize-window-configuration-0
	  (current-window-configuration))
    ;; set increment, window configuration ring, initial buffer
    (setq windresize-increment windresize-default-increment)
    (setq windresize-configuration-ring
	(make-ring windresize-ring-size))
    (ring-insert windresize-configuration-ring
		 (current-window-configuration))
    (setq windresize-buffer (current-buffer))
    ;; set overriding map and pre/post-command hooks
    (setq overriding-terminal-local-map windresize-map)
    (setq overriding-local-map-menu-flag t)
    (windresize-add-command-hooks)
    ;; set the initial message
    (setq windresize-msg
	  (if (one-window-p)
              (cons "Split window with [23]" 2)
            (cons "" 0)))
    (setq windresize-resizing t)
    (windresize-message)))

(defun windresize-message (&optional msg)
  "Display a message at the bottom of the screen.
If MSG is nil, use `windresize-msg' instead."
  (let* ((msg0 (or msg windresize-msg))
	 (msg-l (cdr msg0))
	 (msg-t (car msg0))
	 (method (if windresize-move-borders
		     "move borders " "resize window")))
    (cond ((< msg-l 2) ; information
	   (add-text-properties 0 (length msg-t) '(face bold) msg-t))
	  ((< msg-l 3) ; warnings
	   (add-text-properties 0 (length msg-t) '(face shadow) msg-t)))
    (add-text-properties 0 (length method) '(face bold) method)
    (message "Use arrow keys to %s by %d %s  RET:set  ?:help  %s"
	     method windresize-increment
	     (if (not (equal (abs windresize-increment) 1))
		 "lines" "line ")
	     (if (<= (cdr windresize-msg) windresize-verbose)
		 msg-t ""))))

(defun windresize-add-command-hooks ()
  "Add hooks to commands when entering `windresize'."
  (add-hook 'pre-command-hook 'windresize-pre-command)
  (add-hook 'post-command-hook 'windresize-post-command))

(defun windresize-remove-command-hooks ()
  "Remove hooks to commands when exiting `windresize'."
  (remove-hook 'pre-command-hook 'windresize-pre-command)
  (remove-hook 'post-command-hook 'windresize-post-command))

(defun windresize-pre-command ()
  "Pre-command in `windresize'."
  (setq windresize-msg (cons "" 0)))

(defun windresize-post-command ()
  "Post-command in `windresize'."
  (windresize-message))

(defun windresize-toggle-method ()
  "Toggle resizing method."
  (interactive)
  (setq windresize-move-borders
	(not windresize-move-borders))
  (setq windresize-msg
	(cons (format
	       "Method: %s"
	       (if (not windresize-move-borders)
		   "resize window" "move borders")) 2)))

;;; Use windmove to select the adjacent window:

(defun windresize-select-down (&optional arg)
  "Select the window below the current one.
If ARG is nil or zero, select the window relatively to the point
position.  If ARG is positive, select relatively to the left edge
and select relatively to the right edge otherwise."
  (interactive "P")
  (condition-case nil
      (windmove-down
       (or arg windresize-windmove-relative-to-point))
    (error (setq windresize-msg
		 (cons "[Can't select window below this one]" 1)))))

(defun windresize-select-up (&optional arg)
  "Select the window above the current one.
If ARG is nil or zero, select the window relatively to the point
position.  If ARG is positive, select relatively to the left edge
and select relatively to the right edge otherwise."
  (interactive "P")
  (condition-case nil
      (windmove-up
       (or arg windresize-windmove-relative-to-point))
    (error (setq windresize-msg
		 (cons "[Can't select window above this one]" 1)))))

(defun windresize-select-left (&optional arg)
  "Select the window to the left of the current one.
If ARG is nil or zero, select the window relatively to the point
position.  If ARG is positive, select relatively to the top edge
and select relatively to the bottom edge otherwise."
  (interactive "P")
  (condition-case nil
      (windmove-left
       (or arg windresize-windmove-relative-to-point))
    (error (setq windresize-msg
		 (cons "[Can't select window left this one]" 1)))))

(defun windresize-select-right (&optional arg)
  "Select the window to the right of the current one.
If ARG is nil or zero, select the window relatively to the point
position.  If ARG is positive, select relatively to the top edge
and select relatively to the bottom edge otherwise."
  (interactive "P")
  (condition-case nil
      (windmove-right
       (or arg windresize-windmove-relative-to-point))
    (error (setq windresize-msg
		 (cons "[Can't select window right this one]" 1)))))

;;; Increase/decrease/set the increment value:

(defun windresize-set-increment (&optional n)
  "Set the increment value to N."
  (interactive "p")
  (setq windresize-increment n)
  (setq windresize-msg (cons "Increment set" 2)))

(defun windresize-negate-increment (&optional silent)
  "Negate the increment value.
If SILENT, dont output a message."
  (interactive)
  (setq windresize-increment (- windresize-increment))
  (unless silent (setq windresize-msg (cons "Negated increment" 2))))

(defun windresize-increase-increment (&optional silent)
  "Increase the increment.
If SILENT is non-nil, don't output a message."
  (interactive)
  (let ((i windresize-increment))
    (if (eq i -1) (setq i (- i)) (setq i (1+ i)))
    (setq windresize-increment i))
  (unless silent (setq windresize-msg (cons "Increased increment" 2))))

(defun windresize-decrease-increment (&optional silent)
  "Decrease the increment.
If SILENT is non-nil, don't output a message."
  (interactive)
  (let ((i windresize-increment))
    (if (eq i 1) (setq i (- 1)) (setq i (1- i)))
    (setq windresize-increment i))
  (unless silent (setq windresize-msg (cons "Decreased increment" 2))))

;;; Window configuration ring:

(defun windresize-save-window-configuration ()
  "Save the current window configuration in the ring."
  (interactive)
  (if (equal (ring-ref windresize-configuration-ring 0)
	     (current-window-configuration))
      (setq windresize-msg
	    (cons "[Same window configuration: not saved]" 1))
    (ring-insert windresize-configuration-ring
		 (current-window-configuration))
    (setq windresize-msg
	  (cons "Configuration saved -- use `r' to restore" 2))))

(defun windresize-restore-window-configuration ()
  "Restore the previous window configuration in the ring."
  (interactive)
  (let ((wcf (ring-remove windresize-configuration-ring 0)))
    (set-window-configuration wcf)
    (ring-insert-at-beginning windresize-configuration-ring wcf))
  (setq windresize-msg (cons "Previous configuration restored" 2)))

;;; Commands for arrow keys:

(defun windresize-left (&optional n left-border fixed-width)
  "Main function for handling left commands.
N is the number of lines by which moving borders.
In the move-border method, move the right border to the left.
If LEFT-BORDER is non-nil, move the left border to the left.
In the resize-window method, shrink the window horizontally.

If FIXED-WIDTH is non-nil and both left and right borders are
movable, move the window to the left and preserve its width."
  (interactive "P")
  (let* ((left-w (windmove-find-other-window 'left))
	 (right-w (windmove-find-other-window 'right))
	 (i (if n (prefix-numeric-value n) windresize-increment))
	 (shrink-ok (> (- (window-width) i) window-min-width))
	 (w (selected-window)))
    (if (not windresize-move-borders)
	(if (not shrink-ok)
	    (setq windresize-msg
		  (cons "[Can't shrink window horizontally]" 1))
	  (condition-case nil
	      (if shrink-ok (shrink-window-horizontally i)
		(error t))
	    (error (setq windresize-msg
			 (cons "[Can't shrink window horizontally]" 1)))))
      (cond ((equal (frame-width) (window-width))
	     (setq windresize-msg (cons "No vertical split" 2)))
	    ((and left-w right-w)
	     (if left-border
		 (progn (windmove-left windresize-windmove-relative-to-point)
			(adjust-window-trailing-edge (selected-window) (- i) t)
			(select-window w)
			(if fixed-width (windresize-left)))
	       (condition-case nil
		   (progn (adjust-window-trailing-edge w (- i) t)
			  (if fixed-width (windresize-left nil t)))
		 (error (setq windresize-msg
			      (cons "[Can't move right border left]" 1))))))
	    (left-w
	     (condition-case nil
		 (adjust-window-trailing-edge left-w (- i) t)
	       (error (setq windresize-msg (cons "[Can't move left border left]" 1)))))
	    (right-w (windresize-left-inwards))
	    (t (setq windresize-msg (cons "[Can't move border]" 1)))))))

(defun windresize-right (&optional n left-border fixed-width)
  "Main function for handling right commands.
N is the number of lines by which moving borders.
In the move-border method, move the right border to the right.
If LEFT-BORDER is non-nil, move the left border to the right.
In the resize-window method, enlarge the window horizontally.

If FIXED-WIDTH is non-nil and both left and right borders are
movable, move the window to the right and preserve its width."
  (interactive "P")
  (let ((right-w (windmove-find-other-window 'right))
	(left-w (windmove-find-other-window 'left))
	(i (if n (prefix-numeric-value n) windresize-increment))
	(wcf (current-window-configuration))
	(w (selected-window)))
    (if (not windresize-move-borders)
	(progn (ignore-errors (enlarge-window-horizontally i))
	       (if (equal wcf (current-window-configuration))
		   (setq windresize-msg
			 (cons "[Can't enlarge window horizontally]" 1))))
      (cond ((equal (frame-width) (window-width))
	     (setq windresize-msg (cons "No vertical split" 2)))
	    ((and right-w left-w left-border)
	     (progn (windmove-left windresize-windmove-relative-to-point)
		    (adjust-window-trailing-edge left-w i t)
		    (select-window w)
		    (if fixed-width (windresize-right))))
	    (right-w
	     (condition-case nil
		 (adjust-window-trailing-edge w i t)
	       (error (setq windresize-msg
			    (cons "[Can't move right border right]" 1)))))
	    (left-w (windresize-right-inwards))
	    (t (setq windresize-msg (cons "[Can't move border]" 1)))))))

(defun windresize-up (&optional n upper-border fixed-height)
  "Main function for handling up commands.
N is the number of lines by which moving borders.
In the move-border method, move the bottom border upwards.
If UPPER-BORDER is non-nil, move the upper border upwards.
In the resize-window method, enlarge the window vertically.

If FIXED-HEIGHT is non-nil and both the upper and lower borders
are movable, move the window up and preserve its height."
  (interactive "P")
  (let ((up-w (windmove-find-other-window 'up))
	(down-w (windmove-find-other-window 'down))
	(i (if n (prefix-numeric-value n) windresize-increment))
	(wcf (current-window-configuration))
	(w (selected-window)))
    (if (not windresize-move-borders)
	(progn (ignore-errors (enlarge-window i))
	       (if (equal wcf (current-window-configuration))
		   (setq windresize-msg
			 (cons "[Can't enlarge window vertically]" 1))))
      (cond ((equal (frame-height) (1+ (window-height)))
	     (setq windresize-msg (cons "No horizontal split" 2)))
	    ((and up-w down-w (not (window-minibuffer-p down-w)))
	     (if upper-border
		 (progn (windmove-up windresize-windmove-relative-to-point)
			(adjust-window-trailing-edge (selected-window) (- i) nil)
			(select-window w)
			(if fixed-height (windresize-up)))
	       (condition-case nil
		   (adjust-window-trailing-edge w (- i) nil)
		 (error
		  (setq windresize-msg
			(cons "[Can't move bottom border up]" 1))))))
	    (up-w (condition-case nil
		      (adjust-window-trailing-edge up-w (- i) nil)
		    (error (setq windresize-msg
				 (cons "[Can't move upper border up]" 1)))))
	    ((and down-w (not (window-minibuffer-p down-w)))
	     (windresize-up-inwards))
	    (t (setq windresize-msg (cons "[Can't move border]" 1)))))))

(defun windresize-down (&optional n upper-border fixed-height)
  "Main function for handling down commands.
N is the number of lines by which moving borders.
In the move-border method, move the bottom border down.
If UPPER-BORDER is non-nil, move the upper border down.
In the resize-window method, shrink the window vertically.

If FIXED-HEIGHT is non-nil and both the upper and lower borders
are movable, move the window down and preserve its height."
  (interactive "P")
  (let* ((down-w (windmove-find-other-window 'down))
	 (up-w (windmove-find-other-window 'up))
	 (i (if n (prefix-numeric-value n) windresize-increment))
	 (shrink-ok (> (- (window-width) i) window-min-width))
	 (w (selected-window)))
    (if (not windresize-move-borders)
	(if (or (and (window-minibuffer-p down-w) (not up-w))
		(< (- (window-height) i) window-min-height))
	    (setq windresize-msg (cons "[Can't shrink window vertically]" 1))
	  (if shrink-ok (shrink-window i)
	      (setq windresize-msg (cons "[Can't shrink window vertically]" 1))))
      (cond ((equal (frame-height) (1+ (window-height)))
	     (setq windresize-msg (cons "No horizontal split" 2)))
	    ((and up-w down-w (not (window-minibuffer-p down-w))
		  upper-border)
	     (progn (windmove-up windresize-windmove-relative-to-point)
		    (adjust-window-trailing-edge (selected-window) i nil)
		    (select-window w)
		    (if fixed-height (windresize-down))))
	    ((and down-w (not (window-minibuffer-p down-w)))
	     (condition-case nil
		 (adjust-window-trailing-edge w i nil)
	       (error (setq windresize-msg (cons "[Can't move bottom border down]" 1)))))
	    (up-w (windresize-down-inwards))
	    (t (setq windresize-msg (cons "[Can't move border]" 1)))))))

;;; Moving the opposite border inwards:

(defun windresize-left-inwards (&optional n)
  "Move the right border left by N lines."
  (interactive "P")
  (let ((i (if n (prefix-numeric-value n) windresize-increment)))
    (condition-case nil
	(adjust-window-trailing-edge (selected-window) (- i) t)
      (error (setq windresize-msg
		   (cons "[Can't move right border to the left]" 1))))))

(defun windresize-right-inwards (&optional n)
  "Move the left border right by N lines."
  (interactive "P")
  (let ((i (if n (prefix-numeric-value n) windresize-increment))
	(left-w (windmove-find-other-window 'left)))
    (condition-case nil
	(if left-w (adjust-window-trailing-edge left-w i t) (error t))
      (error (setq windresize-msg
		   (cons "[Can't move left border right]" 1))))))

(defun windresize-up-inwards (&optional n)
  "Move the bottom border up by N lines."
  (interactive "P")
  (let ((i (if n (prefix-numeric-value n) windresize-increment))
	(down-w (windmove-find-other-window 'down)))
    (condition-case nil
	(progn (if (window-minibuffer-p down-w)
		   (setq windresize-msg
			 (cons "[Can't move bottom border up]" 1)))
	       (adjust-window-trailing-edge
		(selected-window) (- i) nil))
      (error (setq windresize-msg
		   (cons "[Can't move bottom border up]" 1))))))

(defun windresize-down-inwards (&optional n)
  "Move the upper border down by N lines."
  (interactive "P")
  (let ((i (if n (prefix-numeric-value n) windresize-increment))
	(up-w (windmove-find-other-window 'up)))
    (condition-case nil
	(if up-w (adjust-window-trailing-edge up-w i nil)
	  (error t))
      (error (setq windresize-msg
		   (cons "[Can't move upper border down]" 1))))))

;;; Arrow keys temoporarily negating the increment value:

(defun windresize-down-minus ()
  "Same as `windresize-down' but negate `windresize-increment'."
  (interactive)
  (let ((windresize-increment (- windresize-increment)))
    (windresize-down)))

(defun windresize-right-minus ()
  "Same as `windresize-right' but negate `windresize-increment'."
  (interactive)
  (let ((windresize-increment (- windresize-increment)))
    (windresize-right)))

(defun windresize-up-minus ()
  "Same as `windresize-up' but negate `windresize-increment'."
  (interactive)
  (let ((windresize-increment (- windresize-increment)))
    (windresize-up)))

(defun windresize-left-minus ()
  "Same as `windresize-left' but negate `windresize-increment'."
  (interactive)
  (let ((windresize-increment (- windresize-increment)))
    (windresize-left)))

;;; Let's left/up borders have priority over right/bottom borders:

(defun windresize-left-force-left (&optional n)
  "If two movable borders, move the left border.
N is the number of lines by which moving borders."
  (interactive "P")
  (let ((i (if n (prefix-numeric-value n)
	     windresize-increment)))
    (windresize-left i t)))

(defun windresize-right-force-left (&optional n)
  "If two movable borders, move the left border.
N is the number of lines by which moving borders."
  (interactive "P")
  (let ((i (if n (prefix-numeric-value n)
	     windresize-increment)))
    (windresize-right i t)))

(defun windresize-up-force-up (n)
  "If two movable borders, move the upper border.
N is the number of lines by which moving borders."
  (interactive "P")
  (let ((i (if n (prefix-numeric-value n)
	     windresize-increment)))
    (windresize-up i t)))

(defun windresize-down-force-up (n)
  "If two movable borders, move the upper border.
N is the number of lines by which moving borders."
  (interactive)
  (let ((i (if n (prefix-numeric-value n)
	     windresize-increment)))
    (windresize-down i t)))

;;; Move the whole window, with fixed width/height:

(defun windresize-left-fixed ()
  "Move the window left, keeping its width constant."
  (interactive)
  (windresize-left nil t t))

(defun windresize-right-fixed ()
  "Move the window right, keeping its width constant."
  (interactive)
  (windresize-right nil t t))

(defun windresize-up-fixed ()
  "Move the window up, keeping its height constant."
  (interactive)
  (windresize-up nil t t))

(defun windresize-down-fixed ()
  "Move the window down, keeping its height constant."
  (interactive)
  (windresize-down nil t t))

;;; Move edges:

(defun windresize-bottom-right ()
  "Call `windresize-right' and `windresize-down' successively.
In move-borders method, move the bottom-right edge of the window
outwards.  In resize-window method, enlarge the window
horizontally and shrink it vertically."
  (interactive)
  (windresize-right)
  (windresize-down))

(defun windresize-up-left ()
  "Call `windresize-left' and `windresize-up' successively.
In move-borders method, move the upper-left edge of the window
outwards.  In resize-window method, shrink the window
horizontally and enlarge it vertically."
  (interactive)
  (windresize-left nil t)
  (windresize-up nil t))

(defun windresize-up-right ()
  "Call `windresize-right' and `windresize-up' successively.
In move-borders method, move the upper-right edge of the window
outwards.  In resize-window method, enlarge the window both
horizontally and horizontally."
  (interactive)
  (windresize-right)
  (windresize-up nil t))

(defun windresize-bottom-left ()
  "Call `windresize-left' and `windresize-up' successively.
In move-borders method, move the bottom-left edge of the window
outwards.  In resize-window method, shrink the window both
horizontally and vertically."
  (interactive)
  (windresize-left nil t)
  (windresize-down))

;;; Cancel, exit and help:

(defun windresize-cancel-and-quit ()
  "Cancel window resizing and quit `windresize'."
  (interactive)
  (if (derived-mode-p 'help-mode)
      (progn (View-quit)
	     (setq windresize-msg '("Help quit" . 2)))
    (switch-to-buffer windresize-buffer)
    (set-window-configuration windresize-window-configuration-0)
    (setq overriding-local-map-menu-flag
	  windresize-overriding-terminal-local-map-0)
    (setq overriding-terminal-local-map
	  windresize-overriding-menu-flag-0)
    (message "Window resizing quit (not saved)")
    (windresize-remove-command-hooks)
    (setq windresize-resizing nil)))

(defun windresize-exit ()
  "Keep this window configuration and exit `windresize'."
  (interactive)
  (setq overriding-local-map-menu-flag
	windresize-overriding-terminal-local-map-0)
  (setq overriding-terminal-local-map
	windresize-overriding-menu-flag-0)
  (message "Window configuration set")
  (windresize-remove-command-hooks)
  (setq windresize-resizing nil))

(defun windresize-help ()
  "Display a help window for `windresize'."
  (interactive)
  (let ((pop-up-frames nil) ; otherwise we exit the loop
	(temp-buffer-show-hook
	 (lambda ()
           (fit-window-to-buffer)
           (shrink-window-if-larger-than-buffer)
           (goto-char (point-min))
           (save-excursion
             (while (re-search-forward
                     "^[ M][^\n:]+:\\|[0123~=oq]:\\|RET:" nil t)
               (add-text-properties (match-beginning 0)
                                    (match-end 0) '(face bold))))))
	(help
"Use the arrow keys to move a border into the arrow direction.
Right and bottom borders have priority over left and up borders.
Press SPC to toggle between moving borders and resizing windows,
where arrow keys mean shrink/enlarge.

Here is a list of default keybindings:

    arrows:  move border or resize windows         =:  balance windows
M-S-arrows:  select adjacent window                o:  other-window
C-M-arrows:  move window with fixed width/height   0:  delete current window
  C-arrows:  temporarily negate INCREMENT          ~:  negate INCREMENT
  M-arrows:  move with priority to left/up         1:  delete other windows
         i:  set INCREMENT (to numeric prefix)     2:  split window vertically
       +/-:  increase/decrease INCREMENT           3:  split window horizontally
         s:  save window configuration             q:  cancel and quit
         r:  restore window configuration          ?:  show this help window
       SPC:  toggle method: move border, resize  RET:  set and exit

         /:  move right-bottom edge outwards or left-upper edge inwards
       M-/:  move left-upper edge outwards or right-bottom edge inwards
         \\:  move right-upper edge outwards or left-bottom edge inwards
       M-\\:  move left-bottom edge outwards or right-upper edge inwards

See the docstring of `windresize' for detailed description."))
    (with-output-to-temp-buffer "*Help*"
      (princ help))))

(provide 'windresize)

;;; windresize.el ends here