forked from emacs-eaf/emacs-application-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eaf.el
2158 lines (1875 loc) · 79.5 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: Tue Jun 30 21:32:00 2020 (-0400)
;; By: Mingde (Matthew) Zeng
;; URL: http://www.emacswiki.org/emacs/download/eaf.el
;; Keywords:
;; Compatibility: GNU Emacs 27.0.50
;;
;; 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
;;
;;
;;
;;; Require
(defun add-subdirs-to-load-path (dir)
"Recursive add directory DIR to `load-path'."
(mapcar
(lambda (path) (add-to-list 'load-path path))
(delete-dups (mapcar 'file-name-directory (directory-files-recursively dir "\.el$")))))
(add-subdirs-to-load-path (expand-file-name "app" (file-name-directory (locate-library "eaf"))))
(require 'dbus)
(require 'subr-x)
(require 'map)
(require 'bookmark)
(require 'seq)
(require 'eaf-mindmap)
(require 'eaf-interleave)
(require 'json)
;;; Code:
;; Remove the relevant environment variables from the process-environment to disable QT scaling,
;; let EAF qt program follow the system scale.
(setq process-environment (seq-filter
(lambda(var)
(and (not (string-match-p "QT_SCALE_FACTOR" var))
(not (string-match-p "QT_SCREEN_SCALE_FACTOR" var)))) process-environment))
(defgroup eaf nil
"Emacs Application Framework."
:group 'applications)
(defcustom eaf-mode-hook '()
"EAF mode hook."
:type 'hook)
(defvar eaf-mode-map*
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-h m") #'eaf-describe-bindings)
(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 e") #'eaf-open-external)
(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-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.")
(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."
;; Split window combinations proportionally.
(setq-local window-combination-resize t)
(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 (default-value 'emulation-mode-map-alists))
;; Disable cursor in eaf buffer.
(setq-local cursor-type nil)
(push (list (cons t eaf-mode-map))
emulation-mode-map-alists)
(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-process nil)
(defvar eaf--active-buffers nil
"Contains a list of '(buffer-url buffer-app-name buffer-args).")
(defvar eaf--webengine-include-private-codec nil)
(defvar eaf-org-file-list '())
(defvar eaf-org-killed-file-list '())
(defvar eaf-last-frame-width 0)
(defvar eaf-last-frame-height 0)
(defvar eaf-grip-token nil)
(defvar eaf-find-alternate-file-in-dired nil
"If non-nil, calling `eaf-open-this-from-dired' determines file types to open.
EAF unrecognizable files will be opened by `dired-find-alternate-file' normally.
Otherwise they will be opened normally with `dired-find-file'.")
(defcustom eaf-browser-search-engines `(("google" . "http://www.google.com/search?ie=utf-8&oe=utf-8&q=%s")
("duckduckgo" . "https://duckduckgo.com/?q=%s"))
"The default search engines offered by EAF.
Each element has the form (NAME . URL).
NAME is a search engine name, as a string.
URL pecifies the url format for the search engine.
It should have a %s as placeholder for search string."
:type '(alist :key-type (string :tag "Search engine name")
:value-type (string :tag "Search engine url")))
(defcustom eaf-browser-default-search-engine "google"
"The default search engine used by `eaf-open-browser' and `eaf-search-it'.
It must defined at `eaf-browser-search-engines'."
:type 'string)
(defcustom eaf-name "*eaf*"
"Name of EAF buffer."
:type 'string)
(defcustom eaf-python-command "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-var-list
'((eaf-camera-save-path . "~/Downloads")
(eaf-browser-enable-plugin . "true")
(eaf-browser-enable-javascript . "true")
(eaf-browser-remember-history . "true")
(eaf-browser-default-zoom . "1.0")
(eaf-browser-blank-page-url . "https://www.google.com")
(eaf-browser-scroll-behavior . "auto")
(eaf-browser-download-path . "~/Downloads")
(eaf-browser-aria2-proxy-host . "")
(eaf-browser-aria2-proxy-port . "")
(eaf-browser-dark-mode . "")
(eaf-pdf-dark-mode . "")
(eaf-terminal-dark-mode . "")
(eaf-terminal-font-size . "13")
(eaf-mindmap-dark-mode . "")
(eaf-mindmap-save-path . "~/Documents")
(eaf-marker-letters . "ASDFHJKLWEOPCNM")
(eaf-emacs-theme-mode . ""))
"The alist storing user-defined variables that's shared with EAF Python side.
Try not to modify this alist directly. Use `eaf-setq' to modify instead."
:type 'cons)
(defcustom eaf-browser-keybinding
'(("C--" . "zoom_out")
("C-=" . "zoom_in")
("C-0" . "zoom_reset")
("C-s" . "search_text_forward")
("C-r" . "search_text_backward")
("C-n" . "scroll_up")
("C-p" . "scroll_down")
("C-f" . "scroll_right")
("C-b" . "scroll_left")
("C-v" . "scroll_up_page")
("C-y" . "yank_text")
("C-w" . "kill_text")
("M-e" . "edit_focus_text")
("M-s" . "open_link")
("M-S" . "open_link_new_buffer")
("M-d" . "open_link_background_buffer")
("C-/" . "undo_action")
("M-_" . "redo_action")
("M-w" . "copy_text")
("M-f" . "history_forward")
("M-b" . "history_backward")
("M-q" . "clear_cookies")
("C-M-q" . "clear_history")
("M-v" . "scroll_down_page")
("M-<" . "scroll_to_begin")
("M->" . "scroll_to_bottom")
("M-t" . "new_blank_page")
("SPC" . "insert_or_scroll_up_page")
("J" . "insert_or_select_left_tab")
("K" . "insert_or_select_right_tab")
("j" . "insert_or_scroll_up")
("k" . "insert_or_scroll_down")
("h" . "insert_or_scroll_left")
("l" . "insert_or_scroll_right")
("f" . "insert_or_open_link")
("F" . "insert_or_open_link_new_buffer")
("D" . "insert_or_open_link_background_buffer")
("c" . "insert_or_copy_link")
("C" . "insert_or_copy_code")
("d" . "insert_or_scroll_up_page")
("u" . "insert_or_scroll_down_page")
("H" . "insert_or_history_backward")
("L" . "insert_or_history_forward")
("t" . "insert_or_new_blank_page")
("T" . "insert_or_recover_prev_close_page")
("i" . "insert_or_focus_input")
("I" . "insert_or_open_download_manage_page")
("r" . "insert_or_refresh_page")
("g" . "insert_or_scroll_to_begin")
("x" . "insert_or_close_buffer")
("G" . "insert_or_scroll_to_bottom")
("-" . "insert_or_zoom_out")
("=" . "insert_or_zoom_in")
("0" . "insert_or_zoom_reset")
("m" . "insert_or_save_as_bookmark")
("o" . "insert_or_open_browser")
("y" . "insert_or_download_youtube_video")
("Y" . "insert_or_download_youtube_audio")
("p" . "insert_or_toggle_device")
("s" . "insert_or_save_as_pdf")
("S" . "insert_or_save_as_single_file")
("v" . "insert_or_view_source")
("e" . "insert_or_edit_url")
("C-a" . "select_all_or_input_text")
("M-u" . "clear_focus")
("M-i" . "open_download_manage_page")
("M-o" . "eval_js")
("M-O" . "eval_js_file")
("M-g" . "exit_fullscreen")
("M-," . "eaf-send-down-key")
("M-." . "eaf-send-up-key")
("M-m" . "eaf-send-return-key")
("<f5>" . "refresh_page")
("<f12>" . "open_dev_tool_page")
("<C-return>" . "eaf-send-ctrl-return-sequence")
)
"The keybinding of EAF Browser."
:type 'cons)
(defcustom eaf-browser-key-alias
'(("C-a" . "<home>")
("C-e" . "<end>"))
"The key alias of EAF Browser."
:type 'cons)
(defcustom eaf-pdf-viewer-keybinding
'(("j" . "scroll_up")
("<down>" . "scroll_up")
("C-n" . "scroll_up")
("k" . "scroll_down")
("<up>" . "scroll_down")
("C-p" . "scroll_down")
("h" . "scroll_left")
("<left>" . "scroll_left")
("C-b" . "scroll_left")
("l" . "scroll_right")
("<right>" . "scroll_right")
("C-f" . "scroll_right")
("SPC" . "scroll_up_page")
("b" . "scroll_down_page")
("C-v" . "scroll_up_page")
("M-v" . "scroll_down_page")
("t" . "toggle_read_mode")
("0" . "zoom_reset")
("=" . "zoom_in")
("-" . "zoom_out")
("g" . "scroll_to_begin")
("G" . "scroll_to_end")
("p" . "jump_to_page")
("P" . "jump_to_percent")
("[" . "save_current_pos")
("]" . "jump_to_saved_pos")
("i" . "toggle_inverted_mode")
("m" . "toggle_mark_link")
("f" . "jump_to_link")
("M-w" . "copy_select")
("C-s" . "search_text_forward")
("C-r" . "search_text_backward")
("x" . "close_buffer")
("M-h" . "add_annot_highlight")
("M-u" . "add_annot_underline")
("M-s" . "add_annot_squiggly")
("M-d" . "add_annot_strikeout_or_delete_annot")
("M-e" . "add_annot_text_or_edit_annot")
("J" . "select_left_tab")
("K" . "select_right_tab")
("o" . "eaf-pdf-outline"))
"The keybinding of EAF PDF Viewer."
:type 'cons)
(defcustom eaf-video-player-keybinding
'(("SPC" . "toggle_play")
("x" . "close_buffer")
("h" . "play_backward")
("l" . "play_forward"))
"The keybinding of EAF Video Player."
:type 'cons)
(defcustom eaf-js-video-player-keybinding
'(("SPC" . "toggle_play")
("M-g" . "exit_fullscreen")
("<f12>" . "open_dev_tool_page")
("h" . "backward")
("l" . "forward")
("r" . "restart")
("j" . "decrease_volume")
("k" . "increase_volume")
("x" . "close_buffer")
("c--" . "zoom_out")
("C-=" . "zoom_in")
("C-0" . "zoom_reset")
)
"The keybinding of EAF JS Video Player."
:type 'cons)
(defcustom eaf-image-viewer-keybinding
'(("n" . "load_next_image")
("p" . "load_prev_image")
("SPC" . "load_prev_image")
("-" . "zoom_out")
("=" . "zoom_in")
("0" . "zoom_reset")
("x" . "close_buffer")
("j" . "scroll_up")
("k" . "scroll_down")
("h" . "scroll_left")
("l" . "scroll_right")
("," . "scroll_up_page")
("." . "scroll_down_page")
("<" . "scroll_to_begin")
(">" . "scroll_to_bottom"))
"The keybinding of EAF Image Viewer."
:type 'cons)
(defcustom eaf-terminal-keybinding
'(("M-n" . "scroll_up")
("M-p" . "scroll_down")
("C-v" . "scroll_up_page")
("M-v" . "scroll_down_page")
("M-<" . "scroll_to_begin")
("M->" . "scroll_to_bottom")
("C--" . "zoom_out")
("C-=" . "zoom_in")
("C-0" . "zoom_reset")
("C-S-c" . "copy_text")
("C-S-v" . "yank_text")
("C-a" . "eaf-send-key-sequence")
("C-e" . "eaf-send-key-sequence")
("C-f" . "eaf-send-key-sequence")
("C-b" . "eaf-send-key-sequence")
("C-d" . "eaf-send-key-sequence")
("C-n" . "eaf-send-key-sequence")
("C-p" . "eaf-send-key-sequence")
("C-r" . "eaf-send-key-sequence")
("C-y" . "eaf-send-key-sequence")
("C-k" . "eaf-send-key-sequence")
("C-o" . "eaf-send-key-sequence")
("C-u" . "eaf-send-key-sequence")
("C-l" . "eaf-send-key-sequence")
("C-w" . "eaf-send-key-sequence")
("M-f" . "eaf-send-key-sequence")
("M-b" . "eaf-send-key-sequence")
("M-d" . "eaf-send-key-sequence")
("C-c C-c" . "eaf-send-second-key-sequence")
("C-c C-x" . "eaf-send-second-key-sequence")
("<f12>" . "open_dev_tool_page")
("M-w" . "copy_text")
("C-y" . "yank_text")
("C-S-a" . "select_all")
("C-S-l" . "clear_selection")
("M-DEL" . "eaf-send-alt-backspace-sequence")
("M-<backspace>" . "eaf-send-alt-backspace-sequence"))
"The keybinding of EAF Terminal."
:type 'cons)
(defcustom eaf-camera-keybinding
'(("j" . "take_photo"))
"The keybinding of EAF Camera."
:type 'cons)
(defcustom eaf-rss-reader-keybinding
'(("a" . "add_subscription")
("d" . "delete_subscription")
("n" . "next_subscription")
("p" . "prev_subscription")
("N" . "last_subscription")
("P" . "first_subscription")
("j" . "next_article")
("k" . "prev_article")
("J" . "last_article")
("K" . "first_article")
("x" . "close_buffer")
("," . "scroll_up")
("." . "scroll_down")
("SPC" . "scroll_up_page")
("b" . "scroll_down_page")
("<" . "scroll_to_begin")
(">" . "scroll_to_bottom")
("M-s" . "open_link")
("M-S" . "open_link_new_buffer")
("C-s" . "search_text_forward")
("C-r" . "search_text_backward")
("C-n" . "scroll_up")
("C-p" . "scroll_down")
("C-v" . "scroll_up_page")
("M-v" . "scroll_down_page")
("M-<" . "scroll_to_begin")
("M->" . "scroll_to_bottom"))
"The keybinding of EAF RSS Reader."
:type 'cons)
(defcustom eaf-mindmap-keybinding
'(("TAB" . "add_sub_node")
("RET" . "add_brother_node")
("<deletechar>" . "remove_node")
("M-m" . "update_node_topic")
("M-e" . "update_node_topic_inline")
("M-r" . "refresh_page")
("C--" . "zoom_out")
("C-=" . "zoom_in")
("C-0" . "zoom_reset")
("M-j" . "select_down_node")
("M-k" . "select_up_node")
("M-h" . "select_left_node")
("M-l" . "select_right_node")
("x" . "insert_or_close_buffer")
("j" . "insert_or_select_down_node")
("k" . "insert_or_select_up_node")
("h" . "insert_or_select_left_node")
("l" . "insert_or_select_right_node")
("w" . "insert_or_copy_node_topic")
("y" . "insert_or_paste_node_topic")
("W" . "insert_or_cut_node_tree")
("Y" . "insert_or_paste_node_tree")
("J" . "insert_or_select_left_tab")
("K" . "insert_or_select_right_tab")
("-" . "insert_or_zoom_out")
("=" . "insert_or_zoom_in")
("0" . "insert_or_zoom_reset")
("d" . "insert_or_remove_node")
("D" . "insert_or_remove_middle_node")
("i" . "insert_or_add_middle_node")
("f" . "insert_or_update_node_topic")
("t" . "insert_or_toggle_node")
("b" . "insert_or_change_node_background")
("1" . "insert_or_save_screenshot")
("2" . "insert_or_save_file")
("3" . "insert_or_save_org_file")
("M-o" . "eval_js")
("M-p" . "eval_js_file")
("<f12>" . "open_dev_tool_page")
)
"The keybinding of EAF Mindmap."
:type 'cons)
(defcustom eaf-mermaid-keybinding
'(("C--" . "zoom_out")
("C-=" . "zoom_in")
("C-0" . "zoom_reset")
("C-s" . "search_text_forward")
("C-r" . "search_text_backward")
("C-n" . "scroll_up")
("C-p" . "scroll_down")
("C-f" . "scroll_right")
("C-b" . "scroll_left")
("C-v" . "scroll_up_page")
("C-w" . "kill_text")
("M-w" . "copy_text")
("M-v" . "scroll_down_page")
("M-<" . "scroll_to_begin")
("M->" . "scroll_to_bottom")
("M-t" . "new_blank_page")
("SPC" . "insert_or_scroll_up_page")
("x" . "insert_or_close_buffer")
("J" . "insert_or_select_left_tab")
("K" . "insert_or_select_right_tab")
("j" . "insert_or_scroll_up")
("k" . "insert_or_scroll_down")
("h" . "insert_or_scroll_left")
("l" . "insert_or_scroll_right")
("d" . "insert_or_scroll_up_page")
("u" . "insert_or_scroll_down_page")
("t" . "insert_or_new_blank_page")
("T" . "insert_or_recover_prev_close_page")
("r" . "insert_or_refresh_page")
("g" . "insert_or_scroll_to_begin")
("x" . "insert_or_close_buffer")
("G" . "insert_or_scroll_to_bottom")
("-" . "insert_or_zoom_out")
("=" . "insert_or_zoom_in")
("0" . "insert_or_zoom_reset")
("m" . "insert_or_save_as_bookmark")
("C-a" . "select_all_or_input_text")
("M-o" . "eval_js")
("M-p" . "eval_js_file")
("<f5>" . "refresh_page")
("<f12>" . "open_dev_tool_page")
)
"The keybinding of EAF Mermaid."
:type 'cons)
(defcustom eaf-pdf-extension-list
'("pdf" "xps" "oxps" "cbz" "epub" "fb2" "fbz" "djvu")
"The extension list of pdf application."
:type 'cons)
(defcustom eaf-markdown-extension-list
'("md")
"The extension list of markdown previewer application."
:type 'cons)
(defcustom eaf-mermaid-extension-list
'("mmd")
"The extension list of mermaid application."
:type 'cons)
(defcustom eaf-image-extension-list
'("jpg" "jpeg" "png" "bmp" "gif" "svg" "webp")
"The extension list of image viewer application."
:type 'cons)
(defcustom eaf-video-extension-list
'("avi" "rmvb" "ogg" "mp4" "mkv")
"The extension list of video player application."
:type 'cons)
(defcustom eaf-browser-extension-list
'("html" "htm")
"The extension list of browser application."
:type 'cons)
(defcustom eaf-org-extension-list
'("org")
"The extension list of org previewer application."
:type 'cons)
(defcustom eaf-mindmap-extension-list
'("emm")
"The extension list of mindmap application."
:type 'cons)
(defcustom eaf-office-extension-list
'("docx" "doc" "ppt" "pptx" "xlsx" "xls")
"The extension list of office application."
:type 'cons)
(defcustom eaf-mua-get-html
'(("^gnus-" . eaf-gnus-get-html)
("^mu4e-" . eaf-mu4e-get-html)
("^notmuch-" . eaf-notmuch-get-html))
"An alist regex mapping a MUA `major-mode' to a function to retrieve HTML part of a mail."
:type 'alist)
(defcustom eaf-browser-continue-where-left-off nil
"Similar to Chromium's Setting -> On start-up -> Continue where you left off.
If non-nil, all active EAF Browser buffers will be saved before Emacs is killed,
and will re-open them when calling `eaf-browser-restore-buffers' in the future session."
:type 'boolean)
(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-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)
(defvar eaf-app-binding-alist
'(("browser" . eaf-browser-keybinding)
("pdf-viewer" . eaf-pdf-viewer-keybinding)
("video-player" . eaf-video-player-keybinding)
("js-video-player" . eaf-js-video-player-keybinding)
("image-viewer" . eaf-image-viewer-keybinding)
("camera" . eaf-camera-keybinding)
("terminal" . eaf-terminal-keybinding)
("markdown-previewer" . eaf-browser-keybinding)
("org-previewer" . eaf-browser-keybinding)
("rss-reader" . eaf-rss-reader-keybinding)
("mindmap" . eaf-mindmap-keybinding)
("mermaid" . eaf-mermaid-keybinding)
)
"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-display-function-alist
'(("mermaid" . eaf--mermaid-preview-display)
("markdown-previewer" . eaf--markdown-preview-display)
("org-previewer" . eaf--org-preview-display))
"Mapping app names to display functions.
Display functions are called to initilize the initial view when
starting an app.
A display function receives the initialized app buffer as
argument and defaults to `switch-to-buffer'.")
(defvar eaf-app-bookmark-handlers-alist
'(("browser" . eaf--browser-bookmark)
("pdf-viewer" . eaf--pdf-viewer-bookmark))
"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-extensions-alist
'(("pdf-viewer" . eaf-pdf-extension-list)
("markdown-previewer" . eaf-markdown-extension-list)
("mermaid" . eaf-mermaid-extension-list)
("image-viewer" . eaf-image-extension-list)
("video-player" . eaf-video-extension-list)
("browser" . eaf-browser-extension-list)
("org-previewer" . eaf-org-extension-list)
("mindmap" . eaf-mindmap-extension-list)
("office" . eaf-office-extension-list))
"Mapping app names to extension list variables.
A new app can use this to configure extensions which should
handled by it.")
(defvar eaf--monitor-configuration-p t
"When this variable is non-nil, `eaf-monitor-configuration-change' execute.
This variable use 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 eaf-pdf-outline-buffer-name "*eaf pdf outline*"
"The name of pdf-outline-buffer.")
(defvar eaf-pdf-outline-window-configuration nil
"Save window configure before popup outline buffer.")
(defvar-local eaf--bookmark-title nil)
(defun eaf-browser-restore-buffers ()
"EAF restore all opened EAF Browser buffers in the previous Emacs session.
This should be used after setting `eaf-browser-continue-where-left-off' to t."
(interactive)
(if eaf-browser-continue-where-left-off
(let* ((browser-restore-file-path
(concat eaf-config-location
(file-name-as-directory "browser")
(file-name-as-directory "history")
"restore.txt"))
(browser-url-list
(with-temp-buffer (insert-file-contents browser-restore-file-path)
(split-string (buffer-string) "\n" t))))
(if (process-live-p eaf-process)
(dolist (url browser-url-list)
(eaf-open-browser url))
(dolist (url browser-url-list)
(push `(,url "browser" "") eaf--active-buffers))
(when eaf--active-buffers (eaf-open-browser (nth 0 (car eaf--active-buffers))))))
(user-error "Please set `eaf-browser-continue-where-left-off' to t first!")))
(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--browser-bookmark ()
`((handler . eaf--bookmark-restore)
(eaf-app . "browser")
(defaults . ,(list eaf--bookmark-title))
(filename . ,(eaf-get-path-or-url))))
(defun eaf--pdf-viewer-bookmark ()
`((handler . eaf--bookmark-restore)
(eaf-app . "pdf-viewer")
(defaults . ,(list eaf--bookmark-title))
(filename . ,(eaf-get-path-or-url))))
(defun eaf--bookmark-restore (bookmark)
"Restore EAF buffer according to BOOKMARK."
(let ((app (cdr (assq 'eaf-app bookmark))))
(cond ((equal app "browser")
(eaf-open-browser (cdr (assq 'filename bookmark))))
((equal app "pdf-viewer")
(eaf-open (cdr (assq 'filename bookmark)))))))
(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)))))
(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 ((string-equal system-type "windows-nt")
(w32-shell-execute "open" path-or-url))
((string-equal system-type "darwin")
(concat "open " (shell-quote-argument path-or-url)))
((string-equal system-type "gnu/linux")
(let ((process-connection-type nil))
(start-process "" nil "xdg-open" path-or-url))))))
(defun eaf-call (method &rest args)
"Call EAF Python process using `dbus-call-method' with METHOD and ARGS."
(let ((result (apply #'dbus-call-method
:session ; use the session (not system) bus
"com.lazycat.eaf" ; service name
"/com/lazycat/eaf" ; path name
"com.lazycat.eaf" ; interface name
method
:timeout 1000000
args)))
(cond ((equal result "True") t)
((equal result "False") nil)
(t result))))
(defun eaf-get-emacs-xid (frame)
(frame-parameter frame 'window-id))
(defun eaf-serialization-var-list ()
(json-encode eaf-var-list))
(defun eaf-start-process ()
"Start EAF process if it isn't started."
(cond
((eq eaf--active-buffers nil)
(user-error "[EAF] Please initiate EAF with eaf-open-... functions only"))
((process-live-p eaf-process)
(user-error "[EAF] Process is already running")))
(let ((eaf-args (append
(list eaf-python-file)
(eaf-get-render-size)
(list eaf-proxy-host eaf-proxy-port eaf-proxy-type eaf-config-location)
(list (eaf-serialization-var-list))
))
(gdb-args (list "-batch" "-ex" "run" "-ex" "bt" "--args" eaf-python-command)))
(setq eaf-process
(if eaf-enable-debug
(apply #'start-process eaf-name eaf-name "gdb" (append gdb-args eaf-args))
(apply #'start-process eaf-name eaf-name eaf-python-command eaf-args))))
(set-process-query-on-exit-flag eaf-process nil)
(set-process-sentinel
eaf-process
#'(lambda (process event)
(when (string-prefix-p "exited abnormally with code" event)
(switch-to-buffer eaf-name))
(message "[EAF] %s %s" process (replace-regexp-in-string "\n$" "" event))))
(message "[EAF] Process starting..."))
(defun eaf-stop-process (&optional restart)
"Stop EAF process and kill all EAF buffers.
When RESTART is non-nil, cached URL and app-name will not be cleared."
(interactive)
;; Kill EAF buffers.
(let ((count 0))
(dolist (buffer (buffer-list))
(set-buffer buffer)
(when (derived-mode-p 'eaf-mode)
(cl-incf count)
(kill-buffer buffer)))
;; Just report to me when EAF buffer exists.
(if (> count 1)
(message "[EAF] Killed %s EAF buffer%s" count (if (> count 1) "s" ""))))
(when (get-buffer eaf-name)
(kill-buffer eaf-name))
;; Clear active buffers
(unless restart
(setq eaf--active-buffers nil))
;; Clean `eaf-org-file-list' and `eaf-org-killed-file-list'.
(dolist (org-file-name eaf-org-file-list)
(eaf--delete-org-preview-file org-file-name))
(setq eaf-org-file-list nil)
(setq eaf-org-killed-file-list nil)
(setq-local eaf-fullscreen-p nil)
;; Kill process after kill buffer, make application can save session data.
(eaf--kill-python-process))
(defalias 'eaf-kill-process #'eaf-stop-process)
(defun eaf--kill-python-process ()
"Kill EAF background python process."
(interactive)
(if (process-live-p eaf-process)
;; Delete EAF server process.
(progn
(delete-process eaf-process)
(message "[EAF] Process terminated."))
(message "[EAF] Process already terminated.")))
(defun eaf-restart-process ()
"Stop and restart EAF process."
(interactive)
(setq eaf--active-buffers nil)
(dolist (buffer (buffer-list))
(set-buffer buffer)
(when (derived-mode-p 'eaf-mode)
(push `(,eaf--buffer-url ,eaf--buffer-app-name ,eaf--buffer-args) eaf--active-buffers)))
(eaf-stop-process t)
(eaf-start-process))
(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)
(let* ((window-edges (window-pixel-edges window))
(x (nth 0 window-edges))
(y (+ (nth 1 window-edges)
(window-header-line-height window)
(if (require 'tab-line nil t)
(if tab-line-mode (window-tab-line-height window) 0)
0)))
(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-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" (kill-new (eaf-call "call_function" eaf--buffer-id "get_url")))
(eaf-call "call_function" eaf--buffer-id "get_url"))
(user-error "This command can only be called in an EAF buffer!")))
(defun eaf-toggle-fullscreen ()
"Toggle fullscreen."