source: editwin.c @ 7ba7d66

release-1.10release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7ba7d66 was 21dd391, checked in by Nelson Elhage <nelhage@mit.edu>, 14 years ago
Remove the defunct oe_count_glyphs. We may want this again later, but we can always grab it from git history.
  • Property mode set to 100644
File size: 29.9 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <unistd.h>
4#include <string.h>
5#include <ctype.h>
6
7#define VALID_EXCURSION (0x9a2b4729)
8
9typedef struct _owl_editwin_excursion { /*noproto*/
10  int valid;
11  int index;
12  int mark;
13  int goal_column;
14  int lock;
15  struct _owl_editwin_excursion *next;
16} oe_excursion;
17
18struct _owl_editwin { /*noproto*/
19  char *buff;
20  owl_history *hist;
21  int bufflen;
22  int allocated;
23  int index;
24  int mark;
25  char *killbuf;
26  int goal_column;
27  int topindex;
28  int cursorx;
29  int winlines, wincols, fillcol, wrapcol;
30  WINDOW *curswin;
31  int style;
32  int lock;
33  int dotsend;
34  int echochar;
35  oe_excursion *excursions;
36
37  void (*callback)(struct _owl_editwin*);
38  void (*destroy_cbdata)(void *);
39  void *cbdata;
40};
41
42static void oe_reframe(owl_editwin *e);
43static void oe_save_excursion(owl_editwin *e, oe_excursion *x);
44static void oe_release_excursion(owl_editwin *e, oe_excursion *x);
45static void oe_restore_excursion(owl_editwin *e, oe_excursion *x);
46static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x);
47static int oe_char_width(gunichar c, int column);
48static int oe_region_width(owl_editwin *e, int start, int end, int width);
49static int oe_find_display_line(owl_editwin *e, int *x, int index);
50static void oe_insert_char(owl_editwin *e, gunichar c);
51static int owl_editwin_limit_maxcols(int v, int maxv);
52static int owl_editwin_check_dotsend(owl_editwin *e);
53static int owl_editwin_is_char_in(owl_editwin *e, const char *set);
54static gunichar owl_editwin_get_char_at_point(owl_editwin *e);
55static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s);
56static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len);
57static int oe_copy_region(owl_editwin *e);
58static char *oe_chunk(owl_editwin *e, int start, int end);
59static void oe_destroy_cbdata(owl_editwin *e);
60
61#define INCR 4096
62
63#define WHITESPACE " \n\t"
64
65static owl_editwin *owl_editwin_allocate(void)
66{
67  owl_editwin *e;
68  e = owl_malloc(sizeof(owl_editwin));
69  memset(e, 0, sizeof(*e));
70  return e;
71}
72
73void owl_editwin_delete(owl_editwin *e)
74{
75  owl_free(e->buff);
76  owl_free(e->killbuf);
77  /* just in case someone forgot to clean up */
78  while (e->excursions) {
79    oe_release_excursion(e, e->excursions);
80  }
81  oe_destroy_cbdata(e);
82
83  owl_free(e);
84}
85
86static inline void oe_set_index(owl_editwin *e, int index)
87{
88  if (index != e->index) {
89    e->goal_column = -1;
90    e->cursorx = -1;
91  }
92  e->index = index;
93}
94
95static inline void oe_set_mark(owl_editwin *e, int mark)
96{
97  e->mark = mark;
98}
99
100void owl_editwin_set_mark(owl_editwin *e)
101{
102  oe_set_mark(e, e->index);
103  /* owl_function_makemsg("Mark set."); */
104}
105
106static void _owl_editwin_init(owl_editwin *e,
107                              WINDOW *win,
108                              int winlines,
109                              int wincols,
110                              int style,
111                              owl_history *hist)
112{
113  e->buff=owl_malloc(INCR);
114  e->buff[0]='\0';
115  e->bufflen=0;
116  e->hist=hist;
117  e->allocated=INCR;
118  oe_set_index(e, 0);
119  oe_set_mark(e, -1);
120  if (e->killbuf != NULL)
121    owl_free(e->killbuf);
122  e->killbuf = NULL;
123  e->goal_column = -1;
124  e->cursorx = -1;
125  e->topindex = 0;
126  e->excursions = NULL;
127  owl_editwin_set_curswin(e, win, winlines, wincols);
128  e->style=style;
129  if ((style!=OWL_EDITWIN_STYLE_MULTILINE) &&
130      (style!=OWL_EDITWIN_STYLE_ONELINE)) {
131    e->style=OWL_EDITWIN_STYLE_MULTILINE;
132  }
133  e->lock=0;
134  e->dotsend=0;
135  e->echochar='\0';
136
137  if (win) werase(win);
138}
139
140owl_editwin *owl_editwin_new(WINDOW *win, int winlines, int wincols, int style, owl_history *hist)
141{
142  owl_editwin *e = owl_editwin_allocate();
143
144  _owl_editwin_init(e, win, winlines, wincols, style, hist);
145  return e;
146}
147
148void owl_editwin_set_curswin(owl_editwin *e, WINDOW *w, int winlines, int wincols)
149{
150  e->curswin=w;
151  e->winlines=winlines;
152  e->wincols=wincols;
153  e->fillcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxfillcols(&g));
154  e->wrapcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxwrapcols(&g));
155}
156
157/* echo the character 'ch' for each normal character keystroke,
158 * excepting locktext.  This is useful for entering passwords etc.  If
159 * ch=='\0' characters are echo'd normally
160 */
161void owl_editwin_set_echochar(owl_editwin *e, int ch)
162{
163  e->echochar=ch;
164}
165
166WINDOW *owl_editwin_get_curswin(owl_editwin *e)
167{
168  return(e->curswin);
169}
170
171owl_history *owl_editwin_get_history(owl_editwin *e)
172{
173  return(e->hist);
174}
175
176void owl_editwin_set_dotsend(owl_editwin *e)
177{
178  e->dotsend=1;
179}
180
181void owl_editwin_set_callback(owl_editwin *e, void (*cb)(owl_editwin*))
182{
183  e->callback = cb;
184}
185
186void (*owl_editwin_get_callback(owl_editwin *e))(owl_editwin*)
187{
188  return e->callback;
189}
190
191static void oe_destroy_cbdata(owl_editwin *e) {
192  if (e->destroy_cbdata)
193    e->destroy_cbdata(e->cbdata);
194  e->cbdata = NULL;
195  e->destroy_cbdata = NULL;
196}
197
198void owl_editwin_set_cbdata(owl_editwin *e, void *data, void (*destroy)(void *))
199{
200  oe_destroy_cbdata(e);
201  e->cbdata = data;
202  e->destroy_cbdata = destroy;
203}
204
205void *owl_editwin_get_cbdata(owl_editwin *e) {
206  return e->cbdata;
207}
208
209void owl_editwin_do_callback(owl_editwin *e) {
210  void (*cb)(owl_editwin*);
211  cb=owl_editwin_get_callback(e);
212  if(!cb) {
213    owl_function_error("Internal error: No editwin callback!");
214  } else {
215    /* owl_function_error("text: |%s|", owl_editwin_get_text(e)); */
216    cb(e);
217  }
218}
219
220static int owl_editwin_limit_maxcols(int v, int maxv)
221{
222  /* maxv > 5 ? MAX(v, vax) : v */
223  if (maxv > 5 && v > maxv) {
224    return(maxv);
225  } else {
226    return(v);
227  }
228}
229
230/* set text to be 'locked in' at the beginning of the buffer, any
231 * previous text (including locked text) will be overwritten
232 */
233void owl_editwin_set_locktext(owl_editwin *e, const char *text)
234{
235  oe_set_index(e, 0);
236  e->lock = 0;
237  owl_editwin_replace(e, e->bufflen, text);
238  e->buff[e->bufflen] = 0;
239  e->lock=e->bufflen;
240  oe_set_index(e, e->lock);
241  owl_editwin_redisplay(e);
242}
243
244int owl_editwin_get_style(owl_editwin *e)
245{
246  return(e->style);
247}
248
249/* clear all text except for locktext and put the cursor at the
250 * beginning
251 */
252void owl_editwin_clear(owl_editwin *e)
253{
254
255  int lock = e->lock;
256  int dotsend=e->dotsend;
257  char *locktext=NULL;
258  char echochar=e->echochar;
259
260  if (lock > 0) {
261    locktext = owl_malloc(lock+1);
262    strncpy(locktext, e->buff, lock);
263    locktext[lock] = 0;
264  }
265
266  owl_free(e->buff);
267  _owl_editwin_init(e, e->curswin, e->winlines, e->wincols, e->style, e->hist);
268
269  if (lock > 0) {
270    owl_editwin_set_locktext(e, locktext);
271  }
272  if (dotsend) {
273    owl_editwin_set_dotsend(e);
274  }
275  if (echochar) {
276    owl_editwin_set_echochar(e, echochar);
277  }
278
279  if (locktext)
280    owl_free(locktext);
281
282  oe_set_index(e, lock);
283}
284
285void owl_editwin_recenter(owl_editwin *e)
286{
287  e->topindex = -1;
288}
289
290static void oe_save_excursion(owl_editwin *e, oe_excursion *x)
291{
292  x->index = e->index;
293  x->mark = e->mark;
294  x->goal_column = e->goal_column;
295  x->lock = e->lock;
296
297  x->valid = VALID_EXCURSION;
298  x->next = e->excursions;
299  e->excursions = x;
300}
301
302static void oe_release_excursion(owl_editwin *e, oe_excursion *x)
303{
304  oe_excursion **px;
305
306  x->valid = 0;
307  for (px = &e->excursions; *px != NULL; px = &(*px)->next)
308    if (*px == x) {
309      *px = x->next;
310      return;
311    }
312  abort();
313}
314
315static void oe_restore_excursion(owl_editwin *e, oe_excursion *x)
316{
317  if (x->valid == VALID_EXCURSION) {
318    oe_set_index(e, x->index);
319    e->goal_column = x->goal_column;
320    e->mark = x->mark;
321    e->lock = x->lock;
322
323    oe_release_excursion(e, x);
324  }
325}
326
327static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x)
328{
329  if (x->valid == VALID_EXCURSION) {
330    e->mark = x->mark;
331
332    oe_release_excursion(e, x);
333  }
334}
335
336/* External interface to oe_save_excursion */
337owl_editwin_excursion *owl_editwin_begin_excursion(owl_editwin *e)
338{
339  owl_editwin_excursion *x = owl_malloc(sizeof *x);
340  oe_save_excursion(e, x);
341  return x;
342}
343
344void owl_editwin_end_excursion(owl_editwin *e, owl_editwin_excursion *x)
345{
346  oe_restore_excursion(e, x);
347  owl_free(x);
348}
349
350static inline const char *oe_next_point(owl_editwin *e, const char *p)
351{
352  const char *boundary = e->buff + e->bufflen + 1;
353  const char *q;
354
355  q = g_utf8_find_next_char(p, boundary);
356  while (q && g_unichar_ismark(g_utf8_get_char(q)))
357    q = g_utf8_find_next_char(q, boundary);
358
359  if (q == p)
360    return NULL;
361  return q;
362}
363
364static inline const char *oe_prev_point(owl_editwin *e, const char *p)
365{
366  const char *boundary = e->buff + e->lock;
367
368  p = g_utf8_find_prev_char(boundary, p);
369  while (p && g_unichar_ismark(g_utf8_get_char(p)))
370    p = g_utf8_find_prev_char(boundary, p);
371
372  return p;
373}
374
375static int oe_char_width(gunichar c, int column)
376{
377  int cw;
378
379  if (c == 9) /* TAB */
380    return TABSIZE - column % TABSIZE;
381
382  cw = mk_wcwidth(c);
383
384  if (cw < 0) /* control characters */
385    cw = 0;
386
387  return cw;
388}
389
390static int oe_find_display_line(owl_editwin *e, int *x, int index)
391{
392  int width = 0, cw;
393  gunichar c;
394  const char *p;
395
396  while(1) {
397    /* note the position of the dot */
398    if (x != NULL && index == e->index && width < e->wincols)
399      *x = width;
400
401    /* get the current character */
402    c = g_utf8_get_char(e->buff + index);
403
404    /* figure out how wide it is */
405    cw = oe_char_width(c, width);
406
407    if (width + cw > e->wincols) {
408      if (x != NULL && *x == width)
409        *x = -1;
410      break;
411    }
412    width += cw;
413
414    if (c == '\n') {
415      if (width < e->wincols)
416        ++index; /* skip the newline */
417      break;
418    }
419
420    /* find the next character */
421    p = oe_next_point(e, e->buff + index);
422    if (p == NULL) { /* we ran off the end */
423      if (x != NULL && e->index > index)
424        *x = width + 1;
425      break;
426    }
427    index = p - e->buff;
428
429  }
430  return index;
431}
432
433static void oe_reframe(owl_editwin *e) {
434  oe_excursion x;
435  int goal = 1 + e->winlines / 2;
436  int index;
437  int count = 0;
438  int n, i;
439  int last;
440
441  oe_save_excursion(e, &x);
442  /* step back line-by-line through the buffer until we have >= goal lines of
443     display text */
444  e->lock = 0; /* we can (must) tread on the locktext */
445
446  last = -1;
447  while (count < goal) {
448    index = e->index;
449    owl_editwin_move_to_beginning_of_line(e);
450    if (last == e->index)
451      break;
452    last = e->index;
453    for (n = 0, i = e->index; i < index; n++)
454      i = oe_find_display_line(e, NULL, i);
455    count += n == 0 ? 1 : n;
456    if (count < goal)
457      owl_editwin_point_move(e, -1);
458  }
459
460  e->topindex = e->index;
461  /* if we overshot, backtrack */
462  for (n = 0; n < (count - goal); n++)
463    e->topindex = oe_find_display_line(e, NULL, e->topindex);
464
465  oe_restore_excursion(e, &x);
466}
467
468static void oe_addnec(owl_editwin *e, int count)
469{
470  int i;
471
472  for (i = 0; i < count; i++)
473    waddch(e->curswin, e->echochar);
474}
475
476static void oe_mvaddnec(owl_editwin *e, int y, int x, int count)
477{
478  wmove(e->curswin, y, x);
479  oe_addnec(e, count);
480}
481
482/* regenerate the text on the curses window */
483void owl_editwin_redisplay(owl_editwin *e)
484{
485  int x = -1, y = -1, t;
486  int line, index, lineindex, times = 0;
487
488  do {
489    werase(e->curswin);
490
491    if (e->topindex == -1 || e->index < e->topindex)
492      oe_reframe(e);
493
494    line = 0;
495    index = e->topindex;
496    while(line < e->winlines) {
497      lineindex = index;
498      t = -1;
499      index = oe_find_display_line(e, &t, lineindex);
500      if (x == -1 && t != -1)
501        x = t, y = line;
502      if (index - lineindex) {
503        if (!e->echochar)
504          mvwaddnstr(e->curswin, line, 0,
505                     e->buff + lineindex,
506                     index - lineindex);
507        else {
508          if(lineindex < e->lock) {
509            mvwaddnstr(e->curswin, line, 0,
510                       e->buff + lineindex,
511                       MIN(index - lineindex,
512                           e->lock - lineindex));
513            if (e->lock < index)
514              oe_addnec(e,
515                        oe_region_width(e, e->lock, index,
516                                        oe_region_width(e, lineindex, e->lock, 0)));
517          } else
518            oe_mvaddnec(e, line, 0, oe_region_width(e, line, index, 0));
519        }
520      }
521      line++;
522    }
523    if (x == -1)
524        e->topindex = -1; /* force a reframe */
525    times++;
526  } while(x == -1 && times < 3);
527
528  wmove(e->curswin, y, x);
529  e->cursorx = x;
530}
531
532static inline void oe_fixup(int *target, int start, int end, int change) {
533  if (*target > start) {
534    if (*target <= end)
535      *target = end + change;
536    else
537      *target += change;
538  }
539}
540
541int owl_editwin_replace_region(owl_editwin *e, const char *s)
542{
543  oe_excursion x;
544  int ret;
545  oe_save_excursion(e, &x);
546
547  if(e->index > e->mark) {
548    owl_editwin_exchange_point_and_mark(e);
549  }
550
551  ret = owl_editwin_replace_internal(e, e->mark - e->index, s);
552
553  oe_restore_excursion(e, &x);
554
555  return ret;
556}
557
558/* replace 'replace' characters at the point with s, returning the change in size */
559int owl_editwin_replace(owl_editwin *e, int replace, const char *s)
560{
561  int start, end, i;
562  const char *p;
563
564  if (!g_utf8_validate(s, -1, NULL)) {
565    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
566    return 0;
567  }
568
569  start = e->index;
570  for (i = 0, p = e->buff + start; i < replace && p != NULL; i++)
571    p = oe_next_point(e, p);
572  if (p != NULL)
573    end = p - e->buff;
574  else
575    end = e->bufflen;
576
577  return owl_editwin_replace_internal(e, end - start, s);
578}
579
580static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s)
581{
582  int start, end, free, need, size, change;
583  oe_excursion *x;
584
585  start = e->index;
586  end   = start + replace;
587
588  free = e->allocated - e->bufflen + end - start;
589
590  need = strlen(s) - free;
591  if (need > 0) {
592    size = e->allocated + need + INCR - (need % INCR);
593    e->buff = owl_realloc(e->buff, size);
594    e->allocated = size;
595  }
596
597  memmove(e->buff + start + strlen(s), e->buff + end, e->bufflen + 1 - end);
598  memcpy(e->buff + start, s, strlen(s));
599  change = start - end + strlen(s);
600  e->bufflen += change;
601  e->index += strlen(s);
602
603  /* fix up the mark */
604  oe_fixup(&e->mark, start, end, change);
605  oe_fixup(&e->topindex, start, end, change);
606  /* fix up any saved points after the replaced area */
607  for (x = e->excursions; x != NULL; x = x->next) {
608    oe_fixup(&x->index, start, end, change);
609    oe_fixup(&x->mark, start, end, change);
610  }
611
612  /* recenter if needed */
613  if (start <= e->topindex)
614    owl_editwin_recenter(e);
615
616  return change;
617}
618
619/* linewrap the word just before the cursor.
620 * returns 0 on success
621 * returns -1 if we could not wrap.
622 */
623static void _owl_editwin_linewrap_word(owl_editwin *e)
624{
625  oe_excursion x;
626  gunichar c;
627
628  oe_save_excursion(e, &x);
629
630  while (owl_editwin_point_move(e, -1)) {
631    c = owl_editwin_get_char_at_point(e);
632    if (owl_util_can_break_after(c) || c == '\n') {
633      if (c != '\n')
634        owl_editwin_replace(e, c != ' ' ? 0 : 1, "\n");
635      break;
636    }
637  }
638
639  oe_restore_excursion(e, &x);
640}
641
642/* delete the character at the current point, following chars
643 * shift left.
644 */
645void owl_editwin_delete_char(owl_editwin *e)
646{
647  owl_editwin_replace(e, 1, "");
648}
649
650/* Swap the character at point with the character at point-1 and
651 * advance the pointer.  If point is at beginning of buffer do
652 * nothing.  If point is after the last character swap point-1 with
653 * point-2.  (Behaves as observed in tcsh and emacs).
654 */
655void owl_editwin_transpose_chars(owl_editwin *e)
656{
657  const char *middle, *end, *start;
658  char *tmp;
659
660  if (e->bufflen == 0) return;
661
662  if (e->index == e->bufflen)
663    owl_editwin_point_move(e, -1);     /* point is after last character */
664
665  if (owl_editwin_at_beginning_of_buffer(e))
666    return;     /* point is at beginning of buffer, do nothing */
667
668  /* Transpose two utf-8 unicode glyphs. */
669  middle = e->buff + e->index;
670
671  end = oe_next_point(e, middle);
672  if (end == NULL)
673    return;
674
675  start = oe_prev_point(e, middle);
676  if (start == NULL)
677    return;
678
679  tmp = owl_malloc((end - start) + 1);
680  tmp[(end - start)] = 0;
681  memcpy(tmp, middle, end - middle);
682  memcpy(tmp + (end - middle), start, middle - start);
683
684  owl_editwin_point_move(e, -1);
685  owl_editwin_replace(e, 2, tmp);
686}
687
688/* insert 'string' at the current point, later text is shifted
689 * right
690 */
691void owl_editwin_insert_string(owl_editwin *e, const char *s)
692{
693  owl_editwin_replace(e, 0, s);
694}
695
696/* We assume index is not set to point to a mid-char */
697static gunichar owl_editwin_get_char_at_point(owl_editwin *e)
698{
699  return g_utf8_get_char(e->buff + e->index);
700}
701
702void owl_editwin_exchange_point_and_mark(owl_editwin *e) {
703  int tmp;
704
705  if (e->mark != -1) {
706    tmp = e->mark;
707    owl_editwin_set_mark(e);
708    oe_set_index(e, tmp);
709  }
710}
711
712int owl_editwin_point_move(owl_editwin *e, int delta)
713{
714  const char *p;
715  int change, d = 0;
716
717  change = MAX(delta, - delta);
718  p = e->buff + e->index;
719
720  while (d < change && p != NULL) {
721    if (delta > 0)
722      p = oe_next_point(e, p);
723    else
724      p = oe_prev_point(e, p);
725    if (p != NULL) {
726      oe_set_index(e, p - e->buff);
727      d++;
728    }
729  }
730
731  return delta > 0 ? d : -d;
732}
733
734int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
735  if (e->index == e->lock)
736    return 1;
737
738  return 0;
739}
740
741int owl_at_end_of_buffer(owl_editwin *e) {
742  if (e->index == e->bufflen)
743    return 1;
744
745  return 0;
746}
747
748static int owl_editwin_at_beginning_of_line(owl_editwin *e)
749{
750  oe_excursion x;
751  int ret;
752
753  if (owl_editwin_at_beginning_of_buffer(e))
754    return 1;
755
756  oe_save_excursion(e, &x);
757  owl_editwin_point_move(e, -1);
758  ret = (owl_editwin_get_char_at_point(e) == '\n');
759  oe_restore_excursion(e, &x);
760
761  return ret;
762}
763
764static int owl_editwin_is_char_in(owl_editwin *e, const char *set)
765{
766  const char *p;
767
768  for (p = set; *p != 0; p = g_utf8_find_next_char(p, NULL))
769    if (owl_editwin_get_char_at_point(e) == g_utf8_get_char(p))
770      return 1;
771  return 0;
772}
773
774int owl_editwin_move_if_in(owl_editwin *e, int delta, const char *set)
775{
776  int change, distance = 0;
777  while (owl_editwin_is_char_in(e, set)) {
778    change = owl_editwin_point_move(e, delta);
779    distance += change;
780    if (change == 0)
781      break;
782  }
783  return distance;
784}
785
786int owl_editwin_move_if_not_in(owl_editwin *e, int delta, const char *set)
787{
788  int change, distance = 0;
789  while (!owl_editwin_is_char_in(e, set)) {
790    change = owl_editwin_point_move(e, delta);
791    distance += change;
792    if (change == 0)
793      break;
794  }
795  return distance;
796}
797
798int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
799{
800  int distance = 0;
801
802  if (!owl_editwin_at_beginning_of_line(e)) {
803    /* move off the \n if were at the end of a line */
804    distance += owl_editwin_point_move(e, -1);
805    distance += owl_editwin_move_if_not_in(e, -1, "\n");
806    if (distance && !owl_editwin_at_beginning_of_buffer(e))
807      distance += owl_editwin_point_move(e, 1);
808  }
809  e->goal_column = 0; /* subtleties */
810
811  return distance;
812}
813
814int owl_editwin_move_to_end_of_line(owl_editwin *e)
815{
816  return owl_editwin_move_if_not_in(e, 1, "\n");
817}
818
819int owl_editwin_line_move(owl_editwin *e, int delta)
820{
821  int goal_column, change, ll, distance;
822  int count = 0;
823
824  change = MAX(delta, -delta);
825
826  goal_column = e->goal_column;
827  distance = owl_editwin_move_to_beginning_of_line(e);
828  goal_column = goal_column == -1 ? -distance : goal_column;
829
830  while(count < change) {
831    if (delta > 0) {
832      distance += owl_editwin_move_if_not_in(e, 1, "\n");
833      distance += owl_editwin_point_move(e, 1);
834    } else {
835      /* I really want to assert delta < 0 here */
836      distance += owl_editwin_point_move(e, -1); /* to the newline on
837                                                    the previous line */
838      distance += owl_editwin_move_to_beginning_of_line(e);
839    }
840    count++;
841  }
842
843  distance += (ll = owl_editwin_move_to_end_of_line(e));
844  if (ll > goal_column)
845    distance += owl_editwin_point_move(e, goal_column - ll);
846
847  e->goal_column = goal_column;
848
849  return distance;
850}
851
852void owl_editwin_backspace(owl_editwin *e)
853{
854  /* delete the char before the current one
855   * and shift later chars left
856   */
857  if(owl_editwin_point_move(e, -1))
858    owl_editwin_delete_char(e);
859}
860
861void owl_editwin_key_up(owl_editwin *e)
862{
863  owl_editwin_line_move(e, -1);
864}
865
866void owl_editwin_key_down(owl_editwin *e)
867{
868  owl_editwin_line_move(e, 1);
869}
870
871void owl_editwin_key_left(owl_editwin *e)
872{
873  owl_editwin_point_move(e, -1);
874}
875
876void owl_editwin_key_right(owl_editwin *e)
877{
878  owl_editwin_point_move(e, 1);
879}
880
881int owl_editwin_forward_word(owl_editwin *e)
882{
883  int distance;
884  /* if we're starting on a space, find the first non-space */
885  distance = owl_editwin_move_if_in(e, 1, WHITESPACE);
886
887  /* now find the end of this word */
888  distance += owl_editwin_move_if_not_in(e, 1, WHITESPACE);
889
890  return distance;
891}
892
893void owl_editwin_move_to_nextword(owl_editwin *e)
894{
895  owl_editwin_forward_word(e);
896}
897
898/* go backwards to the last non-space character
899 */
900int owl_editwin_backward_word(owl_editwin *e)
901{
902  oe_excursion x;
903  int distance = 0;
904  int further = 0;
905  int beginning;
906  /* if in middle of word, beginning of word */
907
908  /* if at beginning of a word, find beginning of previous word */
909
910  if (owl_editwin_is_char_in(e, WHITESPACE)) {
911    /* if in whitespace past end of word, find a word , the find the beginning*/
912    distance += owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
913                                                              character of the word */
914    oe_save_excursion(e, &x);
915    /* are we at the beginning of a word? */
916    owl_editwin_point_move(e, -1);
917    beginning = owl_editwin_is_char_in(e, WHITESPACE);
918    oe_restore_excursion(e, &x);
919    if (beginning)
920      return distance;
921   } else {
922    /* in the middle of the word; */
923    oe_save_excursion(e, &x);
924    further += owl_editwin_point_move(e, -1);
925    if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
926      distance += owl_editwin_backward_word(e); /* previous case */
927      oe_release_excursion(e, &x);
928      return distance + further;
929    } else {
930      oe_restore_excursion(e, &x);
931    }
932  }
933  distance += owl_editwin_move_if_not_in(e, -1, WHITESPACE);
934  /* will go past */
935  if (e->index > e->lock)
936    distance += owl_editwin_point_move(e, 1);
937  return distance;
938}
939
940void owl_editwin_move_to_previousword(owl_editwin *e)
941{
942  owl_editwin_backward_word(e);
943}
944
945void owl_editwin_delete_nextword(owl_editwin *e)
946{
947  oe_excursion x;
948
949  oe_save_excursion(e, &x);
950  oe_set_mark(e, e->index);
951  owl_editwin_forward_word(e);
952  owl_editwin_kill_region(e);
953  oe_restore_mark_only(e, &x);
954}
955
956void owl_editwin_delete_previousword(owl_editwin *e)
957{
958  oe_excursion x;
959
960  oe_save_excursion(e, &x);
961  oe_set_mark(e, e->index);
962  owl_editwin_backward_word(e);
963  owl_editwin_kill_region(e);
964  oe_restore_mark_only(e, &x);
965}
966
967void owl_editwin_move_to_line_end(owl_editwin *e)
968{
969  owl_editwin_move_to_end_of_line(e);
970}
971
972void owl_editwin_delete_to_endofline(owl_editwin *e)
973{
974  oe_excursion x;
975  int distance;
976
977  oe_save_excursion(e, &x);
978  owl_editwin_set_mark(e);
979  distance = owl_editwin_move_to_end_of_line(e);
980  if (distance)
981    owl_editwin_kill_region(e);
982  else
983    owl_editwin_replace(e, 1, "");
984  oe_restore_excursion(e, &x);
985}
986
987void owl_editwin_yank(owl_editwin *e)
988{
989  if (e->killbuf != NULL)
990    owl_editwin_replace(e, 0, e->killbuf);
991}
992
993static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len)
994{
995  char *p;
996
997  p = owl_malloc(len + 1);
998
999  if (p != NULL) {
1000    owl_free(e->killbuf);
1001    e->killbuf = p;
1002    memcpy(e->killbuf, buf, len);
1003    e->killbuf[len] = 0;
1004  }
1005
1006  return p;
1007}
1008
1009static int oe_copy_region(owl_editwin *e)
1010{
1011  const char *p;
1012  int start, end;
1013
1014  if (e->mark == -1)
1015    return 0;
1016
1017  start = MIN(e->index, e->mark);
1018  end = MAX(e->index, e->mark);
1019
1020  p = oe_copy_buf(e, e->buff + start, end - start);
1021  if (p != NULL)
1022    return end - start;
1023  return 0;
1024}
1025
1026void owl_editwin_copy_region_as_kill(owl_editwin *e)
1027{
1028  oe_copy_region(e);
1029}
1030
1031void owl_editwin_kill_region(owl_editwin *e)
1032{
1033  if (e->index > e->mark)
1034    owl_editwin_exchange_point_and_mark(e);
1035
1036  owl_editwin_replace_internal(e, oe_copy_region(e), "");
1037}
1038
1039void owl_editwin_move_to_line_start(owl_editwin *e)
1040{
1041  owl_editwin_move_to_beginning_of_line(e);
1042}
1043
1044void owl_editwin_move_to_end(owl_editwin *e)
1045{
1046  oe_set_index(e, e->bufflen);
1047}
1048
1049void owl_editwin_move_to_top(owl_editwin *e)
1050{
1051  oe_set_index(e, e->lock);
1052}
1053
1054void owl_editwin_backward_paragraph(owl_editwin *e)
1055{
1056  owl_editwin_point_move(e, -1);
1057  for (; e->index >= e->lock; owl_editwin_point_move(e, -1)) {
1058    if (e->index <= e->lock ||
1059        ((e->buff[e->index] == '\n') && (e->buff[e->index - 1]=='\n')))
1060      break;
1061  }
1062}
1063
1064void owl_editwin_forward_paragraph(owl_editwin *e)
1065{
1066  owl_editwin_point_move(e, 1);
1067  /* scan forward to the start of the next paragraph */
1068  for(; e->index < e->bufflen; owl_editwin_point_move(e, 1)) {
1069    if (e->buff[e->index -1] == '\n' && e->buff[e->index] == '\n')
1070      break;
1071  }
1072}
1073
1074int owl_editwin_current_column(owl_editwin *e)
1075{
1076  oe_excursion x;
1077  int lineindex;
1078
1079  oe_save_excursion(e, &x);
1080  owl_editwin_move_to_beginning_of_line(e);
1081  lineindex = e->index;
1082  oe_restore_excursion(e, &x);
1083  return oe_region_width(e, lineindex, e->index, 0);
1084}
1085
1086void owl_editwin_fill_paragraph(owl_editwin *e)
1087{
1088  oe_excursion x;
1089  gunichar ch;
1090  int sentence;
1091
1092  oe_save_excursion(e, &x);
1093
1094  /* Mark the end of the paragraph */
1095  owl_editwin_forward_paragraph(e);
1096  /* Skip the trailing newline */
1097  owl_editwin_point_move(e, -1);
1098  owl_editwin_set_mark(e);
1099
1100  owl_editwin_backward_paragraph(e);
1101
1102  /* Don't mess with the leading newline */
1103  if (owl_editwin_get_char_at_point(e) == '\n')
1104    owl_editwin_point_move(e, 1);
1105
1106  /*
1107   * First pass: Scan forward replacing all series of spaces with ' '
1108   * (or nothing after CJK ideograms)
1109   */
1110  sentence = 0;
1111  for(;e->index < e->mark; owl_editwin_point_move(e, 1)) {
1112    /* bail if we hit a trailing dot on the buffer */
1113    if (strcmp(e->buff + e->index, "\n.") == 0) {
1114      owl_editwin_set_mark(e);
1115      break;
1116    }
1117
1118    ch = owl_editwin_get_char_at_point(e);
1119
1120    if (owl_util_can_break_after(ch) || ch == '\n') {
1121      if (g_unichar_isspace(ch)) {
1122        owl_editwin_replace(e, 1, " ");
1123      }
1124
1125      if (sentence && g_unichar_isspace(owl_editwin_get_char_at_point(e))
1126          && e->index < e->mark)
1127        owl_editwin_point_move(e, 1);
1128
1129      while(g_unichar_isspace(owl_editwin_get_char_at_point(e))
1130            && e->index < e->mark) {
1131        owl_editwin_delete_char(e);
1132      }
1133    }
1134
1135    if(ch == '.' || ch == '!' || ch == '?')
1136      sentence = 1;
1137    else
1138      sentence = 0;
1139  }
1140
1141  owl_editwin_backward_paragraph(e);
1142
1143  /* Now go through inserting newlines as needed */
1144  while(e->index < e->mark) {
1145    /* if we've travelled too far, linewrap */
1146    if (owl_editwin_current_column(e) >= e->fillcol)
1147      _owl_editwin_linewrap_word(e);
1148    owl_editwin_point_move(e, 1);
1149  }
1150
1151  oe_restore_excursion(e, &x);
1152}
1153
1154/* returns true if only whitespace remains */
1155int owl_editwin_is_at_end(owl_editwin *e)
1156{
1157  return (only_whitespace(e->buff + e->index));
1158}
1159
1160static int owl_editwin_check_dotsend(owl_editwin *e)
1161{
1162  int zdot = 0;
1163  oe_excursion x;
1164
1165  if (!e->dotsend) return(0);
1166  if (!owl_editwin_is_at_end(e)) return (0);
1167
1168  oe_save_excursion(e, &x);
1169
1170  owl_editwin_point_move(e, -3);
1171
1172  if(strncmp(e->buff + e->index, "\n.\n", 3) == 0) {
1173    owl_editwin_point_move(e, 1);
1174    zdot = 1;
1175  } else if(e->index == e->lock &&
1176            strncmp(e->buff + e->index, ".\n", 2) == 0) {
1177    zdot = 1;
1178  }
1179
1180  if(zdot) {
1181    owl_editwin_set_mark(e);
1182    owl_editwin_move_to_end(e);
1183    owl_editwin_replace_region(e, "");
1184  }
1185
1186  oe_restore_excursion(e, &x);
1187
1188  return zdot;
1189}
1190
1191void owl_editwin_post_process_char(owl_editwin *e, owl_input j)
1192{
1193  /* XXX force a redisplay? */
1194  if ((j.ch==13 || j.ch==10) && owl_editwin_check_dotsend(e)) {
1195    owl_command_edit_done(e);
1196    return;
1197  }
1198  owl_editwin_redisplay(e);
1199}
1200
1201static int oe_region_width(owl_editwin *e, int start, int end, int offset)
1202{
1203  const char *p;
1204  int width = offset;
1205 
1206  for(p = e->buff + start;
1207      p < e->buff + end;
1208      p = g_utf8_find_next_char(p, NULL))
1209    width += oe_char_width(g_utf8_get_char(p), width);
1210
1211  return width - offset;
1212}
1213
1214static void oe_insert_char(owl_editwin *e, gunichar c)
1215{
1216  oe_excursion x;
1217  char tmp[7];
1218  int replaced = -1;
1219
1220  if (c == '\r') /* translate CRs to NLs */
1221    c = '\n';
1222
1223  if (!g_unichar_iscntrl(c) || c == '\n' || c== '\t' ) {
1224    if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
1225      return;
1226    }
1227
1228    if (e->cursorx != -1 && e->cursorx + oe_char_width(c, e->cursorx) > e->wrapcol) {
1229      /* XXX this is actually wrong:
1230       * + If the line has been been wrapped, we can be past the wrap column but
1231       *   e->cursorx be much smaller.
1232       * + If the user went back and inserted a bunch of stuff in the middle of
1233       *   the line, there may be more than one word past the wrap column.
1234       */
1235      oe_save_excursion(e, &x);
1236
1237      if (c == ' ' || c == '\t') {
1238        owl_editwin_point_move(e, -1);
1239        replaced = -owl_editwin_move_if_in(e, -1, " \t");
1240        if (!replaced) {
1241          c = '\n';
1242          replaced = -1;
1243        }
1244      } else {
1245        while(!owl_editwin_at_beginning_of_line(e)) {
1246          owl_editwin_point_move(e, -1);
1247          if (owl_util_can_break_after(owl_editwin_get_char_at_point(e))) {
1248            replaced = -owl_editwin_move_if_in(e, -1, " \t");
1249            break;
1250          }
1251        }
1252        if (owl_editwin_at_beginning_of_line(e))
1253          replaced = -1;
1254      }
1255      if (replaced && !owl_editwin_at_beginning_of_line(e))
1256        owl_editwin_point_move(e, 1);
1257      if (replaced >= 0) {
1258        owl_editwin_replace(e, replaced, "\n");
1259      }
1260      oe_restore_excursion(e, &x);
1261    }
1262
1263    if (replaced >= 0 && (c == ' ' || c == '\t'))
1264      return; /* our work here is done */
1265
1266    tmp[g_unichar_to_utf8(c, tmp)] = '\0';
1267    owl_editwin_replace(e, 0, tmp);
1268  }
1269}
1270
1271void owl_editwin_process_char(owl_editwin *e, owl_input j)
1272{
1273  if (j.ch == ERR)
1274    return;
1275  /* Ignore ncurses control characters. */
1276  if (j.ch < 0x100) {
1277    oe_insert_char(e, j.uch);
1278  }
1279}
1280
1281const char *owl_editwin_get_text(owl_editwin *e)
1282{
1283  return(e->buff+e->lock);
1284}
1285
1286char *owl_editwin_get_region(owl_editwin *e)
1287{
1288  int start, end;
1289  start = e->index;
1290  end   = e->mark;
1291  if(start > end) {
1292    int tmp = end;
1293    end = start;
1294    start = tmp;
1295  }
1296
1297  return oe_chunk(e, start, end);
1298}
1299
1300int owl_editwin_get_echochar(owl_editwin *e)
1301{
1302  return e->echochar;
1303}
1304
1305static char *oe_chunk(owl_editwin *e, int start, int end)
1306{
1307  char *p;
1308 
1309  p = owl_malloc(end - start + 1);
1310  memcpy(p, e->buff + start, end - start);
1311  p[end - start] = 0;
1312
1313  return p;
1314}
1315
1316/*
1317 * The only guarantee made about these values is that comparisons
1318 * between them, as well as comparison between multiple calls to these
1319 * functions without modifying the editwin in-between, are meaningful.
1320 */
1321
1322int owl_editwin_get_point(owl_editwin *e)
1323{
1324  return e->index;
1325}
1326
1327int owl_editwin_get_mark(owl_editwin *e)
1328{
1329  return e->mark;
1330}
1331
1332
1333/*
1334 * Local Variables:
1335 * mode:C
1336 * c-basic-offset:2
1337 * End:
1338 */
Note: See TracBrowser for help on using the repository browser.