source: editwin.c @ 08263a8

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