source: viewwin.c @ ab9bf01

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