-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
moom.el
1735 lines (1585 loc) · 60.8 KB
/
moom.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
;;; moom.el --- Commands to control frame position and size -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2023 Takaaki ISHIKAWA
;; Author: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; Keywords: frames, faces, convenience
;; Version: 1.6.8
;; Maintainer: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; URL: https://github.com/takaxp/Moom
;; Package-Requires: ((emacs "25.1"))
;; Twitter: @takaxp
;; 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 of the License, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This package provides a set of commands to control frame position and size.
;; The font size in buffers will be changed with synchronization of the updated
;; frame geometry so that the frame width could be maintained at 80 as default.
;;
;; Now make your dominant hand FREE from your mouse by Moom.
;;
;; Install:
;; - Get moom.el and moom-font.el from MELPA or GitHub.
;;
;; Setup:
;; - After installing, activate Moom by (moom-mode 1) in your init.el.
;;
;; Keybindings:
;; - The moom-mode-map is available.
;; - To see more details and examples, go https://github.com/takaxp/moom.
;;
;;; Change Log:
;;; Code:
(eval-when-compile
(require 'cl-lib)
(require 'moom-font nil t))
(defgroup moom nil
"Commands to control frame position and size."
:group 'convenience)
(defcustom moom-move-frame-pixel-offset '(0 . 0)
"Offset of the center position."
:type 'sexp
:group 'moom)
(defcustom moom-min-frame-height 16
"The minimum height."
:type 'integer
:group 'moom)
(defcustom moom-init-line-spacing line-spacing
"The default value to set ‘line-spacing’."
:type 'float
:group 'moom)
(defcustom moom-min-line-spacing 0.1
"The minimum value for line spacing."
:type 'float
:group 'moom)
(defcustom moom-max-line-spacing 0.8
"The maximum value for line spacing."
:type 'float
:group 'moom)
(defcustom moom-frame-width-single 80
"The width of the current frame as the default value."
:type 'integer
:group 'moom)
(defcustom moom-frame-width-double (+ (* 2 moom-frame-width-single) 3)
"The width of the current frame (double size)."
:type 'integer
:group 'moom)
(defcustom moom-horizontal-shifts '(200 200)
"Distance to move the frame horizontally."
:type '(choice (integer :tag "Common value for left and right")
(list (integer :tag "Value for left")
(integer :tag "Value for right")))
:group 'moom)
(defcustom moom-fill-band-options '(:direction vertical :range 50.0)
"Band direction and range to fill screen.
If DIRECTION is horizontal, the frame height is limited based on RANGE.
If it is vertical, then the frame width will be limited based on RANGE.
If the type of RANGE is float, range is calculated in percent.
Note that even if DIRECTION is vertical and RANGE is 100.0, the screen
may not be covered entirely because this package prioritize keeping
the column at `moom-frame-width-single'. Instead, if the value is integer,
the screen will be covered precisely by the specified value in pixels.
:direction symbol, horizontal or vertical
:range float(%) or integer(px)."
:type 'plist
:group 'moom)
(defcustom moom-scaling-gradient (/ 5.0 3)
"Gradient factor between font size and actual font pixel width.
This parameter is used to calculate the font pixel width when resizing
the frame width to keep the column 80. It depends on Font.
For instance,
1.66 or (5.0/3.0) : Menlo, Monaco
2.00 or (5.0/2.5) : Inconsolata
."
:type 'float
:group 'moom)
(defcustom moom-display-line-numbers-width 6
"Width to expand the frame for function `global-display-line-numbers-mode'.
For function `display-line-numbers-mode',
- set this variable to 0, and also
- set `moom-frame-width-single' more than 80 (e.g. 86)"
:group 'moom
:type 'integer)
(defcustom moom-command-with-centering '(split delete)
"List of flags.
It specifies whether centerize the frame after changing the frame width."
:type '(repeat
(choice
(const :tag "Split window" :value split)
(const :tag "Delete windows" :value delete)
(const :tag "Change frame width single" :value single)
(const :tag "Change frame width double" :value double)
(const :tag "Change frame width half again" :value half-again)))
:group 'moom)
(defcustom moom-user-margin '(0 0 0 0)
"User specified margin to adjust to the local environment."
:type '(list (integer :tag "Top margin")
(integer :tag "Bottom margin")
(integer :tag "Left margin")
(integer :tag "Right margin"))
:group 'moom)
(defcustom moom-use-font-module t
"If nil, font module will not integrated even if the module is available.
Configure this variable before activating moom mode.
`moom-toggle-font-module' could be also useful."
:type 'boolean
:group 'moom)
(defcustom moom-command-history-length 100
"Number of commands to remember in history."
:type 'integer
:group 'moom)
(defcustom moom-verbose nil
"Show responses from \"moom\"."
:type 'boolean
:group 'moom)
(defcustom moom-lighter "Moom"
"Package name in mode line."
:type 'string
:group 'moom)
(defcustom moom-before-fill-screen-hook nil
"Hook runs before changing to frame maximized."
:type 'hook
:group 'moom)
(defcustom moom-after-fill-screen-hook nil
"Hook runs after changing to frame maximized."
:type 'hook
:group 'moom)
(defcustom moom-resize-frame-height-hook nil
"Hook runs after resizing the frame height."
:type 'hook
:group 'moom)
(defcustom moom-split-window-hook nil
"Hook runs after splitting window horizontally."
:type 'hook
:group 'moom)
(defcustom moom-delete-window-hook nil
"Hook runs after deleting window horizontally."
:type 'hook
:group 'moom)
(defcustom moom-before-setup-hook nil
"Hook runs before enabling this package."
:type 'hook
:group 'moom)
(defcustom moom-after-reset-hook nil
"Hook runs after reset of internal parameters."
:type 'hook
:group 'moom)
(defcustom moom-after-select-monitor-hook nil
"Hook runs after selecting and jumping to a monitor."
:type 'hook
:group 'moom)
(defvar moom-mode-map
(let ((map (make-sparse-keymap)))
;; No keybindings are configured as default. It's open for users.
map)
"The keymap for `moom'.")
(defvar moom--init-status nil)
(defvar moom--internal-border-width 0)
(defvar moom--font-module-p (require 'moom-font nil t))
(defvar moom--frame-width 80)
(defvar moom--height-list nil)
(defvar moom--height-steps 4)
(defvar moom--last-status nil)
(defvar moom--maximized nil)
(defvar moom--screen-margin nil)
(defvar moom--fill-minimum-range 256)
(defvar moom--frame-resize-pixelwise nil)
(defvar moom--virtual-grid nil)
(defvar moom--screen-grid nil)
(defvar moom--print-status t)
(defvar moom--common-margin
(cond ((memq window-system '(ns mac)) '(23 0 0 0))
(t '(0 0 0 0))))
(defvar moom--pos-options '(:grid nil :bound nil)) ;; {screen,virtual}, {nil,t}
(defvar moom--local-margin (cond ((eq window-system 'w32) '(0 9 -16 16))
((eq window-system 'x) '(-19 0 0 0))
(t '(0 0 0 0))))
(defvar moom--autoreset-hooks '(menu-bar-mode-hook
tool-bar-mode-hook tab-bar-mode-hook
horizontal-scroll-bar-mode-hook
scroll-bar-mode-hook))
(defvar moom--last-monitor nil)
(defvar moom--display-border '(0 0))
(defvar moom--command-history nil)
(defvar moom--non-interactive-history nil)
(defun moom--setup ()
"Init function."
(run-hooks 'moom-before-setup-hook)
(setq moom--font-module-p (when moom-use-font-module
(boundp 'moom-font--pause)))
(setq moom-font--pause (not moom--font-module-p))
(moom-identify-current-monitor)
(unless moom--virtual-grid
(setq moom--virtual-grid (moom--virtual-grid)))
(unless moom--screen-grid
(setq moom--screen-grid (moom--screen-grid)))
(setq moom--internal-border-width
(alist-get 'internal-border-width (frame-geometry)))
(unless (eq (setq moom--frame-width moom-frame-width-single) 80)
(set-frame-width nil moom--frame-width))
(moom--make-frame-height-list)
(moom--save-last-status)
(setq moom--init-status moom--last-status)
(setq moom--frame-resize-pixelwise frame-resize-pixelwise
frame-resize-pixelwise t)
;; JP-font module
(when moom--font-module-p
(add-hook 'moom-font-after-resize-hook #'moom--make-frame-height-list)
(add-hook 'moom-font-after-resize-hook #'moom--stay-in-region))
;; display-line-numbers-mode
(when (fboundp 'global-display-line-numbers-mode)
(add-hook 'global-display-line-numbers-mode-hook
#'moom--update-frame-display-line-numbers))
(mapcar (lambda (hook) (add-hook hook #'moom-reset))
moom--autoreset-hooks))
(defun moom--abort ()
"Abort."
(moom-reset-line-spacing)
(moom-reset)
(setq frame-resize-pixelwise moom--frame-resize-pixelwise)
(setq moom--screen-margin nil
moom--virtual-grid nil
moom--screen-grid nil)
(when (fboundp 'global-display-line-numbers-mode)
(remove-hook 'global-display-line-numbers-mode-hook
#'moom--update-frame-display-line-numbers))
(mapcar (lambda (hook) (remove-hook hook #'moom-reset))
moom--autoreset-hooks))
(defun moom--reload ()
"Reload."
(moom--abort)
(moom--setup))
(defun moom--lighter ()
"Lighter."
(when moom-lighter
(concat " " moom-lighter)))
(defun moom--frame-internal-width ()
"Width of internal objects.
Including fringes and border."
(if window-system
(let ((fp (frame-parameters)))
(+ (alist-get 'right-fringe fp)
(alist-get 'left-fringe fp)
(if (get-scroll-bar-mode)
(alist-get 'scroll-bar-width fp)
0)
(* 2 moom--internal-border-width)))
0)) ;; TODO check this by terminal
(defun moom--internal-border-height ()
"Height of internal border within `frame-pixel-height'."
0)
(defun moom--frame-internal-height ()
"Height of internal objects.
Including title-bar, menu-bar, offset depends on window system, and border."
(if window-system
(let ((geometry (frame-geometry)))
(+ (cdr (alist-get 'title-bar-size geometry))
(cdr (alist-get 'tool-bar-size geometry))
(if (memq window-system '(x w32))
(cdr (alist-get 'menu-bar-size geometry)) 0)
(moom--internal-border-height)))
0)) ;; TODO check this by terminal
(defun moom--frame-pixel-width ()
"Return frame width in pixels."
(let ((edges (frame-edges)))
(- (nth 2 edges)
(nth 0 edges))))
(defun moom--frame-pixel-height ()
"Return frame height in pixels."
(let ((edges (frame-edges)))
(- (nth 3 edges)
(nth 1 edges))))
(defun moom--max-frame-pixel-width ()
"Return the maximum width on pixel base."
(- (display-pixel-width)
(moom--frame-internal-width)
(nth 2 moom--screen-margin)
(nth 3 moom--screen-margin)))
(defun moom--max-frame-pixel-height ()
"Return the maximum height on pixel base."
(- (display-pixel-height)
(moom--frame-internal-height)
(nth 0 moom--screen-margin)
(nth 1 moom--screen-margin)))
(defun moom--max-half-frame-pixel-height ()
"Return the half of maximum height on pixel base."
(floor (/ (- (display-pixel-height)
(nth 0 moom--screen-margin)
(nth 1 moom--screen-margin)
(* 2 (moom--frame-internal-height)))
2.0)))
(defun moom--frame-width ()
"Return frame width."
(/ (- (moom--frame-pixel-width)
(moom--frame-internal-width))
(frame-char-width)))
(defun moom--frame-height ()
"Return frame height."
(/ (- (moom--frame-pixel-height)
(moom--internal-border-height))
(frame-char-height)))
(defun moom--max-frame-width ()
"Return the maximum width based on screen size."
(floor (/ (moom--max-frame-pixel-width)
(frame-char-width))))
(defun moom--max-frame-height ()
"Return the maximum height based on screen size."
(floor (/ (moom--max-frame-pixel-height)
(frame-char-height))))
(defun moom--min-frame-height ()
"Return the minimum height of frame."
moom-min-frame-height)
(defun moom--make-frame-height-list ()
"Create an internal ring to change frame height."
(let ((max-height (moom--max-frame-height))
(min-height (moom--min-frame-height))
(steps moom--height-steps)
(heights nil))
(while (> (setq steps (1- steps)) 0)
(cl-pushnew (max min-height
(/ (* steps max-height) moom--height-steps))
heights))
(cl-pushnew (max min-height max-height) heights)
(setq moom--height-list (copy-sequence heights))
;; Skip the current height
(when (eq (car moom--height-list)
(moom--frame-height))
(moom--cycle-frame-height-list))))
(defun moom--cycle-frame-height-list ()
"Rotate `moom--height-list'."
(setq moom--height-list
(append (cdr moom--height-list)
(list (car moom--height-list)))))
(defun moom--font-size (pixel-width)
"Return an appropriate font-size based on PIXEL-WIDTH.
If `moom-font-table' is non-nil, the returned value will be more precisely
calculated."
(let* ((font-width (/ (float (- pixel-width (moom--frame-internal-width)))
moom-frame-width-single))
(scaled-width (/ font-width
(if moom--font-module-p
moom-font-ascii-scale 1.0)))
(font-size (when moom--font-module-p
(moom-font--find-size
(floor scaled-width) moom-font-table))))
(if font-size font-size
(floor (* (if (< scaled-width 1) 1 scaled-width)
moom-scaling-gradient)))))
(defun moom--font-resize (font-size &optional pixel-width)
"Resize font.
Resize the current font to FONT-SIZE.
If PIXEL-WIDTH is non-nil, ensure an appropriate font size so that
the actual pixel width will not exceed the WIDTH."
(when moom--font-module-p
(set-frame-width nil moom-frame-width-single)
(moom-font-resize font-size pixel-width)))
(defun moom--fullscreen-font-size ()
"Return the maximum font-size for full screen."
(if window-system
(moom--font-size (+ (moom--max-frame-pixel-width)
(moom--frame-internal-width)))
12)) ;; FIXME, use face-attribute
(defun moom--virtual-grid ()
"Alignment for pointing frame left and top position on `set-frame-position'."
(cond ((eq window-system 'w32) '(-16 0))
((and (eq window-system 'x)
(version< emacs-version "26.0")) '(0 27))
((memq window-system '(ns mac)) '(0 0))
((eq window-system 'x) '(10 8))
(t '(0 0))))
(defun moom--screen-grid ()
"Alignment of frame left and top position on monitor."
(cond ((eq window-system 'w32) '(8 0))
((and (eq window-system 'x)
(version< emacs-version "26.0")) '(10 -19))
((memq window-system '(ns mac)) '(0 0))
((eq window-system 'x) '(0 0))
(t '(0 0))))
(defun moom--frame-left ()
"Return outer left position."
(let ((left (frame-parameter nil 'left)))
(+ (if (listp left) (nth 1 left) left)
(nth 0 moom--virtual-grid)
(nth 0 moom--screen-grid))))
(defun moom--frame-top ()
"Return outer top position."
(let ((top (frame-parameter nil 'top)))
(+ (if (listp top) (nth 1 top) top)
(nth 1 moom--virtual-grid)
(nth 1 moom--screen-grid))))
(defun moom--pos-x (posx &optional options)
"Return aligned POSX for `set-frame-position'.
OPTIONS controls grid and bound. See `moom--pos-options'."
(when (listp posx)
(setq posx (nth 1 posx)))
(setq posx (- posx (nth 0 moom--screen-grid)))
(when (eq window-system 'w32)
(setq posx (+ posx 16))) ;; FIXME
(when (or (plist-get options :bound)
(not (memq window-system '(ns mac w32)))) ;; TODO: support others if possible
(let ((bounds-left (- (nth 0 moom--last-monitor)
(nth 0 moom--screen-grid)))
(bounds-right (+ (nth 0 moom--last-monitor)
(nth 2 moom--last-monitor)
(- (moom--frame-pixel-width)))))
(setq posx (cond ((< posx bounds-left) bounds-left)
((> posx bounds-right) bounds-right)
(t posx)))))
(unless (plist-get options :grid)
(setq options (if (eq window-system 'ns)
'(:grid screen) '(:grid virtual))))
(cond ((eq (plist-get options :grid) 'virtual)
(let ((pw (- (nth 0 moom--display-border)
(moom--frame-pixel-width))))
(if (< posx 0)
(- posx (+ pw (nth 0 moom--virtual-grid)))
posx)))
((eq (plist-get options :grid) 'screen)
posx)
(t
posx)))
(defun moom--pos-y (posy &optional options)
"Return aligned POSY for `set-frame-position'.
OPTIONS controls grid and bound. See `moom--pos-options'."
(when (listp posy)
(setq posy (nth 1 posy)))
(setq posy (- posy (nth 1 moom--screen-grid)))
(when (or (plist-get options :bound)
(not (memq window-system '(ns mac w32)))) ;; TODO: support others if possible
(let ((bounds-top (- (nth 1 moom--last-monitor)
(nth 1 moom--screen-grid)))
(bounds-bottom (+ (nth 1 moom--last-monitor)
(nth 3 moom--last-monitor)
(- (moom--frame-pixel-height)))))
(setq posy (cond ((< posy bounds-top) bounds-top)
((> posy bounds-bottom) bounds-bottom)
(t posy)))))
(unless (plist-get options :grid)
(setq options (if (eq window-system 'ns)
'(:grid screen) '(:grid virtual))))
(cond ((eq (plist-get options :grid) 'virtual)
(let ((ph (- (nth 1 moom--display-border)
(moom--frame-pixel-height))))
(if (< posy 0)
(- posy (+ ph (nth 1 moom--virtual-grid)))
posy)))
((eq (plist-get options :grid) 'screen)
posy)
(t
posy)))
(defun moom--horizontal-center ()
"Horizontal center position."
(+ (nth 2 moom--screen-margin)
(floor (/ (+ (moom--max-frame-pixel-width)
(moom--frame-internal-width))
2.0))))
(defun moom--vertical-center ()
"Vertical center position."
(+ (nth 0 moom--screen-margin)
(floor (/ (+ (moom--max-frame-pixel-height)
(moom--frame-internal-height))
2.0))))
(defun moom--horizontal-center-pos ()
"Left edge position when centering."
(+ (car moom-move-frame-pixel-offset)
(moom--horizontal-center)
(let ((scroll (frame-parameter nil 'scroll-bar-width)))
(if (and (> scroll 0) (get-scroll-bar-mode)) ;; TBD
scroll
(frame-parameter nil 'left-fringe)))
(- (/ (+ (moom--frame-pixel-width)
(moom--frame-internal-width)
(- (moom--internal-border-height)))
2))))
(defun moom--vertical-center-pos ()
"Top edge position when centering."
(+ (cdr moom-move-frame-pixel-offset)
(moom--vertical-center)
(- (/ (+ (moom--frame-pixel-height)
(moom--frame-internal-height)
(- (moom--internal-border-height)))
2))))
(defun moom--save-last-status ()
"Store the last frame position, size, and font-size."
(setq moom--last-status
`(("font-size" . ,(if moom--font-module-p moom-font--size nil))
("left" . ,(moom--pos-x (moom--frame-left)))
("top" . ,(moom--frame-top))
("width" . ,(moom--frame-width))
("height" . ,(moom--frame-height))
("pixel-width" . ,(moom--frame-pixel-width))
("pixel-height" . ,(moom--frame-pixel-height)))))
(defun moom--fill-display (area)
"Move the frame to AREA.
Font size will be changed appropriately.
AREA would be \=top, \=bottom, \=left, \=right, \=topl, \=topr,
\=botl, and \=botr."
(let* ((align-width (+ (moom--max-frame-pixel-width)
(moom--frame-internal-width)))
(pixel-width
(- (floor (/ align-width 2.0))
(moom--frame-internal-width)))
(pixel-height (moom--max-half-frame-pixel-height))
(pos-x (nth 2 moom--screen-margin))
(pos-y (nth 0 moom--screen-margin)))
;; Region
(cond ((memq area '(top bottom))
(setq pixel-width (moom--max-frame-pixel-width)))
((memq area '(left right))
(setq pixel-height (moom--max-frame-pixel-height))
(setq align-width (floor (/ align-width 2.0))))
((memq area '(topl topr botl botr))
(setq align-width (floor (/ align-width 2.0))))
(nil t))
;; Font size
(let ((moom-font-before-resize-hook nil)
(moom-font-after-resize-hook nil))
(moom--font-resize (moom--font-size align-width) align-width))
;; Position
(when (memq area '(right topr botr))
(setq pos-x (moom--horizontal-center)))
(when (memq area '(bottom botl botr))
(setq pos-y (moom--vertical-center)))
(when (memq area '(top bottom left right topl topr botl botr))
(let ((flag (memq window-system '(ns mac w32))))
(unless flag
(set-frame-size nil pixel-width pixel-height t))
(when (and (not moom--font-module-p)
(eq window-system 'w32))
(set-frame-width nil moom-frame-width-single)) ;; FIXME
(set-frame-position nil
(moom--pos-x pos-x)
(moom--pos-y pos-y))
(when flag
(set-frame-size nil pixel-width pixel-height t)))))
(moom--make-frame-height-list)
(moom-print-status))
(defun moom--shift-amount (direction)
"Extract shift amount from `moom-horizontal-shifts' based on DIRECTION."
(let ((index (cond ((eq direction 'left) 0)
((eq direction 'right) 1)
(t 0))))
(cond ((integerp moom-horizontal-shifts)
moom-horizontal-shifts)
((listp moom-horizontal-shifts)
(nth index moom-horizontal-shifts))
(t
(error (format "%s is wrong value." moom-horizontal-shifts))))))
(defun moom--frame-monitor-attribute (attribute &optional frame x y)
"Return the value of ATTRIBUTE on FRAME's monitor.
If X and Y are both numbers, then ignore the value of FRAME; the
monitor is determined to be the physical monitor that contains
the pixel coordinate (X, Y).
Taken from frame.el in 26.1 to support previous Emacs versions, 25.1 or later."
(if (and (numberp x)
(numberp y))
(cl-loop for monitor in (display-monitor-attributes-list)
for geometry = (alist-get 'geometry monitor)
for min-x = (pop geometry)
for min-y = (pop geometry)
for max-x = (+ min-x (pop geometry))
for max-y = (+ min-y (car geometry))
when (and (<= min-x x)
(< x max-x)
(<= min-y y)
(< y max-y))
return (alist-get attribute monitor))
(alist-get attribute (frame-monitor-attributes frame))))
(defun moom--frame-monitor-geometry (&optional frame x y)
"Return the geometry of FRAME's monitor.
If X and Y are both numbers, then ignore the value of FRAME; the
monitor is determined to be the physical monitor that contains
the pixel coordinate (X, Y).
Taken from frame.el in 26.1 to support previous Emacs versions, 25.1 or later."
(moom--frame-monitor-attribute 'geometry frame x y))
(defun moom--frame-monitor-workarea (&optional frame x y)
"Return the workarea of FRAME's monitor.
If X and Y are both numbers, then ignore the value of FRAME; the
monitor is determined to be the physical monitor that contains
the pixel coordinate (X, Y).
Taken from frame.el in 26.1 to support previous Emacs versions, 25.1 or later."
(moom--frame-monitor-attribute 'workarea frame x y))
(defun moom--default-screen-margin ()
"Calculate default screen margins."
(let ((geometry (moom--frame-monitor-geometry))
(workarea (moom--frame-monitor-workarea)))
(setq moom--screen-margin
(list (- (nth 1 workarea) (nth 1 geometry))
(- (+ (nth 1 geometry) (nth 3 geometry))
(+ (nth 1 workarea) (nth 3 workarea)))
(- (nth 0 workarea) (nth 0 geometry))
(- (+ (nth 0 geometry) (nth 2 geometry))
(+ (nth 0 workarea) (nth 2 workarea)))))
;; Update
(moom--merge-screen-margin moom--local-margin)))
(defun moom--merge-screen-margin (margin)
"Add MARGIN to `moom--screen-margin'."
(setq moom--screen-margin
(list (+ (nth 0 margin) (nth 0 moom--screen-margin))
(+ (nth 1 margin) (nth 1 moom--screen-margin))
(+ (nth 2 margin) (nth 2 moom--screen-margin))
(+ (nth 3 margin) (nth 3 moom--screen-margin)))))
(defun moom--update-frame-display-line-numbers ()
"Expand frame width by `moom-display-line-numbers-width'.
This feature is available when function `global-display-line-numbers-mode'
is utilized."
(if (numberp moom-display-line-numbers-width)
(unless (eq moom-display-line-numbers-width 0)
(let ((flag (if global-display-line-numbers-mode 1 -1)))
(setq moom-frame-width-single
(+ moom-frame-width-single
(* flag moom-display-line-numbers-width)))
(setq moom-frame-width-double
(+ (* 2 moom-frame-width-single) 3))
(set-frame-width nil
(+ (frame-width)
(* flag moom-display-line-numbers-width)))))
(user-error "Unexpected value of `moom-display-line-numbers-width'"))
(moom-print-status))
(defun moom--centerize-p (arg)
"Return nil when `ARG' is not listed in `moom-command-with-centering'."
(memq arg (if (listp moom-command-with-centering)
moom-command-with-centering
(list moom-command-with-centering))))
(defun moom--stay-in-region (&optional target-width)
"Shift the frame to try to keep the frame staying in the display.
The frame width may be specified with TARGET-WIDTH."
(let ((shift (- (+ (* (frame-char-width) (or target-width
(moom--frame-width)))
(moom--frame-internal-width)
(moom--pos-x (moom--frame-left)))
(- (display-pixel-width)
(nth 3 moom--screen-margin)))))
;; Left side border
(when (< (moom--pos-x (moom--frame-left)) shift)
(setq shift (moom--pos-x (moom--frame-left))))
;; Right side border
(when (> shift 0)
(moom-move-frame-left shift))))
(defun moom--update-display-border ()
"Update right and bottom border for negative coordinate value on windows-nt."
(let ((dma-list (display-monitor-attributes-list))
(rb 0)
(lb 0))
(dotimes (i (length dma-list))
(let ((gm (alist-get 'geometry (nth i dma-list))))
(when (< (nth 0 gm) 0)
(setq rb (nth 0 gm)))
(when (< (nth 1 gm) 0)
(setq lb (nth 1 gm)))))
(setq moom--display-border
(list (+ (display-pixel-width) rb)
(+ (display-pixel-height) lb)))))
(defun moom--current-monitor-id ()
"Provide id of the current monitor."
(let ((dma-list (display-monitor-attributes-list))
(id 0))
(dotimes (i (length dma-list))
(when (equal (alist-get 'geometry (nth i dma-list)) moom--last-monitor)
(setq id i)))
id))
(defun moom--read-user-margin ()
"Read new user margin."
(mapcar #'string-to-number
(split-string
(read-string "New user margin: "
(mapconcat #'identity (mapcar
#'number-to-string
moom-user-margin) " ")))))
(defun moom--add-command-history (status)
"Add the last STATUS to `moom--command-history' for undo feature."
(unless (equal status (car moom--command-history))
(push status moom--command-history)
(when (> (length moom--command-history) moom-command-history-length)
(nbutlast moom--command-history))))
;;;###autoload
(defun moom-undo (&optional index)
"Undo.
If INDEX is non-nil, revert to the provided id of history."
(interactive)
(unless index
(setq index 0))
(if (and (numberp index)
(> (length moom--command-history) index))
(let ((previous (nth index moom--command-history)))
(moom-restore-last-status previous)
(setq moom--command-history (nthcdr (1+ index) moom--command-history))
(when moom-verbose
(message "[moom] %s undo available" (length moom--command-history))))
(message "[moom] No further undo")))
;;;###autoload
(defun moom-identify-current-monitor (&optional shift)
"Update `moom--screen-margin' to identify and focus on the current monitor.
SHIFT can control the margin, if needed.
If SHIFT is nil, `moom--common-margin' will be applied."
(interactive)
(let ((geometry (moom--frame-monitor-geometry))
(workarea (moom--frame-monitor-workarea)))
(if (not (> (length (display-monitor-attributes-list)) 1))
(setq moom--screen-margin (moom--default-screen-margin))
(setq moom--screen-margin
(let ((shift (or shift moom--common-margin)))
(list (+ (nth 1 workarea)
(nth 0 shift))
(+ (- (display-pixel-height)
(nth 1 workarea)
(nth 3 workarea))
(nth 1 shift))
(+ (nth 0 workarea)
(nth 2 shift))
(+ (- (display-pixel-width)
(nth 0 workarea)
(nth 2 workarea))
(nth 3 shift)))))
(moom--merge-screen-margin moom--local-margin))
(moom--merge-screen-margin moom-user-margin)
(moom--update-display-border)
(unless (equal moom--last-monitor geometry)
(setq moom--last-monitor geometry)
(when moom-verbose
(message "[Moom] The current monitor has been changed to %s" geometry))
(moom--make-frame-height-list)))
(when (eq window-system 'mac) ;; To avoid visual error
(redraw-frame)))
;;;###autoload
(defun moom-print-monitors ()
"Print available monitors with index number.
`moom-jump-to-monitor' could be useful to jump to a monitor."
(interactive)
(let ((dma-list (display-monitor-attributes-list))
(msg nil))
(dotimes (i (length dma-list))
(let ((gm (alist-get 'geometry (nth i dma-list))))
(push (format "[%s] W:%04s H:%04s (%05s,%05s) %s"
i (nth 2 gm) (nth 3 gm) (nth 0 gm) (nth 1 gm)
(if (equal gm moom--last-monitor) "<current>" "")) msg)))
(when moom-verbose
(message "%s" (mapconcat 'concat (reverse msg) "\n")))))
;;;###autoload
(defun moom-jump-to-monitor (id)
"Jump to a monitor by specifying ID."
(interactive (list
(read-number
"Target monitor: " (moom--current-monitor-id))))
(let ((dma-list (display-monitor-attributes-list)))
(if (< id (length dma-list))
(let ((workarea (alist-get 'workarea (nth id dma-list))))
(set-frame-position nil (nth 0 workarea) (nth 1 workarea))
(when (memq window-system '(x))
(sleep-for 0.2)) ;; FIXME
(moom-identify-current-monitor)
(run-hooks 'moom-after-select-monitor-hook))
(user-error "Target index shall be less than %s" id (length dma-list)))))
;;;###autoload
(defun moom-cycle-monitors ()
"Cycle monitors.
`moom-after-select-monitor-hook' could be useful to add some additional
actions when selecting a monitor."
(interactive)
(let ((dma-list (display-monitor-attributes-list)))
(moom-jump-to-monitor
(let ((v (1+ (moom--current-monitor-id))))
(if (equal v (length dma-list)) 0 v))))
(moom-print-monitors))
;;;###autoload
(defun moom-fill-screen ()
"Expand frame width and height to fill screen.
The font size in buffers will be increased so that the frame width could be
maintained at 80. The top left corner of the frame is moved to that of screen.
`moom-before-fill-screen-hook' and `moom-after-fill-screen-hook' can be
used to add additional actions."
(interactive)
(let ((last (moom--save-last-status)))
(run-hooks 'moom-before-fill-screen-hook)
(moom--font-resize (moom--fullscreen-font-size)
(+ (moom--max-frame-pixel-width)
(moom--frame-internal-width)))
(set-frame-position nil
(moom--pos-x (nth 2 moom--screen-margin))
(moom--pos-y (nth 0 moom--screen-margin)))
(set-frame-size nil
(moom--max-frame-pixel-width)
(moom--max-frame-pixel-height) t)
(moom-print-status)
(run-hooks 'moom-after-fill-screen-hook)
(when (or moom--non-interactive-history (called-interactively-p 'any))
(moom--add-command-history last))))
;;;###autoload
(defun moom-toggle-frame-maximized ()
"Toggle frame maximized.
No information is stored for undo."
(interactive)
(let ((moom--print-status nil))
(if moom--maximized
(progn
(moom-restore-last-status moom--maximized)
(setq moom--maximized nil))
(setq moom--maximized (moom--save-last-status))
(moom-fill-screen)))
(moom-print-status))
;;;###autoload
(defun moom-fill-top ()
"Fill upper half of screen."
(interactive)
(let ((last (moom--save-last-status)))
(moom--fill-display 'top)
(when (or moom--non-interactive-history (called-interactively-p 'any))
(moom--add-command-history last))))
;;;###autoload
(defun moom-fill-bottom ()
"Fill lower half of screen."
(interactive)
(let ((last (moom--save-last-status)))
(moom--fill-display 'bottom)
(when (or moom--non-interactive-history (called-interactively-p 'any))
(moom--add-command-history last))))
;;;###autoload
(defun moom-fill-left ()
"Fill left half of screen."
(interactive)
(let ((last (moom--save-last-status)))
(moom--fill-display 'left)
(when (or moom--non-interactive-history (called-interactively-p 'any))
(moom--add-command-history last))))
;;;###autoload
(defun moom-fill-right ()
"Fill right half of screen."
(interactive)
(let ((last (moom--save-last-status)))
(moom--fill-display 'right)
(when (eq window-system 'w32)
(moom-move-frame-to-edge-right))
(when (or moom--non-interactive-history (called-interactively-p 'any))
(moom--add-command-history last))))
;;;###autoload
(defun moom-fill-top-left ()
"Fill top left quarter of screen."
(interactive)
(let ((last (moom--save-last-status)))
(moom--fill-display 'topl)
(when (or moom--non-interactive-history (called-interactively-p 'any))
(moom--add-command-history last))))
;;;###autoload
(defun moom-fill-top-right ()
"Fill top right quarter of screen."
(interactive)
(let ((last (moom--save-last-status)))
(moom--fill-display 'topr)
(when (eq window-system 'w32)
(moom-move-frame-to-edge-right))
(when (or moom--non-interactive-history (called-interactively-p 'any))
(moom--add-command-history last))))
;;;###autoload
(defun moom-fill-bottom-left ()
"Fill bottom left quarter of screen."
(interactive)
(let ((last (moom--save-last-status)))
(moom--fill-display 'botl)
(when (or moom--non-interactive-history (called-interactively-p 'any))
(moom--add-command-history last))))
;;;###autoload
(defun moom-fill-bottom-right ()
"Fill bottom right quarter of screen."
(interactive)
(let ((last (moom--save-last-status)))
(moom--fill-display 'botr)
(when (eq window-system 'w32)