source: editwin.c @ 7f0c26f

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