source: editwin.c @ d625cfd

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