source: viewwin.c @ f640876

release-1.10release-1.7release-1.8release-1.9
Last change on this file since f640876 was cedbe9d, checked in by David Benjamin <davidben@mit.edu>, 13 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
Line 
1#include <string.h>
2#include "owl.h"
3
4#define BOTTOM_OFFSET 1
5
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);
9static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w);
10
11/* Create a viewwin.  'win' is an already initialized owl_window that
12 * will be used by the viewwin
13 */
14owl_viewwin *owl_viewwin_new_text(owl_window *win, const char *text)
15{
16  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
17  memset(v, 0, sizeof(*v));
18  owl_fmtext_init_null(&(v->fmtext));
19  if (text) {
20    owl_fmtext_append_normal(&(v->fmtext), text);
21    if (text[0] != '\0' && text[strlen(text) - 1] != '\n') {
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;
28  v->onclose_hook = NULL;
29
30  owl_viewwin_set_window(v, win);
31  return v;
32}
33
34/* Create a viewwin.  'win' is an already initialized owl_window that
35 * will be used by the viewwin
36 */
37owl_viewwin *owl_viewwin_new_fmtext(owl_window *win, const owl_fmtext *fmtext)
38{
39  char *text;
40  owl_viewwin *v = owl_malloc(sizeof(owl_viewwin));
41  memset(v, 0, sizeof(*v));
42
43  owl_fmtext_copy(&(v->fmtext), fmtext);
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  }
48  owl_free(text);
49  v->textlines=owl_fmtext_num_lines(&(v->fmtext));
50  v->topline=0;
51  v->rightshift=0;
52
53  owl_viewwin_set_window(v, win);
54  return v;
55}
56
57static void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
58{
59  v->window = g_object_ref(w);
60  v->content = owl_window_new(v->window);
61  v->status = owl_window_new(v->window);
62  v->cmdwin = NULL;
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);
74}
75
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
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);
86  /* Only one of these will be visible at a time: */
87  owl_window_set_position(v->status, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
88  if (v->cmdwin)
89    owl_window_set_position(v->cmdwin, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
90}
91
92/* regenerate text on the curses window. */
93static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data)
94{
95  owl_fmtext fm1, fm2;
96  owl_viewwin *v = user_data;
97  int winlines, wincols;
98
99  owl_window_get_position(w, &winlines, &wincols, 0, 0);
100
101  werase(curswin);
102  wmove(curswin, 0, 0);
103
104  owl_fmtext_init_null(&fm1);
105  owl_fmtext_init_null(&fm2);
106
107  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines, &fm1);
108  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
109
110  owl_fmtext_curs_waddstr(&fm2, curswin);
111
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);
125  wattrset(curswin, A_REVERSE);
126  if (v->textlines - v->topline > winlines) {
127    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
128  } else {
129    waddstr(curswin, "--End-- (Press 'q' to quit)");
130  }
131  wattroff(curswin, A_REVERSE);
132}
133
134char *owl_viewwin_command_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
135{
136  int direction, consider_current;
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"))) {
147    consider_current = false;
148  } else {
149    owl_function_set_search(buffstart);
150    consider_current = true;
151  }
152
153  if (!owl_viewwin_search(v, owl_global_get_search_re(&g), consider_current, direction))
154    owl_function_error("No more matches");
155  return NULL;
156}
157
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{
165  int consider_current = false;
166  const char *line = owl_editwin_get_text(e);
167  owl_viewwin_search_data *data = owl_editwin_get_cbdata(e);
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  }
174  if (!owl_viewwin_search(data->v, owl_global_get_search_re(&g),
175                          consider_current, data->direction))
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
203  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
204                            owl_viewwin_deactivate_editcontext, v);
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);
209  /* We aren't saving tw, so release the reference we were given. */
210  owl_editwin_unref(tw);
211  return NULL;
212}
213
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
226  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline",
227                            owl_viewwin_deactivate_editcontext, v);
228  owl_global_push_context_obj(&g, ctx);
229  owl_editwin_set_callback(tw, owl_callback_command);
230  /* We aren't saving tw, so release the reference we were given. */
231  owl_editwin_unref(tw);
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;
243  owl_editwin *cmdline;
244  if (v->cmdwin)
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);
250  cmdline = owl_editwin_new(v->cmdwin, lines, cols, OWL_EDITWIN_STYLE_ONELINE, hist);
251  /* Swap out the bottom window. */
252  owl_window_hide(v->status);
253  owl_window_show(v->cmdwin);
254  return cmdline;
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
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{
280  owl_window_dirty(v->content);
281  owl_window_dirty(v->status);
282}
283
284void owl_viewwin_down(owl_viewwin *v, int amount) {
285  int winlines;
286  owl_window_get_position(v->content, &winlines, 0, 0, 0);
287  /* Don't scroll past the bottom. */
288  amount = MIN(amount, v->textlines - (v->topline + winlines));
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);
293  }
294}
295
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)
304{
305  int winlines;
306  owl_window_get_position(v->content, &winlines, 0, 0, 0);
307  owl_viewwin_down(v, winlines);
308}
309
310void owl_viewwin_linedown(owl_viewwin *v)
311{
312  owl_viewwin_down(v, 1);
313}
314
315void owl_viewwin_pageup(owl_viewwin *v)
316{
317  int winlines;
318  owl_window_get_position(v->content, &winlines, 0, 0, 0);
319  owl_viewwin_up(v, winlines);
320}
321
322void owl_viewwin_lineup(owl_viewwin *v)
323{
324  owl_viewwin_up(v, 1);
325}
326
327void owl_viewwin_right(owl_viewwin *v, int n)
328{
329  v->rightshift+=n;
330  owl_viewwin_dirty(v);
331}
332
333void owl_viewwin_left(owl_viewwin *v, int n)
334{
335  v->rightshift-=n;
336  if (v->rightshift<0) v->rightshift=0;
337  owl_viewwin_dirty(v);
338}
339
340void owl_viewwin_top(owl_viewwin *v)
341{
342  v->topline=0;
343  v->rightshift=0;
344  owl_viewwin_dirty(v);
345}
346
347void owl_viewwin_bottom(owl_viewwin *v)
348{
349  int winlines;
350  owl_window_get_position(v->content, &winlines, 0, 0, 0);
351  v->topline = v->textlines - winlines;
352  owl_viewwin_dirty(v);
353}
354
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
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 *
370 * If consider_current is true then stay on the current line
371 * if it matches.
372 */
373int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int consider_current, int direction)
374{
375  int start, end, offset;
376  int lineend, linestart;
377  const char *buf, *linestartp;
378  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
379  if (direction == OWL_DIRECTION_DOWNWARDS) {
380    offset = owl_fmtext_search(&v->fmtext, re,
381                               consider_current ? start : end);
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 {
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);
393    lineend = consider_current ? end : start;
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;
405  }
406}
407
408void owl_viewwin_delete(owl_viewwin *v)
409{
410  if (v->onclose_hook) {
411    v->onclose_hook(v, v->onclose_hook_data);
412    v->onclose_hook = NULL;
413    v->onclose_hook_data = NULL;
414  }
415  owl_viewwin_set_typwin_inactive(v);
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);
427  owl_fmtext_cleanup(&(v->fmtext));
428  owl_free(v);
429}
Note: See TracBrowser for help on using the repository browser.