source: editwin.c @ c6ecf5c

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