source: editwin.c @ f9d257b

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