source: viewwin.c @ b6cf72f

release-1.10release-1.7release-1.8release-1.9
Last change on this file since b6cf72f was b6cf72f, checked in by David Benjamin <davidben@mit.edu>, 14 years ago
FINALLY! Add a popless:search command That took a while.
  • Property mode set to 100644
File size: 10.8 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  v->cmdwin = NULL;
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);
75}
76
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
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);
87  /* Only one of these will be visible at a time: */
88  owl_window_set_position(v->status, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
89  if (v->cmdwin)
90    owl_window_set_position(v->cmdwin, BOTTOM_OFFSET, cols, lines - BOTTOM_OFFSET, 0);
91}
92
93/* regenerate text on the curses window. */
94static void owl_viewwin_redraw_content(owl_window *w, WINDOW *curswin, void *user_data)
95{
96  owl_fmtext fm1, fm2;
97  owl_viewwin *v = user_data;
98  int winlines, wincols;
99  int y;
100
101  owl_window_get_position(w, &winlines, &wincols, 0, 0);
102
103  werase(curswin);
104  wmove(curswin, 0, 0);
105
106  owl_fmtext_init_null(&fm1);
107  owl_fmtext_init_null(&fm2);
108
109  owl_fmtext_truncate_lines(&(v->fmtext), v->topline, winlines, &fm1);
110  owl_fmtext_truncate_cols(&fm1, v->rightshift, wincols-1+v->rightshift, &fm2);
111
112  owl_fmtext_curs_waddstr(&fm2, curswin);
113
114  /* Fill remaining lines with tildes. */
115  y = v->textlines - v->topline;
116  wmove(curswin, y, 0);
117  for (; y < winlines; y++) {
118    waddstr(curswin, EMPTY_INDICATOR);
119    waddstr(curswin, "\n");
120  }
121
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);
135  wattrset(curswin, A_REVERSE);
136  if (v->textlines - v->topline > winlines) {
137    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
138  } else {
139    waddstr(curswin, "--End-- (Press 'q' to quit)");
140  }
141  wattroff(curswin, A_REVERSE);
142}
143
144char *owl_viewwin_command_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
145{
146  int direction, mode;
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"))) {
157    mode = OWL_SEARCH_CONTINUE;
158  } else {
159    owl_function_set_search(buffstart);
160    mode = OWL_SEARCH_MATCH_CURRENT;
161  }
162
163  if (!owl_viewwin_search(v, owl_global_get_search_re(&g), mode, direction))
164    owl_function_error("No more matches");
165  return NULL;
166}
167
168char *owl_viewwin_start_command(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
169{
170  owl_editwin *tw;
171  owl_context *ctx;
172
173  buff = skiptokens(buff, 1);
174
175  tw = owl_viewwin_set_typwin_active(v, owl_global_get_cmd_history(&g));
176  owl_editwin_set_locktext(tw, ":");
177
178  owl_editwin_insert_string(tw, buff);
179
180  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline");
181  ctx->deactivate_cb = owl_viewwin_deactivate_editcontext;
182  ctx->cbdata = v;
183  owl_global_push_context_obj(&g, ctx);
184  owl_editwin_set_callback(tw, owl_callback_command);
185
186  return NULL;
187}
188
189void owl_viewwin_deactivate_editcontext(owl_context *ctx) {
190  owl_viewwin *v = ctx->cbdata;
191  owl_viewwin_set_typwin_inactive(v);
192}
193
194owl_editwin *owl_viewwin_set_typwin_active(owl_viewwin *v, owl_history *hist) {
195  int lines, cols;
196  if (v->cmdwin || v->cmdline)
197    return NULL;
198  /* Create the command line. */
199  v->cmdwin = owl_window_new(v->window);
200  owl_viewwin_layout(v);
201  owl_window_get_position(v->cmdwin, &lines, &cols, NULL, NULL);
202  v->cmdline = owl_editwin_new(v->cmdwin, lines, cols, OWL_EDITWIN_STYLE_ONELINE, hist);
203  /* Swap out the bottom window. */
204  owl_window_hide(v->status);
205  owl_window_show(v->cmdwin);
206  return v->cmdline;
207}
208
209void owl_viewwin_set_typwin_inactive(owl_viewwin *v) {
210  if (v->cmdwin) {
211    /* Swap out the bottom window. */
212    owl_window_hide(v->cmdwin);
213    owl_window_show(v->status);
214    /* Destroy the window itself. */
215    owl_window_unlink(v->cmdwin);
216    g_object_unref(v->cmdwin);
217    v->cmdwin = NULL;
218  }
219  if (v->cmdline) {
220    owl_editwin_unref(v->cmdline);
221    v->cmdline = NULL;
222  }
223}
224
225void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
226    owl_fmtext_append_normal(&(v->fmtext), text);
227    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
228    owl_viewwin_dirty(v);
229}
230
231/* Schedule a redraw of 'v'. Exported for hooking into the search
232   string; when we have some way of listening for changes, this can be
233   removed. */
234void owl_viewwin_dirty(owl_viewwin *v)
235{
236  owl_window_dirty(v->content);
237  owl_window_dirty(v->status);
238}
239
240void owl_viewwin_down(owl_viewwin *v, int amount) {
241  int winlines;
242  owl_window_get_position(v->content, &winlines, 0, 0, 0);
243  /* Don't scroll past the bottom. */
244  amount = MIN(amount, v->textlines - (v->topline + winlines));
245  /* But if we're already past the bottom, don't back up either. */
246  if (amount > 0) {
247    v->topline += amount;
248    owl_viewwin_dirty(v);
249  }
250}
251
252void owl_viewwin_up(owl_viewwin *v, int amount)
253{
254  v->topline -= amount;
255  if (v->topline<0) v->topline=0;
256  owl_viewwin_dirty(v);
257}
258
259void owl_viewwin_pagedown(owl_viewwin *v)
260{
261  int winlines;
262  owl_window_get_position(v->content, &winlines, 0, 0, 0);
263  owl_viewwin_down(v, winlines);
264}
265
266void owl_viewwin_linedown(owl_viewwin *v)
267{
268  owl_viewwin_down(v, 1);
269}
270
271void owl_viewwin_pageup(owl_viewwin *v)
272{
273  int winlines;
274  owl_window_get_position(v->content, &winlines, 0, 0, 0);
275  owl_viewwin_up(v, winlines);
276}
277
278void owl_viewwin_lineup(owl_viewwin *v)
279{
280  owl_viewwin_up(v, 1);
281}
282
283void owl_viewwin_right(owl_viewwin *v, int n)
284{
285  v->rightshift+=n;
286  owl_viewwin_dirty(v);
287}
288
289void owl_viewwin_left(owl_viewwin *v, int n)
290{
291  v->rightshift-=n;
292  if (v->rightshift<0) v->rightshift=0;
293  owl_viewwin_dirty(v);
294}
295
296void owl_viewwin_top(owl_viewwin *v)
297{
298  v->topline=0;
299  v->rightshift=0;
300  owl_viewwin_dirty(v);
301}
302
303void owl_viewwin_bottom(owl_viewwin *v)
304{
305  int winlines;
306  owl_window_get_position(v->content, &winlines, 0, 0, 0);
307  v->topline = v->textlines - winlines;
308  owl_viewwin_dirty(v);
309}
310
311/* This is a bit of a hack, because regexec doesn't have an 'n'
312 * version. */
313static int _re_memcompare(const owl_regex *re, const char *string, int start, int end)
314{
315  int ans;
316  char *tmp = g_strndup(string + start, end - start);
317  ans = owl_regex_compare(re, tmp, NULL, NULL);
318  g_free(tmp);
319  return !ans;
320}
321
322/* Scroll in 'direction' to the next line containing 're' in 'v',
323 * starting from the current line. Returns 0 if no occurrence is
324 * found.
325 *
326 * If mode is OWL_SEARCH_MATCH_CURRENT then stay on the current line
327 * if it matches.
328 */
329int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int mode, int direction)
330{
331  int start, end, offset;
332  int lineend, linestart;
333  const char *buf, *linestartp;
334  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
335  if (direction == OWL_DIRECTION_DOWNWARDS) {
336    offset = owl_fmtext_search(&v->fmtext, re,
337                               (mode == OWL_SEARCH_CONTINUE) ? end : start);
338    if (offset < 0)
339      return 0;
340    v->topline = owl_fmtext_line_number(&v->fmtext, offset);
341    owl_viewwin_dirty(v);
342    return 1;
343  } else {
344    /* TODO: This is a hack. Really, we should have an owl_fmlines or
345     * something containing an array of owl_fmtext split into
346     * lines. Also, it cannot handle multi-line regex, if we ever care about
347     * them. */
348    buf = owl_fmtext_get_text(&v->fmtext);
349    lineend = (mode == OWL_SEARCH_CONTINUE) ? start : end;
350    while (lineend > 0) {
351      linestartp = memrchr(buf, '\n', lineend - 1);
352      linestart = linestartp ? linestartp - buf + 1 : 0;
353      if (_re_memcompare(re, buf, linestart, lineend)) {
354        v->topline = owl_fmtext_line_number(&v->fmtext, linestart);
355        owl_viewwin_dirty(v);
356        return 1;
357      }
358      lineend = linestart;
359    }
360    return 0;
361  }
362}
363
364void owl_viewwin_delete(owl_viewwin *v)
365{
366  if (v->onclose_hook) {
367    v->onclose_hook(v, v->onclose_hook_data);
368    v->onclose_hook = NULL;
369    v->onclose_hook_data = NULL;
370  }
371  owl_viewwin_set_typwin_inactive(v);
372  /* TODO: This is far too tedious. owl_viewwin should own v->window
373   * and just unlink it in one go. Signals should also go away for
374   * free. */
375  g_signal_handler_disconnect(v->window, v->sig_resize_id);
376  g_signal_handler_disconnect(v->content, v->sig_content_redraw_id);
377  g_signal_handler_disconnect(v->status, v->sig_status_redraw_id);
378  owl_window_unlink(v->content);
379  owl_window_unlink(v->status);
380  g_object_unref(v->window);
381  g_object_unref(v->content);
382  g_object_unref(v->status);
383  owl_fmtext_cleanup(&(v->fmtext));
384  owl_free(v);
385}
Note: See TracBrowser for help on using the repository browser.