source: editwin.c @ 2184001

release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2184001 was 2184001, checked in by Anders Kaseorg <andersk@mit.edu>, 12 years ago
Make kill-region stop killing extra bytes after multibyte characters. oe_copy_region returns a number of bytes, so we should pass it to owl_editwin_replace_internal, which skips converting a number of characters into a number of bytes. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 30.7 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{
328  oe_excursion *p;
329
330  x->valid = 0;
331  if (e->excursions == NULL)
332    /* XXX huh. */ ;
333  else if (e->excursions == x)
334    e->excursions = x->next;
335  else {
336    for (p = e->excursions; p->next != NULL; p = p->next)
337      if (p->next == x) {
338        p->next = p->next->next;
339        break;
340      }
341    /* and if we ran off the end? XXX */
342  }
[a0fbdee]343}
344
345static void oe_restore_excursion(owl_editwin *e, oe_excursion *x)
346{
[ebf0128]347  if (x->valid == VALID_EXCURSION) {
348    oe_set_index(e, x->index);
349    e->goal_column = x->goal_column;
[7f0c26f]350    e->mark = x->mark;
[ebf0128]351    e->lock = x->lock;
352
353    oe_release_excursion(e, x);
354  }
[a0fbdee]355}
356
[a60edf2]357static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x)
358{
359  if (x->valid == VALID_EXCURSION) {
360    e->mark = x->mark;
361
362    oe_release_excursion(e, x);
363  }
364}
365
[a88f35a]366/* External interface to oe_save_excursion */
367owl_editwin_excursion *owl_editwin_begin_excursion(owl_editwin *e)
368{
369  owl_editwin_excursion *x = owl_malloc(sizeof *x);
370  oe_save_excursion(e, x);
371  return x;
372}
373
374void owl_editwin_end_excursion(owl_editwin *e, owl_editwin_excursion *x)
375{
376  oe_restore_excursion(e, x);
377  owl_free(x);
378}
379
[e19eb97]380static inline const char *oe_next_point(owl_editwin *e, const char *p)
[e20d8179]381{
[e19eb97]382  const char *boundary = e->buff + e->bufflen + 1;
383  const char *q;
[e20d8179]384
385  q = g_utf8_find_next_char(p, boundary);
386  while (q && g_unichar_ismark(g_utf8_get_char(q)))
387    q = g_utf8_find_next_char(q, boundary);
388
389  if (q == p)
390    return NULL;
391  return q;
392}
393
[e19eb97]394static inline const char *oe_prev_point(owl_editwin *e, const char *p)
[e20d8179]395{
[e19eb97]396  const char *boundary = e->buff + e->lock;
[e20d8179]397
398  p = g_utf8_find_prev_char(boundary, p);
399  while (p && g_unichar_ismark(g_utf8_get_char(p)))
400    p = g_utf8_find_prev_char(boundary, p);
401
402  return p;
403}
404
[ebf0128]405static int oe_char_width(gunichar c, int column)
406{
407  int cw;
408
409  if (c == 9) /* TAB */
410    return TABSIZE - column % TABSIZE;
411
412  cw = mk_wcwidth(c);
413
414  if (cw < 0) /* control characters */
415    cw = 0;
416
417  return cw;
418}
419
[19f765d]420static int oe_find_display_line(owl_editwin *e, int *x, int index)
421{
422  int width = 0, cw;
[ebf0128]423  gunichar c;
[e19eb97]424  const char *p;
[19f765d]425
[77d4402]426  while(1) {
427    /* note the position of the dot */
[2021bea]428    if (x != NULL && index == e->index && width < e->wincols)
[19f765d]429      *x = width;
[77d4402]430
[19f765d]431    /* get the current character */
432    c = g_utf8_get_char(e->buff + index);
433
434    /* figure out how wide it is */
[ebf0128]435    cw = oe_char_width(c, width);
[19f765d]436
[77d4402]437    if (width + cw > e->wincols) {
438      if (x != NULL && *x == width)
439        *x = -1;
[19f765d]440      break;
[77d4402]441    }
[19f765d]442    width += cw;
443
444    if (c == '\n') {
[77d4402]445      if (width < e->wincols)
446        ++index; /* skip the newline */
[19f765d]447      break;
448    }
449
450    /* find the next character */
451    p = oe_next_point(e, e->buff + index);
452    if (p == NULL) { /* we ran off the end */
453      if (x != NULL && e->index > index)
454        *x = width + 1;
455      break;
456    }
457    index = p - e->buff;
[cedc95c]458
[19f765d]459  }
460  return index;
461}
462
[e20d8179]463static void oe_reframe(owl_editwin *e) {
[cedc95c]464  oe_excursion x;
465  int goal = e->winlines / 2;
466  int index;
467  int count = 0;
468  int n, i;
469  int last;
470
471  oe_save_excursion(e, &x);
472  /* step back line-by-line through the buffer until we have >= goal lines of
473     display text */
474  e->lock = 0; /* we can (must) tread on the locktext */
475
476  last = -1;
477  while (count < goal) {
478    index = e->index;
479    owl_editwin_move_to_beginning_of_line(e);
480    if (last == e->index)
481      break;
482    last = e->index;
483    for (n = 0, i = e->index; i < index; n++)
484      i = oe_find_display_line(e, NULL, i);
485    count += n == 0 ? 1 : n;
486    if (count < goal)
487      owl_editwin_point_move(e, -1);
488  }
489
490  e->topindex = e->index;
[521bc84]491  /* if we overshot, backtrack */
492  for (n = 0; n < (count - goal); n++)
493    e->topindex = oe_find_display_line(e, NULL, e->topindex);
[cedc95c]494
495  oe_restore_excursion(e, &x);
[7d4fbcd]496}
497
[3e36085]498static void oe_addnec(owl_editwin *e, int count)
499{
500  int i;
501
502  for (i = 0; i < count; i++)
503    waddch(e->curswin, e->echochar);
504}
505
506static void oe_mvaddnec(owl_editwin *e, int y, int x, int count)
507{
508  wmove(e->curswin, y, x);
509  oe_addnec(e, count);
510}
511
[c9334b1]512/* regenerate the text on the curses window */
513/* if update == 1 then do a doupdate(), otherwise do not */
514void owl_editwin_redisplay(owl_editwin *e, int update)
515{
[19f765d]516  int x = -1, y = -1, t;
[77d4402]517  int line, index, lineindex, times = 0;
[7d4fbcd]518
[bd1a1ae]519  do {
[cedc95c]520    werase(e->curswin);
521
[bd1a1ae]522    if (e->topindex == -1 || e->index < e->topindex)
[e20d8179]523      oe_reframe(e);
524
[bd1a1ae]525    line = 0;
526    index = e->topindex;
527    while(line < e->winlines) {
528      lineindex = index;
[19f765d]529      t = -1;
530      index = oe_find_display_line(e, &t, lineindex);
531      if (x == -1 && t != -1)
532        x = t, y = line;
[3e36085]533      if (index - lineindex) {
534        if (!e->echochar)
535          mvwaddnstr(e->curswin, line, 0,
536                     e->buff + lineindex,
537                     index - lineindex);
538        else {
539          if(lineindex < e->lock) {
540            mvwaddnstr(e->curswin, line, 0,
541                       e->buff + lineindex,
542                       MIN(index - lineindex,
543                           e->lock - lineindex));
544            if (e->lock < index)
545              oe_addnec(e,
546                        oe_region_width(e, e->lock, index,
547                                        oe_region_width(e, lineindex, e->lock, 0)));
548          } else
549            oe_mvaddnec(e, line, 0, oe_region_width(e, line, index, 0));
550        }
551      }
[bd1a1ae]552      line++;
[a556caa]553    }
[19f765d]554    if (x == -1)
[bd1a1ae]555        e->topindex = -1; /* force a reframe */
[77d4402]556    times++;
557  } while(x == -1 && times < 3);
[e20d8179]558
[19f765d]559  wmove(e->curswin, y, x);
[ebf0128]560  e->cursorx = x;
561
[7d4fbcd]562  wnoutrefresh(e->curswin);
[a556caa]563  if (update == 1)
[7d4fbcd]564    doupdate();
565}
566
[7f0c26f]567static inline void oe_fixup(int *target, int start, int end, int change) {
568  if (*target > start) {
[0d9c90c]569    if (*target <= end)
[8459609]570      *target = end + change;
[7f0c26f]571    else
572      *target += change;
573  }
574}
575
[e19eb97]576int owl_editwin_replace_region(owl_editwin *e, const char *s)
[a88f35a]577{
578  oe_excursion x;
579  int ret;
[5ebc202]580  oe_save_excursion(e, &x);
[a88f35a]581
582  if(e->index > e->mark) {
583    owl_editwin_exchange_point_and_mark(e);
584  }
585
586  ret = owl_editwin_replace_internal(e, e->mark - e->index, s);
587
588  oe_restore_excursion(e, &x);
589
590  return ret;
591}
592
[19a023f]593/* replace 'replace' characters at the point with s, returning the change in size */
[e19eb97]594int owl_editwin_replace(owl_editwin *e, int replace, const char *s)
[ebf0128]595{
[19a023f]596  int start, end, i;
[e19eb97]597  const char *p;
[e9bb404]598
[5b5f3e6]599  if (!g_utf8_validate(s, -1, NULL)) {
600    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
[ebf0128]601    return 0;
[47519e1b]602  }
603
[5b5f3e6]604  start = e->index;
605  for (i = 0, p = e->buff + start; i < replace && p != NULL; i++)
606    p = oe_next_point(e, p);
607  if (p != NULL)
608    end = p - e->buff;
609  else
610    end = e->bufflen;
611
[19a023f]612  return owl_editwin_replace_internal(e, end - start, s);
613}
614
[e19eb97]615static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s)
[19a023f]616{
617  int start, end, free, need, size, change;
618  oe_excursion *x;
619  char *p;
620
621  start = e->index;
622  end   = start + replace;
623
[5b5f3e6]624  free = e->allocated - e->bufflen + end - start;
625
626  need = strlen(s) - free;
627  if (need > 0) {
628    size = e->allocated + need + INCR - (need % INCR);
[a60edf2]629    p = owl_realloc(e->buff, size);
[5b5f3e6]630    if (p == NULL) {
631      /* XXX signal impending doom somehow and don't do anything */
[ebf0128]632      return 0;
[e9bb404]633    }
[5b5f3e6]634    e->buff = p;
635    e->allocated = size;
[47519e1b]636  }
[e9bb404]637
[5b5f3e6]638  memmove(e->buff + start + strlen(s), e->buff + end, e->bufflen + 1 - end);
639  memcpy(e->buff + start, s, strlen(s));
[ebf0128]640  change = start - end + strlen(s);
641  e->bufflen += change;
[5b5f3e6]642  e->index += strlen(s);
[ebf0128]643
[7f0c26f]644  /* fix up the mark */
[3e36085]645  oe_fixup(&e->mark, start, end, change);
646  oe_fixup(&e->topindex, start, end, change);
[ebf0128]647  /* fix up any saved points after the replaced area */
[7f0c26f]648  for (x = e->excursions; x != NULL; x = x->next) {
649    oe_fixup(&x->index, start, end, change);
[3e36085]650    oe_fixup(&x->mark, start, end, change);
[7f0c26f]651  }
[ebf0128]652
653  return change;
[47519e1b]654}
655
[c9334b1]656/* linewrap the word just before the cursor.
657 * returns 0 on success
658 * returns -1 if we could not wrap.
659 */
[fc2677b]660static void _owl_editwin_linewrap_word(owl_editwin *e)
[c9334b1]661{
[fc2677b]662  oe_excursion x;
[84027015]663  gunichar c;
[7d4fbcd]664
[fc2677b]665  oe_save_excursion(e, &x);
[84027015]666
[fc2677b]667  while (owl_editwin_point_move(e, -1)) {
668    c = owl_editwin_get_char_at_point(e);
669    if (owl_util_can_break_after(c) || c == '\n') {
670      if (c != '\n')
671        owl_editwin_replace(e, c != ' ' ? 0 : 1, "\n");
672      break;
[2d4ff14]673    }
[7d4fbcd]674  }
[fc2677b]675
676  oe_restore_excursion(e, &x);
[7d4fbcd]677}
678
[c9334b1]679/* delete the character at the current point, following chars
680 * shift left.
[e20d8179]681 */
[c9334b1]682void owl_editwin_delete_char(owl_editwin *e)
683{
[5b5f3e6]684  owl_editwin_replace(e, 1, "");
[7d4fbcd]685}
686
[c9334b1]687/* Swap the character at point with the character at point-1 and
688 * advance the pointer.  If point is at beginning of buffer do
689 * nothing.  If point is after the last character swap point-1 with
[e20d8179]690 * point-2.  (Behaves as observed in tcsh and emacs).
[c9334b1]691 */
692void owl_editwin_transpose_chars(owl_editwin *e)
693{
[e19eb97]694  const char *middle, *end, *start;
[65b2173]695  char *tmp;
[f2e36b5]696
[47519e1b]697  if (e->bufflen == 0) return;
[e20d8179]698
[5b5f3e6]699  if (e->index == e->bufflen)
700    owl_editwin_point_move(e, -1);     /* point is after last character */
[f2e36b5]701
[5b5f3e6]702  if (owl_editwin_at_beginning_of_buffer(e))
703    return;     /* point is at beginning of buffer, do nothing */
[f2e36b5]704
[47519e1b]705  /* Transpose two utf-8 unicode glyphs. */
[5b5f3e6]706  middle = e->buff + e->index;
[47519e1b]707
[5b5f3e6]708  end = oe_next_point(e, middle);
709  if (end == NULL)
[e20d8179]710    return;
[47519e1b]711
[5b5f3e6]712  start = oe_prev_point(e, middle);
713  if (start == NULL)
[e20d8179]714    return;
[47519e1b]715
[5b5f3e6]716  tmp = owl_malloc((end - start) + 1);
717  tmp[(end - start)] = 0;
718  memcpy(tmp, middle, end - middle);
719  memcpy(tmp + (end - middle), start, middle - start);
720
721  owl_editwin_point_move(e, -1);
722  owl_editwin_replace(e, 2, tmp);
[f2e36b5]723}
724
[c9334b1]725/* insert 'string' at the current point, later text is shifted
726 * right
727 */
[e19eb97]728void owl_editwin_insert_string(owl_editwin *e, const char *s)
[c9334b1]729{
[5b5f3e6]730  owl_editwin_replace(e, 0, s);
[7d4fbcd]731}
732
[a556caa]733/* We assume index is not set to point to a mid-char */
[bab52da]734static gunichar owl_editwin_get_char_at_point(owl_editwin *e)
[c9334b1]735{
[a556caa]736  return g_utf8_get_char(e->buff + e->index);
737}
[7d4fbcd]738
[7f0c26f]739void owl_editwin_exchange_point_and_mark(owl_editwin *e) {
740  int tmp;
741
742  if (e->mark != -1) {
743    tmp = e->mark;
744    owl_editwin_set_mark(e);
745    oe_set_index(e, tmp);
746  }
747}
748
[e20d8179]749int owl_editwin_point_move(owl_editwin *e, int delta)
[84027015]750{
[e19eb97]751  const char *p;
[19f765d]752  int change, d = 0;
[a556caa]753
754  change = MAX(delta, - delta);
755  p = e->buff + e->index;
756
757  while (d < change && p != NULL) {
[e20d8179]758    if (delta > 0)
759      p = oe_next_point(e, p);
760    else
761      p = oe_prev_point(e, p);
762    if (p != NULL) {
[521bc84]763      oe_set_index(e, p - e->buff);
[e20d8179]764      d++;
[a556caa]765    }
766  }
767
768  return delta > 0 ? d : -d;
[84027015]769}
770
[a556caa]771int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
772  if (e->index == e->lock)
773    return 1;
[84027015]774
[a556caa]775  return 0;
776}
777
778int owl_at_end_of_buffer(owl_editwin *e) {
779  if (e->index == e->bufflen)
780    return 1;
781
782  return 0;
783}
784
785int owl_editwin_at_beginning_of_line(owl_editwin *e) /*noproto*/
[c9334b1]786{
[a0fbdee]787  oe_excursion x;
[a556caa]788  int ret;
[47519e1b]789
[a556caa]790  if (owl_editwin_at_beginning_of_buffer(e))
791    return 1;
[47519e1b]792
[a0fbdee]793  oe_save_excursion(e, &x);
[a556caa]794  owl_editwin_point_move(e, -1);
[bab52da]795  ret = (owl_editwin_get_char_at_point(e) == '\n');
[a0fbdee]796  oe_restore_excursion(e, &x);
[a556caa]797
798  return ret;
799}
800
[e19eb97]801static int owl_editwin_is_char_in(owl_editwin *e, const char *set)
[a556caa]802{
[e19eb97]803  const char *p;
[5b5f3e6]804
805  for (p = set; *p != 0; p = g_utf8_find_next_char(p, NULL))
806    if (owl_editwin_get_char_at_point(e) == g_utf8_get_char(p))
[a556caa]807      return 1;
808  return 0;
809}
810
[e19eb97]811int owl_editwin_move_if_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;
[47519e1b]819  }
[a556caa]820  return distance;
821}
822
[e19eb97]823int owl_editwin_move_if_not_in(owl_editwin *e, int delta, const char *set)
[a556caa]824{
825  int change, distance = 0;
[e20d8179]826  while (!owl_editwin_is_char_in(e, set)) {
[a556caa]827    change = owl_editwin_point_move(e, delta);
828    distance += change;
829    if (change == 0)
830      break;
[7d4fbcd]831  }
[a556caa]832  return distance;
[7d4fbcd]833}
834
[e20d8179]835int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
[47519e1b]836{
[a556caa]837  int distance = 0;
[47519e1b]838
[a556caa]839  if (!owl_editwin_at_beginning_of_line(e)) {
840    /* move off the \n if were at the end of a line */
841    distance += owl_editwin_point_move(e, -1);
842    distance += owl_editwin_move_if_not_in(e, -1, "\n");
843    if (distance && !owl_editwin_at_beginning_of_buffer(e))
844      distance += owl_editwin_point_move(e, 1);
[47519e1b]845  }
[77d4402]846  e->goal_column = 0; /* subtleties */
[84027015]847
[a556caa]848  return distance;
849}
[e20d8179]850
851int owl_editwin_move_to_end_of_line(owl_editwin *e)
[a556caa]852{
853  return owl_editwin_move_if_not_in(e, 1, "\n");
[47519e1b]854}
855
[e20d8179]856int owl_editwin_line_move(owl_editwin *e, int delta)
[c9334b1]857{
[d7043b4]858  int goal_column, change, ll, distance;
859  int count = 0;
[a556caa]860
861  change = MAX(delta, -delta);
862
[d7043b4]863  goal_column = e->goal_column;
864  distance = owl_editwin_move_to_beginning_of_line(e);
865  goal_column = goal_column == -1 ? -distance : goal_column;
[a556caa]866
867  while(count < change) {
868    if (delta > 0) {
869      distance += owl_editwin_move_if_not_in(e, 1, "\n");
870      distance += owl_editwin_point_move(e, 1);
871    } else {
872      /* I really want to assert delta < 0 here */
873      distance += owl_editwin_point_move(e, -1); /* to the newline on
874                                                    the previous line */
875      distance += owl_editwin_move_to_beginning_of_line(e);
876    }
877    count++;
[7d4fbcd]878  }
[a556caa]879
880  distance += (ll = owl_editwin_move_to_end_of_line(e));
881  if (ll > goal_column)
882    distance += owl_editwin_point_move(e, goal_column - ll);
883
884  e->goal_column = goal_column;
885
886  return distance;
[7d4fbcd]887}
888
[c9334b1]889void owl_editwin_backspace(owl_editwin *e)
890{
[7d4fbcd]891  /* delete the char before the current one
892   * and shift later chars left
893   */
[a556caa]894  if(owl_editwin_point_move(e, -1))
[7d4fbcd]895    owl_editwin_delete_char(e);
896}
897
[c9334b1]898void owl_editwin_key_up(owl_editwin *e)
899{
[a556caa]900  owl_editwin_line_move(e, -1);
[7d4fbcd]901}
902
[c9334b1]903void owl_editwin_key_down(owl_editwin *e)
904{
[a556caa]905  owl_editwin_line_move(e, 1);
[7d4fbcd]906}
907
[c9334b1]908void owl_editwin_key_left(owl_editwin *e)
909{
[a556caa]910  owl_editwin_point_move(e, -1);
[7d4fbcd]911}
912
[c9334b1]913void owl_editwin_key_right(owl_editwin *e)
914{
[a556caa]915  owl_editwin_point_move(e, 1);
[7d4fbcd]916}
917
[5b5f3e6]918int owl_editwin_forward_word(owl_editwin *e)
[c9334b1]919{
[5b5f3e6]920  int distance;
[7d4fbcd]921  /* if we're starting on a space, find the first non-space */
[5b5f3e6]922  distance = owl_editwin_move_if_in(e, 1, WHITESPACE);
[7d4fbcd]923
[a556caa]924  /* now find the end of this word */
[5b5f3e6]925  distance += owl_editwin_move_if_not_in(e, 1, WHITESPACE);
926
927  return distance;
928}
929
930void owl_editwin_move_to_nextword(owl_editwin *e)
931{
932  owl_editwin_forward_word(e);
[7d4fbcd]933}
934
[c9334b1]935/* go backwards to the last non-space character
936 */
[5b5f3e6]937int owl_editwin_backward_word(owl_editwin *e)
[c9334b1]938{
[a0fbdee]939  oe_excursion x;
[5b5f3e6]940  int distance = 0;
941  int further = 0;
[a556caa]942  int beginning;
943  /* if in middle of word, beginning of word */
944
945  /* if at beginning of a word, find beginning of previous word */
946
[e20d8179]947  if (owl_editwin_is_char_in(e, WHITESPACE)) {
[a556caa]948    /* if in whitespace past end of word, find a word , the find the beginning*/
[5b5f3e6]949    distance += owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
950                                                              character of the word */
[a0fbdee]951    oe_save_excursion(e, &x);
[a556caa]952    /* are we at the beginning of a word? */
953    owl_editwin_point_move(e, -1);
[e20d8179]954    beginning = owl_editwin_is_char_in(e, WHITESPACE);
[a0fbdee]955    oe_restore_excursion(e, &x);
[a556caa]956    if (beginning)
[5b5f3e6]957      return distance;
[a556caa]958   } else {
959    /* in the middle of the word; */
[a0fbdee]960    oe_save_excursion(e, &x);
[5b5f3e6]961    further += owl_editwin_point_move(e, -1);
[e20d8179]962    if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
[5b5f3e6]963      distance += owl_editwin_backward_word(e); /* previous case */
[8321cb7]964      oe_release_excursion(e, &x);
[5b5f3e6]965      return distance + further;
[a556caa]966    } else {
[a0fbdee]967      oe_restore_excursion(e, &x);
[7d4fbcd]968    }
969  }
[5b5f3e6]970  distance += owl_editwin_move_if_not_in(e, -1, WHITESPACE);
[a556caa]971  /* will go past */
972  if (e->index > e->lock)
[5b5f3e6]973    distance += owl_editwin_point_move(e, 1);
974  return distance;
975}
976
977void owl_editwin_move_to_previousword(owl_editwin *e)
978{
979  owl_editwin_backward_word(e);
[7d4fbcd]980}
981
[c9334b1]982void owl_editwin_delete_nextword(owl_editwin *e)
983{
[a0fbdee]984  oe_excursion x;
[a556caa]985
[a0fbdee]986  oe_save_excursion(e, &x);
[a60edf2]987  oe_set_mark(e, e->index);
988  owl_editwin_forward_word(e);
989  owl_editwin_kill_region(e);
990  oe_restore_mark_only(e, &x);
[7d4fbcd]991}
992
[c9334b1]993void owl_editwin_delete_previousword(owl_editwin *e)
994{
[a60edf2]995  oe_excursion x;
[b68f9cd]996
[a60edf2]997  oe_save_excursion(e, &x);
998  oe_set_mark(e, e->index);
999  owl_editwin_backward_word(e);
1000  owl_editwin_kill_region(e);
1001  oe_restore_mark_only(e, &x);
[b68f9cd]1002}
1003
[a556caa]1004void owl_editwin_move_to_line_end(owl_editwin *e)
[c9334b1]1005{
[a556caa]1006  owl_editwin_move_to_end_of_line(e);
[7d4fbcd]1007}
1008
[a556caa]1009void owl_editwin_delete_to_endofline(owl_editwin *e)
[c9334b1]1010{
[a0fbdee]1011  oe_excursion x;
[5b5f3e6]1012  int distance;
[a556caa]1013
[a0fbdee]1014  oe_save_excursion(e, &x);
[a60edf2]1015  owl_editwin_set_mark(e);
[5b5f3e6]1016  distance = owl_editwin_move_to_end_of_line(e);
[a60edf2]1017  if (distance)
1018    owl_editwin_kill_region(e);
1019  else
1020    owl_editwin_replace(e, 1, "");
[a0fbdee]1021  oe_restore_excursion(e, &x);
[a60edf2]1022}
1023
1024void owl_editwin_yank(owl_editwin *e)
1025{
1026  if (e->killbuf != NULL)
1027    owl_editwin_replace(e, 0, e->killbuf);
1028}
1029
[e19eb97]1030static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len)
[a60edf2]1031{
1032  char *p;
1033
1034  p = owl_malloc(len + 1);
1035
1036  if (p != NULL) {
1037    owl_free(e->killbuf);
1038    e->killbuf = p;
1039    memcpy(e->killbuf, buf, len);
1040    e->killbuf[len] = 0;
1041  }
1042
1043  return p;
1044}
1045
1046static int oe_copy_region(owl_editwin *e)
1047{
[e19eb97]1048  const char *p;
[a60edf2]1049  int start, end;
1050
1051  if (e->mark == -1)
1052    return 0;
1053
1054  start = MIN(e->index, e->mark);
1055  end = MAX(e->index, e->mark);
1056
1057  p = oe_copy_buf(e, e->buff + start, end - start);
1058  if (p != NULL)
1059    return end - start;
1060  return 0;
1061}
1062
1063void owl_editwin_copy_region_as_kill(owl_editwin *e)
1064{
1065  oe_copy_region(e);
1066}
1067
1068void owl_editwin_kill_region(owl_editwin *e)
1069{
1070  if (e->index > e->mark)
1071    owl_editwin_exchange_point_and_mark(e);
1072
[2184001]1073  owl_editwin_replace_internal(e, oe_copy_region(e), "");
[7d4fbcd]1074}
1075
[c9334b1]1076void owl_editwin_move_to_line_start(owl_editwin *e)
1077{
[a556caa]1078  owl_editwin_move_to_beginning_of_line(e);
[7d4fbcd]1079}
1080
[c9334b1]1081void owl_editwin_move_to_end(owl_editwin *e)
1082{
[521bc84]1083  oe_set_index(e, e->bufflen);
[7d4fbcd]1084}
1085
[c9334b1]1086void owl_editwin_move_to_top(owl_editwin *e)
1087{
[521bc84]1088  oe_set_index(e, e->lock);
[7d4fbcd]1089}
1090
[2fc8397]1091void owl_editwin_backward_paragraph(owl_editwin *e)
1092{
1093  owl_editwin_point_move(e, -1);
1094  for (; e->index >= e->lock; owl_editwin_point_move(e, -1)) {
1095    if (e->index <= e->lock ||
1096        ((e->buff[e->index] == '\n') && (e->buff[e->index - 1]=='\n')))
1097      break;
1098  }
1099}
1100
1101void owl_editwin_forward_paragraph(owl_editwin *e)
1102{
1103  owl_editwin_point_move(e, 1);
1104  /* scan forward to the start of the next paragraph */
1105  for(; e->index < e->bufflen; owl_editwin_point_move(e, 1)) {
1106    if (e->buff[e->index -1] == '\n' && e->buff[e->index] == '\n')
1107      break;
1108  }
1109}
1110
[d41294a]1111int owl_editwin_current_column(owl_editwin *e)
[c9334b1]1112{
[a0fbdee]1113  oe_excursion x;
[fc2677b]1114  int lineindex;
[7d4fbcd]1115
[a0fbdee]1116  oe_save_excursion(e, &x);
[fc2677b]1117  owl_editwin_move_to_beginning_of_line(e);
1118  lineindex = e->index;
1119  oe_restore_excursion(e, &x);
1120  return oe_region_width(e, lineindex, e->index, 0);
1121}
[a556caa]1122
[fc2677b]1123void owl_editwin_fill_paragraph(owl_editwin *e)
1124{
1125  oe_excursion x;
1126  gunichar ch;
1127  int sentence;
[7d4fbcd]1128
[fc2677b]1129  oe_save_excursion(e, &x);
[7d4fbcd]1130
[fc2677b]1131  /* Mark the end of the paragraph */
1132  owl_editwin_forward_paragraph(e);
1133  /* Skip the trailing newline */
1134  owl_editwin_point_move(e, -1);
1135  owl_editwin_set_mark(e);
[7d4fbcd]1136
[fc2677b]1137  owl_editwin_backward_paragraph(e);
[7d4fbcd]1138
[fc2677b]1139  /* Don't mess with the leading newline */
1140  if (owl_editwin_get_char_at_point(e) == '\n')
1141    owl_editwin_point_move(e, 1);
[7d4fbcd]1142
[fc2677b]1143  /*
1144   * First pass: Scan forward replacing all series of spaces with ' '
1145   * (or nothing after CJK ideograms)
1146   */
1147  sentence = 0;
1148  for(;e->index < e->mark; owl_editwin_point_move(e, 1)) {
[50e671c]1149    /* bail if we hit a trailing dot on the buffer */
[fc2677b]1150    if (strcmp(e->buff + e->index, "\n.") == 0) {
1151      owl_editwin_set_mark(e);
[50e671c]1152      break;
[7d4fbcd]1153    }
1154
[fc2677b]1155    ch = owl_editwin_get_char_at_point(e);
[b2c1bd4]1156
[fc2677b]1157    if (owl_util_can_break_after(ch) || ch == '\n') {
1158      if (g_unichar_isspace(ch)) {
1159        owl_editwin_replace(e, 1, " ");
1160      }
1161
[bd7fb58]1162      if (sentence && g_unichar_isspace(owl_editwin_get_char_at_point(e)))
[fc2677b]1163        owl_editwin_point_move(e, 1);
1164
1165      while(g_unichar_isspace(owl_editwin_get_char_at_point(e))
1166            && e->index < e->mark) {
1167        owl_editwin_delete_char(e);
[7d4fbcd]1168      }
1169    }
[fc2677b]1170
1171    if(ch == '.' || ch == '!' || ch == '?')
1172      sentence = 1;
1173    else
1174      sentence = 0;
1175  }
1176
1177  owl_editwin_backward_paragraph(e);
1178
1179  /* Now go through inserting newlines as needed */
1180  while(e->index < e->mark) {
1181    /* if we've travelled too far, linewrap */
[d41294a]1182    if (owl_editwin_current_column(e) >= e->fillcol)
[fc2677b]1183      _owl_editwin_linewrap_word(e);
1184    owl_editwin_point_move(e, 1);
[7d4fbcd]1185  }
1186
[a0fbdee]1187  oe_restore_excursion(e, &x);
[7d4fbcd]1188}
1189
[cf83b7a]1190/* returns true if only whitespace remains */
[c9334b1]1191int owl_editwin_is_at_end(owl_editwin *e)
1192{
[a556caa]1193  return (only_whitespace(e->buff + e->index));
[217a43e]1194}
1195
[bab52da]1196static int owl_editwin_check_dotsend(owl_editwin *e)
[c9334b1]1197{
[dc7884d]1198  int zdot = 0;
[72ab15f]1199  oe_excursion x;
[7d4fbcd]1200
1201  if (!e->dotsend) return(0);
[4ccd92c]1202  if (!owl_editwin_is_at_end(e)) return (0);
[47519e1b]1203
[72ab15f]1204  oe_save_excursion(e, &x);
1205
1206  owl_editwin_point_move(e, -3);
1207
[4ccd92c]1208  if(strncmp(e->buff + e->index, "\n.\n", 3) == 0) {
[dc7884d]1209    owl_editwin_point_move(e, 1);
1210    zdot = 1;
[0509efc]1211  } else if(e->index == e->lock &&
[4ccd92c]1212            strncmp(e->buff + e->index, ".\n", 2) == 0) {
[0509efc]1213    zdot = 1;
1214  }
1215
1216  if(zdot) {
[4ccd92c]1217    owl_editwin_set_mark(e);
1218    owl_editwin_move_to_end(e);
1219    owl_editwin_replace_region(e, "");
[dc7884d]1220  }
[72ab15f]1221
1222  oe_restore_excursion(e, &x);
1223
1224  return zdot;
[7d4fbcd]1225}
1226
[fac5463]1227void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
[c9334b1]1228{
[a556caa]1229  /* XXX force a redisplay? */
[fac5463]1230  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
[435d6b2]1231    owl_command_edit_done(e);
[7d4fbcd]1232    return;
1233  }
[e20d8179]1234  owl_editwin_redisplay(e, 0);
[7d4fbcd]1235}
1236
[3e36085]1237static int oe_region_width(owl_editwin *e, int start, int end, int offset)
[16cfd12a]1238{
[e19eb97]1239  const char *p;
[3e36085]1240  int width = offset;
1241 
[16cfd12a]1242  for(p = e->buff + start;
1243      p < e->buff + end;
1244      p = g_utf8_find_next_char(p, NULL))
1245    width += oe_char_width(g_utf8_get_char(p), width);
1246
[3e36085]1247  return width - offset;
[16cfd12a]1248}
1249
[ebf0128]1250static void oe_insert_char(owl_editwin *e, gunichar c)
[fac5463]1251{
[16cfd12a]1252  oe_excursion x;
[5b5f3e6]1253  char tmp[7];
[16cfd12a]1254  int replaced = -1;
[5b5f3e6]1255
[ebf0128]1256  if (c == '\r') /* translate CRs to NLs */
1257    c = '\n';
1258
[16cfd12a]1259  if (!g_unichar_iscntrl(c) || c == '\n' || c== '\t' ) {
[ebf0128]1260    memset(tmp, 0, 7);
1261
1262    if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
1263      return;
1264    }
1265
[16cfd12a]1266    if (e->cursorx != -1 && e->cursorx + oe_char_width(c, e->cursorx) > e->wrapcol) {
1267      /* XXX this is actually wrong:
1268       * + If the line has been been wrapped, we can be past the wrap column but
1269       *   e->cursorx be much smaller.
1270       * + If the user went back and inserted a bunch of stuff in the middle of
1271       *   the line, there may be more than one word past the wrap column.
1272       */
1273      oe_save_excursion(e, &x);
1274
1275      if (c == ' ' || c == '\t') {
1276        owl_editwin_point_move(e, -1);
1277        replaced = -owl_editwin_move_if_in(e, -1, " \t");
1278        if (!replaced) {
1279          c = '\n';
1280          replaced = -1;
1281        }
1282      } else {
1283        while(!owl_editwin_at_beginning_of_line(e)) {
1284          owl_editwin_point_move(e, -1);
1285          if (owl_util_can_break_after(owl_editwin_get_char_at_point(e))) {
1286            replaced = -owl_editwin_move_if_in(e, -1, " \t");
1287            break;
1288          }
1289        }
1290        if (owl_editwin_at_beginning_of_line(e))
1291          replaced = -1;
1292      }
1293      if (replaced && !owl_editwin_at_beginning_of_line(e))
1294        owl_editwin_point_move(e, 1);
1295      if (replaced >= 0) {
1296        owl_editwin_replace(e, replaced, "\n");
1297      }
1298      oe_restore_excursion(e, &x);
1299    }
1300
1301    if (replaced >= 0 && (c == ' ' || c == '\t'))
1302      return; /* our work here is done */
1303
[ebf0128]1304    g_unichar_to_utf8(c, tmp);
1305    owl_editwin_replace(e, 0, tmp);
1306  }
1307}
1308
1309void owl_editwin_process_char(owl_editwin *e, owl_input j)
1310{
[5b5f3e6]1311  if (j.ch == ERR)
1312    return;
[fac5463]1313  /* Ignore ncurses control characters. */
[e20d8179]1314  if (j.ch < 0x100) {
[ebf0128]1315    oe_insert_char(e, j.uch);
[fac5463]1316  }
1317}
1318
[e19eb97]1319const char *owl_editwin_get_text(owl_editwin *e)
[c9334b1]1320{
[7d4fbcd]1321  return(e->buff+e->lock);
1322}
1323
[d41294a]1324char *owl_editwin_get_region(owl_editwin *e)
1325{
1326  int start, end;
1327  start = e->index;
1328  end   = e->mark;
1329  if(start > end) {
1330    int tmp = end;
1331    end = start;
1332    start = tmp;
1333  }
1334
1335  return oe_chunk(e, start, end);
1336}
1337
[77f605d]1338int owl_editwin_get_echochar(owl_editwin *e)
1339{
[a556caa]1340  return e->echochar;
1341}
[77f605d]1342
1343static char *oe_chunk(owl_editwin *e, int start, int end)
1344{
1345  char *p;
1346 
1347  p = owl_malloc(end - start + 1);
1348  memcpy(p, e->buff + start, end - start);
1349  p[end - start] = 0;
1350
1351  return p;
1352}
1353
[d41294a]1354/*
1355 * The only guarantee made about these values is that comparisons
1356 * between them, as well as comparison between multiple calls to these
1357 * functions without modifying the editwin in-between, are meaningful.
1358 */
1359
1360int owl_editwin_get_point(owl_editwin *e)
1361{
1362  return e->index;
1363}
1364
1365int owl_editwin_get_mark(owl_editwin *e)
1366{
1367  return e->mark;
1368}
1369
[2f21a41]1370
1371/*
1372 * Local Variables:
1373 * mode:C
1374 * c-basic-offset:2
1375 * End:
1376 */
Note: See TracBrowser for help on using the repository browser.