source: editwin.c @ d07af84

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