source: editwin.c @ d83621c4

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