-
Notifications
You must be signed in to change notification settings - Fork 4
/
paw-util.el
2153 lines (1945 loc) · 105 KB
/
paw-util.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
;;; paw-util.el -*- lexical-binding: t; -*-
(require 'paw-vars)
(require 'paw-gptel)
(require 'paw-sdcv)
(require 'paw-goldendict)
(require 'paw-go-translate)
(require 'paw-mac)
(require 'paw-android)
(require 'paw-dictionary)
(require 'compile)
(require 'esxml-query)
(require 'thingatpt)
(require 'esxml-query)
(require 'esxml)
(eval-when-compile (defvar paw-current-entry))
(defcustom paw-player-program
(cond
((eq system-type 'darwin)
(or (executable-find "afplay")
(executable-find "mpv")
(executable-find "mplayer")
(executable-find "mpg123")))
(t
(or (executable-find "mpv")
(executable-find "mplayer")
(executable-find "mpg123"))))
"paw player program"
:group 'paw
:type 'boolean)
(defvar paw-provider-url "")
(defcustom paw-say-word-p t
"paw say word automatically"
:group 'paw
:type 'boolean)
(defcustom paw-tts-english-voice "en-US-AvaNeural"
"English tts voice."
:group 'paw
:type '(choice (const :tag "Female en-US-AvaNeural" "en-US-AvaNeural")
(const :tag "Male en-AU-WilliamNeural" "en-AU-WilliamNeural")
(const :tag "Female en-CA-ClaraNeural" "en-CA-ClaraNeural")
(const :tag "Male en-CA-LiamNeural" "en-CA-LiamNeural")
(const :tag "Female en-GB-LibbyNeural" "en-GB-LibbyNeural")
(const :tag "Female en-GB-MaisieNeural" "en-GB-MaisieNeural")
(const :tag "Male en-GB-RyanNeural" "en-GB-RyanNeural")
(const :tag "Female en-GB-SoniaNeural" "en-GB-SoniaNeural")
(const :tag "Male en-GB-ThomasNeural" "en-GB-ThomasNeural")
(const :tag "Male en-HK-SamNeural" "en-HK-SamNeural")
(const :tag "Female en-HK-YanNeural" "en-HK-YanNeural")
(const :tag "Male en-IE-ConnorNeural" "en-IE-ConnorNeural")
(const :tag "Female en-IE-EmilyNeural" "en-IE-EmilyNeural")
(const :tag "Female en-IN-NeerjaExpressiveNeural" "en-IN-NeerjaExpressiveNeural")
(const :tag "Female en-IN-NeerjaNeural" "en-IN-NeerjaNeural")
(const :tag "Male en-IN-PrabhatNeural" "en-IN-PrabhatNeural")
(const :tag "Female en-KE-AsiliaNeural" "en-KE-AsiliaNeural")
(const :tag "Male en-KE-ChilembaNeural" "en-KE-ChilembaNeural")
(const :tag "Male en-NG-AbeoNeural" "en-NG-AbeoNeural")
(const :tag "Female en-NG-EzinneNeural" "en-NG-EzinneNeural")
(const :tag "Male en-NZ-MitchellNeural" "en-NZ-MitchellNeural")
(const :tag "Female en-NZ-MollyNeural" "en-NZ-MollyNeural")
(const :tag "Male en-PH-JamesNeural" "en-PH-JamesNeural")
(const :tag "Female en-PH-RosaNeural" "en-PH-RosaNeural")
(const :tag "Female en-SG-LunaNeural" "en-SG-LunaNeural")
(const :tag "Male en-SG-WayneNeural" "en-SG-WayneNeural")
(const :tag "Male en-TZ-ElimuNeural" "en-TZ-ElimuNeural")
(const :tag "Female en-TZ-ImaniNeural" "en-TZ-ImaniNeural")
(const :tag "Female en-US-AnaNeural" "en-US-AnaNeural")
(const :tag "Male en-US-AndrewMultilingualNeural" "en-US-AndrewMultilingualNeural")
(const :tag "Male en-US-AndrewNeural" "en-US-AndrewNeural")
(const :tag "Female en-US-AriaNeural" "en-US-AriaNeural")
(const :tag "Female en-US-AvaMultilingualNeural" "en-US-AvaMultilingualNeural")
(const :tag "Female en-US-AvaNeural" "en-US-AvaNeural")
(const :tag "Male en-US-BrianMultilingualNeural" "en-US-BrianMultilingualNeural")
(const :tag "Male en-US-BrianNeural" "en-US-BrianNeural")
(const :tag "Male en-US-ChristopherNeural" "en-US-ChristopherNeural")
(const :tag "Female en-US-EmmaMultilingualNeural" "en-US-EmmaMultilingualNeural")
(const :tag "Female en-US-EmmaNeural" "en-US-EmmaNeural")
(const :tag "Male en-US-EricNeural" "en-US-EricNeural")
(const :tag "Male en-US-GuyNeural" "en-US-GuyNeural")
(const :tag "Female en-US-JennyNeural" "en-US-JennyNeural")
(const :tag "Female en-US-MichelleNeural" "en-US-MichelleNeural")
(const :tag "Male en-US-RogerNeural" "en-US-RogerNeural")
(const :tag "Male en-US-SteffanNeural" "en-US-SteffanNeural")
(const :tag "Female en-ZA-LeahNeural" "en-ZA-LeahNeural")
(const :tag "Male en-ZA-LukeNeural" "en-ZA-LukeNeural")))
(defcustom paw-tts-japanese-voice "ja-JP-NanamiNeural"
"Japanese tts voice."
:group 'paw
:type '(choice (const :tag "Female ja-JP-NanamiNeural" "ja-JP-NanamiNeural")
(const :tag "Male ja-JP-KeitaNeural" "ja-JP-KeitaNeural")))
(defcustom paw-tts-korean-voice "ko-KR-SunHiNeural"
"Korean tts voice."
:group 'paw
:type '(choice (const :tag "Female ko-KR-SunHiNeural" "ko-KR-SunHiNeural")
(const :tag "Male ko-KR-HyunsuNeural" "ko-KR-HyunsuNeural")
(const :tag "Male ko-KR-InJoonNeural" "ko-KR-InJoonNeural")))
(defcustom paw-tts-zh-cn-voice "zh-CN-YunyangNeural"
"Chinese tts voice, if detected as zh."
:group 'paw
:type '(choice (const :tag "Female zh-CN-XiaoxiaoNeural" "zh-CN-XiaoxiaoNeural")
(const :tag "Female zh-CN-XiaoyiNeural" "zh-CN-XiaoyiNeural")
(const :tag "Male zh-CN-YunjianNeural" "zh-CN-YunjianNeural")
(const :tag "Male zh-CN-YunxiNeural" "zh-CN-YunxiNeural")
(const :tag "Male zh-CN-YunxiaNeural" "zh-CN-YunxiaNeural")
(const :tag "Male zh-CN-YunyangNeural" "zh-CN-YunyangNeural")
(const :tag "Female zh-CN-liaoning-XiaobeiNeural" "zh-CN-liaoning-XiaobeiNeural")
(const :tag "Female zh-CN-shaanxi-XiaoniNeural" "zh-CN-shaanxi-XiaoniNeural")
(const :tag "Female zh-TW-HsiaoChenNeural" "zh-TW-HsiaoChenNeural")
(const :tag "Female zh-HK-HiuGaaiNeural" "zh-HK-HiuGaaiNeural")
(const :tag "Female zh-HK-HiuMaanNeural" "zh-HK-HiuMaanNeural")
(const :tag "Male zh-HK-WanLungNeural" "zh-HK-WanLungNeural")
(const :tag "Female zh-TW-HsiaoYuNeural" "zh-TW-HsiaoYuNeural")
(const :tag "Male zh-TW-YunJheNeural" "zh-TW-YunJheNeural")))
(defcustom paw-tts-zh-tw-voice "zh-TW-HsiaoChenNeural"
"Chinese tts voice, if detected as zh-Hant."
:group 'paw
:type '(choice (const :tag "Female zh-CN-XiaoxiaoNeural" "zh-CN-XiaoxiaoNeural")
(const :tag "Male zh-CN-YunJheNeural" "zh-CN-YunJheNeural")
(const :tag "Female zh-TW-HsiaoChenNeural" "zh-TW-HsiaoChenNeural")
(const :tag "Female zh-HK-HiuGaaiNeural" "zh-HK-HiuGaaiNeural")
(const :tag "Female zh-CN-XiaoyiNeural" "zh-CN-XiaoyiNeural")
(const :tag "Male zh-CN-YunjianNeural" "zh-CN-YunjianNeural")
(const :tag "Male zh-CN-YunxiNeural" "zh-CN-YunxiNeural")
(const :tag "Male zh-CN-YunxiaNeural" "zh-CN-YunxiaNeural")
(const :tag "Male zh-CN-YunyangNeural" "zh-CN-YunyangNeural")
(const :tag "Female zh-CN-liaoning-XiaobeiNeural" "zh-CN-liaoning-XiaobeiNeural")
(const :tag "Female zh-CN-shaanxi-XiaoniNeural" "zh-CN-shaanxi-XiaoniNeural")
(const :tag "Female zh-HK-HiuMaanNeural" "zh-HK-HiuMaanNeural")
(const :tag "Male zh-HK-WanLungNeural" "zh-HK-WanLungNeural")
(const :tag "Female zh-TW-HsiaoYuNeural" "zh-TW-HsiaoYuNeural")
(const :tag "Male zh-TW-YunJheNeural" "zh-TW-YunJheNeural")))
(defcustom paw-tts-multilingual-voice "en-US-AvaMultilingualNeural"
"Multilingual tts voice."
:group 'paw
:type '(choice (const :tag "Female en-US-AvaMultilingualNeural" "en-US-AvaMultilingualNeural")
(const :tag "Male de-DE-ConradNeural" "de-DE-ConradNeural")
(const :tag "Female de-DE-SeraphinaMultilingualNeural" "de-DE-SeraphinaMultilingualNeural")
(const :tag "Male en-US-AndrewMultilingualNeural" "en-US-AndrewMultilingualNeural")
(const :tag "Male fr-FR-RemyMultilingualNeural" "fr-FR-RemyMultilingualNeural")
(const :tag "Female fr-FR-VivienneMultilingualNeural" "fr-FR-VivienneMultilingualNeural")))
(defcustom paw-posframe-p nil
"show paw-view-note in posframe"
:group 'paw
:type 'boolean)
(defcustom paw-translate-p t
"translate automatically"
:group 'paw
:type 'boolean)
(define-obsolete-variable-alias 'paw-transalte-p
'paw-translate-p "paw 1.1.1")
(defcustom paw-translate-context-p t
"translate context automatically"
:group 'paw
:type 'boolean)
(define-obsolete-variable-alias 'paw-transalte-context-p
'paw-translate-context-p "paw 1.1.1")
(defcustom paw-default-say-word-function 'paw-say-word ;; paw-resay-word to regenerate the pronunciation
"paw read function"
:group 'paw
:type '(choice (function-item paw-say-word)
(function-item paw-android-say-word)
(function-item paw-youdao-say-word)
function))
(defcustom paw-share-word-function
(cond
((eq system-type 'android)
'paw-android-search-details)
((eq system-type 'darwin)
'paw-mac-dictionary-search-details)
(t
'paw-goldendict-search-details))
"paw share the word to system tool"
:group 'paw
:type '(choice (function-item paw-android-search-details)
(function-item paw-mac-dictionary-search-details)
(function-item paw-moji-search-details)
(function-item paw-eudic-search-details)
(function-item paw-chatgpt-search-details)
function))
(defcustom paw-dictionary-browse-function 'browse-url
"paw external dictionary browse function"
:group 'paw
:type '(choice (function-item browse-url)
(function-item popweb-url-input)
function))
(defcustom paw-dictionary-function
(cond
((eq system-type 'android)
'paw-eudic-search-details)
((eq system-type 'darwin)
'paw-mac-dictionary-search-details)
(t
'paw-goldendict-search-details))
"paw dictionary function, Default dictionary function for querying
the WORD."
:group 'paw
:type '(choice (function-item paw-dictionary-search)
(function-item paw-mac-dictionary-search-details)
(function-item paw-eudic-search-details)
(function-item paw-goldendict-search-details)
function))
(defcustom paw-search-function #'paw-sdcv-search-with-dictionary-async
"Default search function for querying the WORD. Its purpose is to
search the WORD, and replace the content under Meaning section
inside BUFFER.
By deafult, we use sdcv to search the WORD, you can change it
other search function. We may change it in the future.
Don't change it if you are unsure.
Be careful, the following behavior may be changed in the future:
1. It is required to be able to insert into the Meaning section direcly
2. This function can be overridden by the `:kagome' property in paw-view-note."
:group 'paw
:type 'function)
(defcustom paw-translate-function 'paw-go-translate-insert
"paw translate function. Its purpose is to tranlate the WORD with
LANG, and replace the content under Translation section inside
BUFFER.
By deafult, we use go-translate to search the word, you can change it
other search function. We may change it in the future.
Don't change it if you are unsure.
Be careful, the following behavior may be changed in the future.
1. It is required to be able to insert into the Translation section direcly."
:group 'paw
:type '(choice (function-item paw-go-translate-insert)
function))
(defcustom paw-ai-translate-function 'paw-gptel-translate
"paw ai translate (gptel) function"
:group 'paw
:type '(choice (function-item paw-gptel-translate)
function))
(defcustom paw-stardict-function 'paw-sdcv-search-detail
"paw internal (sdcv) dictionary function"
:group 'paw
:type '(choice (function-item paw-sdcv-search-detail)
function))
(defcustom paw-external-dictionary-function
(cond
((eq system-type 'android)
'paw-eudic-search-details)
((eq system-type 'darwin)
'paw-mac-dictionary-search-details)
(t
'paw-goldendict-search-details))
"paw external dictionary function"
:group 'paw
:type '(choice (function-item paw-goldendict-search-details)
(function-item paw-mac-dictionary-search-details)
(function-item paw-eudic-search-details)
function))
(defcustom paw-mdict-dictionary-function 'browse-url
"paw mdict dictionary function"
:group 'paw
:type '(choice (function-item browse-url)
function))
(defun paw-parse-json (json)
(append (alist-get 'data json ) nil))
(defun paw-buffer ()
"Create buffer *paw*."
(get-buffer-create "*paw*"))
(defun paw-format-column (string width &optional align)
"Return STRING truncated or padded to WIDTH following ALIGNment.
Align should be a keyword :left or :right."
(if (<= width 0)
""
(format (format "%%%s%d.%ds" (if (eq align :left) "-" "") width width)
string)))
(defun paw-clamp (min value max)
"Clamp a value between two values."
(min max (max min value)))
(defun paw-attach-icon-for (path)
(char-to-string
(pcase (downcase (file-name-extension path))
((or "jpg" "jpeg" "png" "gif") ?)
("pdf" ?)
((or "ppt" "pptx") ?)
((or "xls" "xlsx") ?)
((or "doc" "docx") ?)
((or "ogg" "mp3" "wav" "aiff" "flac") ?)
((or "mp4" "mov" "avi") ?)
((or "zip" "gz" "tar" "7z" "rar") ?)
(_ ?))))
(defun paw-get-real-word (entry)
"Get the word excluded the id."
(if (stringp entry)
(replace-regexp-in-string ":id:.*" "" entry)
(if entry
(replace-regexp-in-string ":id:.*" "" (alist-get 'word entry))
"")))
(defun paw-entry-p (entry)
"Check if the entry is a valid entry."
(alist-get 'word entry))
(defun paw-new-entry(word &rest properties)
;; create new entry
;; example ((word . "major") (exp . "adj. 主要的;主修的;重要的;较多的; n. 成年人;主修科目;陆军少校; vi. 主修<br>...") (content . 0) (serverp . 1) (note . "") (note_type word . "✎") (origin_type) (origin_path . "PN") (origin_id . "1709212272") (origin_point) (created_at . "2024-04-24 19:11:00"))
;; kagome: NOT the database field
;; lang: NOT the database field
`((word . ,word)
(exp . ,(plist-get properties :exp))
(content . ,(plist-get properties :content)) ;; sam as other annotations which has id, currently it only saves the real content of the word, or json string for internal usage
(serverp . ,(or (plist-get properties :serverp) 3))
(note . ,(plist-get properties :note))
(note_type word . "✎")
(origin_type . ,(or (plist-get properties :origin_type)
(if (boundp 'eaf--buffer-app-name)
eaf--buffer-app-name
major-mode) ))
(origin_path . ,(or (plist-get properties :origin_path) (paw-get-origin-path) ))
(origin_id . "")
(origin_point . ,(plist-get properties :origin_point))
(created_at . ,(plist-get properties :created-at))
(kagome . ,(plist-get properties :kagome))
(sound . ,(plist-get properties :sound))
(lang . ,(or (plist-get properties :lang) (paw-check-language word)))
(add-to-known-words . ,(plist-get properties :add-to-known-words))
(context . ,(plist-get properties :context))))
(defun paw-edge-tts-say-word (word &rest args)
"Listen to WORD pronunciation."
(let ((lang (plist-get args :lang))
(lambda (plist-get args :lambda))
(download-only (plist-get args :download-only)))
(paw-download-and-say-word
:source-name "edge-tts"
:word word
:lambda lambda
:extension "mp3"
:edge-tts t
:edge-tts-lang (or lang (paw-check-language word))
:download-only download-only) )
)
;; (paw-edge-tts-say-word "hello" "en")
;; (paw-edge-tts-say-word "hello" "ja" nil t)
(defvar paw-youdao-say-word-running-process nil)
(defun paw-youdao-say-word (word &rest args)
"Listen to WORD pronunciation."
(let ((lambda (plist-get args :lambda))
(download-only (plist-get args :download-only)))
(paw-download-and-say-word
:source-name "youdao"
:word word
:extension "mp3"
:audio-url (format "http://dict.youdao.com/dictvoice?type=2&audio=%s" (url-hexify-string word))
:lambda lambda
:download-only download-only) ))
;; (paw-youdao-say-word "hello")
;; (paw-youdao-say-word "hello" nil t)
(defun paw-download-and-say-word (&rest args)
"Use curl to download AUDIO-URL, finally play the sound file.
If LAMBDA is non-nil, call it after creating the download process."
(let* ((source-name (plist-get args :source-name))
(word (plist-get args :word))
(audio-url (plist-get args :audio-url))
(lambda (plist-get args :lambda))
(extension (plist-get args :extension))
(edge-tts (plist-get args :edge-tts))
(edge-tts-lang (plist-get args :edge-tts-lang))
(edge-tts-lang (plist-get args :edge-tts-lang))
(download-only (plist-get args :download-only))
(word-hash (md5 word))
(default-mp3-file (concat (expand-file-name word-hash paw-tts-cache-dir) "." (if extension extension (file-name-extension audio-url))))
(mp3-file (concat (expand-file-name (concat word-hash "+" source-name) paw-tts-cache-dir) "." (if extension extension (file-name-extension audio-url))))
(proc (if edge-tts
(start-process "*paw-tts*" "*paw-tts*" paw-tts-program
"--text" word
"--write-media" mp3-file
;; "--write-subtitles" subtitle-file
"--voice" (pcase (if edge-tts-lang
edge-tts-lang
(completing-read (format "Select TTS Sound Engine (%s): " word) '("en" "ja" "zh" "zh-Hant" "ko" "Multilingual") nil t))
("en" paw-tts-english-voice)
("ja" paw-tts-japanese-voice)
("zh" paw-tts-zh-cn-voice)
("zh-Hant" paw-tts-zh-tw-voice)
("ko" paw-tts-korean-voice)
(_ paw-tts-multilingual-voice)))
(start-process
(executable-find "curl")
"*paw-audio-downloder*"
(executable-find "curl")
"-L"
"--user-agent"
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"
audio-url
"--output"
mp3-file) )))
;; (message mp3-file)
(if audio-url (message "%s" audio-url) )
(if download-only
(set-process-sentinel
proc
(lambda (process event)
(when (string= event "finished\n")
(if lambda (funcall lambda mp3-file)))))
(setq paw-say-word-running-process proc)
(set-process-sentinel
proc
(lambda (process event)
(paw-play-mp3-process-sentiel process event mp3-file)
(when (string= event "finished\n")
(if (file-exists-p mp3-file) (copy-file mp3-file default-mp3-file t) )
(if lambda (funcall lambda mp3-file))))))))
(defcustom paw-tts-cache-dir
(expand-file-name (concat user-emacs-directory "edge-tts"))
"paw say word cache directory."
:group 'paw
:type 'directory)
(defvar paw-say-word-running-process nil)
(defcustom paw-tts-program "edge-tts"
"The tts program to use."
:group 'paw
:type 'string)
(defvar paw-say-word-english-functions '(paw-say-word-cambridge paw-say-word-oxford paw-youdao-say-word))
(defvar paw-say-word-japanese-functions '(paw-say-word-jpod101-alternate))
(defcustom paw-say-word-functions '(paw-say-word-cambridge paw-say-word-oxford paw-say-word-jpod101-alternate paw-edge-tts-say-word paw-youdao-say-word paw-say-word-forvo)
"The functions to download and play sound file one by one, used in `paw-say-word' if arg is nil. If any one success, it will break."
:type 'list
:group 'paw)
(defun paw-say-word-function (say-word-fns-list word finished)
;; delete the function from the list if the language is not matched
(pcase (paw-check-language word)
("en" (dolist (fn paw-say-word-japanese-functions)
(setq say-word-fns-list (cl-remove fn say-word-fns-list))))
(_ (dolist (fn paw-say-word-english-functions)
(setq say-word-fns-list (cl-remove fn say-word-fns-list)))))
(when say-word-fns-list
(let ((say-word-function (car say-word-fns-list))
(remaining-functions (cdr say-word-fns-list)))
(funcall say-word-function word
:lambda (lambda (file)
(if (and file (file-exists-p file) (> (file-attribute-size (file-attributes file)) 0))
(funcall finished file)
(paw-say-word-function remaining-functions word finished)))))))
(defun paw-say-word (word &rest args)
"Listen to WORD pronunciation with multiple SOURCEs.
If arg LANG is non-nil, use it as the edge-tts language.
If arg REFRESH is t, regenerate the pronunciation.
If arg SOURCE is t, select from source, regenerate the pronunciation.
1. Default will use edge-tts to download the pronunciation with
auto-decteded langauge.
2. If SOURCE is t, you can force edge-tts to use a specific sound
engine. After the audio file is downloaded and prounced, it will
become the default audio file for this WORD and cached in
`paw-tts-cache-dir'.
3. For Jisho/Jpod101/Jpod101Alternative, it will ask you to input
the reading of the word before downloading. It will try to get
the marked words or word at point as the reading. Reading is very
important for download Japanese audio correctly, that's why it
will prompt you every first time when download the audio file. "
(unless (executable-find paw-tts-program)
(error "edge-tts is not found, please install via 'pip install edge-tts' first."))
(when (process-live-p paw-say-word-running-process)
(kill-process paw-say-word-running-process)
(setq paw-say-word-running-process nil))
(let* ((lang (plist-get args :lang))
(refresh (plist-get args :refresh))
(source (plist-get args :source))
(word-hash (md5 word))
(mp3-file (concat (expand-file-name word-hash paw-tts-cache-dir) ".mp3"))
(audio-url)
(lambda (lambda (file)
(if (and file (file-exists-p file) (> (file-attribute-size (file-attributes file)) 0) )
(copy-file file mp3-file t)))))
(make-directory paw-tts-cache-dir t) ;; ensure cache directory exists
(when refresh
(message "Re-Downloading the audio file...")
(setq paw-say-word-jpod101-alternate-audio-list nil)
(setq pay-say-word-cambridge-audio-list nil)
(setq pay-say-word-oxford-audio-list nil)
(setq paw-say-word-forvo-audio-list nil))
(paw-say-word-delete-mp3-file (concat word-hash "+edge-tts") refresh)
(paw-say-word-delete-mp3-file (concat word-hash "+youdao") refresh)
(paw-say-word-delete-mp3-file (concat word-hash "+jisho") refresh)
(paw-say-word-delete-mp3-file (concat word-hash "+jpod101") refresh)
(paw-say-word-delete-mp3-file (concat word-hash "+jpod101-alternate") refresh)
(paw-say-word-delete-mp3-file (concat word-hash "+frovo") refresh)
(paw-say-word-delete-mp3-file (concat word-hash "+cambridge") refresh)
(paw-say-word-delete-mp3-file (concat word-hash "+oxford") refresh)
(let ((source (if source (completing-read (format "Select Audio Playback Source (%s): " word) '("edge-tts" "forvo" "youdao" "cambridge" "oxford" "jisho" "jpod101" "jpod101-alternate") nil t) "default")))
(pcase source
("default"
(if (file-exists-p mp3-file)
(setq audio-url mp3-file)
(setq audio-url mp3-file)
(paw-say-word-function paw-say-word-functions word lambda)))
("edge-tts"
(paw-edge-tts-say-word word
:lang (completing-read (format "Select TTS Sound Engine (%s): " word) '("en" "ja" "zh" "zh-Hant" "ko" "Multilingual") nil t)
:lambda lambda))
("forvo"
(paw-say-word-forvo word
:lang (completing-read (format "Select TTS Sound Engine (%s): " word) '("en" "ja" "zh" "ko") nil t)
:lambda lambda))
("youdao" (paw-youdao-say-word word :lambda lambda))
("cambridge" (paw-say-word-cambridge word :lambda lambda))
("oxford" (paw-say-word-oxford word :lambda lambda))
("jisho"
(paw-say-word-jisho word
;; have to provide a reading
(read-string (format "Reading for '%s': " word)
(let ((input (if mark-active
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'word t))))
(if (string= input paw-play-source-button)
word
input)))
:lambda lambda))
("jpod101"
(paw-say-word-jpod101 word
;; have to provide a reading
(read-string (format "Reading for '%s': " word)
(let ((input (if mark-active
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'word t))))
(if (string= input paw-play-source-button)
word
input)))
:lambda lambda))
("jpod101-alternate" (paw-say-word-jpod101-alternate word :lambda lambda))))
(if (and audio-url (file-exists-p audio-url) )
(setq paw-say-word-running-process (start-process "*paw say word*" nil paw-player-program audio-url)))
(if (and audio-url (file-exists-p audio-url) ) audio-url )))
(defun paw-say-word-delete-mp3-file (hash refresh)
(let* ((mp3-file (concat (expand-file-name hash paw-tts-cache-dir) ".mp3")))
(when (or (and refresh (file-exists-p mp3-file))
(and (file-exists-p mp3-file) (= (file-attribute-size (file-attributes mp3-file)) 0) ))
(delete-file mp3-file))))
(defun paw-play-mp3-process-sentiel(process event mp3-file)
;; When process "finished", then begin playback
(when (string= event "finished\n")
(start-process "*paw say word*" nil paw-player-program mp3-file)))
;;;###autoload
(defun paw-tts-cache-clear ()
"Clear tts cache. This will delete all the audio cache, not only tts but other sounces."
(interactive)
(delete-directory paw-tts-cache-dir t)
(make-directory paw-tts-cache-dir t))
(defun paw-resay-word (word &optional lang)
"Delete the mp3 and subtitle then regenerate."
(paw-say-word word :lang lang :refresh t))
(defun paw-resay-word-with-source (&optional word lang)
"Play with soruce."
(interactive)
(paw-say-word (or word (or (paw-note-word) (thing-at-point 'word t) ))
:lang lang :refresh t :source t))
(defun paw-say-word-with-source (&optional word lang)
"Play with soruce."
(interactive)
(paw-say-word (or word (or (paw-note-word) (thing-at-point 'word t) ))
:lang lang :source t))
(defun paw-get-note ()
(pcase major-mode
;; disable nov get header-line-format, since it is annoying, so that notes are totally control by myself
('nov-mode
(paw-remove-spaces-based-on-ascii-rate (or (thing-at-point 'sentence t) "")))
('pdf-view-mode "")
('paw-search-mode "")
('paw-view-note-mode (alist-get 'context paw-current-entry))
('eaf-mode "")
(_ (or (paw-get-sentence-or-line t) ""))))
(defvar paw-get-sentence-max-length 300)
(defun paw-get-sentence-or-line(&optional no-click-show)
"Get the sentence or line at point. If the line is too long (>
`paw-get-sentence-max-length'), then use the current line. Remove
org link in the sentence."
(let ((current-thing (thing-at-point 'sentence t)))
(if current-thing
(let* ((length-of-thing (length current-thing))
(bounds (bounds-of-thing-at-point 'sentence))
(beg (car bounds))
(end (cdr bounds)))
(cond ((or (> length-of-thing paw-get-sentence-max-length) (= length-of-thing 0)) ;; if the sentence is too long, like detect failed, then use the current line
(let* ((line (thing-at-point 'line t))
(bounds (bounds-of-thing-at-point 'line))
(beg (car bounds))
(end (cdr bounds)))
;; remove org links
(when (string-match "\\[\\[.*?\\]\\[.*?\\]\\]" line)
(setq line (replace-match "" nil nil line)))
(unless no-click-show (paw-click-show beg end 'paw-focus-face) )
line))
;; remove org links
(t (when (string-match "\\[\\[.*?\\]\\[.*?\\]\\]" current-thing)
(setq current-thing (replace-match "" nil nil current-thing)))
(unless no-click-show (paw-click-show beg end 'paw-focus-face) )
current-thing)))
"")))
(defcustom paw-ascii-rate 0.5
"The rate of ascii characters in the text.
If `paw-ascii-rate' < 1, use the ascii rate to determine the language of the text:
- if matched, use `paw-default-language'.
- if umatched, and `paw-detect-language-p' is t, use `paw-detect-language-program' to determine the language.
- if umatched, and if `paw-detect-language-p' is nil, use `paw-non-ascii-language' directly.
If `paw-ascii-rate' >= 1, it will turn of ascii rate pre-checking, then ignore `paw-default-language', and:
- if `paw-detect-language-p' is t, use `paw-detect-language-program' to determine the language
- if `paw-detect-language-p' is nil, use `paw-non-ascii-language' directly.
The external language detection tools are known to be bad on
short words, so it is here to use `paw-ascii-rate' to determine
the language first.
If you always work on two languages, I recommend you find the
comfortable `paw-ascii-rate' (< 1), and turn off
`paw-detect-language-p'. This works well on ascii language +
non-ascii language, such as English + Japanese/Korean.
If you only want to use language detection tool to determine the
language. I recommend you set `paw-ascii-rate' to 1, and
`paw-default-language' to t, in this case, both
`paw-default-language' and `paw-non-ascii-language' will be
ingored."
:group 'paw
:type 'float)
(defcustom paw-default-language "en"
"The default language for ascii characters."
:group 'paw
:type 'string)
(defcustom paw-non-ascii-language "ja"
"The default language for non-ascii characters. It only works when
`paw-detect-language-program' is nil"
:group 'paw
:type 'string)
(defcustom paw-non-ascii-word-separator " "
"The default separator to be placed between words in non-ascii languages."
:group 'paw
:type 'string)
(defun paw-check-language (text)
"If provide a filename as TEXT, it will use the file content to detect the
language, if `paw-detect-language-p' is t, or return as
`paw-default-language' if `paw-detect-language-p' is nil.
For an org file, setup the Local Variables like so:
# Local Variables:
# eval: (setq-local paw-detect-language-p nil)
# eval: (setq-local paw-default-language \"ja\")
# eval: (paw-annotation-mode +1)
# End:
Could always use the specified langauge, and avoid false/redundant detection.
If provide a string TEXT, use simple ascii rate: `paw-ascii-rate' to detect the language,
if the rate is greater than `paw-ascii-rate', then it is
considered as `paw-default-language', otherwise use
`paw-detect-language-program' to detect the language of the TEXT,
if `paw-detect-language-p' is t, or return as `paw-non-ascii-language' if
`paw-detect-language-p' is nil."
(cond ((string-empty-p text)
"en")
((file-exists-p text)
(if paw-detect-language-p
(with-temp-buffer
(pcase paw-detect-language-program
('gcld3 (call-process paw-python-program nil t nil "-c"
"import sys, gcld3; detector = gcld3.NNetLanguageIdentifier(min_num_bytes=0, max_num_bytes=2000); result = detector.FindLanguage(text=open(sys.argv[1], 'r').read()); print(result.language)"
text))
('pycld2 (call-process paw-python-program nil t nil "-c"
"import sys; import pycld2 as cld2; reliable, _, detections = cld2.detect(open(sys.argv[1], 'r').read()); print(detections[0][1])"
text))
(_ (call-process paw-detect-language-program nil t nil text)))
(goto-char (point-min))
(let ((detected-lang (string-trim (buffer-string))))
(if (string-equal "un" detected-lang) "en" detected-lang)))
paw-default-language))
(t (let* ((strs (split-string text "")) ;; after spliting, it has two redundant elements, that's why minus 2 below
(number (cl-count-if (lambda (str) (string-match-p "[[:ascii:]]+" str)) strs))
(rate paw-ascii-rate)
(lang (if (> (/ number (float (- (length strs) 2))) rate) ;; it is impossible to > 1, if 1 or larger, will ignore `paw-default-language'
paw-default-language
(if paw-detect-language-p
(with-temp-buffer
(pcase paw-detect-language-program
('gcld3 (call-process
paw-python-program
nil ;; Infile
t ;; Buffer
nil ;; Display
"-c"
"import sys, gcld3; detector = gcld3.NNetLanguageIdentifier(min_num_bytes=0, max_num_bytes=2000); result = detector.FindLanguage(text=sys.argv[1]); print(result.language)"
text))
('pycld2 (call-process
paw-python-program
nil
t
nil
"-c"
"import sys; import pycld2 as cld2; reliable, _, detections = cld2.detect(sys.argv[1]); print(detections[0][1])"
text))
(_ (call-process paw-detect-language-program
nil
t
nil
text)))
(goto-char (point-min))
(let ((detected-lang (string-trim (buffer-string))))
(if (string-equal "un" detected-lang) "en" detected-lang)))
paw-non-ascii-language))))
;; (message "Text: %s, Language: %s" text lang)
lang))
)
)
(defun paw-remove-spaces (text lang)
"TODO Refomat the TEXT based on the LANG."
(cond ((string= lang "en") (replace-regexp-in-string "[ \n]+" " " (replace-regexp-in-string "^[ \n]+" "" text)))
((or (string= lang "ja")
(string= lang "zh"))
(replace-regexp-in-string "\\(^[ \t\n\r]+\\|[ \t\n\r]+\\)" "" text))
(t text)))
(defun paw-remove-spaces-based-on-ascii-rate-return-cons (text)
"TODO Refomat the text based on the language."
(let ((lang (paw-check-language text)))
(cons lang (paw-remove-spaces text lang))))
(defun paw-remove-spaces-based-on-ascii-rate (text)
"TODO Refomat the text based on the language."
(let ((lang (paw-check-language text)))
(paw-remove-spaces text lang)))
(defun paw-provider-lookup (word provider alist)
(let* ((provider-alist alist)
(url-template (cadr (assoc provider provider-alist))))
(format url-template (paw-get-real-word word ))))
(defun paw-get-id ()
(pcase major-mode
('wallabag-entry-mode
(alist-get 'id (get-text-property 1 'wallabag-entry)))
('eaf-mode
nil)
(_ nil)))
;;; mark/unmark
(defun paw-previous ()
(let ((location (get-text-property (point) 'paw-id)) previous)
(cond
;; check the current point headline number first
((numberp location)
(setq previous (text-property-any (point-min) (point-max) 'paw-id (1- location)))
(if (numberp previous)
(goto-char previous)
(goto-char (point-min))))
;; check the current point if >= the first header (no matter level), keep (point) if no headlines
((>= (or (text-property-not-all (point-min) (point-max) 'paw-id nil) (point)) (point))
(message "Beginning of buffer")
(goto-char (point-min)))
(t
(let ((current (point-min)) (start (1+ (point))) point number)
;; Scan from point-min to (1+ (point)) to find the current headline.
;; (1+ (point)) to include under current point headline into the scan range.
(if (<= start (point-max))
(while (setq point (text-property-not-all
current start 'paw-id nil))
(setq current (1+ point))) ; not at (point-max)
(while (setq point (text-property-not-all
current (point-max) 'paw-id nil))
(setq current (1+ point)))) ; at the (point-max)
(setq number (1- (get-text-property (1- current) 'paw-id)))
(goto-char (text-property-any (point-min) (point-max) 'paw-id (1+ number))))))))
(defun paw-next ()
(let* ((header-in-line (text-property-not-all (line-beginning-position) (line-end-position) 'paw-id nil))
(location (get-text-property (or header-in-line (point)) 'paw-id))
next)
(cond
;; check the current line headline number first, since if use org-cycle, cursor will go to the begining of line
((numberp location)
(setq next (text-property-any (point-min) (point-max) 'paw-id (1+ location)))
(if (numberp next)
(goto-char next)
(goto-char (point-max))))
;; check the current point if >= the first header (no matter level), keep (point) if no headlines
((>= (setq next (or (text-property-not-all (point-min) (point-max) 'paw-id nil) (point))) (point))
(if (equal next (point))
(progn
(message "End of buffer")
(goto-char (point-max)) )
(goto-char next)))
(t
(let ((current (point-min)) (start (1+ (point))) point number)
;; Scan from point-min to (1+ (point)) to find the current headline.
;; (1+ (point)) to include under current point headline into the scan range.
(unless (> start (point-max))
(while (setq point (text-property-not-all
current start 'paw-id nil))
(setq current (1+ point))))
(cond ((equal (point) 1) (setq number 0))
((equal (point) 2) (setq number 0))
((equal (point) (point-max)) (setq number (point-max)) (message "End of buffer"))
(t
(setq number (1- (get-text-property (1- current) 'paw-id)))))
(goto-char (or (text-property-any (point-min) (point-max) 'paw-id (+ 2 number)) (point-max))))))))
;;;###autoload
(defun paw-mark-at-point ()
"Mark the current line."
(interactive)
(remove-overlays (line-beginning-position) (line-end-position))
(let* ((beg (line-beginning-position))
(end (line-end-position))
(inhibit-read-only t)
(overlay (make-overlay beg end)))
(overlay-put overlay 'face 'paw-mark-face)
(put-text-property beg end 'paw-mark ?>)))
;;;###autoload
(defun paw-unmark-at-point ()
"Unmark the current line."
(interactive)
(let* ((beg (line-beginning-position))
(end (line-end-position))
(inhibit-read-only t))
(remove-overlays (line-beginning-position) (line-end-position))
(remove-text-properties beg end '(paw-mark nil))))
;;;###autoload
(defun paw-mark-and-forward ()
"Mark the current line and forward."
(interactive)
(paw-mark-at-point)
(paw-next))
;;;###autoload
(defun paw-unmark-and-forward ()
"Unmark the current line and forward."
(interactive)
(paw-unmark-at-point)
(paw-next))
;;;###autoload
(defun paw-unmark-and-backward ()
"Unmark the current line and backward."
(interactive)
(paw-previous)
(paw-unmark-at-point))
(defun paw-clear-marks ()
(if (eq (current-buffer) (get-buffer "*paw*"))
(let* ((beg (point-min))
(end (point-max))
(inhibit-read-only t))
(remove-overlays beg end)
(remove-text-properties beg end '(paw-mark nil)))))
;;;###autoload
(defun paw-find-candidate-at-point ()
"Find candidate at point and return the list."
(interactive)
(get-text-property (point) 'paw-entry))
;;;###autoload
(defun paw-find-marked-candidates ()
"Find marked candidates and return the alist."
(interactive)
(save-excursion
(let (candidate beg end cand-list)
(when (text-property-not-all (point-min) (point-max) 'paw-mark nil)
(setq end (text-property-any (point-min) (point-max) 'paw-mark ?>))
(while (setq beg (text-property-any end (point-max) 'paw-mark ?>) )
(goto-char beg)
(setq candidate (paw-find-candidate-at-point))
(push candidate cand-list)
;; (message (number-to-string beg))
(forward-line 1)
(setq end (point)))
cand-list))))
(defun paw-insert-and-make-overlay (str prop val &optional export)
"when `export' is t, use insert directly, otherwise use overlay"
(if export
(insert str)
(let* ((start (point))
(end (+ start (length str)))
overlay)
(insert str)
(setq overlay (make-overlay start end))
(overlay-put overlay prop val)
overlay)))
;;;###autoload
(defun paw-scroll-up(arg)
(interactive "p")
(cond ((eq major-mode 'paw-view-note-mode)
(call-interactively 'org-forward-element)
(recenter 0))
((eq major-mode 'paw-search-mode)
(call-interactively 'paw-search-next-page))
((eq major-mode 'nov-mode)
(call-interactively 'nov-scroll-up))
(t (call-interactively 'scroll-up))))
;;;###autoload
(defun paw-scroll-down(arg)
(interactive "P")
(cond ((eq major-mode 'paw-view-note-mode)
(call-interactively 'org-backward-element)
(recenter 0))
((eq major-mode 'paw-search-mode)
(call-interactively 'paw-search-previous-page))
((eq major-mode 'nov-mode)
(call-interactively 'nov-scroll-down))
(t (call-interactively 'scroll-down))))
;;;###autoload
(defun paw-goto-toc()
(interactive)
(pcase major-mode
('nov-mode (nov-goto-toc))
('wallabag-entry-mode (wallabag))
(_ (consult-notes))))
;;;###autoload
(defun paw-step-backward()
(interactive)