-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib-messageline.c
312 lines (282 loc) · 7.97 KB
/
lib-messageline.c
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
/*
* Copyright Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* trim a line off the bottom of a pane and capture messages
* to go there. They disappear on the next keystroke.
*
* Later it might be good to allow boarderless popups to appear here.
*
* The message displayed is:
* a 'modal' message until a keystroke, or
* a normal message which remains until it has been visible without a modal for
* seven seconds with keystrokes, or 30 seconds without keystrokes, or
* a 'default' message ... which is hardly used, or
* a the current time
*
* Refreshed about every 15 seconds, so timestamp can be a little out of date
* but not much.
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define PANE_DATA_TYPE struct mlinfo
#include "core.h"
struct mlinfo {
char *message;
char *modal; /* message displays a mode, and must
* remain exactly until a keystroke
*/
struct pane *line safe, *child;
struct pane *log;
int hidden;
time_t last_message; /* message should stay for at least 10 seconds */
};
#include "core-pane.h"
static struct pane *do_messageline_attach(struct pane *p safe);
static struct map *messageline_map;
DEF_LOOKUP_CMD(messageline_handle, messageline_map);
DEF_CMD(messageline_clone)
{
struct pane *p = do_messageline_attach(ci->focus);
pane_clone_children(ci->home, p);
return 1;
}
DEF_CMD(messageline_border)
{
struct mlinfo *mli = ci->home->data;
if (ci->num > 0)
mli->hidden = 0;
else
mli->hidden = 1;
/* trigger a resize of children */
pane_damaged(ci->home, DAMAGED_SIZE);
return Efallthrough; /* Allow other panes to remove other borders */
}
DEF_CMD(messageline_msg)
{
struct mlinfo *mli = ci->home->data;
if (ci->str && (strcmp(ci->key, "Message:default") != 0 ||
mli->message == NULL)) {
if (!mli->message) {
call("Window:request:Keystroke-notify", ci->home);
call("Window:request:Mouse-event-notify", ci->home);
}
if (strcmp(ci->key, "Message:modal") == 0) {
free(mli->modal);
if (ci->str[0])
mli->modal = strdup(ci->str);
else
mli->modal = NULL;
} else {
free(mli->message);
if (ci->str[0])
mli->message = strdup(ci->str);
else
mli->message = NULL;
/* x==0 check ensures we only append message once when
* it comes in via a broadcast notification
*/
if (ci->x == 0 && mli->log && ci->str[0])
call("doc:log:append", mli->log,
0, NULL, ci->str);
}
time(&mli->last_message);
pane_damaged(ci->home, DAMAGED_VIEW);
}
if (strcmp(ci->key, "Message:broadcast") == 0)
return 1; /* Acknowledge message */
else
return Efallthrough; /* allow other handlers */
}
DEF_CMD(messageline_abort)
{
struct mlinfo *mli = ci->home->data;
if (!mli->message) {
call("Window:request:Keystroke-notify", ci->home);
call("Window:request:Mouse-event-notify", ci->home);
}
free(mli->message);
mli->message = strdup("ABORTED");
free(mli->modal);
mli->modal = NULL;
time(&mli->last_message);
pane_damaged(ci->home, DAMAGED_VIEW);
return Efallthrough;
}
DEF_CMD(messageline_refresh_size)
{
struct mlinfo *mli = ci->home->data;
struct pane *p = mli->line;
if (mli->hidden) {
pane_resize(p, 0, ci->home->h,
ci->home->w, ci->home->h / 3);
if (mli->child)
pane_resize(mli->child, 0, 0,
ci->home->w, ci->home->h);
} else {
pane_resize(p, p->x, p->y, ci->home->w, ci->home->h/3);
call("render-line:measure", p, -1);
pane_resize(p, p->x, ci->home->h - p->h,
ci->home->w, p->h);
if (mli->child && ci->home->h > p->h)
pane_resize(mli->child, 0, 0,
ci->home->w,
ci->home->h - p->h);
}
pane_damaged(p, DAMAGED_REFRESH);
return 1;
}
DEF_CMD(messageline_child_notify)
{
struct mlinfo *mli = ci->home->data;
if (ci->focus->z)
/* Ignore */
return 1;
if (ci->num < 0) {
if (ci->home->focus == ci->focus)
ci->home->focus = NULL;
mli->child = NULL;
} else {
if (mli->child)
pane_close(mli->child);
mli->child = ci->focus;
ci->home->focus = ci->focus;
}
return 1;
}
DEF_CMD(messageline_notify)
{
/* Keystroke notification clears the message line */
struct mlinfo *mli = ci->home->data;
int wait_time = 7;
if (edlib_testing(ci->home))
wait_time = 0;
if (mli->modal) {
free(mli->modal);
mli->modal = NULL;
if (mli->message)
mli->last_message = time(NULL);
pane_damaged(ci->home, DAMAGED_VIEW);
}
if (mli->message &&
time(NULL) >= mli->last_message + wait_time) {
free(mli->message);
mli->message = NULL;
pane_damaged(ci->home, DAMAGED_VIEW);
}
if (!mli->message && !mli->modal) {
pane_drop_notifiers(ci->home, "Keystroke-notify");
pane_drop_notifiers(ci->home, "Mouse-event-notify");
}
return 1;
}
static void pane_str(struct pane *p safe, char *s, char *attr)
{
struct mlinfo *mli = p->parent->data;
char *l = strconcat(p, SOH, attr, STX, s, ETX);
call("render-line:set", p, -1, NULL, l);
/* Allow message line to use up to 1/3 of total height */
pane_resize(p, p->x, p->y, p->w, p->parent->h/3);
call("render-line:measure", p, -1);
if (!mli->hidden) {
pane_resize(p, p->x, p->parent->h - p->h, p->w, p->h);
if (mli->child) {
struct pane *c = mli->child;
pane_resize(c, 0, 0, c->w, p->parent->h - p->h);
}
}
}
DEF_CMD(messageline_refresh)
{
struct mlinfo *mli = ci->home->data;
if (mli->message && !mli->modal &&
time(NULL) >= mli->last_message + 30) {
free(mli->message);
mli->message = NULL;
pane_drop_notifiers(ci->home, "Keystroke-notify");
pane_drop_notifiers(ci->home, "Mouse-event-notify");
}
if (mli->modal)
pane_str(mli->line, mli->modal, "bold,fg:magenta-60,bg:white");
else if (mli->message)
pane_str(mli->line, mli->message, "bold,fg:red,bg:cyan");
else {
char buf[80];
time_t t;
struct tm *tm;
t = time(NULL);
if (edlib_testing(ci->home))
t = 1581382278;
tm = localtime(&t);
if (tm)
strftime(buf, sizeof(buf), "%H:%M %d-%b-%Y", tm);
else
buf[0] = 0;
pane_str(mli->line, buf, "bold,fg:blue,rtab");
}
return 1;
}
DEF_CMD(force_refresh)
{
pane_damaged(ci->home, DAMAGED_VIEW);
return 1;
}
static struct pane *do_messageline_attach(struct pane *p safe)
{
struct mlinfo *mli;
struct pane *ret, *mlp;
ret = pane_register(p, 0, &messageline_handle.c);
if (!ret)
return NULL;
mli = ret->data;
call("editor:request:Message:broadcast", ret);
/* z=1 to avoid clone_children affecting it */
mlp = call_ret(pane, "attach-renderline", ret, 1);
if (!mlp) {
pane_close(ret);
return NULL;
}
/* Support wrapping */
attr_set_str(&mlp->attrs, "render:wrap", "yes");
pane_damaged(ret, DAMAGED_VIEW);
mli->line = mlp;
pane_take_focus(ret);
if (!edlib_testing(p))
/* This can introduce unwanted variablitiy in tests */
call_comm("event:timer", ret, &force_refresh, 15000);
mli->log = call_ret(pane, "docs:byname", p, 0, NULL, "*Messages*");
if (!mli->log)
mli->log = call_ret(pane, "log:create", ret, 0, NULL,
"*Messages*");
return ret;
}
DEF_CMD(messageline_attach)
{
struct pane *ret;
ret = do_messageline_attach(ci->focus);
if (!ret)
return Efail;
return comm_call(ci->comm2, "callback:attach", ret);
}
void edlib_init(struct pane *ed safe)
{
call_comm("global-set-command", ed, &messageline_attach, 0, NULL,
"attach-messageline");
if (messageline_map)
return;
messageline_map = key_alloc();
key_add(messageline_map, "Clone", &messageline_clone);
key_add(messageline_map, "Window:border", &messageline_border);
key_add(messageline_map, "Message", &messageline_msg);
key_add(messageline_map, "Message:modal", &messageline_msg);
key_add(messageline_map, "Message:default", &messageline_msg);
key_add(messageline_map, "Message:broadcast", &messageline_msg);
key_add(messageline_map, "Abort", &messageline_abort);
key_add(messageline_map, "Refresh:size", &messageline_refresh_size);
key_add(messageline_map, "Child-Notify",&messageline_child_notify);
key_add(messageline_map, "Keystroke-notify", &messageline_notify);
key_add(messageline_map, "Mouse-event-notify", &messageline_notify);
key_add(messageline_map, "Refresh:view", &messageline_refresh);
}