source: editwin.c @ c38bfe0

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