forked from davidben/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
viewwin.c
427 lines (369 loc) · 12.3 KB
/
viewwin.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
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
#include <string.h>
#include "owl.h"
#define BOTTOM_OFFSET 1
static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data);
static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data);
static void owl_viewwin_layout(owl_viewwin *v);
static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w);
/* Create a viewwin. 'win' is an already initialized owl_window that
* will be used by the viewwin
*/
owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text)
{
owl_viewwin *v = g_new0(owl_viewwin, 1);
owl_fmtext_init_null(&(v->fmtext));
if (text) {
owl_fmtext_append_normal(&(v->fmtext), text);
if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
owl_fmtext_append_normal(&(v->fmtext), "\n");
}
v->textlines=owl_fmtext_num_lines(&(v->fmtext));
}
v->topline=0;
v->rightshift=0;
v->onclose_hook = NULL;
owl_viewwin_set_window(v, win);
return v;
}
/* Create a viewwin. 'win' is an already initialized owl_window that
* will be used by the viewwin
*/
owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext)
{
char *text;
owl_viewwin *v = g_new0(owl_viewwin, 1);
owl_fmtext_copy(&(v->fmtext), fmtext);
text = owl_fmtext_print_plain(fmtext);
if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
owl_fmtext_append_normal(&(v->fmtext), "\n");
}
g_free(text);
v->textlines=owl_fmtext_num_lines(&(v->fmtext));
v->topline=0;
v->rightshift=0;
owl_viewwin_set_window(v, win);
return v;
}
static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
{
v->window = g_object_ref(w);
v->content = owl_window_new(v->window);
v->status = owl_window_new(v->window);
v->cmdwin = NULL;
v->sig_content_redraw_id =
g_signal_connect(v->content, "redraw", G_CALLBACK(owl_viewwin_redraw_content), v);
v->sig_status_redraw_id =
g_signal_connect(v->status, "redraw", G_CALLBACK(owl_viewwin_redraw_status), v);
v->sig_resize_id =
g_signal_connect_swapped(v->window, "resized", G_CALLBACK(owl_viewwin_layout), v);
owl_viewwin_layout(v);
owl_window_show(v->content);
owl_window_show(v->status);
}
void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
v->onclose_hook = onclose_hook;
v->onclose_hook_data = onclose_hook_data;
}
static void owl_viewwin_layout(owl_viewwin *v)
{
int lines, cols;
owl_window_get_position(v->window, &lines, &cols, NULL, NULL);
owl_window_set_position(v->content, lines - BOTTOM_OFFSET, cols, 0, 0);
/* Only one of these will be visible at a time: */
owl_window_set_position(v->status, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
if (v->cmdwin)
owl_window_set_position(v->cmdwin, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
}
/* regenerate text on the curses window. */
static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data)
{
owl_fmtext fm1, fm2;
owl_viewwin *v = user_data;
int winlines, wincols;
owl_window_get_position(w, &winlines, &wincols, 0, 0);
werase(curswin);
wmove(curswin, 0, 0);
owl_fmtext_init_null(&fm1);
owl_fmtext_init_null(&fm2);
owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines, &fm1);
owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
owl_fmtext_curs_waddstr(&fm2, curswin, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
owl_fmtext_cleanup(&fm1);
owl_fmtext_cleanup(&fm2);
}
static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data)
{
owl_viewwin *v = user_data;
int winlines, wincols;
owl_window_get_position(v->content, &winlines, &wincols, 0, 0);
werase(curswin);
wmove(curswin, 0, 0);
wattrset(curswin, A_REVERSE);
if (v->textlines - v->topline > winlines) {
waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
} else {
waddstr(curswin, "--End-- (Press 'q' to quit)");
}
wattroff(curswin, A_REVERSE);
}
char *owl_viewwin_command_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
{
int direction, consider_current;
const char *buffstart;
direction=OWL_DIRECTION_DOWNWARDS;
buffstart=skiptokens(buff, 1);
if (argc>1 && !strcmp(argv[1], "-r")) {
direction=OWL_DIRECTION_UPWARDS;
buffstart=skiptokens(buff, 2);
}
if (argc==1 || (argc==2 && !strcmp(argv[1], "-r"))) {
consider_current = false;
} else {
owl_function_set_search(buffstart);
consider_current = true;
}
if (!owl_viewwin_search(v, owl_global_get_search_re(&g), consider_current, direction))
owl_function_makemsg("No more matches");
return NULL;
}
typedef struct _owl_viewwin_search_data { /*noproto*/
owl_viewwin *v;
int direction;
} owl_viewwin_search_data;
static void owl_viewwin_callback_search(owl_editwin *e)
{
int consider_current = false;
const char *line = owl_editwin_get_text(e);
owl_viewwin_search_data *data = owl_editwin_get_cbdata(e);
/* Given an empty string, just continue the current search. */
if (line && *line) {
owl_function_set_search(line);
consider_current = true;
}
if (!owl_viewwin_search(data->v, owl_global_get_search_re(&g),
consider_current, data->direction))
owl_function_makemsg("No matches");
}
char *owl_viewwin_command_start_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
{
int direction;
const char *buffstart;
owl_editwin *tw;
owl_context *ctx;
owl_viewwin_search_data *data;
direction=OWL_DIRECTION_DOWNWARDS;
buffstart=skiptokens(buff, 1);
if (argc>1 && !strcmp(argv[1], "-r")) {
direction=OWL_DIRECTION_UPWARDS;
buffstart=skiptokens(buff, 2);
}
/* TODO: Add a search history? */
tw = owl_viewwin_set_typwin_active(v, NULL);
owl_editwin_set_locktext(tw, (direction == OWL_DIRECTION_DOWNWARDS) ? "/" : "?");
owl_editwin_insert_string(tw, buffstart);
data = g_new(owl_viewwin_search_data, 1);
data->v = v;
data->direction = direction;
ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
owl_viewwin_deactivate_editcontext, v);
ctx->cbdata = v;
owl_global_push_context_obj(&g, ctx);
owl_editwin_set_callback(tw, owl_viewwin_callback_search);
owl_editwin_set_cbdata(tw, data, g_free);
/* We aren't saving tw, so release the reference we were given. */
owl_editwin_unref(tw);
return NULL;
}
char *owl_viewwin_start_command(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
{
owl_editwin *tw;
owl_context *ctx;
buff = skiptokens(buff, 1);
tw = owl_viewwin_set_typwin_active(v, owl_global_get_cmd_history(&g));
owl_editwin_set_locktext(tw, ":");
owl_editwin_insert_string(tw, buff);
ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
owl_viewwin_deactivate_editcontext, v);
owl_global_push_context_obj(&g, ctx);
owl_editwin_set_callback(tw, owl_callback_command);
/* We aren't saving tw, so release the reference we were given. */
owl_editwin_unref(tw);
return NULL;
}
void owl_viewwin_deactivate_editcontext(owl_context *ctx) {
owl_viewwin *v = ctx->cbdata;
owl_viewwin_set_typwin_inactive(v);
}
owl_editwin *owl_viewwin_set_typwin_active(owl_viewwin *v, owl_history *hist) {
int lines, cols;
owl_editwin *cmdline;
if (v->cmdwin)
return NULL;
/* Create the command line. */
v->cmdwin = owl_window_new(v->window);
owl_viewwin_layout(v);
owl_window_get_position(v->cmdwin, &lines, &cols, NULL, NULL);
cmdline = owl_editwin_new(v->cmdwin, lines, cols, OWL_EDITWIN_STYLE_ONELINE, hist);
/* Swap out the bottom window. */
owl_window_hide(v->status);
owl_window_show(v->cmdwin);
return cmdline;
}
void owl_viewwin_set_typwin_inactive(owl_viewwin *v) {
if (v->cmdwin) {
/* Swap out the bottom window. */
owl_window_hide(v->cmdwin);
owl_window_show(v->status);
/* Destroy the window itself. */
owl_window_unlink(v->cmdwin);
g_object_unref(v->cmdwin);
v->cmdwin = NULL;
}
}
void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
owl_fmtext_append_normal(&(v->fmtext), text);
v->textlines=owl_fmtext_num_lines(&(v->fmtext));
owl_viewwin_dirty(v);
}
/* Schedule a redraw of 'v'. Exported for hooking into the search
string; when we have some way of listening for changes, this can be
removed. */
void owl_viewwin_dirty(owl_viewwin *v)
{
owl_window_dirty(v->content);
owl_window_dirty(v->status);
}
void owl_viewwin_down(owl_viewwin *v, int amount) {
int winlines;
owl_window_get_position(v->content, &winlines, 0, 0, 0);
/* Don't scroll past the bottom. */
amount = MIN(amount, v->textlines - (v->topline + winlines));
/* But if we're already past the bottom, don't back up either. */
if (amount > 0) {
v->topline += amount;
owl_viewwin_dirty(v);
}
}
void owl_viewwin_up(owl_viewwin *v, int amount)
{
v->topline -= amount;
if (v->topline<0) v->topline=0;
owl_viewwin_dirty(v);
}
void owl_viewwin_pagedown(owl_viewwin *v)
{
int winlines;
owl_window_get_position(v->content, &winlines, 0, 0, 0);
owl_viewwin_down(v, winlines);
}
void owl_viewwin_linedown(owl_viewwin *v)
{
owl_viewwin_down(v, 1);
}
void owl_viewwin_pageup(owl_viewwin *v)
{
int winlines;
owl_window_get_position(v->content, &winlines, 0, 0, 0);
owl_viewwin_up(v, winlines);
}
void owl_viewwin_lineup(owl_viewwin *v)
{
owl_viewwin_up(v, 1);
}
void owl_viewwin_right(owl_viewwin *v, int n)
{
v->rightshift+=n;
owl_viewwin_dirty(v);
}
void owl_viewwin_left(owl_viewwin *v, int n)
{
v->rightshift-=n;
if (v->rightshift<0) v->rightshift=0;
owl_viewwin_dirty(v);
}
void owl_viewwin_top(owl_viewwin *v)
{
v->topline=0;
v->rightshift=0;
owl_viewwin_dirty(v);
}
void owl_viewwin_bottom(owl_viewwin *v)
{
int winlines;
owl_window_get_position(v->content, &winlines, 0, 0, 0);
v->topline = v->textlines - winlines;
owl_viewwin_dirty(v);
}
/* This is a bit of a hack, because regexec doesn't have an 'n'
* version. */
static int _re_memcompare(const owl_regex *re, const char *string, int start, int end)
{
int ans;
char *tmp = g_strndup(string + start, end - start);
ans = owl_regex_compare(re, tmp, NULL, NULL);
g_free(tmp);
return !ans;
}
/* Scroll in 'direction' to the next line containing 're' in 'v',
* starting from the current line. Returns 0 if no occurrence is
* found.
*
* If consider_current is true then stay on the current line
* if it matches.
*/
int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int consider_current, int direction)
{
int start, end, offset;
int lineend, linestart;
const char *buf, *linestartp;
owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
if (direction == OWL_DIRECTION_DOWNWARDS) {
offset = owl_fmtext_search(&v->fmtext, re,
consider_current ? start : end);
if (offset < 0)
return 0;
v->topline = owl_fmtext_line_number(&v->fmtext, offset);
owl_viewwin_dirty(v);
return 1;
} else {
/* TODO: This is a hack. Really, we should have an owl_fmlines or
* something containing an array of owl_fmtext split into
* lines. Also, it cannot handle multi-line regex, if we ever care about
* them. */
buf = owl_fmtext_get_text(&v->fmtext);
lineend = consider_current ? end : start;
while (lineend > 0) {
linestartp = memrchr(buf, '\n', lineend - 1);
linestart = linestartp ? linestartp - buf + 1 : 0;
if (_re_memcompare(re, buf, linestart, lineend)) {
v->topline = owl_fmtext_line_number(&v->fmtext, linestart);
owl_viewwin_dirty(v);
return 1;
}
lineend = linestart;
}
return 0;
}
}
void owl_viewwin_delete(owl_viewwin *v)
{
if (v->onclose_hook) {
v->onclose_hook(v, v->onclose_hook_data);
v->onclose_hook = NULL;
v->onclose_hook_data = NULL;
}
owl_viewwin_set_typwin_inactive(v);
/* TODO: This is far too tedious. owl_viewwin should own v->window
* and just unlink it in one go. Signals should also go away for
* free. */
g_signal_handler_disconnect(v->window, v->sig_resize_id);
g_signal_handler_disconnect(v->content, v->sig_content_redraw_id);
g_signal_handler_disconnect(v->status, v->sig_status_redraw_id);
owl_window_unlink(v->content);
owl_window_unlink(v->status);
g_object_unref(v->window);
g_object_unref(v->content);
g_object_unref(v->status);
owl_fmtext_cleanup(&(v->fmtext));
g_free(v);
}