-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
moc.el
1734 lines (1577 loc) · 68.2 KB
/
moc.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
;;; moc.el --- Master of Ceremonies -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Positron Solutions <[email protected]>
;; Author: Positron Solutions <[email protected]>
;; Keywords: convenience, outline
;; Version: 0.6.3
;; Package-Requires: ((emacs "29.4") (hide-mode-line "1.0.3") (transient "0.7.2"))
;; Homepage: http://github.com/positron-solutions/moc
;;; Copying:
;; 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:
;;
;; Master of ceremonies. Tools for display, screen capture, and presentation:
;;
;; - fullscreen focus with highlight and playback with `moc-focus'
;; - set an exact frame resolution for capture with `moc-fixed-frame-set'
;; - subtle, disappearing cursor with `moc-subtle-cursor-mode'
;; - hide cursor entirely with `moc-hide-cursor-mode'
;; - supress all messages with `moc-quiet-mode'
;; - remap many faces with `moc-face-remap'
;; - set many options at once with `moc-dispatch'
;;
;; To all the MCs out there who go by MC Focus, my sincerest apologies for the
;; unfortunate naming collision. We will attempt to bring glory to your name.
;;; Code:
(require 'frame)
(require 'face-remap)
(require 'rect)
(require 'transient)
(require 'hide-mode-line)
(eval-when-compile (require 'cl-lib))
(defgroup moc nil "Master of ceremonies."
:prefix 'mc
:group 'outline)
(defcustom moc-subtle-cursor-blinks 3
"The number of blinks of the subtle cursor.
When using a transient cursor effect, the duration of cursor visibility
is the product of this and `moc-subtle-cursor-interval'.
\\[info] elisp::Cursor Parameters."
:type 'integer)
(defcustom moc-subtle-cursor-interval 0.2
"Length of cursor blink interval in seconds.
Values smaller than 0.013 will be treated as 0.013."
:type 'number)
(defcustom moc-focus-max-height-factor 0.75
"Focused text maximum height fraction.
This is never exceeded."
:type 'float)
(defcustom moc-focus-max-width-factor 0.75
"Focused text maximum width fraction.
This is never exceeded."
:type 'float)
(defcustom moc-focus-max-area-factor 0.40
"Focused text goal area.
Area conveniently expresses the dependency between height and width.
Text that is extremely long or extremely tall will be limited by
`moc-focus-height-factor-max' and `moc-focus-width-factor-max'. Text that
is approximately screen-shaped will often be limited by this factor
first. Screen proportions are taken into account, so width usually has
a larger effect on screen area than height."
:type 'float)
(defcustom moc-focus-max-scale 20.0
"Maximum scale of focused text.
When focusing extremely small regions, this value prevents the text from
being scaled comically large. If you just want to render single symbols
or extremely short expressions, this setting can be used to control
excessively large results."
:type 'float)
(defcustom moc-focus-default-remaps '(org-block-no-background)
"A list of remap presets to apply to focused text.
Each symbol is a key of `moc-face-remap-presets'. You can still manually
apply or clear remaps using `moc-face-remap' and `moc-face-remap-clear'.
The defaults will just be turned on to save time in the usual cases."
:type '(repeat symbol))
;; TODO buffer name cannot be varied unless certain hooks are aware of which
;; buffers are focus buffers. Either scan, track, or don't bother. Multiple
;; focus buffers are impossible without names.
(defcustom moc-screenshot-dir #'temporary-file-directory
"Directory path or function that returns a directory path.
Directory path is a string."
:type '(choice string function))
(defcustom moc-screenshot-type 'svg
"What type of file to save.
Options are same as supported by the backend, `x-export-frames' for now,
either pdf (default), png, postscript, or svg. Supported types are
determined by the compile-time configuration of cairo."
:type '(choice (const :tag "PNG" png)
(const :tag "Scalable Vector Graphics" svg)
(const :tag "PDF" pdf)
(const :tag "Postscript" postscript)))
(defcustom moc-fixed-frame-sizes
'((youtube-short . (1080 . 1920))
(1080p . (1920 . 1080))
(2k . (2560 . 1440))
(4k . (3840 . 2160))
(fullscreen . fullboth))
"Frequent screen capture resolutions.
Form is one of:
- (NAME . (WIDTH . HEIGHT))
- (NAME . FULLSCREEN)
NAME is a symbol, WIDTH and HEIGHT are integers, and FULLSCREEN
is valid value for the `fullscreen' frame parameter.
\\[info] elisp::Frame Parameters"
:type '(cons symbol
(choice (cons number number)
symbol)))
(defcustom moc-face-remap-presets
'((bold . ((default :weight bold)))
(org-block-no-background . ((org-block :background reset
:extend reset))))
"Face remapping presets.
Value is an alist. Each entry should be a cons of SYMBOL PRESET.
SYMBOL will be used to choose the PRESET. PRESET is an ALIST where each
element of PRESET is a cons of FACE SPECS where SPECS is one of the
forms understood by `face-remap-add-relative'.
\\[info] elisp::Face Remapping"
:type 'alist)
(defcustom moc-frame-text-scale-step 1.05
"The factor of increase or decrease.
Because this is applied via `expt' and the steps are tracked, you can
return to the original sized text and not some noise-accumulating silly
value."
:type 'float)
(defvar moc--quiet-old-inhibit-message nil)
;; TODO naming consistency
(defvar moc--blink-cursor-old nil)
(defvar moc--subtle-cursor-dead-windows nil
"Store windows where the cursor was left off.")
(defvar moc--subtle-cursor-old-point-buffer nil
"Last position of the cursor when blinking was started.")
(defvar moc-subtle-cursor-timer nil
"Timer started from `moc-subtle-cursor-start'.
This timer calls `moc-subtle-cursor-timer-function' every
`moc-subtle-cursor-interval' seconds.")
(defvar moc-subtle-cursor-blinks-done 0
"Number of blinks done since we started blinking on NS, X, and MS-Windows.")
(defvar-local moc--focus-margin-left 0
"For margin maintenance in `moc--focus-refresh'.")
(defvar-local moc--focus-margin-right 0
"For margin maintenance in `moc--focus-refresh'.")
(defvar-local moc--focus-overlay-specs nil
"Serialized specifications of overlays.
Structure is a list of (BEG END . PROPS) where PROPS comes from
`overlay-properties.'")
(defvar-local moc--focus-invisibilty-spec nil
"The invisibility spec from the source.
Same structure as valid values for `buffer-invisibility-spec'.")
(defvar-local moc--focus-continuation nil "Line continuation strategy.")
(defvar-local moc--focus-overlays nil
"Overlays applied from `moc--focus-overlay-specs'.")
(defvar-local moc--focus-scale-overlay nil
"Overlay responsible for scaling content.")
(defvar-local moc--focus-highlight-overlays nil
"Overlays used to highlight focused text.
Each region is a cons of BEG END. In actuality these overlays are a
negative image of highlighted spans. They add the shadow face to
non-highlighted text.")
(defvar-local moc--focus-highlights nil
"List of highlighted regions.
Each region is a cons of BEG END.")
(defvar-local moc--focus-occluding-overlays nil
"Overlays used to occlude.
Unlike the so-called highlight overlays, these overlays really do
occlude text and their implementation is a bit simpler.")
(defvar-local moc--focus-occludes nil
"List of occluded regions.")
(defvar-local moc--focus-cleaned-text nil
"Copy of cleaned input text for replay expressions.")
(defvar-local moc--focus-old-fringe-background nil
"For restoring the fringe background.")
(defvar-local moc--focus-old-subtle-cursor nil
"Whether subtle cursor was active before focusing.")
(defvar-local moc--focus-old-quiet nil
"Whether quiet mode was active before focusing.")
(defvar-local moc--focus-old-window-config nil)
(defvar-local moc-focus-base-buffer nil
"Stores a reference to the focus buffer was called from.
Focus buffers can be discarded a lot. This allows buffer locals of a
base buffer to be relied upon for implementing things.")
(defvar moc--fixed-frame-timer nil)
(defvar-local moc--face-remap-cookies nil)
(defconst moc-focus-playback-version 0
"🚧 This version is considered unstable.
It will be updated to 1 after some stability has been observed. It is
being added now so that plaback expressions will be clearly marked as
unstable.")
;; * Mass Face Remapping
(defun moc--read-remap (&optional preset)
"Prompt for a preset.
PRESET is passed from elisp programs to load pre-deteremined presets."
(when-let* ((key (or preset
(completing-read
"Choose a remap preset: "
moc-face-remap-presets))))
(cdr (assoc-string key moc-face-remap-presets))))
(defun moc-face-remap-clear ()
"Unmap any previously remapped faces."
(interactive)
(while-let ((cookie (pop moc--face-remap-cookies)))
(face-remap-remove-relative cookie)))
;;;###autoload
(defun moc-face-remap (remap &optional keep-existing)
"Remap many faces at once.
REMAP can be a symbol specifying a preset or an alist of FACE REMAP
pairs. If any faces have already been remapped, you can pass non-nil
KEEP-EXISTING"
(interactive (list (moc--read-remap) current-prefix-arg))
(unless keep-existing
(moc-face-remap-clear))
;; TODO anonymous remapping, perhaps informed by text properties at point to
;; select the correct face?
(let ((remap (if (symbolp remap)
(or (moc--read-remap remap)
(user-error "Remapping not found"))
remap)))
(mapc (lambda (r)
(let ((face (car r))
(specs (cdr r)))
(push (face-remap-add-relative face specs)
moc--face-remap-cookies)))
remap)))
;; * Hide Cursor Mode
(defvar moc-subtle-cursor-mode) ; compiler appeasement
(define-minor-mode moc-hide-cursor-mode
"Make cursor completely hidden."
:group 'moc
(cond
(moc-hide-cursor-mode
(if (minibufferp)
(moc-hide-cursor-mode -1)
(when moc-subtle-cursor-mode
(moc-subtle-cursor-mode -1))
(setq-local cursor-type nil)))
(t
(setq-local cursor-type (default-value 'cursor-type)))))
;; * Frame Text Scale Mode
;;;###autoload
(define-minor-mode moc-frame-text-scale-mode
"Scale the default font for a given frame.
When frames have had their text scale set, activating this mode recovers that
scale and deactivating it resets their original scale.
This mode is not aware of other mechanisms of tracking the frame's text
scale and conflicting modes will clobber each other."
:group 'moc
:global t
(cond
(moc-frame-text-scale-mode
(cl-loop
for f in (frame-list)
do (when-let ((step (frame-parameter f 'moc--frame-text-scale)))
(let* ((orig (or (frame-parameter f 'moc--frame-text-scale-orig)
(face-attribute 'default :height f))))
(set-face-attribute
'default f :height
(round (* orig (expt moc-frame-text-scale-step step))))))))
(t
(cl-loop
for f in (frame-list)
do (when-let ((orig (frame-parameter f 'moc--frame-text-scale-orig)))
(set-face-attribute 'default f :height orig))))))
(defun moc--frame-text-scale-cleanup-when-done ()
"Self-explanatory.
If no frames have a non-zero step value, turn off the mode."
(unless (cl-loop
for f in (frame-list)
when (when-let ((step (frame-parameter f 'moc--frame-text-scale)))
(not (= 0 step)))
return f)
(moc-frame-text-scale-mode -1)))
(defun moc-frame-text-scale-increase ()
"Increase the frame text scale."
(interactive)
(unless moc-frame-text-scale-mode
(moc-frame-text-scale-mode 1))
(let* ((frame (selected-frame))
(orig (or (frame-parameter frame 'moc--frame-text-scale-orig)
(face-attribute 'default :height frame)))
(step (1+ (or (frame-parameter frame 'moc--frame-text-scale)
0)))
(new (round (* orig (expt moc-frame-text-scale-step step)))))
(set-frame-parameter frame 'moc--frame-text-scale step)
(set-face-attribute 'default frame :height new)
(unless (frame-parameter frame 'moc--frame-text-scale-orig)
(set-frame-parameter frame 'moc--frame-text-scale-orig orig))
(when (= step 0)
(moc--frame-text-scale-cleanup-when-done))))
(defun moc-frame-text-scale-decrease ()
"Decrease the frame text scale."
(interactive)
(unless moc-frame-text-scale-mode
(moc-frame-text-scale-mode 1))
(let* ((frame (selected-frame))
(orig (or (frame-parameter frame 'moc--frame-text-scale-orig)
(face-attribute 'default :height frame)))
(step (1- (or (frame-parameter frame 'moc--frame-text-scale)
0)))
(new (round (* orig (expt moc-frame-text-scale-step step)))))
(set-frame-parameter frame 'moc--frame-text-scale step)
(set-face-attribute 'default frame :height new)
(unless (frame-parameter frame 'moc--frame-text-scale-orig)
(set-frame-parameter frame 'moc--frame-text-scale-orig orig))
(when (= step 0)
(moc--frame-text-scale-cleanup-when-done))))
(defun moc-frame-text-scale-set (steps)
"Set a specific number of STEPS.
Tired of trying to +/- it right? Set a specific value with this
command. STEPS can be 0 for no height or positive or negative integers.
The result is identical to increasing or decreasing STEPS times."
(interactive
(let ((current-step
(or 0 (frame-parameter frame 'moc--frame-text-steps))))
(list (moc--read-N "New height steps: " current-step))))
(unless moc-frame-text-scale-mode
(moc-frame-text-scale-mode 1))
(let* ((frame (selected-frame))
(orig (or (frame-parameter frame 'moc--frame-text-scale-orig)
(face-attribute 'default :height frame)))
(height (round (* orig (expt moc-frame-text-scale-step steps)))))
(set-face-attribute 'default frame :height height)
(set-frame-parameter frame 'moc--frame-text-scale steps)
(unless (frame-parameter frame 'moc--frame-text-scale-orig)
(set-frame-parameter frame 'moc--frame-text-scale-orig orig))
(when (= steps 0)
(moc--frame-text-scale-cleanup-when-done))))
(defun moc-frame-text-scale-reset ()
"Set the current frame back to its original text scale."
(interactive)
(moc-frame-text-scale-set 0))
;; Really wish this was an interactive short code...
(defun moc--read-N (prompt &optional initial)
"Read an integer, including zero and negative numbers.
PROMPT will be used as a prompt. INITIAL is an initial value.
Shocking."
(let ((str (read-from-minibuffer
prompt
(when initial (number-to-string initial)) nil nil nil)))
(if (string-match-p "^-?[0-9]*$" str)
(string-to-number str)
(user-error "Could not read number: %s" str))))
;; * Subtle Cursor Mode
(defun moc-subtle-cursor-start ()
"Start the `moc-subtle-cursor-timer'.
This starts the timer `moc-subtle-cursor-timer', which makes the cursor
blink if appropriate."
(cond
;; stale hook fired
((null moc-subtle-cursor-mode) (moc-subtle-cursor-mode -1))
(t
;; TODO detect when buffer contents were changed but cursor stayed in the
;; same place.
(setq moc--subtle-cursor-old-point-buffer
(cons (point) (current-buffer)))
(when moc-subtle-cursor-timer
(cancel-timer moc-subtle-cursor-timer))
;; TODO figure out the termination for 1 blink
(setq moc-subtle-cursor-blinks-done 1)
(setq moc-subtle-cursor-timer
(run-with-timer (max 0.013 moc-subtle-cursor-interval)
(max 0.013 moc-subtle-cursor-interval)
#'moc-subtle-cursor-timer-function))
;; Use the `cursor-type' ON-STATE
(internal-show-cursor nil t))))
(defun moc-subtle-cursor-timer-function ()
"Timer function of timer `moc-subtle-cursor-timer'."
(when moc-subtle-cursor-mode
(internal-show-cursor nil (not (internal-show-cursor-p))))
;; Suspend counting blinks when the w32 menu-bar menu is displayed,
;; since otherwise menu tooltips will behave erratically.
(or (and (fboundp 'w32--menu-bar-in-use)
(w32--menu-bar-in-use))
;; XXX guarding this expression upsets the blink count and I don't know
;; how it's supposed to work.
(setq moc-subtle-cursor-blinks-done (1+ moc-subtle-cursor-blinks-done)))
;; Each blink is two calls to this function.
(when (and (> moc-subtle-cursor-blinks 0)
(>= moc-subtle-cursor-blinks-done (* 2 moc-subtle-cursor-blinks)))
(when moc-subtle-cursor-timer (cancel-timer moc-subtle-cursor-timer)
(setq moc-subtle-cursor-timer nil))
(push (selected-window) moc--subtle-cursor-dead-windows)
(when (internal-show-cursor-p)
(message "Subtle cursor cancelled timer in ON-STATE"))))
(defun moc-subtle-cursor--should-blink ()
"Determine whether we should be blinking.
Returns whether we have any focused non-TTY frame."
(and moc-subtle-cursor-mode
(not (and (eq (point) (car moc--subtle-cursor-old-point-buffer))
(eq (current-buffer)
(cdr moc--subtle-cursor-old-point-buffer))))
(let ((frame-list (frame-list))
(any-graphical-focused nil))
(while frame-list
(let ((frame (pop frame-list)))
(when (and (display-graphic-p frame) (frame-focus-state frame))
(setf any-graphical-focused t)
(setf frame-list nil))))
any-graphical-focused)))
(defun moc-subtle-cursor-check ()
"Check if cursor blinking shall be restarted.."
(when (moc-subtle-cursor--should-blink)
(moc-subtle-cursor-start)))
;;;###autoload
(define-minor-mode moc-subtle-cursor-mode
"Like `blink-cursor-mode' but leaves cursor off.
This is a modification of `blink-cursor-mode' that immediately
transitions to the ON-STATE when commands are entered and finishes
blinking in the OFF-STATE, enabling customization of `cursor-type' and
`blink-cursor-alist' to achieve a transient cursor or a very subtle
cursor when the user is not moving the point.
\\[info] elisp::Cursor Parameters.
When you do anything to move the cursor, it will remain visible for the
product of `moc-subtle-cursor-blinks' and `moc-subtle-cursor-interval'.
Because this mode conflicts with `blink-cursor-mode', it is turned off when
found active.
🚧 The mode is experimental."
:global t
(cond
(moc-subtle-cursor-mode
(setq moc--blink-cursor-old blink-cursor-mode)
(when blink-cursor-mode
(blink-cursor-mode -1))
(when moc-hide-cursor-mode
(moc-hide-cursor-mode -1))
(add-function :after after-focus-change-function
#'moc-subtle-cursor-check)
(add-hook 'after-delete-frame-functions #'moc-subtle-cursor-check)
(add-hook 'post-command-hook #'moc-subtle-cursor-check)
(moc-subtle-cursor-check))
(t
(remove-hook 'post-command-hook #'moc-subtle-cursor-check)
(remove-hook 'after-delete-frame-functions #'moc-subtle-cursor-check)
(remove-function after-focus-change-function
#'moc-subtle-cursor-check)
(when moc-subtle-cursor-timer
(cancel-timer moc-subtle-cursor-timer))
;; Make sure to leave the cursor in the ON-STATE in all windows when
;; quitting.
;; TODO seems like this never actually happens. Cursor has an alternate
;; state when left around in another window, regardless of whether it was
;; blink on or off when the window changed.
(while-let ((win (pop moc--subtle-cursor-dead-windows)))
(internal-show-cursor win t))
;; Selected window likely not in above dead window cleanup and could be in
;; blink off state.
(internal-show-cursor nil t)
(setq moc--subtle-cursor-old-point-buffer nil)
(when moc--blink-cursor-old
(blink-cursor-mode 1)
(setq moc--blink-cursor-old nil)))))
;; * Quiet mode
;;;###autoload
(define-minor-mode moc-quiet-mode
"Inhibit messages in the echo area.
⚠️ Inhibiting messages is a bit dangerous. If anything fails, because messages
are disabled, there may be no obvious user feedback ☠️"
:group 'moc
:global t
(cond
(moc-quiet-mode
;; Naturally the manual sets not to set this, but the point is that the user
;; doesn't want to have messages for a while. If it is never to be turned
;; off, how else can messages be avoided except case by case with
;; let-binding?
(unless inhibit-message
(setq moc--quiet-old-inhibit-message inhibit-message
inhibit-message t)))
(t
(setq inhibit-message moc--quiet-old-inhibit-message))))
;; * Fixed Frame Size
(defun moc--fixed-frame-check-cleanup ()
"Clean up hook if not guarding any more frames."
(let ((frames (frame-list))
guarded)
(while (and frames (not guarded))
(when (frame-parameter (pop frames) 'moc--fixed-frame-notify)
(setq guarded t)))
(unless guarded
(remove-hook 'window-size-change-functions #'moc--fixed-frame-notify))))
(defun moc--fixed-frame-release (frame)
"Release FRAME from size management.
Allow state cleanup if no more frames are under management."
(set-frame-parameter frame 'moc--fixed-frame-goal nil)
(moc--fixed-frame-check-cleanup))
(defun moc--fixed-frame-notify (frame)
"Check if FRAME has the right size."
(if (frame-parameter frame 'fullscreen)
;; Only frames with a non-fullscreen size are guarded, so we bail if they
;; have acquired a fullscreen parameter.
(progn (message "Frame: %s has become fullscreen. Releasing." frame)
(moc--fixed-frame-release frame))
(when-let* ((size (frame-parameter frame 'moc--fixed-frame-goal)))
(moc--fixed-frame-verify frame size))))
(defun moc--fixed-frame-verify (frame size)
"Verify FRAME is SIZE or schedule correction."
(let ((width-correction (- (car size) (frame-pixel-width frame)))
(height-correction (- (cdr size) (frame-pixel-height frame))))
(unless (and (= width-correction 0)
(= height-correction 0)
(null moc--fixed-frame-timer))
(setq moc--fixed-frame-timer
(run-with-timer 0.0 nil #'moc--fixed-frame-correct-all)))))
;;;###autoload
(defun moc-fixed-frame-release-all ()
"Release all guarded frames."
(interactive)
(let ((frames (frame-list)))
(while-let ((frame (pop frames)))
(set-frame-parameter frame 'moc--fixed-frame-goal nil))
(moc--fixed-frame-check-cleanup)))
(defun moc--fixed-frame-correct (frame size &optional no-set)
"Check and correct that FRAME is SIZE.
When optional NO-SET is non-nil, only check and set once. Otherwise
set, check and set."
;; Its necessary to set once to find the correction needed to get the exact
;; frame size we want. This same function can set up for itself and will not
;; do unnecssary work if no correction is needed.
(unless no-set (moc--fixed-frame-set frame size))
(let ((width-correction (- (car size) (frame-pixel-width frame)))
(height-correction (- (cdr size) (frame-pixel-height frame))))
(unless (and (= width-correction 0)
(= height-correction 0))
(let ((frame-resize-pixelwise t))
(message "making corrections: %sw %sh"
width-correction height-correction)
(set-frame-size frame
(+ (car size) width-correction)
(+ (cdr size) height-correction)
t))
(message "corrected size: %sw %sh"
(frame-pixel-width frame)
(frame-pixel-height frame)))))
(defun moc--fixed-frame-correct-all ()
"Used as a single-call post-command hook to avoid thrashing."
;; Updating the frame size during the `window-size-change-functions' is not a
;; good idea. Temporarily removing the hook was an ineffective strategy in
;; this case. Instead, this function runs in the post command hook and, if
;; added, corrects all frames and removes itself.
(setq moc--fixed-frame-timer nil)
(dolist (frame (frame-list))
(if (frame-parameter frame 'fullscreen)
;; Only frames with a non-fullscreen size are guarded, so we bail if they
;; have acquired a fullscreen parameter.
(progn (message "Frame: %s has become fullscreen. Releasing." frame)
(moc--fixed-frame-release frame))
(when-let* ((size (frame-parameter frame 'moc--fixed-frame-goal)))
(moc--fixed-frame-correct frame size)))))
(defun moc--fixed-frame-set (frame size)
"Set SIZE on FRAME.
SIZE is either a (H . W) cons or a symbol that can be used as a frame
parameter for `fullscreen'."
(if (consp size)
(unless (and (= (car size) (frame-pixel-width frame))
(= (cdr size) (frame-pixel-height frame)))
(let ((frame-resize-pixelwise t))
(set-frame-parameter nil 'fullscreen nil)
(set-frame-size nil (car size) (cdr size) t)
(message "set size: %sw %sh"
(frame-pixel-width frame)
(frame-pixel-height frame))
(moc--fixed-frame-correct frame size t)))
(set-frame-parameter nil 'fullscreen size)
(message "fullscreen: %s" size)))
;;;###autoload
(defun moc-fixed-frame-set (frame-size)
"Set and maintain a fixed FRAME-SIZE.
FRAME-SIZE is either a key for `moc-fixed-frame-sizes' or a valid value
of it.
Will correct the frame size if any window manager silliness attempts to
make your frame another size. Adds a hook to preserve the desired frame
size.
🚧 This feature is experimental and has some behaviors that may be
confusing. A fixed frame will be released if it is converted to full
screen. Only fixed frames have their size maintained. When resizing
with a mouse, the resize will appear successful, but then the size will
revert after the first command. With the right comination of hooks,
these behaviors may become more consistent."
(interactive (list (completing-read
"Select size: "
(if (frame-parameter (selected-frame)
'moc--fixed-frame-revert)
(cons 'revert moc-fixed-frame-sizes)
moc-fixed-frame-sizes))))
(let* ((frame (selected-frame))
(revert (string= frame-size "revert"))
(new (cond
(revert
(frame-parameter (selected-frame) 'moc--fixed-frame-revert))
((stringp frame-size)
(cdr (assoc-string frame-size moc-fixed-frame-sizes)))
((symbolp frame-size)
(cdr (assq frame-size moc-fixed-frame-sizes)))
((consp frame-size) frame-size)
(t (error "Unrecognized size: %s" frame-size))))
(current (if-let* ((fullscreen (frame-parameter nil 'fullscreen)))
fullscreen
(cons (frame-pixel-width)
(frame-pixel-height)))))
(set-frame-parameter nil 'moc--fixed-frame-revert (if revert nil current))
(moc--fixed-frame-set frame new)
(when (consp new)
(if revert
(set-frame-parameter frame 'moc--fixed-frame-goal nil)
(set-frame-parameter frame 'moc--fixed-frame-goal new)
(add-hook 'window-size-change-functions #'moc--fixed-frame-notify)))))
;; * Master of Ceremonies Dispatch
;; Let us tie everything together into. A transient.
;; There isn't a ton of consistency in how these are used. Still in the
;; trial-and-error phase of building up an in-transient UI
(defun moc--dispatch-frame-size ()
"Return frame size for use in info class."
(format
"current: %s"
(propertize
(if-let* ((full (frame-parameter nil 'fullscreen)))
(symbol-name full)
(format "%s %s" (frame-pixel-width) (frame-pixel-height)))
'face 'transient-value)))
(defun moc--dispatch-fixed-frames ()
"Return description for clearing fixed frames.
Used in suffix command."
(let ((frames (frame-list))
(fixed 0))
(while-let ((frame (pop frames)))
(when (frame-parameter frame 'moc--fixed-frame-goal)
(setq fixed (1+ fixed))))
(format
"release %-3s"
(if (> fixed 0)
(propertize (format "%3s frames" fixed) 'face 'success)
""))))
(defun moc--dispatch-cursor-mode ()
"Return cursor state for use in info class."
(if-let* ((cursor (if (consp cursor-type)
(car cursor-type)
(if (eq cursor-type t)
(frame-parameter nil 'cursor-type)
cursor-type))))
(if moc-subtle-cursor-mode
(propertize (format "subtle %-4s" cursor)
'face 'transient-value)
(propertize (format "%-11s" (symbol-name cursor))
'face 'transient-value))
(propertize "hidden " 'face 'shadow)))
(defun moc--dispatch-faces-remapped ()
"Return remap clear description including current remap state.
Use in suffix command."
(let ((remaps (length moc--face-remap-cookies)))
(format
"clear %s"
(if (> remaps 0)
(propertize (format "remaps %-4d" remaps) 'face 'success)
""))))
(defun moc--dispatch-frame-text-scale ()
"Return current frame text scale for info class."
(if-let ((step (frame-parameter (selected-frame) 'moc--frame-text-scale)))
(propertize (format "frame scale: %s" step)
'face 'transient-value)
(propertize "off" 'face 'shadow)))
(defun moc--dispatch-frame-text-scale-p ()
"Return if current frame has a non-zero text scale."
(when-let ((step (frame-parameter (selected-frame) 'moc--frame-text-scale)))
(not (eq 0 step))))
(defun moc--dispatch-text-scale ()
"Return current text scale for info class."
(if text-scale-mode
(propertize (format "scale: %s" text-scale-mode-amount)
'face 'transient-value)
(propertize "off" 'face 'shadow)))
(defun moc--dispatch-quiet-mode ()
"Return description and quiet mode state for suffix."
(format
"quiet %s"
(if moc-quiet-mode
(propertize "on " 'face 'success)
(propertize "off" 'face 'shadow))))
;;;###autoload (autoload 'moc-dispatch "moc" nil t)
(transient-define-prefix moc-dispatch ()
"You are the MC.
This is likely the command you want to bind globally to become familiar
with MoC commands and to make many adjustments at once."
:refresh-suffixes t
[["Frame Text Scale"
(:info #'moc--dispatch-frame-text-scale)
("+" "increase" moc-frame-text-scale-increase :transient t)
("-" "decrease" moc-frame-text-scale-decrease :transient t)
("=" "reset" moc-frame-text-scale-reset :transient t
:inapt-if-not moc--dispatch-frame-text-scale-p)]
["Buffer Text Scale"
(:info #'moc--dispatch-text-scale)
("t+" "increase" text-scale-increase :transient t)
("t-" "decrease" text-scale-decrease :transient t)
("t=" "reset" text-scale-mode :transient t
:inapt-if-nil text-scale-mode)]]
["Fixed Frame"
(:info #'moc--dispatch-frame-size)
("s" "set" moc-fixed-frame-set :transient t)
("R" moc-fixed-frame-release-all :transient t
:description moc--dispatch-fixed-frames)]
["Face Remapping"
("r" "remap" moc-face-remap :transient t)
("c" moc-face-remap-clear :transient t
:description moc--dispatch-faces-remapped)]
[["Cursor"
(:info #'moc--dispatch-cursor-mode)
("?" "hide" moc-hide-cursor-mode :transient t)
("." "subtle" moc-subtle-cursor-mode :transient t)]
["Mode Line"
("m" "hide" hide-mode-line-mode :transient t)]
["Echo area"
("e" moc-quiet-mode :transient t
:description moc--dispatch-quiet-mode)]])
;; * Screenshot
;; 🚧 If you consider working on this feature, support for other file type
;; support and naming support for workflows like animation are good to add along
;; the way. There are other packages for building gifs etc that would be
;; welcome in MoC as optional dependencies.
(defun moc--screenshot-save-dir ()
"Return the users screenshot save path, which may be computed."
(if (stringp moc-screenshot-dir)
moc-screenshot-dir
(if (functionp moc-screenshot-dir)
(or (funcall moc-screenshot-dir)
default-directory)
default-directory)))
;;;###autoload
(defun moc-screenshot ()
"Save a screenshot of the current frame as an SVG image.
This just provides minor conveniences like pre-configured save path with
`moc-screenshot-dir'."
(interactive)
(let* ((timestamp (format-time-string "%F-%T" (current-time)))
(filename (format "screenshot-%s.svg" timestamp))
(dir (moc--screenshot-save-dir))
(path (concat dir filename))
(data (x-export-frames nil moc-screenshot-type)))
(unless (file-exists-p dir)
(make-directory dir t))
(with-temp-file path
(insert data))
(message "Saved to: %s" filename)))
;; * Focus Fullscreen Text
;; Only add to the `buffer-list-update-hook' locally so we don't need to unhook
(defun moc--focus-refresh (window)
"Refresh buffer in WINDOW if buffer is visible again."
(when (eq (window-buffer window) (get-buffer "*MoC Focus*"))
(set-window-fringes window 0 0)
(set-face-attribute 'fringe (window-frame window)
:background 'unspecified)
(unless (= (current-left-margin) moc--focus-margin-left)
(set-window-margins window
moc--focus-margin-right
moc--focus-margin-left))))
(defun moc--focus-apply-overlays (overlay-specs &optional offset)
"Apply OVERLAY-SPECS to the buffer.
OVERLAY-SPECS is a list of (BEG END . PROPS) where PROPS is obtained
from `overlay-properties'.
Optional OFFSET is for overlay toggling or other update cases where the
buffer state is not pristine."
(while-let ((o (pop overlay-specs)))
(let* ((offset (or offset 0))
(beg (+ (pop o) offset))
(end (+ (pop o) offset))
(ov (make-overlay beg end nil t nil)))
(while-let ((prop (pop o)))
(overlay-put ov prop (pop o)))
(push ov moc--focus-overlays))))
(defun moc--focus-cleanup ()
"Clean up state for focus buffer upon kill."
(remove-hook 'window-state-change-functions #'moc--focus-refresh)
;; hidden cursor is buffer local and naturally goes away, but subtle cursor is
;; global and needs to be turned off if it wasn't on when focusing began.
;; XXX two MoC buffers could restore a modified value
(when moc--focus-old-fringe-background
(set-face-attribute 'fringe (selected-frame) :background
moc--focus-old-fringe-background))
(if (not moc--focus-old-quiet)
(when moc-quiet-mode
(moc-quiet-mode -1))
(setq moc--focus-old-quiet nil)
(unless moc-quiet-mode
(moc-quiet-mode 1)))
(if (not moc--focus-old-subtle-cursor)
(when moc-subtle-cursor-mode
(moc-subtle-cursor-mode -1))
(setq moc--focus-old-subtle-cursor nil)
(unless moc-subtle-cursor-mode
(moc-subtle-cursor-mode 1)))
(when moc--focus-old-window-config
(set-window-configuration moc--focus-old-window-config))
;; TODO buffer locals will die on their own
(setq moc--focus-cleaned-text nil))
(defun moc--focus-text-pixel-size (window continuation scale)
"Calculate the effective size of text in WINDOW.
The effective size depends on the content and our CONTINUATION strategy.
Since calculating how Emacs will layout text and its size is a
phenomenal waste of time even if it is done right, we temporarily set
the margins to the `fill-column' and turn on the correct continuation
modes and then measure the text with the benefit of everything that went
into Emacs text flow logic in the first place.
🚧 SCALE is an experimental argument that adjusts the margin size when
checking the final text size before adjusting the horizontal and
vertical offset in `moc-focus-replay'."
(cond
((member 'truncate-lines continuation)
(set-window-margins window
(max 0 (- (window-width)
(ceiling (* fill-column scale)))))
(toggle-truncate-lines 1)
(prog1 (window-text-pixel-size window)
(set-window-margins window nil)))
((member 'visual-line-mode continuation)
(visual-line-mode 1)
(set-window-margins window
(max 0 (- (window-width)
(ceiling (* fill-column scale)))))
(prog1 (window-text-pixel-size window)
(set-window-margins window nil)))
(t (window-text-pixel-size window))))
(defun moc-focus-playback (&rest args)
"Replay ARGS in a focus buffer.
See `mc-focus' for meaning of keys in ARGS.
⚠️ The :version is not checked in this function. The caller is
responsible for maintaining this package as a properly versioned
dependency and performing their own check of
`moc-focus-playback-version' in that case and throw throw your user
errors upstream!
🚧 Presently this code does a good job on the first pass and replay.
However, displaying in other buffers or re-displaying the buffer in
another window will likely leave something to be desired."
(when-let* ((old (get-buffer "*MoC Focus*")))
(kill-buffer old))
(let* ((base (current-buffer))
(buffer (get-buffer-create "*MoC Focus*"))
(text (plist-get args :text))
(overlay-specs (plist-get args :overlays))
(invisibility-spec (plist-get args :invisibility-spec))
(continuation (plist-get args :continuation))
(highlights (plist-get args :highlights))
(occludes (plist-get args :occludes)))
(set-buffer buffer)
(moc-focus-mode)
;; TODO many of these stored states are already buffer local
(setq-local moc--focus-old-window-config (current-window-configuration))
(delete-other-windows)
(switch-to-buffer buffer)
(setq-local moc-focus-base-buffer base)
(add-hook 'kill-buffer-hook #'moc--focus-cleanup nil t)
(setq-local mode-line-format nil)
(setq moc--focus-old-fringe-background (face-attribute 'fringe :background))
(set-face-attribute 'fringe (selected-frame) :background 'unspecified)
(setq buffer-invisibility-spec invisibility-spec)
(setq moc--focus-invisibilty-spec invisibility-spec)
(setq moc--focus-continuation continuation)
(setq moc--focus-old-subtle-cursor moc-subtle-cursor-mode)
(moc-hide-cursor-mode 1)
(setq moc--focus-old-quiet moc-quiet-mode)
(moc-quiet-mode 1)
(read-only-mode -1)
(setq-local moc--focus-cleaned-text text)
(insert text)
(setq-local moc--focus-overlay-specs overlay-specs)
(when overlay-specs (moc--focus-apply-overlays overlay-specs))
(mapc (lambda (remap) (moc-face-remap remap t))
moc-focus-default-remaps)
(setq moc--focus-highlights highlights)
;; TODO distinguish fully shadowed versus no highlights
(when highlights
(moc--focus-apply-highlights highlights))
(setq moc--focus-occludes occludes)
(when occludes
(moc--focus-apply-occludes occludes))
;; First scale the text up to the size it will need to be after the
;; continuation strategy and horizontal & vertical centering.
(let* ((w (window-pixel-width))
(h (window-pixel-height))
(window-pixel-area (* h w))
(text-pixel-size (moc--focus-text-pixel-size
(selected-window) continuation 1.0))
(text-pixel-w (float (car text-pixel-size)))
(text-pixel-h (float (cdr text-pixel-size)))
(text-pixel-area (* text-pixel-w text-pixel-h))
(max-scale-horizontal (/ (* w moc-focus-max-width-factor)
text-pixel-w))
(max-scale-vertical (/ (* h moc-focus-max-height-factor)
text-pixel-h))
(max-scale-by-area (/ (* window-pixel-area
moc-focus-max-area-factor)
text-pixel-area))
(scale (min max-scale-horizontal
max-scale-vertical