1 | #include <string.h> |
---|
2 | #include "owl.h" |
---|
3 | |
---|
4 | #define BOTTOM_OFFSET 1 |
---|
5 | #define EMPTY_INDICATOR "~" |
---|
6 | |
---|
7 | static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data); |
---|
8 | static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data); |
---|
9 | static void owl_viewwin_layout(owl_viewwin *v); |
---|
10 | static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w); |
---|
11 | |
---|
12 | /* Create a viewwin. 'win' is an already initialized owl_window that |
---|
13 | * will be used by the viewwin |
---|
14 | */ |
---|
15 | owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text) |
---|
16 | { |
---|
17 | owl_viewwin *v = owl_malloc(sizeof(owl_viewwin)); |
---|
18 | memset(v, 0, sizeof(*v)); |
---|
19 | owl_fmtext_init_null(&(v->fmtext)); |
---|
20 | if (text) { |
---|
21 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
22 | if (text[0] != '\0' && text[strlen(text) - 1] != '\n') { |
---|
23 | owl_fmtext_append_normal(&(v->fmtext), "\n"); |
---|
24 | } |
---|
25 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
26 | } |
---|
27 | v->topline=0; |
---|
28 | v->rightshift=0; |
---|
29 | v->onclose_hook = NULL; |
---|
30 | |
---|
31 | owl_viewwin_set_window(v, win); |
---|
32 | return v; |
---|
33 | } |
---|
34 | |
---|
35 | /* Create a viewwin. 'win' is an already initialized owl_window that |
---|
36 | * will be used by the viewwin |
---|
37 | */ |
---|
38 | owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext) |
---|
39 | { |
---|
40 | char *text; |
---|
41 | owl_viewwin *v = owl_malloc(sizeof(owl_viewwin)); |
---|
42 | memset(v, 0, sizeof(*v)); |
---|
43 | |
---|
44 | owl_fmtext_copy(&(v->fmtext), fmtext); |
---|
45 | text = owl_fmtext_print_plain(fmtext); |
---|
46 | if (text[0] != '\0' && text[strlen(text) - 1] != '\n') { |
---|
47 | owl_fmtext_append_normal(&(v->fmtext), "\n"); |
---|
48 | } |
---|
49 | owl_free(text); |
---|
50 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
51 | v->topline=0; |
---|
52 | v->rightshift=0; |
---|
53 | |
---|
54 | owl_viewwin_set_window(v, win); |
---|
55 | return v; |
---|
56 | } |
---|
57 | |
---|
58 | static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w) |
---|
59 | { |
---|
60 | v->window = g_object_ref(w); |
---|
61 | v->content = owl_window_new(v->window); |
---|
62 | v->status = owl_window_new(v->window); |
---|
63 | v->cmdwin = NULL; |
---|
64 | |
---|
65 | v->sig_content_redraw_id = |
---|
66 | g_signal_connect(v->content, "redraw", G_CALLBACK(owl_viewwin_redraw_content), v); |
---|
67 | v->sig_status_redraw_id = |
---|
68 | g_signal_connect(v->status, "redraw", G_CALLBACK(owl_viewwin_redraw_status), v); |
---|
69 | v->sig_resize_id = |
---|
70 | g_signal_connect_swapped(v->window, "resized", G_CALLBACK(owl_viewwin_layout), v); |
---|
71 | owl_viewwin_layout(v); |
---|
72 | |
---|
73 | owl_window_show(v->content); |
---|
74 | owl_window_show(v->status); |
---|
75 | } |
---|
76 | |
---|
77 | void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) { |
---|
78 | v->onclose_hook = onclose_hook; |
---|
79 | v->onclose_hook_data = onclose_hook_data; |
---|
80 | } |
---|
81 | |
---|
82 | static void owl_viewwin_layout(owl_viewwin *v) |
---|
83 | { |
---|
84 | int lines, cols; |
---|
85 | owl_window_get_position(v->window, &lines, &cols, NULL, NULL); |
---|
86 | owl_window_set_position(v->content, lines - BOTTOM_OFFSET, cols, 0, 0); |
---|
87 | /* Only one of these will be visible at a time: */ |
---|
88 | owl_window_set_position(v->status, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0); |
---|
89 | if (v->cmdwin) |
---|
90 | owl_window_set_position(v->cmdwin, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0); |
---|
91 | } |
---|
92 | |
---|
93 | /* regenerate text on the curses window. */ |
---|
94 | static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data) |
---|
95 | { |
---|
96 | owl_fmtext fm1, fm2; |
---|
97 | owl_viewwin *v = user_data; |
---|
98 | int winlines, wincols; |
---|
99 | int y; |
---|
100 | |
---|
101 | owl_window_get_position(w, &winlines, &wincols, 0, 0); |
---|
102 | |
---|
103 | werase(curswin); |
---|
104 | wmove(curswin, 0, 0); |
---|
105 | |
---|
106 | owl_fmtext_init_null(&fm1); |
---|
107 | owl_fmtext_init_null(&fm2); |
---|
108 | |
---|
109 | owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines, &fm1); |
---|
110 | owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2); |
---|
111 | |
---|
112 | owl_fmtext_curs_waddstr(&fm2, curswin); |
---|
113 | |
---|
114 | /* Fill remaining lines with tildes. */ |
---|
115 | y = v->textlines - v->topline; |
---|
116 | wmove(curswin, y, 0); |
---|
117 | for (; y < winlines; y++) { |
---|
118 | waddstr(curswin, EMPTY_INDICATOR); |
---|
119 | waddstr(curswin, "\n"); |
---|
120 | } |
---|
121 | |
---|
122 | owl_fmtext_cleanup(&fm1); |
---|
123 | owl_fmtext_cleanup(&fm2); |
---|
124 | } |
---|
125 | |
---|
126 | static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data) |
---|
127 | { |
---|
128 | owl_viewwin *v = user_data; |
---|
129 | int winlines, wincols; |
---|
130 | |
---|
131 | owl_window_get_position(v->content, &winlines, &wincols, 0, 0); |
---|
132 | |
---|
133 | werase(curswin); |
---|
134 | wmove(curswin, 0, 0); |
---|
135 | wattrset(curswin, A_REVERSE); |
---|
136 | if (v->textlines - v->topline > winlines) { |
---|
137 | waddstr(curswin, "--More-- (Space to see more, 'q' to quit)"); |
---|
138 | } else { |
---|
139 | waddstr(curswin, "--End-- (Press 'q' to quit)"); |
---|
140 | } |
---|
141 | wattroff(curswin, A_REVERSE); |
---|
142 | } |
---|
143 | |
---|
144 | char *owl_viewwin_command_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff) |
---|
145 | { |
---|
146 | int direction, consider_current; |
---|
147 | const char *buffstart; |
---|
148 | |
---|
149 | direction=OWL_DIRECTION_DOWNWARDS; |
---|
150 | buffstart=skiptokens(buff, 1); |
---|
151 | if (argc>1 && !strcmp(argv[1], "-r")) { |
---|
152 | direction=OWL_DIRECTION_UPWARDS; |
---|
153 | buffstart=skiptokens(buff, 2); |
---|
154 | } |
---|
155 | |
---|
156 | if (argc==1 || (argc==2 && !strcmp(argv[1], "-r"))) { |
---|
157 | consider_current = false; |
---|
158 | } else { |
---|
159 | owl_function_set_search(buffstart); |
---|
160 | consider_current = true; |
---|
161 | } |
---|
162 | |
---|
163 | if (!owl_viewwin_search(v, owl_global_get_search_re(&g), consider_current, direction)) |
---|
164 | owl_function_error("No more matches"); |
---|
165 | return NULL; |
---|
166 | } |
---|
167 | |
---|
168 | typedef struct _owl_viewwin_search_data { /*noproto*/ |
---|
169 | owl_viewwin *v; |
---|
170 | int direction; |
---|
171 | } owl_viewwin_search_data; |
---|
172 | |
---|
173 | static void owl_viewwin_callback_search(owl_editwin *e) |
---|
174 | { |
---|
175 | int consider_current = false; |
---|
176 | const char *line = owl_editwin_get_text(e); |
---|
177 | owl_viewwin_search_data *data = owl_editwin_get_cbdata(e); |
---|
178 | |
---|
179 | /* Given an empty string, just continue the current search. */ |
---|
180 | if (line && *line) { |
---|
181 | owl_function_set_search(line); |
---|
182 | consider_current = true; |
---|
183 | } |
---|
184 | if (!owl_viewwin_search(data->v, owl_global_get_search_re(&g), |
---|
185 | consider_current, data->direction)) |
---|
186 | owl_function_error("No matches"); |
---|
187 | } |
---|
188 | |
---|
189 | char *owl_viewwin_command_start_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff) |
---|
190 | { |
---|
191 | int direction; |
---|
192 | const char *buffstart; |
---|
193 | owl_editwin *tw; |
---|
194 | owl_context *ctx; |
---|
195 | owl_viewwin_search_data *data; |
---|
196 | |
---|
197 | direction=OWL_DIRECTION_DOWNWARDS; |
---|
198 | buffstart=skiptokens(buff, 1); |
---|
199 | if (argc>1 && !strcmp(argv[1], "-r")) { |
---|
200 | direction=OWL_DIRECTION_UPWARDS; |
---|
201 | buffstart=skiptokens(buff, 2); |
---|
202 | } |
---|
203 | |
---|
204 | /* TODO: Add a search history? */ |
---|
205 | tw = owl_viewwin_set_typwin_active(v, NULL); |
---|
206 | owl_editwin_set_locktext(tw, (direction == OWL_DIRECTION_DOWNWARDS) ? "/" : "?"); |
---|
207 | owl_editwin_insert_string(tw, buffstart); |
---|
208 | |
---|
209 | data = owl_malloc(sizeof(owl_viewwin_search_data)); |
---|
210 | data->v = v; |
---|
211 | data->direction = direction; |
---|
212 | |
---|
213 | ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline", |
---|
214 | owl_viewwin_deactivate_editcontext, v); |
---|
215 | ctx->cbdata = v; |
---|
216 | owl_global_push_context_obj(&g, ctx); |
---|
217 | owl_editwin_set_callback(tw, owl_viewwin_callback_search); |
---|
218 | owl_editwin_set_cbdata(tw, data, owl_free); |
---|
219 | return NULL; |
---|
220 | } |
---|
221 | |
---|
222 | char *owl_viewwin_start_command(owl_viewwin *v, int argc, const char *const *argv, const char *buff) |
---|
223 | { |
---|
224 | owl_editwin *tw; |
---|
225 | owl_context *ctx; |
---|
226 | |
---|
227 | buff = skiptokens(buff, 1); |
---|
228 | |
---|
229 | tw = owl_viewwin_set_typwin_active(v, owl_global_get_cmd_history(&g)); |
---|
230 | owl_editwin_set_locktext(tw, ":"); |
---|
231 | |
---|
232 | owl_editwin_insert_string(tw, buff); |
---|
233 | |
---|
234 | ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline", |
---|
235 | owl_viewwin_deactivate_editcontext, v); |
---|
236 | owl_global_push_context_obj(&g, ctx); |
---|
237 | owl_editwin_set_callback(tw, owl_callback_command); |
---|
238 | |
---|
239 | return NULL; |
---|
240 | } |
---|
241 | |
---|
242 | void owl_viewwin_deactivate_editcontext(owl_context *ctx) { |
---|
243 | owl_viewwin *v = ctx->cbdata; |
---|
244 | owl_viewwin_set_typwin_inactive(v); |
---|
245 | } |
---|
246 | |
---|
247 | owl_editwin *owl_viewwin_set_typwin_active(owl_viewwin *v, owl_history *hist) { |
---|
248 | int lines, cols; |
---|
249 | owl_editwin *cmdline; |
---|
250 | if (v->cmdwin) |
---|
251 | return NULL; |
---|
252 | /* Create the command line. */ |
---|
253 | v->cmdwin = owl_window_new(v->window); |
---|
254 | owl_viewwin_layout(v); |
---|
255 | owl_window_get_position(v->cmdwin, &lines, &cols, NULL, NULL); |
---|
256 | cmdline = owl_editwin_new(v->cmdwin, lines, cols, OWL_EDITWIN_STYLE_ONELINE, hist); |
---|
257 | /* Swap out the bottom window. */ |
---|
258 | owl_window_hide(v->status); |
---|
259 | owl_window_show(v->cmdwin); |
---|
260 | return cmdline; |
---|
261 | } |
---|
262 | |
---|
263 | void owl_viewwin_set_typwin_inactive(owl_viewwin *v) { |
---|
264 | if (v->cmdwin) { |
---|
265 | /* Swap out the bottom window. */ |
---|
266 | owl_window_hide(v->cmdwin); |
---|
267 | owl_window_show(v->status); |
---|
268 | /* Destroy the window itself. */ |
---|
269 | owl_window_unlink(v->cmdwin); |
---|
270 | g_object_unref(v->cmdwin); |
---|
271 | v->cmdwin = NULL; |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | void owl_viewwin_append_text(owl_viewwin *v, const char *text) { |
---|
276 | owl_fmtext_append_normal(&(v->fmtext), text); |
---|
277 | v->textlines=owl_fmtext_num_lines(&(v->fmtext)); |
---|
278 | owl_viewwin_dirty(v); |
---|
279 | } |
---|
280 | |
---|
281 | /* Schedule a redraw of 'v'. Exported for hooking into the search |
---|
282 | string; when we have some way of listening for changes, this can be |
---|
283 | removed. */ |
---|
284 | void owl_viewwin_dirty(owl_viewwin *v) |
---|
285 | { |
---|
286 | owl_window_dirty(v->content); |
---|
287 | owl_window_dirty(v->status); |
---|
288 | } |
---|
289 | |
---|
290 | void owl_viewwin_down(owl_viewwin *v, int amount) { |
---|
291 | int winlines; |
---|
292 | owl_window_get_position(v->content, &winlines, 0, 0, 0); |
---|
293 | /* Don't scroll past the bottom. */ |
---|
294 | amount = MIN(amount, v->textlines - (v->topline + winlines)); |
---|
295 | /* But if we're already past the bottom, don't back up either. */ |
---|
296 | if (amount > 0) { |
---|
297 | v->topline += amount; |
---|
298 | owl_viewwin_dirty(v); |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | void owl_viewwin_up(owl_viewwin *v, int amount) |
---|
303 | { |
---|
304 | v->topline -= amount; |
---|
305 | if (v->topline<0) v->topline=0; |
---|
306 | owl_viewwin_dirty(v); |
---|
307 | } |
---|
308 | |
---|
309 | void owl_viewwin_pagedown(owl_viewwin *v) |
---|
310 | { |
---|
311 | int winlines; |
---|
312 | owl_window_get_position(v->content, &winlines, 0, 0, 0); |
---|
313 | owl_viewwin_down(v, winlines); |
---|
314 | } |
---|
315 | |
---|
316 | void owl_viewwin_linedown(owl_viewwin *v) |
---|
317 | { |
---|
318 | owl_viewwin_down(v, 1); |
---|
319 | } |
---|
320 | |
---|
321 | void owl_viewwin_pageup(owl_viewwin *v) |
---|
322 | { |
---|
323 | int winlines; |
---|
324 | owl_window_get_position(v->content, &winlines, 0, 0, 0); |
---|
325 | owl_viewwin_up(v, winlines); |
---|
326 | } |
---|
327 | |
---|
328 | void owl_viewwin_lineup(owl_viewwin *v) |
---|
329 | { |
---|
330 | owl_viewwin_up(v, 1); |
---|
331 | } |
---|
332 | |
---|
333 | void owl_viewwin_right(owl_viewwin *v, int n) |
---|
334 | { |
---|
335 | v->rightshift+=n; |
---|
336 | owl_viewwin_dirty(v); |
---|
337 | } |
---|
338 | |
---|
339 | void owl_viewwin_left(owl_viewwin *v, int n) |
---|
340 | { |
---|
341 | v->rightshift-=n; |
---|
342 | if (v->rightshift<0) v->rightshift=0; |
---|
343 | owl_viewwin_dirty(v); |
---|
344 | } |
---|
345 | |
---|
346 | void owl_viewwin_top(owl_viewwin *v) |
---|
347 | { |
---|
348 | v->topline=0; |
---|
349 | v->rightshift=0; |
---|
350 | owl_viewwin_dirty(v); |
---|
351 | } |
---|
352 | |
---|
353 | void owl_viewwin_bottom(owl_viewwin *v) |
---|
354 | { |
---|
355 | int winlines; |
---|
356 | owl_window_get_position(v->content, &winlines, 0, 0, 0); |
---|
357 | v->topline = v->textlines - winlines; |
---|
358 | owl_viewwin_dirty(v); |
---|
359 | } |
---|
360 | |
---|
361 | /* This is a bit of a hack, because regexec doesn't have an 'n' |
---|
362 | * version. */ |
---|
363 | static int _re_memcompare(const owl_regex *re, const char *string, int start, int end) |
---|
364 | { |
---|
365 | int ans; |
---|
366 | char *tmp = g_strndup(string + start, end - start); |
---|
367 | ans = owl_regex_compare(re, tmp, NULL, NULL); |
---|
368 | g_free(tmp); |
---|
369 | return !ans; |
---|
370 | } |
---|
371 | |
---|
372 | /* Scroll in 'direction' to the next line containing 're' in 'v', |
---|
373 | * starting from the current line. Returns 0 if no occurrence is |
---|
374 | * found. |
---|
375 | * |
---|
376 | * If consider_current is true then stay on the current line |
---|
377 | * if it matches. |
---|
378 | */ |
---|
379 | int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int consider_current, int direction) |
---|
380 | { |
---|
381 | int start, end, offset; |
---|
382 | int lineend, linestart; |
---|
383 | const char *buf, *linestartp; |
---|
384 | owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end); |
---|
385 | if (direction == OWL_DIRECTION_DOWNWARDS) { |
---|
386 | offset = owl_fmtext_search(&v->fmtext, re, |
---|
387 | consider_current ? start : end); |
---|
388 | if (offset < 0) |
---|
389 | return 0; |
---|
390 | v->topline = owl_fmtext_line_number(&v->fmtext, offset); |
---|
391 | owl_viewwin_dirty(v); |
---|
392 | return 1; |
---|
393 | } else { |
---|
394 | /* TODO: This is a hack. Really, we should have an owl_fmlines or |
---|
395 | * something containing an array of owl_fmtext split into |
---|
396 | * lines. Also, it cannot handle multi-line regex, if we ever care about |
---|
397 | * them. */ |
---|
398 | buf = owl_fmtext_get_text(&v->fmtext); |
---|
399 | lineend = consider_current ? end : start; |
---|
400 | while (lineend > 0) { |
---|
401 | linestartp = memrchr(buf, '\n', lineend - 1); |
---|
402 | linestart = linestartp ? linestartp - buf + 1 : 0; |
---|
403 | if (_re_memcompare(re, buf, linestart, lineend)) { |
---|
404 | v->topline = owl_fmtext_line_number(&v->fmtext, linestart); |
---|
405 | owl_viewwin_dirty(v); |
---|
406 | return 1; |
---|
407 | } |
---|
408 | lineend = linestart; |
---|
409 | } |
---|
410 | return 0; |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | void owl_viewwin_delete(owl_viewwin *v) |
---|
415 | { |
---|
416 | if (v->onclose_hook) { |
---|
417 | v->onclose_hook(v, v->onclose_hook_data); |
---|
418 | v->onclose_hook = NULL; |
---|
419 | v->onclose_hook_data = NULL; |
---|
420 | } |
---|
421 | owl_viewwin_set_typwin_inactive(v); |
---|
422 | /* TODO: This is far too tedious. owl_viewwin should own v->window |
---|
423 | * and just unlink it in one go. Signals should also go away for |
---|
424 | * free. */ |
---|
425 | g_signal_handler_disconnect(v->window, v->sig_resize_id); |
---|
426 | g_signal_handler_disconnect(v->content, v->sig_content_redraw_id); |
---|
427 | g_signal_handler_disconnect(v->status, v->sig_status_redraw_id); |
---|
428 | owl_window_unlink(v->content); |
---|
429 | owl_window_unlink(v->status); |
---|
430 | g_object_unref(v->window); |
---|
431 | g_object_unref(v->content); |
---|
432 | g_object_unref(v->status); |
---|
433 | owl_fmtext_cleanup(&(v->fmtext)); |
---|
434 | owl_free(v); |
---|
435 | } |
---|