source: editwin.c @ 68ab07c

release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 68ab07c was 6c171f1, checked in by Anders Kaseorg <andersk@mit.edu>, 12 years ago
Use g_unichar_to_utf8 in a way that doesn’t require memset. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 30.5 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  char *p;
614
615  start = e->index;
616  end   = start + replace;
617
[5b5f3e6]618  free = e->allocated - e->bufflen + end - start;
619
620  need = strlen(s) - free;
621  if (need > 0) {
622    size = e->allocated + need + INCR - (need % INCR);
[a60edf2]623    p = owl_realloc(e->buff, size);
[5b5f3e6]624    if (p == NULL) {
625      /* XXX signal impending doom somehow and don't do anything */
[ebf0128]626      return 0;
[e9bb404]627    }
[5b5f3e6]628    e->buff = p;
629    e->allocated = size;
[47519e1b]630  }
[e9bb404]631
[5b5f3e6]632  memmove(e->buff + start + strlen(s), e->buff + end, e->bufflen + 1 - end);
633  memcpy(e->buff + start, s, strlen(s));
[ebf0128]634  change = start - end + strlen(s);
635  e->bufflen += change;
[5b5f3e6]636  e->index += strlen(s);
[ebf0128]637
[7f0c26f]638  /* fix up the mark */
[3e36085]639  oe_fixup(&e->mark, start, end, change);
640  oe_fixup(&e->topindex, start, end, change);
[ebf0128]641  /* fix up any saved points after the replaced area */
[7f0c26f]642  for (x = e->excursions; x != NULL; x = x->next) {
643    oe_fixup(&x->index, start, end, change);
[3e36085]644    oe_fixup(&x->mark, start, end, change);
[7f0c26f]645  }
[ebf0128]646
647  return change;
[47519e1b]648}
649
[c9334b1]650/* linewrap the word just before the cursor.
651 * returns 0 on success
652 * returns -1 if we could not wrap.
653 */
[fc2677b]654static void _owl_editwin_linewrap_word(owl_editwin *e)
[c9334b1]655{
[fc2677b]656  oe_excursion x;
[84027015]657  gunichar c;
[7d4fbcd]658
[fc2677b]659  oe_save_excursion(e, &x);
[84027015]660
[fc2677b]661  while (owl_editwin_point_move(e, -1)) {
662    c = owl_editwin_get_char_at_point(e);
663    if (owl_util_can_break_after(c) || c == '\n') {
664      if (c != '\n')
665        owl_editwin_replace(e, c != ' ' ? 0 : 1, "\n");
666      break;
[2d4ff14]667    }
[7d4fbcd]668  }
[fc2677b]669
670  oe_restore_excursion(e, &x);
[7d4fbcd]671}
672
[c9334b1]673/* delete the character at the current point, following chars
674 * shift left.
[e20d8179]675 */
[c9334b1]676void owl_editwin_delete_char(owl_editwin *e)
677{
[5b5f3e6]678  owl_editwin_replace(e, 1, "");
[7d4fbcd]679}
680
[c9334b1]681/* Swap the character at point with the character at point-1 and
682 * advance the pointer.  If point is at beginning of buffer do
683 * nothing.  If point is after the last character swap point-1 with
[e20d8179]684 * point-2.  (Behaves as observed in tcsh and emacs).
[c9334b1]685 */
686void owl_editwin_transpose_chars(owl_editwin *e)
687{
[e19eb97]688  const char *middle, *end, *start;
[65b2173]689  char *tmp;
[f2e36b5]690
[47519e1b]691  if (e->bufflen == 0) return;
[e20d8179]692
[5b5f3e6]693  if (e->index == e->bufflen)
694    owl_editwin_point_move(e, -1);     /* point is after last character */
[f2e36b5]695
[5b5f3e6]696  if (owl_editwin_at_beginning_of_buffer(e))
697    return;     /* point is at beginning of buffer, do nothing */
[f2e36b5]698
[47519e1b]699  /* Transpose two utf-8 unicode glyphs. */
[5b5f3e6]700  middle = e->buff + e->index;
[47519e1b]701
[5b5f3e6]702  end = oe_next_point(e, middle);
703  if (end == NULL)
[e20d8179]704    return;
[47519e1b]705
[5b5f3e6]706  start = oe_prev_point(e, middle);
707  if (start == NULL)
[e20d8179]708    return;
[47519e1b]709
[5b5f3e6]710  tmp = owl_malloc((end - start) + 1);
711  tmp[(end - start)] = 0;
712  memcpy(tmp, middle, end - middle);
713  memcpy(tmp + (end - middle), start, middle - start);
714
715  owl_editwin_point_move(e, -1);
716  owl_editwin_replace(e, 2, tmp);
[f2e36b5]717}
718
[c9334b1]719/* insert 'string' at the current point, later text is shifted
720 * right
721 */
[e19eb97]722void owl_editwin_insert_string(owl_editwin *e, const char *s)
[c9334b1]723{
[5b5f3e6]724  owl_editwin_replace(e, 0, s);
[7d4fbcd]725}
726
[a556caa]727/* We assume index is not set to point to a mid-char */
[bab52da]728static gunichar owl_editwin_get_char_at_point(owl_editwin *e)
[c9334b1]729{
[a556caa]730  return g_utf8_get_char(e->buff + e->index);
731}
[7d4fbcd]732
[7f0c26f]733void owl_editwin_exchange_point_and_mark(owl_editwin *e) {
734  int tmp;
735
736  if (e->mark != -1) {
737    tmp = e->mark;
738    owl_editwin_set_mark(e);
739    oe_set_index(e, tmp);
740  }
741}
742
[e20d8179]743int owl_editwin_point_move(owl_editwin *e, int delta)
[84027015]744{
[e19eb97]745  const char *p;
[19f765d]746  int change, d = 0;
[a556caa]747
748  change = MAX(delta, - delta);
749  p = e->buff + e->index;
750
751  while (d < change && p != NULL) {
[e20d8179]752    if (delta > 0)
753      p = oe_next_point(e, p);
754    else
755      p = oe_prev_point(e, p);
756    if (p != NULL) {
[521bc84]757      oe_set_index(e, p - e->buff);
[e20d8179]758      d++;
[a556caa]759    }
760  }
761
762  return delta > 0 ? d : -d;
[84027015]763}
764
[a556caa]765int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
766  if (e->index == e->lock)
767    return 1;
[84027015]768
[a556caa]769  return 0;
770}
771
772int owl_at_end_of_buffer(owl_editwin *e) {
773  if (e->index == e->bufflen)
774    return 1;
775
776  return 0;
777}
778
779int owl_editwin_at_beginning_of_line(owl_editwin *e) /*noproto*/
[c9334b1]780{
[a0fbdee]781  oe_excursion x;
[a556caa]782  int ret;
[47519e1b]783
[a556caa]784  if (owl_editwin_at_beginning_of_buffer(e))
785    return 1;
[47519e1b]786
[a0fbdee]787  oe_save_excursion(e, &x);
[a556caa]788  owl_editwin_point_move(e, -1);
[bab52da]789  ret = (owl_editwin_get_char_at_point(e) == '\n');
[a0fbdee]790  oe_restore_excursion(e, &x);
[a556caa]791
792  return ret;
793}
794
[e19eb97]795static int owl_editwin_is_char_in(owl_editwin *e, const char *set)
[a556caa]796{
[e19eb97]797  const char *p;
[5b5f3e6]798
799  for (p = set; *p != 0; p = g_utf8_find_next_char(p, NULL))
800    if (owl_editwin_get_char_at_point(e) == g_utf8_get_char(p))
[a556caa]801      return 1;
802  return 0;
803}
804
[e19eb97]805int owl_editwin_move_if_in(owl_editwin *e, int delta, const char *set)
[a556caa]806{
807  int change, distance = 0;
[e20d8179]808  while (owl_editwin_is_char_in(e, set)) {
[a556caa]809    change = owl_editwin_point_move(e, delta);
810    distance += change;
811    if (change == 0)
812      break;
[47519e1b]813  }
[a556caa]814  return distance;
815}
816
[e19eb97]817int owl_editwin_move_if_not_in(owl_editwin *e, int delta, const char *set)
[a556caa]818{
819  int change, distance = 0;
[e20d8179]820  while (!owl_editwin_is_char_in(e, set)) {
[a556caa]821    change = owl_editwin_point_move(e, delta);
822    distance += change;
823    if (change == 0)
824      break;
[7d4fbcd]825  }
[a556caa]826  return distance;
[7d4fbcd]827}
828
[e20d8179]829int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
[47519e1b]830{
[a556caa]831  int distance = 0;
[47519e1b]832
[a556caa]833  if (!owl_editwin_at_beginning_of_line(e)) {
834    /* move off the \n if were at the end of a line */
835    distance += owl_editwin_point_move(e, -1);
836    distance += owl_editwin_move_if_not_in(e, -1, "\n");
837    if (distance && !owl_editwin_at_beginning_of_buffer(e))
838      distance += owl_editwin_point_move(e, 1);
[47519e1b]839  }
[77d4402]840  e->goal_column = 0; /* subtleties */
[84027015]841
[a556caa]842  return distance;
843}
[e20d8179]844
845int owl_editwin_move_to_end_of_line(owl_editwin *e)
[a556caa]846{
847  return owl_editwin_move_if_not_in(e, 1, "\n");
[47519e1b]848}
849
[e20d8179]850int owl_editwin_line_move(owl_editwin *e, int delta)
[c9334b1]851{
[d7043b4]852  int goal_column, change, ll, distance;
853  int count = 0;
[a556caa]854
855  change = MAX(delta, -delta);
856
[d7043b4]857  goal_column = e->goal_column;
858  distance = owl_editwin_move_to_beginning_of_line(e);
859  goal_column = goal_column == -1 ? -distance : goal_column;
[a556caa]860
861  while(count < change) {
862    if (delta > 0) {
863      distance += owl_editwin_move_if_not_in(e, 1, "\n");
864      distance += owl_editwin_point_move(e, 1);
865    } else {
866      /* I really want to assert delta < 0 here */
867      distance += owl_editwin_point_move(e, -1); /* to the newline on
868                                                    the previous line */
869      distance += owl_editwin_move_to_beginning_of_line(e);
870    }
871    count++;
[7d4fbcd]872  }
[a556caa]873
874  distance += (ll = owl_editwin_move_to_end_of_line(e));
875  if (ll > goal_column)
876    distance += owl_editwin_point_move(e, goal_column - ll);
877
878  e->goal_column = goal_column;
879
880  return distance;
[7d4fbcd]881}
882
[c9334b1]883void owl_editwin_backspace(owl_editwin *e)
884{
[7d4fbcd]885  /* delete the char before the current one
886   * and shift later chars left
887   */
[a556caa]888  if(owl_editwin_point_move(e, -1))
[7d4fbcd]889    owl_editwin_delete_char(e);
890}
891
[c9334b1]892void owl_editwin_key_up(owl_editwin *e)
893{
[a556caa]894  owl_editwin_line_move(e, -1);
[7d4fbcd]895}
896
[c9334b1]897void owl_editwin_key_down(owl_editwin *e)
898{
[a556caa]899  owl_editwin_line_move(e, 1);
[7d4fbcd]900}
901
[c9334b1]902void owl_editwin_key_left(owl_editwin *e)
903{
[a556caa]904  owl_editwin_point_move(e, -1);
[7d4fbcd]905}
906
[c9334b1]907void owl_editwin_key_right(owl_editwin *e)
908{
[a556caa]909  owl_editwin_point_move(e, 1);
[7d4fbcd]910}
911
[5b5f3e6]912int owl_editwin_forward_word(owl_editwin *e)
[c9334b1]913{
[5b5f3e6]914  int distance;
[7d4fbcd]915  /* if we're starting on a space, find the first non-space */
[5b5f3e6]916  distance = owl_editwin_move_if_in(e, 1, WHITESPACE);
[7d4fbcd]917
[a556caa]918  /* now find the end of this word */
[5b5f3e6]919  distance += owl_editwin_move_if_not_in(e, 1, WHITESPACE);
920
921  return distance;
922}
923
924void owl_editwin_move_to_nextword(owl_editwin *e)
925{
926  owl_editwin_forward_word(e);
[7d4fbcd]927}
928
[c9334b1]929/* go backwards to the last non-space character
930 */
[5b5f3e6]931int owl_editwin_backward_word(owl_editwin *e)
[c9334b1]932{
[a0fbdee]933  oe_excursion x;
[5b5f3e6]934  int distance = 0;
935  int further = 0;
[a556caa]936  int beginning;
937  /* if in middle of word, beginning of word */
938
939  /* if at beginning of a word, find beginning of previous word */
940
[e20d8179]941  if (owl_editwin_is_char_in(e, WHITESPACE)) {
[a556caa]942    /* if in whitespace past end of word, find a word , the find the beginning*/
[5b5f3e6]943    distance += owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
944                                                              character of the word */
[a0fbdee]945    oe_save_excursion(e, &x);
[a556caa]946    /* are we at the beginning of a word? */
947    owl_editwin_point_move(e, -1);
[e20d8179]948    beginning = owl_editwin_is_char_in(e, WHITESPACE);
[a0fbdee]949    oe_restore_excursion(e, &x);
[a556caa]950    if (beginning)
[5b5f3e6]951      return distance;
[a556caa]952   } else {
953    /* in the middle of the word; */
[a0fbdee]954    oe_save_excursion(e, &x);
[5b5f3e6]955    further += owl_editwin_point_move(e, -1);
[e20d8179]956    if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
[5b5f3e6]957      distance += owl_editwin_backward_word(e); /* previous case */
[8321cb7]958      oe_release_excursion(e, &x);
[5b5f3e6]959      return distance + further;
[a556caa]960    } else {
[a0fbdee]961      oe_restore_excursion(e, &x);
[7d4fbcd]962    }
963  }
[5b5f3e6]964  distance += owl_editwin_move_if_not_in(e, -1, WHITESPACE);
[a556caa]965  /* will go past */
966  if (e->index > e->lock)
[5b5f3e6]967    distance += owl_editwin_point_move(e, 1);
968  return distance;
969}
970
971void owl_editwin_move_to_previousword(owl_editwin *e)
972{
973  owl_editwin_backward_word(e);
[7d4fbcd]974}
975
[c9334b1]976void owl_editwin_delete_nextword(owl_editwin *e)
977{
[a0fbdee]978  oe_excursion x;
[a556caa]979
[a0fbdee]980  oe_save_excursion(e, &x);
[a60edf2]981  oe_set_mark(e, e->index);
982  owl_editwin_forward_word(e);
983  owl_editwin_kill_region(e);
984  oe_restore_mark_only(e, &x);
[7d4fbcd]985}
986
[c9334b1]987void owl_editwin_delete_previousword(owl_editwin *e)
988{
[a60edf2]989  oe_excursion x;
[b68f9cd]990
[a60edf2]991  oe_save_excursion(e, &x);
992  oe_set_mark(e, e->index);
993  owl_editwin_backward_word(e);
994  owl_editwin_kill_region(e);
995  oe_restore_mark_only(e, &x);
[b68f9cd]996}
997
[a556caa]998void owl_editwin_move_to_line_end(owl_editwin *e)
[c9334b1]999{
[a556caa]1000  owl_editwin_move_to_end_of_line(e);
[7d4fbcd]1001}
1002
[a556caa]1003void owl_editwin_delete_to_endofline(owl_editwin *e)
[c9334b1]1004{
[a0fbdee]1005  oe_excursion x;
[5b5f3e6]1006  int distance;
[a556caa]1007
[a0fbdee]1008  oe_save_excursion(e, &x);
[a60edf2]1009  owl_editwin_set_mark(e);
[5b5f3e6]1010  distance = owl_editwin_move_to_end_of_line(e);
[a60edf2]1011  if (distance)
1012    owl_editwin_kill_region(e);
1013  else
1014    owl_editwin_replace(e, 1, "");
[a0fbdee]1015  oe_restore_excursion(e, &x);
[a60edf2]1016}
1017
1018void owl_editwin_yank(owl_editwin *e)
1019{
1020  if (e->killbuf != NULL)
1021    owl_editwin_replace(e, 0, e->killbuf);
1022}
1023
[e19eb97]1024static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len)
[a60edf2]1025{
1026  char *p;
1027
1028  p = owl_malloc(len + 1);
1029
1030  if (p != NULL) {
1031    owl_free(e->killbuf);
1032    e->killbuf = p;
1033    memcpy(e->killbuf, buf, len);
1034    e->killbuf[len] = 0;
1035  }
1036
1037  return p;
1038}
1039
1040static int oe_copy_region(owl_editwin *e)
1041{
[e19eb97]1042  const char *p;
[a60edf2]1043  int start, end;
1044
1045  if (e->mark == -1)
1046    return 0;
1047
1048  start = MIN(e->index, e->mark);
1049  end = MAX(e->index, e->mark);
1050
1051  p = oe_copy_buf(e, e->buff + start, end - start);
1052  if (p != NULL)
1053    return end - start;
1054  return 0;
1055}
1056
1057void owl_editwin_copy_region_as_kill(owl_editwin *e)
1058{
1059  oe_copy_region(e);
1060}
1061
1062void owl_editwin_kill_region(owl_editwin *e)
1063{
1064  if (e->index > e->mark)
1065    owl_editwin_exchange_point_and_mark(e);
1066
[2184001]1067  owl_editwin_replace_internal(e, oe_copy_region(e), "");
[7d4fbcd]1068}
1069
[c9334b1]1070void owl_editwin_move_to_line_start(owl_editwin *e)
1071{
[a556caa]1072  owl_editwin_move_to_beginning_of_line(e);
[7d4fbcd]1073}
1074
[c9334b1]1075void owl_editwin_move_to_end(owl_editwin *e)
1076{
[521bc84]1077  oe_set_index(e, e->bufflen);
[7d4fbcd]1078}
1079
[c9334b1]1080void owl_editwin_move_to_top(owl_editwin *e)
1081{
[521bc84]1082  oe_set_index(e, e->lock);
[7d4fbcd]1083}
1084
[2fc8397]1085void owl_editwin_backward_paragraph(owl_editwin *e)
1086{
1087  owl_editwin_point_move(e, -1);
1088  for (; e->index >= e->lock; owl_editwin_point_move(e, -1)) {
1089    if (e->index <= e->lock ||
1090        ((e->buff[e->index] == '\n') && (e->buff[e->index - 1]=='\n')))
1091      break;
1092  }
1093}
1094
1095void owl_editwin_forward_paragraph(owl_editwin *e)
1096{
1097  owl_editwin_point_move(e, 1);
1098  /* scan forward to the start of the next paragraph */
1099  for(; e->index < e->bufflen; owl_editwin_point_move(e, 1)) {
1100    if (e->buff[e->index -1] == '\n' && e->buff[e->index] == '\n')
1101      break;
1102  }
1103}
1104
[d41294a]1105int owl_editwin_current_column(owl_editwin *e)
[c9334b1]1106{
[a0fbdee]1107  oe_excursion x;
[fc2677b]1108  int lineindex;
[7d4fbcd]1109
[a0fbdee]1110  oe_save_excursion(e, &x);
[fc2677b]1111  owl_editwin_move_to_beginning_of_line(e);
1112  lineindex = e->index;
1113  oe_restore_excursion(e, &x);
1114  return oe_region_width(e, lineindex, e->index, 0);
1115}
[a556caa]1116
[fc2677b]1117void owl_editwin_fill_paragraph(owl_editwin *e)
1118{
1119  oe_excursion x;
1120  gunichar ch;
1121  int sentence;
[7d4fbcd]1122
[fc2677b]1123  oe_save_excursion(e, &x);
[7d4fbcd]1124
[fc2677b]1125  /* Mark the end of the paragraph */
1126  owl_editwin_forward_paragraph(e);
1127  /* Skip the trailing newline */
1128  owl_editwin_point_move(e, -1);
1129  owl_editwin_set_mark(e);
[7d4fbcd]1130
[fc2677b]1131  owl_editwin_backward_paragraph(e);
[7d4fbcd]1132
[fc2677b]1133  /* Don't mess with the leading newline */
1134  if (owl_editwin_get_char_at_point(e) == '\n')
1135    owl_editwin_point_move(e, 1);
[7d4fbcd]1136
[fc2677b]1137  /*
1138   * First pass: Scan forward replacing all series of spaces with ' '
1139   * (or nothing after CJK ideograms)
1140   */
1141  sentence = 0;
1142  for(;e->index < e->mark; owl_editwin_point_move(e, 1)) {
[50e671c]1143    /* bail if we hit a trailing dot on the buffer */
[fc2677b]1144    if (strcmp(e->buff + e->index, "\n.") == 0) {
1145      owl_editwin_set_mark(e);
[50e671c]1146      break;
[7d4fbcd]1147    }
1148
[fc2677b]1149    ch = owl_editwin_get_char_at_point(e);
[b2c1bd4]1150
[fc2677b]1151    if (owl_util_can_break_after(ch) || ch == '\n') {
1152      if (g_unichar_isspace(ch)) {
1153        owl_editwin_replace(e, 1, " ");
1154      }
1155
[bd7fb58]1156      if (sentence && g_unichar_isspace(owl_editwin_get_char_at_point(e)))
[fc2677b]1157        owl_editwin_point_move(e, 1);
1158
1159      while(g_unichar_isspace(owl_editwin_get_char_at_point(e))
1160            && e->index < e->mark) {
1161        owl_editwin_delete_char(e);
[7d4fbcd]1162      }
1163    }
[fc2677b]1164
1165    if(ch == '.' || ch == '!' || ch == '?')
1166      sentence = 1;
1167    else
1168      sentence = 0;
1169  }
1170
1171  owl_editwin_backward_paragraph(e);
1172
1173  /* Now go through inserting newlines as needed */
1174  while(e->index < e->mark) {
1175    /* if we've travelled too far, linewrap */
[d41294a]1176    if (owl_editwin_current_column(e) >= e->fillcol)
[fc2677b]1177      _owl_editwin_linewrap_word(e);
1178    owl_editwin_point_move(e, 1);
[7d4fbcd]1179  }
1180
[a0fbdee]1181  oe_restore_excursion(e, &x);
[7d4fbcd]1182}
1183
[cf83b7a]1184/* returns true if only whitespace remains */
[c9334b1]1185int owl_editwin_is_at_end(owl_editwin *e)
1186{
[a556caa]1187  return (only_whitespace(e->buff + e->index));
[217a43e]1188}
1189
[bab52da]1190static int owl_editwin_check_dotsend(owl_editwin *e)
[c9334b1]1191{
[dc7884d]1192  int zdot = 0;
[72ab15f]1193  oe_excursion x;
[7d4fbcd]1194
1195  if (!e->dotsend) return(0);
[4ccd92c]1196  if (!owl_editwin_is_at_end(e)) return (0);
[47519e1b]1197
[72ab15f]1198  oe_save_excursion(e, &x);
1199
1200  owl_editwin_point_move(e, -3);
1201
[4ccd92c]1202  if(strncmp(e->buff + e->index, "\n.\n", 3) == 0) {
[dc7884d]1203    owl_editwin_point_move(e, 1);
1204    zdot = 1;
[0509efc]1205  } else if(e->index == e->lock &&
[4ccd92c]1206            strncmp(e->buff + e->index, ".\n", 2) == 0) {
[0509efc]1207    zdot = 1;
1208  }
1209
1210  if(zdot) {
[4ccd92c]1211    owl_editwin_set_mark(e);
1212    owl_editwin_move_to_end(e);
1213    owl_editwin_replace_region(e, "");
[dc7884d]1214  }
[72ab15f]1215
1216  oe_restore_excursion(e, &x);
1217
1218  return zdot;
[7d4fbcd]1219}
1220
[fac5463]1221void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
[c9334b1]1222{
[a556caa]1223  /* XXX force a redisplay? */
[fac5463]1224  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
[435d6b2]1225    owl_command_edit_done(e);
[7d4fbcd]1226    return;
1227  }
[e20d8179]1228  owl_editwin_redisplay(e, 0);
[7d4fbcd]1229}
1230
[3e36085]1231static int oe_region_width(owl_editwin *e, int start, int end, int offset)
[16cfd12a]1232{
[e19eb97]1233  const char *p;
[3e36085]1234  int width = offset;
1235 
[16cfd12a]1236  for(p = e->buff + start;
1237      p < e->buff + end;
1238      p = g_utf8_find_next_char(p, NULL))
1239    width += oe_char_width(g_utf8_get_char(p), width);
1240
[3e36085]1241  return width - offset;
[16cfd12a]1242}
1243
[ebf0128]1244static void oe_insert_char(owl_editwin *e, gunichar c)
[fac5463]1245{
[16cfd12a]1246  oe_excursion x;
[5b5f3e6]1247  char tmp[7];
[16cfd12a]1248  int replaced = -1;
[5b5f3e6]1249
[ebf0128]1250  if (c == '\r') /* translate CRs to NLs */
1251    c = '\n';
1252
[16cfd12a]1253  if (!g_unichar_iscntrl(c) || c == '\n' || c== '\t' ) {
[ebf0128]1254    if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
1255      return;
1256    }
1257
[16cfd12a]1258    if (e->cursorx != -1 && e->cursorx + oe_char_width(c, e->cursorx) > e->wrapcol) {
1259      /* XXX this is actually wrong:
1260       * + If the line has been been wrapped, we can be past the wrap column but
1261       *   e->cursorx be much smaller.
1262       * + If the user went back and inserted a bunch of stuff in the middle of
1263       *   the line, there may be more than one word past the wrap column.
1264       */
1265      oe_save_excursion(e, &x);
1266
1267      if (c == ' ' || c == '\t') {
1268        owl_editwin_point_move(e, -1);
1269        replaced = -owl_editwin_move_if_in(e, -1, " \t");
1270        if (!replaced) {
1271          c = '\n';
1272          replaced = -1;
1273        }
1274      } else {
1275        while(!owl_editwin_at_beginning_of_line(e)) {
1276          owl_editwin_point_move(e, -1);
1277          if (owl_util_can_break_after(owl_editwin_get_char_at_point(e))) {
1278            replaced = -owl_editwin_move_if_in(e, -1, " \t");
1279            break;
1280          }
1281        }
1282        if (owl_editwin_at_beginning_of_line(e))
1283          replaced = -1;
1284      }
1285      if (replaced && !owl_editwin_at_beginning_of_line(e))
1286        owl_editwin_point_move(e, 1);
1287      if (replaced >= 0) {
1288        owl_editwin_replace(e, replaced, "\n");
1289      }
1290      oe_restore_excursion(e, &x);
1291    }
1292
1293    if (replaced >= 0 && (c == ' ' || c == '\t'))
1294      return; /* our work here is done */
1295
[6c171f1]1296    tmp[g_unichar_to_utf8(c, tmp)] = '\0';
[ebf0128]1297    owl_editwin_replace(e, 0, tmp);
1298  }
1299}
1300
1301void owl_editwin_process_char(owl_editwin *e, owl_input j)
1302{
[5b5f3e6]1303  if (j.ch == ERR)
1304    return;
[fac5463]1305  /* Ignore ncurses control characters. */
[e20d8179]1306  if (j.ch < 0x100) {
[ebf0128]1307    oe_insert_char(e, j.uch);
[fac5463]1308  }
1309}
1310
[e19eb97]1311const char *owl_editwin_get_text(owl_editwin *e)
[c9334b1]1312{
[7d4fbcd]1313  return(e->buff+e->lock);
1314}
1315
[d41294a]1316char *owl_editwin_get_region(owl_editwin *e)
1317{
1318  int start, end;
1319  start = e->index;
1320  end   = e->mark;
1321  if(start > end) {
1322    int tmp = end;
1323    end = start;
1324    start = tmp;
1325  }
1326
1327  return oe_chunk(e, start, end);
1328}
1329
[77f605d]1330int owl_editwin_get_echochar(owl_editwin *e)
1331{
[a556caa]1332  return e->echochar;
1333}
[77f605d]1334
1335static char *oe_chunk(owl_editwin *e, int start, int end)
1336{
1337  char *p;
1338 
1339  p = owl_malloc(end - start + 1);
1340  memcpy(p, e->buff + start, end - start);
1341  p[end - start] = 0;
1342
1343  return p;
1344}
1345
[d41294a]1346/*
1347 * The only guarantee made about these values is that comparisons
1348 * between them, as well as comparison between multiple calls to these
1349 * functions without modifying the editwin in-between, are meaningful.
1350 */
1351
1352int owl_editwin_get_point(owl_editwin *e)
1353{
1354  return e->index;
1355}
1356
1357int owl_editwin_get_mark(owl_editwin *e)
1358{
1359  return e->mark;
1360}
1361
[2f21a41]1362
1363/*
1364 * Local Variables:
1365 * mode:C
1366 * c-basic-offset:2
1367 * End:
1368 */
Note: See TracBrowser for help on using the repository browser.