source: editwin.c @ ab6d8f0

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