-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathholo-layer.el
995 lines (839 loc) · 41.2 KB
/
holo-layer.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
;;; holo-layer.el --- Holo Layer -*- lexical-binding: t -*-
;; Filename: holo-layer.el
;; Description: Holo Layer
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2018, Andy Stewart, all rights reserved.
;; Created: 2018-06-15 14:10:12
;; Version: 0.5
;; Last-Updated: 2022-10-10 15:23:53 +0800
;; By: Andy Stewart
;; URL: https://github.com/manateelazycat/holo-layer
;; Keywords:
;; Compatibility: emacs-version >= 28
;; Package-Requires: ((emacs "28") (posframe "1.1.7") (markdown-mode "2.6"))
;;
;; Features that might be required by this library:
;;
;; Please check README
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Holo-Layer
;;
;;; Installation:
;;
;; Please check README
;;
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET holo-layer RET
;;
;;; Change log:
;;
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Code:
(require 'cl-lib)
(require 'json)
(require 'map)
(require 'seq)
(require 'subr-x)
(require 'holo-layer-epc)
(defgroup holo-layer nil
"Holo-Layer group."
:group 'applications)
(defvar holo-layer-server nil
"The Holo-Layer Server.")
(defvar holo-layer-python-file (expand-file-name "holo_layer.py" (if load-file-name
(file-name-directory load-file-name)
default-directory)))
(defvar holo-layer-server-port nil)
(defvar holo-layer--sort-tab-is-load-p (require 'sort-tab nil t))
(defvar holo-layer--blink-search-is-load-p (require 'blink-search nil t))
(defun holo-layer--start-epc-server ()
"Function to start the EPC server."
(unless (process-live-p holo-layer-server)
(setq holo-layer-server
(holo-layer-epc-server-start
(lambda (mngr)
(let ((mngr mngr))
(holo-layer-epc-define-method mngr 'eval-in-emacs 'holo-layer--eval-in-emacs-func)
(holo-layer-epc-define-method mngr 'get-emacs-var 'holo-layer--get-emacs-var-func)
(holo-layer-epc-define-method mngr 'get-emacs-vars 'holo-layer--get-emacs-vars-func)
(holo-layer-epc-define-method mngr 'get-user-emacs-directory 'holo-layer--user-emacs-directory)
(holo-layer-epc-define-method mngr 'get-emacs-id 'holo-layer--get-emacs-id)
(holo-layer-epc-define-method mngr 'get-emacs-name 'holo-layer--get-emacs-name)
(holo-layer-epc-define-method mngr 'get-theme-mode 'holo-layer-get-theme-mode)
(holo-layer-epc-define-method mngr 'get-theme-foreground 'holo-layer-get-theme-foreground-color)
(holo-layer-epc-define-method mngr 'get-theme-background 'holo-layer-get-theme-background-color)
))))
(if holo-layer-server
(setq holo-layer-server-port (process-contact holo-layer-server :service))
(error "[Holo-Layer] holo-layer-server failed to start")))
holo-layer-server)
(defun holo-layer--eval-in-emacs-func (sexp-string)
(eval (read sexp-string))
;; Return nil to avoid epc error `Got too many arguments in the reply'.
nil)
(defun holo-layer--get-emacs-var-func (var-name)
(let* ((var-symbol (intern var-name))
(var-value (symbol-value var-symbol))
;; We need convert result of booleanp to string.
;; Otherwise, python-epc will convert all `nil' to [] at Python side.
(var-is-bool (prin1-to-string (booleanp var-value))))
(list var-value var-is-bool)))
(defun holo-layer--get-emacs-vars-func (&rest vars)
(mapcar #'holo-layer--get-emacs-var-func vars))
(defvar holo-layer-epc-process nil)
(defvar holo-layer-internal-process nil)
(defvar holo-layer-internal-process-prog nil)
(defvar holo-layer-internal-process-args nil)
(defcustom holo-layer-name "*holo-layer*"
"Name of Holo-Layer buffer."
:type 'string)
(defcustom holo-layer-python-command (if (memq system-type '(cygwin windows-nt ms-dos)) "python.exe" "python3")
"The Python interpreter used to run holo_layer.py."
:type 'string)
(defcustom holo-layer-enable-debug nil
"If you got segfault error, please turn this option.
Then Holo-Layer will start by gdb, please send new issue with `*holo-layer*' buffer content when next crash."
:type 'boolean)
(defcustom holo-layer-enable-log nil
"Enable this option to print log message in `*holo-layer*' buffer, default only print message header."
:type 'boolean)
(defcustom holo-layer-active-window-color "#cc2444"
"Border color for active window."
:type 'string)
(defcustom holo-layer-inactive-window-color "#000000"
"Border color for active window."
:type 'string)
(defcustom holo-layer-enable-cursor-animation nil
"Enable cursor animation."
:type 'boolean)
(defcustom holo-layer-enable-type-animation nil
"Enable type animation."
:type 'boolean)
(defcustom holo-layer-type-animation-style "flame"
"Type animation style, we can set it with `firework', `flame', `supernova', `lightning' string, default is `flame'."
:type 'string)
(defcustom holo-layer-cursor-animation-color-gradient t
"Enable cursor color gradient."
:type 'boolean)
(defcustom holo-layer-cursor-animation-color-gradient-start-value 50
"The start value of gradient cursor color, start color is lighter 50 than cursor color."
:type 'interger)
(defcustom holo-layer-cursor-animation-type "jelly"
"Cursor animation type can be (jelly, arrow, jelly easing)"
:type 'string)
(defcustom holo-layer-cursor-color nil
"Cursor color.
If you set it with nil, cursor color will follow current state dynamically."
:type 'string)
(defcustom holo-layer-cursor-alpha 200
"Cursor alpha(0-255)."
:type 'interger)
(defcustom holo-layer-cursor-animation-duration 200
"Animation duration for cursor (200ms)."
:type 'integer)
(defcustom holo-layer-cursor-animation-interval 10
"Animation interval for cursor (10ms)."
:type 'integer)
(defcustom holo-layer-hide-mode-line nil
"Hide mode-line if this option is enable."
:type 'boolean)
(defcustom holo-layer-place-info-font-size 18
"Place info font size."
:type 'integer)
(defcustom holo-layer-enable-place-info nil
"Turn on the option to display some information at the cursor in the upper right corner of the screen, such as the translation of the word at the cursor, which is disabled by default."
:type 'boolean)
(defcustom holo-layer-place-info-dictionary "kdic-ec-11w"
"SDCV dictionary for word completion.
Default is `kdic-ec-11w', you can replace it with StarDict dictionary path
Example, if you have dictionary `/usr/share/stardict/dic/stardict-oxford-gb-formated-2.4.2/oxford-gb-formated.ifo',
you need set this value to `/usr/share/stardict/dic/stardict-oxford-gb-formated-2.4.2/oxford-gb-formated', not include `.ifo' extension."
:type 'string)
(defcustom holo-layer-enable-window-border nil
"Show window border if enable this option."
:type 'boolean)
(defcustom holo-layer-enable-window-number-background nil
"Show background for window number more clarity if enable this option."
:type 'boolean)
(defcustom holo-layer-enable-indent-rainbow nil
"Show window border if enable this option."
:type 'boolean)
(defcustom holo-layer-window-number-color "#cc2444"
"Color for window number."
:type 'string)
(defcustom holo-layer-window-number-font-size 40
"Font size for window number."
:type 'integer)
(defcustom holo-layer-cursor-block-commands '("watch-other-window-up" "watch-other-window-down")
"Cursor animation is disabled if the current command matches `holo-layer-cursor-block-commands'."
:type 'list)
(defcustom holo-layer-sort-tab-ui nil
"Whether render tab ui for sort-tab.
Default is disable.")
(defcustom holo-layer-sort-tab-font-size 18
"Sort tab font size."
:type 'integer)
(defcustom holo-layer-indent-colors '("#F8FE29" "#C8F424" "#5BAB3C" "#4B713F" "#244E30"
"#F8D991" "#F6B080" "#F58B60" "#E1664C" "#774C3E"
"#F7D967" "#3DB3D0" "#C24347" "#AA5A9C" "#1E588D")
"Colors for indent line."
:type 'list)
(defconst holo-layer--w32-frame-p (eq (framep-on-display) 'w32))
(defun holo-layer--user-emacs-directory ()
"Get lang server with project path, file path or file extension."
(expand-file-name user-emacs-directory))
(defun holo-layer-call-async (method &rest args)
"Call Python EPC function METHOD and ARGS asynchronously."
(if (holo-layer-epc-live-p holo-layer-epc-process)
(holo-layer-deferred-chain
(holo-layer-epc-call-deferred holo-layer-epc-process (read method) args))
(setq holo-layer-first-call-method method)
(setq holo-layer-first-call-args args)
;;(holo-layer-start-process)
))
(defvar holo-layer-first-call-method nil)
(defvar holo-layer-first-call-args nil)
(defun holo-layer-restart-process ()
"Stop and restart Holo-Layer process."
(interactive)
(holo-layer-kill-process)
(holo-layer-start-process)
(message "[Holo-Layer] Process restarted."))
(defun holo-layer--build-process-environment ()
;; Turn on DEBUG info when `holo-layer-enable-debug' is non-nil.
(let ((environments (seq-filter
(lambda (var)
(and (not (string-match-p "QT_SCALE_FACTOR" var))
(not (string-match-p "QT_SCREEN_SCALE_FACTOR" var))))
process-environment)))
(when holo-layer-enable-debug
(add-to-list 'environments "QT_DEBUG_PLUGINS=1" t))
(unless (eq system-type 'darwin)
(add-to-list 'environments
(cond
((holo-layer-emacs-running-in-wayland-native)
;; Wayland native need to set QT_AUTO_SCREEN_SCALE_FACTOR=1
;; otherwise Qt window only have half of screen.
"QT_AUTO_SCREEN_SCALE_FACTOR=1")
(t
;; XWayland need to set QT_AUTO_SCREEN_SCALE_FACTOR=0
;; otherwise Qt which explicitly force high DPI enabling get scaled TWICE.
"QT_AUTO_SCREEN_SCALE_FACTOR=0"))
t)
(add-to-list 'environments "QT_FONT_DPI=96" t)
;; Make sure holo layer application scale support 4k screen.
(add-to-list 'environments "QT_SCALE_FACTOR=1" t)
;; Fix CORS problem.
(add-to-list 'environments "QTWEBENGINE_CHROMIUM_FLAGS=--disable-web-security" t)
;; Use XCB for input event transfer.
;; Only enable this option on Linux platform.
(when (and (eq system-type 'gnu/linux)
(not (holo-layer-emacs-running-in-wayland-native)))
(add-to-list 'environments "QT_QPA_PLATFORM=xcb" t)))
environments))
(defun holo-layer-start-process ()
"Start Holo-Layer process if it isn't started."
(if (holo-layer-epc-live-p holo-layer-epc-process)
(remove-hook 'post-command-hook #'holo-layer-start-process)
;; start epc server and set `holo-layer-server-port'
(holo-layer--start-epc-server)
(let* ((holo-layer-args (append
(list holo-layer-python-file)
(list (number-to-string holo-layer-server-port))
)))
;; Set process arguments.
(if holo-layer-enable-debug
(progn
(setq holo-layer-internal-process-prog "gdb")
(setq holo-layer-internal-process-args (append (list "-batch" "-ex" "run" "-ex" "bt" "--args" holo-layer-python-command) holo-layer-args)))
(setq holo-layer-internal-process-prog holo-layer-python-command)
(setq holo-layer-internal-process-args holo-layer-args))
;; Start python process.
(let ((process-connection-type t)
(process-environment (holo-layer--build-process-environment)))
(setq holo-layer-internal-process
(apply 'start-process
holo-layer-name holo-layer-name
holo-layer-internal-process-prog holo-layer-internal-process-args)))
(set-process-query-on-exit-flag holo-layer-internal-process nil))))
(defvar holo-layer-stop-process-hook nil)
(defun holo-layer-kill-process ()
"Stop Holo-Layer process and kill all Holo-Layer buffers."
(interactive)
;; Run stop process hooks.
(run-hooks 'holo-layer-stop-process-hook)
;; Kill process after kill buffer, make application can save session data.
(holo-layer--kill-python-process))
(add-hook 'kill-emacs-hook #'holo-layer-kill-process)
(defun holo-layer--kill-python-process ()
"Kill Holo-Layer background python process."
(when (holo-layer-epc-live-p holo-layer-epc-process)
;; Cleanup before exit Holo-Layer server process.
(holo-layer-call-async "cleanup")
;; Delete Holo-Layer server process.
(holo-layer-epc-stop-epc holo-layer-epc-process)
;; Kill *holo-layer* buffer.
(when (get-buffer holo-layer-name)
(kill-buffer holo-layer-name))
(setq holo-layer-epc-process nil)
(message "[Holo-Layer] Process terminated.")))
(defun holo-layer--first-start (holo-layer-epc-port)
"Call `holo-layer--open-internal' upon receiving `start_finish' signal from server."
(setq holo-layer-emacs-frame (window-frame))
;; Make EPC process.
(setq holo-layer-epc-process (make-holo-layer-epc-manager
:server-process holo-layer-internal-process
:commands (cons holo-layer-internal-process-prog holo-layer-internal-process-args)
:title (mapconcat 'identity (cons holo-layer-internal-process-prog holo-layer-internal-process-args) " ")
:port holo-layer-epc-port
:connection (holo-layer-epc-connect "127.0.0.1" holo-layer-epc-port)
))
(holo-layer-epc-init-epc-layer holo-layer-epc-process)
(when (and holo-layer-first-call-method
holo-layer-first-call-args)
(holo-layer-deferred-chain
(holo-layer-epc-call-deferred holo-layer-epc-process
(read holo-layer-first-call-method)
holo-layer-first-call-args)
(setq holo-layer-first-call-method nil)
(setq holo-layer-first-call-args nil)
)))
(defun holo-layer-emacs-running-in-wayland-native ()
(eq window-system 'pgtk))
(defun holo-layer--get-titlebar-height ()
"We need fetch height of window titlebar to adjust y coordinate of holo-layer when Emacs is not fullscreen."
(cond ((holo-layer-emacs-running-in-wayland-native)
(let ((is-fullscreen-p (memq (frame-parameter nil 'fullscreen) '(fullscreen fullboth))))
(if is-fullscreen-p
0
;; `32' is titlebar of Gnome3, we need change this value in other environment.
(cond ((string-equal (getenv "XDG_CURRENT_DESKTOP") "Hyprland")
0)
(t
32)))))
(t
0)))
(defvar holo-layer-build-dir (file-name-directory (locate-library "holo-layer")))
(defun holo-layer--get-frame-coordinate ()
"We need fetch Emacs coordinate to adjust coordinate of holo-layer if it running on system not support cross-process reparent technology.
Such as, wayland native, macOS etc."
(cond ((string-equal (getenv "XDG_CURRENT_DESKTOP") "sway")
(holo-layer--split-number (shell-command-to-string (concat holo-layer-build-dir "swaymsg-treefetch/swaymsg-rectfetcher.sh emacs"))))
((string-equal (getenv "XDG_CURRENT_DESKTOP") "Hyprland")
(let* ((frame-coordinate (json-parse-string (shell-command-to-string
(concat "hyprctl -j clients | jq '.[] | select(.pid == "
(number-to-string (emacs-pid))
") | .at'"))))
(monitor-coordinate (json-parse-string (shell-command-to-string
"hyprctl monitors -j | jq '.[] | select(.focused == true) | [.x,.y]'")))
(frame-x (- (aref frame-coordinate 0) (aref monitor-coordinate 0)))
(frame-y (- (aref frame-coordinate 1) (aref monitor-coordinate 1))))
(list frame-x frame-y)))
((holo-layer-emacs-running-in-wayland-native)
(require 'dbus)
(let* ((coordinate (holo-layer--split-number
(dbus-call-method :session "org.gnome.Shell" "/org/eaf/wayland" "org.eaf.wayland" "get_emacs_window_coordinate" :timeout 1000)
","))
;; HiDPI need except by `frame-scale-factor'.
(frame-x (truncate (/ (car coordinate) (frame-scale-factor))))
(frame-y (truncate (/ (cadr coordinate) (frame-scale-factor)))))
(list frame-x frame-y)))
(t
(list (car (frame-position)) (cdr (frame-position))))))
(defun holo-layer-get-window-allocation (&optional window)
"Get WINDOW allocation."
(let* ((window-edges (window-pixel-edges window))
(x (nth 0 window-edges))
(y (+ (nth 1 window-edges)
(if (version< emacs-version "27.0")
(window-header-line-height window)
(window-tab-line-height window))))
(w (- (nth 2 window-edges) x))
(h (- (nth 3 window-edges) (window-mode-line-height window) y)))
(list x y w h)))
(defun holo-layer--split-number (string)
(mapcar #'string-to-number (split-string string)))
(defun holo-layer--frame-left (frame)
"Return outer left position"
(let ((left (frame-parameter frame 'left)))
(if (listp left) (nth 1 left) left)))
(defun holo-layer--frame-top (frame)
"Return outer top position."
(let ((top (frame-parameter frame 'top)))
(if (listp top) (nth 1 top) top)))
(defun holo-layer--frame-internal-height (frame)
"Height of internal objects.
Including title-bar, menu-bar, offset depends on window system, and border."
(let ((geometry (frame-geometry frame)))
(+ (cdr (alist-get 'title-bar-size geometry))
(cdr (alist-get 'tool-bar-size geometry)))))
(defun holo-layer-is-normal-window-p (window)
(not (or (minibufferp (window-buffer window))
(and holo-layer--sort-tab-is-load-p
(string-equal (buffer-name (window-buffer window)) sort-tab-buffer-name)))))
(defun holo-layer-get-emacs-frame-info ()
(let ((pos (holo-layer--get-frame-coordinate))
(width (frame-pixel-width))
(height (frame-pixel-height))
(external-border-size (cdr (nth 2 (frame-geometry))))
(title-bar-size (or (cdr (nth (if holo-layer--w32-frame-p 3 4) (frame-geometry)))
(alist-get 'title-bar-size (frame-geometry))
(cons 0 0))))
(list (+ (car pos) (car external-border-size) (if (memq system-type '(cygwin windows-nt ms-dos)) 0 (car title-bar-size)))
(+ (cadr pos) (cdr external-border-size) (cdr title-bar-size))
width
height
(cl-position (frame-monitor-geometry) (display-monitor-attributes-list)
:test (lambda (f attr) (equal f (cdr (car attr))))))))
(defun holo-layer-eaf-fullscreen-p ()
(and (featurep 'eaf)
eaf-fullscreen-p
(equal (length (cl-remove-if #'window-dedicated-p (window-list frame))) 1)))
(defvar holo-layer-cache-emacs-frame-info nil)
(defvar holo-layer-cache-window-info nil)
(defvar holo-layer-last-cursor-info nil)
(defvar holo-layer-last-buffer-mode nil)
(defvar holo-layer-last-window nil)
(defvar holo-layer-last-fullsreen-info nil)
(defun holo-layer-is-insert-command-p ()
(eq last-command 'self-insert-command))
(defun holo-layer-monitor-fullscreen-state-change ()
;; macOS has fullscreen animation. So need delay 1s.
(run-with-timer
1 nil
(lambda ()
(unless (equal (memq (frame-parameter nil 'fullscreen) '(fullscreen fullboth))
(memq holo-layer-last-fullsreen-info '(fullscreen fullboth)))
(setq holo-layer-last-fullsreen-info (frame-parameter nil 'fullscreen))
(holo-layer-monitor-configuration-change)))))
(defun holo-layer-monitor-cursor-change ()
(when-let* ((cursor-info (ignore-errors (holo-layer-get-cursor-info)))
(changed (and cursor-info
(not (equal cursor-info holo-layer-last-cursor-info)))))
(if (and holo-layer-cache-emacs-frame-info holo-layer-cache-window-info)
(holo-layer-call-async "update_window_info"
holo-layer-cache-emacs-frame-info
holo-layer-cache-window-info
cursor-info
(holo-layer-get-menu-info)
(holo-layer-is-insert-command-p))
(holo-layer-monitor-configuration-change))
(setq holo-layer-last-cursor-info cursor-info)))
(defun holo-layer-indent-change (&rest _)
(holo-layer-call-async "update_indent_info" (holo-layer-get-indent-infos)))
(defun holo-layer-monitor-frame-changed (&rest _)
(when (and (holo-layer-epc-live-p holo-layer-epc-process)
(not (equal (window-frame) holo-layer-emacs-frame)))
(setq holo-layer-emacs-frame (window-frame))
))
(defun holo-layer-monitor-make-frame (frame)
(when (and (holo-layer-epc-live-p holo-layer-epc-process)
(not (equal frame holo-layer-emacs-frame)))
(setq holo-layer-emacs-frame frame)
))
(defun holo-layer-monitor-frame-change (_)
"Detecting frame moved and update window info"
(when (holo-layer-epc-live-p holo-layer-epc-process)
(ignore-errors
(let ((emacs-frame-info (holo-layer-get-emacs-frame-info)))
(holo-layer-call-async "update_window_info"
emacs-frame-info
""
""
(holo-layer-get-menu-info)
(holo-layer-is-insert-command-p))
(setq holo-layer-cache-emacs-frame-info emacs-frame-info)
))))
(defun holo-layer-monitor-configuration-change (&rest _)
"Detecting a window configuration change."
(when (and (holo-layer-epc-live-p holo-layer-epc-process)
;; When current frame is same with `emacs-frame'.
(equal (window-frame) holo-layer-emacs-frame))
(ignore-errors
(let ((emacs-frame-info (holo-layer-get-emacs-frame-info))
(current-window (selected-window))
view-infos)
(cond
;; Support holo-layer fullscreen.
((holo-layer-eaf-fullscreen-p)
(holo-layer-call-async "update_window_info"
emacs-frame-info
""
""
(holo-layer-get-menu-info)
(holo-layer-is-insert-command-p)))
;; Support blink-search.
((and holo-layer--blink-search-is-load-p
(equal (buffer-name (window-buffer current-window)) blink-search-input-buffer))
(let* ((top-window (get-buffer-window blink-search-start-buffer))
(top-window-info (holo-layer-get-window-info holo-layer-emacs-frame top-window current-window))
(top-window-info-list (split-string top-window-info ":")))
(push top-window-info view-infos)
(push (format "%s:%s:%s:%s:%s"
(nth 0 top-window-info-list)
(+ (string-to-number (nth 1 top-window-info-list))
(string-to-number (nth 3 top-window-info-list)))
(nth 2 top-window-info-list)
(+ (window-pixel-height (get-buffer-window blink-search-input-buffer))
(window-pixel-height (get-buffer-window blink-search-candidate-buffer)))
(equal current-window current-window))
view-infos)
(setq holo-layer-cache-window-info (mapconcat #'identity view-infos ","))
;; skip update cursor
(holo-layer-call-async "update_window_info"
emacs-frame-info
holo-layer-cache-window-info
""
(holo-layer-get-menu-info)
(holo-layer-is-insert-command-p))))
;; Normal window layout.
(t
(dolist (frame (frame-list))
(dolist (window (window-list frame))
(when (and (equal (window-frame window) holo-layer-emacs-frame)
(holo-layer-is-normal-window-p window))
(push (holo-layer-get-window-info frame window current-window) view-infos))))
(setq holo-layer-cache-window-info (mapconcat #'identity view-infos ","))
;; skip update cursor
(holo-layer-call-async "update_window_info"
emacs-frame-info
holo-layer-cache-window-info
""
(holo-layer-get-menu-info)
(holo-layer-is-insert-command-p))))
(setq holo-layer-cache-emacs-frame-info emacs-frame-info)
))))
(defun holo-layer-cursor-is-block-command-p ()
(member (format "%s" this-command) holo-layer-cursor-block-commands))
(defun holo-layer-get-rime-frame-info ()
(when (and (featurep 'rime)
(equal rime-show-candidate 'posframe))
(let ((buffer-list-update-hook nil))
(cl-dolist (frame (frame-list))
(let ((buffer-info (frame-parameter frame 'posframe-buffer)))
(when (or (equal rime-posframe-buffer (car buffer-info))
(equal rime-posframe-buffer (cdr buffer-info)))
(when (and (frame-live-p frame)
(frame-visible-p frame))
(cl-return
(format "%s:%s:%s:%s"
(car (frame-position frame))
(cdr (frame-position frame))
(frame-outer-width frame)
(frame-outer-height frame))))
))))))
(defun holo-layer-get-acm-frame-info ()
(when (and (frame-live-p acm-menu-frame)
(frame-visible-p acm-menu-frame))
(let* ((acm-menu-frame-pos (frame-position acm-menu-frame))
(acm-menu-frame-info (format "%s:%s:%s:%s"
(car acm-menu-frame-pos)
(cdr acm-menu-frame-pos)
(frame-outer-width acm-menu-frame)
(frame-outer-height acm-menu-frame)
)))
acm-menu-frame-info
)))
(defun holo-layer-get-acm-doc-frame-info ()
(when (and (frame-live-p acm-doc-frame)
(frame-visible-p acm-doc-frame))
(let* ((acm-doc-frame-pos (frame-position acm-doc-frame))
(acm-doc-frame-info (format "%s:%s:%s:%s"
(car acm-doc-frame-pos)
(cdr acm-doc-frame-pos)
(frame-outer-width acm-doc-frame)
(frame-outer-height acm-doc-frame)
)))
acm-doc-frame-info
)))
(defun holo-layer-get-minibuffer-info ()
(let* ((minibuffer-window (minibuffer-window))
(edges (window-pixel-edges minibuffer-window))
(x (nth 0 edges))
(y (nth 1 edges))
(width (- (nth 2 edges) x))
(height (- (nth 3 edges) y)))
(format "%s:%s:%s:%s" x y width height)))
(defun holo-layer-get-menu-info ()
(ignore-errors
(let* ((acm-frame-info (holo-layer-get-acm-frame-info))
(acm-doc-frame-info (holo-layer-get-acm-doc-frame-info))
(rime-frame-info (holo-layer-get-rime-frame-info))
(minibuffer-info (holo-layer-get-minibuffer-info))
(info (mapconcat 'identity (delq nil (list
acm-frame-info
acm-doc-frame-info
rime-frame-info
minibuffer-info
)) ","))
)
info
)))
(defun holo-layer-get-cursor-info ()
"Get the pixel position of the cursor in the current window."
(interactive)
(when-let* ((p (point)) (window (selected-window))
(cursor-pos
(or (pos-visible-in-window-p p window t)
;; make cursor visble
(and (redisplay)
(pos-visible-in-window-p p window t))))
(window-allocation (holo-layer-get-window-allocation window))
(window-margin (* (or (car (window-margins)) 0) (frame-char-width)))
;; Don't render cursor match below rules:
;; 1. Current buffer or previous buffer is EAF mode
;; 2. Current command match `holo-layer-cursor-block-commands'
(ok-rendeor-cursor
(and (not (equal major-mode 'eaf-mode))
(not (equal holo-layer-last-buffer-mode 'eaf-mode))
(not (holo-layer-cursor-is-block-command-p))))
(window-y (nth 1 window-allocation))
(window-h (nth 3 window-allocation))
(left-fringe-w (car (window-fringes))))
(when-let* ((overlays (overlays-at p))
(overlay
(catch 'after-cursor-overlay
(while overlays
(when (= p (overlay-start (car overlays)))
(throw 'after-cursor-overlay (car overlays)))
(setq overlays (cdr overlays))))))
(setq cursor-pos
`(,(- (nth 0 cursor-pos) (string-pixel-width (overlay-get overlay 'display)))
,(nth 1 cursor-pos))))
(setq holo-layer-last-buffer-mode major-mode)
(let ((x (+ (nth 0 cursor-pos) (nth 0 window-allocation)
window-margin left-fringe-w))
(y (+ (nth 1 cursor-pos) (nth 1 window-allocation)))
(w (if (equal cursor-type 'bar) 1
(if-let ((glyph (when (< p (point-max))
(aref (font-get-glyphs (font-at p) p (1+ p)) 0))))
(aref glyph 4)
(frame-char-width))))
(h (line-pixel-height)))
(when (and (> y (- (+ window-y window-h) h)) (equal holo-layer-last-window (selected-window)))
(setq y (- (+ window-y window-h) h)))
(setq holo-layer-last-window (selected-window))
(format "%s:%s:%s:%s:%s" x y w h (face-background 'cursor)))))
(defun holo-layer-get-window-info (frame window current-window)
(with-current-buffer (window-buffer window)
(let* ((window-allocation (holo-layer-get-window-allocation window))
(window-divider-right-padding (if window-divider-mode window-divider-default-right-width 0))
(window-divider-bottom-padding (if window-divider-mode window-divider-default-bottom-width 0))
(titlebar-height (holo-layer--get-titlebar-height))
(x (nth 0 window-allocation))
(y (nth 1 window-allocation))
(w (nth 2 window-allocation))
(h (nth 3 window-allocation)))
(format "%s:%s:%s:%s:%s"
x
(+ y titlebar-height )
(- w window-divider-right-padding)
(- h window-divider-bottom-padding)
(equal window current-window)))))
(defun holo-layer-show-place-info ()
(let ((word (if (derived-mode-p 'eaf-mode)
""
(if mark-active
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'word t)))))
(holo-layer-call-async "update_place_info" (if word word ""))))
(setq holo-layer-place-info-last-buffer nil)
(defun holo-layer-hide-place-info ()
"Only run when actually switching buffers"
(let ((current (current-buffer)))
(when (and holo-layer-place-info-last-buffer
(not (eq holo-layer-place-info-last-buffer current))
(not (minibufferp)))
(holo-layer-call-async "update_place_info" ""))
(setq holo-layer-place-info-last-buffer current)))
(add-hook 'window-configuration-change-hook 'holo-layer-hide-place-info)
(add-hook 'minibuffer-exit-hook 'holo-layer-hide-place-info)
(defun holo-layer-enable ()
(add-hook 'post-command-hook #'holo-layer-start-process)
(when holo-layer-enable-cursor-animation
(add-hook 'post-command-hook #'holo-layer-monitor-cursor-change))
(when (and holo-layer-sort-tab-ui
holo-layer--sort-tab-is-load-p)
(setq sort-tab-render-function 'holo-layer-render-sort-tab))
(add-hook 'post-command-hook #'holo-layer-show-place-info)
(when holo-layer-enable-indent-rainbow
(add-hook 'post-command-hook #'holo-layer-indent-change)
(add-hook 'window-configuration-change-hook #'holo-layer-indent-change)
(add-hook 'window-scroll-functions #'holo-layer-indent-change))
(add-hook 'window-size-change-functions #'holo-layer-monitor-configuration-change)
(add-hook 'window-configuration-change-hook #'holo-layer-monitor-configuration-change)
(add-hook 'buffer-list-update-hook #'holo-layer-monitor-configuration-change)
(when (eq system-type 'darwin)
(add-hook 'window-state-change-hook #'holo-layer-monitor-fullscreen-state-change))
(advice-add #'other-frame :after #'holo-layer-monitor-frame-changed)
(advice-add #'maximize-frame :after #'holo-layer-monitor-frame-changed)
(advice-add #'mouse-set-point :after #'holo-layer-monitor-frame-changed)
(add-hook 'move-frame-functions #'holo-layer-monitor-frame-change)
(add-hook 'delete-frame-functions #'holo-layer-monitor-frame-change)
(add-hook 'after-make-frame-functions #'holo-layer-monitor-make-frame)
(if holo-layer-hide-mode-line
(setq-default mode-line-format nil)))
(defun holo-layer-disable ()
(remove-hook 'post-command-hook #'holo-layer-start-process)
(when holo-layer-enable-cursor-animation
(remove-hook 'post-command-hook #'holo-layer-monitor-cursor-change))
(when (and holo-layer-sort-tab-ui
holo-layer--sort-tab-is-load-p)
(setq sort-tab-render-function 'sort-tab-render-tabs))
(remove-hook 'post-command-hook #'holo-layer-show-place-info)
(when holo-layer-enable-indent-rainbow
(remove-hook 'post-command-hook #'holo-layer-indent-change)
(remove-hook 'window-configuration-change-hook #'holo-layer-indent-change)
(remove-hook 'window-scroll-functions #'holo-layer-indent-change))
(remove-hook 'window-size-change-functions #'holo-layer-monitor-configuration-change)
(remove-hook 'window-configuration-change-hook #'holo-layer-monitor-configuration-change)
(remove-hook 'buffer-list-update-hook #'holo-layer-monitor-configuration-change)
(when (eq system-type 'darwin)
(remove-hook 'window-state-change-hook #'holo-layer-monitor-fullscreen-state-change))
(advice-remove #'other-frame #'holo-layer-monitor-frame-changed)
(advice-remove #'maximize-frame #'holo-layer-monitor-frame-changed)
(advice-remove #'mouse-set-point #'holo-layer-monitor-frame-changed)
(remove-hook 'move-frame-functions #'holo-layer-monitor-frame-change)
(remove-hook 'delete-frame-functions #'holo-layer-monitor-frame-change)
(remove-hook 'after-make-frame-functions #'holo-layer-monitor-make-frame)
;; hide holo layer
(holo-layer-call-async "update_window_info"
(holo-layer-get-emacs-frame-info)
""
""
(holo-layer-get-menu-info)
(holo-layer-is-insert-command-p)))
(defun holo-layer-compare-windows (w1 w2)
"Compare the positions of two windows. The upper bounds are compared first, and then the left bounds are compared if the upper bounds are the same."
(let ((edges1 (window-edges w1))
(edges2 (window-edges w2)))
(if (= (nth 1 edges1) (nth 1 edges2))
(< (nth 0 edges1) (nth 0 edges2))
(< (nth 1 edges1) (nth 1 edges2)))))
(defun holo-layer-jump-to-window ()
(interactive)
(let ((windows (sort (cl-remove-if #'window-dedicated-p (window-list)) 'holo-layer-compare-windows)))
(cond ((length> windows 2)
(holo-layer-call-async "show_window_number")
(ignore-errors
(let* ((prompt "Jump to window number: ")
(window-number
(if (length> windows 9)
(read-number prompt)
(string-to-number (string (read-char prompt))))))
(select-window (nth (- window-number 1) windows))))
(holo-layer-call-async "hide_window_number"))
((length= windows 2)
(other-window 1))
(t
(message "Only one window, don't need switch.")))))
(defun holo-layer-take-window-screenshot ()
(interactive)
(holo-layer-call-async "take_window_screenshot" (holo-layer-get-window-info holo-layer-emacs-frame (selected-window) (selected-window))))
(defun holo-layer--get-emacs-id ()
(if (eq system-type 'darwin)
(emacs-pid)
(string-to-number (frame-parameter nil 'outer-window-id))))
(defun holo-layer--get-emacs-name ()
(frame-parameter nil 'name))
(defun holo-layer-get-theme-mode ()
(format "%s" (frame-parameter nil 'background-mode)))
(defun holo-layer-get-theme-background-color ()
(format "%s" (frame-parameter nil 'background-color)))
(defun holo-layer-get-theme-foreground-color ()
(format "%s" (frame-parameter nil 'foreground-color)))
(defun holo-layer-get-buffer-mode (buf)
(let ((mode (with-current-buffer buf (format "%s" major-mode))))
(pcase mode
("eaf-mode" (format "eaf-%s" (with-current-buffer buf eaf--buffer-app-name)))
(_ mode))))
(defun holo-layer-render-sort-tab (visible-buffer-infos current-buffer)
(let* ((tab-names (mapcar #'buffer-name visible-buffer-infos))
(tab-modes (mapcar #'holo-layer-get-buffer-mode visible-buffer-infos))
(current-tab-name (buffer-name current-buffer))
(current-tab-index (or (cl-position current-buffer visible-buffer-infos)
0)))
(holo-layer-call-async "render_sort_tab"
tab-names
tab-modes
current-tab-index
current-tab-name
(window-pixel-height sort-tab-window)
sort-tab-name-max-length
holo-layer-cache-emacs-frame-info
(holo-layer-get-theme-mode)
(holo-layer-get-theme-foreground-color)
(holo-layer-get-theme-background-color)
)))
(defun holo-layer-get-indent-infos ()
(save-window-excursion
(save-excursion
(let (indent-infos window-start-cursor-info)
(when (and (boundp 'holo-layer-emacs-frame)
holo-layer-emacs-frame)
(dolist (window (window-list))
(with-selected-window window
(when (derived-mode-p 'prog-mode)
(let ((window-info (holo-layer-get-window-info holo-layer-emacs-frame (selected-window) (selected-window)))
current-line
current-line-indent-offset
indent-offsets)
;; Jump to window start.
(goto-char (window-start))
;; Using cursor info at first point to detect bias of x y
(setq window-start-cursor-info (holo-layer-get-cursor-info))
(catch 'stop
(while (and (not (= (line-number-at-pos (point))
(line-number-at-pos (window-end)))))
(setq current-line (line-number-at-pos (point)))
(setq current-line-indent-offset (holo-layer-get-line-indent-offset))
(forward-line)
(if (equal current-line (line-number-at-pos (point)))
(throw 'stop nil)
(setq indent-offsets (append indent-offsets (list current-line-indent-offset))))))
(setq indent-infos (append indent-infos
(list (format "%s_%s_%s"
window-info
window-start-cursor-info
(mapconcat #'number-to-string indent-offsets ","))))))))
))
indent-infos
))))
(defun holo-layer-get-line-indent-offset ()
(back-to-indentation)
(if (and (= (current-column) 0)
(= (point-at-eol) (point)))
-1
(current-column)))
(provide 'holo-layer)
;;; holo-layer.el ends here