source: editwin.c @ a60edf2

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