source: editwin.c @ a8186dc

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