source: editwin.c @ a5a9572

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