source: editwin.c @ d3c318b

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