source: editwin.c @ 58a16cc

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