source: viewwin.c @ 4c0ac34

release-1.7release-1.8release-1.9
Last change on this file since 4c0ac34 was 4c0ac34, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Don't leak editwins in the viewwin All the objects we pass the editwin to take a reference, but no one ever claims the reference it was initially created with.
  • Property mode set to 100644
File size: 12.5 KB
RevLine 
[7d4fbcd]1#include <string.h>
2#include "owl.h"
3
4#define BOTTOM_OFFSET 1
[9116936]5#define EMPTY_INDICATOR "~"
[7d4fbcd]6
[60c1386]7static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data);
8static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data);
9static void owl_viewwin_layout(owl_viewwin *v);
[61c1f19]10static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w);
[0b9e607]11
[9eb38bb]12/* Create a viewwin.  'win' is an already initialized owl_window that
13 * will be used by the viewwin
[b2b0773]14 */
[9eb38bb]15owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text)
[b2b0773]16{
[9eb38bb]17  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
18  memset(v, 0, sizeof(*v));
[7d4fbcd]19  owl_fmtext_init_null(&(v->fmtext));
20  if (text) {
21    owl_fmtext_append_normal(&(v->fmtext), text);
[4083c49]22    if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
[7d4fbcd]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;
[afbf668]29  v->onclose_hook = NULL;
[68f63a2]30
31  owl_viewwin_set_window(v, win);
[9eb38bb]32  return v;
[afbf668]33}
34
[9eb38bb]35/* Create a viewwin.  'win' is an already initialized owl_window that
36 * will be used by the viewwin
[b2b0773]37 */
[9eb38bb]38owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext)
[b2b0773]39{
[dd6af02]40  char *text;
[9eb38bb]41  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
42  memset(v, 0, sizeof(*v));
[dd6af02]43
[7d4fbcd]44  owl_fmtext_copy(&(v->fmtext), fmtext);
[dd6af02]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  }
[3e55268]49  owl_free(text);
[7d4fbcd]50  v->textlines=owl_fmtext_num_lines(&(v->fmtext));
51  v->topline=0;
52  v->rightshift=0;
[68f63a2]53
54  owl_viewwin_set_window(v, win);
[9eb38bb]55  return v;
[7d4fbcd]56}
57
[61c1f19]58static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
[b2b0773]59{
[60c1386]60  v->window = g_object_ref(w);
61  v->content = owl_window_new(v->window);
62  v->status = owl_window_new(v->window);
[09ceee3]63  v->cmdwin = NULL;
[60c1386]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);
[7d4fbcd]75}
76
[afbf668]77void 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
[60c1386]82static 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);
[09ceee3]87  /* Only one of these will be visible at a time: */
[60c1386]88  owl_window_set_position(v->status, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
[09ceee3]89  if (v->cmdwin)
90    owl_window_set_position(v->cmdwin, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
[60c1386]91}
92
[b2b0773]93/* regenerate text on the curses window. */
[60c1386]94static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data)
[b2b0773]95{
[7d4fbcd]96  owl_fmtext fm1, fm2;
[68f63a2]97  owl_viewwin *v = user_data;
[c447d9c]98  int winlines, wincols;
[9116936]99  int y;
[c447d9c]100
101  owl_window_get_position(w, &winlines, &wincols, 0, 0);
[60c1386]102
[68f63a2]103  werase(curswin);
104  wmove(curswin, 0, 0);
[7d4fbcd]105
[af2ca19]106  owl_fmtext_init_null(&fm1);
107  owl_fmtext_init_null(&fm2);
[60c1386]108
109  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines, &fm1);
[c447d9c]110  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
[7d4fbcd]111
[449c682]112  owl_fmtext_curs_waddstr(&fm2, curswin);
[7d4fbcd]113
[9116936]114  /* Fill remaining lines with tildes. */
115  y = v->textlines - v->topline;
116  wmove(curswin, y, 0);
[60c1386]117  for (; y < winlines; y++) {
[9116936]118    waddstr(curswin, EMPTY_INDICATOR);
119    waddstr(curswin, "\n");
120  }
121
[60c1386]122  owl_fmtext_cleanup(&fm1);
123  owl_fmtext_cleanup(&fm2);
124}
125
126static 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);
[68f63a2]135  wattrset(curswin, A_REVERSE);
[60c1386]136  if (v->textlines - v->topline > winlines) {
[68f63a2]137    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
[7d4fbcd]138  } else {
[68f63a2]139    waddstr(curswin, "--End-- (Press 'q' to quit)");
[7d4fbcd]140  }
[68f63a2]141  wattroff(curswin, A_REVERSE);
[7d4fbcd]142}
143
[b6cf72f]144char *owl_viewwin_command_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
145{
[118c919]146  int direction, consider_current;
[b6cf72f]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"))) {
[118c919]157    consider_current = false;
[b6cf72f]158  } else {
159    owl_function_set_search(buffstart);
[118c919]160    consider_current = true;
[b6cf72f]161  }
162
[118c919]163  if (!owl_viewwin_search(v, owl_global_get_search_re(&g), consider_current, direction))
[b6cf72f]164    owl_function_error("No more matches");
165  return NULL;
166}
167
[d2fd2f7]168typedef struct _owl_viewwin_search_data { /*noproto*/
169  owl_viewwin *v;
170  int direction;
171} owl_viewwin_search_data;
172
173static void owl_viewwin_callback_search(owl_editwin *e)
174{
[09065ed]175  int consider_current = false;
[d2fd2f7]176  const char *line = owl_editwin_get_text(e);
177  owl_viewwin_search_data *data = owl_editwin_get_cbdata(e);
[09065ed]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  }
[d2fd2f7]184  if (!owl_viewwin_search(data->v, owl_global_get_search_re(&g),
[09065ed]185                          consider_current, data->direction))
[d2fd2f7]186    owl_function_error("No matches");
187}
188
189char *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
[4a41f16]213  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
214                            owl_viewwin_deactivate_editcontext, v);
[d2fd2f7]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);
[4c0ac34]219  /* We aren't saving tw, so release the reference we were given. */
220  owl_editwin_unref(tw);
[d2fd2f7]221  return NULL;
222}
223
[09ceee3]224char *owl_viewwin_start_command(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
225{
226  owl_editwin *tw;
227  owl_context *ctx;
228
229  buff = skiptokens(buff, 1);
230
231  tw = owl_viewwin_set_typwin_active(v, owl_global_get_cmd_history(&g));
232  owl_editwin_set_locktext(tw, ":");
233
234  owl_editwin_insert_string(tw, buff);
235
[4a41f16]236  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
237                            owl_viewwin_deactivate_editcontext, v);
[09ceee3]238  owl_global_push_context_obj(&g, ctx);
239  owl_editwin_set_callback(tw, owl_callback_command);
[4c0ac34]240  /* We aren't saving tw, so release the reference we were given. */
241  owl_editwin_unref(tw);
[09ceee3]242
243  return NULL;
244}
245
246void owl_viewwin_deactivate_editcontext(owl_context *ctx) {
247  owl_viewwin *v = ctx->cbdata;
248  owl_viewwin_set_typwin_inactive(v);
249}
250
251owl_editwin *owl_viewwin_set_typwin_active(owl_viewwin *v, owl_history *hist) {
252  int lines, cols;
[1b8c3f8]253  owl_editwin *cmdline;
254  if (v->cmdwin)
[09ceee3]255    return NULL;
256  /* Create the command line. */
257  v->cmdwin = owl_window_new(v->window);
258  owl_viewwin_layout(v);
259  owl_window_get_position(v->cmdwin, &lines, &cols, NULL, NULL);
[1b8c3f8]260  cmdline = owl_editwin_new(v->cmdwin, lines, cols, OWL_EDITWIN_STYLE_ONELINE, hist);
[09ceee3]261  /* Swap out the bottom window. */
262  owl_window_hide(v->status);
263  owl_window_show(v->cmdwin);
[1b8c3f8]264  return cmdline;
[09ceee3]265}
266
267void owl_viewwin_set_typwin_inactive(owl_viewwin *v) {
268  if (v->cmdwin) {
269    /* Swap out the bottom window. */
270    owl_window_hide(v->cmdwin);
271    owl_window_show(v->status);
272    /* Destroy the window itself. */
273    owl_window_unlink(v->cmdwin);
274    g_object_unref(v->cmdwin);
275    v->cmdwin = NULL;
276  }
277}
278
[2640b63]279void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
280    owl_fmtext_append_normal(&(v->fmtext), text);
281    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
282    owl_viewwin_dirty(v);
283}
284
285/* Schedule a redraw of 'v'. Exported for hooking into the search
286   string; when we have some way of listening for changes, this can be
287   removed. */
288void owl_viewwin_dirty(owl_viewwin *v)
289{
[60c1386]290  owl_window_dirty(v->content);
291  owl_window_dirty(v->status);
[2640b63]292}
293
[d574d61]294void owl_viewwin_down(owl_viewwin *v, int amount) {
[c447d9c]295  int winlines;
[60c1386]296  owl_window_get_position(v->content, &winlines, 0, 0, 0);
[fa23002]297  /* Don't scroll past the bottom. */
[60c1386]298  amount = MIN(amount, v->textlines - (v->topline + winlines));
[fa23002]299  /* But if we're already past the bottom, don't back up either. */
300  if (amount > 0) {
301    v->topline += amount;
302    owl_viewwin_dirty(v);
[7d4fbcd]303  }
304}
305
[d574d61]306void owl_viewwin_up(owl_viewwin *v, int amount)
307{
308  v->topline -= amount;
309  if (v->topline<0) v->topline=0;
310  owl_viewwin_dirty(v);
311}
312
313void owl_viewwin_pagedown(owl_viewwin *v)
[b2b0773]314{
[c447d9c]315  int winlines;
[60c1386]316  owl_window_get_position(v->content, &winlines, 0, 0, 0);
317  owl_viewwin_down(v, winlines);
[d574d61]318}
319
320void owl_viewwin_linedown(owl_viewwin *v)
321{
322  owl_viewwin_down(v, 1);
[7d4fbcd]323}
324
[b2b0773]325void owl_viewwin_pageup(owl_viewwin *v)
326{
[c447d9c]327  int winlines;
[60c1386]328  owl_window_get_position(v->content, &winlines, 0, 0, 0);
329  owl_viewwin_up(v, winlines);
[7d4fbcd]330}
331
[b2b0773]332void owl_viewwin_lineup(owl_viewwin *v)
333{
[d574d61]334  owl_viewwin_up(v, 1);
[7d4fbcd]335}
336
[b2b0773]337void owl_viewwin_right(owl_viewwin *v, int n)
338{
[7d4fbcd]339  v->rightshift+=n;
[5b68c05]340  owl_viewwin_dirty(v);
[7d4fbcd]341}
342
[b2b0773]343void owl_viewwin_left(owl_viewwin *v, int n)
344{
[7d4fbcd]345  v->rightshift-=n;
346  if (v->rightshift<0) v->rightshift=0;
[5b68c05]347  owl_viewwin_dirty(v);
[7d4fbcd]348}
349
[b2b0773]350void owl_viewwin_top(owl_viewwin *v)
351{
[7d4fbcd]352  v->topline=0;
353  v->rightshift=0;
[5b68c05]354  owl_viewwin_dirty(v);
[7d4fbcd]355}
356
[b2b0773]357void owl_viewwin_bottom(owl_viewwin *v)
358{
[c447d9c]359  int winlines;
[60c1386]360  owl_window_get_position(v->content, &winlines, 0, 0, 0);
361  v->topline = v->textlines - winlines;
[5b68c05]362  owl_viewwin_dirty(v);
[7d4fbcd]363}
364
[6a35938]365/* This is a bit of a hack, because regexec doesn't have an 'n'
366 * version. */
367static int _re_memcompare(const owl_regex *re, const char *string, int start, int end)
368{
369  int ans;
370  char *tmp = g_strndup(string + start, end - start);
371  ans = owl_regex_compare(re, tmp, NULL, NULL);
372  g_free(tmp);
373  return !ans;
374}
375
[b8742ba]376/* Scroll in 'direction' to the next line containing 're' in 'v',
377 * starting from the current line. Returns 0 if no occurrence is
378 * found.
379 *
[118c919]380 * If consider_current is true then stay on the current line
[9c1e61d]381 * if it matches.
[b8742ba]382 */
[118c919]383int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int consider_current, int direction)
[b8742ba]384{
385  int start, end, offset;
[6a35938]386  int lineend, linestart;
387  const char *buf, *linestartp;
[b8742ba]388  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
389  if (direction == OWL_DIRECTION_DOWNWARDS) {
[9c1e61d]390    offset = owl_fmtext_search(&v->fmtext, re,
[118c919]391                               consider_current ? start : end);
[b8742ba]392    if (offset < 0)
393      return 0;
394    v->topline = owl_fmtext_line_number(&v->fmtext, offset);
395    owl_viewwin_dirty(v);
396    return 1;
397  } else {
[6a35938]398    /* TODO: This is a hack. Really, we should have an owl_fmlines or
399     * something containing an array of owl_fmtext split into
400     * lines. Also, it cannot handle multi-line regex, if we ever care about
401     * them. */
402    buf = owl_fmtext_get_text(&v->fmtext);
[118c919]403    lineend = consider_current ? end : start;
[6a35938]404    while (lineend > 0) {
405      linestartp = memrchr(buf, '\n', lineend - 1);
406      linestart = linestartp ? linestartp - buf + 1 : 0;
407      if (_re_memcompare(re, buf, linestart, lineend)) {
408        v->topline = owl_fmtext_line_number(&v->fmtext, linestart);
409        owl_viewwin_dirty(v);
410        return 1;
411      }
412      lineend = linestart;
413    }
414    return 0;
[b8742ba]415  }
416}
417
[9eb38bb]418void owl_viewwin_delete(owl_viewwin *v)
[b2b0773]419{
[afbf668]420  if (v->onclose_hook) {
421    v->onclose_hook(v, v->onclose_hook_data);
[8721756]422    v->onclose_hook = NULL;
423    v->onclose_hook_data = NULL;
[afbf668]424  }
[09ceee3]425  owl_viewwin_set_typwin_inactive(v);
[60c1386]426  /* TODO: This is far too tedious. owl_viewwin should own v->window
427   * and just unlink it in one go. Signals should also go away for
428   * free. */
429  g_signal_handler_disconnect(v->window, v->sig_resize_id);
430  g_signal_handler_disconnect(v->content, v->sig_content_redraw_id);
431  g_signal_handler_disconnect(v->status, v->sig_status_redraw_id);
432  owl_window_unlink(v->content);
433  owl_window_unlink(v->status);
434  g_object_unref(v->window);
435  g_object_unref(v->content);
436  g_object_unref(v->status);
[7ab0020]437  owl_fmtext_cleanup(&(v->fmtext));
[9eb38bb]438  owl_free(v);
[7d4fbcd]439}
Note: See TracBrowser for help on using the repository browser.