source: editwin.c @ 2d3ed95

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