source: viewwin.c @ 67f6d7d

release-1.10release-1.9
Last change on this file since 67f6d7d was f271129, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Fix up headers The additions to owl.h and some of the removals were done by Alejandro Sedeño <asedeno@mit.edu> in commit 77a0258b3919468fc9d7f7602588ac427ab36e6c. Notes: * I think owl.c lost the need for sys/time.h when we punted select() in favor of glib's main loop. * We don't actually need to include things like stdarg.h, stdio.h, glib/gstdio.h, glib-object.h. I think they get indirectly included via owl.h and/or glib.h. They're left in (or added in to) the files that use functions/types from them. * I'm not entirely sure what sys/socket.h is doing in message.c. It is there from the initial commit. I suspect it might have had something to do with the call to getnameinfo. message.c compiles without it, but http://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html suggests that we're supposed to include it? *shrugs* I'm leaving it in, for now. (Rather, I'll leave one copy of the #include in.)
  • Property mode set to 100644
File size: 12.3 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2
3#define BOTTOM_OFFSET 1
4
[60c1386]5static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data);
6static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data);
7static void owl_viewwin_layout(owl_viewwin *v);
[61c1f19]8static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w);
[0b9e607]9
[9eb38bb]10/* Create a viewwin.  'win' is an already initialized owl_window that
11 * will be used by the viewwin
[b2b0773]12 */
[6829afc]13CALLER_OWN owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text)
[b2b0773]14{
[96828e4]15  owl_viewwin *v = g_new0(owl_viewwin, 1);
[7d4fbcd]16  owl_fmtext_init_null(&(v->fmtext));
17  if (text) {
18    owl_fmtext_append_normal(&(v->fmtext), text);
[4083c49]19    if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
[7d4fbcd]20      owl_fmtext_append_normal(&(v->fmtext), "\n");
21    }
22    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
23  }
24  v->topline=0;
25  v->rightshift=0;
[afbf668]26  v->onclose_hook = NULL;
[68f63a2]27
28  owl_viewwin_set_window(v, win);
[9eb38bb]29  return v;
[afbf668]30}
31
[9eb38bb]32/* Create a viewwin.  'win' is an already initialized owl_window that
33 * will be used by the viewwin
[b2b0773]34 */
[6829afc]35CALLER_OWN owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext)
[b2b0773]36{
[dd6af02]37  char *text;
[96828e4]38  owl_viewwin *v = g_new0(owl_viewwin, 1);
[dd6af02]39
[7d4fbcd]40  owl_fmtext_copy(&(v->fmtext), fmtext);
[dd6af02]41  text = owl_fmtext_print_plain(fmtext);
42  if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
43      owl_fmtext_append_normal(&(v->fmtext), "\n");
44  }
[ddbbcffa]45  g_free(text);
[7d4fbcd]46  v->textlines=owl_fmtext_num_lines(&(v->fmtext));
47  v->topline=0;
48  v->rightshift=0;
[68f63a2]49
50  owl_viewwin_set_window(v, win);
[9eb38bb]51  return v;
[7d4fbcd]52}
53
[61c1f19]54static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
[b2b0773]55{
[60c1386]56  v->window = g_object_ref(w);
57  v->content = owl_window_new(v->window);
58  v->status = owl_window_new(v->window);
[09ceee3]59  v->cmdwin = NULL;
[60c1386]60
61  v->sig_content_redraw_id =
62    g_signal_connect(v->content, "redraw", G_CALLBACK(owl_viewwin_redraw_content), v);
63  v->sig_status_redraw_id =
64    g_signal_connect(v->status, "redraw", G_CALLBACK(owl_viewwin_redraw_status), v);
65  v->sig_resize_id =
66    g_signal_connect_swapped(v->window, "resized", G_CALLBACK(owl_viewwin_layout), v);
67  owl_viewwin_layout(v);
68
69  owl_window_show(v->content);
70  owl_window_show(v->status);
[7d4fbcd]71}
72
[afbf668]73void owl_viewwin_set_onclose_hook(owl_viewwin *v, void (*onclose_hook) (owl_viewwin *vwin, void *data), void *onclose_hook_data) {
74  v->onclose_hook = onclose_hook;
75  v->onclose_hook_data = onclose_hook_data;
76}
77
[60c1386]78static void owl_viewwin_layout(owl_viewwin *v)
79{
80  int lines, cols;
81  owl_window_get_position(v->window, &lines, &cols, NULL, NULL);
82  owl_window_set_position(v->content, lines - BOTTOM_OFFSET, cols, 0, 0);
[09ceee3]83  /* Only one of these will be visible at a time: */
[60c1386]84  owl_window_set_position(v->status, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
[09ceee3]85  if (v->cmdwin)
86    owl_window_set_position(v->cmdwin, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
[60c1386]87}
88
[b2b0773]89/* regenerate text on the curses window. */
[60c1386]90static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data)
[b2b0773]91{
[7d4fbcd]92  owl_fmtext fm1, fm2;
[68f63a2]93  owl_viewwin *v = user_data;
[c447d9c]94  int winlines, wincols;
95
96  owl_window_get_position(w, &winlines, &wincols, 0, 0);
[60c1386]97
[68f63a2]98  werase(curswin);
99  wmove(curswin, 0, 0);
[7d4fbcd]100
[af2ca19]101  owl_fmtext_init_null(&fm1);
102  owl_fmtext_init_null(&fm2);
[60c1386]103
104  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines, &fm1);
[c447d9c]105  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
[7d4fbcd]106
[237d02c]107  owl_fmtext_curs_waddstr(&fm2, curswin, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
[7d4fbcd]108
[60c1386]109  owl_fmtext_cleanup(&fm1);
110  owl_fmtext_cleanup(&fm2);
111}
112
113static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data)
114{
115  owl_viewwin *v = user_data;
116  int winlines, wincols;
117
118  owl_window_get_position(v->content, &winlines, &wincols, 0, 0);
119
120  werase(curswin);
121  wmove(curswin, 0, 0);
[68f63a2]122  wattrset(curswin, A_REVERSE);
[60c1386]123  if (v->textlines - v->topline > winlines) {
[68f63a2]124    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
[7d4fbcd]125  } else {
[68f63a2]126    waddstr(curswin, "--End-- (Press 'q' to quit)");
[7d4fbcd]127  }
[68f63a2]128  wattroff(curswin, A_REVERSE);
[7d4fbcd]129}
130
[b6cf72f]131char *owl_viewwin_command_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
132{
[118c919]133  int direction, consider_current;
[b6cf72f]134  const char *buffstart;
135
136  direction=OWL_DIRECTION_DOWNWARDS;
137  buffstart=skiptokens(buff, 1);
138  if (argc>1 && !strcmp(argv[1], "-r")) {
139    direction=OWL_DIRECTION_UPWARDS;
140    buffstart=skiptokens(buff, 2);
141  }
142
143  if (argc==1 || (argc==2 && !strcmp(argv[1], "-r"))) {
[118c919]144    consider_current = false;
[b6cf72f]145  } else {
146    owl_function_set_search(buffstart);
[118c919]147    consider_current = true;
[b6cf72f]148  }
149
[118c919]150  if (!owl_viewwin_search(v, owl_global_get_search_re(&g), consider_current, direction))
[4fd211f]151    owl_function_makemsg("No more matches");
[b6cf72f]152  return NULL;
153}
154
[d2fd2f7]155typedef struct _owl_viewwin_search_data { /*noproto*/
156  owl_viewwin *v;
157  int direction;
158} owl_viewwin_search_data;
159
160static void owl_viewwin_callback_search(owl_editwin *e)
161{
[09065ed]162  int consider_current = false;
[d2fd2f7]163  const char *line = owl_editwin_get_text(e);
164  owl_viewwin_search_data *data = owl_editwin_get_cbdata(e);
[09065ed]165
166  /* Given an empty string, just continue the current search. */
167  if (line && *line) {
168    owl_function_set_search(line);
169    consider_current = true;
170  }
[d2fd2f7]171  if (!owl_viewwin_search(data->v, owl_global_get_search_re(&g),
[09065ed]172                          consider_current, data->direction))
[4fd211f]173    owl_function_makemsg("No matches");
[d2fd2f7]174}
175
176char *owl_viewwin_command_start_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
177{
178  int direction;
179  const char *buffstart;
180  owl_editwin *tw;
181  owl_context *ctx;
182  owl_viewwin_search_data *data;
183
184  direction=OWL_DIRECTION_DOWNWARDS;
185  buffstart=skiptokens(buff, 1);
186  if (argc>1 && !strcmp(argv[1], "-r")) {
187    direction=OWL_DIRECTION_UPWARDS;
188    buffstart=skiptokens(buff, 2);
189  }
190
191  /* TODO: Add a search history? */
192  tw = owl_viewwin_set_typwin_active(v, NULL);
193  owl_editwin_set_locktext(tw, (direction == OWL_DIRECTION_DOWNWARDS) ? "/" : "?");
194  owl_editwin_insert_string(tw, buffstart);
195
[96828e4]196  data = g_new(owl_viewwin_search_data, 1);
[d2fd2f7]197  data->v = v;
198  data->direction = direction;
199
[4a41f16]200  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
201                            owl_viewwin_deactivate_editcontext, v);
[d2fd2f7]202  ctx->cbdata = v;
203  owl_global_push_context_obj(&g, ctx);
204  owl_editwin_set_callback(tw, owl_viewwin_callback_search);
[ddbbcffa]205  owl_editwin_set_cbdata(tw, data, g_free);
[4c0ac34]206  /* We aren't saving tw, so release the reference we were given. */
207  owl_editwin_unref(tw);
[d2fd2f7]208  return NULL;
209}
210
[09ceee3]211char *owl_viewwin_start_command(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
212{
213  owl_editwin *tw;
214  owl_context *ctx;
215
216  buff = skiptokens(buff, 1);
217
218  tw = owl_viewwin_set_typwin_active(v, owl_global_get_cmd_history(&g));
219  owl_editwin_set_locktext(tw, ":");
220
221  owl_editwin_insert_string(tw, buff);
222
[4a41f16]223  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
224                            owl_viewwin_deactivate_editcontext, v);
[09ceee3]225  owl_global_push_context_obj(&g, ctx);
226  owl_editwin_set_callback(tw, owl_callback_command);
[4c0ac34]227  /* We aren't saving tw, so release the reference we were given. */
228  owl_editwin_unref(tw);
[09ceee3]229
230  return NULL;
231}
232
233void owl_viewwin_deactivate_editcontext(owl_context *ctx) {
234  owl_viewwin *v = ctx->cbdata;
235  owl_viewwin_set_typwin_inactive(v);
236}
237
[6829afc]238CALLER_OWN owl_editwin *owl_viewwin_set_typwin_active(owl_viewwin *v, owl_history *hist) {
[09ceee3]239  int lines, cols;
[1b8c3f8]240  owl_editwin *cmdline;
241  if (v->cmdwin)
[09ceee3]242    return NULL;
243  /* Create the command line. */
244  v->cmdwin = owl_window_new(v->window);
245  owl_viewwin_layout(v);
246  owl_window_get_position(v->cmdwin, &lines, &cols, NULL, NULL);
[1b8c3f8]247  cmdline = owl_editwin_new(v->cmdwin, lines, cols, OWL_EDITWIN_STYLE_ONELINE, hist);
[09ceee3]248  /* Swap out the bottom window. */
249  owl_window_hide(v->status);
250  owl_window_show(v->cmdwin);
[1b8c3f8]251  return cmdline;
[09ceee3]252}
253
254void owl_viewwin_set_typwin_inactive(owl_viewwin *v) {
255  if (v->cmdwin) {
256    /* Swap out the bottom window. */
257    owl_window_hide(v->cmdwin);
258    owl_window_show(v->status);
259    /* Destroy the window itself. */
260    owl_window_unlink(v->cmdwin);
261    g_object_unref(v->cmdwin);
262    v->cmdwin = NULL;
263  }
264}
265
[2640b63]266void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
267    owl_fmtext_append_normal(&(v->fmtext), text);
268    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
269    owl_viewwin_dirty(v);
270}
271
272/* Schedule a redraw of 'v'. Exported for hooking into the search
273   string; when we have some way of listening for changes, this can be
274   removed. */
275void owl_viewwin_dirty(owl_viewwin *v)
276{
[60c1386]277  owl_window_dirty(v->content);
278  owl_window_dirty(v->status);
[2640b63]279}
280
[d574d61]281void owl_viewwin_down(owl_viewwin *v, int amount) {
[c447d9c]282  int winlines;
[60c1386]283  owl_window_get_position(v->content, &winlines, 0, 0, 0);
[fa23002]284  /* Don't scroll past the bottom. */
[60c1386]285  amount = MIN(amount, v->textlines - (v->topline + winlines));
[fa23002]286  /* But if we're already past the bottom, don't back up either. */
287  if (amount > 0) {
288    v->topline += amount;
289    owl_viewwin_dirty(v);
[7d4fbcd]290  }
291}
292
[d574d61]293void owl_viewwin_up(owl_viewwin *v, int amount)
294{
295  v->topline -= amount;
296  if (v->topline<0) v->topline=0;
297  owl_viewwin_dirty(v);
298}
299
300void owl_viewwin_pagedown(owl_viewwin *v)
[b2b0773]301{
[c447d9c]302  int winlines;
[60c1386]303  owl_window_get_position(v->content, &winlines, 0, 0, 0);
304  owl_viewwin_down(v, winlines);
[d574d61]305}
306
307void owl_viewwin_linedown(owl_viewwin *v)
308{
309  owl_viewwin_down(v, 1);
[7d4fbcd]310}
311
[b2b0773]312void owl_viewwin_pageup(owl_viewwin *v)
313{
[c447d9c]314  int winlines;
[60c1386]315  owl_window_get_position(v->content, &winlines, 0, 0, 0);
316  owl_viewwin_up(v, winlines);
[7d4fbcd]317}
318
[b2b0773]319void owl_viewwin_lineup(owl_viewwin *v)
320{
[d574d61]321  owl_viewwin_up(v, 1);
[7d4fbcd]322}
323
[b2b0773]324void owl_viewwin_right(owl_viewwin *v, int n)
325{
[7d4fbcd]326  v->rightshift+=n;
[5b68c05]327  owl_viewwin_dirty(v);
[7d4fbcd]328}
329
[b2b0773]330void owl_viewwin_left(owl_viewwin *v, int n)
331{
[7d4fbcd]332  v->rightshift-=n;
333  if (v->rightshift<0) v->rightshift=0;
[5b68c05]334  owl_viewwin_dirty(v);
[7d4fbcd]335}
336
[b2b0773]337void owl_viewwin_top(owl_viewwin *v)
338{
[7d4fbcd]339  v->topline=0;
340  v->rightshift=0;
[5b68c05]341  owl_viewwin_dirty(v);
[7d4fbcd]342}
343
[b2b0773]344void owl_viewwin_bottom(owl_viewwin *v)
345{
[c447d9c]346  int winlines;
[60c1386]347  owl_window_get_position(v->content, &winlines, 0, 0, 0);
348  v->topline = v->textlines - winlines;
[5b68c05]349  owl_viewwin_dirty(v);
[7d4fbcd]350}
351
[6a35938]352/* This is a bit of a hack, because regexec doesn't have an 'n'
353 * version. */
354static int _re_memcompare(const owl_regex *re, const char *string, int start, int end)
355{
356  int ans;
357  char *tmp = g_strndup(string + start, end - start);
358  ans = owl_regex_compare(re, tmp, NULL, NULL);
359  g_free(tmp);
360  return !ans;
361}
362
[b8742ba]363/* Scroll in 'direction' to the next line containing 're' in 'v',
364 * starting from the current line. Returns 0 if no occurrence is
365 * found.
366 *
[118c919]367 * If consider_current is true then stay on the current line
[9c1e61d4]368 * if it matches.
[b8742ba]369 */
[118c919]370int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int consider_current, int direction)
[b8742ba]371{
372  int start, end, offset;
[6a35938]373  int lineend, linestart;
374  const char *buf, *linestartp;
[b8742ba]375  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
376  if (direction == OWL_DIRECTION_DOWNWARDS) {
[9c1e61d4]377    offset = owl_fmtext_search(&v->fmtext, re,
[118c919]378                               consider_current ? start : end);
[b8742ba]379    if (offset < 0)
380      return 0;
381    v->topline = owl_fmtext_line_number(&v->fmtext, offset);
382    owl_viewwin_dirty(v);
383    return 1;
384  } else {
[6a35938]385    /* TODO: This is a hack. Really, we should have an owl_fmlines or
386     * something containing an array of owl_fmtext split into
387     * lines. Also, it cannot handle multi-line regex, if we ever care about
388     * them. */
389    buf = owl_fmtext_get_text(&v->fmtext);
[118c919]390    lineend = consider_current ? end : start;
[6a35938]391    while (lineend > 0) {
392      linestartp = memrchr(buf, '\n', lineend - 1);
393      linestart = linestartp ? linestartp - buf + 1 : 0;
394      if (_re_memcompare(re, buf, linestart, lineend)) {
395        v->topline = owl_fmtext_line_number(&v->fmtext, linestart);
396        owl_viewwin_dirty(v);
397        return 1;
398      }
399      lineend = linestart;
400    }
401    return 0;
[b8742ba]402  }
403}
404
[9eb38bb]405void owl_viewwin_delete(owl_viewwin *v)
[b2b0773]406{
[afbf668]407  if (v->onclose_hook) {
408    v->onclose_hook(v, v->onclose_hook_data);
[8721756]409    v->onclose_hook = NULL;
410    v->onclose_hook_data = NULL;
[afbf668]411  }
[09ceee3]412  owl_viewwin_set_typwin_inactive(v);
[60c1386]413  /* TODO: This is far too tedious. owl_viewwin should own v->window
414   * and just unlink it in one go. Signals should also go away for
415   * free. */
416  g_signal_handler_disconnect(v->window, v->sig_resize_id);
417  g_signal_handler_disconnect(v->content, v->sig_content_redraw_id);
418  g_signal_handler_disconnect(v->status, v->sig_status_redraw_id);
419  owl_window_unlink(v->content);
420  owl_window_unlink(v->status);
421  g_object_unref(v->window);
422  g_object_unref(v->content);
423  g_object_unref(v->status);
[7ab0020]424  owl_fmtext_cleanup(&(v->fmtext));
[ddbbcffa]425  g_free(v);
[7d4fbcd]426}
Note: See TracBrowser for help on using the repository browser.