source: editwin.c @ 80ed757

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