source: editwin.c @ b5ef65c

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