source: editwin.c @ 919cbf2

release-1.10release-1.9
Last change on this file since 919cbf2 was 7803326, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
editwin callback for canceling the editwin The code for editwin callbacks (called when the editwin is created) has been extended so that callbacks are called when the editwin is canceled. The old (perl) editwin callbacks still exist and have the same functionality that they always have. They are now deprecated.
  • Property mode set to 100644
File size: 32.8 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2
[ebf0128]3#define VALID_EXCURSION (0x9a2b4729)
4
[a88f35a]5typedef struct _owl_editwin_excursion { /*noproto*/
[ebf0128]6  int valid;
7  int index;
[16cfd12a]8  int mark;
[ebf0128]9  int goal_column;
10  int lock;
[a88f35a]11  struct _owl_editwin_excursion *next;
[ebf0128]12} oe_excursion;
13
[a556caa]14struct _owl_editwin { /*noproto*/
[9190285]15  int refcount;
[a556caa]16  char *buff;
17  owl_history *hist;
18  int bufflen;
19  int allocated;
20  int index;
[16cfd12a]21  int mark;
[a556caa]22  int goal_column;
23  int topindex;
[ac6d4e4]24  int column;
[a556caa]25  int winlines, wincols, fillcol, wrapcol;
[f6fae8d]26  owl_window *win;
27  gulong repaint_id;
28  gulong resized_id;
[a556caa]29  int style;
30  int lock;
31  int dotsend;
32  int echochar;
[ebf0128]33  oe_excursion *excursions;
[a556caa]34
[7803326]35  void (*callback)(struct _owl_editwin *e, bool success);
[1b1cd2c]36  void (*destroy_cbdata)(void *);
[a556caa]37  void *cbdata;
38};
39
[3f11c00]40static void oe_set_window(owl_editwin *e, owl_window *w, int winlines, int wincols);
[0b9e607]41static void oe_redraw(owl_window *win, WINDOW *curswin, void *user_data);
[e20d8179]42static void oe_reframe(owl_editwin *e);
[bab52da]43static void oe_save_excursion(owl_editwin *e, oe_excursion *x);
[ebf0128]44static void oe_release_excursion(owl_editwin *e, oe_excursion *x);
[bab52da]45static void oe_restore_excursion(owl_editwin *e, oe_excursion *x);
[a60edf2]46static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x);
[ebf0128]47static int oe_char_width(gunichar c, int column);
[ac6d4e4]48static int oe_region_column(owl_editwin *e, int start, int end, int offset);
49static int oe_region_width(owl_editwin *e, int start, int end, int offset);
[7149832]50static int oe_find_display_line(owl_editwin *e, int *x, int index, int *hard);
[ebf0128]51static void oe_insert_char(owl_editwin *e, gunichar c);
[d625cfd]52static int owl_editwin_limit_maxcols(int width, int cols);
[bab52da]53static int owl_editwin_check_dotsend(owl_editwin *e);
[e19eb97]54static int owl_editwin_is_char_in(owl_editwin *e, const char *set);
[bab52da]55static gunichar owl_editwin_get_char_at_point(owl_editwin *e);
[e19eb97]56static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s);
57static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len);
[a60edf2]58static int oe_copy_region(owl_editwin *e);
[6829afc]59static CALLER_OWN char *oe_chunk(owl_editwin *e, int start, int end);
[1b1cd2c]60static void oe_destroy_cbdata(owl_editwin *e);
[f6fae8d]61static void oe_dirty(owl_editwin *e);
62static void oe_window_resized(owl_window *w, owl_editwin *e);
[bab52da]63
[5b5f3e6]64#define INCR 4096
[7d4fbcd]65
[a556caa]66#define WHITESPACE " \n\t"
67
[6829afc]68static CALLER_OWN owl_editwin *owl_editwin_allocate(void)
[bab52da]69{
[96828e4]70  owl_editwin *e = g_new0(owl_editwin, 1);
[9190285]71  e->refcount = 1;
[8321cb7]72  return e;
[a556caa]73}
74
[9190285]75static void _owl_editwin_delete(owl_editwin *e)
[6211c76]76{
[f6fae8d]77  if (e->win) {
78    g_signal_handler_disconnect(e->win, e->repaint_id);
79    g_signal_handler_disconnect(e->win, e->resized_id);
[a8186dc]80    g_object_unref(e->win);
[f6fae8d]81  }
[ddbbcffa]82  g_free(e->buff);
[6211c76]83  /* just in case someone forgot to clean up */
84  while (e->excursions) {
85    oe_release_excursion(e, e->excursions);
86  }
[38cc669]87  oe_destroy_cbdata(e);
[6211c76]88
[ddbbcffa]89  g_free(e);
[6211c76]90}
91
[bab52da]92static inline void oe_set_index(owl_editwin *e, int index)
93{
[ebf0128]94  if (index != e->index) {
95    e->goal_column = -1;
[ac6d4e4]96    e->column = -1;
[ebf0128]97  }
[521bc84]98  e->index = index;
[f6fae8d]99  oe_dirty(e);
[521bc84]100}
101
[7f0c26f]102static inline void oe_set_mark(owl_editwin *e, int mark)
103{
104  e->mark = mark;
105}
106
107void owl_editwin_set_mark(owl_editwin *e)
108{
109  oe_set_mark(e, e->index);
110  /* owl_function_makemsg("Mark set."); */
111}
112
[38cc669]113static void _owl_editwin_init(owl_editwin *e,
114                              int winlines,
115                              int wincols,
116                              int style,
117                              owl_history *hist)
[c9334b1]118{
[96828e4]119  e->buff=g_new(char, INCR);
[7d4fbcd]120  e->buff[0]='\0';
121  e->bufflen=0;
[10b866d]122  e->hist=hist;
[7d4fbcd]123  e->allocated=INCR;
[521bc84]124  oe_set_index(e, 0);
[7f0c26f]125  oe_set_mark(e, -1);
[a556caa]126  e->goal_column = -1;
[ac6d4e4]127  e->column = -1;
[ebf0128]128  e->topindex = 0;
129  e->excursions = NULL;
[7d4fbcd]130  e->style=style;
131  if ((style!=OWL_EDITWIN_STYLE_MULTILINE) &&
132      (style!=OWL_EDITWIN_STYLE_ONELINE)) {
133    e->style=OWL_EDITWIN_STYLE_MULTILINE;
134  }
135  e->lock=0;
136  e->dotsend=0;
[c9334b1]137  e->echochar='\0';
[7d4fbcd]138}
139
[6829afc]140CALLER_OWN owl_editwin *owl_editwin_new(owl_window *win, int winlines, int wincols, int style, owl_history *hist)
[38cc669]141{
142  owl_editwin *e = owl_editwin_allocate();
143
[f6fae8d]144  _owl_editwin_init(e, winlines, wincols, style, hist);
[3f11c00]145  oe_set_window(e, win, winlines, wincols);
[38cc669]146  return e;
147}
148
[9190285]149owl_editwin *owl_editwin_ref(owl_editwin *e)
150{
151  e->refcount++;
152  return e;
153}
154
155void owl_editwin_unref(owl_editwin *e)
156{
157  e->refcount--;
158  if (e->refcount <= 0)
159    _owl_editwin_delete(e);
160}
161
[52172cc]162/* TODO: collapse this window into oe_window_resized. Need to stop
163 * passing winlines and wincols to oe_set_window and get them out of
164 * the owl_window. The tester code will first need to pass in an
165 * owl_window headless or so. */
166static void oe_set_window_size(owl_editwin *e, int winlines, int wincols)
[f6fae8d]167{
[7d4fbcd]168  e->winlines=winlines;
169  e->wincols=wincols;
[52172cc]170  /* fillcol and wrapcol may have changed. */
[9a6cc40]171  e->fillcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxfillcols(&g));
[9d7a720]172  if (e->style == OWL_EDITWIN_STYLE_MULTILINE)
173    e->wrapcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxwrapcols(&g));
174  else
175    e->wrapcol = 0;
[52172cc]176}
177
178static void oe_window_resized(owl_window *w, owl_editwin *e)
179{
180  int winlines, wincols;
181  owl_window_get_position(w, &winlines, &wincols, NULL, NULL);
182  oe_set_window_size(e, winlines, wincols);
183}
184
185static void oe_set_window(owl_editwin *e, owl_window *w, int winlines, int wincols)
186{
187  e->win=w;
188  oe_set_window_size(e, winlines, wincols);
[f6fae8d]189  if (e->win) {
[a8186dc]190    g_object_ref(e->win);
[0b9e607]191    e->repaint_id = g_signal_connect(w, "redraw", G_CALLBACK(oe_redraw), e);
[f6fae8d]192    e->resized_id = g_signal_connect(w, "resized", G_CALLBACK(oe_window_resized), e);
193    owl_window_dirty(e->win);
194  }
[7d4fbcd]195}
196
[08263a8]197owl_window *owl_editwin_get_window(owl_editwin *e)
198{
199  return e->win;
200}
201
[c9334b1]202/* echo the character 'ch' for each normal character keystroke,
203 * excepting locktext.  This is useful for entering passwords etc.  If
204 * ch=='\0' characters are echo'd normally
205 */
206void owl_editwin_set_echochar(owl_editwin *e, int ch)
207{
208  e->echochar=ch;
[f6fae8d]209  oe_dirty(e);
[7d4fbcd]210}
211
[c9334b1]212owl_history *owl_editwin_get_history(owl_editwin *e)
213{
[060b3b4]214  return(e->hist);
[10b866d]215}
216
[c9334b1]217void owl_editwin_set_dotsend(owl_editwin *e)
218{
[7d4fbcd]219  e->dotsend=1;
220}
221
[7803326]222void owl_editwin_set_callback(owl_editwin *e, void (*cb)(owl_editwin *, bool))
[bab52da]223{
[e74c01c]224  e->callback = cb;
225}
226
[7803326]227void (*owl_editwin_get_callback(owl_editwin *e))(owl_editwin *, bool)
[bab52da]228{
[e74c01c]229  return e->callback;
230}
231
[1b1cd2c]232static void oe_destroy_cbdata(owl_editwin *e) {
233  if (e->destroy_cbdata)
234    e->destroy_cbdata(e->cbdata);
235  e->cbdata = NULL;
236  e->destroy_cbdata = NULL;
237}
238
239void owl_editwin_set_cbdata(owl_editwin *e, void *data, void (*destroy)(void *))
[bab52da]240{
[1b1cd2c]241  oe_destroy_cbdata(e);
[e74c01c]242  e->cbdata = data;
[1b1cd2c]243  e->destroy_cbdata = destroy;
[e74c01c]244}
245
[a556caa]246void *owl_editwin_get_cbdata(owl_editwin *e) {
[e74c01c]247  return e->cbdata;
248}
249
[7803326]250void owl_editwin_do_callback(owl_editwin *e, bool success)
251{
252  void (*cb)(owl_editwin *, bool);
253  cb = owl_editwin_get_callback(e);
254  if (!cb) {
[e74c01c]255    owl_function_error("Internal error: No editwin callback!");
256  } else {
[7803326]257    cb(e, success);
[e74c01c]258  }
259}
260
[d625cfd]261static int owl_editwin_limit_maxcols(int width, int cols)
[c9334b1]262{
[d625cfd]263  if (cols == 0)
264    return width;
265  return cols;
[d36f2cb]266}
267
[c9334b1]268/* set text to be 'locked in' at the beginning of the buffer, any
269 * previous text (including locked text) will be overwritten
270 */
[e19eb97]271void owl_editwin_set_locktext(owl_editwin *e, const char *text)
[c9334b1]272{
[521bc84]273  oe_set_index(e, 0);
[5b5f3e6]274  e->lock = 0;
275  owl_editwin_replace(e, e->bufflen, text);
276  e->buff[e->bufflen] = 0;
277  e->lock=e->bufflen;
[521bc84]278  oe_set_index(e, e->lock);
[f6fae8d]279  oe_dirty(e);
[7d4fbcd]280}
281
[c9334b1]282int owl_editwin_get_style(owl_editwin *e)
283{
[7d4fbcd]284  return(e->style);
285}
286
[c9334b1]287/* clear all text except for locktext and put the cursor at the
288 * beginning
289 */
290void owl_editwin_clear(owl_editwin *e)
291{
292
[6073462]293  int lock = e->lock;
[10b866d]294  int dotsend=e->dotsend;
[7d4fbcd]295  char *locktext=NULL;
[cc6f009]296  char echochar=e->echochar;
[10b866d]297
[6073462]298  if (lock > 0) {
[47e0a6a]299    locktext = g_strndup(e->buff, lock);
[7d4fbcd]300  }
301
[ddbbcffa]302  g_free(e->buff);
[f6fae8d]303  _owl_editwin_init(e, e->winlines, e->wincols, e->style, e->hist);
[7d4fbcd]304
305  if (lock > 0) {
306    owl_editwin_set_locktext(e, locktext);
307  }
[10b866d]308  if (dotsend) {
309    owl_editwin_set_dotsend(e);
310  }
[cc6f009]311  if (echochar) {
312    owl_editwin_set_echochar(e, echochar);
313  }
[7d4fbcd]314
[3b8a563]315  g_free(locktext);
[6073462]316
[521bc84]317  oe_set_index(e, lock);
[7d4fbcd]318}
319
[c9334b1]320void owl_editwin_recenter(owl_editwin *e)
321{
[a556caa]322  e->topindex = -1;
[f6fae8d]323  oe_dirty(e);
[a556caa]324}
[e20d8179]325
[a0fbdee]326static void oe_save_excursion(owl_editwin *e, oe_excursion *x)
327{
328  x->index = e->index;
[7f0c26f]329  x->mark = e->mark;
[a0fbdee]330  x->goal_column = e->goal_column;
331  x->lock = e->lock;
[ebf0128]332
333  x->valid = VALID_EXCURSION;
334  x->next = e->excursions;
335  e->excursions = x;
336}
337
338static void oe_release_excursion(owl_editwin *e, oe_excursion *x)
339{
[a85d225]340  oe_excursion **px;
[ebf0128]341
342  x->valid = 0;
[a85d225]343  for (px = &e->excursions; *px != NULL; px = &(*px)->next)
344    if (*px == x) {
345      *px = x->next;
346      return;
347    }
348  abort();
[a0fbdee]349}
350
351static void oe_restore_excursion(owl_editwin *e, oe_excursion *x)
352{
[ebf0128]353  if (x->valid == VALID_EXCURSION) {
354    oe_set_index(e, x->index);
355    e->goal_column = x->goal_column;
[7f0c26f]356    e->mark = x->mark;
[ebf0128]357    e->lock = x->lock;
358
359    oe_release_excursion(e, x);
360  }
[a0fbdee]361}
362
[a60edf2]363static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x)
364{
365  if (x->valid == VALID_EXCURSION) {
366    e->mark = x->mark;
367
368    oe_release_excursion(e, x);
369  }
370}
371
[a88f35a]372/* External interface to oe_save_excursion */
373owl_editwin_excursion *owl_editwin_begin_excursion(owl_editwin *e)
374{
[96828e4]375  owl_editwin_excursion *x = g_new(owl_editwin_excursion, 1);
[a88f35a]376  oe_save_excursion(e, x);
377  return x;
378}
379
380void owl_editwin_end_excursion(owl_editwin *e, owl_editwin_excursion *x)
381{
382  oe_restore_excursion(e, x);
[ddbbcffa]383  g_free(x);
[a88f35a]384}
385
[e19eb97]386static inline const char *oe_next_point(owl_editwin *e, const char *p)
[e20d8179]387{
[e19eb97]388  const char *boundary = e->buff + e->bufflen + 1;
389  const char *q;
[e20d8179]390
391  q = g_utf8_find_next_char(p, boundary);
392  while (q && g_unichar_ismark(g_utf8_get_char(q)))
393    q = g_utf8_find_next_char(q, boundary);
394
395  if (q == p)
396    return NULL;
397  return q;
398}
399
[e19eb97]400static inline const char *oe_prev_point(owl_editwin *e, const char *p)
[e20d8179]401{
[e19eb97]402  const char *boundary = e->buff + e->lock;
[e20d8179]403
404  p = g_utf8_find_prev_char(boundary, p);
405  while (p && g_unichar_ismark(g_utf8_get_char(p)))
406    p = g_utf8_find_prev_char(boundary, p);
407
408  return p;
409}
410
[ebf0128]411static int oe_char_width(gunichar c, int column)
412{
413  int cw;
414
415  if (c == 9) /* TAB */
416    return TABSIZE - column % TABSIZE;
417
418  cw = mk_wcwidth(c);
419
420  if (cw < 0) /* control characters */
421    cw = 0;
422
423  return cw;
424}
425
[e75a6df]426/* Finds the display line of 'e' starting at the character
427 * 'index'. The index just after the line is returned. Whether the
428 * line ends at a hard break (newline) or soft break (wrapping) is
429 * returned in 'hard'.
430 *
431 * If the point (e->index) is contained in the line, its position is
432 * returned in 'x'.
433 */
[7149832]434static int oe_find_display_line(owl_editwin *e, int *x, int index, int *hard)
[19f765d]435{
436  int width = 0, cw;
[ebf0128]437  gunichar c;
[e19eb97]438  const char *p;
[19f765d]439
[77d4402]440  while(1) {
441    /* note the position of the dot */
[2021bea]442    if (x != NULL && index == e->index && width < e->wincols)
[19f765d]443      *x = width;
[77d4402]444
[19f765d]445    /* get the current character */
446    c = g_utf8_get_char(e->buff + index);
447
448    /* figure out how wide it is */
[ebf0128]449    cw = oe_char_width(c, width);
[19f765d]450
[7ce30a9]451    if (width + cw > e->wincols - 1) {
[77d4402]452      if (x != NULL && *x == width)
453        *x = -1;
[7149832]454      if (hard != NULL) *hard = 0;
[19f765d]455      break;
[77d4402]456    }
[19f765d]457    width += cw;
458
459    if (c == '\n') {
[77d4402]460      if (width < e->wincols)
461        ++index; /* skip the newline */
[7149832]462      if (hard != NULL) *hard = 1;
[19f765d]463      break;
464    }
465
466    /* find the next character */
467    p = oe_next_point(e, e->buff + index);
468    if (p == NULL) { /* we ran off the end */
469      if (x != NULL && e->index > index)
470        *x = width + 1;
[7149832]471      if (hard != NULL) *hard = 1;
[19f765d]472      break;
473    }
474    index = p - e->buff;
[cedc95c]475
[19f765d]476  }
477  return index;
478}
479
[e20d8179]480static void oe_reframe(owl_editwin *e) {
[cedc95c]481  oe_excursion x;
[460fbe8]482  int goal = 1 + e->winlines / 2;
[cedc95c]483  int index;
484  int count = 0;
485  int n, i;
486  int last;
487
488  oe_save_excursion(e, &x);
489  /* step back line-by-line through the buffer until we have >= goal lines of
490     display text */
491  e->lock = 0; /* we can (must) tread on the locktext */
492
493  last = -1;
494  while (count < goal) {
495    index = e->index;
496    owl_editwin_move_to_beginning_of_line(e);
497    if (last == e->index)
498      break;
499    last = e->index;
500    for (n = 0, i = e->index; i < index; n++)
[7149832]501      i = oe_find_display_line(e, NULL, i, NULL);
[cedc95c]502    count += n == 0 ? 1 : n;
503    if (count < goal)
504      owl_editwin_point_move(e, -1);
505  }
506
507  e->topindex = e->index;
[521bc84]508  /* if we overshot, backtrack */
509  for (n = 0; n < (count - goal); n++)
[7149832]510    e->topindex = oe_find_display_line(e, NULL, e->topindex, NULL);
[cedc95c]511
512  oe_restore_excursion(e, &x);
[f6fae8d]513  oe_dirty(e);
[7d4fbcd]514}
515
[f6fae8d]516static void oe_addnec(owl_editwin *e, WINDOW *curswin, int count)
[3e36085]517{
518  int i;
519
520  for (i = 0; i < count; i++)
[f6fae8d]521    waddch(curswin, e->echochar);
[3e36085]522}
523
[f6fae8d]524static void oe_mvaddnec(owl_editwin *e, WINDOW *curswin, int y, int x, int count)
[3e36085]525{
[f6fae8d]526  wmove(curswin, y, x);
527  oe_addnec(e, curswin, count);
[3e36085]528}
529
[c9334b1]530/* regenerate the text on the curses window */
[0b9e607]531static void oe_redraw(owl_window *win, WINDOW *curswin, void *user_data)
[c9334b1]532{
[7149832]533  int x = -1, y = -1, t, hard;
[77d4402]534  int line, index, lineindex, times = 0;
[f6fae8d]535  owl_editwin *e = user_data;
[7d4fbcd]536
[bd1a1ae]537  do {
[f6fae8d]538    werase(curswin);
[36a16fc]539
540    if (e->topindex == -1 || e->index < e->topindex)
[e20d8179]541      oe_reframe(e);
542
[bd1a1ae]543    line = 0;
544    index = e->topindex;
545    while(line < e->winlines) {
546      lineindex = index;
[19f765d]547      t = -1;
[7149832]548      index = oe_find_display_line(e, &t, lineindex, &hard);
[19f765d]549      if (x == -1 && t != -1)
550        x = t, y = line;
[3e36085]551      if (index - lineindex) {
552        if (!e->echochar)
[f6fae8d]553          mvwaddnstr(curswin, line, 0,
[3e36085]554                     e->buff + lineindex,
555                     index - lineindex);
556        else {
557          if(lineindex < e->lock) {
[f6fae8d]558            mvwaddnstr(curswin, line, 0,
[3e36085]559                       e->buff + lineindex,
560                       MIN(index - lineindex,
561                           e->lock - lineindex));
562            if (e->lock < index)
[f6fae8d]563              oe_addnec(e, curswin,
[3e36085]564                        oe_region_width(e, e->lock, index,
565                                        oe_region_width(e, lineindex, e->lock, 0)));
566          } else
[f6fae8d]567            oe_mvaddnec(e, curswin, line, 0, oe_region_width(e, lineindex, index, 0));
[3e36085]568        }
[7149832]569        if (!hard)
[f6fae8d]570          waddch(curswin, '\\');
[3e36085]571      }
[bd1a1ae]572      line++;
[a556caa]573    }
[19f765d]574    if (x == -1)
[bd1a1ae]575        e->topindex = -1; /* force a reframe */
[77d4402]576    times++;
577  } while(x == -1 && times < 3);
[e20d8179]578
[f6fae8d]579  wmove(curswin, y, x);
[7d4fbcd]580}
581
[7f0c26f]582static inline void oe_fixup(int *target, int start, int end, int change) {
583  if (*target > start) {
[0d9c90c]584    if (*target <= end)
[8459609]585      *target = end + change;
[7f0c26f]586    else
587      *target += change;
588  }
589}
590
[e19eb97]591int owl_editwin_replace_region(owl_editwin *e, const char *s)
[a88f35a]592{
593  oe_excursion x;
594  int ret;
[6e58ff2]595
596  if (e->mark == -1) {
597    owl_function_error("The mark is unset, there is no region to replace.");
598    return 0;
599  }
600
[5ebc202]601  oe_save_excursion(e, &x);
[a88f35a]602
603  if(e->index > e->mark) {
604    owl_editwin_exchange_point_and_mark(e);
605  }
606
607  ret = owl_editwin_replace_internal(e, e->mark - e->index, s);
608
609  oe_restore_excursion(e, &x);
610
611  return ret;
612}
613
[19a023f]614/* replace 'replace' characters at the point with s, returning the change in size */
[e19eb97]615int owl_editwin_replace(owl_editwin *e, int replace, const char *s)
[ebf0128]616{
[19a023f]617  int start, end, i;
[e19eb97]618  const char *p;
[e9bb404]619
[5b5f3e6]620  if (!g_utf8_validate(s, -1, NULL)) {
621    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
[ebf0128]622    return 0;
[47519e1b]623  }
624
[5b5f3e6]625  start = e->index;
626  for (i = 0, p = e->buff + start; i < replace && p != NULL; i++)
627    p = oe_next_point(e, p);
628  if (p != NULL)
629    end = p - e->buff;
630  else
631    end = e->bufflen;
632
[19a023f]633  return owl_editwin_replace_internal(e, end - start, s);
634}
635
[e19eb97]636static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s)
[19a023f]637{
[ac6d4e4]638  int start, end, free, need, size, change, oldindex;
[19a023f]639  oe_excursion *x;
640
641  start = e->index;
642  end   = start + replace;
643
[5b5f3e6]644  free = e->allocated - e->bufflen + end - start;
645
646  need = strlen(s) - free;
647  if (need > 0) {
648    size = e->allocated + need + INCR - (need % INCR);
[35b6eb9]649    e->buff = g_renew(char, e->buff, size);
[5b5f3e6]650    e->allocated = size;
[47519e1b]651  }
[e9bb404]652
[5b5f3e6]653  memmove(e->buff + start + strlen(s), e->buff + end, e->bufflen + 1 - end);
654  memcpy(e->buff + start, s, strlen(s));
[ebf0128]655  change = start - end + strlen(s);
656  e->bufflen += change;
[ac6d4e4]657  oldindex = e->index;
[5b5f3e6]658  e->index += strlen(s);
[ac6d4e4]659  if (e->column != -1)
660    e->column = oe_region_column(e, oldindex, e->index, e->column);
[ebf0128]661
[7f0c26f]662  /* fix up the mark */
[3e36085]663  oe_fixup(&e->mark, start, end, change);
664  oe_fixup(&e->topindex, start, end, change);
[ebf0128]665  /* fix up any saved points after the replaced area */
[7f0c26f]666  for (x = e->excursions; x != NULL; x = x->next) {
667    oe_fixup(&x->index, start, end, change);
[3e36085]668    oe_fixup(&x->mark, start, end, change);
[7f0c26f]669  }
[ebf0128]670
[c471e85]671  /* recenter if needed */
672  if (start <= e->topindex)
673    owl_editwin_recenter(e);
674
[f6fae8d]675  oe_dirty(e);
676
[ebf0128]677  return change;
[47519e1b]678}
679
[c9334b1]680/* linewrap the word just before the cursor.
681 * returns 0 on success
682 * returns -1 if we could not wrap.
683 */
[fc2677b]684static void _owl_editwin_linewrap_word(owl_editwin *e)
[c9334b1]685{
[fc2677b]686  oe_excursion x;
[84027015]687  gunichar c;
[7d4fbcd]688
[fc2677b]689  oe_save_excursion(e, &x);
[84027015]690
[fc2677b]691  while (owl_editwin_point_move(e, -1)) {
692    c = owl_editwin_get_char_at_point(e);
693    if (owl_util_can_break_after(c) || c == '\n') {
694      if (c != '\n')
695        owl_editwin_replace(e, c != ' ' ? 0 : 1, "\n");
696      break;
[2d4ff14]697    }
[7d4fbcd]698  }
[fc2677b]699
700  oe_restore_excursion(e, &x);
[7d4fbcd]701}
702
[c9334b1]703/* delete the character at the current point, following chars
704 * shift left.
[e20d8179]705 */
[c9334b1]706void owl_editwin_delete_char(owl_editwin *e)
707{
[5b5f3e6]708  owl_editwin_replace(e, 1, "");
[7d4fbcd]709}
710
[c9334b1]711/* Swap the character at point with the character at point-1 and
712 * advance the pointer.  If point is at beginning of buffer do
713 * nothing.  If point is after the last character swap point-1 with
[e20d8179]714 * point-2.  (Behaves as observed in tcsh and emacs).
[c9334b1]715 */
716void owl_editwin_transpose_chars(owl_editwin *e)
717{
[e19eb97]718  const char *middle, *end, *start;
[65b2173]719  char *tmp;
[f2e36b5]720
[47519e1b]721  if (e->bufflen == 0) return;
[e20d8179]722
[5b5f3e6]723  if (e->index == e->bufflen)
724    owl_editwin_point_move(e, -1);     /* point is after last character */
[f2e36b5]725
[5b5f3e6]726  if (owl_editwin_at_beginning_of_buffer(e))
727    return;     /* point is at beginning of buffer, do nothing */
[f2e36b5]728
[47519e1b]729  /* Transpose two utf-8 unicode glyphs. */
[5b5f3e6]730  middle = e->buff + e->index;
[47519e1b]731
[5b5f3e6]732  end = oe_next_point(e, middle);
733  if (end == NULL)
[e20d8179]734    return;
[47519e1b]735
[5b5f3e6]736  start = oe_prev_point(e, middle);
737  if (start == NULL)
[e20d8179]738    return;
[47519e1b]739
[96828e4]740  tmp = g_new(char, (end - start) + 1);
[5b5f3e6]741  tmp[(end - start)] = 0;
742  memcpy(tmp, middle, end - middle);
743  memcpy(tmp + (end - middle), start, middle - start);
744
745  owl_editwin_point_move(e, -1);
746  owl_editwin_replace(e, 2, tmp);
[f2e36b5]747}
748
[c9334b1]749/* insert 'string' at the current point, later text is shifted
750 * right
751 */
[e19eb97]752void owl_editwin_insert_string(owl_editwin *e, const char *s)
[c9334b1]753{
[5b5f3e6]754  owl_editwin_replace(e, 0, s);
[7d4fbcd]755}
756
[a556caa]757/* We assume index is not set to point to a mid-char */
[bab52da]758static gunichar owl_editwin_get_char_at_point(owl_editwin *e)
[c9334b1]759{
[a556caa]760  return g_utf8_get_char(e->buff + e->index);
761}
[7d4fbcd]762
[7f0c26f]763void owl_editwin_exchange_point_and_mark(owl_editwin *e) {
764  int tmp;
765
766  if (e->mark != -1) {
767    tmp = e->mark;
768    owl_editwin_set_mark(e);
769    oe_set_index(e, tmp);
770  }
771}
772
[e20d8179]773int owl_editwin_point_move(owl_editwin *e, int delta)
[84027015]774{
[e19eb97]775  const char *p;
[19f765d]776  int change, d = 0;
[a556caa]777
778  change = MAX(delta, - delta);
779  p = e->buff + e->index;
780
781  while (d < change && p != NULL) {
[e20d8179]782    if (delta > 0)
783      p = oe_next_point(e, p);
784    else
785      p = oe_prev_point(e, p);
786    if (p != NULL) {
[521bc84]787      oe_set_index(e, p - e->buff);
[e20d8179]788      d++;
[a556caa]789    }
790  }
791
792  return delta > 0 ? d : -d;
[84027015]793}
794
[a556caa]795int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
796  if (e->index == e->lock)
797    return 1;
[84027015]798
[a556caa]799  return 0;
800}
801
802int owl_at_end_of_buffer(owl_editwin *e) {
803  if (e->index == e->bufflen)
804    return 1;
805
806  return 0;
807}
808
[e9c6fc8]809static int owl_editwin_at_beginning_of_line(owl_editwin *e)
[c9334b1]810{
[a0fbdee]811  oe_excursion x;
[a556caa]812  int ret;
[47519e1b]813
[a556caa]814  if (owl_editwin_at_beginning_of_buffer(e))
815    return 1;
[47519e1b]816
[a0fbdee]817  oe_save_excursion(e, &x);
[a556caa]818  owl_editwin_point_move(e, -1);
[bab52da]819  ret = (owl_editwin_get_char_at_point(e) == '\n');
[a0fbdee]820  oe_restore_excursion(e, &x);
[a556caa]821
822  return ret;
823}
824
[e19eb97]825static int owl_editwin_is_char_in(owl_editwin *e, const char *set)
[a556caa]826{
[e19eb97]827  const char *p;
[5b5f3e6]828
829  for (p = set; *p != 0; p = g_utf8_find_next_char(p, NULL))
830    if (owl_editwin_get_char_at_point(e) == g_utf8_get_char(p))
[a556caa]831      return 1;
832  return 0;
833}
834
[e19eb97]835int owl_editwin_move_if_in(owl_editwin *e, int delta, const char *set)
[a556caa]836{
837  int change, distance = 0;
[e20d8179]838  while (owl_editwin_is_char_in(e, set)) {
[a556caa]839    change = owl_editwin_point_move(e, delta);
840    distance += change;
841    if (change == 0)
842      break;
[47519e1b]843  }
[a556caa]844  return distance;
845}
846
[e19eb97]847int owl_editwin_move_if_not_in(owl_editwin *e, int delta, const char *set)
[a556caa]848{
849  int change, distance = 0;
[e20d8179]850  while (!owl_editwin_is_char_in(e, set)) {
[a556caa]851    change = owl_editwin_point_move(e, delta);
852    distance += change;
853    if (change == 0)
854      break;
[7d4fbcd]855  }
[a556caa]856  return distance;
[7d4fbcd]857}
858
[e20d8179]859int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
[47519e1b]860{
[a556caa]861  int distance = 0;
[47519e1b]862
[a556caa]863  if (!owl_editwin_at_beginning_of_line(e)) {
864    /* move off the \n if were at the end of a line */
865    distance += owl_editwin_point_move(e, -1);
866    distance += owl_editwin_move_if_not_in(e, -1, "\n");
[335413f1]867    /* If we stopped because we reached a '\n', rather than because we
868     * hit the top of the buffer, move forward from the end of the
869     * previous line to the start of the current. */
870    if (owl_editwin_get_char_at_point(e) == '\n')
[a556caa]871      distance += owl_editwin_point_move(e, 1);
[47519e1b]872  }
[77d4402]873  e->goal_column = 0; /* subtleties */
[84027015]874
[a556caa]875  return distance;
876}
[e20d8179]877
878int owl_editwin_move_to_end_of_line(owl_editwin *e)
[a556caa]879{
880  return owl_editwin_move_if_not_in(e, 1, "\n");
[47519e1b]881}
882
[e20d8179]883int owl_editwin_line_move(owl_editwin *e, int delta)
[c9334b1]884{
[d7043b4]885  int goal_column, change, ll, distance;
886  int count = 0;
[a556caa]887
888  change = MAX(delta, -delta);
889
[d7043b4]890  goal_column = e->goal_column;
891  distance = owl_editwin_move_to_beginning_of_line(e);
892  goal_column = goal_column == -1 ? -distance : goal_column;
[a556caa]893
894  while(count < change) {
895    if (delta > 0) {
896      distance += owl_editwin_move_if_not_in(e, 1, "\n");
897      distance += owl_editwin_point_move(e, 1);
898    } else {
899      /* I really want to assert delta < 0 here */
900      distance += owl_editwin_point_move(e, -1); /* to the newline on
901                                                    the previous line */
902      distance += owl_editwin_move_to_beginning_of_line(e);
903    }
904    count++;
[7d4fbcd]905  }
[a556caa]906
907  distance += (ll = owl_editwin_move_to_end_of_line(e));
908  if (ll > goal_column)
909    distance += owl_editwin_point_move(e, goal_column - ll);
910
911  e->goal_column = goal_column;
[f6fae8d]912  oe_dirty(e);
[a556caa]913
914  return distance;
[7d4fbcd]915}
916
[c9334b1]917void owl_editwin_backspace(owl_editwin *e)
918{
[7d4fbcd]919  /* delete the char before the current one
920   * and shift later chars left
921   */
[a556caa]922  if(owl_editwin_point_move(e, -1))
[7d4fbcd]923    owl_editwin_delete_char(e);
924}
925
[c9334b1]926void owl_editwin_key_up(owl_editwin *e)
927{
[a556caa]928  owl_editwin_line_move(e, -1);
[7d4fbcd]929}
930
[c9334b1]931void owl_editwin_key_down(owl_editwin *e)
932{
[a556caa]933  owl_editwin_line_move(e, 1);
[7d4fbcd]934}
935
[c9334b1]936void owl_editwin_key_left(owl_editwin *e)
937{
[a556caa]938  owl_editwin_point_move(e, -1);
[7d4fbcd]939}
940
[c9334b1]941void owl_editwin_key_right(owl_editwin *e)
942{
[a556caa]943  owl_editwin_point_move(e, 1);
[7d4fbcd]944}
945
[5b5f3e6]946int owl_editwin_forward_word(owl_editwin *e)
[c9334b1]947{
[5b5f3e6]948  int distance;
[7d4fbcd]949  /* if we're starting on a space, find the first non-space */
[5b5f3e6]950  distance = owl_editwin_move_if_in(e, 1, WHITESPACE);
[7d4fbcd]951
[a556caa]952  /* now find the end of this word */
[5b5f3e6]953  distance += owl_editwin_move_if_not_in(e, 1, WHITESPACE);
954
955  return distance;
956}
957
958void owl_editwin_move_to_nextword(owl_editwin *e)
959{
960  owl_editwin_forward_word(e);
[7d4fbcd]961}
962
[c9334b1]963/* go backwards to the last non-space character
964 */
[5b5f3e6]965int owl_editwin_backward_word(owl_editwin *e)
[c9334b1]966{
[a0fbdee]967  oe_excursion x;
[5b5f3e6]968  int distance = 0;
969  int further = 0;
[a556caa]970  int beginning;
971  /* if in middle of word, beginning of word */
972
973  /* if at beginning of a word, find beginning of previous word */
974
[e20d8179]975  if (owl_editwin_is_char_in(e, WHITESPACE)) {
[a556caa]976    /* if in whitespace past end of word, find a word , the find the beginning*/
[5b5f3e6]977    distance += owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
978                                                              character of the word */
[a0fbdee]979    oe_save_excursion(e, &x);
[a556caa]980    /* are we at the beginning of a word? */
981    owl_editwin_point_move(e, -1);
[e20d8179]982    beginning = owl_editwin_is_char_in(e, WHITESPACE);
[a0fbdee]983    oe_restore_excursion(e, &x);
[a556caa]984    if (beginning)
[5b5f3e6]985      return distance;
[a556caa]986   } else {
987    /* in the middle of the word; */
[a0fbdee]988    oe_save_excursion(e, &x);
[5b5f3e6]989    further += owl_editwin_point_move(e, -1);
[e20d8179]990    if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
[5b5f3e6]991      distance += owl_editwin_backward_word(e); /* previous case */
[8321cb7]992      oe_release_excursion(e, &x);
[5b5f3e6]993      return distance + further;
[a556caa]994    } else {
[a0fbdee]995      oe_restore_excursion(e, &x);
[7d4fbcd]996    }
997  }
[5b5f3e6]998  distance += owl_editwin_move_if_not_in(e, -1, WHITESPACE);
[a556caa]999  /* will go past */
1000  if (e->index > e->lock)
[5b5f3e6]1001    distance += owl_editwin_point_move(e, 1);
1002  return distance;
1003}
1004
1005void owl_editwin_move_to_previousword(owl_editwin *e)
1006{
1007  owl_editwin_backward_word(e);
[7d4fbcd]1008}
1009
[c9334b1]1010void owl_editwin_delete_nextword(owl_editwin *e)
1011{
[a0fbdee]1012  oe_excursion x;
[a556caa]1013
[a0fbdee]1014  oe_save_excursion(e, &x);
[a60edf2]1015  oe_set_mark(e, e->index);
1016  owl_editwin_forward_word(e);
1017  owl_editwin_kill_region(e);
1018  oe_restore_mark_only(e, &x);
[7d4fbcd]1019}
1020
[c9334b1]1021void owl_editwin_delete_previousword(owl_editwin *e)
1022{
[a60edf2]1023  oe_excursion x;
[b68f9cd]1024
[a60edf2]1025  oe_save_excursion(e, &x);
1026  oe_set_mark(e, e->index);
1027  owl_editwin_backward_word(e);
1028  owl_editwin_kill_region(e);
1029  oe_restore_mark_only(e, &x);
[b68f9cd]1030}
1031
[a556caa]1032void owl_editwin_move_to_line_end(owl_editwin *e)
[c9334b1]1033{
[a556caa]1034  owl_editwin_move_to_end_of_line(e);
[7d4fbcd]1035}
1036
[a556caa]1037void owl_editwin_delete_to_endofline(owl_editwin *e)
[c9334b1]1038{
[a0fbdee]1039  oe_excursion x;
[5b5f3e6]1040  int distance;
[a556caa]1041
[a0fbdee]1042  oe_save_excursion(e, &x);
[a60edf2]1043  owl_editwin_set_mark(e);
[5b5f3e6]1044  distance = owl_editwin_move_to_end_of_line(e);
[a60edf2]1045  if (distance)
1046    owl_editwin_kill_region(e);
1047  else
1048    owl_editwin_replace(e, 1, "");
[a0fbdee]1049  oe_restore_excursion(e, &x);
[a60edf2]1050}
1051
1052void owl_editwin_yank(owl_editwin *e)
1053{
[47e0a6a]1054  const char *killbuf = owl_global_get_kill_buffer(&g);
[5f8ec6b]1055
1056  if (killbuf != NULL)
1057    owl_editwin_replace(e, 0, killbuf);
[a60edf2]1058}
1059
[e19eb97]1060static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len)
[a60edf2]1061{
[47e0a6a]1062  owl_global_set_kill_buffer(&g, buf, len);
1063  return owl_global_get_kill_buffer(&g);
[a60edf2]1064}
1065
1066static int oe_copy_region(owl_editwin *e)
1067{
[e19eb97]1068  const char *p;
[a60edf2]1069  int start, end;
1070
1071  if (e->mark == -1)
1072    return 0;
1073
1074  start = MIN(e->index, e->mark);
1075  end = MAX(e->index, e->mark);
1076
1077  p = oe_copy_buf(e, e->buff + start, end - start);
1078  if (p != NULL)
1079    return end - start;
1080  return 0;
1081}
1082
1083void owl_editwin_copy_region_as_kill(owl_editwin *e)
1084{
1085  oe_copy_region(e);
1086}
1087
1088void owl_editwin_kill_region(owl_editwin *e)
1089{
1090  if (e->index > e->mark)
1091    owl_editwin_exchange_point_and_mark(e);
1092
[2184001]1093  owl_editwin_replace_internal(e, oe_copy_region(e), "");
[7d4fbcd]1094}
1095
[c9334b1]1096void owl_editwin_move_to_line_start(owl_editwin *e)
1097{
[a556caa]1098  owl_editwin_move_to_beginning_of_line(e);
[7d4fbcd]1099}
1100
[c9334b1]1101void owl_editwin_move_to_end(owl_editwin *e)
1102{
[521bc84]1103  oe_set_index(e, e->bufflen);
[7d4fbcd]1104}
1105
[c9334b1]1106void owl_editwin_move_to_top(owl_editwin *e)
1107{
[521bc84]1108  oe_set_index(e, e->lock);
[7d4fbcd]1109}
1110
[2fc8397]1111void owl_editwin_backward_paragraph(owl_editwin *e)
1112{
1113  owl_editwin_point_move(e, -1);
1114  for (; e->index >= e->lock; owl_editwin_point_move(e, -1)) {
1115    if (e->index <= e->lock ||
1116        ((e->buff[e->index] == '\n') && (e->buff[e->index - 1]=='\n')))
1117      break;
1118  }
1119}
1120
1121void owl_editwin_forward_paragraph(owl_editwin *e)
1122{
1123  owl_editwin_point_move(e, 1);
1124  /* scan forward to the start of the next paragraph */
1125  for(; e->index < e->bufflen; owl_editwin_point_move(e, 1)) {
1126    if (e->buff[e->index -1] == '\n' && e->buff[e->index] == '\n')
1127      break;
1128  }
1129}
1130
[d41294a]1131int owl_editwin_current_column(owl_editwin *e)
[c9334b1]1132{
[ac6d4e4]1133  if (e->column == -1) {
1134    oe_excursion x;
1135    int lineindex;
[7d4fbcd]1136
[ac6d4e4]1137    oe_save_excursion(e, &x);
1138    owl_editwin_move_to_beginning_of_line(e);
1139    lineindex = e->index;
1140    oe_restore_excursion(e, &x);
1141    e->column = oe_region_width(e, lineindex, e->index, 0);
1142  }
1143  return e->column;
[fc2677b]1144}
[a556caa]1145
[fc2677b]1146void owl_editwin_fill_paragraph(owl_editwin *e)
1147{
1148  oe_excursion x;
[f661cee]1149  gunichar ch = 0;
1150  gunichar last_ch;
[fc2677b]1151  int sentence;
[7d4fbcd]1152
[d625cfd]1153  if (e->fillcol < 0)
1154    /* auto-fill disabled */
1155    return;
1156
[fc2677b]1157  oe_save_excursion(e, &x);
[7d4fbcd]1158
[fc2677b]1159  /* Mark the end of the paragraph */
1160  owl_editwin_forward_paragraph(e);
1161  /* Skip the trailing newline */
1162  owl_editwin_point_move(e, -1);
1163  owl_editwin_set_mark(e);
[7d4fbcd]1164
[fc2677b]1165  owl_editwin_backward_paragraph(e);
[7d4fbcd]1166
[fc2677b]1167  /* Don't mess with the leading newline */
1168  if (owl_editwin_get_char_at_point(e) == '\n')
1169    owl_editwin_point_move(e, 1);
[7d4fbcd]1170
[fc2677b]1171  /*
1172   * First pass: Scan forward replacing all series of spaces with ' '
1173   * (or nothing after CJK ideograms)
1174   */
1175  sentence = 0;
1176  for(;e->index < e->mark; owl_editwin_point_move(e, 1)) {
[50e671c]1177    /* bail if we hit a trailing dot on the buffer */
[fc2677b]1178    if (strcmp(e->buff + e->index, "\n.") == 0) {
1179      owl_editwin_set_mark(e);
[50e671c]1180      break;
[7d4fbcd]1181    }
1182
[f661cee]1183    last_ch = ch;
[fc2677b]1184    ch = owl_editwin_get_char_at_point(e);
[b2c1bd4]1185
[fc2677b]1186    if (owl_util_can_break_after(ch) || ch == '\n') {
1187      if (g_unichar_isspace(ch)) {
1188        owl_editwin_replace(e, 1, " ");
1189      }
1190
[f9d257b]1191      if (sentence && g_unichar_isspace(owl_editwin_get_char_at_point(e))
1192          && e->index < e->mark)
[fc2677b]1193        owl_editwin_point_move(e, 1);
1194
1195      while(g_unichar_isspace(owl_editwin_get_char_at_point(e))
1196            && e->index < e->mark) {
1197        owl_editwin_delete_char(e);
[7d4fbcd]1198      }
1199    }
[fc2677b]1200
[f661cee]1201    if (ch == '.' || ch == '!' || ch == '?' ||
1202        (ch == '"' && (last_ch == '.' || last_ch == '!' || last_ch == '?')))
[fc2677b]1203      sentence = 1;
1204    else
1205      sentence = 0;
1206  }
1207
1208  owl_editwin_backward_paragraph(e);
1209
1210  /* Now go through inserting newlines as needed */
1211  while(e->index < e->mark) {
1212    /* if we've travelled too far, linewrap */
[d41294a]1213    if (owl_editwin_current_column(e) >= e->fillcol)
[fc2677b]1214      _owl_editwin_linewrap_word(e);
1215    owl_editwin_point_move(e, 1);
[7d4fbcd]1216  }
1217
[a0fbdee]1218  oe_restore_excursion(e, &x);
[7d4fbcd]1219}
1220
[cf83b7a]1221/* returns true if only whitespace remains */
[c9334b1]1222int owl_editwin_is_at_end(owl_editwin *e)
1223{
[a556caa]1224  return (only_whitespace(e->buff + e->index));
[217a43e]1225}
1226
[bab52da]1227static int owl_editwin_check_dotsend(owl_editwin *e)
[c9334b1]1228{
[dc7884d]1229  int zdot = 0;
[72ab15f]1230  oe_excursion x;
[7d4fbcd]1231
1232  if (!e->dotsend) return(0);
[4ccd92c]1233  if (!owl_editwin_is_at_end(e)) return (0);
[47519e1b]1234
[72ab15f]1235  oe_save_excursion(e, &x);
1236
1237  owl_editwin_point_move(e, -3);
1238
[4ccd92c]1239  if(strncmp(e->buff + e->index, "\n.\n", 3) == 0) {
[dc7884d]1240    owl_editwin_point_move(e, 1);
1241    zdot = 1;
[0509efc]1242  } else if(e->index == e->lock &&
[4ccd92c]1243            strncmp(e->buff + e->index, ".\n", 2) == 0) {
[0509efc]1244    zdot = 1;
1245  }
1246
1247  if(zdot) {
[4ccd92c]1248    owl_editwin_set_mark(e);
1249    owl_editwin_move_to_end(e);
1250    owl_editwin_replace_region(e, "");
[dc7884d]1251  }
[72ab15f]1252
1253  oe_restore_excursion(e, &x);
1254
1255  return zdot;
[7d4fbcd]1256}
1257
[fac5463]1258void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
[c9334b1]1259{
[a556caa]1260  /* XXX force a redisplay? */
[fac5463]1261  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
[435d6b2]1262    owl_command_edit_done(e);
[7d4fbcd]1263    return;
1264  }
1265}
1266
[ac6d4e4]1267static int oe_region_column(owl_editwin *e, int start, int end, int offset)
1268{
1269  const char *p;
1270  int column = offset;
1271
1272  for(p = e->buff + start;
1273      p < e->buff + end;
1274      p = g_utf8_find_next_char(p, NULL)) {
1275    gunichar c = g_utf8_get_char(p);
1276    if (c == '\n')
1277      column = 0;
1278    else
1279      column += oe_char_width(c, column);
1280  }
1281  return column;
1282}
1283
[3e36085]1284static int oe_region_width(owl_editwin *e, int start, int end, int offset)
[16cfd12a]1285{
[e19eb97]1286  const char *p;
[3e36085]1287  int width = offset;
1288 
[16cfd12a]1289  for(p = e->buff + start;
1290      p < e->buff + end;
1291      p = g_utf8_find_next_char(p, NULL))
1292    width += oe_char_width(g_utf8_get_char(p), width);
1293
[3e36085]1294  return width - offset;
[16cfd12a]1295}
1296
[ebf0128]1297static void oe_insert_char(owl_editwin *e, gunichar c)
[fac5463]1298{
[16cfd12a]1299  oe_excursion x;
[5b5f3e6]1300  char tmp[7];
[16cfd12a]1301  int replaced = -1;
[9c3f334]1302  int column;
[5b5f3e6]1303
[ebf0128]1304  if (c == '\r') /* translate CRs to NLs */
1305    c = '\n';
1306
[16cfd12a]1307  if (!g_unichar_iscntrl(c) || c == '\n' || c== '\t' ) {
[ebf0128]1308    if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
1309      return;
1310    }
1311
[9c3f334]1312    column = owl_editwin_current_column(e);
1313    if (e->wrapcol > 0 && column != -1 &&
1314        column + oe_char_width(c, column) > e->wrapcol) {
[16cfd12a]1315      /* XXX this is actually wrong:
1316       * + If the user went back and inserted a bunch of stuff in the middle of
1317       *   the line, there may be more than one word past the wrap column.
1318       */
1319      oe_save_excursion(e, &x);
1320
1321      if (c == ' ' || c == '\t') {
1322        owl_editwin_point_move(e, -1);
1323        replaced = -owl_editwin_move_if_in(e, -1, " \t");
1324        if (!replaced) {
1325          c = '\n';
1326          replaced = -1;
1327        }
1328      } else {
1329        while(!owl_editwin_at_beginning_of_line(e)) {
1330          owl_editwin_point_move(e, -1);
1331          if (owl_util_can_break_after(owl_editwin_get_char_at_point(e))) {
1332            replaced = -owl_editwin_move_if_in(e, -1, " \t");
1333            break;
1334          }
1335        }
1336        if (owl_editwin_at_beginning_of_line(e))
1337          replaced = -1;
1338      }
1339      if (replaced && !owl_editwin_at_beginning_of_line(e))
1340        owl_editwin_point_move(e, 1);
1341      if (replaced >= 0) {
1342        owl_editwin_replace(e, replaced, "\n");
1343      }
1344      oe_restore_excursion(e, &x);
1345    }
1346
1347    if (replaced >= 0 && (c == ' ' || c == '\t'))
1348      return; /* our work here is done */
1349
[6c171f1]1350    tmp[g_unichar_to_utf8(c, tmp)] = '\0';
[ebf0128]1351    owl_editwin_replace(e, 0, tmp);
1352  }
1353}
1354
1355void owl_editwin_process_char(owl_editwin *e, owl_input j)
1356{
[5b5f3e6]1357  if (j.ch == ERR)
1358    return;
[fac5463]1359  /* Ignore ncurses control characters. */
[e20d8179]1360  if (j.ch < 0x100) {
[ebf0128]1361    oe_insert_char(e, j.uch);
[fac5463]1362  }
1363}
1364
[e19eb97]1365const char *owl_editwin_get_text(owl_editwin *e)
[c9334b1]1366{
[7d4fbcd]1367  return(e->buff+e->lock);
1368}
1369
[6829afc]1370CALLER_OWN char *owl_editwin_get_region(owl_editwin *e)
[d41294a]1371{
1372  int start, end;
1373  start = e->index;
1374  end   = e->mark;
1375  if(start > end) {
1376    int tmp = end;
1377    end = start;
1378    start = tmp;
1379  }
1380
1381  return oe_chunk(e, start, end);
1382}
1383
[77f605d]1384int owl_editwin_get_echochar(owl_editwin *e)
1385{
[a556caa]1386  return e->echochar;
1387}
[77f605d]1388
[6829afc]1389static CALLER_OWN char *oe_chunk(owl_editwin *e, int start, int end)
[77f605d]1390{
1391  char *p;
1392 
[96828e4]1393  p = g_new(char, end - start + 1);
[77f605d]1394  memcpy(p, e->buff + start, end - start);
1395  p[end - start] = 0;
1396
1397  return p;
1398}
1399
[d41294a]1400/*
1401 * The only guarantee made about these values is that comparisons
1402 * between them, as well as comparison between multiple calls to these
1403 * functions without modifying the editwin in-between, are meaningful.
1404 */
1405
1406int owl_editwin_get_point(owl_editwin *e)
1407{
1408  return e->index;
1409}
1410
1411int owl_editwin_get_mark(owl_editwin *e)
1412{
1413  return e->mark;
1414}
1415
[f6fae8d]1416static void oe_dirty(owl_editwin *e)
1417{
1418  if (e->win) owl_window_dirty(e->win);
1419}
1420
[2f21a41]1421
1422/*
1423 * Local Variables:
1424 * mode:C
1425 * c-basic-offset:2
1426 * End:
1427 */
Note: See TracBrowser for help on using the repository browser.