-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho-bar-engine.el
More file actions
207 lines (181 loc) · 7.89 KB
/
echo-bar-engine.el
File metadata and controls
207 lines (181 loc) · 7.89 KB
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
;;; echo-bar-engine.el --- Update engine and hook management -*- lexical-binding: t -*-
;; Author: Anton Chen <mail@antonchen.ca>
;; Version: 0.1.0
;; Package-Requires: ((emacs "29.1"))
;; Keywords: frames, convenience
;; URL: https://github.com/chenanton/echo-bar
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Copyright (C) 2026 Anton Chen
;;; Commentary:
;; The core update loop: dirty tracking, coalescing, hook/advice
;; registration. Nothing here knows about layout or overlays --
;; it calls into echo-bar-render.el for those.
;;; Code:
(require 'cl-lib)
(require 'echo-bar-segments)
(require 'echo-bar-render)
;;; State
(defvar echo-bar--update-pending nil
"Non-nil when an update is already scheduled.")
(defvar echo-bar--last-display nil
"The last display string written to overlays.")
(defvar echo-bar--idle-timer nil
"Idle timer for safety-net full refresh.")
(defvar echo-bar--segment-timers nil
"List of active segment timers.")
(defvar echo-bar--registered-hooks nil
"Alist of (HOOK . FUNCTION) pairs registered by echo-bar.")
(defvar echo-bar--registered-advice nil
"Alist of (SYMBOL . FUNCTION) pairs for advice registered by echo-bar.")
;;; Dirty tracking
(defun echo-bar--mark-dirty (name)
"Mark segment NAME as dirty and schedule an update."
(when-let* ((seg (gethash name echo-bar--segments)))
(puthash name t (if (eq (echo-bar-segment-scope seg) :global)
echo-bar--global-dirty
(echo-bar--buffer-dirty)))
(echo-bar--schedule-update)))
(defun echo-bar--mark-dirty-fn (name)
"Return a function that marks segment NAME dirty."
(lambda (&rest _) (echo-bar--mark-dirty name)))
(defun echo-bar--mark-all-dirty ()
"Mark every segment as dirty."
(let ((buf-dirty (echo-bar--buffer-dirty)))
(maphash (lambda (name seg)
(puthash name t (if (eq (echo-bar-segment-scope seg) :global)
echo-bar--global-dirty
buf-dirty)))
echo-bar--segments)))
;;; Coalescer and update loop
(defun echo-bar--schedule-update ()
"Schedule a coalesced update for the next event cycle.
Multiple triggers in the same command cycle produce only one update."
(unless echo-bar--update-pending
(setq echo-bar--update-pending t)
(run-at-time 0 nil #'echo-bar--do-update)))
(defun echo-bar--do-update ()
"Rebuild display from dirty segments, write overlays if changed."
(setq echo-bar--update-pending nil)
(when (and echo-bar-mode (not (active-minibuffer-window)))
;; Recompute dirty buffer-local segments
(let ((buf-dirty (echo-bar--buffer-dirty)))
(maphash (lambda (name _)
(echo-bar--recompute-segment name))
buf-dirty)
(clrhash buf-dirty))
;; Recompute dirty global segments
(maphash (lambda (name _)
(echo-bar--recompute-segment name))
echo-bar--global-dirty)
(clrhash echo-bar--global-dirty)
;; Render and write if changed
(let ((new-display (echo-bar--render)))
(unless (equal new-display echo-bar--last-display)
(setq echo-bar--last-display new-display)
(echo-bar--write-overlays new-display)))))
;;; Event handlers
(defun echo-bar--post-command-handler ()
"Single handler for all post-command-hook segments."
(let ((buf-dirty (echo-bar--buffer-dirty)))
(dolist (name echo-bar--post-command-segments)
(when-let* ((seg (gethash name echo-bar--segments)))
(puthash name t (if (eq (echo-bar-segment-scope seg) :global)
echo-bar--global-dirty
buf-dirty)))))
(echo-bar--schedule-update))
(defun echo-bar--on-buffer-change (&rest _)
"Mark all buffer-local segments dirty on buffer switch."
(setq echo-bar--last-display nil)
(let ((buf-dirty (echo-bar--buffer-dirty)))
(maphash (lambda (name seg)
(when (eq (echo-bar-segment-scope seg) :buffer)
(puthash name t buf-dirty)))
echo-bar--segments))
(echo-bar--schedule-update))
(defun echo-bar--idle-refresh ()
"Safety net: mark everything dirty and update."
(let ((buf-dirty (echo-bar--buffer-dirty)))
(maphash (lambda (name seg)
(puthash name t (if (eq (echo-bar-segment-scope seg) :global)
echo-bar--global-dirty
buf-dirty)))
echo-bar--segments))
(echo-bar--schedule-update))
;;; Hook and advice registration
(defun echo-bar--register-all-hooks ()
"Register hooks and advice for all segments."
;; Combined post-command-hook handler
(when echo-bar--post-command-segments
(add-hook 'post-command-hook #'echo-bar--post-command-handler)
(push (cons 'post-command-hook #'echo-bar--post-command-handler)
echo-bar--registered-hooks))
;; Buffer change
(add-hook 'window-buffer-change-functions #'echo-bar--on-buffer-change)
(push (cons 'window-buffer-change-functions #'echo-bar--on-buffer-change)
echo-bar--registered-hooks)
;; Window resize
(add-hook 'window-size-change-functions #'echo-bar--on-resize)
(push (cons 'window-size-change-functions #'echo-bar--on-resize)
echo-bar--registered-hooks)
;; Font change
(when (boundp 'after-setting-font-hook)
(add-hook 'after-setting-font-hook #'echo-bar--on-font-change)
(push (cons 'after-setting-font-hook #'echo-bar--on-font-change)
echo-bar--registered-hooks))
;; Per-segment hooks and advice (excluding post-command-hook)
(maphash
(lambda (name seg)
(let ((triggers (echo-bar-segment-triggers seg)))
;; Hooks
(dolist (hook (plist-get triggers :hooks))
(unless (eq hook 'post-command-hook)
(let ((fn (echo-bar--mark-dirty-fn name)))
(add-hook hook fn)
(push (cons hook fn) echo-bar--registered-hooks))))
;; Advice
(dolist (spec (plist-get triggers :advice))
(let ((target (car spec))
(how (cdr spec))
(fn (echo-bar--mark-dirty-fn name)))
(advice-add target how fn)
(push (cons target fn) echo-bar--registered-advice)))
;; Timers
(when-let* ((timer-spec (plist-get triggers :timers)))
(let* ((idle (plist-get timer-spec :idle))
(repeat (plist-get timer-spec :repeat))
(fn (echo-bar--mark-dirty-fn name))
(timer (run-with-idle-timer (or idle 10) repeat fn)))
(push timer echo-bar--segment-timers)))))
echo-bar--segments)
;; Message advice
(advice-add 'message :around #'echo-bar--message-advice)
;; Re-apply face remap on minibuffer transitions (Emacs resets
;; face-remapping-alist when entering/exiting the minibuffer)
(add-hook 'minibuffer-inactive-mode-hook #'echo-bar--fontify-echo-area)
(push (cons 'minibuffer-inactive-mode-hook #'echo-bar--fontify-echo-area)
echo-bar--registered-hooks)
(add-hook 'minibuffer-setup-hook #'echo-bar--fontify-echo-area)
(push (cons 'minibuffer-setup-hook #'echo-bar--fontify-echo-area)
echo-bar--registered-hooks)
;; Idle fallback timer
(when echo-bar-idle-fallback-interval
(setq echo-bar--idle-timer
(run-with-idle-timer echo-bar-idle-fallback-interval t
#'echo-bar--idle-refresh))))
(defun echo-bar--remove-all-hooks ()
"Remove all hooks and advice registered by echo-bar."
(dolist (entry echo-bar--registered-hooks)
(remove-hook (car entry) (cdr entry)))
(setq echo-bar--registered-hooks nil)
(dolist (entry echo-bar--registered-advice)
(advice-remove (car entry) (cdr entry)))
(setq echo-bar--registered-advice nil)
(advice-remove 'message #'echo-bar--message-advice)
(dolist (timer echo-bar--segment-timers)
(when (timerp timer) (cancel-timer timer)))
(setq echo-bar--segment-timers nil)
(when (timerp echo-bar--idle-timer)
(cancel-timer echo-bar--idle-timer)
(setq echo-bar--idle-timer nil)))
(provide 'echo-bar-engine)
;;; echo-bar-engine.el ends here