source: editwin.c @ 13ebf92

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