forked from alphapapa/ement.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ement-room.el
5792 lines (5335 loc) · 299 KB
/
ement-room.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
863
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
;;; ement-room.el --- Ement room buffers -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2023 Free Software Foundation, Inc.
;; Author: Adam Porter <[email protected]>
;; Maintainer: Adam Porter <[email protected]>
;; 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 of the License, 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. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This library implements buffers displaying events in a room.
;; EWOC is a great library. If I had known about it and learned it
;; sooner, it would have saved me a lot of time in other projects.
;; I'm glad I decided to try it for this one.
;;; Code:
;;;; Debugging
;; NOTE: Uncomment this form and `emacs-lisp-byte-compile-and-load' the file to enable
;; `ement-debug' messages. This is commented out by default because, even though the
;; messages are only displayed when `warning-minimum-log-level' is `:debug' at runtime, if
;; that is so at expansion time, the expanded macro calls format the message and check the
;; log level at runtime, which is not zero-cost.
;; (eval-and-compile
;; (setq-local warning-minimum-log-level nil)
;; (setq-local warning-minimum-log-level :debug))
;;;; Requirements
(require 'color)
(require 'ewoc)
(require 'mailcap)
(require 'shr)
(require 'subr-x)
(require 'mwheel)
(require 'dnd)
(require 'ement-api)
(require 'ement-lib)
(require 'ement-macros)
(require 'ement-structs)
;;;; Structs
(cl-defstruct ement-room-membership-events
"Struct grouping membership events.
After adding events, use `ement-room-membership-events--update'
to sort events and update other slots."
(events nil :documentation "Membership events, latest first.")
(earliest-ts nil :documentation "Timestamp of earliest event.")
(latest-ts nil :documentation "Timestamp of latest event."))
(defun ement-room-membership-events--update (struct)
"Return STRUCT having sorted its events and updated its slots."
;; Like the room timeline slot, events are sorted latest-first. We also deduplicate
;; them , because it seems that we can end up with multiple copies of a membership event
;; (e.g. when loading old messages).
(setf (ement-room-membership-events-events struct) (cl-delete-duplicates (ement-room-membership-events-events struct)
:key #'ement-event-id :test #'equal)
(ement-room-membership-events-events struct) (cl-sort (ement-room-membership-events-events struct) #'>
:key #'ement-event-origin-server-ts)
(ement-room-membership-events-earliest-ts struct) (ement-event-origin-server-ts
(car (last (ement-room-membership-events-events struct))))
(ement-room-membership-events-latest-ts struct) (ement-event-origin-server-ts
(car (ement-room-membership-events-events struct))))
struct)
;;;; Variables
(defvar-local ement-ewoc nil
"EWOC for Ement room buffers.")
(defvar-local ement-room nil
"Ement room for current buffer.")
(defvar-local ement-session nil
"Ement session for current buffer.")
;; TODO: Convert some of these buffer-local variables into keys in one buffer-local map variable.
(defvar-local ement-room-retro-loading nil
"Non-nil when earlier messages are being loaded.
Used to avoid overlapping requests.")
(defvar-local ement-room-editing-event nil
"When non-nil, the user is editing this event.
Used by `ement-room-send-message'.")
(defvar-local ement-room-replying-to-event nil
"When non-nil, the user is replying to this event.
Used by `ement-room-send-message'.")
(defvar-local ement-room-replying-to-overlay nil
"Used by `ement-room-write-reply'.")
(defvar-local ement-room-read-receipt-request nil
"Maps event ID to request updating read receipt to that event.
An alist of one entry.")
(defvar ement-room-read-string-setup-hook nil
"Normal hook run by `ement-room-read-string' after switching to minibuffer.
Should be used to, e.g. propagate variables to the minibuffer.")
(defvar ement-room-compose-hook nil
"Hook run in compose buffers when created.
Used to, e.g. call `ement-room-compose-org'.")
(declare-function ement-room-list "ement-room-list.el")
(declare-function ement-notify-switch-to-mentions-buffer "ement-notify")
(declare-function ement-notify-switch-to-notifications-buffer "ement-notify")
(defvar ement-room-mode-self-insert-keymap (make-sparse-keymap)
"The `ement-room-mode' keymap under `ement-room-self-insert-mode'.
Set as the parent keymap of `ement-room-mode-effective-keymap'
when `ement-room-self-insert-mode' is enabled.
This keymap is derived from the `ement-room-self-insert-chars'
and `ement-room-self-insert-commands' user options, along with
`ement-room-mode-map-prefix-key' which provides access to the
full `ement-room-mode-map'. (Non-conflicting key bindings from
`ement-room-mode-map' are also available directly).
This keymap is generated when `ement-room-self-insert-mode' is
enabled, and after customizing any of the above options when the
minor mode is enabled.
The hook `ement-room-mode-self-insert-keymap-update-hook' runs
after generating this keymap.
Note: Emacs bug#66792 may cause `describe-keymap' to include
unreachable key bindings from the parent `ement-room-mode-map' in
its help output. This problem affects only the help, and we work
around it for the `ement-room-mode' help; but when viewing the
keymap directly the issue may be visible.")
(defvar ement-room-mode-map
(let ((map (make-sparse-keymap))
(prefixes '(("M-g" . "group:switching")
("s" . "group:messages")
("u" . "group:users")
("r" . "group:room")
("R" . "group:membership"))))
;; Use symbols for prefix maps so that `which-key' can display their names.
(dolist (prefix prefixes)
(let ((cmd (define-prefix-command (make-symbol (cdr prefix)))))
(define-key map (kbd (car prefix)) cmd)))
;; Menu
(define-key map (kbd "?") #'ement-room-transient)
;; Movement
(define-key map (kbd "n") #'ement-room-goto-next)
(define-key map (kbd "N") #'end-of-buffer)
(define-key map (kbd "p") #'ement-room-goto-prev)
(define-key map (kbd "SPC") #'ement-room-scroll-up-mark-read)
(define-key map (kbd "S-SPC") #'ement-room-scroll-down-command)
(define-key map (kbd "M-g M-p") #'ement-room-goto-fully-read-marker)
(define-key map (kbd "m") #'ement-room-mark-read)
(define-key map [remap scroll-down-command] #'ement-room-scroll-down-command)
(define-key map [remap mwheel-scroll] #'ement-room-mwheel-scroll)
(define-key map (kbd "<tab>") #'forward-button)
(define-key map (kbd "<backtab>") #'backward-button)
;; Switching
(define-key map (kbd "M-g M-l") #'ement-room-list)
(define-key map (kbd "M-g M-r") #'ement-view-room)
(define-key map (kbd "M-g M-m") #'ement-notify-switch-to-mentions-buffer)
(define-key map (kbd "M-g M-n") #'ement-notify-switch-to-notifications-buffer)
(define-key map (kbd "q") #'quit-window)
;; Messages
(define-key map (kbd "RET") #'ement-room-dispatch-new-message)
(define-key map (kbd "M-RET") #'ement-room-dispatch-new-message-alt)
(define-key map (kbd "S-<return>") #'ement-room-dispatch-reply-to-message)
(define-key map (kbd "<insert>") #'ement-room-dispatch-edit-message)
(define-key map (kbd "C-k") #'ement-room-delete-message)
(define-key map (kbd "s r") #'ement-room-send-reaction)
(define-key map (kbd "s e") #'ement-room-send-emote)
(define-key map (kbd "s f") #'ement-room-send-file)
(define-key map (kbd "s i") #'ement-room-send-image)
(define-key map (kbd "v") #'ement-room-view-event)
;; Users
(define-key map (kbd "u RET") #'ement-send-direct-message)
(define-key map (kbd "u i") #'ement-invite-user)
(define-key map (kbd "u I") #'ement-ignore-user)
;; Room
(define-key map (kbd "M-s o") #'ement-room-occur)
(define-key map (kbd "r d") #'ement-describe-room)
(define-key map (kbd "r m") #'ement-list-members)
(define-key map (kbd "r t") #'ement-room-set-topic)
(define-key map (kbd "r f") #'ement-room-set-message-format)
(define-key map (kbd "r n") #'ement-room-set-notification-state)
(define-key map (kbd "r N") #'ement-room-override-name)
(define-key map (kbd "r T") #'ement-tag-room)
;; Room membership
(define-key map (kbd "R c") #'ement-create-room)
(define-key map (kbd "R j") #'ement-join-room)
(define-key map (kbd "R l") #'ement-leave-room)
(define-key map (kbd "R F") #'ement-forget-room)
(define-key map (kbd "R n") #'ement-room-set-display-name)
(define-key map (kbd "R s") #'ement-room-toggle-space)
;; Other
(define-key map (kbd "g") #'ement-room-sync)
map)
"Keymap for Ement room buffers.")
(defvar ement-room-mode-effective-keymap
(let ((map (make-sparse-keymap)))
(set-keymap-parent map ement-room-mode-map)
map)
"The actual keymap used in `ement-room-mode'.
This keymap reflects the state of `ement-room-self-insert-mode',
with a parent of `ement-room-mode-map' when the mode is disabled,
or `ement-room-mode-self-insert-keymap' when the mode is enabled.")
(defvar ement-room-mode--advertised-keymap ement-room-mode-map
"The keymap advertised by `ement-room-mode'.
This keymap should represent the functional behaviour of
`ement-room-mode-effective-keymap' without the confusion arising
from Emacs bug#66792 on account of the effective keymap having
`ement-room-mode-map' as a parent if `ement-room-self-insert-mode'
is enabled.
Because it does not always have `ement-room-mode-map' as a
parent, it is possible for that map to get out of sync with the
advertised map, but `ement-room-mode-self-insert-keymap-update'
makes a best effort to keep it accurate.")
(defvar ement-room-minibuffer-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map (kbd "C-c '") #'ement-room-compose-from-minibuffer)
map)
"Keymap used in `ement-room-read-string'.")
(defvar ement-room-reaction-map
(let ((map (make-sparse-keymap)))
(define-key map "c" #'insert-char)
(when (commandp 'emoji-insert)
(define-key map "i" 'emoji-insert))
(when (commandp 'emoji-search)
(define-key map "s" 'emoji-search))
(when (assoc "emoji" input-method-alist)
(define-key map "m" 'ement-room-use-emoji-input-method))
map)
"Keymap used in `ement-room-send-reaction'.")
(defvar ement-room-sender-in-headers nil
"Non-nil when sender is displayed in headers.
In that case, sender names are aligned to the margin edge.")
(defvar ement-room-messages-filter
'((lazy_load_members . t))
;; NOTE: The confusing differences between what /sync and /messages
;; expect. See <https://github.com/matrix-org/matrix-doc/issues/706>.
"Default RoomEventFilter for /messages requests.")
(defvar ement-room-typing-timer nil
"Timer used to send notifications while typing.")
(defvar ement-room-matrix.to-url-regexp
(rx "http" (optional "s") "://"
"matrix.to" "/#/"
(group (or "!" "#") (1 (not (any "/"))))
(optional "/" (group "$" (1 (not (any "?" "/")))))
(optional "?" (group (1 anything))))
"Regexp matching \"matrix.to\" URLs.")
(defvar ement-room-message-history nil
"History list of messages entered with `ement-room' commands.
Does not include filenames, emotes, etc.")
(defvar ement-room-emote-history nil
"History list of emotes entered with `ement-room' commands.")
;; Variables from other files.
(defvar ement-sessions)
(defvar ement-syncs)
(defvar ement-auto-sync)
(defvar ement-users)
(defvar ement-images-queue)
(defvar ement-notify-limit-room-name-width)
(defvar ement-view-room-display-buffer-action)
;; Defined in Emacs 28.1: silence byte-compilation warning in earlier versions.
(defvar browse-url-handlers)
;;;; Customization
(defgroup ement-room nil
"Options for room buffers."
:group 'ement)
(defcustom ement-room-timestamp-header-align 'right
"Where to align timestamp headers."
:type '(choice (const :tag "Left" left)
(const :tag "Center" center)
(const :tag "Right" right)))
(defcustom ement-room-view-hook
'(ement-room-view-hook-room-list-auto-update)
"Functions called when `ement-room-view' is called.
Called with two arguments, the room and the session."
:type 'hook)
(defcustom ement-room-reaction-names-limit 3
"Up to this many users, show a reaction's senders' names.
If more than this many users have sent a reaction, show the
number of senders instead (and the names in a tooltip)."
:type 'natnum)
;;;;; Faces
(defface ement-room-name
'((t (:inherit font-lock-function-name-face)))
"Room name shown in header line.")
(defface ement-room-membership
'((t (:height 0.8 :inherit font-lock-comment-face)))
"Membership events (join/part).")
(defface ement-room-reactions
'((t (:inherit font-lock-comment-face :height 0.9)))
"Reactions to messages (including the user count).")
(defface ement-room-reactions-key
'((t (:inherit ement-room-reactions :height 1.5)))
"Reactions to messages (the key, i.e. the emoji part).
Uses a separate face to allow the key to be shown at a different
size, because in some fonts, emojis are too small relative to
normal text.")
(defface ement-room-timestamp
'((t (:inherit font-lock-comment-face)))
"Event timestamps.")
(defface ement-room-user
'((t (:inherit font-lock-function-name-face :weight bold :overline t)))
"Usernames.")
(defface ement-room-self
'((t (:inherit (font-lock-variable-name-face ement-room-user) :weight bold)))
"Own username.")
(defface ement-room-message-text
'((t (:inherit default)))
"Text message bodies.")
(defface ement-room-message-emote
'((t (:inherit italic)))
"Emote message bodies.")
(defface ement-room-quote
'((t (:height 0.9 :inherit font-lock-comment-face)))
"Quoted parts of messages.
Anything wrapped by HTML BLOCKQUOTE tag.")
(defface ement-room-redacted
'((t (:strike-through t)))
"Redacted messages.")
(defface ement-room-self-message
'((t (:inherit (font-lock-variable-name-face))))
"Oneself's message bodies.
Note that this does not need to inherit
`ement-room-message-text', because that face is combined with
this one automatically.")
(defface ement-room-timestamp-header
'((t (:inherit header-line :weight bold :height 1.1)))
"Timestamp headers.")
(defface ement-room-mention
;; TODO(30.1): Remove when not supporting Emacs 27 anymore.
(if (version< emacs-version "27.1")
'((t (:inherit hl-line)))
'((t (:inherit hl-line :extend t))))
"Messages that mention the local user.")
(defface ement-room-wrap-prefix
`((t :inherit highlight))
"Face applied to `ement-room-wrap-prefix', which see.")
;;;;; Options
(defcustom ement-room-ellipsis "⋮"
"String used when abbreviating certain strings."
:type 'string)
(defcustom ement-room-avatars (display-images-p)
"Show room avatars."
:type 'boolean)
(defcustom ement-room-avatar-max-width 32
"Maximum width in pixels of room avatars shown in header lines."
:type 'integer)
(defcustom ement-room-avatar-max-height 32
"Maximum height in pixels of room avatars shown in header lines."
:type 'integer)
(defcustom ement-room-coalesce-events t
"Coalesce certain events in room buffers.
For example, membership events can be overwhelming in large
rooms, especially ones bridged to IRC. This option groups them
together so they take less space."
:type 'boolean)
(defcustom ement-room-header-line-format
;; TODO: Show in new screenshots.
'(:eval (concat (if ement-room-avatars
(or (ement-room-avatar ement-room)
"")
"")
" " (propertize (ement-room--escape-%
(or (ement-room-display-name ement-room)
"[no room name]"))
'face 'ement-room-name)
": " (propertize (ement-room--escape-%
(or (ement-room-topic ement-room)
"[no topic]"))
;; Also set help-echo in case the topic is too wide to fit.
'help-echo (ement-room-topic ement-room))))
"Header line format for room buffers.
See Info node `(elisp)Header lines'."
:type 'sexp)
(put 'ement-room-header-line-format 'risky-local-variable t)
(defcustom ement-room-buffer-name-prefix "*Ement Room: "
"Prefix for Ement room buffer names."
:type 'string)
(defcustom ement-room-buffer-name-suffix "*"
"Suffix for Ement room buffer names."
:type 'string)
(defcustom ement-room-timestamp-format "%H:%M:%S"
"Format string for event timestamps.
See function `format-time-string'."
:type '(choice (const "%H:%M:%S")
(const "%Y-%m-%d %H:%M:%S")
string))
(defcustom ement-room-left-margin-width 0
"Width of left margin in room buffers.
When using a non-graphical display, this should be set slightly
wider than when using a graphical display, to prevent sender
display names from colliding with event text."
:type 'integer)
(defcustom ement-room-right-margin-width (length ement-room-timestamp-format)
"Width of right margin in room buffers."
:type 'integer)
(defcustom ement-room-sender-headers t
"Show sender headers.
Automatically set by setting `ement-room-message-format-spec',
but may be overridden manually."
:type 'boolean)
(defcustom ement-room-unread-only-counts-notifications t
"Only use notification counts to mark rooms unread.
Notification counts are set by the server based on each room's
notification settings. Otherwise, whether a room is marked
unread depends on the room's fully-read marker, read-receipt
marker, whether the local user sent the latest events, etc."
:type 'boolean)
(defcustom ement-room-compose-method 'minibuffer
"How to compose messages.
The value `minibuffer' means the minibuffer will be used to write
and edit messages. You can use \
\\<ement-room-minibuffer-map>\\[ement-room-compose-from-minibuffer] \
to switch from the minibuffer
to a separate compose buffer, and \\[save-buffer] in the compose buffer
will then return you to the minibuffer to confirm the message
before sending.
The value `compose-buffer' means that the minibuffer is not used --
messages are written in a compose buffer by default, and \\[save-buffer]
sends the composed message directly."
:type '(choice (const :tag "Minibuffer" minibuffer)
(const :tag "Compose buffer" compose-buffer)))
(defcustom ement-room-compose-buffer-display-action
(cons 'display-buffer-below-selected
'((window-height . 3)
(inhibit-same-window . t)
(reusable-frames . nil)))
"`display-buffer' action for displaying compose buffers.
See also option `ement-room-compose-buffer-window-auto-height'
and `ement-room-compose-buffer-window-dedicated'."
:type display-buffer--action-custom-type
:risky t)
(defcustom ement-room-compose-buffer-window-dedicated 'created
"Whether windows for compose buffers should be dedicated.
A dedicated compose buffer window will not be used to display any
other buffer, and will be deleted once the message has been sent
or aborted (see `ement-room-compose-buffer-quit-restore-window').
The values t and nil mean \"always\" and \"never\" respectively.
The value `created' means newly-created windows are dedicated.
\(The default `ement-room-compose-buffer-display-action' always
creates a new window.)
The value `auto-height' means that windows will be dedicated if
the option `ement-room-compose-buffer-window-auto-height' is
enabled (this option generally keeps the windows too small to
usefully display other buffers).
The value `delete' means that windows will not be dedicated, but
they will still be deleted once the message is sent or aborted
\(even when they have also been used to display other buffers).
See also `set-window-dedicated-p' and
`switch-to-buffer-in-dedicated-window'."
:type '(radio (const :tag "Always" t)
(const :tag "Never" nil)
(const :tag "Never (but always delete window)" delete)
(const :tag "Newly-created windows" created)
(const :tag "When auto-height enabled" auto-height)))
(defcustom ement-room-compose-buffer-window-auto-height t
"Dynamically match the compose buffer window height to its contents.
See also `ement-room-compose-buffer-window-auto-height-max' and
`ement-room-compose-buffer-window-auto-height-min'."
:type 'boolean)
;; Experimental. Disabled by default. Set to 'height to use this.
(defvar ement-room-compose-buffer-window-auto-height-fixed nil
"The buffer-local `window-size-fixed' value in compose buffers.")
(defvar ement-room-compose-buffer-window-auto-height-pixelwise t
"Whether to adjust the window height for pixel-precise lines.")
;; This is a mutex to ensure that auto-height resizing cannot trigger itself
;; recursively. This may prevent desirable resizing in certain cases, but we
;; get the correct result in the majority of situations, and it is simple.
(defvar ement-room-compose-buffer-window-auto-height-resizing-p)
(defcustom ement-room-compose-buffer-window-auto-height-min nil
"If non-nil, limits the body height of the compose buffer window.
See also option `ement-room-compose-buffer-window-auto-height'
and `ement-room-compose-buffer-window-auto-height-max'."
:type '(choice (const :tag "Default" nil)
(natnum :tag "Lines")))
(defcustom ement-room-compose-buffer-window-auto-height-max nil
"If non-nil, limits the body height of the compose buffer window.
See also option `ement-room-compose-buffer-window-auto-height'
and `ement-room-compose-buffer-window-auto-height-min'."
:type '(choice (const :tag "Default" nil)
(natnum :tag "Lines")))
(defcustom ement-room-mode-self-insert-keymap-update-hook nil
"Hook run after rebuilding `ement-room-mode-self-insert-keymap'.
This happens at the time `ement-room-self-insert-mode' is
enabled, and also if user options `ement-room-self-insert-chars',
`ement-room-self-insert-commands', or
`ement-room-mode-map-prefix-key' are customized while the mode is
enabled.
You can use this hook to define any desired custom bindings which
are not accounted for by those user options."
:type 'hook)
(defvar ement-room-self-insert-mode)
(defvar ement-room-self-insert-chars)
(defvar ement-room-self-insert-commands)
(defun ement-room-mode-self-insert-keymap-update ()
"Rebuilds `ement-room-mode-self-insert-keymap'.
Also rebuilds `ement-room-mode--advertised-keymap'."
;; Must be defined ahead of `ement-room-self-insert-option-setter'.
(let ((map (make-sparse-keymap)))
;; Ensure that `ement-room-self-insert-chars' start a message.
(dolist (range ement-room-self-insert-chars)
(if (consp range)
;; Process a range the same way that `global-map' does.
(let ((vec1 (make-vector 1 nil))
(from (car range))
(to (cdr range)))
(while (<= from to)
(aset vec1 0 from)
(define-key map vec1 #'ement-room-self-insert-new-message)
(setq from (1 from))))
;; Else `range' is a single character.
(define-key map (vector range) #'ement-room-self-insert-new-message)))
;; Provide access to `ement-room-mode-map' via a prefix binding.
(when (bound-and-true-p ement-room-mode-map-prefix-key)
(define-key map ement-room-mode-map-prefix-key ement-room-mode-map))
;; This is now the basis for `ement-room-mode-self-insert-keymap' and also
;; `ement-room-mode--advertised-keymap' (when `ement-room-self-insert-mode'
;; is enabled), but we need to keep the remaining differences between them
;; separate. (We do still need some identical `remap' bindings for both
;; keymaps, but we can't do that just yet.)
(setq ement-room-mode-self-insert-keymap (copy-keymap map))
;; To `ement-room-mode-self-insert-keymap', add `ement-room-mode-map'
;; as the keymap parent. (This is the keymap which is actually used.)
(set-keymap-parent ement-room-mode-self-insert-keymap ement-room-mode-map)
(if (not (bound-and-true-p ement-room-self-insert-mode))
;; Advertise the real `ement-room-mode-map'.
(setq ement-room-mode--advertised-keymap ement-room-mode-map)
;; Otherwise we base `ement-room-mode--advertised-keymap' on the same base
;; map previously copied to `ement-room-mode-self-insert-keymap'.
(setq ement-room-mode--advertised-keymap map)
;; To `ement-room-mode--advertised-keymap' (the keymap displayed when
;; `describe-mode' is called), rather than setting a parent we instead
;; copy the non-conflicting top-level bindings from `ement-room-mode-map'.
;; Not using a keymap parent means the advertised map doesn't see any
;; future changes to `ement-room-mode-map', but having a keymap parent
;; would make the `describe-mode' output very confusing on account of
;; Emacs bug#66792, so we accept potential inaccuracy as a trade-off for
;; showing more comprehensible help.
;;
;; The following will copy the `remap' keymap verbatim, clobbering any
;; pre-existing remappings; so we do this before we define other
;; remappings.
(cl-labels ((copy-from (key definition)
(unless (lookup-key ement-room-mode--advertised-keymap
(vector key))
(define-key ement-room-mode--advertised-keymap
(vector key) definition))))
;; Copy from a copy of `ement-room-mode-map', otherwise the latter will
;; also acquire (share) the remap keybindings which are added below.
(map-keymap #'copy-from (copy-keymap ement-room-mode-map))))
;; Now define our additional `remap' bindings in both keymaps.
(let ((keymaps (if (bound-and-true-p ement-room-self-insert-mode)
(list ement-room-mode-self-insert-keymap
ement-room-mode--advertised-keymap)
(list ement-room-mode-self-insert-keymap))))
(dolist (keymap keymaps)
;; Make `self-insert-command' (and friends) start a new message.
(dolist (cmd ement-room-self-insert-commands)
(define-key keymap (vector 'remap cmd)
#'ement-room-self-insert-new-message)))))
(run-hooks 'ement-room-mode-self-insert-keymap-update-hook))
(defun ement-room-mode-effective-keymap-update ()
"Sets the parent keymap for `ement-room-mode-effective-keymap'.
Either `ement-room-mode-self-insert-keymap' or `ement-room-mode-map',
depending on `ement-room-self-insert-mode'."
;; Must be defined ahead of `ement-room-self-insert-option-setter'.
(set-keymap-parent ement-room-mode-effective-keymap
(if (bound-and-true-p ement-room-self-insert-mode)
ement-room-mode-self-insert-keymap
ement-room-mode-map)))
(defun ement-room-self-insert-option-setter (option value)
"Setter for options affecting `ement-room-self-insert-mode'.
This is the setter function for `ement-room-self-insert-chars'
and `ement-room-self-insert-commands'.
Sets the value with (set-default-toplevel-value OPTION VALUE),
and then rebuilds `ement-room-mode-self-insert-keymap'."
;; Must be defined ahead of `ement-room-self-insert-chars' and
;; `ement-room-self-insert-commands'.
;;
;; Update the variable.
(set-default-toplevel-value option value)
;; Update keymaps when necessary.
(when (bound-and-true-p ement-room-self-insert-mode)
(ement-room-mode-self-insert-keymap-update)
(ement-room-mode-effective-keymap-update)))
(defcustom ement-room-self-insert-chars
'((33 . 62) (64 . 126))
"Characters handled by `ement-room-self-insert-mode'.
These are in addition to any `self-insert-command' key bindings
-- this list is to ensure that certain keys will be treated this
way even when they have `ement-room-mode-map' bindings.
Cons cell elements represent the range from the car to the cdr
\(inclusive). The default value covers the common \"printable\"
ASCII characters excluding SPC (32), ? (63), and DEL (127).
Customizing this option updates `ement-room-mode-self-insert-keymap'
via the setter function `ement-room-self-insert-option-setter'.
To do the same in lisp code, set the option with `setopt'.
See also `ement-room-self-insert-commands'."
:type '(repeat (choice (character :tag "Character")
(cons :tag "Character range"
(character :tag "From")
(character :tag "To"))))
:set #'ement-room-self-insert-option-setter)
(defcustom ement-room-self-insert-commands
'(self-insert-command yank)
"Commands handled by `ement-room-self-insert-mode'.
When the mode is enabled, the listed commands are remapped to
`ement-room-self-insert-new-message' such that when one of those
commands is invoked in a room buffer, a new message will be
started and the event which triggered the command (typically a
`self-insert-command' key binding) will be re-issued in the
message buffer.
Customizing this option updates `ement-room-mode-self-insert-keymap'
via the setter function `ement-room-self-insert-option-setter'.
To do the same in lisp code, set the option with `setopt'.
See also `ement-room-self-insert-chars'."
:type '(repeat (function :tag "Command"))
:set #'ement-room-self-insert-option-setter)
(defcustom ement-room-mode-map-prefix-key (kbd "DEL")
"A prefix key sequence to access `ement-room-mode-map'.
Active when `ement-room-self-insert-mode' is enabled.
The default key is DEL.
Customizing this option updates `ement-room-mode-self-insert-keymap'
via the setter function `ement-room-self-insert-option-setter'.
To do the same in lisp code, set the option with `setopt'."
:type 'key-sequence
:set #'ement-room-self-insert-option-setter)
(defcustom ement-room-reaction-picker (if (commandp 'emoji-search)
'emoji-search
#'insert-char)
"Command used to select a reaction by `ement-room-send-reaction'.
Should be set to a command that somehow prompts the user for an
emoji and inserts it into the current buffer. In Emacs 29
reasonable choices include `emoji-insert' which uses a transient
interface, and `emoji-search' which uses `completing-read'. If
those are not available, one can use `insert-char'."
:type `(choice
(const :tag "Complete unicode character name" insert-char)
,@(when (commandp 'emoji-insert)
'((const :tag "Categorized emoji menu" emoji-insert)))
,@(when (commandp 'emoji-search)
'((const :tag "Complete emoji name" emoji-search)))
,@(when (assoc "emoji" input-method-alist)
'((const :tag "Emoji input method"
ement-room-use-emoji-input-method)))
(const :tag "Type an emoji without assistance" ignore)
(function :tag "Use other command")))
(defvar ement-room-sender-in-left-margin nil
"Whether sender is shown in left margin.
Set by `ement-room-message-format-spec-setter'.")
(defun ement-room-message-format-spec-setter (option value &optional local)
"Set relevant options for `ement-room-message-format-spec', which see.
To be used as that option's setter. OPTION and VALUE are
received from setting the customization option. If LOCAL is
non-nil, set the variables buffer-locally (i.e. when called from
`ement-room-set-message-format'."
(cl-macrolet ((set-vars (&rest pairs)
;; Set variable-value pairs, locally if LOCAL is non-nil.
`(progn
,@(cl-loop for (symbol value) on pairs by #'cddr
collect `(if local
(set (make-local-variable ',symbol) ,value)
(set ',symbol ,value))))))
(if local
(set (make-local-variable option) value)
(set-default option value))
(pcase value
;; Try to set the margin widths smartly.
("%B%r%R%t" ;; "Elemental"
(set-vars ement-room-left-margin-width 0
ement-room-right-margin-width 8
ement-room-sender-headers t
ement-room-sender-in-headers t
ement-room-sender-in-left-margin nil))
("%S%L%B%r%R%t" ;; "IRC-style using margins"
(set-vars ement-room-left-margin-width 12
ement-room-right-margin-width 8
ement-room-sender-headers nil
ement-room-sender-in-headers nil
ement-room-sender-in-left-margin t))
("[%t] %S> %B%r" ;; "IRC-style without margins"
(set-vars ement-room-left-margin-width 0
ement-room-right-margin-width 0
ement-room-sender-headers nil
ement-room-sender-in-headers nil
ement-room-sender-in-left-margin nil))
(_ (set-vars ement-room-left-margin-width
(if (string-match-p "%L" value)
12 0)
ement-room-right-margin-width
(if (string-match-p "%R" value)
8 0)
ement-room-sender-in-left-margin
(if (string-match-p (rx (1 anything) (or "%S" "%s") (1 anything) "%L") value)
t nil)
;; NOTE: The following two variables may seem redundant, but one is an
;; option that the user may override, while the other is set
;; automatically.
ement-room-sender-headers
(if (string-match-p (or "%S" "%s") value)
;; If "%S" or "%s" isn't found, assume it's to be shown in headers.
nil t)
ement-room-sender-in-headers
(if (string-match-p (rx (or "%S" "%s")) value)
;; If "%S" or "%s" isn't found, assume it's to be shown in headers.
nil t))
(message "Ement: When using custom message format, setting margin widths may be necessary")))
(unless ement-room-sender-in-headers
;; HACK: Disable overline on sender face.
(require 'face-remap)
(if local
(progn
(face-remap-reset-base 'ement-room-user)
(face-remap-add-relative 'ement-room-user '(:overline nil)))
(set-face-attribute 'ement-room-user nil :overline nil)))
(unless local
(when (and (bound-and-true-p ement-sessions) (car ement-sessions))
;; Only display when a session is connected (not sure why `bound-and-true-p'
;; is required to avoid compilation warnings).
(message "Ement: Kill and reopen room buffers to display in new format")))))
(defcustom ement-room-message-format-spec "%S%L%B%r%R%t"
"Format messages according to this spec.
It may contain these specifiers:
%L End of left margin
%R Start of right margin
%W End of wrap-prefix
%b Message body (plain-text)
%B Message body (formatted if available)
%i Event ID
%O Room display name (used for mentions buffer)
%r Reactions
%s Sender ID
%S Sender display name
%t Event timestamp, formatted according to
`ement-room-timestamp-format'
Note that margin sizes must be set manually with
`ement-room-left-margin-width' and
`ement-room-right-margin-width'."
:type '(choice (const :tag "IRC-style using margins" "%S%L%B%r%R%t")
(const :tag "IRC-style without margins" "[%t] %S> %B%r")
(const :tag "IRC-style without margins, with wrap-prefix" "[%t] %S> %W%B%r")
(const :tag "IRC-style with right margin, with wrap-prefix" "%S> %W%B%r%R%t")
(const :tag "Elemental" "%B%r%R%t")
(string :tag "Custom format"))
:set #'ement-room-message-format-spec-setter
:set-after '(ement-room-left-margin-width ement-room-right-margin-width
ement-room-sender-headers)
;; This file must be loaded before calling the setter to define the
;; `ement-room-user' face used in it.
:require 'ement-room)
(defcustom ement-room-retro-messages-number 30
"Number of messages to retrieve when loading earlier messages."
:type 'integer)
(defcustom ement-room-timestamp-header-format " %H:%M "
"Format string for timestamp headers where date is unchanged.
See function `format-time-string'. If this string ends in a
newline, its background color will extend to the end of the
line."
:type '(choice (const :tag "Time-only" " %H:%M ")
(const :tag "Always show date" " %Y-%m-%d %H:%M ")
string))
(defcustom ement-room-timestamp-header-with-date-format " %Y-%m-%d (%A)\n"
;; FIXME: In Emacs 27 , maybe use :extend t instead of adding a newline.
"Format string for timestamp headers where date changes.
See function `format-time-string'. If this string ends in a
newline, its background color will extend to the end of the
line."
:type '(choice (const " %Y-%m-%d (%A)\n")
string))
(defcustom ement-room-replace-edited-messages t
"Replace edited messages with their new content.
When nil, edited messages are displayed as new messages, leaving
the original messages visible."
:type 'boolean)
(define-obsolete-variable-alias 'ement-room-shr-use-fonts
'ement-room-use-variable-pitch "ement-0.14")
(defcustom ement-room-use-variable-pitch nil
"Use proportional fonts for message bodies.
If non-nil, plain text message bodies are displayed in a
variable-pitch font, and `shr-use-fonts' is enabled for rendering
HTML-formatted message bodies (which includes most replies)."
:type '(choice (const :tag "Disable variable-pitch fonts" nil)
(const :tag "Enable variable-pitch fonts" t)))
(defcustom ement-room-username-display-property '(raise -0.25)
"Display property applied to username strings.
See Info node `(elisp)Other Display Specs'."
:type '(choice (list :tag "Raise" (const :tag "Raise" raise) (number :tag "Factor"))
(list :tag "Height" (const height)
(choice (list :tag "Larger" (const :tag "Larger" ) (number :tag "Steps"))
(list :tag "Smaller" (const :tag "Smaller" -) (number :tag "Steps"))
(number :tag "Factor")
(function :tag "Function")
(sexp :tag "Form"))) ))
(defcustom ement-room-event-separator-display-property '(space :ascent 50)
"Display property applied to invisible space string after events.
Allows visual separation between events without, e.g. inserting
newlines.
See Info node `(elisp)Specified Space'."
:type 'sexp)
(defcustom ement-room-timestamp-header-delta 600
"Show timestamp header where events are at least this many seconds apart."
:type 'integer)
(defcustom ement-room-send-message-filter nil
"Function through which to pass message content before sending.
Used to, e.g. send an Org-formatted message by exporting it to
HTML first."
:type '(choice (const :tag "Send messages as-is" nil)
(const :tag "Send messages in Org format" ement-room-send-org-filter)
(function :tag "Custom filter function"))
:set (lambda (option value)
(set-default option value)
(pcase value
('ement-room-send-org-filter
;; Activate in compose buffer by default.
(add-hook 'ement-room-compose-hook #'ement-room-compose-org))
(_ (remove-hook 'ement-room-compose-hook #'ement-room-compose-org)))))
(defcustom ement-room-mark-rooms-read t
"Mark rooms as read automatically.
Moves read and fully-read markers in rooms on the server when
`ement-room-scroll-up-mark-read' is called at the end of a
buffer. When `send', also marks room as read when sending a
message in it. When disabled, rooms may still be marked as read
manually by calling `ement-room-mark-read'. Note that this is
not strictly the same as read receipts."
:type '(choice (const :tag "When scrolling past end of buffer" t)
(const :tag "Also when sending" send)
(const :tag "Never" nil)))
(defcustom ement-room-send-typing t
"Send typing notifications to the server while typing a message."
:type 'boolean)
(defcustom ement-room-join-view-buffer t
"View room buffer when joining a room."
:type 'boolean)
(defcustom ement-room-leave-kill-buffer t
"Kill room buffer when leaving a room.
When disabled, the room's buffer will remain open, but
Matrix-related commands in it will fail."
:type 'boolean)
(defcustom ement-room-warn-for-already-seen-messages nil
"Warn when a sent message has already been seen.
Such a case could very rarely indicate a reused transaction ID,
which would prevent further messages from being sent (and would
be solved by logging in with a new session, generating a new
token), but most often it happens when the server echoes back a
sent message before acknowledging the sending of the
message (which is harmless and can be ignored)."
:type 'boolean)
(defcustom ement-room-wrap-prefix
(concat (propertize " "
'face 'ement-room-wrap-prefix)
" ")
"String prefixing certain events in room buffers.
Events include membership events, image attachments, etc.
Generally users should prefer to customize the face