Skip to content

Commit 74d10eb

Browse files
committed
Tidy: Faces, timestamp-colors variable, etc.
1 parent 90f2213 commit 74d10eb

File tree

1 file changed

+105
-21
lines changed

1 file changed

+105
-21
lines changed

ement-room-list.el

Lines changed: 105 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
(require 'taxy)
3232
(require 'taxy-magit-section)
3333

34-
(require 'ement-tabulated-room-list)
35-
3634
(defgroup ement-room-list nil
3735
"Group Ement rooms with Taxy."
3836
:group 'ement)
@@ -47,6 +45,10 @@
4745
(define-key map [mouse-1] #'ement-room-list-mouse-1)
4846
map))
4947

48+
(defvar ement-room-list-timestamp-colors nil
49+
"List of colors used for timestamps.
50+
Set automatically when `ement-room-list-mode' is activated.")
51+
5052
;;;; Customization
5153

5254
(defcustom ement-room-list-auto-update t
@@ -55,9 +57,48 @@
5557

5658
;;;;; Faces
5759

58-
(defface ement-tabulated-room-list-space '((t (:inherit (font-lock-regexp-grouping-backslash ement-tabulated-room-list-name))))
60+
(defface ement-room-list-direct
61+
;; In case `font-lock-constant-face' is bold, we set the weight to normal, so it can be
62+
;; made bold for unread rooms only.
63+
'((t (:weight normal :inherit (font-lock-constant-face ement-room-list-name))))
64+
"Direct rooms.")
65+
66+
(defface ement-room-list-favourite '((t (:inherit (font-lock-doc-face ement-room-list-name))))
67+
"Favourite rooms.")
68+
69+
(defface ement-room-list-invited
70+
'((t (:inherit italic ement-room-list-name)))
71+
"Invited rooms.")
72+
73+
(defface ement-room-list-left
74+
'((t (:strike-through t :inherit ement-room-list-name)))
75+
"Left rooms.")
76+
77+
(defface ement-room-list-low-priority '((t (:inherit (font-lock-comment-face ement-room-list-name))))
78+
"Low-priority rooms.")
79+
80+
(defface ement-room-list-name
81+
'((t (:inherit font-lock-function-name-face button)))
82+
"Non-direct rooms.")
83+
84+
(defface ement-room-list-space '((t (:inherit (font-lock-regexp-grouping-backslash ement-room-list-name))))
5985
"Space rooms."
60-
:group 'ement-tabulated-room-list)
86+
:group 'ement-room-list)
87+
88+
(defface ement-room-list-unread
89+
'((t (:inherit bold ement-room-list-name)))
90+
"Unread rooms.")
91+
92+
(defface ement-room-list-recent '((t (:inherit font-lock-warning-face)))
93+
"Latest timestamp of recently updated rooms.
94+
The foreground color is used to generate a gradient of colors
95+
from recent to non-recent for rooms updated in the past 24
96+
hours but at least one hour ago.")
97+
98+
(defface ement-room-list-very-recent '((t (:inherit error)))
99+
"Latest timestamp of very recently updated rooms.
100+
The foreground color is used to generate a gradient of colors
101+
from recent to non-recent for rooms updated in the past hour.")
61102

62103
;;;; Keys
63104

@@ -101,7 +142,7 @@
101142
(ement-room-list-define-key people ()
102143
(pcase-let ((`[,room ,session] item))
103144
(when (ement--room-direct-p room session)
104-
(propertize "People" 'face 'ement-tabulated-room-list-direct))))
145+
(propertize "People" 'face 'ement-room-list-direct))))
105146

106147
(ement-room-list-define-key space (&key name id)
107148
(pcase-let* ((`[,room ,session] item)
@@ -133,7 +174,7 @@
133174
(_
134175
;; TODO: How to handle this better? (though it should be very rare)
135176
(string-join (mapcar #'format-space parents) ", "))))))
136-
(propertize key 'face 'ement-tabulated-room-list-space)))))
177+
(propertize key 'face 'ement-room-list-space)))))
137178

138179
(ement-room-list-define-key space-p ()
139180
"Groups rooms that are themselves spaces."
@@ -204,7 +245,7 @@
204245
:then #'identity
205246
(pcase-let ((`[,room ,_session] item))
206247
(when (ement--room-favourite-p room)
207-
(propertize "Favourite" 'face 'ement-tabulated-room-list-favourite))))
248+
(propertize "Favourite" 'face 'ement-room-list-favourite))))
208249

209250
(ement-room-list-define-key low-priority ()
210251
:then #'identity
@@ -271,25 +312,25 @@
271312
(face))
272313
(or (when display-name
273314
;; TODO: Use code from ement-room-list and put in a dedicated function.
274-
(setf face (cl-copy-list '(:inherit (ement-tabulated-room-list-name))))
315+
(setf face (cl-copy-list '(:inherit (ement-room-list-name))))
275316
;; In concert with the "Unread" column, this is roughly equivalent to the
276317
;; "red/gray/bold/idle" states listed in <https://github.com/matrix-org/matrix-react-sdk/blob/b0af163002e8252d99b6d7075c83aadd91866735/docs/room-list-store.md#list-ordering-algorithm-importance>.
277318
(when (ement--room-unread-p room session)
278319
;; For some reason, `push' doesn't work with `map-elt'...or does it?
279-
(push 'ement-tabulated-room-list-unread (map-elt face :inherit)))
320+
(push 'ement-room-list-unread (map-elt face :inherit)))
280321
(when (equal "m.space" type)
281-
(push 'ement-tabulated-room-list-space (map-elt face :inherit)))
322+
(push 'ement-room-list-space (map-elt face :inherit)))
282323
(when (ement--room-direct-p room session)
283-
(push 'ement-tabulated-room-list-direct (map-elt face :inherit)))
324+
(push 'ement-room-list-direct (map-elt face :inherit)))
284325
(when (ement--room-favourite-p room)
285-
(push 'ement-tabulated-room-list-favourite (map-elt face :inherit)))
326+
(push 'ement-room-list-favourite (map-elt face :inherit)))
286327
(when (ement--room-low-priority-p room)
287-
(push 'ement-tabulated-room-list-low-priority (map-elt face :inherit)))
328+
(push 'ement-room-list-low-priority (map-elt face :inherit)))
288329
(pcase (ement-room-status room)
289330
('invite
290-
(push 'ement-tabulated-room-list-invited (map-elt face :inherit)))
331+
(push 'ement-room-list-invited (map-elt face :inherit)))
291332
('leave
292-
(push 'ement-tabulated-room-list-left (map-elt face :inherit))))
333+
(push 'ement-room-list-left (map-elt face :inherit))))
293334
(propertize (ement--button-buttonize display-name #'ement-room-list-mouse-1)
294335
'face face
295336
'mouse-face 'highlight))
@@ -320,9 +361,9 @@
320361
((number 3600 86400) ;; 1 hour to 1 day: 24 1-hour periods.
321362
(+ 6 (truncate (/ difference-seconds 3600))))
322363
(otherwise ;; Difference in weeks.
323-
(min (/ (length ement-tabulated-room-list-timestamp-colors) 2)
364+
(min (/ (length ement-room-list-timestamp-colors) 2)
324365
(+ 24 (truncate (/ difference-seconds 86400 7)))))))
325-
(face (list :foreground (elt ement-tabulated-room-list-timestamp-colors n)))
366+
(face (list :foreground (elt ement-room-list-timestamp-colors n)))
326367
(formatted-ts (ement--human-format-duration difference-seconds 'abbreviate)))
327368
(string-match (rx (1+ digit) (repeat 1 alpha)) formatted-ts)
328369
(propertize (match-string 0 formatted-ts) 'face face
@@ -336,10 +377,10 @@
336377
(setf topic (replace-regexp-in-string "\n" " " topic 'fixedcase 'literal)))
337378
(pcase status
338379
('invite (concat (propertize "[invited]"
339-
'face 'ement-tabulated-room-list-invited)
380+
'face 'ement-room-list-invited)
340381
" " topic))
341382
('leave (concat (propertize "[left]"
342-
'face 'ement-tabulated-room-list-left)
383+
'face 'ement-room-list-left)
343384
" " topic))
344385
(_ (or topic "")))))
345386

@@ -619,9 +660,9 @@ left."
619660

620661
(define-derived-mode ement-room-list-mode magit-section-mode "Ement-Room-List"
621662
:global nil
622-
;; FIXME: Initialize `ement-tabulated-room-list-timestamp-colors' here.
623663
(setq-local bookmark-make-record-function #'ement-room-list-bookmark-make-record
624-
revert-buffer-function #'ement-room-list-revert))
664+
revert-buffer-function #'ement-room-list-revert
665+
ement-room-list-timestamp-colors (ement-room-list--timestamp-colors)))
625666

626667
;;;; Functions
627668

@@ -643,6 +684,49 @@ left."
643684
;; minibuffer is open, which should be unrelated to this.
644685
(revert-buffer)))))
645686

687+
(defun ement-room-list--timestamp-colors ()
688+
"Return a vector of generated latest-timestamp colors for rooms.
689+
Used in `ement-tabulated-room-list' and `ement-room-list'."
690+
(if (or (equal "unspecified-fg" (face-foreground 'default nil 'default))
691+
(equal "unspecified-bg" (face-background 'default nil 'default)))
692+
;; NOTE: On a TTY, the default face's foreground and background colors may be the
693+
;; special values "unspecified-fg"/"unspecified-bg", in which case we can't generate
694+
;; gradients, so we just return a vector of "unspecified-fg". See
695+
;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=55623>.
696+
(make-vector 134 "unspecified-fg")
697+
(cl-coerce
698+
(append (mapcar
699+
;; One face per 10-minute period, from "recent" to 1-hour.
700+
(lambda (rgb)
701+
(pcase-let ((`(,r ,g ,b) rgb))
702+
(color-rgb-to-hex r g b 2)))
703+
(color-gradient (color-name-to-rgb (face-foreground 'ement-room-list-very-recent
704+
nil 'default))
705+
(color-name-to-rgb (face-foreground 'ement-room-list-recent
706+
nil 'default))
707+
6))
708+
(mapcar
709+
;; One face per hour, from "recent" to default.
710+
(lambda (rgb)
711+
(pcase-let ((`(,r ,g ,b) rgb))
712+
(color-rgb-to-hex r g b 2)))
713+
(color-gradient (color-name-to-rgb (face-foreground 'ement-room-list-recent
714+
nil 'default))
715+
(color-name-to-rgb (face-foreground 'default nil 'default))
716+
24))
717+
(mapcar
718+
;; One face per week for the last year (actually we
719+
;; generate colors for the past two years' worth so
720+
;; that the face for one-year-ago is halfway to
721+
;; invisible, and we don't use colors past that point).
722+
(lambda (rgb)
723+
(pcase-let ((`(,r ,g ,b) rgb))
724+
(color-rgb-to-hex r g b 2)))
725+
(color-gradient (color-name-to-rgb (face-foreground 'default nil 'default))
726+
(color-name-to-rgb (face-background 'default nil 'default))
727+
104)))
728+
'vector)))
729+
646730
;;;; Footer
647731

648732
(provide 'ement-room-list)

0 commit comments

Comments
 (0)