source: editwin.c @ 118c919

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