source: editwin.c @ b644688

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