source: viewwin.c @ 09ceee3

release-1.7release-1.8release-1.9
Last change on this file since 09ceee3 was 09ceee3, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Add a command-line to the viewwin It's triggered via popless:start-command.
  • Property mode set to 100644
File size: 10.1 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_start_command(owl_viewwin *v, int argc, const char *const *argv, const char *buff)
145{
146  owl_editwin *tw;
147  owl_context *ctx;
148
149  buff = skiptokens(buff, 1);
150
151  tw = owl_viewwin_set_typwin_active(v, owl_global_get_cmd_history(&g));
152  owl_editwin_set_locktext(tw, ":");
153
154  owl_editwin_insert_string(tw, buff);
155
156  ctx = owl_editcontext_new(OWL_CTX_EDITLINE, tw, "editline");
157  ctx->deactivate_cb = owl_viewwin_deactivate_editcontext;
158  ctx->cbdata = v;
159  owl_global_push_context_obj(&g, ctx);
160  owl_editwin_set_callback(tw, owl_callback_command);
161
162  return NULL;
163}
164
165void owl_viewwin_deactivate_editcontext(owl_context *ctx) {
166  owl_viewwin *v = ctx->cbdata;
167  owl_viewwin_set_typwin_inactive(v);
168}
169
170owl_editwin *owl_viewwin_set_typwin_active(owl_viewwin *v, owl_history *hist) {
171  int lines, cols;
172  if (v->cmdwin || v->cmdline)
173    return NULL;
174  /* Create the command line. */
175  v->cmdwin = owl_window_new(v->window);
176  owl_viewwin_layout(v);
177  owl_window_get_position(v->cmdwin, &lines, &cols, NULL, NULL);
178  v->cmdline = owl_editwin_new(v->cmdwin, lines, cols, OWL_EDITWIN_STYLE_ONELINE, hist);
179  /* Swap out the bottom window. */
180  owl_window_hide(v->status);
181  owl_window_show(v->cmdwin);
182  return v->cmdline;
183}
184
185void owl_viewwin_set_typwin_inactive(owl_viewwin *v) {
186  if (v->cmdwin) {
187    /* Swap out the bottom window. */
188    owl_window_hide(v->cmdwin);
189    owl_window_show(v->status);
190    /* Destroy the window itself. */
191    owl_window_unlink(v->cmdwin);
192    g_object_unref(v->cmdwin);
193    v->cmdwin = NULL;
194  }
195  if (v->cmdline) {
196    owl_editwin_unref(v->cmdline);
197    v->cmdline = NULL;
198  }
199}
200
201void owl_viewwin_append_text(owl_viewwin *v, const char *text) {
202    owl_fmtext_append_normal(&(v->fmtext), text);
203    v->textlines=owl_fmtext_num_lines(&(v->fmtext));
204    owl_viewwin_dirty(v);
205}
206
207/* Schedule a redraw of 'v'. Exported for hooking into the search
208   string; when we have some way of listening for changes, this can be
209   removed. */
210void owl_viewwin_dirty(owl_viewwin *v)
211{
212  owl_window_dirty(v->content);
213  owl_window_dirty(v->status);
214}
215
216void owl_viewwin_down(owl_viewwin *v, int amount) {
217  int winlines;
218  owl_window_get_position(v->content, &winlines, 0, 0, 0);
219  /* Don't scroll past the bottom. */
220  amount = MIN(amount, v->textlines - (v->topline + winlines));
221  /* But if we're already past the bottom, don't back up either. */
222  if (amount > 0) {
223    v->topline += amount;
224    owl_viewwin_dirty(v);
225  }
226}
227
228void owl_viewwin_up(owl_viewwin *v, int amount)
229{
230  v->topline -= amount;
231  if (v->topline<0) v->topline=0;
232  owl_viewwin_dirty(v);
233}
234
235void owl_viewwin_pagedown(owl_viewwin *v)
236{
237  int winlines;
238  owl_window_get_position(v->content, &winlines, 0, 0, 0);
239  owl_viewwin_down(v, winlines);
240}
241
242void owl_viewwin_linedown(owl_viewwin *v)
243{
244  owl_viewwin_down(v, 1);
245}
246
247void owl_viewwin_pageup(owl_viewwin *v)
248{
249  int winlines;
250  owl_window_get_position(v->content, &winlines, 0, 0, 0);
251  owl_viewwin_up(v, winlines);
252}
253
254void owl_viewwin_lineup(owl_viewwin *v)
255{
256  owl_viewwin_up(v, 1);
257}
258
259void owl_viewwin_right(owl_viewwin *v, int n)
260{
261  v->rightshift+=n;
262  owl_viewwin_dirty(v);
263}
264
265void owl_viewwin_left(owl_viewwin *v, int n)
266{
267  v->rightshift-=n;
268  if (v->rightshift<0) v->rightshift=0;
269  owl_viewwin_dirty(v);
270}
271
272void owl_viewwin_top(owl_viewwin *v)
273{
274  v->topline=0;
275  v->rightshift=0;
276  owl_viewwin_dirty(v);
277}
278
279void owl_viewwin_bottom(owl_viewwin *v)
280{
281  int winlines;
282  owl_window_get_position(v->content, &winlines, 0, 0, 0);
283  v->topline = v->textlines - winlines;
284  owl_viewwin_dirty(v);
285}
286
287/* This is a bit of a hack, because regexec doesn't have an 'n'
288 * version. */
289static int _re_memcompare(const owl_regex *re, const char *string, int start, int end)
290{
291  int ans;
292  char *tmp = g_strndup(string + start, end - start);
293  ans = owl_regex_compare(re, tmp, NULL, NULL);
294  g_free(tmp);
295  return !ans;
296}
297
298/* Scroll in 'direction' to the next line containing 're' in 'v',
299 * starting from the current line. Returns 0 if no occurrence is
300 * found.
301 *
302 * If mode==0 then stay on the current line if it matches.
303 */
304int owl_viewwin_search(owl_viewwin *v, const owl_regex *re, int mode, int direction)
305{
306  int start, end, offset;
307  int lineend, linestart;
308  const char *buf, *linestartp;
309  owl_fmtext_line_extents(&v->fmtext, v->topline, &start, &end);
310  if (direction == OWL_DIRECTION_DOWNWARDS) {
311    offset = owl_fmtext_search(&v->fmtext, re, mode ? end : start);
312    if (offset < 0)
313      return 0;
314    v->topline = owl_fmtext_line_number(&v->fmtext, offset);
315    owl_viewwin_dirty(v);
316    return 1;
317  } else {
318    /* TODO: This is a hack. Really, we should have an owl_fmlines or
319     * something containing an array of owl_fmtext split into
320     * lines. Also, it cannot handle multi-line regex, if we ever care about
321     * them. */
322    buf = owl_fmtext_get_text(&v->fmtext);
323    lineend = mode ? start : end;
324    while (lineend > 0) {
325      linestartp = memrchr(buf, '\n', lineend - 1);
326      linestart = linestartp ? linestartp - buf + 1 : 0;
327      if (_re_memcompare(re, buf, linestart, lineend)) {
328        v->topline = owl_fmtext_line_number(&v->fmtext, linestart);
329        owl_viewwin_dirty(v);
330        return 1;
331      }
332      lineend = linestart;
333    }
334    return 0;
335  }
336}
337
338void owl_viewwin_delete(owl_viewwin *v)
339{
340  if (v->onclose_hook) {
341    v->onclose_hook(v, v->onclose_hook_data);
342    v->onclose_hook = NULL;
343    v->onclose_hook_data = NULL;
344  }
345  owl_viewwin_set_typwin_inactive(v);
346  /* TODO: This is far too tedious. owl_viewwin should own v->window
347   * and just unlink it in one go. Signals should also go away for
348   * free. */
349  g_signal_handler_disconnect(v->window, v->sig_resize_id);
350  g_signal_handler_disconnect(v->content, v->sig_content_redraw_id);
351  g_signal_handler_disconnect(v->status, v->sig_status_redraw_id);
352  owl_window_unlink(v->content);
353  owl_window_unlink(v->status);
354  g_object_unref(v->window);
355  g_object_unref(v->content);
356  g_object_unref(v->status);
357  owl_fmtext_cleanup(&(v->fmtext));
358  owl_free(v);
359}
Note: See TracBrowser for help on using the repository browser.