source: editwin.c @ 1b1cd2c

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