source: editwin.c @ bab52da

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