-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
telega-modes.el
2160 lines (1920 loc) · 87.4 KB
/
telega-modes.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
;;; telega-modes.el --- Minor modes for the telega -*- lexical-binding:t -*-
;; Copyright (C) 2019 by Zajcev Evgeny.
;; Author: Zajcev Evgeny <[email protected]>
;; Created: Thu Aug 15 19:18:23 2019
;; Keywords:
;; telega is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; telega 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 telega. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; See https://zevlg.github.io/telega.el/#minor-modes
;;; Code:
(require 'telega-customize)
(require 'telega-server)
(require 'telega-filter)
(require 'telega-util)
(defvar tracking-buffers)
(declare-function telega-account-current "telega")
(declare-function telega "telega" (&optional arg))
(declare-function telega-kill "telega" (force))
(defgroup telega-modes nil
"Customization for telega minor modes."
:prefix "telega-"
:group 'telega)
;;; ellit-org: minor-modes
;; ** telega-mode-line-mode
;;
;; Global minor mode to display =telega= status in modeline.
;;
;; Enable with ~(telega-mode-line-mode 1)~ or at =telega= load time:
;; #+begin_src emacs-lisp
;; (add-hook 'telega-load-hook 'telega-mode-line-mode)
;; #+end_src
;;
;; Customizable options:
;;
;; - {{{user-option(telega-mode-line-string-format, 2)}}}
(defcustom telega-mode-line-string-format
'(" " (:eval (telega-mode-line-icon))
(:eval (car (telega-account-current)))
(:eval (telega-mode-line-online-status))
(:eval (when telega-use-tracking-for
(telega-mode-line-tracking)))
(:eval (telega-mode-line-unread-unmuted))
(:eval (telega-mode-line-mentions 'messages)))
"Format in mode-line-format for `telega-mode-line-string'."
:type 'sexp
:group 'telega-modes)
(defvar telega-mode-line-string ""
"Used to cache formatted modeline string.")
(defcustom telega-mode-line-format
(list '(:eval (when (telega-server-live-p)
telega-mode-line-string)))
"Format in mode-line-format to be used as part of `global-mode-string'."
:type 'sexp
:group 'telega-modes
:risky t)
(defvar telega-mode-line--logo-image-cache nil "Cached loaded logo image.")
(defun telega-mode-line-logo-image ()
"Return telega logo image to be used in modeline."
(let* ((box-line-width-raw
(plist-get (face-attribute telega--default-face :box) :line-width))
(box-line-width
(if (consp box-line-width-raw)
(car box-line-width-raw)
(or box-line-width-raw 0)))
(mode-line-height
(+ (telega-chars-xheight 1)
;; NOTE: height adjustment only needed if box-line-width
;; is negative. See https://t.me/emacs_telega/26677
(if (< box-line-width 0)
(* 2 box-line-width)
0))))
(if (eq mode-line-height
(plist-get (cdr telega-mode-line--logo-image-cache) :height))
telega-mode-line--logo-image-cache
(setq telega-mode-line--logo-image-cache
(find-image
(list (list :type 'svg :file "etc/telega-logo.svg"
:scale 1 :ascent 'center :mask 'heuristic
:height mode-line-height)
(list :type (when (fboundp 'imagemagick-types) 'imagemagick)
:file "etc/telega-logo.png"
:scale 1 :ascent 'center :mask 'heuristic
:height mode-line-height)
(list :type 'xpm :file "etc/telega-logo.xpm"
:scale 1 :ascent 'center
:height mode-line-height)))))))
(defun telega-mode-line-icon ()
"Return telegram logo icon to be used in modeline."
(propertize telega-symbol-telegram
'display (telega-mode-line-logo-image)
'local-map (eval-when-compile
(make-mode-line-mouse-map 'mouse-1 'telega))
'mouse-face 'mode-line-highlight
'help-echo "Click to show telega root buffer"))
(defun telega-mode-line-online-status ()
"Return online status symbol."
;; NOTE: At start time user info might not be available
(when-let ((me-user (telega-user-me 'locally)))
(if (telega-user-online-p me-user)
telega-symbol-online-status
(propertize telega-symbol-online-status 'face 'telega-shadow))))
(defun telega-mode-line--online-status-update (event)
(when (eq (plist-get event :user_id) telega--me-id)
(telega-mode-line-update)))
(defmacro telega-mode-line-filter-gen (filter-spec)
"Generate filtering command for `telega-mode-line-mode' using FILTER-SPEC."
`(lambda ()
(interactive)
(telega nil)
(telega-view-default)
(telega-filters-push ,filter-spec)))
(defun telega-mode-line-tracking ()
"Format number of tracking chats."
(when tracking-buffers
(concat
" "
(propertize (concat "[" (number-to-string (length tracking-buffers)) "]")
'local-map
(eval-when-compile
(make-mode-line-mouse-map
'mouse-1 (telega-mode-line-filter-gen '(tracking))))
'mouse-face 'mode-line-highlight
'help-echo "Click to filter tracking chats"))))
(defun telega-mode-line-unread-unmuted (&optional messages-p)
"Format unread-unmuted chats/messages.
If MESSAGES-P is non-nil then use number of unread unmuted messages."
(let ((uu-count (if messages-p
(plist-get telega--unread-message-count :unread_unmuted_count)
(plist-get telega--unread-chat-count :unread_unmuted_count))))
;; NOTE: `telega--unread-chat-count' or
;; `telega--unread-message-count' might not be yet updated, so
;; `uu-count' can be nil
(unless (zerop (or uu-count 0))
(concat
" "
(propertize (number-to-string uu-count)
'face 'telega-unread-unmuted-modeline
'local-map
(eval-when-compile
(make-mode-line-mouse-map
'mouse-1 (telega-mode-line-filter-gen '(unread unmuted))))
'mouse-face 'mode-line-highlight
'help-echo "Click to filter chats with unread/unmuted messages")))))
(defun telega-mode-line-mentions (&optional messages-p)
"Format number of chats/messages with mentions.
If MESSAGES-P is non-nil then use number of messages with mentions."
(let* ((m-chats (telega-filter-chats telega--ordered-chats '(mention)))
(m-count (if messages-p
(apply '+ (mapcar (telega--tl-prop :unread_mention_count) m-chats))
(length m-chats))))
(unless (zerop m-count)
(concat
" "
(propertize (concat "@" (number-to-string m-count))
'face 'telega-mention-count
'local-map
(eval-when-compile
(make-mode-line-mouse-map
'mouse-1 (telega-mode-line-filter-gen '(mention))))
'mouse-face 'mode-line-highlight
'help-echo "Click to filter chats with mentions")))))
(defun telega-mode-line-update (&rest _ignored)
"Update value for `telega-mode-line-string'."
(when telega-mode-line-mode
(setq telega-mode-line-string
(when (telega-server-live-p)
(telega-format-mode-line telega-mode-line-string-format)))
(force-mode-line-update 'all)))
;;;###autoload
(define-minor-mode telega-mode-line-mode
"Toggle display of the unread chats/mentions in the modeline."
:init-value nil :global t :group 'telega-modes
(setq telega-mode-line-string "")
(unless global-mode-string
(setq global-mode-string '("")))
(if telega-mode-line-mode
(progn
(unless (memq 'telega-mode-line-format global-mode-string)
(setq global-mode-string
(append global-mode-string '(telega-mode-line-format))))
(advice-add 'telega--on-updateUnreadMessageCount
:after 'telega-mode-line-update)
(advice-add 'telega--on-updateUnreadChatCount
:after 'telega-mode-line-update)
(advice-add 'telega--on-updateChatUnreadMentionCount
:after 'telega-mode-line-update)
(advice-add 'telega--on-updateUserStatus
:after 'telega-mode-line--online-status-update)
(add-hook 'telega-ready-hook 'telega-mode-line-update)
(add-hook 'telega-chats-fetched-hook 'telega-mode-line-update)
(add-hook 'telega-kill-hook 'telega-mode-line-update)
;; NOTE: `tracking-buffer-added-hook', and
;; `tracking-buffer-removed-hook' are called *before*
;; tracking-buffers modification, so use advices instead
(advice-add 'tracking-add-buffer :after 'telega-mode-line-update)
(advice-add 'tracking-remove-buffer :after 'telega-mode-line-update)
(telega-mode-line-update))
(setq global-mode-string
(delq 'telega-mode-line-format global-mode-string))
(advice-remove 'telega--on-updateUnreadMessageCount
'telega-mode-line-update)
(advice-remove 'telega--on-updateUnreadChatCount
'telega-mode-line-update)
(advice-remove 'telega--on-updateChatUnreadMentionCount
'telega-mode-line-update)
(advice-remove 'telega--on-updateUserStatus
'telega-mode-line--online-status-update)
(remove-hook 'telega-ready-hook 'telega-mode-line-update)
(remove-hook 'telega-chats-fetched-hook 'telega-mode-line-update)
(remove-hook 'telega-kill-hook 'telega-mode-line-update)
(advice-remove 'tracking-add-buffer 'telega-mode-line-update)
(advice-remove 'tracking-remove-buffer 'telega-mode-line-update)
))
;;; ellit-org: minor-modes
;; ** telega-appindicator-mode
;;
;; Global minor mode to display =telega= status in system tray. This
;; mode requires appindicator support in the =telega-server=. To add
;; appindicator support to =telega-server=, please install
;; =libappindicator3-dev= or =libayatana-appindicator3-dev=
;; system package and rebuild =telega-server=
;; with {{{kbd(M-x telega-server-build RET}}}.
;;
;; Screenshot of system tray with enabled =telega= appindicator:
;; [[https://zevlg.github.io/telega/screen-appindicator.png]]
;;
;; Enable with ~(telega-appindicator-mode 1)~ or at =telega= load time:
;; #+begin_src emacs-lisp
;; (add-hook 'telega-load-hook 'telega-appindicator-mode)
;; #+end_src
;;
;; Customizable options:
;; - {{{user-option(telega-appindicator-use-label, 2)}}}
;; - {{{user-option(telega-appindicator-icon-colors, 2)}}}
;; - {{{user-option(telega-appindicator-show-account-name, 2)}}}
;; - {{{user-option(telega-appindicator-show-mentions, 2)}}}
;; - {{{user-option(telega-appindicator-labels, 2)}}}
(defcustom telega-appindicator-use-label nil
"Non-nil to add text labels to the icon.
Otherwise use just icon to show info.
labels are not supported by XEMBED based system trays, such as
`exwm-systemtray' or `polybar'."
:package-version '(telega . "0.7.20")
:type 'boolean
:group 'telega-modes)
(defcustom telega-appindicator-icon-colors
'((offline "white" "black" nil)
(online "#7739aa" "white" "#00ff00")
(connecting "gray" "white" "white"))
"Colors to use for offline/online appindicator icon.
Alist with `offline', `online' or `connecting' as key, and value in form
(CIRCLE-COLOR TRIANGLE-COLOR ONLINE-CIRCLE-COLOR)."
:package-version '(telega . "0.7.34")
:type '(alist :key-type symbol :value-type (repeat string))
:group 'telega-modes)
(defcustom telega-appindicator-show-account-name t
"*Non-nil to show current account name in appindicator label.
Applied only if `telega-appindicator-use-label' is non-nil."
:package-version '(telega . "0.7.2")
:type 'boolean
:group 'telega-modes)
(defcustom telega-appindicator-show-mentions t
"*Non-nil to show number of mentions in appindicator label.
Applied only if `telega-appindicator-use-label' is non-nil."
:package-version '(telega . "0.7.2")
:type 'boolean
:group 'telega-modes)
(defcustom telega-appindicator-labels
'("❶" "❷" "❸" "❹" "❺" "❻" "❼" "❽" "❾" "❿"
"⓫" "⓬" "⓭" "⓮" "⓯" "⓰" "⓱" "⓲" "⓳" "⓴")
"List of number labels to use for the number of unread unmuted chats.
Use this labels instead of plain number.
Set to nil to use plain number."
:package-version '(telega . "0.7.2")
:type '(repeat string)
:group 'telega-modes)
(defvar telega-appindicator--cached-icons nil
"Cached icons for offline/online statuses.")
;;;###autoload
(define-minor-mode telega-appindicator-mode
"Toggle display of the unread chats/mentions in the system tray."
:init-value nil :global t :group 'telega-modes
(if telega-appindicator-mode
(progn
(advice-add 'telega--on-updateUnreadChatCount
:after 'telega-appindicator-update)
(advice-add 'telega--on-updateChatUnreadMentionCount
:after 'telega-appindicator-update)
(add-hook 'telega-ready-hook 'telega-appindicator-init)
(add-hook 'telega-chats-fetched-hook 'telega-appindicator-update)
(add-hook 'telega-online-status-hook 'telega-appindicator-update)
(add-hook 'telega-connection-state-hook 'telega-appindicator-update)
(when (telega-server-live-p)
(telega-appindicator-init)))
(remove-hook 'telega-connection-state-hook 'telega-appindicator-update)
(remove-hook 'telega-online-status-hook 'telega-appindicator-update)
(remove-hook 'telega-chats-fetched-hook 'telega-appindicator-update)
(remove-hook 'telega-ready-hook 'telega-appindicator-init)
(advice-remove 'telega--on-updateChatUnreadMentionCount
'telega-appindicator-update)
(advice-remove 'telega--on-updateUnreadChatCount
'telega-appindicator-update)
;; Deactivate appindicator
(when (telega-server-live-p)
(telega-server--send "status passive" "appindicator"))
))
(defun telega-appindicator--gen-svg-icon (&optional label)
"Generate svg icon to be used in appindicator.
Return filename of the generated icon."
(let* ((state (cond ((eq telega--conn-state 'Connecting) 'connecting)
((funcall telega-online-status-function) 'online)
(t 'offline)))
(cached-label (concat (symbol-name state) label))
(cached-entry (assoc cached-label telega-appindicator--cached-icons)))
;; NOTE: Check cached icon is still accessible
(when (and cached-entry (not (file-exists-p (cdr cached-entry))))
(setq telega-appindicator--cached-icons
(remove cached-entry telega-appindicator--cached-icons)
cached-entry nil))
(or (cdr cached-entry)
(let* ((w 48) (h 48) (logo-w 36)
(svg (telega-svg-create w h))
(colors (cdr (assq state telega-appindicator-icon-colors))))
(svg-circle svg (/ h 2) (/ h 2) (/ h 2)
:fill-color (nth 0 colors))
(when (nth 2 colors)
(svg-circle svg (/ h 6) (/ h 6) (/ h 6)
:fill-color (nth 2 colors)))
(telega-svg-telega-logo svg logo-w
:fill-color (nth 1 colors)
:transform (format "translate(%f, %f)"
(/ (- h logo-w) 3) (- h logo-w)))
;; Label
(when (and label (not (string-empty-p label)))
(let ((fsz 36))
;; XXX: white background below the label
;; TODO: find a better way in the future
(svg-circle svg (- w (/ fsz 2)) (- h (/ fsz 2)) (1- (/ fsz 2))
:fill-color "white")
(svg-text svg label
:font-size fsz
:font-weight "bold"
:fill "#ff0000"
:font-family "monospace"
:stroke-width 1
:stroke-color "white"
;; XXX insane X/Y calculation
:x (- w fsz)
:y (- h (/ fsz 8)))))
(let ((image (telega-svg-image svg))
(svg-icon-file
(expand-file-name
(concat "appindicator-icon-" cached-label ".svg")
telega-temp-dir)))
(write-region (plist-get (cdr image) :data)
nil svg-icon-file nil 'quiet)
;; Cache it
(setq telega-appindicator--cached-icons
(cons (cons cached-label svg-icon-file)
telega-appindicator--cached-icons))
svg-icon-file)))))
(defun telega-appindicator-init ()
"Initialize appindicator."
(when telega-appindicator-mode
(telega-server--send
(concat "setup " (telega-appindicator--gen-svg-icon))
"appindicator")
(telega-appindicator-update)))
(defun telega-appindicator-update (&rest _ignored)
"Update appindicator label."
(when telega-appindicator-mode
(let* ((account
(when (and telega-appindicator-use-label
telega-appindicator-show-account-name)
(car (telega-account-current))))
(uu-chats-num
(or (plist-get telega--unread-chat-count :unread_unmuted_count)
0))
(uu-chats-str
(unless (zerop uu-chats-num)
(or (nth (1- uu-chats-num) telega-appindicator-labels)
(number-to-string uu-chats-num))))
(mentions-num
(or (when (and telega-appindicator-use-label
telega-appindicator-show-mentions)
(length (telega-filter-chats
telega--ordered-chats '(mention))))
0))
(mentions-str
(unless (zerop mentions-num)
(format "@%d" mentions-num)))
(label-strings
(remove nil (list account
(when (and account
(or uu-chats-str mentions-str))
"-")
uu-chats-str
mentions-str)))
(new-label (mapconcat #'identity label-strings " "))
(icon-filename
(telega-appindicator--gen-svg-icon
(unless telega-appindicator-use-label
new-label))))
(telega-server--send (concat "icon " icon-filename) "appindicator")
(when telega-appindicator-use-label
(telega-server--send (concat "label " new-label) "appindicator")))))
(defun telega-appindicator--on-event (event)
"Function called when event from appindicator is received."
(cond ((string= event "open")
;; NOTE: Raise Emacs frame and open rootbuf
(x-focus-frame nil)
(telega)
;; If there is single important chat, then switch to it
(let ((ichats (telega-filter-chats telega--ordered-chats 'important)))
(when (= 1 (length ichats))
(telega-switch-important-chat (car ichats)))))
((string= event "quit")
(x-focus-frame nil)
(telega-kill nil))
(t
(message "telega-server: Unknown appindicator-event: %s" event))))
;;; ellit-org: minor-modes
;; ** telega-autoplay-mode
;;
;; Global minor mode to automatically open content for incoming
;; messages. Message automatically opens if it matches against
;; ~telega-autoplay-msg-temex~ [[#telega-match-expressions][Message
;; Temex]] and message's content is fully observable.
;;
;; Enable with ~(telega-autoplay-mode 1)~ or at =telega= load time:
;; #+begin_src emacs-lisp
;; (add-hook 'telega-load-hook 'telega-autoplay-mode)
;; #+end_src
;;
;; Customizable options:
;; - {{{user-option(telega-autoplay-msg-temex, 2)}}}
;; - {{{user-option(telega-autoplay-custom-emojis, 2)}}}
(defcustom telega-autoplay-msg-temex
'(type Animation Sticker AnimatedEmoji Gift)
"Message Temex for messages to automatically play content for."
:type 'telega-msg-temex
:options '((and (not outgoing)
(or (type Animation Sticker AnimatedEmoji Gift)
(link-preview Animation Sticker))))
:group 'telega-modes)
(defcustom telega-autoplay-custom-emojis 10
"Non-nil to automatically play this number of custom emojis in the message."
:type '(choice (const :tag "Autoplay Disabled" nil)
integer)
:group 'telega-modes)
(defun telega-autoplay-custom-emojis (msg &optional force)
"Animate custom emojis for the message MSG."
(when telega-autoplay-custom-emojis
(when (or force (telega-msg-observable-p msg))
(let* ((custom-emoji-ids
(nconc (telega-custom-emoji--ids-for-msg msg '(content))
;; NOTE: reactions in the SavedMessages chat are tags
;; We don't animate tags
(unless (telega-msg-match-p msg '(chat saved-messages))
(telega-custom-emoji--ids-for-msg msg '(reactions)))))
(custom-emoji-stickers
(seq-take (seq-filter
(lambda (sticker)
(and sticker
(not (telega-sticker-static-p sticker))))
(mapcar #'telega-custom-emoji-get custom-emoji-ids))
telega-autoplay-custom-emojis)))
(dolist (custom-emoji custom-emoji-stickers)
(when (and (not (telega-sticker-static-p custom-emoji))
telega-sticker-animated-play)
(telega-sticker--animate custom-emoji))))
)))
(defun telega-autoplay-on-msg (msg)
"Automatically play contents of the message MSG.
Play in muted mode."
(when (telega-msg-observable-p msg)
;; Animate custom emojis first
(telega-autoplay-custom-emojis msg 'observable)
;; Then animate message's content itself
(when (telega-msg-match-p msg telega-autoplay-msg-temex)
(cond ((telega-msg-match-p msg
'(or (type Animation) (link-preview Animation)))
;; NOTE: special case for animations, animate only those
;; which can be animated inline, see
;; `telega-animation-play-inline'
(let ((animation
(or (telega--tl-get msg :content :animation)
(telega--tl-get msg :content :link_preview
:type :animation))))
(when (telega-animation-play-inline-p animation)
(telega-msg-open-animation msg animation))))
((telega-msg-match-p msg
'(or (type Sticker Gift) (link-preview Sticker)))
;; NOTE: special case for sticker messages, play animated
;; sticker only if `telega-sticker-animated-play' is set
(let ((sticker (or (telega--tl-get msg :content :sticker)
(telega--tl-get msg :content :gift :sticker)
(telega--tl-get msg :content :link_preview
:type :sticker))))
(when (and (not (telega-sticker-static-p sticker))
telega-sticker-animated-play)
(telega-sticker--animate sticker))))
(t
(telega-msg-open-content msg))))))
(defun telega-autoplay-on-msg--hover-out (msg)
"Handle hover leaving for the message MSG.
Cancel downloading of the corresporting file."
(when (telega-msg-match-p msg telega-autoplay-msg-temex)
(when-let ((file (telega-msg--content-file msg)))
(telega--cancelDownloadFile file))))
;;;###autoload
(define-minor-mode telega-autoplay-mode
"Automatically play animation messages."
:init-value nil :global t :group 'telega-modes
(if telega-autoplay-mode
(progn
(add-hook 'telega-chat-post-message-hook #'telega-autoplay-on-msg)
(add-hook 'telega-msg-hover-in-hook #'telega-autoplay-on-msg)
(add-hook 'telega-msg-hover-out-hook #'telega-autoplay-on-msg--hover-out))
(remove-hook 'telega-msg-hover-out-hook #'telega-autoplay-on-msg--hover-out)
(remove-hook 'telega-msg-hover-in-hook #'telega-autoplay-on-msg)
(remove-hook 'telega-chat-post-message-hook #'telega-autoplay-on-msg)))
;;; ellit-org: minor-modes
;; ** telega-squash-message-mode
;;
;; Minor mode for chatbuf to squash messages into single one while
;; nobody saw this.
;;
;; Squashing mean adding contents of the new message to the previous
;; message by editing contents of the previous message.
;;
;; New message in a chat is squashed into your previous message only
;; if all next conditions are met:
;;
;; 1. Last message in chat is sent by you
;; 2. Nobody seen your last message
;; 3. Last and new message are both text messages
;; 4. Last message can be edited
;; 5. Last and new messages are *not* replying to any message
;; 6. Last message has no associated link preview
;; 7. New message has no ~messageSendOptions~ to avoid squashing
;; scheduled messages or similar
;; 8. New message is sent within ~telega-squash-message-within-seconds~
;; seconds from last message
;; Can be enabled globally in all chats matching
;; ~telega-squash-message-mode-for~ (see below) chat filter with
;; ~(global-telega-squash-message-mode 1)~ or by adding:
;;
;; #+begin_src emacs-lisp
;; (add-hook 'telega-load-hook 'global-telega-squash-message-mode)
;; #+end_src
;;
;; Customizable options:
;;
;; - {{{user-option(telega-squash-message-mode-for, 2)}}}
;; - {{{user-option(telega-squash-message-within-seconds, 2)}}}
(defcustom telega-squash-message-mode-for
'(not (or saved-messages (type channel)))
"*Chat Temex for `global-telega-squash-message-mode'.
Global squash message mode enables message squashing only in
chats matching this chat temex."
:type 'telega-chat-temex
:group 'telega-modes)
(defcustom telega-squash-message-within-seconds 60
"Maximum number of seconds between last and new message to apply squashing.
If new message is sent later then this number of seconds, then
squashing is not applied."
:type 'integer
:group 'telega-modes)
(defvar telega-squash-message-mode-lighter
(concat " " (telega-symbol 'mode) "Squash")
"Lighter for the `telega-squash-message-mode'.")
;;;###autoload
(define-minor-mode telega-squash-message-mode
"Toggle message squashing minor mode."
:init-value nil
:lighter telega-squash-message-mode-lighter
:group 'telega-modes
(if telega-squash-message-mode
(advice-add 'telega--sendMessage
:around 'telega-squash-message--send-message)
(advice-remove 'telega--sendMessage
'telega-squash-message--send-message)))
(defun telega-squash-message-mode--maybe (&optional arg)
(when (telega-chatbuf-match-p telega-squash-message-mode-for)
(telega-squash-message-mode arg)))
;;;###autoload
(define-minor-mode global-telega-squash-message-mode
"Global mode to squashing messages."
:init-value nil :global t :group 'telega-modes
(if global-telega-squash-message-mode
(progn
(add-hook 'telega-chat-mode-hook 'telega-squash-message-mode--maybe)
(dolist (buf (telega-chat-buffers))
(with-current-buffer buf
(telega-squash-message-mode--maybe 1))))
(remove-hook 'telega-chat-mode-hook 'telega-squash-message-mode--maybe)
(dolist (buf (telega-chat-buffers))
(with-current-buffer buf
(telega-squash-message-mode -1)))))
(defun telega-squash-message--squash (chat imc reply-to-msg options)
"Return non-nil if message has been squashed."
(with-telega-chatbuf chat
(when (and telega-squash-message-mode
;; Check 3., 5. and 7.0 for new message
(not reply-to-msg)
(not options)
(eq (telega--tl-type imc) 'inputMessageText))
(let ((last-msg (plist-get chat :last_message))
(last-read-id (plist-get chat :last_read_outbox_message_id)))
(when (and last-msg
;; Checking for 1. 2. 3. 4. and 5.
(telega-msg-by-me-p last-msg)
(< last-read-id (plist-get last-msg :id))
(telega-msg-match-p last-msg
'(and (prop :can_be_edited)
(not is-reply-to-msg)
(not is-reply-to-story)
(type Text)))
;; Check for 6.
(not (telega--tl-get last-msg :content :link_preview))
;; Check for 8.
(< (- (telega-time-seconds)
(if (zerop (plist-get last-msg :edit_date))
(plist-get last-msg :date)
(plist-get last-msg :edit_date)))
telega-squash-message-within-seconds)
)
;; Squashing IMC with `last-msg' by modifying IMC inplace
(plist-put imc :text (telega-fmt-text-desurrogate
(telega-fmt-text-concat
(telega--tl-get last-msg :content :text)
(telega-string-fmt-text "\n")
(plist-get imc :text))))
(telega--editMessageText last-msg imc)
t)))))
(defun telega-squash-message--send-message (send-msg-fun chat imc &optional reply-to-msg options &rest args)
"Advice for `telega--sendMessage' used to squash messages."
(unless (telega-squash-message--squash chat imc reply-to-msg options)
(apply send-msg-fun chat imc reply-to-msg options args)))
;;; ellit-org: minor-modes
;; ** telega-image-mode
;;
;; Major mode to view images in chatbuf. Same as ~image-mode~,
;; however has special bindings:
;;
;; - {{{where-is(telega-image-next,telega-image-mode-map)}}} ::
;; {{{fundoc(telega-image-next)}}}
;;
;; - {{{where-is(telega-image-prev,telega-image-mode-map)}}} ::
;; {{{fundoc(telega-image-prev)}}}
;;
;; To view high resolution image in chatbuf with ~telega-image-mode~
;; press {{{kbd(RET)}}} on the message with photo.
(require 'image-mode)
(declare-function telega-chat-title "telega-chat" (chat &optional no-badges))
(declare-function telega-chatbuf--next-msg "telega-chat" (msg msg-temex &optional backward))
(declare-function telega-chatbuf--goto-msg "telega-chat" (msg-id &optional highlight callback))
(defcustom telega-image-mode-mode-line-format
'(" → "
(:eval (telega-image-mode-mode-line-chat-title))
" "
(:eval (telega-image-mode-mode-line-chat-position)))
"Format for the modeline."
:type 'sexp
:group 'telega-modes)
(defvar telega-image--message nil
"Message corresponding to image currently viewed.")
(make-variable-buffer-local 'telega-image--message)
(defvar telega-image--position (cons nil nil)
"Position of the image message in the chat's history
To be displayed in the modeline.")
(make-variable-buffer-local 'telega-image--position)
(defun telega-image-mode-mode-line-chat-title ()
(substring-no-properties
(telega-chatbuf--name (telega-msg-chat telega-image--message))))
(defun telega-image-mode-mode-line-chat-position ()
(when (and (car telega-image--position)
(cdr telega-image--position))
(format "[%d/%d]"
(car telega-image--position)
(cdr telega-image--position))))
(defun telega-image-mode--update-modeline ()
(setq mode-line-buffer-identification
(list (propertized-buffer-identification "%b")
(telega-format-mode-line telega-image-mode-mode-line-format)))
(force-mode-line-update))
(defun telega-image-mode--chat-position-fetch ()
"Asynchronously fetch message's position in the chat's history."
(telega--getChatMessageCount (telega-msg-chat telega-image--message)
'(:@type "searchMessagesFilterPhoto") nil
(lambda-with-current-buffer (count)
(setcdr telega-image--position count)
(when (car telega-image--position)
(telega-image-mode--update-modeline))))
(telega--getChatMessagePosition
telega-image--message '(:@type "searchMessagesFilterPhoto")
:callback
(lambda-with-current-buffer (count)
(setcar telega-image--position count)
(when (cdr telega-image--position)
(telega-image-mode--update-modeline)))))
(defvar telega-image-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map image-mode-map)
(define-key map "n" 'telega-image-next)
(define-key map "p" 'telega-image-prev)
(define-key map "q" 'telega-image-quit)
map))
(define-derived-mode telega-image-mode image-mode nil
"Major mode to view images from chat buffer."
(setq mode-name
(concat (telega-symbol 'mode) "Image"
(when image-type (format "[%s]" image-type))))
)
(defun telega-image-mode-p (buffer-name &rest _unused)
"Return non-nil if buffer named BUFFER-NAME has `telega-image-mode' major-mode.
Could be used as condition function in `display-buffer-alist'."
(with-current-buffer buffer-name
(eq major-mode 'telega-image-mode)))
(defun telega-image-view-file (tl-file &optional for-msg)
"View image in telegram TL-FILE from message FOR-MSG."
(cl-assert (telega-file--downloaded-p tl-file))
;; NOTE: Use `pop-to-buffer' so `display-buffer-alist' is considered
;; when showing the image
(pop-to-buffer-same-window
(with-current-buffer (find-file-noselect
(telega--tl-get tl-file :local :path) nil t)
(telega-image-mode)
(setq telega-image--message for-msg)
(telega-image-mode--update-modeline)
;; NOTE: fetch chat position only for "Photo" messages, to avoid
;; 400 errors
(when (and for-msg (telega-msg-match-p for-msg '(type Photo)))
(telega-image-mode--chat-position-fetch))
(current-buffer))))
(defun telega-image-view-msg (image-msg)
"View image associated with the IMAGE-MSG message."
;; Download highres photo
(let* ((photo (telega--tl-get image-msg :content :photo))
(hr (telega-photo--highres photo))
(hr-file (telega-file--renew hr :photo))
(oldbuffer (current-buffer)))
;; Jump to corresponding message in the chatbuf
(with-telega-chatbuf (telega-msg-chat image-msg)
;; (telega-msg-goto image-msg)))
(telega-chatbuf--history-state-set
:newer-freezed (list :at-msg image-msg)))
(telega-file--download hr-file
:priority 32
:update-callback
(lambda (tl-file)
;; Show downloading progress in modeline
(let ((progress (telega-file--downloading-progress tl-file)))
(message "telega: %s%d%%" (telega-i18n "telega_loading")
(* progress 100)))
;; TL-FILE Downloaded
(when (telega-file--downloaded-p tl-file)
(message "")
(telega-image-view-file tl-file image-msg)
(ignore-errors
(kill-buffer oldbuffer)))))))
(defun telega-image-next (&optional backward)
"Show next image in chat."
(interactive "P")
(unless telega-image--message
(user-error "No telega message associated with the image"))
(if-let ((next-image-msg (telega-chatbuf--next-msg telega-image--message
'(type Photo) backward)))
(telega-image-view-msg next-image-msg)
;; `next-image-msg' is nil (not found)
;; Need to fetch older/newer messages from the chat's history
(message "telega: %s" (telega-i18n "telega_loading"))
(let ((chat (telega-msg-chat telega-image--message))
(img-msg telega-image--message)
(img-buffer (current-buffer)))
(telega--searchChatMessages chat
'(:@type "searchMessagesFilterPhoto")
(plist-get telega-image--message :id)
(if backward 0 -2)
:limit 3
:callback
(lambda (reply)
(let ((found-messages (append (plist-get reply :messages) nil)))
(if (or (telega--tl-error-p reply)
(null found-messages)
(telega-msg-id= img-msg (car found-messages)))
(message "telega: No %s image in the %s"
(if backward "previous" "next")
(telega-ins--as-string
(telega-ins--msg-sender chat
:with-avatar-p t
:with-username-p t
:with-brackets-p t)))
;; Found a message
(message "")
(when (buffer-live-p img-buffer)
(with-current-buffer img-buffer
(telega-image-view-msg (car found-messages))))
)))
))))
(defun telega-image-prev ()
"Show previous image in chat."
(interactive)
(telega-image-next 'previous))
(defun telega-image-quit ()
"Kill image buffer and its window."
(interactive)
(quit-window 'kill))
;;; ellit-org: minor-modes
;; ** telega-edit-file-mode
;;
;; {{{fundoc1(telega-edit-file-mode)}}}
;; In this mode {{{kbd(C-x C-s)}}} will save file to Telegram cloud.
;; To enable ~telega-edit-file-mode~ for files opened from message
;; with {{{kbd(RET)}}}, use:
;;
;; #+BEGIN_SRC emacs-lisp
;; (add-hook 'telega-open-file-hook 'telega-edit-file-mode)
;; #+END_SRC
;;
;; While editing file press
;; {{{where-is(telega-edit-file-goto-message,telega-edit-file-mode-map)}}}
;; to go back to the file's message.
;;
;; To switch between files opened from =telega= use
;; {{{where-is(telega-edit-file-switch-buffer,telega-prefix-map)}}}
;; binding from the [[#telega-prefix-map][Telega prefix map]].
;;
;; To distinguish files opened from =telega= with ordinary files
;; suffix is added to the buffer name. You can modify this suffix
;; using user option:
;;
;; - {{{user-option(telega-edit-file-buffer-name-function, 2)}}}
(declare-function telega-chatbuf--gen-input-file "telega-chat" (filename &optional file-type preview-p upload-callback))
(defcustom telega-edit-file-buffer-name-function 'telega-edit-file-buffer-name
"Function to return buffer name when `telega-edit-file-mode' is enabled.
Function is called without arguments and should return a buffer name string.
Inside a function you can use `telega-edit-file-message<f>' to
get message associated with the file."
:package-version '(telega . "0.7.59")
:type 'function
:group 'telega-modes)
(defvar telega-edit-file-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap save-buffer] 'telega-edit-file-save-buffer)
(define-key map (kbd "M-g t") 'telega-edit-file-goto-message)
(define-key map [menu-bar goto-message]
'("Goto Telegram Message" . telega-edit-file-goto-message))
(define-key map [menu-bar turn-off]
'("Turn off minor mode" . telega-edit-file-mode))
map))
(defun telega-edit-file-message ()
"Return message for the currently edited file with `telega-edit-file-mode'."
(cl-assert telega-edit-file-mode)
telega--help-win-param)
(defun telega-edit-file-buffer-name ()
"Return buffer name for a file edited with `telega-edit-file-mode'."
(concat (buffer-name) (telega-symbol 'mode)
(telega-chatbuf--name
(telega-msg-chat (telega-edit-file-message)))))
(defvar telega-edit-file-mode-lighter
(concat " " (telega-symbol 'mode) "Edit")
"Lighter for the `telega-edit-file-mode'.")
;;;###autoload
(define-minor-mode telega-edit-file-mode
"Minor mode to edit files from Telegram messages.
Can be enabled only for content from editable messages."
:lighter telega-edit-file-mode-lighter
:map 'telega-edit-file-mode-map
(if telega-edit-file-mode
(let ((msg telega--help-win-param))
(if (not (plist-get msg :can_be_edited))
(progn
;; No message or message can't be edited
(telega-edit-file-mode -1)
(read-only-mode 1)
(message (concat "telega: File opened in read-only-mode "
"since message is read only")))
;; Apply buffer name modification to distinguish files
;; opened from telega with ordinary files. Apply
;; `telega-edit-file-buffer-name-function' only once, by
;; marking buffer name with special text property
(when telega-edit-file-buffer-name-function
(let ((old-bufname (buffer-name))
(new-bufname (funcall telega-edit-file-buffer-name-function)))
(unless (get-text-property 0 :telega-buffer-name old-bufname)
(rename-buffer
(propertize new-bufname :telega-buffer-name t)))))
(setq mode-line-buffer-identification