source: editwin.c @ 521bc84

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