source: viewwin.c @ 5f0bcde

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 5f0bcde was 6a35938, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
Add an unspeakably ugly version of reverse search I think we really want the viewwin to have pre-split the output into lines.
  • Property mode set to 100644
File size: 8.3 KB
Line 
1#include <string.h>
2#include "owl.h"
3
4#define BOTTOM_OFFSET 1
5#define EMPTY_INDICATOR "~"
6
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);
10static 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 */
15owl_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 */
38owl_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
58static 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
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  owl_window_set_position(v->status, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
87}
88
89/* regenerate text on the curses window. */
90static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data)
91{
92  owl_fmtext fm1, fm2;
93  owl_viewwin *v = user_data;
94  int winlines, wincols;
95  int y;
96
97  owl_window_get_position(w, &winlines, &wincols, 0, 0);
98
99  werase(curswin);
100  wmove(curswin, 0, 0);
101
102  owl_fmtext_init_null(&fm1);
103  owl_fmtext_init_null(&fm2);
104
105  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines, &fm1);
106  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
107
108  owl_fmtext_curs_waddstr(&fm2, curswin);
109
110  /* Fill remaining lines with tildes. */
111  y = v->textlines - v->topline;
112  wmove(curswin, y, 0);
113  for (; y < winlines; y++) {
114    waddstr(curswin, EMPTY_INDICATOR);
115    waddstr(curswin, "\n");
116  }
117
118  owl_fmtext_cleanup(&fm1);
119  owl_fmtext_cleanup(&fm2);
120}
121
122static void owl_viewwin_redraw_status(owl_window *w, WINDOW *curswin, void *user_data)
123{
124  owl_viewwin *v = user_data;
125  int winlines, wincols;
126
127  owl_window_get_position(v->content, &winlines, &wincols, 0, 0);
128
129  werase(curswin);
130  wmove(curswin, 0, 0);
131  wattrset(curswin, A_REVERSE);
132  if (v->textlines - v->topline > winlines) {
133    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
134  } else {
135    waddstr(curswin, "--End-- (Press 'q' to quit)");
136  }
137  wattroff(curswin, A_REVERSE);
138}
139
140void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
141    owl_fmtext_append_normal(&(v->fmtext), text);
142    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
143    owl_viewwin_dirty(v);
144}
145
146/* Schedule a redraw of 'v'. Exported for hooking into the search
147   string; when we have some way of listening for changes, this can be
148   removed. */
149void owl_viewwin_dirty(owl_viewwin *v)
150{
151  owl_window_dirty(v->content);
152  owl_window_dirty(v->status);
153}
154
155void owl_viewwin_down(owl_viewwin *v, int amount) {
156  int winlines;
157  owl_window_get_position(v->content, &winlines, 0, 0, 0);
158  /* Don't scroll past the bottom. */
159  amount = MIN(amount, v->textlines - (v->topline + winlines));
160  /* But if we're already past the bottom, don't back up either. */
161  if (amount > 0) {
162    v->topline += amount;
163    owl_viewwin_dirty(v);
164  }
165}
166
167void owl_viewwin_up(owl_viewwin *v, int amount)
168{
169  v->topline -= amount;
170  if (v->topline<0) v->topline=0;
171  owl_viewwin_dirty(v);
172}
173
174void owl_viewwin_pagedown(owl_viewwin *v)
175{
176  int winlines;
177  owl_window_get_position(v->content, &winlines, 0, 0, 0);
178  owl_viewwin_down(v, winlines);
179}
180
181void owl_viewwin_linedown(owl_viewwin *v)
182{
183  owl_viewwin_down(v, 1);
184}
185
186void owl_viewwin_pageup(owl_viewwin *v)
187{
188  int winlines;
189  owl_window_get_position(v->content, &winlines, 0, 0, 0);
190  owl_viewwin_up(v, winlines);
191}
192
193void owl_viewwin_lineup(owl_viewwin *v)
194{
195  owl_viewwin_up(v, 1);
196}
197
198void owl_viewwin_right(owl_viewwin *v, int n)
199{
200  v->rightshift+=n;
201  owl_viewwin_dirty(v);
202}
203
204void owl_viewwin_left(owl_viewwin *v, int n)
205{
206  v->rightshift-=n;
207  if (v->rightshift<0) v->rightshift=0;
208  owl_viewwin_dirty(v);
209}
210
211void owl_viewwin_top(owl_viewwin *v)
212{
213  v->topline=0;
214  v->rightshift=0;
215  owl_viewwin_dirty(v);
216}
217
218void owl_viewwin_bottom(owl_viewwin *v)
219{
220  int winlines;
221  owl_window_get_position(v->content, &winlines, 0, 0, 0);
222  v->topline = v->textlines - winlines;
223  owl_viewwin_dirty(v);
224}
225
226/* This is a bit of a hack, because regexec doesn't have an 'n'
227 * version. */
228static int _re_memcompare(const owl_regex *re, const char *string, int start, int end)
229{
230  int ans;
231  char *tmp = g_strndup(string + start, end - start);
232  ans = owl_regex_compare(re, tmp, NULL, NULL);
233  g_free(tmp);
234  return !ans;
235}
236
237/* Scroll in 'direction' to the next line containing 're' in 'v',
238 * starting from the current line. Returns 0 if no occurrence is
239 * found.
240 *
241 * If mode==0 then stay on the current line if it matches.
242 */
243int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int mode, int direction)
244{
245  int start, end, offset;
246  int lineend, linestart;
247  const char *buf, *linestartp;
248  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
249  if (direction == OWL_DIRECTION_DOWNWARDS) {
250    offset = owl_fmtext_search(&v->fmtext, re, mode ? end : start);
251    if (offset < 0)
252      return 0;
253    v->topline = owl_fmtext_line_number(&v->fmtext, offset);
254    owl_viewwin_dirty(v);
255    return 1;
256  } else {
257    /* TODO: This is a hack. Really, we should have an owl_fmlines or
258     * something containing an array of owl_fmtext split into
259     * lines. Also, it cannot handle multi-line regex, if we ever care about
260     * them. */
261    buf = owl_fmtext_get_text(&v->fmtext);
262    lineend = mode ? start : end;
263    while (lineend > 0) {
264      linestartp = memrchr(buf, '\n', lineend - 1);
265      linestart = linestartp ? linestartp - buf + 1 : 0;
266      if (_re_memcompare(re, buf, linestart, lineend)) {
267        v->topline = owl_fmtext_line_number(&v->fmtext, linestart);
268        owl_viewwin_dirty(v);
269        return 1;
270      }
271      lineend = linestart;
272    }
273    return 0;
274  }
275}
276
277void owl_viewwin_delete(owl_viewwin *v)
278{
279  if (v->onclose_hook) {
280    v->onclose_hook(v, v->onclose_hook_data);
281    v->onclose_hook = NULL;
282    v->onclose_hook_data = NULL;
283  }
284  /* TODO: This is far too tedious. owl_viewwin should own v->window
285   * and just unlink it in one go. Signals should also go away for
286   * free. */
287  g_signal_handler_disconnect(v->window, v->sig_resize_id);
288  g_signal_handler_disconnect(v->content, v->sig_content_redraw_id);
289  g_signal_handler_disconnect(v->status, v->sig_status_redraw_id);
290  owl_window_unlink(v->content);
291  owl_window_unlink(v->status);
292  g_object_unref(v->window);
293  g_object_unref(v->content);
294  g_object_unref(v->status);
295  owl_fmtext_cleanup(&(v->fmtext));
296  owl_free(v);
297}
Note: See TracBrowser for help on using the repository browser.