-
Notifications
You must be signed in to change notification settings - Fork 233
/
eaf.el
2134 lines (1774 loc) · 82.4 KB
/
eaf.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
;;; eaf.el --- Emacs application framework -*- lexical-binding: t; -*-
;; Filename: eaf.el
;; Description: Emacs application framework
;; Author: Andy Stewart <lazycat.manatee@gmail.com>
;; Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
;; Copyright (C) 2018, Andy Stewart, all rights reserved.
;; Created: 2018-06-15 14:10:12
;; Version: 0.5
;; Last-Updated: Fri Apr 8 12:23:33 2022 (-0400)
;; By: Mingde (Matthew) Zeng
;; URL: https://github.com/emacs-eaf/emacs-application-framework
;; Keywords:
;; Compatibility: emacs-version >= 27
;;
;; 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:
;;
;; Emacs Application Framework
;;
;;; Installation:
;;
;; Please check README
;;
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET eaf RET
;;
;;; Change log:
;;
;; 2018/06/15
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Code:
(require 'cl-lib)
(require 'json)
(require 'map)
(require 'seq)
(require 'subr-x)
(require 'bookmark)
(declare-function straight--symlink-recursively "straight")
(define-obsolete-function-alias 'eaf-setq 'setq "Version 0.5, Commit d8abd23"
"See https://github.com/emacs-eaf/emacs-application-framework/issues/734.
for more information.
In summary, the EAF's python applications can now access users' customized
variables with the get_emacs_var module, eliminating the need to store
them in `eaf-var-list' and thus, `eaf-setq' is no longer necessary.")
(defvar eaf-app-binding-alist '()
"Mapping app names to keybinding variables.
Any new app should add the its name and the corresponding
keybinding variable to this list.")
(defvar eaf-app-module-path-alist '())
(defvar eaf-app-bookmark-handlers-alist '()
"Mapping app names to bookmark handler functions.
A bookmark handler function is used as
`bookmark-make-record-function' and should follow its spec.")
(defvar eaf-app-bookmark-restore-alist '())
(defvar eaf-preview-display-function-alist '()
"Preview display function.")
(defvar eaf-app-extensions-alist '()
"Mapping app names to extension list variables.
A new app can use this to configure extensions which should
handled by it.")
(defvar eaf-app-hook-alist '()
"Application running hook.")
(defvar eaf-build-dir (file-name-directory (locate-library "eaf")))
(defvar eaf-source-dir (file-name-directory (file-truename (concat eaf-build-dir "eaf.el"))))
;; Helper functions for generating the defcustom entry for the list of apps
(defun eaf--alist-to-defcustom-const (entry)
"Map an alist for an app to an entry for the defcustom set"
`(const :tag ,(cdr (assoc 'name entry)) ,(car entry)))
(defun eaf--json-to-defcustom-set ()
"Generate the 'set choice for the defcustom entry"
(let ((apps-alist
(with-temp-buffer
(insert-file-contents (concat eaf-build-dir "applications.json"))
(goto-char 0)
(json-parse-buffer :object-type 'alist))))
(cons 'set (mapcar 'eaf--alist-to-defcustom-const (cdr apps-alist)))))
(defcustom eaf-apps-to-install nil
"List of applications to install"
:group 'eaf
:type (eaf--json-to-defcustom-set))
(defun eaf-add-subdirs-to-load-path (search-dir)
(interactive)
(let* ((dir (file-name-as-directory search-dir)))
(dolist (subdir
(cl-remove-if
#'(lambda (subdir)
(or
(not (file-directory-p (concat dir subdir)))
(member subdir '("." ".."
"dist" "node_modules" "__pycache__"
"RCS" "CVS" "rcs" "cvs" ".git" ".github"))))
(directory-files dir)))
(let ((subdir-path (concat dir (file-name-as-directory subdir))))
(when (cl-some #'(lambda (subdir-file)
(and (file-regular-p (concat subdir-path subdir-file))
(member (file-name-extension subdir-file) '("el" "so" "dll"))))
(directory-files subdir-path))
(add-to-list 'load-path subdir-path t))
(eaf-add-subdirs-to-load-path subdir-path)))))
;; Add EAF app directories where .el exists to `load-path'.
(eaf-add-subdirs-to-load-path eaf-build-dir)
(require 'eaf-epc)
(defgroup eaf nil
"Emacs Application Framework."
:group 'applications)
(defcustom eaf-mode-hook '()
"EAF mode hook."
:type 'hook)
(defcustom eaf-mode-line-format mode-line-format
"`mode-line-format' used by eaf-mode.")
(defcustom eaf-frame-title-format frame-title-format
"`frame-title-format' used by eaf-mode.")
(defvar eaf-mode-map*
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-h m") #'eaf-describe-bindings)
(define-key map (kbd "C-o") #'eaf-duplicate-current-buffer)
(define-key map [remap describe-bindings] #'eaf-describe-bindings)
(define-key map (kbd "C-c b") #'eaf-open-bookmark)
(define-key map (kbd "C-c i") #'eaf-import-chrome-bookmarks)
(define-key map (kbd "C-c e") #'eaf-open-external)
(define-key map (kbd "C-h k") #'describe-key)
(define-key map (kbd "C-h v") #'describe-variable)
(define-key map (kbd "C-h f") #'describe-function)
(define-key map (kbd "C-h V") #'apropos-variable)
(define-key map (kbd "C-h F") #'apropos-function)
(define-key map (kbd "M-0") #'eaf-get-buffer-screenshot)
(define-key map (kbd "M-'") #'eaf-toggle-fullscreen)
(define-key map (kbd "M-/") #'eaf-get-path-or-url)
(define-key map (kbd "M-[") #'eaf-share-path-or-url)
(define-key map (vector 'remap #'keyboard-quit) #'eaf-keyboard-quit)
(define-key map (vector 'remap #'self-insert-command) #'eaf-send-key)
(dolist (single-key '("RET" "DEL" "TAB" "SPC" "<backtab>" "<home>" "<end>" "<left>" "<right>" "<up>" "<down>" "<prior>" "<next>" "<delete>" "<backspace>" "<return>"))
(define-key map (kbd single-key) #'eaf-send-key))
map)
"Keymap for default bindings available in all apps.")
(defvar eaf-mode-map nil
"Keymap used by `eaf-mode'.
Don't modify this map directly. To bind keys for all apps use
`eaf-mode-map*' and to bind keys for individual apps use
`eaf-bind-key'.")
(defvar eaf-server nil
"The EAF Server.")
(defvar eaf-edit-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-t") #'eaf-edit-buffer-switch-to-org-mode)
(define-key map (kbd "C-c C-k") #'eaf-edit-buffer-cancel)
(define-key map (kbd "C-c C-c") #'eaf-edit-buffer-confirm)
map))
(define-derived-mode eaf-edit-mode text-mode "EAF/edit"
"The major mode to edit focus text input.")
(defun eaf-describe-bindings ()
"Like `describe-bindings' for EAF buffers."
(interactive)
(let ((emulation-mode-map-alists nil)
(eaf-mode-map (current-local-map)))
(call-interactively 'describe-mode)))
(defvar-local eaf--buffer-id nil
"Internal id used by EAF app.")
(defvar-local eaf--buffer-url nil
"EAF buffer-local URL.")
(defvar-local eaf--buffer-app-name nil
"EAF buffer-local app-name.")
(defvar-local eaf--buffer-args nil
"EAF buffer-local args.")
(defvar-local eaf--buffer-map-alist nil
"EAF buffer-local map alist.")
(defvar-local eaf--buffer-map-alist-order 1
"Order of EAF buffer-local map alist in `emulation-mode-map-alists'.")
(define-derived-mode eaf-mode fundamental-mode "EAF"
"Major mode for Emacs Application Framework buffers.
This mode is used by all apps. The mode map `eaf-mode-map' is
created dynamically for each app and should not be changed
manually. See `eaf-bind-key' for customization of app bindings.
Within EAF buffers the variable `eaf--buffer-app-name' holds the
name of the current app. Each app can setup app hooks by using
`eaf-<app-name>-hook'. This hook runs after the app buffer has
been initialized."
(if (require 'holo-layer nil t)
;; Don't show mode-line if `holo-layer' is installed.
(setq-local mode-line-format nil)
;; Let eaf can set its mode-line and frame-title.
(setq-local mode-line-format eaf-mode-line-format))
(setq-local frame-title-format eaf-frame-title-format)
;; Split window combinations proportionally.
(setq-local window-combination-resize t)
;; Disable cursor in eaf buffer.
(setq-local cursor-type nil)
(set (make-local-variable 'eaf--buffer-id) (eaf--generate-id))
(setq-local bookmark-make-record-function #'eaf--bookmark-make-record)
;; Copy default value in case user already has bindings there
(setq-local emulation-mode-map-alists
(copy-alist (default-value 'emulation-mode-map-alists)))
;; Construct map alist
(setq-local eaf--buffer-map-alist (list (cons t eaf-mode-map)))
;; Eanble mode map and make it the first priority
(add-to-ordered-list
'emulation-mode-map-alists
'eaf--buffer-map-alist
'eaf--buffer-map-alist-order)
(add-hook 'kill-buffer-hook #'eaf--monitor-buffer-kill nil t)
(add-hook 'kill-emacs-hook #'eaf--monitor-emacs-kill))
(defvar eaf-python-file (expand-file-name "eaf.py" (file-name-directory load-file-name)))
(defvar eaf-server-port nil)
(defun eaf--start-epc-server ()
"Function to start the EPC server."
(unless eaf-server
(setq eaf-server
(eaf-epc-server-start
(lambda (mngr)
(let ((mngr mngr))
(eaf-epc-define-method mngr 'eval-in-emacs 'eaf--eval-in-emacs)
(eaf-epc-define-method mngr 'get-emacs-func-result 'eaf--get-emacs-func-result)
(eaf-epc-define-method mngr 'get-emacs-var 'eaf--get-emacs-var)
(eaf-epc-define-method mngr 'get-emacs-vars 'eaf--get-emacs-vars)
))))
(if eaf-server
(setq eaf-server-port (process-contact eaf-server :service))
(error "[EAF] eaf-server failed to start")))
eaf-server)
(defun eaf--get-emacs-func-result (sexp-string)
(eval (read sexp-string)))
(defun eaf--eval-in-emacs (sexp-string)
(eval (read sexp-string))
;; Return nil to avoid epc error `Got too many arguments in the reply'.
nil)
(defun eaf--get-emacs-var (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 eaf--get-emacs-vars (&rest vars)
(mapcar #'eaf--get-emacs-var vars))
(defun get-emacs-face-foregrounds (&rest faces)
(mapcar #'(lambda (face-name) (eaf-color-name-to-hex (face-attribute (intern face-name) :foreground nil 'default))) faces))
(defun eaf-color-int-to-hex (int)
(substring (format (concat "%0" (int-to-string 4) "X") int) (- 2)))
(defun eaf-color-name-to-hex (color)
(let ((components (x-color-values color)))
(concat "#"
(eaf-color-int-to-hex (nth 0 components))
(eaf-color-int-to-hex (nth 1 components))
(eaf-color-int-to-hex (nth 2 components)))))
(defvar eaf-epc-process nil)
(defvar eaf-internal-process nil)
(defvar eaf-internal-process-prog nil)
(defvar eaf-internal-process-args nil)
(defvar eaf--first-start-app-buffers nil
"Contains a list of '(buffer-url buffer-app-name buffer-args).")
(defvar eaf-last-frame-width 0)
(defvar eaf-last-frame-height 0)
(defcustom eaf-name "*eaf*"
"Name of EAF buffer."
:type 'string)
(defcustom eaf-python-command (if (memq system-type '(cygwin windows-nt ms-dos)) "python.exe" "python3")
"The Python interpreter used to run eaf.py."
:type 'string)
(defcustom eaf-config-location (expand-file-name (locate-user-emacs-file "eaf/"))
"Directory where eaf will store configuration files."
:type 'directory)
(defcustom eaf-marker-letters "ASDFHJKLWEOPCNM"
""
:type 'string)
(defcustom eaf-marker-quit-keys " "
""
:type 'string)
(defcustom eaf-marker-fontsize 11.5
"Fontsize (px) of marker."
:type 'number)
(defcustom eaf-buffer-background-color "#000000"
""
:type 'string)
(defcustom eaf-webengine-show-hover-link nil
""
:type 'boolean)
(defcustom eaf-find-file-ext-blacklist '("md" "org" "html" "htm" "epub")
"A blacklist of extensions to avoid when opening `find-file' file using EAF."
:type 'cons)
(defcustom eaf-proxy-host ""
"Proxy Host used by EAF Browser."
:type 'string)
(defcustom eaf-proxy-port ""
"Proxy Port used by EAF Browser."
:type 'string)
(defcustom eaf-proxy-type ""
"Proxy Type used by EAF Browser. The value is either \"http\" or \"socks5\"."
:type 'string)
(defcustom eaf-webengine-default-zoom 1.0
"Set the default zoom factor for EAF Browser."
:type 'float)
(defcustom eaf-webengine-zoom-step 0.1
"Set the zoom step factor for EAF Browser."
:type 'float)
(defcustom eaf-webengine-scroll-step 400
"Set the scroll step for EAF Browser, increase/decrease for bigger/smaller steps."
:type 'float)
(defcustom eaf-webengine-pc-user-agent "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"
"Simulate a PC User-Agent for EAF Browser."
:type 'string)
(defcustom eaf-webengine-phone-user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
"Simulate a Phone User-Agent for EAF Browser."
:type 'string)
(defcustom eaf-webengine-font-family ""
"Set font family for EAF Browser."
:type 'string)
(defcustom eaf-webengine-font-size 16
"Set font size for EAF Browser."
:type 'integer)
(defcustom eaf-webengine-fixed-font-family ""
"Set fixed font family for EAF Browser."
:type 'string)
(defcustom eaf-webengine-fixed-font-size 16
"Set fixed font size for EAF Browser."
:type 'integer)
(defcustom eaf-webengine-serif-font-family ""
"Set serif font family for EAF Browser."
:type 'string)
(defcustom eaf-webengine-enable-plugin t
"If non-nil, enable QtWebEngine plugins for EAF Browser."
:type 'boolean)
(defcustom eaf-webengine-enable-javascript t
"If non-nil, enable javascript for EAF Browser."
:type 'boolean)
(defcustom eaf-webengine-enable-javascript-access-clipboard t
"If non-nil, enable javascript access user clipboard for EAF Browser."
:type 'boolean)
(defcustom eaf-webengine-enable-scrollbar nil
"If non-nil, enable scroll bar for EAF Browser."
:type 'boolean)
(defcustom eaf-webengine-unknown-url-scheme-policy "AllowUnknownUrlSchemesFromUserInteraction"
"Allowed options: DisallowUnknownUrlSchemes, AllowUnknownUrlSchemesFromUserInteraction, or AllowAllUnknownUrlSchemes."
:type 'string)
(defcustom eaf-webengine-download-path "~/Downloads"
"Set the download path for EAF Browser."
:type 'string)
(defcustom eaf-enable-debug nil
"If you got segfault error, please turn this option.
Then EAF will start by gdb, please send new issue with `*eaf*' buffer content when next crash."
:type 'boolean)
(defcustom eaf-kill-process-after-last-buffer-closed nil
"Kill eaf process when last eaf buffer closed, default is nil.
If you don't want EAF process exist when all EAF buffer closed, turn on this option.
Turn off this option will improve EAF new page creation speed."
:type 'boolean)
(defcustom eaf-wm-name ""
"The desktop name, set by function `eaf--get-current-desktop-name'."
:type 'string)
(defcustom eaf-wm-focus-fix-wms
`(
"i3" ;i3
"LG3D" ;QTile
"Xpra" ;Windows WSL
"EXWM" ;EXWM
"Xfwm4" ;Xfce
"wlroots wm" ;hyprland(may work in other Wayland compositors based on wlroots)
)
"Set mouse cursor to frame bottom in these wms, to make EAF receive input event.
EAF confirms that the desktop environment or window manager you can work includes:
KDE, Gnome2, Gnome3, Mate, Xfce, LXDE, Sway, i3, QTile, Xpra, EXWM.
In which, KDE, Gnome2, Gnome3, Mate, Xfce, LXDE, Sway works well default,
i3, QTile, Xpra, EXWM need move cursor to corner to get focus of Window manager.
If your window manager can't receive input event, you can try add `NAME' of command `wmctrl -m' to this list.
Please send PR if it works.
Please fill an issue if it still doesn't work."
:type 'list)
(defcustom eaf-start-python-process-when-require t
"Start EAF python process when require `eaf', default is turn on.
Turn on this option will improve start speed."
:type 'boolean)
(defcustom eaf-byte-compile-apps nil
"If this option turn on, EAF will byte-compile elisp code.")
(defcustom eaf-clean-duplicate-buffers t
"Clean duplicate file manager buffers."
:type 'boolean)
(defcustom eaf-goto-right-after-close-buffer nil
"EAF will switch to right buffer after close current EAF buffer if this option is enable.
And you need re-implement `eaf-goto-right-tab' self."
:type 'boolean)
(defcustom eaf-duplicate-buffer-survival-time 60
"If file manager buffer not show in current frame, and existence time exceeds than 60 seconds,
EAF will remove the duplicate file manager buffer."
:type 'integer)
(defcustom eaf-rebuild-buffer-after-crash t
"Rebuild EAF buffer after it crash.
Turning on this option can quickly rebuild the buffer very conveniently, avoiding the current buffer crash and affecting other buffers.
Only turn off this option when you want investigate the cause of the crash."
:type 'boolean)
(defcustom eaf-dired-advisor-enable t
"Toggle to enable/disable the EAF advisor in dired.
When enabled, EAF will use eaf-file-manager to open file from dired."
:type 'boolean)
(defvar eaf--monitor-configuration-p t
"When this variable is non-nil, `eaf-monitor-configuration-change' executes.
This variable is used to open buffer in backend and avoid graphics blink.
EAF call python method `new_buffer' to create EAF application buffer.
EAF call python method `update_views' to create EAF application view.
Python process only create application view when Emacs window or buffer state change.")
(defvar eaf-fullscreen-p nil
"When non-nil, EAF will intelligently hide modeline as necessray.")
(defvar eaf-buffer-title-format "%s")
(defvar-local eaf--bookmark-title nil)
(defmacro eaf-for-each-eaf-buffer (&rest body)
"A syntactic sugar to loop through each EAF buffer and evaluat BODY.
Within BODY, `buffer' can be used to"
`(dolist (buffer (eaf--get-eaf-buffers))
(with-current-buffer buffer
,@body)))
(defun eaf--bookmark-make-record ()
"Create a EAF bookmark.
The bookmark will try to recreate EAF buffer session.
For now only EAF browser app is supported."
(let ((handler (cdr
(assoc eaf--buffer-app-name
eaf-app-bookmark-handlers-alist))))
(when handler
(funcall handler))))
(defun eaf--bookmark-restore (bookmark)
"Restore EAF buffer according to BOOKMARK."
(let ((app (cdr (assq 'eaf-app bookmark))))
(funcall (cdr (assoc app eaf-app-bookmark-restore-alist)) bookmark)))
;;;###autoload
(defun eaf-open-bookmark ()
"Command to open or create EAF bookmarks with completion."
(interactive)
(bookmark-maybe-load-default-file)
(let* ((bookmarks (cl-remove-if-not
(lambda (entry)
(bookmark-prop-get entry 'eaf-app))
bookmark-alist))
(names (mapcar #'car bookmarks))
(cand (completing-read "EAF Bookmarks: " bookmarks)))
(cond ((member cand names)
(bookmark-jump cand))
(t
(unless (derived-mode-p 'eaf-mode)
(message "This command can only be called in an EAF buffer!"))
;; create new one for current buffer with provided name
(bookmark-set cand)))))
(defvar eaf--existing-bookmarks nil
"Existing bookmarks in Emacs.
A hashtable, key is url and value is title.")
(defun eaf--load-existing-bookmarks()
"Load existing bookmarks."
(let ((bookmarks (make-hash-table :test 'equal)))
(dolist (bm bookmark-alist)
(let* ((name (car bm))
(file (bookmark-get-filename name)))
(puthash file name bookmarks)))
bookmarks))
(defun eaf-open-external ()
"Command to open current path or url with external application."
(interactive)
(let ((path-or-url (eaf-get-path-or-url)))
(cond ((memq system-type '(cygwin windows-nt ms-dos))
(w32-shell-execute "open" path-or-url))
((eq system-type 'darwin)
(shell-command (concat "open " (shell-quote-argument path-or-url))))
((eq system-type 'gnu/linux)
(let ((process-connection-type nil))
(start-process "" nil "xdg-open" path-or-url))))))
(defun eaf-call-async (method &rest args)
"Call Python EPC function METHOD and ARGS asynchronously."
(eaf-deferred-chain
(eaf-epc-call-deferred eaf-epc-process (read method) args)))
(defun eaf-call-sync (method &rest args)
"Call Python EPC function METHOD and ARGS synchronously."
(eaf-epc-call-sync eaf-epc-process (read method) args))
(defun eaf--called-from-wsl-on-windows-p ()
"Check whether eaf is called by Emacs on WSL and is running on Windows."
(and (eq system-type 'gnu/linux)
(string-match-p ".exe" eaf-python-command)))
(defun eaf-get-emacs-xid (frame)
"Get Emacs FRAME xid."
(if (eaf--called-from-wsl-on-windows-p)
(eaf-call-sync "get_emacs_wsl_window_id")
(frame-parameter frame 'window-id)))
(defun eaf--build-process-environment ()
;; Turn on DEBUG info when `eaf-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 eaf-enable-debug
(add-to-list 'environments "QT_DEBUG_PLUGINS=1" t))
(unless (eq system-type 'darwin)
(add-to-list 'environments
(cond
((eaf-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 EAF 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 (eaf-emacs-running-in-wayland-native)))
(add-to-list 'environments "QT_QPA_PLATFORM=xcb" t)))
environments))
(defvar eaf-start-process-hook nil)
(defun eaf-start-process ()
"Start EAF process if it isn't started."
(unless (eaf-epc-live-p eaf-epc-process)
;; start epc server and set `eaf-server-port'
(eaf--start-epc-server)
(let* ((eaf-args (append
(list eaf-python-file)
(eaf-get-render-size)
(list (number-to-string eaf-server-port))
))
environments)
;; Folow system DPI.
(setq environments (eaf--build-process-environment))
;; Set process arguments.
(if eaf-enable-debug
(progn
(setq eaf-internal-process-prog "gdb")
(setq eaf-internal-process-args (append (list "-batch" "-ex" "run" "-ex" "bt" "--args" eaf-python-command) eaf-args)))
(setq eaf-internal-process-prog eaf-python-command)
(setq eaf-internal-process-args eaf-args))
;; Start python process.
(let ((process-connection-type (not (eaf--called-from-wsl-on-windows-p)))
(process-environment environments))
(setq eaf-internal-process (apply 'start-process eaf-name eaf-name eaf-internal-process-prog eaf-internal-process-args)))
(set-process-query-on-exit-flag eaf-internal-process nil)))
;; Run start process hooks.
(run-hooks 'eaf-start-process-hook))
(run-with-idle-timer
1 nil
#'(lambda ()
;; Start EAF python process when load `eaf'.
;; It will improve start speed.
(when eaf-start-python-process-when-require
(eaf-start-process))))
(defvar eaf-stop-process-hook nil)
(defun eaf-kill-process (&optional restart)
"Stop EAF process and kill all EAF buffers.
If RESTART is non-nil, cached URL and app-name will not be cleared."
(interactive)
;; Run stop process hooks.
(run-hooks 'eaf-stop-process-hook)
(unless restart
;; Clear active buffers
(setq eaf--first-start-app-buffers nil)
;; Remove all EAF related hooks since the EAF process is stopped.
(remove-hook 'kill-buffer-hook #'eaf--monitor-buffer-kill)
(remove-hook 'kill-emacs-hook #'eaf--monitor-emacs-kill)
(remove-hook 'after-save-hook #'eaf--org-preview-monitor-buffer-save)
(remove-hook 'kill-buffer-hook #'eaf--org-preview-monitor-kill)
(remove-hook 'window-size-change-functions #'eaf-monitor-window-size-change)
(remove-hook 'window-configuration-change-hook #'eaf-monitor-configuration-change))
;; Set `eaf-fullscreen-p'.
(setq-local eaf-fullscreen-p nil)
;; Kill EAF-mode buffers.
(let* ((eaf-buffers (eaf--get-eaf-buffers))
(count (length eaf-buffers)))
(dolist (buffer eaf-buffers)
(kill-buffer buffer))
;; Just report to me when EAF buffer exists.
(message "[EAF] Killed %s EAF buffer%s" count (if (> count 1) "s!" "!")))
;; Kill process after kill buffer, make application can save session data.
(eaf--kill-python-process))
(defalias 'eaf-stop-process #'eaf-kill-process)
(defun eaf--kill-python-process ()
"Kill EAF background python process."
(interactive)
(when (eaf-epc-live-p eaf-epc-process)
;; Cleanup before exit EAF server process.
(eaf-call-async "cleanup")
;; Delete EAF server process.
(eaf-epc-stop-epc eaf-epc-process)
;; Kill *eaf* buffer.
(when (get-buffer eaf-name)
(kill-buffer eaf-name))
(message "[EAF] Process terminated.")))
(defun eaf--kill-devtools-buffers ()
(dolist (buffer (eaf--get-eaf-buffers))
(with-current-buffer buffer
(when (string-prefix-p "DevTools - " (buffer-name buffer))
(kill-buffer buffer)))))
(defun eaf-restart-process ()
"Stop and restart EAF process."
(interactive)
;; We need kill debug page first.
(eaf--kill-devtools-buffers)
;; Record reset buffers to `eaf--first-start-app-buffers'.
(setq eaf--first-start-app-buffers nil)
(eaf-for-each-eaf-buffer
(push `(,eaf--buffer-url ,eaf--buffer-app-name ,eaf--buffer-args) eaf--first-start-app-buffers))
;; Stop EAF process.
(eaf-kill-process t)
;; Start EAF process, EAF will restore page in `eaf--first-start-app-buffers'.
(eaf-start-process))
(defun eaf--encode-string (str)
"Encode string STR with UTF-8 coding using Base64."
(base64-encode-string (encode-coding-string str 'utf-8)))
(defun eaf-get-render-size ()
"Get allocation for render application in backend.
We need calcuate render allocation to make sure no black border around render content."
(let* (;; We use `window-inside-pixel-edges' and `window-absolute-pixel-edges' calcuate height of window header, such as tabbar.
(window-header-height (- (nth 1 (window-inside-pixel-edges)) (nth 1 (window-absolute-pixel-edges))))
(width (frame-pixel-width))
;; Render height should minus mode-line height, minibuffer height, header height.
(height (- (frame-pixel-height) (window-mode-line-height) (window-pixel-height (minibuffer-window)) window-header-height)))
(mapcar (lambda (x) (format "%s" x)) (list width height))))
(defun eaf-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 eaf--generate-id ()
"Randomly generate a seven digit id used for EAF buffers."
(format "%04x-%04x-%04x-%04x-%04x-%04x-%04x"
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))
(random (expt 16 4))))
(defun eaf-execute-app-cmd (cmd &optional buf)
"Execute app CMD.
If BUF is given it should be the EAF buffer for the command
otherwise it is assumed that the current buffer is the EAF
buffer."
(with-current-buffer (or buf (current-buffer))
(let ((this-command cmd))
(call-interactively cmd))))
(defun eaf-copy-to-clipboard (string)
(if (version< "29.0" emacs-version)
(progn
(kill-new string)
string)
(kill-new string)))
(defun eaf-get-path-or-url ()
"Get the current file path or web URL.
When called interactively, copy to ‘kill-ring’."
(interactive)
(if (derived-mode-p 'eaf-mode)
(if (called-interactively-p 'any)
(message "%s" (eaf-copy-to-clipboard (eaf-call-sync "execute_function" eaf--buffer-id "get_url")))
(eaf-call-sync "execute_function" eaf--buffer-id "get_url"))
(user-error "This command can only be called in an EAF buffer!")))
(defun eaf-toggle-fullscreen ()
"Toggle fullscreen."
(interactive)
(eaf-call-async "eval_function" eaf--buffer-id "toggle_fullscreen" (key-description (this-command-keys-vector))))
(defun eaf--enter-fullscreen-request ()
"Entering fullscreen use Emacs frame's size."
(message "[EAF] Enter fullscreen")
(setq-local eaf-fullscreen-p t)
(eaf-monitor-configuration-change)
(when (and eaf-browser-fullscreen-move-cursor-corner
(string= eaf--buffer-app-name "browser"))
(eaf-call-async "eval_function" eaf--buffer-id "move_cursor_to_corner" (key-description (this-command-keys-vector)))))
(defun eaf--exit_fullscreen_request ()
"Exit fullscreen."
(message "[EAF] Exit fullscreen")
(setq-local eaf-fullscreen-p nil)
(eaf-monitor-configuration-change))
(defun eaf--make-py-proxy-function (fun)
"Define elisp command which can call Python function string FUN."
(let ((sym (intern (format "eaf-py-proxy-%s" fun))))
(unless (fboundp sym)
(defalias sym
(lambda nil
(interactive)
;; Ensure this is only called from EAF buffer
(if (derived-mode-p 'eaf-mode)
(eaf-call-async "eval_function" eaf--buffer-id fun (key-description (this-command-keys-vector)))
(message "%s command can only be called in an EAF buffer!" sym)))
(format
"Proxy function to call \"%s\" on the Python side.
Use `eaf-execute-app-cmd' if you want to execute this command programmatically.
Please ONLY use `eaf-bind-key' and use the unprefixed command name (\"%s\")
to edit EAF keybindings!" fun fun)))
sym))
(defun eaf--make-js-proxy-function (fun &optional args)
"Define elisp command which can call JavaScript function string FUN."
(let ((sym (intern (format "eaf-js-proxy-%s" fun))))
(unless (fboundp sym)
(defalias sym
(lambda nil
(interactive)
(unless args
(setq args ""))
;; Ensure this is only called from EAF buffer
(when (derived-mode-p 'eaf-mode)
(eaf-call-async "eval_js_function" eaf--buffer-id (string-trim-left fun "js_") args)
))
(format
"Proxy function to call \"%s\" on the JavaScript side.
Please ONLY use `eaf-bind-key' and use the unprefixed command name (\"%s\")
to edit EAF keybindings!" fun fun)))
sym))
(defun eaf--gen-keybinding-map (keybinding &optional no-inherit-eaf-mode-map*)
"Configure the `eaf-mode-map' from KEYBINDING, one of the eaf-.*-keybinding variables."
(setq eaf-mode-map
(let ((map (make-sparse-keymap)))
(unless no-inherit-eaf-mode-map*
(set-keymap-parent map eaf-mode-map*))
(cl-loop for (key . fun) in (reverse keybinding)
do (define-key map (kbd key)
(cond
;; If command is normal symbol, just call it directly.
((symbolp fun)
fun)
;; If command is string and include - , it's elisp function, use `intern' build elisp function from function name.
((string-match "-" fun)
(intern fun))
;; If command prefix with js_, call JavaScript function directly.
((string-prefix-p "js_" fun)
(eaf--make-js-proxy-function fun))
;; If command is not built-in function and not include char '-'
;; it's command in python side, build elisp proxy function to call it.
(t
(eaf--make-py-proxy-function fun))
))
finally return map))))
(defun eaf--get-app-bindings (app-name)
"Get the specified APP-NAME keybinding.
Every app has its name and the corresponding
keybinding variable to eaf-app-binding-alist."
(symbol-value
(cdr (assoc app-name eaf-app-binding-alist))))
(defun eaf--get-app-module-path (app-name)
(symbol-value
(cdr (assoc app-name eaf-app-module-path-alist))))
(defun eaf--get-app-hook (app-name)
(funcall
(cdr (assoc app-name eaf-app-hook-alist))))
(defun eaf--create-buffer (url app-name args)
"Create an EAF buffer given URL, APP-NAME, and ARGS."
(eaf--gen-keybinding-map (eaf--get-app-bindings app-name))
(let* ((eaf-buffer-name (if (equal (file-name-nondirectory url) "")
url
(file-name-nondirectory url)))
(eaf-buffer (generate-new-buffer eaf-buffer-name))
(url-directory (or (file-name-directory url) url)))
(with-current-buffer eaf-buffer
(eaf-mode)
;; Don't promt user when exist EAF python process.
(setq-local confirm-kill-processes nil)
(when (file-accessible-directory-p url-directory)
(setq-local default-directory url-directory)
;; NOTE:
;; Don't set buffer `buffer-file-name' here.
;; Otherwise, markdown or org previewer will switch to EAF preview buffer instead open real file.
)
;; `eaf-buffer-url' should record full path of url, otherwise `eaf-open' will open duplicate PDF tab for same url.
(set (make-local-variable 'eaf--buffer-url) url)
(set (make-local-variable 'eaf--buffer-app-name) app-name)
(set (make-local-variable 'eaf--buffer-args) args)
(run-hooks (intern (format "eaf-%s-hook" app-name)))