-
Notifications
You must be signed in to change notification settings - Fork 11
/
hycontrol.el
2105 lines (1863 loc) · 94.1 KB
/
hycontrol.el
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
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; hycontrol.el --- Interactive sizing, moving, replicating and deleting of windows and frames -*- lexical-binding: t; -*-
;;
;; Author: Bob Weiner
;;
;; Orig-Date: 1-Jun-16 at 15:35:36
;; Last-Mod: 10-Nov-24 at 14:57:49 by Bob Weiner
;;
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; Copyright (C) 2016-2024 Free Software Foundation, Inc.
;; See the "HY-COPY" file for license information.
;;
;; This file is part of GNU Hyperbole.
;;; Commentary:
;;
;; This library provides full interactive control of window and
;; frame sizes and locations utilizing quick single key commands.
;; It has the ability to change from increasing a window height by
;; 5 lines, {.5 h}, to moving a frame 82 pixels, {.82 right-arrow},
;; with just a few keystrokes (the leading . just resets the numeric
;; argument to 0 prior to typing the new number).
;;
;; See the Info manual entry "(hyperbole)HyControl" for reference
;; documentation. See below for key usage information.
;;
;; ----
;;
;; HyControl is invoked via either of two global minor modes under
;; the Hyperbole screen menu, both of which can toggle to the other
;; by pressing {t}. `hycontrol-enable-frames-mode' bound to {C-h h s
;; f} manages visible frame creation, deletion, sizing, position and
;; face zooming (enlarging and shrinking); if called interactively,
;; it stores the current frame configuration for restoration via a
;; press of the `)' key. `hycontrol-enable-windows-mode' manages
;; per frame window creation, deletion, sizing, reframing and face
;; zooming; if called interactively, it stores the current window
;; configuration for restoration via a press of the `)' key.
;; `hycontrol-enable-windows-mode' is typically bound by Hyperbole
;; to {C-c \ } or just use {C-h h s w}. Then press {t} if you want
;; to switch to frame control.
;;
;; With a HyControl minor mode active, a multi-line help summary of
;; most available key bindings is shown in the minibuffer. Simply
;; read this and try each command out to get a feel for it. Below
;; we highlight some of the most unique commands.
;;
;; ----
;;
;; In either HyControl mode, you can instantly create a grid of
;; windows to display many buffers by choosing a number of rows as
;; your first digit, then a number of columns of windows as the
;; second digit and then pressing {@}, e.g. {.26 @} produces 2 rows,
;; each with 6 columns of windows in the selected frame. Grids can
;; be from 1x1 to 9x9 windows. This command also works outside of a
;; HyControl mode when in Dired, Buffer Menu or IBuffer modes with
;; a prefix argument (no preceding period).
;;
;; The buffers displayed by the {@} command are chosen smartly.
;; With a current buffer in Dired, Buffer Menu or IBuffer mode with
;; marked items, the buffers associated with those items are
;; displayed first. Then the most recently used buffers are
;; displayed in each window, first selecting from buffers which
;; match any of the predicate expressions in
;; `hycontrol-display-buffer-predicate-list'. Then, if there are
;; not enough buffers for all windows, the buffers that failed to
;; match to any predicate are used. The default predicate list
;; chooses buffers with attached files. In all cases, buffers whose
;; names start with a space are filtered out. If a prefix argument
;; of 0 is given, a major mode symbol is prompted for and buffers
;; with that major mode are preferred for display instead of those
;; matching the predicate list.
;;
;; ----
;;
;; HyControl allows placement of frames at screen edges and corners
;; using the keys of the numeric keypad, matching their physical
;; layout, e.g. {3} moves to the lower right corner. Press {p} for
;; a prompt with a virtual numeric keypad if you lack a physical one.
;; You can also cycle through all of these placement positions with
;; the {c} key.
;;
;; HyControl can rapidly resize frames to common percentages of
;; screen sizes via a number of commands. Each press of {a} or {A}
;; cycles through resizing the selected frame's width and height
;; respectively to a percentage of the screen given by the lists,
;; `hycontrol-frame-widths' and `hycontrol-frame-heights', e.g. 25%,
;; 50%, etc. The keys: {i} top, {j} left, {k} right, and {m}
;; bottom, first maximize a frame to the respective screen edge and
;; then with successive presses, shrink the frame dimension
;; perpendicular to that edge by 50% while keeping the original edge
;; fixed in place. Try them and you will quickly see how they can
;; help.
;;
;; ----
;;
;; When HyControl creates a new frame, it automatically sizes it to the
;; same size as the previously selected frame and offsets it from that
;; frame by the (X . Y) number of pixels given in the variable,
;; `hycontrol-frame-offset'.
;;
;; A display screen may span multiple physical monitors. To prevent
;; widgets and toolbars at the corners of the screen from being
;; obscured, HyControl can offset each frame from each screen edge
;; by a fixed number of pixels. These offsets are specified by the
;; variable, `hycontrol-screen-offset-alist' and can differ for each
;; type of screen; see its documentation for details. If you change
;; its value, then call `hycontrol-set-screen-offsets' to set any
;; new offset values. `hycontrol-get-screen-offsets' returns the
;; list of offsets in clockwise order starting from the top edge.
;;
;; ----
;;
;; Please note that the frame zoom in/out commands on Z and z will
;; not work unless you have the separately available "zoom-frm.el"
;; library (which itself requires another library). If not available,
;; this command will just beep at you. The window-based zoom commands
;; utilize a built-in Emacs library, so they will always work under
;; any window system. These commands enlarge and shrink the default
;; text face.
;;; Code:
;;; ************************************************************************
;;; Other required Elisp libraries
;;; ************************************************************************
(require 'cl-lib)
(require 'hhist) ; To store frame-config when hycontrol-windows-grid is used
(require 'hypb)
;; Avoid any potential library name conflict by giving the load directory.
(require 'set (expand-file-name "set" hyperb:dir))
(eval-and-compile
(require 'framemove nil t) ;; Elpa package
(require 'windmove))
;; Frame face enlarging/shrinking (zooming) requires this separately available library.
;; Everything else works fine without it, so don't make it a required dependency.
(require 'zoom-frm nil t)
;;; ************************************************************************
;;; Public declarations
;;; ************************************************************************
(declare-function fm-next-frame "ext:framemove")
(defvar frame-zoom-font-difference)
(defvar hyperbole-mode-map) ; "hyperbole.el"
(defvar org-mode-map) ; "org.el"
(defvar outline-mode-map) ; "outline.el"
(defvar outline-minor-mode-map) ; "outline.el"
(declare-function kbd-key:key-series-to-events "hib-kbd")
;;; ************************************************************************
;;; Public variables
;;; ************************************************************************
(defvar hycontrol-debug nil
"Whether some HyControl functions log debugging messages to *Messages*.")
(defvar hycontrol-display-buffer-predicate-list
;; Display only buffers attached to files.
(list #'buffer-file-name)
"List of single buffer/name predicates.
If any predicate returns non-nil for a buffer, include that buffer in
the list to display in the windows created by `hycontrol-windows-grid'.
A predicate may be either a function that takes a single buffer
argument or a boolean expression, in which case the expression is
evaluated with the buffer argument as the current buffer, e.g. (eq
major-mode \\='c-mode).")
(defcustom hycontrol-help-flag t
"*Non-nil means show key binding help in the minibuffer when in a HyControl mode."
:type 'boolean
:group 'hyperbole-screen)
(defcustom hycontrol-invert-mode-line-flag t
"*Non-nil means invert mode-line to emphasize the special key bindings in effect."
:type 'boolean
:group 'hyperbole-screen)
(defcustom hycontrol-keep-window-flag nil
"*Non-nil means leave original window when tear off window to another frame."
:type 'boolean
:group 'hyperbole-screen)
(defcustom hycontrol-maximum-units 1000
"*Maximum units setting allowed for hycontrol commands.
The unit counter resets to the last digit entered whenever this
value is exceeded."
:type '(integer :match (lambda (_widget value)
(and (integerp value) (> value 0)
(<= value (max 1000 (display-pixel-width))))))
:group 'hyperbole-screen)
(defcustom hycontrol-frame-offset '(13 . 23)
"*Increase in offset for new hycontrol frames relative to the selected frame.
Its value is an (x-offset . y-offset) pair in pixels."
:type '(cons integer integer)
:group 'hyperbole-screen)
(defvar hycontrol-screen-offset-alist
'(((1920 . 1080) . (0 10 0 68)) ; 24" iMac HD display
((2560 . 1440) . (0 15 0 93)) ; 27" iMac HD display
(t . (0 0 0 0)))
"*Alist of screen predicate offset pairs.
The format is (screen-predicate . (top-offset right-offset
bottom-offset left-offset).
Offsets are integers given in pixels. The offsets associated with the first
matching screen-predicate are used in HyControl screen edge frame placement
commands; this is set when HyControl is first loaded/used.
Screen-predicate must be one of: a boolean function of no arguments, an
integer dotted pair of (width . height) in pixels to match to, or an Emacs
Lisp boolean form to evaluate.
The final predicate should always be t, for default values, typically of zero.")
(defcustom hycontrol-screen-top-offset 0
"*Pixel offset from top used when placing a frame at a top corner."
:type '(integer :match (lambda (_widget value)
(and (integerp value) (>= value 0)
(< value (display-pixel-height)))))
:group 'hyperbole-screen)
(defcustom hycontrol-screen-right-offset 0
"*Pixel offset from right used when placing a frame at a right corner."
:type '(integer :match (lambda (_widget value)
(and (integerp value) (>= value 0)
(< value (display-pixel-width)))))
:group 'hyperbole-screen)
(defcustom hycontrol-screen-bottom-offset 0
"*Pixel offset from bottom used when placing a frame at a bottom corner."
:type '(integer :match (lambda (_widget value)
(and (integerp value) (>= value 0)
(< value (display-pixel-height)))))
:group 'hyperbole-screen)
(defcustom hycontrol-screen-left-offset 0
"*Pixel offset from left used when placing a frame at a left corner."
:type '(integer :match (lambda (_widget value)
(and (integerp value) (>= value 0)
(< value (display-pixel-width)))))
:group 'hyperbole-screen)
(defcustom hycontrol-blank-buffer-name " BLANK"
"Blank buffer name used for to display in extra window grid windows.
Used after selected buffer list is exhausted. Start name with a space
for it to be omitted by `list-buffers'."
:type 'string
:group 'hyperbole-screen)
(defvar hycontrol-frame-widths
'(1.0 0.75 0.666 0.5 0.333 0.25)
"List of width percentages to cycle through when adjusting a frame's width.
0.75 and 75 are treated as the same percentage.")
(defvar hycontrol-frame-heights
'(1.0 0.75 0.666 0.5 0.333 0.25)
"List of height percentages to cycle through when adjusting a frame's height.
0.75 and 75 are treated as the same percentage.")
(defvar hycontrol-arg nil
"HyControl copy of `prefix-arg' that it changes within key bindings.
`pre-command-hook' synchronizes this value to `prefix-arg'.")
;;; ************************************************************************
;;; Public declarations
;;; ************************************************************************
(defvar ibuffer-mode-map)
;;; Frame Keys
(defvar hycontrol-frames-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map t) ;; Disable self-inserting keys and prefix keys
(define-key map [up] (lambda () (interactive) (hycontrol-move-frame 'up hycontrol-arg)))
(define-key map [down] (lambda () (interactive) (hycontrol-move-frame 'down hycontrol-arg)))
(define-key map [left] (lambda () (interactive) (hycontrol-move-frame 'left hycontrol-arg)))
(define-key map [right] (lambda () (interactive) (hycontrol-move-frame 'right hycontrol-arg)))
(define-key map [kp-0] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-0 hycontrol-arg)))
(define-key map [kp-1] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-1 hycontrol-arg)))
(define-key map [kp-2] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-2 hycontrol-arg)))
(define-key map [kp-3] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-3 hycontrol-arg)))
(define-key map [kp-4] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-4 hycontrol-arg)))
(define-key map [kp-5] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-5 hycontrol-arg)))
(define-key map [kp-6] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-6 hycontrol-arg)))
(define-key map [kp-7] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-7 hycontrol-arg)))
(define-key map [kp-8] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-8 hycontrol-arg)))
(define-key map [kp-9] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-9 hycontrol-arg)))
;; Clear hycontrol-arg
(define-key map "." 'hycontrol-reset-prefix-arg)
(define-key map "@" 'hycontrol-windows-grid)
(define-key map "?" 'hycontrol-toggle-help)
(define-key map "a" 'hycontrol-frame-adjust-widths)
(define-key map "A" 'hycontrol-frame-adjust-heights)
(define-key map "b" 'bury-buffer)
(define-key map "c" 'hycontrol-frame-to-screen-edges)
(define-key map "d" 'delete-frame)
(define-key map "D" 'hycontrol-delete-other-frames)
(define-key map "f" 'hycontrol-clone-window-to-new-frame)
(define-key map "F" 'hycontrol-window-to-new-frame)
(define-key map "\C-g" 'hycontrol-abort)
(define-key map "%" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-percentage-of-screen hycontrol-arg))))
(define-key map "H" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-height-percentage-of-screen hycontrol-arg))))
(define-key map "W" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-width-percentage-of-screen hycontrol-arg))))
(define-key map "h" (lambda () (interactive) (hycontrol-set-frame-height nil (+ (frame-height) hycontrol-arg))))
;; Move directionally among frames
(define-key map "I" 'hycontrol-framemove-up)
(define-key map "J" 'hycontrol-framemove-left)
(define-key map "K" 'hycontrol-framemove-right)
(define-key map "M" 'hycontrol-framemove-down)
(define-key map "i" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-resize-to-top hycontrol-arg))))
(define-key map "j" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-resize-to-left hycontrol-arg))))
(define-key map "k" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-resize-to-right hycontrol-arg))))
(define-key map "l" 'lower-frame)
(define-key map "m" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-resize-to-bottom hycontrol-arg))))
(define-key map "n" (lambda () (interactive) (hycontrol-set-frame-width nil (- (frame-width) hycontrol-arg))))
(define-key map "o" (lambda () (interactive) (let ((w (selected-window))) (other-window hycontrol-arg) (if (eq w (selected-window)) (other-window 1)))))
(define-key map "O" (lambda () (interactive) (let ((w (selected-window))) (other-frame hycontrol-arg) (if (eq w (selected-window)) (other-frame 1)))))
;; Numeric keypad emulation for keyboards that lack one.
(define-key map "p" (lambda () (interactive) (hycontrol-virtual-numeric-keypad hycontrol-arg)))
(define-key map "q" 'hycontrol-quit)
(define-key map "Q" 'hycontrol-quit)
(define-key map "r" 'raise-frame)
(define-key map "s" (lambda () (interactive) (hycontrol-set-frame-height nil (- (frame-height) hycontrol-arg))))
(define-key map "t" 'hycontrol-enable-windows-mode)
(define-key map "u" 'unbury-buffer)
(define-key map "w" (lambda () (interactive) (hycontrol-set-frame-width nil (+ (frame-width) hycontrol-arg))))
(define-key map "Z" (lambda () (interactive) (if (> hycontrol-arg 9) (setq hycontrol-arg 1)) (hycontrol-frame-zoom 'zoom-frm-in hycontrol-arg hycontrol-debug)))
(define-key map "z" (lambda () (interactive) (if (> hycontrol-arg 9) (setq hycontrol-arg 1)) (hycontrol-frame-zoom 'zoom-frm-out hycontrol-arg hycontrol-debug)))
(define-key map "\[" 'hycontrol-make-frame)
(define-key map "\]" 'hycontrol-make-frame)
(define-key map "\(" 'hycontrol-save-frame-configuration)
(define-key map "\)" 'hycontrol-restore-frame-configuration)
;; Something in this command's event handling when used within HyControl's event loop slows down
;; frame iconification under macOS 100-fold, so don't enable it until this issue is resolved.
;; (define-key map "^" 'iconify-frame)
(define-key map "~" (lambda () (interactive)
(unless (hycontrol-frame-swap-buffers)
(hycontrol-user-error hycontrol-debug "(HyControl): There must be at least two frames and the frame to swap to must have only a single window."))))
(define-key map "-" 'hycontrol-minus-key)
(define-key map "+" 'toggle-frame-maximized)
(define-key map "=" (lambda () (interactive)
(and (> (length (visible-frame-list)) 1)
(y-or-n-p "Resize all other frames to the size of the selected frame?")
(mapc (lambda (f)
(hycontrol-set-frame-size
f (frame-pixel-width) (frame-pixel-height) t))
(visible-frame-list)))))
(define-key map "\C-u" 'hycontrol-multiply-universal-arg)
(define-key map "0" (lambda () (interactive) (hycontrol-universal-arg-digit 0)))
(define-key map "1" (lambda () (interactive) (hycontrol-universal-arg-digit 1)))
(define-key map "2" (lambda () (interactive) (hycontrol-universal-arg-digit 2)))
(define-key map "3" (lambda () (interactive) (hycontrol-universal-arg-digit 3)))
(define-key map "4" (lambda () (interactive) (hycontrol-universal-arg-digit 4)))
(define-key map "5" (lambda () (interactive) (hycontrol-universal-arg-digit 5)))
(define-key map "6" (lambda () (interactive) (hycontrol-universal-arg-digit 6)))
(define-key map "7" (lambda () (interactive) (hycontrol-universal-arg-digit 7)))
(define-key map "8" (lambda () (interactive) (hycontrol-universal-arg-digit 8)))
(define-key map "9" (lambda () (interactive) (hycontrol-universal-arg-digit 9)))
map)
"Keymap to use when in Hyperbole HyControl frames mode.")
;;; Window Keys
;;;###autoload
(eval-after-load "buff-menu" '(define-key Buffer-menu-mode-map "@" 'hycontrol-windows-grid))
;;;###autoload
(eval-after-load "ibuffer" '(define-key ibuffer-mode-map "@" 'hycontrol-windows-grid))
;;;###autoload
(eval-after-load "dired" '(define-key dired-mode-map "@" 'hycontrol-windows-grid))
(defvar hycontrol-windows-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map t) ;; Disable self-inserting keys and prefix keys
(define-key map [up] (lambda () (interactive) (hycontrol-move-frame 'up hycontrol-arg)))
(define-key map [down] (lambda () (interactive) (hycontrol-move-frame 'down hycontrol-arg)))
(define-key map [left] (lambda () (interactive) (hycontrol-move-frame 'left hycontrol-arg)))
(define-key map [right] (lambda () (interactive) (hycontrol-move-frame 'right hycontrol-arg)))
(define-key map [kp-0] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-0 hycontrol-arg)))
(define-key map [kp-1] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-1 hycontrol-arg)))
(define-key map [kp-2] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-2 hycontrol-arg)))
(define-key map [kp-3] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-3 hycontrol-arg)))
(define-key map [kp-4] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-4 hycontrol-arg)))
(define-key map [kp-5] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-5 hycontrol-arg)))
(define-key map [kp-6] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-6 hycontrol-arg)))
(define-key map [kp-7] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-7 hycontrol-arg)))
(define-key map [kp-8] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-8 hycontrol-arg)))
(define-key map [kp-9] (lambda () (interactive) (hycontrol-numeric-keypad 'kp-9 hycontrol-arg)))
;; Clear hycontrol-arg
(define-key map "." 'hycontrol-reset-prefix-arg)
(define-key map "@" 'hycontrol-windows-grid)
(define-key map "?" 'hycontrol-toggle-help)
(define-key map "a" 'hycontrol-frame-adjust-widths)
(define-key map "A" 'hycontrol-frame-adjust-heights)
(define-key map "b" 'bury-buffer)
(define-key map "c" 'hycontrol-frame-to-screen-edges)
(define-key map "d" 'delete-window)
(define-key map "D" 'hycontrol-delete-other-windows)
(define-key map "f" 'hycontrol-clone-window-to-new-frame)
(define-key map "F" 'hycontrol-window-to-new-frame)
(define-key map "\C-g" 'hycontrol-abort)
(define-key map "h" (lambda () (interactive) (enlarge-window hycontrol-arg)))
;; Move directionally among windows within current frame
(define-key map "I" 'windmove-up)
(define-key map "J" 'windmove-left)
(define-key map "K" 'windmove-right)
(define-key map "M" 'windmove-down)
;; Allow frame resizing even when in window control mode because
;; it may be used often.
(define-key map "i" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-resize-to-top hycontrol-arg))))
(define-key map "j" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-resize-to-left hycontrol-arg))))
(define-key map "k" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-resize-to-right hycontrol-arg))))
(define-key map "m" (lambda () (interactive) (setq hycontrol-arg (hycontrol-frame-resize-to-bottom hycontrol-arg))))
(define-key map "n" (lambda () (interactive) (shrink-window-horizontally hycontrol-arg)))
(define-key map "o" (lambda () (interactive) (let ((w (selected-window))) (other-window hycontrol-arg) (if (eq w (selected-window)) (other-window 1)))))
(define-key map "O" (lambda () (interactive) (let ((w (selected-window))) (other-frame hycontrol-arg) (if (eq w (selected-window)) (other-frame 1)))))
;; Numeric keypad emulation for keyboards that lack one.
(define-key map "p" (lambda () (interactive) (hycontrol-virtual-numeric-keypad hycontrol-arg)))
(define-key map "q" 'hycontrol-quit)
(define-key map "Q" 'hycontrol-quit)
(define-key map "s" (lambda () (interactive) (shrink-window hycontrol-arg)))
(define-key map "t" 'hycontrol-enable-frames-mode)
(define-key map "u" 'unbury-buffer)
(define-key map "w" (lambda () (interactive) (enlarge-window-horizontally hycontrol-arg)))
(define-key map "Z" (lambda () (interactive) (if (fboundp 'text-scale-increase)
;; Emacs autoloaded function
(text-scale-increase (if (< hycontrol-arg 10) hycontrol-arg (setq hycontrol-arg 1))))))
(define-key map "z" (lambda () (interactive) (if (fboundp 'text-scale-decrease)
;; Emacs autoloaded function
(text-scale-decrease (if (< hycontrol-arg 10) hycontrol-arg (setq hycontrol-arg 1))))))
;; Don't call these interactively because a prefix arg of 1 tries
;; to make one window 1 line tall.
(define-key map "[" (lambda () (interactive) (split-window-vertically)))
(define-key map "]" (lambda () (interactive) (split-window-horizontally)))
(define-key map "(" 'hycontrol-save-frame-configuration)
(define-key map ")" 'hycontrol-restore-frame-configuration)
(define-key map "~" (lambda () (interactive)
(unless (hycontrol-window-swap-buffers)
(hycontrol-user-error hycontrol-debug "(HyControl): There must be precisely two windows within the selected frame to swap buffers."))))
(define-key map "-" 'hycontrol-minus-key)
(define-key map "+" 'hycontrol-window-maximize-lines)
(define-key map "=" (lambda () (interactive) (and (> (length (window-list)) 1)
(y-or-n-p "Resize windows evenly across this frame?")
(balance-windows))))
(define-key map "\C-u" 'hycontrol-multiply-universal-arg)
(define-key map "0" (lambda () (interactive) (hycontrol-universal-arg-digit 0)))
(define-key map "1" (lambda () (interactive) (hycontrol-universal-arg-digit 1)))
(define-key map "2" (lambda () (interactive) (hycontrol-universal-arg-digit 2)))
(define-key map "3" (lambda () (interactive) (hycontrol-universal-arg-digit 3)))
(define-key map "4" (lambda () (interactive) (hycontrol-universal-arg-digit 4)))
(define-key map "5" (lambda () (interactive) (hycontrol-universal-arg-digit 5)))
(define-key map "6" (lambda () (interactive) (hycontrol-universal-arg-digit 6)))
(define-key map "7" (lambda () (interactive) (hycontrol-universal-arg-digit 7)))
(define-key map "8" (lambda () (interactive) (hycontrol-universal-arg-digit 8)))
(define-key map "9" (lambda () (interactive) (hycontrol-universal-arg-digit 9)))
map)
"Keymap to use when in Hyperbole HyControl window mode.")
;;; ************************************************************************
;;; Public declarations
;;; ************************************************************************
(declare-function ibuffer-get-marked-buffers "ibuffer")
(declare-function which-key--hide-popup-ignore-command "ext:which-key")
;;; ************************************************************************
;;; Private variables
;;; ************************************************************************
(defvar hycontrol--frames-prompt-format
(concat "FRAMES: (h=heighten, s=shorten, w=widen, n=narrow, %%/H/W=screen %%age, arrow=move frame) by %d unit%s, .=clear units\n"
;; d/^/D=delete/iconify frame/others - iconify left out due to some bug on macOS (see comment near ^ below)
"a/A=cycle adjust width/height, d/D=delete frame/others, o/O=other win/frame, I/J/K/M=to frame, [/]=create frame, (/)=save/restore fconfig\n"
"@=grid of wins, f/F=clone/move win to new frame, -/+=minimize/maximize frame, ==frames same size, u/b/~=un/bury/swap bufs\n"
"Frame to edges: c=cycle, i/j/k/m=expand/contract, p/num-keypad=move; z/Z=zoom out/in, t=to WINDOWS mode, Q=quit")
"HyControl frames-mode minibuffer prompt string to pass to format.
Format it with 2 arguments: `prefix-arg' and a plural string indicating if
`prefix-arg' is not equal to 1.")
(defvar hycontrol--windows-prompt-format
(concat
"WINDOWS: (h=heighten, s=shorten, w=widen, n=narrow, arrow=move frame) by %d unit%s, .=clear units\n"
"a/A=cycle adjust frame width/height, d/D=delete win/others, o/O=other win/frame, I/J/K/M=to window, [/]=split win atop/sideways, (/)=save/restore wconfig\n"
"@=grid of wins, f/F=clone/move win to new frame, -/+=minimize/maximize win, ==wins same size, u/b/~=un/bury/swap bufs\n"
"Frame to edges: c=cycle, i/j/k/m=expand/contract, p/num-keypad=move; z/Z=zoom out/in, t=to FRAMES mode, Q=quit")
"HyControl windows-mode minibuffer prompt string to pass to format.
Format it with 2 arguments: `prefix-arg' and a plural string indicating if
`prefix-arg' is not equal to 1.")
(defvar hycontrol--prompt-format nil
"The current HyControl mode help format string or nil if not active.")
(defvar hycontrol--exit-status nil
"Internal HyControl status indicator of how it was exited.
After exit, it should be one of the following symbols triggered by the
associated key: quit {q}, abort {\\`C-g'}, or toggle {t}.")
(defvar hycontrol--fconfig nil
"Used to store a frame configuration while in hycontrol.")
(defvar hycontrol--wconfig nil
"Used to store a window configuration while in hycontrol.")
(defvar hycontrol--invert-display-buffer-predicates nil)
(defvar hycontrol--quit-function nil
"Store function to remove the transient-map later.
The function is auto-generated by a call to `set-transient-map'")
(defvar hycontrol--screen-edge-position 0
"Cycles between 0-7 representing corner and center edge positions.
The cycle is in clockwise order from the upper left corner.")
(defvar hycontrol--frame-widths-pointer nil)
(defvar hycontrol--frame-heights-pointer nil)
(defvar hycontrol--buffer-list nil)
(defvar hycontrol--buffer-list-pointer nil)
(defvar hycontrol--initial-which-key-inhibit nil
"Store value of `which-key-inhibit' flag from \"which-key\" package, if any.
Used on entry to HyControl.")
;;; ************************************************************************
;;; Private functions
;;; ************************************************************************
(defsubst hycontrol-windows-grid-valid-size-p (grid-size)
(when grid-size
(and (setq grid-size (prefix-numeric-value grid-size))
(integerp grid-size) (>= grid-size 11) (<= grid-size 99)
(not (zerop (% grid-size 10))))))
;;; HyControl private per keyboard key functions
(defun hycontrol-pre-command-hook ()
"Added to `pre-command-hook' while in any HyControl mode."
(when (null hycontrol-arg) (setq hycontrol-arg 1))
(setq prefix-arg hycontrol-arg))
(defun hycontrol-post-command-hook ()
"Added to `post-command-hook' while in HyControl frames mode."
(when (null hycontrol-arg) (setq hycontrol-arg 1))
(if (zerop (minibuffer-depth))
(if hycontrol-help-flag
(let ((message-log-max nil) ; prevent logging of HyControl help messages
(resize-mini-windows t) ; automatically shrink
(use-dialog-box)) ; prevent y-or-n dialog boxes
(message hycontrol--prompt-format hycontrol-arg (if (= hycontrol-arg 1) "" "s"))))
;; Quit from HyControl at any minibuffer prompt so that
;; self-insert-keys work for typing at the prompt.
(hycontrol-disable-modes)))
(defun hycontrol-end-mode ()
"Prepare to abort or quit from HyControl."
(interactive)
(remove-hook 'pre-command-hook 'hycontrol-pre-command-hook)
(remove-hook 'post-command-hook 'hycontrol-post-command-hook)
(if (boundp 'which-key-inhibit)
(setq which-key-inhibit hycontrol--initial-which-key-inhibit))
;; May be called when turning on HyControl before this next function
;; is defined.
(if (functionp hycontrol--quit-function)
(funcall hycontrol--quit-function))
(setq inhibit-quit nil
hycontrol--exit-status t
hycontrol-arg 1
prefix-arg nil
hycontrol-debug nil
hycontrol-frames-mode nil
hycontrol-windows-mode nil
hycontrol--prompt-format nil))
(defun hycontrol-stay-in-mode ()
"Return non-nil if HyControl mode should remain active."
(null hycontrol--exit-status))
(defun hycontrol-invert-prefix-arg ()
"Invert the sign of the current Hycontrol prefix argument.
If the argument is 0, set it to -1."
(interactive)
(setq this-command 'hycontrol-invert-prefix-arg
hycontrol-arg (if (zerop hycontrol-arg) -1 (- hycontrol-arg))))
(defun hycontrol-minus-key ()
"Conditional command to execute when the minus key is pressed."
(interactive)
(cond ((and (symbolp last-command)
(string-match-p "\\`\\(hui:menu-enter\\|hycontrol.*-\\(frames\\|windows\\|prefix\\|universal\\)-\\(mode\\|arg\\)\\)" (symbol-name last-command)))
(hycontrol-invert-prefix-arg))
(hycontrol-frames-mode
(hycontrol-frame-minimize-lines))
(t
(hycontrol-window-minimize-lines))))
(defun hycontrol-multiply-universal-arg ()
"Return prefix arg based on `hycontrol-arg' and one press of the universal arg."
(interactive)
(setq this-command 'hycontrol-multiply-universal-arg
hycontrol-arg (* hycontrol-arg 4))
(when (> (abs hycontrol-arg) hycontrol-maximum-units)
(setq hycontrol-arg (if (< hycontrol-arg 0) -4 4)))
hycontrol-arg)
(defun hycontrol-reset-prefix-arg ()
"Reset HyControl prefix arg to 0."
(interactive)
(setq hycontrol-arg 0)
(hycontrol-frame-to-screen-edges 0))
(defun hycontrol-universal-arg-digit (digit)
"Return the new prefix argument based on existing `hycontrol-arg' and new DIGIT."
(setq this-command 'hycontrol-universal-arg-digit
hycontrol-arg
(if (< hycontrol-arg 0)
(if (and (= hycontrol-arg -1)
(not (zerop digit)))
(- digit)
(- (* hycontrol-arg 10) digit))
(+ (* hycontrol-arg 10) digit)))
(when (> (abs hycontrol-arg) hycontrol-maximum-units)
(setq hycontrol-arg
(if (< hycontrol-arg 0)
(- digit)
digit)))
hycontrol-arg)
;;; HyControl private initialization functions
(defun hycontrol-setup (arg setup-function)
"HyControl initialization; passes through ARG and SETUP-FUNCTION.
SETUP-FUNCTION is HyControl mode-specific."
;; Save status value of which-key help package and quit any
;; in-progress which-key help without any user alert.
(when (boundp 'which-key-inhibit)
(setq hycontrol--initial-which-key-inhibit which-key-inhibit
which-key-inhibit t)
(which-key--hide-popup-ignore-command))
(setq arg (prefix-numeric-value arg)
inhibit-quit t
hycontrol--exit-status nil)
(and hycontrol-debug (not (integerp hycontrol-debug)) (setq hycontrol-debug message-log-max))
(if (called-interactively-p 'interactive) (hycontrol-save-configurations))
(cond ((or (not (integerp arg)) (< arg 1))
(setq arg 1))
((> arg hycontrol-maximum-units)
(setq arg hycontrol-maximum-units)))
(setq hycontrol-arg arg
prefix-arg arg)
(hycontrol-invert-mode-line)
(add-hook 'pre-command-hook 'hycontrol-pre-command-hook)
(add-hook 'post-command-hook 'hycontrol-post-command-hook)
(funcall setup-function))
(defun hycontrol-frames-setup ()
"HyControl frames-specific initializations."
(setq hycontrol--prompt-format hycontrol--frames-prompt-format)
(hycontrol-post-command-hook)
;; Use normal event loop with transient-map until {C-g} or {q} is
;; pressed, then exit.
(setq hycontrol--quit-function
(set-transient-map hycontrol-frames-mode-map #'hycontrol-stay-in-mode)))
(defun hycontrol-frames (&optional arg)
"Interactively delete, jump to, move, replicate, and resize frames.
With optional numeric prefix ARG, move and resize by ARG (an
integer) units. If ARG is < 1, it is set to 1. If it is >
`hycontrol-maximum-units', it is set to `hycontrol-maximum-units'."
(interactive "p")
(hycontrol-setup arg #'hycontrol-frames-setup)
(unless hycontrol-help-flag
(message "(HyControl) Frames global minor mode enabled; use {%s} for help"
(hycontrol-help-key-description))))
(defun hycontrol-windows-setup ()
"HyControl windows-specific initializations."
(setq hycontrol--prompt-format hycontrol--windows-prompt-format)
(hycontrol-post-command-hook)
;; Use normal event loop with transient-map until {C-g} or {q} is
;; pressed, then exit.
(setq hycontrol--quit-function
(set-transient-map hycontrol-windows-mode-map #'hycontrol-stay-in-mode)))
(defun hycontrol-windows (&optional arg)
"Interactively delete, jump to, rebalance, resize, and split windows.
With optional numeric prefix ARG, move and resize by ARG (an
integer) units. If ARG is < 1, it is set to 1. If it is >
`hycontrol-maximum-units', it is set to `hycontrol-maximum-units'."
(interactive "p")
(hycontrol-setup arg #'hycontrol-windows-setup)
(unless hycontrol-help-flag
(message "(HyControl) Windows global minor mode enabled; use {%s} for help"
(hycontrol-help-key-description))))
;;; HyControl general private functions
(defsubst hycontrol-frame-edges (&optional frame)
"Return the outermost edge coordinates of optional or selected FRAME.
FRAME must be a live frame and defaults to the selected one. The
list returned has the form (Left Top Right Bottom) where all
values are in pixels relative to the origin - the position (0, 0)
- of FRAME’s display. For terminal frames all values are
relative to Left and Top which are both zero."
(frame-edges frame 'outer-edges))
(defsubst hycontrol-frame-x-origin (&optional frame)
"Return the X origin coordinate of optional FRAME or the selected frame.
This includes all graphical window manager decorations. The X
origin coordinate is the upper left point. Under a graphical
window system, this is in pixels; otherwise, it is in characters."
(nth 0 (hycontrol-frame-edges frame)))
(defsubst hycontrol-frame-y-origin (&optional frame)
"Return the Y origin coordinate of optional FRAME or the selected frame.
This includes all graphical window manager decorations. The Y
origin coordinate is the upper left point. Under a graphical
window system, this is in pixels; otherwise, it is in characters."
(nth 1 (hycontrol-frame-edges frame)))
(defun hycontrol-frame-height (&optional frame)
"Return the height of optional FRAME or the selected frame.
This includes all graphical window manager decorations. Under a
graphical window system, this is in pixels; otherwise, it is in
characters."
(frame-pixel-height frame))
(defun hycontrol-frame-width (&optional frame)
"Return the width of optional FRAME or the selected frame.
This includes all graphical window manager decorations. Under a
graphical window system, this is in pixels; otherwise, it is in
characters."
(frame-pixel-width frame))
;; Frame Resizing Support
(defconst hycontrol-screen-offset-sensitivity 12
"Screen edge sensitivity in pixels.
Number of pixels a frame dimension can be off from its
screen-offset and still be considered at the screen edge.")
(defun hycontrol-frame-at-left-p ()
"Non-nil if selected frame's left edge is at the left edge of the screen.
Screen left edge is adjusted based on `hycontrol-screen-left-offset'."
(<= (- (nth 0 (hycontrol-frame-edges)) hycontrol-screen-left-offset)
hycontrol-screen-offset-sensitivity))
(defun hycontrol-frame-at-top-p ()
"Non-nil if selected frame's bottom is at the top of the screen.
Screen top is adjusted based on `hycontrol-screen-top-offset'."
(<= (- (nth 1 (hycontrol-frame-edges)) hycontrol-screen-top-offset
;; Under macOS, frames are automatically offset vertically by
;; the height of the global menubar, so account for that.
(if (eq system-type 'darwin) 23 0))
hycontrol-screen-offset-sensitivity))
(defun hycontrol-frame-at-right-p ()
"Non-nil if selected frame's right edge is at the right edge of the screen.
Screen right edge is adjusted based on `hycontrol-screen-right-offset'."
(<= (- (display-pixel-width) (nth 2 (hycontrol-frame-edges)) hycontrol-screen-right-offset)
hycontrol-screen-offset-sensitivity))
(defun hycontrol-frame-at-bottom-p ()
"Non-nil if selected frame's bottom is at the bottom of the screen.
Screen bottom edge is adjusted based on `hycontrol-screen-bottom-offset'."
(<= (- (display-pixel-height) (nth 3 (hycontrol-frame-edges)) hycontrol-screen-bottom-offset
;; Under macOS, frames are automatically offset vertically by
;; the height of the global menubar, so account for that.
(if (eq system-type 'darwin) -23 0))
hycontrol-screen-offset-sensitivity))
;; Frame Zoom Support
(defun hycontrol-frame-zoom (zoom-func arg max-msgs)
"Zoom default frame face using ZOOM-FUNC and amount ARG (must be 1-9).
MAX-MSGS is a number used only if ZOOM-FUNC is undefined and an
error message is logged."
(if (fboundp zoom-func)
(let ((frame-zoom-font-difference arg))
(funcall zoom-func))
(hycontrol-user-error max-msgs "(HyControl): Zooming requires separate \"zoom-frm.el\" Emacs Lisp library installation")))
(defun hycontrol-make-frame ()
"Create a new frame with the same size and selected buffer as the selected frame.
The new frame is selected. It is offset from the selected frame
by `hycontrol-frame-offset' (x . y) pixels."
(interactive)
(select-frame (make-frame (list (cons 'width (frame-width)) (cons 'height (frame-height))
(cons 'left (+ (car hycontrol-frame-offset) (car (frame-position))))
(cons 'top (+ (cdr hycontrol-frame-offset) (cdr (frame-position))))))))
(defun hycontrol-move-frame (arrow pixels)
(let ((x (car (frame-position)))
(y (cdr (frame-position))))
(pcase arrow
('up (set-frame-position nil x (- y pixels)))
('down (set-frame-position nil x (+ y pixels)))
('left (set-frame-position nil (- x pixels) y))
('right (set-frame-position nil (+ x pixels) y)))))
(defun hycontrol-numeric-keypad (e _arg)
"Move frame to screen location based on the last pressed numeric keypad key E."
(let ((num (if (integerp e)
e
;; kp-<num> symbol
(- (aref (symbol-name e) 3) ?0))))
(funcall
(nth num '(nil hycontrol-frame-to-bottom-left hycontrol-frame-to-bottom-center hycontrol-frame-to-bottom-right
hycontrol-frame-to-left-center hycontrol-frame-to-center hycontrol-frame-to-right-center
hycontrol-frame-to-top-left hycontrol-frame-to-top-center hycontrol-frame-to-top-right)))))
(defun hycontrol-set-frame-height (frame height &optional pretend pixelwise)
"Set text height of frame FRAME to HEIGHT lines and fit it to the screen.
Optional third arg PRETEND non-nil means that redisplay should use
HEIGHT lines but that the idea of the actual height of the frame should
not be changed.
Optional fourth argument PIXELWISE non-nil means that FRAME should be
HEIGHT pixels high. Note: When ‘frame-resize-pixelwise’ is nil, some
window managers may refuse to honor a HEIGHT that is not an integer
multiple of the default frame font height."
(let ((frame-resize-pixelwise t))
(set-frame-height frame height pretend pixelwise)
(hycontrol-frame-fit-to-screen frame)))
(defun hycontrol-set-frame-position (frame x y)
"Set position of FRAME to (X, Y) and ensure it fits on screen.
FRAME must be a live frame and defaults to the selected one. X and Y,
if positive, specify the coordinate of the left and top edge of FRAME’s
outer frame in pixels relative to an origin (0, 0) of FRAME’s display.
If any of X or Y is negative, it specifies the coordinates of the right
or bottom edge of the outer frame of FRAME relative to the right or
bottom edge of FRAME’s display."
(let ((frame-resize-pixelwise t))
(hycontrol-frame-fit-to-screen frame)
(set-frame-position frame x y)))
(defun hycontrol-set-frame-size (frame width height &optional pixelwise)
"Set text size of FRAME to WIDTH by HEIGHT, measured in characters.
Ensure frame fits within the screen size.
Optional argument PIXELWISE non-nil means to measure in pixels. Note:
When ‘frame-resize-pixelwise’ is nil, some window managers may refuse to
honor a WIDTH that is not an integer multiple of the default frame font
width or a HEIGHT that is not an integer multiple of the default frame
font height."
(let ((x-origin (hycontrol-frame-x-origin))
(y-origin (hycontrol-frame-y-origin))
(frame-resize-pixelwise t))
(set-frame-size frame width height pixelwise)
(hycontrol-frame-fit-to-screen frame x-origin y-origin)))
(defun hycontrol-set-frame-width (frame width &optional pretend pixelwise)
"Set text width of frame FRAME to WIDTH columns and fit it to the screen.
Optional third arg PRETEND non-nil means that redisplay should use WIDTH
columns but that the idea of the actual width of the frame should not
be changed.
Optional fourth argument PIXELWISE non-nil means that FRAME should be
WIDTH pixels wide. Note: When ‘frame-resize-pixelwise’ is nil, some
window managers may refuse to honor a WIDTH that is not an integer
multiple of the default frame font width."
(let ((x-origin (hycontrol-frame-x-origin))
(y-origin (hycontrol-frame-y-origin))
(frame-resize-pixelwise t))
(set-frame-width frame width pretend pixelwise)
(hycontrol-frame-fit-to-screen frame x-origin y-origin)))
(defun hycontrol-display-buffer-predicate-results (buffer)
(condition-case err
(mapcar (lambda (expr)
(if (functionp expr)
(funcall expr buffer)
(with-current-buffer buffer
(eval expr))))
hycontrol-display-buffer-predicate-list)
(error "(HyDebug): Invalid expression in `hycontrol-display-buffer-predicate-list' - %s" err)))
(defvar hycontrol--blank-buffer (get-buffer-create hycontrol-blank-buffer-name)
"Blank buffer to display in extra window grid windows.
Used after selected buffer list is exhausted.")
(defun hycontrol-window-display-buffer (window)
"Given a WINDOW, choose the next appropriate buffer to display.
If WINDOW is dedicated, ignore it and do nothing.
Uses hycontrol--buffer-list'.
When `hycontrol--invert-display-buffer-predicates' is non-nil and not
\\='ignore, the list of buffers used is further filtered using the
functions and sexpressions in `hycontrol-display-buffer-predicate-list',
which by default filters a frame's buffers to just those with attached
files.
Filtering is disabled if a specific list of buffers is sent to the
`hycontrol-make-windows-grid' function that calls this."
(unless (or (null window) (window-dedicated-p window))
(let ((buf (car hycontrol--buffer-list-pointer)))
(setq hycontrol--buffer-list-pointer (cdr hycontrol--buffer-list-pointer))
(while (and buf (or (= (aref (buffer-name buf) 0) ?\ )
(and (not hycontrol--invert-display-buffer-predicates)
(not (eval (cons 'or (hycontrol-display-buffer-predicate-results buf)))))
(and hycontrol--invert-display-buffer-predicates
(not (eq hycontrol--invert-display-buffer-predicates 'ignore))
(eval (cons 'or (hycontrol-display-buffer-predicate-results buf))))))
;; Buffer is not one to display, get the next one and test again.
(setq buf (car hycontrol--buffer-list-pointer)
hycontrol--buffer-list-pointer (cdr hycontrol--buffer-list-pointer)))
(set-window-buffer window
(or buf
;; Out of buffers to display, display a blank one
hycontrol--blank-buffer)))))
(defun hycontrol-window-display-buffer-with-repeats (window)
"This is no longer used since Hyperbole V8. Left here for reference.
Given a WINDOW, choose the next appropriate buffer to display
therein using `hycontrol-display-buffer-predicate-list'. Also
uses the value of`hycontrol--buffer-list' as the list of buffers
to distribute among the windows."
(let ((buf (car hycontrol--buffer-list-pointer)))
(setq hycontrol--buffer-list-pointer (cdr hycontrol--buffer-list-pointer))
(unless buf
;; Now on each new pass through buffer-list, the buffer predicate tests will
;; be inverted to expand the list of allowed buffers because the
;; 1st pass did not produce a buffer for this window.
(setq hycontrol--buffer-list-pointer hycontrol--buffer-list
buf (car hycontrol--buffer-list-pointer)
hycontrol--buffer-list-pointer (cdr hycontrol--buffer-list-pointer))
(unless (eq hycontrol--invert-display-buffer-predicates 'ignore)
(setq hycontrol--invert-display-buffer-predicates (not hycontrol--invert-display-buffer-predicates))))
(while (and buf (or (= (aref (buffer-name buf) 0) ?\ )
(and (not hycontrol--invert-display-buffer-predicates)
(not (eval (cons 'or (hycontrol-display-buffer-predicate-results buf)))))
(and hycontrol--invert-display-buffer-predicates
(not (eq hycontrol--invert-display-buffer-predicates 'ignore))
(eval (cons 'or (hycontrol-display-buffer-predicate-results buf))))))
;; Buffer is not one to display, get the next one and test again.
(setq buf (car hycontrol--buffer-list-pointer)
hycontrol--buffer-list-pointer (cdr hycontrol--buffer-list-pointer)))
(cond (buf
(set-window-buffer window buf))
(t
;; Start 2nd or greater pass through buffer list with predicates inverted.
(hycontrol-window-display-buffer window)))))
(defun hycontrol-message (max-msgs &rest msg-args)
"Log MAX-MSGS, adding MSG-ARGS to the *Messages* buffer log."
(let ((message-log-max max-msgs))
(apply #'message msg-args)))
(defun hycontrol-user-error (max-msgs &rest err)
"Log MAX-MSGS, adding ERR to *Messages* buffer; display ERR for 2 seconds."
(let ((message-log-max max-msgs))
(beep)
(apply #'message err)
(sit-for 2)))
;;; ************************************************************************
;;; Public functions
;;; ************************************************************************
;;; HyControl Global Minor Modes
;;;###autoload
(defun hycontrol-enable-frames-mode (&optional arg)
"Globally enable HyControl Frames mode for rapid Emacs frame control.
Interactively delete, jump to, move, replicate, and resize frames.
With optional numeric prefix ARG, move and resize by ARG (an
integer) units. If ARG is < 1, it is set to 1. If it is >
`hycontrol-maximum-units', it is set to `hycontrol-maximum-units'."
(interactive "p")
(hycontrol-disable-modes)
(hycontrol-frames-mode (if (and (integerp arg) (>= arg 1)) arg 1)))