source: editwin.c @ 77d4402

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 77d4402 was 77d4402, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
cursor location improved; other behavior The cursor locater now handles wrapped lines /better/ but still messes up when the line is the width of the screen and the cursor is at the end (so it should be a the beginning of a blank screen line) C-a set the goal column kill-to-end-of-line will eat a final newline and thus join.
  • 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
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  while(1) {
315    /* note the position of the dot */
316    if (x != NULL && index == e->index)
317      *x = width;
318
319    /* get the current character */
320    c = g_utf8_get_char(e->buff + index);
321
322    /* figure out how wide it is */
323    if (c == 9) /* TAB */
324      cw = TABSIZE - width % TABSIZE;
325    else
326      cw = mk_wcwidth(c);
327    if (cw < 0) /* control characters */
328        cw = 0;
329
330    if (width + cw > e->wincols) {
331      if (x != NULL && *x == width)
332        *x = -1;
333      break;
334    }
335    width += cw;
336
337    if (c == '\n') {
338      if (width < e->wincols)
339        ++index; /* skip the newline */
340      break;
341    }
342
343    /* find the next character */
344    p = oe_next_point(e, e->buff + index);
345    if (p == NULL) { /* we ran off the end */
346      if (x != NULL && e->index > index)
347        *x = width + 1;
348      break;
349    }
350    index = p - e->buff;
351
352  }
353  return index;
354}
355
356static void oe_reframe(owl_editwin *e) {
357  oe_excursion x;
358  int goal = e->winlines / 2;
359  int index;
360  int count = 0;
361  int point;
362  int n, i;
363  int last;
364
365  oe_save_excursion(e, &x);
366  /* step back line-by-line through the buffer until we have >= goal lines of
367     display text */
368  e->lock = 0; /* we can (must) tread on the locktext */
369
370  point = e->index;
371  last = -1;
372  while (count < goal) {
373    index = e->index;
374    owl_editwin_move_to_beginning_of_line(e);
375    if (last == e->index)
376      break;
377    last = e->index;
378    for (n = 0, i = e->index; i < index; n++)
379      i = oe_find_display_line(e, NULL, i);
380    count += n == 0 ? 1 : n;
381    if (count < goal)
382      owl_editwin_point_move(e, -1);
383  }
384  /* if we overshot, backtrack */
385  for (n = 0; n < (count - goal); n++)
386    e->index = oe_find_display_line(e, NULL, e->index);
387
388  e->topindex = e->index;
389
390  oe_restore_excursion(e, &x);
391}
392
393/* regenerate the text on the curses window */
394/* if update == 1 then do a doupdate(), otherwise do not */
395void owl_editwin_redisplay(owl_editwin *e, int update)
396{
397  int x = -1, y = -1, t;
398  int line, index, lineindex, times = 0;
399
400  do {
401    werase(e->curswin);
402
403    if (e->topindex == -1 || e->index < e->topindex)
404      oe_reframe(e);
405
406    line = 0;
407    index = e->topindex;
408    while(line < e->winlines) {
409      lineindex = index;
410      t = -1;
411      index = oe_find_display_line(e, &t, lineindex);
412      if (x == -1 && t != -1)
413        x = t, y = line;
414      if (index - lineindex)
415        mvwaddnstr(e->curswin, line, 0,
416                   e->buff + lineindex,
417                   index - lineindex);
418      line++;
419    }
420    if (x == -1)
421        e->topindex = -1; /* force a reframe */
422    times++;
423  } while(x == -1 && times < 3);
424
425  wmove(e->curswin, y, x);
426  wnoutrefresh(e->curswin);
427  if (update == 1)
428    doupdate();
429}
430
431/* Remove n bytes at cursor. */
432void _owl_editwin_remove_bytes(owl_editwin *e, int n) /*noproto*/
433{
434  int i = e->index + n;
435  for (; i < e->bufflen; i++) {
436    e->buff[i-n] = e->buff[i];
437  }
438
439  e->bufflen -= n;
440  e->buff[e->bufflen] = '\0';
441}
442
443/* Insert n bytes at cursor.*/
444void _owl_editwin_insert_bytes(owl_editwin *e, int n) /*noproto*/
445{
446  int i;
447
448  if ((e->bufflen + n) > (e->allocated - 5)) {
449    _owl_editwin_addspace(e);
450  }
451
452  if(e->index != e->bufflen) {
453    for (i = e->bufflen + n - 1; i > e->index; i--) {
454      e->buff[i] = e->buff[i - n];
455    }
456  }
457
458  e->bufflen += n;
459  e->buff[e->bufflen] = '\0';
460
461}
462
463
464/* linewrap the word just before the cursor.
465 * returns 0 on success
466 * returns -1 if we could not wrap.
467 */
468int _owl_editwin_linewrap_word(owl_editwin *e)
469{
470  int i;
471  char *ptr1, *start;
472  gunichar c;
473
474  start = e->buff + e->lock;
475
476  ptr1 = e->buff + e->index;
477  ptr1 = g_utf8_find_prev_char(start, ptr1);
478
479  while (ptr1) {
480    c = g_utf8_get_char(ptr1);
481    if (owl_util_can_break_after(c)) {
482      if (c != ' ') {
483        i = ptr1 - e->buff;
484        e->index = i;
485        _owl_editwin_insert_bytes(e, 1);
486        /* _owl_editwin_insert_bytes may move e->buff. */
487        ptr1 = e->buff + i;
488      }
489      *ptr1 = '\n';
490      return 0;
491    }
492    else if (c == '\n') {
493      return 0;
494    }
495    ptr1 = g_utf8_find_prev_char(start, ptr1);
496  }
497  return -1;
498}
499
500/* insert a character at the current point (shift later
501 * characters over)
502 */
503void owl_editwin_insert_char(owl_editwin *e, gunichar c)
504{
505  int i, ret, len;
506  char tmp[6];
507  memset(tmp, '\0', 6);
508
509  /* \r is \n */
510  if (c == '\r') {
511    c = '\n';
512  }
513
514  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
515    /* perhaps later this will change some state that allows the string
516       to be read */
517    return;
518  }
519
520  g_unichar_to_utf8(c, tmp);
521  len = strlen(tmp);
522
523  /* make sure there is enough memory for the new text */
524  if ((e->bufflen + len) > (e->allocated - 5)) {
525    _owl_editwin_addspace(e);
526  }
527
528  /* If we're going to insert at the last column do word wrapping, unless it's a \n */
529#if 0 /* XXX */
530  if ((e->buffx + 1 == e->wrapcol) && (c != '\n')) {
531    ret = _owl_editwin_linewrap_word(e);
532    if (ret == -1) {
533      /* we couldn't wrap, insert a hard newline instead */
534      owl_editwin_insert_char(e, '\n');
535    }
536  }
537#endif
538
539  /* shift all the other characters right */
540  _owl_editwin_insert_bytes(e, len);
541
542  /* insert the new character */
543  for(i = 0; i < len; i++) {
544    e->buff[e->index + i] = tmp[i];
545  }
546
547  /* advance the cursor */
548  e->index += len;
549}
550
551/* overwrite the character at the current point with 'c' */
552void owl_editwin_overwrite_char(owl_editwin *e, gunichar c)
553{
554  int oldlen, newlen, i;
555  char tmp[6], *t;
556  memset(tmp, '\0', 6);
557
558  /* \r is \n */
559  if (c == '\r') {
560    c = '\n';
561  }
562
563  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
564    /* perhaps later this will change some state that allows the string
565       to be read */
566    return;
567  }
568
569  g_unichar_to_utf8(c, tmp);
570  newlen = strlen(tmp);
571
572  t = g_utf8_find_next_char(e->buff + e->index, NULL);
573  oldlen = (t ? (t - (e->buff + e->index)) : 0);
574
575  /* only if we are at the end of the buffer do we create new space here */
576  if (e->index == e->bufflen) {
577    if ((e->bufflen+newlen) > (e->allocated-5)) {
578      _owl_editwin_addspace(e);
579    }
580  }
581  /* if not at the end of the buffer, adjust based in char size difference. */
582  else if (oldlen > newlen) {
583    _owl_editwin_remove_bytes(e, oldlen-newlen);
584  }
585  else /* oldlen < newlen */ {
586    _owl_editwin_insert_bytes(e, newlen-oldlen);
587  }
588  /* Overwrite the old char*/
589  for (i = 0; i < newlen; i++) {
590    e->buff[e->index + i] = tmp[i];
591  }
592
593  /* housekeeping */
594  if (e->index == e->bufflen) {
595    e->bufflen += newlen;
596    e->buff[e->bufflen] = '\0';
597  }
598
599  /* advance the cursor */
600  e->index += newlen;
601}
602
603/* delete the character at the current point, following chars
604 * shift left.
605 */
606void owl_editwin_delete_char(owl_editwin *e)
607{
608  char *p1, *p2;
609  gunichar c;
610
611  if (e->bufflen == 0) return;
612
613  if (e->index == e->bufflen) return;
614
615  p1 = e->buff + e->index;
616  p2 = g_utf8_next_char(p1);
617  c = g_utf8_get_char(p2);
618  while (g_unichar_ismark(c)) {
619    p2 = g_utf8_next_char(p2);
620    c = g_utf8_get_char(p2);
621  }
622  _owl_editwin_remove_bytes(e, p2-p1);
623}
624
625/* Swap the character at point with the character at point-1 and
626 * advance the pointer.  If point is at beginning of buffer do
627 * nothing.  If point is after the last character swap point-1 with
628 * point-2.  (Behaves as observed in tcsh and emacs).
629 */
630void owl_editwin_transpose_chars(owl_editwin *e)
631{
632  char *p1, *p2, *p3, *tmp;
633
634  if (e->bufflen == 0) return;
635
636  if (e->index == e->bufflen) {
637    /* point is after last character */
638    e->index--;
639  }
640
641  if (e->index - 1 < e->lock) {
642    /* point is at beginning of buffer, do nothing */
643    return;
644  }
645
646  /* Transpose two utf-8 unicode glyphs. */
647  p1 = e->buff + e->index;
648
649  p2 = oe_next_point(e, p1); /* XXX make sure we can't transpose past the end
650                                of the buffer */
651  if (p2 == NULL)
652    return;
653
654  p3 = oe_prev_point(e, p1);
655  if (p3 == NULL)
656    return;
657
658  tmp = owl_malloc(p2 - p3 + 5);
659  *tmp = '\0';
660  strncat(tmp, p1, p2 - p1);
661  strncat(tmp, p3, p1 - p3);
662  strncpy(p3, tmp, p2 - p3);
663  owl_free(tmp);
664  e->index = p3 - e->buff;
665}
666
667/* insert 'string' at the current point, later text is shifted
668 * right
669 */
670void owl_editwin_insert_string(owl_editwin *e, char *string)
671{
672  char *p;
673  gunichar c;
674  if (!g_utf8_validate(string, -1, NULL)) {
675    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
676    return;
677  }
678  p = string;
679  c = g_utf8_get_char(p);
680  while (c) {
681    _owl_editwin_process_char(e, c);
682    p = g_utf8_next_char(p);
683    c = g_utf8_get_char(p);
684  }
685}
686
687/* write 'string' at the current point, overwriting text that is
688 * already there
689 */
690
691void owl_editwin_overwrite_string(owl_editwin *e, char *string)
692{
693  char *p;
694  gunichar c;
695
696  if (!g_utf8_validate(string, -1, NULL)) {
697    owl_function_debugmsg("owl_editwin_overwrite_string: received non-utf-8 string.");
698    return;
699  }
700  p = string;
701  c = g_utf8_get_char(p);
702  while (c) {
703    owl_editwin_overwrite_char(e, c);
704    p = g_utf8_next_char(p);
705    c = g_utf8_get_char(p);
706  }
707}
708
709/* We assume index is not set to point to a mid-char */
710gunichar _owl_editwin_get_char_at_point(owl_editwin *e)
711{
712  return g_utf8_get_char(e->buff + e->index);
713}
714
715void owl_editwin_adjust_for_locktext(owl_editwin *e)
716{
717  /* if we happen to have the cursor over locked text
718   * move it to be out of the locktext region */
719  if (e->index < e->lock) {
720    e->index = e->lock;
721  }
722}
723
724int owl_editwin_point_move(owl_editwin *e, int delta)
725{
726  char *p;
727  int change, d = 0;
728
729  change = MAX(delta, - delta);
730  p = e->buff + e->index;
731
732  while (d < change && p != NULL) {
733    if (delta > 0)
734      p = oe_next_point(e, p);
735    else
736      p = oe_prev_point(e, p);
737    if (p != NULL) {
738      e->index = p - e->buff;
739      d++;
740    }
741  }
742
743  if (delta)
744    e->goal_column = -1;
745
746  return delta > 0 ? d : -d;
747}
748
749int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
750  if (e->index == e->lock)
751    return 1;
752
753  return 0;
754}
755
756int owl_at_end_of_buffer(owl_editwin *e) {
757  if (e->index == e->bufflen)
758    return 1;
759
760  return 0;
761}
762
763int owl_editwin_at_beginning_of_line(owl_editwin *e) /*noproto*/
764{
765  oe_excursion x;
766  int ret;
767
768  if (owl_editwin_at_beginning_of_buffer(e))
769    return 1;
770
771  oe_save_excursion(e, &x);
772  owl_editwin_point_move(e, -1);
773  ret = (_owl_editwin_get_char_at_point(e) == '\n');
774  oe_restore_excursion(e, &x);
775
776  return ret;
777}
778
779static int owl_editwin_is_char_in(owl_editwin *e, char *set)
780{
781  char *p;
782  /* It would be awfully nice if we could do UTF-8 comparisons */
783  for (p = set; *p != 0; p++)
784    if (_owl_editwin_get_char_at_point(e) == *p)
785      return 1;
786  return 0;
787}
788
789int owl_editwin_move_if_in(owl_editwin *e, int delta, char *set)
790{
791  int change, distance = 0;
792  while (owl_editwin_is_char_in(e, set)) {
793    change = owl_editwin_point_move(e, delta);
794    distance += change;
795    if (change == 0)
796      break;
797  }
798  return distance;
799}
800
801int owl_editwin_move_if_not_in(owl_editwin *e, int delta, char *set)
802{
803  int change, distance = 0;
804  while (!owl_editwin_is_char_in(e, set)) {
805    change = owl_editwin_point_move(e, delta);
806    distance += change;
807    if (change == 0)
808      break;
809  }
810  return distance;
811}
812
813int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
814{
815  int distance = 0;
816
817  if (!owl_editwin_at_beginning_of_line(e)) {
818    /* move off the \n if were at the end of a line */
819    distance += owl_editwin_point_move(e, -1);
820    distance += owl_editwin_move_if_not_in(e, -1, "\n");
821    if (distance && !owl_editwin_at_beginning_of_buffer(e))
822      distance += owl_editwin_point_move(e, 1);
823  }
824  e->goal_column = 0; /* subtleties */
825
826  return distance;
827}
828
829int owl_editwin_move_to_end_of_line(owl_editwin *e)
830{
831  return owl_editwin_move_if_not_in(e, 1, "\n");
832}
833
834int owl_editwin_line_move(owl_editwin *e, int delta)
835{
836  int goal_column, change, ll, distance;
837  int count = 0;
838
839  change = MAX(delta, -delta);
840
841  goal_column = e->goal_column;
842  distance = owl_editwin_move_to_beginning_of_line(e);
843  goal_column = goal_column == -1 ? -distance : goal_column;
844
845  while(count < change) {
846    if (delta > 0) {
847      distance += owl_editwin_move_if_not_in(e, 1, "\n");
848      distance += owl_editwin_point_move(e, 1);
849    } else {
850      /* I really want to assert delta < 0 here */
851      distance += owl_editwin_point_move(e, -1); /* to the newline on
852                                                    the previous line */
853      distance += owl_editwin_move_to_beginning_of_line(e);
854    }
855    count++;
856  }
857
858  distance += (ll = owl_editwin_move_to_end_of_line(e));
859  if (ll > goal_column)
860    distance += owl_editwin_point_move(e, goal_column - ll);
861
862  e->goal_column = goal_column;
863
864  return distance;
865}
866
867void owl_editwin_backspace(owl_editwin *e)
868{
869  /* delete the char before the current one
870   * and shift later chars left
871   */
872  if(owl_editwin_point_move(e, -1))
873    owl_editwin_delete_char(e);
874}
875
876void owl_editwin_key_up(owl_editwin *e)
877{
878  owl_editwin_line_move(e, -1);
879}
880
881void owl_editwin_key_down(owl_editwin *e)
882{
883  owl_editwin_line_move(e, 1);
884}
885
886void owl_editwin_key_left(owl_editwin *e)
887{
888  owl_editwin_point_move(e, -1);
889}
890
891void owl_editwin_key_right(owl_editwin *e)
892{
893  owl_editwin_point_move(e, 1);
894}
895
896void owl_editwin_move_to_nextword(owl_editwin *e)
897{
898  /* if we're starting on a space, find the first non-space */
899  owl_editwin_move_if_in(e, 1, WHITESPACE);
900
901  /* now find the end of this word */
902  owl_editwin_move_if_not_in(e, 1, WHITESPACE);
903}
904
905/* go backwards to the last non-space character
906 */
907void owl_editwin_move_to_previousword(owl_editwin *e)
908{
909  oe_excursion x;
910  int beginning;
911  /* if in middle of word, beginning of word */
912
913  /* if at beginning of a word, find beginning of previous word */
914
915  if (owl_editwin_is_char_in(e, WHITESPACE)) {
916    /* if in whitespace past end of word, find a word , the find the beginning*/
917    owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
918                                                    character of the word */
919    oe_save_excursion(e, &x);
920    /* are we at the beginning of a word? */
921    owl_editwin_point_move(e, -1);
922    beginning = owl_editwin_is_char_in(e, WHITESPACE);
923    oe_restore_excursion(e, &x);
924    if (beginning)
925      return;
926   } else {
927    /* in the middle of the word; */
928    oe_save_excursion(e, &x);
929    owl_editwin_point_move(e, -1);
930    if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
931      owl_editwin_move_to_previousword(e); /* previous case */
932      return;
933    } else {
934      oe_restore_excursion(e, &x);
935    }
936  }
937  owl_editwin_move_if_not_in(e, -1, WHITESPACE);
938  /* will go past */
939  if (e->index > e->lock)
940    owl_editwin_point_move(e, 1);
941}
942
943void owl_editwin_delete_nextword(owl_editwin *e)
944{
945  oe_excursion x;
946  int end;
947
948  oe_save_excursion(e, &x);
949  owl_editwin_move_to_nextword(e);
950  end = e->index;
951  oe_restore_excursion(e, &x);
952  _owl_editwin_remove_bytes(e, end - e->index);
953}
954
955void owl_editwin_delete_previousword(owl_editwin *e)
956{
957  /* go backwards to the last non-space character, then delete chars */
958  int startpos, endpos;
959
960  startpos = e->index;
961  owl_editwin_move_to_previousword(e);
962  endpos = e->index;
963  _owl_editwin_remove_bytes(e, startpos-endpos);
964}
965
966void owl_editwin_move_to_line_end(owl_editwin *e)
967{
968  owl_editwin_move_to_end_of_line(e);
969}
970
971void owl_editwin_delete_to_endofline(owl_editwin *e)
972{
973  oe_excursion x;
974  int end;
975
976  oe_save_excursion(e, &x);
977  owl_editwin_move_to_line_end(e);
978  end = e->index;
979  oe_restore_excursion(e, &x);
980  if (end - e->index)
981    _owl_editwin_remove_bytes(e, end - e->index);
982  else if (end < e->bufflen)
983    _owl_editwin_remove_bytes(e, 1);
984}
985
986void owl_editwin_move_to_line_start(owl_editwin *e)
987{
988  owl_editwin_move_to_beginning_of_line(e);
989}
990
991void owl_editwin_move_to_end(owl_editwin *e)
992{
993  e->index = e->bufflen;
994}
995
996void owl_editwin_move_to_top(owl_editwin *e)
997{
998  e->index = e->lock;
999}
1000
1001void owl_editwin_fill_paragraph(owl_editwin *e)
1002{
1003#if 0 /* XXX */
1004  oe_excursion x;
1005  int i, save;
1006
1007  /* save our starting point */
1008  oe_save_excursion(e, &x);
1009
1010  save = e->index;
1011
1012  /* scan back to the beginning of this paragraph */
1013  for (i=save; i>=e->lock; i--) {
1014    if ( (i<=e->lock) ||
1015         ((e->buff[i]=='\n') && (e->buff[i-1]=='\n'))) {
1016      e->index = i + 1;
1017      break;
1018    }
1019  }
1020
1021  /* main loop */
1022  while (1) {
1023    i = _owl_editwin_get_index_from_xy(e);
1024
1025    /* bail if we hit the end of the buffer */
1026    if (i >= e->bufflen || e->buff[i] == '\0') break;
1027
1028    /* bail if we hit the end of the paragraph */
1029    if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break;
1030
1031    /* bail if we hit a trailing dot on the buffer */
1032    if (e->buff[i] == '\n' && e->buff[i+1] == '.'
1033        && ((i+2) >= e->bufflen || e->buff[i+2] == '\0'))
1034      break;
1035
1036    /* if we've travelled too far, linewrap */
1037    if ((e->buffx) >= e->fillcol) {
1038      int len = e->bufflen;
1039      _owl_editwin_linewrap_word(e);
1040      /* we may have added a character. */
1041      if (i < save) save += e->bufflen - len;
1042      e->index = i;
1043    }
1044
1045    /* did we hit the end of a line too soon? */
1046    /* asedeno: Here we replace a newline with a space. We may want to
1047       consider removing the space if the characters to either side
1048       are CJK ideograms.*/
1049    i = _owl_editwin_get_index_from_xy(e);
1050    if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) {
1051      /* ********* we need to make sure we don't pull in a word that's too long ***********/
1052      e->buff[i]=' ';
1053    }
1054
1055    /* fix spacing */
1056    i = _owl_editwin_get_index_from_xy(e);
1057    if (e->buff[i] == ' ' && e->buff[i+1] == ' ') {
1058      if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] == '?') {
1059        owl_editwin_key_right(e);
1060      } else {
1061        owl_editwin_delete_char(e);
1062        /* if we did this ahead of the save point, adjust it. Changing
1063           by one is fine here because we're only removing an ASCII
1064           space. */
1065        if (i < save) save--;
1066      }
1067    } else {
1068      owl_editwin_key_right(e);
1069    }
1070  }
1071
1072  /* put cursor back at starting point */
1073  oe_restore_excursion(e, &x);
1074#endif
1075}
1076
1077/* returns true if only whitespace remains */
1078int owl_editwin_is_at_end(owl_editwin *e)
1079{
1080  return (only_whitespace(e->buff + e->index));
1081}
1082
1083int owl_editwin_check_dotsend(owl_editwin *e)
1084{
1085  char *p, *p_n, *p_p;
1086  gunichar c;
1087
1088  if (!e->dotsend) return(0);
1089
1090  p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen);
1091  p_n = g_utf8_find_next_char(p, NULL);
1092  p_p = g_utf8_find_prev_char(e->buff, p);
1093  c = g_utf8_get_char(p);
1094  while (p != NULL) {
1095    if (*p == '.'
1096        && p_p != NULL && (*p_p == '\n' || *p_p == '\r')
1097        && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) {
1098      e->bufflen = p - e->buff;
1099      e->buff[e->bufflen] = '\0';
1100      return(1);
1101    }
1102    if (c != '\0' && !g_unichar_isspace(c)) return(0);
1103    p_n = p;
1104    p = p_p;
1105    c = g_utf8_get_char(p);
1106    p_p = g_utf8_find_prev_char(e->buff, p);
1107  }
1108  return(0);
1109}
1110
1111void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
1112{
1113  /* XXX force a redisplay? */
1114  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
1115    owl_command_editmulti_done(e);
1116    return;
1117  }
1118  owl_editwin_redisplay(e, 0);
1119}
1120
1121void _owl_editwin_process_char(owl_editwin *e, gunichar j)
1122{
1123  if (!(g_unichar_iscntrl(j) && (j != 10) && (j != 13)) || j==9 ) {
1124    owl_editwin_insert_char(e, j);
1125  }
1126}
1127
1128
1129void owl_editwin_process_char(owl_editwin *e, owl_input j)
1130{
1131  if (j.ch == ERR) return;
1132  /* Ignore ncurses control characters. */
1133  if (j.ch < 0x100) {
1134    _owl_editwin_process_char(e, j.uch);
1135  }
1136}
1137
1138char *owl_editwin_get_text(owl_editwin *e)
1139{
1140  return(e->buff+e->lock);
1141}
1142
1143int owl_editwin_get_numlines(owl_editwin *e)
1144{
1145  return(owl_text_num_lines(e->buff));
1146}
1147
1148int owl_editwin_get_echochar(owl_editwin *e) {
1149  return e->echochar;
1150}
1151
1152void owl_editwin_set_goal_column(owl_editwin *e, int column)
1153{
1154  e->goal_column = column;
1155}
1156
1157int owl_editwin_get_goal_column(owl_editwin *e)
1158{
1159  return e->goal_column;
1160}
Note: See TracBrowser for help on using the repository browser.