source: editwin.c @ 9c3f334

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