source: editwin.c @ 16cfd12a

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